hapi(17.6): update docs, also update joi ecosys

This commit is contained in:
Simon Schick
2018-09-25 21:45:35 +02:00
parent adf4e18eef
commit 7de95ee5d5
10 changed files with 107 additions and 52 deletions

86
types/hapi/index.d.ts vendored
View File

@@ -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 <https://github.com/rafaelsouzaf>
// Justin Simms <https://github.com/jhsimms>
@@ -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<RouteOptionsResponseSchema>;
}
/**
* @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<T> = (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<Request>, options: {apply: true, extend: true}): void;
decorate(type: 'request', property: string, method: (request: Request) => DecorationMethod<Request>, options: {apply: true, extend?: boolean}): void;
decorate(type: 'request', property: string, method: DecorationMethod<Request>, options?: {apply?: boolean, extend?: boolean}): void;
decorate(type: 'toolkit', property: string, method: (existing: ((...args: any[]) => any)) => DecorationMethod<ResponseToolkit>, options: {apply?: boolean, extend: true}): void;
decorate(type: 'toolkit', property: string, method: DecorationMethod<ResponseToolkit>, options?: {apply?: boolean, extend?: boolean}): void;
decorate(type: 'server', property: string, method: (existing: ((...args: any[]) => any)) => DecorationMethod<Server>, options: {apply?: boolean, extend: true}): void;
decorate(type: 'server', property: string, method: DecorationMethod<Server>, 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<Request>, options: {apply: true, extend: true}): void;
decorate(type: 'request', property: DecorateName, method: (request: Request) => DecorationMethod<Request>, options: {apply: true, extend?: boolean}): void;
decorate(type: 'request', property: DecorateName, method: DecorationMethod<Request>, options?: {apply?: boolean, extend?: boolean}): void;
decorate(type: 'toolkit', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod<ResponseToolkit>, options: {apply?: boolean, extend: true}): void;
decorate(type: 'toolkit', property: DecorateName, method: DecorationMethod<ResponseToolkit>, options?: {apply?: boolean, extend?: boolean}): void;
decorate(type: 'server', property: DecorateName, method: (existing: ((...args: any[]) => any)) => DecorationMethod<Server>, options: {apply?: boolean, extend: true}): void;
decorate(type: 'server', property: DecorateName, method: DecorationMethod<Server>, options?: {apply?: boolean, extend?: boolean}): void;
/**
* Used within a plugin to declare a required dependency on other plugins where:

View File

@@ -0,0 +1,4 @@
import { Request } from 'hapi';
const req: Request = <any> {};
const act: boolean = req.active();

View File

@@ -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 = {

View File

@@ -0,0 +1,3 @@
import { Server } from "hapi";
new Server().control(new Server());

View File

@@ -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',

View File

@@ -3,7 +3,10 @@ import { Server } from "hapi";
const server = new Server({
port: 8000,
load: { sampleInterval: 1000 }
load: {
sampleInterval: 1000,
concurrent: 123,
}
});
server.start();

View File

@@ -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",

20
types/joi/index.d.ts vendored
View File

@@ -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 <https://github.com/Bartvds>
// Laurence Dougal Myers <https://github.com/laurence-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<T> extends Pick<Promise<T>, '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<T, R>(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<T extends Schema>(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<T extends Schema>(schema: ObjectSchema, path: string): T;
export function reach(schema: ObjectSchema, path: string[]): Schema;
export function reach<T extends Schema>(schema: ObjectSchema, path: string[]): T;
/**
* Creates a new Joi instance customized with the extension(s) you provide included.

View File

@@ -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<string, symbol>());
schema = Joi.symbol().map({
key: Symbol('asd'),
});

View File

@@ -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
}
}