@frost.js/adapter-node 0.0.0-stage → 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 +21 -0
- package/README.md +15 -3
- package/dist/bridge.d.ts +11 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +228 -0
- package/dist/bridge.js.map +1 -0
- package/dist/compression.d.ts +17 -0
- package/dist/compression.d.ts.map +1 -0
- package/dist/compression.js +71 -0
- package/dist/compression.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/mime.d.ts +2 -0
- package/dist/mime.d.ts.map +1 -0
- package/dist/mime.js +23 -0
- package/dist/mime.js.map +1 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +57 -0
- package/dist/server.js.map +1 -0
- package/dist/static.d.ts +14 -0
- package/dist/static.d.ts.map +1 -0
- package/dist/static.js +158 -0
- package/dist/static.js.map +1 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Frost contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @frost.js/adapter-node
|
|
2
|
+
|
|
3
|
+
Node.js http adapter for Frost: request bridge, static file serving, compression.
|
|
4
|
+
|
|
5
|
+
Part of the [Frost](https://frostjs.dev) framework monorepo
|
|
6
|
+
([github.com/ben-bader/Frost](https://github.com/ben-bader/Frost)).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @frost.js/adapter-node
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
See the repository docs for routing, data loading, contracts, middleware,
|
|
15
|
+
deployment, and the full API surface.
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type CompressionOptions } from "./compression.js";
|
|
3
|
+
/**
|
|
4
|
+
* Returns true when the raw request target can be turned into a safe
|
|
5
|
+
* origin-form or absolute http(s) URL. Anything else (e.g. a malformed
|
|
6
|
+
* absolute URI) should be rejected with 400 before bridging.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isSafeRequestTarget(rawUrl: string | undefined): boolean;
|
|
9
|
+
export declare function nodeReqToWebRequest(req: IncomingMessage, defaultHost?: string): Request;
|
|
10
|
+
export declare function sendWebResponseToNode(webResponse: Response, nodeRes: ServerResponse, compression?: CompressionOptions): Promise<void>;
|
|
11
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGjE,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAK1B;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAcvE;AA0CD,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,eAAe,EACpB,WAAW,GAAE,MAAsB,GAClC,OAAO,CAwCT;AAED,wBAAsB,qBAAqB,CACzC,WAAW,EAAE,QAAQ,EACrB,OAAO,EAAE,cAAc,EACvB,WAAW,CAAC,EAAE,kBAAkB,GAC/B,OAAO,CAAC,IAAI,CAAC,CAsHf"}
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
import zlib from "node:zlib";
|
|
3
|
+
import { isCompressible, negotiateEncoding, } from "./compression.js";
|
|
4
|
+
const FALLBACK_HOST = "localhost:3000";
|
|
5
|
+
const FALLBACK_ORIGIN = "http://frost.local";
|
|
6
|
+
/**
|
|
7
|
+
* Returns true when the raw request target can be turned into a safe
|
|
8
|
+
* origin-form or absolute http(s) URL. Anything else (e.g. a malformed
|
|
9
|
+
* absolute URI) should be rejected with 400 before bridging.
|
|
10
|
+
*/
|
|
11
|
+
export function isSafeRequestTarget(rawUrl) {
|
|
12
|
+
const target = rawUrl ?? "/";
|
|
13
|
+
if (target === "*")
|
|
14
|
+
return true;
|
|
15
|
+
if (target.startsWith("/"))
|
|
16
|
+
return true;
|
|
17
|
+
// Absolute-form (used by proxies): must parse as http(s).
|
|
18
|
+
if (/^https?:\/\//i.test(target)) {
|
|
19
|
+
try {
|
|
20
|
+
const u = new URL(target);
|
|
21
|
+
return u.protocol === "http:" || u.protocol === "https:";
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Reduces an IncomingMessage url to a safe path+query string.
|
|
31
|
+
* - "//host/path" and "///path" collapse to a single-slash path
|
|
32
|
+
* (they are treated as paths, never as protocol-relative URLs).
|
|
33
|
+
* - Absolute http(s) URLs are reduced to pathname+search.
|
|
34
|
+
* - Authority-form / asterisk targets become "/".
|
|
35
|
+
*/
|
|
36
|
+
function toSafePath(rawUrl) {
|
|
37
|
+
let target = rawUrl ?? "/";
|
|
38
|
+
if (/^https?:\/\//i.test(target)) {
|
|
39
|
+
try {
|
|
40
|
+
const u = new URL(target);
|
|
41
|
+
target = `${u.pathname}${u.search}` || "/";
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
target = "/";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (!target.startsWith("/")) {
|
|
48
|
+
// authority-form (CONNECT) or "*" — not routable here
|
|
49
|
+
target = "/";
|
|
50
|
+
}
|
|
51
|
+
// Collapse leading slashes so "//evil.example/x" is a path, not a host.
|
|
52
|
+
target = "/" + target.replace(/^\/+/, "");
|
|
53
|
+
if (!target.includes("?")) {
|
|
54
|
+
// keep query if present; nothing else to do
|
|
55
|
+
}
|
|
56
|
+
return target || "/";
|
|
57
|
+
}
|
|
58
|
+
/** Host header values are interpolated into a URL; reject anything odd. */
|
|
59
|
+
function sanitizeHost(rawHost) {
|
|
60
|
+
if (!rawHost)
|
|
61
|
+
return FALLBACK_HOST;
|
|
62
|
+
// Allow DNS names, IPv4, IPv6 literals, ports. Nothing else.
|
|
63
|
+
if (/^[a-zA-Z0-9.\-:[\]]{1,255}$/.test(rawHost)) {
|
|
64
|
+
return rawHost;
|
|
65
|
+
}
|
|
66
|
+
return FALLBACK_HOST;
|
|
67
|
+
}
|
|
68
|
+
export function nodeReqToWebRequest(req, defaultHost = FALLBACK_HOST) {
|
|
69
|
+
const host = sanitizeHost(req.headers.host ?? defaultHost);
|
|
70
|
+
const protocol = req.socket?.encrypted ? "https" : "http";
|
|
71
|
+
const base = `${protocol}://${host}`;
|
|
72
|
+
const path = toSafePath(req.url);
|
|
73
|
+
let url;
|
|
74
|
+
try {
|
|
75
|
+
url = new URL(path, base);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Last-resort: never throw from the bridge. The request will 404.
|
|
79
|
+
url = new URL("/", FALLBACK_ORIGIN);
|
|
80
|
+
}
|
|
81
|
+
const headers = new Headers();
|
|
82
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
83
|
+
if (value === undefined)
|
|
84
|
+
continue;
|
|
85
|
+
if (Array.isArray(value)) {
|
|
86
|
+
for (const v of value) {
|
|
87
|
+
headers.append(key, v);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
headers.set(key, value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const hasBody = req.method !== "GET" && req.method !== "HEAD";
|
|
95
|
+
const init = {
|
|
96
|
+
method: req.method ?? "GET",
|
|
97
|
+
headers,
|
|
98
|
+
};
|
|
99
|
+
if (hasBody) {
|
|
100
|
+
// In Node.js 18+, Readable.toWeb creates a web ReadableStream
|
|
101
|
+
init.body = Readable.toWeb(req);
|
|
102
|
+
// duplex is required by Node's fetch/Request implementation for body streams
|
|
103
|
+
init.duplex = "half";
|
|
104
|
+
}
|
|
105
|
+
return new Request(url.toString(), init);
|
|
106
|
+
}
|
|
107
|
+
export async function sendWebResponseToNode(webResponse, nodeRes, compression) {
|
|
108
|
+
nodeRes.statusCode = webResponse.status;
|
|
109
|
+
nodeRes.statusMessage = webResponse.statusText;
|
|
110
|
+
const headers = new Headers(webResponse.headers);
|
|
111
|
+
const acceptEncoding = nodeRes.req?.headers?.["accept-encoding"];
|
|
112
|
+
const contentType = headers.get("content-type");
|
|
113
|
+
const encoding = webResponse.body && compression?.enabled
|
|
114
|
+
? negotiateEncoding(acceptEncoding, compression)
|
|
115
|
+
: null;
|
|
116
|
+
const threshold = compression?.threshold ?? 1024;
|
|
117
|
+
const contentLength = Number(headers.get("content-length") ?? 0);
|
|
118
|
+
const maybeCompress = !!webResponse.body &&
|
|
119
|
+
!!encoding &&
|
|
120
|
+
isCompressible(contentType, "") &&
|
|
121
|
+
(contentLength === 0 || contentLength >= threshold);
|
|
122
|
+
if (!webResponse.body) {
|
|
123
|
+
headers.forEach((val, key) => nodeRes.setHeader(key, val));
|
|
124
|
+
nodeRes.end();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const reader = webResponse.body.getReader();
|
|
128
|
+
if (!maybeCompress || !encoding) {
|
|
129
|
+
headers.set("Vary", mergeVary(headers.get("Vary"), "Accept-Encoding"));
|
|
130
|
+
headers.forEach((val, key) => nodeRes.setHeader(key, val));
|
|
131
|
+
try {
|
|
132
|
+
while (true) {
|
|
133
|
+
const { done, value } = await reader.read();
|
|
134
|
+
if (done)
|
|
135
|
+
break;
|
|
136
|
+
if (value)
|
|
137
|
+
nodeRes.write(Buffer.from(value));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
reader.releaseLock();
|
|
142
|
+
nodeRes.end();
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// Peek up to `threshold` bytes: bodies that end below it are sent
|
|
147
|
+
// raw (spec: skip small bodies); larger bodies stream through a zlib
|
|
148
|
+
// transform so TTFB is preserved — no full-buffer wait.
|
|
149
|
+
const prefix = [];
|
|
150
|
+
let prefixBytes = 0;
|
|
151
|
+
let overflow = false;
|
|
152
|
+
while (prefixBytes <= threshold) {
|
|
153
|
+
const { done, value } = await reader.read();
|
|
154
|
+
if (done)
|
|
155
|
+
break;
|
|
156
|
+
if (!value)
|
|
157
|
+
continue;
|
|
158
|
+
const buf = Buffer.from(value);
|
|
159
|
+
prefix.push(buf);
|
|
160
|
+
prefixBytes += buf.byteLength;
|
|
161
|
+
if (prefixBytes > threshold) {
|
|
162
|
+
overflow = true;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (!overflow) {
|
|
167
|
+
// Small body — send identity, no Content-Encoding.
|
|
168
|
+
headers.set("Vary", mergeVary(headers.get("Vary"), "Accept-Encoding"));
|
|
169
|
+
headers.forEach((val, key) => nodeRes.setHeader(key, val));
|
|
170
|
+
for (const buf of prefix)
|
|
171
|
+
nodeRes.write(buf);
|
|
172
|
+
nodeRes.end();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
headers.set("Content-Encoding", encoding);
|
|
176
|
+
headers.delete("Content-Length");
|
|
177
|
+
// A strong ETag describes the identity body; drop it rather than
|
|
178
|
+
// serve a mismatched validator for the compressed variant.
|
|
179
|
+
headers.delete("etag");
|
|
180
|
+
headers.set("Vary", mergeVary(headers.get("Vary"), "Accept-Encoding"));
|
|
181
|
+
headers.forEach((val, key) => nodeRes.setHeader(key, val));
|
|
182
|
+
const compressor = encoding === "br"
|
|
183
|
+
? zlib.createBrotliCompress({
|
|
184
|
+
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5 },
|
|
185
|
+
})
|
|
186
|
+
: zlib.createGzip({ level: 6 });
|
|
187
|
+
compressor.on("error", () => nodeRes.destroy());
|
|
188
|
+
compressor.pipe(nodeRes);
|
|
189
|
+
// zlib buffers output internally. Flush once after the first chunk
|
|
190
|
+
// so headers + leading compressed bytes reach the client early
|
|
191
|
+
// (TTFB), then let zlib batch the rest — per-chunk sync flushes are
|
|
192
|
+
// too expensive for React's many small stream chunks.
|
|
193
|
+
let flushed = false;
|
|
194
|
+
const writeChunk = async (buf) => {
|
|
195
|
+
await new Promise((resolve, reject) => compressor.write(buf, (err) => (err ? reject(err) : resolve())));
|
|
196
|
+
if (!flushed) {
|
|
197
|
+
flushed = true;
|
|
198
|
+
await new Promise((resolve) => compressor.flush(() => resolve()));
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
try {
|
|
202
|
+
for (const buf of prefix)
|
|
203
|
+
await writeChunk(buf);
|
|
204
|
+
while (true) {
|
|
205
|
+
const { done, value } = await reader.read();
|
|
206
|
+
if (done)
|
|
207
|
+
break;
|
|
208
|
+
if (value)
|
|
209
|
+
await writeChunk(Buffer.from(value));
|
|
210
|
+
}
|
|
211
|
+
compressor.end();
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
nodeRes.destroy();
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
reader.releaseLock();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function mergeVary(existing, value) {
|
|
221
|
+
if (!existing)
|
|
222
|
+
return value;
|
|
223
|
+
const parts = existing.split(",").map((v) => v.trim().toLowerCase());
|
|
224
|
+
if (parts.includes(value.toLowerCase()) || parts.includes("*"))
|
|
225
|
+
return existing;
|
|
226
|
+
return `${existing}, ${value}`;
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,cAAc,EACd,iBAAiB,GAElB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,aAAa,GAAG,gBAAgB,CAAC;AACvC,MAAM,eAAe,GAAG,oBAAoB,CAAC;AAE7C;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAA0B;IAC5D,MAAM,MAAM,GAAG,MAAM,IAAI,GAAG,CAAC;IAC7B,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,CA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brotli: boolean;
|
|
5
|
+
gzip: boolean;
|
|
6
|
+
threshold: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function toCompressionOptions(config?: CompressionConfig): CompressionOptions;
|
|
9
|
+
export type ContentEncoding = "br" | "gzip";
|
|
10
|
+
/** Picks the best encoding the client accepts, or null. */
|
|
11
|
+
export declare function negotiateEncoding(acceptEncoding: string | undefined, options: CompressionOptions): ContentEncoding | null;
|
|
12
|
+
export declare function isCompressible(contentType: string | null, pathname?: string): boolean;
|
|
13
|
+
export declare function compressBuffer(data: Buffer, encoding: ContentEncoding): Buffer;
|
|
14
|
+
export declare function decompressBuffer(data: Buffer, encoding: string): Buffer;
|
|
15
|
+
/** Encoding suffix for ETags so each variant caches independently. */
|
|
16
|
+
export declare function etagForEncoding(etag: string, encoding: ContentEncoding): string;
|
|
17
|
+
//# sourceMappingURL=compression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compression.d.ts","sourceRoot":"","sources":["../src/compression.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,oBAAoB,CAAC,MAAM,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,CAOnF;AAED,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,MAAM,CAAC;AAE5C,2DAA2D;AAC3D,wBAAgB,iBAAiB,CAC/B,cAAc,EAAE,MAAM,GAAG,SAAS,EAClC,OAAO,EAAE,kBAAkB,GAC1B,eAAe,GAAG,IAAI,CAwBxB;AAMD,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,SAAK,GAAG,OAAO,CAKjF;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,eAAe,GAAG,MAAM,CAO9E;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIvE;AAED,sEAAsE;AACtE,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,eAAe,GAAG,MAAM,CAE/E"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as zlib from "node:zlib";
|
|
2
|
+
export function toCompressionOptions(config) {
|
|
3
|
+
return {
|
|
4
|
+
enabled: config?.enabled ?? true,
|
|
5
|
+
brotli: config?.brotli ?? true,
|
|
6
|
+
gzip: config?.gzip ?? true,
|
|
7
|
+
threshold: config?.threshold ?? 1024,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/** Picks the best encoding the client accepts, or null. */
|
|
11
|
+
export function negotiateEncoding(acceptEncoding, options) {
|
|
12
|
+
if (!options.enabled || (!options.brotli && !options.gzip))
|
|
13
|
+
return null;
|
|
14
|
+
if (!acceptEncoding)
|
|
15
|
+
return null;
|
|
16
|
+
// Simple quality-aware parse: "gzip, deflate, br" or "br;q=0.8, gzip;q=1.0"
|
|
17
|
+
const accepted = new Map();
|
|
18
|
+
for (const part of acceptEncoding.split(",")) {
|
|
19
|
+
const [name, ...params] = part.trim().split(";");
|
|
20
|
+
const coding = name?.trim().toLowerCase();
|
|
21
|
+
if (!coding)
|
|
22
|
+
continue;
|
|
23
|
+
let q = 1;
|
|
24
|
+
for (const p of params) {
|
|
25
|
+
const m = p.trim().match(/^q=([\d.]+)$/);
|
|
26
|
+
if (m)
|
|
27
|
+
q = parseFloat(m[1]);
|
|
28
|
+
}
|
|
29
|
+
accepted.set(coding, q);
|
|
30
|
+
}
|
|
31
|
+
const brQ = accepted.get("br") ?? 0;
|
|
32
|
+
const gzQ = accepted.get("gzip") ?? 0;
|
|
33
|
+
if (options.brotli && brQ > 0 && (!options.gzip || brQ >= gzQ))
|
|
34
|
+
return "br";
|
|
35
|
+
if (options.gzip && gzQ > 0)
|
|
36
|
+
return "gzip";
|
|
37
|
+
if (options.brotli && brQ > 0)
|
|
38
|
+
return "br";
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const COMPRESSIBLE_RE = /^(text\/|application\/(json|javascript|xml|x-javascript|x-www-form-urlencoded|wasm)|image\/svg\+xml|font\/(woff|ttf|otf))/i;
|
|
42
|
+
const SKIP_RE = /\.(br|gz|zip|png|jpe?g|gif|webp|avif|ico|woff2?|mp4|webm)$/i;
|
|
43
|
+
export function isCompressible(contentType, pathname = "") {
|
|
44
|
+
if (!contentType)
|
|
45
|
+
return false;
|
|
46
|
+
if (!COMPRESSIBLE_RE.test(contentType))
|
|
47
|
+
return false;
|
|
48
|
+
if (SKIP_RE.test(pathname))
|
|
49
|
+
return false;
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
export function compressBuffer(data, encoding) {
|
|
53
|
+
if (encoding === "br") {
|
|
54
|
+
return zlib.brotliCompressSync(data, {
|
|
55
|
+
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5 },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return zlib.gzipSync(data, { level: 6 });
|
|
59
|
+
}
|
|
60
|
+
export function decompressBuffer(data, encoding) {
|
|
61
|
+
if (encoding === "br")
|
|
62
|
+
return zlib.brotliDecompressSync(data);
|
|
63
|
+
if (encoding === "gzip")
|
|
64
|
+
return zlib.gunzipSync(data);
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
/** Encoding suffix for ETags so each variant caches independently. */
|
|
68
|
+
export function etagForEncoding(etag, encoding) {
|
|
69
|
+
return `${etag}-${encoding}`;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=compression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compression.js","sourceRoot":"","sources":["../src/compression.ts"],"names":[],"mappings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AAK,EAAE,CAAC,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,QAAgB;IAC7D,IAAI,QAAQ,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;IAC9D,IAAI,QAAQ,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IACtD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,QAAyB;IACrE,OAAO,GAAG,IAAI,IAAI,QAAQ,EAAE,CAAC;AAC/B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
package/dist/mime.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime.d.ts","sourceRoot":"","sources":["../src/mime.ts"],"names":[],"mappings":"AAmBA,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAGpD"}
|
package/dist/mime.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const MIME_MAP = {
|
|
2
|
+
".html": "text/html; charset=utf-8",
|
|
3
|
+
".js": "text/javascript; charset=utf-8",
|
|
4
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
5
|
+
".css": "text/css; charset=utf-8",
|
|
6
|
+
".json": "application/json; charset=utf-8",
|
|
7
|
+
".svg": "image/svg+xml",
|
|
8
|
+
".png": "image/png",
|
|
9
|
+
".jpg": "image/jpeg",
|
|
10
|
+
".jpeg": "image/jpeg",
|
|
11
|
+
".gif": "image/gif",
|
|
12
|
+
".webp": "image/webp",
|
|
13
|
+
".ico": "image/x-icon",
|
|
14
|
+
".woff": "font/woff",
|
|
15
|
+
".woff2": "font/woff2",
|
|
16
|
+
".ttf": "font/ttf",
|
|
17
|
+
".txt": "text/plain; charset=utf-8",
|
|
18
|
+
};
|
|
19
|
+
export function getMimeType(filePath) {
|
|
20
|
+
const ext = filePath.slice(filePath.lastIndexOf(".")).toLowerCase();
|
|
21
|
+
return MIME_MAP[ext] ?? "application/octet-stream";
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=mime.js.map
|
package/dist/mime.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mime.js","sourceRoot":"","sources":["../src/mime.ts"],"names":[],"mappings":"AAAA,MAAM,QAAQ,GAA2B;IACvC,OAAO,EAAE,0BAA0B;IACnC,KAAK,EAAE,gCAAgC;IACvC,MAAM,EAAE,gCAAgC;IACxC,MAAM,EAAE,yBAAyB;IACjC,OAAO,EAAE,iCAAiC;IAC1C,MAAM,EAAE,eAAe;IACvB,MAAM,EAAE,WAAW;IACnB,MAAM,EAAE,YAAY;IACpB,OAAO,EAAE,YAAY;IACrB,MAAM,EAAE,WAAW;IACnB,OAAO,EAAE,YAAY;IACrB,MAAM,EAAE,cAAc;IACtB,OAAO,EAAE,WAAW;IACpB,QAAQ,EAAE,YAAY;IACtB,MAAM,EAAE,UAAU;IAClB,MAAM,EAAE,2BAA2B;CACpC,CAAC;AAEF,MAAM,UAAU,WAAW,CAAC,QAAgB;IAC1C,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;IACpE,OAAO,QAAQ,CAAC,GAAG,CAAC,IAAI,0BAA0B,CAAC;AACrD,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as http from "node:http";
|
|
2
|
+
import { type CompressionOptions } from "./compression.js";
|
|
3
|
+
import type { CompressionConfig } from "@frost.js/core";
|
|
4
|
+
export interface NodeServerOptions {
|
|
5
|
+
port?: number;
|
|
6
|
+
host?: string;
|
|
7
|
+
staticDirs?: string[];
|
|
8
|
+
handleRequest: (request: Request) => Promise<Response>;
|
|
9
|
+
/** Response compression settings (from frost.config compression.*). */
|
|
10
|
+
compression?: CompressionConfig | CompressionOptions;
|
|
11
|
+
}
|
|
12
|
+
export declare function createNodeServer(options: NodeServerOptions): http.Server;
|
|
13
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAGlC,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACjF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,aAAa,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvD,uEAAuE;IACvE,WAAW,CAAC,EAAE,iBAAiB,GAAG,kBAAkB,CAAC;CACtD;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,CAAC,MAAM,CA0DxE"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as http from "node:http";
|
|
2
|
+
import { isSafeRequestTarget, nodeReqToWebRequest, sendWebResponseToNode } from "./bridge.js";
|
|
3
|
+
import { tryServeStaticFile } from "./static.js";
|
|
4
|
+
import { toCompressionOptions } from "./compression.js";
|
|
5
|
+
export function createNodeServer(options) {
|
|
6
|
+
const { port = 3000, host = "localhost", staticDirs = [], handleRequest } = options;
|
|
7
|
+
const compression = options.compression
|
|
8
|
+
? "enabled" in options.compression && "threshold" in options.compression
|
|
9
|
+
? options.compression
|
|
10
|
+
: toCompressionOptions(options.compression)
|
|
11
|
+
: undefined;
|
|
12
|
+
const server = http.createServer(async (req, res) => {
|
|
13
|
+
try {
|
|
14
|
+
// 1. Health check endpoint
|
|
15
|
+
if (req.url === "/healthz") {
|
|
16
|
+
res.statusCode = 200;
|
|
17
|
+
res.setHeader("Content-Type", "application/json");
|
|
18
|
+
res.end(JSON.stringify({ status: "healthy", timestamp: Date.now() }));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
// 2. Reject malformed request targets before bridging (never throw).
|
|
22
|
+
if (!isSafeRequestTarget(req.url)) {
|
|
23
|
+
res.statusCode = 400;
|
|
24
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
25
|
+
res.end("Bad Request");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
// 3. Try static asset serving
|
|
29
|
+
if (tryServeStaticFile(req, res, { staticDirs, compression })) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// 4. Dispatch to Web standard request handler
|
|
33
|
+
const webRequest = nodeReqToWebRequest(req, `${host}:${port}`);
|
|
34
|
+
const webResponse = await handleRequest(webRequest);
|
|
35
|
+
// 5. Send back Web response (with content negotiation)
|
|
36
|
+
await sendWebResponseToNode(webResponse, res, compression);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
console.error("[Frost Adapter Node] Uncaught request error:", err);
|
|
40
|
+
if (!res.headersSent) {
|
|
41
|
+
res.statusCode = 500;
|
|
42
|
+
res.setHeader("Content-Type", "text/plain");
|
|
43
|
+
res.end("Internal Server Error");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
// Handle graceful shutdown
|
|
48
|
+
const shutdown = () => {
|
|
49
|
+
server.close(() => {
|
|
50
|
+
process.exit(0);
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
process.once("SIGINT", shutdown);
|
|
54
|
+
process.once("SIGTERM", shutdown);
|
|
55
|
+
return server;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,oBAAoB,EAA2B,MAAM,kBAAkB,CAAC;AAYjF,MAAM,UAAU,gBAAgB,CAAC,OAA0B;IACzD,MAAM,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,GAAG,WAAW,EAAE,UAAU,GAAG,EAAE,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IACpF,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW;QACrC,CAAC,CAAC,SAAS,IAAI,OAAO,CAAC,WAAW,IAAI,WAAW,IAAI,OAAO,CAAC,WAAW;YACtE,CAAC,CAAE,OAAO,CAAC,WAAkC;YAC7C,CAAC,CAAC,oBAAoB,CAAC,OAAO,CAAC,WAAgC,CAAC;QAClE,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EA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AAC;gBACrB,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;gBAC5C,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,2BAA2B;IAC3B,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;YAChB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAElC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/static.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type CompressionOptions } from "./compression.js";
|
|
3
|
+
export interface StaticServerOptions {
|
|
4
|
+
staticDirs: string[];
|
|
5
|
+
compression?: CompressionOptions;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Resolves a URL pathname to a real file inside `dir`, or null when the
|
|
9
|
+
* path escapes the root (traversal, absolute paths, drive letters, UNC,
|
|
10
|
+
* null bytes, or symlink escapes). Percent-escapes are decoded first.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveStaticPath(dir: string, pathname: string): string | null;
|
|
13
|
+
export declare function tryServeStaticFile(req: IncomingMessage, res: ServerResponse, options: StaticServerOptions): boolean;
|
|
14
|
+
//# sourceMappingURL=static.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"static.d.ts","sourceRoot":"","sources":["../src/static.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE,OAAO,EAKL,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAQD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAuC9E;AAyCD,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAsFT"}
|
package/dist/static.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { getMimeType } from "./mime.js";
|
|
4
|
+
import { compressBuffer, etagForEncoding, isCompressible, negotiateEncoding, } from "./compression.js";
|
|
5
|
+
/** True when `child` is `parent` or inside it (path-segment aware). */
|
|
6
|
+
function isInsideDir(parent, child) {
|
|
7
|
+
const rel = path.relative(parent, child);
|
|
8
|
+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolves a URL pathname to a real file inside `dir`, or null when the
|
|
12
|
+
* path escapes the root (traversal, absolute paths, drive letters, UNC,
|
|
13
|
+
* null bytes, or symlink escapes). Percent-escapes are decoded first.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveStaticPath(dir, pathname) {
|
|
16
|
+
if (!pathname || pathname.includes("\0") || pathname.includes("%00")) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
let decoded = pathname;
|
|
20
|
+
try {
|
|
21
|
+
decoded = decodeURIComponent(pathname);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// malformed escape — reject rather than guess
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
if (decoded.includes("\0"))
|
|
28
|
+
return null;
|
|
29
|
+
const root = path.resolve(dir);
|
|
30
|
+
// Strip query already handled by caller; drop leading separators so the
|
|
31
|
+
// result is always relative to root. Backslashes are separators on
|
|
32
|
+
// Windows and harmless-but-normalized elsewhere via path.resolve.
|
|
33
|
+
const rel = decoded.replace(/^[/\\]+/, "");
|
|
34
|
+
if (!rel)
|
|
35
|
+
return null;
|
|
36
|
+
const candidate = path.resolve(root, rel);
|
|
37
|
+
if (!isInsideDir(root, candidate)) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
// Symlink escape check: compare real paths when the file exists.
|
|
41
|
+
try {
|
|
42
|
+
const realRoot = fs.realpathSync(root);
|
|
43
|
+
const realCandidate = fs.realpathSync(candidate);
|
|
44
|
+
if (!isInsideDir(realRoot, realCandidate)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return realCandidate;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Doesn't exist (or realpath failed) — fall through to existsSync below.
|
|
51
|
+
return candidate;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function sendFile(req, res, filePath, opts) {
|
|
55
|
+
if (req.headers["if-none-match"] === opts.etag) {
|
|
56
|
+
res.statusCode = 304;
|
|
57
|
+
res.setHeader("ETag", opts.etag);
|
|
58
|
+
res.setHeader("Cache-Control", opts.cacheControl);
|
|
59
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
60
|
+
res.end();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
res.statusCode = 200;
|
|
64
|
+
res.setHeader("Content-Type", opts.contentType);
|
|
65
|
+
res.setHeader("Content-Length", opts.contentLength);
|
|
66
|
+
res.setHeader("ETag", opts.etag);
|
|
67
|
+
res.setHeader("Cache-Control", opts.cacheControl);
|
|
68
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
69
|
+
if (opts.contentEncoding) {
|
|
70
|
+
res.setHeader("Content-Encoding", opts.contentEncoding);
|
|
71
|
+
}
|
|
72
|
+
if (req.method === "HEAD") {
|
|
73
|
+
res.end();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
fs.createReadStream(filePath).pipe(res);
|
|
77
|
+
}
|
|
78
|
+
export function tryServeStaticFile(req, res, options) {
|
|
79
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const rawUrl = req.url ?? "/";
|
|
83
|
+
const pathname = rawUrl.split("?")[0];
|
|
84
|
+
const compression = options.compression;
|
|
85
|
+
const acceptEncoding = req.headers["accept-encoding"];
|
|
86
|
+
const encoding = compression?.enabled ? negotiateEncoding(acceptEncoding, compression) : null;
|
|
87
|
+
for (const dir of options.staticDirs) {
|
|
88
|
+
const fullPath = resolveStaticPath(dir, pathname);
|
|
89
|
+
if (!fullPath)
|
|
90
|
+
continue;
|
|
91
|
+
if (!fs.existsSync(fullPath))
|
|
92
|
+
continue;
|
|
93
|
+
const stat = fs.statSync(fullPath);
|
|
94
|
+
if (!stat.isFile())
|
|
95
|
+
continue;
|
|
96
|
+
const mime = getMimeType(fullPath);
|
|
97
|
+
const cacheControl = pathname.includes("/assets/")
|
|
98
|
+
? "public, max-age=31536000, immutable"
|
|
99
|
+
: "public, max-age=3600, must-revalidate";
|
|
100
|
+
const baseEtag = `W/"${stat.size}-${stat.mtimeMs.toString(16)}"`;
|
|
101
|
+
// 1. Pre-compressed variant built at `frost build` time (.br/.gz
|
|
102
|
+
// next to the original) — zero CPU cost per request.
|
|
103
|
+
if (encoding) {
|
|
104
|
+
const ext = encoding === "br" ? ".br" : ".gz";
|
|
105
|
+
const precompressed = fullPath + ext;
|
|
106
|
+
if (fs.existsSync(precompressed)) {
|
|
107
|
+
const pstat = fs.statSync(precompressed);
|
|
108
|
+
sendFile(req, res, precompressed, {
|
|
109
|
+
contentType: mime,
|
|
110
|
+
etag: etagForEncoding(baseEtag, encoding),
|
|
111
|
+
cacheControl,
|
|
112
|
+
contentLength: pstat.size,
|
|
113
|
+
contentEncoding: encoding,
|
|
114
|
+
});
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// 2. On-the-fly compression for compressible types above threshold.
|
|
119
|
+
if (encoding &&
|
|
120
|
+
stat.size >= (compression?.threshold ?? 1024) &&
|
|
121
|
+
isCompressible(mime, pathname)) {
|
|
122
|
+
const data = fs.readFileSync(fullPath);
|
|
123
|
+
const compressed = compressBuffer(data, encoding);
|
|
124
|
+
// Send buffered compressed body directly.
|
|
125
|
+
const etag = etagForEncoding(baseEtag, encoding);
|
|
126
|
+
if (req.headers["if-none-match"] === etag) {
|
|
127
|
+
res.statusCode = 304;
|
|
128
|
+
res.setHeader("ETag", etag);
|
|
129
|
+
res.setHeader("Cache-Control", cacheControl);
|
|
130
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
131
|
+
res.end();
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
res.statusCode = 200;
|
|
135
|
+
res.setHeader("Content-Type", mime);
|
|
136
|
+
res.setHeader("Content-Length", compressed.byteLength);
|
|
137
|
+
res.setHeader("ETag", etag);
|
|
138
|
+
res.setHeader("Cache-Control", cacheControl);
|
|
139
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
140
|
+
res.setHeader("Content-Encoding", encoding);
|
|
141
|
+
if (req.method === "HEAD") {
|
|
142
|
+
res.end();
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
res.end(compressed);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
sendFile(req, res, fullPath, {
|
|
149
|
+
contentType: mime,
|
|
150
|
+
etag: baseEtag,
|
|
151
|
+
cacheControl,
|
|
152
|
+
contentLength: stat.size,
|
|
153
|
+
});
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=static.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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AAE,CAAC;gBACV,OAAO,IAAI,CAAC;YACd,CAAC;YACD,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACpB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE;YAC3B,WAAW,EAAE,IAAI;YACjB,IAAI,EAAE,QAAQ;YACd,YAAY;YACZ,aAAa,EAAE,IAAI,CAAC,IAAI;SACzB,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frost.js/adapter-node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js http adapter for Frost: request bridge, static file serving, compression",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@frost.js/core": "^0.1.0",
|
|
16
|
+
"@frost.js/router": "^0.1.0",
|
|
17
|
+
"@frost.js/server": "^0.1.0",
|
|
18
|
+
"@frost.js/react": "^0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/ben-bader/Frost.git",
|
|
27
|
+
"directory": "packages/adapter-node"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"framework",
|
|
31
|
+
"frost",
|
|
32
|
+
"full-stack",
|
|
33
|
+
"react",
|
|
34
|
+
"ssr"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20"
|
|
38
|
+
},
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"author": "Frost contributors",
|
|
44
|
+
"homepage": "https://frostjs.dev",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/ben-bader/Frost/issues"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc --build && node ../../scripts/strip-dist-tests.mjs",
|
|
50
|
+
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\""
|
|
51
|
+
}
|
|
6
52
|
}
|