jiren 1.3.1 → 1.4.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.
@@ -183,7 +183,7 @@ export class JirenClient<
183
183
  if (cacheConfig?.enabled) {
184
184
  const cached = self.cache.get(baseUrl, options?.path, options);
185
185
  if (cached) {
186
- return self.rehydrateResponse(cached);
186
+ return cached as any;
187
187
  }
188
188
  }
189
189
 
@@ -535,39 +535,42 @@ export class JirenClient<
535
535
  // Copy body content
536
536
  buffer = Buffer.from(koffi.decode(bodyPtr, "uint8_t", len));
537
537
 
538
- // Handle GZIP compression
538
+ // Handle GZIP compression - search for magic bytes in first 16 bytes
539
+ // (handles chunked encoding or other framing that may add prefix bytes)
539
540
  const contentEncoding = headersProxy["content-encoding"]?.toLowerCase();
541
+ let gzipOffset = -1;
540
542
 
541
- if (
542
- contentEncoding === "gzip" ||
543
- (buffer.length > 2 && buffer[0] === 0x1f && buffer[1] === 0x8b)
544
- ) {
543
+ // Search for gzip magic bytes (0x1f 0x8b) in first 16 bytes
544
+ for (let i = 0; i < Math.min(16, buffer.length - 1); i++) {
545
+ if (buffer[i] === 0x1f && buffer[i + 1] === 0x8b) {
546
+ gzipOffset = i;
547
+ break;
548
+ }
549
+ }
550
+
551
+ if (contentEncoding === "gzip" || gzipOffset >= 0) {
545
552
  try {
546
- console.log("[Jiren] Attempting gunzip...");
547
- buffer = zlib.gunzipSync(buffer);
548
- console.log("[Jiren] Gunzip success!");
553
+ // If we found gzip at an offset, slice from there
554
+ const gzipData = gzipOffset > 0 ? buffer.slice(gzipOffset) : buffer;
555
+ console.log(
556
+ `[Jiren] Decompressing gzip (offset: ${
557
+ gzipOffset >= 0 ? gzipOffset : 0
558
+ }, size: ${gzipData.length})`
559
+ );
560
+ buffer = zlib.gunzipSync(gzipData);
549
561
  } catch (e) {
550
562
  console.warn("Failed to gunzip response body:", e);
551
563
  }
552
564
  }
553
565
  }
554
566
 
555
- // Convert to base64 for caching persistence
556
- let base64Data = "";
557
- try {
558
- base64Data = buffer.toString("base64");
559
- } catch (e) {
560
- // Should not happen
561
- }
562
-
563
567
  let bodyUsed = false;
564
568
  const consumeBody = () => {
565
569
  bodyUsed = true;
566
570
  };
567
571
 
568
- const bodyObj: JirenResponseBody<T> & { _raw?: string } = {
572
+ const bodyObj: JirenResponseBody<T> = {
569
573
  bodyUsed: false,
570
- _raw: base64Data,
571
574
  arrayBuffer: async () => {
572
575
  consumeBody();
573
576
  return buffer.buffer.slice(
@@ -607,63 +610,6 @@ export class JirenClient<
607
610
  lib.symbols.zclient_response_free(respPtr);
608
611
  }
609
612
  }
610
-
611
- /**
612
- * Rehydrates a cached response object by restoring the body interface methods.
613
- * This is necessary because JSON serialization strips functions.
614
- */
615
- private rehydrateResponse(cached: any): JirenResponse {
616
- // If it already has methods, return as is
617
- if (typeof cached.body.text === "function") return cached;
618
-
619
- // Retrieve raw data
620
- const rawData = cached.body._raw;
621
- let buffer: Buffer;
622
-
623
- if (rawData) {
624
- buffer = Buffer.from(rawData, "base64");
625
- } else {
626
- buffer = Buffer.from("");
627
- }
628
-
629
- let bodyUsed = cached.body.bodyUsed || false;
630
- const consumeBody = () => {
631
- bodyUsed = true;
632
- };
633
-
634
- const bodyObj: JirenResponseBody = {
635
- _raw: rawData, // Keep it for re-caching if needed
636
- bodyUsed,
637
- arrayBuffer: async () => {
638
- consumeBody();
639
- return buffer.buffer.slice(
640
- buffer.byteOffset,
641
- buffer.byteOffset + buffer.byteLength
642
- ) as ArrayBuffer;
643
- },
644
- blob: async () => {
645
- consumeBody();
646
- return new Blob([buffer as any]);
647
- },
648
- text: async () => {
649
- consumeBody();
650
- return buffer.toString("utf-8");
651
- },
652
- json: async <R = any>(): Promise<R> => {
653
- consumeBody();
654
- return JSON.parse(buffer.toString("utf-8"));
655
- },
656
- } as any;
657
-
658
- Object.defineProperty(bodyObj, "bodyUsed", {
659
- get: () => bodyUsed,
660
- });
661
-
662
- return {
663
- ...cached,
664
- body: bodyObj,
665
- };
666
- }
667
613
  }
668
614
 
669
615
  class NativeHeaders {
@@ -220,7 +220,7 @@ export class JirenClient<
220
220
  // Try to get from cache
221
221
  const cached = self.cache.get(baseUrl, options?.path, options);
222
222
  if (cached) {
223
- return self.rehydrateResponse(cached);
223
+ return cached as any;
224
224
  }
225
225
  }
226
226
 
@@ -699,14 +699,6 @@ export class JirenClient<
699
699
  buffer = toArrayBuffer(bodyPtr, 0, len).slice(0);
700
700
  }
701
701
 
702
- // Convert to base64 for caching persistence
703
- let base64Data = "";
704
- try {
705
- base64Data = Buffer.from(buffer).toString("base64");
706
- } catch (e) {
707
- // Should not happen for valid buffer
708
- }
709
-
710
702
  let bodyUsed = false;
711
703
  const consumeBody = () => {
712
704
  if (bodyUsed) {
@@ -714,9 +706,8 @@ export class JirenClient<
714
706
  bodyUsed = true;
715
707
  };
716
708
 
717
- const bodyObj: JirenResponseBody<T> & { _raw?: string } = {
709
+ const bodyObj: JirenResponseBody<T> = {
718
710
  bodyUsed: false,
719
- _raw: base64Data,
720
711
  arrayBuffer: async () => {
721
712
  consumeBody();
722
713
  if (Buffer.isBuffer(buffer)) {
@@ -791,64 +782,6 @@ export class JirenClient<
791
782
 
792
783
  return { headers, serializedBody };
793
784
  }
794
-
795
- /**
796
- * Rehydrates a cached response object by restoring the body interface methods.
797
- * This is necessary because JSON serialization strips functions.
798
- */
799
- private rehydrateResponse(cached: any): JirenResponse {
800
- // If it already has methods, return as is
801
- if (typeof cached.body.text === "function") return cached;
802
-
803
- // Retrieve raw data
804
- const rawData = cached.body._raw;
805
- let buffer: Buffer;
806
-
807
- if (rawData) {
808
- buffer = Buffer.from(rawData, "base64");
809
- } else {
810
- buffer = Buffer.from("");
811
- }
812
-
813
- let bodyUsed = cached.body.bodyUsed || false;
814
- const consumeBody = () => {
815
- bodyUsed = true;
816
- };
817
-
818
- const bodyObj: JirenResponseBody = {
819
- _raw: rawData, // Keep it for re-caching if needed
820
- bodyUsed,
821
- arrayBuffer: async () => {
822
- consumeBody();
823
- return buffer.buffer.slice(
824
- buffer.byteOffset,
825
- buffer.byteOffset + buffer.byteLength
826
- ) as ArrayBuffer;
827
- },
828
- blob: async () => {
829
- consumeBody();
830
- return new Blob([buffer]);
831
- },
832
- text: async () => {
833
- consumeBody();
834
- return new TextDecoder().decode(buffer);
835
- },
836
- json: async <R = any>(): Promise<R> => {
837
- consumeBody();
838
- const text = new TextDecoder().decode(buffer);
839
- return JSON.parse(text);
840
- },
841
- } as any;
842
-
843
- Object.defineProperty(bodyObj, "bodyUsed", {
844
- get: () => bodyUsed,
845
- });
846
-
847
- return {
848
- ...cached,
849
- body: bodyObj,
850
- };
851
- }
852
785
  }
853
786
 
854
787
  class NativeHeaders {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "jiren",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "author": "",
5
- "main": "dist/index-node.js",
6
- "module": "dist/index.js",
5
+ "main": "index.ts",
6
+ "module": "index.ts",
7
7
  "types": "index.ts",
8
8
  "devDependencies": {
9
9
  "@types/bun": "^1.3.4"
@@ -17,8 +17,7 @@
17
17
  "index-node.ts",
18
18
  "types",
19
19
  "lib",
20
- "components",
21
- "dist"
20
+ "components"
22
21
  ],
23
22
  "keywords": [
24
23
  "http",
@@ -35,7 +34,6 @@
35
34
  "license": "MIT",
36
35
  "scripts": {
37
36
  "build:zig": "cd .. && zig build --release=fast",
38
- "build": "bun build ./index.ts --outfile ./dist/index.js --target bun && bun build ./index-node.ts --outfile ./dist/index-node.js --target node --external koffi",
39
37
  "test": "bun run examples/basic.ts"
40
38
  },
41
39
  "engines": {
@@ -45,14 +43,8 @@
45
43
  "type": "module",
46
44
  "exports": {
47
45
  ".": {
48
- "bun": {
49
- "types": "./index.ts",
50
- "default": "./dist/index.js"
51
- },
52
- "default": {
53
- "types": "./index-node.ts",
54
- "default": "./dist/index-node.js"
55
- }
46
+ "bun": "./index.ts",
47
+ "default": "./index-node.ts"
56
48
  },
57
49
  "./package.json": "./package.json"
58
50
  },