wreq-js 2.1.1 → 2.2.1

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 CHANGED
@@ -80,6 +80,24 @@ function detectLibc() {
80
80
  }
81
81
  }
82
82
  var require2 = typeof import_meta !== "undefined" && import_meta.url ? (0, import_node_module.createRequire)(import_meta.url) : (0, import_node_module.createRequire)(__filename);
83
+ function requirePlatformBinary(platformArch) {
84
+ switch (platformArch) {
85
+ case "darwin-x64":
86
+ return require2("../rust/wreq-js.darwin-x64.node");
87
+ case "darwin-arm64":
88
+ return require2("../rust/wreq-js.darwin-arm64.node");
89
+ case "linux-x64-gnu":
90
+ return require2("../rust/wreq-js.linux-x64-gnu.node");
91
+ case "linux-x64-musl":
92
+ return require2("../rust/wreq-js.linux-x64-musl.node");
93
+ case "linux-arm64-gnu":
94
+ return require2("../rust/wreq-js.linux-arm64-gnu.node");
95
+ case "win32-x64-msvc":
96
+ return require2("../rust/wreq-js.win32-x64-msvc.node");
97
+ default:
98
+ return void 0;
99
+ }
100
+ }
83
101
  function loadNativeBinding() {
84
102
  const platform = process.platform;
85
103
  const arch = process.arch;
@@ -101,15 +119,18 @@ function loadNativeBinding() {
101
119
  }
102
120
  const binaryName = `wreq-js.${platformArch}.node`;
103
121
  try {
104
- return require2(`../rust/${binaryName}`);
105
- } catch {
106
- try {
107
- return require2("../rust/wreq-js.node");
108
- } catch {
109
- throw new Error(
110
- `Failed to load native module for ${platform}-${arch}. Tried: ../rust/${binaryName} and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.`
111
- );
122
+ const platformBinding = requirePlatformBinary(platformArch);
123
+ if (platformBinding) {
124
+ return platformBinding;
112
125
  }
126
+ } catch {
127
+ }
128
+ try {
129
+ return require2("../rust/wreq-js.node");
130
+ } catch {
131
+ throw new Error(
132
+ `Failed to load native module for ${platform}-${arch}. Tried: ../rust/${binaryName} and ../rust/wreq-js.node. Make sure the package is installed correctly and the native module is built for your platform.`
133
+ );
113
134
  }
114
135
  }
115
136
  nativeBinding = loadNativeBinding();
@@ -141,8 +162,7 @@ function generateSessionId() {
141
162
  function normalizeSessionOptions(options) {
142
163
  const sessionId = options?.sessionId ?? generateSessionId();
143
164
  const defaults = {
144
- browser: options?.browser ?? DEFAULT_BROWSER,
145
- os: options?.os ?? DEFAULT_OS
165
+ transportMode: resolveEmulationMode(options?.browser, options?.os, options?.emulation)
146
166
  };
147
167
  if (options?.proxy !== void 0) {
148
168
  defaults.proxy = options.proxy;
@@ -866,21 +886,26 @@ function resolveTransportContext(config, sessionDefaults) {
866
886
  throw new RequestError("Transport has been closed");
867
887
  }
868
888
  const hasProxy = config.proxy !== void 0;
869
- if (config.browser !== void 0 || config.os !== void 0 || hasProxy || config.insecure !== void 0) {
870
- throw new RequestError("`transport` cannot be combined with browser/os/proxy/insecure options");
889
+ if (config.browser !== void 0 || config.os !== void 0 || config.emulation !== void 0 || hasProxy || config.insecure !== void 0) {
890
+ throw new RequestError("`transport` cannot be combined with browser/os/emulation/proxy/insecure options");
871
891
  }
872
892
  return { transportId: config.transport.id };
873
893
  }
874
894
  if (sessionDefaults?.transportId) {
895
+ if (config.emulation !== void 0) {
896
+ throw new RequestError("Session emulation cannot be changed after creation");
897
+ }
875
898
  if (config.browser !== void 0) {
876
899
  validateBrowserProfile(config.browser);
877
- if (config.browser !== sessionDefaults.browser) {
900
+ const lockedBrowser = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.browser;
901
+ if (config.browser !== lockedBrowser) {
878
902
  throw new RequestError("Session browser cannot be changed after creation");
879
903
  }
880
904
  }
881
905
  if (config.os !== void 0) {
882
906
  validateOperatingSystem(config.os);
883
- if (config.os !== sessionDefaults.os) {
907
+ const lockedOs = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.os;
908
+ if (config.os !== lockedOs) {
884
909
  throw new RequestError("Session operating system cannot be changed after creation");
885
910
  }
886
911
  }
@@ -897,11 +922,9 @@ function resolveTransportContext(config, sessionDefaults) {
897
922
  }
898
923
  return { transportId: sessionDefaults.transportId };
899
924
  }
900
- const browser = config.browser ?? DEFAULT_BROWSER;
901
- const os = config.os ?? DEFAULT_OS;
902
- validateBrowserProfile(browser);
903
- validateOperatingSystem(os);
904
- const resolved = { browser, os };
925
+ const resolved = {
926
+ mode: resolveEmulationMode(config.browser, config.os, config.emulation)
927
+ };
905
928
  if (config.proxy !== void 0) {
906
929
  resolved.proxy = config.proxy;
907
930
  }
@@ -1146,6 +1169,553 @@ function validatePositiveInteger(value, label) {
1146
1169
  throw new RequestError(`${label} must be greater than 0`);
1147
1170
  }
1148
1171
  }
1172
+ function validateIntegerInRange(value, min, max, label) {
1173
+ validateNonNegativeInteger(value, label);
1174
+ if (value < min || value > max) {
1175
+ throw new RequestError(`${label} must be between ${min} and ${max}`);
1176
+ }
1177
+ }
1178
+ var SUPPORTED_ALPN_PROTOCOLS = /* @__PURE__ */ new Set(["HTTP1", "HTTP2", "HTTP3"]);
1179
+ var SUPPORTED_ALPS_PROTOCOLS = /* @__PURE__ */ new Set(["HTTP1", "HTTP2", "HTTP3"]);
1180
+ var SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS = /* @__PURE__ */ new Set(["zlib", "brotli", "zstd"]);
1181
+ var HTTP2_SETTING_IDS = /* @__PURE__ */ new Set([
1182
+ "HeaderTableSize",
1183
+ "EnablePush",
1184
+ "MaxConcurrentStreams",
1185
+ "InitialWindowSize",
1186
+ "MaxFrameSize",
1187
+ "MaxHeaderListSize",
1188
+ "EnableConnectProtocol",
1189
+ "NoRfc7540Priorities"
1190
+ ]);
1191
+ var HTTP2_PSEUDO_HEADER_IDS = /* @__PURE__ */ new Set(["Method", "Scheme", "Authority", "Path", "Protocol"]);
1192
+ var STANDARD_HTTP2_SETTING_ID_VALUES = /* @__PURE__ */ new Set([1, 2, 3, 4, 5, 6, 8, 9]);
1193
+ var MAX_HTTP2_EXPERIMENTAL_SETTING_ID = 15;
1194
+ var TLS_VERSION_ALIASES = /* @__PURE__ */ new Map([
1195
+ ["1.0", "1.0"],
1196
+ ["1.1", "1.1"],
1197
+ ["1.2", "1.2"],
1198
+ ["1.3", "1.3"],
1199
+ ["tls1.0", "1.0"],
1200
+ ["tls1.1", "1.1"],
1201
+ ["tls1.2", "1.2"],
1202
+ ["tls1.3", "1.3"]
1203
+ ]);
1204
+ function isNonEmpty(value) {
1205
+ for (const _ in value) return true;
1206
+ return false;
1207
+ }
1208
+ function normalizeProtocolList(value, label, allowed) {
1209
+ if (value === void 0) {
1210
+ return void 0;
1211
+ }
1212
+ if (!Array.isArray(value)) {
1213
+ throw new RequestError(`${label} must be an array`);
1214
+ }
1215
+ for (const protocol of value) {
1216
+ if (!allowed.has(protocol)) {
1217
+ throw new RequestError(`${label} values must be one of: HTTP1, HTTP2, HTTP3`);
1218
+ }
1219
+ }
1220
+ return [...value];
1221
+ }
1222
+ function normalizeTlsVersion(value, label) {
1223
+ if (value === void 0) {
1224
+ return void 0;
1225
+ }
1226
+ if (typeof value !== "string") {
1227
+ throw new RequestError(`${label} must be a string`);
1228
+ }
1229
+ const normalized = TLS_VERSION_ALIASES.get(value.trim().toLowerCase());
1230
+ if (!normalized) {
1231
+ throw new RequestError(`${label} must be one of: 1.0, 1.1, 1.2, 1.3`);
1232
+ }
1233
+ return normalized;
1234
+ }
1235
+ function normalizeOrigHeaders(origHeaders) {
1236
+ if (origHeaders === void 0) {
1237
+ return void 0;
1238
+ }
1239
+ if (!Array.isArray(origHeaders)) {
1240
+ throw new RequestError("emulation.origHeaders must be an array of strings");
1241
+ }
1242
+ const normalized = [];
1243
+ const seen = /* @__PURE__ */ new Set();
1244
+ for (const entry of origHeaders) {
1245
+ if (typeof entry !== "string" || entry.trim().length === 0) {
1246
+ throw new RequestError("emulation.origHeaders entries must be non-empty strings");
1247
+ }
1248
+ const trimmed = entry.trim();
1249
+ const duplicateKey = trimmed.toLowerCase();
1250
+ if (seen.has(duplicateKey)) {
1251
+ throw new RequestError(`Duplicate emulation.origHeaders entry: ${trimmed}`);
1252
+ }
1253
+ seen.add(duplicateKey);
1254
+ normalized.push(trimmed);
1255
+ }
1256
+ return normalized.length > 0 ? normalized : void 0;
1257
+ }
1258
+ function normalizeCustomTlsOptions(options) {
1259
+ if (options === void 0) {
1260
+ return void 0;
1261
+ }
1262
+ const normalized = {};
1263
+ const alpnProtocols = normalizeProtocolList(
1264
+ options.alpnProtocols,
1265
+ "emulation.tlsOptions.alpnProtocols",
1266
+ SUPPORTED_ALPN_PROTOCOLS
1267
+ );
1268
+ if (alpnProtocols !== void 0) {
1269
+ normalized.alpnProtocols = alpnProtocols;
1270
+ }
1271
+ const alpsProtocols = normalizeProtocolList(
1272
+ options.alpsProtocols,
1273
+ "emulation.tlsOptions.alpsProtocols",
1274
+ SUPPORTED_ALPS_PROTOCOLS
1275
+ );
1276
+ if (alpsProtocols !== void 0) {
1277
+ normalized.alpsProtocols = alpsProtocols;
1278
+ }
1279
+ const minTlsVersion = normalizeTlsVersion(options.minTlsVersion, "emulation.tlsOptions.minTlsVersion");
1280
+ if (minTlsVersion !== void 0) {
1281
+ normalized.minTlsVersion = minTlsVersion;
1282
+ }
1283
+ const maxTlsVersion = normalizeTlsVersion(options.maxTlsVersion, "emulation.tlsOptions.maxTlsVersion");
1284
+ if (maxTlsVersion !== void 0) {
1285
+ normalized.maxTlsVersion = maxTlsVersion;
1286
+ }
1287
+ if (options.alpsUseNewCodepoint !== void 0) {
1288
+ normalized.alpsUseNewCodepoint = options.alpsUseNewCodepoint;
1289
+ }
1290
+ if (options.sessionTicket !== void 0) {
1291
+ normalized.sessionTicket = options.sessionTicket;
1292
+ }
1293
+ if (options.preSharedKey !== void 0) {
1294
+ normalized.preSharedKey = options.preSharedKey;
1295
+ }
1296
+ if (options.enableEchGrease !== void 0) {
1297
+ normalized.enableEchGrease = options.enableEchGrease;
1298
+ }
1299
+ if (options.permuteExtensions !== void 0) {
1300
+ normalized.permuteExtensions = options.permuteExtensions;
1301
+ }
1302
+ if (options.greaseEnabled !== void 0) {
1303
+ normalized.greaseEnabled = options.greaseEnabled;
1304
+ }
1305
+ if (options.enableOcspStapling !== void 0) {
1306
+ normalized.enableOcspStapling = options.enableOcspStapling;
1307
+ }
1308
+ if (options.enableSignedCertTimestamps !== void 0) {
1309
+ normalized.enableSignedCertTimestamps = options.enableSignedCertTimestamps;
1310
+ }
1311
+ if (options.pskSkipSessionTicket !== void 0) {
1312
+ normalized.pskSkipSessionTicket = options.pskSkipSessionTicket;
1313
+ }
1314
+ if (options.pskDheKe !== void 0) {
1315
+ normalized.pskDheKe = options.pskDheKe;
1316
+ }
1317
+ if (options.renegotiation !== void 0) {
1318
+ normalized.renegotiation = options.renegotiation;
1319
+ }
1320
+ if (options.aesHwOverride !== void 0) {
1321
+ normalized.aesHwOverride = options.aesHwOverride;
1322
+ }
1323
+ if (options.preserveTls13CipherList !== void 0) {
1324
+ normalized.preserveTls13CipherList = options.preserveTls13CipherList;
1325
+ }
1326
+ if (options.randomAesHwOverride !== void 0) {
1327
+ normalized.randomAesHwOverride = options.randomAesHwOverride;
1328
+ }
1329
+ if (options.delegatedCredentials !== void 0) {
1330
+ normalized.delegatedCredentials = options.delegatedCredentials;
1331
+ }
1332
+ if (options.curvesList !== void 0) {
1333
+ normalized.curvesList = options.curvesList;
1334
+ }
1335
+ if (options.cipherList !== void 0) {
1336
+ normalized.cipherList = options.cipherList;
1337
+ }
1338
+ if (options.sigalgsList !== void 0) {
1339
+ normalized.sigalgsList = options.sigalgsList;
1340
+ }
1341
+ if (options.recordSizeLimit !== void 0) {
1342
+ validateIntegerInRange(options.recordSizeLimit, 0, 65535, "emulation.tlsOptions.recordSizeLimit");
1343
+ normalized.recordSizeLimit = options.recordSizeLimit;
1344
+ }
1345
+ if (options.keySharesLimit !== void 0) {
1346
+ validateIntegerInRange(options.keySharesLimit, 0, 255, "emulation.tlsOptions.keySharesLimit");
1347
+ normalized.keySharesLimit = options.keySharesLimit;
1348
+ }
1349
+ if (options.certificateCompressionAlgorithms !== void 0) {
1350
+ if (!Array.isArray(options.certificateCompressionAlgorithms)) {
1351
+ throw new RequestError("emulation.tlsOptions.certificateCompressionAlgorithms must be an array");
1352
+ }
1353
+ const algorithms = [];
1354
+ const seen = /* @__PURE__ */ new Set();
1355
+ for (const algorithm of options.certificateCompressionAlgorithms) {
1356
+ if (!SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS.has(algorithm)) {
1357
+ throw new RequestError(
1358
+ "emulation.tlsOptions.certificateCompressionAlgorithms values must be one of: zlib, brotli, zstd"
1359
+ );
1360
+ }
1361
+ if (seen.has(algorithm)) {
1362
+ throw new RequestError(`Duplicate emulation.tlsOptions.certificateCompressionAlgorithms entry: ${algorithm}`);
1363
+ }
1364
+ seen.add(algorithm);
1365
+ algorithms.push(algorithm);
1366
+ }
1367
+ normalized.certificateCompressionAlgorithms = algorithms;
1368
+ }
1369
+ if (options.extensionPermutation !== void 0) {
1370
+ if (!Array.isArray(options.extensionPermutation)) {
1371
+ throw new RequestError("emulation.tlsOptions.extensionPermutation must be an array");
1372
+ }
1373
+ const permutation = [];
1374
+ const seen = /* @__PURE__ */ new Set();
1375
+ for (const extensionId of options.extensionPermutation) {
1376
+ validateIntegerInRange(extensionId, 0, 65535, "emulation.tlsOptions.extensionPermutation");
1377
+ if (seen.has(extensionId)) {
1378
+ throw new RequestError(`Duplicate emulation.tlsOptions.extensionPermutation entry: ${extensionId}`);
1379
+ }
1380
+ seen.add(extensionId);
1381
+ permutation.push(extensionId);
1382
+ }
1383
+ normalized.extensionPermutation = permutation;
1384
+ }
1385
+ return isNonEmpty(normalized) ? normalized : void 0;
1386
+ }
1387
+ function normalizeCustomHttp1Options(options) {
1388
+ if (options === void 0) {
1389
+ return void 0;
1390
+ }
1391
+ const normalized = {};
1392
+ if (options.http09Responses !== void 0) {
1393
+ normalized.http09Responses = options.http09Responses;
1394
+ }
1395
+ if (options.writev !== void 0) {
1396
+ normalized.writev = options.writev;
1397
+ }
1398
+ if (options.ignoreInvalidHeadersInResponses !== void 0) {
1399
+ normalized.ignoreInvalidHeadersInResponses = options.ignoreInvalidHeadersInResponses;
1400
+ }
1401
+ if (options.allowSpacesAfterHeaderNameInResponses !== void 0) {
1402
+ normalized.allowSpacesAfterHeaderNameInResponses = options.allowSpacesAfterHeaderNameInResponses;
1403
+ }
1404
+ if (options.allowObsoleteMultilineHeadersInResponses !== void 0) {
1405
+ normalized.allowObsoleteMultilineHeadersInResponses = options.allowObsoleteMultilineHeadersInResponses;
1406
+ }
1407
+ if (options.maxHeaders !== void 0) {
1408
+ validateNonNegativeInteger(options.maxHeaders, "emulation.http1Options.maxHeaders");
1409
+ normalized.maxHeaders = options.maxHeaders;
1410
+ }
1411
+ if (options.readBufExactSize !== void 0) {
1412
+ validateNonNegativeInteger(options.readBufExactSize, "emulation.http1Options.readBufExactSize");
1413
+ normalized.readBufExactSize = options.readBufExactSize;
1414
+ }
1415
+ if (options.maxBufSize !== void 0) {
1416
+ validateNonNegativeInteger(options.maxBufSize, "emulation.http1Options.maxBufSize");
1417
+ if (options.maxBufSize < 8192) {
1418
+ throw new RequestError("emulation.http1Options.maxBufSize must be greater than or equal to 8192");
1419
+ }
1420
+ normalized.maxBufSize = options.maxBufSize;
1421
+ }
1422
+ if (normalized.readBufExactSize !== void 0 && normalized.maxBufSize !== void 0) {
1423
+ throw new RequestError("emulation.http1Options.readBufExactSize and maxBufSize cannot both be set");
1424
+ }
1425
+ return isNonEmpty(normalized) ? normalized : void 0;
1426
+ }
1427
+ function normalizeHttp2StreamDependency(dependency, label) {
1428
+ if (!isPlainObject(dependency)) {
1429
+ throw new RequestError(`${label} must be an object`);
1430
+ }
1431
+ validateIntegerInRange(dependency.dependencyId, 0, 2147483647, `${label}.dependencyId`);
1432
+ validateIntegerInRange(dependency.weight, 0, 255, `${label}.weight`);
1433
+ const normalized = {
1434
+ dependencyId: dependency.dependencyId,
1435
+ weight: dependency.weight
1436
+ };
1437
+ if (dependency.exclusive !== void 0) {
1438
+ normalized.exclusive = dependency.exclusive;
1439
+ }
1440
+ return normalized;
1441
+ }
1442
+ function normalizeCustomHttp2Options(options) {
1443
+ if (options === void 0) {
1444
+ return void 0;
1445
+ }
1446
+ const normalized = {};
1447
+ if (options.adaptiveWindow !== void 0) {
1448
+ normalized.adaptiveWindow = options.adaptiveWindow;
1449
+ }
1450
+ if (options.keepAliveWhileIdle !== void 0) {
1451
+ normalized.keepAliveWhileIdle = options.keepAliveWhileIdle;
1452
+ }
1453
+ if (options.enablePush !== void 0) {
1454
+ normalized.enablePush = options.enablePush;
1455
+ }
1456
+ if (options.enableConnectProtocol !== void 0) {
1457
+ normalized.enableConnectProtocol = options.enableConnectProtocol;
1458
+ }
1459
+ if (options.noRfc7540Priorities !== void 0) {
1460
+ normalized.noRfc7540Priorities = options.noRfc7540Priorities;
1461
+ }
1462
+ if (options.initialStreamId !== void 0) {
1463
+ validateNonNegativeInteger(options.initialStreamId, "emulation.http2Options.initialStreamId");
1464
+ normalized.initialStreamId = options.initialStreamId;
1465
+ }
1466
+ if (options.initialConnectionWindowSize !== void 0) {
1467
+ validateNonNegativeInteger(
1468
+ options.initialConnectionWindowSize,
1469
+ "emulation.http2Options.initialConnectionWindowSize"
1470
+ );
1471
+ normalized.initialConnectionWindowSize = options.initialConnectionWindowSize;
1472
+ }
1473
+ if (options.initialWindowSize !== void 0) {
1474
+ validateNonNegativeInteger(options.initialWindowSize, "emulation.http2Options.initialWindowSize");
1475
+ normalized.initialWindowSize = options.initialWindowSize;
1476
+ }
1477
+ if (options.initialMaxSendStreams !== void 0) {
1478
+ validateNonNegativeInteger(options.initialMaxSendStreams, "emulation.http2Options.initialMaxSendStreams");
1479
+ normalized.initialMaxSendStreams = options.initialMaxSendStreams;
1480
+ }
1481
+ if (options.maxFrameSize !== void 0) {
1482
+ validateNonNegativeInteger(options.maxFrameSize, "emulation.http2Options.maxFrameSize");
1483
+ normalized.maxFrameSize = options.maxFrameSize;
1484
+ }
1485
+ if (options.keepAliveInterval !== void 0) {
1486
+ validateNonNegativeInteger(options.keepAliveInterval, "emulation.http2Options.keepAliveInterval");
1487
+ normalized.keepAliveInterval = options.keepAliveInterval;
1488
+ }
1489
+ if (options.keepAliveTimeout !== void 0) {
1490
+ validateNonNegativeInteger(options.keepAliveTimeout, "emulation.http2Options.keepAliveTimeout");
1491
+ normalized.keepAliveTimeout = options.keepAliveTimeout;
1492
+ }
1493
+ if (options.maxConcurrentResetStreams !== void 0) {
1494
+ validateNonNegativeInteger(options.maxConcurrentResetStreams, "emulation.http2Options.maxConcurrentResetStreams");
1495
+ normalized.maxConcurrentResetStreams = options.maxConcurrentResetStreams;
1496
+ }
1497
+ if (options.maxSendBufferSize !== void 0) {
1498
+ validateNonNegativeInteger(options.maxSendBufferSize, "emulation.http2Options.maxSendBufferSize");
1499
+ normalized.maxSendBufferSize = options.maxSendBufferSize;
1500
+ }
1501
+ if (options.maxConcurrentStreams !== void 0) {
1502
+ validateNonNegativeInteger(options.maxConcurrentStreams, "emulation.http2Options.maxConcurrentStreams");
1503
+ normalized.maxConcurrentStreams = options.maxConcurrentStreams;
1504
+ }
1505
+ if (options.maxHeaderListSize !== void 0) {
1506
+ validateNonNegativeInteger(options.maxHeaderListSize, "emulation.http2Options.maxHeaderListSize");
1507
+ normalized.maxHeaderListSize = options.maxHeaderListSize;
1508
+ }
1509
+ if (options.maxPendingAcceptResetStreams !== void 0) {
1510
+ validateNonNegativeInteger(
1511
+ options.maxPendingAcceptResetStreams,
1512
+ "emulation.http2Options.maxPendingAcceptResetStreams"
1513
+ );
1514
+ normalized.maxPendingAcceptResetStreams = options.maxPendingAcceptResetStreams;
1515
+ }
1516
+ if (options.headerTableSize !== void 0) {
1517
+ validateNonNegativeInteger(options.headerTableSize, "emulation.http2Options.headerTableSize");
1518
+ normalized.headerTableSize = options.headerTableSize;
1519
+ }
1520
+ if (options.settingsOrder !== void 0) {
1521
+ if (!Array.isArray(options.settingsOrder)) {
1522
+ throw new RequestError("emulation.http2Options.settingsOrder must be an array");
1523
+ }
1524
+ const settingsOrder = [];
1525
+ const seen = /* @__PURE__ */ new Set();
1526
+ for (const settingId of options.settingsOrder) {
1527
+ if (!HTTP2_SETTING_IDS.has(settingId)) {
1528
+ throw new RequestError("emulation.http2Options.settingsOrder contains an unsupported setting id");
1529
+ }
1530
+ if (seen.has(settingId)) {
1531
+ throw new RequestError(`Duplicate emulation.http2Options.settingsOrder entry: ${settingId}`);
1532
+ }
1533
+ seen.add(settingId);
1534
+ settingsOrder.push(settingId);
1535
+ }
1536
+ normalized.settingsOrder = settingsOrder;
1537
+ }
1538
+ if (options.headersPseudoOrder !== void 0) {
1539
+ if (!Array.isArray(options.headersPseudoOrder)) {
1540
+ throw new RequestError("emulation.http2Options.headersPseudoOrder must be an array");
1541
+ }
1542
+ const headersPseudoOrder = [];
1543
+ const seenPseudo = /* @__PURE__ */ new Set();
1544
+ for (const pseudoId of options.headersPseudoOrder) {
1545
+ if (!HTTP2_PSEUDO_HEADER_IDS.has(pseudoId)) {
1546
+ throw new RequestError("emulation.http2Options.headersPseudoOrder contains an unsupported pseudo-header id");
1547
+ }
1548
+ if (seenPseudo.has(pseudoId)) {
1549
+ throw new RequestError(`Duplicate emulation.http2Options.headersPseudoOrder entry: ${pseudoId}`);
1550
+ }
1551
+ seenPseudo.add(pseudoId);
1552
+ headersPseudoOrder.push(pseudoId);
1553
+ }
1554
+ normalized.headersPseudoOrder = headersPseudoOrder;
1555
+ }
1556
+ if (options.headersStreamDependency !== void 0) {
1557
+ normalized.headersStreamDependency = normalizeHttp2StreamDependency(
1558
+ options.headersStreamDependency,
1559
+ "emulation.http2Options.headersStreamDependency"
1560
+ );
1561
+ }
1562
+ if (options.priorities !== void 0) {
1563
+ if (!Array.isArray(options.priorities)) {
1564
+ throw new RequestError("emulation.http2Options.priorities must be an array");
1565
+ }
1566
+ const priorities = [];
1567
+ const seenStreamIds = /* @__PURE__ */ new Set();
1568
+ for (const [index, priority] of options.priorities.entries()) {
1569
+ if (!isPlainObject(priority)) {
1570
+ throw new RequestError(`emulation.http2Options.priorities[${index}] must be an object`);
1571
+ }
1572
+ validatePositiveInteger(priority.streamId, `emulation.http2Options.priorities[${index}].streamId`);
1573
+ if (seenStreamIds.has(priority.streamId)) {
1574
+ throw new RequestError(`Duplicate emulation.http2Options.priorities streamId: ${priority.streamId}`);
1575
+ }
1576
+ seenStreamIds.add(priority.streamId);
1577
+ priorities.push({
1578
+ streamId: priority.streamId,
1579
+ dependency: normalizeHttp2StreamDependency(
1580
+ priority.dependency,
1581
+ `emulation.http2Options.priorities[${index}].dependency`
1582
+ )
1583
+ });
1584
+ }
1585
+ normalized.priorities = priorities;
1586
+ }
1587
+ if (options.experimentalSettings !== void 0) {
1588
+ if (!Array.isArray(options.experimentalSettings)) {
1589
+ throw new RequestError("emulation.http2Options.experimentalSettings must be an array");
1590
+ }
1591
+ const experimentalSettings = [];
1592
+ const seenIds = /* @__PURE__ */ new Set();
1593
+ for (const [index, setting] of options.experimentalSettings.entries()) {
1594
+ if (!isPlainObject(setting)) {
1595
+ throw new RequestError(`emulation.http2Options.experimentalSettings[${index}] must be an object`);
1596
+ }
1597
+ validateIntegerInRange(
1598
+ setting.id,
1599
+ 1,
1600
+ MAX_HTTP2_EXPERIMENTAL_SETTING_ID,
1601
+ `emulation.http2Options.experimentalSettings[${index}].id`
1602
+ );
1603
+ if (STANDARD_HTTP2_SETTING_ID_VALUES.has(setting.id)) {
1604
+ throw new RequestError(
1605
+ `emulation.http2Options.experimentalSettings[${index}].id must not be a standard HTTP/2 setting id`
1606
+ );
1607
+ }
1608
+ if (seenIds.has(setting.id)) {
1609
+ throw new RequestError(`Duplicate emulation.http2Options.experimentalSettings id: ${setting.id}`);
1610
+ }
1611
+ seenIds.add(setting.id);
1612
+ validateIntegerInRange(
1613
+ setting.value,
1614
+ 0,
1615
+ 4294967295,
1616
+ `emulation.http2Options.experimentalSettings[${index}].value`
1617
+ );
1618
+ experimentalSettings.push({
1619
+ id: setting.id,
1620
+ value: setting.value
1621
+ });
1622
+ }
1623
+ normalized.experimentalSettings = experimentalSettings;
1624
+ }
1625
+ return isNonEmpty(normalized) ? normalized : void 0;
1626
+ }
1627
+ function normalizeCustomEmulationOptions(emulation, allowEmpty) {
1628
+ if (emulation === void 0) {
1629
+ return void 0;
1630
+ }
1631
+ if (!isPlainObject(emulation)) {
1632
+ throw new RequestError("emulation must be an object");
1633
+ }
1634
+ const source = emulation;
1635
+ const normalized = {};
1636
+ const tlsOptions = normalizeCustomTlsOptions(source.tlsOptions);
1637
+ if (tlsOptions !== void 0) {
1638
+ normalized.tlsOptions = tlsOptions;
1639
+ }
1640
+ const http1Options = normalizeCustomHttp1Options(source.http1Options);
1641
+ if (http1Options !== void 0) {
1642
+ normalized.http1Options = http1Options;
1643
+ }
1644
+ const http2Options = normalizeCustomHttp2Options(source.http2Options);
1645
+ if (http2Options !== void 0) {
1646
+ normalized.http2Options = http2Options;
1647
+ }
1648
+ if (source.headers !== void 0) {
1649
+ const headers = headersToTuples(source.headers);
1650
+ if (headers.length > 0) {
1651
+ normalized.headers = headers;
1652
+ }
1653
+ }
1654
+ const origHeaders = normalizeOrigHeaders(source.origHeaders);
1655
+ if (origHeaders !== void 0) {
1656
+ normalized.origHeaders = origHeaders;
1657
+ }
1658
+ if (!allowEmpty && !isNonEmpty(normalized)) {
1659
+ throw new RequestError(
1660
+ "Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders"
1661
+ );
1662
+ }
1663
+ return isNonEmpty(normalized) ? normalized : void 0;
1664
+ }
1665
+ function serializeCustomEmulationOptions(emulation, allowEmpty) {
1666
+ const normalized = normalizeCustomEmulationOptions(emulation, allowEmpty);
1667
+ return normalized ? JSON.stringify(normalized) : void 0;
1668
+ }
1669
+ function resolveEmulationMode(browser, os, emulation) {
1670
+ if (browser !== void 0) {
1671
+ validateBrowserProfile(browser);
1672
+ if (os !== void 0) {
1673
+ validateOperatingSystem(os);
1674
+ }
1675
+ const emulationJson = serializeCustomEmulationOptions(emulation, true);
1676
+ return {
1677
+ kind: "preset",
1678
+ browser,
1679
+ os: os ?? DEFAULT_OS,
1680
+ ...emulationJson !== void 0 && { emulationJson }
1681
+ };
1682
+ }
1683
+ if (os !== void 0) {
1684
+ validateOperatingSystem(os);
1685
+ const emulationJson = serializeCustomEmulationOptions(emulation, true);
1686
+ return {
1687
+ kind: "preset",
1688
+ browser: DEFAULT_BROWSER,
1689
+ os,
1690
+ ...emulationJson !== void 0 && { emulationJson }
1691
+ };
1692
+ }
1693
+ if (emulation !== void 0) {
1694
+ const emulationJson = serializeCustomEmulationOptions(emulation, false);
1695
+ if (emulationJson === void 0) {
1696
+ throw new RequestError(
1697
+ "Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders"
1698
+ );
1699
+ }
1700
+ return { kind: "custom", emulationJson };
1701
+ }
1702
+ return {
1703
+ kind: "preset",
1704
+ browser: DEFAULT_BROWSER,
1705
+ os: DEFAULT_OS
1706
+ };
1707
+ }
1708
+ function applyNativeEmulationMode(target, mode) {
1709
+ if (mode.kind === "custom") {
1710
+ target.emulationJson = mode.emulationJson;
1711
+ return;
1712
+ }
1713
+ target.browser = mode.browser;
1714
+ target.os = mode.os;
1715
+ if (mode.emulationJson !== void 0) {
1716
+ target.emulationJson = mode.emulationJson;
1717
+ }
1718
+ }
1149
1719
  async function dispatchRequest(options, requestUrl, signal) {
1150
1720
  if (!signal) {
1151
1721
  const requestId2 = generateRequestId();
@@ -1220,8 +1790,9 @@ async function fetch(input, init) {
1220
1790
  if (transport.transportId) {
1221
1791
  requestOptions.transportId = transport.transportId;
1222
1792
  } else {
1223
- requestOptions.browser = transport.browser ?? DEFAULT_BROWSER;
1224
- requestOptions.os = transport.os ?? DEFAULT_OS;
1793
+ if (transport.mode !== void 0) {
1794
+ applyNativeEmulationMode(requestOptions, transport.mode);
1795
+ }
1225
1796
  if (transport.proxy !== void 0) {
1226
1797
  requestOptions.proxy = transport.proxy;
1227
1798
  }
@@ -1245,10 +1816,7 @@ async function fetch(input, init) {
1245
1816
  return dispatchRequest(requestOptions, url, config.signal ?? null);
1246
1817
  }
1247
1818
  async function createTransport(options) {
1248
- const browser = options?.browser ?? DEFAULT_BROWSER;
1249
- const os = options?.os ?? DEFAULT_OS;
1250
- validateBrowserProfile(browser);
1251
- validateOperatingSystem(os);
1819
+ const mode = resolveEmulationMode(options?.browser, options?.os, options?.emulation);
1252
1820
  if (options?.poolIdleTimeout !== void 0) {
1253
1821
  validatePositiveNumber(options.poolIdleTimeout, "poolIdleTimeout");
1254
1822
  }
@@ -1265,9 +1833,7 @@ async function createTransport(options) {
1265
1833
  validatePositiveNumber(options.readTimeout, "readTimeout");
1266
1834
  }
1267
1835
  try {
1268
- const id = nativeBinding.createTransport({
1269
- browser,
1270
- os,
1836
+ const transportOptions = {
1271
1837
  ...options?.proxy !== void 0 && { proxy: options.proxy },
1272
1838
  ...options?.insecure !== void 0 && { insecure: options.insecure },
1273
1839
  ...options?.poolIdleTimeout !== void 0 && { poolIdleTimeout: options.poolIdleTimeout },
@@ -1275,7 +1841,9 @@ async function createTransport(options) {
1275
1841
  ...options?.poolMaxSize !== void 0 && { poolMaxSize: options.poolMaxSize },
1276
1842
  ...options?.connectTimeout !== void 0 && { connectTimeout: options.connectTimeout },
1277
1843
  ...options?.readTimeout !== void 0 && { readTimeout: options.readTimeout }
1278
- });
1844
+ };
1845
+ applyNativeEmulationMode(transportOptions, mode);
1846
+ const id = nativeBinding.createTransport(transportOptions);
1279
1847
  return new Transport(id);
1280
1848
  } catch (error) {
1281
1849
  throw new RequestError(String(error));
@@ -1283,17 +1851,15 @@ async function createTransport(options) {
1283
1851
  }
1284
1852
  async function createSession(options) {
1285
1853
  const { sessionId, defaults } = normalizeSessionOptions(options);
1286
- validateBrowserProfile(defaults.browser);
1287
- validateOperatingSystem(defaults.os);
1288
1854
  let createdId;
1289
1855
  let transportId;
1290
1856
  try {
1291
- transportId = nativeBinding.createTransport({
1292
- browser: defaults.browser,
1293
- os: defaults.os,
1857
+ const transportOptions = {
1294
1858
  ...defaults.proxy !== void 0 && { proxy: defaults.proxy },
1295
1859
  ...defaults.insecure !== void 0 && { insecure: defaults.insecure }
1296
- });
1860
+ };
1861
+ applyNativeEmulationMode(transportOptions, defaults.transportMode);
1862
+ transportId = nativeBinding.createTransport(transportOptions);
1297
1863
  } catch (error) {
1298
1864
  throw new RequestError(String(error));
1299
1865
  }
@@ -1342,6 +1908,9 @@ async function request(options) {
1342
1908
  if (rest.os !== void 0) {
1343
1909
  init.os = rest.os;
1344
1910
  }
1911
+ if (rest.emulation !== void 0) {
1912
+ init.emulation = rest.emulation;
1913
+ }
1345
1914
  if (rest.proxy !== void 0) {
1346
1915
  init.proxy = rest.proxy;
1347
1916
  }
@@ -1475,6 +2044,9 @@ function normalizeStandaloneWebSocketOptions(options) {
1475
2044
  if (options.os !== void 0) {
1476
2045
  normalized.os = options.os;
1477
2046
  }
2047
+ if (options.emulation !== void 0) {
2048
+ normalized.emulation = options.emulation;
2049
+ }
1478
2050
  if (options.headers !== void 0) {
1479
2051
  normalized.headers = options.headers;
1480
2052
  }
@@ -1506,6 +2078,11 @@ function normalizeSessionWebSocketOptions(options) {
1506
2078
  if (optionsWithOverrides.os !== void 0) {
1507
2079
  throw new RequestError("`os` is not supported in session.websocket(); the session controls OS emulation.");
1508
2080
  }
2081
+ if (optionsWithOverrides.emulation !== void 0) {
2082
+ throw new RequestError(
2083
+ "`emulation` is not supported in session.websocket(); the session transport controls emulation."
2084
+ );
2085
+ }
1509
2086
  if (optionsWithOverrides.proxy !== void 0) {
1510
2087
  throw new RequestError("`proxy` is not supported in session.websocket(); the session transport controls proxying.");
1511
2088
  }
@@ -1671,10 +2248,11 @@ var WebSocket = class _WebSocket {
1671
2248
  typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols
1672
2249
  );
1673
2250
  assertNoManualWebSocketProtocolHeader(normalizedOptions.headers);
1674
- validateBrowserProfile(normalizedOptions.browser);
1675
- const os = normalizedOptions.os ?? DEFAULT_OS;
1676
- validateOperatingSystem(os);
1677
- const browser = normalizedOptions.browser ?? DEFAULT_BROWSER;
2251
+ const emulationMode = resolveEmulationMode(
2252
+ normalizedOptions.browser,
2253
+ normalizedOptions.os,
2254
+ normalizedOptions.emulation
2255
+ );
1678
2256
  const protocols = normalizeWebSocketProtocolList(
1679
2257
  typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols
1680
2258
  );
@@ -1683,17 +2261,19 @@ var WebSocket = class _WebSocket {
1683
2261
  url: normalizeWebSocketUrl(url),
1684
2262
  options: normalizedOptions,
1685
2263
  openDispatchMode: "automatic",
1686
- connect: (callbacks) => nativeBinding.websocketConnect({
1687
- url: normalizeWebSocketUrl(url),
1688
- browser,
1689
- os,
1690
- headers: headersToTuples(normalizedOptions.headers ?? {}),
1691
- ...protocols && protocols.length > 0 && { protocols },
1692
- ...normalizedOptions.proxy !== void 0 && { proxy: normalizedOptions.proxy },
1693
- onMessage: callbacks.onMessage,
1694
- onClose: callbacks.onClose,
1695
- onError: callbacks.onError
1696
- }),
2264
+ connect: (callbacks) => {
2265
+ const nativeOptions = {
2266
+ url: normalizeWebSocketUrl(url),
2267
+ headers: headersToTuples(normalizedOptions.headers ?? {}),
2268
+ ...protocols && protocols.length > 0 && { protocols },
2269
+ ...normalizedOptions.proxy !== void 0 && { proxy: normalizedOptions.proxy },
2270
+ onMessage: callbacks.onMessage,
2271
+ onClose: callbacks.onClose,
2272
+ onError: callbacks.onError
2273
+ };
2274
+ applyNativeEmulationMode(nativeOptions, emulationMode);
2275
+ return nativeBinding.websocketConnect(nativeOptions);
2276
+ },
1697
2277
  legacyCallbacks: extractLegacyWebSocketCallbacks(optionsCandidate)
1698
2278
  };
1699
2279
  }
@@ -2087,27 +2667,30 @@ async function websocket(urlOrOptions, options) {
2087
2667
  const normalized = normalizeStandaloneWebSocketArgs(urlOrOptions, options);
2088
2668
  validateWebSocketProtocols(normalized.options.protocols);
2089
2669
  assertNoManualWebSocketProtocolHeader(normalized.options.headers);
2090
- validateBrowserProfile(normalized.options.browser);
2091
- const os = normalized.options.os ?? DEFAULT_OS;
2092
- validateOperatingSystem(os);
2093
- const browser = normalized.options.browser ?? DEFAULT_BROWSER;
2670
+ const emulationMode = resolveEmulationMode(
2671
+ normalized.options.browser,
2672
+ normalized.options.os,
2673
+ normalized.options.emulation
2674
+ );
2094
2675
  const protocols = normalizeWebSocketProtocolList(normalized.options.protocols);
2095
2676
  return WebSocket._connectWithInit({
2096
2677
  _internal: true,
2097
2678
  url: normalized.url,
2098
2679
  options: normalized.options,
2099
2680
  openDispatchMode: "deferred",
2100
- connect: (callbacks) => nativeBinding.websocketConnect({
2101
- url: normalized.url,
2102
- browser,
2103
- os,
2104
- headers: headersToTuples(normalized.options.headers ?? {}),
2105
- ...protocols && protocols.length > 0 && { protocols },
2106
- ...normalized.options.proxy !== void 0 && { proxy: normalized.options.proxy },
2107
- onMessage: callbacks.onMessage,
2108
- onClose: callbacks.onClose,
2109
- onError: callbacks.onError
2110
- }),
2681
+ connect: (callbacks) => {
2682
+ const nativeOptions = {
2683
+ url: normalized.url,
2684
+ headers: headersToTuples(normalized.options.headers ?? {}),
2685
+ ...protocols && protocols.length > 0 && { protocols },
2686
+ ...normalized.options.proxy !== void 0 && { proxy: normalized.options.proxy },
2687
+ onMessage: callbacks.onMessage,
2688
+ onClose: callbacks.onClose,
2689
+ onError: callbacks.onError
2690
+ };
2691
+ applyNativeEmulationMode(nativeOptions, emulationMode);
2692
+ return nativeBinding.websocketConnect(nativeOptions);
2693
+ },
2111
2694
  legacyCallbacks: normalized.legacyCallbacks
2112
2695
  });
2113
2696
  }