@spider-cloud/spider-client 0.1.23 → 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.
package/dist/client.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.Spider = void 0;
|
|
|
4
4
|
const config_1 = require("./config");
|
|
5
5
|
const package_json_1 = require("../package.json");
|
|
6
6
|
const stream_reader_1 = require("./utils/stream-reader");
|
|
7
|
+
const exponential_backoff_1 = require("exponential-backoff");
|
|
7
8
|
/**
|
|
8
9
|
* A class to interact with the Spider API.
|
|
9
10
|
*/
|
|
@@ -29,10 +30,12 @@ class Spider {
|
|
|
29
30
|
*/
|
|
30
31
|
async _apiPost(endpoint, data, stream, jsonl) {
|
|
31
32
|
const headers = jsonl ? this.prepareHeadersJsonL : this.prepareHeaders;
|
|
32
|
-
const response = await fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
33
|
+
const response = await (0, exponential_backoff_1.backOff)(() => fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
33
34
|
method: "POST",
|
|
34
35
|
headers: headers,
|
|
35
36
|
body: JSON.stringify(data),
|
|
37
|
+
}), {
|
|
38
|
+
numOfAttempts: 5,
|
|
36
39
|
});
|
|
37
40
|
if (!stream) {
|
|
38
41
|
if (response.ok) {
|
|
@@ -51,9 +54,11 @@ class Spider {
|
|
|
51
54
|
*/
|
|
52
55
|
async _apiGet(endpoint) {
|
|
53
56
|
const headers = this.prepareHeaders;
|
|
54
|
-
const response = await fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
57
|
+
const response = await (0, exponential_backoff_1.backOff)(() => fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
55
58
|
method: "GET",
|
|
56
59
|
headers: headers,
|
|
60
|
+
}), {
|
|
61
|
+
numOfAttempts: 5,
|
|
57
62
|
});
|
|
58
63
|
if (response.ok) {
|
|
59
64
|
return response.json();
|
|
@@ -69,9 +74,11 @@ class Spider {
|
|
|
69
74
|
*/
|
|
70
75
|
async _apiDelete(endpoint) {
|
|
71
76
|
const headers = this.prepareHeaders;
|
|
72
|
-
const response = await fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
77
|
+
const response = await (0, exponential_backoff_1.backOff)(() => fetch(`${config_1.APISchema["url"]}/${config_1.ApiVersion.V1}/${endpoint}`, {
|
|
73
78
|
method: "DELETE",
|
|
74
79
|
headers,
|
|
80
|
+
}), {
|
|
81
|
+
numOfAttempts: 5,
|
|
75
82
|
});
|
|
76
83
|
if (response.ok) {
|
|
77
84
|
return response;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { SpiderCoreResponse } from "../config";
|
|
2
|
-
export declare const
|
|
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.
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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.
|
|
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
|
-
//
|
|
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
|
-
|
|
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.
|
|
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",
|
|
@@ -23,9 +23,12 @@
|
|
|
23
23
|
"author": "Jeff Mendez<jeff@a11ywatch.com>",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/node": "22.
|
|
27
|
-
"dotenv": "^16.4.
|
|
28
|
-
"tsx": "^4.19.
|
|
29
|
-
"typescript": "5.
|
|
26
|
+
"@types/node": "22.10.1",
|
|
27
|
+
"dotenv": "^16.4.7",
|
|
28
|
+
"tsx": "^4.19.2",
|
|
29
|
+
"typescript": "5.7.2"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"exponential-backoff": "^3.1.1"
|
|
30
33
|
}
|
|
31
34
|
}
|