dx-server 0.12.2 → 0.13.0-alpha.2
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 +417 -293
- package/{cjs → lib}/body.d.ts +2 -3
- package/lib/body.js +10 -0
- package/lib/body.js.map +1 -0
- package/{esm → lib}/bodyHelpers.d.ts +2 -4
- package/lib/bodyHelpers.js +102 -0
- package/lib/bodyHelpers.js.map +1 -0
- package/{esm → lib}/dx.d.ts +6 -9
- package/lib/dx.js +133 -0
- package/lib/dx.js.map +1 -0
- package/{cjs → lib}/dxHelpers.d.ts +2 -5
- package/lib/dxHelpers.js +135 -0
- package/lib/dxHelpers.js.map +1 -0
- package/lib/helpers.js.map +1 -0
- package/{cjs → lib}/index.d.ts +1 -2
- package/{esm/index.d.ts → lib/index.js} +1 -2
- package/lib/index.js.map +1 -0
- package/lib/logger.d.ts +4 -0
- package/lib/logger.js +64 -0
- package/lib/logger.js.map +1 -0
- package/lib/router.d.ts +42 -0
- package/lib/router.js +43 -0
- package/lib/router.js.map +1 -0
- package/lib/static.js +23 -0
- package/lib/static.js.map +1 -0
- package/{cjs → lib}/staticHelpers.d.ts +5 -3
- package/lib/staticHelpers.js +202 -0
- package/lib/staticHelpers.js.map +1 -0
- package/{cjs → lib}/stream.d.ts +3 -8
- package/lib/stream.js +96 -0
- package/lib/stream.js.map +1 -0
- package/lib/vendors/contentType.js +64 -0
- package/lib/vendors/contentType.js.map +1 -0
- package/{cjs → lib}/vendors/etag.d.ts +2 -5
- package/lib/vendors/etag.js +94 -0
- package/lib/vendors/etag.js.map +1 -0
- package/{cjs → lib}/vendors/fresh.d.ts +2 -2
- package/lib/vendors/fresh.js +88 -0
- package/lib/vendors/fresh.js.map +1 -0
- package/lib/vendors/mime.d.ts +1 -0
- package/lib/vendors/mime.js +35 -0
- package/lib/vendors/mime.js.map +1 -0
- package/{cjs → lib}/vendors/mimeDb.d.ts +2544 -2544
- package/lib/vendors/mimeDb.js +9435 -0
- package/lib/vendors/mimeDb.js.map +1 -0
- package/{cjs → lib}/vendors/mimeScore.d.ts +1 -1
- package/lib/vendors/mimeScore.js +44 -0
- package/lib/vendors/mimeScore.js.map +1 -0
- package/{cjs → lib}/vendors/onFinished.d.ts +1 -1
- package/lib/vendors/onFinished.js +231 -0
- package/lib/vendors/rangeParser.d.ts +12 -0
- package/lib/vendors/rangeParser.js +108 -0
- package/lib/vendors/rangeParser.js.map +1 -0
- package/package.json +32 -36
- package/cjs/body.js +0 -14
- package/cjs/bodyHelpers.d.ts +0 -16
- package/cjs/bodyHelpers.js +0 -101
- package/cjs/connect.d.ts +0 -5
- package/cjs/connect.js +0 -44
- package/cjs/dx.d.ts +0 -46
- package/cjs/dx.js +0 -144
- package/cjs/dxHelpers.js +0 -123
- package/cjs/express.d.ts +0 -4
- package/cjs/express.js +0 -43
- package/cjs/helpers.js +0 -14
- package/cjs/index.js +0 -38
- package/cjs/logger.d.ts +0 -3
- package/cjs/logger.js +0 -61
- package/cjs/package.json +0 -3
- package/cjs/polyfillWithResolvers.d.ts +0 -1
- package/cjs/polyfillWithResolvers.js +0 -17
- package/cjs/router.js +0 -47
- package/cjs/static.js +0 -27
- package/cjs/staticHelpers.js +0 -195
- package/cjs/stream.js +0 -97
- package/cjs/vendors/contentType.js +0 -92
- package/cjs/vendors/etag.js +0 -136
- package/cjs/vendors/fresh.js +0 -102
- package/cjs/vendors/mime.d.ts +0 -1
- package/cjs/vendors/mime.js +0 -42
- package/cjs/vendors/mimeDb.js +0 -9417
- package/cjs/vendors/mimeScore.js +0 -50
- package/cjs/vendors/onFinished.js +0 -245
- package/cjs/vendors/rangeParser.d.ts +0 -10
- package/cjs/vendors/rangeParser.js +0 -126
- package/esm/body.d.ts +0 -8
- package/esm/body.js +0 -11
- package/esm/bodyHelpers.js +0 -90
- package/esm/connect.d.ts +0 -5
- package/esm/connect.js +0 -40
- package/esm/dx.js +0 -128
- package/esm/dxHelpers.d.ts +0 -49
- package/esm/dxHelpers.js +0 -119
- package/esm/express.d.ts +0 -4
- package/esm/express.js +0 -35
- package/esm/helpers.js +0 -3
- package/esm/index.js +0 -9
- package/esm/logger.d.ts +0 -3
- package/esm/logger.js +0 -57
- package/esm/polyfillWithResolvers.d.ts +0 -1
- package/esm/polyfillWithResolvers.js +0 -16
- package/esm/router.js +0 -44
- package/esm/static.d.ts +0 -5
- package/esm/static.js +0 -23
- package/esm/staticHelpers.d.ts +0 -18
- package/esm/staticHelpers.js +0 -188
- package/esm/stream.d.ts +0 -12
- package/esm/stream.js +0 -92
- package/esm/vendors/contentType.d.ts +0 -4
- package/esm/vendors/contentType.js +0 -88
- package/esm/vendors/etag.d.ts +0 -10
- package/esm/vendors/etag.js +0 -105
- package/esm/vendors/fresh.d.ts +0 -23
- package/esm/vendors/fresh.js +0 -96
- package/esm/vendors/mime.d.ts +0 -1
- package/esm/vendors/mime.js +0 -35
- package/esm/vendors/mimeDb.d.ts +0 -9413
- package/esm/vendors/mimeDb.js +0 -9415
- package/esm/vendors/mimeScore.d.ts +0 -5
- package/esm/vendors/mimeScore.js +0 -46
- package/esm/vendors/onFinished.d.ts +0 -14
- package/esm/vendors/onFinished.js +0 -241
- package/esm/vendors/rangeParser.d.ts +0 -10
- package/esm/vendors/rangeParser.js +0 -122
- /package/{cjs → lib}/helpers.d.ts +0 -0
- /package/{esm/helpers.d.ts → lib/helpers.js} +0 -0
- /package/{cjs → lib}/static.d.ts +0 -0
- /package/{cjs → lib}/vendors/contentType.d.ts +0 -0
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
* Get each component of the score for a mime type. The sum of these is the
|
|
3
3
|
* total score. The higher the score, the more "official" the type.
|
|
4
4
|
*/
|
|
5
|
-
export declare function mimeScore(mimeType:
|
|
5
|
+
export declare function mimeScore(mimeType: string, source?: string): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// 'mime-score' back-ported to CommonJS
|
|
2
|
+
// Score RFC facets (see https://tools.ietf.org/html/rfc6838#section-3)
|
|
3
|
+
const facetScores = {
|
|
4
|
+
'prs.': 100,
|
|
5
|
+
'x-': 200,
|
|
6
|
+
'x.': 300,
|
|
7
|
+
'vnd.': 400,
|
|
8
|
+
default: 900,
|
|
9
|
+
};
|
|
10
|
+
// Score mime source (Logic originally from `jshttp/mime-types` module)
|
|
11
|
+
const sourceScores = {
|
|
12
|
+
nginx: 10,
|
|
13
|
+
apache: 20,
|
|
14
|
+
iana: 40,
|
|
15
|
+
default: 30, // definitions added by `jshttp/mime-db` project?
|
|
16
|
+
};
|
|
17
|
+
const typeScores = {
|
|
18
|
+
// prefer application/xml over text/xml
|
|
19
|
+
// prefer application/rtf over text/rtf
|
|
20
|
+
application: 1,
|
|
21
|
+
// prefer font/woff over application/font-woff
|
|
22
|
+
font: 2,
|
|
23
|
+
// prefer video/mp4 over audio/mp4 over application/mp4
|
|
24
|
+
// See https://www.rfc-editor.org/rfc/rfc4337.html#section-2
|
|
25
|
+
audio: 2,
|
|
26
|
+
video: 3,
|
|
27
|
+
default: 0,
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Get each component of the score for a mime type. The sum of these is the
|
|
31
|
+
* total score. The higher the score, the more "official" the type.
|
|
32
|
+
*/
|
|
33
|
+
export function mimeScore(mimeType, source = 'default') {
|
|
34
|
+
if (mimeType === 'application/octet-stream')
|
|
35
|
+
return 0;
|
|
36
|
+
const [type, subtype] = mimeType.split('/');
|
|
37
|
+
const facet = subtype.replace(/(\.|x-).*/, '$1');
|
|
38
|
+
const facetScore = facetScores[facet] || facetScores.default;
|
|
39
|
+
const sourceScore = sourceScores[source] || sourceScores.default;
|
|
40
|
+
const typeScore = typeScores[type] || typeScores.default;
|
|
41
|
+
// All else being equal prefer shorter types
|
|
42
|
+
const lengthScore = 1 - mimeType.length / 100;
|
|
43
|
+
return facetScore + sourceScore + typeScore + lengthScore;
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mimeScore.js","sourceRoot":"","s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CAAA;IAE7C,OAAO,UAAU,GAAG,WAAW,GAAG,SAAS,GAAG,WAAW,CAAA;AAC1D,CAAC","sourcesContent":["// 'mime-score' back-ported to CommonJS\n\n// Score RFC facets (see https://tools.ietf.org/html/rfc6838#section-3)\nconst facetScores = {\n\t'prs.': 100,\n\t'x-': 200,\n\t'x.': 300,\n\t'vnd.': 400,\n\tdefault: 900,\n}\n\n// Score mime source (Logic originally from `jshttp/mime-types` module)\nconst sourceScores = {\n\tnginx: 10,\n\tapache: 20,\n\tiana: 40,\n\tdefault: 30, // definitions added by `jshttp/mime-db` project?\n}\n\nconst typeScores = {\n\t// prefer application/xml over text/xml\n\t// prefer application/rtf over text/rtf\n\tapplication: 1,\n\n\t// prefer font/woff over application/font-woff\n\tfont: 2,\n\n\t// prefer video/mp4 over audio/mp4 over application/mp4\n\t// See https://www.rfc-editor.org/rfc/rfc4337.html#section-2\n\taudio: 2,\n\tvideo: 3,\n\n\tdefault: 0,\n}\n\n/**\n * Get each component of the score for a mime type. The sum of these is the\n * total score. The higher the score, the more \"official\" the type.\n */\nexport function mimeScore(mimeType: string, source = 'default') {\n\tif (mimeType === 'application/octet-stream') return 0\n\n\tconst [type, subtype] = mimeType.split('/')\n\n\tconst facet = subtype.replace(/(\\.|x-).*/, '$1')\n\n\tconst facetScore = facetScores[facet as keyof typeof facetScores] || facetScores.default\n\tconst sourceScore = sourceScores[source as keyof typeof sourceScores] || sourceScores.default\n\tconst typeScore = typeScores[type as keyof typeof typeScores] || typeScores.default\n\n\t// All else being equal prefer shorter types\n\tconst lengthScore = 1 - mimeType.length / 100\n\n\treturn facetScore + sourceScore + typeScore + lengthScore\n}\n"]}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variables.
|
|
3
|
+
* @private
|
|
4
|
+
*/
|
|
5
|
+
/* istanbul ignore next */
|
|
6
|
+
import { AsyncResource } from 'node:async_hooks';
|
|
7
|
+
const defer = typeof setImmediate === 'function'
|
|
8
|
+
? setImmediate
|
|
9
|
+
: function (fn) {
|
|
10
|
+
process.nextTick(fn.bind.apply(fn, arguments));
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Invoke callback when the response has finished, useful for
|
|
14
|
+
* cleaning up resources afterwards.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} msg
|
|
17
|
+
* @param {function} listener
|
|
18
|
+
* @return {object}
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export function onFinished(msg, listener) {
|
|
22
|
+
if (isFinished(msg) !== false) {
|
|
23
|
+
defer(listener, null, msg);
|
|
24
|
+
return msg;
|
|
25
|
+
}
|
|
26
|
+
// attach the listener to the message
|
|
27
|
+
attachListener(msg, wrap(listener));
|
|
28
|
+
return msg;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Determine if message is already finished.
|
|
32
|
+
*
|
|
33
|
+
* @param {object} msg
|
|
34
|
+
* @return {boolean}
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
function isFinished(msg) {
|
|
38
|
+
const socket = msg.socket;
|
|
39
|
+
if (typeof msg.finished === 'boolean') {
|
|
40
|
+
// OutgoingMessage
|
|
41
|
+
return Boolean(msg.finished || (socket && !socket.writable));
|
|
42
|
+
}
|
|
43
|
+
if (typeof msg.complete === 'boolean') {
|
|
44
|
+
// IncomingMessage
|
|
45
|
+
return Boolean(msg.upgrade || !socket || !socket.readable || (msg.complete && !msg.readable));
|
|
46
|
+
}
|
|
47
|
+
// don't know
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Attach a finished listener to the message.
|
|
52
|
+
*
|
|
53
|
+
* @param {object} msg
|
|
54
|
+
* @param {function} callback
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
function attachFinishedListener(msg, callback) {
|
|
58
|
+
let eeMsg;
|
|
59
|
+
let eeSocket;
|
|
60
|
+
let finished = false;
|
|
61
|
+
function onFinish(error) {
|
|
62
|
+
eeMsg.cancel();
|
|
63
|
+
eeSocket.cancel();
|
|
64
|
+
finished = true;
|
|
65
|
+
callback(error);
|
|
66
|
+
}
|
|
67
|
+
// finished on first message event
|
|
68
|
+
eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish);
|
|
69
|
+
function onSocket(socket) {
|
|
70
|
+
// remove listener
|
|
71
|
+
msg.removeListener('socket', onSocket);
|
|
72
|
+
if (finished)
|
|
73
|
+
return;
|
|
74
|
+
if (eeMsg !== eeSocket)
|
|
75
|
+
return;
|
|
76
|
+
// finished on first socket event
|
|
77
|
+
eeSocket = first([[socket, 'error', 'close']], onFinish);
|
|
78
|
+
}
|
|
79
|
+
if (msg.socket) {
|
|
80
|
+
// socket already assigned
|
|
81
|
+
onSocket(msg.socket);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
// wait for socket to be assigned
|
|
85
|
+
msg.on('socket', onSocket);
|
|
86
|
+
if (msg.socket === undefined) {
|
|
87
|
+
// istanbul ignore next: node.js 0.8 patch
|
|
88
|
+
patchAssignSocket(msg, onSocket);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function attachListener(msg, listener) {
|
|
92
|
+
let attached = msg.__onFinished;
|
|
93
|
+
// create a private single listener with queue
|
|
94
|
+
if (!attached || !attached.queue) {
|
|
95
|
+
attached = msg.__onFinished = createListener(msg);
|
|
96
|
+
attachFinishedListener(msg, attached);
|
|
97
|
+
}
|
|
98
|
+
attached.queue.push(listener);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Create listener on message.
|
|
102
|
+
*
|
|
103
|
+
* @param {object} msg
|
|
104
|
+
* @return {function}
|
|
105
|
+
* @private
|
|
106
|
+
*/
|
|
107
|
+
function createListener(msg) {
|
|
108
|
+
const listener = ((err) => {
|
|
109
|
+
if (msg.__onFinished === listener)
|
|
110
|
+
msg.__onFinished = null;
|
|
111
|
+
if (!listener.queue)
|
|
112
|
+
return;
|
|
113
|
+
const queue = listener.queue;
|
|
114
|
+
listener.queue = null;
|
|
115
|
+
for (let i = 0; i < queue.length; i++) {
|
|
116
|
+
queue[i](err, msg);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
listener.queue = [];
|
|
120
|
+
return listener;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Patch ServerResponse.prototype.assignSocket for node.js 0.8.
|
|
124
|
+
*
|
|
125
|
+
* @param {ServerResponse} res
|
|
126
|
+
* @param {function} callback
|
|
127
|
+
* @private
|
|
128
|
+
*/
|
|
129
|
+
// istanbul ignore next: node.js 0.8 patch
|
|
130
|
+
function patchAssignSocket(res, callback) {
|
|
131
|
+
const assignSocket = res.assignSocket;
|
|
132
|
+
if (typeof assignSocket !== 'function')
|
|
133
|
+
return;
|
|
134
|
+
// res.on('socket', callback) is broken in 0.8
|
|
135
|
+
res.assignSocket = function _assignSocket(socket) {
|
|
136
|
+
assignSocket.call(this, socket);
|
|
137
|
+
callback(socket);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Try to require async_hooks
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
function tryRequireAsyncHooks() {
|
|
145
|
+
try {
|
|
146
|
+
return require('async_hooks');
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
return {};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Wrap function with async resource, if possible.
|
|
154
|
+
* AsyncResource.bind static method backported.
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
function wrap(fn) {
|
|
158
|
+
// create anonymous resource
|
|
159
|
+
const res = new AsyncResource(fn.name || 'bound-anonymous-fn');
|
|
160
|
+
// incompatible node.js
|
|
161
|
+
if (!res || !res.runInAsyncScope) {
|
|
162
|
+
return fn;
|
|
163
|
+
}
|
|
164
|
+
// return bound function
|
|
165
|
+
return res.runInAsyncScope.bind(res, fn, null);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get the first event in a set of event emitters and event pairs.
|
|
169
|
+
*
|
|
170
|
+
* @param {array} stuff
|
|
171
|
+
* @param {function} done
|
|
172
|
+
* @public
|
|
173
|
+
*/
|
|
174
|
+
function first(stuff, done) {
|
|
175
|
+
if (!Array.isArray(stuff)) {
|
|
176
|
+
throw new TypeError('arg must be an array of [ee, events...] arrays');
|
|
177
|
+
}
|
|
178
|
+
const cleanups = [];
|
|
179
|
+
for (let i = 0; i < stuff.length; i++) {
|
|
180
|
+
const arr = stuff[i];
|
|
181
|
+
if (!Array.isArray(arr) || arr.length < 2) {
|
|
182
|
+
throw new TypeError('each array member must be [ee, events...]');
|
|
183
|
+
}
|
|
184
|
+
const ee = arr[0];
|
|
185
|
+
for (let j = 1; j < arr.length; j++) {
|
|
186
|
+
const event = arr[j];
|
|
187
|
+
const fn = listener(event, callback);
|
|
188
|
+
// listen to the event
|
|
189
|
+
ee.on(event, fn);
|
|
190
|
+
// push this listener to the list of cleanups
|
|
191
|
+
cleanups.push({
|
|
192
|
+
ee: ee,
|
|
193
|
+
event: event,
|
|
194
|
+
fn: fn
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function callback(...args) {
|
|
199
|
+
cleanup();
|
|
200
|
+
done.apply(null, args);
|
|
201
|
+
}
|
|
202
|
+
function cleanup() {
|
|
203
|
+
for (let i = 0; i < cleanups.length; i++) {
|
|
204
|
+
const x = cleanups[i];
|
|
205
|
+
x.ee.removeListener(x.event, x.fn);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const thunk = function thunk(fn) {
|
|
209
|
+
done = fn;
|
|
210
|
+
};
|
|
211
|
+
thunk.cancel = cleanup;
|
|
212
|
+
return thunk;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Create the event listener.
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
function listener(event, done) {
|
|
219
|
+
return function onevent(arg1) {
|
|
220
|
+
const args = new Array(arguments.length);
|
|
221
|
+
const ee = this;
|
|
222
|
+
const err = event === 'error'
|
|
223
|
+
? arg1
|
|
224
|
+
: null;
|
|
225
|
+
// copy args to prevent arguments escaping scope
|
|
226
|
+
for (let i = 0; i < args.length; i++) {
|
|
227
|
+
args[i] = arguments[i];
|
|
228
|
+
}
|
|
229
|
+
done(err, ee, event, args);
|
|
230
|
+
};
|
|
231
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface Range {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
}
|
|
5
|
+
export type Ranges = Range[] & {
|
|
6
|
+
type: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function parseRange(size: number, str: string | undefined, { combine, maxRanges }?: {
|
|
9
|
+
combine?: boolean;
|
|
10
|
+
maxRanges?: number;
|
|
11
|
+
}): Ranges | -1 | -2;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export function parseRange(size, str, { combine, maxRanges = 100 } = {}) {
|
|
2
|
+
if (typeof str !== 'string')
|
|
3
|
+
throw new TypeError('argument str must be a string');
|
|
4
|
+
const index = str.indexOf('=');
|
|
5
|
+
if (index === -1)
|
|
6
|
+
return -2;
|
|
7
|
+
// split the range string
|
|
8
|
+
const arr = str.slice(index + 1).split(',');
|
|
9
|
+
// bound the number of ranges: each costs a parseInt and combineRanges sorts O(n log n),
|
|
10
|
+
// so an attacker could amplify CPU with a header full of tiny ranges
|
|
11
|
+
if (arr.length > maxRanges)
|
|
12
|
+
return -1;
|
|
13
|
+
const ranges = [];
|
|
14
|
+
// add ranges type
|
|
15
|
+
ranges.type = str.slice(0, index);
|
|
16
|
+
// parse all ranges
|
|
17
|
+
for (let i = 0; i < arr.length; i++) {
|
|
18
|
+
const range = arr[i].split('-');
|
|
19
|
+
let start = parseInt(range[0], 10);
|
|
20
|
+
let end = parseInt(range[1], 10);
|
|
21
|
+
// -nnn
|
|
22
|
+
if (isNaN(start)) {
|
|
23
|
+
start = size - end;
|
|
24
|
+
end = size - 1;
|
|
25
|
+
// nnn-
|
|
26
|
+
}
|
|
27
|
+
else if (isNaN(end))
|
|
28
|
+
end = size - 1;
|
|
29
|
+
// limit last-byte-pos to current length
|
|
30
|
+
if (end > size - 1)
|
|
31
|
+
end = size - 1;
|
|
32
|
+
// invalid or unsatisifiable
|
|
33
|
+
if (isNaN(start) || isNaN(end) || start > end || start < 0)
|
|
34
|
+
continue;
|
|
35
|
+
// add range
|
|
36
|
+
ranges.push({
|
|
37
|
+
start: start,
|
|
38
|
+
end: end,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// unsatisifiable
|
|
42
|
+
if (ranges.length < 1)
|
|
43
|
+
return -1;
|
|
44
|
+
return combine ? combineRanges(ranges) : ranges;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Combine overlapping & adjacent ranges.
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
function combineRanges(ranges) {
|
|
51
|
+
const ordered = ranges.map(mapWithIndex).sort(sortByRangeStart);
|
|
52
|
+
let j = 0;
|
|
53
|
+
for (let i = 1; i < ordered.length; i++) {
|
|
54
|
+
const range = ordered[i];
|
|
55
|
+
const current = ordered[j];
|
|
56
|
+
if (range.start > current.end + 1) {
|
|
57
|
+
// next range
|
|
58
|
+
ordered[++j] = range;
|
|
59
|
+
}
|
|
60
|
+
else if (range.end > current.end) {
|
|
61
|
+
// extend range
|
|
62
|
+
current.end = range.end;
|
|
63
|
+
current.index = Math.min(current.index, range.index);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// trim ordered array
|
|
67
|
+
ordered.length = j + 1;
|
|
68
|
+
// generate combined range
|
|
69
|
+
const combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex);
|
|
70
|
+
// copy ranges type
|
|
71
|
+
combined.type = ranges.type;
|
|
72
|
+
return combined;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Map function to add index value to ranges.
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
function mapWithIndex(range, index) {
|
|
79
|
+
return {
|
|
80
|
+
start: range.start,
|
|
81
|
+
end: range.end,
|
|
82
|
+
index: index,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Map function to remove index value from ranges.
|
|
87
|
+
* @private
|
|
88
|
+
*/
|
|
89
|
+
function mapWithoutIndex(range) {
|
|
90
|
+
return {
|
|
91
|
+
start: range.start,
|
|
92
|
+
end: range.end,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Sort function to sort ranges by index.
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
function sortByRangeIndex(a, b) {
|
|
100
|
+
return a.index - b.index;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Sort function to sort ranges by start position.
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
function sortByRangeStart(a, b) {
|
|
107
|
+
return a.start - b.start;
|
|
108
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rangeParser.js","sourceRoot":"","sources":["../../src/vendors/rangeParser.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,UAAU,CACzB,IAAY,EACZ,GAAuB,EACvB,EAAC,OAAO,EAAE,SAAS,GAAG,GAAG,KAA6C,EAAE;IAExE,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,MAAM,IAAI,SAAS,CAAC,+BAA+B,CAAC,CAAA;IAEjF,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAE9B,IAAI,KAAK,KAAK,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC,CAAA;IAE3B,yBAAyB;IACzB,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IA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AA;IAE3B,OAAO,QAAQ,CAAA;AAChB,CAAC;AAED;;;GAGG;AAEH,SAAS,YAAY,CAAC,KAAY,EAAE,KAAa;IAChD,OAAO;QACN,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,KAAK,EAAE,KAAK;KACZ,CAAA;AACF,CAAC;AAED;;;GAGG;AAEH,SAAS,eAAe,CAAC,KAAmB;IAC3C,OAAO;QACN,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,GAAG,EAAE,KAAK,CAAC,GAAG;KACd,CAAA;AACF,CAAC;AAED;;;GAGG;AAEH,SAAS,gBAAgB,CAAC,CAAe,EAAE,CAAe;IACzD,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAA;AACzB,CAAC;AAED;;;GAGG;AAEH,SAAS,gBAAgB,CAAC,CAAe,EAAE,CAAe;IACzD,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAA;AACzB,CAAC","sourcesContent":["interface Range {\n\tstart: number\n\tend: number\n}\ninterface IndexedRange extends Range {\n\tindex: number\n}\nexport type Ranges = Range[] & {type: string}\n\nexport function parseRange(\n\tsize: number,\n\tstr: string | undefined,\n\t{combine, maxRanges = 100}: {combine?: boolean; maxRanges?: number} = {},\n): Ranges | -1 | -2 {\n\tif (typeof str !== 'string') throw new TypeError('argument str must be a string')\n\n\tconst index = str.indexOf('=')\n\n\tif (index === -1) return -2\n\n\t// split the range string\n\tconst arr = str.slice(index + 1).split(',')\n\n\t// bound the number of ranges: each costs a parseInt and combineRanges sorts O(n log n),\n\t// so an attacker could amplify CPU with a header full of tiny ranges\n\tif (arr.length > maxRanges) return -1\n\n\tconst ranges = [] as unknown as Ranges\n\n\t// add ranges type\n\tranges.type = str.slice(0, index)\n\n\t// parse all ranges\n\tfor (let i = 0; i < arr.length; i++) {\n\t\tconst range = arr[i].split('-')\n\t\tlet start = parseInt(range[0], 10)\n\t\tlet end = parseInt(range[1], 10)\n\n\t\t// -nnn\n\t\tif (isNaN(start)) {\n\t\t\tstart = size - end\n\t\t\tend = size - 1\n\t\t\t// nnn-\n\t\t} else if (isNaN(end)) end = size - 1\n\n\t\t// limit last-byte-pos to current length\n\t\tif (end > size - 1) end = size - 1\n\n\t\t// invalid or unsatisifiable\n\t\tif (isNaN(start) || isNaN(end) || start > end || start < 0) continue\n\n\t\t// add range\n\t\tranges.push({\n\t\t\tstart: start,\n\t\t\tend: end,\n\t\t})\n\t}\n\n\t// unsatisifiable\n\tif (ranges.length < 1) return -1\n\n\treturn combine ? combineRanges(ranges) : ranges\n}\n\n/**\n * Combine overlapping & adjacent ranges.\n * @private\n */\n\nfunction combineRanges(ranges: Ranges) {\n\tconst ordered = ranges.map(mapWithIndex).sort(sortByRangeStart)\n\n\tlet j = 0\n\tfor (let i = 1; i < ordered.length; i++) {\n\t\tconst range = ordered[i]\n\t\tconst current = ordered[j]\n\n\t\tif (range.start > current.end + 1) {\n\t\t\t// next range\n\t\t\tordered[++j] = range\n\t\t} else if (range.end > current.end) {\n\t\t\t// extend range\n\t\t\tcurrent.end = range.end\n\t\t\tcurrent.index = Math.min(current.index, range.index)\n\t\t}\n\t}\n\n\t// trim ordered array\n\tordered.length = j + 1\n\n\t// generate combined range\n\tconst combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex) as unknown as Ranges\n\n\t// copy ranges type\n\tcombined.type = ranges.type\n\n\treturn combined\n}\n\n/**\n * Map function to add index value to ranges.\n * @private\n */\n\nfunction mapWithIndex(range: Range, index: number) {\n\treturn {\n\t\tstart: range.start,\n\t\tend: range.end,\n\t\tindex: index,\n\t}\n}\n\n/**\n * Map function to remove index value from ranges.\n * @private\n */\n\nfunction mapWithoutIndex(range: IndexedRange) {\n\treturn {\n\t\tstart: range.start,\n\t\tend: range.end,\n\t}\n}\n\n/**\n * Sort function to sort ranges by index.\n * @private\n */\n\nfunction sortByRangeIndex(a: IndexedRange, b: IndexedRange) {\n\treturn a.index - b.index\n}\n\n/**\n * Sort function to sort ranges by start position.\n * @private\n */\n\nfunction sortByRangeStart(a: IndexedRange, b: IndexedRange) {\n\treturn a.start - b.start\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,38 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
"devDependencies": {
|
|
35
|
-
"@types/node": "^20.12.8",
|
|
36
|
-
"typescript": "^5.4.5"
|
|
37
|
-
}
|
|
2
|
+
"name": "dx-server",
|
|
3
|
+
"version": "0.13.0-alpha.2",
|
|
4
|
+
"main": "./lib/index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/tranvansang/dx-server.git"
|
|
8
|
+
},
|
|
9
|
+
"author": "Sang Tran <t@sang.jp>",
|
|
10
|
+
"files": [
|
|
11
|
+
"lib"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"default": "./lib/index.js",
|
|
16
|
+
"types": "./lib/index.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./helpers": {
|
|
19
|
+
"default": "./lib/helpers.js",
|
|
20
|
+
"types": "./lib/helpers.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prepublishOnly": "tsc",
|
|
26
|
+
"test": "tsc && node --test test/*.test.ts",
|
|
27
|
+
"coverage": "tsc && node --test --experimental-test-coverage --test-coverage-include='lib/**' --test-coverage-exclude='lib/vendors/mimeDb.js' --test-coverage-exclude='test/**' test/*.test.ts"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^25.6.0",
|
|
32
|
+
"typescript": "^6.0.2"
|
|
33
|
+
}
|
|
38
34
|
}
|
package/cjs/body.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getQuery = exports.getUrlEncoded = exports.getText = exports.getRaw = exports.getJson = exports.getBuffer = void 0;
|
|
4
|
-
const dx_js_1 = require("./dx.js");
|
|
5
|
-
const bodyHelpers_js_1 = require("./bodyHelpers.js");
|
|
6
|
-
exports.getBuffer = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.bufferFromReq)((0, dx_js_1.getReq)(), options));
|
|
7
|
-
exports.getJson = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.jsonFromReq)((0, dx_js_1.getReq)(), options));
|
|
8
|
-
exports.getRaw = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.rawFromReq)((0, dx_js_1.getReq)(), options));
|
|
9
|
-
exports.getText = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.textFromReq)((0, dx_js_1.getReq)(), options));
|
|
10
|
-
exports.getUrlEncoded = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.urlEncodedFromReq)((0, dx_js_1.getReq)(), options));
|
|
11
|
-
exports.getQuery = (0, dx_js_1.makeDxContext)((options) => (0, bodyHelpers_js_1.queryFromReq)((0, dx_js_1.getReq)(), options));
|
|
12
|
-
// to getFile use busboy
|
|
13
|
-
// https://github.com/mscdex/busboy
|
|
14
|
-
//# sourceMappingURL=data:application/json;base64,ey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QUFDLElBQUEsNkJBQVksRUFBQyxJQUFBLGNBQU0sR0FBRSxFQUFFLE9BQU8sQ0FBQyxDQUFDLENBQUE7QUFFaEgsd0JBQXdCO0FBQ3hCLG1DQUFtQyJ9
|
package/cjs/bodyHelpers.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
-
import { IncomingMessage } from 'node:http';
|
|
4
|
-
export interface BufferBodyOptions {
|
|
5
|
-
bodyLimit: number;
|
|
6
|
-
urlEncodedParser?(search: string): any;
|
|
7
|
-
queryParser?(search: string): any;
|
|
8
|
-
}
|
|
9
|
-
export declare function setBufferBodyDefaultOptions(options: Partial<BufferBodyOptions>): void;
|
|
10
|
-
export declare function bufferFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): Promise<Buffer | undefined>;
|
|
11
|
-
export declare function jsonFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): Promise<any>;
|
|
12
|
-
export declare function rawFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): Promise<Buffer | undefined>;
|
|
13
|
-
export declare function textFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): Promise<string | undefined>;
|
|
14
|
-
export declare function urlEncodedFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): Promise<any>;
|
|
15
|
-
export declare function urlFromReq(req: IncomingMessage): URL;
|
|
16
|
-
export declare function queryFromReq(req: IncomingMessage, options?: Partial<BufferBodyOptions>): any;
|
package/cjs/bodyHelpers.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.queryFromReq = exports.urlFromReq = exports.urlEncodedFromReq = exports.textFromReq = exports.rawFromReq = exports.jsonFromReq = exports.bufferFromReq = exports.setBufferBodyDefaultOptions = void 0;
|
|
4
|
-
const stream_js_1 = require("./stream.js");
|
|
5
|
-
const contentType_js_1 = require("./vendors/contentType.js");
|
|
6
|
-
function defaultQueryParser(search) {
|
|
7
|
-
return Object.fromEntries(new URLSearchParams(search)); // support both leading ? and not
|
|
8
|
-
}
|
|
9
|
-
let bodyDefaultOptions = { bodyLimit: 100 << 10 }; // 100kb
|
|
10
|
-
function setBufferBodyDefaultOptions(options) {
|
|
11
|
-
bodyDefaultOptions = { ...bodyDefaultOptions, ...options };
|
|
12
|
-
}
|
|
13
|
-
exports.setBufferBodyDefaultOptions = setBufferBodyDefaultOptions;
|
|
14
|
-
async function bufferFromReq(req, options) {
|
|
15
|
-
const { bodyLimit } = { ...bodyDefaultOptions, ...options };
|
|
16
|
-
/**
|
|
17
|
-
* Check if a request has a request body.
|
|
18
|
-
* A request with a body __must__ either have `transfer-encoding`
|
|
19
|
-
* or `content-length` headers set.
|
|
20
|
-
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
|
|
21
|
-
*/
|
|
22
|
-
// https://github.com/jshttp/type-is/blob/cdcfe23e9833872e425b0aaf71ca0311373b6116/index.js#L92
|
|
23
|
-
const contentLengthParsed = parseInt(req.headers['content-length'] ?? '', 10);
|
|
24
|
-
if (req.headers['transfer-encoding'] === undefined
|
|
25
|
-
&& isNaN(contentLengthParsed))
|
|
26
|
-
return;
|
|
27
|
-
const contentLength = isNaN(contentLengthParsed) ? undefined : contentLengthParsed;
|
|
28
|
-
// read
|
|
29
|
-
const encoding = (req.headers['content-encoding'] ?? 'identity').toLowerCase();
|
|
30
|
-
const stream = (0, stream_js_1.getContentStream)(req, encoding);
|
|
31
|
-
return await (0, stream_js_1.readStream)(stream, {
|
|
32
|
-
length: encoding === 'identity' ? contentLength : undefined,
|
|
33
|
-
limit: bodyLimit,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
exports.bufferFromReq = bufferFromReq;
|
|
37
|
-
// if content-type is not as expected, return undefined
|
|
38
|
-
function forceGetContentTypeParams(req, expected) {
|
|
39
|
-
const contentTypeRaw = req.headers['content-type'];
|
|
40
|
-
if (!contentTypeRaw)
|
|
41
|
-
return;
|
|
42
|
-
const { mediaType, parameters } = (0, contentType_js_1.parseContentType)(contentTypeRaw);
|
|
43
|
-
if (mediaType !== expected)
|
|
44
|
-
return;
|
|
45
|
-
return parameters;
|
|
46
|
-
}
|
|
47
|
-
function forceGetCharset(req, expected) {
|
|
48
|
-
const parameters = forceGetContentTypeParams(req, expected);
|
|
49
|
-
if (!parameters)
|
|
50
|
-
return;
|
|
51
|
-
// assert charset per RFC 7159 sec 8.1
|
|
52
|
-
const charset = parameters.charset?.toLowerCase() || 'utf-8';
|
|
53
|
-
if (!charset.startsWith('utf-'))
|
|
54
|
-
throw new Error(`unsupported charset "${charset.toUpperCase()}"`);
|
|
55
|
-
return charset;
|
|
56
|
-
}
|
|
57
|
-
async function jsonFromReq(req, options) {
|
|
58
|
-
const charset = forceGetCharset(req, 'application/json');
|
|
59
|
-
if (!charset)
|
|
60
|
-
return;
|
|
61
|
-
const buffer = await bufferFromReq(req, options);
|
|
62
|
-
if (buffer) {
|
|
63
|
-
const str = buffer.toString(charset);
|
|
64
|
-
return str ? JSON.parse(str) : undefined;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.jsonFromReq = jsonFromReq;
|
|
68
|
-
async function rawFromReq(req, options) {
|
|
69
|
-
if (!forceGetContentTypeParams(req, 'application/octet-stream'))
|
|
70
|
-
return;
|
|
71
|
-
return await bufferFromReq(req, options);
|
|
72
|
-
}
|
|
73
|
-
exports.rawFromReq = rawFromReq;
|
|
74
|
-
async function textFromReq(req, options) {
|
|
75
|
-
const charset = forceGetCharset(req, 'text/plain');
|
|
76
|
-
if (!charset)
|
|
77
|
-
return;
|
|
78
|
-
const buffer = await bufferFromReq(req, options);
|
|
79
|
-
if (buffer)
|
|
80
|
-
return buffer.toString(charset);
|
|
81
|
-
}
|
|
82
|
-
exports.textFromReq = textFromReq;
|
|
83
|
-
async function urlEncodedFromReq(req, options) {
|
|
84
|
-
const charset = forceGetCharset(req, 'application/x-www-form-urlencoded');
|
|
85
|
-
if (!charset)
|
|
86
|
-
return;
|
|
87
|
-
const buffer = await bufferFromReq(req, options);
|
|
88
|
-
if (buffer) {
|
|
89
|
-
return (bodyDefaultOptions.urlEncodedParser ?? options?.urlEncodedParser ?? defaultQueryParser)(buffer.toString(charset));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
exports.urlEncodedFromReq = urlEncodedFromReq;
|
|
93
|
-
function urlFromReq(req) {
|
|
94
|
-
return new URL(req.url ?? '', 'https://example.com');
|
|
95
|
-
}
|
|
96
|
-
exports.urlFromReq = urlFromReq;
|
|
97
|
-
function queryFromReq(req, options) {
|
|
98
|
-
return (bodyDefaultOptions.queryParser ?? options?.queryParser ?? defaultQueryParser)(urlFromReq(req).searchParams.toString());
|
|
99
|
-
}
|
|
100
|
-
exports.queryFromReq = queryFromReq;
|
|
101
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYm9keUhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvYm9keUhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQ0EsMkNBQXdEO0FBQ3hELDZEQUF5RDtBQVF6RCxTQUFTLGtCQUFrQixDQUFDLE1BQWM7SUFDekMsT0FBTyxNQUFNLENBQUMsV0FBVyxDQUFDLElBQUksZUFBZSxDQUFDLE1BQU0sQ0FBQyxDQUFDLENBQUEsQ0FBQyxpQ0FBaUM7QUFDekYsQ0FBQztBQUVELElBQUksa0JBQWtCLEdBQXNCLEVBQUMsU0FBUyxFQUFFLEdBQUcsSUFBSSxFQUFFLEVBQUMsQ0FBQSxDQUFDLFFBQVE7QUFDM0UsU0FBZ0IsMkJBQTJCLENBQUMsT0FBbUM7SUFDOUUsa0JBQWtCLEdBQUcsRUFBQyxHQUFHLGtCQUFrQ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UFBZSxDQUFDLEdBQUcsRUFBRSxtQ0FBbUMsQ0FBQyxDQUFBO0lBQ3pFLElBQUksQ0FBQyxPQUFPO1FBQUUsT0FBTTtJQUNwQixNQUFNLE1BQU0sR0FBRyxNQUFNLGFBQWEsQ0FBQyxHQUFHLEVBQUUsT0FBTyxDQUFDLENBQUE7SUFDaEQsSUFBSSxNQUFNLEVBQUUsQ0FBQztRQUNaLE9BQU8sQ0FBQyxrQkFBa0IsQ0FBQyxnQkFBZ0IsSUFBSSxPQUFPLEVBQUUsZ0JBQWdCLElBQUksa0JBQWtCLENBQUMsQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUE7SUFDMUgsQ0FBQztBQUNGLENBQUM7QUFQRCw4Q0FPQztBQUVELFNBQWdCLFVBQVUsQ0FBQyxHQUFvQjtJQUM5QyxPQUFPLElBQUksR0FBRyxDQUFDLEdBQUcsQ0FBQyxHQUFHLElBQUksRUFBRSxFQUFFLHFCQUFxQixDQUFDLENBQUE7QUFDckQsQ0FBQztBQUZELGdDQUVDO0FBRUQsU0FBZ0IsWUFBWSxDQUFDLEdBQW9CLEVBQUUsT0FBb0M7SUFDdEYsT0FBTyxDQUFDLGtCQUFrQixDQUFDLFdBQVcsSUFBSSxPQUFPLEVBQUUsV0FBVyxJQUFJLGtCQUFrQixDQUFDLENBQUMsVUFBVSxDQUFDLEdBQUcsQ0FBQyxDQUFDLFlBQVksQ0FBQyxRQUFRLEVBQUUsQ0FBQyxDQUFBO0FBQy9ILENBQUM7QUFGRCxvQ0FFQyJ9
|
package/cjs/connect.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
-
import { type Chainable } from './dx.js';
|
|
4
|
-
import './polyfillWithResolvers.js';
|
|
5
|
-
export declare function connectMiddlewares(...middlewares: Array<(req: IncomingMessage, res: ServerResponse, next: () => any) => any>): Chainable;
|