fulmine.js 5.19.2 → 5.19.4

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.
@@ -0,0 +1,88 @@
1
+ /*
2
+ Copyright 2024 dimden.dev
3
+ Copyright 2026 Nigro Simone
4
+
5
+ This file is derived from Ultimate Express and has been modified.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+ */
19
+
20
+ const http = require("http");
21
+ const statuses = require("statuses");
22
+ const { EventEmitter } = require("events");
23
+
24
+ const outgoingMessage = new http.OutgoingMessage();
25
+ const symbols = Object.getOwnPropertySymbols(outgoingMessage);
26
+ // if a future node renames it, fall back to a private symbol rather than writing a property
27
+ // literally named "undefined", which is what indexing with undefined would do
28
+ const kOutHeaders = symbols.find((s) => s.toString() === "Symbol(kOutHeaders)") ?? Symbol("kOutHeaders");
29
+ // node's emitters tombstone a removed listener's slot instead of deleting it when this flag is set,
30
+ // which keeps _events in a stable shape. EventEmitter.init sets it and never runs for the lazily
31
+ // materialized response, so it is set by hand. A future rename degrades to the delete
32
+ const kShapeMode =
33
+ Object.getOwnPropertySymbols(new EventEmitter()).find((s) => s.toString() === "Symbol(shapeMode)") ??
34
+ Symbol("shapeMode");
35
+ // names setHeader has validated and lowercased, so the constant names middleware writes per
36
+ // request are one Map hit. Insert-only after validation, bounded; only setHeader may insert,
37
+ // the never-throwing readers keep their plain toLowerCase
38
+ const VALIDATED_HEADER_NAMES = new Map();
39
+ // The names and values that recur on every response, kept as Buffers for the uWS crossing: a Buffer
40
+ // is memcpy'd as it is, a string pays a UTF-8 scan and copy per call. A header that is not here
41
+ // crosses as the string it was. Names must stay lowercase, as writeHeaders receives them.
42
+ const HEADER_NAME_BUF = { __proto__: null };
43
+ const HEADER_VALUE_BUF = { __proto__: null };
44
+ for (const s of ["connection", "keep-alive", "content-type", "vary", "x-powered-by", "content-encoding"]) {
45
+ HEADER_NAME_BUF[s] = Buffer.from(s);
46
+ }
47
+ for (const s of [
48
+ "keep-alive",
49
+ "timeout=10",
50
+ "close",
51
+ "Fulmine",
52
+ "Accept-Encoding",
53
+ "text/html; charset=utf-8",
54
+ "text/plain; charset=utf-8",
55
+ "application/json; charset=utf-8",
56
+ "application/octet-stream",
57
+ "gzip",
58
+ "br",
59
+ "deflate",
60
+ "zstd"
61
+ ]) {
62
+ HEADER_VALUE_BUF[s] = Buffer.from(s);
63
+ }
64
+
65
+ // One status line per code, built on first use: the default path, with no custom reason phrase,
66
+ // paid a template string and a trim per request for a line that never changes. Bounded to real
67
+ // HTTP codes so a wild writeHead value cannot grow the array or flip it into dictionary mode.
68
+ const STATUS_LINES = [];
69
+ /**
70
+ * @param {number} code
71
+ * @param {string|undefined} text an explicit reason phrase, which bypasses the cache
72
+ * @returns {string} the uWS status line, e.g. "200 OK"
73
+ */
74
+ function statusLine(code, text) {
75
+ if (text === undefined && Number.isInteger(code) && code >= 100 && code <= 999) {
76
+ return STATUS_LINES[code] ?? (STATUS_LINES[code] = code + " " + (statuses.message[code] ?? "unknown"));
77
+ }
78
+ return `${code} ${text ?? statuses.message[code] ?? "unknown"}`.trim();
79
+ }
80
+
81
+ module.exports = {
82
+ kOutHeaders,
83
+ kShapeMode,
84
+ VALIDATED_HEADER_NAMES,
85
+ HEADER_NAME_BUF,
86
+ HEADER_VALUE_BUF,
87
+ statusLine
88
+ };