@spider-cloud/spider-client 0.1.24 → 0.1.25

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.
@@ -1,2 +1,2 @@
1
1
  import type { SpiderCoreResponse } from "../config";
2
- export declare const processChunk: (chunk: string, cb: (r: SpiderCoreResponse) => void) => boolean;
2
+ export declare const createJsonLineProcessor: (cb: (r: SpiderCoreResponse) => void) => (chunk: Buffer | string) => void;
@@ -1,13 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.processChunk = void 0;
4
- const processChunk = (chunk, cb) => {
5
- try {
6
- cb(chunk ? JSON.parse(chunk.trim()) : null);
7
- return true;
8
- }
9
- catch (_error) {
10
- return false;
11
- }
3
+ exports.createJsonLineProcessor = void 0;
4
+ const createJsonLineProcessor = (cb) => {
5
+ let buffer = "";
6
+ return (chunk) => {
7
+ buffer += chunk.toString();
8
+ let boundary;
9
+ while ((boundary = buffer.indexOf("\n")) !== -1) {
10
+ const line = buffer.slice(0, boundary);
11
+ buffer = buffer.slice(boundary + 1);
12
+ if (line.trim()) {
13
+ try {
14
+ cb(JSON.parse(line));
15
+ }
16
+ catch (_error) { }
17
+ }
18
+ }
19
+ };
12
20
  };
13
- exports.processChunk = processChunk;
21
+ exports.createJsonLineProcessor = createJsonLineProcessor;
@@ -2,12 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamReader = void 0;
4
4
  const process_chunk_1 = require("./process-chunk");
5
- // stream the response via callbacks.
5
+ // Stream the response via callbacks.
6
6
  const streamReader = async (res, cb) => {
7
7
  var _a;
8
8
  if (res.ok) {
9
9
  const reader = (_a = res.body) === null || _a === void 0 ? void 0 : _a.getReader();
10
10
  const decoder = new TextDecoder();
11
+ const processChunk = (0, process_chunk_1.createJsonLineProcessor)(cb);
11
12
  if (reader) {
12
13
  while (true) {
13
14
  const { done, value } = await reader.read();
@@ -15,8 +16,9 @@ const streamReader = async (res, cb) => {
15
16
  break;
16
17
  }
17
18
  const chunk = decoder.decode(value, { stream: true });
18
- (0, process_chunk_1.processChunk)(chunk, cb);
19
+ processChunk(chunk);
19
20
  }
21
+ processChunk(decoder.decode(new Uint8Array(), { stream: false }));
20
22
  }
21
23
  }
22
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spider-cloud/spider-client",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
4
4
  "description": "Isomorphic Javascript SDK for Spider Cloud services",
5
5
  "scripts": {
6
6
  "test": "node --import tsx --test __tests__/*test.ts",