corecdtl 0.1.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/LICENSE.md +21 -0
- package/README.md +202 -0
- package/build/Release/hypernode.node +0 -0
- package/dist/http/chunker/ChunkParser.d.ts +8 -0
- package/dist/http/chunker/ChunkParser.js +2 -0
- package/dist/http/chunker/ChunkProgression.d.ts +25 -0
- package/dist/http/chunker/ChunkProgression.js +55 -0
- package/dist/http/chunker/FixedChunkedParser.d.ts +14 -0
- package/dist/http/chunker/FixedChunkedParser.js +49 -0
- package/dist/http/chunker/StreamingChunkedParser.d.ts +19 -0
- package/dist/http/chunker/StreamingChunkedParser.js +94 -0
- package/dist/http/chunker/UntilEndChunkerParser.d.ts +37 -0
- package/dist/http/chunker/UntilEndChunkerParser.js +64 -0
- package/dist/http/content/encoding.d.ts +3 -0
- package/dist/http/content/encoding.js +38 -0
- package/dist/http/content/parser.d.ts +1 -0
- package/dist/http/content/parser.js +64 -0
- package/dist/http/context/ApiContext.d.ts +14 -0
- package/dist/http/context/ApiContext.js +151 -0
- package/dist/http/context/HttpContext.d.ts +42 -0
- package/dist/http/context/HttpContext.js +231 -0
- package/dist/http/context/WebContext.d.ts +31 -0
- package/dist/http/context/WebContext.js +320 -0
- package/dist/http/factory/accumulator.d.ts +13 -0
- package/dist/http/factory/accumulator.js +221 -0
- package/dist/http/factory/factory.d.ts +5 -0
- package/dist/http/factory/factory.js +97 -0
- package/dist/http/factory/pipeline.d.ts +3 -0
- package/dist/http/factory/pipeline.js +102 -0
- package/dist/http/response/HttpResponseBase.d.ts +10 -0
- package/dist/http/response/HttpResponseBase.js +2 -0
- package/dist/http/response/PipeResponseBase.d.ts +31 -0
- package/dist/http/response/PipeResponseBase.js +115 -0
- package/dist/http.d.ts +567 -0
- package/dist/http.js +59 -0
- package/dist/hypernode.d.ts +31 -0
- package/dist/hypernode.js +8 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +39 -0
- package/package.json +63 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPipeline = createPipeline;
|
|
4
|
+
const types_1 = require("util/types");
|
|
5
|
+
const http_1 = require("../../http");
|
|
6
|
+
const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
|
|
7
|
+
function createMwsInline(mws) {
|
|
8
|
+
let ret = ``;
|
|
9
|
+
for (let i = 0; i < mws.length; i++) {
|
|
10
|
+
const mw = mws[i];
|
|
11
|
+
if ((0, types_1.isAsyncFunction)(mw)) {
|
|
12
|
+
ret += `!res.finishedFlag && await mws[${i}](req, res);\n`;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
ret += `!res.finishedFlag && mws[${i}](req, res);\n`; // await yok
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return { inlineCode: ret };
|
|
19
|
+
}
|
|
20
|
+
function createPipeline(ep, pipeFns) {
|
|
21
|
+
const hasAsync = pipeFns.some(mw => (0, types_1.isAsyncFunction)(mw));
|
|
22
|
+
const { inlineCode } = createMwsInline(pipeFns);
|
|
23
|
+
const PipelineCtor = hasAsync ? AsyncFunction : Function;
|
|
24
|
+
const setRequestObj = `const req = {
|
|
25
|
+
headers: p.headers,
|
|
26
|
+
params: p.params,
|
|
27
|
+
query: p.query,
|
|
28
|
+
url: ""`;
|
|
29
|
+
// -----------------------
|
|
30
|
+
// GET & HEAD (NO BODY)
|
|
31
|
+
// -----------------------
|
|
32
|
+
if (ep.method === http_1.Http.HttpMethod.GET || ep.method === http_1.Http.HttpMethod.HEAD) {
|
|
33
|
+
return new PipelineCtor("p", "mws", "cb", `
|
|
34
|
+
${setRequestObj}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const res = p.allocateResp();
|
|
38
|
+
|
|
39
|
+
${inlineCode}
|
|
40
|
+
ret = res.getResp();
|
|
41
|
+
res.freeCPool();
|
|
42
|
+
cb(ret);
|
|
43
|
+
`);
|
|
44
|
+
}
|
|
45
|
+
// -----------------------
|
|
46
|
+
// BODY PIPELINE
|
|
47
|
+
// -----------------------
|
|
48
|
+
const content = ep.ct;
|
|
49
|
+
// DECODING
|
|
50
|
+
let decodeStep = "";
|
|
51
|
+
let bVar = "b";
|
|
52
|
+
if (content?.encoding) {
|
|
53
|
+
decodeStep = `
|
|
54
|
+
if (${bVar} != null) {
|
|
55
|
+
${bVar} = contentDecodingTable["${content.encoding}"](${bVar});
|
|
56
|
+
}`;
|
|
57
|
+
}
|
|
58
|
+
else if (content?.encoding === null) {
|
|
59
|
+
decodeStep = `/* no encoding */`;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
decodeStep = `
|
|
63
|
+
if (${bVar} != null && p.headers["content-encoding"]) {
|
|
64
|
+
const enc = p.headers["Content-Encoding"];
|
|
65
|
+
if (contentDecodingTable[enc]) {
|
|
66
|
+
${bVar} = contentDecodingTable[enc](${bVar});
|
|
67
|
+
}
|
|
68
|
+
}`;
|
|
69
|
+
}
|
|
70
|
+
// PARSING
|
|
71
|
+
let bodyParser = "";
|
|
72
|
+
if (content?.type) {
|
|
73
|
+
bodyParser = `
|
|
74
|
+
${bVar} = contentTypeTable["${content.type}"](${bVar});
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
else if (content?.type === null) {
|
|
78
|
+
bodyParser = `/* no content-type parser */`;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
bodyParser = `
|
|
82
|
+
if (${bVar} != null) {
|
|
83
|
+
const ctype = p.headers["content-type"];
|
|
84
|
+
${bVar} = contentTypeTable[ctype]?.(${bVar}) ?? ${bVar};
|
|
85
|
+
}
|
|
86
|
+
`;
|
|
87
|
+
}
|
|
88
|
+
return new PipelineCtor("b", "p", "contentTypeTable", "contentDecodingTable", "mws", "cb", `
|
|
89
|
+
${decodeStep}
|
|
90
|
+
${bodyParser}
|
|
91
|
+
|
|
92
|
+
${setRequestObj},
|
|
93
|
+
body: ${bVar}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const res = p.allocateResp();
|
|
97
|
+
${inlineCode}
|
|
98
|
+
let ret = res.getResp();
|
|
99
|
+
res.freeCPool();
|
|
100
|
+
cb(ret);
|
|
101
|
+
`);
|
|
102
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface IHttpResponseBase {
|
|
2
|
+
setStatus(code: number): this;
|
|
3
|
+
setHeader(key: string, value: string): this;
|
|
4
|
+
setHeaders(obj: Record<string, string>): this;
|
|
5
|
+
send(payload: string): void;
|
|
6
|
+
json(obj: unknown): void;
|
|
7
|
+
redirect(url: string, code?: number): void;
|
|
8
|
+
getResp(): Buffer;
|
|
9
|
+
freeCPool(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IHttpResponseBase } from "./HttpResponseBase";
|
|
2
|
+
/**
|
|
3
|
+
* @class PipeResponseBase
|
|
4
|
+
* @description The base Response class used by Http.
|
|
5
|
+
* This class manages the HTTP response status code, headers, and body.
|
|
6
|
+
* Developers can inherit this class and override methods (e.g., `json`, `getResp`) to create custom response types.
|
|
7
|
+
*/
|
|
8
|
+
export declare class PipeResponseBase implements IHttpResponseBase {
|
|
9
|
+
protected body: string;
|
|
10
|
+
protected status: number;
|
|
11
|
+
protected headers: Record<string, string>;
|
|
12
|
+
protected finishedFlag: boolean;
|
|
13
|
+
protected contentEncodingTable: Record<string, Function>;
|
|
14
|
+
protected compression: "gzip" | "br" | "deflate" | null;
|
|
15
|
+
protected objId: number;
|
|
16
|
+
protected cPool: any;
|
|
17
|
+
constructor();
|
|
18
|
+
setCPool(cPool: any, objId: number): void;
|
|
19
|
+
freeCPool(): void;
|
|
20
|
+
getStatus(): number;
|
|
21
|
+
getHeaders(): Record<string, string>;
|
|
22
|
+
getBody(): string;
|
|
23
|
+
setStatus(code: number): this;
|
|
24
|
+
setHeader(key: string, value: string): this;
|
|
25
|
+
setHeaders(obj: Record<string, string>): this;
|
|
26
|
+
send(payload: string): void;
|
|
27
|
+
json(obj: unknown): void;
|
|
28
|
+
redirect(url: string, code?: number): void;
|
|
29
|
+
setCompression(enc: "gzip" | "br" | "deflate"): this;
|
|
30
|
+
getResp(): Buffer;
|
|
31
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PipeResponseBase = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @class PipeResponseBase
|
|
6
|
+
* @description The base Response class used by Http.
|
|
7
|
+
* This class manages the HTTP response status code, headers, and body.
|
|
8
|
+
* Developers can inherit this class and override methods (e.g., `json`, `getResp`) to create custom response types.
|
|
9
|
+
*/
|
|
10
|
+
class PipeResponseBase {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.body = "";
|
|
13
|
+
this.status = 200;
|
|
14
|
+
this.headers = Object.create(null);
|
|
15
|
+
this.finishedFlag = false;
|
|
16
|
+
this.compression = null;
|
|
17
|
+
this.objId = -1;
|
|
18
|
+
// JIT / inline safety
|
|
19
|
+
Object.defineProperty(this, "getResp", {
|
|
20
|
+
value: this.getResp.bind(this),
|
|
21
|
+
writable: false,
|
|
22
|
+
configurable: false,
|
|
23
|
+
enumerable: false
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/* ===================== */
|
|
27
|
+
/* ===== POOL API ====== */
|
|
28
|
+
/* ===================== */
|
|
29
|
+
setCPool(cPool, objId) {
|
|
30
|
+
this.objId = objId;
|
|
31
|
+
this.cPool = cPool;
|
|
32
|
+
}
|
|
33
|
+
freeCPool() {
|
|
34
|
+
this.body = "";
|
|
35
|
+
this.status = 200;
|
|
36
|
+
this.headers = Object.create(null);
|
|
37
|
+
this.finishedFlag = false;
|
|
38
|
+
this.compression = null;
|
|
39
|
+
this.cPool.free(this.objId);
|
|
40
|
+
}
|
|
41
|
+
/* ===================== */
|
|
42
|
+
/* ===== GETTERS ======= */
|
|
43
|
+
/* ===================== */
|
|
44
|
+
getStatus() {
|
|
45
|
+
return this.status;
|
|
46
|
+
}
|
|
47
|
+
getHeaders() {
|
|
48
|
+
return this.headers;
|
|
49
|
+
}
|
|
50
|
+
getBody() {
|
|
51
|
+
return this.body;
|
|
52
|
+
}
|
|
53
|
+
/* ===================== */
|
|
54
|
+
/* ===== MUTATORS ====== */
|
|
55
|
+
/* ===================== */
|
|
56
|
+
setStatus(code) {
|
|
57
|
+
this.status = code | 0;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
setHeader(key, value) {
|
|
61
|
+
this.headers[key] = value;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
setHeaders(obj) {
|
|
65
|
+
for (const k in obj)
|
|
66
|
+
this.headers[k] = obj[k];
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/* ===================== */
|
|
70
|
+
/* ===== SEND API ====== */
|
|
71
|
+
/* ===================== */
|
|
72
|
+
send(payload) {
|
|
73
|
+
this.body = payload;
|
|
74
|
+
this.finishedFlag = true;
|
|
75
|
+
}
|
|
76
|
+
json(obj) {
|
|
77
|
+
this.setHeader("Content-Type", "application/json");
|
|
78
|
+
this.body = JSON.stringify(obj);
|
|
79
|
+
this.finishedFlag = true;
|
|
80
|
+
}
|
|
81
|
+
redirect(url, code = 302) {
|
|
82
|
+
this.status = code | 0;
|
|
83
|
+
this.headers["Location"] = url;
|
|
84
|
+
this.body = "";
|
|
85
|
+
this.finishedFlag = true;
|
|
86
|
+
}
|
|
87
|
+
setCompression(enc) {
|
|
88
|
+
this.compression = enc;
|
|
89
|
+
this.headers["Content-Encoding"] = enc;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/* ===================== */
|
|
93
|
+
/* ===== FINALIZE ====== */
|
|
94
|
+
/* ===================== */
|
|
95
|
+
getResp() {
|
|
96
|
+
const hdr = { ...this.headers };
|
|
97
|
+
let bodyBuf = Buffer.from(this.body, "utf-8");
|
|
98
|
+
if (this.compression) {
|
|
99
|
+
const fn = this.contentEncodingTable[this.compression];
|
|
100
|
+
if (fn)
|
|
101
|
+
bodyBuf = fn(bodyBuf);
|
|
102
|
+
hdr["Content-Encoding"] = this.compression;
|
|
103
|
+
}
|
|
104
|
+
hdr["Content-Length"] = Buffer.byteLength(bodyBuf).toString();
|
|
105
|
+
let headerStr = `HTTP/1.1 ${this.status}\r\n`;
|
|
106
|
+
for (const k in hdr)
|
|
107
|
+
headerStr += `${k}: ${hdr[k]}\r\n`;
|
|
108
|
+
headerStr += `\r\n`;
|
|
109
|
+
return Buffer.concat([
|
|
110
|
+
Buffer.from(headerStr, "ascii"),
|
|
111
|
+
bodyBuf
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.PipeResponseBase = PipeResponseBase;
|