barehttp 0.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/context/execution.js +3 -10
- package/lib/context/index.d.ts +2 -3
- package/lib/context/index.js +6 -14
- package/lib/env.js +1 -4
- package/lib/index.d.ts +5 -5
- package/lib/index.js +3 -9
- package/lib/logger/index.d.ts +3 -5
- package/lib/logger/index.js +16 -43
- package/lib/logger/serializers.d.ts +2 -3
- package/lib/logger/serializers.js +12 -22
- package/lib/middlewares/cookies/cookie-manager.d.ts +7 -6
- package/lib/middlewares/cookies/cookie-manager.js +14 -18
- package/lib/middlewares/cookies/signer.js +4 -11
- package/lib/middlewares/cors/cors.d.ts +3 -3
- package/lib/middlewares/cors/cors.js +1 -4
- package/lib/request.d.ts +7 -8
- package/lib/request.js +22 -29
- package/lib/schemas/custom-schema.d.ts +7 -7
- package/lib/schemas/custom-schema.js +13 -20
- package/lib/schemas/dirty-tsm.js +18 -20
- package/lib/schemas/generator.d.ts +3 -3
- package/lib/schemas/generator.js +25 -32
- package/lib/schemas/helpers.d.ts +1 -1
- package/lib/schemas/helpers.js +26 -36
- package/lib/schemas/json-schema.d.ts +1 -1
- package/lib/schemas/json-schema.js +4 -8
- package/lib/schemas/openami-schema.d.ts +2 -2
- package/lib/schemas/openami-schema.js +4 -8
- package/lib/schemas/project.d.ts +1 -0
- package/lib/schemas/project.js +1 -1
- package/lib/server.d.ts +17 -18
- package/lib/server.js +35 -42
- package/lib/utils/content-type.js +2 -5
- package/lib/utils/http-methods.d.ts +2 -2
- package/lib/utils/http-methods.js +1 -4
- package/lib/utils/index.d.ts +4 -4
- package/lib/utils/index.js +4 -12
- package/lib/utils/safe-json.js +2 -7
- package/lib/utils/status-codes.d.ts +1 -1
- package/lib/utils/status-codes.js +1 -4
- package/lib/utils/status-phrases.js +1 -4
- package/lib/websocket.d.ts +6 -7
- package/lib/websocket.js +24 -27
- package/package.json +48 -37
package/lib/context/execution.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Execution = void 0;
|
|
7
|
-
const hyperid_1 = __importDefault(require("hyperid"));
|
|
8
|
-
const generateId = (0, hyperid_1.default)();
|
|
9
|
-
class Execution {
|
|
1
|
+
import hyperid from 'hyperid';
|
|
2
|
+
const generateId = hyperid();
|
|
3
|
+
export class Execution {
|
|
10
4
|
id;
|
|
11
5
|
type;
|
|
12
6
|
store;
|
|
@@ -18,4 +12,3 @@ class Execution {
|
|
|
18
12
|
this.headers = new Map();
|
|
19
13
|
}
|
|
20
14
|
}
|
|
21
|
-
exports.Execution = Execution;
|
package/lib/context/index.d.ts
CHANGED
package/lib/context/index.js
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.enableContext = exports.newContext = exports.context = void 0;
|
|
7
|
-
const execution_1 = require("./execution");
|
|
8
|
-
const async_hooks_1 = __importDefault(require("async_hooks"));
|
|
1
|
+
import { Execution } from './execution.js';
|
|
2
|
+
import asyncHooks from 'async_hooks';
|
|
9
3
|
const running = new Map();
|
|
10
4
|
const previous = new Map();
|
|
11
5
|
const newContext = (type) => {
|
|
12
6
|
if (!context.enabled)
|
|
13
7
|
return;
|
|
14
|
-
context.current = new
|
|
15
|
-
running.set(
|
|
8
|
+
context.current = new Execution(type);
|
|
9
|
+
running.set(asyncHooks.executionAsyncId(), context.current);
|
|
16
10
|
};
|
|
17
|
-
exports.newContext = newContext;
|
|
18
11
|
const context = {
|
|
19
12
|
enabled: false,
|
|
20
13
|
};
|
|
21
|
-
exports.context = context;
|
|
22
14
|
function init(asyncId, _type, triggerAsyncId) {
|
|
23
15
|
if (running.get(triggerAsyncId)) {
|
|
24
16
|
running.set(asyncId, running.get(triggerAsyncId));
|
|
@@ -41,7 +33,7 @@ function destroy(asyncId) {
|
|
|
41
33
|
previous.delete(asyncId);
|
|
42
34
|
}
|
|
43
35
|
}
|
|
44
|
-
const hook =
|
|
36
|
+
const hook = asyncHooks.createHook({
|
|
45
37
|
init,
|
|
46
38
|
before,
|
|
47
39
|
after,
|
|
@@ -51,4 +43,4 @@ const enableContext = () => {
|
|
|
51
43
|
context.enabled = true;
|
|
52
44
|
return hook.enable();
|
|
53
45
|
};
|
|
54
|
-
|
|
46
|
+
export { context, newContext, enableContext };
|
package/lib/env.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.envs = void 0;
|
|
4
|
-
exports.envs = {
|
|
1
|
+
export const envs = {
|
|
5
2
|
isProd: process.env.NODE_ENV === 'production',
|
|
6
3
|
isDev: process.env.NODE_ENV === 'development',
|
|
7
4
|
isTest: process.env.NODE_ENV === 'test',
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { BareHttp } from './server';
|
|
2
|
-
export type { BareHttpType } from './server';
|
|
3
|
-
export type { BareRequest } from './request';
|
|
4
|
-
export { context } from './context';
|
|
5
|
-
export { logMe } from './logger';
|
|
1
|
+
export { BareHttp } from './server.js';
|
|
2
|
+
export type { BareHttpType } from './server.js';
|
|
3
|
+
export type { BareRequest } from './request.js';
|
|
4
|
+
export { context } from './context/index.js';
|
|
5
|
+
export { logMe } from './logger/index.js';
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var server_1 = require("./server");
|
|
5
|
-
Object.defineProperty(exports, "BareHttp", { enumerable: true, get: function () { return server_1.BareHttp; } });
|
|
6
|
-
var context_1 = require("./context");
|
|
7
|
-
Object.defineProperty(exports, "context", { enumerable: true, get: function () { return context_1.context; } });
|
|
8
|
-
var logger_1 = require("./logger");
|
|
9
|
-
Object.defineProperty(exports, "logMe", { enumerable: true, get: function () { return logger_1.logMe; } });
|
|
1
|
+
export { BareHttp } from './server.js';
|
|
2
|
+
export { context } from './context/index.js';
|
|
3
|
+
export { logMe } from './logger/index.js';
|
package/lib/logger/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { serializeHttp } from './serializers.js';
|
|
2
2
|
interface LogMeFn {
|
|
3
3
|
(obj: unknown, ...args: []): void;
|
|
4
4
|
(msg: string, ...args: any[]): void;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
type LogMe = {
|
|
7
7
|
info: LogMeFn;
|
|
8
8
|
warn: LogMeFn;
|
|
9
9
|
error: LogMeFn;
|
|
@@ -11,8 +11,6 @@ declare type LogMe = {
|
|
|
11
11
|
debug: LogMeFn;
|
|
12
12
|
trace: LogMeFn;
|
|
13
13
|
};
|
|
14
|
-
export declare const logHttp: (
|
|
15
|
-
[k: string]: any;
|
|
16
|
-
}, startDate: Date, remoteClient: string, req: import("http").IncomingMessage, res: import("http").ServerResponse) => void;
|
|
14
|
+
export declare const logHttp: (...params: Parameters<typeof serializeHttp>) => void;
|
|
17
15
|
export declare const logMe: LogMe;
|
|
18
16
|
export {};
|
package/lib/logger/index.js
CHANGED
|
@@ -1,53 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.logMe = exports.logHttp = void 0;
|
|
27
|
-
const pino_1 = __importStar(require("pino"));
|
|
28
|
-
const serializers_1 = require("./serializers");
|
|
29
|
-
const env_1 = require("../env");
|
|
30
|
-
const asyncDest = env_1.envs.isProd ? [(0, pino_1.destination)({ sync: false })] : [];
|
|
1
|
+
import pino, { destination } from 'pino';
|
|
2
|
+
import { serializeLog, serializeHttp } from './serializers.js';
|
|
3
|
+
import { envs } from '../env.js';
|
|
4
|
+
const asyncDest = envs.isProd ? [destination({ sync: false })] : [];
|
|
31
5
|
const pinoCommonOptions = {
|
|
32
|
-
timestamp: () => `,"time":"${new Date()[
|
|
6
|
+
timestamp: () => `,"time":"${new Date()[envs.isProd ? 'toISOString' : 'toLocaleTimeString']()}"`,
|
|
33
7
|
formatters: {
|
|
34
8
|
level: (label) => ({ level: label }),
|
|
35
9
|
},
|
|
36
10
|
messageKey: 'message',
|
|
37
|
-
transport:
|
|
11
|
+
transport: envs.isProd ? undefined : { target: 'pino-pretty', options: { colorize: true } },
|
|
38
12
|
};
|
|
39
|
-
const logger = (
|
|
40
|
-
const logHttp = (...params) => {
|
|
41
|
-
const { level, logObject } =
|
|
13
|
+
const logger = pino(pinoCommonOptions, asyncDest[0]);
|
|
14
|
+
export const logHttp = (...params) => {
|
|
15
|
+
const { level, logObject } = serializeHttp(...params);
|
|
42
16
|
logger[level](logObject);
|
|
43
17
|
};
|
|
44
|
-
exports.logHttp = logHttp;
|
|
45
18
|
// TODO: remove the test condition
|
|
46
|
-
|
|
47
|
-
debug: (...args) => !
|
|
48
|
-
info: (...args) => !
|
|
49
|
-
warn: (...args) => !
|
|
50
|
-
error: (...args) => !
|
|
51
|
-
fatal: (...args) => !
|
|
52
|
-
trace: (...args) => !
|
|
19
|
+
export const logMe = {
|
|
20
|
+
debug: (...args) => !envs.isTest && logger.debug(serializeLog(...args)),
|
|
21
|
+
info: (...args) => !envs.isTest && logger.info(serializeLog(...args)),
|
|
22
|
+
warn: (...args) => !envs.isTest && logger.warn(serializeLog(...args)),
|
|
23
|
+
error: (...args) => !envs.isTest && logger.error(serializeLog(...args)),
|
|
24
|
+
fatal: (...args) => !envs.isTest && logger.fatal(serializeLog(...args)),
|
|
25
|
+
trace: (...args) => !envs.isTest && logger.trace(serializeLog(...args)),
|
|
53
26
|
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { IncomingMessage, ServerResponse } from 'http';
|
|
3
2
|
export declare function parseError(e: any, meta: any): any;
|
|
4
3
|
export declare function serializeLog(...args: any[]): any;
|
|
5
|
-
export declare function getStatusLevel(statusCode: number): "error" | "
|
|
4
|
+
export declare function getStatusLevel(statusCode: number): "error" | "info" | "warn";
|
|
6
5
|
export declare function serializeHttp(headers: {
|
|
7
6
|
[k: string]: any;
|
|
8
7
|
}, startDate: Date, remoteClient: string, req: IncomingMessage, res: ServerResponse): {
|
|
@@ -12,7 +11,7 @@ export declare function serializeHttp(headers: {
|
|
|
12
11
|
timestamp: number;
|
|
13
12
|
trace: string | number | undefined;
|
|
14
13
|
request: {
|
|
15
|
-
headers: import("http").IncomingHttpHeaders;
|
|
14
|
+
headers: import("node:http").IncomingHttpHeaders;
|
|
16
15
|
http_version: string;
|
|
17
16
|
id: any;
|
|
18
17
|
method: string | undefined;
|
|
@@ -1,44 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.serializeHttp = exports.getStatusLevel = exports.serializeLog = exports.parseError = void 0;
|
|
7
|
-
const callsites_1 = __importDefault(require("callsites"));
|
|
8
|
-
const context_1 = require("../context");
|
|
9
|
-
const util_1 = __importDefault(require("util"));
|
|
10
|
-
function parseError(e, meta) {
|
|
1
|
+
import callsites from 'callsites';
|
|
2
|
+
import { context } from '../context/index.js';
|
|
3
|
+
import util from 'util';
|
|
4
|
+
export function parseError(e, meta) {
|
|
11
5
|
const toSend = { error: { ...e }, ...meta };
|
|
12
6
|
toSend.message = e.message;
|
|
13
7
|
toSend.error.stack = e.stack;
|
|
14
8
|
toSend.error.kind = e.constructor.name;
|
|
15
9
|
return toSend;
|
|
16
10
|
}
|
|
17
|
-
exports.parseError = parseError;
|
|
18
11
|
const makeLoggerMetadata = (method) => ({
|
|
19
12
|
name: 'pino',
|
|
20
13
|
version: 'v1.0.0',
|
|
21
14
|
method_name: method,
|
|
22
15
|
});
|
|
23
16
|
const parseArgs = (argSlice) => argSlice.map((arg) => {
|
|
24
|
-
if (
|
|
17
|
+
if (util.types.isNativeError(arg))
|
|
25
18
|
return parseError(arg, {});
|
|
26
19
|
return arg;
|
|
27
20
|
});
|
|
28
|
-
function serializeLog(...args) {
|
|
29
|
-
const site = (
|
|
21
|
+
export function serializeLog(...args) {
|
|
22
|
+
const site = callsites()[2];
|
|
30
23
|
const meta = {
|
|
31
24
|
timestamp: Date.now(),
|
|
32
25
|
location: `${site.getFileName()}:${site.getLineNumber()}:${site.getColumnNumber()}`,
|
|
33
26
|
logger: makeLoggerMetadata(site.getFunctionName()),
|
|
34
|
-
trace:
|
|
27
|
+
trace: context.current?.store.get('id'),
|
|
35
28
|
};
|
|
36
29
|
if (!args.length)
|
|
37
30
|
return { message: 'EMPTY_LOG', ...meta };
|
|
38
31
|
if (args.length === 1) {
|
|
39
32
|
if (typeof args[0] === 'string')
|
|
40
33
|
return { message: args[0], ...meta };
|
|
41
|
-
if (
|
|
34
|
+
if (util.types.isNativeError(args[0]))
|
|
42
35
|
return parseError(args[0], meta);
|
|
43
36
|
if (args[0].message)
|
|
44
37
|
return { message: args[0].message, ...args };
|
|
@@ -48,11 +41,10 @@ function serializeLog(...args) {
|
|
|
48
41
|
return { message: args.shift(), args: parseArgs(args), ...meta };
|
|
49
42
|
return { message: 'EMPTY_MESSAGE', args: parseArgs(args), ...meta };
|
|
50
43
|
}
|
|
51
|
-
exports.serializeLog = serializeLog;
|
|
52
44
|
function apacheLogFormat(startDate, remoteClient, content, req, statusCode) {
|
|
53
45
|
return `${req.headers['x-forwarded-for'] || req.socket.remoteAddress} ${remoteClient || '-'} ${startDate.toISOString()} "${req.method} ${req.url} HTTP/${req.httpVersionMajor}.${req.httpVersionMinor}" ${statusCode} ${content || '-'}`;
|
|
54
46
|
}
|
|
55
|
-
function getStatusLevel(statusCode) {
|
|
47
|
+
export function getStatusLevel(statusCode) {
|
|
56
48
|
if (statusCode >= 400 && statusCode < 500) {
|
|
57
49
|
return 'warn';
|
|
58
50
|
}
|
|
@@ -61,9 +53,8 @@ function getStatusLevel(statusCode) {
|
|
|
61
53
|
}
|
|
62
54
|
return 'info';
|
|
63
55
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const executionId = context_1.context.current?.store.get('id');
|
|
56
|
+
export function serializeHttp(headers, startDate, remoteClient, req, res) {
|
|
57
|
+
const executionId = context.current?.store.get('id');
|
|
67
58
|
return {
|
|
68
59
|
level: getStatusLevel(res.statusCode),
|
|
69
60
|
logObject: {
|
|
@@ -85,4 +76,3 @@ function serializeHttp(headers, startDate, remoteClient, req, res) {
|
|
|
85
76
|
},
|
|
86
77
|
};
|
|
87
78
|
}
|
|
88
|
-
exports.serializeHttp = serializeHttp;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { secretsOperator } from './signer';
|
|
3
|
-
import type { BareRequest } from '../../request';
|
|
4
|
-
export
|
|
1
|
+
import { type ParseOptions, type SerializeOptions } from 'cookie';
|
|
2
|
+
import { secretsOperator } from './signer.js';
|
|
3
|
+
import type { BareRequest } from '../../request.js';
|
|
4
|
+
export type CookiesManagerOptions = Omit<SerializeOptions, 'expires'> & {
|
|
5
|
+
expires?: Date | number;
|
|
5
6
|
signed?: boolean;
|
|
6
|
-
parseOptions?:
|
|
7
|
+
parseOptions?: ParseOptions;
|
|
7
8
|
secret?: string | string[];
|
|
8
9
|
};
|
|
9
10
|
export declare class CookiesManager {
|
|
10
11
|
private options;
|
|
11
12
|
private flow;
|
|
12
13
|
signer: null | ReturnType<typeof secretsOperator>;
|
|
13
|
-
constructor(options: CookiesManagerOptions, flow: BareRequest);
|
|
14
|
+
constructor(options: CookiesManagerOptions | undefined, flow: BareRequest);
|
|
14
15
|
setCookie(name: string, value: string, options?: CookiesManagerOptions, signer?: ReturnType<typeof secretsOperator>): void;
|
|
15
16
|
clearCookie(name: string, options?: CookiesManagerOptions): void;
|
|
16
17
|
parseCookie(rawCookie?: string): {
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CookiesManager = void 0;
|
|
7
|
-
const cookie_1 = __importDefault(require("cookie"));
|
|
8
|
-
const signer_1 = require("./signer");
|
|
9
|
-
const logger_1 = require("../../logger");
|
|
10
|
-
class CookiesManager {
|
|
1
|
+
import { serialize } from 'cookie';
|
|
2
|
+
import { secretsOperator } from './signer.js';
|
|
3
|
+
import { logMe } from '../../logger/index.js';
|
|
4
|
+
export class CookiesManager {
|
|
11
5
|
options;
|
|
12
6
|
flow;
|
|
13
7
|
signer;
|
|
@@ -16,18 +10,21 @@ class CookiesManager {
|
|
|
16
10
|
this.flow = flow;
|
|
17
11
|
const secret = this.options.secret || '';
|
|
18
12
|
const enableRotation = Array.isArray(secret);
|
|
19
|
-
this.signer = typeof secret === 'string' || enableRotation ?
|
|
13
|
+
this.signer = typeof secret === 'string' || enableRotation ? secretsOperator(secret) : null;
|
|
20
14
|
}
|
|
21
15
|
setCookie(name, value, options, signer) {
|
|
22
16
|
const localSigner = signer || this.signer;
|
|
23
17
|
const opts = options || this.options;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
const { signed, ...rest } = opts;
|
|
19
|
+
const normalizedExpires = typeof rest.expires === 'number' ? new Date(rest.expires) : rest.expires;
|
|
20
|
+
const serializeOptions = {
|
|
21
|
+
...rest,
|
|
22
|
+
expires: normalizedExpires,
|
|
23
|
+
};
|
|
24
|
+
if (signed && localSigner) {
|
|
28
25
|
value = localSigner.sign(value);
|
|
29
26
|
}
|
|
30
|
-
const serialized =
|
|
27
|
+
const serialized = serialize(name, value, serializeOptions);
|
|
31
28
|
let setCookie = this.flow.getHeader('Set-Cookie');
|
|
32
29
|
if (!setCookie) {
|
|
33
30
|
this.flow.setHeader('Set-Cookie', serialized);
|
|
@@ -63,10 +60,9 @@ class CookiesManager {
|
|
|
63
60
|
}
|
|
64
61
|
unsignCookie(value) {
|
|
65
62
|
if (!this.signer) {
|
|
66
|
-
|
|
63
|
+
logMe.error('No signer defined for the cookies, unsign wont work');
|
|
67
64
|
return;
|
|
68
65
|
}
|
|
69
66
|
return this.signer.unsign(value);
|
|
70
67
|
}
|
|
71
68
|
}
|
|
72
|
-
exports.CookiesManager = CookiesManager;
|
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.secretsOperator = void 0;
|
|
7
|
-
const cookie_signature_1 = __importDefault(require("cookie-signature"));
|
|
8
|
-
function secretsOperator(secret) {
|
|
1
|
+
import cookieSignature from 'cookie-signature';
|
|
2
|
+
export function secretsOperator(secret) {
|
|
9
3
|
const secrets = Array.isArray(secret) ? secret : [secret];
|
|
10
4
|
const [signingKey] = secrets;
|
|
11
5
|
return {
|
|
12
6
|
sign(value) {
|
|
13
|
-
return
|
|
7
|
+
return cookieSignature.sign(value, signingKey);
|
|
14
8
|
},
|
|
15
9
|
unsign(signedValue) {
|
|
16
10
|
let valid = false;
|
|
17
11
|
let renew = false;
|
|
18
12
|
let value = null;
|
|
19
13
|
for (const key of secrets) {
|
|
20
|
-
const result =
|
|
14
|
+
const result = cookieSignature.unsign(signedValue, key);
|
|
21
15
|
if (result !== false) {
|
|
22
16
|
valid = true;
|
|
23
17
|
renew = key !== signingKey;
|
|
@@ -29,4 +23,3 @@ function secretsOperator(secret) {
|
|
|
29
23
|
},
|
|
30
24
|
};
|
|
31
25
|
}
|
|
32
|
-
exports.secretsOperator = secretsOperator;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { HttpMethodsUnionUppercase } from '../../utils';
|
|
2
|
-
import type { BareRequest } from '../../request';
|
|
3
|
-
export
|
|
1
|
+
import type { HttpMethodsUnionUppercase } from '../../utils/index.js';
|
|
2
|
+
import type { BareRequest } from '../../request.js';
|
|
3
|
+
export type CorsOptions = {
|
|
4
4
|
origin?: string | RegExp;
|
|
5
5
|
methods?: Array<HttpMethodsUnionUppercase>;
|
|
6
6
|
preflightContinue?: boolean;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
exports.Cors = void 0;
|
|
4
|
-
class Cors {
|
|
2
|
+
export class Cors {
|
|
5
3
|
options;
|
|
6
4
|
defaults = {
|
|
7
5
|
origin: '*',
|
|
@@ -164,4 +162,3 @@ class Cors {
|
|
|
164
162
|
}
|
|
165
163
|
}
|
|
166
164
|
}
|
|
167
|
-
exports.Cors = Cors;
|
package/lib/request.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { CookiesManager } from './middlewares/cookies/cookie-manager';
|
|
1
|
+
import { StatusCodesUnion } from './utils/index.js';
|
|
2
|
+
import { CookiesManager } from './middlewares/cookies/cookie-manager.js';
|
|
4
3
|
import type { IncomingMessage, ServerResponse } from 'http';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export
|
|
4
|
+
type Cacheability = 'public' | 'private' | 'no-cache' | 'no-store';
|
|
5
|
+
type ExpirationType = 'max-age' | 's-maxage' | 'max-stale' | 'min-fresh' | 'stale-while-revalidate' | 'stale-if-error';
|
|
6
|
+
type Revalidation = 'must-revalidate' | 'proxy-revalidate' | 'immutable';
|
|
7
|
+
export type CacheOpts = {
|
|
9
8
|
cacheability: Cacheability;
|
|
10
9
|
expirationKind: ExpirationType;
|
|
11
10
|
/**
|
|
@@ -62,7 +61,7 @@ export declare class BareRequest<H extends {
|
|
|
62
61
|
getHeader(header: string): string | string[];
|
|
63
62
|
getCookie(cookie: string): string;
|
|
64
63
|
getCookies(): {
|
|
65
|
-
[
|
|
64
|
+
[cooke: string]: string;
|
|
66
65
|
};
|
|
67
66
|
disableCache(): void;
|
|
68
67
|
setCache(cacheOpts: CacheOpts): void;
|
package/lib/request.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const util_1 = require("util");
|
|
13
|
-
const stream_1 = require("stream");
|
|
14
|
-
const url_1 = __importDefault(require("url"));
|
|
15
|
-
const generateId = (0, hyperid_1.default)();
|
|
16
|
-
const statusTuples = Object.entries(utils_1.StatusCodes).reduce((acc, [name, status]) => {
|
|
17
|
-
acc[status] = utils_1.StatusPhrases[name];
|
|
1
|
+
import hyperid from 'hyperid';
|
|
2
|
+
import { StatusCodes, StatusPhrases } from './utils/index.js';
|
|
3
|
+
import { JSONParse, JSONStringify } from './utils/safe-json.js';
|
|
4
|
+
import { logHttp, logMe } from './logger/index.js';
|
|
5
|
+
import { CookiesManager } from './middlewares/cookies/cookie-manager.js';
|
|
6
|
+
import { types } from 'util';
|
|
7
|
+
import { Writable } from 'stream';
|
|
8
|
+
import url from 'url';
|
|
9
|
+
const generateId = hyperid();
|
|
10
|
+
const statusTuples = Object.entries(StatusCodes).reduce((acc, [name, status]) => {
|
|
11
|
+
acc[status] = StatusPhrases[name];
|
|
18
12
|
return acc;
|
|
19
13
|
}, {});
|
|
20
|
-
class BareRequest {
|
|
14
|
+
export class BareRequest {
|
|
21
15
|
_originalRequest;
|
|
22
16
|
_originalResponse;
|
|
23
17
|
ID;
|
|
@@ -49,7 +43,7 @@ class BareRequest {
|
|
|
49
43
|
this.requestHeaders = this._originalRequest.headers;
|
|
50
44
|
this.logging = options?.logging ?? false;
|
|
51
45
|
// this is a placeholder URL base that we need to make class working
|
|
52
|
-
new
|
|
46
|
+
new url.URL(`http://localhost/${this._originalRequest.url}`).searchParams.forEach((value, name) => (this.query[name] = value));
|
|
53
47
|
// parsed;
|
|
54
48
|
_originalRequest['flow'] = this; // to receive flow object later on in the route handler
|
|
55
49
|
this.addHeaders({
|
|
@@ -72,7 +66,7 @@ class BareRequest {
|
|
|
72
66
|
.on('data', (chunk) => temp.push(chunk))
|
|
73
67
|
.on('end', () => {
|
|
74
68
|
const parsed = this.classifyRequestBody(temp);
|
|
75
|
-
if (
|
|
69
|
+
if (types.isNativeError(parsed))
|
|
76
70
|
return reject(parsed);
|
|
77
71
|
this.requestBody = parsed;
|
|
78
72
|
resolve(parsed);
|
|
@@ -84,7 +78,7 @@ class BareRequest {
|
|
|
84
78
|
}
|
|
85
79
|
}
|
|
86
80
|
attachCookieManager(opts) {
|
|
87
|
-
this.cm = new
|
|
81
|
+
this.cm = new CookiesManager(opts, this);
|
|
88
82
|
}
|
|
89
83
|
populateCookies() {
|
|
90
84
|
this.cookies = this.cm?.parseCookie(this._originalRequest.headers.cookie) || {};
|
|
@@ -95,7 +89,7 @@ class BareRequest {
|
|
|
95
89
|
case 'text/plain':
|
|
96
90
|
return wholeChunk.toString();
|
|
97
91
|
case 'application/json':
|
|
98
|
-
return
|
|
92
|
+
return JSONParse(wholeChunk.toString());
|
|
99
93
|
case 'application/x-www-form-urlencoded':
|
|
100
94
|
const store = {};
|
|
101
95
|
for (const curr of wholeChunk.toString().split('&')) {
|
|
@@ -197,17 +191,17 @@ class BareRequest {
|
|
|
197
191
|
}
|
|
198
192
|
json(data) {
|
|
199
193
|
// to generate with fast-json-stringify schema issue #1
|
|
200
|
-
const jsoned =
|
|
194
|
+
const jsoned = JSONStringify(data);
|
|
201
195
|
this.setHeader('Content-Type', 'application/json');
|
|
202
196
|
this._send(jsoned ? jsoned : undefined);
|
|
203
197
|
}
|
|
204
198
|
_send(chunk) {
|
|
205
199
|
if (this._originalResponse.socket?.destroyed) {
|
|
206
|
-
|
|
200
|
+
logMe.error("Tying to send into closed client's stream");
|
|
207
201
|
return;
|
|
208
202
|
}
|
|
209
203
|
if (this._originalResponse.headersSent || this.sent) {
|
|
210
|
-
|
|
204
|
+
logMe.error('Trying to send with the headers already sent');
|
|
211
205
|
return;
|
|
212
206
|
}
|
|
213
207
|
// work basic headers
|
|
@@ -225,7 +219,7 @@ class BareRequest {
|
|
|
225
219
|
this.sent = true;
|
|
226
220
|
// call logging section
|
|
227
221
|
if (this.logging === true) {
|
|
228
|
-
|
|
222
|
+
logHttp(this.headers, this.startDate, this.remoteClient, this._originalRequest, this._originalResponse);
|
|
229
223
|
}
|
|
230
224
|
}
|
|
231
225
|
sendStringifiedJson(data) {
|
|
@@ -251,7 +245,7 @@ class BareRequest {
|
|
|
251
245
|
case Number:
|
|
252
246
|
this._send('' + anything);
|
|
253
247
|
break;
|
|
254
|
-
case
|
|
248
|
+
case Writable:
|
|
255
249
|
this.stream(anything);
|
|
256
250
|
break;
|
|
257
251
|
case Object:
|
|
@@ -260,8 +254,7 @@ class BareRequest {
|
|
|
260
254
|
break;
|
|
261
255
|
default:
|
|
262
256
|
this._send();
|
|
263
|
-
|
|
257
|
+
logMe.warn('Unknown type to send');
|
|
264
258
|
}
|
|
265
259
|
}
|
|
266
260
|
}
|
|
267
|
-
exports.BareRequest = BareRequest;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { ts, Type } from 'ts-morph';
|
|
2
|
-
export
|
|
2
|
+
export type StringSchemaType = {
|
|
3
3
|
type: 'string';
|
|
4
4
|
nullable: boolean;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export type NumberSchemaType = {
|
|
7
7
|
type: 'number';
|
|
8
8
|
nullable: boolean;
|
|
9
9
|
};
|
|
10
|
-
export
|
|
10
|
+
export type BooleanSchemaType = {
|
|
11
11
|
type: 'boolean';
|
|
12
12
|
nullable: boolean;
|
|
13
13
|
};
|
|
14
|
-
export
|
|
14
|
+
export type ArraySchemaType = {
|
|
15
15
|
type: 'array';
|
|
16
16
|
items: StringSchemaType | NumberSchemaType | BooleanSchemaType | ArraySchemaType | ObjectSchemaType;
|
|
17
17
|
nullable: boolean;
|
|
18
18
|
};
|
|
19
|
-
export
|
|
19
|
+
export type ObjectSchemaType = {
|
|
20
20
|
type: 'object';
|
|
21
21
|
properties: {
|
|
22
22
|
[key: string]: StringSchemaType | NumberSchemaType | BooleanSchemaType | ArraySchemaType | ObjectSchemaType;
|
|
23
23
|
};
|
|
24
24
|
nullable: boolean;
|
|
25
25
|
};
|
|
26
|
-
export
|
|
26
|
+
export type UnionSchemaType = {
|
|
27
27
|
type: 'union';
|
|
28
28
|
anyOf: CustomSchema[];
|
|
29
29
|
nullable: boolean;
|
|
30
30
|
};
|
|
31
|
-
export
|
|
31
|
+
export type CustomSchema = StringSchemaType | NumberSchemaType | BooleanSchemaType | ArraySchemaType | ObjectSchemaType | UnionSchemaType;
|
|
32
32
|
export declare const generateCustomSchema: (t: Type<ts.Type>) => CustomSchema;
|