Comparing version 0.2.4 to 0.3.0
109
index.js
'use strict'; | ||
var debug = require('debug')('bigpipe:pagelet') | ||
, stringify = require('json-stringify-safe') | ||
, jstringify = require('json-stringify-safe') | ||
, FreeList = require('freelist').FreeList | ||
@@ -15,3 +15,3 @@ , dot = require('dot-component') | ||
// | ||
var temper = new Temper; | ||
var temper = new Temper(); | ||
@@ -28,5 +28,5 @@ /** | ||
this.readable('temper', temper); // Template parser. | ||
this.writable('_authorized', null); // Are we authorized | ||
this.writable('substream', null); // Substream from Primus. | ||
this.readable('temper', temper); // Template parser. | ||
this.writable('id', null); // Custom ID of the pagelet. | ||
@@ -40,3 +40,3 @@ | ||
this.configure(); // Prepare the instance. | ||
this.configure(); | ||
} | ||
@@ -62,2 +62,5 @@ | ||
// | ||
// Clean up possible old references. | ||
// | ||
if (this.substream) this.substream.end(); | ||
@@ -70,2 +73,22 @@ this.substream = this._authorized = null; | ||
/** | ||
* A safe and fast `JSON.stringify`. | ||
* | ||
* @param {Mixed} data Data that needs to be transformed in to a string. | ||
* @param {Function} replacer Data replacer. | ||
* @returns {String} | ||
* @api public | ||
*/ | ||
Pagelet.readable('stringify', function stringify(data, replacer) { | ||
var result; | ||
try { result = JSON.stringify(data, replacer); } | ||
catch (e) { | ||
this.debug('Failed to normally stringify the data'); | ||
result = jstringify(data, replacer); | ||
} | ||
return result; | ||
}); | ||
/** | ||
* The name of this pagelet so it can checked to see if's enabled. In addition | ||
@@ -313,3 +336,8 @@ * to that, it can be injected in to placeholders using this name. | ||
function fragment(content) { | ||
data = stringify(data, function sanitize(key, data) { | ||
if (options.substream) { | ||
data.view = content; | ||
return fn.call(context, undefined, data); | ||
} | ||
data = pagelet.stringify(data, function sanitize(key, data) { | ||
if ('string' !== typeof data) return data; | ||
@@ -377,3 +405,4 @@ | ||
message: e.message, | ||
stack: e.stack | ||
stack: e.stack, | ||
error: e | ||
})); | ||
@@ -425,3 +454,2 @@ } | ||
case 'rpc': | ||
// pagelet.trigger(data.method, data.args, data.id); | ||
pagelet.call(data); | ||
@@ -434,3 +462,22 @@ break; | ||
// @TODO handle get/post/put | ||
case 'get': | ||
pagelet.render({ substream: true }, function renderd(err, fragment) { | ||
stream.write({ type: 'fragment', frag: fragment, err: err }); | ||
}); | ||
break; | ||
case 'post': | ||
case 'put': | ||
if (!(data.type in pagelet)) { | ||
return stream.write({ type: data.type, err: new Error('Method not supported by pagelet') }); | ||
} | ||
pagelet[data.type](data.body || {}, data.files || [], function processed(err, data) { | ||
if (err) return stream.write({ type: data.type, err: err }); | ||
pagelet.render({ data: data, substream: true }, function rendered(err, fragment) { | ||
stream.write({ type: 'fragment', frag: fragment, err: err }); | ||
}); | ||
}); | ||
break; | ||
} | ||
@@ -545,4 +592,4 @@ }); | ||
if (prototype.view) { | ||
Pagelet.prototype.view = path.resolve(dir, prototype.view); | ||
temper.prefetch(Pagelet.prototype.view, Pagelet.prototype.engine); | ||
prototype.view = path.resolve(dir, prototype.view); | ||
temper.prefetch(prototype.view, prototype.engine); | ||
} | ||
@@ -555,11 +602,11 @@ | ||
if (prototype.error) { | ||
Pagelet.prototype.error = path.resolve(dir, prototype.error); | ||
temper.prefetch(Pagelet.prototype.error, Pagelet.prototype.engine); | ||
prototype.error = path.resolve(dir, prototype.error); | ||
temper.prefetch(prototype.error, prototype.engine); | ||
} else { | ||
Pagelet.prototype.error = path.resolve(__dirname, 'error.ejs'); | ||
temper.prefetch(Pagelet.prototype.error, ''); | ||
prototype.error = path.resolve(__dirname, 'error.ejs'); | ||
temper.prefetch(prototype.error, ''); | ||
} | ||
if (prototype.css) Pagelet.prototype.css = path.resolve(dir, prototype.css); | ||
if (prototype.js) Pagelet.prototype.js = path.resolve(dir, prototype.js); | ||
if (prototype.css) prototype.css = path.resolve(dir, prototype.css); | ||
if (prototype.js) prototype.js = path.resolve(dir, prototype.js); | ||
@@ -571,3 +618,3 @@ // | ||
if (prototype.dependencies) { | ||
Pagelet.prototype.dependencies = prototype.dependencies.map(function each(dep) { | ||
prototype.dependencies = prototype.dependencies.map(function each(dep) { | ||
if (/^(http:|https:)?\/\//.test(dep)) return dep; | ||
@@ -584,7 +631,11 @@ return path.resolve(dir, dep); | ||
if (Array.isArray(prototype.rpc) && !prototype.RPC.length) { | ||
Pagelet.prototype.RPC = prototype.rpc; | ||
prototype.RPC = prototype.rpc; | ||
} | ||
if ('string' === typeof prototype.RPC) { | ||
prototype.RPC= prototype.RPC.split(/[\s|\,]/); | ||
} | ||
if ('function' === typeof prototype.initialise) { | ||
Pagelet.prototype.initialize = prototype.initialise; | ||
prototype.initialize = prototype.initialise; | ||
} | ||
@@ -605,12 +656,16 @@ | ||
// | ||
Pagelet.freelist = new FreeList('pagelet', Pagelet.prototype.freelist || 1000, function allocate() { | ||
var pagelet = new Pagelet(); | ||
Pagelet.freelist = new FreeList( | ||
'pagelet', | ||
prototype.freelist || 1000, | ||
function allocate() { | ||
var pagelet = new Pagelet(); | ||
pagelet.once('free', function free() { | ||
Pagelet.freelist.free(pagelet); | ||
pagelet = null; | ||
}); | ||
pagelet.once('free', function free() { | ||
Pagelet.freelist.free(pagelet); | ||
pagelet = null; | ||
}); | ||
return pagelet; | ||
}); | ||
return pagelet; | ||
} | ||
); | ||
@@ -617,0 +672,0 @@ return Pagelet; |
{ | ||
"name": "pagelet", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "pagelet", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,2 +39,3 @@ # Pagelet [![Build Status][status]](https://travis-ci.org/bigpipe/pagelet) [![NPM version][npmimgurl]](http://badge.fury.io/js/pagelet) [![Coverage Status][coverage]](http://coveralls.io/r/bigpipe/pagelet?branch=master) | ||
- [Pagelet: engine](#pagelet-engine) | ||
- [Pagelet: query](#pagelet-query) | ||
- [Pagelet: css](#pagelet-css) | ||
@@ -281,2 +282,25 @@ - [Pagelet: js](#pagelet-js) | ||
### Pagelet: query | ||
_optional:_ **writable, array** | ||
For optimal performance the data that is send to the client will be minimal | ||
and dependant on they query that is provided. Data can be supplied to the client | ||
by listing the keys (nested paths in dot notation) of which the data should be | ||
send to the client. In the example only the content of `mydata` and `nested.is` | ||
will be send. | ||
```js | ||
Pagelet.extend({ | ||
query: [ 'mydata', 'nested.is' ], | ||
get: function get(done) { | ||
done(null, { | ||
mydata: 'test', | ||
nested: { is: 'allowed', left: 'alone' }, | ||
more: 'data' | ||
}); | ||
} | ||
}).on(module); | ||
``` | ||
### Pagelet: css | ||
@@ -283,0 +307,0 @@ |
31843
610
392