@synthient/synthient 1.0.1 → 2.1.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/LICENSE +198 -11
- package/README.md +425 -84
- package/dist/client.d.ts +105 -30
- package/dist/client.js +166 -88
- package/dist/errors.d.ts +72 -0
- package/dist/errors.js +122 -0
- package/dist/events.d.ts +243 -0
- package/dist/events.js +1 -0
- package/dist/feed.d.ts +146 -0
- package/dist/feed.js +313 -0
- package/dist/grpcschema.d.ts +56 -0
- package/dist/grpcschema.js +278 -0
- package/dist/index.d.ts +23 -5
- package/dist/index.js +20 -21
- package/dist/options.d.ts +5 -0
- package/dist/options.js +23 -0
- package/dist/transport.d.ts +76 -0
- package/dist/transport.js +328 -0
- package/dist/types.d.ts +341 -0
- package/dist/types.js +4 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +70 -12
- package/dist/constants.d.ts +0 -2
- package/dist/constants.js +0 -5
- package/dist/exceptions.d.ts +0 -33
- package/dist/exceptions.js +0 -48
- package/dist/models.d.ts +0 -191
- package/dist/models.js +0 -221
- package/dist/utils.d.ts +0 -15
- package/dist/utils.js +0 -29
- package/src/client.ts +0 -106
- package/src/constants.ts +0 -2
- package/src/exceptions.ts +0 -44
- package/src/index.ts +0 -5
- package/src/models.ts +0 -276
- package/src/utils.ts +0 -28
- package/tsconfig.json +0 -15
package/dist/index.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
__exportStar(require("./utils"), exports);
|
|
1
|
+
/**
|
|
2
|
+
* The official Node.js client for the Synthient API: IP intelligence, account details,
|
|
3
|
+
* and real-time and bulk data feeds.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Synthient } from "@synthient/synthient";
|
|
8
|
+
*
|
|
9
|
+
* const client = new Synthient(); // reads SYNTHIENT_API_KEY
|
|
10
|
+
* const info = await client.lookupIP("8.8.8.8");
|
|
11
|
+
* console.log(info.intelligence?.risk_score);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
export { DEFAULT_BASE_URL, Synthient, } from "./client.js";
|
|
17
|
+
export { APIConnectionError, APIError, APITimeoutError, SynthientError, } from "./errors.js";
|
|
18
|
+
export { Dataset, Feed, dailyExport, hourlyExport, } from "./feed.js";
|
|
19
|
+
export { toDate } from "./types.js";
|
|
20
|
+
export { VERSION } from "./version.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Rejects a key that a header cannot carry, without putting the key in the error message. */
|
|
2
|
+
export declare function resolveAPIKey(apiKey: string | undefined): string;
|
|
3
|
+
/** The upper bound is the longest delay setTimeout accepts. */
|
|
4
|
+
export declare function checkTimeout(timeout: number): void;
|
|
5
|
+
export declare function limitQuery(limit: number | undefined): Record<string, string>;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Rejects a key that a header cannot carry, without putting the key in the error message. */
|
|
2
|
+
export function resolveAPIKey(apiKey) {
|
|
3
|
+
const key = (apiKey ?? process.env.SYNTHIENT_API_KEY ?? "").trim();
|
|
4
|
+
if (/[^\x20-\x7e]/.test(key)) {
|
|
5
|
+
throw new TypeError("invalid API key: it must be printable ASCII");
|
|
6
|
+
}
|
|
7
|
+
return key;
|
|
8
|
+
}
|
|
9
|
+
/** The upper bound is the longest delay setTimeout accepts. */
|
|
10
|
+
export function checkTimeout(timeout) {
|
|
11
|
+
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 2 ** 31 - 1) {
|
|
12
|
+
throw new RangeError("timeout must be an integer number of milliseconds from 1 to 2147483647");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function limitQuery(limit) {
|
|
16
|
+
if (limit === undefined) {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
if (!Number.isSafeInteger(limit) || limit < 1) {
|
|
20
|
+
throw new RangeError("limit must be a positive integer");
|
|
21
|
+
}
|
|
22
|
+
return { limit: String(limit) };
|
|
23
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { APIError } from "./errors.ts";
|
|
2
|
+
export type Fetch = (input: string, init: RequestInit) => Promise<Response>;
|
|
3
|
+
export interface ResponseMetadata {
|
|
4
|
+
status: number;
|
|
5
|
+
/** The X-Request-Id header, or "". */
|
|
6
|
+
requestID: string;
|
|
7
|
+
/**
|
|
8
|
+
* A copy of the response headers, including RateLimit-Limit, RateLimit-Remaining,
|
|
9
|
+
* and RateLimit-Reset when present. Headers can contain sensitive data, such as
|
|
10
|
+
* presigned download URLs.
|
|
11
|
+
*/
|
|
12
|
+
headers: Headers;
|
|
13
|
+
}
|
|
14
|
+
export interface RequestOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Cancels the call, which rejects with the signal's reason. Use `AbortSignal.timeout(ms)`
|
|
17
|
+
* to bound a whole call, including retries.
|
|
18
|
+
*/
|
|
19
|
+
signal?: AbortSignal | undefined;
|
|
20
|
+
}
|
|
21
|
+
export type Wait = (ms: number, signal: AbortSignal | undefined) => Promise<void>;
|
|
22
|
+
export declare const kWait: unique symbol;
|
|
23
|
+
export interface TransportConfig {
|
|
24
|
+
apiKey: string;
|
|
25
|
+
baseURL: URL;
|
|
26
|
+
maxRetries: number;
|
|
27
|
+
timeout: number;
|
|
28
|
+
fetch: Fetch;
|
|
29
|
+
userAgent: string;
|
|
30
|
+
onResponse: ((metadata: ResponseMetadata) => void) | undefined;
|
|
31
|
+
wait: Wait;
|
|
32
|
+
}
|
|
33
|
+
export interface Request {
|
|
34
|
+
method?: "GET" | "POST";
|
|
35
|
+
path: string;
|
|
36
|
+
query?: Record<string, string>;
|
|
37
|
+
body?: string;
|
|
38
|
+
accept?: string;
|
|
39
|
+
policy: "transient" | "rateLimit";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Its signal follows the caller's signal and an idle timer that runs only while the
|
|
43
|
+
* attempt waits on the network.
|
|
44
|
+
*/
|
|
45
|
+
export declare class Attempt {
|
|
46
|
+
#private;
|
|
47
|
+
readonly caller: AbortSignal | undefined;
|
|
48
|
+
readonly signal: AbortSignal;
|
|
49
|
+
constructor(caller: AbortSignal | undefined, timeout: number, surface?: "api" | "storage");
|
|
50
|
+
/**
|
|
51
|
+
* Storage failures are rebuilt without their message or cause, either of which can
|
|
52
|
+
* contain the presigned URL.
|
|
53
|
+
*/
|
|
54
|
+
watch<T>(work: Promise<T>): Promise<T>;
|
|
55
|
+
}
|
|
56
|
+
export declare class Transport {
|
|
57
|
+
#private;
|
|
58
|
+
constructor(config: TransportConfig);
|
|
59
|
+
url(request: Request): string;
|
|
60
|
+
/** The caller must read or cancel the returned response's body. */
|
|
61
|
+
send(request: Request, signal?: AbortSignal): Promise<[Response, Attempt]>;
|
|
62
|
+
json<T extends object>(request: Request, signal?: AbortSignal): Promise<T>;
|
|
63
|
+
stream(request: Request, signal?: AbortSignal): Promise<ReadableStream<Uint8Array>>;
|
|
64
|
+
error(response: Response, attempt: Attempt): Promise<APIError>;
|
|
65
|
+
/**
|
|
66
|
+
* The API key is never sent to the storage host. Errors never include the presigned
|
|
67
|
+
* URL, and storage error bodies are discarded.
|
|
68
|
+
*/
|
|
69
|
+
download(start: URL, signal?: AbortSignal): Promise<ReadableStream<Uint8Array>>;
|
|
70
|
+
}
|
|
71
|
+
export declare function isHTTP(url: URL | null): url is URL;
|
|
72
|
+
export declare function redirectTarget(from: URL, response: Response): URL;
|
|
73
|
+
export declare function wait(ms: number, signal: AbortSignal | undefined): Promise<void>;
|
|
74
|
+
export declare function backoff(attempt: number): number;
|
|
75
|
+
/** Discards a body so its connection can be reused. */
|
|
76
|
+
export declare function drain(response: Response, attempt: Attempt): Promise<void>;
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
import { APIConnectionError, APIError, APITimeoutError, SynthientError, apiError, isObject, parseRetryAfter, } from "./errors.js";
|
|
3
|
+
const MAX_RESPONSE_SIZE = 16 << 20;
|
|
4
|
+
const MAX_ERROR_SIZE = 64 << 10;
|
|
5
|
+
const MAX_BACKOFF = 30_000;
|
|
6
|
+
const MAX_RETRY_AFTER = 60_000;
|
|
7
|
+
const MAX_STORAGE_REDIRECTS = 10;
|
|
8
|
+
// Transport failures retried on account and feed requests, matched anywhere in the
|
|
9
|
+
// error's cause chain, including errors from a custom fetch. Anything else, such as a
|
|
10
|
+
// TLS certificate error, is not retried.
|
|
11
|
+
const TRANSIENT_CODES = new Set([
|
|
12
|
+
"ECONNRESET",
|
|
13
|
+
"ECONNREFUSED",
|
|
14
|
+
"EPIPE",
|
|
15
|
+
"ETIMEDOUT",
|
|
16
|
+
"ENOTFOUND",
|
|
17
|
+
"EAI_AGAIN",
|
|
18
|
+
"ENETUNREACH",
|
|
19
|
+
"EHOSTUNREACH",
|
|
20
|
+
"UND_ERR_SOCKET",
|
|
21
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
22
|
+
]);
|
|
23
|
+
const RETRY_STATUSES = new Set([500, 502, 503, 504]);
|
|
24
|
+
// Tests replace the backoff wait through this key in the client options.
|
|
25
|
+
export const kWait = Symbol("synthient.wait");
|
|
26
|
+
/**
|
|
27
|
+
* Its signal follows the caller's signal and an idle timer that runs only while the
|
|
28
|
+
* attempt waits on the network.
|
|
29
|
+
*/
|
|
30
|
+
export class Attempt {
|
|
31
|
+
caller;
|
|
32
|
+
signal;
|
|
33
|
+
#timer = new AbortController();
|
|
34
|
+
#timeout;
|
|
35
|
+
#surface;
|
|
36
|
+
constructor(caller, timeout, surface = "api") {
|
|
37
|
+
this.caller = caller;
|
|
38
|
+
this.#timeout = timeout;
|
|
39
|
+
this.#surface = surface;
|
|
40
|
+
this.signal = caller ? AbortSignal.any([caller, this.#timer.signal]) : this.#timer.signal;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Storage failures are rebuilt without their message or cause, either of which can
|
|
44
|
+
* contain the presigned URL.
|
|
45
|
+
*/
|
|
46
|
+
async watch(work) {
|
|
47
|
+
const timer = setTimeout(() => {
|
|
48
|
+
this.#timer.abort();
|
|
49
|
+
}, this.#timeout);
|
|
50
|
+
try {
|
|
51
|
+
const result = await work;
|
|
52
|
+
this.caller?.throwIfAborted();
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (this.caller?.aborted) {
|
|
57
|
+
throw this.caller.reason;
|
|
58
|
+
}
|
|
59
|
+
if (this.#timer.signal.aborted ||
|
|
60
|
+
(this.#surface === "storage" && error instanceof APITimeoutError)) {
|
|
61
|
+
throw new APITimeoutError("request timed out");
|
|
62
|
+
}
|
|
63
|
+
if (this.#surface === "storage") {
|
|
64
|
+
throw new APIConnectionError("connection failed");
|
|
65
|
+
}
|
|
66
|
+
if (error instanceof SynthientError) {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
throw new APIConnectionError("connection failed", { cause: error });
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export class Transport {
|
|
77
|
+
#config;
|
|
78
|
+
#base;
|
|
79
|
+
#headers;
|
|
80
|
+
constructor(config) {
|
|
81
|
+
this.#config = config;
|
|
82
|
+
this.#base = (config.baseURL.origin + config.baseURL.pathname).replace(/\/+$/, "");
|
|
83
|
+
this.#headers = new Headers({ "x-api-key": config.apiKey, "user-agent": config.userAgent });
|
|
84
|
+
}
|
|
85
|
+
url(request) {
|
|
86
|
+
const search = new URLSearchParams(request.query).toString();
|
|
87
|
+
return search ? `${this.#base}${request.path}?${search}` : this.#base + request.path;
|
|
88
|
+
}
|
|
89
|
+
/** The caller must read or cancel the returned response's body. */
|
|
90
|
+
send(request, signal) {
|
|
91
|
+
return this.#request(request, signal, (response, attempt) => [
|
|
92
|
+
response,
|
|
93
|
+
attempt,
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
async #request(request, signal, consume) {
|
|
97
|
+
const url = this.url(request);
|
|
98
|
+
const headers = new Headers(this.#headers);
|
|
99
|
+
headers.set("accept", request.accept ?? "application/json");
|
|
100
|
+
if (request.body !== undefined) {
|
|
101
|
+
headers.set("content-type", "application/json");
|
|
102
|
+
}
|
|
103
|
+
for (let attempt = 0;; attempt++) {
|
|
104
|
+
signal?.throwIfAborted();
|
|
105
|
+
const canRetry = attempt < this.#config.maxRetries;
|
|
106
|
+
const current = new Attempt(signal, this.#config.timeout);
|
|
107
|
+
let delay = backoff(attempt);
|
|
108
|
+
try {
|
|
109
|
+
const response = await current.watch(this.#fetch(url, {
|
|
110
|
+
method: request.method ?? "GET",
|
|
111
|
+
headers,
|
|
112
|
+
body: request.body ?? null,
|
|
113
|
+
redirect: "manual",
|
|
114
|
+
signal: current.signal,
|
|
115
|
+
}));
|
|
116
|
+
this.#notify(response);
|
|
117
|
+
// A 429 without Retry-After is a spent monthly budget or concurrent stream
|
|
118
|
+
// limit, which a retry cannot fix.
|
|
119
|
+
const retryAfter = parseRetryAfter(response.headers.get("retry-after"));
|
|
120
|
+
const retryStatus = (response.status === 429 && retryAfter !== undefined) ||
|
|
121
|
+
(request.policy === "transient" && RETRY_STATUSES.has(response.status));
|
|
122
|
+
if (!canRetry || !retryStatus || (retryAfter ?? 0) > MAX_RETRY_AFTER) {
|
|
123
|
+
return await consume(response, current);
|
|
124
|
+
}
|
|
125
|
+
await drain(response, current);
|
|
126
|
+
delay = Math.max(delay, retryAfter ?? 0);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (!canRetry || request.policy !== "transient" || !transient(error, signal)) {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
await this.#config.wait(delay, signal);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
json(request, signal) {
|
|
137
|
+
return this.#request(request, signal, async (response, attempt) => {
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
throw await this.error(response, attempt);
|
|
140
|
+
}
|
|
141
|
+
const [body, complete] = await read(response, attempt, MAX_RESPONSE_SIZE);
|
|
142
|
+
if (!complete) {
|
|
143
|
+
throw new SynthientError(`response body exceeds ${MAX_RESPONSE_SIZE} bytes`);
|
|
144
|
+
}
|
|
145
|
+
let value;
|
|
146
|
+
try {
|
|
147
|
+
value = JSON.parse(new TextDecoder().decode(body));
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new SynthientError("invalid JSON in response", { cause: error });
|
|
151
|
+
}
|
|
152
|
+
if (!isObject(value)) {
|
|
153
|
+
throw new SynthientError("response is not a JSON object");
|
|
154
|
+
}
|
|
155
|
+
// The client trusts the API's schema; only protocol control data is validated.
|
|
156
|
+
return value;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
async stream(request, signal) {
|
|
160
|
+
const [response, attempt] = await this.send(request, signal);
|
|
161
|
+
if (response.status !== 200) {
|
|
162
|
+
throw await this.error(response, attempt);
|
|
163
|
+
}
|
|
164
|
+
return watchedBody(response, attempt);
|
|
165
|
+
}
|
|
166
|
+
async error(response, attempt) {
|
|
167
|
+
try {
|
|
168
|
+
const [body] = await read(response, attempt, MAX_ERROR_SIZE);
|
|
169
|
+
return apiError(response, body);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (attempt.caller?.aborted) {
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
return apiError(response, new Uint8Array(), error);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The API key is never sent to the storage host. Errors never include the presigned
|
|
180
|
+
* URL, and storage error bodies are discarded.
|
|
181
|
+
*/
|
|
182
|
+
async download(start, signal) {
|
|
183
|
+
let url = start;
|
|
184
|
+
for (let redirects = 0;; redirects++) {
|
|
185
|
+
signal?.throwIfAborted();
|
|
186
|
+
const attempt = new Attempt(signal, this.#config.timeout, "storage");
|
|
187
|
+
const response = await attempt.watch(this.#fetch(url.href, {
|
|
188
|
+
headers: { "user-agent": this.#config.userAgent },
|
|
189
|
+
redirect: "manual",
|
|
190
|
+
signal: attempt.signal,
|
|
191
|
+
}));
|
|
192
|
+
if (response.status === 200) {
|
|
193
|
+
return watchedBody(response, attempt);
|
|
194
|
+
}
|
|
195
|
+
await drain(response, attempt);
|
|
196
|
+
if (response.status < 300 || response.status > 399) {
|
|
197
|
+
throw new APIError({
|
|
198
|
+
status: response.status,
|
|
199
|
+
retryAfter: parseRetryAfter(response.headers.get("retry-after")),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
if (redirects === MAX_STORAGE_REDIRECTS) {
|
|
203
|
+
throw new SynthientError("too many export redirects");
|
|
204
|
+
}
|
|
205
|
+
url = redirectTarget(url, response);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// A custom fetch that throws synchronously still rejects, so watch() wraps its error.
|
|
209
|
+
async #fetch(url, init) {
|
|
210
|
+
return this.#config.fetch(url, init);
|
|
211
|
+
}
|
|
212
|
+
#notify(response) {
|
|
213
|
+
const hook = this.#config.onResponse;
|
|
214
|
+
if (!hook) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
hook({
|
|
219
|
+
status: response.status,
|
|
220
|
+
requestID: response.headers.get("x-request-id") ?? "",
|
|
221
|
+
headers: new Headers(response.headers),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
process.emitWarning(error instanceof Error ? error : String(error));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
export function isHTTP(url) {
|
|
230
|
+
return url?.protocol === "https:" || url?.protocol === "http:";
|
|
231
|
+
}
|
|
232
|
+
export function redirectTarget(from, response) {
|
|
233
|
+
const location = response.headers.get("location");
|
|
234
|
+
const target = location === null ? null : URL.parse(location, from.href);
|
|
235
|
+
if (!isHTTP(target) || target.username || target.password) {
|
|
236
|
+
throw new SynthientError("export redirect has no valid location");
|
|
237
|
+
}
|
|
238
|
+
if (from.protocol === "https:" && target.protocol !== "https:") {
|
|
239
|
+
throw new SynthientError("export redirect does not use https");
|
|
240
|
+
}
|
|
241
|
+
return target;
|
|
242
|
+
}
|
|
243
|
+
export async function wait(ms, signal) {
|
|
244
|
+
try {
|
|
245
|
+
await sleep(ms, undefined, { signal });
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
// timers/promises rejects with an AbortError, not the signal's reason.
|
|
249
|
+
throw signal?.aborted ? signal.reason : error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
export function backoff(attempt) {
|
|
253
|
+
return Math.min(1000 * 2 ** Math.min(attempt, 5), MAX_BACKOFF) * (0.75 + Math.random() / 2);
|
|
254
|
+
}
|
|
255
|
+
function transient(error, signal) {
|
|
256
|
+
if (signal?.aborted) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
if (error instanceof APITimeoutError) {
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
if (!(error instanceof APIConnectionError)) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
let cause = error.cause;
|
|
266
|
+
for (let depth = 0; depth < 8 && typeof cause === "object" && cause !== null; depth++) {
|
|
267
|
+
if ("code" in cause && TRANSIENT_CODES.has(String(cause.code))) {
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
cause = "cause" in cause ? cause.cause : undefined;
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
function watchedBody(response, attempt) {
|
|
275
|
+
if (!response.body) {
|
|
276
|
+
return new ReadableStream({
|
|
277
|
+
start(controller) {
|
|
278
|
+
controller.close();
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
const reader = response.body.getReader();
|
|
283
|
+
return new ReadableStream({
|
|
284
|
+
async pull(controller) {
|
|
285
|
+
const { done, value } = await attempt.watch(reader.read());
|
|
286
|
+
if (done) {
|
|
287
|
+
controller.close();
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
controller.enqueue(value);
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
cancel(reason) {
|
|
294
|
+
return reader.cancel(reason).catch(() => undefined);
|
|
295
|
+
},
|
|
296
|
+
}, { highWaterMark: 0 });
|
|
297
|
+
}
|
|
298
|
+
/** Returns false with the first `limit` bytes when the body is longer than `limit`. */
|
|
299
|
+
async function read(response, attempt, limit) {
|
|
300
|
+
if (!response.body) {
|
|
301
|
+
return [new Uint8Array(), true];
|
|
302
|
+
}
|
|
303
|
+
const reader = response.body.getReader();
|
|
304
|
+
const chunks = [];
|
|
305
|
+
let size = 0;
|
|
306
|
+
try {
|
|
307
|
+
for (;;) {
|
|
308
|
+
const { done, value } = await attempt.watch(reader.read());
|
|
309
|
+
if (done) {
|
|
310
|
+
return [Buffer.concat(chunks, size), true];
|
|
311
|
+
}
|
|
312
|
+
if (size + value.byteLength > limit) {
|
|
313
|
+
chunks.push(value.subarray(0, limit - size));
|
|
314
|
+
return [Buffer.concat(chunks, limit), false];
|
|
315
|
+
}
|
|
316
|
+
chunks.push(value);
|
|
317
|
+
size += value.byteLength;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
finally {
|
|
321
|
+
reader.cancel().catch(() => undefined);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** Discards a body so its connection can be reused. */
|
|
325
|
+
export async function drain(response, attempt) {
|
|
326
|
+
await read(response, attempt, MAX_ERROR_SIZE).catch(() => undefined);
|
|
327
|
+
attempt.caller?.throwIfAborted();
|
|
328
|
+
}
|