@siddharatha/adapter-node-rolldown 1.0.8 → 1.1.3
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 +480 -38
- package/files/shims.js +30 -4
- package/index.js +33 -164
- package/package.json +15 -16
- package/files/middlewares.js +0 -162
- package/files/telemetry.js +0 -126
package/files/index.js
CHANGED
|
@@ -1,46 +1,488 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import process from 'node:process';
|
|
3
3
|
import { handler } from 'HANDLER';
|
|
4
|
+
import { env, timeout_env } from 'ENV';
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
function getDefaultExportFromCjs (x) {
|
|
8
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getAugmentedNamespace(n) {
|
|
12
|
+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;
|
|
13
|
+
var f = n.default;
|
|
14
|
+
if (typeof f == "function") {
|
|
15
|
+
var a = function a () {
|
|
16
|
+
var isInstance = false;
|
|
17
|
+
try {
|
|
18
|
+
isInstance = this instanceof a;
|
|
19
|
+
} catch {}
|
|
20
|
+
if (isInstance) {
|
|
21
|
+
return Reflect.construct(f, arguments, this.constructor);
|
|
22
|
+
}
|
|
23
|
+
return f.apply(this, arguments);
|
|
24
|
+
};
|
|
25
|
+
a.prototype = f.prototype;
|
|
26
|
+
} else a = {};
|
|
27
|
+
Object.defineProperty(a, '__esModule', {value: true});
|
|
28
|
+
Object.keys(n).forEach(function (k) {
|
|
29
|
+
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
30
|
+
Object.defineProperty(a, k, d.get ? d : {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return n[k];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return a;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const require$2 = createRequire(import.meta.url);
|
|
41
|
+
function __require$1() { return require$2("node:http"); }
|
|
42
|
+
|
|
43
|
+
function every (arr, cb) {
|
|
44
|
+
var i=0, len=arr.length;
|
|
45
|
+
|
|
46
|
+
for (; i < len; i++) {
|
|
47
|
+
if (!cb(arr[i], i, arr)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const SEP = '/';
|
|
56
|
+
// Types ~> static, param, any, optional
|
|
57
|
+
const STYPE=0, PTYPE=1, ATYPE=2, OTYPE=3;
|
|
58
|
+
// Char Codes ~> / : *
|
|
59
|
+
const SLASH=47, COLON=58, ASTER=42, QMARK=63;
|
|
60
|
+
|
|
61
|
+
function strip(str) {
|
|
62
|
+
if (str === SEP) return str;
|
|
63
|
+
(str.charCodeAt(0) === SLASH) && (str=str.substring(1));
|
|
64
|
+
var len = str.length - 1;
|
|
65
|
+
return str.charCodeAt(len) === SLASH ? str.substring(0, len) : str;
|
|
27
66
|
}
|
|
28
67
|
|
|
29
|
-
|
|
30
|
-
|
|
68
|
+
function split(str) {
|
|
69
|
+
return (str=strip(str)) === SEP ? [SEP] : str.split(SEP);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isMatch(arr, obj, idx) {
|
|
73
|
+
idx = arr[idx];
|
|
74
|
+
return (obj.val === idx && obj.type === STYPE) || (idx === SEP ? obj.type > PTYPE : obj.type !== STYPE && (idx || '').endsWith(obj.end));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function match(str, all) {
|
|
78
|
+
var i=0, tmp, segs=split(str), len=segs.length, l;
|
|
79
|
+
var fn = isMatch.bind(isMatch, segs);
|
|
80
|
+
|
|
81
|
+
for (; i < all.length; i++) {
|
|
82
|
+
tmp = all[i];
|
|
83
|
+
if ((l=tmp.length) === len || (l < len && tmp[l-1].type === ATYPE) || (l > len && tmp[l-1].type === OTYPE)) {
|
|
84
|
+
if (every(tmp, fn)) return tmp;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function parse(str) {
|
|
92
|
+
if (str === SEP) {
|
|
93
|
+
return [{ old:str, type:STYPE, val:str, end:'' }];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var c, x, t, sfx, nxt=strip(str), i=-1, j=0, len=nxt.length, out=[];
|
|
97
|
+
|
|
98
|
+
while (++i < len) {
|
|
99
|
+
c = nxt.charCodeAt(i);
|
|
100
|
+
|
|
101
|
+
if (c === COLON) {
|
|
102
|
+
j = i + 1; // begining of param
|
|
103
|
+
t = PTYPE; // set type
|
|
104
|
+
x = 0; // reset mark
|
|
105
|
+
sfx = '';
|
|
106
|
+
|
|
107
|
+
while (i < len && nxt.charCodeAt(i) !== SLASH) {
|
|
108
|
+
c = nxt.charCodeAt(i);
|
|
109
|
+
if (c === QMARK) {
|
|
110
|
+
x=i; t=OTYPE;
|
|
111
|
+
} else if (c === 46 && sfx.length === 0) {
|
|
112
|
+
sfx = nxt.substring(x=i);
|
|
113
|
+
}
|
|
114
|
+
i++; // move on
|
|
115
|
+
}
|
|
31
116
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
117
|
+
out.push({
|
|
118
|
+
old: str,
|
|
119
|
+
type: t,
|
|
120
|
+
val: nxt.substring(j, x||i),
|
|
121
|
+
end: sfx
|
|
122
|
+
});
|
|
35
123
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
124
|
+
// shorten string & update pointers
|
|
125
|
+
nxt=nxt.substring(i); len-=i; i=0;
|
|
126
|
+
|
|
127
|
+
continue; // loop
|
|
128
|
+
} else if (c === ASTER) {
|
|
129
|
+
out.push({
|
|
130
|
+
old: str,
|
|
131
|
+
type: ATYPE,
|
|
132
|
+
val: nxt.substring(i),
|
|
133
|
+
end: ''
|
|
134
|
+
});
|
|
135
|
+
continue; // loop
|
|
136
|
+
} else {
|
|
137
|
+
j = i;
|
|
138
|
+
while (i < len && nxt.charCodeAt(i) !== SLASH) {
|
|
139
|
+
++i; // skip to next slash
|
|
140
|
+
}
|
|
141
|
+
out.push({
|
|
142
|
+
old: str,
|
|
143
|
+
type: STYPE,
|
|
144
|
+
val: nxt.substring(j, i),
|
|
145
|
+
end: ''
|
|
146
|
+
});
|
|
147
|
+
// shorten string & update pointers
|
|
148
|
+
nxt=nxt.substring(i); len-=i; i=j=0;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function exec(str, arr) {
|
|
156
|
+
var i=0, x, y, segs=split(str), out={};
|
|
157
|
+
for (; i < arr.length; i++) {
|
|
158
|
+
x=segs[i]; y=arr[i];
|
|
159
|
+
if (x === SEP) continue;
|
|
160
|
+
if (x !== void 0 && y.type | 2 === OTYPE) {
|
|
161
|
+
out[ y.val ] = x.replace(y.end, '');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
var matchit = /*#__PURE__*/Object.freeze({
|
|
168
|
+
__proto__: null,
|
|
169
|
+
exec: exec,
|
|
170
|
+
match: match,
|
|
171
|
+
parse: parse
|
|
40
172
|
});
|
|
41
173
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
174
|
+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(matchit);
|
|
175
|
+
|
|
176
|
+
var trouter;
|
|
177
|
+
var hasRequiredTrouter;
|
|
178
|
+
|
|
179
|
+
function requireTrouter () {
|
|
180
|
+
if (hasRequiredTrouter) return trouter;
|
|
181
|
+
hasRequiredTrouter = 1;
|
|
182
|
+
const { exec, match, parse } = require$$0;
|
|
183
|
+
|
|
184
|
+
class Trouter {
|
|
185
|
+
constructor(opts) {
|
|
186
|
+
this.opts = opts || {};
|
|
187
|
+
this.routes = {};
|
|
188
|
+
this.handlers = {};
|
|
189
|
+
|
|
190
|
+
this.all = this.add.bind(this, '*');
|
|
191
|
+
this.get = this.add.bind(this, 'GET');
|
|
192
|
+
this.head = this.add.bind(this, 'HEAD');
|
|
193
|
+
this.patch = this.add.bind(this, 'PATCH');
|
|
194
|
+
this.options = this.add.bind(this, 'OPTIONS');
|
|
195
|
+
this.connect = this.add.bind(this, 'CONNECT');
|
|
196
|
+
this.delete = this.add.bind(this, 'DELETE');
|
|
197
|
+
this.trace = this.add.bind(this, 'TRACE');
|
|
198
|
+
this.post = this.add.bind(this, 'POST');
|
|
199
|
+
this.put = this.add.bind(this, 'PUT');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
add(method, pattern, ...fns) {
|
|
203
|
+
// Save decoded pattern info
|
|
204
|
+
if (this.routes[method] === void 0) this.routes[method]=[];
|
|
205
|
+
this.routes[method].push(parse(pattern));
|
|
206
|
+
// Save route handler(s)
|
|
207
|
+
if (this.handlers[method] === void 0) this.handlers[method]={};
|
|
208
|
+
this.handlers[method][pattern] = fns;
|
|
209
|
+
// Allow chainable
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
find(method, url) {
|
|
214
|
+
let arr = match(url, this.routes[method] || []);
|
|
215
|
+
if (arr.length === 0) {
|
|
216
|
+
arr = match(url, this.routes[method='*'] || []);
|
|
217
|
+
if (!arr.length) return false;
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
params: exec(url, arr),
|
|
221
|
+
handlers: this.handlers[method][arr[0].old]
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
trouter = Trouter;
|
|
227
|
+
return trouter;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const require$1 = createRequire(import.meta.url);
|
|
231
|
+
function __require() { return require$1("node:querystring"); }
|
|
232
|
+
|
|
233
|
+
var url;
|
|
234
|
+
var hasRequiredUrl;
|
|
235
|
+
|
|
236
|
+
function requireUrl () {
|
|
237
|
+
if (hasRequiredUrl) return url;
|
|
238
|
+
hasRequiredUrl = 1;
|
|
239
|
+
url = function (req) {
|
|
240
|
+
let url = req.url;
|
|
241
|
+
if (url === void 0) return url;
|
|
242
|
+
|
|
243
|
+
let obj = req._parsedUrl;
|
|
244
|
+
if (obj && obj._raw === url) return obj;
|
|
245
|
+
|
|
246
|
+
obj = {};
|
|
247
|
+
obj.query = obj.search = null;
|
|
248
|
+
obj.href = obj.path = obj.pathname = url;
|
|
249
|
+
|
|
250
|
+
let idx = url.indexOf('?', 1);
|
|
251
|
+
if (idx !== -1) {
|
|
252
|
+
obj.search = url.substring(idx);
|
|
253
|
+
obj.query = obj.search.substring(1);
|
|
254
|
+
obj.pathname = url.substring(0, idx);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
obj._raw = url;
|
|
258
|
+
|
|
259
|
+
return (req._parsedUrl = obj);
|
|
260
|
+
};
|
|
261
|
+
return url;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
var polka$1;
|
|
265
|
+
var hasRequiredPolka;
|
|
266
|
+
|
|
267
|
+
function requirePolka () {
|
|
268
|
+
if (hasRequiredPolka) return polka$1;
|
|
269
|
+
hasRequiredPolka = 1;
|
|
270
|
+
const http = __require$1();
|
|
271
|
+
const Router = requireTrouter();
|
|
272
|
+
const { parse } = __require();
|
|
273
|
+
const parser = requireUrl();
|
|
274
|
+
|
|
275
|
+
function lead(x) {
|
|
276
|
+
return x.charCodeAt(0) === 47 ? x : ('/' + x);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function value(x) {
|
|
280
|
+
let y = x.indexOf('/', 1);
|
|
281
|
+
return y > 1 ? x.substring(0, y) : x;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function mutate(str, req) {
|
|
285
|
+
req.url = req.url.substring(str.length) || '/';
|
|
286
|
+
req.path = req.path.substring(str.length) || '/';
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function onError(err, req, res, next) {
|
|
290
|
+
let code = (res.statusCode = err.code || err.status || 500);
|
|
291
|
+
res.end(err.length && err || err.message || http.STATUS_CODES[code]);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
class Polka extends Router {
|
|
295
|
+
constructor(opts={}) {
|
|
296
|
+
super(opts);
|
|
297
|
+
this.apps = {};
|
|
298
|
+
this.wares = [];
|
|
299
|
+
this.bwares = {};
|
|
300
|
+
this.parse = parser;
|
|
301
|
+
this.server = opts.server;
|
|
302
|
+
this.handler = this.handler.bind(this);
|
|
303
|
+
this.onError = opts.onError || onError; // catch-all handler
|
|
304
|
+
this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { code:404 });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
add(method, pattern, ...fns) {
|
|
308
|
+
let base = lead(value(pattern));
|
|
309
|
+
if (this.apps[base] !== void 0) throw new Error(`Cannot mount ".${method.toLowerCase()}('${lead(pattern)}')" because a Polka application at ".use('${base}')" already exists! You should move this handler into your Polka application instead.`);
|
|
310
|
+
return super.add(method, pattern, ...fns);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
use(base, ...fns) {
|
|
314
|
+
if (typeof base === 'function') {
|
|
315
|
+
this.wares = this.wares.concat(base, fns);
|
|
316
|
+
} else if (base === '/') {
|
|
317
|
+
this.wares = this.wares.concat(fns);
|
|
318
|
+
} else {
|
|
319
|
+
base = lead(base);
|
|
320
|
+
fns.forEach(fn => {
|
|
321
|
+
if (fn instanceof Polka) {
|
|
322
|
+
this.apps[base] = fn;
|
|
323
|
+
} else {
|
|
324
|
+
let arr = this.bwares[base] || [];
|
|
325
|
+
arr.length > 0 || arr.push((r, _, nxt) => (mutate(base, r),nxt()));
|
|
326
|
+
this.bwares[base] = arr.concat(fn);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return this; // chainable
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
listen() {
|
|
334
|
+
(this.server = this.server || http.createServer()).on('request', this.handler);
|
|
335
|
+
this.server.listen.apply(this.server, arguments);
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
handler(req, res, info) {
|
|
340
|
+
info = info || this.parse(req);
|
|
341
|
+
let fns=[], arr=this.wares, obj=this.find(req.method, info.pathname);
|
|
342
|
+
req.originalUrl = req.originalUrl || req.url;
|
|
343
|
+
let base = value(req.path = info.pathname);
|
|
344
|
+
if (this.bwares[base] !== void 0) {
|
|
345
|
+
arr = arr.concat(this.bwares[base]);
|
|
346
|
+
}
|
|
347
|
+
if (obj) {
|
|
348
|
+
fns = obj.handlers;
|
|
349
|
+
req.params = obj.params;
|
|
350
|
+
} else if (this.apps[base] !== void 0) {
|
|
351
|
+
mutate(base, req); info.pathname=req.path; //=> updates
|
|
352
|
+
fns.push(this.apps[base].handler.bind(null, req, res, info));
|
|
353
|
+
} else if (fns.length === 0) {
|
|
354
|
+
fns.push(this.onNoMatch);
|
|
355
|
+
}
|
|
356
|
+
// Grab addl values from `info`
|
|
357
|
+
req.search = info.search;
|
|
358
|
+
req.query = parse(info.query);
|
|
359
|
+
// Exit if only a single function
|
|
360
|
+
let i=0, len=arr.length, num=fns.length;
|
|
361
|
+
if (len === i && num === 1) return fns[0](req, res);
|
|
362
|
+
// Otherwise loop thru all middlware
|
|
363
|
+
let next = err => err ? this.onError(err, req, res, next) : loop();
|
|
364
|
+
let loop = _ => res.finished || (i < len) && arr[i++](req, res, next);
|
|
365
|
+
arr = arr.concat(fns);
|
|
366
|
+
len += num;
|
|
367
|
+
loop(); // init
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
polka$1 = opts => new Polka(opts);
|
|
372
|
+
return polka$1;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
var polkaExports = requirePolka();
|
|
376
|
+
var polka = /*@__PURE__*/getDefaultExportFromCjs(polkaExports);
|
|
377
|
+
|
|
378
|
+
const path = env('SOCKET_PATH', false);
|
|
379
|
+
const host = env('HOST', '0.0.0.0');
|
|
380
|
+
const port = env('PORT', !path && '3000');
|
|
381
|
+
|
|
382
|
+
const shutdown_timeout = parseInt(env('SHUTDOWN_TIMEOUT', '30'));
|
|
383
|
+
const idle_timeout = parseInt(env('IDLE_TIMEOUT', '0'));
|
|
384
|
+
const listen_pid = parseInt(env('LISTEN_PID', '0'));
|
|
385
|
+
const listen_fds = parseInt(env('LISTEN_FDS', '0'));
|
|
386
|
+
// https://www.freedesktop.org/software/systemd/man/latest/sd_listen_fds.html
|
|
387
|
+
const SD_LISTEN_FDS_START = 3;
|
|
388
|
+
|
|
389
|
+
if (listen_pid !== 0 && listen_pid !== process.pid) {
|
|
390
|
+
throw new Error(`received LISTEN_PID ${listen_pid} but current process id is ${process.pid}`);
|
|
391
|
+
}
|
|
392
|
+
if (listen_fds > 1) {
|
|
393
|
+
throw new Error(
|
|
394
|
+
`only one socket is allowed for socket activation, but LISTEN_FDS was set to ${listen_fds}`
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const socket_activation = listen_pid === process.pid && listen_fds === 1;
|
|
399
|
+
|
|
400
|
+
let requests = 0;
|
|
401
|
+
/** @type {NodeJS.Timeout | void} */
|
|
402
|
+
let shutdown_timeout_id;
|
|
403
|
+
/** @type {NodeJS.Timeout | void} */
|
|
404
|
+
let idle_timeout_id;
|
|
405
|
+
|
|
406
|
+
// Initialize the HTTP server here so that we can set properties before starting to listen.
|
|
407
|
+
// Otherwise, polka delays creating the server until listen() is called. Settings these
|
|
408
|
+
// properties after the server has started listening could lead to race conditions.
|
|
409
|
+
const httpServer = http.createServer();
|
|
410
|
+
|
|
411
|
+
const keep_alive_timeout = timeout_env('KEEP_ALIVE_TIMEOUT');
|
|
412
|
+
if (keep_alive_timeout !== undefined) {
|
|
413
|
+
// Convert the keep-alive timeout from seconds to milliseconds (the unit Node.js expects).
|
|
414
|
+
httpServer.keepAliveTimeout = keep_alive_timeout * 1000;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const headers_timeout = timeout_env('HEADERS_TIMEOUT');
|
|
418
|
+
if (headers_timeout !== undefined) {
|
|
419
|
+
// Convert the headers timeout from seconds to milliseconds (the unit Node.js expects).
|
|
420
|
+
httpServer.headersTimeout = headers_timeout * 1000;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const server = polka({ server: httpServer }).use(handler);
|
|
424
|
+
|
|
425
|
+
if (socket_activation) {
|
|
426
|
+
server.listen({ fd: SD_LISTEN_FDS_START }, () => {
|
|
427
|
+
console.log(`Listening on file descriptor ${SD_LISTEN_FDS_START}`);
|
|
428
|
+
});
|
|
429
|
+
} else {
|
|
430
|
+
server.listen({ path, host, port }, () => {
|
|
431
|
+
console.log(`Listening on ${path || `http://${host}:${port}`}`);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** @param {'SIGINT' | 'SIGTERM' | 'IDLE'} reason */
|
|
436
|
+
function graceful_shutdown(reason) {
|
|
437
|
+
if (shutdown_timeout_id) return;
|
|
438
|
+
|
|
439
|
+
// If a connection was opened with a keep-alive header close() will wait for the connection to
|
|
440
|
+
// time out rather than close it even if it is not handling any requests, so call this first
|
|
441
|
+
httpServer.closeIdleConnections();
|
|
442
|
+
|
|
443
|
+
httpServer.close((error) => {
|
|
444
|
+
// occurs if the server is already closed
|
|
445
|
+
if (error) return;
|
|
446
|
+
|
|
447
|
+
if (shutdown_timeout_id) {
|
|
448
|
+
clearTimeout(shutdown_timeout_id);
|
|
449
|
+
}
|
|
450
|
+
if (idle_timeout_id) {
|
|
451
|
+
clearTimeout(idle_timeout_id);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// @ts-expect-error custom events cannot be typed
|
|
455
|
+
process.emit('sveltekit:shutdown', reason);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
shutdown_timeout_id = setTimeout(() => httpServer.closeAllConnections(), shutdown_timeout * 1000);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
httpServer.on(
|
|
462
|
+
'request',
|
|
463
|
+
/** @param {import('node:http').IncomingMessage} req */
|
|
464
|
+
(req) => {
|
|
465
|
+
requests++;
|
|
466
|
+
|
|
467
|
+
if (socket_activation && idle_timeout_id) {
|
|
468
|
+
idle_timeout_id = clearTimeout(idle_timeout_id);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
req.on('close', () => {
|
|
472
|
+
requests--;
|
|
473
|
+
|
|
474
|
+
if (shutdown_timeout_id) {
|
|
475
|
+
// close connections as soon as they become idle, so they don't accept new requests
|
|
476
|
+
httpServer.closeIdleConnections();
|
|
477
|
+
}
|
|
478
|
+
if (requests === 0 && socket_activation && idle_timeout) {
|
|
479
|
+
idle_timeout_id = setTimeout(() => graceful_shutdown('IDLE'), idle_timeout * 1000);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
process.on('SIGTERM', graceful_shutdown);
|
|
486
|
+
process.on('SIGINT', graceful_shutdown);
|
|
487
|
+
|
|
488
|
+
export { host, path, port, server };
|
package/files/shims.js
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import buffer from 'node:buffer';
|
|
2
|
+
import { webcrypto } from 'node:crypto';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// `buffer.File` was added in Node 18.13.0 while the `File` global was added in Node 20.0.0
|
|
5
|
+
const File = /** @type {import('node:buffer') & { File?: File}} */ (buffer).File;
|
|
6
|
+
|
|
7
|
+
/** @type {Record<string, any>} */
|
|
8
|
+
const globals = {
|
|
9
|
+
crypto: webcrypto,
|
|
10
|
+
File
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// exported for dev/preview and node environments
|
|
14
|
+
/**
|
|
15
|
+
* Make various web APIs available as globals:
|
|
16
|
+
* - `crypto`
|
|
17
|
+
* - `File`
|
|
18
|
+
*/
|
|
19
|
+
function installPolyfills() {
|
|
20
|
+
for (const name in globals) {
|
|
21
|
+
if (name in globalThis) continue;
|
|
22
|
+
|
|
23
|
+
Object.defineProperty(globalThis, name, {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: globals[name]
|
|
28
|
+
});
|
|
29
|
+
}
|
|
6
30
|
}
|
|
31
|
+
|
|
32
|
+
installPolyfills();
|