@curveball/core
Advanced tools
Comparing version 0.17.0 to 0.18.0-alpha.0
@@ -7,3 +7,3 @@ "use strict"; | ||
const http = require("http"); | ||
const base_context_1 = require("./base-context"); | ||
const context_1 = require("./context"); | ||
const memory_request_1 = require("./memory-request"); | ||
@@ -92,3 +92,3 @@ const memory_response_1 = require("./memory-response"); | ||
const response = new memory_response_1.default(); | ||
const context = new base_context_1.default(request, response); | ||
const context = new context_1.Context(request, response); | ||
context.webSocket = ws; | ||
@@ -140,3 +140,3 @@ await this.handle(context); | ||
const response = new memory_response_1.default(); | ||
const context = new base_context_1.default(request, response); | ||
const context = new context_1.Context(request, response); | ||
context.webSocket = ws; | ||
@@ -158,3 +158,3 @@ await this.handle(context); | ||
} | ||
const context = new base_context_1.default(request, new memory_response_1.default()); | ||
const context = new context_1.Context(request, new memory_response_1.default()); | ||
try { | ||
@@ -185,3 +185,3 @@ await this.handle(context); | ||
buildContextFromHttp(req, res) { | ||
const context = new base_context_1.default(new request_1.default(req), new response_1.default(res)); | ||
const context = new context_1.Context(new request_1.default(req), new response_1.default(res)); | ||
return context; | ||
@@ -188,0 +188,0 @@ } |
@@ -6,3 +6,8 @@ import { Middleware } from './application'; | ||
import * as WebSocket from 'ws'; | ||
export interface Context<ReqT = unknown, ResT = any> { | ||
/** | ||
* The Context object encapsulates a single HTTP request. | ||
* | ||
* It has references to the internal request and response object. | ||
*/ | ||
export declare class Context<ReqT = any, ResT = any> { | ||
/** | ||
@@ -28,2 +33,3 @@ * HTTP Request | ||
}; | ||
constructor(req: Request<ReqT>, res: Response<ResT>); | ||
/** | ||
@@ -34,3 +40,3 @@ * The Request path. | ||
*/ | ||
path: string; | ||
get path(): string; | ||
/** | ||
@@ -41,3 +47,3 @@ * HTTP method | ||
*/ | ||
method: string; | ||
get method(): string; | ||
/** | ||
@@ -48,3 +54,3 @@ * This object contains parsed query string parameters. | ||
*/ | ||
query: { | ||
get query(): { | ||
[s: string]: string; | ||
@@ -72,4 +78,10 @@ }; | ||
*/ | ||
status: number; | ||
get status(): number; | ||
/** | ||
* Sets the HTTP response status code. | ||
* | ||
* This is a shortcut for response.status. | ||
*/ | ||
set status(value: number); | ||
/** | ||
* Sends an informational (1xx status code) response. | ||
@@ -110,14 +122,13 @@ * | ||
/** | ||
* WebSocket Context | ||
* | ||
* This is the Context that will be passed in case a WebSocket request was | ||
* initiated. | ||
* WsContext always has a 'webSocket' property defined. | ||
*/ | ||
export interface WsContext extends Context<unknown, any> { | ||
export declare type WsContext = Context & { | ||
/** | ||
* WebSocket object. | ||
* | ||
* If the current request is a websocket request, this proprerty will be set | ||
* | ||
* @see https://github.com/websockets/ws#simple-server | ||
*/ | ||
webSocket: WebSocket; | ||
} | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Context = void 0; | ||
/** | ||
* The Context object encapsulates a single HTTP request. | ||
* | ||
* It has references to the internal request and response object. | ||
*/ | ||
class Context { | ||
constructor(req, res) { | ||
this.request = req; | ||
this.response = res; | ||
this.state = {}; | ||
} | ||
/** | ||
* The Request path. | ||
* | ||
* Shortcut for request.path | ||
*/ | ||
get path() { | ||
return this.request.path; | ||
} | ||
/** | ||
* HTTP method | ||
* | ||
* Shortcut for request.method | ||
*/ | ||
get method() { | ||
return this.request.method; | ||
} | ||
/** | ||
* This object contains parsed query string parameters. | ||
* | ||
* This is a shortcut for request.query | ||
*/ | ||
get query() { | ||
return this.request.query; | ||
} | ||
/** | ||
* accepts is used for negotation the Content-Type with a client. | ||
* | ||
* You can pass a content-type, or an array of content-types. | ||
* The Content-Types you provide are a list of types your application | ||
* supports. | ||
* | ||
* This function will then return the best possible type based on the Accept | ||
* header. | ||
* | ||
* If no compatible types are found, this function returns null. | ||
* | ||
* This is a shortcut to request.accepts() | ||
*/ | ||
accepts(...types) { | ||
return this.request.accepts(...types); | ||
} | ||
/** | ||
* HTTP status code. | ||
* | ||
* This is a shortcut for response.status | ||
*/ | ||
get status() { | ||
return this.response.status; | ||
} | ||
/** | ||
* Sets the HTTP response status code. | ||
* | ||
* This is a shortcut for response.status. | ||
*/ | ||
set status(value) { | ||
this.response.status = value; | ||
} | ||
/** | ||
* Sends an informational (1xx status code) response. | ||
* | ||
* This is a shortcut for response.sendInformational() | ||
*/ | ||
sendInformational(status, headers) { | ||
return this.response.sendInformational(status, headers); | ||
} | ||
/** | ||
* Sends a HTTP/2 push. | ||
* | ||
* The passed middleware will be called with a new Context object specific | ||
* for pushes. | ||
* | ||
* This is a shortcut for response.push() | ||
*/ | ||
push(callback) { | ||
return this.response.push(callback); | ||
} | ||
/** | ||
* returns the ip address of the client that's trying to connect. | ||
* | ||
* If 'trustProxy' is set to true, it means the server is running behind a | ||
* proxy, and the X-Forwarded-For header should be parsed instead. | ||
* | ||
* If there was no real HTTP client, this method will return null. | ||
*/ | ||
ip(trustProxy = false) { | ||
if (this.request.ip !== undefined) { | ||
return this.request.ip(trustProxy); | ||
} | ||
return null; | ||
} | ||
/** | ||
* redirect redirects the response with an optionally provided HTTP status | ||
* code in the first position to the location provided in address. If no status | ||
* is provided, 303 See Other is used. | ||
* | ||
* It is a wrapper method for the underlying Response.redirect function. | ||
* | ||
* @param {(string|number)} addrOrStatus if passed a string, the string will | ||
* be used to set the Location header of the response object and the default status | ||
* of 303 See Other will be used. If a number, an addressed must be passed in the second | ||
* argument. | ||
* @param {string} address If addrOrStatus is passed a status code, this value is | ||
* set as the value of the response's Location header. | ||
*/ | ||
redirect(addrOrStatus, address = '') { | ||
if (typeof (addrOrStatus) === 'number') { | ||
return this.response.redirect(addrOrStatus, address); | ||
} | ||
else { | ||
return this.response.redirect(addrOrStatus); | ||
} | ||
} | ||
} | ||
exports.Context = Context; | ||
//# sourceMappingURL=context.js.map |
import { default as Application, invokeMiddlewares, Middleware, middlewareCall } from './application'; | ||
import BaseContext from './base-context'; | ||
export { Context, WsContext } from './context'; | ||
export { Context, Context as BaseContext, WsContext } from './context'; | ||
import Headers from './headers'; | ||
@@ -11,2 +10,2 @@ import MemoryRequest from './memory-request'; | ||
export default Application; | ||
export { Application, BaseContext, conditionalCheck, Headers, invokeMiddlewares, middlewareCall, Middleware, Request, Response, MemoryRequest, MemoryResponse, }; | ||
export { Application, conditionalCheck, Headers, invokeMiddlewares, middlewareCall, Middleware, Request, Response, MemoryRequest, MemoryResponse, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MemoryResponse = exports.MemoryRequest = exports.Response = exports.Request = exports.middlewareCall = exports.invokeMiddlewares = exports.Headers = exports.conditionalCheck = exports.BaseContext = exports.Application = void 0; | ||
exports.MemoryResponse = exports.MemoryRequest = exports.Response = exports.Request = exports.middlewareCall = exports.invokeMiddlewares = exports.Headers = exports.conditionalCheck = exports.Application = exports.BaseContext = exports.Context = void 0; | ||
const application_1 = require("./application"); | ||
@@ -8,4 +8,6 @@ Object.defineProperty(exports, "Application", { enumerable: true, get: function () { return application_1.default; } }); | ||
Object.defineProperty(exports, "middlewareCall", { enumerable: true, get: function () { return application_1.middlewareCall; } }); | ||
const base_context_1 = require("./base-context"); | ||
exports.BaseContext = base_context_1.default; | ||
var context_1 = require("./context"); | ||
Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } }); | ||
// For backwards compatibility | ||
Object.defineProperty(exports, "BaseContext", { enumerable: true, get: function () { return context_1.Context; } }); | ||
const headers_1 = require("./headers"); | ||
@@ -12,0 +14,0 @@ exports.Headers = headers_1.default; |
@@ -7,3 +7,3 @@ "use strict"; | ||
const application_1 = require("../application"); | ||
const base_context_1 = require("../base-context"); | ||
const context_1 = require("../context"); | ||
const memory_request_1 = require("../memory-request"); | ||
@@ -120,3 +120,3 @@ const memory_response_1 = require("../memory-response"); | ||
} | ||
const pushCtx = new base_context_1.default(new memory_request_1.default('GET', '|||DELIBERATELY_INVALID|||'), new memory_response_1.default()); | ||
const pushCtx = new context_1.Context(new memory_request_1.default('GET', '|||DELIBERATELY_INVALID|||'), new memory_response_1.default()); | ||
await (0, application_1.invokeMiddlewares)(pushCtx, [callback]); | ||
@@ -123,0 +123,0 @@ if (pushCtx.request.requestTarget === '|||DELIBERATELY_INVALID|||') { |
{ | ||
"name": "@curveball/core", | ||
"version": "0.17.0", | ||
"version": "0.18.0-alpha.0", | ||
"description": "Curveball is a framework writting in Typescript for Node.js", | ||
@@ -8,3 +8,4 @@ "main": "dist/index.js", | ||
"prepublishOnly": "make build", | ||
"test": "make lint test", | ||
"test": "make test", | ||
"lint": "make lint", | ||
"tsc": "tsc" | ||
@@ -63,6 +64,6 @@ }, | ||
"@curveball/http-errors": "^0.4.0", | ||
"@types/ws": "^7.4.7", | ||
"@types/ws": "^8.5.3", | ||
"accepts": "^1.3.7", | ||
"raw-body": "^2.4.1", | ||
"ws": "^7.5.7" | ||
"ws": "^8.5.0" | ||
}, | ||
@@ -69,0 +70,0 @@ "engines": { |
import { isHttpError } from '@curveball/http-errors'; | ||
import { EventEmitter } from 'events'; | ||
import * as http from 'http'; | ||
import BaseContext from './base-context'; | ||
import { Context } from './context'; | ||
@@ -128,3 +127,3 @@ import { HeadersInterface, HeadersObject } from './headers'; | ||
const response = new MemoryResponse(); | ||
const context = new BaseContext(request, response); | ||
const context = new Context(request, response); | ||
@@ -187,3 +186,3 @@ context.webSocket = ws; | ||
const response = new MemoryResponse(); | ||
const context = new BaseContext(request, response); | ||
const context = new Context(request, response); | ||
@@ -224,3 +223,3 @@ context.webSocket = ws; | ||
const context = new BaseContext(request, new MemoryResponse()); | ||
const context = new Context(request, new MemoryResponse()); | ||
@@ -254,3 +253,3 @@ try { | ||
): Context { | ||
const context = new BaseContext(new NodeRequest(req), new NodeResponse(res)); | ||
const context = new Context(new NodeRequest(req), new NodeResponse(res)); | ||
@@ -257,0 +256,0 @@ return context; |
@@ -7,3 +7,8 @@ import { Middleware } from './application'; | ||
export interface Context<ReqT = unknown, ResT = any> { | ||
/** | ||
* The Context object encapsulates a single HTTP request. | ||
* | ||
* It has references to the internal request and response object. | ||
*/ | ||
export class Context<ReqT = any, ResT = any> { | ||
@@ -33,2 +38,10 @@ /** | ||
constructor(req: Request<ReqT>, res: Response<ResT>) { | ||
this.request = req; | ||
this.response = res; | ||
this.state = {}; | ||
} | ||
/** | ||
@@ -39,4 +52,8 @@ * The Request path. | ||
*/ | ||
path: string; | ||
get path(): string { | ||
return this.request.path; | ||
} | ||
/** | ||
@@ -47,4 +64,8 @@ * HTTP method | ||
*/ | ||
method: string; | ||
get method(): string { | ||
return this.request.method; | ||
} | ||
/** | ||
@@ -55,4 +76,8 @@ * This object contains parsed query string parameters. | ||
*/ | ||
query: { [s: string]: string }; | ||
get query(): { [s: string]: string } { | ||
return this.request.query; | ||
} | ||
/** | ||
@@ -72,4 +97,8 @@ * accepts is used for negotation the Content-Type with a client. | ||
*/ | ||
accepts(...types: string[]): null | string; | ||
accepts(...types: string[]): null | string { | ||
return this.request.accepts(...types); | ||
} | ||
/** | ||
@@ -80,5 +109,20 @@ * HTTP status code. | ||
*/ | ||
status: number; | ||
get status(): number { | ||
return this.response.status; | ||
} | ||
/** | ||
* Sets the HTTP response status code. | ||
* | ||
* This is a shortcut for response.status. | ||
*/ | ||
set status(value: number) { | ||
this.response.status = value; | ||
} | ||
/** | ||
* Sends an informational (1xx status code) response. | ||
@@ -88,4 +132,8 @@ * | ||
*/ | ||
sendInformational(status: number, headers?: HeadersInterface | HeadersObject): Promise<void>; | ||
sendInformational(status: number, headers?: HeadersInterface | HeadersObject): Promise<void> { | ||
return this.response.sendInformational(status, headers); | ||
} | ||
/** | ||
@@ -99,4 +147,8 @@ * Sends a HTTP/2 push. | ||
*/ | ||
push(callback: Middleware): Promise<void>; | ||
push(callback: Middleware): Promise<void> { | ||
return this.response.push(callback); | ||
} | ||
/** | ||
@@ -110,6 +162,34 @@ * returns the ip address of the client that's trying to connect. | ||
*/ | ||
ip(trustProxy?: boolean): null | string; | ||
ip(trustProxy = false): null | string { | ||
if ((this.request as any).ip !== undefined) { | ||
return (this.request as any).ip(trustProxy); | ||
} | ||
return null; | ||
} | ||
redirect(address: string): void; | ||
redirect(status: number, address: string): void; | ||
/** | ||
* redirect redirects the response with an optionally provided HTTP status | ||
* code in the first position to the location provided in address. If no status | ||
* is provided, 303 See Other is used. | ||
* | ||
* It is a wrapper method for the underlying Response.redirect function. | ||
* | ||
* @param {(string|number)} addrOrStatus if passed a string, the string will | ||
* be used to set the Location header of the response object and the default status | ||
* of 303 See Other will be used. If a number, an addressed must be passed in the second | ||
* argument. | ||
* @param {string} address If addrOrStatus is passed a status code, this value is | ||
* set as the value of the response's Location header. | ||
*/ | ||
redirect(addrOrStatus: string|number, address = ''): void { | ||
if (typeof(addrOrStatus) === 'number') { | ||
return this.response.redirect(addrOrStatus, address); | ||
} else { | ||
return this.response.redirect(addrOrStatus); | ||
} | ||
} | ||
@@ -124,11 +204,9 @@ /** | ||
webSocket?: WebSocket; | ||
} | ||
/** | ||
* WebSocket Context | ||
* | ||
* This is the Context that will be passed in case a WebSocket request was | ||
* initiated. | ||
* WsContext always has a 'webSocket' property defined. | ||
*/ | ||
export interface WsContext extends Context<unknown, any> { | ||
export type WsContext = Context & { | ||
@@ -138,6 +216,7 @@ /** | ||
* | ||
* If the current request is a websocket request, this proprerty will be set | ||
* | ||
* @see https://github.com/websockets/ws#simple-server | ||
*/ | ||
webSocket: WebSocket; | ||
} | ||
}; |
import { default as Application, invokeMiddlewares, Middleware, middlewareCall } from './application'; | ||
import BaseContext from './base-context'; | ||
export { Context, WsContext } from './context'; | ||
export { | ||
Context, | ||
// For backwards compatibility | ||
Context as BaseContext, | ||
WsContext | ||
} from './context'; | ||
import Headers from './headers'; | ||
@@ -14,3 +18,2 @@ import MemoryRequest from './memory-request'; | ||
Application, | ||
BaseContext, | ||
conditionalCheck, | ||
@@ -17,0 +20,0 @@ Headers, |
import * as http from 'http'; | ||
import { promisify } from 'util'; | ||
import { invokeMiddlewares, Middleware } from '../application'; | ||
import BaseContext from '../base-context'; | ||
import { Context } from '../context'; | ||
import { HeadersInterface, HeadersObject } from '../headers'; | ||
@@ -151,3 +151,3 @@ import MemoryRequest from '../memory-request'; | ||
const pushCtx = new BaseContext( | ||
const pushCtx = new Context( | ||
new MemoryRequest('GET', '|||DELIBERATELY_INVALID|||'), | ||
@@ -154,0 +154,0 @@ new MemoryResponse() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
77
4087
175002
+ Added@types/ws@8.5.13(transitive)
+ Addedws@8.18.0(transitive)
- Removed@types/ws@7.4.7(transitive)
- Removedws@7.5.10(transitive)
Updated@types/ws@^8.5.3
Updatedws@^8.5.0