Comparing version 0.1.2 to 0.1.3
/// <reference types="node" /> | ||
/// <reference types="cookie" /> | ||
import * as http from 'http'; | ||
import * as cookie from 'cookie'; | ||
/** | ||
* HTTP request decorator | ||
*/ | ||
export class Request { | ||
@@ -285,2 +286,5 @@ /** | ||
/** | ||
* HTTP response decorator | ||
*/ | ||
export class Response { | ||
@@ -468,3 +472,3 @@ /** | ||
*/ | ||
setCookie(name: string, value: string, options?: cookie.CookieSerializeOptions): this; | ||
setCookie(name: string, value: string, options?: SerializeOptions): this; | ||
/** | ||
@@ -477,3 +481,3 @@ * Unset the cookie `name`. | ||
*/ | ||
clearCookie(name: string, options?: cookie.CookieSerializeOptions): this; | ||
clearCookie(name: string, options?: SerializeOptions): this; | ||
/** | ||
@@ -523,5 +527,2 @@ * Append additional header name | ||
* Create a decorated version of the native HTTP server | ||
* | ||
* @param {RequestListener} fn | ||
* @returns {Server} | ||
*/ | ||
@@ -531,7 +532,76 @@ export function createServer(fn?: RequestListener): http.Server; | ||
/** | ||
* Request listener | ||
* | ||
* @param {Request} request | ||
* @param {Response} response | ||
* Request listener signature | ||
*/ | ||
export type RequestListener = (request: Request, response: Response) => void; | ||
/** | ||
* Response `setCookie` options | ||
*/ | ||
export interface SerializeOptions { | ||
/** | ||
* Specifies the value for the Domain Set-Cookie attribute. By default, no | ||
* domain is set, and most clients will consider the cookie to apply to only | ||
* the current domain. | ||
*/ | ||
domain?: string; | ||
/** | ||
* Specifies the `Date` object to be the value for the `Expires` | ||
* `Set-Cookie` attribute. By default, no expiration is set, and most | ||
* clients will consider this a "non-persistent cookie" and will delete it | ||
* on a condition like exiting a web browser application. | ||
* | ||
* *Note* the cookie storage model specification states that if both | ||
* `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is | ||
* possible not all clients by obey this, so if both are set, they should | ||
* point to the same date and time. | ||
*/ | ||
expires?: Date; | ||
/** | ||
* Specifies the boolean value for the `HttpOnly` `Set-Cookie` attribute. | ||
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By | ||
* default, the `HttpOnly` attribute is not set. | ||
* | ||
* *Note* be careful when setting this to true, as compliant clients will | ||
* not allow client-side JavaScript to see the cookie in `document.cookie`. | ||
*/ | ||
httpOnly?: boolean; | ||
/** | ||
* Specifies the number (in seconds) to be the value for the `Max-Age` | ||
* `Set-Cookie` attribute. The given number will be converted to an integer | ||
* by rounding down. By default, no maximum age is set. | ||
* | ||
* *Note* the cookie storage model specification states that if both | ||
* `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is | ||
* possible not all clients by obey this, so if both are set, they should | ||
* point to the same date and time. | ||
*/ | ||
maxAge?: number; | ||
/** | ||
* Specifies the value for the `Path` `Set-Cookie` attribute. By default, | ||
* the path is considered the "default path". | ||
*/ | ||
path?: string; | ||
/** | ||
* Specifies the boolean or string to be the value for the `SameSite` | ||
* `Set-Cookie` attribute. | ||
* | ||
* - `true` will set the `SameSite` attribute to `Strict` for strict same | ||
* site enforcement. | ||
* - `false` will not set the `SameSite` attribute. | ||
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site | ||
* enforcement. | ||
* - `'strict'` will set the `SameSite` attribute to Strict for strict same | ||
* site enforcement. | ||
*/ | ||
sameSite?: boolean | 'lax' | 'strict'; | ||
/** | ||
* Specifies the boolean value for the `Secure` `Set-Cookie` attribute. When | ||
* truthy, the `Secure` attribute is set, otherwise it is not. By default, | ||
* the `Secure` attribute is not set. | ||
* | ||
* *Note* be careful when setting this to `true`, as compliant clients will | ||
* not send the cookie back to the server in the future if the browser does | ||
* not have an HTTPS connection. | ||
*/ | ||
secure?: boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var request_1 = require("./request"); | ||
const server_1 = require("./server"); | ||
const request_1 = require("./request"); | ||
exports.Request = request_1.default; | ||
var response_1 = require("./response"); | ||
const response_1 = require("./response"); | ||
exports.Response = response_1.default; | ||
var server_1 = require("./server"); | ||
exports.createServer = server_1.default; | ||
/** | ||
* Create HTTP Server | ||
* | ||
* @param {Function} fn | ||
* @returns {Server} | ||
*/ | ||
function createServer(fn) { | ||
var server = new server_1.default(); | ||
fn && server.on('request', fn); | ||
return server; | ||
} | ||
exports.createServer = createServer; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http = require("http"); | ||
const request_1 = require("./request"); | ||
const response_1 = require("./response"); | ||
const timers_1 = require("timers"); | ||
const http_1 = require("http"); | ||
/** | ||
* Create a decorated version of the native HTTP server | ||
* | ||
* @param {RequestListener} fn | ||
* @returns {Server} | ||
*/ | ||
function createServer(fn) { | ||
var server = _decorate(new http_1.Server()); | ||
// attach request event listener | ||
fn && server.on('request', fn); | ||
return server; | ||
class Server extends http.Server { | ||
on(event, listener) { | ||
return this.addListener(event, listener); | ||
} | ||
addListener(event, listener) { | ||
return super.addListener(event, _wrap(event, listener)); | ||
} | ||
} | ||
exports.default = createServer; | ||
exports.default = Server; | ||
/** | ||
* Decorate native server instance | ||
* | ||
* @param {Server} server | ||
* @returns {Server} | ||
* @private | ||
*/ | ||
function _decorate(server) { | ||
var oldOn = server.on; | ||
server.on = function on(event, fn) { | ||
if (event === 'request') { | ||
fn = _wrap(fn); | ||
} | ||
oldOn.call(this, event, fn); | ||
return this; | ||
}; | ||
return server; | ||
} | ||
/** | ||
* Wrap the event listener | ||
* | ||
* @param {Listener} fn | ||
* @returns {Listener} | ||
* @param {Function} fn | ||
* @returns {Function} | ||
* @private | ||
*/ | ||
function _wrap(fn) { | ||
function _wrap(event, fn) { | ||
if (event !== 'request') | ||
return fn; | ||
return (req, res) => { | ||
@@ -47,0 +27,0 @@ timers_1.setImmediate(fn, new request_1.default(req), new response_1.default(res)); |
{ | ||
"name": "aldo-http", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Enhanced HTTP createServer module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48237
1650
14