@bananapus/nana-sdk-core 1.6.1 → 1.8.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/cjs/utils/bendystraw.d.ts +57 -0
- package/dist/cjs/utils/bendystraw.d.ts.map +1 -0
- package/dist/cjs/utils/bendystraw.js +236 -0
- package/dist/cjs/utils/bendystraw.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +2 -0
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +2 -0
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/timeSeries.d.ts +14 -0
- package/dist/cjs/utils/timeSeries.d.ts.map +1 -0
- package/dist/cjs/utils/timeSeries.js +67 -0
- package/dist/cjs/utils/timeSeries.js.map +1 -0
- package/dist/esm/utils/bendystraw.d.ts +57 -0
- package/dist/esm/utils/bendystraw.d.ts.map +1 -0
- package/dist/esm/utils/bendystraw.js +227 -0
- package/dist/esm/utils/bendystraw.js.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/timeSeries.d.ts +14 -0
- package/dist/esm/utils/timeSeries.d.ts.map +1 -0
- package/dist/esm/utils/timeSeries.js +64 -0
- package/dist/esm/utils/timeSeries.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export declare const BENDYSTRAW_TIMEOUT_MS = 15000;
|
|
2
|
+
export declare const MAX_BENDYSTRAW_RESPONSE_BYTES: number;
|
|
3
|
+
export declare const BENDYSTRAW_PROJECT_REF_BATCH_SIZE = 200;
|
|
4
|
+
export type BendystrawFilter = Record<string, unknown>;
|
|
5
|
+
export type BendystrawProjectRef = {
|
|
6
|
+
chainId: number;
|
|
7
|
+
projectId: number;
|
|
8
|
+
version?: number;
|
|
9
|
+
};
|
|
10
|
+
export type BendystrawProjectRow = {
|
|
11
|
+
chainId?: unknown;
|
|
12
|
+
projectId?: unknown;
|
|
13
|
+
version?: unknown;
|
|
14
|
+
};
|
|
15
|
+
export declare class BendystrawRequestError extends Error {
|
|
16
|
+
readonly status?: number | undefined;
|
|
17
|
+
constructor(message: string, status?: number | undefined);
|
|
18
|
+
}
|
|
19
|
+
export type BendystrawRequestOptions = {
|
|
20
|
+
fetch?: typeof fetch;
|
|
21
|
+
maxResponseBytes?: number;
|
|
22
|
+
retryDelaysMs?: readonly number[];
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Build an explicit conjunction for one versioned deployment. Keeping each
|
|
28
|
+
* identity field in its own `AND` member prevents accidental project/chain
|
|
29
|
+
* cross-products when multiple deployments are combined with `OR`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bendystrawProjectRefFilter(ref: BendystrawProjectRef): BendystrawFilter;
|
|
32
|
+
/**
|
|
33
|
+
* Build an exact filter for one or more deployments. An empty list throws
|
|
34
|
+
* instead of silently producing a broad, unscoped indexer query.
|
|
35
|
+
*/
|
|
36
|
+
export declare function bendystrawProjectRefsFilter(refs: readonly BendystrawProjectRef[]): BendystrawFilter;
|
|
37
|
+
/**
|
|
38
|
+
* Build independently queryable exact-deployment filters without handing an
|
|
39
|
+
* indexer one unbounded `OR`. Empty input produces no queries; malformed refs
|
|
40
|
+
* still fail closed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function bendystrawProjectRefsFilters(refs: readonly BendystrawProjectRef[], batchSize?: number): BendystrawFilter[];
|
|
43
|
+
/**
|
|
44
|
+
* Verify that an indexer row belongs to one of the requested deployments.
|
|
45
|
+
* Use this at the response boundary as defense in depth against stale schemas,
|
|
46
|
+
* permissive filters, and indexer regressions.
|
|
47
|
+
*/
|
|
48
|
+
export declare function matchesBendystrawProjectRef(row: BendystrawProjectRow, refs: readonly BendystrawProjectRef[]): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Execute a bounded, timeout-protected Bendystraw GraphQL request.
|
|
51
|
+
*
|
|
52
|
+
* The helper retries only transient transport failures. GraphQL errors,
|
|
53
|
+
* invalid envelopes, invalid content types, oversized bodies, and caller
|
|
54
|
+
* cancellation fail closed without being retried.
|
|
55
|
+
*/
|
|
56
|
+
export declare function requestBendystraw<TResult, TVariables extends object = Record<string, never>>(endpoint: string, query: string, variables: TVariables, options?: BendystrawRequestOptions): Promise<TResult>;
|
|
57
|
+
//# sourceMappingURL=bendystraw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bendystraw.d.ts","sourceRoot":"","sources":["../../../src/utils/bendystraw.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,QAAS,CAAC;AAC5C,eAAO,MAAM,6BAA6B,QAAkB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,MAAM,CAAC;AAKrD,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEvD,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,qBAAa,sBAAuB,SAAQ,KAAK;IAG7C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM;gBADxB,OAAO,EAAE,MAAM,EACN,MAAM,CAAC,EAAE,MAAM,YAAA;CAK3B;AAED,MAAM,MAAM,wBAAwB,GAAG;IACrC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAmBF;;;;GAIG;AACH,wBAAgB,0BAA0B,CACxC,GAAG,EAAE,oBAAoB,GACxB,gBAAgB,CASlB;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,SAAS,oBAAoB,EAAE,GACpC,gBAAgB,CAgBlB;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,SAAS,oBAAoB,EAAE,EACrC,SAAS,SAAoC,GAC5C,gBAAgB,EAAE,CAwBpB;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,GAAG,EAAE,oBAAoB,EACzB,IAAI,EAAE,SAAS,oBAAoB,EAAE,GACpC,OAAO,CAmBT;AA6DD;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EACP,UAAU,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAEjD,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,UAAU,EACrB,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,OAAO,CAAC,CA+FlB"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BendystrawRequestError = exports.BENDYSTRAW_PROJECT_REF_BATCH_SIZE = exports.MAX_BENDYSTRAW_RESPONSE_BYTES = exports.BENDYSTRAW_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.bendystrawProjectRefFilter = bendystrawProjectRefFilter;
|
|
5
|
+
exports.bendystrawProjectRefsFilter = bendystrawProjectRefsFilter;
|
|
6
|
+
exports.bendystrawProjectRefsFilters = bendystrawProjectRefsFilters;
|
|
7
|
+
exports.matchesBendystrawProjectRef = matchesBendystrawProjectRef;
|
|
8
|
+
exports.requestBendystraw = requestBendystraw;
|
|
9
|
+
exports.BENDYSTRAW_TIMEOUT_MS = 15_000;
|
|
10
|
+
exports.MAX_BENDYSTRAW_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
11
|
+
exports.BENDYSTRAW_PROJECT_REF_BATCH_SIZE = 200;
|
|
12
|
+
const RETRY_DELAYS_MS = [250, 750];
|
|
13
|
+
const RETRYABLE_STATUSES = new Set([408, 429, 500, 502, 503, 504]);
|
|
14
|
+
class BendystrawRequestError extends Error {
|
|
15
|
+
constructor(message, status) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.name = "BendystrawRequestError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.BendystrawRequestError = BendystrawRequestError;
|
|
22
|
+
function normalizeProjectRef(ref) {
|
|
23
|
+
const version = ref.version ?? 6;
|
|
24
|
+
if (!Number.isSafeInteger(ref.chainId) ||
|
|
25
|
+
ref.chainId <= 0 ||
|
|
26
|
+
!Number.isSafeInteger(ref.projectId) ||
|
|
27
|
+
ref.projectId <= 0 ||
|
|
28
|
+
!Number.isSafeInteger(version) ||
|
|
29
|
+
version <= 0) {
|
|
30
|
+
throw new TypeError("Invalid Bendystraw project reference");
|
|
31
|
+
}
|
|
32
|
+
return { chainId: ref.chainId, projectId: ref.projectId, version };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Build an explicit conjunction for one versioned deployment. Keeping each
|
|
36
|
+
* identity field in its own `AND` member prevents accidental project/chain
|
|
37
|
+
* cross-products when multiple deployments are combined with `OR`.
|
|
38
|
+
*/
|
|
39
|
+
function bendystrawProjectRefFilter(ref) {
|
|
40
|
+
const normalized = normalizeProjectRef(ref);
|
|
41
|
+
return {
|
|
42
|
+
AND: [
|
|
43
|
+
{ chainId: normalized.chainId },
|
|
44
|
+
{ projectId: normalized.projectId },
|
|
45
|
+
{ version: normalized.version },
|
|
46
|
+
],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Build an exact filter for one or more deployments. An empty list throws
|
|
51
|
+
* instead of silently producing a broad, unscoped indexer query.
|
|
52
|
+
*/
|
|
53
|
+
function bendystrawProjectRefsFilter(refs) {
|
|
54
|
+
const unique = new Map();
|
|
55
|
+
for (const ref of refs) {
|
|
56
|
+
const normalized = normalizeProjectRef(ref);
|
|
57
|
+
unique.set(`${normalized.chainId}:${normalized.projectId}:${normalized.version}`, normalized);
|
|
58
|
+
}
|
|
59
|
+
const filters = [...unique.values()].map(bendystrawProjectRefFilter);
|
|
60
|
+
if (filters.length === 0) {
|
|
61
|
+
throw new TypeError("At least one Bendystraw project reference is required");
|
|
62
|
+
}
|
|
63
|
+
return filters.length === 1 ? filters[0] : { OR: filters };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Build independently queryable exact-deployment filters without handing an
|
|
67
|
+
* indexer one unbounded `OR`. Empty input produces no queries; malformed refs
|
|
68
|
+
* still fail closed.
|
|
69
|
+
*/
|
|
70
|
+
function bendystrawProjectRefsFilters(refs, batchSize = exports.BENDYSTRAW_PROJECT_REF_BATCH_SIZE) {
|
|
71
|
+
if (!Number.isSafeInteger(batchSize) || batchSize <= 0) {
|
|
72
|
+
throw new TypeError("batchSize must be a positive safe integer");
|
|
73
|
+
}
|
|
74
|
+
const unique = new Map();
|
|
75
|
+
for (const ref of refs) {
|
|
76
|
+
const normalized = normalizeProjectRef(ref);
|
|
77
|
+
unique.set(`${normalized.chainId}:${normalized.projectId}:${normalized.version}`, normalized);
|
|
78
|
+
}
|
|
79
|
+
const normalized = [...unique.values()];
|
|
80
|
+
const batches = [];
|
|
81
|
+
for (let index = 0; index < normalized.length; index += batchSize) {
|
|
82
|
+
batches.push({
|
|
83
|
+
OR: normalized
|
|
84
|
+
.slice(index, index + batchSize)
|
|
85
|
+
.map(bendystrawProjectRefFilter),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return batches;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Verify that an indexer row belongs to one of the requested deployments.
|
|
92
|
+
* Use this at the response boundary as defense in depth against stale schemas,
|
|
93
|
+
* permissive filters, and indexer regressions.
|
|
94
|
+
*/
|
|
95
|
+
function matchesBendystrawProjectRef(row, refs) {
|
|
96
|
+
const chainId = Number(row.chainId);
|
|
97
|
+
const projectId = Number(row.projectId);
|
|
98
|
+
const version = Number(row.version);
|
|
99
|
+
if (!Number.isSafeInteger(chainId) ||
|
|
100
|
+
!Number.isSafeInteger(projectId) ||
|
|
101
|
+
!Number.isSafeInteger(version)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return refs.some((ref) => {
|
|
105
|
+
const normalized = normalizeProjectRef(ref);
|
|
106
|
+
return (chainId === normalized.chainId &&
|
|
107
|
+
projectId === normalized.projectId &&
|
|
108
|
+
version === normalized.version);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async function readBoundedBody(response, maxResponseBytes) {
|
|
112
|
+
const declaredSize = Number(response.headers.get("content-length") ?? 0);
|
|
113
|
+
if (Number.isFinite(declaredSize) && declaredSize > maxResponseBytes) {
|
|
114
|
+
await response.body?.cancel();
|
|
115
|
+
throw new BendystrawRequestError("Bendystraw response exceeds the size limit", 502);
|
|
116
|
+
}
|
|
117
|
+
if (!response.headers.get("content-type")?.toLowerCase().includes("json")) {
|
|
118
|
+
await response.body?.cancel();
|
|
119
|
+
throw new BendystrawRequestError("Bendystraw returned an invalid content type", 502);
|
|
120
|
+
}
|
|
121
|
+
if (!response.body)
|
|
122
|
+
return new Uint8Array();
|
|
123
|
+
const reader = response.body.getReader();
|
|
124
|
+
const chunks = [];
|
|
125
|
+
let total = 0;
|
|
126
|
+
try {
|
|
127
|
+
for (;;) {
|
|
128
|
+
const { done, value } = await reader.read();
|
|
129
|
+
if (done)
|
|
130
|
+
break;
|
|
131
|
+
total += value.byteLength;
|
|
132
|
+
if (total > maxResponseBytes) {
|
|
133
|
+
await reader.cancel("Bendystraw response exceeds the size limit");
|
|
134
|
+
throw new BendystrawRequestError("Bendystraw response exceeds the size limit", 502);
|
|
135
|
+
}
|
|
136
|
+
chunks.push(value);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
reader.releaseLock();
|
|
141
|
+
}
|
|
142
|
+
const body = new Uint8Array(total);
|
|
143
|
+
let offset = 0;
|
|
144
|
+
for (const chunk of chunks) {
|
|
145
|
+
body.set(chunk, offset);
|
|
146
|
+
offset += chunk.byteLength;
|
|
147
|
+
}
|
|
148
|
+
return body;
|
|
149
|
+
}
|
|
150
|
+
function abortName(error) {
|
|
151
|
+
return error instanceof Error ? error.name : undefined;
|
|
152
|
+
}
|
|
153
|
+
function delay(milliseconds) {
|
|
154
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Execute a bounded, timeout-protected Bendystraw GraphQL request.
|
|
158
|
+
*
|
|
159
|
+
* The helper retries only transient transport failures. GraphQL errors,
|
|
160
|
+
* invalid envelopes, invalid content types, oversized bodies, and caller
|
|
161
|
+
* cancellation fail closed without being retried.
|
|
162
|
+
*/
|
|
163
|
+
async function requestBendystraw(endpoint, query, variables, options = {}) {
|
|
164
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
165
|
+
const maxResponseBytes = options.maxResponseBytes ?? exports.MAX_BENDYSTRAW_RESPONSE_BYTES;
|
|
166
|
+
const retryDelaysMs = options.retryDelaysMs ?? RETRY_DELAYS_MS;
|
|
167
|
+
const timeoutMs = options.timeoutMs ?? exports.BENDYSTRAW_TIMEOUT_MS;
|
|
168
|
+
if (!Number.isSafeInteger(maxResponseBytes) || maxResponseBytes <= 0) {
|
|
169
|
+
throw new TypeError("maxResponseBytes must be a positive safe integer");
|
|
170
|
+
}
|
|
171
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
172
|
+
throw new TypeError("timeoutMs must be positive");
|
|
173
|
+
}
|
|
174
|
+
for (let attempt = 0;; attempt += 1) {
|
|
175
|
+
const controller = new AbortController();
|
|
176
|
+
const abort = () => controller.abort(options.signal?.reason);
|
|
177
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
178
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
179
|
+
try {
|
|
180
|
+
if (options.signal?.aborted)
|
|
181
|
+
abort();
|
|
182
|
+
const response = await fetchImpl(endpoint, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers: {
|
|
185
|
+
Accept: "application/graphql-response+json, application/json",
|
|
186
|
+
"Content-Type": "application/json",
|
|
187
|
+
},
|
|
188
|
+
body: JSON.stringify({ query, variables }),
|
|
189
|
+
signal: controller.signal,
|
|
190
|
+
});
|
|
191
|
+
if (RETRYABLE_STATUSES.has(response.status) &&
|
|
192
|
+
attempt < retryDelaysMs.length) {
|
|
193
|
+
await response.body?.cancel();
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const bytes = await readBoundedBody(response, maxResponseBytes);
|
|
197
|
+
let envelope;
|
|
198
|
+
try {
|
|
199
|
+
envelope = JSON.parse(new TextDecoder().decode(bytes));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
throw new BendystrawRequestError("Bendystraw returned invalid JSON", 502);
|
|
203
|
+
}
|
|
204
|
+
if (!response.ok) {
|
|
205
|
+
throw new BendystrawRequestError(`Bendystraw request failed (${response.status})`, response.status);
|
|
206
|
+
}
|
|
207
|
+
const message = envelope.errors?.find((error) => typeof error?.message === "string")?.message;
|
|
208
|
+
if (envelope.errors?.length) {
|
|
209
|
+
throw new BendystrawRequestError(typeof message === "string"
|
|
210
|
+
? message.slice(0, 500)
|
|
211
|
+
: "Bendystraw query failed", 502);
|
|
212
|
+
}
|
|
213
|
+
if (!Object.prototype.hasOwnProperty.call(envelope, "data")) {
|
|
214
|
+
throw new BendystrawRequestError("Bendystraw response is missing data", 502);
|
|
215
|
+
}
|
|
216
|
+
return envelope.data;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
const aborted = options.signal?.aborted ||
|
|
221
|
+
abortName(error) === "AbortError" ||
|
|
222
|
+
abortName(error) === "TimeoutError";
|
|
223
|
+
if (aborted ||
|
|
224
|
+
error instanceof BendystrawRequestError ||
|
|
225
|
+
attempt >= retryDelaysMs.length) {
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
clearTimeout(timeout);
|
|
231
|
+
options.signal?.removeEventListener("abort", abort);
|
|
232
|
+
}
|
|
233
|
+
await delay(retryDelaysMs[attempt]);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=bendystraw.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bendystraw.js","sourceRoot":"","sources":["../../../src/utils/bendystraw.ts"],"names":[],"mappings":";;;AA6DA,gEAWC;AAMD,kEAkBC;AAOD,oEA2BC;AAOD,kEAsBC;AAoED,8CAuGC;AA1UY,QAAA,qBAAqB,GAAG,MAAM,CAAC;AAC/B,QAAA,6BAA6B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAChD,QAAA,iCAAiC,GAAG,GAAG,CAAC;AAErD,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,CAAU,CAAC;AAC5C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAgBnE,MAAa,sBAAuB,SAAQ,KAAK;IAC/C,YACE,OAAe,EACN,MAAe;QAExB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,WAAM,GAAN,MAAM,CAAS;QAGxB,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AARD,wDAQC;AAUD,SAA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
export * from "./contracts.js";
|
|
2
|
+
export * from "./bendystraw.js";
|
|
2
3
|
export * from "./data.js";
|
|
3
4
|
export * from "./debug.js";
|
|
4
5
|
export * from "./deploy.js";
|
|
@@ -8,6 +9,7 @@ export * from "./hook.js";
|
|
|
8
9
|
export * from "./ipfs.js";
|
|
9
10
|
export * from "./revnet.js";
|
|
10
11
|
export * from "./ruleset.js";
|
|
12
|
+
export * from "./timeSeries.js";
|
|
11
13
|
export * from "./token.js";
|
|
12
14
|
export * from "./tx.js";
|
|
13
15
|
export * from "./urn.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./contracts.js"), exports);
|
|
18
|
+
__exportStar(require("./bendystraw.js"), exports);
|
|
18
19
|
__exportStar(require("./data.js"), exports);
|
|
19
20
|
__exportStar(require("./debug.js"), exports);
|
|
20
21
|
__exportStar(require("./deploy.js"), exports);
|
|
@@ -24,6 +25,7 @@ __exportStar(require("./hook.js"), exports);
|
|
|
24
25
|
__exportStar(require("./ipfs.js"), exports);
|
|
25
26
|
__exportStar(require("./revnet.js"), exports);
|
|
26
27
|
__exportStar(require("./ruleset.js"), exports);
|
|
28
|
+
__exportStar(require("./timeSeries.js"), exports);
|
|
27
29
|
__exportStar(require("./token.js"), exports);
|
|
28
30
|
__exportStar(require("./tx.js"), exports);
|
|
29
31
|
__exportStar(require("./urn.js"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,4CAA0B;AAC1B,6CAA2B;AAC3B,8CAA4B;AAC5B,2CAAyB;AACzB,8CAA4B;AAC5B,4CAA0B;AAC1B,4CAA0B;AAC1B,8CAA4B;AAC5B,+CAA6B;AAC7B,6CAA2B;AAC3B,0CAAwB;AACxB,2CAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,kDAAgC;AAChC,4CAA0B;AAC1B,6CAA2B;AAC3B,8CAA4B;AAC5B,2CAAyB;AACzB,8CAA4B;AAC5B,4CAA0B;AAC1B,4CAA0B;AAC1B,8CAA4B;AAC5B,+CAA6B;AAC7B,kDAAgC;AAChC,6CAA2B;AAC3B,0CAAwB;AACxB,2CAAyB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downsample an ordered time series with the Largest-Triangle-Three-Buckets
|
|
3
|
+
* algorithm.
|
|
4
|
+
*
|
|
5
|
+
* The returned series never mutates `rows`, retains both endpoints whenever
|
|
6
|
+
* `maxPoints` is at least two, and preserves visually significant intermediate
|
|
7
|
+
* points better than fixed-interval sampling. Callers should pass rows in
|
|
8
|
+
* ascending X order.
|
|
9
|
+
*
|
|
10
|
+
* Non-finite projected coordinates are treated as zero so malformed source
|
|
11
|
+
* observations cannot make every triangle area `NaN`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function downsampleTimeSeries<T>(rows: readonly T[], maxPoints: number, xOf: (row: T) => number, yOf: (row: T) => number): T[];
|
|
14
|
+
//# sourceMappingURL=timeSeries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.d.ts","sourceRoot":"","sources":["../../../src/utils/timeSeries.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,IAAI,EAAE,SAAS,CAAC,EAAE,EAClB,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,EACvB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,GACtB,CAAC,EAAE,CA4DL"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downsampleTimeSeries = downsampleTimeSeries;
|
|
4
|
+
/**
|
|
5
|
+
* Downsample an ordered time series with the Largest-Triangle-Three-Buckets
|
|
6
|
+
* algorithm.
|
|
7
|
+
*
|
|
8
|
+
* The returned series never mutates `rows`, retains both endpoints whenever
|
|
9
|
+
* `maxPoints` is at least two, and preserves visually significant intermediate
|
|
10
|
+
* points better than fixed-interval sampling. Callers should pass rows in
|
|
11
|
+
* ascending X order.
|
|
12
|
+
*
|
|
13
|
+
* Non-finite projected coordinates are treated as zero so malformed source
|
|
14
|
+
* observations cannot make every triangle area `NaN`.
|
|
15
|
+
*/
|
|
16
|
+
function downsampleTimeSeries(rows, maxPoints, xOf, yOf) {
|
|
17
|
+
if (!Number.isSafeInteger(maxPoints) || maxPoints < 0) {
|
|
18
|
+
throw new RangeError("maxPoints must be a non-negative safe integer");
|
|
19
|
+
}
|
|
20
|
+
if (maxPoints === 0 || rows.length === 0)
|
|
21
|
+
return [];
|
|
22
|
+
if (rows.length <= maxPoints)
|
|
23
|
+
return rows.slice();
|
|
24
|
+
if (maxPoints < 3) {
|
|
25
|
+
return [rows[0], rows[rows.length - 1]].slice(0, maxPoints);
|
|
26
|
+
}
|
|
27
|
+
const x = rows.map((row) => finite(xOf(row)));
|
|
28
|
+
const y = rows.map((row) => finite(yOf(row)));
|
|
29
|
+
const sampled = [rows[0]];
|
|
30
|
+
const bucketWidth = (rows.length - 2) / (maxPoints - 2);
|
|
31
|
+
let anchorIndex = 0;
|
|
32
|
+
for (let bucketIndex = 0; bucketIndex < maxPoints - 2; bucketIndex += 1) {
|
|
33
|
+
const averageStart = Math.floor((bucketIndex + 1) * bucketWidth) + 1;
|
|
34
|
+
const averageEnd = Math.min(Math.floor((bucketIndex + 2) * bucketWidth) + 1, rows.length);
|
|
35
|
+
const averageLength = averageEnd - averageStart;
|
|
36
|
+
let averageX = 0;
|
|
37
|
+
let averageY = 0;
|
|
38
|
+
for (let index = averageStart; index < averageEnd; index += 1) {
|
|
39
|
+
averageX += x[index];
|
|
40
|
+
averageY += y[index];
|
|
41
|
+
}
|
|
42
|
+
averageX /= averageLength;
|
|
43
|
+
averageY /= averageLength;
|
|
44
|
+
const rangeStart = Math.floor(bucketIndex * bucketWidth) + 1;
|
|
45
|
+
const rangeEnd = Math.min(Math.floor((bucketIndex + 1) * bucketWidth) + 1, rows.length - 1);
|
|
46
|
+
const anchorX = x[anchorIndex];
|
|
47
|
+
const anchorY = y[anchorIndex];
|
|
48
|
+
let largestArea = -1;
|
|
49
|
+
let selectedIndex = rangeStart;
|
|
50
|
+
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
51
|
+
const area = Math.abs((anchorX - averageX) * (y[index] - anchorY) -
|
|
52
|
+
(anchorX - x[index]) * (averageY - anchorY));
|
|
53
|
+
if (area > largestArea) {
|
|
54
|
+
largestArea = area;
|
|
55
|
+
selectedIndex = index;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
sampled.push(rows[selectedIndex]);
|
|
59
|
+
anchorIndex = selectedIndex;
|
|
60
|
+
}
|
|
61
|
+
sampled.push(rows[rows.length - 1]);
|
|
62
|
+
return sampled;
|
|
63
|
+
}
|
|
64
|
+
function finite(value) {
|
|
65
|
+
return Number.isFinite(value) ? value : 0;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=timeSeries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.js","sourceRoot":"","sources":["../../../src/utils/timeSeries.ts"],"names":[],"mappings":";;AAYA,oDAiEC;AA7ED;;;;;;;;;;;GAWG;AACH,SAAgB,oBAAoB,CAClC,IAAkB,EAClB,SAAiB,EACjB,GAAuB,EACvB,GAAuB;IAEvB,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,UAAU,CAAC,+CAA+C,CAAC,CAAC;IACxE,CAAC;IACD,IAAI,SAAS,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACpD,IAAI,IAAI,CAAC,MAAM,IAAI,SAAS;QAAE,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IAClD,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,OAAO,CAAC,IAAI,CAAC,CAAC,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AC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC,CAAC;IACrC,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,MAAM,CAAC,KAAa;IAC3B,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export declare const BENDYSTRAW_TIMEOUT_MS = 15000;
|
|
2
|
+
export declare const MAX_BENDYSTRAW_RESPONSE_BYTES: number;
|
|
3
|
+
export declare const BENDYSTRAW_PROJECT_REF_BATCH_SIZE = 200;
|
|
4
|
+
export type BendystrawFilter = Record<string, unknown>;
|
|
5
|
+
export type BendystrawProjectRef = {
|
|
6
|
+
chainId: number;
|
|
7
|
+
projectId: number;
|
|
8
|
+
version?: number;
|
|
9
|
+
};
|
|
10
|
+
export type BendystrawProjectRow = {
|
|
11
|
+
chainId?: unknown;
|
|
12
|
+
projectId?: unknown;
|
|
13
|
+
version?: unknown;
|
|
14
|
+
};
|
|
15
|
+
export declare class BendystrawRequestError extends Error {
|
|
16
|
+
readonly status?: number | undefined;
|
|
17
|
+
constructor(message: string, status?: number | undefined);
|
|
18
|
+
}
|
|
19
|
+
export type BendystrawRequestOptions = {
|
|
20
|
+
fetch?: typeof fetch;
|
|
21
|
+
maxResponseBytes?: number;
|
|
22
|
+
retryDelaysMs?: readonly number[];
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Build an explicit conjunction for one versioned deployment. Keeping each
|
|
28
|
+
* identity field in its own `AND` member prevents accidental project/chain
|
|
29
|
+
* cross-products when multiple deployments are combined with `OR`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bendystrawProjectRefFilter(ref: BendystrawProjectRef): BendystrawFilter;
|
|
32
|
+
/**
|
|
33
|
+
* Build an exact filter for one or more deployments. An empty list throws
|
|
34
|
+
* instead of silently producing a broad, unscoped indexer query.
|
|
35
|
+
*/
|
|
36
|
+
export declare function bendystrawProjectRefsFilter(refs: readonly BendystrawProjectRef[]): BendystrawFilter;
|
|
37
|
+
/**
|
|
38
|
+
* Build independently queryable exact-deployment filters without handing an
|
|
39
|
+
* indexer one unbounded `OR`. Empty input produces no queries; malformed refs
|
|
40
|
+
* still fail closed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function bendystrawProjectRefsFilters(refs: readonly BendystrawProjectRef[], batchSize?: number): BendystrawFilter[];
|
|
43
|
+
/**
|
|
44
|
+
* Verify that an indexer row belongs to one of the requested deployments.
|
|
45
|
+
* Use this at the response boundary as defense in depth against stale schemas,
|
|
46
|
+
* permissive filters, and indexer regressions.
|
|
47
|
+
*/
|
|
48
|
+
export declare function matchesBendystrawProjectRef(row: BendystrawProjectRow, refs: readonly BendystrawProjectRef[]): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Execute a bounded, timeout-protected Bendystraw GraphQL request.
|
|
51
|
+
*
|
|
52
|
+
* The helper retries only transient transport failures. GraphQL errors,
|
|
53
|
+
* invalid envelopes, invalid content types, oversized bodies, and caller
|
|
54
|
+
* cancellation fail closed without being retried.
|
|
55
|
+
*/
|
|
56
|
+
export declare function requestBendystraw<TResult, TVariables extends object = Record<string, never>>(endpoint: string, query: string, variables: TVariables, options?: BendystrawRequestOptions): Promise<TResult>;
|
|
57
|
+
//# sourceMappingURL=bendystraw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bendystraw.d.ts","sourceRoot":"","sources":["../../../src/utils/bendystraw.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,QAAS,CAAC;AAC5C,eAAO,MAAM,6BAA6B,QAAkB,CAAC;AAC7D,eAAO,MAAM,iCAAiC,MAAM,CAAC;AAKrD,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAA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C,EAEjD,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,UAAU,EACrB,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,OAAO,CAAC,CA+FlB"}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
export const BENDYSTRAW_TIMEOUT_MS = 15_000;
|
|
2
|
+
export const MAX_BENDYSTRAW_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
3
|
+
export const BENDYSTRAW_PROJECT_REF_BATCH_SIZE = 200;
|
|
4
|
+
const RETRY_DELAYS_MS = [250, 750];
|
|
5
|
+
const RETRYABLE_STATUSES = new Set([408, 429, 500, 502, 503, 504]);
|
|
6
|
+
export class BendystrawRequestError extends Error {
|
|
7
|
+
constructor(message, status) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.name = "BendystrawRequestError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function normalizeProjectRef(ref) {
|
|
14
|
+
const version = ref.version ?? 6;
|
|
15
|
+
if (!Number.isSafeInteger(ref.chainId) ||
|
|
16
|
+
ref.chainId <= 0 ||
|
|
17
|
+
!Number.isSafeInteger(ref.projectId) ||
|
|
18
|
+
ref.projectId <= 0 ||
|
|
19
|
+
!Number.isSafeInteger(version) ||
|
|
20
|
+
version <= 0) {
|
|
21
|
+
throw new TypeError("Invalid Bendystraw project reference");
|
|
22
|
+
}
|
|
23
|
+
return { chainId: ref.chainId, projectId: ref.projectId, version };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Build an explicit conjunction for one versioned deployment. Keeping each
|
|
27
|
+
* identity field in its own `AND` member prevents accidental project/chain
|
|
28
|
+
* cross-products when multiple deployments are combined with `OR`.
|
|
29
|
+
*/
|
|
30
|
+
export function bendystrawProjectRefFilter(ref) {
|
|
31
|
+
const normalized = normalizeProjectRef(ref);
|
|
32
|
+
return {
|
|
33
|
+
AND: [
|
|
34
|
+
{ chainId: normalized.chainId },
|
|
35
|
+
{ projectId: normalized.projectId },
|
|
36
|
+
{ version: normalized.version },
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build an exact filter for one or more deployments. An empty list throws
|
|
42
|
+
* instead of silently producing a broad, unscoped indexer query.
|
|
43
|
+
*/
|
|
44
|
+
export function bendystrawProjectRefsFilter(refs) {
|
|
45
|
+
const unique = new Map();
|
|
46
|
+
for (const ref of refs) {
|
|
47
|
+
const normalized = normalizeProjectRef(ref);
|
|
48
|
+
unique.set(`${normalized.chainId}:${normalized.projectId}:${normalized.version}`, normalized);
|
|
49
|
+
}
|
|
50
|
+
const filters = [...unique.values()].map(bendystrawProjectRefFilter);
|
|
51
|
+
if (filters.length === 0) {
|
|
52
|
+
throw new TypeError("At least one Bendystraw project reference is required");
|
|
53
|
+
}
|
|
54
|
+
return filters.length === 1 ? filters[0] : { OR: filters };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Build independently queryable exact-deployment filters without handing an
|
|
58
|
+
* indexer one unbounded `OR`. Empty input produces no queries; malformed refs
|
|
59
|
+
* still fail closed.
|
|
60
|
+
*/
|
|
61
|
+
export function bendystrawProjectRefsFilters(refs, batchSize = BENDYSTRAW_PROJECT_REF_BATCH_SIZE) {
|
|
62
|
+
if (!Number.isSafeInteger(batchSize) || batchSize <= 0) {
|
|
63
|
+
throw new TypeError("batchSize must be a positive safe integer");
|
|
64
|
+
}
|
|
65
|
+
const unique = new Map();
|
|
66
|
+
for (const ref of refs) {
|
|
67
|
+
const normalized = normalizeProjectRef(ref);
|
|
68
|
+
unique.set(`${normalized.chainId}:${normalized.projectId}:${normalized.version}`, normalized);
|
|
69
|
+
}
|
|
70
|
+
const normalized = [...unique.values()];
|
|
71
|
+
const batches = [];
|
|
72
|
+
for (let index = 0; index < normalized.length; index += batchSize) {
|
|
73
|
+
batches.push({
|
|
74
|
+
OR: normalized
|
|
75
|
+
.slice(index, index + batchSize)
|
|
76
|
+
.map(bendystrawProjectRefFilter),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return batches;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Verify that an indexer row belongs to one of the requested deployments.
|
|
83
|
+
* Use this at the response boundary as defense in depth against stale schemas,
|
|
84
|
+
* permissive filters, and indexer regressions.
|
|
85
|
+
*/
|
|
86
|
+
export function matchesBendystrawProjectRef(row, refs) {
|
|
87
|
+
const chainId = Number(row.chainId);
|
|
88
|
+
const projectId = Number(row.projectId);
|
|
89
|
+
const version = Number(row.version);
|
|
90
|
+
if (!Number.isSafeInteger(chainId) ||
|
|
91
|
+
!Number.isSafeInteger(projectId) ||
|
|
92
|
+
!Number.isSafeInteger(version)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return refs.some((ref) => {
|
|
96
|
+
const normalized = normalizeProjectRef(ref);
|
|
97
|
+
return (chainId === normalized.chainId &&
|
|
98
|
+
projectId === normalized.projectId &&
|
|
99
|
+
version === normalized.version);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async function readBoundedBody(response, maxResponseBytes) {
|
|
103
|
+
const declaredSize = Number(response.headers.get("content-length") ?? 0);
|
|
104
|
+
if (Number.isFinite(declaredSize) && declaredSize > maxResponseBytes) {
|
|
105
|
+
await response.body?.cancel();
|
|
106
|
+
throw new BendystrawRequestError("Bendystraw response exceeds the size limit", 502);
|
|
107
|
+
}
|
|
108
|
+
if (!response.headers.get("content-type")?.toLowerCase().includes("json")) {
|
|
109
|
+
await response.body?.cancel();
|
|
110
|
+
throw new BendystrawRequestError("Bendystraw returned an invalid content type", 502);
|
|
111
|
+
}
|
|
112
|
+
if (!response.body)
|
|
113
|
+
return new Uint8Array();
|
|
114
|
+
const reader = response.body.getReader();
|
|
115
|
+
const chunks = [];
|
|
116
|
+
let total = 0;
|
|
117
|
+
try {
|
|
118
|
+
for (;;) {
|
|
119
|
+
const { done, value } = await reader.read();
|
|
120
|
+
if (done)
|
|
121
|
+
break;
|
|
122
|
+
total += value.byteLength;
|
|
123
|
+
if (total > maxResponseBytes) {
|
|
124
|
+
await reader.cancel("Bendystraw response exceeds the size limit");
|
|
125
|
+
throw new BendystrawRequestError("Bendystraw response exceeds the size limit", 502);
|
|
126
|
+
}
|
|
127
|
+
chunks.push(value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
reader.releaseLock();
|
|
132
|
+
}
|
|
133
|
+
const body = new Uint8Array(total);
|
|
134
|
+
let offset = 0;
|
|
135
|
+
for (const chunk of chunks) {
|
|
136
|
+
body.set(chunk, offset);
|
|
137
|
+
offset += chunk.byteLength;
|
|
138
|
+
}
|
|
139
|
+
return body;
|
|
140
|
+
}
|
|
141
|
+
function abortName(error) {
|
|
142
|
+
return error instanceof Error ? error.name : undefined;
|
|
143
|
+
}
|
|
144
|
+
function delay(milliseconds) {
|
|
145
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Execute a bounded, timeout-protected Bendystraw GraphQL request.
|
|
149
|
+
*
|
|
150
|
+
* The helper retries only transient transport failures. GraphQL errors,
|
|
151
|
+
* invalid envelopes, invalid content types, oversized bodies, and caller
|
|
152
|
+
* cancellation fail closed without being retried.
|
|
153
|
+
*/
|
|
154
|
+
export async function requestBendystraw(endpoint, query, variables, options = {}) {
|
|
155
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
156
|
+
const maxResponseBytes = options.maxResponseBytes ?? MAX_BENDYSTRAW_RESPONSE_BYTES;
|
|
157
|
+
const retryDelaysMs = options.retryDelaysMs ?? RETRY_DELAYS_MS;
|
|
158
|
+
const timeoutMs = options.timeoutMs ?? BENDYSTRAW_TIMEOUT_MS;
|
|
159
|
+
if (!Number.isSafeInteger(maxResponseBytes) || maxResponseBytes <= 0) {
|
|
160
|
+
throw new TypeError("maxResponseBytes must be a positive safe integer");
|
|
161
|
+
}
|
|
162
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
163
|
+
throw new TypeError("timeoutMs must be positive");
|
|
164
|
+
}
|
|
165
|
+
for (let attempt = 0;; attempt += 1) {
|
|
166
|
+
const controller = new AbortController();
|
|
167
|
+
const abort = () => controller.abort(options.signal?.reason);
|
|
168
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
169
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
170
|
+
try {
|
|
171
|
+
if (options.signal?.aborted)
|
|
172
|
+
abort();
|
|
173
|
+
const response = await fetchImpl(endpoint, {
|
|
174
|
+
method: "POST",
|
|
175
|
+
headers: {
|
|
176
|
+
Accept: "application/graphql-response+json, application/json",
|
|
177
|
+
"Content-Type": "application/json",
|
|
178
|
+
},
|
|
179
|
+
body: JSON.stringify({ query, variables }),
|
|
180
|
+
signal: controller.signal,
|
|
181
|
+
});
|
|
182
|
+
if (RETRYABLE_STATUSES.has(response.status) &&
|
|
183
|
+
attempt < retryDelaysMs.length) {
|
|
184
|
+
await response.body?.cancel();
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
const bytes = await readBoundedBody(response, maxResponseBytes);
|
|
188
|
+
let envelope;
|
|
189
|
+
try {
|
|
190
|
+
envelope = JSON.parse(new TextDecoder().decode(bytes));
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
throw new BendystrawRequestError("Bendystraw returned invalid JSON", 502);
|
|
194
|
+
}
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
throw new BendystrawRequestError(`Bendystraw request failed (${response.status})`, response.status);
|
|
197
|
+
}
|
|
198
|
+
const message = envelope.errors?.find((error) => typeof error?.message === "string")?.message;
|
|
199
|
+
if (envelope.errors?.length) {
|
|
200
|
+
throw new BendystrawRequestError(typeof message === "string"
|
|
201
|
+
? message.slice(0, 500)
|
|
202
|
+
: "Bendystraw query failed", 502);
|
|
203
|
+
}
|
|
204
|
+
if (!Object.prototype.hasOwnProperty.call(envelope, "data")) {
|
|
205
|
+
throw new BendystrawRequestError("Bendystraw response is missing data", 502);
|
|
206
|
+
}
|
|
207
|
+
return envelope.data;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
const aborted = options.signal?.aborted ||
|
|
212
|
+
abortName(error) === "AbortError" ||
|
|
213
|
+
abortName(error) === "TimeoutError";
|
|
214
|
+
if (aborted ||
|
|
215
|
+
error instanceof BendystrawRequestError ||
|
|
216
|
+
attempt >= retryDelaysMs.length) {
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
clearTimeout(timeout);
|
|
222
|
+
options.signal?.removeEventListener("abort", abort);
|
|
223
|
+
}
|
|
224
|
+
await delay(retryDelaysMs[attempt]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=bendystraw.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bendystraw.js","sourceRoot":"","sources":["../../../src/utils/bendystraw.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAC5C,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAC7D,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAAG,CAAC;AAErD,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,CAAU,CAAC;AAC5C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAgBnE,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C,YACE,OAAe,EACN,MAAe;QAExB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,WAAM,GAAN,MAAM,CAAS;QAGxB,IAAI,CAAC,IAAI,GAAG,wBA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CAAC;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "./contracts.js";
|
|
2
|
+
export * from "./bendystraw.js";
|
|
2
3
|
export * from "./data.js";
|
|
3
4
|
export * from "./debug.js";
|
|
4
5
|
export * from "./deploy.js";
|
|
@@ -8,6 +9,7 @@ export * from "./hook.js";
|
|
|
8
9
|
export * from "./ipfs.js";
|
|
9
10
|
export * from "./revnet.js";
|
|
10
11
|
export * from "./ruleset.js";
|
|
12
|
+
export * from "./timeSeries.js";
|
|
11
13
|
export * from "./token.js";
|
|
12
14
|
export * from "./tx.js";
|
|
13
15
|
export * from "./urn.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "./contracts.js";
|
|
2
|
+
export * from "./bendystraw.js";
|
|
2
3
|
export * from "./data.js";
|
|
3
4
|
export * from "./debug.js";
|
|
4
5
|
export * from "./deploy.js";
|
|
@@ -8,6 +9,7 @@ export * from "./hook.js";
|
|
|
8
9
|
export * from "./ipfs.js";
|
|
9
10
|
export * from "./revnet.js";
|
|
10
11
|
export * from "./ruleset.js";
|
|
12
|
+
export * from "./timeSeries.js";
|
|
11
13
|
export * from "./token.js";
|
|
12
14
|
export * from "./tx.js";
|
|
13
15
|
export * from "./urn.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downsample an ordered time series with the Largest-Triangle-Three-Buckets
|
|
3
|
+
* algorithm.
|
|
4
|
+
*
|
|
5
|
+
* The returned series never mutates `rows`, retains both endpoints whenever
|
|
6
|
+
* `maxPoints` is at least two, and preserves visually significant intermediate
|
|
7
|
+
* points better than fixed-interval sampling. Callers should pass rows in
|
|
8
|
+
* ascending X order.
|
|
9
|
+
*
|
|
10
|
+
* Non-finite projected coordinates are treated as zero so malformed source
|
|
11
|
+
* observations cannot make every triangle area `NaN`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function downsampleTimeSeries<T>(rows: readonly T[], maxPoints: number, xOf: (row: T) => number, yOf: (row: T) => number): T[];
|
|
14
|
+
//# sourceMappingURL=timeSeries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.d.ts","sourceRoot":"","sources":["../../../src/utils/timeSeries.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,IAAI,EAAE,SAAS,CAAC,EAAE,EAClB,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,EACvB,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,GACtB,CAAC,EAAE,CA4DL"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downsample an ordered time series with the Largest-Triangle-Three-Buckets
|
|
3
|
+
* algorithm.
|
|
4
|
+
*
|
|
5
|
+
* The returned series never mutates `rows`, retains both endpoints whenever
|
|
6
|
+
* `maxPoints` is at least two, and preserves visually significant intermediate
|
|
7
|
+
* points better than fixed-interval sampling. Callers should pass rows in
|
|
8
|
+
* ascending X order.
|
|
9
|
+
*
|
|
10
|
+
* Non-finite projected coordinates are treated as zero so malformed source
|
|
11
|
+
* observations cannot make every triangle area `NaN`.
|
|
12
|
+
*/
|
|
13
|
+
export function downsampleTimeSeries(rows, maxPoints, xOf, yOf) {
|
|
14
|
+
if (!Number.isSafeInteger(maxPoints) || maxPoints < 0) {
|
|
15
|
+
throw new RangeError("maxPoints must be a non-negative safe integer");
|
|
16
|
+
}
|
|
17
|
+
if (maxPoints === 0 || rows.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
if (rows.length <= maxPoints)
|
|
20
|
+
return rows.slice();
|
|
21
|
+
if (maxPoints < 3) {
|
|
22
|
+
return [rows[0], rows[rows.length - 1]].slice(0, maxPoints);
|
|
23
|
+
}
|
|
24
|
+
const x = rows.map((row) => finite(xOf(row)));
|
|
25
|
+
const y = rows.map((row) => finite(yOf(row)));
|
|
26
|
+
const sampled = [rows[0]];
|
|
27
|
+
const bucketWidth = (rows.length - 2) / (maxPoints - 2);
|
|
28
|
+
let anchorIndex = 0;
|
|
29
|
+
for (let bucketIndex = 0; bucketIndex < maxPoints - 2; bucketIndex += 1) {
|
|
30
|
+
const averageStart = Math.floor((bucketIndex + 1) * bucketWidth) + 1;
|
|
31
|
+
const averageEnd = Math.min(Math.floor((bucketIndex + 2) * bucketWidth) + 1, rows.length);
|
|
32
|
+
const averageLength = averageEnd - averageStart;
|
|
33
|
+
let averageX = 0;
|
|
34
|
+
let averageY = 0;
|
|
35
|
+
for (let index = averageStart; index < averageEnd; index += 1) {
|
|
36
|
+
averageX += x[index];
|
|
37
|
+
averageY += y[index];
|
|
38
|
+
}
|
|
39
|
+
averageX /= averageLength;
|
|
40
|
+
averageY /= averageLength;
|
|
41
|
+
const rangeStart = Math.floor(bucketIndex * bucketWidth) + 1;
|
|
42
|
+
const rangeEnd = Math.min(Math.floor((bucketIndex + 1) * bucketWidth) + 1, rows.length - 1);
|
|
43
|
+
const anchorX = x[anchorIndex];
|
|
44
|
+
const anchorY = y[anchorIndex];
|
|
45
|
+
let largestArea = -1;
|
|
46
|
+
let selectedIndex = rangeStart;
|
|
47
|
+
for (let index = rangeStart; index < rangeEnd; index += 1) {
|
|
48
|
+
const area = Math.abs((anchorX - averageX) * (y[index] - anchorY) -
|
|
49
|
+
(anchorX - x[index]) * (averageY - anchorY));
|
|
50
|
+
if (area > largestArea) {
|
|
51
|
+
largestArea = area;
|
|
52
|
+
selectedIndex = index;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
sampled.push(rows[selectedIndex]);
|
|
56
|
+
anchorIndex = selectedIndex;
|
|
57
|
+
}
|
|
58
|
+
sampled.push(rows[rows.length - 1]);
|
|
59
|
+
return sampled;
|
|
60
|
+
}
|
|
61
|
+
function finite(value) {
|
|
62
|
+
return Number.isFinite(value) ? value : 0;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=timeSeries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.js","sourceRoot":"","sources":["../../../src/utils/timeSeries.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAkB,EAClB,SAAiB,EACjB,GAAuB,EACvB,GAAuB;IAEvB,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,UAAU,CAAC,+CAA+C,CAAC,CAAC;IACxE,CAAC;IACD,IAAI,SAAS,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACpD,IAAI,IAAI,CAAC,MAAM,IAAI,SAAS;QAAE,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IAClD,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAE,EAAE,IAAI,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