wreq-js 2.2.2 → 2.3.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/dist/wreq-js.cjs +2046 -2605
- package/dist/wreq-js.cjs.map +1 -1
- package/dist/wreq-js.d.ts +829 -724
- package/dist/wreq-js.js +2029 -2569
- package/dist/wreq-js.js.map +1 -1
- package/package.json +10 -13
- package/rust/wreq-js.darwin-arm64.node +0 -0
- package/rust/wreq-js.darwin-x64.node +0 -0
- package/rust/wreq-js.linux-arm64-gnu.node +0 -0
- package/rust/wreq-js.linux-x64-gnu.node +0 -0
- package/rust/wreq-js.linux-x64-musl.node +0 -0
- package/rust/wreq-js.win32-x64-msvc.node +0 -0
- package/dist/wreq-js.d.cts +0 -1041
package/dist/wreq-js.cjs
CHANGED
|
@@ -1,2764 +1,2205 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/wreq-js.ts
|
|
21
|
-
var wreq_js_exports = {};
|
|
22
|
-
__export(wreq_js_exports, {
|
|
23
|
-
Headers: () => Headers,
|
|
24
|
-
RequestError: () => RequestError,
|
|
25
|
-
Response: () => Response,
|
|
26
|
-
Session: () => Session,
|
|
27
|
-
Transport: () => Transport,
|
|
28
|
-
WebSocket: () => WebSocket,
|
|
29
|
-
createSession: () => createSession,
|
|
30
|
-
createTransport: () => createTransport,
|
|
31
|
-
default: () => wreq_js_default,
|
|
32
|
-
fetch: () => fetch,
|
|
33
|
-
get: () => get,
|
|
34
|
-
getOperatingSystems: () => getOperatingSystems,
|
|
35
|
-
getProfiles: () => getProfiles,
|
|
36
|
-
post: () => post,
|
|
37
|
-
request: () => request,
|
|
38
|
-
websocket: () => websocket,
|
|
39
|
-
withSession: () => withSession
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
40
4
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
5
|
+
let node_crypto = require("node:crypto");
|
|
6
|
+
let node_http = require("node:http");
|
|
7
|
+
let node_stream = require("node:stream");
|
|
8
|
+
let node_stream_web = require("node:stream/web");
|
|
9
|
+
//#region src/native-require-cjs.ts
|
|
10
|
+
const nativeRequire = require;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/types.ts
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when a request fails. This can occur due to network errors,
|
|
15
|
+
* timeouts, invalid URLs, or other request-related issues.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* try {
|
|
20
|
+
* const response = await fetch('https://api.example.com');
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* if (error instanceof RequestError) {
|
|
23
|
+
* console.error('Request failed:', error.message);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
48
28
|
var RequestError = class extends TypeError {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "RequestError";
|
|
32
|
+
}
|
|
53
33
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/wreq-js.ts
|
|
36
|
+
let nativeBinding;
|
|
37
|
+
let cachedProfiles;
|
|
38
|
+
let cachedProfileSet;
|
|
39
|
+
let cachedOperatingSystems;
|
|
40
|
+
let cachedOperatingSystemSet;
|
|
61
41
|
function detectLibc() {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const glibcVersion = report?.header?.glibcVersionRuntime;
|
|
72
|
-
if (glibcVersion) {
|
|
73
|
-
return "gnu";
|
|
74
|
-
}
|
|
75
|
-
return "musl";
|
|
76
|
-
} catch {
|
|
77
|
-
return "gnu";
|
|
78
|
-
}
|
|
42
|
+
if (process.platform !== "linux") return;
|
|
43
|
+
const envLibc = process.env.LIBC ?? process.env.npm_config_libc;
|
|
44
|
+
if (envLibc) return envLibc.toLowerCase().includes("musl") ? "musl" : "gnu";
|
|
45
|
+
try {
|
|
46
|
+
if ((process.report?.getReport?.())?.header?.glibcVersionRuntime) return "gnu";
|
|
47
|
+
return "musl";
|
|
48
|
+
} catch {
|
|
49
|
+
return "gnu";
|
|
50
|
+
}
|
|
79
51
|
}
|
|
80
52
|
function loadNativeBinding() {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return require("../rust/wreq-js.node");
|
|
142
|
-
} catch {
|
|
143
|
-
throw new Error(
|
|
144
|
-
"Failed to load native module for linux-arm64-gnu. Tried: ../rust/wreq-js.linux-arm64-gnu.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform."
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (platform === "win32" && arch === "x64") {
|
|
150
|
-
try {
|
|
151
|
-
return require("../rust/wreq-js.win32-x64-msvc.node");
|
|
152
|
-
} catch {
|
|
153
|
-
try {
|
|
154
|
-
return require("../rust/wreq-js.node");
|
|
155
|
-
} catch {
|
|
156
|
-
throw new Error(
|
|
157
|
-
"Failed to load native module for win32-x64-msvc. Tried: ../rust/wreq-js.win32-x64-msvc.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform."
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
throw new Error(
|
|
163
|
-
`Unsupported platform: ${platform}-${arch}${libc ? `-${libc}` : ""}. Supported platforms: darwin-x64, darwin-arm64, linux-x64-gnu, linux-x64-musl, linux-arm64-gnu, win32-x64-msvc`
|
|
164
|
-
);
|
|
53
|
+
const platform = process.platform;
|
|
54
|
+
const arch = process.arch;
|
|
55
|
+
const libc = detectLibc();
|
|
56
|
+
if (platform === "darwin" && arch === "x64") try {
|
|
57
|
+
return nativeRequire("../rust/wreq-js.darwin-x64.node");
|
|
58
|
+
} catch {
|
|
59
|
+
try {
|
|
60
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
61
|
+
} catch {
|
|
62
|
+
throw new Error("Failed to load native module for darwin-x64. Tried: ../rust/wreq-js.darwin-x64.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (platform === "darwin" && arch === "arm64") try {
|
|
66
|
+
return nativeRequire("../rust/wreq-js.darwin-arm64.node");
|
|
67
|
+
} catch {
|
|
68
|
+
try {
|
|
69
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
70
|
+
} catch {
|
|
71
|
+
throw new Error("Failed to load native module for darwin-arm64. Tried: ../rust/wreq-js.darwin-arm64.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (platform === "linux" && arch === "x64") {
|
|
75
|
+
if (libc === "musl") try {
|
|
76
|
+
return nativeRequire("../rust/wreq-js.linux-x64-musl.node");
|
|
77
|
+
} catch {
|
|
78
|
+
try {
|
|
79
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
80
|
+
} catch {
|
|
81
|
+
throw new Error("Failed to load native module for linux-x64-musl. Tried: ../rust/wreq-js.linux-x64-musl.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
return nativeRequire("../rust/wreq-js.linux-x64-gnu.node");
|
|
86
|
+
} catch {
|
|
87
|
+
try {
|
|
88
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
89
|
+
} catch {
|
|
90
|
+
throw new Error("Failed to load native module for linux-x64-gnu. Tried: ../rust/wreq-js.linux-x64-gnu.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (platform === "linux" && arch === "arm64") try {
|
|
95
|
+
return nativeRequire("../rust/wreq-js.linux-arm64-gnu.node");
|
|
96
|
+
} catch {
|
|
97
|
+
try {
|
|
98
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
99
|
+
} catch {
|
|
100
|
+
throw new Error("Failed to load native module for linux-arm64-gnu. Tried: ../rust/wreq-js.linux-arm64-gnu.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (platform === "win32" && arch === "x64") try {
|
|
104
|
+
return nativeRequire("../rust/wreq-js.win32-x64-msvc.node");
|
|
105
|
+
} catch {
|
|
106
|
+
try {
|
|
107
|
+
return nativeRequire("../rust/wreq-js.node");
|
|
108
|
+
} catch {
|
|
109
|
+
throw new Error("Failed to load native module for win32-x64-msvc. Tried: ../rust/wreq-js.win32-x64-msvc.node and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`Unsupported platform: ${platform}-${arch}${libc ? `-${libc}` : ""}. Supported platforms: darwin-x64, darwin-arm64, linux-x64-gnu, linux-x64-musl, linux-arm64-gnu, win32-x64-msvc`);
|
|
165
113
|
}
|
|
166
114
|
nativeBinding = loadNativeBinding();
|
|
167
|
-
|
|
168
|
-
|
|
115
|
+
const websocketFinalizer = typeof FinalizationRegistry === "function" ? new FinalizationRegistry((connection) => {
|
|
116
|
+
nativeBinding.websocketClose(connection).catch(() => void 0);
|
|
169
117
|
}) : void 0;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
nativeBinding.cancelBody(handle.id);
|
|
177
|
-
} catch {
|
|
178
|
-
}
|
|
118
|
+
const bodyHandleFinalizer = typeof FinalizationRegistry === "function" ? new FinalizationRegistry((handle) => {
|
|
119
|
+
if (handle.released) return;
|
|
120
|
+
handle.released = true;
|
|
121
|
+
try {
|
|
122
|
+
nativeBinding.cancelBody(handle.id);
|
|
123
|
+
} catch {}
|
|
179
124
|
}) : void 0;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
125
|
+
const DEFAULT_BROWSER = "chrome_142";
|
|
126
|
+
const DEFAULT_OS = "macos";
|
|
127
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 3e5;
|
|
128
|
+
const DEFAULT_TRUST_STORE = "combined";
|
|
129
|
+
const SUPPORTED_OSES = [
|
|
130
|
+
"windows",
|
|
131
|
+
"macos",
|
|
132
|
+
"linux",
|
|
133
|
+
"android",
|
|
134
|
+
"ios"
|
|
135
|
+
];
|
|
136
|
+
const UTF8_DECODER = new TextDecoder("utf-8");
|
|
137
|
+
let ephemeralIdCounter = 0;
|
|
186
138
|
function generateEphemeralSessionId() {
|
|
187
|
-
|
|
139
|
+
return `_e${++ephemeralIdCounter}`;
|
|
188
140
|
}
|
|
189
141
|
function generateSessionId() {
|
|
190
|
-
|
|
142
|
+
return (0, node_crypto.randomUUID)();
|
|
191
143
|
}
|
|
192
144
|
function normalizeSessionOptions(options) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
145
|
+
const sessionId = options?.sessionId ?? generateSessionId();
|
|
146
|
+
const defaults = {
|
|
147
|
+
transportMode: resolveEmulationMode(options?.browser, options?.os, options?.emulation),
|
|
148
|
+
trustStore: DEFAULT_TRUST_STORE
|
|
149
|
+
};
|
|
150
|
+
if (options?.proxy !== void 0) defaults.proxy = options.proxy;
|
|
151
|
+
if (options?.timeout !== void 0) {
|
|
152
|
+
validateTimeout(options.timeout);
|
|
153
|
+
defaults.timeout = options.timeout;
|
|
154
|
+
}
|
|
155
|
+
if (options?.insecure !== void 0) defaults.insecure = options.insecure;
|
|
156
|
+
if (options?.trustStore !== void 0) {
|
|
157
|
+
validateTrustStore(options.trustStore);
|
|
158
|
+
defaults.trustStore = options.trustStore;
|
|
159
|
+
}
|
|
160
|
+
if (options?.defaultHeaders !== void 0) defaults.defaultHeaders = headersToTuples(options.defaultHeaders);
|
|
161
|
+
if (options?.captureDiagnostics !== void 0) defaults.captureDiagnostics = options.captureDiagnostics;
|
|
162
|
+
return {
|
|
163
|
+
sessionId,
|
|
164
|
+
defaults
|
|
165
|
+
};
|
|
211
166
|
}
|
|
212
167
|
function isIterable(value) {
|
|
213
|
-
|
|
168
|
+
return Boolean(value) && typeof value[Symbol.iterator] === "function";
|
|
214
169
|
}
|
|
215
170
|
function isPlainObject(value) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const proto = Object.getPrototypeOf(value);
|
|
220
|
-
return proto === Object.prototype || proto === null;
|
|
171
|
+
if (typeof value !== "object" || value === null) return false;
|
|
172
|
+
const proto = Object.getPrototypeOf(value);
|
|
173
|
+
return proto === Object.prototype || proto === null;
|
|
221
174
|
}
|
|
222
175
|
function coerceHeaderValue(value) {
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
var Headers = class
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
callback.call(thisArg, value, name, this);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
[Symbol.iterator]() {
|
|
327
|
-
const generator = function* (store) {
|
|
328
|
-
for (const entry of store.values()) {
|
|
329
|
-
yield [entry.name, entry.values.join(", ")];
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
return generator(this.store);
|
|
333
|
-
}
|
|
334
|
-
toObject() {
|
|
335
|
-
const result = {};
|
|
336
|
-
for (const [name, value] of this) {
|
|
337
|
-
result[name] = value;
|
|
338
|
-
}
|
|
339
|
-
return result;
|
|
340
|
-
}
|
|
341
|
-
toTuples() {
|
|
342
|
-
const result = [];
|
|
343
|
-
for (const [name, value] of this) {
|
|
344
|
-
result.push([name, value]);
|
|
345
|
-
}
|
|
346
|
-
return result;
|
|
347
|
-
}
|
|
176
|
+
return String(value);
|
|
177
|
+
}
|
|
178
|
+
var Headers = class Headers {
|
|
179
|
+
store = /* @__PURE__ */ new Map();
|
|
180
|
+
constructor(init) {
|
|
181
|
+
if (init) this.applyInit(init);
|
|
182
|
+
}
|
|
183
|
+
applyInit(init) {
|
|
184
|
+
if (init instanceof Headers) {
|
|
185
|
+
for (const [name, value] of init) this.append(name, value);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(init) || isIterable(init)) {
|
|
189
|
+
for (const tuple of init) {
|
|
190
|
+
if (!tuple) continue;
|
|
191
|
+
const [name, value] = tuple;
|
|
192
|
+
this.append(name, value);
|
|
193
|
+
}
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (isPlainObject(init)) for (const [name, value] of Object.entries(init)) {
|
|
197
|
+
if (value === void 0 || value === null) continue;
|
|
198
|
+
this.set(name, coerceHeaderValue(value));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
normalizeName(name) {
|
|
202
|
+
if (typeof name !== "string") throw new TypeError("Header name must be a string");
|
|
203
|
+
const trimmed = name.trim();
|
|
204
|
+
if (!trimmed) throw new TypeError("Header name must not be empty");
|
|
205
|
+
return {
|
|
206
|
+
key: trimmed.toLowerCase(),
|
|
207
|
+
display: trimmed
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
assertValue(value) {
|
|
211
|
+
if (value === void 0 || value === null) throw new TypeError("Header value must not be null or undefined");
|
|
212
|
+
return coerceHeaderValue(value);
|
|
213
|
+
}
|
|
214
|
+
append(name, value) {
|
|
215
|
+
const normalized = this.normalizeName(name);
|
|
216
|
+
const existing = this.store.get(normalized.key);
|
|
217
|
+
const coercedValue = this.assertValue(value);
|
|
218
|
+
if (existing) {
|
|
219
|
+
existing.values.push(coercedValue);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
this.store.set(normalized.key, {
|
|
223
|
+
name: normalized.display,
|
|
224
|
+
values: [coercedValue]
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
set(name, value) {
|
|
228
|
+
const normalized = this.normalizeName(name);
|
|
229
|
+
const coercedValue = this.assertValue(value);
|
|
230
|
+
this.store.set(normalized.key, {
|
|
231
|
+
name: normalized.display,
|
|
232
|
+
values: [coercedValue]
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
get(name) {
|
|
236
|
+
const normalized = this.normalizeName(name);
|
|
237
|
+
const entry = this.store.get(normalized.key);
|
|
238
|
+
return entry ? entry.values.join(", ") : null;
|
|
239
|
+
}
|
|
240
|
+
has(name) {
|
|
241
|
+
const normalized = this.normalizeName(name);
|
|
242
|
+
return this.store.has(normalized.key);
|
|
243
|
+
}
|
|
244
|
+
delete(name) {
|
|
245
|
+
const normalized = this.normalizeName(name);
|
|
246
|
+
this.store.delete(normalized.key);
|
|
247
|
+
}
|
|
248
|
+
entries() {
|
|
249
|
+
return this[Symbol.iterator]();
|
|
250
|
+
}
|
|
251
|
+
*keys() {
|
|
252
|
+
for (const [name] of this) yield name;
|
|
253
|
+
}
|
|
254
|
+
*values() {
|
|
255
|
+
for (const [, value] of this) yield value;
|
|
256
|
+
}
|
|
257
|
+
forEach(callback, thisArg) {
|
|
258
|
+
for (const [name, value] of this) callback.call(thisArg, value, name, this);
|
|
259
|
+
}
|
|
260
|
+
[Symbol.iterator]() {
|
|
261
|
+
const generator = function* (store) {
|
|
262
|
+
for (const entry of store.values()) yield [entry.name, entry.values.join(", ")];
|
|
263
|
+
};
|
|
264
|
+
return generator(this.store);
|
|
265
|
+
}
|
|
266
|
+
toObject() {
|
|
267
|
+
const result = {};
|
|
268
|
+
for (const [name, value] of this) result[name] = value;
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
toTuples() {
|
|
272
|
+
const result = [];
|
|
273
|
+
for (const [name, value] of this) result.push([name, value]);
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
348
276
|
};
|
|
349
277
|
function headersToTuples(init) {
|
|
350
|
-
|
|
278
|
+
return new Headers(init).toTuples();
|
|
351
279
|
}
|
|
352
280
|
function hasHeaderName(tuples, name) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
for (const [headerName] of tuples) {
|
|
358
|
-
if (headerName.toLowerCase() === target) {
|
|
359
|
-
return true;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return false;
|
|
281
|
+
if (!tuples) return false;
|
|
282
|
+
const target = name.toLowerCase();
|
|
283
|
+
for (const [headerName] of tuples) if (headerName.toLowerCase() === target) return true;
|
|
284
|
+
return false;
|
|
363
285
|
}
|
|
364
286
|
function hasWebSocketProtocolHeader(headers) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
return hasHeaderName(headersToTuples(headers), protocolHeaderName);
|
|
287
|
+
const protocolHeaderName = "Sec-WebSocket-Protocol";
|
|
288
|
+
if (!headers) return false;
|
|
289
|
+
return hasHeaderName(headersToTuples(headers), protocolHeaderName);
|
|
370
290
|
}
|
|
371
291
|
function assertNoManualWebSocketProtocolHeader(headers) {
|
|
372
|
-
|
|
373
|
-
throw new RequestError("Do not set `Sec-WebSocket-Protocol` header manually; use the `protocols` option instead.");
|
|
374
|
-
}
|
|
292
|
+
if (hasWebSocketProtocolHeader(headers)) throw new RequestError("Do not set `Sec-WebSocket-Protocol` header manually; use the `protocols` option instead.");
|
|
375
293
|
}
|
|
376
294
|
function normalizeWebSocketProtocolList(protocols) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
380
|
-
return typeof protocols === "string" ? [protocols] : [...protocols];
|
|
295
|
+
if (protocols === void 0) return;
|
|
296
|
+
return typeof protocols === "string" ? [protocols] : [...protocols];
|
|
381
297
|
}
|
|
382
298
|
function mergeHeaderTuples(defaults, overrides) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const overrideKeys = /* @__PURE__ */ new Set();
|
|
394
|
-
for (const tuple of overrideTuples) {
|
|
395
|
-
overrideKeys.add(tuple[0].toLowerCase());
|
|
396
|
-
}
|
|
397
|
-
const merged = [];
|
|
398
|
-
for (const tuple of defaults) {
|
|
399
|
-
if (!overrideKeys.has(tuple[0].toLowerCase())) {
|
|
400
|
-
merged.push(tuple);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
for (const tuple of overrideTuples) {
|
|
404
|
-
merged.push(tuple);
|
|
405
|
-
}
|
|
406
|
-
return merged;
|
|
299
|
+
if (!defaults) return overrides === void 0 ? void 0 : headersToTuples(overrides);
|
|
300
|
+
if (overrides === void 0) return defaults;
|
|
301
|
+
const overrideTuples = headersToTuples(overrides);
|
|
302
|
+
if (overrideTuples.length === 0) return defaults;
|
|
303
|
+
const overrideKeys = /* @__PURE__ */ new Set();
|
|
304
|
+
for (const tuple of overrideTuples) overrideKeys.add(tuple[0].toLowerCase());
|
|
305
|
+
const merged = [];
|
|
306
|
+
for (const tuple of defaults) if (!overrideKeys.has(tuple[0].toLowerCase())) merged.push(tuple);
|
|
307
|
+
for (const tuple of overrideTuples) merged.push(tuple);
|
|
308
|
+
return merged;
|
|
407
309
|
}
|
|
408
310
|
function cloneNativeResponse(payload) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
311
|
+
return {
|
|
312
|
+
status: payload.status,
|
|
313
|
+
headers: payload.headers.map(([name, value]) => [name, value]),
|
|
314
|
+
bodyHandle: payload.bodyHandle,
|
|
315
|
+
bodyBytes: payload.bodyBytes,
|
|
316
|
+
contentLength: payload.contentLength,
|
|
317
|
+
cookies: payload.cookies.map(([name, value]) => [name, value]),
|
|
318
|
+
url: payload.url,
|
|
319
|
+
diagnostics: payload.diagnostics ? { ...payload.diagnostics } : null
|
|
320
|
+
};
|
|
418
321
|
}
|
|
419
322
|
function releaseNativeBody(handle) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
} catch {
|
|
427
|
-
}
|
|
428
|
-
bodyHandleFinalizer?.unregister(handle);
|
|
323
|
+
if (handle.released) return;
|
|
324
|
+
handle.released = true;
|
|
325
|
+
try {
|
|
326
|
+
nativeBinding.cancelBody(handle.id);
|
|
327
|
+
} catch {}
|
|
328
|
+
bodyHandleFinalizer?.unregister(handle);
|
|
429
329
|
}
|
|
430
330
|
function markNativeBodyReleased(handle) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
handle.released = true;
|
|
435
|
-
bodyHandleFinalizer?.unregister(handle);
|
|
331
|
+
if (handle.released) return;
|
|
332
|
+
handle.released = true;
|
|
333
|
+
bodyHandleFinalizer?.unregister(handle);
|
|
436
334
|
}
|
|
437
335
|
function createNativeBodyStream(handle) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
336
|
+
const stream = new node_stream_web.ReadableStream({
|
|
337
|
+
async pull(controller) {
|
|
338
|
+
try {
|
|
339
|
+
const chunk = await nativeBinding.readBodyChunk(handle.id);
|
|
340
|
+
if (chunk === null) {
|
|
341
|
+
releaseNativeBody(handle);
|
|
342
|
+
controller.close();
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
controller.enqueue(chunk);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
releaseNativeBody(handle);
|
|
348
|
+
controller.error(error);
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
cancel() {
|
|
352
|
+
releaseNativeBody(handle);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
bodyHandleFinalizer?.register(stream, handle, handle);
|
|
356
|
+
return stream;
|
|
459
357
|
}
|
|
460
358
|
function wrapBodyStream(source, onFirstUse) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
this.inlineBody = null;
|
|
688
|
-
return bytes;
|
|
689
|
-
}
|
|
690
|
-
if (this.nativeHandleAvailable && this.payload.bodyHandle !== null) {
|
|
691
|
-
this.nativeHandleAvailable = false;
|
|
692
|
-
try {
|
|
693
|
-
return await nativeBinding.readBodyAll(this.payload.bodyHandle);
|
|
694
|
-
} catch (error) {
|
|
695
|
-
if (String(error).includes("Body handle") && String(error).includes("not found")) {
|
|
696
|
-
return Buffer.alloc(0);
|
|
697
|
-
}
|
|
698
|
-
throw error;
|
|
699
|
-
} finally {
|
|
700
|
-
if (this.nativeHandle) {
|
|
701
|
-
markNativeBodyReleased(this.nativeHandle);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
const stream = this.body;
|
|
706
|
-
if (!stream) {
|
|
707
|
-
return Buffer.alloc(0);
|
|
708
|
-
}
|
|
709
|
-
const reader = stream.getReader();
|
|
710
|
-
const chunks = [];
|
|
711
|
-
try {
|
|
712
|
-
while (true) {
|
|
713
|
-
const { done, value } = await reader.read();
|
|
714
|
-
if (done) {
|
|
715
|
-
break;
|
|
716
|
-
}
|
|
717
|
-
if (value && value.byteLength > 0) {
|
|
718
|
-
if (Buffer.isBuffer(value)) {
|
|
719
|
-
chunks.push(value);
|
|
720
|
-
} else {
|
|
721
|
-
chunks.push(Buffer.from(value.buffer, value.byteOffset, value.byteLength));
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
} finally {
|
|
726
|
-
}
|
|
727
|
-
return chunks.length === 0 ? Buffer.alloc(0) : Buffer.concat(chunks);
|
|
728
|
-
}
|
|
359
|
+
let started = false;
|
|
360
|
+
let reader = null;
|
|
361
|
+
return new node_stream_web.ReadableStream({
|
|
362
|
+
async pull(controller) {
|
|
363
|
+
if (!started) {
|
|
364
|
+
started = true;
|
|
365
|
+
onFirstUse();
|
|
366
|
+
}
|
|
367
|
+
if (!reader) reader = source.getReader();
|
|
368
|
+
try {
|
|
369
|
+
const { done, value } = await reader.read();
|
|
370
|
+
if (done) {
|
|
371
|
+
controller.close();
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
controller.enqueue(value);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
controller.error(error);
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
cancel(reason) {
|
|
380
|
+
if (!reader) return source.cancel(reason);
|
|
381
|
+
return reader.cancel(reason);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
var Response = class Response {
|
|
386
|
+
status;
|
|
387
|
+
ok;
|
|
388
|
+
contentLength;
|
|
389
|
+
url;
|
|
390
|
+
diagnostics;
|
|
391
|
+
type = "basic";
|
|
392
|
+
bodyUsed = false;
|
|
393
|
+
payload;
|
|
394
|
+
requestUrl;
|
|
395
|
+
redirectedMemo;
|
|
396
|
+
headersInit;
|
|
397
|
+
headersInstance;
|
|
398
|
+
cookiesInit;
|
|
399
|
+
cookiesRecord;
|
|
400
|
+
inlineBody;
|
|
401
|
+
bodySource;
|
|
402
|
+
bodyStream;
|
|
403
|
+
nativeHandleAvailable;
|
|
404
|
+
nativeHandle;
|
|
405
|
+
constructor(payload, requestUrl, bodySource) {
|
|
406
|
+
this.payload = payload;
|
|
407
|
+
this.requestUrl = requestUrl;
|
|
408
|
+
this.status = this.payload.status;
|
|
409
|
+
this.ok = this.status >= 200 && this.status < 300;
|
|
410
|
+
this.headersInit = this.payload.headers;
|
|
411
|
+
this.headersInstance = null;
|
|
412
|
+
this.url = this.payload.url;
|
|
413
|
+
this.diagnostics = this.payload.diagnostics ?? null;
|
|
414
|
+
this.cookiesInit = this.payload.cookies;
|
|
415
|
+
this.cookiesRecord = null;
|
|
416
|
+
this.contentLength = this.payload.contentLength ?? null;
|
|
417
|
+
this.inlineBody = this.payload.bodyBytes ?? null;
|
|
418
|
+
this.nativeHandle = null;
|
|
419
|
+
if (typeof bodySource !== "undefined") {
|
|
420
|
+
this.bodySource = bodySource;
|
|
421
|
+
this.nativeHandleAvailable = false;
|
|
422
|
+
} else if (this.inlineBody !== null) {
|
|
423
|
+
this.bodySource = null;
|
|
424
|
+
this.nativeHandleAvailable = false;
|
|
425
|
+
} else if (this.payload.bodyHandle !== null) {
|
|
426
|
+
this.bodySource = null;
|
|
427
|
+
this.nativeHandleAvailable = true;
|
|
428
|
+
this.nativeHandle = {
|
|
429
|
+
id: this.payload.bodyHandle,
|
|
430
|
+
released: false
|
|
431
|
+
};
|
|
432
|
+
bodyHandleFinalizer?.register(this, this.nativeHandle, this.nativeHandle);
|
|
433
|
+
} else {
|
|
434
|
+
this.bodySource = null;
|
|
435
|
+
this.nativeHandleAvailable = false;
|
|
436
|
+
}
|
|
437
|
+
this.bodyStream = void 0;
|
|
438
|
+
}
|
|
439
|
+
get redirected() {
|
|
440
|
+
if (this.redirectedMemo !== void 0) return this.redirectedMemo;
|
|
441
|
+
if (this.url === this.requestUrl) {
|
|
442
|
+
this.redirectedMemo = false;
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
const normalizedRequestUrl = normalizeUrlForComparison(this.requestUrl);
|
|
446
|
+
this.redirectedMemo = normalizedRequestUrl ? this.url !== normalizedRequestUrl : true;
|
|
447
|
+
return this.redirectedMemo;
|
|
448
|
+
}
|
|
449
|
+
get statusText() {
|
|
450
|
+
return node_http.STATUS_CODES[this.status] ?? "";
|
|
451
|
+
}
|
|
452
|
+
get headers() {
|
|
453
|
+
if (!this.headersInstance) this.headersInstance = new Headers(this.headersInit);
|
|
454
|
+
return this.headersInstance;
|
|
455
|
+
}
|
|
456
|
+
get cookies() {
|
|
457
|
+
if (!this.cookiesRecord) {
|
|
458
|
+
const record = Object.create(null);
|
|
459
|
+
for (const [name, value] of this.cookiesInit) {
|
|
460
|
+
const existing = record[name];
|
|
461
|
+
if (existing === void 0) record[name] = value;
|
|
462
|
+
else if (Array.isArray(existing)) existing.push(value);
|
|
463
|
+
else record[name] = [existing, value];
|
|
464
|
+
}
|
|
465
|
+
this.cookiesRecord = record;
|
|
466
|
+
}
|
|
467
|
+
return this.cookiesRecord;
|
|
468
|
+
}
|
|
469
|
+
get body() {
|
|
470
|
+
if (this.inlineBody && this.bodySource === null) {
|
|
471
|
+
const bytes = this.inlineBody;
|
|
472
|
+
this.inlineBody = null;
|
|
473
|
+
this.bodySource = new node_stream_web.ReadableStream({ start(controller) {
|
|
474
|
+
controller.enqueue(bytes);
|
|
475
|
+
controller.close();
|
|
476
|
+
} });
|
|
477
|
+
}
|
|
478
|
+
if (this.inlineBody === null && this.payload.bodyHandle === null && this.bodySource === null) return null;
|
|
479
|
+
if (this.bodySource === null && this.nativeHandleAvailable && this.payload.bodyHandle !== null) {
|
|
480
|
+
if (this.nativeHandle) bodyHandleFinalizer?.unregister(this.nativeHandle);
|
|
481
|
+
const handle = this.nativeHandle ?? {
|
|
482
|
+
id: this.payload.bodyHandle,
|
|
483
|
+
released: false
|
|
484
|
+
};
|
|
485
|
+
this.nativeHandle = handle;
|
|
486
|
+
this.bodySource = createNativeBodyStream(handle);
|
|
487
|
+
this.nativeHandleAvailable = false;
|
|
488
|
+
}
|
|
489
|
+
if (this.bodySource === null) return null;
|
|
490
|
+
if (this.bodyStream === void 0) this.bodyStream = wrapBodyStream(this.bodySource, () => {
|
|
491
|
+
this.bodyUsed = true;
|
|
492
|
+
});
|
|
493
|
+
return this.bodyStream;
|
|
494
|
+
}
|
|
495
|
+
async json() {
|
|
496
|
+
const text = await this.text();
|
|
497
|
+
return JSON.parse(text);
|
|
498
|
+
}
|
|
499
|
+
async arrayBuffer() {
|
|
500
|
+
const bytes = await this.consumeBody();
|
|
501
|
+
const { buffer, byteOffset, byteLength } = bytes;
|
|
502
|
+
if (buffer instanceof ArrayBuffer) {
|
|
503
|
+
if (byteOffset === 0 && byteLength === buffer.byteLength) return buffer;
|
|
504
|
+
return buffer.slice(byteOffset, byteOffset + byteLength);
|
|
505
|
+
}
|
|
506
|
+
const view = new Uint8Array(byteLength);
|
|
507
|
+
view.set(bytes);
|
|
508
|
+
return view.buffer;
|
|
509
|
+
}
|
|
510
|
+
async text() {
|
|
511
|
+
const bytes = await this.consumeBody();
|
|
512
|
+
return UTF8_DECODER.decode(bytes);
|
|
513
|
+
}
|
|
514
|
+
async blob() {
|
|
515
|
+
const bytes = await this.consumeBody();
|
|
516
|
+
const contentType = this.headers.get("content-type") ?? "";
|
|
517
|
+
return new Blob([bytes], contentType ? { type: contentType } : void 0);
|
|
518
|
+
}
|
|
519
|
+
async formData() {
|
|
520
|
+
const bytes = await this.consumeBody();
|
|
521
|
+
const contentType = this.headers.get("content-type");
|
|
522
|
+
return new globalThis.Response(bytes, contentType ? { headers: { "content-type": contentType } } : void 0).formData();
|
|
523
|
+
}
|
|
524
|
+
readable() {
|
|
525
|
+
this.assertBodyAvailable();
|
|
526
|
+
this.bodyUsed = true;
|
|
527
|
+
const stream = this.body;
|
|
528
|
+
if (stream === null) return node_stream.Readable.from([]);
|
|
529
|
+
return node_stream.Readable.fromWeb(stream);
|
|
530
|
+
}
|
|
531
|
+
clone() {
|
|
532
|
+
if (this.bodyUsed) throw new TypeError("Cannot clone a Response whose body is already used");
|
|
533
|
+
if (this.nativeHandleAvailable && this.payload.bodyHandle !== null) {
|
|
534
|
+
if (this.nativeHandle) bodyHandleFinalizer?.unregister(this.nativeHandle);
|
|
535
|
+
const handle = this.nativeHandle ?? {
|
|
536
|
+
id: this.payload.bodyHandle,
|
|
537
|
+
released: false
|
|
538
|
+
};
|
|
539
|
+
this.nativeHandle = handle;
|
|
540
|
+
this.bodySource = createNativeBodyStream(handle);
|
|
541
|
+
this.nativeHandleAvailable = false;
|
|
542
|
+
}
|
|
543
|
+
if (this.bodySource === null) return new Response(cloneNativeResponse(this.payload), this.requestUrl, null);
|
|
544
|
+
const [branchA, branchB] = this.bodySource.tee();
|
|
545
|
+
this.bodySource = branchA;
|
|
546
|
+
this.bodyStream = void 0;
|
|
547
|
+
return new Response(cloneNativeResponse(this.payload), this.requestUrl, branchB);
|
|
548
|
+
}
|
|
549
|
+
assertBodyAvailable() {
|
|
550
|
+
if (this.bodyUsed) throw new TypeError("Response body is already used");
|
|
551
|
+
}
|
|
552
|
+
async consumeBody() {
|
|
553
|
+
this.assertBodyAvailable();
|
|
554
|
+
this.bodyUsed = true;
|
|
555
|
+
if (this.inlineBody) {
|
|
556
|
+
const bytes = this.inlineBody;
|
|
557
|
+
this.inlineBody = null;
|
|
558
|
+
return bytes;
|
|
559
|
+
}
|
|
560
|
+
if (this.nativeHandleAvailable && this.payload.bodyHandle !== null) {
|
|
561
|
+
this.nativeHandleAvailable = false;
|
|
562
|
+
try {
|
|
563
|
+
return await nativeBinding.readBodyAll(this.payload.bodyHandle);
|
|
564
|
+
} catch (error) {
|
|
565
|
+
if (String(error).includes("Body handle") && String(error).includes("not found")) return Buffer.alloc(0);
|
|
566
|
+
throw error;
|
|
567
|
+
} finally {
|
|
568
|
+
if (this.nativeHandle) markNativeBodyReleased(this.nativeHandle);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
const stream = this.body;
|
|
572
|
+
if (!stream) return Buffer.alloc(0);
|
|
573
|
+
const reader = stream.getReader();
|
|
574
|
+
const chunks = [];
|
|
575
|
+
try {
|
|
576
|
+
while (true) {
|
|
577
|
+
const { done, value } = await reader.read();
|
|
578
|
+
if (done) break;
|
|
579
|
+
if (value && value.byteLength > 0) if (Buffer.isBuffer(value)) chunks.push(value);
|
|
580
|
+
else chunks.push(Buffer.from(value.buffer, value.byteOffset, value.byteLength));
|
|
581
|
+
}
|
|
582
|
+
} finally {}
|
|
583
|
+
return chunks.length === 0 ? Buffer.alloc(0) : Buffer.concat(chunks);
|
|
584
|
+
}
|
|
729
585
|
};
|
|
730
586
|
var Transport = class {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
}
|
|
749
|
-
}
|
|
587
|
+
id;
|
|
588
|
+
disposed = false;
|
|
589
|
+
constructor(id) {
|
|
590
|
+
this.id = id;
|
|
591
|
+
}
|
|
592
|
+
get closed() {
|
|
593
|
+
return this.disposed;
|
|
594
|
+
}
|
|
595
|
+
async close() {
|
|
596
|
+
if (this.disposed) return;
|
|
597
|
+
this.disposed = true;
|
|
598
|
+
try {
|
|
599
|
+
nativeBinding.dropTransport(this.id);
|
|
600
|
+
} catch (error) {
|
|
601
|
+
throw new RequestError(String(error));
|
|
602
|
+
}
|
|
603
|
+
}
|
|
750
604
|
};
|
|
751
605
|
var Session = class {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
}
|
|
606
|
+
id;
|
|
607
|
+
disposed = false;
|
|
608
|
+
defaults;
|
|
609
|
+
constructor(id, defaults) {
|
|
610
|
+
this.id = id;
|
|
611
|
+
this.defaults = defaults;
|
|
612
|
+
}
|
|
613
|
+
get closed() {
|
|
614
|
+
return this.disposed;
|
|
615
|
+
}
|
|
616
|
+
ensureActive() {
|
|
617
|
+
if (this.disposed) throw new RequestError("Session has been closed");
|
|
618
|
+
}
|
|
619
|
+
/** @internal */
|
|
620
|
+
getDefaults() {
|
|
621
|
+
const snapshot = { ...this.defaults };
|
|
622
|
+
if (this.defaults.defaultHeaders) snapshot.defaultHeaders = [...this.defaults.defaultHeaders];
|
|
623
|
+
return snapshot;
|
|
624
|
+
}
|
|
625
|
+
/** @internal */
|
|
626
|
+
_defaultsRef() {
|
|
627
|
+
return this.defaults;
|
|
628
|
+
}
|
|
629
|
+
async fetch(input, init) {
|
|
630
|
+
this.ensureActive();
|
|
631
|
+
return fetch(input, init ? {
|
|
632
|
+
...init,
|
|
633
|
+
session: this
|
|
634
|
+
} : { session: this });
|
|
635
|
+
}
|
|
636
|
+
async clearCookies() {
|
|
637
|
+
this.ensureActive();
|
|
638
|
+
try {
|
|
639
|
+
nativeBinding.clearSession(this.id);
|
|
640
|
+
} catch (error) {
|
|
641
|
+
throw new RequestError(String(error));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
getCookies(url) {
|
|
645
|
+
this.ensureActive();
|
|
646
|
+
try {
|
|
647
|
+
return nativeBinding.getCookies(this.id, String(url));
|
|
648
|
+
} catch (error) {
|
|
649
|
+
throw new RequestError(String(error));
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
getAllCookies() {
|
|
653
|
+
this.ensureActive();
|
|
654
|
+
try {
|
|
655
|
+
return nativeBinding.getAllCookies(this.id);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
throw new RequestError(String(error));
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
setCookie(name, value, url) {
|
|
661
|
+
this.ensureActive();
|
|
662
|
+
try {
|
|
663
|
+
nativeBinding.setCookie(this.id, name, value, String(url));
|
|
664
|
+
} catch (error) {
|
|
665
|
+
throw new RequestError(String(error));
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
async websocket(urlOrOptions, options) {
|
|
669
|
+
this.ensureActive();
|
|
670
|
+
const normalized = normalizeSessionWebSocketArgs(urlOrOptions, options);
|
|
671
|
+
validateWebSocketProtocols(normalized.options.protocols);
|
|
672
|
+
assertNoManualWebSocketProtocolHeader(normalized.options.headers);
|
|
673
|
+
const protocols = normalizeWebSocketProtocolList(normalized.options.protocols);
|
|
674
|
+
const transportId = this.defaults.transportId;
|
|
675
|
+
if (!transportId) throw new RequestError("Session has no transport. Create the session with browser/os options or pass a transport to use session.websocket().");
|
|
676
|
+
return WebSocket._connectWithInit({
|
|
677
|
+
_internal: true,
|
|
678
|
+
url: normalized.url,
|
|
679
|
+
options: normalized.options,
|
|
680
|
+
openDispatchMode: "deferred",
|
|
681
|
+
connect: (callbacks) => nativeBinding.websocketConnectSession({
|
|
682
|
+
url: normalized.url,
|
|
683
|
+
sessionId: this.id,
|
|
684
|
+
transportId,
|
|
685
|
+
headers: headersToTuples(normalized.options.headers ?? {}),
|
|
686
|
+
...protocols && protocols.length > 0 && { protocols },
|
|
687
|
+
...normalized.options.maxFrameSize !== void 0 && { maxFrameSize: normalized.options.maxFrameSize },
|
|
688
|
+
...normalized.options.maxMessageSize !== void 0 && { maxMessageSize: normalized.options.maxMessageSize },
|
|
689
|
+
onMessage: callbacks.onMessage,
|
|
690
|
+
onClose: callbacks.onClose,
|
|
691
|
+
onError: callbacks.onError
|
|
692
|
+
}),
|
|
693
|
+
legacyCallbacks: normalized.legacyCallbacks
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
async close() {
|
|
697
|
+
if (this.disposed) return;
|
|
698
|
+
this.disposed = true;
|
|
699
|
+
const transportId = this.defaults.transportId;
|
|
700
|
+
const ownsTransport = this.defaults.ownsTransport;
|
|
701
|
+
try {
|
|
702
|
+
nativeBinding.dropSession(this.id);
|
|
703
|
+
} catch (error) {
|
|
704
|
+
if (!ownsTransport || !transportId) throw new RequestError(String(error));
|
|
705
|
+
const originalError = error;
|
|
706
|
+
try {
|
|
707
|
+
nativeBinding.dropTransport(transportId);
|
|
708
|
+
} catch {}
|
|
709
|
+
throw new RequestError(String(originalError));
|
|
710
|
+
}
|
|
711
|
+
if (ownsTransport && transportId) try {
|
|
712
|
+
nativeBinding.dropTransport(transportId);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
throw new RequestError(String(error));
|
|
715
|
+
}
|
|
716
|
+
}
|
|
866
717
|
};
|
|
867
718
|
function resolveSessionContext(config) {
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
cookieMode: "session",
|
|
898
|
-
dropAfterRequest: false
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
if (requestedMode === "session") {
|
|
902
|
-
throw new RequestError("cookieMode 'session' requires a session or sessionId");
|
|
903
|
-
}
|
|
904
|
-
return {
|
|
905
|
-
sessionId: generateEphemeralSessionId(),
|
|
906
|
-
cookieMode: "ephemeral",
|
|
907
|
-
dropAfterRequest: true
|
|
908
|
-
};
|
|
719
|
+
const requestedMode = config.cookieMode ?? "ephemeral";
|
|
720
|
+
const sessionCandidate = config.session;
|
|
721
|
+
const providedSessionId = typeof config.sessionId === "string" ? config.sessionId.trim() : void 0;
|
|
722
|
+
if (sessionCandidate && providedSessionId) throw new RequestError("Provide either `session` or `sessionId`, not both.");
|
|
723
|
+
if (sessionCandidate) {
|
|
724
|
+
if (!(sessionCandidate instanceof Session)) throw new RequestError("`session` must be created via createSession()");
|
|
725
|
+
if (sessionCandidate.closed) throw new RequestError("Session has been closed");
|
|
726
|
+
return {
|
|
727
|
+
sessionId: sessionCandidate.id,
|
|
728
|
+
cookieMode: "session",
|
|
729
|
+
dropAfterRequest: false,
|
|
730
|
+
defaults: sessionCandidate._defaultsRef()
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
if (providedSessionId) {
|
|
734
|
+
if (!providedSessionId) throw new RequestError("sessionId must not be empty");
|
|
735
|
+
if (requestedMode === "ephemeral") throw new RequestError("cookieMode 'ephemeral' cannot be combined with sessionId");
|
|
736
|
+
return {
|
|
737
|
+
sessionId: providedSessionId,
|
|
738
|
+
cookieMode: "session",
|
|
739
|
+
dropAfterRequest: false
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
if (requestedMode === "session") throw new RequestError("cookieMode 'session' requires a session or sessionId");
|
|
743
|
+
return {
|
|
744
|
+
sessionId: generateEphemeralSessionId(),
|
|
745
|
+
cookieMode: "ephemeral",
|
|
746
|
+
dropAfterRequest: true
|
|
747
|
+
};
|
|
909
748
|
}
|
|
910
749
|
function resolveTransportContext(config, sessionDefaults) {
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
mode: resolveEmulationMode(config.browser, config.os, config.emulation)
|
|
957
|
-
};
|
|
958
|
-
if (config.proxy !== void 0) {
|
|
959
|
-
resolved.proxy = config.proxy;
|
|
960
|
-
}
|
|
961
|
-
if (config.insecure !== void 0) {
|
|
962
|
-
resolved.insecure = config.insecure;
|
|
963
|
-
}
|
|
964
|
-
return resolved;
|
|
750
|
+
if (config.transport !== void 0) {
|
|
751
|
+
if (!(config.transport instanceof Transport)) throw new RequestError("`transport` must be created via createTransport()");
|
|
752
|
+
if (config.transport.closed) throw new RequestError("Transport has been closed");
|
|
753
|
+
const hasProxy = config.proxy !== void 0;
|
|
754
|
+
if (config.browser !== void 0 || config.os !== void 0 || config.emulation !== void 0 || hasProxy || config.insecure !== void 0 || config.trustStore !== void 0) throw new RequestError("`transport` cannot be combined with browser/os/emulation/proxy/insecure/trustStore options");
|
|
755
|
+
return {
|
|
756
|
+
transportId: config.transport.id,
|
|
757
|
+
...config.captureDiagnostics !== void 0 && { captureDiagnostics: config.captureDiagnostics }
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
if (sessionDefaults?.transportId) {
|
|
761
|
+
if (config.emulation !== void 0) throw new RequestError("Session emulation cannot be changed after creation");
|
|
762
|
+
if (config.browser !== void 0) {
|
|
763
|
+
validateBrowserProfile(config.browser);
|
|
764
|
+
const lockedBrowser = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.browser;
|
|
765
|
+
if (config.browser !== lockedBrowser) throw new RequestError("Session browser cannot be changed after creation");
|
|
766
|
+
}
|
|
767
|
+
if (config.os !== void 0) {
|
|
768
|
+
validateOperatingSystem(config.os);
|
|
769
|
+
const lockedOs = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.os;
|
|
770
|
+
if (config.os !== lockedOs) throw new RequestError("Session operating system cannot be changed after creation");
|
|
771
|
+
}
|
|
772
|
+
const initHasProxy = Object.hasOwn(config, "proxy");
|
|
773
|
+
const requestedProxy = initHasProxy ? config.proxy : void 0;
|
|
774
|
+
if (initHasProxy && requestedProxy !== void 0 && (sessionDefaults.proxy ?? null) !== (requestedProxy ?? null)) throw new RequestError("Session proxy cannot be changed after creation");
|
|
775
|
+
if (config.insecure !== void 0) {
|
|
776
|
+
const lockedInsecure = sessionDefaults.insecure ?? false;
|
|
777
|
+
if (config.insecure !== lockedInsecure) throw new RequestError("Session insecure setting cannot be changed after creation");
|
|
778
|
+
}
|
|
779
|
+
if (config.trustStore !== void 0) {
|
|
780
|
+
const lockedTrustStore = sessionDefaults.trustStore ?? DEFAULT_TRUST_STORE;
|
|
781
|
+
if (config.trustStore !== lockedTrustStore) throw new RequestError("Session trustStore setting cannot be changed after creation");
|
|
782
|
+
}
|
|
783
|
+
const captureDiagnostics = config.captureDiagnostics ?? sessionDefaults.captureDiagnostics;
|
|
784
|
+
return {
|
|
785
|
+
transportId: sessionDefaults.transportId,
|
|
786
|
+
...captureDiagnostics !== void 0 && { captureDiagnostics }
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
const resolved = { mode: resolveEmulationMode(config.browser, config.os, config.emulation) };
|
|
790
|
+
if (config.proxy !== void 0) resolved.proxy = config.proxy;
|
|
791
|
+
if (config.insecure !== void 0) resolved.insecure = config.insecure;
|
|
792
|
+
resolved.trustStore = config.trustStore ?? DEFAULT_TRUST_STORE;
|
|
793
|
+
if (config.captureDiagnostics !== void 0) resolved.captureDiagnostics = config.captureDiagnostics;
|
|
794
|
+
return resolved;
|
|
965
795
|
}
|
|
966
796
|
function createAbortError(reason) {
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
}
|
|
980
|
-
const error = new Error(fallbackMessage);
|
|
981
|
-
error.name = "AbortError";
|
|
982
|
-
return error;
|
|
797
|
+
const fallbackMessage = typeof reason === "string" ? reason : "The operation was aborted";
|
|
798
|
+
if (typeof DOMException !== "undefined" && reason instanceof DOMException) return reason.name === "AbortError" ? reason : new DOMException(reason.message || fallbackMessage, "AbortError");
|
|
799
|
+
if (reason instanceof Error) {
|
|
800
|
+
const error = new Error(reason.message);
|
|
801
|
+
error.name = "AbortError";
|
|
802
|
+
error.cause = reason;
|
|
803
|
+
return error;
|
|
804
|
+
}
|
|
805
|
+
if (typeof DOMException !== "undefined") return new DOMException(fallbackMessage, "AbortError");
|
|
806
|
+
const error = new Error(fallbackMessage);
|
|
807
|
+
error.name = "AbortError";
|
|
808
|
+
return error;
|
|
983
809
|
}
|
|
984
810
|
function isAbortError(error) {
|
|
985
|
-
|
|
811
|
+
return Boolean(error) && typeof error.name === "string" && error.name === "AbortError";
|
|
986
812
|
}
|
|
987
|
-
|
|
988
|
-
|
|
813
|
+
const REQUEST_ID_MAX = 2 ** 48;
|
|
814
|
+
let requestIdCounter = Math.trunc(Number(process.hrtime.bigint() % BigInt(REQUEST_ID_MAX - 1))) + 1;
|
|
989
815
|
function generateRequestId() {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
}
|
|
994
|
-
return requestIdCounter;
|
|
816
|
+
requestIdCounter += 1;
|
|
817
|
+
if (requestIdCounter >= REQUEST_ID_MAX) requestIdCounter = 1;
|
|
818
|
+
return requestIdCounter;
|
|
995
819
|
}
|
|
996
820
|
function setupAbort(signal, cancelNative) {
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
821
|
+
if (!signal) return null;
|
|
822
|
+
if (signal.aborted) {
|
|
823
|
+
cancelNative();
|
|
824
|
+
throw createAbortError(signal.reason);
|
|
825
|
+
}
|
|
826
|
+
let onAbortListener;
|
|
827
|
+
const promise = new Promise((_, reject) => {
|
|
828
|
+
onAbortListener = () => {
|
|
829
|
+
cancelNative();
|
|
830
|
+
reject(createAbortError(signal.reason));
|
|
831
|
+
};
|
|
832
|
+
signal.addEventListener("abort", onAbortListener, { once: true });
|
|
833
|
+
});
|
|
834
|
+
const cleanup = () => {
|
|
835
|
+
if (onAbortListener) {
|
|
836
|
+
signal.removeEventListener("abort", onAbortListener);
|
|
837
|
+
onAbortListener = void 0;
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
return {
|
|
841
|
+
promise,
|
|
842
|
+
cleanup
|
|
843
|
+
};
|
|
1019
844
|
}
|
|
1020
845
|
function coerceUrlInput(input) {
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
if (input.charCodeAt(0) > 32 && input.charCodeAt(input.length - 1) > 32) {
|
|
1028
|
-
return input;
|
|
1029
|
-
}
|
|
1030
|
-
const trimmed = input.trim();
|
|
1031
|
-
if (trimmed.length === 0) {
|
|
1032
|
-
throw new RequestError("URL is required");
|
|
1033
|
-
}
|
|
1034
|
-
return trimmed;
|
|
846
|
+
if (input instanceof URL) return input.href;
|
|
847
|
+
if (input.length === 0) throw new RequestError("URL is required");
|
|
848
|
+
if (input.charCodeAt(0) > 32 && input.charCodeAt(input.length - 1) > 32) return input;
|
|
849
|
+
const trimmed = input.trim();
|
|
850
|
+
if (trimmed.length === 0) throw new RequestError("URL is required");
|
|
851
|
+
return trimmed;
|
|
1035
852
|
}
|
|
1036
853
|
function isRequestLike(input) {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
return true;
|
|
1042
|
-
}
|
|
1043
|
-
const candidate = input;
|
|
1044
|
-
return typeof candidate.url === "string" && typeof candidate.method === "string" && typeof candidate.arrayBuffer === "function" && typeof candidate.redirect === "string";
|
|
854
|
+
if (!input || typeof input !== "object") return false;
|
|
855
|
+
if (typeof Request !== "undefined" && input instanceof Request) return true;
|
|
856
|
+
const candidate = input;
|
|
857
|
+
return typeof candidate.url === "string" && typeof candidate.method === "string" && typeof candidate.arrayBuffer === "function" && typeof candidate.redirect === "string";
|
|
1045
858
|
}
|
|
1046
859
|
async function resolveFetchArgs(input, init) {
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
if (input.bodyUsed) {
|
|
1065
|
-
throw new TypeError("Request body is already used");
|
|
1066
|
-
}
|
|
1067
|
-
mergedInit.body = Buffer.from(await input.arrayBuffer());
|
|
1068
|
-
}
|
|
1069
|
-
return { url: coerceUrlInput(input.url), init: mergedInit };
|
|
860
|
+
if (!isRequestLike(input)) return {
|
|
861
|
+
url: coerceUrlInput(input),
|
|
862
|
+
init: init ?? {}
|
|
863
|
+
};
|
|
864
|
+
const mergedInit = init ? { ...init } : {};
|
|
865
|
+
if (mergedInit.method === void 0) mergedInit.method = input.method;
|
|
866
|
+
if (mergedInit.headers === void 0) mergedInit.headers = input.headers;
|
|
867
|
+
if (mergedInit.redirect === void 0 && (input.redirect === "follow" || input.redirect === "manual" || input.redirect === "error")) mergedInit.redirect = input.redirect;
|
|
868
|
+
if (mergedInit.signal === void 0) mergedInit.signal = input.signal;
|
|
869
|
+
if (mergedInit.body === void 0 && input.body !== null) {
|
|
870
|
+
if (input.bodyUsed) throw new TypeError("Request body is already used");
|
|
871
|
+
mergedInit.body = Buffer.from(await input.arrayBuffer());
|
|
872
|
+
}
|
|
873
|
+
return {
|
|
874
|
+
url: coerceUrlInput(input.url),
|
|
875
|
+
init: mergedInit
|
|
876
|
+
};
|
|
1070
877
|
}
|
|
1071
878
|
function normalizeUrlForComparison(value) {
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
879
|
+
try {
|
|
880
|
+
return new URL(value).toString();
|
|
881
|
+
} catch {
|
|
882
|
+
return null;
|
|
883
|
+
}
|
|
1077
884
|
}
|
|
1078
885
|
function validateRedirectMode(mode) {
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
}
|
|
1082
|
-
throw new RequestError(`Redirect mode '${mode}' is not supported`);
|
|
886
|
+
if (mode === void 0 || mode === "follow" || mode === "manual" || mode === "error") return;
|
|
887
|
+
throw new RequestError(`Redirect mode '${mode}' is not supported`);
|
|
1083
888
|
}
|
|
1084
889
|
async function serializeBody(body) {
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
const buffer = Buffer.from(await body.arrayBuffer());
|
|
1108
|
-
return { body: buffer, ...body.type ? { contentType: body.type } : {} };
|
|
1109
|
-
}
|
|
1110
|
-
if (typeof FormData !== "undefined" && body instanceof FormData) {
|
|
1111
|
-
const encoded = new globalThis.Response(body);
|
|
1112
|
-
const contentType = encoded.headers.get("content-type") ?? void 0;
|
|
1113
|
-
const buffer = Buffer.from(await encoded.arrayBuffer());
|
|
1114
|
-
return { body: buffer, ...contentType ? { contentType } : {} };
|
|
1115
|
-
}
|
|
1116
|
-
throw new TypeError(
|
|
1117
|
-
"Unsupported body type; expected string, Buffer, ArrayBuffer, ArrayBufferView, URLSearchParams, Blob, or FormData"
|
|
1118
|
-
);
|
|
890
|
+
if (body === null || body === void 0) return {};
|
|
891
|
+
if (typeof body === "string") return { body: Buffer.from(body, "utf8") };
|
|
892
|
+
if (Buffer.isBuffer(body)) return { body };
|
|
893
|
+
if (body instanceof URLSearchParams) return {
|
|
894
|
+
body: Buffer.from(body.toString(), "utf8"),
|
|
895
|
+
contentType: "application/x-www-form-urlencoded;charset=UTF-8"
|
|
896
|
+
};
|
|
897
|
+
if (body instanceof ArrayBuffer) return { body: Buffer.from(body) };
|
|
898
|
+
if (ArrayBuffer.isView(body)) return { body: Buffer.from(body.buffer, body.byteOffset, body.byteLength) };
|
|
899
|
+
if (typeof Blob !== "undefined" && body instanceof Blob) return {
|
|
900
|
+
body: Buffer.from(await body.arrayBuffer()),
|
|
901
|
+
...body.type ? { contentType: body.type } : {}
|
|
902
|
+
};
|
|
903
|
+
if (typeof FormData !== "undefined" && body instanceof FormData) {
|
|
904
|
+
const encoded = new globalThis.Response(body);
|
|
905
|
+
const contentType = encoded.headers.get("content-type") ?? void 0;
|
|
906
|
+
return {
|
|
907
|
+
body: Buffer.from(await encoded.arrayBuffer()),
|
|
908
|
+
...contentType ? { contentType } : {}
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
throw new TypeError("Unsupported body type; expected string, Buffer, ArrayBuffer, ArrayBufferView, URLSearchParams, Blob, or FormData");
|
|
1119
912
|
}
|
|
1120
913
|
function ensureMethod(method) {
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
}
|
|
1134
|
-
const normalized = method.trim().toUpperCase();
|
|
1135
|
-
return normalized.length > 0 ? normalized : "GET";
|
|
914
|
+
if (method === void 0 || method.length === 0) return "GET";
|
|
915
|
+
switch (method) {
|
|
916
|
+
case "GET":
|
|
917
|
+
case "POST":
|
|
918
|
+
case "PUT":
|
|
919
|
+
case "DELETE":
|
|
920
|
+
case "PATCH":
|
|
921
|
+
case "HEAD":
|
|
922
|
+
case "OPTIONS": return method;
|
|
923
|
+
}
|
|
924
|
+
const normalized = method.trim().toUpperCase();
|
|
925
|
+
return normalized.length > 0 ? normalized : "GET";
|
|
1136
926
|
}
|
|
1137
927
|
function ensureBodyAllowed(method, body) {
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
}
|
|
1141
|
-
if (method === "GET" || method === "HEAD") {
|
|
1142
|
-
throw new RequestError(`Request with ${method} method cannot have a body`);
|
|
1143
|
-
}
|
|
928
|
+
if (body === void 0) return;
|
|
929
|
+
if (method === "GET" || method === "HEAD") throw new RequestError(`Request with ${method} method cannot have a body`);
|
|
1144
930
|
}
|
|
1145
931
|
function validateBrowserProfile(browser) {
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
if (typeof browser !== "string" || browser.trim().length === 0) {
|
|
1150
|
-
throw new RequestError("Browser profile must not be empty");
|
|
1151
|
-
}
|
|
1152
|
-
if (!getProfileSet().has(browser)) {
|
|
1153
|
-
throw new RequestError(`Invalid browser profile: ${browser}. Available profiles: ${getProfiles().join(", ")}`);
|
|
1154
|
-
}
|
|
932
|
+
if (browser === void 0) return;
|
|
933
|
+
if (typeof browser !== "string" || browser.trim().length === 0) throw new RequestError("Browser profile must not be empty");
|
|
934
|
+
if (!getProfileSet().has(browser)) throw new RequestError(`Invalid browser profile: ${browser}. Available profiles: ${getProfiles().join(", ")}`);
|
|
1155
935
|
}
|
|
1156
936
|
function validateOperatingSystem(os) {
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
if (typeof os !== "string" || os.trim().length === 0) {
|
|
1161
|
-
throw new RequestError("Operating system must not be empty");
|
|
1162
|
-
}
|
|
1163
|
-
if (!getOperatingSystemSet().has(os)) {
|
|
1164
|
-
throw new RequestError(`Invalid operating system: ${os}. Available options: ${getOperatingSystems().join(", ")}`);
|
|
1165
|
-
}
|
|
937
|
+
if (os === void 0) return;
|
|
938
|
+
if (typeof os !== "string" || os.trim().length === 0) throw new RequestError("Operating system must not be empty");
|
|
939
|
+
if (!getOperatingSystemSet().has(os)) throw new RequestError(`Invalid operating system: ${os}. Available options: ${getOperatingSystems().join(", ")}`);
|
|
1166
940
|
}
|
|
1167
941
|
function validateTimeout(timeout) {
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
throw new RequestError("Timeout must be 0 (no timeout) or a positive number");
|
|
1176
|
-
}
|
|
942
|
+
if (timeout === void 0) return;
|
|
943
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout)) throw new RequestError("Timeout must be a finite number");
|
|
944
|
+
if (timeout < 0) throw new RequestError("Timeout must be 0 (no timeout) or a positive number");
|
|
945
|
+
}
|
|
946
|
+
function validateTrustStore(trustStore) {
|
|
947
|
+
if (trustStore === void 0) return;
|
|
948
|
+
if (trustStore !== "combined" && trustStore !== "mozilla" && trustStore !== "defaultPaths") throw new RequestError("trustStore must be one of: combined, mozilla, defaultPaths");
|
|
1177
949
|
}
|
|
1178
950
|
function validatePositiveNumber(value, label) {
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
}
|
|
1182
|
-
if (value <= 0) {
|
|
1183
|
-
throw new RequestError(`${label} must be greater than 0`);
|
|
1184
|
-
}
|
|
951
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new RequestError(`${label} must be a finite number`);
|
|
952
|
+
if (value <= 0) throw new RequestError(`${label} must be greater than 0`);
|
|
1185
953
|
}
|
|
1186
954
|
function validateNonNegativeInteger(value, label) {
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
}
|
|
1190
|
-
if (value < 0) {
|
|
1191
|
-
throw new RequestError(`${label} must be greater than or equal to 0`);
|
|
1192
|
-
}
|
|
955
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !Number.isInteger(value)) throw new RequestError(`${label} must be an integer`);
|
|
956
|
+
if (value < 0) throw new RequestError(`${label} must be greater than or equal to 0`);
|
|
1193
957
|
}
|
|
1194
958
|
function validatePositiveInteger(value, label) {
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
}
|
|
1198
|
-
if (value <= 0) {
|
|
1199
|
-
throw new RequestError(`${label} must be greater than 0`);
|
|
1200
|
-
}
|
|
959
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !Number.isInteger(value)) throw new RequestError(`${label} must be an integer`);
|
|
960
|
+
if (value <= 0) throw new RequestError(`${label} must be greater than 0`);
|
|
1201
961
|
}
|
|
1202
962
|
function validateIntegerInRange(value, min, max, label) {
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
963
|
+
validateNonNegativeInteger(value, label);
|
|
964
|
+
if (value < min || value > max) throw new RequestError(`${label} must be between ${min} and ${max}`);
|
|
965
|
+
}
|
|
966
|
+
const SUPPORTED_ALPN_PROTOCOLS = new Set([
|
|
967
|
+
"HTTP1",
|
|
968
|
+
"HTTP2",
|
|
969
|
+
"HTTP3"
|
|
970
|
+
]);
|
|
971
|
+
const SUPPORTED_ALPS_PROTOCOLS = new Set([
|
|
972
|
+
"HTTP1",
|
|
973
|
+
"HTTP2",
|
|
974
|
+
"HTTP3"
|
|
975
|
+
]);
|
|
976
|
+
const SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS = new Set([
|
|
977
|
+
"zlib",
|
|
978
|
+
"brotli",
|
|
979
|
+
"zstd"
|
|
980
|
+
]);
|
|
981
|
+
const HTTP2_SETTING_IDS = new Set([
|
|
982
|
+
"HeaderTableSize",
|
|
983
|
+
"EnablePush",
|
|
984
|
+
"MaxConcurrentStreams",
|
|
985
|
+
"InitialWindowSize",
|
|
986
|
+
"MaxFrameSize",
|
|
987
|
+
"MaxHeaderListSize",
|
|
988
|
+
"EnableConnectProtocol",
|
|
989
|
+
"NoRfc7540Priorities"
|
|
1220
990
|
]);
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
991
|
+
const HTTP2_PSEUDO_HEADER_IDS = new Set([
|
|
992
|
+
"Method",
|
|
993
|
+
"Scheme",
|
|
994
|
+
"Authority",
|
|
995
|
+
"Path",
|
|
996
|
+
"Protocol"
|
|
997
|
+
]);
|
|
998
|
+
const STANDARD_HTTP2_SETTING_ID_VALUES = new Set([
|
|
999
|
+
1,
|
|
1000
|
+
2,
|
|
1001
|
+
3,
|
|
1002
|
+
4,
|
|
1003
|
+
5,
|
|
1004
|
+
6,
|
|
1005
|
+
8,
|
|
1006
|
+
9
|
|
1007
|
+
]);
|
|
1008
|
+
const MAX_HTTP2_EXPERIMENTAL_SETTING_ID = 15;
|
|
1009
|
+
const TLS_VERSION_ALIASES = new Map([
|
|
1010
|
+
["1.0", "1.0"],
|
|
1011
|
+
["1.1", "1.1"],
|
|
1012
|
+
["1.2", "1.2"],
|
|
1013
|
+
["1.3", "1.3"],
|
|
1014
|
+
["tls1.0", "1.0"],
|
|
1015
|
+
["tls1.1", "1.1"],
|
|
1016
|
+
["tls1.2", "1.2"],
|
|
1017
|
+
["tls1.3", "1.3"]
|
|
1233
1018
|
]);
|
|
1234
1019
|
function isNonEmpty(value) {
|
|
1235
|
-
|
|
1236
|
-
|
|
1020
|
+
for (const _ in value) return true;
|
|
1021
|
+
return false;
|
|
1237
1022
|
}
|
|
1238
1023
|
function normalizeProtocolList(value, label, allowed) {
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
throw new RequestError(`${label} must be an array`);
|
|
1244
|
-
}
|
|
1245
|
-
for (const protocol of value) {
|
|
1246
|
-
if (!allowed.has(protocol)) {
|
|
1247
|
-
throw new RequestError(`${label} values must be one of: HTTP1, HTTP2, HTTP3`);
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
return [...value];
|
|
1024
|
+
if (value === void 0) return;
|
|
1025
|
+
if (!Array.isArray(value)) throw new RequestError(`${label} must be an array`);
|
|
1026
|
+
for (const protocol of value) if (!allowed.has(protocol)) throw new RequestError(`${label} values must be one of: HTTP1, HTTP2, HTTP3`);
|
|
1027
|
+
return [...value];
|
|
1251
1028
|
}
|
|
1252
1029
|
function normalizeTlsVersion(value, label) {
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
}
|
|
1259
|
-
const normalized = TLS_VERSION_ALIASES.get(value.trim().toLowerCase());
|
|
1260
|
-
if (!normalized) {
|
|
1261
|
-
throw new RequestError(`${label} must be one of: 1.0, 1.1, 1.2, 1.3`);
|
|
1262
|
-
}
|
|
1263
|
-
return normalized;
|
|
1030
|
+
if (value === void 0) return;
|
|
1031
|
+
if (typeof value !== "string") throw new RequestError(`${label} must be a string`);
|
|
1032
|
+
const normalized = TLS_VERSION_ALIASES.get(value.trim().toLowerCase());
|
|
1033
|
+
if (!normalized) throw new RequestError(`${label} must be one of: 1.0, 1.1, 1.2, 1.3`);
|
|
1034
|
+
return normalized;
|
|
1264
1035
|
}
|
|
1265
1036
|
function normalizeOrigHeaders(origHeaders) {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
const duplicateKey = trimmed.toLowerCase();
|
|
1280
|
-
if (seen.has(duplicateKey)) {
|
|
1281
|
-
throw new RequestError(`Duplicate emulation.origHeaders entry: ${trimmed}`);
|
|
1282
|
-
}
|
|
1283
|
-
seen.add(duplicateKey);
|
|
1284
|
-
normalized.push(trimmed);
|
|
1285
|
-
}
|
|
1286
|
-
return normalized.length > 0 ? normalized : void 0;
|
|
1037
|
+
if (origHeaders === void 0) return;
|
|
1038
|
+
if (!Array.isArray(origHeaders)) throw new RequestError("emulation.origHeaders must be an array of strings");
|
|
1039
|
+
const normalized = [];
|
|
1040
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1041
|
+
for (const entry of origHeaders) {
|
|
1042
|
+
if (typeof entry !== "string" || entry.trim().length === 0) throw new RequestError("emulation.origHeaders entries must be non-empty strings");
|
|
1043
|
+
const trimmed = entry.trim();
|
|
1044
|
+
const duplicateKey = trimmed.toLowerCase();
|
|
1045
|
+
if (seen.has(duplicateKey)) throw new RequestError(`Duplicate emulation.origHeaders entry: ${trimmed}`);
|
|
1046
|
+
seen.add(duplicateKey);
|
|
1047
|
+
normalized.push(trimmed);
|
|
1048
|
+
}
|
|
1049
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
1287
1050
|
}
|
|
1288
1051
|
function normalizeCustomTlsOptions(options) {
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
if (options.aesHwOverride !== void 0) {
|
|
1351
|
-
normalized.aesHwOverride = options.aesHwOverride;
|
|
1352
|
-
}
|
|
1353
|
-
if (options.preserveTls13CipherList !== void 0) {
|
|
1354
|
-
normalized.preserveTls13CipherList = options.preserveTls13CipherList;
|
|
1355
|
-
}
|
|
1356
|
-
if (options.randomAesHwOverride !== void 0) {
|
|
1357
|
-
normalized.randomAesHwOverride = options.randomAesHwOverride;
|
|
1358
|
-
}
|
|
1359
|
-
if (options.delegatedCredentials !== void 0) {
|
|
1360
|
-
normalized.delegatedCredentials = options.delegatedCredentials;
|
|
1361
|
-
}
|
|
1362
|
-
if (options.curvesList !== void 0) {
|
|
1363
|
-
normalized.curvesList = options.curvesList;
|
|
1364
|
-
}
|
|
1365
|
-
if (options.cipherList !== void 0) {
|
|
1366
|
-
normalized.cipherList = options.cipherList;
|
|
1367
|
-
}
|
|
1368
|
-
if (options.sigalgsList !== void 0) {
|
|
1369
|
-
normalized.sigalgsList = options.sigalgsList;
|
|
1370
|
-
}
|
|
1371
|
-
if (options.recordSizeLimit !== void 0) {
|
|
1372
|
-
validateIntegerInRange(options.recordSizeLimit, 0, 65535, "emulation.tlsOptions.recordSizeLimit");
|
|
1373
|
-
normalized.recordSizeLimit = options.recordSizeLimit;
|
|
1374
|
-
}
|
|
1375
|
-
if (options.keySharesLimit !== void 0) {
|
|
1376
|
-
validateIntegerInRange(options.keySharesLimit, 0, 255, "emulation.tlsOptions.keySharesLimit");
|
|
1377
|
-
normalized.keySharesLimit = options.keySharesLimit;
|
|
1378
|
-
}
|
|
1379
|
-
if (options.certificateCompressionAlgorithms !== void 0) {
|
|
1380
|
-
if (!Array.isArray(options.certificateCompressionAlgorithms)) {
|
|
1381
|
-
throw new RequestError("emulation.tlsOptions.certificateCompressionAlgorithms must be an array");
|
|
1382
|
-
}
|
|
1383
|
-
const algorithms = [];
|
|
1384
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1385
|
-
for (const algorithm of options.certificateCompressionAlgorithms) {
|
|
1386
|
-
if (!SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS.has(algorithm)) {
|
|
1387
|
-
throw new RequestError(
|
|
1388
|
-
"emulation.tlsOptions.certificateCompressionAlgorithms values must be one of: zlib, brotli, zstd"
|
|
1389
|
-
);
|
|
1390
|
-
}
|
|
1391
|
-
if (seen.has(algorithm)) {
|
|
1392
|
-
throw new RequestError(`Duplicate emulation.tlsOptions.certificateCompressionAlgorithms entry: ${algorithm}`);
|
|
1393
|
-
}
|
|
1394
|
-
seen.add(algorithm);
|
|
1395
|
-
algorithms.push(algorithm);
|
|
1396
|
-
}
|
|
1397
|
-
normalized.certificateCompressionAlgorithms = algorithms;
|
|
1398
|
-
}
|
|
1399
|
-
if (options.extensionPermutation !== void 0) {
|
|
1400
|
-
if (!Array.isArray(options.extensionPermutation)) {
|
|
1401
|
-
throw new RequestError("emulation.tlsOptions.extensionPermutation must be an array");
|
|
1402
|
-
}
|
|
1403
|
-
const permutation = [];
|
|
1404
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1405
|
-
for (const extensionId of options.extensionPermutation) {
|
|
1406
|
-
validateIntegerInRange(extensionId, 0, 65535, "emulation.tlsOptions.extensionPermutation");
|
|
1407
|
-
if (seen.has(extensionId)) {
|
|
1408
|
-
throw new RequestError(`Duplicate emulation.tlsOptions.extensionPermutation entry: ${extensionId}`);
|
|
1409
|
-
}
|
|
1410
|
-
seen.add(extensionId);
|
|
1411
|
-
permutation.push(extensionId);
|
|
1412
|
-
}
|
|
1413
|
-
normalized.extensionPermutation = permutation;
|
|
1414
|
-
}
|
|
1415
|
-
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1052
|
+
if (options === void 0) return;
|
|
1053
|
+
const normalized = {};
|
|
1054
|
+
const alpnProtocols = normalizeProtocolList(options.alpnProtocols, "emulation.tlsOptions.alpnProtocols", SUPPORTED_ALPN_PROTOCOLS);
|
|
1055
|
+
if (alpnProtocols !== void 0) normalized.alpnProtocols = alpnProtocols;
|
|
1056
|
+
const alpsProtocols = normalizeProtocolList(options.alpsProtocols, "emulation.tlsOptions.alpsProtocols", SUPPORTED_ALPS_PROTOCOLS);
|
|
1057
|
+
if (alpsProtocols !== void 0) normalized.alpsProtocols = alpsProtocols;
|
|
1058
|
+
const minTlsVersion = normalizeTlsVersion(options.minTlsVersion, "emulation.tlsOptions.minTlsVersion");
|
|
1059
|
+
if (minTlsVersion !== void 0) normalized.minTlsVersion = minTlsVersion;
|
|
1060
|
+
const maxTlsVersion = normalizeTlsVersion(options.maxTlsVersion, "emulation.tlsOptions.maxTlsVersion");
|
|
1061
|
+
if (maxTlsVersion !== void 0) normalized.maxTlsVersion = maxTlsVersion;
|
|
1062
|
+
if (options.alpsUseNewCodepoint !== void 0) normalized.alpsUseNewCodepoint = options.alpsUseNewCodepoint;
|
|
1063
|
+
if (options.sessionTicket !== void 0) normalized.sessionTicket = options.sessionTicket;
|
|
1064
|
+
if (options.preSharedKey !== void 0) normalized.preSharedKey = options.preSharedKey;
|
|
1065
|
+
if (options.enableEchGrease !== void 0) normalized.enableEchGrease = options.enableEchGrease;
|
|
1066
|
+
if (options.permuteExtensions !== void 0) normalized.permuteExtensions = options.permuteExtensions;
|
|
1067
|
+
if (options.greaseEnabled !== void 0) normalized.greaseEnabled = options.greaseEnabled;
|
|
1068
|
+
if (options.enableOcspStapling !== void 0) normalized.enableOcspStapling = options.enableOcspStapling;
|
|
1069
|
+
if (options.enableSignedCertTimestamps !== void 0) normalized.enableSignedCertTimestamps = options.enableSignedCertTimestamps;
|
|
1070
|
+
if (options.pskSkipSessionTicket !== void 0) normalized.pskSkipSessionTicket = options.pskSkipSessionTicket;
|
|
1071
|
+
if (options.pskDheKe !== void 0) normalized.pskDheKe = options.pskDheKe;
|
|
1072
|
+
if (options.renegotiation !== void 0) normalized.renegotiation = options.renegotiation;
|
|
1073
|
+
if (options.aesHwOverride !== void 0) normalized.aesHwOverride = options.aesHwOverride;
|
|
1074
|
+
if (options.preserveTls13CipherList !== void 0) normalized.preserveTls13CipherList = options.preserveTls13CipherList;
|
|
1075
|
+
if (options.randomAesHwOverride !== void 0) normalized.randomAesHwOverride = options.randomAesHwOverride;
|
|
1076
|
+
if (options.delegatedCredentials !== void 0) normalized.delegatedCredentials = options.delegatedCredentials;
|
|
1077
|
+
if (options.curvesList !== void 0) normalized.curvesList = options.curvesList;
|
|
1078
|
+
if (options.cipherList !== void 0) normalized.cipherList = options.cipherList;
|
|
1079
|
+
if (options.sigalgsList !== void 0) normalized.sigalgsList = options.sigalgsList;
|
|
1080
|
+
if (options.recordSizeLimit !== void 0) {
|
|
1081
|
+
validateIntegerInRange(options.recordSizeLimit, 0, 65535, "emulation.tlsOptions.recordSizeLimit");
|
|
1082
|
+
normalized.recordSizeLimit = options.recordSizeLimit;
|
|
1083
|
+
}
|
|
1084
|
+
if (options.keySharesLimit !== void 0) {
|
|
1085
|
+
validateIntegerInRange(options.keySharesLimit, 0, 255, "emulation.tlsOptions.keySharesLimit");
|
|
1086
|
+
normalized.keySharesLimit = options.keySharesLimit;
|
|
1087
|
+
}
|
|
1088
|
+
if (options.certificateCompressionAlgorithms !== void 0) {
|
|
1089
|
+
if (!Array.isArray(options.certificateCompressionAlgorithms)) throw new RequestError("emulation.tlsOptions.certificateCompressionAlgorithms must be an array");
|
|
1090
|
+
const algorithms = [];
|
|
1091
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1092
|
+
for (const algorithm of options.certificateCompressionAlgorithms) {
|
|
1093
|
+
if (!SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS.has(algorithm)) throw new RequestError("emulation.tlsOptions.certificateCompressionAlgorithms values must be one of: zlib, brotli, zstd");
|
|
1094
|
+
if (seen.has(algorithm)) throw new RequestError(`Duplicate emulation.tlsOptions.certificateCompressionAlgorithms entry: ${algorithm}`);
|
|
1095
|
+
seen.add(algorithm);
|
|
1096
|
+
algorithms.push(algorithm);
|
|
1097
|
+
}
|
|
1098
|
+
normalized.certificateCompressionAlgorithms = algorithms;
|
|
1099
|
+
}
|
|
1100
|
+
if (options.extensionPermutation !== void 0) {
|
|
1101
|
+
if (!Array.isArray(options.extensionPermutation)) throw new RequestError("emulation.tlsOptions.extensionPermutation must be an array");
|
|
1102
|
+
const permutation = [];
|
|
1103
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1104
|
+
for (const extensionId of options.extensionPermutation) {
|
|
1105
|
+
validateIntegerInRange(extensionId, 0, 65535, "emulation.tlsOptions.extensionPermutation");
|
|
1106
|
+
if (seen.has(extensionId)) throw new RequestError(`Duplicate emulation.tlsOptions.extensionPermutation entry: ${extensionId}`);
|
|
1107
|
+
seen.add(extensionId);
|
|
1108
|
+
permutation.push(extensionId);
|
|
1109
|
+
}
|
|
1110
|
+
normalized.extensionPermutation = permutation;
|
|
1111
|
+
}
|
|
1112
|
+
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1416
1113
|
}
|
|
1417
1114
|
function normalizeCustomHttp1Options(options) {
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
}
|
|
1441
|
-
if (options.readBufExactSize !== void 0) {
|
|
1442
|
-
validateNonNegativeInteger(options.readBufExactSize, "emulation.http1Options.readBufExactSize");
|
|
1443
|
-
normalized.readBufExactSize = options.readBufExactSize;
|
|
1444
|
-
}
|
|
1445
|
-
if (options.maxBufSize !== void 0) {
|
|
1446
|
-
validateNonNegativeInteger(options.maxBufSize, "emulation.http1Options.maxBufSize");
|
|
1447
|
-
if (options.maxBufSize < 8192) {
|
|
1448
|
-
throw new RequestError("emulation.http1Options.maxBufSize must be greater than or equal to 8192");
|
|
1449
|
-
}
|
|
1450
|
-
normalized.maxBufSize = options.maxBufSize;
|
|
1451
|
-
}
|
|
1452
|
-
if (normalized.readBufExactSize !== void 0 && normalized.maxBufSize !== void 0) {
|
|
1453
|
-
throw new RequestError("emulation.http1Options.readBufExactSize and maxBufSize cannot both be set");
|
|
1454
|
-
}
|
|
1455
|
-
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1115
|
+
if (options === void 0) return;
|
|
1116
|
+
const normalized = {};
|
|
1117
|
+
if (options.http09Responses !== void 0) normalized.http09Responses = options.http09Responses;
|
|
1118
|
+
if (options.writev !== void 0) normalized.writev = options.writev;
|
|
1119
|
+
if (options.ignoreInvalidHeadersInResponses !== void 0) normalized.ignoreInvalidHeadersInResponses = options.ignoreInvalidHeadersInResponses;
|
|
1120
|
+
if (options.allowSpacesAfterHeaderNameInResponses !== void 0) normalized.allowSpacesAfterHeaderNameInResponses = options.allowSpacesAfterHeaderNameInResponses;
|
|
1121
|
+
if (options.allowObsoleteMultilineHeadersInResponses !== void 0) normalized.allowObsoleteMultilineHeadersInResponses = options.allowObsoleteMultilineHeadersInResponses;
|
|
1122
|
+
if (options.maxHeaders !== void 0) {
|
|
1123
|
+
validateNonNegativeInteger(options.maxHeaders, "emulation.http1Options.maxHeaders");
|
|
1124
|
+
normalized.maxHeaders = options.maxHeaders;
|
|
1125
|
+
}
|
|
1126
|
+
if (options.readBufExactSize !== void 0) {
|
|
1127
|
+
validateNonNegativeInteger(options.readBufExactSize, "emulation.http1Options.readBufExactSize");
|
|
1128
|
+
normalized.readBufExactSize = options.readBufExactSize;
|
|
1129
|
+
}
|
|
1130
|
+
if (options.maxBufSize !== void 0) {
|
|
1131
|
+
validateNonNegativeInteger(options.maxBufSize, "emulation.http1Options.maxBufSize");
|
|
1132
|
+
if (options.maxBufSize < 8192) throw new RequestError("emulation.http1Options.maxBufSize must be greater than or equal to 8192");
|
|
1133
|
+
normalized.maxBufSize = options.maxBufSize;
|
|
1134
|
+
}
|
|
1135
|
+
if (normalized.readBufExactSize !== void 0 && normalized.maxBufSize !== void 0) throw new RequestError("emulation.http1Options.readBufExactSize and maxBufSize cannot both be set");
|
|
1136
|
+
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1456
1137
|
}
|
|
1457
1138
|
function normalizeHttp2StreamDependency(dependency, label) {
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
if (dependency.exclusive !== void 0) {
|
|
1468
|
-
normalized.exclusive = dependency.exclusive;
|
|
1469
|
-
}
|
|
1470
|
-
return normalized;
|
|
1139
|
+
if (!isPlainObject(dependency)) throw new RequestError(`${label} must be an object`);
|
|
1140
|
+
validateIntegerInRange(dependency.dependencyId, 0, 2147483647, `${label}.dependencyId`);
|
|
1141
|
+
validateIntegerInRange(dependency.weight, 0, 255, `${label}.weight`);
|
|
1142
|
+
const normalized = {
|
|
1143
|
+
dependencyId: dependency.dependencyId,
|
|
1144
|
+
weight: dependency.weight
|
|
1145
|
+
};
|
|
1146
|
+
if (dependency.exclusive !== void 0) normalized.exclusive = dependency.exclusive;
|
|
1147
|
+
return normalized;
|
|
1471
1148
|
}
|
|
1472
1149
|
function normalizeCustomHttp2Options(options) {
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
if (options.priorities !== void 0) {
|
|
1593
|
-
if (!Array.isArray(options.priorities)) {
|
|
1594
|
-
throw new RequestError("emulation.http2Options.priorities must be an array");
|
|
1595
|
-
}
|
|
1596
|
-
const priorities = [];
|
|
1597
|
-
const seenStreamIds = /* @__PURE__ */ new Set();
|
|
1598
|
-
for (const [index, priority] of options.priorities.entries()) {
|
|
1599
|
-
if (!isPlainObject(priority)) {
|
|
1600
|
-
throw new RequestError(`emulation.http2Options.priorities[${index}] must be an object`);
|
|
1601
|
-
}
|
|
1602
|
-
validatePositiveInteger(priority.streamId, `emulation.http2Options.priorities[${index}].streamId`);
|
|
1603
|
-
if (seenStreamIds.has(priority.streamId)) {
|
|
1604
|
-
throw new RequestError(`Duplicate emulation.http2Options.priorities streamId: ${priority.streamId}`);
|
|
1605
|
-
}
|
|
1606
|
-
seenStreamIds.add(priority.streamId);
|
|
1607
|
-
priorities.push({
|
|
1608
|
-
streamId: priority.streamId,
|
|
1609
|
-
dependency: normalizeHttp2StreamDependency(
|
|
1610
|
-
priority.dependency,
|
|
1611
|
-
`emulation.http2Options.priorities[${index}].dependency`
|
|
1612
|
-
)
|
|
1613
|
-
});
|
|
1614
|
-
}
|
|
1615
|
-
normalized.priorities = priorities;
|
|
1616
|
-
}
|
|
1617
|
-
if (options.experimentalSettings !== void 0) {
|
|
1618
|
-
if (!Array.isArray(options.experimentalSettings)) {
|
|
1619
|
-
throw new RequestError("emulation.http2Options.experimentalSettings must be an array");
|
|
1620
|
-
}
|
|
1621
|
-
const experimentalSettings = [];
|
|
1622
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
1623
|
-
for (const [index, setting] of options.experimentalSettings.entries()) {
|
|
1624
|
-
if (!isPlainObject(setting)) {
|
|
1625
|
-
throw new RequestError(`emulation.http2Options.experimentalSettings[${index}] must be an object`);
|
|
1626
|
-
}
|
|
1627
|
-
validateIntegerInRange(
|
|
1628
|
-
setting.id,
|
|
1629
|
-
1,
|
|
1630
|
-
MAX_HTTP2_EXPERIMENTAL_SETTING_ID,
|
|
1631
|
-
`emulation.http2Options.experimentalSettings[${index}].id`
|
|
1632
|
-
);
|
|
1633
|
-
if (STANDARD_HTTP2_SETTING_ID_VALUES.has(setting.id)) {
|
|
1634
|
-
throw new RequestError(
|
|
1635
|
-
`emulation.http2Options.experimentalSettings[${index}].id must not be a standard HTTP/2 setting id`
|
|
1636
|
-
);
|
|
1637
|
-
}
|
|
1638
|
-
if (seenIds.has(setting.id)) {
|
|
1639
|
-
throw new RequestError(`Duplicate emulation.http2Options.experimentalSettings id: ${setting.id}`);
|
|
1640
|
-
}
|
|
1641
|
-
seenIds.add(setting.id);
|
|
1642
|
-
validateIntegerInRange(
|
|
1643
|
-
setting.value,
|
|
1644
|
-
0,
|
|
1645
|
-
4294967295,
|
|
1646
|
-
`emulation.http2Options.experimentalSettings[${index}].value`
|
|
1647
|
-
);
|
|
1648
|
-
experimentalSettings.push({
|
|
1649
|
-
id: setting.id,
|
|
1650
|
-
value: setting.value
|
|
1651
|
-
});
|
|
1652
|
-
}
|
|
1653
|
-
normalized.experimentalSettings = experimentalSettings;
|
|
1654
|
-
}
|
|
1655
|
-
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1150
|
+
if (options === void 0) return;
|
|
1151
|
+
const normalized = {};
|
|
1152
|
+
if (options.adaptiveWindow !== void 0) normalized.adaptiveWindow = options.adaptiveWindow;
|
|
1153
|
+
if (options.keepAliveWhileIdle !== void 0) normalized.keepAliveWhileIdle = options.keepAliveWhileIdle;
|
|
1154
|
+
if (options.enablePush !== void 0) normalized.enablePush = options.enablePush;
|
|
1155
|
+
if (options.enableConnectProtocol !== void 0) normalized.enableConnectProtocol = options.enableConnectProtocol;
|
|
1156
|
+
if (options.noRfc7540Priorities !== void 0) normalized.noRfc7540Priorities = options.noRfc7540Priorities;
|
|
1157
|
+
if (options.initialStreamId !== void 0) {
|
|
1158
|
+
validateNonNegativeInteger(options.initialStreamId, "emulation.http2Options.initialStreamId");
|
|
1159
|
+
normalized.initialStreamId = options.initialStreamId;
|
|
1160
|
+
}
|
|
1161
|
+
if (options.initialConnectionWindowSize !== void 0) {
|
|
1162
|
+
validateNonNegativeInteger(options.initialConnectionWindowSize, "emulation.http2Options.initialConnectionWindowSize");
|
|
1163
|
+
normalized.initialConnectionWindowSize = options.initialConnectionWindowSize;
|
|
1164
|
+
}
|
|
1165
|
+
if (options.initialWindowSize !== void 0) {
|
|
1166
|
+
validateNonNegativeInteger(options.initialWindowSize, "emulation.http2Options.initialWindowSize");
|
|
1167
|
+
normalized.initialWindowSize = options.initialWindowSize;
|
|
1168
|
+
}
|
|
1169
|
+
if (options.initialMaxSendStreams !== void 0) {
|
|
1170
|
+
validateNonNegativeInteger(options.initialMaxSendStreams, "emulation.http2Options.initialMaxSendStreams");
|
|
1171
|
+
normalized.initialMaxSendStreams = options.initialMaxSendStreams;
|
|
1172
|
+
}
|
|
1173
|
+
if (options.maxFrameSize !== void 0) {
|
|
1174
|
+
validateNonNegativeInteger(options.maxFrameSize, "emulation.http2Options.maxFrameSize");
|
|
1175
|
+
normalized.maxFrameSize = options.maxFrameSize;
|
|
1176
|
+
}
|
|
1177
|
+
if (options.keepAliveInterval !== void 0) {
|
|
1178
|
+
validateNonNegativeInteger(options.keepAliveInterval, "emulation.http2Options.keepAliveInterval");
|
|
1179
|
+
normalized.keepAliveInterval = options.keepAliveInterval;
|
|
1180
|
+
}
|
|
1181
|
+
if (options.keepAliveTimeout !== void 0) {
|
|
1182
|
+
validateNonNegativeInteger(options.keepAliveTimeout, "emulation.http2Options.keepAliveTimeout");
|
|
1183
|
+
normalized.keepAliveTimeout = options.keepAliveTimeout;
|
|
1184
|
+
}
|
|
1185
|
+
if (options.maxConcurrentResetStreams !== void 0) {
|
|
1186
|
+
validateNonNegativeInteger(options.maxConcurrentResetStreams, "emulation.http2Options.maxConcurrentResetStreams");
|
|
1187
|
+
normalized.maxConcurrentResetStreams = options.maxConcurrentResetStreams;
|
|
1188
|
+
}
|
|
1189
|
+
if (options.maxSendBufferSize !== void 0) {
|
|
1190
|
+
validateNonNegativeInteger(options.maxSendBufferSize, "emulation.http2Options.maxSendBufferSize");
|
|
1191
|
+
normalized.maxSendBufferSize = options.maxSendBufferSize;
|
|
1192
|
+
}
|
|
1193
|
+
if (options.maxConcurrentStreams !== void 0) {
|
|
1194
|
+
validateNonNegativeInteger(options.maxConcurrentStreams, "emulation.http2Options.maxConcurrentStreams");
|
|
1195
|
+
normalized.maxConcurrentStreams = options.maxConcurrentStreams;
|
|
1196
|
+
}
|
|
1197
|
+
if (options.maxHeaderListSize !== void 0) {
|
|
1198
|
+
validateNonNegativeInteger(options.maxHeaderListSize, "emulation.http2Options.maxHeaderListSize");
|
|
1199
|
+
normalized.maxHeaderListSize = options.maxHeaderListSize;
|
|
1200
|
+
}
|
|
1201
|
+
if (options.maxPendingAcceptResetStreams !== void 0) {
|
|
1202
|
+
validateNonNegativeInteger(options.maxPendingAcceptResetStreams, "emulation.http2Options.maxPendingAcceptResetStreams");
|
|
1203
|
+
normalized.maxPendingAcceptResetStreams = options.maxPendingAcceptResetStreams;
|
|
1204
|
+
}
|
|
1205
|
+
if (options.headerTableSize !== void 0) {
|
|
1206
|
+
validateNonNegativeInteger(options.headerTableSize, "emulation.http2Options.headerTableSize");
|
|
1207
|
+
normalized.headerTableSize = options.headerTableSize;
|
|
1208
|
+
}
|
|
1209
|
+
if (options.settingsOrder !== void 0) {
|
|
1210
|
+
if (!Array.isArray(options.settingsOrder)) throw new RequestError("emulation.http2Options.settingsOrder must be an array");
|
|
1211
|
+
const settingsOrder = [];
|
|
1212
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1213
|
+
for (const settingId of options.settingsOrder) {
|
|
1214
|
+
if (!HTTP2_SETTING_IDS.has(settingId)) throw new RequestError("emulation.http2Options.settingsOrder contains an unsupported setting id");
|
|
1215
|
+
if (seen.has(settingId)) throw new RequestError(`Duplicate emulation.http2Options.settingsOrder entry: ${settingId}`);
|
|
1216
|
+
seen.add(settingId);
|
|
1217
|
+
settingsOrder.push(settingId);
|
|
1218
|
+
}
|
|
1219
|
+
normalized.settingsOrder = settingsOrder;
|
|
1220
|
+
}
|
|
1221
|
+
if (options.headersPseudoOrder !== void 0) {
|
|
1222
|
+
if (!Array.isArray(options.headersPseudoOrder)) throw new RequestError("emulation.http2Options.headersPseudoOrder must be an array");
|
|
1223
|
+
const headersPseudoOrder = [];
|
|
1224
|
+
const seenPseudo = /* @__PURE__ */ new Set();
|
|
1225
|
+
for (const pseudoId of options.headersPseudoOrder) {
|
|
1226
|
+
if (!HTTP2_PSEUDO_HEADER_IDS.has(pseudoId)) throw new RequestError("emulation.http2Options.headersPseudoOrder contains an unsupported pseudo-header id");
|
|
1227
|
+
if (seenPseudo.has(pseudoId)) throw new RequestError(`Duplicate emulation.http2Options.headersPseudoOrder entry: ${pseudoId}`);
|
|
1228
|
+
seenPseudo.add(pseudoId);
|
|
1229
|
+
headersPseudoOrder.push(pseudoId);
|
|
1230
|
+
}
|
|
1231
|
+
normalized.headersPseudoOrder = headersPseudoOrder;
|
|
1232
|
+
}
|
|
1233
|
+
if (options.headersStreamDependency !== void 0) normalized.headersStreamDependency = normalizeHttp2StreamDependency(options.headersStreamDependency, "emulation.http2Options.headersStreamDependency");
|
|
1234
|
+
if (options.priorities !== void 0) {
|
|
1235
|
+
if (!Array.isArray(options.priorities)) throw new RequestError("emulation.http2Options.priorities must be an array");
|
|
1236
|
+
const priorities = [];
|
|
1237
|
+
const seenStreamIds = /* @__PURE__ */ new Set();
|
|
1238
|
+
for (const [index, priority] of options.priorities.entries()) {
|
|
1239
|
+
if (!isPlainObject(priority)) throw new RequestError(`emulation.http2Options.priorities[${index}] must be an object`);
|
|
1240
|
+
validatePositiveInteger(priority.streamId, `emulation.http2Options.priorities[${index}].streamId`);
|
|
1241
|
+
if (seenStreamIds.has(priority.streamId)) throw new RequestError(`Duplicate emulation.http2Options.priorities streamId: ${priority.streamId}`);
|
|
1242
|
+
seenStreamIds.add(priority.streamId);
|
|
1243
|
+
priorities.push({
|
|
1244
|
+
streamId: priority.streamId,
|
|
1245
|
+
dependency: normalizeHttp2StreamDependency(priority.dependency, `emulation.http2Options.priorities[${index}].dependency`)
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
normalized.priorities = priorities;
|
|
1249
|
+
}
|
|
1250
|
+
if (options.experimentalSettings !== void 0) {
|
|
1251
|
+
if (!Array.isArray(options.experimentalSettings)) throw new RequestError("emulation.http2Options.experimentalSettings must be an array");
|
|
1252
|
+
const experimentalSettings = [];
|
|
1253
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
1254
|
+
for (const [index, setting] of options.experimentalSettings.entries()) {
|
|
1255
|
+
if (!isPlainObject(setting)) throw new RequestError(`emulation.http2Options.experimentalSettings[${index}] must be an object`);
|
|
1256
|
+
validateIntegerInRange(setting.id, 1, MAX_HTTP2_EXPERIMENTAL_SETTING_ID, `emulation.http2Options.experimentalSettings[${index}].id`);
|
|
1257
|
+
if (STANDARD_HTTP2_SETTING_ID_VALUES.has(setting.id)) throw new RequestError(`emulation.http2Options.experimentalSettings[${index}].id must not be a standard HTTP/2 setting id`);
|
|
1258
|
+
if (seenIds.has(setting.id)) throw new RequestError(`Duplicate emulation.http2Options.experimentalSettings id: ${setting.id}`);
|
|
1259
|
+
seenIds.add(setting.id);
|
|
1260
|
+
validateIntegerInRange(setting.value, 0, 4294967295, `emulation.http2Options.experimentalSettings[${index}].value`);
|
|
1261
|
+
experimentalSettings.push({
|
|
1262
|
+
id: setting.id,
|
|
1263
|
+
value: setting.value
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
normalized.experimentalSettings = experimentalSettings;
|
|
1267
|
+
}
|
|
1268
|
+
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1656
1269
|
}
|
|
1657
1270
|
function normalizeCustomEmulationOptions(emulation, allowEmpty) {
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
normalized.http2Options = http2Options;
|
|
1677
|
-
}
|
|
1678
|
-
if (source.headers !== void 0) {
|
|
1679
|
-
const headers = headersToTuples(source.headers);
|
|
1680
|
-
if (headers.length > 0) {
|
|
1681
|
-
normalized.headers = headers;
|
|
1682
|
-
}
|
|
1683
|
-
}
|
|
1684
|
-
const origHeaders = normalizeOrigHeaders(source.origHeaders);
|
|
1685
|
-
if (origHeaders !== void 0) {
|
|
1686
|
-
normalized.origHeaders = origHeaders;
|
|
1687
|
-
}
|
|
1688
|
-
if (!allowEmpty && !isNonEmpty(normalized)) {
|
|
1689
|
-
throw new RequestError(
|
|
1690
|
-
"Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders"
|
|
1691
|
-
);
|
|
1692
|
-
}
|
|
1693
|
-
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1271
|
+
if (emulation === void 0) return;
|
|
1272
|
+
if (!isPlainObject(emulation)) throw new RequestError("emulation must be an object");
|
|
1273
|
+
const source = emulation;
|
|
1274
|
+
const normalized = {};
|
|
1275
|
+
const tlsOptions = normalizeCustomTlsOptions(source.tlsOptions);
|
|
1276
|
+
if (tlsOptions !== void 0) normalized.tlsOptions = tlsOptions;
|
|
1277
|
+
const http1Options = normalizeCustomHttp1Options(source.http1Options);
|
|
1278
|
+
if (http1Options !== void 0) normalized.http1Options = http1Options;
|
|
1279
|
+
const http2Options = normalizeCustomHttp2Options(source.http2Options);
|
|
1280
|
+
if (http2Options !== void 0) normalized.http2Options = http2Options;
|
|
1281
|
+
if (source.headers !== void 0) {
|
|
1282
|
+
const headers = headersToTuples(source.headers);
|
|
1283
|
+
if (headers.length > 0) normalized.headers = headers;
|
|
1284
|
+
}
|
|
1285
|
+
const origHeaders = normalizeOrigHeaders(source.origHeaders);
|
|
1286
|
+
if (origHeaders !== void 0) normalized.origHeaders = origHeaders;
|
|
1287
|
+
if (!allowEmpty && !isNonEmpty(normalized)) throw new RequestError("Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders");
|
|
1288
|
+
return isNonEmpty(normalized) ? normalized : void 0;
|
|
1694
1289
|
}
|
|
1695
1290
|
function serializeCustomEmulationOptions(emulation, allowEmpty) {
|
|
1696
|
-
|
|
1697
|
-
|
|
1291
|
+
const normalized = normalizeCustomEmulationOptions(emulation, allowEmpty);
|
|
1292
|
+
return normalized ? JSON.stringify(normalized) : void 0;
|
|
1698
1293
|
}
|
|
1699
1294
|
function resolveEmulationMode(browser, os, emulation) {
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
browser: DEFAULT_BROWSER,
|
|
1735
|
-
os: DEFAULT_OS
|
|
1736
|
-
};
|
|
1295
|
+
if (browser !== void 0) {
|
|
1296
|
+
validateBrowserProfile(browser);
|
|
1297
|
+
if (os !== void 0) validateOperatingSystem(os);
|
|
1298
|
+
const emulationJson = serializeCustomEmulationOptions(emulation, true);
|
|
1299
|
+
return {
|
|
1300
|
+
kind: "preset",
|
|
1301
|
+
browser,
|
|
1302
|
+
os: os ?? DEFAULT_OS,
|
|
1303
|
+
...emulationJson !== void 0 && { emulationJson }
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
if (os !== void 0) {
|
|
1307
|
+
validateOperatingSystem(os);
|
|
1308
|
+
const emulationJson = serializeCustomEmulationOptions(emulation, true);
|
|
1309
|
+
return {
|
|
1310
|
+
kind: "preset",
|
|
1311
|
+
browser: DEFAULT_BROWSER,
|
|
1312
|
+
os,
|
|
1313
|
+
...emulationJson !== void 0 && { emulationJson }
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
if (emulation !== void 0) {
|
|
1317
|
+
const emulationJson = serializeCustomEmulationOptions(emulation, false);
|
|
1318
|
+
if (emulationJson === void 0) throw new RequestError("Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders");
|
|
1319
|
+
return {
|
|
1320
|
+
kind: "custom",
|
|
1321
|
+
emulationJson
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
return {
|
|
1325
|
+
kind: "preset",
|
|
1326
|
+
browser: DEFAULT_BROWSER,
|
|
1327
|
+
os: DEFAULT_OS
|
|
1328
|
+
};
|
|
1737
1329
|
}
|
|
1738
1330
|
function applyNativeEmulationMode(target, mode) {
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
target.emulationJson = mode.emulationJson;
|
|
1747
|
-
}
|
|
1331
|
+
if (mode.kind === "custom") {
|
|
1332
|
+
target.emulationJson = mode.emulationJson;
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
target.browser = mode.browser;
|
|
1336
|
+
target.os = mode.os;
|
|
1337
|
+
if (mode.emulationJson !== void 0) target.emulationJson = mode.emulationJson;
|
|
1748
1338
|
}
|
|
1749
1339
|
async function dispatchRequest(options, requestUrl, signal) {
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1340
|
+
if (!signal) {
|
|
1341
|
+
const requestId = generateRequestId();
|
|
1342
|
+
let payload;
|
|
1343
|
+
try {
|
|
1344
|
+
payload = await nativeBinding.request(options, requestId, false);
|
|
1345
|
+
} catch (error) {
|
|
1346
|
+
if (error instanceof RequestError) throw error;
|
|
1347
|
+
throw new RequestError(String(error));
|
|
1348
|
+
}
|
|
1349
|
+
return new Response(payload, requestUrl);
|
|
1350
|
+
}
|
|
1351
|
+
const requestId = generateRequestId();
|
|
1352
|
+
const cancelNative = () => {
|
|
1353
|
+
try {
|
|
1354
|
+
nativeBinding.cancelRequest(requestId);
|
|
1355
|
+
} catch {}
|
|
1356
|
+
};
|
|
1357
|
+
const abortHandler = setupAbort(signal, cancelNative);
|
|
1358
|
+
const pending = Promise.race([nativeBinding.request(options, requestId, true), abortHandler.promise]);
|
|
1359
|
+
let payload;
|
|
1360
|
+
try {
|
|
1361
|
+
payload = await pending;
|
|
1362
|
+
} catch (error) {
|
|
1363
|
+
if (isAbortError(error)) throw error;
|
|
1364
|
+
if (error instanceof RequestError) throw error;
|
|
1365
|
+
throw new RequestError(String(error));
|
|
1366
|
+
} finally {
|
|
1367
|
+
abortHandler.cleanup();
|
|
1368
|
+
}
|
|
1369
|
+
return new Response(payload, requestUrl);
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Fetch-compatible entry point that adds browser impersonation controls.
|
|
1373
|
+
*
|
|
1374
|
+
* **Important:** The default fetch path is isolated and non-persistent by design.
|
|
1375
|
+
* Each call uses an isolated request context, so cookies are not shared across calls.
|
|
1376
|
+
* Connection and TLS reuse behavior is handled by the native layer.
|
|
1377
|
+
*
|
|
1378
|
+
* **Use {@link createSession} or {@link withSession} if you need:**
|
|
1379
|
+
* - Cookie persistence across requests
|
|
1380
|
+
* - Shared session defaults across requests
|
|
1381
|
+
* - A single session context for multi-step flows
|
|
1382
|
+
*
|
|
1383
|
+
* **Concurrency:** The core is unthrottled by design. Callers are expected to implement
|
|
1384
|
+
* their own concurrency control (e.g., p-limit) if needed. Built-in throttling would
|
|
1385
|
+
* reduce performance for high-throughput workloads.
|
|
1386
|
+
*
|
|
1387
|
+
* @param input - Request URL (string or URL) or a Request object
|
|
1388
|
+
* @param init - Fetch-compatible init options
|
|
1389
|
+
*
|
|
1390
|
+
* @example
|
|
1391
|
+
* ```typescript
|
|
1392
|
+
* // Isolated request (no state persistence)
|
|
1393
|
+
* const response = await fetch('https://example.com');
|
|
1394
|
+
*
|
|
1395
|
+
* // For persistent cookies and connection reuse, use a session:
|
|
1396
|
+
* await withSession(async (session) => {
|
|
1397
|
+
* await session.fetch('https://example.com/login', { method: 'POST', body: loginData });
|
|
1398
|
+
* await session.fetch('https://example.com/protected'); // Cookies from login are sent
|
|
1399
|
+
* });
|
|
1400
|
+
* ```
|
|
1401
|
+
*/
|
|
1788
1402
|
async function fetch(input, init) {
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
requestOptions.insecure = transport.insecure;
|
|
1831
|
-
}
|
|
1832
|
-
}
|
|
1833
|
-
requestOptions.timeout = timeout;
|
|
1834
|
-
if (config.redirect !== void 0) {
|
|
1835
|
-
requestOptions.redirect = config.redirect;
|
|
1836
|
-
}
|
|
1837
|
-
if (config.disableDefaultHeaders !== void 0) {
|
|
1838
|
-
requestOptions.disableDefaultHeaders = config.disableDefaultHeaders;
|
|
1839
|
-
}
|
|
1840
|
-
if (config.compress !== void 0) {
|
|
1841
|
-
requestOptions.compress = config.compress;
|
|
1842
|
-
}
|
|
1843
|
-
if (headerTuples && headerTuples.length > 0) {
|
|
1844
|
-
requestOptions.headers = headerTuples;
|
|
1845
|
-
}
|
|
1846
|
-
return dispatchRequest(requestOptions, url, config.signal ?? null);
|
|
1403
|
+
const resolved = await resolveFetchArgs(input, init);
|
|
1404
|
+
const url = resolved.url;
|
|
1405
|
+
const config = resolved.init;
|
|
1406
|
+
const sessionContext = resolveSessionContext(config);
|
|
1407
|
+
const sessionDefaults = sessionContext.defaults;
|
|
1408
|
+
validateRedirectMode(config.redirect);
|
|
1409
|
+
if (config.timeout !== void 0) validateTimeout(config.timeout);
|
|
1410
|
+
validateTrustStore(config.trustStore);
|
|
1411
|
+
const method = ensureMethod(config.method);
|
|
1412
|
+
const serializedBody = await serializeBody(config.body ?? null);
|
|
1413
|
+
const body = serializedBody.body;
|
|
1414
|
+
ensureBodyAllowed(method, body);
|
|
1415
|
+
let headerTuples = mergeHeaderTuples(sessionDefaults?.defaultHeaders, config.headers);
|
|
1416
|
+
if (serializedBody.contentType && !hasHeaderName(headerTuples, "content-type")) {
|
|
1417
|
+
if (!headerTuples) headerTuples = [];
|
|
1418
|
+
headerTuples.push(["Content-Type", serializedBody.contentType]);
|
|
1419
|
+
}
|
|
1420
|
+
const transport = resolveTransportContext(config, sessionDefaults);
|
|
1421
|
+
const timeout = config.timeout ?? sessionDefaults?.timeout ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
1422
|
+
const requestOptions = {
|
|
1423
|
+
url,
|
|
1424
|
+
method,
|
|
1425
|
+
sessionId: sessionContext.sessionId,
|
|
1426
|
+
ephemeral: sessionContext.dropAfterRequest
|
|
1427
|
+
};
|
|
1428
|
+
if (body !== void 0) requestOptions.body = body;
|
|
1429
|
+
if (transport.captureDiagnostics !== void 0) requestOptions.captureDiagnostics = transport.captureDiagnostics;
|
|
1430
|
+
if (config.onRequestEvent !== void 0) requestOptions.onRequestEvent = config.onRequestEvent;
|
|
1431
|
+
if (transport.transportId) requestOptions.transportId = transport.transportId;
|
|
1432
|
+
else {
|
|
1433
|
+
if (transport.mode !== void 0) applyNativeEmulationMode(requestOptions, transport.mode);
|
|
1434
|
+
if (transport.proxy !== void 0) requestOptions.proxy = transport.proxy;
|
|
1435
|
+
if (transport.insecure !== void 0) requestOptions.insecure = transport.insecure;
|
|
1436
|
+
if (transport.trustStore !== void 0) requestOptions.trustStore = transport.trustStore;
|
|
1437
|
+
}
|
|
1438
|
+
requestOptions.timeout = timeout;
|
|
1439
|
+
if (config.redirect !== void 0) requestOptions.redirect = config.redirect;
|
|
1440
|
+
if (config.disableDefaultHeaders !== void 0) requestOptions.disableDefaultHeaders = config.disableDefaultHeaders;
|
|
1441
|
+
if (config.compress !== void 0) requestOptions.compress = config.compress;
|
|
1442
|
+
if (headerTuples && headerTuples.length > 0) requestOptions.headers = headerTuples;
|
|
1443
|
+
return dispatchRequest(requestOptions, url, config.signal ?? null);
|
|
1847
1444
|
}
|
|
1848
1445
|
async function createTransport(options) {
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
...options?.readTimeout !== void 0 && { readTimeout: options.readTimeout }
|
|
1874
|
-
};
|
|
1875
|
-
applyNativeEmulationMode(transportOptions, mode);
|
|
1876
|
-
const id = nativeBinding.createTransport(transportOptions);
|
|
1877
|
-
return new Transport(id);
|
|
1878
|
-
} catch (error) {
|
|
1879
|
-
throw new RequestError(String(error));
|
|
1880
|
-
}
|
|
1446
|
+
const mode = resolveEmulationMode(options?.browser, options?.os, options?.emulation);
|
|
1447
|
+
validateTrustStore(options?.trustStore);
|
|
1448
|
+
if (options?.poolIdleTimeout !== void 0) validatePositiveNumber(options.poolIdleTimeout, "poolIdleTimeout");
|
|
1449
|
+
if (options?.poolMaxIdlePerHost !== void 0) validateNonNegativeInteger(options.poolMaxIdlePerHost, "poolMaxIdlePerHost");
|
|
1450
|
+
if (options?.poolMaxSize !== void 0) validatePositiveInteger(options.poolMaxSize, "poolMaxSize");
|
|
1451
|
+
if (options?.connectTimeout !== void 0) validatePositiveNumber(options.connectTimeout, "connectTimeout");
|
|
1452
|
+
if (options?.readTimeout !== void 0) validatePositiveNumber(options.readTimeout, "readTimeout");
|
|
1453
|
+
try {
|
|
1454
|
+
const transportOptions = {
|
|
1455
|
+
...options?.proxy !== void 0 && { proxy: options.proxy },
|
|
1456
|
+
...options?.insecure !== void 0 && { insecure: options.insecure },
|
|
1457
|
+
trustStore: options?.trustStore ?? DEFAULT_TRUST_STORE,
|
|
1458
|
+
...options?.poolIdleTimeout !== void 0 && { poolIdleTimeout: options.poolIdleTimeout },
|
|
1459
|
+
...options?.poolMaxIdlePerHost !== void 0 && { poolMaxIdlePerHost: options.poolMaxIdlePerHost },
|
|
1460
|
+
...options?.poolMaxSize !== void 0 && { poolMaxSize: options.poolMaxSize },
|
|
1461
|
+
...options?.connectTimeout !== void 0 && { connectTimeout: options.connectTimeout },
|
|
1462
|
+
...options?.readTimeout !== void 0 && { readTimeout: options.readTimeout },
|
|
1463
|
+
...options?.captureDiagnostics !== void 0 && { captureDiagnostics: options.captureDiagnostics }
|
|
1464
|
+
};
|
|
1465
|
+
applyNativeEmulationMode(transportOptions, mode);
|
|
1466
|
+
return new Transport(nativeBinding.createTransport(transportOptions));
|
|
1467
|
+
} catch (error) {
|
|
1468
|
+
throw new RequestError(String(error));
|
|
1469
|
+
}
|
|
1881
1470
|
}
|
|
1882
1471
|
async function createSession(options) {
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
return new Session(createdId, defaults);
|
|
1472
|
+
const { sessionId, defaults } = normalizeSessionOptions(options);
|
|
1473
|
+
let createdId;
|
|
1474
|
+
let transportId;
|
|
1475
|
+
try {
|
|
1476
|
+
const transportOptions = {
|
|
1477
|
+
...defaults.proxy !== void 0 && { proxy: defaults.proxy },
|
|
1478
|
+
...defaults.insecure !== void 0 && { insecure: defaults.insecure },
|
|
1479
|
+
trustStore: defaults.trustStore ?? DEFAULT_TRUST_STORE,
|
|
1480
|
+
...defaults.captureDiagnostics !== void 0 && { captureDiagnostics: defaults.captureDiagnostics }
|
|
1481
|
+
};
|
|
1482
|
+
applyNativeEmulationMode(transportOptions, defaults.transportMode);
|
|
1483
|
+
transportId = nativeBinding.createTransport(transportOptions);
|
|
1484
|
+
} catch (error) {
|
|
1485
|
+
throw new RequestError(String(error));
|
|
1486
|
+
}
|
|
1487
|
+
try {
|
|
1488
|
+
createdId = nativeBinding.createSession({ sessionId });
|
|
1489
|
+
} catch (error) {
|
|
1490
|
+
try {
|
|
1491
|
+
nativeBinding.dropTransport(transportId);
|
|
1492
|
+
} catch {}
|
|
1493
|
+
throw new RequestError(String(error));
|
|
1494
|
+
}
|
|
1495
|
+
defaults.transportId = transportId;
|
|
1496
|
+
defaults.ownsTransport = true;
|
|
1497
|
+
return new Session(createdId, defaults);
|
|
1910
1498
|
}
|
|
1911
1499
|
async function withSession(fn, options) {
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
}
|
|
1500
|
+
const session = await createSession(options);
|
|
1501
|
+
try {
|
|
1502
|
+
return await fn(session);
|
|
1503
|
+
} finally {
|
|
1504
|
+
await session.close();
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* @deprecated Use {@link fetch} instead.
|
|
1509
|
+
*/
|
|
1919
1510
|
async function request(options) {
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
if (rest.disableDefaultHeaders !== void 0) {
|
|
1960
|
-
init.disableDefaultHeaders = rest.disableDefaultHeaders;
|
|
1961
|
-
}
|
|
1962
|
-
if (rest.redirect !== void 0) {
|
|
1963
|
-
init.redirect = rest.redirect;
|
|
1964
|
-
}
|
|
1965
|
-
if (legacy.signal !== void 0) {
|
|
1966
|
-
init.signal = legacy.signal;
|
|
1967
|
-
}
|
|
1968
|
-
if (legacy.session !== void 0) {
|
|
1969
|
-
init.session = legacy.session;
|
|
1970
|
-
}
|
|
1971
|
-
if (legacy.cookieMode !== void 0) {
|
|
1972
|
-
init.cookieMode = legacy.cookieMode;
|
|
1973
|
-
} else if (legacy.ephemeral === true) {
|
|
1974
|
-
init.cookieMode = "ephemeral";
|
|
1975
|
-
}
|
|
1976
|
-
return fetch(url, init);
|
|
1977
|
-
}
|
|
1511
|
+
if (!options.url) throw new RequestError("URL is required");
|
|
1512
|
+
const { url, ...rest } = options;
|
|
1513
|
+
const init = {};
|
|
1514
|
+
const legacy = rest;
|
|
1515
|
+
if (rest.method !== void 0) init.method = rest.method;
|
|
1516
|
+
if (rest.headers !== void 0) init.headers = rest.headers;
|
|
1517
|
+
if (rest.body !== void 0) init.body = rest.body;
|
|
1518
|
+
if (rest.browser !== void 0) init.browser = rest.browser;
|
|
1519
|
+
if (rest.os !== void 0) init.os = rest.os;
|
|
1520
|
+
if (rest.emulation !== void 0) init.emulation = rest.emulation;
|
|
1521
|
+
if (rest.proxy !== void 0) init.proxy = rest.proxy;
|
|
1522
|
+
if (rest.timeout !== void 0) init.timeout = rest.timeout;
|
|
1523
|
+
if (rest.sessionId !== void 0) init.sessionId = rest.sessionId;
|
|
1524
|
+
if (rest.transport !== void 0) init.transport = rest.transport;
|
|
1525
|
+
if (rest.insecure !== void 0) init.insecure = rest.insecure;
|
|
1526
|
+
if (rest.trustStore !== void 0) init.trustStore = rest.trustStore;
|
|
1527
|
+
if (rest.disableDefaultHeaders !== void 0) init.disableDefaultHeaders = rest.disableDefaultHeaders;
|
|
1528
|
+
if (rest.redirect !== void 0) init.redirect = rest.redirect;
|
|
1529
|
+
if (legacy.signal !== void 0) init.signal = legacy.signal;
|
|
1530
|
+
if (legacy.session !== void 0) init.session = legacy.session;
|
|
1531
|
+
if (legacy.cookieMode !== void 0) init.cookieMode = legacy.cookieMode;
|
|
1532
|
+
else if (legacy.ephemeral === true) init.cookieMode = "ephemeral";
|
|
1533
|
+
if (legacy.onRequestEvent !== void 0) init.onRequestEvent = legacy.onRequestEvent;
|
|
1534
|
+
if (legacy.captureDiagnostics !== void 0) init.captureDiagnostics = legacy.captureDiagnostics;
|
|
1535
|
+
return fetch(url, init);
|
|
1536
|
+
}
|
|
1537
|
+
/**
|
|
1538
|
+
* Get list of available browser profiles
|
|
1539
|
+
*
|
|
1540
|
+
* @returns Array of browser profile names
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* ```typescript
|
|
1544
|
+
* import { getProfiles } from 'wreq-js';
|
|
1545
|
+
*
|
|
1546
|
+
* const profiles = getProfiles();
|
|
1547
|
+
* console.log(profiles); // ['chrome_131', 'chrome_142', 'firefox_135', 'safari_18', ...]
|
|
1548
|
+
* ```
|
|
1549
|
+
*/
|
|
1978
1550
|
function getProfiles() {
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
}
|
|
1982
|
-
return cachedProfiles;
|
|
1551
|
+
if (!cachedProfiles) cachedProfiles = nativeBinding.getProfiles();
|
|
1552
|
+
return cachedProfiles;
|
|
1983
1553
|
}
|
|
1984
1554
|
function getProfileSet() {
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1555
|
+
if (!cachedProfileSet) cachedProfileSet = new Set(getProfiles());
|
|
1556
|
+
return cachedProfileSet;
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Get list of supported operating systems for emulation.
|
|
1560
|
+
*
|
|
1561
|
+
* @returns Array of operating system identifiers
|
|
1562
|
+
*/
|
|
1990
1563
|
function getOperatingSystems() {
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1564
|
+
if (!cachedOperatingSystems) {
|
|
1565
|
+
const fromNative = nativeBinding.getOperatingSystems?.();
|
|
1566
|
+
cachedOperatingSystems = fromNative && fromNative.length > 0 ? fromNative : [...SUPPORTED_OSES];
|
|
1567
|
+
}
|
|
1568
|
+
return cachedOperatingSystems;
|
|
1996
1569
|
}
|
|
1997
1570
|
function getOperatingSystemSet() {
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
}
|
|
2001
|
-
return cachedOperatingSystemSet;
|
|
1571
|
+
if (!cachedOperatingSystemSet) cachedOperatingSystemSet = new Set(getOperatingSystems());
|
|
1572
|
+
return cachedOperatingSystemSet;
|
|
2002
1573
|
}
|
|
1574
|
+
/**
|
|
1575
|
+
* Convenience helper for GET requests using {@link fetch}.
|
|
1576
|
+
*/
|
|
2003
1577
|
async function get(url, init) {
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
}
|
|
1578
|
+
const config = {};
|
|
1579
|
+
if (init) Object.assign(config, init);
|
|
1580
|
+
config.method = "GET";
|
|
1581
|
+
return fetch(url, config);
|
|
1582
|
+
}
|
|
1583
|
+
/**
|
|
1584
|
+
* Convenience helper for POST requests using {@link fetch}.
|
|
1585
|
+
*/
|
|
2011
1586
|
async function post(url, body, init) {
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
if (body !== void 0) {
|
|
2018
|
-
config.body = body;
|
|
2019
|
-
}
|
|
2020
|
-
return fetch(url, config);
|
|
1587
|
+
const config = {};
|
|
1588
|
+
if (init) Object.assign(config, init);
|
|
1589
|
+
config.method = "POST";
|
|
1590
|
+
if (body !== void 0) config.body = body;
|
|
1591
|
+
return fetch(url, config);
|
|
2021
1592
|
}
|
|
2022
1593
|
function normalizeWebSocketUrl(url) {
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
if (parsed.protocol === "http:") {
|
|
2037
|
-
parsed.protocol = "ws:";
|
|
2038
|
-
} else if (parsed.protocol === "https:") {
|
|
2039
|
-
parsed.protocol = "wss:";
|
|
2040
|
-
}
|
|
2041
|
-
if (parsed.protocol !== "ws:" && parsed.protocol !== "wss:") {
|
|
2042
|
-
throw new RequestError("expected a ws: or wss: url");
|
|
2043
|
-
}
|
|
2044
|
-
return parsed.toString();
|
|
1594
|
+
const normalized = String(url).trim();
|
|
1595
|
+
if (!normalized) throw new RequestError("URL is required");
|
|
1596
|
+
let parsed;
|
|
1597
|
+
try {
|
|
1598
|
+
parsed = new URL(normalized);
|
|
1599
|
+
} catch (error) {
|
|
1600
|
+
throw new RequestError(String(error));
|
|
1601
|
+
}
|
|
1602
|
+
if (parsed.hash) throw new RequestError("WebSocket URL must not include a hash fragment");
|
|
1603
|
+
if (parsed.protocol === "http:") parsed.protocol = "ws:";
|
|
1604
|
+
else if (parsed.protocol === "https:") parsed.protocol = "wss:";
|
|
1605
|
+
if (parsed.protocol !== "ws:" && parsed.protocol !== "wss:") throw new RequestError("expected a ws: or wss: url");
|
|
1606
|
+
return parsed.toString();
|
|
2045
1607
|
}
|
|
2046
1608
|
function validateWebSocketProtocols(protocols) {
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
}
|
|
2063
|
-
seen.add(protocol);
|
|
2064
|
-
}
|
|
1609
|
+
if (protocols === void 0) return;
|
|
1610
|
+
const protocolList = typeof protocols === "string" ? [protocols] : protocols;
|
|
1611
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1612
|
+
const validToken = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
1613
|
+
for (const protocol of protocolList) {
|
|
1614
|
+
if (typeof protocol !== "string" || protocol.length === 0) throw new RequestError("WebSocket protocol values must be non-empty strings");
|
|
1615
|
+
if (!validToken.test(protocol)) throw new RequestError(`Invalid WebSocket protocol value: ${protocol}`);
|
|
1616
|
+
if (seen.has(protocol)) throw new RequestError(`Duplicate WebSocket protocol: ${protocol}`);
|
|
1617
|
+
seen.add(protocol);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
function normalizeWebSocketSizeOption(value, label) {
|
|
1621
|
+
if (value === void 0) return;
|
|
1622
|
+
if (!Number.isSafeInteger(value) || value <= 0) throw new RequestError(`${label} must be a positive safe integer`);
|
|
1623
|
+
return value;
|
|
2065
1624
|
}
|
|
2066
1625
|
function normalizeStandaloneWebSocketOptions(options) {
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
}
|
|
2089
|
-
if (options.binaryType !== void 0) {
|
|
2090
|
-
if (options.binaryType !== "nodebuffer" && options.binaryType !== "arraybuffer" && options.binaryType !== "blob") {
|
|
2091
|
-
throw new RequestError("binaryType must be one of: 'nodebuffer', 'arraybuffer', 'blob'");
|
|
2092
|
-
}
|
|
2093
|
-
normalized.binaryType = options.binaryType;
|
|
2094
|
-
}
|
|
2095
|
-
return normalized;
|
|
1626
|
+
const normalized = {};
|
|
1627
|
+
if (!options) return normalized;
|
|
1628
|
+
if (options.browser !== void 0) normalized.browser = options.browser;
|
|
1629
|
+
if (options.os !== void 0) normalized.os = options.os;
|
|
1630
|
+
if (options.emulation !== void 0) normalized.emulation = options.emulation;
|
|
1631
|
+
if (options.headers !== void 0) normalized.headers = options.headers;
|
|
1632
|
+
if (options.proxy !== void 0) normalized.proxy = options.proxy;
|
|
1633
|
+
if (options.protocols !== void 0) normalized.protocols = options.protocols;
|
|
1634
|
+
if (options.maxFrameSize !== void 0) {
|
|
1635
|
+
const maxFrameSize = normalizeWebSocketSizeOption(options.maxFrameSize, "maxFrameSize");
|
|
1636
|
+
if (maxFrameSize !== void 0) normalized.maxFrameSize = maxFrameSize;
|
|
1637
|
+
}
|
|
1638
|
+
if (options.maxMessageSize !== void 0) {
|
|
1639
|
+
const maxMessageSize = normalizeWebSocketSizeOption(options.maxMessageSize, "maxMessageSize");
|
|
1640
|
+
if (maxMessageSize !== void 0) normalized.maxMessageSize = maxMessageSize;
|
|
1641
|
+
}
|
|
1642
|
+
if (options.binaryType !== void 0) {
|
|
1643
|
+
if (options.binaryType !== "nodebuffer" && options.binaryType !== "arraybuffer" && options.binaryType !== "blob") throw new RequestError("binaryType must be one of: 'nodebuffer', 'arraybuffer', 'blob'");
|
|
1644
|
+
normalized.binaryType = options.binaryType;
|
|
1645
|
+
}
|
|
1646
|
+
return normalized;
|
|
2096
1647
|
}
|
|
2097
1648
|
function normalizeSessionWebSocketOptions(options) {
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
normalized.headers = options.headers;
|
|
2121
|
-
}
|
|
2122
|
-
if (options.protocols !== void 0) {
|
|
2123
|
-
normalized.protocols = options.protocols;
|
|
2124
|
-
}
|
|
2125
|
-
if (options.binaryType !== void 0) {
|
|
2126
|
-
if (options.binaryType !== "nodebuffer" && options.binaryType !== "arraybuffer" && options.binaryType !== "blob") {
|
|
2127
|
-
throw new RequestError("binaryType must be one of: 'nodebuffer', 'arraybuffer', 'blob'");
|
|
2128
|
-
}
|
|
2129
|
-
normalized.binaryType = options.binaryType;
|
|
2130
|
-
}
|
|
2131
|
-
return normalized;
|
|
1649
|
+
const normalized = {};
|
|
1650
|
+
if (!options) return normalized;
|
|
1651
|
+
const optionsWithOverrides = options;
|
|
1652
|
+
if (optionsWithOverrides.browser !== void 0) throw new RequestError("`browser` is not supported in session.websocket(); the session controls browser emulation.");
|
|
1653
|
+
if (optionsWithOverrides.os !== void 0) throw new RequestError("`os` is not supported in session.websocket(); the session controls OS emulation.");
|
|
1654
|
+
if (optionsWithOverrides.emulation !== void 0) throw new RequestError("`emulation` is not supported in session.websocket(); the session transport controls emulation.");
|
|
1655
|
+
if (optionsWithOverrides.proxy !== void 0) throw new RequestError("`proxy` is not supported in session.websocket(); the session transport controls proxying.");
|
|
1656
|
+
if (options.headers !== void 0) normalized.headers = options.headers;
|
|
1657
|
+
if (options.protocols !== void 0) normalized.protocols = options.protocols;
|
|
1658
|
+
if (options.maxFrameSize !== void 0) {
|
|
1659
|
+
const maxFrameSize = normalizeWebSocketSizeOption(options.maxFrameSize, "maxFrameSize");
|
|
1660
|
+
if (maxFrameSize !== void 0) normalized.maxFrameSize = maxFrameSize;
|
|
1661
|
+
}
|
|
1662
|
+
if (options.maxMessageSize !== void 0) {
|
|
1663
|
+
const maxMessageSize = normalizeWebSocketSizeOption(options.maxMessageSize, "maxMessageSize");
|
|
1664
|
+
if (maxMessageSize !== void 0) normalized.maxMessageSize = maxMessageSize;
|
|
1665
|
+
}
|
|
1666
|
+
if (options.binaryType !== void 0) {
|
|
1667
|
+
if (options.binaryType !== "nodebuffer" && options.binaryType !== "arraybuffer" && options.binaryType !== "blob") throw new RequestError("binaryType must be one of: 'nodebuffer', 'arraybuffer', 'blob'");
|
|
1668
|
+
normalized.binaryType = options.binaryType;
|
|
1669
|
+
}
|
|
1670
|
+
return normalized;
|
|
2132
1671
|
}
|
|
2133
1672
|
function extractLegacyWebSocketCallbacks(options) {
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
}
|
|
2142
|
-
if (typeof maybeCallbacks.onClose === "function") {
|
|
2143
|
-
callbacks.onClose = maybeCallbacks.onClose;
|
|
2144
|
-
}
|
|
2145
|
-
if (typeof maybeCallbacks.onError === "function") {
|
|
2146
|
-
callbacks.onError = maybeCallbacks.onError;
|
|
2147
|
-
}
|
|
2148
|
-
return Object.keys(callbacks).length > 0 ? callbacks : void 0;
|
|
1673
|
+
if (!isPlainObject(options)) return;
|
|
1674
|
+
const maybeCallbacks = options;
|
|
1675
|
+
const callbacks = {};
|
|
1676
|
+
if (typeof maybeCallbacks.onMessage === "function") callbacks.onMessage = maybeCallbacks.onMessage;
|
|
1677
|
+
if (typeof maybeCallbacks.onClose === "function") callbacks.onClose = maybeCallbacks.onClose;
|
|
1678
|
+
if (typeof maybeCallbacks.onError === "function") callbacks.onError = maybeCallbacks.onError;
|
|
1679
|
+
return Object.keys(callbacks).length > 0 ? callbacks : void 0;
|
|
2149
1680
|
}
|
|
2150
1681
|
function normalizeWebSocketCloseOptions(code, reason) {
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
throw new RequestError("Close code must be 1000 or in range 3000-4999");
|
|
2162
|
-
}
|
|
2163
|
-
const normalizedReason = reason ?? "";
|
|
2164
|
-
if (Buffer.byteLength(normalizedReason, "utf8") > 123) {
|
|
2165
|
-
throw new RequestError("Close reason must be 123 bytes or fewer");
|
|
2166
|
-
}
|
|
2167
|
-
return {
|
|
2168
|
-
code,
|
|
2169
|
-
reason: normalizedReason
|
|
2170
|
-
};
|
|
1682
|
+
if (code === void 0 && reason === void 0) return;
|
|
1683
|
+
if (code === void 0) throw new RequestError("A close code is required when providing a close reason");
|
|
1684
|
+
if (!Number.isInteger(code)) throw new RequestError("Close code must be an integer");
|
|
1685
|
+
if (code !== 1e3 && (code < 3e3 || code > 4999)) throw new RequestError("Close code must be 1000 or in range 3000-4999");
|
|
1686
|
+
const normalizedReason = reason ?? "";
|
|
1687
|
+
if (Buffer.byteLength(normalizedReason, "utf8") > 123) throw new RequestError("Close reason must be 123 bytes or fewer");
|
|
1688
|
+
return {
|
|
1689
|
+
code,
|
|
1690
|
+
reason: normalizedReason
|
|
1691
|
+
};
|
|
2171
1692
|
}
|
|
2172
1693
|
function isWebSocketListenerType(type) {
|
|
2173
|
-
|
|
2174
|
-
}
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
return;
|
|
2579
|
-
}
|
|
2580
|
-
const listenerMap = this.getListenerMap(type);
|
|
2581
|
-
const descriptor = listenerMap.get(normalizedListener);
|
|
2582
|
-
if (!descriptor) {
|
|
2583
|
-
return;
|
|
2584
|
-
}
|
|
2585
|
-
if (descriptor.abortSignal && descriptor.abortHandler) {
|
|
2586
|
-
descriptor.abortSignal.removeEventListener("abort", descriptor.abortHandler);
|
|
2587
|
-
}
|
|
2588
|
-
listenerMap.delete(normalizedListener);
|
|
2589
|
-
}
|
|
2590
|
-
getSendByteLength(data) {
|
|
2591
|
-
if (typeof data === "string") {
|
|
2592
|
-
return Buffer.byteLength(data);
|
|
2593
|
-
}
|
|
2594
|
-
if (Buffer.isBuffer(data)) {
|
|
2595
|
-
return data.byteLength;
|
|
2596
|
-
}
|
|
2597
|
-
if (data instanceof ArrayBuffer) {
|
|
2598
|
-
return data.byteLength;
|
|
2599
|
-
}
|
|
2600
|
-
if (ArrayBuffer.isView(data)) {
|
|
2601
|
-
return data.byteLength;
|
|
2602
|
-
}
|
|
2603
|
-
if (typeof Blob !== "undefined" && data instanceof Blob) {
|
|
2604
|
-
return data.size;
|
|
2605
|
-
}
|
|
2606
|
-
throw new TypeError("WebSocket data must be a string, Buffer, ArrayBuffer, ArrayBufferView, or Blob");
|
|
2607
|
-
}
|
|
2608
|
-
async normalizeSendPayload(data) {
|
|
2609
|
-
if (typeof data === "string") {
|
|
2610
|
-
return data;
|
|
2611
|
-
}
|
|
2612
|
-
if (Buffer.isBuffer(data)) {
|
|
2613
|
-
return data;
|
|
2614
|
-
}
|
|
2615
|
-
if (data instanceof ArrayBuffer) {
|
|
2616
|
-
return Buffer.from(data);
|
|
2617
|
-
}
|
|
2618
|
-
if (ArrayBuffer.isView(data)) {
|
|
2619
|
-
return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
2620
|
-
}
|
|
2621
|
-
if (typeof Blob !== "undefined" && data instanceof Blob) {
|
|
2622
|
-
return Buffer.from(await data.arrayBuffer());
|
|
2623
|
-
}
|
|
2624
|
-
throw new TypeError("WebSocket data must be a string, Buffer, ArrayBuffer, ArrayBufferView, or Blob");
|
|
2625
|
-
}
|
|
2626
|
-
send(data) {
|
|
2627
|
-
if (this.readyState !== _WebSocket.OPEN || !this._connection) {
|
|
2628
|
-
throw new RequestError("WebSocket is not open");
|
|
2629
|
-
}
|
|
2630
|
-
const queuedBytes = this.getSendByteLength(data);
|
|
2631
|
-
const connection = this._connection;
|
|
2632
|
-
this._bufferedAmount += queuedBytes;
|
|
2633
|
-
const sendTask = async () => {
|
|
2634
|
-
try {
|
|
2635
|
-
const payload = await this.normalizeSendPayload(data);
|
|
2636
|
-
await nativeBinding.websocketSend(connection, payload);
|
|
2637
|
-
} catch (error) {
|
|
2638
|
-
this.handleNativeError(String(error));
|
|
2639
|
-
this.finalizeClosed({ code: 1006, reason: "" }, false);
|
|
2640
|
-
} finally {
|
|
2641
|
-
this._bufferedAmount = Math.max(0, this._bufferedAmount - queuedBytes);
|
|
2642
|
-
}
|
|
2643
|
-
};
|
|
2644
|
-
this._sendChain = this._sendChain.then(sendTask, sendTask);
|
|
2645
|
-
}
|
|
2646
|
-
close(code, reason) {
|
|
2647
|
-
if (this.readyState === _WebSocket.CLOSING || this.readyState === _WebSocket.CLOSED) {
|
|
2648
|
-
return;
|
|
2649
|
-
}
|
|
2650
|
-
this._closeOptions = normalizeWebSocketCloseOptions(code, reason);
|
|
2651
|
-
this.readyState = _WebSocket.CLOSING;
|
|
2652
|
-
this.startNativeClose();
|
|
2653
|
-
}
|
|
1694
|
+
return type === "open" || type === "message" || type === "close" || type === "error";
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* WHATWG-style WebSocket API with async connection establishment.
|
|
1698
|
+
*/
|
|
1699
|
+
var WebSocket = class WebSocket {
|
|
1700
|
+
static CONNECTING = 0;
|
|
1701
|
+
static OPEN = 1;
|
|
1702
|
+
static CLOSING = 2;
|
|
1703
|
+
static CLOSED = 3;
|
|
1704
|
+
url;
|
|
1705
|
+
protocol = "";
|
|
1706
|
+
extensions = "";
|
|
1707
|
+
readyState = WebSocket.CONNECTING;
|
|
1708
|
+
_binaryType = "nodebuffer";
|
|
1709
|
+
_bufferedAmount = 0;
|
|
1710
|
+
_onopen = null;
|
|
1711
|
+
_onmessage = null;
|
|
1712
|
+
_onclose = null;
|
|
1713
|
+
_onerror = null;
|
|
1714
|
+
_onHandlerOrder = {
|
|
1715
|
+
open: -1,
|
|
1716
|
+
message: -1,
|
|
1717
|
+
close: -1,
|
|
1718
|
+
error: -1
|
|
1719
|
+
};
|
|
1720
|
+
_listenerOrderCounter = 0;
|
|
1721
|
+
_listeners = {
|
|
1722
|
+
open: /* @__PURE__ */ new Map(),
|
|
1723
|
+
message: /* @__PURE__ */ new Map(),
|
|
1724
|
+
close: /* @__PURE__ */ new Map(),
|
|
1725
|
+
error: /* @__PURE__ */ new Map()
|
|
1726
|
+
};
|
|
1727
|
+
_legacyCallbacks;
|
|
1728
|
+
_openDispatchMode;
|
|
1729
|
+
_connection;
|
|
1730
|
+
_connectPromise;
|
|
1731
|
+
_closeOptions;
|
|
1732
|
+
_finalizerToken;
|
|
1733
|
+
_openEventDispatched = false;
|
|
1734
|
+
_openEventQueued = false;
|
|
1735
|
+
_closeEventDispatched = false;
|
|
1736
|
+
_nativeCloseStarted = false;
|
|
1737
|
+
_pendingMessages = [];
|
|
1738
|
+
_sendChain = Promise.resolve();
|
|
1739
|
+
constructor(urlOrInit, protocolsOrOptions, maybeOptions) {
|
|
1740
|
+
let init;
|
|
1741
|
+
if (isInternalWebSocketInit(urlOrInit)) init = urlOrInit;
|
|
1742
|
+
else init = WebSocket.buildStandaloneInit(urlOrInit, protocolsOrOptions, maybeOptions);
|
|
1743
|
+
this.url = init.url;
|
|
1744
|
+
this.binaryType = init.options.binaryType ?? "nodebuffer";
|
|
1745
|
+
this._legacyCallbacks = init.legacyCallbacks;
|
|
1746
|
+
this._openDispatchMode = init.openDispatchMode;
|
|
1747
|
+
this._connectPromise = this.connect(init.connect);
|
|
1748
|
+
this._connectPromise.catch(() => void 0);
|
|
1749
|
+
}
|
|
1750
|
+
get binaryType() {
|
|
1751
|
+
return this._binaryType;
|
|
1752
|
+
}
|
|
1753
|
+
set binaryType(value) {
|
|
1754
|
+
if (value === "arraybuffer" || value === "blob" || value === "nodebuffer") this._binaryType = value;
|
|
1755
|
+
}
|
|
1756
|
+
get bufferedAmount() {
|
|
1757
|
+
return this._bufferedAmount;
|
|
1758
|
+
}
|
|
1759
|
+
get onopen() {
|
|
1760
|
+
return this._onopen;
|
|
1761
|
+
}
|
|
1762
|
+
set onopen(listener) {
|
|
1763
|
+
this._onopen = listener;
|
|
1764
|
+
this._onHandlerOrder.open = listener ? ++this._listenerOrderCounter : -1;
|
|
1765
|
+
}
|
|
1766
|
+
get onmessage() {
|
|
1767
|
+
return this._onmessage;
|
|
1768
|
+
}
|
|
1769
|
+
set onmessage(listener) {
|
|
1770
|
+
this._onmessage = listener;
|
|
1771
|
+
this._onHandlerOrder.message = listener ? ++this._listenerOrderCounter : -1;
|
|
1772
|
+
}
|
|
1773
|
+
get onclose() {
|
|
1774
|
+
return this._onclose;
|
|
1775
|
+
}
|
|
1776
|
+
set onclose(listener) {
|
|
1777
|
+
this._onclose = listener;
|
|
1778
|
+
this._onHandlerOrder.close = listener ? ++this._listenerOrderCounter : -1;
|
|
1779
|
+
}
|
|
1780
|
+
get onerror() {
|
|
1781
|
+
return this._onerror;
|
|
1782
|
+
}
|
|
1783
|
+
set onerror(listener) {
|
|
1784
|
+
this._onerror = listener;
|
|
1785
|
+
this._onHandlerOrder.error = listener ? ++this._listenerOrderCounter : -1;
|
|
1786
|
+
}
|
|
1787
|
+
static async _connectWithInit(init) {
|
|
1788
|
+
const ws = new WebSocket(init);
|
|
1789
|
+
await ws._waitUntilConnected();
|
|
1790
|
+
ws.scheduleOpenEventAfterAwait();
|
|
1791
|
+
return ws;
|
|
1792
|
+
}
|
|
1793
|
+
static buildStandaloneInit(url, protocolsOrOptions, maybeOptions) {
|
|
1794
|
+
const optionsCandidate = typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? maybeOptions : protocolsOrOptions ?? maybeOptions;
|
|
1795
|
+
const normalizedOptions = normalizeStandaloneWebSocketOptions(optionsCandidate);
|
|
1796
|
+
validateWebSocketProtocols(typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols);
|
|
1797
|
+
assertNoManualWebSocketProtocolHeader(normalizedOptions.headers);
|
|
1798
|
+
const emulationMode = resolveEmulationMode(normalizedOptions.browser, normalizedOptions.os, normalizedOptions.emulation);
|
|
1799
|
+
const protocols = normalizeWebSocketProtocolList(typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols);
|
|
1800
|
+
return {
|
|
1801
|
+
_internal: true,
|
|
1802
|
+
url: normalizeWebSocketUrl(url),
|
|
1803
|
+
options: normalizedOptions,
|
|
1804
|
+
openDispatchMode: "automatic",
|
|
1805
|
+
connect: (callbacks) => {
|
|
1806
|
+
const nativeOptions = {
|
|
1807
|
+
url: normalizeWebSocketUrl(url),
|
|
1808
|
+
headers: headersToTuples(normalizedOptions.headers ?? {}),
|
|
1809
|
+
...protocols && protocols.length > 0 && { protocols },
|
|
1810
|
+
...normalizedOptions.proxy !== void 0 && { proxy: normalizedOptions.proxy },
|
|
1811
|
+
...normalizedOptions.maxFrameSize !== void 0 && { maxFrameSize: normalizedOptions.maxFrameSize },
|
|
1812
|
+
...normalizedOptions.maxMessageSize !== void 0 && { maxMessageSize: normalizedOptions.maxMessageSize },
|
|
1813
|
+
onMessage: callbacks.onMessage,
|
|
1814
|
+
onClose: callbacks.onClose,
|
|
1815
|
+
onError: callbacks.onError
|
|
1816
|
+
};
|
|
1817
|
+
applyNativeEmulationMode(nativeOptions, emulationMode);
|
|
1818
|
+
return nativeBinding.websocketConnect(nativeOptions);
|
|
1819
|
+
},
|
|
1820
|
+
legacyCallbacks: extractLegacyWebSocketCallbacks(optionsCandidate)
|
|
1821
|
+
};
|
|
1822
|
+
}
|
|
1823
|
+
async connect(connectFn) {
|
|
1824
|
+
try {
|
|
1825
|
+
const connection = await connectFn({
|
|
1826
|
+
onMessage: (data) => {
|
|
1827
|
+
this.handleNativeMessage(data);
|
|
1828
|
+
},
|
|
1829
|
+
onClose: (event) => {
|
|
1830
|
+
this.handleNativeClose(event);
|
|
1831
|
+
},
|
|
1832
|
+
onError: (message) => {
|
|
1833
|
+
this.handleNativeError(message);
|
|
1834
|
+
}
|
|
1835
|
+
});
|
|
1836
|
+
this._connection = connection;
|
|
1837
|
+
this.protocol = connection.protocol ?? "";
|
|
1838
|
+
this.extensions = connection.extensions ?? "";
|
|
1839
|
+
if (websocketFinalizer) {
|
|
1840
|
+
this._finalizerToken = connection;
|
|
1841
|
+
websocketFinalizer.register(this, connection, connection);
|
|
1842
|
+
}
|
|
1843
|
+
if (this.readyState === WebSocket.CLOSING) {
|
|
1844
|
+
this.startNativeClose();
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
this.readyState = WebSocket.OPEN;
|
|
1848
|
+
if (this._openDispatchMode === "automatic") this.scheduleOpenEventAfterConnect();
|
|
1849
|
+
} catch (error) {
|
|
1850
|
+
this.handleNativeError(String(error));
|
|
1851
|
+
this.finalizeClosed({
|
|
1852
|
+
code: 1006,
|
|
1853
|
+
reason: ""
|
|
1854
|
+
}, false);
|
|
1855
|
+
throw new RequestError(String(error));
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
_waitUntilConnected() {
|
|
1859
|
+
return this._connectPromise;
|
|
1860
|
+
}
|
|
1861
|
+
scheduleOpenEventAfterConnect() {
|
|
1862
|
+
this.scheduleOpenEventWithDepth(2);
|
|
1863
|
+
}
|
|
1864
|
+
scheduleOpenEventAfterAwait() {
|
|
1865
|
+
this.scheduleOpenEventWithDepth(3);
|
|
1866
|
+
}
|
|
1867
|
+
scheduleOpenEventWithDepth(depth) {
|
|
1868
|
+
if (this._openEventDispatched || this._openEventQueued || this.readyState !== WebSocket.OPEN) return;
|
|
1869
|
+
this._openEventQueued = true;
|
|
1870
|
+
const queue = (remaining) => {
|
|
1871
|
+
if (remaining === 0) {
|
|
1872
|
+
this._openEventQueued = false;
|
|
1873
|
+
if (this._openEventDispatched || this.readyState !== WebSocket.OPEN) return;
|
|
1874
|
+
this._openEventDispatched = true;
|
|
1875
|
+
this.dispatchOpenEvent();
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
queueMicrotask(() => {
|
|
1879
|
+
queue(remaining - 1);
|
|
1880
|
+
});
|
|
1881
|
+
};
|
|
1882
|
+
queue(depth);
|
|
1883
|
+
}
|
|
1884
|
+
releaseConnectionTracking() {
|
|
1885
|
+
if (!this._finalizerToken || !websocketFinalizer) return;
|
|
1886
|
+
websocketFinalizer.unregister(this._finalizerToken);
|
|
1887
|
+
this._finalizerToken = void 0;
|
|
1888
|
+
}
|
|
1889
|
+
toMessageEventData(data) {
|
|
1890
|
+
if (typeof data === "string") return data;
|
|
1891
|
+
if (this._binaryType === "arraybuffer") {
|
|
1892
|
+
const arrayBuffer = new ArrayBuffer(data.byteLength);
|
|
1893
|
+
new Uint8Array(arrayBuffer).set(data);
|
|
1894
|
+
return arrayBuffer;
|
|
1895
|
+
}
|
|
1896
|
+
if (this._binaryType === "blob") return new Blob([data]);
|
|
1897
|
+
return data;
|
|
1898
|
+
}
|
|
1899
|
+
invokeListener(listener, event) {
|
|
1900
|
+
try {
|
|
1901
|
+
if (typeof listener === "function") listener.call(this, event);
|
|
1902
|
+
else listener.handleEvent(event);
|
|
1903
|
+
} catch {}
|
|
1904
|
+
}
|
|
1905
|
+
createBaseEvent(type) {
|
|
1906
|
+
return {
|
|
1907
|
+
type,
|
|
1908
|
+
isTrusted: false,
|
|
1909
|
+
timeStamp: Date.now(),
|
|
1910
|
+
target: this,
|
|
1911
|
+
currentTarget: this
|
|
1912
|
+
};
|
|
1913
|
+
}
|
|
1914
|
+
getOnHandler(type) {
|
|
1915
|
+
switch (type) {
|
|
1916
|
+
case "open": return this._onopen;
|
|
1917
|
+
case "message": return this._onmessage;
|
|
1918
|
+
case "close": return this._onclose;
|
|
1919
|
+
case "error": return this._onerror;
|
|
1920
|
+
default: return null;
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
getOnHandlerOrder(type) {
|
|
1924
|
+
return this._onHandlerOrder[type];
|
|
1925
|
+
}
|
|
1926
|
+
getListenerMap(type) {
|
|
1927
|
+
return this._listeners[type];
|
|
1928
|
+
}
|
|
1929
|
+
dispatchEvent(type, event) {
|
|
1930
|
+
const listenerMap = this.getListenerMap(type);
|
|
1931
|
+
const onHandler = this.getOnHandler(type);
|
|
1932
|
+
if (listenerMap.size === 0 && !onHandler) return;
|
|
1933
|
+
const ordered = [];
|
|
1934
|
+
for (const descriptor of listenerMap.values()) ordered.push({
|
|
1935
|
+
order: descriptor.order,
|
|
1936
|
+
listener: descriptor.listener,
|
|
1937
|
+
once: descriptor.once
|
|
1938
|
+
});
|
|
1939
|
+
if (onHandler) ordered.push({
|
|
1940
|
+
order: this.getOnHandlerOrder(type),
|
|
1941
|
+
listener: onHandler,
|
|
1942
|
+
once: false
|
|
1943
|
+
});
|
|
1944
|
+
ordered.sort((a, b) => a.order - b.order);
|
|
1945
|
+
for (const entry of ordered) {
|
|
1946
|
+
if (entry.once) this.removeEventListener(type, entry.listener);
|
|
1947
|
+
this.invokeListener(entry.listener, event);
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
dispatchOpenEvent() {
|
|
1951
|
+
const event = this.createBaseEvent("open");
|
|
1952
|
+
this.dispatchEvent("open", event);
|
|
1953
|
+
if (!this._closeEventDispatched && this._pendingMessages.length > 0) {
|
|
1954
|
+
const pending = this._pendingMessages;
|
|
1955
|
+
this._pendingMessages = [];
|
|
1956
|
+
for (const data of pending) this.dispatchMessageEvent(this.toMessageEventData(data));
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
dispatchMessageEvent(data) {
|
|
1960
|
+
const event = {
|
|
1961
|
+
...this.createBaseEvent("message"),
|
|
1962
|
+
data
|
|
1963
|
+
};
|
|
1964
|
+
this.dispatchEvent("message", event);
|
|
1965
|
+
}
|
|
1966
|
+
dispatchCloseEvent(event) {
|
|
1967
|
+
this.dispatchEvent("close", event);
|
|
1968
|
+
}
|
|
1969
|
+
dispatchErrorEvent(message) {
|
|
1970
|
+
const event = {
|
|
1971
|
+
...this.createBaseEvent("error"),
|
|
1972
|
+
...message !== void 0 && { message }
|
|
1973
|
+
};
|
|
1974
|
+
this.dispatchEvent("error", event);
|
|
1975
|
+
}
|
|
1976
|
+
handleNativeMessage(data) {
|
|
1977
|
+
if (this._closeEventDispatched) return;
|
|
1978
|
+
this._legacyCallbacks?.onMessage?.(data);
|
|
1979
|
+
if (!this._openEventDispatched && this.readyState === WebSocket.OPEN) {
|
|
1980
|
+
this._pendingMessages.push(data);
|
|
1981
|
+
return;
|
|
1982
|
+
}
|
|
1983
|
+
this.dispatchMessageEvent(this.toMessageEventData(data));
|
|
1984
|
+
}
|
|
1985
|
+
handleNativeError(message) {
|
|
1986
|
+
this._legacyCallbacks?.onError?.(message);
|
|
1987
|
+
this.dispatchErrorEvent(message);
|
|
1988
|
+
}
|
|
1989
|
+
handleNativeClose(event) {
|
|
1990
|
+
const wasClean = this.readyState === WebSocket.CLOSING || event.code === 1e3;
|
|
1991
|
+
this.finalizeClosed(event, wasClean);
|
|
1992
|
+
}
|
|
1993
|
+
finalizeClosed(event, wasClean) {
|
|
1994
|
+
if (this._closeEventDispatched) return;
|
|
1995
|
+
this.readyState = WebSocket.CLOSED;
|
|
1996
|
+
this._closeEventDispatched = true;
|
|
1997
|
+
this._pendingMessages = [];
|
|
1998
|
+
this.releaseConnectionTracking();
|
|
1999
|
+
const closeEvent = {
|
|
2000
|
+
...this.createBaseEvent("close"),
|
|
2001
|
+
code: event.code,
|
|
2002
|
+
reason: event.reason,
|
|
2003
|
+
wasClean
|
|
2004
|
+
};
|
|
2005
|
+
this._legacyCallbacks?.onClose?.(closeEvent);
|
|
2006
|
+
this.dispatchCloseEvent(closeEvent);
|
|
2007
|
+
}
|
|
2008
|
+
startNativeClose() {
|
|
2009
|
+
if (this._nativeCloseStarted || !this._connection) return;
|
|
2010
|
+
this._nativeCloseStarted = true;
|
|
2011
|
+
const connection = this._connection;
|
|
2012
|
+
const closeOptions = this._closeOptions;
|
|
2013
|
+
nativeBinding.websocketClose(connection, closeOptions).catch((error) => {
|
|
2014
|
+
this.handleNativeError(String(error));
|
|
2015
|
+
this.finalizeClosed({
|
|
2016
|
+
code: 1006,
|
|
2017
|
+
reason: ""
|
|
2018
|
+
}, false);
|
|
2019
|
+
});
|
|
2020
|
+
}
|
|
2021
|
+
addEventListener(type, listener, options) {
|
|
2022
|
+
if (!listener || !isWebSocketListenerType(type)) return;
|
|
2023
|
+
const normalizedListener = listener;
|
|
2024
|
+
if (typeof normalizedListener !== "function" && (typeof normalizedListener !== "object" || normalizedListener === null || typeof normalizedListener.handleEvent !== "function")) return;
|
|
2025
|
+
const listenerMap = this.getListenerMap(type);
|
|
2026
|
+
if (listenerMap.has(normalizedListener)) return;
|
|
2027
|
+
const parsedOptions = typeof options === "boolean" ? {} : options ?? {};
|
|
2028
|
+
const once = parsedOptions.once === true;
|
|
2029
|
+
const signal = parsedOptions.signal;
|
|
2030
|
+
if (signal?.aborted) return;
|
|
2031
|
+
const descriptor = {
|
|
2032
|
+
listener: normalizedListener,
|
|
2033
|
+
order: ++this._listenerOrderCounter,
|
|
2034
|
+
once
|
|
2035
|
+
};
|
|
2036
|
+
if (signal) {
|
|
2037
|
+
const onAbort = () => {
|
|
2038
|
+
this.removeEventListener(type, normalizedListener);
|
|
2039
|
+
};
|
|
2040
|
+
descriptor.abortSignal = signal;
|
|
2041
|
+
descriptor.abortHandler = onAbort;
|
|
2042
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2043
|
+
}
|
|
2044
|
+
listenerMap.set(normalizedListener, descriptor);
|
|
2045
|
+
}
|
|
2046
|
+
removeEventListener(type, listener) {
|
|
2047
|
+
if (!listener || !isWebSocketListenerType(type)) return;
|
|
2048
|
+
const normalizedListener = listener;
|
|
2049
|
+
if (typeof normalizedListener !== "function" && typeof normalizedListener !== "object") return;
|
|
2050
|
+
const listenerMap = this.getListenerMap(type);
|
|
2051
|
+
const descriptor = listenerMap.get(normalizedListener);
|
|
2052
|
+
if (!descriptor) return;
|
|
2053
|
+
if (descriptor.abortSignal && descriptor.abortHandler) descriptor.abortSignal.removeEventListener("abort", descriptor.abortHandler);
|
|
2054
|
+
listenerMap.delete(normalizedListener);
|
|
2055
|
+
}
|
|
2056
|
+
getSendByteLength(data) {
|
|
2057
|
+
if (typeof data === "string") return Buffer.byteLength(data);
|
|
2058
|
+
if (Buffer.isBuffer(data)) return data.byteLength;
|
|
2059
|
+
if (data instanceof ArrayBuffer) return data.byteLength;
|
|
2060
|
+
if (ArrayBuffer.isView(data)) return data.byteLength;
|
|
2061
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return data.size;
|
|
2062
|
+
throw new TypeError("WebSocket data must be a string, Buffer, ArrayBuffer, ArrayBufferView, or Blob");
|
|
2063
|
+
}
|
|
2064
|
+
async normalizeSendPayload(data) {
|
|
2065
|
+
if (typeof data === "string") return data;
|
|
2066
|
+
if (Buffer.isBuffer(data)) return data;
|
|
2067
|
+
if (data instanceof ArrayBuffer) return Buffer.from(data);
|
|
2068
|
+
if (ArrayBuffer.isView(data)) return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
2069
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return Buffer.from(await data.arrayBuffer());
|
|
2070
|
+
throw new TypeError("WebSocket data must be a string, Buffer, ArrayBuffer, ArrayBufferView, or Blob");
|
|
2071
|
+
}
|
|
2072
|
+
send(data) {
|
|
2073
|
+
if (this.readyState !== WebSocket.OPEN || !this._connection) throw new RequestError("WebSocket is not open");
|
|
2074
|
+
const queuedBytes = this.getSendByteLength(data);
|
|
2075
|
+
const connection = this._connection;
|
|
2076
|
+
this._bufferedAmount += queuedBytes;
|
|
2077
|
+
const sendTask = async () => {
|
|
2078
|
+
try {
|
|
2079
|
+
const payload = await this.normalizeSendPayload(data);
|
|
2080
|
+
await nativeBinding.websocketSend(connection, payload);
|
|
2081
|
+
} catch (error) {
|
|
2082
|
+
this.handleNativeError(String(error));
|
|
2083
|
+
this.finalizeClosed({
|
|
2084
|
+
code: 1006,
|
|
2085
|
+
reason: ""
|
|
2086
|
+
}, false);
|
|
2087
|
+
} finally {
|
|
2088
|
+
this._bufferedAmount = Math.max(0, this._bufferedAmount - queuedBytes);
|
|
2089
|
+
}
|
|
2090
|
+
};
|
|
2091
|
+
this._sendChain = this._sendChain.then(sendTask, sendTask);
|
|
2092
|
+
}
|
|
2093
|
+
close(code, reason) {
|
|
2094
|
+
if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) return;
|
|
2095
|
+
this._closeOptions = normalizeWebSocketCloseOptions(code, reason);
|
|
2096
|
+
this.readyState = WebSocket.CLOSING;
|
|
2097
|
+
this.startNativeClose();
|
|
2098
|
+
}
|
|
2654
2099
|
};
|
|
2655
2100
|
function isInternalWebSocketInit(value) {
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
const candidate = value;
|
|
2660
|
-
return candidate._internal === true && typeof candidate.url === "string" && typeof candidate.connect === "function";
|
|
2101
|
+
if (!isPlainObject(value)) return false;
|
|
2102
|
+
const candidate = value;
|
|
2103
|
+
return candidate._internal === true && typeof candidate.url === "string" && typeof candidate.connect === "function";
|
|
2661
2104
|
}
|
|
2662
2105
|
function normalizeStandaloneWebSocketArgs(urlOrOptions, options) {
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2106
|
+
if (typeof urlOrOptions === "string" || urlOrOptions instanceof URL) {
|
|
2107
|
+
const normalizedOptions = normalizeStandaloneWebSocketOptions(options);
|
|
2108
|
+
return {
|
|
2109
|
+
url: normalizeWebSocketUrl(urlOrOptions),
|
|
2110
|
+
options: normalizedOptions,
|
|
2111
|
+
legacyCallbacks: extractLegacyWebSocketCallbacks(options)
|
|
2112
|
+
};
|
|
2113
|
+
}
|
|
2114
|
+
const legacy = urlOrOptions;
|
|
2115
|
+
const normalizedOptions = normalizeStandaloneWebSocketOptions(legacy);
|
|
2116
|
+
return {
|
|
2117
|
+
url: normalizeWebSocketUrl(legacy.url),
|
|
2118
|
+
options: normalizedOptions,
|
|
2119
|
+
legacyCallbacks: extractLegacyWebSocketCallbacks(legacy)
|
|
2120
|
+
};
|
|
2678
2121
|
}
|
|
2679
2122
|
function normalizeSessionWebSocketArgs(urlOrOptions, options) {
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2123
|
+
if (typeof urlOrOptions === "string" || urlOrOptions instanceof URL) {
|
|
2124
|
+
const normalizedOptions = normalizeSessionWebSocketOptions(options);
|
|
2125
|
+
return {
|
|
2126
|
+
url: normalizeWebSocketUrl(urlOrOptions),
|
|
2127
|
+
options: normalizedOptions,
|
|
2128
|
+
legacyCallbacks: extractLegacyWebSocketCallbacks(options)
|
|
2129
|
+
};
|
|
2130
|
+
}
|
|
2131
|
+
const legacy = urlOrOptions;
|
|
2132
|
+
const normalizedOptions = normalizeSessionWebSocketOptions(legacy);
|
|
2133
|
+
return {
|
|
2134
|
+
url: normalizeWebSocketUrl(legacy.url),
|
|
2135
|
+
options: normalizedOptions,
|
|
2136
|
+
legacyCallbacks: extractLegacyWebSocketCallbacks(legacy)
|
|
2137
|
+
};
|
|
2695
2138
|
}
|
|
2696
2139
|
async function websocket(urlOrOptions, options) {
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
legacyCallbacks: normalized.legacyCallbacks
|
|
2725
|
-
});
|
|
2140
|
+
const normalized = normalizeStandaloneWebSocketArgs(urlOrOptions, options);
|
|
2141
|
+
validateWebSocketProtocols(normalized.options.protocols);
|
|
2142
|
+
assertNoManualWebSocketProtocolHeader(normalized.options.headers);
|
|
2143
|
+
const emulationMode = resolveEmulationMode(normalized.options.browser, normalized.options.os, normalized.options.emulation);
|
|
2144
|
+
const protocols = normalizeWebSocketProtocolList(normalized.options.protocols);
|
|
2145
|
+
return WebSocket._connectWithInit({
|
|
2146
|
+
_internal: true,
|
|
2147
|
+
url: normalized.url,
|
|
2148
|
+
options: normalized.options,
|
|
2149
|
+
openDispatchMode: "deferred",
|
|
2150
|
+
connect: (callbacks) => {
|
|
2151
|
+
const nativeOptions = {
|
|
2152
|
+
url: normalized.url,
|
|
2153
|
+
headers: headersToTuples(normalized.options.headers ?? {}),
|
|
2154
|
+
...protocols && protocols.length > 0 && { protocols },
|
|
2155
|
+
...normalized.options.proxy !== void 0 && { proxy: normalized.options.proxy },
|
|
2156
|
+
...normalized.options.maxFrameSize !== void 0 && { maxFrameSize: normalized.options.maxFrameSize },
|
|
2157
|
+
...normalized.options.maxMessageSize !== void 0 && { maxMessageSize: normalized.options.maxMessageSize },
|
|
2158
|
+
onMessage: callbacks.onMessage,
|
|
2159
|
+
onClose: callbacks.onClose,
|
|
2160
|
+
onError: callbacks.onError
|
|
2161
|
+
};
|
|
2162
|
+
applyNativeEmulationMode(nativeOptions, emulationMode);
|
|
2163
|
+
return nativeBinding.websocketConnect(nativeOptions);
|
|
2164
|
+
},
|
|
2165
|
+
legacyCallbacks: normalized.legacyCallbacks
|
|
2166
|
+
});
|
|
2726
2167
|
}
|
|
2727
2168
|
var wreq_js_default = {
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2169
|
+
fetch,
|
|
2170
|
+
request,
|
|
2171
|
+
get,
|
|
2172
|
+
post,
|
|
2173
|
+
getProfiles,
|
|
2174
|
+
getOperatingSystems,
|
|
2175
|
+
createTransport,
|
|
2176
|
+
createSession,
|
|
2177
|
+
withSession,
|
|
2178
|
+
websocket,
|
|
2179
|
+
WebSocket,
|
|
2180
|
+
Headers,
|
|
2181
|
+
Response,
|
|
2182
|
+
Transport,
|
|
2183
|
+
Session,
|
|
2184
|
+
RequestError
|
|
2744
2185
|
};
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2186
|
+
//#endregion
|
|
2187
|
+
exports.Headers = Headers;
|
|
2188
|
+
exports.RequestError = RequestError;
|
|
2189
|
+
exports.Response = Response;
|
|
2190
|
+
exports.Session = Session;
|
|
2191
|
+
exports.Transport = Transport;
|
|
2192
|
+
exports.WebSocket = WebSocket;
|
|
2193
|
+
exports.createSession = createSession;
|
|
2194
|
+
exports.createTransport = createTransport;
|
|
2195
|
+
exports.default = wreq_js_default;
|
|
2196
|
+
exports.fetch = fetch;
|
|
2197
|
+
exports.get = get;
|
|
2198
|
+
exports.getOperatingSystems = getOperatingSystems;
|
|
2199
|
+
exports.getProfiles = getProfiles;
|
|
2200
|
+
exports.post = post;
|
|
2201
|
+
exports.request = request;
|
|
2202
|
+
exports.websocket = websocket;
|
|
2203
|
+
exports.withSession = withSession;
|
|
2204
|
+
|
|
2764
2205
|
//# sourceMappingURL=wreq-js.cjs.map
|