From b33942b17b4117f2fa5b2eae6ad3baa55323768f Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Mon, 3 Apr 2017 18:02:25 +0100 Subject: [PATCH 1/4] Update definition for inert 4.2 and add more tests --- types/inert/index.d.ts | 94 ++++++++++++++++++++++++++++++++++---- types/inert/inert-tests.ts | 23 ++++++++++ 2 files changed, 107 insertions(+), 10 deletions(-) diff --git a/types/inert/index.d.ts b/types/inert/index.d.ts index 9acbd6f1a7..d65b27e196 100644 --- a/types/inert/index.d.ts +++ b/types/inert/index.d.ts @@ -1,15 +1,89 @@ -// Type definitions for inert 4.0 +// Type definitions for inert 4.2 // Project: https://github.com/hapijs/inert/ -// Definitions by: Steve Ognibene +// Definitions by: Steve Ognibene , AJP // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Note: this definition contains only enough to make TypeScript happy (hapi?) when -// importing inert. The functionality that importing inert adds to the reply -// object and its other features are implemented in the hapi definition on -// DefinitelyTyped. The inert object is never actually used in practice except to -// be passed as an extension to the server.register() method which already takes -// an `any` argument, so just using `any` here is fine. +import * as hapi from 'hapi'; -/// +declare module 'hapi' { + interface IFileHandler { + /** path - a path string or function as described above (required). */ + path: string | IRequestHandler; + /** confine - serve file relative to this directory and returns 403 Forbidden if the path resolves outside the confine directory. Defaults to true which uses the relativeTo route option as the confine. Set to false to disable this security feature. */ + confine?: boolean; + /** filename - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path */ + filename?: string; + /** + * mode - specifies whether to include the 'Content-Disposition' header with the response. Available values: + * * false - header is not included. This is the default value. + * * 'attachment' + * *'inline' + */ + mode?: false | 'attachment' | 'inline'; + /** lookupCompressed - if true, looks for for a pre-compressed version of the file with the same filename with an extension, depending on the accepted encoding. Defaults to false. */ + lookupCompressed?: boolean; + /** lookupMap - an object which maps content encoding to expected file name extension. Defaults to `{ gzip: '.gz' }. */ + lookupMap?: {[index: string]: string}; + /** + * etagMethod - specifies the method used to calculate the ETag header response. Available values: + * * 'hash' - SHA1 sum of the file contents, suitable for distributed deployments. Default value. + * * 'simple' - Hex encoded size and modification date, suitable when files are stored on a single server. + * * false - Disable ETag computation. + */ + etagMethod?: 'hash' | 'simple' | false; + /** start - offset in file to reading from, defaults to 0. */ + start?: number; + /** end - offset in file to stop reading from. If not set, will read to end of file. */ + end?: number; + } -export const inert: any; + interface IDirectoryHandler { + // TODO check if function returns error, what this type can be or is it raising an error which is caught? + /** path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: */ + path: string | string[] | IRequestHandler | IRequestHandler; + /** + * index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. + * * a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. + * * an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). + * * a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. + */ + index?: boolean | string | string[]; + /** listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. */ + listing?: boolean; + /** showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. */ + showHidden?: boolean; + /** redirectToSlash - optional boolean, determines if requests for a directory without a trailing slash are redirected to the same path with the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Disabled when the server config router.stripTrailingSlash is true.Defaults to false. */ + redirectToSlash?: boolean; + /** lookupCompressed - optional boolean, instructs the file processor to look for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. */ + lookupCompressed?: boolean; + /** + * etagMethod - specifies the method used to calculate the ETag header response. Available values: + * * 'hash' - SHA1 sum of the file contents, suitable for distributed deployments. Default value. + * * 'simple' - Hex encoded size and modification date, suitable when files are stored on a single server. + * * false - Disable ETag computation. + */ + etagMethod?: 'hash' | 'simple' | false; + /** defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension. */ + defaultExtension?: string; + } + + interface IRouteConfiguration { + /** + * The file handler + * + * Generates a static file endpoint for serving a single file. file can be set to: + * * a relative or absolute file path string (relative paths are resolved based on the route files configuration). + * * a function with the signature function(request) which returns the relative or absolute file path. + * * an object with one or more of the following options: + * @see {@link https://github.com/hapijs/inert#the-file-handler} + */ + file?: string | IRequestHandler | IFileHandler; + /** + * The directory handler + * + * Generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a path parameter at the end of the path string (e.g. /path/to/somewhere/{param} where the parameter name does not matter). The path parameter can use any of the parameter options (e.g. {param} for one level files only, {param?} for one level files or the directory root, {param*} for any level, or {param*3} for a specific level). If additional path parameters are present, they are ignored for the purpose of selecting the file system resource. The directory handler is an object with the following options: + * @see {@link https://github.com/hapijs/inert#the-directory-handler} + */ + directory?: IDirectoryHandler; + } +} diff --git a/types/inert/inert-tests.ts b/types/inert/inert-tests.ts index e917fb9e41..0741065d20 100644 --- a/types/inert/inert-tests.ts +++ b/types/inert/inert-tests.ts @@ -3,3 +3,26 @@ import * as InertES6 from 'inert'; const server = new HapiES6.Server({}); server.register(InertES6, () => {}); + +server.route({ + path: '', + method: 'GET', + file: { + path: '', + confine: true, + }, + directory: { + path: '', + listing: true + } +}) + +var fileHandler: HapiES6.IFileHandler = { + path: '', + confine: true, +} + +var directoryHandler: HapiES6.IDirectoryHandler = { + path: '', + listing: true, +} From 8cc99268058c93851f6523db4a3476ecc4992f60 Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Mon, 3 Apr 2017 20:16:47 +0100 Subject: [PATCH 2/4] Correct documentation of path option of directory handler --- types/inert/index.d.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/types/inert/index.d.ts b/types/inert/index.d.ts index d65b27e196..949b3610af 100644 --- a/types/inert/index.d.ts +++ b/types/inert/index.d.ts @@ -38,15 +38,14 @@ declare module 'hapi' { } interface IDirectoryHandler { - // TODO check if function returns error, what this type can be or is it raising an error which is caught? - /** path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: */ - path: string | string[] | IRequestHandler | IRequestHandler; - /** - * index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. + /** path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: * * a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. * * an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). * * a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. + * TODO check what type of Error the "function returns an error" can be or is it raising an error which is caught? */ + path: string | string[] | IRequestHandler | IRequestHandler; + /** index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. */ index?: boolean | string | string[]; /** listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. */ listing?: boolean; From 9ed0009123299b9236515e76e9aa3618a365f405 Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Tue, 4 Apr 2017 14:28:27 +0100 Subject: [PATCH 3/4] Correct union type for directory handler path function --- types/inert/index.d.ts | 3 +-- types/inert/inert-tests.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/types/inert/index.d.ts b/types/inert/index.d.ts index 949b3610af..cb9b8e8edb 100644 --- a/types/inert/index.d.ts +++ b/types/inert/index.d.ts @@ -42,9 +42,8 @@ declare module 'hapi' { * * a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. * * an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). * * a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. - * TODO check what type of Error the "function returns an error" can be or is it raising an error which is caught? */ - path: string | string[] | IRequestHandler | IRequestHandler; + path: string | string[] | IRequestHandler; /** index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. */ index?: boolean | string | string[]; /** listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. */ diff --git a/types/inert/inert-tests.ts b/types/inert/inert-tests.ts index 0741065d20..38830694cd 100644 --- a/types/inert/inert-tests.ts +++ b/types/inert/inert-tests.ts @@ -23,6 +23,14 @@ var fileHandler: HapiES6.IFileHandler = { } var directoryHandler: HapiES6.IDirectoryHandler = { - path: '', + path: function(){ + if(Math.random() > 0.5) { + return ''; + } + else if(Math.random() > 0) { + return ['']; + } + return new Error(''); + }, listing: true, } From e76b06fdbf7f215d8e3a551d0c3080c2b338c9b9 Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Tue, 4 Apr 2017 15:03:59 +0100 Subject: [PATCH 4/4] Temporarily remove tslint --- types/inert/tslint.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 types/inert/tslint.json diff --git a/types/inert/tslint.json b/types/inert/tslint.json deleted file mode 100644 index 377cc837d4..0000000000 --- a/types/inert/tslint.json +++ /dev/null @@ -1 +0,0 @@ -{ "extends": "../tslint.json" }