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.js CHANGED
@@ -39,6 +39,24 @@ function detectLibc() {
39
39
  }
40
40
  }
41
41
  var require2 = typeof import.meta !== "undefined" && import.meta.url ? createRequire(import.meta.url) : createRequire(__filename);
42
+ function requirePlatformBinary(platformArch) {
43
+ switch (platformArch) {
44
+ case "darwin-x64":
45
+ return require2("../rust/wreq-js.darwin-x64.node");
46
+ case "darwin-arm64":
47
+ return require2("../rust/wreq-js.darwin-arm64.node");
48
+ case "linux-x64-gnu":
49
+ return require2("../rust/wreq-js.linux-x64-gnu.node");
50
+ case "linux-x64-musl":
51
+ return require2("../rust/wreq-js.linux-x64-musl.node");
52
+ case "linux-arm64-gnu":
53
+ return require2("../rust/wreq-js.linux-arm64-gnu.node");
54
+ case "win32-x64-msvc":
55
+ return require2("../rust/wreq-js.win32-x64-msvc.node");
56
+ default:
57
+ return void 0;
58
+ }
59
+ }
42
60
  function loadNativeBinding() {
43
61
  const platform = process.platform;
44
62
  const arch = process.arch;
@@ -60,15 +78,18 @@ function loadNativeBinding() {
60
78
  }
61
79
  const binaryName = `wreq-js.${platformArch}.node`;
62
80
  try {
63
- return require2(`../rust/${binaryName}`);
64
- } catch {
65
- try {
66
- return require2("../rust/wreq-js.node");
67
- } catch {
68
- throw new Error(
69
- `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.`
70
- );
81
+ const platformBinding = requirePlatformBinary(platformArch);
82
+ if (platformBinding) {
83
+ return platformBinding;
71
84
  }
85
+ } catch {
86
+ }
87
+ try {
88
+ return require2("../rust/wreq-js.node");
89
+ } catch {
90
+ throw new Error(
91
+ `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.`
92
+ );
72
93
  }
73
94
  }
74
95
  nativeBinding = loadNativeBinding();
@@ -100,8 +121,7 @@ function generateSessionId() {
100
121
  function normalizeSessionOptions(options) {
101
122
  const sessionId = options?.sessionId ?? generateSessionId();
102
123
  const defaults = {
103
- browser: options?.browser ?? DEFAULT_BROWSER,
104
- os: options?.os ?? DEFAULT_OS
124
+ transportMode: resolveEmulationMode(options?.browser, options?.os, options?.emulation)
105
125
  };
106
126
  if (options?.proxy !== void 0) {
107
127
  defaults.proxy = options.proxy;
@@ -825,21 +845,26 @@ function resolveTransportContext(config, sessionDefaults) {
825
845
  throw new RequestError("Transport has been closed");
826
846
  }
827
847
  const hasProxy = config.proxy !== void 0;
828
- if (config.browser !== void 0 || config.os !== void 0 || hasProxy || config.insecure !== void 0) {
829
- throw new RequestError("`transport` cannot be combined with browser/os/proxy/insecure options");
848
+ if (config.browser !== void 0 || config.os !== void 0 || config.emulation !== void 0 || hasProxy || config.insecure !== void 0) {
849
+ throw new RequestError("`transport` cannot be combined with browser/os/emulation/proxy/insecure options");
830
850
  }
831
851
  return { transportId: config.transport.id };
832
852
  }
833
853
  if (sessionDefaults?.transportId) {
854
+ if (config.emulation !== void 0) {
855
+ throw new RequestError("Session emulation cannot be changed after creation");
856
+ }
834
857
  if (config.browser !== void 0) {
835
858
  validateBrowserProfile(config.browser);
836
- if (config.browser !== sessionDefaults.browser) {
859
+ const lockedBrowser = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.browser;
860
+ if (config.browser !== lockedBrowser) {
837
861
  throw new RequestError("Session browser cannot be changed after creation");
838
862
  }
839
863
  }
840
864
  if (config.os !== void 0) {
841
865
  validateOperatingSystem(config.os);
842
- if (config.os !== sessionDefaults.os) {
866
+ const lockedOs = sessionDefaults.transportMode.kind === "custom" ? void 0 : sessionDefaults.transportMode.os;
867
+ if (config.os !== lockedOs) {
843
868
  throw new RequestError("Session operating system cannot be changed after creation");
844
869
  }
845
870
  }
@@ -856,11 +881,9 @@ function resolveTransportContext(config, sessionDefaults) {
856
881
  }
857
882
  return { transportId: sessionDefaults.transportId };
858
883
  }
859
- const browser = config.browser ?? DEFAULT_BROWSER;
860
- const os = config.os ?? DEFAULT_OS;
861
- validateBrowserProfile(browser);
862
- validateOperatingSystem(os);
863
- const resolved = { browser, os };
884
+ const resolved = {
885
+ mode: resolveEmulationMode(config.browser, config.os, config.emulation)
886
+ };
864
887
  if (config.proxy !== void 0) {
865
888
  resolved.proxy = config.proxy;
866
889
  }
@@ -1105,6 +1128,553 @@ function validatePositiveInteger(value, label) {
1105
1128
  throw new RequestError(`${label} must be greater than 0`);
1106
1129
  }
1107
1130
  }
1131
+ function validateIntegerInRange(value, min, max, label) {
1132
+ validateNonNegativeInteger(value, label);
1133
+ if (value < min || value > max) {
1134
+ throw new RequestError(`${label} must be between ${min} and ${max}`);
1135
+ }
1136
+ }
1137
+ var SUPPORTED_ALPN_PROTOCOLS = /* @__PURE__ */ new Set(["HTTP1", "HTTP2", "HTTP3"]);
1138
+ var SUPPORTED_ALPS_PROTOCOLS = /* @__PURE__ */ new Set(["HTTP1", "HTTP2", "HTTP3"]);
1139
+ var SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS = /* @__PURE__ */ new Set(["zlib", "brotli", "zstd"]);
1140
+ var HTTP2_SETTING_IDS = /* @__PURE__ */ new Set([
1141
+ "HeaderTableSize",
1142
+ "EnablePush",
1143
+ "MaxConcurrentStreams",
1144
+ "InitialWindowSize",
1145
+ "MaxFrameSize",
1146
+ "MaxHeaderListSize",
1147
+ "EnableConnectProtocol",
1148
+ "NoRfc7540Priorities"
1149
+ ]);
1150
+ var HTTP2_PSEUDO_HEADER_IDS = /* @__PURE__ */ new Set(["Method", "Scheme", "Authority", "Path", "Protocol"]);
1151
+ var STANDARD_HTTP2_SETTING_ID_VALUES = /* @__PURE__ */ new Set([1, 2, 3, 4, 5, 6, 8, 9]);
1152
+ var MAX_HTTP2_EXPERIMENTAL_SETTING_ID = 15;
1153
+ var TLS_VERSION_ALIASES = /* @__PURE__ */ new Map([
1154
+ ["1.0", "1.0"],
1155
+ ["1.1", "1.1"],
1156
+ ["1.2", "1.2"],
1157
+ ["1.3", "1.3"],
1158
+ ["tls1.0", "1.0"],
1159
+ ["tls1.1", "1.1"],
1160
+ ["tls1.2", "1.2"],
1161
+ ["tls1.3", "1.3"]
1162
+ ]);
1163
+ function isNonEmpty(value) {
1164
+ for (const _ in value) return true;
1165
+ return false;
1166
+ }
1167
+ function normalizeProtocolList(value, label, allowed) {
1168
+ if (value === void 0) {
1169
+ return void 0;
1170
+ }
1171
+ if (!Array.isArray(value)) {
1172
+ throw new RequestError(`${label} must be an array`);
1173
+ }
1174
+ for (const protocol of value) {
1175
+ if (!allowed.has(protocol)) {
1176
+ throw new RequestError(`${label} values must be one of: HTTP1, HTTP2, HTTP3`);
1177
+ }
1178
+ }
1179
+ return [...value];
1180
+ }
1181
+ function normalizeTlsVersion(value, label) {
1182
+ if (value === void 0) {
1183
+ return void 0;
1184
+ }
1185
+ if (typeof value !== "string") {
1186
+ throw new RequestError(`${label} must be a string`);
1187
+ }
1188
+ const normalized = TLS_VERSION_ALIASES.get(value.trim().toLowerCase());
1189
+ if (!normalized) {
1190
+ throw new RequestError(`${label} must be one of: 1.0, 1.1, 1.2, 1.3`);
1191
+ }
1192
+ return normalized;
1193
+ }
1194
+ function normalizeOrigHeaders(origHeaders) {
1195
+ if (origHeaders === void 0) {
1196
+ return void 0;
1197
+ }
1198
+ if (!Array.isArray(origHeaders)) {
1199
+ throw new RequestError("emulation.origHeaders must be an array of strings");
1200
+ }
1201
+ const normalized = [];
1202
+ const seen = /* @__PURE__ */ new Set();
1203
+ for (const entry of origHeaders) {
1204
+ if (typeof entry !== "string" || entry.trim().length === 0) {
1205
+ throw new RequestError("emulation.origHeaders entries must be non-empty strings");
1206
+ }
1207
+ const trimmed = entry.trim();
1208
+ const duplicateKey = trimmed.toLowerCase();
1209
+ if (seen.has(duplicateKey)) {
1210
+ throw new RequestError(`Duplicate emulation.origHeaders entry: ${trimmed}`);
1211
+ }
1212
+ seen.add(duplicateKey);
1213
+ normalized.push(trimmed);
1214
+ }
1215
+ return normalized.length > 0 ? normalized : void 0;
1216
+ }
1217
+ function normalizeCustomTlsOptions(options) {
1218
+ if (options === void 0) {
1219
+ return void 0;
1220
+ }
1221
+ const normalized = {};
1222
+ const alpnProtocols = normalizeProtocolList(
1223
+ options.alpnProtocols,
1224
+ "emulation.tlsOptions.alpnProtocols",
1225
+ SUPPORTED_ALPN_PROTOCOLS
1226
+ );
1227
+ if (alpnProtocols !== void 0) {
1228
+ normalized.alpnProtocols = alpnProtocols;
1229
+ }
1230
+ const alpsProtocols = normalizeProtocolList(
1231
+ options.alpsProtocols,
1232
+ "emulation.tlsOptions.alpsProtocols",
1233
+ SUPPORTED_ALPS_PROTOCOLS
1234
+ );
1235
+ if (alpsProtocols !== void 0) {
1236
+ normalized.alpsProtocols = alpsProtocols;
1237
+ }
1238
+ const minTlsVersion = normalizeTlsVersion(options.minTlsVersion, "emulation.tlsOptions.minTlsVersion");
1239
+ if (minTlsVersion !== void 0) {
1240
+ normalized.minTlsVersion = minTlsVersion;
1241
+ }
1242
+ const maxTlsVersion = normalizeTlsVersion(options.maxTlsVersion, "emulation.tlsOptions.maxTlsVersion");
1243
+ if (maxTlsVersion !== void 0) {
1244
+ normalized.maxTlsVersion = maxTlsVersion;
1245
+ }
1246
+ if (options.alpsUseNewCodepoint !== void 0) {
1247
+ normalized.alpsUseNewCodepoint = options.alpsUseNewCodepoint;
1248
+ }
1249
+ if (options.sessionTicket !== void 0) {
1250
+ normalized.sessionTicket = options.sessionTicket;
1251
+ }
1252
+ if (options.preSharedKey !== void 0) {
1253
+ normalized.preSharedKey = options.preSharedKey;
1254
+ }
1255
+ if (options.enableEchGrease !== void 0) {
1256
+ normalized.enableEchGrease = options.enableEchGrease;
1257
+ }
1258
+ if (options.permuteExtensions !== void 0) {
1259
+ normalized.permuteExtensions = options.permuteExtensions;
1260
+ }
1261
+ if (options.greaseEnabled !== void 0) {
1262
+ normalized.greaseEnabled = options.greaseEnabled;
1263
+ }
1264
+ if (options.enableOcspStapling !== void 0) {
1265
+ normalized.enableOcspStapling = options.enableOcspStapling;
1266
+ }
1267
+ if (options.enableSignedCertTimestamps !== void 0) {
1268
+ normalized.enableSignedCertTimestamps = options.enableSignedCertTimestamps;
1269
+ }
1270
+ if (options.pskSkipSessionTicket !== void 0) {
1271
+ normalized.pskSkipSessionTicket = options.pskSkipSessionTicket;
1272
+ }
1273
+ if (options.pskDheKe !== void 0) {
1274
+ normalized.pskDheKe = options.pskDheKe;
1275
+ }
1276
+ if (options.renegotiation !== void 0) {
1277
+ normalized.renegotiation = options.renegotiation;
1278
+ }
1279
+ if (options.aesHwOverride !== void 0) {
1280
+ normalized.aesHwOverride = options.aesHwOverride;
1281
+ }
1282
+ if (options.preserveTls13CipherList !== void 0) {
1283
+ normalized.preserveTls13CipherList = options.preserveTls13CipherList;
1284
+ }
1285
+ if (options.randomAesHwOverride !== void 0) {
1286
+ normalized.randomAesHwOverride = options.randomAesHwOverride;
1287
+ }
1288
+ if (options.delegatedCredentials !== void 0) {
1289
+ normalized.delegatedCredentials = options.delegatedCredentials;
1290
+ }
1291
+ if (options.curvesList !== void 0) {
1292
+ normalized.curvesList = options.curvesList;
1293
+ }
1294
+ if (options.cipherList !== void 0) {
1295
+ normalized.cipherList = options.cipherList;
1296
+ }
1297
+ if (options.sigalgsList !== void 0) {
1298
+ normalized.sigalgsList = options.sigalgsList;
1299
+ }
1300
+ if (options.recordSizeLimit !== void 0) {
1301
+ validateIntegerInRange(options.recordSizeLimit, 0, 65535, "emulation.tlsOptions.recordSizeLimit");
1302
+ normalized.recordSizeLimit = options.recordSizeLimit;
1303
+ }
1304
+ if (options.keySharesLimit !== void 0) {
1305
+ validateIntegerInRange(options.keySharesLimit, 0, 255, "emulation.tlsOptions.keySharesLimit");
1306
+ normalized.keySharesLimit = options.keySharesLimit;
1307
+ }
1308
+ if (options.certificateCompressionAlgorithms !== void 0) {
1309
+ if (!Array.isArray(options.certificateCompressionAlgorithms)) {
1310
+ throw new RequestError("emulation.tlsOptions.certificateCompressionAlgorithms must be an array");
1311
+ }
1312
+ const algorithms = [];
1313
+ const seen = /* @__PURE__ */ new Set();
1314
+ for (const algorithm of options.certificateCompressionAlgorithms) {
1315
+ if (!SUPPORTED_CERTIFICATE_COMPRESSION_ALGORITHMS.has(algorithm)) {
1316
+ throw new RequestError(
1317
+ "emulation.tlsOptions.certificateCompressionAlgorithms values must be one of: zlib, brotli, zstd"
1318
+ );
1319
+ }
1320
+ if (seen.has(algorithm)) {
1321
+ throw new RequestError(`Duplicate emulation.tlsOptions.certificateCompressionAlgorithms entry: ${algorithm}`);
1322
+ }
1323
+ seen.add(algorithm);
1324
+ algorithms.push(algorithm);
1325
+ }
1326
+ normalized.certificateCompressionAlgorithms = algorithms;
1327
+ }
1328
+ if (options.extensionPermutation !== void 0) {
1329
+ if (!Array.isArray(options.extensionPermutation)) {
1330
+ throw new RequestError("emulation.tlsOptions.extensionPermutation must be an array");
1331
+ }
1332
+ const permutation = [];
1333
+ const seen = /* @__PURE__ */ new Set();
1334
+ for (const extensionId of options.extensionPermutation) {
1335
+ validateIntegerInRange(extensionId, 0, 65535, "emulation.tlsOptions.extensionPermutation");
1336
+ if (seen.has(extensionId)) {
1337
+ throw new RequestError(`Duplicate emulation.tlsOptions.extensionPermutation entry: ${extensionId}`);
1338
+ }
1339
+ seen.add(extensionId);
1340
+ permutation.push(extensionId);
1341
+ }
1342
+ normalized.extensionPermutation = permutation;
1343
+ }
1344
+ return isNonEmpty(normalized) ? normalized : void 0;
1345
+ }
1346
+ function normalizeCustomHttp1Options(options) {
1347
+ if (options === void 0) {
1348
+ return void 0;
1349
+ }
1350
+ const normalized = {};
1351
+ if (options.http09Responses !== void 0) {
1352
+ normalized.http09Responses = options.http09Responses;
1353
+ }
1354
+ if (options.writev !== void 0) {
1355
+ normalized.writev = options.writev;
1356
+ }
1357
+ if (options.ignoreInvalidHeadersInResponses !== void 0) {
1358
+ normalized.ignoreInvalidHeadersInResponses = options.ignoreInvalidHeadersInResponses;
1359
+ }
1360
+ if (options.allowSpacesAfterHeaderNameInResponses !== void 0) {
1361
+ normalized.allowSpacesAfterHeaderNameInResponses = options.allowSpacesAfterHeaderNameInResponses;
1362
+ }
1363
+ if (options.allowObsoleteMultilineHeadersInResponses !== void 0) {
1364
+ normalized.allowObsoleteMultilineHeadersInResponses = options.allowObsoleteMultilineHeadersInResponses;
1365
+ }
1366
+ if (options.maxHeaders !== void 0) {
1367
+ validateNonNegativeInteger(options.maxHeaders, "emulation.http1Options.maxHeaders");
1368
+ normalized.maxHeaders = options.maxHeaders;
1369
+ }
1370
+ if (options.readBufExactSize !== void 0) {
1371
+ validateNonNegativeInteger(options.readBufExactSize, "emulation.http1Options.readBufExactSize");
1372
+ normalized.readBufExactSize = options.readBufExactSize;
1373
+ }
1374
+ if (options.maxBufSize !== void 0) {
1375
+ validateNonNegativeInteger(options.maxBufSize, "emulation.http1Options.maxBufSize");
1376
+ if (options.maxBufSize < 8192) {
1377
+ throw new RequestError("emulation.http1Options.maxBufSize must be greater than or equal to 8192");
1378
+ }
1379
+ normalized.maxBufSize = options.maxBufSize;
1380
+ }
1381
+ if (normalized.readBufExactSize !== void 0 && normalized.maxBufSize !== void 0) {
1382
+ throw new RequestError("emulation.http1Options.readBufExactSize and maxBufSize cannot both be set");
1383
+ }
1384
+ return isNonEmpty(normalized) ? normalized : void 0;
1385
+ }
1386
+ function normalizeHttp2StreamDependency(dependency, label) {
1387
+ if (!isPlainObject(dependency)) {
1388
+ throw new RequestError(`${label} must be an object`);
1389
+ }
1390
+ validateIntegerInRange(dependency.dependencyId, 0, 2147483647, `${label}.dependencyId`);
1391
+ validateIntegerInRange(dependency.weight, 0, 255, `${label}.weight`);
1392
+ const normalized = {
1393
+ dependencyId: dependency.dependencyId,
1394
+ weight: dependency.weight
1395
+ };
1396
+ if (dependency.exclusive !== void 0) {
1397
+ normalized.exclusive = dependency.exclusive;
1398
+ }
1399
+ return normalized;
1400
+ }
1401
+ function normalizeCustomHttp2Options(options) {
1402
+ if (options === void 0) {
1403
+ return void 0;
1404
+ }
1405
+ const normalized = {};
1406
+ if (options.adaptiveWindow !== void 0) {
1407
+ normalized.adaptiveWindow = options.adaptiveWindow;
1408
+ }
1409
+ if (options.keepAliveWhileIdle !== void 0) {
1410
+ normalized.keepAliveWhileIdle = options.keepAliveWhileIdle;
1411
+ }
1412
+ if (options.enablePush !== void 0) {
1413
+ normalized.enablePush = options.enablePush;
1414
+ }
1415
+ if (options.enableConnectProtocol !== void 0) {
1416
+ normalized.enableConnectProtocol = options.enableConnectProtocol;
1417
+ }
1418
+ if (options.noRfc7540Priorities !== void 0) {
1419
+ normalized.noRfc7540Priorities = options.noRfc7540Priorities;
1420
+ }
1421
+ if (options.initialStreamId !== void 0) {
1422
+ validateNonNegativeInteger(options.initialStreamId, "emulation.http2Options.initialStreamId");
1423
+ normalized.initialStreamId = options.initialStreamId;
1424
+ }
1425
+ if (options.initialConnectionWindowSize !== void 0) {
1426
+ validateNonNegativeInteger(
1427
+ options.initialConnectionWindowSize,
1428
+ "emulation.http2Options.initialConnectionWindowSize"
1429
+ );
1430
+ normalized.initialConnectionWindowSize = options.initialConnectionWindowSize;
1431
+ }
1432
+ if (options.initialWindowSize !== void 0) {
1433
+ validateNonNegativeInteger(options.initialWindowSize, "emulation.http2Options.initialWindowSize");
1434
+ normalized.initialWindowSize = options.initialWindowSize;
1435
+ }
1436
+ if (options.initialMaxSendStreams !== void 0) {
1437
+ validateNonNegativeInteger(options.initialMaxSendStreams, "emulation.http2Options.initialMaxSendStreams");
1438
+ normalized.initialMaxSendStreams = options.initialMaxSendStreams;
1439
+ }
1440
+ if (options.maxFrameSize !== void 0) {
1441
+ validateNonNegativeInteger(options.maxFrameSize, "emulation.http2Options.maxFrameSize");
1442
+ normalized.maxFrameSize = options.maxFrameSize;
1443
+ }
1444
+ if (options.keepAliveInterval !== void 0) {
1445
+ validateNonNegativeInteger(options.keepAliveInterval, "emulation.http2Options.keepAliveInterval");
1446
+ normalized.keepAliveInterval = options.keepAliveInterval;
1447
+ }
1448
+ if (options.keepAliveTimeout !== void 0) {
1449
+ validateNonNegativeInteger(options.keepAliveTimeout, "emulation.http2Options.keepAliveTimeout");
1450
+ normalized.keepAliveTimeout = options.keepAliveTimeout;
1451
+ }
1452
+ if (options.maxConcurrentResetStreams !== void 0) {
1453
+ validateNonNegativeInteger(options.maxConcurrentResetStreams, "emulation.http2Options.maxConcurrentResetStreams");
1454
+ normalized.maxConcurrentResetStreams = options.maxConcurrentResetStreams;
1455
+ }
1456
+ if (options.maxSendBufferSize !== void 0) {
1457
+ validateNonNegativeInteger(options.maxSendBufferSize, "emulation.http2Options.maxSendBufferSize");
1458
+ normalized.maxSendBufferSize = options.maxSendBufferSize;
1459
+ }
1460
+ if (options.maxConcurrentStreams !== void 0) {
1461
+ validateNonNegativeInteger(options.maxConcurrentStreams, "emulation.http2Options.maxConcurrentStreams");
1462
+ normalized.maxConcurrentStreams = options.maxConcurrentStreams;
1463
+ }
1464
+ if (options.maxHeaderListSize !== void 0) {
1465
+ validateNonNegativeInteger(options.maxHeaderListSize, "emulation.http2Options.maxHeaderListSize");
1466
+ normalized.maxHeaderListSize = options.maxHeaderListSize;
1467
+ }
1468
+ if (options.maxPendingAcceptResetStreams !== void 0) {
1469
+ validateNonNegativeInteger(
1470
+ options.maxPendingAcceptResetStreams,
1471
+ "emulation.http2Options.maxPendingAcceptResetStreams"
1472
+ );
1473
+ normalized.maxPendingAcceptResetStreams = options.maxPendingAcceptResetStreams;
1474
+ }
1475
+ if (options.headerTableSize !== void 0) {
1476
+ validateNonNegativeInteger(options.headerTableSize, "emulation.http2Options.headerTableSize");
1477
+ normalized.headerTableSize = options.headerTableSize;
1478
+ }
1479
+ if (options.settingsOrder !== void 0) {
1480
+ if (!Array.isArray(options.settingsOrder)) {
1481
+ throw new RequestError("emulation.http2Options.settingsOrder must be an array");
1482
+ }
1483
+ const settingsOrder = [];
1484
+ const seen = /* @__PURE__ */ new Set();
1485
+ for (const settingId of options.settingsOrder) {
1486
+ if (!HTTP2_SETTING_IDS.has(settingId)) {
1487
+ throw new RequestError("emulation.http2Options.settingsOrder contains an unsupported setting id");
1488
+ }
1489
+ if (seen.has(settingId)) {
1490
+ throw new RequestError(`Duplicate emulation.http2Options.settingsOrder entry: ${settingId}`);
1491
+ }
1492
+ seen.add(settingId);
1493
+ settingsOrder.push(settingId);
1494
+ }
1495
+ normalized.settingsOrder = settingsOrder;
1496
+ }
1497
+ if (options.headersPseudoOrder !== void 0) {
1498
+ if (!Array.isArray(options.headersPseudoOrder)) {
1499
+ throw new RequestError("emulation.http2Options.headersPseudoOrder must be an array");
1500
+ }
1501
+ const headersPseudoOrder = [];
1502
+ const seenPseudo = /* @__PURE__ */ new Set();
1503
+ for (const pseudoId of options.headersPseudoOrder) {
1504
+ if (!HTTP2_PSEUDO_HEADER_IDS.has(pseudoId)) {
1505
+ throw new RequestError("emulation.http2Options.headersPseudoOrder contains an unsupported pseudo-header id");
1506
+ }
1507
+ if (seenPseudo.has(pseudoId)) {
1508
+ throw new RequestError(`Duplicate emulation.http2Options.headersPseudoOrder entry: ${pseudoId}`);
1509
+ }
1510
+ seenPseudo.add(pseudoId);
1511
+ headersPseudoOrder.push(pseudoId);
1512
+ }
1513
+ normalized.headersPseudoOrder = headersPseudoOrder;
1514
+ }
1515
+ if (options.headersStreamDependency !== void 0) {
1516
+ normalized.headersStreamDependency = normalizeHttp2StreamDependency(
1517
+ options.headersStreamDependency,
1518
+ "emulation.http2Options.headersStreamDependency"
1519
+ );
1520
+ }
1521
+ if (options.priorities !== void 0) {
1522
+ if (!Array.isArray(options.priorities)) {
1523
+ throw new RequestError("emulation.http2Options.priorities must be an array");
1524
+ }
1525
+ const priorities = [];
1526
+ const seenStreamIds = /* @__PURE__ */ new Set();
1527
+ for (const [index, priority] of options.priorities.entries()) {
1528
+ if (!isPlainObject(priority)) {
1529
+ throw new RequestError(`emulation.http2Options.priorities[${index}] must be an object`);
1530
+ }
1531
+ validatePositiveInteger(priority.streamId, `emulation.http2Options.priorities[${index}].streamId`);
1532
+ if (seenStreamIds.has(priority.streamId)) {
1533
+ throw new RequestError(`Duplicate emulation.http2Options.priorities streamId: ${priority.streamId}`);
1534
+ }
1535
+ seenStreamIds.add(priority.streamId);
1536
+ priorities.push({
1537
+ streamId: priority.streamId,
1538
+ dependency: normalizeHttp2StreamDependency(
1539
+ priority.dependency,
1540
+ `emulation.http2Options.priorities[${index}].dependency`
1541
+ )
1542
+ });
1543
+ }
1544
+ normalized.priorities = priorities;
1545
+ }
1546
+ if (options.experimentalSettings !== void 0) {
1547
+ if (!Array.isArray(options.experimentalSettings)) {
1548
+ throw new RequestError("emulation.http2Options.experimentalSettings must be an array");
1549
+ }
1550
+ const experimentalSettings = [];
1551
+ const seenIds = /* @__PURE__ */ new Set();
1552
+ for (const [index, setting] of options.experimentalSettings.entries()) {
1553
+ if (!isPlainObject(setting)) {
1554
+ throw new RequestError(`emulation.http2Options.experimentalSettings[${index}] must be an object`);
1555
+ }
1556
+ validateIntegerInRange(
1557
+ setting.id,
1558
+ 1,
1559
+ MAX_HTTP2_EXPERIMENTAL_SETTING_ID,
1560
+ `emulation.http2Options.experimentalSettings[${index}].id`
1561
+ );
1562
+ if (STANDARD_HTTP2_SETTING_ID_VALUES.has(setting.id)) {
1563
+ throw new RequestError(
1564
+ `emulation.http2Options.experimentalSettings[${index}].id must not be a standard HTTP/2 setting id`
1565
+ );
1566
+ }
1567
+ if (seenIds.has(setting.id)) {
1568
+ throw new RequestError(`Duplicate emulation.http2Options.experimentalSettings id: ${setting.id}`);
1569
+ }
1570
+ seenIds.add(setting.id);
1571
+ validateIntegerInRange(
1572
+ setting.value,
1573
+ 0,
1574
+ 4294967295,
1575
+ `emulation.http2Options.experimentalSettings[${index}].value`
1576
+ );
1577
+ experimentalSettings.push({
1578
+ id: setting.id,
1579
+ value: setting.value
1580
+ });
1581
+ }
1582
+ normalized.experimentalSettings = experimentalSettings;
1583
+ }
1584
+ return isNonEmpty(normalized) ? normalized : void 0;
1585
+ }
1586
+ function normalizeCustomEmulationOptions(emulation, allowEmpty) {
1587
+ if (emulation === void 0) {
1588
+ return void 0;
1589
+ }
1590
+ if (!isPlainObject(emulation)) {
1591
+ throw new RequestError("emulation must be an object");
1592
+ }
1593
+ const source = emulation;
1594
+ const normalized = {};
1595
+ const tlsOptions = normalizeCustomTlsOptions(source.tlsOptions);
1596
+ if (tlsOptions !== void 0) {
1597
+ normalized.tlsOptions = tlsOptions;
1598
+ }
1599
+ const http1Options = normalizeCustomHttp1Options(source.http1Options);
1600
+ if (http1Options !== void 0) {
1601
+ normalized.http1Options = http1Options;
1602
+ }
1603
+ const http2Options = normalizeCustomHttp2Options(source.http2Options);
1604
+ if (http2Options !== void 0) {
1605
+ normalized.http2Options = http2Options;
1606
+ }
1607
+ if (source.headers !== void 0) {
1608
+ const headers = headersToTuples(source.headers);
1609
+ if (headers.length > 0) {
1610
+ normalized.headers = headers;
1611
+ }
1612
+ }
1613
+ const origHeaders = normalizeOrigHeaders(source.origHeaders);
1614
+ if (origHeaders !== void 0) {
1615
+ normalized.origHeaders = origHeaders;
1616
+ }
1617
+ if (!allowEmpty && !isNonEmpty(normalized)) {
1618
+ throw new RequestError(
1619
+ "Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders"
1620
+ );
1621
+ }
1622
+ return isNonEmpty(normalized) ? normalized : void 0;
1623
+ }
1624
+ function serializeCustomEmulationOptions(emulation, allowEmpty) {
1625
+ const normalized = normalizeCustomEmulationOptions(emulation, allowEmpty);
1626
+ return normalized ? JSON.stringify(normalized) : void 0;
1627
+ }
1628
+ function resolveEmulationMode(browser, os, emulation) {
1629
+ if (browser !== void 0) {
1630
+ validateBrowserProfile(browser);
1631
+ if (os !== void 0) {
1632
+ validateOperatingSystem(os);
1633
+ }
1634
+ const emulationJson = serializeCustomEmulationOptions(emulation, true);
1635
+ return {
1636
+ kind: "preset",
1637
+ browser,
1638
+ os: os ?? DEFAULT_OS,
1639
+ ...emulationJson !== void 0 && { emulationJson }
1640
+ };
1641
+ }
1642
+ if (os !== void 0) {
1643
+ validateOperatingSystem(os);
1644
+ const emulationJson = serializeCustomEmulationOptions(emulation, true);
1645
+ return {
1646
+ kind: "preset",
1647
+ browser: DEFAULT_BROWSER,
1648
+ os,
1649
+ ...emulationJson !== void 0 && { emulationJson }
1650
+ };
1651
+ }
1652
+ if (emulation !== void 0) {
1653
+ const emulationJson = serializeCustomEmulationOptions(emulation, false);
1654
+ if (emulationJson === void 0) {
1655
+ throw new RequestError(
1656
+ "Standalone custom emulation requires at least one of tlsOptions, http1Options, http2Options, headers, or origHeaders"
1657
+ );
1658
+ }
1659
+ return { kind: "custom", emulationJson };
1660
+ }
1661
+ return {
1662
+ kind: "preset",
1663
+ browser: DEFAULT_BROWSER,
1664
+ os: DEFAULT_OS
1665
+ };
1666
+ }
1667
+ function applyNativeEmulationMode(target, mode) {
1668
+ if (mode.kind === "custom") {
1669
+ target.emulationJson = mode.emulationJson;
1670
+ return;
1671
+ }
1672
+ target.browser = mode.browser;
1673
+ target.os = mode.os;
1674
+ if (mode.emulationJson !== void 0) {
1675
+ target.emulationJson = mode.emulationJson;
1676
+ }
1677
+ }
1108
1678
  async function dispatchRequest(options, requestUrl, signal) {
1109
1679
  if (!signal) {
1110
1680
  const requestId2 = generateRequestId();
@@ -1179,8 +1749,9 @@ async function fetch(input, init) {
1179
1749
  if (transport.transportId) {
1180
1750
  requestOptions.transportId = transport.transportId;
1181
1751
  } else {
1182
- requestOptions.browser = transport.browser ?? DEFAULT_BROWSER;
1183
- requestOptions.os = transport.os ?? DEFAULT_OS;
1752
+ if (transport.mode !== void 0) {
1753
+ applyNativeEmulationMode(requestOptions, transport.mode);
1754
+ }
1184
1755
  if (transport.proxy !== void 0) {
1185
1756
  requestOptions.proxy = transport.proxy;
1186
1757
  }
@@ -1204,10 +1775,7 @@ async function fetch(input, init) {
1204
1775
  return dispatchRequest(requestOptions, url, config.signal ?? null);
1205
1776
  }
1206
1777
  async function createTransport(options) {
1207
- const browser = options?.browser ?? DEFAULT_BROWSER;
1208
- const os = options?.os ?? DEFAULT_OS;
1209
- validateBrowserProfile(browser);
1210
- validateOperatingSystem(os);
1778
+ const mode = resolveEmulationMode(options?.browser, options?.os, options?.emulation);
1211
1779
  if (options?.poolIdleTimeout !== void 0) {
1212
1780
  validatePositiveNumber(options.poolIdleTimeout, "poolIdleTimeout");
1213
1781
  }
@@ -1224,9 +1792,7 @@ async function createTransport(options) {
1224
1792
  validatePositiveNumber(options.readTimeout, "readTimeout");
1225
1793
  }
1226
1794
  try {
1227
- const id = nativeBinding.createTransport({
1228
- browser,
1229
- os,
1795
+ const transportOptions = {
1230
1796
  ...options?.proxy !== void 0 && { proxy: options.proxy },
1231
1797
  ...options?.insecure !== void 0 && { insecure: options.insecure },
1232
1798
  ...options?.poolIdleTimeout !== void 0 && { poolIdleTimeout: options.poolIdleTimeout },
@@ -1234,7 +1800,9 @@ async function createTransport(options) {
1234
1800
  ...options?.poolMaxSize !== void 0 && { poolMaxSize: options.poolMaxSize },
1235
1801
  ...options?.connectTimeout !== void 0 && { connectTimeout: options.connectTimeout },
1236
1802
  ...options?.readTimeout !== void 0 && { readTimeout: options.readTimeout }
1237
- });
1803
+ };
1804
+ applyNativeEmulationMode(transportOptions, mode);
1805
+ const id = nativeBinding.createTransport(transportOptions);
1238
1806
  return new Transport(id);
1239
1807
  } catch (error) {
1240
1808
  throw new RequestError(String(error));
@@ -1242,17 +1810,15 @@ async function createTransport(options) {
1242
1810
  }
1243
1811
  async function createSession(options) {
1244
1812
  const { sessionId, defaults } = normalizeSessionOptions(options);
1245
- validateBrowserProfile(defaults.browser);
1246
- validateOperatingSystem(defaults.os);
1247
1813
  let createdId;
1248
1814
  let transportId;
1249
1815
  try {
1250
- transportId = nativeBinding.createTransport({
1251
- browser: defaults.browser,
1252
- os: defaults.os,
1816
+ const transportOptions = {
1253
1817
  ...defaults.proxy !== void 0 && { proxy: defaults.proxy },
1254
1818
  ...defaults.insecure !== void 0 && { insecure: defaults.insecure }
1255
- });
1819
+ };
1820
+ applyNativeEmulationMode(transportOptions, defaults.transportMode);
1821
+ transportId = nativeBinding.createTransport(transportOptions);
1256
1822
  } catch (error) {
1257
1823
  throw new RequestError(String(error));
1258
1824
  }
@@ -1301,6 +1867,9 @@ async function request(options) {
1301
1867
  if (rest.os !== void 0) {
1302
1868
  init.os = rest.os;
1303
1869
  }
1870
+ if (rest.emulation !== void 0) {
1871
+ init.emulation = rest.emulation;
1872
+ }
1304
1873
  if (rest.proxy !== void 0) {
1305
1874
  init.proxy = rest.proxy;
1306
1875
  }
@@ -1434,6 +2003,9 @@ function normalizeStandaloneWebSocketOptions(options) {
1434
2003
  if (options.os !== void 0) {
1435
2004
  normalized.os = options.os;
1436
2005
  }
2006
+ if (options.emulation !== void 0) {
2007
+ normalized.emulation = options.emulation;
2008
+ }
1437
2009
  if (options.headers !== void 0) {
1438
2010
  normalized.headers = options.headers;
1439
2011
  }
@@ -1465,6 +2037,11 @@ function normalizeSessionWebSocketOptions(options) {
1465
2037
  if (optionsWithOverrides.os !== void 0) {
1466
2038
  throw new RequestError("`os` is not supported in session.websocket(); the session controls OS emulation.");
1467
2039
  }
2040
+ if (optionsWithOverrides.emulation !== void 0) {
2041
+ throw new RequestError(
2042
+ "`emulation` is not supported in session.websocket(); the session transport controls emulation."
2043
+ );
2044
+ }
1468
2045
  if (optionsWithOverrides.proxy !== void 0) {
1469
2046
  throw new RequestError("`proxy` is not supported in session.websocket(); the session transport controls proxying.");
1470
2047
  }
@@ -1630,10 +2207,11 @@ var WebSocket = class _WebSocket {
1630
2207
  typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols
1631
2208
  );
1632
2209
  assertNoManualWebSocketProtocolHeader(normalizedOptions.headers);
1633
- validateBrowserProfile(normalizedOptions.browser);
1634
- const os = normalizedOptions.os ?? DEFAULT_OS;
1635
- validateOperatingSystem(os);
1636
- const browser = normalizedOptions.browser ?? DEFAULT_BROWSER;
2210
+ const emulationMode = resolveEmulationMode(
2211
+ normalizedOptions.browser,
2212
+ normalizedOptions.os,
2213
+ normalizedOptions.emulation
2214
+ );
1637
2215
  const protocols = normalizeWebSocketProtocolList(
1638
2216
  typeof protocolsOrOptions === "string" || Array.isArray(protocolsOrOptions) ? protocolsOrOptions : normalizedOptions.protocols
1639
2217
  );
@@ -1642,17 +2220,19 @@ var WebSocket = class _WebSocket {
1642
2220
  url: normalizeWebSocketUrl(url),
1643
2221
  options: normalizedOptions,
1644
2222
  openDispatchMode: "automatic",
1645
- connect: (callbacks) => nativeBinding.websocketConnect({
1646
- url: normalizeWebSocketUrl(url),
1647
- browser,
1648
- os,
1649
- headers: headersToTuples(normalizedOptions.headers ?? {}),
1650
- ...protocols && protocols.length > 0 && { protocols },
1651
- ...normalizedOptions.proxy !== void 0 && { proxy: normalizedOptions.proxy },
1652
- onMessage: callbacks.onMessage,
1653
- onClose: callbacks.onClose,
1654
- onError: callbacks.onError
1655
- }),
2223
+ connect: (callbacks) => {
2224
+ const nativeOptions = {
2225
+ url: normalizeWebSocketUrl(url),
2226
+ headers: headersToTuples(normalizedOptions.headers ?? {}),
2227
+ ...protocols && protocols.length > 0 && { protocols },
2228
+ ...normalizedOptions.proxy !== void 0 && { proxy: normalizedOptions.proxy },
2229
+ onMessage: callbacks.onMessage,
2230
+ onClose: callbacks.onClose,
2231
+ onError: callbacks.onError
2232
+ };
2233
+ applyNativeEmulationMode(nativeOptions, emulationMode);
2234
+ return nativeBinding.websocketConnect(nativeOptions);
2235
+ },
1656
2236
  legacyCallbacks: extractLegacyWebSocketCallbacks(optionsCandidate)
1657
2237
  };
1658
2238
  }
@@ -2046,27 +2626,30 @@ async function websocket(urlOrOptions, options) {
2046
2626
  const normalized = normalizeStandaloneWebSocketArgs(urlOrOptions, options);
2047
2627
  validateWebSocketProtocols(normalized.options.protocols);
2048
2628
  assertNoManualWebSocketProtocolHeader(normalized.options.headers);
2049
- validateBrowserProfile(normalized.options.browser);
2050
- const os = normalized.options.os ?? DEFAULT_OS;
2051
- validateOperatingSystem(os);
2052
- const browser = normalized.options.browser ?? DEFAULT_BROWSER;
2629
+ const emulationMode = resolveEmulationMode(
2630
+ normalized.options.browser,
2631
+ normalized.options.os,
2632
+ normalized.options.emulation
2633
+ );
2053
2634
  const protocols = normalizeWebSocketProtocolList(normalized.options.protocols);
2054
2635
  return WebSocket._connectWithInit({
2055
2636
  _internal: true,
2056
2637
  url: normalized.url,
2057
2638
  options: normalized.options,
2058
2639
  openDispatchMode: "deferred",
2059
- connect: (callbacks) => nativeBinding.websocketConnect({
2060
- url: normalized.url,
2061
- browser,
2062
- os,
2063
- headers: headersToTuples(normalized.options.headers ?? {}),
2064
- ...protocols && protocols.length > 0 && { protocols },
2065
- ...normalized.options.proxy !== void 0 && { proxy: normalized.options.proxy },
2066
- onMessage: callbacks.onMessage,
2067
- onClose: callbacks.onClose,
2068
- onError: callbacks.onError
2069
- }),
2640
+ connect: (callbacks) => {
2641
+ const nativeOptions = {
2642
+ url: normalized.url,
2643
+ headers: headersToTuples(normalized.options.headers ?? {}),
2644
+ ...protocols && protocols.length > 0 && { protocols },
2645
+ ...normalized.options.proxy !== void 0 && { proxy: normalized.options.proxy },
2646
+ onMessage: callbacks.onMessage,
2647
+ onClose: callbacks.onClose,
2648
+ onError: callbacks.onError
2649
+ };
2650
+ applyNativeEmulationMode(nativeOptions, emulationMode);
2651
+ return nativeBinding.websocketConnect(nativeOptions);
2652
+ },
2070
2653
  legacyCallbacks: normalized.legacyCallbacks
2071
2654
  });
2072
2655
  }