@zimic/interceptor 0.20.2-canary.2 → 0.21.0-canary.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/http.mjs CHANGED
@@ -558,6 +558,52 @@ __name(jsonEquals, "jsonEquals");
558
558
  __name2(jsonEquals, "jsonEquals");
559
559
  var jsonEquals_default = jsonEquals;
560
560
 
561
+ // src/utils/data.ts
562
+ async function convertReadableStreamToBlob(stream, options) {
563
+ const chunks = [];
564
+ const reader = stream.getReader();
565
+ while (true) {
566
+ const readResult = await reader.read();
567
+ if (readResult.value) {
568
+ chunks.push(readResult.value);
569
+ }
570
+ if (readResult.done) {
571
+ break;
572
+ }
573
+ }
574
+ return new Blob(chunks, options);
575
+ }
576
+ __name(convertReadableStreamToBlob, "convertReadableStreamToBlob");
577
+ function convertArrayBufferToBlob(buffer, options) {
578
+ return new Blob([buffer], options);
579
+ }
580
+ __name(convertArrayBufferToBlob, "convertArrayBufferToBlob");
581
+ function convertArrayBufferToBase64(buffer) {
582
+ if (isClientSide()) {
583
+ const bufferBytes = new Uint8Array(buffer);
584
+ const bufferAsStringArray = [];
585
+ for (const byte of bufferBytes) {
586
+ const byteCode = String.fromCharCode(byte);
587
+ bufferAsStringArray.push(byteCode);
588
+ }
589
+ const bufferAsString = bufferAsStringArray.join("");
590
+ return btoa(bufferAsString);
591
+ } else {
592
+ return Buffer.from(buffer).toString("base64");
593
+ }
594
+ }
595
+ __name(convertArrayBufferToBase64, "convertArrayBufferToBase64");
596
+ function convertBase64ToArrayBuffer(base64Value) {
597
+ if (isClientSide()) {
598
+ const bufferAsString = atob(base64Value);
599
+ const array = Uint8Array.from(bufferAsString, (character) => character.charCodeAt(0));
600
+ return array.buffer;
601
+ } else {
602
+ return Buffer.from(base64Value, "base64");
603
+ }
604
+ }
605
+ __name(convertBase64ToArrayBuffer, "convertBase64ToArrayBuffer");
606
+
561
607
  // src/http/requestHandler/errors/NoResponseDefinitionError.ts
562
608
  var NoResponseDefinitionError = class extends TypeError {
563
609
  static {
@@ -766,14 +812,22 @@ var HttpRequestHandlerClient = class {
766
812
  diff: { expected: restrictionBody, received: body }
767
813
  };
768
814
  }
769
- if (restrictionBody instanceof Blob) {
815
+ if (restrictionBody instanceof Blob || restrictionBody instanceof ArrayBuffer || restrictionBody instanceof ReadableStream) {
770
816
  if (!(body instanceof Blob)) {
771
817
  return {
772
818
  value: false,
773
819
  diff: { expected: restrictionBody, received: body }
774
820
  };
775
821
  }
776
- const matchesRestriction2 = await blobEquals_default(body, restrictionBody);
822
+ let restrictionBodyAsBlob;
823
+ if (restrictionBody instanceof ArrayBuffer) {
824
+ restrictionBodyAsBlob = convertArrayBufferToBlob(restrictionBody, { type: body.type });
825
+ } else if (restrictionBody instanceof ReadableStream) {
826
+ restrictionBodyAsBlob = await convertReadableStreamToBlob(restrictionBody, { type: body.type });
827
+ } else {
828
+ restrictionBodyAsBlob = restrictionBody;
829
+ }
830
+ const matchesRestriction2 = await blobEquals_default(body, restrictionBodyAsBlob);
777
831
  return matchesRestriction2 ? { value: true } : {
778
832
  value: false,
779
833
  diff: { expected: restrictionBody, received: body }
@@ -1104,7 +1158,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1104
1158
  if (!canHaveBody) {
1105
1159
  return new Response(null, { headers, status });
1106
1160
  }
1107
- if (typeof declaration.body === "string" || declaration.body === void 0 || declaration.body instanceof FormData || declaration.body instanceof URLSearchParams || declaration.body instanceof Blob || declaration.body instanceof ArrayBuffer) {
1161
+ if (typeof declaration.body === "string" || declaration.body === void 0 || declaration.body instanceof FormData || declaration.body instanceof URLSearchParams || declaration.body instanceof Blob || declaration.body instanceof ArrayBuffer || declaration.body instanceof ReadableStream) {
1108
1162
  return new Response(declaration.body ?? null, { headers, status });
1109
1163
  }
1110
1164
  return Response.json(declaration.body, { headers, status });
@@ -1242,7 +1296,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1242
1296
  try {
1243
1297
  return await this.parseRawBodyAsJSON(resource);
1244
1298
  } catch {
1245
- return await this.parseRawBodyAsText(resourceClone);
1299
+ return await this.parseRawBodyAsBlob(resourceClone);
1246
1300
  }
1247
1301
  } catch (error) {
1248
1302
  console.error(error);
@@ -1847,33 +1901,6 @@ var importCrypto = createCachedDynamicImport_default(async () => {
1847
1901
  return globalCrypto ?? await import('crypto');
1848
1902
  });
1849
1903
 
1850
- // src/utils/data.ts
1851
- function convertArrayBufferToBase64(buffer) {
1852
- if (isClientSide()) {
1853
- const bufferBytes = new Uint8Array(buffer);
1854
- const bufferAsStringArray = [];
1855
- for (const byte of bufferBytes) {
1856
- const byteCode = String.fromCharCode(byte);
1857
- bufferAsStringArray.push(byteCode);
1858
- }
1859
- const bufferAsString = bufferAsStringArray.join("");
1860
- return btoa(bufferAsString);
1861
- } else {
1862
- return Buffer.from(buffer).toString("base64");
1863
- }
1864
- }
1865
- __name(convertArrayBufferToBase64, "convertArrayBufferToBase64");
1866
- function convertBase64ToArrayBuffer(base64Value) {
1867
- if (isClientSide()) {
1868
- const bufferAsString = atob(base64Value);
1869
- const array = Uint8Array.from(bufferAsString, (character) => character.charCodeAt(0));
1870
- return array.buffer;
1871
- } else {
1872
- return Buffer.from(base64Value, "base64");
1873
- }
1874
- }
1875
- __name(convertBase64ToArrayBuffer, "convertBase64ToArrayBuffer");
1876
-
1877
1904
  // src/utils/fetch.ts
1878
1905
  function deserializeRequest(serializedRequest) {
1879
1906
  const deserializedBody = serializedRequest.body ? convertBase64ToArrayBuffer(serializedRequest.body) : null;