@siddharatha/adapter-node-rolldown 1.0.9 → 1.1.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.
- package/files/env.js +92 -48
- package/files/handler.js +1457 -24
- package/files/index.js +477 -29
- package/files/shims.js +30 -4
- package/index.js +61 -162
- package/package.json +15 -16
- package/files/middlewares.js +0 -162
- package/files/telemetry.js +0 -126
package/files/env.js
CHANGED
|
@@ -1,50 +1,94 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
|
|
3
|
+
/* global ENV_PREFIX */
|
|
4
|
+
|
|
5
|
+
const expected = new Set([
|
|
6
|
+
'SOCKET_PATH',
|
|
7
|
+
'HOST',
|
|
8
|
+
'PORT',
|
|
9
|
+
'ORIGIN',
|
|
10
|
+
'XFF_DEPTH',
|
|
11
|
+
'ADDRESS_HEADER',
|
|
12
|
+
'PROTOCOL_HEADER',
|
|
13
|
+
'HOST_HEADER',
|
|
14
|
+
'PORT_HEADER',
|
|
15
|
+
'BODY_SIZE_LIMIT',
|
|
16
|
+
'SHUTDOWN_TIMEOUT',
|
|
17
|
+
'IDLE_TIMEOUT',
|
|
18
|
+
'KEEP_ALIVE_TIMEOUT',
|
|
19
|
+
'HEADERS_TIMEOUT'
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
const expected_unprefixed = new Set(['LISTEN_PID', 'LISTEN_FDS']);
|
|
23
|
+
|
|
24
|
+
if (ENV_PREFIX) {
|
|
25
|
+
for (const name in process.env) {
|
|
26
|
+
if (name.startsWith(ENV_PREFIX)) {
|
|
27
|
+
const unprefixed = name.slice(ENV_PREFIX.length);
|
|
28
|
+
if (!expected.has(unprefixed)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`You should change envPrefix (${ENV_PREFIX}) to avoid conflicts with existing environment variables — unexpectedly saw ${name}`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} name
|
|
39
|
+
* @param {any} fallback
|
|
40
|
+
*/
|
|
41
|
+
function env(name, fallback) {
|
|
42
|
+
const prefix = expected_unprefixed.has(name) ? '' : ENV_PREFIX;
|
|
43
|
+
const prefixed = prefix + name;
|
|
44
|
+
return prefixed in process.env ? process.env[prefixed] : fallback;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const integer_regexp = /^\d+$/;
|
|
48
|
+
|
|
1
49
|
/**
|
|
2
|
-
*
|
|
50
|
+
* Throw a consistently-structured parsing error for environment variables.
|
|
51
|
+
* @param {string} name
|
|
52
|
+
* @param {any} value
|
|
53
|
+
* @param {string} description
|
|
54
|
+
* @returns {never}
|
|
3
55
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
maxRequestsPerSocket: parseInt(process.env.MAX_REQUESTS_PER_SOCKET || '0', 10),
|
|
44
|
-
|
|
45
|
-
// Origin for CORS (if needed)
|
|
46
|
-
origin: process.env.ORIGIN || undefined,
|
|
47
|
-
|
|
48
|
-
// XFF header handling (trust proxy)
|
|
49
|
-
trustProxy: process.env.TRUST_PROXY === 'true'
|
|
50
|
-
};
|
|
56
|
+
function parsing_error(name, value, description) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Invalid value for environment variable ${name}: ${JSON.stringify(value)} (${description})`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check the environment for a timeout value (non-negative integer) in seconds.
|
|
64
|
+
* @param {string} name
|
|
65
|
+
* @param {number} [fallback]
|
|
66
|
+
* @returns {number | undefined}
|
|
67
|
+
*/
|
|
68
|
+
function timeout_env(name, fallback) {
|
|
69
|
+
const raw = env(name, fallback);
|
|
70
|
+
if (!raw) {
|
|
71
|
+
return fallback;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!integer_regexp.test(raw)) {
|
|
75
|
+
parsing_error(name, raw, 'should be a non-negative integer');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const parsed = Number.parseInt(raw, 10);
|
|
79
|
+
|
|
80
|
+
// We don't technically need to check `Number.isNaN` because the value already passed the regexp test.
|
|
81
|
+
// However, just in case there's some new codepath introduced somewhere down the line, it's probably good
|
|
82
|
+
// to stick this in here.
|
|
83
|
+
if (Number.isNaN(parsed)) {
|
|
84
|
+
parsing_error(name, raw, 'should be a non-negative integer');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (parsed < 0) {
|
|
88
|
+
parsing_error(name, raw, 'should be a non-negative integer');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { env, timeout_env };
|