Started Plugin
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 7f1eeaf · Browse files · 5 files·+78 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+78 added−2 removed
- tests:+32 added−0 removed
- other:+46 added−2 removed
Provenance
- Commit
8158a3d- Tree
f1ab6bf- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
7f1eeaf
5 files changed+78 added−2 removed
modified:lib/express/spec/mock-routes.js+1 added
115 unmodified linesExpand all11611711811912012112212312424 unmodified lines115 unmodified linesExpand all */
reset = function() { Express.routes = [] Express.plugins = [] Express.settings = {}}
/**24 unmodified linesmodified:lib/express/core.js+5 added-2 removed
2 unmodified lines3 4 process.mixin(require('sys'))5 process.mixin(require('express/exceptions'))6 process.mixin(require('express/helpers'))7+process.mixin(require('express/plugin'))8 process.mixin(require('express/mime'))9 process.mixin(require('express/dsl'))10 11 // --- Helpers239 unmodified lines251 * @api private252 */253 254 respond: function(body){255+ this.response.body = body, trigger('response')254- if (typeof body != 'string') throw new InvalidResponseBody(this.request)256+ if (typeof this.response.body != 'string') throw new InvalidResponseBody(this.request)257 if (typeof this.response.status != 'number') throw new InvalidStatusCode(this.response.status)258 this.response.sendHeader(this.response.status, this.response.headers)257- this.response.sendBody(body)259+ this.response.sendBody(this.response.body)260 this.response.finish()261 }262 })263 2 unmodified lines266 Express = { 267 version: '0.0.1',268 config: [],269 routes: [],270+ plugins: [],271 settings: {},272 server: new Server,273 router: new Router274 }added:lib/express/plugin.js+41 added
1234567891011121314151617181920212223242526272829303132333435363738394041
// Express - Plugin - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// --- Event
var Event = exports.Event = Class({ init: function(name) { this.name = name this.request = Express.server.request this.response = Express.server.response }})
// --- Helpers
exports.trigger = function(name) { $(Express.plugins).each(function(plugin){ plugin.trigger(new Event(name)) })}
/** * Push _plugin_ to Express` plugin stack. * * @param {Plugin} plugin * @api public */
exports.use = function(plugin) { Express.plugins.push(new plugin)}
// --- Plugin
exports.Plugin = Class({ trigger: function(e) { if ('on' in this) if (e.name in this.on) this.on[e.name].call(this, e) }})No newline at end of filemodified:spec/spec.node.js+1 added
25 unmodified lines26 .exec('spec/spec.routing.js')27 .exec('spec/spec.helpers.js')28 .exec('spec/spec.mime.js')29 .exec('spec/spec.collection.js')30+ .exec('spec/spec.plugins.js')31 JSpec.run({ formatter: JSpec.formatters.Terminal, failuresOnly: true })32 JSpec.report()added:spec/spec.plugins.js+30 added
123456789101112131415161718192021222324252627282930
CSSVariables = Plugin.extend({ on: { response: function(e) { if (e.response.headers['content-type'] == mime('css')) e.response.body = e.response.body.replace('black', '#000') } }})
describe 'Express' describe 'use()' before_each reset() use(CSSVariables) end describe 'events' describe 'onResponse' it 'should be triggered before headers and body are sent' get('/style.css', function(){ contentType('css') return 'body { color: black; }' }) get('/style.css').body.should.eql 'body { color: #000; }' end end end endendNo newline at end of file