From 7de95ee5d5611fcdb299f41d5af96dd1fe8dc843 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Tue, 25 Sep 2018 21:45:35 +0200 Subject: [PATCH] hapi(17.6): update docs, also update joi ecosys --- types/hapi/index.d.ts | 86 ++++++++++++-------- types/hapi/test/request/methods.ts | 4 + types/hapi/test/route/route-options.ts | 18 +++- types/hapi/test/server/server-control.ts | 3 + types/hapi/test/server/server-decorations.ts | 2 + types/hapi/test/server/server-load.ts | 5 +- types/hapi/tsconfig.json | 12 +-- types/joi/index.d.ts | 20 +++-- types/joi/joi-tests.ts | 6 ++ types/joi/tslint.json | 3 - 10 files changed, 107 insertions(+), 52 deletions(-) create mode 100644 types/hapi/test/request/methods.ts create mode 100644 types/hapi/test/server/server-control.ts diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index b152d34e5b..fe6ba44f69 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for hapi 17.0 +// Type definitions for hapi 17.6 // Project: https://github.com/hapijs/hapi // Definitions by: Rafael Souza Fijalkowski // Justin Simms @@ -503,6 +503,14 @@ export interface Request extends Podium { */ readonly url: url.Url; + /** + * Returns `true` when the request is active and processing should continue and `false` when the + * request terminated early or completed its lifecycle. Useful when request processing is a + * resource-intensive operation and should be terminated early if the request is no longer active + * (e.g. client disconnected or aborted early). + */ + active(): boolean; + /** * Returns a response which you can pass into the reply interface where: * @param source - the value to set as the source of the reply interface, optional. @@ -1457,6 +1465,12 @@ export interface RouteOptionsResponse { status?: Util.Dictionary; } +/** + * @see https://www.w3.org/TR/referrer-policy/ + */ +export type RefererPolicy = '' | 'no-referrer' | 'no-referrer-when-downgrade' | 'unsafe-url' | + 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin'; + /** * Default value: false (security headers disabled). * Sets common security headers. To enable, set security to true or to an object with the following options: @@ -1531,6 +1545,12 @@ export interface RouteOptionsSecureObject { * boolean controlling the 'X-Content-Type-Options' header. Defaults to true setting the header to its only and default option, 'nosniff'. */ noSniff?: boolean; + + /** + * Controls the `Referrer-Policy` header, which has the following possible values. + * @default false Header will not be send. + */ + referer?: false | RefererPolicy; } export type RouteOptionsSecure = boolean | RouteOptionsSecureObject; @@ -1842,30 +1862,6 @@ export interface RouteOptions { /** * Default value: false (security headers disabled). - * Sets common security headers. To enable, set security to true or to an object with the following options: - * * hsts - controls the 'Strict-Transport-Security' header, where: - * * * true - the header will be set to max-age=15768000. This is the default value. - * * * a number - the maxAge parameter will be set to the provided value. - * * * an object with the following fields: - * * * * maxAge - the max-age portion of the header, as a number. Default is 15768000. - * * * * includeSubDomains - a boolean specifying whether to add the includeSubDomains flag to the header. - * * * * preload - a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) to the header. - * * xframe - controls the 'X-Frame-Options' header, where: - * * * true - the header will be set to 'DENY'. This is the default value. - * * * 'deny' - the headers will be set to 'DENY'. - * * * 'sameorigin' - the headers will be set to 'SAMEORIGIN'. - * * * an object for specifying the 'allow-from' rule, where: - * * * * rule - one of: - * * * * * 'deny' - * * * * * 'sameorigin' - * * * * * 'allow-from' - * * * * source - when rule is 'allow-from' this is used to form the rest of the header, otherwise this field is ignored. If rule is 'allow-from' but source is unset, the rule will be - * automatically changed to 'sameorigin'. - * * xss - boolean that controls the 'X-XSS-PROTECTION' header for Internet Explorer. Defaults to true which sets the header to equal '1; mode=block'. - * Note: this setting can create a security vulnerability in versions of Internet Exploere below 8, as well as unpatched versions of IE8. See here and here for more information. If you - * actively support old versions of IE, it may be wise to explicitly set this flag to false. - * * noOpen - boolean controlling the 'X-Download-Options' header for Internet Explorer, preventing downloads from executing in your context. Defaults to true setting the header to 'noopen'. - * * noSniff - boolean controlling the 'X-Content-Type-Options' header. Defaults to true setting the header to its only and default option, 'nosniff'. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionssecurity) */ security?: RouteOptionsSecure; @@ -2821,6 +2817,12 @@ export interface ServerOptions { load?: { /** the frequency of sampling in milliseconds. When set to 0, the other load options are ignored. Defaults to 0 (no sampling). */ sampleInterval?: number; + + /** + * Max concurrent requests. + */ + concurrent?: number; + /** maximum V8 heap size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ maxHeapUsedBytes?: number; /** @@ -3177,7 +3179,9 @@ export interface ServerState { * An object containing the configuration of each cookie added via [server.state()](https://github.com/hapijs/hapi/blob/master/API.md#server.state()) where each key is the * cookie name and value is the configuration object. */ - readonly cookies: object; + readonly cookies: { + [key: string]: ServerStateCookieOptions; + }; /** * An array containing the names of all configued cookies. @@ -3232,6 +3236,8 @@ export type DecorationMethod = (this: T, ...args: any[]) => any; export interface PluginProperties { } +export type DecorateName = string | symbol; + /** * The server object is the main application container. The server manages all incoming requests along with all * the facilities provided by the framework. Each server supports a single connection (e.g. listen to port 80). @@ -3258,6 +3264,13 @@ export class Server extends Podium { */ auth: ServerAuth; + /** + * Links another server to the initialize/start/stop state of the current server by calling the + * controlled server `initialize()`/`start()`/`stop()` methods whenever the current server methods + * are called, where: + */ + control(server: Server): void; + /** * Provides access to the decorations already applied to various framework interfaces. The object must not be * modified directly, but only through server.decorate. @@ -3350,6 +3363,11 @@ export class Server extends Podium { * event loop delay milliseconds. */ eventLoopDelay: number; + + /** + * Max concurrent requests. + */ + concurrent: number /** * V8 heap usage. */ @@ -3473,14 +3491,14 @@ export class Server extends Podium { * @return void; * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoratetype-property-method-options) */ - decorate(type: 'handler', property: string, method: HandlerDecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; - decorate(type: 'request', property: string, method: (existing: ((...args: any[]) => any)) => (request: Request) => DecorationMethod, options: {apply: true, extend: true}): void; - decorate(type: 'request', property: string, method: (request: Request) => DecorationMethod, options: {apply: true, extend?: boolean}): void; - decorate(type: 'request', property: string, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; - decorate(type: 'toolkit', property: string, method: (existing: ((...args: any[]) => any)) => DecorationMethod, options: {apply?: boolean, extend: true}): void; - decorate(type: 'toolkit', property: string, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; - decorate(type: 'server', property: string, method: (existing: ((...args: any[]) => any)) => DecorationMethod, options: {apply?: boolean, extend: true}): void; - decorate(type: 'server', property: string, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; + decorate(type: 'handler', property: DecorateName, method: HandlerDecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; + decorate(type: 'request', property: DecorateName, method: (existing: ((...args: any[]) => any)) => (request: Request) => DecorationMethod, options: {apply: true, extend: true}): void; + decorate(type: 'request', property: DecorateName, method: (request: Request) => DecorationMethod, options: {apply: true, extend?: boolean}): void; + decorate(type: 'request', property: DecorateName, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; + decorate(type: 'toolkit', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod, options: {apply?: boolean, extend: true}): void; + decorate(type: 'toolkit', property: DecorateName, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; + decorate(type: 'server', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod, options: {apply?: boolean, extend: true}): void; + decorate(type: 'server', property: DecorateName, method: DecorationMethod, options?: {apply?: boolean, extend?: boolean}): void; /** * Used within a plugin to declare a required dependency on other plugins where: diff --git a/types/hapi/test/request/methods.ts b/types/hapi/test/request/methods.ts new file mode 100644 index 0000000000..cfe7db2383 --- /dev/null +++ b/types/hapi/test/request/methods.ts @@ -0,0 +1,4 @@ +import { Request } from 'hapi'; + +const req: Request = {}; +const act: boolean = req.active(); diff --git a/types/hapi/test/route/route-options.ts b/types/hapi/test/route/route-options.ts index d114154e60..7151002627 100644 --- a/types/hapi/test/route/route-options.ts +++ b/types/hapi/test/route/route-options.ts @@ -9,7 +9,8 @@ import { RouteOptionsPayload, RouteOptionsResponse, RouteOptionsValidate, - Server + Server, + RouteOptionsSecureObject } from "hapi"; const routeOptionsAccess: RouteOptionsAccess = { @@ -87,7 +88,20 @@ const routeOptionsResponse: RouteOptionsResponse = { 200: true, 302: true, 404: false, - } + }, +}; + +const routeOptionSecure: RouteOptionsSecureObject = { + referer: 'origin', + noSniff: true, + xframe: "deny", + hsts: { + includeSubdomains: true, + maxAge: 1111, + preload: false, + }, + noOpen: false, + xss: true, }; const routeOptionsValidate: RouteOptionsValidate = { diff --git a/types/hapi/test/server/server-control.ts b/types/hapi/test/server/server-control.ts new file mode 100644 index 0000000000..8b145f1388 --- /dev/null +++ b/types/hapi/test/server/server-control.ts @@ -0,0 +1,3 @@ +import { Server } from "hapi"; + +new Server().control(new Server()); diff --git a/types/hapi/test/server/server-decorations.ts b/types/hapi/test/server/server-decorations.ts index c0ab94cc0e..7c060735a9 100644 --- a/types/hapi/test/server/server-decorations.ts +++ b/types/hapi/test/server/server-decorations.ts @@ -58,6 +58,8 @@ function decorateToolkitWithParams(this: ResponseToolkit, x: number, y: string) server.decorate('toolkit', 'withParams', decorateToolkitWithParams); +server.decorate('toolkit', Symbol('hi'), decorateToolkitWithParams); + server.route({ method: 'GET', path: '/toolkitWithParams', diff --git a/types/hapi/test/server/server-load.ts b/types/hapi/test/server/server-load.ts index d2ae11848a..8ac89fa006 100644 --- a/types/hapi/test/server/server-load.ts +++ b/types/hapi/test/server/server-load.ts @@ -3,7 +3,10 @@ import { Server } from "hapi"; const server = new Server({ port: 8000, - load: { sampleInterval: 1000 } + load: { + sampleInterval: 1000, + concurrent: 123, + } }); server.start(); diff --git a/types/hapi/tsconfig.json b/types/hapi/tsconfig.json index c93f0dfc7f..b197e3f861 100644 --- a/types/hapi/tsconfig.json +++ b/types/hapi/tsconfig.json @@ -21,32 +21,34 @@ "test/request/catch-all.ts", "test/request/event-types.ts", "test/request/get-log.ts", + "test/request/methods.ts", "test/request/parameters.ts", "test/request/query.ts", "test/response/continue.ts", "test/response/error.ts", "test/response/redirect.ts", - "test/response/response.ts", "test/response/response-events.ts", + "test/response/response.ts", "test/route/adding-routes.ts", "test/route/config.ts", - "test/route/handler.ts", "test/route/ext.ts", - "test/route/route-options.ts", + "test/route/handler.ts", "test/route/route-options-pre.ts", + "test/route/route-options.ts", "test/route/validation.ts", "test/server/server-app.ts", "test/server/server-auth-api.ts", "test/server/server-auth-default.ts", "test/server/server-auth-test.ts", "test/server/server-bind.ts", - "test/server/server-cache.ts", "test/server/server-cache-provision.ts", + "test/server/server-cache.ts", + "test/server/server-control.ts", "test/server/server-decoder.ts", "test/server/server-decorations.ts", "test/server/server-encoder.ts", - "test/server/server-events.ts", "test/server/server-events-once.ts", + "test/server/server-events.ts", "test/server/server-expose.ts", "test/server/server-info.ts", "test/server/server-inject.ts", diff --git a/types/joi/index.d.ts b/types/joi/index.d.ts index 166be7d526..2dd5c7f9e7 100644 --- a/types/joi/index.d.ts +++ b/types/joi/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for joi 13.4 +// Type definitions for joi 13.6 // Project: https://github.com/hapijs/joi // Definitions by: Bart van der Schoor // Laurence Dougal Myers @@ -197,6 +197,7 @@ export interface ReferenceOptions { functions?: boolean; } +// tslint:disable-next-line:interface-name export interface IPOptions { version?: string[]; cidr?: string; @@ -225,9 +226,7 @@ export interface ValidationErrorItem { context?: Context; } -export interface ValidationErrorFunction { - (errors: ValidationErrorItem[]): string | ValidationErrorItem | ValidationErrorItem[] | Error; -} +export type ValidationErrorFunction = (errors: ValidationErrorItem[]) => string | ValidationErrorItem | ValidationErrorItem[] | Error; export interface ValidationResult extends Pick, 'then' | 'catch'> { error: ValidationError; @@ -685,6 +684,11 @@ export interface StringSchema extends AnySchema { trim(): this; } +export interface SymbolSchema extends AnySchema { + // TODO: support number and symbol index + map(iterable: Iterable<[string | number | boolean | symbol, symbol]> | { [key: string]: symbol }): this; +} + export interface ArraySchema extends AnySchema { /** * Allow this array to be sparse. @@ -1078,6 +1082,11 @@ export function object(schema?: SchemaMap): ObjectSchema; */ export function string(): StringSchema; +/** + * Generates a schema object that matches any symbol. + */ +export function symbol(): SymbolSchema; + /** * Generates a type that will match one of the provided alternative schemas */ @@ -1110,7 +1119,6 @@ export function validate(value: T, schema: SchemaLike, options: Validation * Converts literal schema definition to joi schema object (or returns the same back if already a joi schema object). */ export function compile(schema: SchemaLike): Schema; -export function compile(schema: SchemaLike): T; /** * Validates a value against a schema and throws if validation fails. @@ -1145,9 +1153,7 @@ export function isRef(ref: any): ref is Reference; * of strings For string values path separator is a dot (`.`) */ export function reach(schema: ObjectSchema, path: string): Schema; -export function reach(schema: ObjectSchema, path: string): T; export function reach(schema: ObjectSchema, path: string[]): Schema; -export function reach(schema: ObjectSchema, path: string[]): T; /** * Creates a new Joi instance customized with the extension(s) you provide included. diff --git a/types/joi/joi-tests.ts b/types/joi/joi-tests.ts index c9805e1fc3..832858c7e9 100644 --- a/types/joi/joi-tests.ts +++ b/types/joi/joi-tests.ts @@ -1142,3 +1142,9 @@ schema = Joi.raw(bool); schema = Joi.empty(); schema = Joi.empty(str); schema = Joi.empty(anySchema); + +schema = Joi.symbol(); +schema = Joi.symbol().map(new Map()); +schema = Joi.symbol().map({ + key: Symbol('asd'), +}); diff --git a/types/joi/tslint.json b/types/joi/tslint.json index 87b70df769..c6e5f080fb 100644 --- a/types/joi/tslint.json +++ b/types/joi/tslint.json @@ -3,11 +3,8 @@ "rules": { // All are TODOs "ban-types": false, - "callable-types": false, - "interface-name": false, "no-empty-interface": false, "no-self-import": false, - "no-unnecessary-generics": false, "unified-signatures": false } }