@siddharatha/adapter-node-rolldown 1.1.6 → 1.1.7
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 +21 -86
- package/files/handler.js +865 -1231
- package/files/index.js +223 -469
- package/files/shims.js +12 -13
- package/package.json +55 -50
package/files/index.js
CHANGED
|
@@ -1,488 +1,242 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
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
|
-
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;
|
|
66
|
-
}
|
|
67
|
-
|
|
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
|
-
}
|
|
1
|
+
import { handler } from "HANDLER";
|
|
2
|
+
import { env } from "ENV";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import { setImmediate } from "node:timers";
|
|
5
|
+
import * as qs from "node:querystring";
|
|
6
|
+
|
|
7
|
+
//#region node_modules/.pnpm/regexparam@3.0.0/node_modules/regexparam/dist/index.mjs
|
|
8
|
+
/**
|
|
9
|
+
* @param {string|RegExp} input The route pattern
|
|
10
|
+
* @param {boolean} [loose] Allow open-ended matching. Ignored with `RegExp` input.
|
|
11
|
+
*/
|
|
12
|
+
function parse$1(input, loose) {
|
|
13
|
+
if (input instanceof RegExp) return {
|
|
14
|
+
keys: false,
|
|
15
|
+
pattern: input
|
|
16
|
+
};
|
|
17
|
+
var c, o, tmp, ext, keys = [], pattern = "", arr = input.split("/");
|
|
18
|
+
arr[0] || arr.shift();
|
|
19
|
+
while (tmp = arr.shift()) {
|
|
20
|
+
c = tmp[0];
|
|
21
|
+
if (c === "*") {
|
|
22
|
+
keys.push(c);
|
|
23
|
+
pattern += tmp[1] === "?" ? "(?:/(.*))?" : "/(.*)";
|
|
24
|
+
} else if (c === ":") {
|
|
25
|
+
o = tmp.indexOf("?", 1);
|
|
26
|
+
ext = tmp.indexOf(".", 1);
|
|
27
|
+
keys.push(tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length));
|
|
28
|
+
pattern += !!~o && !~ext ? "(?:/([^/]+?))?" : "/([^/]+?)";
|
|
29
|
+
if (!!~ext) pattern += (!!~o ? "?" : "") + "\\" + tmp.substring(ext);
|
|
30
|
+
} else pattern += "/" + tmp;
|
|
86
31
|
}
|
|
87
|
-
|
|
88
|
-
|
|
32
|
+
return {
|
|
33
|
+
keys,
|
|
34
|
+
pattern: new RegExp("^" + pattern + (loose ? "(?=$|/)" : "/?$"), "i")
|
|
35
|
+
};
|
|
89
36
|
}
|
|
90
37
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region node_modules/.pnpm/trouter@4.0.0/node_modules/trouter/index.mjs
|
|
40
|
+
const MAP = {
|
|
41
|
+
"": 0,
|
|
42
|
+
GET: 1,
|
|
43
|
+
HEAD: 2,
|
|
44
|
+
PATCH: 3,
|
|
45
|
+
OPTIONS: 4,
|
|
46
|
+
CONNECT: 5,
|
|
47
|
+
DELETE: 6,
|
|
48
|
+
TRACE: 7,
|
|
49
|
+
POST: 8,
|
|
50
|
+
PUT: 9
|
|
51
|
+
};
|
|
52
|
+
var Trouter = class {
|
|
53
|
+
constructor() {
|
|
54
|
+
this.routes = [];
|
|
55
|
+
this.all = this.add.bind(this, "");
|
|
56
|
+
this.get = this.add.bind(this, "GET");
|
|
57
|
+
this.head = this.add.bind(this, "HEAD");
|
|
58
|
+
this.patch = this.add.bind(this, "PATCH");
|
|
59
|
+
this.options = this.add.bind(this, "OPTIONS");
|
|
60
|
+
this.connect = this.add.bind(this, "CONNECT");
|
|
61
|
+
this.delete = this.add.bind(this, "DELETE");
|
|
62
|
+
this.trace = this.add.bind(this, "TRACE");
|
|
63
|
+
this.post = this.add.bind(this, "POST");
|
|
64
|
+
this.put = this.add.bind(this, "PUT");
|
|
94
65
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
}
|
|
116
|
-
|
|
117
|
-
out.push({
|
|
118
|
-
old: str,
|
|
119
|
-
type: t,
|
|
120
|
-
val: nxt.substring(j, x||i),
|
|
121
|
-
end: sfx
|
|
122
|
-
});
|
|
123
|
-
|
|
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
|
-
}
|
|
66
|
+
use(route, ...fns) {
|
|
67
|
+
let handlers = [].concat.apply([], fns);
|
|
68
|
+
let { keys, pattern } = parse$1(route, true);
|
|
69
|
+
this.routes.push({
|
|
70
|
+
keys,
|
|
71
|
+
pattern,
|
|
72
|
+
method: "",
|
|
73
|
+
handlers,
|
|
74
|
+
midx: MAP[""]
|
|
75
|
+
});
|
|
76
|
+
return this;
|
|
150
77
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
78
|
+
add(method, route, ...fns) {
|
|
79
|
+
let { keys, pattern } = parse$1(route);
|
|
80
|
+
let handlers = [].concat.apply([], fns);
|
|
81
|
+
this.routes.push({
|
|
82
|
+
keys,
|
|
83
|
+
pattern,
|
|
84
|
+
method,
|
|
85
|
+
handlers,
|
|
86
|
+
midx: MAP[method]
|
|
87
|
+
});
|
|
88
|
+
return this;
|
|
163
89
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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;
|
|
90
|
+
find(method, url) {
|
|
91
|
+
let midx = MAP[method];
|
|
92
|
+
let isHEAD = midx === 2;
|
|
93
|
+
let i = 0, j = 0, k, tmp, arr = this.routes;
|
|
94
|
+
let matches = [], params = {}, handlers = [];
|
|
95
|
+
for (; i < arr.length; i++) {
|
|
96
|
+
tmp = arr[i];
|
|
97
|
+
if (tmp.midx === midx || tmp.midx === 0 || isHEAD && tmp.midx === 1) {
|
|
98
|
+
if (tmp.keys === false) {
|
|
99
|
+
matches = tmp.pattern.exec(url);
|
|
100
|
+
if (matches === null) continue;
|
|
101
|
+
if (matches.groups !== void 0) for (k in matches.groups) params[k] = matches.groups[k];
|
|
102
|
+
tmp.handlers.length > 1 ? handlers = handlers.concat(tmp.handlers) : handlers.push(tmp.handlers[0]);
|
|
103
|
+
} else if (tmp.keys.length > 0) {
|
|
104
|
+
matches = tmp.pattern.exec(url);
|
|
105
|
+
if (matches === null) continue;
|
|
106
|
+
for (j = 0; j < tmp.keys.length;) params[tmp.keys[j]] = matches[++j];
|
|
107
|
+
tmp.handlers.length > 1 ? handlers = handlers.concat(tmp.handlers) : handlers.push(tmp.handlers[0]);
|
|
108
|
+
} else if (tmp.pattern.test(url)) tmp.handlers.length > 1 ? handlers = handlers.concat(tmp.handlers) : handlers.push(tmp.handlers[0]);
|
|
218
109
|
}
|
|
219
|
-
return {
|
|
220
|
-
params: exec(url, arr),
|
|
221
|
-
handlers: this.handlers[method][arr[0].old]
|
|
222
|
-
};
|
|
223
110
|
}
|
|
111
|
+
return {
|
|
112
|
+
params,
|
|
113
|
+
handlers
|
|
114
|
+
};
|
|
224
115
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
let idx = url.indexOf('?', 1);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region node_modules/.pnpm/@polka+url@1.0.0-next.29/node_modules/@polka/url/build.mjs
|
|
120
|
+
/**
|
|
121
|
+
* @typedef ParsedURL
|
|
122
|
+
* @type {import('.').ParsedURL}
|
|
123
|
+
*/
|
|
124
|
+
/**
|
|
125
|
+
* @typedef Request
|
|
126
|
+
* @property {string} url
|
|
127
|
+
* @property {ParsedURL} _parsedUrl
|
|
128
|
+
*/
|
|
129
|
+
/**
|
|
130
|
+
* @param {Request} req
|
|
131
|
+
* @returns {ParsedURL|void}
|
|
132
|
+
*/
|
|
133
|
+
function parse(req) {
|
|
134
|
+
let raw = req.url;
|
|
135
|
+
if (raw == null) return;
|
|
136
|
+
let prev = req._parsedUrl;
|
|
137
|
+
if (prev && prev.raw === raw) return prev;
|
|
138
|
+
let pathname = raw, search = "", query, hash;
|
|
139
|
+
if (raw.length > 1) {
|
|
140
|
+
let idx = raw.indexOf("#", 1);
|
|
251
141
|
if (idx !== -1) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
obj.pathname = url.substring(0, idx);
|
|
142
|
+
hash = raw.substring(idx);
|
|
143
|
+
pathname = raw.substring(0, idx);
|
|
255
144
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
145
|
+
idx = pathname.indexOf("?", 1);
|
|
146
|
+
if (idx !== -1) {
|
|
147
|
+
search = pathname.substring(idx);
|
|
148
|
+
pathname = pathname.substring(0, idx);
|
|
149
|
+
if (search.length > 1) query = qs.parse(search.substring(1));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return req._parsedUrl = {
|
|
153
|
+
pathname,
|
|
154
|
+
search,
|
|
155
|
+
query,
|
|
156
|
+
hash,
|
|
157
|
+
raw
|
|
260
158
|
};
|
|
261
|
-
return url;
|
|
262
159
|
}
|
|
263
160
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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]);
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region node_modules/.pnpm/polka@1.0.0-next.28/node_modules/polka/build.mjs
|
|
163
|
+
function onError(err, req, res) {
|
|
164
|
+
let code = typeof err.status === "number" && err.status;
|
|
165
|
+
code = res.statusCode = code && code >= 100 ? code : 500;
|
|
166
|
+
if (typeof err === "string" || Buffer.isBuffer(err)) res.end(err);
|
|
167
|
+
else res.end(err.message || http.STATUS_CODES[code]);
|
|
168
|
+
}
|
|
169
|
+
const mount = (fn) => fn instanceof Polka ? fn.attach : fn;
|
|
170
|
+
var Polka = class Polka extends Trouter {
|
|
171
|
+
constructor(opts = {}) {
|
|
172
|
+
super();
|
|
173
|
+
this.parse = parse;
|
|
174
|
+
this.server = opts.server;
|
|
175
|
+
this.handler = this.handler.bind(this);
|
|
176
|
+
this.onError = opts.onError || onError;
|
|
177
|
+
this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { status: 404 });
|
|
178
|
+
this.attach = (req, res) => setImmediate(this.handler, req, res);
|
|
292
179
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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);
|
|
180
|
+
use(base, ...fns) {
|
|
181
|
+
if (base === "/") super.use(base, fns.map(mount));
|
|
182
|
+
else if (typeof base === "function" || base instanceof Polka) super.use("/", [base, ...fns].map(mount));
|
|
183
|
+
else super.use(base, (req, _, next) => {
|
|
184
|
+
if (typeof base === "string") {
|
|
185
|
+
let len = base.length;
|
|
186
|
+
base.startsWith("/") || len++;
|
|
187
|
+
req.url = req.url.substring(len) || "/";
|
|
188
|
+
req.path = req.path.substring(len) || "/";
|
|
318
189
|
} else {
|
|
319
|
-
|
|
320
|
-
|
|
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
|
-
});
|
|
190
|
+
req.url = req.url.replace(base, "") || "/";
|
|
191
|
+
req.path = req.path.replace(base, "") || "/";
|
|
329
192
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
-
}
|
|
193
|
+
if (req.url.charAt(0) !== "/") req.url = "/" + req.url;
|
|
194
|
+
next();
|
|
195
|
+
}, fns.map(mount), (req, _, next) => {
|
|
196
|
+
req.path = req._parsedUrl.pathname;
|
|
197
|
+
req.url = req.path + req._parsedUrl.search;
|
|
198
|
+
next();
|
|
199
|
+
});
|
|
200
|
+
return this;
|
|
369
201
|
}
|
|
202
|
+
listen() {
|
|
203
|
+
(this.server = this.server || http.createServer()).on("request", this.attach);
|
|
204
|
+
this.server.listen.apply(this.server, arguments);
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
handler(req, res, next) {
|
|
208
|
+
let info = this.parse(req), path$1 = info.pathname;
|
|
209
|
+
let obj = this.find(req.method, req.path = path$1);
|
|
210
|
+
req.url = path$1 + info.search;
|
|
211
|
+
req.originalUrl = req.originalUrl || req.url;
|
|
212
|
+
req.query = info.query || {};
|
|
213
|
+
req.search = info.search;
|
|
214
|
+
req.params = obj.params;
|
|
215
|
+
if (path$1.length > 1 && path$1.indexOf("%", 1) !== -1) for (let k in req.params) try {
|
|
216
|
+
req.params[k] = decodeURIComponent(req.params[k]);
|
|
217
|
+
} catch (e) {}
|
|
218
|
+
let i = 0, arr = obj.handlers.concat(this.onNoMatch), len = arr.length;
|
|
219
|
+
let loop = async () => res.finished || i < len && arr[i++](req, res, next);
|
|
220
|
+
(next = next || ((err) => err ? this.onError(err, req, res, next) : loop().catch(next)))();
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function build_default(opts) {
|
|
224
|
+
return new Polka(opts);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/index.js
|
|
229
|
+
const path = env("SOCKET_PATH", false);
|
|
230
|
+
const host = env("HOST", "0.0.0.0");
|
|
231
|
+
const port = env("PORT", !path && "3000");
|
|
232
|
+
const server = build_default().use(handler);
|
|
233
|
+
server.listen({
|
|
234
|
+
path,
|
|
235
|
+
host,
|
|
236
|
+
port
|
|
237
|
+
}, () => {
|
|
238
|
+
console.log(`Listening on ${path ? path : host + ":" + port}`);
|
|
239
|
+
});
|
|
370
240
|
|
|
371
|
-
|
|
372
|
-
|
|
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 };
|
|
241
|
+
//#endregion
|
|
242
|
+
export { host, path, port, server };
|