@miqro/core 1.7.1 → 1.7.2
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/dist/api-router.js +0 -1
- package/dist/app-utils.js +0 -1
- package/dist/app.js +0 -1
- package/dist/common/api-router-utils.js +0 -1
- package/dist/common/api-router.js +0 -1
- package/dist/common/config/config.js +0 -1
- package/dist/common/config/featuretoggle.js +0 -1
- package/dist/common/config/index.js +0 -1
- package/dist/common/config/loader.js +0 -1
- package/dist/common/context.js +0 -1
- package/dist/common/error/badrequesterror.js +0 -1
- package/dist/common/error/configfilenotfound.js +0 -1
- package/dist/common/error/forbiddenerror.js +0 -1
- package/dist/common/error/index.js +0 -1
- package/dist/common/error/notfound.js +0 -1
- package/dist/common/error/notimplemented.js +0 -1
- package/dist/common/error/unauthorizederror.js +0 -1
- package/dist/common/helpers.js +0 -1
- package/dist/common/index.js +0 -1
- package/dist/common/logger/constants.js +0 -1
- package/dist/common/logger/factory.js +0 -1
- package/dist/common/logger/index.js +0 -1
- package/dist/common/logger/interfaces.js +0 -1
- package/dist/common/logger/logger.js +0 -1
- package/dist/common/logger/transport/console.js +0 -1
- package/dist/common/logger/transport/file.js +0 -1
- package/dist/common/router-handler.js +0 -1
- package/dist/common/router-helpers.js +0 -1
- package/dist/common/router-utils.js +0 -1
- package/dist/common/session/common.js +0 -1
- package/dist/common/session/index.js +0 -1
- package/dist/common/session/service.js +0 -1
- package/dist/common/tokenize-match.js +0 -1
- package/dist/index.js +0 -1
- package/dist/middleware/buffer.js +0 -1
- package/dist/middleware/cookie.js +0 -1
- package/dist/middleware/cors.js +0 -1
- package/dist/middleware/form.js +0 -1
- package/dist/middleware/group.js +0 -1
- package/dist/middleware/index.js +0 -1
- package/dist/middleware/json.js +0 -1
- package/dist/middleware/logger.js +0 -1
- package/dist/middleware/parse.js +0 -1
- package/dist/middleware/result.js +0 -1
- package/dist/middleware/session.js +0 -1
- package/dist/middleware/tag.js +0 -1
- package/dist/middleware/text.js +0 -1
- package/dist/proxy.js +0 -1
- package/dist/router.js +0 -1
- package/dist/static.js +0 -1
- package/package.json +6 -6
- package/dist/api-router.js.map +0 -1
- package/dist/app-utils.js.map +0 -1
- package/dist/app.js.map +0 -1
- package/dist/common/api-router-utils.js.map +0 -1
- package/dist/common/api-router.js.map +0 -1
- package/dist/common/config/config.js.map +0 -1
- package/dist/common/config/featuretoggle.js.map +0 -1
- package/dist/common/config/index.js.map +0 -1
- package/dist/common/config/loader.js.map +0 -1
- package/dist/common/context.js.map +0 -1
- package/dist/common/error/badrequesterror.js.map +0 -1
- package/dist/common/error/configfilenotfound.js.map +0 -1
- package/dist/common/error/forbiddenerror.js.map +0 -1
- package/dist/common/error/index.js.map +0 -1
- package/dist/common/error/notfound.js.map +0 -1
- package/dist/common/error/notimplemented.js.map +0 -1
- package/dist/common/error/unauthorizederror.js.map +0 -1
- package/dist/common/helpers.js.map +0 -1
- package/dist/common/index.js.map +0 -1
- package/dist/common/logger/constants.js.map +0 -1
- package/dist/common/logger/factory.js.map +0 -1
- package/dist/common/logger/index.js.map +0 -1
- package/dist/common/logger/interfaces.js.map +0 -1
- package/dist/common/logger/logger.js.map +0 -1
- package/dist/common/logger/transport/console.js.map +0 -1
- package/dist/common/logger/transport/file.js.map +0 -1
- package/dist/common/router-handler.js.map +0 -1
- package/dist/common/router-helpers.js.map +0 -1
- package/dist/common/router-utils.js.map +0 -1
- package/dist/common/session/common.js.map +0 -1
- package/dist/common/session/index.js.map +0 -1
- package/dist/common/session/service.js.map +0 -1
- package/dist/common/tokenize-match.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/middleware/buffer.js.map +0 -1
- package/dist/middleware/cookie.js.map +0 -1
- package/dist/middleware/cors.js.map +0 -1
- package/dist/middleware/form.js.map +0 -1
- package/dist/middleware/group.js.map +0 -1
- package/dist/middleware/index.js.map +0 -1
- package/dist/middleware/json.js.map +0 -1
- package/dist/middleware/logger.js.map +0 -1
- package/dist/middleware/parse.js.map +0 -1
- package/dist/middleware/result.js.map +0 -1
- package/dist/middleware/session.js.map +0 -1
- package/dist/middleware/tag.js.map +0 -1
- package/dist/middleware/text.js.map +0 -1
- package/dist/proxy.js.map +0 -1
- package/dist/router.js.map +0 -1
- package/dist/static.js.map +0 -1
- package/src/api-router.ts +0 -60
- package/src/api-router.unit.test.js +0 -273
- package/src/app-utils.ts +0 -82
- package/src/app-utils.unit.test.js +0 -100
- package/src/app.ts +0 -51
- package/src/app.unit.test.js +0 -333
- package/src/common/api-router-utils.ts +0 -193
- package/src/common/api-router.ts +0 -19
- package/src/common/config/config.ts +0 -111
- package/src/common/config/featuretoggle.ts +0 -6
- package/src/common/config/index.ts +0 -3
- package/src/common/config/loader.ts +0 -80
- package/src/common/context.ts +0 -167
- package/src/common/context.unit.test.js +0 -288
- package/src/common/error/badrequesterror.ts +0 -6
- package/src/common/error/configfilenotfound.ts +0 -6
- package/src/common/error/forbiddenerror.ts +0 -6
- package/src/common/error/index.ts +0 -6
- package/src/common/error/notfound.ts +0 -6
- package/src/common/error/notimplemented.ts +0 -6
- package/src/common/error/unauthorizederror.ts +0 -6
- package/src/common/helpers.ts +0 -11
- package/src/common/index.ts +0 -7
- package/src/common/logger/constants.ts +0 -27
- package/src/common/logger/factory.ts +0 -53
- package/src/common/logger/index.ts +0 -6
- package/src/common/logger/interfaces.ts +0 -19
- package/src/common/logger/logger.ts +0 -136
- package/src/common/logger/transport/console.ts +0 -34
- package/src/common/logger/transport/file.ts +0 -26
- package/src/common/router-handler.ts +0 -159
- package/src/common/router-helpers.ts +0 -20
- package/src/common/router-utils.ts +0 -95
- package/src/common/session/common.ts +0 -40
- package/src/common/session/index.ts +0 -2
- package/src/common/session/service.ts +0 -160
- package/src/common/tokenize-match.ts +0 -130
- package/src/index.ts +0 -7
- package/src/middleware/buffer.ts +0 -80
- package/src/middleware/cookie.ts +0 -20
- package/src/middleware/cors.ts +0 -96
- package/src/middleware/form.ts +0 -59
- package/src/middleware/group.ts +0 -70
- package/src/middleware/index.ts +0 -48
- package/src/middleware/json.ts +0 -55
- package/src/middleware/logger.ts +0 -18
- package/src/middleware/parse.ts +0 -93
- package/src/middleware/result.ts +0 -43
- package/src/middleware/session.ts +0 -136
- package/src/middleware/tag.ts +0 -15
- package/src/middleware/text.ts +0 -45
- package/src/proxy.ts +0 -77
- package/src/proxy.unit.test.js +0 -159
- package/src/router.ts +0 -112
- package/src/router.unit.test.js +0 -396
- package/src/setup-test.js +0 -5
- package/src/static.ts +0 -238
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import {extname, resolve} from "path";
|
|
2
|
-
import {existsSync, readdirSync, readFileSync} from "fs";
|
|
3
|
-
import {ConfigOutput, ConfigPathResolver, ConfigStore, LoadConfigOut} from "./config";
|
|
4
|
-
|
|
5
|
-
interface Logger {
|
|
6
|
-
log(message?: any, ...optionalParams: any[]): void;
|
|
7
|
-
|
|
8
|
-
info(message?: any, ...optionalParams: any[]): void;
|
|
9
|
-
|
|
10
|
-
trace(message?: any, ...optionalParams: any[]): void;
|
|
11
|
-
|
|
12
|
-
debug(message?: any, ...optionalParams: any[]): void;
|
|
13
|
-
|
|
14
|
-
warn(message?: any, ...optionalParams: any[]): void;
|
|
15
|
-
|
|
16
|
-
error(message?: any, ...optionalParams: any[]): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class ConfigFileNotFoundError extends Error {
|
|
21
|
-
constructor(args: any) {
|
|
22
|
-
super(args);
|
|
23
|
-
this.name = "ConfigFileNotFoundError";
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
|
|
28
|
-
const ret: ConfigOutput = Object.create(null);
|
|
29
|
-
envFileContent.split("\n")
|
|
30
|
-
.filter(value => value && value.length > 0 && value.substring(0, 1) !== "#")
|
|
31
|
-
.forEach((line) => {
|
|
32
|
-
const separator = line.indexOf("=");
|
|
33
|
-
if (separator === -1) {
|
|
34
|
-
throw new Error(`cannot read ${line}`);
|
|
35
|
-
}
|
|
36
|
-
ret[line.substring(0, separator)] = line.substring(separator + 1);
|
|
37
|
-
});
|
|
38
|
-
const keys = Object.keys(ret);
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
if (combined) {
|
|
41
|
-
combined[key] = ret[key];
|
|
42
|
-
}
|
|
43
|
-
ConfigStore.putConfig(key, ret[key]);
|
|
44
|
-
}
|
|
45
|
-
return ret;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const loadConfigFile = (envFilePath: string, combined?: ConfigOutput, logger?: Logger): ConfigOutput => {
|
|
49
|
-
if (!existsSync(envFilePath)) {
|
|
50
|
-
throw new ConfigFileNotFoundError(`config file [${envFilePath}] doesnt exists!`);
|
|
51
|
-
} else {
|
|
52
|
-
if (logger) {
|
|
53
|
-
logger.trace(`loading config from [${envFilePath}].`);
|
|
54
|
-
}
|
|
55
|
-
return loadConfigString(readFileSync(envFilePath).toString(), combined);
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const MISSED_TO_RUNMIQRO_INIT = (configDirname: string): string => `no config loaded. env files in [${configDirname}] doesnt exist!.`;
|
|
60
|
-
|
|
61
|
-
export const loadConfig = (configDirname: string = ConfigPathResolver.getConfigDirname(), logger?: Logger): LoadConfigOut => {
|
|
62
|
-
const outputs: ConfigOutput[] = [];
|
|
63
|
-
const combined = Object.create(null);
|
|
64
|
-
if (existsSync(configDirname)) {
|
|
65
|
-
const configFiles = readdirSync(configDirname);
|
|
66
|
-
for (const configFile of configFiles) {
|
|
67
|
-
const configFilePath = resolve(configDirname, configFile);
|
|
68
|
-
const ext = extname(configFilePath);
|
|
69
|
-
if (ext === ".env") {
|
|
70
|
-
outputs.push(loadConfigFile(configFilePath, combined, logger));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (logger && configFiles.length === 0) {
|
|
74
|
-
logger.debug(MISSED_TO_RUNMIQRO_INIT(configDirname));
|
|
75
|
-
}
|
|
76
|
-
} else if (logger) {
|
|
77
|
-
logger.debug(MISSED_TO_RUNMIQRO_INIT(configDirname));
|
|
78
|
-
}
|
|
79
|
-
return {combined, outputs};
|
|
80
|
-
};
|
package/src/common/context.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import {Logger} from "./logger/index.js";
|
|
2
|
-
import {Session} from "./session";
|
|
3
|
-
import {IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, ServerResponse} from "http";
|
|
4
|
-
import {normalizePath, parseSearchParams} from "./tokenize-match";
|
|
5
|
-
import {URL} from "url";
|
|
6
|
-
import {newURL} from "./helpers";
|
|
7
|
-
import {CookieSerializeOptions, serialize} from "cookie";
|
|
8
|
-
|
|
9
|
-
export type Method =
|
|
10
|
-
"get"
|
|
11
|
-
| "post"
|
|
12
|
-
| "put"
|
|
13
|
-
| "delete"
|
|
14
|
-
| "patch"
|
|
15
|
-
| "options"
|
|
16
|
-
| "GET"
|
|
17
|
-
| "POST"
|
|
18
|
-
| "PUT"
|
|
19
|
-
| "DELETE"
|
|
20
|
-
| "PATCH"
|
|
21
|
-
| "OPTIONS";
|
|
22
|
-
|
|
23
|
-
export const IGNORE_HEADER = ["keep-alive", "connection"];
|
|
24
|
-
|
|
25
|
-
export type Handler<T = boolean | void | any> = ((ctx: Context) => Promise<T>) | ((ctx: Context) => T);
|
|
26
|
-
|
|
27
|
-
export type ErrorHandler<T = boolean | void | any> =
|
|
28
|
-
((e: unknown, ctx: Context) => Promise<T>)
|
|
29
|
-
| ((e: unknown, ctx: Context) => T);
|
|
30
|
-
|
|
31
|
-
export class Context {
|
|
32
|
-
public readonly url: URL; // new URL(req.url ? req.url : "/", `http://${req.headers.host}`)
|
|
33
|
-
public readonly startMS: number; // Date.now()
|
|
34
|
-
public session?: Session; // a middleware will fill this
|
|
35
|
-
public results: any[]; // handlers will fill this
|
|
36
|
-
public readonly path: string; // normalized path
|
|
37
|
-
public readonly hash: string; // from this.url.hash
|
|
38
|
-
public readonly method: string; // from this.url.method
|
|
39
|
-
public headers: IncomingHttpHeaders; // from this.req.headers
|
|
40
|
-
public readonly cookies: { [name: string]: string }; // a middleware will fill this
|
|
41
|
-
public query: { [name: string]: string | string[] }; // parseSearchParams will fill this with this.url.searchParams
|
|
42
|
-
public params: { [name: string]: string | undefined }; // the router will fill this
|
|
43
|
-
public buffer?: Buffer; // empty buffer. middleware must read it
|
|
44
|
-
public readonly remoteAddress?: string;
|
|
45
|
-
public body: any; // a middleware will fill this reading the buffer
|
|
46
|
-
constructor(public readonly uuid: string, public readonly req: IncomingMessage, public readonly res: ServerResponse, public readonly logger: Logger) {
|
|
47
|
-
this.body = undefined as any;
|
|
48
|
-
this.res.removeHeader("Date");
|
|
49
|
-
this.url = newURL(req.url ? req.url : "/", `http://${req.headers.host}`) as URL;
|
|
50
|
-
this.method = req.method ? req.method.toUpperCase() as string : "GET";
|
|
51
|
-
this.path = normalizePath(this.url.pathname); // normalized path
|
|
52
|
-
this.hash = this.url.hash;
|
|
53
|
-
this.params = Object.create(null);
|
|
54
|
-
this.query = parseSearchParams(this.url.searchParams);
|
|
55
|
-
//this.buffer = Buffer.from("");
|
|
56
|
-
this.remoteAddress = req.socket.remoteAddress;
|
|
57
|
-
this.cookies = Object.create(null); // a middleware will fill
|
|
58
|
-
this.headers = req.headers;
|
|
59
|
-
this.startMS = Date.now();
|
|
60
|
-
this.results = []; // handlers will fill this
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public async close(): Promise<void> {
|
|
64
|
-
return this.end({
|
|
65
|
-
status: 400,
|
|
66
|
-
headers: {
|
|
67
|
-
connection: "close"
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public addVaryHeader(value: number | string | ReadonlyArray<string>): ServerResponse {
|
|
73
|
-
const c = this.res.getHeader("Vary");
|
|
74
|
-
const current = c ? c instanceof Array ? c.join(", ") : String(c) : "";
|
|
75
|
-
const nV = value instanceof Array ? value.join(", ") : String(value);
|
|
76
|
-
const newValue = current ? `${current}, ${nV}` : nV;
|
|
77
|
-
return this.setHeader("Vary", newValue.indexOf("*") !== -1 ? "*" : newValue);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public setHeader(name: string, value: number | string | ReadonlyArray<string>): ServerResponse {
|
|
81
|
-
return this.res.setHeader(name, value);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public setCookie(name: string, value: string, options?: CookieSerializeOptions): ServerResponse {
|
|
85
|
-
return this.setHeader('Set-Cookie', serialize(name, String(value), options));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async end({status, headers, body}: {
|
|
89
|
-
body?: any;
|
|
90
|
-
status: number;
|
|
91
|
-
headers: OutgoingHttpHeaders;
|
|
92
|
-
}): Promise<void> {
|
|
93
|
-
if (this.res.headersSent) {
|
|
94
|
-
return Promise.reject(new Error("already ended"));
|
|
95
|
-
}
|
|
96
|
-
return new Promise<void>((resolve, reject) => {
|
|
97
|
-
try {
|
|
98
|
-
if (this.res.headersSent) {
|
|
99
|
-
reject(new Error("already ended"));
|
|
100
|
-
} else {
|
|
101
|
-
this.res.statusCode = status;
|
|
102
|
-
const keys = Object.keys(headers);
|
|
103
|
-
for (const key of keys) {
|
|
104
|
-
if (IGNORE_HEADER.indexOf(key.toLocaleLowerCase()) !== -1) {
|
|
105
|
-
this.logger.warn("ignoring " + key);
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
if (headers[key] !== undefined) {
|
|
109
|
-
this.res.setHeader(key, headers[key] as any);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
this.res.setHeader("connection", "close");
|
|
113
|
-
this.res.end(body !== undefined ? (body instanceof Buffer ? body : String(body)) : undefined, () => {
|
|
114
|
-
resolve();
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
} catch (e) {
|
|
118
|
-
reject(e);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
124
|
-
public async json(body: any, headers?: OutgoingHttpHeaders, status?: number): Promise<void> {
|
|
125
|
-
return this.end({
|
|
126
|
-
status: status !== undefined ? status : 200,
|
|
127
|
-
headers: {
|
|
128
|
-
['Content-Type']: 'application/json; charset=utf-8',
|
|
129
|
-
...headers,
|
|
130
|
-
},
|
|
131
|
-
body: JSON.stringify(body)
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
public async text(text: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void> {
|
|
136
|
-
return this.end({
|
|
137
|
-
status: status !== undefined ? status : 200,
|
|
138
|
-
headers: {
|
|
139
|
-
['Content-Type']: 'plain/text; charset=utf-8',
|
|
140
|
-
...headers
|
|
141
|
-
},
|
|
142
|
-
body: text
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public async html(html: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void> {
|
|
147
|
-
return this.end({
|
|
148
|
-
status: status !== undefined ? status : 200,
|
|
149
|
-
headers: {
|
|
150
|
-
['Content-Type']: 'text/html; charset=utf-8',
|
|
151
|
-
...headers
|
|
152
|
-
},
|
|
153
|
-
body: html
|
|
154
|
-
})
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public async redirect(url: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void> {
|
|
158
|
-
return this.end({
|
|
159
|
-
status: status !== undefined ? status : 302,
|
|
160
|
-
headers: {
|
|
161
|
-
['Location']: url,
|
|
162
|
-
...headers
|
|
163
|
-
}
|
|
164
|
-
})
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
const {fake, requireMock} = require("@miqro/test");
|
|
2
|
-
const {resolve} = require("path");
|
|
3
|
-
const {format} = require("util");
|
|
4
|
-
const {strictEqual} = require("assert");
|
|
5
|
-
const {distPath} = require("../setup-test.js");
|
|
6
|
-
const {serialize} = require("cookie");
|
|
7
|
-
|
|
8
|
-
const testFilePath = resolve(distPath, "common", "context.js");
|
|
9
|
-
|
|
10
|
-
describe("context unit tests", () => {
|
|
11
|
-
|
|
12
|
-
const res = {
|
|
13
|
-
headersSent: false,
|
|
14
|
-
end: fake((body, cb) => {
|
|
15
|
-
res.headersSent = true
|
|
16
|
-
cb();
|
|
17
|
-
}),
|
|
18
|
-
setHeader: fake(() => {
|
|
19
|
-
|
|
20
|
-
}),
|
|
21
|
-
removeHeader: fake(() => {
|
|
22
|
-
|
|
23
|
-
})
|
|
24
|
-
};
|
|
25
|
-
const logger = "logger1";
|
|
26
|
-
const newURLRet = {
|
|
27
|
-
pathname: "pathname",
|
|
28
|
-
searchParams: "searchParams"
|
|
29
|
-
};
|
|
30
|
-
const parseSearchParamsRet = "parseSearchParamsRet";
|
|
31
|
-
const normalizePathRet = "normalizePathRet";
|
|
32
|
-
const newURL = fake(() => {
|
|
33
|
-
return newURLRet;
|
|
34
|
-
});
|
|
35
|
-
const normalizePath = fake(() => {
|
|
36
|
-
return normalizePathRet;
|
|
37
|
-
});
|
|
38
|
-
const parseSearchParams = fake(() => {
|
|
39
|
-
return parseSearchParamsRet;
|
|
40
|
-
});
|
|
41
|
-
const cookieSerializeRet = "cookieSerializeRet";
|
|
42
|
-
const cookieSerialize = fake(() => {
|
|
43
|
-
return cookieSerializeRet;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
let Context;
|
|
47
|
-
|
|
48
|
-
before(() => {
|
|
49
|
-
res.headersSent = false;
|
|
50
|
-
cookieSerialize.reset();
|
|
51
|
-
res.end.reset();
|
|
52
|
-
res.setHeader.reset();
|
|
53
|
-
res.removeHeader.reset();
|
|
54
|
-
newURL.reset();
|
|
55
|
-
normalizePath.reset();
|
|
56
|
-
parseSearchParams.reset();
|
|
57
|
-
const lib = requireMock(testFilePath, {
|
|
58
|
-
"./logger": {},
|
|
59
|
-
"./session": {},
|
|
60
|
-
"http": {},
|
|
61
|
-
"./tokenize-match": {
|
|
62
|
-
normalizePath,
|
|
63
|
-
parseSearchParams
|
|
64
|
-
},
|
|
65
|
-
"url": {},
|
|
66
|
-
"./helpers": {
|
|
67
|
-
newURL
|
|
68
|
-
},
|
|
69
|
-
"cookie": {
|
|
70
|
-
serialize: cookieSerialize
|
|
71
|
-
},
|
|
72
|
-
}, distPath);
|
|
73
|
-
Context = lib.Context;
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const uuid = "uuid1";
|
|
77
|
-
[{
|
|
78
|
-
req: {
|
|
79
|
-
url: "/url",
|
|
80
|
-
socket: {
|
|
81
|
-
remoteAddress: "remoteAddress"
|
|
82
|
-
},
|
|
83
|
-
headers: {
|
|
84
|
-
host: "fakeHost"
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}, {
|
|
88
|
-
req: {
|
|
89
|
-
url: "/url",
|
|
90
|
-
method: "MeThoD",
|
|
91
|
-
socket: {
|
|
92
|
-
remoteAddress: "remoteAddress"
|
|
93
|
-
},
|
|
94
|
-
headers: {
|
|
95
|
-
host: "fakeHost"
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}].forEach(({req}) => {
|
|
99
|
-
|
|
100
|
-
it(format("ctor req %o", req), async () => {
|
|
101
|
-
const context = new Context(uuid, req, res, logger);
|
|
102
|
-
strictEqual(context.uuid, uuid);
|
|
103
|
-
strictEqual(context.body, undefined);
|
|
104
|
-
strictEqual(res.removeHeader.callCount, 1);
|
|
105
|
-
strictEqual(res.removeHeader.callArgs[0][0], "Date");
|
|
106
|
-
strictEqual(context.req, req);
|
|
107
|
-
strictEqual(context.res, res);
|
|
108
|
-
strictEqual(context.query, parseSearchParamsRet);
|
|
109
|
-
strictEqual(cookieSerialize.callCount, 0);
|
|
110
|
-
strictEqual(parseSearchParams.callCount, 1);
|
|
111
|
-
strictEqual(parseSearchParams.callArgs[0][0], newURLRet.searchParams);
|
|
112
|
-
strictEqual(normalizePath.callCount, 1);
|
|
113
|
-
strictEqual(normalizePath.callArgs[0][0], newURLRet.pathname);
|
|
114
|
-
strictEqual(context.path, normalizePathRet);
|
|
115
|
-
strictEqual(context.method, req.method ? req.method.toUpperCase() : "GET");
|
|
116
|
-
strictEqual(context.logger, logger);
|
|
117
|
-
strictEqual(context.url, newURLRet);
|
|
118
|
-
strictEqual(newURL.callCount, 1);
|
|
119
|
-
strictEqual(newURL.callArgs[0][0], req.url);
|
|
120
|
-
strictEqual(newURL.callArgs[0][1], `http://${req.headers.host}`);
|
|
121
|
-
strictEqual(res.end.callCount, 0);
|
|
122
|
-
strictEqual(res.setHeader.callCount, 0);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
it("this.setHeader alias for this.res.setHeader", () => {
|
|
128
|
-
const context = new Context(uuid, {
|
|
129
|
-
url: "/url",
|
|
130
|
-
method: "MeThoD",
|
|
131
|
-
socket: {
|
|
132
|
-
remoteAddress: "remoteAddress"
|
|
133
|
-
},
|
|
134
|
-
headers: {
|
|
135
|
-
host: "fakeHost"
|
|
136
|
-
}
|
|
137
|
-
}, res, logger);
|
|
138
|
-
context.setHeader("key", "value");
|
|
139
|
-
strictEqual(cookieSerialize.callCount, 0);
|
|
140
|
-
strictEqual(res.end.callCount, 0);
|
|
141
|
-
strictEqual(res.setHeader.callCount, 1);
|
|
142
|
-
strictEqual(res.setHeader.callArgs[0][0], "key");
|
|
143
|
-
strictEqual(res.setHeader.callArgs[0][1], "value");
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it("this.close calls this.end", async () => {
|
|
147
|
-
const context = new Context(uuid, {
|
|
148
|
-
url: "/url",
|
|
149
|
-
method: "MeThoD",
|
|
150
|
-
socket: {
|
|
151
|
-
remoteAddress: "remoteAddress"
|
|
152
|
-
},
|
|
153
|
-
headers: {
|
|
154
|
-
host: "fakeHost"
|
|
155
|
-
}
|
|
156
|
-
}, res, logger);
|
|
157
|
-
context.end = fake(async () => {
|
|
158
|
-
|
|
159
|
-
});
|
|
160
|
-
await context.close();
|
|
161
|
-
strictEqual(cookieSerialize.callCount, 0);
|
|
162
|
-
strictEqual(res.end.callCount, 0);
|
|
163
|
-
strictEqual(res.setHeader.callCount, 0);
|
|
164
|
-
strictEqual(context.end.callCount, 1);
|
|
165
|
-
strictEqual(context.end.callArgs[0][0].status, 400);
|
|
166
|
-
strictEqual(context.end.callArgs[0][0].headers.connection, "close");
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("this.addVaryHeader", () => {
|
|
170
|
-
// TODO
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it("this.setCookie alias for this.res.setHeader with cookie.serializer", async () => {
|
|
174
|
-
const context = new Context(uuid, {
|
|
175
|
-
url: "/url",
|
|
176
|
-
method: "MeThoD",
|
|
177
|
-
socket: {
|
|
178
|
-
remoteAddress: "remoteAddress"
|
|
179
|
-
},
|
|
180
|
-
headers: {
|
|
181
|
-
host: "fakeHost"
|
|
182
|
-
}
|
|
183
|
-
}, res, logger);
|
|
184
|
-
await context.setCookie("name", "value");
|
|
185
|
-
strictEqual(cookieSerialize.callCount, 1);
|
|
186
|
-
strictEqual(cookieSerialize.callArgs[0][0], "name");
|
|
187
|
-
strictEqual(cookieSerialize.callArgs[0][1], "value");
|
|
188
|
-
strictEqual(res.end.callCount, 0);
|
|
189
|
-
strictEqual(res.setHeader.callCount, 1);
|
|
190
|
-
strictEqual(res.setHeader.callArgs[0][0], "Set-Cookie");
|
|
191
|
-
strictEqual(res.setHeader.callArgs[0][1], cookieSerializeRet);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("this.end with headers not sent", async () => {
|
|
195
|
-
const context = new Context(uuid, {
|
|
196
|
-
url: "/url",
|
|
197
|
-
method: "MeThoD",
|
|
198
|
-
socket: {
|
|
199
|
-
remoteAddress: "remoteAddress"
|
|
200
|
-
},
|
|
201
|
-
headers: {
|
|
202
|
-
host: "fakeHost"
|
|
203
|
-
}
|
|
204
|
-
}, res, logger);
|
|
205
|
-
res.headersSent = true;
|
|
206
|
-
try {
|
|
207
|
-
await context.end("end1");
|
|
208
|
-
strictEqual(false, true);
|
|
209
|
-
} catch (e) {
|
|
210
|
-
strictEqual(e.message, "already ended");
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it("this.end with headers sent", async () => {
|
|
215
|
-
const context = new Context(uuid, {
|
|
216
|
-
url: "/url",
|
|
217
|
-
method: "MeThoD",
|
|
218
|
-
socket: {
|
|
219
|
-
remoteAddress: "remoteAddress"
|
|
220
|
-
},
|
|
221
|
-
headers: {
|
|
222
|
-
host: "fakeHost"
|
|
223
|
-
}
|
|
224
|
-
}, res, logger
|
|
225
|
-
);
|
|
226
|
-
const headers = {
|
|
227
|
-
key1: "value1",
|
|
228
|
-
key2: "value2"
|
|
229
|
-
};
|
|
230
|
-
const body = "body";
|
|
231
|
-
const status = "status";
|
|
232
|
-
await context.end({
|
|
233
|
-
headers,
|
|
234
|
-
body,
|
|
235
|
-
status
|
|
236
|
-
});
|
|
237
|
-
strictEqual(res.end.callCount, 1);
|
|
238
|
-
strictEqual(res.end.callArgs[0][0], body);
|
|
239
|
-
strictEqual(res.setHeader.callCount, 3);
|
|
240
|
-
strictEqual(res.setHeader.callArgs[0][0], "key1");
|
|
241
|
-
strictEqual(res.setHeader.callArgs[0][1], "value1");
|
|
242
|
-
strictEqual(res.setHeader.callArgs[1][0], "key2");
|
|
243
|
-
strictEqual(res.setHeader.callArgs[1][1], "value2");
|
|
244
|
-
strictEqual(res.setHeader.callArgs[2][0], "connection");
|
|
245
|
-
strictEqual(res.setHeader.callArgs[2][1], "close");
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
[{status: "status"}, {status: undefined}].forEach(({status}) => {
|
|
249
|
-
[{headers: "headers"}, {status: undefined}].forEach(({headers}) => {
|
|
250
|
-
[{body: {attr1: "value2"}}].forEach(({body}) => {
|
|
251
|
-
it(format("this.json alias for this.end %o", {status, headers, body}), async () => {
|
|
252
|
-
const context = new Context(uuid, {
|
|
253
|
-
url: "/url",
|
|
254
|
-
method: "MeThoD",
|
|
255
|
-
socket: {
|
|
256
|
-
remoteAddress: "remoteAddress"
|
|
257
|
-
},
|
|
258
|
-
headers: {
|
|
259
|
-
host: "fakeHost"
|
|
260
|
-
}
|
|
261
|
-
}, res, logger);
|
|
262
|
-
context.end = fake(async () => {
|
|
263
|
-
|
|
264
|
-
});
|
|
265
|
-
await context.json(body, headers, status);
|
|
266
|
-
strictEqual(cookieSerialize.callCount, 0);
|
|
267
|
-
strictEqual(res.end.callCount, 0);
|
|
268
|
-
strictEqual(res.setHeader.callCount, 0);
|
|
269
|
-
strictEqual(context.end.callCount, 1);
|
|
270
|
-
strictEqual(context.end.callArgs[0][0].status, status ? status : 200);
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
it("this.text", () => {
|
|
278
|
-
// TODO
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it("this.html", () => {
|
|
282
|
-
// TODO
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it("this.redirect", () => {
|
|
286
|
-
// TODO
|
|
287
|
-
});
|
|
288
|
-
});
|
package/src/common/helpers.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export const newURL = (input: string, base?: string | any): any => {
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
return new URL(input, base)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const newURLSearchParams = (arg?: string | any): any => {
|
|
8
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
return new URLSearchParams(arg);
|
|
11
|
-
}
|
package/src/common/index.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import {LoggerFormatter, LoggerTransport} from "./interfaces.js";
|
|
2
|
-
import {File} from "./transport/file.js";
|
|
3
|
-
import {Console} from "./transport/console.js";
|
|
4
|
-
|
|
5
|
-
export const DEFAULT_LOGGER_TRANSPORTS: () => LoggerTransport[] = () => [
|
|
6
|
-
Console(),
|
|
7
|
-
File()
|
|
8
|
-
];
|
|
9
|
-
|
|
10
|
-
export const DEFAULT_LOGGER_FORMATTER: LoggerFormatter =
|
|
11
|
-
({identifier, level, message}) => `${new Date().toISOString()} ${process.pid} ` +
|
|
12
|
-
`${identifier ? `[${identifier}] ` : ""}` +
|
|
13
|
-
`${level !== "info" ? (level === "error" || level === "warn" ? `[${level.toUpperCase()}] ` : `[${level}] `) : ""}` +
|
|
14
|
-
`${message}`;
|
|
15
|
-
|
|
16
|
-
export const LOG_LEVEL_MAP =
|
|
17
|
-
{
|
|
18
|
-
"error": 1,
|
|
19
|
-
"warn": 2,
|
|
20
|
-
"info": 3,
|
|
21
|
-
"debug": 4,
|
|
22
|
-
"trace": 5,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const LoggerEvents = {
|
|
26
|
-
error: "error"
|
|
27
|
-
};
|