heliumts 0.9.4 → 0.9.6
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/README.md +5 -1
- package/dist/bin/helium.js +57 -53
- package/dist/bin/helium.js.map +1 -1
- package/dist/client/Router.d.ts +2 -2
- package/dist/client/Router.d.ts.map +1 -1
- package/dist/client/Router.js +32 -29
- package/dist/client/Router.js.map +1 -1
- package/dist/client/cache.d.ts.map +1 -1
- package/dist/client/cache.js +13 -13
- package/dist/client/cache.js.map +1 -1
- package/dist/client/env.js.map +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/routerManifest.d.ts.map +1 -1
- package/dist/client/routerManifest.js +18 -29
- package/dist/client/routerManifest.js.map +1 -1
- package/dist/client/rpcClient.d.ts.map +1 -1
- package/dist/client/rpcClient.js +71 -72
- package/dist/client/rpcClient.js.map +1 -1
- package/dist/client/transitions.d.ts +1 -1
- package/dist/client/transitions.d.ts.map +1 -1
- package/dist/client/useCall.d.ts.map +1 -1
- package/dist/client/useCall.js +4 -1
- package/dist/client/useCall.js.map +1 -1
- package/dist/client/useFetch.d.ts.map +1 -1
- package/dist/client/useFetch.js +48 -92
- package/dist/client/useFetch.js.map +1 -1
- package/dist/runtime/routeOrder.d.ts +3 -0
- package/dist/runtime/routeOrder.d.ts.map +1 -0
- package/dist/runtime/routeOrder.js +15 -0
- package/dist/runtime/routeOrder.js.map +1 -0
- package/dist/server/config.d.ts.map +1 -1
- package/dist/server/config.js +51 -26
- package/dist/server/config.js.map +1 -1
- package/dist/server/defineHTTPRequest.d.ts +1 -1
- package/dist/server/defineHTTPRequest.d.ts.map +1 -1
- package/dist/server/defineHTTPRequest.js.map +1 -1
- package/dist/server/defineWorker.d.ts.map +1 -1
- package/dist/server/defineWorker.js +68 -37
- package/dist/server/defineWorker.js.map +1 -1
- package/dist/server/devServer.d.ts.map +1 -1
- package/dist/server/devServer.js +62 -17
- package/dist/server/devServer.js.map +1 -1
- package/dist/server/httpRouter.d.ts.map +1 -1
- package/dist/server/httpRouter.js +147 -43
- package/dist/server/httpRouter.js.map +1 -1
- package/dist/server/meta.d.ts.map +1 -1
- package/dist/server/meta.js +23 -15
- package/dist/server/meta.js.map +1 -1
- package/dist/server/prodServer.d.ts.map +1 -1
- package/dist/server/prodServer.js +79 -23
- package/dist/server/prodServer.js.map +1 -1
- package/dist/server/rateLimiter.d.ts +9 -0
- package/dist/server/rateLimiter.d.ts.map +1 -1
- package/dist/server/rateLimiter.js +46 -0
- package/dist/server/rateLimiter.js.map +1 -1
- package/dist/server/requestHandler.d.ts +4 -0
- package/dist/server/requestHandler.d.ts.map +1 -0
- package/dist/server/requestHandler.js +20 -0
- package/dist/server/requestHandler.js.map +1 -0
- package/dist/server/rpcRegistry.d.ts.map +1 -1
- package/dist/server/rpcRegistry.js +46 -6
- package/dist/server/rpcRegistry.js.map +1 -1
- package/dist/server/security.d.ts.map +1 -1
- package/dist/server/security.js +3 -2
- package/dist/server/security.js.map +1 -1
- package/dist/server/serializer.js +11 -6
- package/dist/server/serializer.js.map +1 -1
- package/dist/server/ssr.d.ts +2 -1
- package/dist/server/ssr.d.ts.map +1 -1
- package/dist/server/ssr.js +29 -32
- package/dist/server/ssr.js.map +1 -1
- package/dist/utils/envLoader.js +1 -1
- package/dist/utils/envLoader.js.map +1 -1
- package/dist/utils/html.d.ts +7 -0
- package/dist/utils/html.d.ts.map +1 -0
- package/dist/utils/html.js +197 -0
- package/dist/utils/html.js.map +1 -0
- package/dist/utils/ipExtractor.d.ts +6 -52
- package/dist/utils/ipExtractor.d.ts.map +1 -1
- package/dist/utils/ipExtractor.js +21 -118
- package/dist/utils/ipExtractor.js.map +1 -1
- package/dist/vite/heliumPlugin.d.ts.map +1 -1
- package/dist/vite/heliumPlugin.js +71 -83
- package/dist/vite/heliumPlugin.js.map +1 -1
- package/dist/vite/scanner.d.ts +3 -0
- package/dist/vite/scanner.d.ts.map +1 -1
- package/dist/vite/scanner.js +89 -18
- package/dist/vite/scanner.js.map +1 -1
- package/dist/vite/ssg.d.ts +4 -0
- package/dist/vite/ssg.d.ts.map +1 -1
- package/dist/vite/ssg.js +62 -39
- package/dist/vite/ssg.js.map +1 -1
- package/dist/vite/virtualServerModule.d.ts +1 -1
- package/dist/vite/virtualServerModule.d.ts.map +1 -1
- package/dist/vite/virtualServerModule.js +28 -16
- package/dist/vite/virtualServerModule.js.map +1 -1
- package/package.json +12 -7
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const RAW_TEXT_TAGS = new Set(["script", "style", "textarea", "title", "xmp", "iframe", "noembed", "noframes"]);
|
|
2
|
+
const VOID_TAGS = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
|
|
3
|
+
function tagEnd(html, start) {
|
|
4
|
+
let quote = "";
|
|
5
|
+
for (let index = start; index < html.length; index++) {
|
|
6
|
+
const character = html[index];
|
|
7
|
+
if (quote) {
|
|
8
|
+
if (character === quote) {
|
|
9
|
+
quote = "";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
else if (character === '"' || character === "'") {
|
|
13
|
+
quote = character;
|
|
14
|
+
}
|
|
15
|
+
else if (character === ">") {
|
|
16
|
+
return index + 1;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return -1;
|
|
20
|
+
}
|
|
21
|
+
function readAttributes(html, start, end) {
|
|
22
|
+
const attributes = [];
|
|
23
|
+
let index = start;
|
|
24
|
+
while (index < end - 1) {
|
|
25
|
+
while (/\s/.test(html[index] ?? "")) {
|
|
26
|
+
index++;
|
|
27
|
+
}
|
|
28
|
+
if (index >= end - 1) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
if (html[index] === "/") {
|
|
32
|
+
index++;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const attributeStart = index;
|
|
36
|
+
while (index < end - 1 && !/[\s=/>]/.test(html[index])) {
|
|
37
|
+
index++;
|
|
38
|
+
}
|
|
39
|
+
const name = html.slice(attributeStart, index).toLowerCase();
|
|
40
|
+
let attributeEnd = index;
|
|
41
|
+
while (index < end - 1 && /\s/.test(html[index])) {
|
|
42
|
+
index++;
|
|
43
|
+
}
|
|
44
|
+
let value = "";
|
|
45
|
+
if (html[index] === "=") {
|
|
46
|
+
index++;
|
|
47
|
+
while (index < end - 1 && /\s/.test(html[index])) {
|
|
48
|
+
index++;
|
|
49
|
+
}
|
|
50
|
+
const quote = html[index] === '"' || html[index] === "'" ? html[index++] : undefined;
|
|
51
|
+
const valueStart = index;
|
|
52
|
+
if (quote) {
|
|
53
|
+
while (index < end - 1 && html[index] !== quote) {
|
|
54
|
+
index++;
|
|
55
|
+
}
|
|
56
|
+
value = html.slice(valueStart, index);
|
|
57
|
+
if (html[index] === quote) {
|
|
58
|
+
index++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
while (index < end - 1 && !/\s/.test(html[index])) {
|
|
63
|
+
index++;
|
|
64
|
+
}
|
|
65
|
+
value = html.slice(valueStart, index);
|
|
66
|
+
}
|
|
67
|
+
attributeEnd = index;
|
|
68
|
+
}
|
|
69
|
+
if (name) {
|
|
70
|
+
attributes.push({ name, value, start: attributeStart, end: attributeEnd });
|
|
71
|
+
}
|
|
72
|
+
// Make progress even for malformed attributes.
|
|
73
|
+
if (index === attributeStart) {
|
|
74
|
+
index++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return attributes;
|
|
78
|
+
}
|
|
79
|
+
function* tags(html) {
|
|
80
|
+
const lowerHtml = html.toLowerCase();
|
|
81
|
+
let index = 0;
|
|
82
|
+
while (index < html.length) {
|
|
83
|
+
const start = html.indexOf("<", index);
|
|
84
|
+
if (start === -1) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (html.startsWith("<!--", start)) {
|
|
88
|
+
const end = html.indexOf("-->", start + 4);
|
|
89
|
+
index = end === -1 ? html.length : end + 3;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (html.startsWith("<![CDATA[", start)) {
|
|
93
|
+
const end = html.indexOf("]]>", start + 9);
|
|
94
|
+
index = end === -1 ? html.length : end + 3;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const match = /^<(\/)?([a-z][a-z0-9:-]*)(?=[\s/>])/i.exec(html.slice(start));
|
|
98
|
+
if (!match) {
|
|
99
|
+
if (html[start + 1] === "!" || html[start + 1] === "?") {
|
|
100
|
+
const end = tagEnd(html, start + 2);
|
|
101
|
+
index = end === -1 ? html.length : end;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
index = start + 1;
|
|
105
|
+
}
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const end = tagEnd(html, start + match[0].length);
|
|
109
|
+
if (end === -1) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const name = match[2].toLowerCase();
|
|
113
|
+
const closing = Boolean(match[1]);
|
|
114
|
+
yield { name, closing, start, end, attributes: closing ? [] : readAttributes(html, start + match[0].length, end) };
|
|
115
|
+
index = end;
|
|
116
|
+
if (!closing && name === "plaintext") {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!closing && RAW_TEXT_TAGS.has(name)) {
|
|
120
|
+
// HTML raw text ends at its closing tag, regardless of JavaScript or
|
|
121
|
+
// CSS quoting. Markup-looking strings inside it are not elements.
|
|
122
|
+
let close = lowerHtml.indexOf(`</${name}`, index);
|
|
123
|
+
while (close !== -1 && !/[\s/>]/.test(html[close + name.length + 2] ?? "")) {
|
|
124
|
+
close = lowerHtml.indexOf(`</${name}`, close + 2);
|
|
125
|
+
}
|
|
126
|
+
index = close === -1 ? html.length : close;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function escapeAttribute(value) {
|
|
131
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
132
|
+
}
|
|
133
|
+
function patchOpeningTag(html, tag, attributes) {
|
|
134
|
+
let opening = html.slice(tag.start, tag.end);
|
|
135
|
+
const updates = new Map(Object.entries(attributes).map(([name, value]) => [name.toLowerCase(), value]));
|
|
136
|
+
const replacements = [];
|
|
137
|
+
const handled = new Set();
|
|
138
|
+
for (const attribute of tag.attributes) {
|
|
139
|
+
if (!updates.has(attribute.name)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const value = updates.get(attribute.name);
|
|
143
|
+
replacements.push({
|
|
144
|
+
start: attribute.start - tag.start,
|
|
145
|
+
end: attribute.end - tag.start,
|
|
146
|
+
value: value === null || handled.has(attribute.name) ? "" : `${attribute.name}="${escapeAttribute(value)}"`,
|
|
147
|
+
});
|
|
148
|
+
handled.add(attribute.name);
|
|
149
|
+
}
|
|
150
|
+
for (const replacement of replacements.reverse()) {
|
|
151
|
+
opening = opening.slice(0, replacement.start) + replacement.value + opening.slice(replacement.end);
|
|
152
|
+
}
|
|
153
|
+
const additions = [...updates]
|
|
154
|
+
.filter(([name, value]) => !handled.has(name) && value !== null)
|
|
155
|
+
.map(([name, value]) => ` ${name}="${escapeAttribute(value)}"`)
|
|
156
|
+
.join("");
|
|
157
|
+
if (additions) {
|
|
158
|
+
const slash = /\/\s*>$/.exec(opening)?.index;
|
|
159
|
+
const slashBelongsToValue = html[tag.end - 2] === "/" && tag.attributes.some((attribute) => attribute.end === tag.end - 1);
|
|
160
|
+
const insertion = slash !== undefined && !slashBelongsToValue ? slash : opening.length - 1;
|
|
161
|
+
opening = opening.slice(0, insertion) + additions + opening.slice(insertion);
|
|
162
|
+
}
|
|
163
|
+
return opening;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Replace the contents of the live HTML root without consuming nested closing
|
|
167
|
+
* tags or matching examples inside comments, scripts, styles, or templates.
|
|
168
|
+
* Missing or unclosed roots leave the template unchanged.
|
|
169
|
+
*/
|
|
170
|
+
export function replaceHtmlRootContents(html, contents, attributes = {}) {
|
|
171
|
+
let root;
|
|
172
|
+
let depth = 0;
|
|
173
|
+
let templateDepth = 0;
|
|
174
|
+
for (const tag of tags(html)) {
|
|
175
|
+
if (!root) {
|
|
176
|
+
if (tag.name === "template") {
|
|
177
|
+
templateDepth += tag.closing ? -1 : 1;
|
|
178
|
+
templateDepth = Math.max(0, templateDepth);
|
|
179
|
+
}
|
|
180
|
+
if (!tag.closing && !templateDepth && !VOID_TAGS.has(tag.name) && tag.attributes.find((attribute) => attribute.name === "id")?.value === "root") {
|
|
181
|
+
root = tag;
|
|
182
|
+
depth = 1;
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (tag.name !== root.name) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
// A self-closing slash is ignored on non-void HTML elements such as div.
|
|
190
|
+
depth += tag.closing ? -1 : 1;
|
|
191
|
+
if (depth === 0) {
|
|
192
|
+
return html.slice(0, root.start) + patchOpeningTag(html, root, attributes) + contents + html.slice(tag.start);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return html;
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=html.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","sourceRoot":"","sources":["../../src/utils/html.ts"],"names":[],"mappings":"AAGA,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC;AAChH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;AA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I,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;YACR,IAAI,GAAG,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;gBAC1B,aAAa,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBACtC,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;YAC/C,CAAC;YACD,IAAI,CAAC,GAAG,CAAC,OAAO,IAAI,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,KAAK,KAAK,MAAM,EAAE,CAAC;gBAC9I,IAAI,GAAG,GAAG,CAAC;gBACX,KAAK,GAAG,CAAC,CAAC;YACd,CAAC;YACD,SAAS;QACb,CAAC;QACD,IAAI,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;YACzB,SAAS;QACb,CAAC;QACD,yEAAyE;QACzE,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9B,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACd,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAClH,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC","sourcesContent":["type Attribute = { name: string; value: string; start: number; end: number };\ntype Tag = { name: string; closing: boolean; start: number; end: number; attributes: Attribute[] };\n\nconst RAW_TEXT_TAGS = new Set([\"script\", \"style\", \"textarea\", \"title\", \"xmp\", \"iframe\", \"noembed\", \"noframes\"]);\nconst VOID_TAGS = new Set([\"area\", \"base\", \"br\", \"col\", \"embed\", \"hr\", \"img\", \"input\", \"link\", \"meta\", \"param\", \"source\", \"track\", \"wbr\"]);\n\nfunction tagEnd(html: string, start: number): number {\n let quote = \"\";\n for (let index = start; index < html.length; index++) {\n const character = html[index];\n if (quote) {\n if (character === quote) {\n quote = \"\";\n }\n } else if (character === '\"' || character === \"'\") {\n quote = character;\n } else if (character === \">\") {\n return index + 1;\n }\n }\n return -1;\n}\n\nfunction readAttributes(html: string, start: number, end: number): Attribute[] {\n const attributes: Attribute[] = [];\n let index = start;\n while (index < end - 1) {\n while (/\\s/.test(html[index] ?? \"\")) {\n index++;\n }\n if (index >= end - 1) {\n break;\n }\n if (html[index] === \"/\") {\n index++;\n continue;\n }\n const attributeStart = index;\n while (index < end - 1 && !/[\\s=/>]/.test(html[index])) {\n index++;\n }\n const name = html.slice(attributeStart, index).toLowerCase();\n let attributeEnd = index;\n while (index < end - 1 && /\\s/.test(html[index])) {\n index++;\n }\n let value = \"\";\n if (html[index] === \"=\") {\n index++;\n while (index < end - 1 && /\\s/.test(html[index])) {\n index++;\n }\n const quote = html[index] === '\"' || html[index] === \"'\" ? html[index++] : undefined;\n const valueStart = index;\n if (quote) {\n while (index < end - 1 && html[index] !== quote) {\n index++;\n }\n value = html.slice(valueStart, index);\n if (html[index] === quote) {\n index++;\n }\n } else {\n while (index < end - 1 && !/\\s/.test(html[index])) {\n index++;\n }\n value = html.slice(valueStart, index);\n }\n attributeEnd = index;\n }\n if (name) {\n attributes.push({ name, value, start: attributeStart, end: attributeEnd });\n }\n // Make progress even for malformed attributes.\n if (index === attributeStart) {\n index++;\n }\n }\n return attributes;\n}\n\nfunction* tags(html: string): Generator<Tag> {\n const lowerHtml = html.toLowerCase();\n let index = 0;\n while (index < html.length) {\n const start = html.indexOf(\"<\", index);\n if (start === -1) {\n return;\n }\n if (html.startsWith(\"<!--\", start)) {\n const end = html.indexOf(\"-->\", start + 4);\n index = end === -1 ? html.length : end + 3;\n continue;\n }\n if (html.startsWith(\"<![CDATA[\", start)) {\n const end = html.indexOf(\"]]>\", start + 9);\n index = end === -1 ? html.length : end + 3;\n continue;\n }\n const match = /^<(\\/)?([a-z][a-z0-9:-]*)(?=[\\s/>])/i.exec(html.slice(start));\n if (!match) {\n if (html[start + 1] === \"!\" || html[start + 1] === \"?\") {\n const end = tagEnd(html, start + 2);\n index = end === -1 ? html.length : end;\n } else {\n index = start + 1;\n }\n continue;\n }\n const end = tagEnd(html, start + match[0].length);\n if (end === -1) {\n return;\n }\n const name = match[2].toLowerCase();\n const closing = Boolean(match[1]);\n yield { name, closing, start, end, attributes: closing ? [] : readAttributes(html, start + match[0].length, end) };\n index = end;\n if (!closing && name === \"plaintext\") {\n return;\n }\n if (!closing && RAW_TEXT_TAGS.has(name)) {\n // HTML raw text ends at its closing tag, regardless of JavaScript or\n // CSS quoting. Markup-looking strings inside it are not elements.\n let close = lowerHtml.indexOf(`</${name}`, index);\n while (close !== -1 && !/[\\s/>]/.test(html[close + name.length + 2] ?? \"\")) {\n close = lowerHtml.indexOf(`</${name}`, close + 2);\n }\n index = close === -1 ? html.length : close;\n }\n }\n}\n\nfunction escapeAttribute(value: string): string {\n return value.replace(/&/g, \"&\").replace(/\"/g, \""\").replace(/</g, \"<\").replace(/>/g, \">\");\n}\n\nfunction patchOpeningTag(html: string, tag: Tag, attributes: Record<string, string | null>): string {\n let opening = html.slice(tag.start, tag.end);\n const updates = new Map(Object.entries(attributes).map(([name, value]) => [name.toLowerCase(), value]));\n const replacements: Array<{ start: number; end: number; value: string }> = [];\n const handled = new Set<string>();\n for (const attribute of tag.attributes) {\n if (!updates.has(attribute.name)) {\n continue;\n }\n const value = updates.get(attribute.name);\n replacements.push({\n start: attribute.start - tag.start,\n end: attribute.end - tag.start,\n value: value === null || handled.has(attribute.name) ? \"\" : `${attribute.name}=\"${escapeAttribute(value!)}\"`,\n });\n handled.add(attribute.name);\n }\n for (const replacement of replacements.reverse()) {\n opening = opening.slice(0, replacement.start) + replacement.value + opening.slice(replacement.end);\n }\n const additions = [...updates]\n .filter(([name, value]) => !handled.has(name) && value !== null)\n .map(([name, value]) => ` ${name}=\"${escapeAttribute(value!)}\"`)\n .join(\"\");\n if (additions) {\n const slash = /\\/\\s*>$/.exec(opening)?.index;\n const slashBelongsToValue = html[tag.end - 2] === \"/\" && tag.attributes.some((attribute) => attribute.end === tag.end - 1);\n const insertion = slash !== undefined && !slashBelongsToValue ? slash : opening.length - 1;\n opening = opening.slice(0, insertion) + additions + opening.slice(insertion);\n }\n return opening;\n}\n\n/**\n * Replace the contents of the live HTML root without consuming nested closing\n * tags or matching examples inside comments, scripts, styles, or templates.\n * Missing or unclosed roots leave the template unchanged.\n */\nexport function replaceHtmlRootContents(html: string, contents: string, attributes: Record<string, string | null> = {}): string {\n let root: Tag | undefined;\n let depth = 0;\n let templateDepth = 0;\n for (const tag of tags(html)) {\n if (!root) {\n if (tag.name === \"template\") {\n templateDepth += tag.closing ? -1 : 1;\n templateDepth = Math.max(0, templateDepth);\n }\n if (!tag.closing && !templateDepth && !VOID_TAGS.has(tag.name) && tag.attributes.find((attribute) => attribute.name === \"id\")?.value === \"root\") {\n root = tag;\n depth = 1;\n }\n continue;\n }\n if (tag.name !== root.name) {\n continue;\n }\n // A self-closing slash is ignored on non-void HTML elements such as div.\n depth += tag.closing ? -1 : 1;\n if (depth === 0) {\n return html.slice(0, root.start) + patchOpeningTag(html, root, attributes) + contents + html.slice(tag.start);\n }\n }\n return html;\n}\n"]}
|
|
@@ -1,59 +1,13 @@
|
|
|
1
1
|
import type http from "http";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* Extract the client IP by trusting exactly `trustProxyDepth` hops, including
|
|
4
|
+
* the direct peer. For one trusted proxy, use the rightmost X-Forwarded-For
|
|
5
|
+
* entry; for two, use the next entry to its left. Untrusted prefixes are ignored.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* 2. True-Client-IP (Cloudflare Enterprise, Akamai)
|
|
9
|
-
* 3. X-Real-IP (Nginx, other proxies)
|
|
10
|
-
* 4. X-Forwarded-For (Standard, but can be spoofed)
|
|
11
|
-
* 5. req.socket.remoteAddress (Direct connection)
|
|
12
|
-
*
|
|
13
|
-
* When behind proxies, the X-Forwarded-For header contains a chain of IP addresses.
|
|
14
|
-
* Format: "client, proxy1, proxy2, ..."
|
|
15
|
-
*
|
|
16
|
-
* @param req - The HTTP request object
|
|
17
|
-
* @param trustProxyDepth - Number of proxy levels to trust
|
|
18
|
-
* - 0: Only use req.socket.remoteAddress (no proxy trust)
|
|
19
|
-
* - 1: Trust 1 proxy level (recommended for Vercel, Netlify, Railway)
|
|
20
|
-
* - 2+: Trust multiple proxy levels (for complex setups)
|
|
21
|
-
*
|
|
22
|
-
* Examples:
|
|
23
|
-
* - trustProxyDepth=0: Direct connection, no proxies
|
|
24
|
-
* All proxy headers ignored
|
|
25
|
-
* Result: req.socket.remoteAddress
|
|
26
|
-
*
|
|
27
|
-
* - trustProxyDepth=1: Behind one proxy (e.g., Vercel, Netlify)
|
|
28
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1"
|
|
29
|
-
* Result: "203.0.113.1" (client IP)
|
|
30
|
-
*
|
|
31
|
-
* - trustProxyDepth=2: Behind two proxies (e.g., Cloudflare -> Load Balancer)
|
|
32
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1, 192.0.2.1"
|
|
33
|
-
* Result: "203.0.113.1" (client IP)
|
|
34
|
-
*
|
|
35
|
-
* Common configurations:
|
|
36
|
-
* - Vercel/Netlify/Railway: trustProxyDepth=1
|
|
37
|
-
* - Cloudflare -> Origin: trustProxyDepth=1 (use CF-Connecting-IP)
|
|
38
|
-
* - AWS ALB -> EC2: trustProxyDepth=1
|
|
39
|
-
* - Nginx -> Node: trustProxyDepth=1 (use X-Real-IP)
|
|
40
|
-
* - Cloudflare -> Nginx -> Node: trustProxyDepth=2
|
|
7
|
+
* With no forwarded chain, provider-specific headers are accepted only when
|
|
8
|
+
* proxy trust is enabled. The trusted proxy must overwrite those headers.
|
|
41
9
|
*/
|
|
42
10
|
export declare function extractClientIP(req: http.IncomingMessage, trustProxyDepth?: number): string;
|
|
43
|
-
/**
|
|
44
|
-
* Alternative extraction method that works from the right (trusts the rightmost IPs).
|
|
45
|
-
* This is useful when you want to trust the last N proxies in the chain.
|
|
46
|
-
*
|
|
47
|
-
* Note: This only applies to X-Forwarded-For parsing. Single-value headers like
|
|
48
|
-
* CF-Connecting-IP are still checked first.
|
|
49
|
-
*
|
|
50
|
-
* @param req - The HTTP request object
|
|
51
|
-
* @param trustProxyDepth - Number of proxy levels to trust from the right
|
|
52
|
-
*
|
|
53
|
-
* Example:
|
|
54
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1, 192.0.2.1"
|
|
55
|
-
* trustProxyDepth=1: Result is "198.51.100.1" (skip the last trusted proxy)
|
|
56
|
-
* trustProxyDepth=2: Result is "203.0.113.1" (skip the last 2 trusted proxies)
|
|
57
|
-
*/
|
|
11
|
+
/** Uses the same depth-based extraction as extractClientIP. */
|
|
58
12
|
export declare function extractClientIPFromRight(req: http.IncomingMessage, trustProxyDepth?: number): string;
|
|
59
13
|
//# sourceMappingURL=ipExtractor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipExtractor.d.ts","sourceRoot":"","sources":["../../src/utils/ipExtractor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"ipExtractor.d.ts","sourceRoot":"","sources":["../../src/utils/ipExtractor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAG7B;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,eAAe,GAAE,MAAU,GAAG,MAAM,CAoB9F;AAED,+DAA+D;AAC/D,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,eAAe,GAAE,MAAU,GAAG,MAAM,CAEvG"}
|
|
@@ -1,130 +1,33 @@
|
|
|
1
|
+
import { isIP } from "net";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
3
|
+
* Extract the client IP by trusting exactly `trustProxyDepth` hops, including
|
|
4
|
+
* the direct peer. For one trusted proxy, use the rightmost X-Forwarded-For
|
|
5
|
+
* entry; for two, use the next entry to its left. Untrusted prefixes are ignored.
|
|
4
6
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* 2. True-Client-IP (Cloudflare Enterprise, Akamai)
|
|
8
|
-
* 3. X-Real-IP (Nginx, other proxies)
|
|
9
|
-
* 4. X-Forwarded-For (Standard, but can be spoofed)
|
|
10
|
-
* 5. req.socket.remoteAddress (Direct connection)
|
|
11
|
-
*
|
|
12
|
-
* When behind proxies, the X-Forwarded-For header contains a chain of IP addresses.
|
|
13
|
-
* Format: "client, proxy1, proxy2, ..."
|
|
14
|
-
*
|
|
15
|
-
* @param req - The HTTP request object
|
|
16
|
-
* @param trustProxyDepth - Number of proxy levels to trust
|
|
17
|
-
* - 0: Only use req.socket.remoteAddress (no proxy trust)
|
|
18
|
-
* - 1: Trust 1 proxy level (recommended for Vercel, Netlify, Railway)
|
|
19
|
-
* - 2+: Trust multiple proxy levels (for complex setups)
|
|
20
|
-
*
|
|
21
|
-
* Examples:
|
|
22
|
-
* - trustProxyDepth=0: Direct connection, no proxies
|
|
23
|
-
* All proxy headers ignored
|
|
24
|
-
* Result: req.socket.remoteAddress
|
|
25
|
-
*
|
|
26
|
-
* - trustProxyDepth=1: Behind one proxy (e.g., Vercel, Netlify)
|
|
27
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1"
|
|
28
|
-
* Result: "203.0.113.1" (client IP)
|
|
29
|
-
*
|
|
30
|
-
* - trustProxyDepth=2: Behind two proxies (e.g., Cloudflare -> Load Balancer)
|
|
31
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1, 192.0.2.1"
|
|
32
|
-
* Result: "203.0.113.1" (client IP)
|
|
33
|
-
*
|
|
34
|
-
* Common configurations:
|
|
35
|
-
* - Vercel/Netlify/Railway: trustProxyDepth=1
|
|
36
|
-
* - Cloudflare -> Origin: trustProxyDepth=1 (use CF-Connecting-IP)
|
|
37
|
-
* - AWS ALB -> EC2: trustProxyDepth=1
|
|
38
|
-
* - Nginx -> Node: trustProxyDepth=1 (use X-Real-IP)
|
|
39
|
-
* - Cloudflare -> Nginx -> Node: trustProxyDepth=2
|
|
7
|
+
* With no forwarded chain, provider-specific headers are accepted only when
|
|
8
|
+
* proxy trust is enabled. The trusted proxy must overwrite those headers.
|
|
40
9
|
*/
|
|
41
10
|
export function extractClientIP(req, trustProxyDepth = 0) {
|
|
42
|
-
|
|
43
|
-
if (trustProxyDepth
|
|
44
|
-
return
|
|
11
|
+
const directIP = req.socket?.remoteAddress || "unknown";
|
|
12
|
+
if (!Number.isInteger(trustProxyDepth) || trustProxyDepth <= 0) {
|
|
13
|
+
return directIP;
|
|
45
14
|
}
|
|
46
|
-
// Security: When trustProxyDepth >= 1, extract IP using X-Forwarded-For first.
|
|
47
|
-
// Single-value headers (CF-Connecting-IP, True-Client-IP, X-Real-IP) are only
|
|
48
|
-
// checked as a fallback since they can be spoofed by direct clients when no
|
|
49
|
-
// trusted proxy IP allowlist is configured.
|
|
50
|
-
// 1. Check X-Forwarded-For (Standard, most reliable with depth-based extraction)
|
|
51
15
|
const forwardedFor = req.headers["x-forwarded-for"];
|
|
52
|
-
if (forwardedFor) {
|
|
53
|
-
const forwardedIPs = (Array.isArray(forwardedFor) ? forwardedFor.join(",") : forwardedFor)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return forwardedIPs[0];
|
|
62
|
-
}
|
|
16
|
+
if (forwardedFor !== undefined) {
|
|
17
|
+
const forwardedIPs = (Array.isArray(forwardedFor) ? forwardedFor.join(",") : forwardedFor).split(",").map((ip) => ip.trim());
|
|
18
|
+
const clientIP = forwardedIPs[forwardedIPs.length - trustProxyDepth];
|
|
19
|
+
return clientIP && isIP(clientIP) ? clientIP : directIP;
|
|
20
|
+
}
|
|
21
|
+
for (const name of ["cf-connecting-ip", "true-client-ip", "x-real-ip"]) {
|
|
22
|
+
const value = req.headers[name];
|
|
23
|
+
if (typeof value === "string" && isIP(value.trim())) {
|
|
24
|
+
return value.trim();
|
|
63
25
|
}
|
|
64
26
|
}
|
|
65
|
-
|
|
66
|
-
const cfConnectingIP = req.headers["cf-connecting-ip"];
|
|
67
|
-
if (cfConnectingIP && typeof cfConnectingIP === "string" && cfConnectingIP.trim().length > 0) {
|
|
68
|
-
return cfConnectingIP.trim();
|
|
69
|
-
}
|
|
70
|
-
// 3. Fallback: Check True-Client-IP (Cloudflare Enterprise, Akamai)
|
|
71
|
-
const trueClientIP = req.headers["true-client-ip"];
|
|
72
|
-
if (trueClientIP && typeof trueClientIP === "string" && trueClientIP.trim().length > 0) {
|
|
73
|
-
return trueClientIP.trim();
|
|
74
|
-
}
|
|
75
|
-
// 4. Fallback: Check X-Real-IP (Nginx, other proxies)
|
|
76
|
-
const xRealIP = req.headers["x-real-ip"];
|
|
77
|
-
if (xRealIP && typeof xRealIP === "string" && xRealIP.trim().length > 0) {
|
|
78
|
-
return xRealIP.trim();
|
|
79
|
-
}
|
|
80
|
-
// 5. Fall back to direct connection
|
|
81
|
-
return req.socket.remoteAddress || "unknown";
|
|
27
|
+
return directIP;
|
|
82
28
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Alternative extraction method that works from the right (trusts the rightmost IPs).
|
|
85
|
-
* This is useful when you want to trust the last N proxies in the chain.
|
|
86
|
-
*
|
|
87
|
-
* Note: This only applies to X-Forwarded-For parsing. Single-value headers like
|
|
88
|
-
* CF-Connecting-IP are still checked first.
|
|
89
|
-
*
|
|
90
|
-
* @param req - The HTTP request object
|
|
91
|
-
* @param trustProxyDepth - Number of proxy levels to trust from the right
|
|
92
|
-
*
|
|
93
|
-
* Example:
|
|
94
|
-
* X-Forwarded-For: "203.0.113.1, 198.51.100.1, 192.0.2.1"
|
|
95
|
-
* trustProxyDepth=1: Result is "198.51.100.1" (skip the last trusted proxy)
|
|
96
|
-
* trustProxyDepth=2: Result is "203.0.113.1" (skip the last 2 trusted proxies)
|
|
97
|
-
*/
|
|
29
|
+
/** Uses the same depth-based extraction as extractClientIP. */
|
|
98
30
|
export function extractClientIPFromRight(req, trustProxyDepth = 0) {
|
|
99
|
-
|
|
100
|
-
return req.socket.remoteAddress || "unknown";
|
|
101
|
-
}
|
|
102
|
-
// Check single-value headers first (same as extractClientIP)
|
|
103
|
-
const cfConnectingIP = req.headers["cf-connecting-ip"];
|
|
104
|
-
if (cfConnectingIP && typeof cfConnectingIP === "string" && cfConnectingIP.trim().length > 0) {
|
|
105
|
-
return cfConnectingIP.trim();
|
|
106
|
-
}
|
|
107
|
-
const trueClientIP = req.headers["true-client-ip"];
|
|
108
|
-
if (trueClientIP && typeof trueClientIP === "string" && trueClientIP.trim().length > 0) {
|
|
109
|
-
return trueClientIP.trim();
|
|
110
|
-
}
|
|
111
|
-
const xRealIP = req.headers["x-real-ip"];
|
|
112
|
-
if (xRealIP && typeof xRealIP === "string" && xRealIP.trim().length > 0) {
|
|
113
|
-
return xRealIP.trim();
|
|
114
|
-
}
|
|
115
|
-
// For X-Forwarded-For, use right-based extraction
|
|
116
|
-
const forwardedFor = req.headers["x-forwarded-for"];
|
|
117
|
-
if (forwardedFor) {
|
|
118
|
-
const forwardedIPs = (Array.isArray(forwardedFor) ? forwardedFor.join(",") : forwardedFor)
|
|
119
|
-
.split(",")
|
|
120
|
-
.map((ip) => ip.trim())
|
|
121
|
-
.filter((ip) => ip.length > 0);
|
|
122
|
-
if (forwardedIPs.length > 0) {
|
|
123
|
-
// Calculate which IP to trust by skipping the rightmost N trusted proxies
|
|
124
|
-
const clientIPIndex = Math.max(0, forwardedIPs.length - trustProxyDepth - 1);
|
|
125
|
-
return forwardedIPs[clientIPIndex];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return req.socket.remoteAddress || "unknown";
|
|
31
|
+
return extractClientIP(req, trustProxyDepth);
|
|
129
32
|
}
|
|
130
33
|
//# sourceMappingURL=ipExtractor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipExtractor.js","sourceRoot":"","sources":["../../src/utils/ipExtractor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ipExtractor.js","sourceRoot":"","sources":["../../src/utils/ipExtractor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC;AAE3B;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,GAAyB,EAAE,kBAA0B,CAAC;IAClF,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,EAAE,aAAa,IAAI,SAAS,CAAC;IACxD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,eAAe,IAAI,CAAC,EAAE,CAAC;QAC7D,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,MAAM,YAAY,GAAG,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;IACpD,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,YAAY,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;QAC7H,MAAM,QAAQ,GAAG,YAAY,CAAC,YAAY,CAAC,MAAM,GAAG,eAAe,CAAC,CAAC;QACrE,OAAO,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC5D,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,CAAC,kBAAkB,EAAE,gBAAgB,EAAE,WAAW,CAAC,EAAE,CAAC;QACrE,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;YAClD,OAAO,KAAK,CAAC,IAAI,EAAE,CAAC;QACxB,CAAC;IACL,CAAC;IACD,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,wBAAwB,CAAC,GAAyB,EAAE,kBAA0B,CAAC;IAC3F,OAAO,eAAe,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import type http from \"http\";\nimport { isIP } from \"net\";\n\n/**\n * Extract the client IP by trusting exactly `trustProxyDepth` hops, including\n * the direct peer. For one trusted proxy, use the rightmost X-Forwarded-For\n * entry; for two, use the next entry to its left. Untrusted prefixes are ignored.\n *\n * With no forwarded chain, provider-specific headers are accepted only when\n * proxy trust is enabled. The trusted proxy must overwrite those headers.\n */\nexport function extractClientIP(req: http.IncomingMessage, trustProxyDepth: number = 0): string {\n const directIP = req.socket?.remoteAddress || \"unknown\";\n if (!Number.isInteger(trustProxyDepth) || trustProxyDepth <= 0) {\n return directIP;\n }\n\n const forwardedFor = req.headers[\"x-forwarded-for\"];\n if (forwardedFor !== undefined) {\n const forwardedIPs = (Array.isArray(forwardedFor) ? forwardedFor.join(\",\") : forwardedFor).split(\",\").map((ip) => ip.trim());\n const clientIP = forwardedIPs[forwardedIPs.length - trustProxyDepth];\n return clientIP && isIP(clientIP) ? clientIP : directIP;\n }\n\n for (const name of [\"cf-connecting-ip\", \"true-client-ip\", \"x-real-ip\"]) {\n const value = req.headers[name];\n if (typeof value === \"string\" && isIP(value.trim())) {\n return value.trim();\n }\n }\n return directIP;\n}\n\n/** Uses the same depth-based extraction as extractClientIP. */\nexport function extractClientIPFromRight(req: http.IncomingMessage, trustProxyDepth: number = 0): string {\n return extractClientIP(req, trustProxyDepth);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heliumPlugin.d.ts","sourceRoot":"","sources":["../../src/vite/heliumPlugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAkBnC,MAAM,CAAC,OAAO,UAAU,MAAM,IAAI,MAAM,
|
|
1
|
+
{"version":3,"file":"heliumPlugin.d.ts","sourceRoot":"","sources":["../../src/vite/heliumPlugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAkBnC,MAAM,CAAC,OAAO,UAAU,MAAM,IAAI,MAAM,CAwbvC;AAkID;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAiBrG;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAUhD"}
|