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.
Files changed (40) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +202 -0
  3. package/build/Release/hypernode.node +0 -0
  4. package/dist/http/chunker/ChunkParser.d.ts +8 -0
  5. package/dist/http/chunker/ChunkParser.js +2 -0
  6. package/dist/http/chunker/ChunkProgression.d.ts +25 -0
  7. package/dist/http/chunker/ChunkProgression.js +55 -0
  8. package/dist/http/chunker/FixedChunkedParser.d.ts +14 -0
  9. package/dist/http/chunker/FixedChunkedParser.js +49 -0
  10. package/dist/http/chunker/StreamingChunkedParser.d.ts +19 -0
  11. package/dist/http/chunker/StreamingChunkedParser.js +94 -0
  12. package/dist/http/chunker/UntilEndChunkerParser.d.ts +37 -0
  13. package/dist/http/chunker/UntilEndChunkerParser.js +64 -0
  14. package/dist/http/content/encoding.d.ts +3 -0
  15. package/dist/http/content/encoding.js +38 -0
  16. package/dist/http/content/parser.d.ts +1 -0
  17. package/dist/http/content/parser.js +64 -0
  18. package/dist/http/context/ApiContext.d.ts +14 -0
  19. package/dist/http/context/ApiContext.js +151 -0
  20. package/dist/http/context/HttpContext.d.ts +42 -0
  21. package/dist/http/context/HttpContext.js +231 -0
  22. package/dist/http/context/WebContext.d.ts +31 -0
  23. package/dist/http/context/WebContext.js +320 -0
  24. package/dist/http/factory/accumulator.d.ts +13 -0
  25. package/dist/http/factory/accumulator.js +221 -0
  26. package/dist/http/factory/factory.d.ts +5 -0
  27. package/dist/http/factory/factory.js +97 -0
  28. package/dist/http/factory/pipeline.d.ts +3 -0
  29. package/dist/http/factory/pipeline.js +102 -0
  30. package/dist/http/response/HttpResponseBase.d.ts +10 -0
  31. package/dist/http/response/HttpResponseBase.js +2 -0
  32. package/dist/http/response/PipeResponseBase.d.ts +31 -0
  33. package/dist/http/response/PipeResponseBase.js +115 -0
  34. package/dist/http.d.ts +567 -0
  35. package/dist/http.js +59 -0
  36. package/dist/hypernode.d.ts +31 -0
  37. package/dist/hypernode.js +8 -0
  38. package/dist/index.d.ts +9 -0
  39. package/dist/index.js +39 -0
  40. package/package.json +63 -0
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contentParserTable = void 0;
4
+ function formParse(b) {
5
+ // application/x-www-form-urlencoded
6
+ const s = b.toString("utf8");
7
+ const out = {};
8
+ for (const pair of s.split("&")) {
9
+ const [k, v] = pair.split("=");
10
+ if (!k)
11
+ continue;
12
+ out[decodeURIComponent(k)] = decodeURIComponent(v || "");
13
+ }
14
+ return out;
15
+ }
16
+ function multipartParser(b) {
17
+ const raw = b.toString("utf8");
18
+ const boundaryMatch = raw.match(/boundary=([^\s;]+)/);
19
+ if (!boundaryMatch) {
20
+ return {};
21
+ }
22
+ const boundary = `--${boundaryMatch[1]}`;
23
+ const parts = raw.split(boundary).slice(1, -1);
24
+ const result = {};
25
+ for (let part of parts) {
26
+ part = part.trim();
27
+ if (!part)
28
+ continue;
29
+ const [rawHeaders, ...bodyParts] = part.split("\r\n\r\n");
30
+ const body = bodyParts.join("\r\n\r\n");
31
+ const headers = rawHeaders.split("\r\n");
32
+ let name = "";
33
+ let filename = "";
34
+ for (const h of headers) {
35
+ const m = h.match(/name="([^"]+)"/);
36
+ if (m)
37
+ name = m[1];
38
+ const f = h.match(/filename="([^"]+)"/);
39
+ if (f)
40
+ filename = f[1];
41
+ }
42
+ if (!name)
43
+ continue;
44
+ // File part
45
+ if (filename) {
46
+ result[name] = {
47
+ filename,
48
+ data: Buffer.from(body, "utf8")
49
+ };
50
+ }
51
+ else {
52
+ // Text field
53
+ result[name] = body.trim();
54
+ }
55
+ }
56
+ return result;
57
+ }
58
+ const decoder = new TextDecoder('utf-8');
59
+ exports.contentParserTable = {
60
+ "application/json": JSON.parse,
61
+ "application/x-www-form-urlencoded": formParse,
62
+ "multipart/form-data": multipartParser,
63
+ "text/plain": (b) => decoder.decode
64
+ };
@@ -0,0 +1,14 @@
1
+ import { Http } from "../../http";
2
+ import HttpContext from "./HttpContext";
3
+ declare class ApiContext extends HttpContext {
4
+ protected contentDecoding: Http.ContentDecoding;
5
+ protected contentEncoding: Http.ContentEncoding;
6
+ protected contentTypeParsers: Record<string, (b: any) => any>;
7
+ constructor(opts?: Http.ServerOptions);
8
+ private bindServer;
9
+ protected parseInitial: Http.ParseInitialFn;
10
+ protected parseHeader: Http.ParseInitialFn;
11
+ registerRouters(mainRoute: Http.Route): void;
12
+ setHttpCore(): void;
13
+ }
14
+ export default ApiContext;
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const parser_1 = require("../content/parser");
7
+ const encoding_1 = require("../content/encoding");
8
+ const http_1 = require("../../http");
9
+ const HttpContext_1 = __importDefault(require("./HttpContext"));
10
+ const net_1 = __importDefault(require("net"));
11
+ class ApiContext extends HttpContext_1.default {
12
+ constructor(opts) {
13
+ super(opts);
14
+ this.contentDecoding = encoding_1.contentDecodingTable;
15
+ this.contentEncoding = encoding_1.contentEncodingTable;
16
+ this.contentTypeParsers = parser_1.contentParserTable;
17
+ this.parseInitial = (socket, chunk, p) => {
18
+ const routeId = this.httpCore.scannerRouteFirst(chunk, p, this.state.maxHeaderNameSize, this.state.maxHeaderValueSize, this.state.maxHeaderSize, this.state.requestQuerySize);
19
+ if (p.retFlag !== http_1.Http.RetFlagBits.FLAG_OK) {
20
+ switch (p.retFlag) {
21
+ // --- CORS 204 ---
22
+ case http_1.Http.RetFlagBits.FLAG_CORS_PREFLIGHT:
23
+ socket.write(Buffer.from("HTTP/1.1 204 No Content\r\n" +
24
+ this.state.corsHeaders + "\r\n" +
25
+ "Content-Length: 0\r\n\r\n"));
26
+ socket.destroy();
27
+ return;
28
+ // --- VERSION UNSUPPORTED ---
29
+ case http_1.Http.RetFlagBits.FLAG_HTTP_VERSION_UNSUPPORTED:
30
+ socket.write(this.errorRespMap.RESP_505);
31
+ socket.destroy();
32
+ return;
33
+ // --- METHOD NOT ALLOWED ---
34
+ case http_1.Http.RetFlagBits.FLAG_METHOD_NOT_ALLOWED:
35
+ socket.write(this.errorRespMap.RESP_405);
36
+ socket.destroy();
37
+ return;
38
+ // --- REQUEST QUERY EXCEEDED ---
39
+ case http_1.Http.RetFlagBits.FLAG_REQUEST_QUERY_EXCEEDED:
40
+ socket.write(this.errorRespMap.RESP_414);
41
+ socket.destroySoon();
42
+ return;
43
+ // --- NOT FOUND ---
44
+ case http_1.Http.RetFlagBits.FLAG_NOT_FOUND:
45
+ if (this.enableCors) {
46
+ socket.write(this.errorRespMap.RESP_204);
47
+ }
48
+ else {
49
+ socket.write(this.errorRespMap.RESP_404);
50
+ }
51
+ socket.destroy();
52
+ return;
53
+ // === HEADER ERRORS ===
54
+ case http_1.Http.RetFlagBits.FLAG_INVALID_ARGUMENT:
55
+ case http_1.Http.RetFlagBits.FLAG_INVALID_HEADER:
56
+ case http_1.Http.RetFlagBits.FLAG_INVALID_CONTENT_LENGTH:
57
+ case http_1.Http.RetFlagBits.FLAG_CONTENT_LENGTH_EXCEEDED:
58
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_SIZE:
59
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_NAME_SIZE:
60
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_VALUE_SIZE:
61
+ case http_1.Http.RetFlagBits.FLAG_DUPLICATE_SINGLE_HEADER:
62
+ socket.write(this.errorRespMap.RESP_400);
63
+ socket.destroy();
64
+ return;
65
+ case http_1.Http.RetFlagBits.FLAG_UNTERMINATED_HEADERS:
66
+ p.rawBuf = chunk;
67
+ p.routePipe = this.routePipes[routeId];
68
+ p.fn = this.parseHeader;
69
+ return;
70
+ // --- OTHER (fallback) ---
71
+ default:
72
+ socket.write(this.errorRespMap.RESP_400);
73
+ socket.destroy();
74
+ return;
75
+ }
76
+ }
77
+ p.rawBuf = chunk;
78
+ const h = p.headers;
79
+ const hostHeader = h.host;
80
+ if (!hostHeader) {
81
+ socket.write(this.errorRespMap.RESP_400);
82
+ socket.destroy();
83
+ return;
84
+ }
85
+ p.routePipe = this.routePipes[routeId];
86
+ p.routePipe.accumulateHandler(socket, p);
87
+ return;
88
+ };
89
+ this.parseHeader = (socket, chunk, p) => {
90
+ p.rawBuf = Buffer.concat([p.rawBuf, chunk]);
91
+ this.httpCore.scannerHeader(p.rawBuf, p, this.state.maxHeaderNameSize, this.state.maxHeaderValueSize, this.state.maxHeaderSize);
92
+ if (p.retFlag !== http_1.Http.RetFlagBits.FLAG_OK) {
93
+ switch (p.retFlag) {
94
+ case http_1.Http.RetFlagBits.FLAG_INVALID_ARGUMENT:
95
+ case http_1.Http.RetFlagBits.FLAG_INVALID_HEADER:
96
+ case http_1.Http.RetFlagBits.FLAG_INVALID_CONTENT_LENGTH:
97
+ case http_1.Http.RetFlagBits.FLAG_CONTENT_LENGTH_EXCEEDED:
98
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_SIZE:
99
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_NAME_SIZE:
100
+ case http_1.Http.RetFlagBits.FLAG_MAX_HEADER_VALUE_SIZE:
101
+ socket.write(this.errorRespMap.RESP_400);
102
+ socket.destroySoon();
103
+ return;
104
+ case http_1.Http.RetFlagBits.FLAG_UNTERMINATED_HEADERS:
105
+ return;
106
+ }
107
+ }
108
+ const h = p.headers;
109
+ const hostHeader = h.Host ?? h.host ?? h.HOST;
110
+ if (!hostHeader) {
111
+ socket.write(this.errorRespMap.RESP_400);
112
+ return socket.destroy();
113
+ }
114
+ p.routePipe.accumulateHandler(socket, p);
115
+ return;
116
+ };
117
+ this.initRuntime();
118
+ this.bindServer(opts?.netServerOptions);
119
+ }
120
+ bindServer(netServerOptions) {
121
+ this.server = new net_1.default.Server(netServerOptions, (socket) => {
122
+ let p = this.chunkPool.allocate();
123
+ if (!p) {
124
+ socket.destroy();
125
+ return;
126
+ }
127
+ socket.setTimeout(this.state.timeout);
128
+ // socket.setNoDelay(true);
129
+ // socket.setKeepAlive(true, 60000);
130
+ socket.on("data", chunk => {
131
+ p.fn(socket, chunk, p);
132
+ });
133
+ socket.on("timeout", () => {
134
+ socket.destroy();
135
+ });
136
+ socket.on("error", (err) => {
137
+ socket.destroy();
138
+ });
139
+ socket.on("close", () => {
140
+ p.free();
141
+ });
142
+ });
143
+ }
144
+ registerRouters(mainRoute) {
145
+ super.registerRouters(mainRoute);
146
+ }
147
+ setHttpCore() {
148
+ super.setHttpCore("api");
149
+ }
150
+ }
151
+ exports.default = ApiContext;
@@ -0,0 +1,42 @@
1
+ import { Http } from "../../http";
2
+ import { type IHttpCore, type ICPool } from "../../hypernode";
3
+ import net from "net";
4
+ import { createAccumulators } from "../factory/accumulator";
5
+ declare abstract class HttpContext {
6
+ protected MODE: "web" | "api";
7
+ protected abstract contentDecoding: Http.ContentDecoding;
8
+ protected abstract contentEncoding: Http.ContentEncoding;
9
+ protected abstract contentTypeParsers: Http.ContentTypeParser;
10
+ protected errorRespMap: Http.HttpStaticResponseMap;
11
+ server: net.Server;
12
+ protected chunkPool: ICPool;
13
+ protected respPool: ICPool;
14
+ protected routePipes: Http.RoutePipe[];
15
+ protected accumulators: ReturnType<typeof createAccumulators>;
16
+ protected abstract parseInitial: Http.ParseInitialFn;
17
+ protected state: Http.ServerState;
18
+ protected enableCors: boolean;
19
+ protected httpCore: IHttpCore;
20
+ protected setHttpCore(mode: "web" | "api"): void;
21
+ private bootstrapPoolChunkProgressionFn?;
22
+ constructor(opts?: Http.ServerOptions);
23
+ private setRegisterResp;
24
+ private setRegisterChunkProgression;
25
+ protected initRuntime(): void;
26
+ protected registerRouters(mainRoute: Http.Route): void;
27
+ private buildRoute;
28
+ enabledCors(cfg: Http.CorsConfig): void;
29
+ setTimeout(timeout: number): void;
30
+ setRequestQuerySize(requestQuerySize: number): void;
31
+ setMaxHeaderNameSize(maxHeaderNameSize: number): void;
32
+ setMaxHeaderValueSize(maxHeaderValueSize: number): void;
33
+ setMaxContentSize(maxContentSize: number): void;
34
+ listen(port?: number | undefined, hostname?: string | undefined, backlog?: number | undefined, listeningListener?: (() => void) | undefined): this;
35
+ setMaxRequests(n: number): boolean;
36
+ getTimeout(): number;
37
+ getRequestQuerySize(): number;
38
+ getMaxHeaderNameSize(): number;
39
+ getMaxHeaderValueSize(): number;
40
+ getMaxContentSize(): number;
41
+ }
42
+ export default HttpContext;
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const http_1 = require("../../http");
7
+ const hypernode_1 = require("../../hypernode");
8
+ const pipeline_1 = require("../factory/pipeline");
9
+ const PipeResponseBase_1 = require("../response/PipeResponseBase");
10
+ const ChunkProgression_1 = __importDefault(require("../chunker/ChunkProgression"));
11
+ const accumulator_1 = require("../factory/accumulator");
12
+ class HttpContext {
13
+ setHttpCore(mode) {
14
+ this.MODE = mode;
15
+ this.httpCore = new hypernode_1.hypernode.HttpCore();
16
+ }
17
+ constructor(opts) {
18
+ this.errorRespMap = {
19
+ RESP_505: Buffer.from("HTTP/1.1 505 HTTP Version Not Supported\r\nContent-Length: 0\r\n\r\n"),
20
+ RESP_405: Buffer.from("HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n"),
21
+ RESP_400: Buffer.from("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"),
22
+ RESP_404: Buffer.from("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"),
23
+ RESP_413: Buffer.from("HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\n\r\n"),
24
+ RESP_414: Buffer.from("HTTP/1.1 414 Request-URI Too Large\r\nContent-Length: 0\r\n\r\n"),
25
+ RESP_204: Buffer.from("HTTP/1.1 204 No Content\r\n\r\n")
26
+ };
27
+ this.enableCors = false;
28
+ this.state = {
29
+ corsHeaders: "",
30
+ maxHeaderSize: opts?.maxHeaderSize || 10 * 1024,
31
+ maxHeaderNameSize: opts?.maxHeaderNameSize || 512,
32
+ maxHeaderValueSize: opts?.maxHeaderValueSize || 1024,
33
+ maxContentSize: opts?.maxContentSize || 3 * 1024 * 1024,
34
+ requestQuerySize: opts?.requestQuerySize || 2048,
35
+ timeout: opts?.timeout || 3000,
36
+ untilEnd: opts?.untilEnd || false,
37
+ maxRequests: opts?.maxRequests || 5000,
38
+ ResponseCtor: opts?.ResponseCtor || PipeResponseBase_1.PipeResponseBase
39
+ };
40
+ if (opts?.bootstrapPoolChunkProgression) {
41
+ // @ts-ignore
42
+ this.bootstrapPoolChunkProgressionFn = opts?.bootstrapPoolChunkProgression;
43
+ }
44
+ }
45
+ setRegisterResp(n, cPool) {
46
+ for (let i = 0; i < n; i++) {
47
+ let cObj = new this.state.ResponseCtor();
48
+ let objId = cPool.registerObj(cObj);
49
+ cObj.setCPool(cPool, objId);
50
+ }
51
+ }
52
+ setRegisterChunkProgression(n, cPool, respCPool) {
53
+ const objs = [];
54
+ for (let i = 0; i < n; i++) {
55
+ const cpObj = new ChunkProgression_1.default(cPool, this.parseInitial, respCPool);
56
+ if (this.bootstrapPoolChunkProgressionFn) {
57
+ this.bootstrapPoolChunkProgressionFn(cpObj);
58
+ }
59
+ objs.push(cpObj);
60
+ }
61
+ return objs;
62
+ }
63
+ initRuntime() {
64
+ this.respPool = new hypernode_1.hypernode.CPool();
65
+ this.respPool.initializePool(this.state.maxRequests);
66
+ this.setRegisterResp(this.state.maxRequests, this.respPool);
67
+ this.chunkPool = new hypernode_1.hypernode.CPool();
68
+ this.chunkPool.initializePool(this.state.maxRequests);
69
+ this.setRegisterChunkProgression(this.state.maxRequests, this.chunkPool, this.respPool);
70
+ }
71
+ registerRouters(mainRoute) {
72
+ this.accumulators = (0, accumulator_1.createAccumulators)({
73
+ contentDecoding: this.contentDecoding,
74
+ contentTypeParsers: this.contentTypeParsers,
75
+ errorRespMap: this.errorRespMap
76
+ });
77
+ this.buildRoute(mainRoute, this.state);
78
+ }
79
+ buildRoute(_Route, state) {
80
+ let buildedRoutes = [];
81
+ let routePipes = [];
82
+ let accumulators = this.accumulators;
83
+ function getMethodStr(method) {
84
+ switch (method) {
85
+ case http_1.Http.HttpMethod.GET: return "GET";
86
+ case http_1.Http.HttpMethod.POST: return "POST";
87
+ case http_1.Http.HttpMethod.PUT: return "PUT";
88
+ case http_1.Http.HttpMethod.PATCH: return "PATCH";
89
+ case http_1.Http.HttpMethod.DELETE: return "DELETE";
90
+ case http_1.Http.HttpMethod.OPTIONS: return "OPTIONS";
91
+ case http_1.Http.HttpMethod.HEAD: return "HEAD";
92
+ default: return "UNKNOWN";
93
+ }
94
+ }
95
+ function normalizeRoutePattern(route) {
96
+ let normalized = route.replace(/:([^\/]*)/g, ':');
97
+ if (normalized.endsWith(':')) {
98
+ normalized += '/';
99
+ }
100
+ return normalized;
101
+ }
102
+ function desicionMaker(ep) {
103
+ if (ep.method === http_1.Http.HttpMethod.GET || ep.method === http_1.Http.HttpMethod.HEAD)
104
+ return accumulators.accumulatorHeadGet;
105
+ if (ep.accumulateHandle)
106
+ return ep.accumulateHandle;
107
+ if (ep.ct) {
108
+ if (ep.ct.encoding && ep.ct.type)
109
+ return accumulators.accumulatorDefinedTypeEncode;
110
+ else if (ep.ct.encoding)
111
+ return accumulators.accumulatorDefinedEncode;
112
+ else if (ep.ct.type)
113
+ return accumulators.accumulatorDefinedType;
114
+ }
115
+ return accumulators.decisionAccumulate;
116
+ }
117
+ function buildSubTree(rootRoute, url, _mws) {
118
+ let mws = [..._mws];
119
+ let mwIdx = 0;
120
+ while (mwIdx < rootRoute.middlewares.length) {
121
+ let mw = rootRoute.middlewares[mwIdx++];
122
+ mws.push(mw);
123
+ }
124
+ let epIdx = 0;
125
+ while (epIdx < rootRoute.endpoints.length) {
126
+ let ep = rootRoute.endpoints[epIdx++];
127
+ let pipeFns = [...mws.map((v) => v.handle), ...ep.middlewares.map((v) => v.handle), ep.handle];
128
+ let accumulateHandler = desicionMaker(ep);
129
+ let mainIndex = routePipes.push({
130
+ url: url + ep.url,
131
+ ct: ep?.ct,
132
+ mws: pipeFns,
133
+ pipeHandler: (0, pipeline_1.createPipeline)(ep, pipeFns),
134
+ ResponseCtor: state.ResponseCtor,
135
+ accumulateHandler: accumulateHandler,
136
+ routeId: epIdx,
137
+ maxContentSize: ep.maxContentSize || state.maxContentSize,
138
+ maxHeaderSize: ep.maxHeaderSize || state.maxHeaderNameSize,
139
+ untilEnd: ep.untilEnd || state.untilEnd,
140
+ }) - 1;
141
+ let bRoute = {
142
+ method: getMethodStr(ep.method),
143
+ route: normalizeRoutePattern(url + ep.url),
144
+ vptrTableIndex: mainIndex
145
+ };
146
+ buildedRoutes.push(bRoute);
147
+ }
148
+ let routeIdx = 0;
149
+ while (routeIdx < rootRoute.routes.length) {
150
+ let childRoute = rootRoute.routes[routeIdx++];
151
+ let childUrl = url + childRoute.url;
152
+ buildSubTree(childRoute, childUrl, mws);
153
+ }
154
+ }
155
+ buildSubTree(_Route, _Route.url, []);
156
+ if (this.httpCore.registerRoutes(buildedRoutes) != buildedRoutes.length)
157
+ throw new Error("Building Route Tree");
158
+ this.routePipes = routePipes;
159
+ }
160
+ enabledCors(cfg) {
161
+ function toHeaderValue(v) {
162
+ if (!v)
163
+ return undefined;
164
+ return Array.isArray(v) ? v.join(",") : v;
165
+ }
166
+ const headers = [];
167
+ const allowedOrigins = cfg.allowedOrigins == true ? "*" : toHeaderValue(cfg.allowedOrigins);
168
+ const allowedMethods = toHeaderValue(cfg.allowedMethods);
169
+ const allowedHeaders = toHeaderValue(cfg.allowedHeaders);
170
+ const exposedHeaders = toHeaderValue(cfg.exposedHeaders);
171
+ const credentials = cfg.credentials;
172
+ const maxAge = cfg.maxAge;
173
+ if (allowedOrigins)
174
+ headers.push(`Access-Control-Allow-Origin: ${allowedOrigins}`);
175
+ if (allowedMethods)
176
+ headers.push(`Access-Control-Allow-Methods: ${allowedMethods}`);
177
+ if (allowedHeaders)
178
+ headers.push(`Access-Control-Allow-Headers: ${allowedHeaders}`);
179
+ if (exposedHeaders)
180
+ headers.push(`Access-Control-Expose-Headers: ${exposedHeaders}`);
181
+ if (credentials !== undefined)
182
+ headers.push(`Access-Control-Allow-Credentials: ${credentials}`);
183
+ if (maxAge !== undefined)
184
+ headers.push(`Access-Control-Max-Age: ${maxAge}`);
185
+ this.state.corsHeaders = headers.join("\n");
186
+ this.errorRespMap.RESP_204 = Buffer.from("HTTP/1.1 404 Not Found\r\n" +
187
+ this.state.corsHeaders + "\r\n" +
188
+ "Content-Length: 0\r\n\r\n");
189
+ this.enableCors = true;
190
+ }
191
+ setTimeout(timeout) {
192
+ timeout > 0 ? this.state.timeout = timeout : null;
193
+ }
194
+ setRequestQuerySize(requestQuerySize) {
195
+ requestQuerySize > 0 ? this.state.requestQuerySize = requestQuerySize : null;
196
+ }
197
+ setMaxHeaderNameSize(maxHeaderNameSize) {
198
+ maxHeaderNameSize > 0 ? this.state.maxHeaderNameSize = maxHeaderNameSize : null;
199
+ }
200
+ setMaxHeaderValueSize(maxHeaderValueSize) {
201
+ maxHeaderValueSize > 0 ? this.state.maxHeaderValueSize = maxHeaderValueSize : null;
202
+ }
203
+ setMaxContentSize(maxContentSize) {
204
+ maxContentSize > 0 ? this.state.maxContentSize = maxContentSize : null;
205
+ }
206
+ listen(port, hostname, backlog, listeningListener) {
207
+ this.server.listen(port, hostname, backlog, listeningListener);
208
+ return this;
209
+ }
210
+ setMaxRequests(n) {
211
+ if (n < 1) {
212
+ return false;
213
+ }
214
+ this.state.maxRequests = n;
215
+ try {
216
+ this.chunkPool.resizePool(n);
217
+ this.respPool.resizePool(n);
218
+ }
219
+ catch (error) {
220
+ console.error(error);
221
+ return false;
222
+ }
223
+ return true;
224
+ }
225
+ getTimeout() { return this.state.timeout; }
226
+ getRequestQuerySize() { return this.state.requestQuerySize; }
227
+ getMaxHeaderNameSize() { return this.state.maxHeaderNameSize; }
228
+ getMaxHeaderValueSize() { return this.state.maxHeaderValueSize; }
229
+ getMaxContentSize() { return this.state.maxContentSize; }
230
+ }
231
+ exports.default = HttpContext;
@@ -0,0 +1,31 @@
1
+ import { Http } from "../../http";
2
+ import HttpContext from "./HttpContext";
3
+ import net from "net";
4
+ type RouteDefinationFn = (socket: net.Socket, p: Http.ChunkProgression, routeId: number, chunk: Buffer<ArrayBufferLike>) => void;
5
+ declare class WebContext extends HttpContext {
6
+ protected contentDecoding: Http.ContentDecoding;
7
+ protected contentEncoding: Http.ContentEncoding;
8
+ protected contentTypeParsers: Record<string, (b: any) => any>;
9
+ private assetCache;
10
+ private assetParser;
11
+ protected spaRootPath: string;
12
+ protected spaRespBuffer: Buffer;
13
+ private publicRoutePathName;
14
+ private publicStaticRoute;
15
+ private routeDefinationFns;
16
+ constructor(ctxOpts: Http.WebContextState, opts?: Http.ServerOptions);
17
+ private bindServer;
18
+ protected parseInitial: Http.ParseInitialFn;
19
+ protected publicRouteDefinationFn: RouteDefinationFn;
20
+ protected spaRouteDefinationFn: RouteDefinationFn;
21
+ protected dynamicRouteDefinationFn: RouteDefinationFn;
22
+ protected parseHeader: Http.ParseInitialFn;
23
+ registerRouters(mainRoute: Http.Route | undefined): void;
24
+ private makeRouteSPA;
25
+ private makeRoutePublic;
26
+ private setRouteDefinationFn;
27
+ protected setAllAssets(): void;
28
+ private loadAsset;
29
+ setHttpCore(): void;
30
+ }
31
+ export default WebContext;