Using Plugin.init() to add methods to Request
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Previously doing it on require() which is not ideal, we only really want to add these methods when we use() the plugin
authored by visionmedia · committed by visionmedia · parent 78d538a · Browse files · 3 files·+69 added−68 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+69 added−68 removed
- other:+69 added−68 removed
Provenance
- Commit
f21c1de- Tree
2278bc6- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
78d538a
3 files changed+69 added−68 removed
modified:lib/express/plugins/cookie.js+33 added-32 removed
37 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273424344454647484950515253545556575859606162636465666768697071727374757677787980819 unmodified lines37 unmodified lines return val === true ? key : key + '=' + val }).toArray().join('; ')}
// --- Request
Request.include({ /** * Get or set cookie values. * * Options: * * - path: Cookie path, defaults to '/' * - domain: Tail matched domain name such as 'vision-media.ca' or 'blog.vision-media.ca' etc * - expires: Date object converted to 'Wdy, DD-Mon-YYYY HH:MM:SS GMT' * when undefined the cookie will last the duration of a the * client's session. * - secure: When true the cookie will be sent by the client only when transfering data via HTTPS * - httpOnly When true the cookie will be sent to the server only and will not be accessable via * client-side scripting. * * @param {string} name * @param {string} val * @param {hash} options * @return {string} * @api public */
cookie: function(name, val, options) { return val ? this.response.cookies.push(exports.compileCookie(name, val, options)) : this.cookies[name] } })
// --- Cookie
exports.Cookie = Plugin.extend({ extend: { init: function() { Request.include({
/** * Get or set cookie values. * * Options: * * - path: Cookie path, defaults to '/' * - domain: Tail matched domain name such as 'vision-media.ca' or 'blog.vision-media.ca' etc * - expires: Date object converted to 'Wdy, DD-Mon-YYYY HH:MM:SS GMT' * when undefined the cookie will last the duration of a the * client's session. * - secure: When true the cookie will be sent by the client only when transfering data via HTTPS * - httpOnly When true the cookie will be sent to the server only and will not be accessable via * client-side scripting. * * @param {string} name * @param {string} val * @param {hash} options * @return {string} * @api public */
cookie: function(name, val, options) { return val ? this.response.cookies.push(exports.compileCookie(name, val, options)) : this.cookies[name] } }) } }, on: { request: function(event) { event.request.response.cookies = [] if (event.request.headers.cookie)9 unmodified linesmodified:lib/express/plugins/hooks.js+5 added-4 removed
24 unmodified lines252627282930313229303132333435363738394010 unmodified lines24 unmodified linesexports.after = function(fn) { after.push(fn)}
// --- Expose
process.mixin(GLOBAL, exports)
// --- Hooks
exports.Hooks = Plugin.extend({ extend: { init: function() { process.mixin(GLOBAL, exports) } }, on: { request: function(event) { $(before).each(function(fn){ fn.call(event.request)10 unmodified linesmodified:lib/express/plugins/redirect.js+31 added-32 removed
12344567867891011121314151617181920212223242526910111213141516171819202122232425262728282930313233343536372930313233343536373839403 unmodified lines
// Express - Redirect - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// --- Requestexports.Redirect = Plugin.extend({ extend: { init: function() { Request.include({
Request.include({ /** * Redirect to _uri_ with optional status _code_, * defaulting to 303 "See Other". * * When using redirect('home') the resolution * is as follows: * * - set('home') * - set('basepath') * - '/' * * When using redirect('back') the HTTP referrer * header is used. Commonly misspelled as 'referer' * which is supported as well. * * @param {string} uri * @settings 'home', 'basepath' * @api public */ /** * Redirect to _uri_ with optional status _code_, * defaulting to 303 "See Other". * * When using redirect('home') the resolution * is as follows: * * - set('home') * - set('basepath') * - '/' * * When using redirect('back') the HTTP referrer * header is used. Commonly misspelled as 'referer' * which is supported as well. * * @param {string} uri * @settings 'home', 'basepath' * @api public */
redirect: function(uri, code) { if (uri == 'back' || uri == 'home') uri = this[uri] this.header('location', uri) this.halt(code || 303) } })
// --- Redirect
exports.Redirect = Plugin.extend({ redirect: function(uri, code) { if (uri == 'back' || uri == 'home') uri = this[uri] this.header('location', uri) this.halt(code || 303) } }) } }, on: { request: function(event) { event.request.home = set('home') || set('basepath') || '/' event.request.back = event.request.header('referrer') || event.request.header('referer')3 unmodified lines