@settlemint/dalp-sdk 3.1.18 → 3.1.19-main.34482536277
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/README.md +156 -169
- package/dist/auth/client.d.ts +113 -0
- package/dist/auth/live.d.ts +4366 -0
- package/dist/{auth-permissions.d.ts → auth/permissions.d.ts} +1 -2
- package/dist/{auth-wallet-plugins.d.ts → auth/wallet-plugins.d.ts} +69 -5
- package/dist/auth.d.ts +22 -115
- package/dist/auth.js +329 -0
- package/dist/bundler.d.ts +21 -205
- package/dist/bundler.js +97 -0
- package/dist/chunk-09prmw53.js +4 -0
- package/dist/chunk-d0295j58.js +33 -0
- package/dist/chunk-j4sxrfzz.js +54 -0
- package/dist/chunk-qvtjb3dz.js +102 -0
- package/dist/chunk-rb8k4w3q.js +67 -0
- package/dist/chunk-shp8v4ws.js +913 -0
- package/dist/chunk-sk9nc2q3.js +10 -0
- package/dist/chunk-xp3fx2yt.js +63 -0
- package/dist/chunk-ybqqdgrd.js +260 -0
- package/dist/client/conditional-request.d.ts +30 -0
- package/dist/client/factory.d.ts +14 -0
- package/dist/client/query-serializer.d.ts +2 -0
- package/dist/client/request-compression.d.ts +47 -0
- package/dist/client/ssr-fetch.d.ts +12 -0
- package/dist/effect.d.ts +78 -0
- package/dist/effect.js +87 -0
- package/dist/errors/api-error.d.ts +33 -0
- package/dist/errors/find-current-error.d.ts +8 -0
- package/dist/errors/sdk-error.d.ts +52 -0
- package/dist/errors/sdk-error.js +8 -0
- package/dist/generated/api-types.d.ts +56950 -0
- package/dist/generated/response-scalars.d.ts +9 -0
- package/dist/generated/routes.d.ts +12 -0
- package/dist/index.d.ts +28 -40
- package/dist/index.js +127 -96109
- package/dist/proxy/builder.d.ts +47 -0
- package/dist/proxy/client.d.ts +32 -0
- package/dist/proxy/fetch-executor.d.ts +30 -0
- package/dist/proxy/flat-args.d.ts +52 -0
- package/dist/proxy/sse.d.ts +75 -0
- package/dist/proxy/tanstack.d.ts +27 -0
- package/dist/proxy/transport.d.ts +74 -0
- package/dist/react-query.d.ts +16 -0
- package/dist/react-query.js +12 -0
- package/dist/readonly-deep.d.ts +12 -0
- package/dist/reads-as-funding-paused.d.ts +20 -0
- package/dist/reads-as-indexer-rebuild.d.ts +14 -0
- package/dist/routes/index.d.ts +20 -0
- package/dist/routes/index.js +10 -0
- package/dist/routes/types.d.ts +57 -0
- package/dist/runtime-exports.d.ts +20 -0
- package/dist/runtime-exports.js +16 -0
- package/dist/scalars/big-decimal.d.ts +16 -0
- package/dist/scalars/ethereum-address.d.ts +20 -0
- package/dist/scalars/exchange-rates.d.ts +27 -0
- package/dist/scalars/index.d.ts +18 -0
- package/dist/scalars/index.js +86 -0
- package/dist/scalars/organization-id.d.ts +20 -0
- package/dist/secret.d.ts +14 -0
- package/dist/sign-webhook-receipt.d.ts +76 -0
- package/dist/sign-webhook-receipt.js +26 -0
- package/dist/transport/cookie-store.d.ts +9 -0
- package/dist/transport/sse-lifecycle.d.ts +44 -0
- package/dist/types.d.ts +69 -102
- package/dist/url.d.ts +0 -1
- package/dist/verify-webhook.d.ts +17 -28
- package/dist/verify-webhook.js +8 -0
- package/dist/version.d.ts +0 -1
- package/dist/wait-for-transaction.d.ts +66 -79
- package/dist/wait-for-transaction.js +142 -0
- package/dist/webhook-events.d.ts +44 -27
- package/package.json +103 -30
- package/dist/auth-permissions.d.ts.map +0 -1
- package/dist/auth-wallet-plugins.d.ts.map +0 -1
- package/dist/auth.d.ts.map +0 -1
- package/dist/bundler.d.ts.map +0 -1
- package/dist/chunk-xm8h1dzh.js +0 -33
- package/dist/client.d.ts +0 -19
- package/dist/client.d.ts.map +0 -1
- package/dist/contract-errors.d.ts +0 -4156
- package/dist/contract-errors.d.ts.map +0 -1
- package/dist/contract.d.ts +0 -17
- package/dist/contract.d.ts.map +0 -1
- package/dist/cookie-store.d.ts +0 -10
- package/dist/cookie-store.d.ts.map +0 -1
- package/dist/dapi-errors.d.ts +0 -650
- package/dist/dapi-errors.d.ts.map +0 -1
- package/dist/errors.d.ts +0 -633
- package/dist/errors.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/platform.d.ts +0 -41
- package/dist/platform.d.ts.map +0 -1
- package/dist/plugins/index.d.ts +0 -13
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -10
- package/dist/sdk-error.d.ts +0 -74
- package/dist/sdk-error.d.ts.map +0 -1
- package/dist/serializers.d.ts +0 -41
- package/dist/serializers.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/url.d.ts.map +0 -1
- package/dist/verify-webhook.d.ts.map +0 -1
- package/dist/version.d.ts.map +0 -1
- package/dist/wait-for-transaction.d.ts.map +0 -1
- package/dist/webhook-events.d.ts.map +0 -1
|
@@ -0,0 +1,913 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DalpSdkError2,
|
|
3
|
+
createDalpSdkConfigurationError2
|
|
4
|
+
} from "./chunk-j4sxrfzz.js";
|
|
5
|
+
import {
|
|
6
|
+
Secret2,
|
|
7
|
+
secret2,
|
|
8
|
+
unwrapSecret,
|
|
9
|
+
normalizeDalpBaseUrl
|
|
10
|
+
} from "./chunk-xp3fx2yt.js";
|
|
11
|
+
|
|
12
|
+
// src/errors/find-current-error.ts
|
|
13
|
+
var readProperty = (target, key) => Reflect.get(target, key);
|
|
14
|
+
var findCurrentError = (error, maxNodes = 20) => {
|
|
15
|
+
const queue = [error];
|
|
16
|
+
const seen = new Set;
|
|
17
|
+
let visited = 0;
|
|
18
|
+
while (queue.length > 0 && visited < maxNodes) {
|
|
19
|
+
const current = queue.shift();
|
|
20
|
+
if (current === null || current === undefined || seen.has(current)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
seen.add(current);
|
|
24
|
+
visited += 1;
|
|
25
|
+
if (current instanceof DalpSdkError2) {
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
if (typeof current !== "object") {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
queue.push(readProperty(current, "cause"));
|
|
32
|
+
const reasons = readProperty(current, "reasons");
|
|
33
|
+
if (Array.isArray(reasons)) {
|
|
34
|
+
for (const reason of reasons) {
|
|
35
|
+
if (typeof reason === "object" && reason !== null) {
|
|
36
|
+
queue.push(readProperty(reason, "error"), readProperty(reason, "defect"));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/errors/api-error.ts
|
|
45
|
+
class DalpApiError2 extends Error {
|
|
46
|
+
id;
|
|
47
|
+
code;
|
|
48
|
+
category;
|
|
49
|
+
status;
|
|
50
|
+
retryable;
|
|
51
|
+
why;
|
|
52
|
+
fix;
|
|
53
|
+
retryAfterSeconds;
|
|
54
|
+
data;
|
|
55
|
+
constructor(options) {
|
|
56
|
+
super(options.message, { cause: options.cause });
|
|
57
|
+
this.name = "DalpApiError";
|
|
58
|
+
this.id = options.id;
|
|
59
|
+
this.code = options.code;
|
|
60
|
+
this.category = options.category;
|
|
61
|
+
this.status = options.status;
|
|
62
|
+
this.retryable = options.retryable;
|
|
63
|
+
this.why = options.why;
|
|
64
|
+
this.fix = options.fix;
|
|
65
|
+
this.retryAfterSeconds = options.retryAfterSeconds;
|
|
66
|
+
this.data = options.data;
|
|
67
|
+
}
|
|
68
|
+
static make(options) {
|
|
69
|
+
return new DalpApiError2(options);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
var isDalpApiError2 = (error) => error instanceof DalpApiError2;
|
|
73
|
+
var findDalpSdkError2 = findCurrentError;
|
|
74
|
+
var API_ERROR_CATEGORIES = new Set([
|
|
75
|
+
"client",
|
|
76
|
+
"auth",
|
|
77
|
+
"permission",
|
|
78
|
+
"domain",
|
|
79
|
+
"contract",
|
|
80
|
+
"dependency",
|
|
81
|
+
"operational",
|
|
82
|
+
"config",
|
|
83
|
+
"unknown"
|
|
84
|
+
]);
|
|
85
|
+
var mapSdkErrorCategory = (category) => {
|
|
86
|
+
if (category === "sdk.configuration") {
|
|
87
|
+
return "config";
|
|
88
|
+
}
|
|
89
|
+
if (API_ERROR_CATEGORIES.has(category)) {
|
|
90
|
+
return category;
|
|
91
|
+
}
|
|
92
|
+
return "operational";
|
|
93
|
+
};
|
|
94
|
+
var makeDalpApiError2 = (error) => {
|
|
95
|
+
const sdkError = findDalpSdkError2(error);
|
|
96
|
+
if (sdkError === undefined) {
|
|
97
|
+
return new DalpApiError2({
|
|
98
|
+
message: "DALP API request failed",
|
|
99
|
+
cause: error,
|
|
100
|
+
code: "sdk.transport.REQUEST_FAILED",
|
|
101
|
+
category: "dependency",
|
|
102
|
+
retryable: true,
|
|
103
|
+
why: "The DALP API request failed before the server returned a structured response.",
|
|
104
|
+
fix: "Check network connectivity and retry the request."
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const detailsId = sdkError.details?.id;
|
|
108
|
+
return new DalpApiError2({
|
|
109
|
+
message: sdkError.message,
|
|
110
|
+
cause: sdkError,
|
|
111
|
+
id: typeof detailsId === "string" ? detailsId : undefined,
|
|
112
|
+
code: sdkError.code,
|
|
113
|
+
category: mapSdkErrorCategory(sdkError.category),
|
|
114
|
+
retryable: sdkError.retryable,
|
|
115
|
+
why: sdkError.why,
|
|
116
|
+
fix: sdkError.fix,
|
|
117
|
+
status: sdkError.status,
|
|
118
|
+
retryAfterSeconds: sdkError.retryAfterSeconds,
|
|
119
|
+
data: sdkError.details
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/client/query-serializer.ts
|
|
124
|
+
function appendQueryValue(search, path, value) {
|
|
125
|
+
if (value === undefined || value === null) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (value instanceof Date) {
|
|
129
|
+
search.append(path, value.toISOString());
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (Array.isArray(value)) {
|
|
133
|
+
for (const [index, item] of value.entries()) {
|
|
134
|
+
const itemPath = typeof item === "object" && item !== null ? `${path}[${String(index)}]` : path;
|
|
135
|
+
appendQueryValue(search, itemPath, item);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (typeof value === "object") {
|
|
140
|
+
for (const [key, item] of Object.entries(value)) {
|
|
141
|
+
appendQueryValue(search, `${path}[${key}]`, item);
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
search.append(path, String(value));
|
|
146
|
+
}
|
|
147
|
+
function serializeDalpQuery(query) {
|
|
148
|
+
const search = new URLSearchParams;
|
|
149
|
+
if (typeof query === "object" && query !== null) {
|
|
150
|
+
for (const [key, value] of Object.entries(query)) {
|
|
151
|
+
appendQueryValue(search, key, value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return search.toString();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/client/request-compression.ts
|
|
158
|
+
var DALP_SDK_REQUEST_COMPRESSION_MIN_BODY_BYTES = 1024;
|
|
159
|
+
var DALP_SDK_REQUEST_COMPRESSION_MAX_BODY_BYTES = 16777216;
|
|
160
|
+
var NO_BODY_METHODS = new Set(["GET", "HEAD"]);
|
|
161
|
+
function bufferedBodySize(body) {
|
|
162
|
+
if (typeof body === "string") {
|
|
163
|
+
return Buffer.byteLength(body);
|
|
164
|
+
}
|
|
165
|
+
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
|
|
166
|
+
return body.byteLength;
|
|
167
|
+
}
|
|
168
|
+
if (body instanceof Blob) {
|
|
169
|
+
return body.size;
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
function isCompressibleMediaType(contentType) {
|
|
174
|
+
const mediaType = contentType?.split(";")[0]?.trim().toLowerCase();
|
|
175
|
+
if (mediaType === undefined || mediaType === "") {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return mediaType === "application/json" || mediaType.endsWith("+json") || mediaType.startsWith("text/");
|
|
179
|
+
}
|
|
180
|
+
async function cancelReaderBestEffort(reader) {
|
|
181
|
+
try {
|
|
182
|
+
await reader.cancel();
|
|
183
|
+
} catch {}
|
|
184
|
+
}
|
|
185
|
+
async function readRequestBodyWithinCap(request, maxBodyBytes) {
|
|
186
|
+
let body;
|
|
187
|
+
try {
|
|
188
|
+
const { body: clonedBody } = request.clone();
|
|
189
|
+
body = clonedBody;
|
|
190
|
+
} catch {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (body === null) {
|
|
194
|
+
return Buffer.alloc(0);
|
|
195
|
+
}
|
|
196
|
+
const reader = body.getReader();
|
|
197
|
+
const chunks = [];
|
|
198
|
+
let totalBytes = 0;
|
|
199
|
+
try {
|
|
200
|
+
while (true) {
|
|
201
|
+
const next = await reader.read();
|
|
202
|
+
if (next.done) {
|
|
203
|
+
return Buffer.concat(chunks, totalBytes);
|
|
204
|
+
}
|
|
205
|
+
totalBytes += next.value.byteLength;
|
|
206
|
+
if (totalBytes > maxBodyBytes) {
|
|
207
|
+
cancelReaderBestEffort(reader);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
chunks.push(next.value);
|
|
211
|
+
}
|
|
212
|
+
} catch {
|
|
213
|
+
return;
|
|
214
|
+
} finally {
|
|
215
|
+
reader.releaseLock();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function withRequestBodyCompression(fetchImpl, options) {
|
|
219
|
+
const minBodyBytes = options?.minBodyBytes ?? DALP_SDK_REQUEST_COMPRESSION_MIN_BODY_BYTES;
|
|
220
|
+
const maxBodyBytes = options?.maxBodyBytes ?? DALP_SDK_REQUEST_COMPRESSION_MAX_BODY_BYTES;
|
|
221
|
+
const requestCompressionFetch = async (input, init) => {
|
|
222
|
+
if (init?.compress !== undefined) {
|
|
223
|
+
return fetchImpl(input, init);
|
|
224
|
+
}
|
|
225
|
+
let request;
|
|
226
|
+
if (input instanceof Request) {
|
|
227
|
+
request = init === undefined ? input : new Request(input, init);
|
|
228
|
+
}
|
|
229
|
+
const method = (request?.method ?? init?.method ?? "GET").toUpperCase();
|
|
230
|
+
if (NO_BODY_METHODS.has(method)) {
|
|
231
|
+
return fetchImpl(input, init);
|
|
232
|
+
}
|
|
233
|
+
const headers = new Headers(request?.headers ?? init?.headers);
|
|
234
|
+
if (headers.get("content-encoding") !== null) {
|
|
235
|
+
return fetchImpl(input, init);
|
|
236
|
+
}
|
|
237
|
+
let compress;
|
|
238
|
+
let bufferedRequestBody;
|
|
239
|
+
if (request !== undefined && request.body !== null && isCompressibleMediaType(headers.get("content-type"))) {
|
|
240
|
+
bufferedRequestBody = await readRequestBodyWithinCap(request, maxBodyBytes);
|
|
241
|
+
if (bufferedRequestBody !== undefined && bufferedRequestBody.byteLength >= minBodyBytes) {
|
|
242
|
+
compress = "gzip";
|
|
243
|
+
}
|
|
244
|
+
} else if (init?.body !== null && init?.body !== undefined) {
|
|
245
|
+
const size = bufferedBodySize(init.body);
|
|
246
|
+
if (size !== undefined && size >= minBodyBytes && size <= maxBodyBytes) {
|
|
247
|
+
compress = "gzip";
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (compress === undefined) {
|
|
251
|
+
return fetchImpl(input, init);
|
|
252
|
+
}
|
|
253
|
+
if (request !== undefined && bufferedRequestBody !== undefined) {
|
|
254
|
+
return fetchImpl(request.url, {
|
|
255
|
+
...init,
|
|
256
|
+
body: bufferedRequestBody,
|
|
257
|
+
cache: init?.cache ?? request.cache,
|
|
258
|
+
compress,
|
|
259
|
+
credentials: init?.credentials ?? request.credentials,
|
|
260
|
+
headers: request.headers,
|
|
261
|
+
integrity: init?.integrity ?? request.integrity,
|
|
262
|
+
keepalive: init?.keepalive ?? request.keepalive,
|
|
263
|
+
method: request.method,
|
|
264
|
+
mode: init?.mode ?? request.mode,
|
|
265
|
+
redirect: init?.redirect ?? request.redirect,
|
|
266
|
+
referrer: init?.referrer ?? request.referrer,
|
|
267
|
+
referrerPolicy: init?.referrerPolicy ?? request.referrerPolicy,
|
|
268
|
+
signal: init?.signal ?? request.signal
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return fetchImpl(input, { ...init, compress });
|
|
272
|
+
};
|
|
273
|
+
return Object.assign(requestCompressionFetch, {
|
|
274
|
+
preconnect: fetchImpl.preconnect
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/client/ssr-fetch.ts
|
|
279
|
+
class DalpSsrSecurityError extends Error {
|
|
280
|
+
name = "DalpSsrSecurityError";
|
|
281
|
+
}
|
|
282
|
+
var originOf = (value) => {
|
|
283
|
+
const url = new URL(value);
|
|
284
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
285
|
+
throw new DalpSsrSecurityError(`DALP SDK: ${url.protocol} is not an approved SSR protocol.`);
|
|
286
|
+
}
|
|
287
|
+
return url.origin;
|
|
288
|
+
};
|
|
289
|
+
var REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
290
|
+
var SENSITIVE_HEADERS = [
|
|
291
|
+
"authorization",
|
|
292
|
+
"baggage",
|
|
293
|
+
"cookie",
|
|
294
|
+
"idempotency-key",
|
|
295
|
+
"origin",
|
|
296
|
+
"referer",
|
|
297
|
+
"traceparent",
|
|
298
|
+
"tracestate",
|
|
299
|
+
"x-api-key",
|
|
300
|
+
"x-chain-id",
|
|
301
|
+
"x-executor",
|
|
302
|
+
"x-organization-id",
|
|
303
|
+
"x-participant",
|
|
304
|
+
"x-selected-signers"
|
|
305
|
+
];
|
|
306
|
+
var withoutSensitiveHeaders = (source) => {
|
|
307
|
+
const headers = new Headers(source);
|
|
308
|
+
for (const name of SENSITIVE_HEADERS) {
|
|
309
|
+
headers.delete(name);
|
|
310
|
+
}
|
|
311
|
+
return headers;
|
|
312
|
+
};
|
|
313
|
+
function createSsrFetch(options) {
|
|
314
|
+
const incoming = new Headers(options.requestHeaders);
|
|
315
|
+
const approvedOrigins = new Set([originOf(options.baseUrl), ...(options.allowedOrigins ?? []).map(originOf)]);
|
|
316
|
+
const ssrFetch = async (input, init) => {
|
|
317
|
+
const request = input instanceof Request && init === undefined ? input : new Request(input, init);
|
|
318
|
+
if (!approvedOrigins.has(new URL(request.url).origin)) {
|
|
319
|
+
throw new DalpSsrSecurityError(`DALP SDK: ${request.url} is not an approved SSR destination.`);
|
|
320
|
+
}
|
|
321
|
+
const cookie = incoming.get("cookie");
|
|
322
|
+
if (cookie !== null && !["GET", "HEAD", "OPTIONS"].includes(request.method)) {
|
|
323
|
+
const incomingOrigin = incoming.get("origin");
|
|
324
|
+
let trustedOrigin = false;
|
|
325
|
+
try {
|
|
326
|
+
trustedOrigin = incomingOrigin !== null && approvedOrigins.has(originOf(incomingOrigin));
|
|
327
|
+
} catch {
|
|
328
|
+
trustedOrigin = false;
|
|
329
|
+
}
|
|
330
|
+
if (!trustedOrigin || incoming.get("sec-fetch-site") === "cross-site") {
|
|
331
|
+
throw new DalpSsrSecurityError("DALP SDK: a cookie-authenticated SSR mutation requires a trusted Origin.");
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const headers = new Headers(request.headers);
|
|
335
|
+
if (cookie === null) {
|
|
336
|
+
headers.delete("cookie");
|
|
337
|
+
} else {
|
|
338
|
+
headers.set("cookie", cookie);
|
|
339
|
+
}
|
|
340
|
+
for (const name of ["origin", "traceparent", "tracestate", "baggage"]) {
|
|
341
|
+
const value = incoming.get(name);
|
|
342
|
+
if (value !== null && !headers.has(name)) {
|
|
343
|
+
headers.set(name, value);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
let current = new Request(request, { headers, redirect: "manual" });
|
|
347
|
+
let sensitiveAllowed = true;
|
|
348
|
+
for (let redirectCount = 0;redirectCount <= 5; redirectCount += 1) {
|
|
349
|
+
const replay = current.clone();
|
|
350
|
+
const response = await options.fetch(current, { credentials: "omit", redirect: "manual" });
|
|
351
|
+
const location = response.headers.get("location");
|
|
352
|
+
if (!REDIRECT_STATUSES.has(response.status) || location === null) {
|
|
353
|
+
return response;
|
|
354
|
+
}
|
|
355
|
+
if (redirectCount === 5) {
|
|
356
|
+
throw new DalpSsrSecurityError("DALP SDK: SSR fetch exceeded the redirect limit.");
|
|
357
|
+
}
|
|
358
|
+
const nextUrl = new URL(location, current.url);
|
|
359
|
+
sensitiveAllowed = sensitiveAllowed && approvedOrigins.has(nextUrl.origin);
|
|
360
|
+
const redirectToGet = response.status === 303 || (response.status === 301 || response.status === 302) && replay.method === "POST";
|
|
361
|
+
const method = redirectToGet ? "GET" : replay.method;
|
|
362
|
+
if (!sensitiveAllowed && method !== "GET" && method !== "HEAD") {
|
|
363
|
+
throw new DalpSsrSecurityError("DALP SDK: refused to forward an SSR request body outside the trust boundary.");
|
|
364
|
+
}
|
|
365
|
+
const nextHeaders = sensitiveAllowed ? new Headers(replay.headers) : withoutSensitiveHeaders(replay.headers);
|
|
366
|
+
if (redirectToGet) {
|
|
367
|
+
nextHeaders.delete("content-length");
|
|
368
|
+
nextHeaders.delete("content-type");
|
|
369
|
+
}
|
|
370
|
+
current = new Request(nextUrl, {
|
|
371
|
+
body: method === "GET" || method === "HEAD" ? undefined : await replay.arrayBuffer(),
|
|
372
|
+
credentials: sensitiveAllowed ? replay.credentials : "omit",
|
|
373
|
+
headers: nextHeaders,
|
|
374
|
+
method,
|
|
375
|
+
redirect: "manual",
|
|
376
|
+
signal: replay.signal
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
throw new DalpSsrSecurityError("DALP SDK: unreachable redirect state.");
|
|
380
|
+
};
|
|
381
|
+
return Object.assign(ssrFetch, {
|
|
382
|
+
preconnect: options.fetch.preconnect ?? (() => {})
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/client/conditional-request.ts
|
|
387
|
+
var IF_NONE_MATCH = "If-None-Match";
|
|
388
|
+
var CONDITIONAL_RESPONSE_STORE_CAPACITY = 64;
|
|
389
|
+
var CONDITIONAL_RESPONSE_MAX_BODY_BYTES = 1048576;
|
|
390
|
+
var IDENTITY_RESET_PATH_SUFFIXES = [
|
|
391
|
+
"/api/auth/sign-out",
|
|
392
|
+
"/api/auth/revoke-session",
|
|
393
|
+
"/api/auth/revoke-sessions",
|
|
394
|
+
"/api/auth/revoke-other-sessions",
|
|
395
|
+
"/api/auth/organization/set-active"
|
|
396
|
+
];
|
|
397
|
+
var headerOrDefault = (headers, name) => headers.get(name)?.trim() || "default";
|
|
398
|
+
var tenantOf = (headers) => `${headerOrDefault(headers, "X-Organization-Id")}\x00${headerOrDefault(headers, "X-Participant")}`;
|
|
399
|
+
var resourceKey = (request) => {
|
|
400
|
+
const url = new URL(request.url);
|
|
401
|
+
return `${url.origin}${url.pathname}${url.search}\x00${tenantOf(request.headers)}`;
|
|
402
|
+
};
|
|
403
|
+
var isIdentityResetPath = (url) => {
|
|
404
|
+
const path = new URL(url).pathname;
|
|
405
|
+
return IDENTITY_RESET_PATH_SUFFIXES.some((suffix) => path === suffix || path.endsWith(suffix));
|
|
406
|
+
};
|
|
407
|
+
var isEventStream = (response) => (response.headers.get("content-type") ?? "").includes("text/event-stream");
|
|
408
|
+
var remember = (store, key, entry) => {
|
|
409
|
+
store.delete(key);
|
|
410
|
+
store.set(key, entry);
|
|
411
|
+
if (store.size > CONDITIONAL_RESPONSE_STORE_CAPACITY) {
|
|
412
|
+
const oldest = store.keys().next().value;
|
|
413
|
+
if (oldest !== undefined) {
|
|
414
|
+
store.delete(oldest);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
var touch = (store, key) => {
|
|
419
|
+
const entry = store.get(key);
|
|
420
|
+
if (entry === undefined) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
store.delete(key);
|
|
424
|
+
store.set(key, entry);
|
|
425
|
+
return entry;
|
|
426
|
+
};
|
|
427
|
+
var requestWithoutIfNoneMatch = (request) => {
|
|
428
|
+
const headers = new Headers;
|
|
429
|
+
for (const [name, value] of request.headers.entries()) {
|
|
430
|
+
if (name.toLowerCase() !== IF_NONE_MATCH.toLowerCase()) {
|
|
431
|
+
headers.append(name, value);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return new Request(request.url, {
|
|
435
|
+
method: request.method,
|
|
436
|
+
headers,
|
|
437
|
+
signal: request.signal,
|
|
438
|
+
credentials: request.credentials,
|
|
439
|
+
redirect: request.redirect
|
|
440
|
+
});
|
|
441
|
+
};
|
|
442
|
+
async function captureSuccessfulGet(store, key, response) {
|
|
443
|
+
if (!response.ok || isEventStream(response)) {
|
|
444
|
+
return response;
|
|
445
|
+
}
|
|
446
|
+
const etag = response.headers.get("etag")?.trim();
|
|
447
|
+
if (etag === undefined || etag.length === 0) {
|
|
448
|
+
store.delete(key);
|
|
449
|
+
return response;
|
|
450
|
+
}
|
|
451
|
+
const body = new Uint8Array(await response.arrayBuffer());
|
|
452
|
+
const headers = new Headers(response.headers);
|
|
453
|
+
headers.set("etag", etag);
|
|
454
|
+
if (body.byteLength <= CONDITIONAL_RESPONSE_MAX_BODY_BYTES) {
|
|
455
|
+
remember(store, key, { body, etag, headers });
|
|
456
|
+
}
|
|
457
|
+
return new Response(body, {
|
|
458
|
+
headers,
|
|
459
|
+
status: response.status,
|
|
460
|
+
statusText: response.statusText
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
function withConditionalRequests(base) {
|
|
464
|
+
const state = { store: new Map, lastIdentity: undefined };
|
|
465
|
+
const syncIdentity = (headers) => {
|
|
466
|
+
const identity = tenantOf(headers);
|
|
467
|
+
if (state.lastIdentity !== undefined && identity !== state.lastIdentity) {
|
|
468
|
+
state.store.clear();
|
|
469
|
+
}
|
|
470
|
+
state.lastIdentity = identity;
|
|
471
|
+
};
|
|
472
|
+
const decorated = async (input, init) => {
|
|
473
|
+
const request = new Request(input, init);
|
|
474
|
+
syncIdentity(request.headers);
|
|
475
|
+
if (request.method !== "GET") {
|
|
476
|
+
const response = await base(request);
|
|
477
|
+
if (isIdentityResetPath(request.url) && response.ok) {
|
|
478
|
+
state.store.clear();
|
|
479
|
+
state.lastIdentity = undefined;
|
|
480
|
+
}
|
|
481
|
+
return response;
|
|
482
|
+
}
|
|
483
|
+
const key = resourceKey(request);
|
|
484
|
+
let sent = request;
|
|
485
|
+
if (!request.headers.has(IF_NONE_MATCH)) {
|
|
486
|
+
const entry = touch(state.store, key);
|
|
487
|
+
if (entry !== undefined) {
|
|
488
|
+
const headers = new Headers(request.headers);
|
|
489
|
+
headers.set(IF_NONE_MATCH, entry.etag);
|
|
490
|
+
sent = new Request(request, { headers });
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const response = await base(sent);
|
|
494
|
+
if (response.status !== 304) {
|
|
495
|
+
return captureSuccessfulGet(state.store, key, response);
|
|
496
|
+
}
|
|
497
|
+
const entry = touch(state.store, key);
|
|
498
|
+
if (entry === undefined) {
|
|
499
|
+
const retried = await base(requestWithoutIfNoneMatch(sent));
|
|
500
|
+
return retried.status === 304 ? retried : captureSuccessfulGet(state.store, key, retried);
|
|
501
|
+
}
|
|
502
|
+
return new Response(new Uint8Array(entry.body), { status: 200, statusText: "OK", headers: entry.headers });
|
|
503
|
+
};
|
|
504
|
+
return {
|
|
505
|
+
fetch: decorated,
|
|
506
|
+
store: {
|
|
507
|
+
clear: () => {
|
|
508
|
+
state.store.clear();
|
|
509
|
+
state.lastIdentity = undefined;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// src/proxy/flat-args.ts
|
|
516
|
+
function splitFlatArgs(route, input) {
|
|
517
|
+
const source = input === null || input === undefined || typeof input !== "object" ? {} : input;
|
|
518
|
+
const path = {};
|
|
519
|
+
const query = {};
|
|
520
|
+
const rest = {};
|
|
521
|
+
const pathParams = new Set(route.params);
|
|
522
|
+
const queryParams = new Set(route.hasPayload ? route.queryParams : []);
|
|
523
|
+
for (const [key, value] of Object.entries(source)) {
|
|
524
|
+
if (pathParams.has(key)) {
|
|
525
|
+
path[key] = value;
|
|
526
|
+
} else if (queryParams.has(key)) {
|
|
527
|
+
query[key] = value;
|
|
528
|
+
} else {
|
|
529
|
+
rest[key] = value;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const missing = route.params.filter((name) => path[name] === undefined);
|
|
533
|
+
if (missing.length > 0) {
|
|
534
|
+
throw new TypeError(`Missing path parameter${missing.length === 1 ? "" : "s"} ${missing.map((name) => `\`${name}\``).join(", ")} for ${route.method} ${route.path}`);
|
|
535
|
+
}
|
|
536
|
+
if (!route.hasPayload) {
|
|
537
|
+
return { path, query: rest, body: undefined };
|
|
538
|
+
}
|
|
539
|
+
if (input === null || input === undefined) {
|
|
540
|
+
return { path, query, body: undefined };
|
|
541
|
+
}
|
|
542
|
+
for (const name of route.bodyParams) {
|
|
543
|
+
const value = path[name];
|
|
544
|
+
if (value !== undefined) {
|
|
545
|
+
rest[name] = value;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return { path, query, body: rest };
|
|
549
|
+
}
|
|
550
|
+
function buildPath(route, path) {
|
|
551
|
+
return route.path.replaceAll(/:([A-Za-z0-9_]+)/gu, (_match, name) => {
|
|
552
|
+
const value = path[name];
|
|
553
|
+
return encodeURIComponent(typeof value === "string" ? value : String(value));
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/proxy/transport.ts
|
|
558
|
+
var CONTEXT_HEADERS = {
|
|
559
|
+
organizationId: "X-Organization-Id",
|
|
560
|
+
participant: "X-Participant",
|
|
561
|
+
executor: "X-Executor",
|
|
562
|
+
chainId: "X-Chain-Id",
|
|
563
|
+
selectedSigners: "X-Selected-Signers",
|
|
564
|
+
idempotencyKey: "Idempotency-Key"
|
|
565
|
+
};
|
|
566
|
+
function setNonEmptyHeader(headers, name, value) {
|
|
567
|
+
if (value !== undefined && String(value).trim().length > 0) {
|
|
568
|
+
headers.set(name, String(value));
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function createDalpContextHeaders2(context) {
|
|
572
|
+
const headers = new Headers;
|
|
573
|
+
setNonEmptyHeader(headers, CONTEXT_HEADERS.organizationId, context.organizationId);
|
|
574
|
+
setNonEmptyHeader(headers, CONTEXT_HEADERS.participant, context.participant);
|
|
575
|
+
setNonEmptyHeader(headers, CONTEXT_HEADERS.executor, context.executor);
|
|
576
|
+
setNonEmptyHeader(headers, CONTEXT_HEADERS.chainId, context.chainId);
|
|
577
|
+
setNonEmptyHeader(headers, CONTEXT_HEADERS.idempotencyKey, context.idempotencyKey);
|
|
578
|
+
if (context.selectedSigners !== undefined && context.selectedSigners.length > 0) {
|
|
579
|
+
headers.set(CONTEXT_HEADERS.selectedSigners, JSON.stringify(context.selectedSigners));
|
|
580
|
+
}
|
|
581
|
+
return headers;
|
|
582
|
+
}
|
|
583
|
+
var DEFAULT_PREFER_WAIT_SECONDS = 15;
|
|
584
|
+
var MIN_PREFER_WAIT_SECONDS = 5;
|
|
585
|
+
var MAX_PREFER_WAIT_SECONDS = 99;
|
|
586
|
+
function preferDirective(timeoutMs) {
|
|
587
|
+
if (timeoutMs === undefined) {
|
|
588
|
+
return `wait=${String(DEFAULT_PREFER_WAIT_SECONDS)}`;
|
|
589
|
+
}
|
|
590
|
+
if (timeoutMs <= 0) {
|
|
591
|
+
return "respond-async";
|
|
592
|
+
}
|
|
593
|
+
const seconds = Math.min(MAX_PREFER_WAIT_SECONDS, Math.max(MIN_PREFER_WAIT_SECONDS, Math.ceil(timeoutMs / 1000)));
|
|
594
|
+
return `wait=${String(seconds)}`;
|
|
595
|
+
}
|
|
596
|
+
function createTransport(options) {
|
|
597
|
+
const baseUrl = normalizeDalpBaseUrl(String(options.baseUrl));
|
|
598
|
+
const runtime = options.runtime ?? (globalThis.window === undefined ? "server" : "browser");
|
|
599
|
+
const baseFetch = options.fetch ?? globalThis.fetch;
|
|
600
|
+
const transportFetch = runtime === "browser" ? baseFetch : withRequestBodyCompression(baseFetch);
|
|
601
|
+
const { apiKey: apiKeyOption } = options;
|
|
602
|
+
let apiKey;
|
|
603
|
+
if (apiKeyOption === undefined) {
|
|
604
|
+
apiKey = undefined;
|
|
605
|
+
} else if (apiKeyOption instanceof Secret2) {
|
|
606
|
+
apiKey = apiKeyOption;
|
|
607
|
+
} else {
|
|
608
|
+
apiKey = secret2(apiKeyOption.trim());
|
|
609
|
+
}
|
|
610
|
+
if (runtime === "browser" && apiKey !== undefined) {
|
|
611
|
+
throw createDalpSdkConfigurationError2({
|
|
612
|
+
message: "DALP SDK: API keys are server/CLI-only credentials and cannot configure a browser client.",
|
|
613
|
+
why: "An API key placed in a browser client ships the credential to every visitor's bundle.",
|
|
614
|
+
fix: "Keep the API key on the server or CLI process; browser clients authenticate with the session cookie."
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
const sessionFetch = options.ssr === undefined ? transportFetch : createSsrFetch({
|
|
618
|
+
allowedOrigins: options.ssr.allowedOrigins,
|
|
619
|
+
baseUrl,
|
|
620
|
+
fetch: transportFetch,
|
|
621
|
+
requestHeaders: options.ssr.requestHeaders
|
|
622
|
+
});
|
|
623
|
+
const conditional = withConditionalRequests(sessionFetch);
|
|
624
|
+
return {
|
|
625
|
+
baseUrl,
|
|
626
|
+
fetch: conditional.fetch,
|
|
627
|
+
conditionalRequests: conditional.store,
|
|
628
|
+
credentials: runtime === "browser" ? "include" : "omit",
|
|
629
|
+
apiKey,
|
|
630
|
+
contextHeaders: createDalpContextHeaders2(options.context ?? {}),
|
|
631
|
+
settlementTimeoutMs: options.settlementTimeoutMs
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
function callHeaders(transport, options) {
|
|
635
|
+
const headers = new Headers({ Accept: "application/json" });
|
|
636
|
+
if (transport.apiKey !== undefined) {
|
|
637
|
+
headers.set("X-Api-Key", unwrapSecret(transport.apiKey));
|
|
638
|
+
}
|
|
639
|
+
for (const [name, value] of transport.contextHeaders) {
|
|
640
|
+
if (!headers.has(name)) {
|
|
641
|
+
headers.set(name, value);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
const extra = typeof options?.headers === "function" ? options.headers() : options?.headers ?? {};
|
|
645
|
+
for (const [name, value] of Object.entries(extra)) {
|
|
646
|
+
headers.set(name, value);
|
|
647
|
+
}
|
|
648
|
+
return headers;
|
|
649
|
+
}
|
|
650
|
+
function prepareRequest(transport, route, input, options) {
|
|
651
|
+
const split = splitFlatArgs(route, input);
|
|
652
|
+
const query = serializeDalpQuery(split.query);
|
|
653
|
+
const url = `${transport.baseUrl}/api/v2${buildPath(route, split.path)}${query === "" ? "" : `?${query}`}`;
|
|
654
|
+
const headers = callHeaders(transport, options);
|
|
655
|
+
if (split.body !== undefined) {
|
|
656
|
+
headers.set("Content-Type", "application/json");
|
|
657
|
+
}
|
|
658
|
+
if (route.queueBacked && !headers.has("Prefer")) {
|
|
659
|
+
headers.set("Prefer", preferDirective(options?.settlementTimeoutMs ?? transport.settlementTimeoutMs));
|
|
660
|
+
}
|
|
661
|
+
return {
|
|
662
|
+
url,
|
|
663
|
+
method: route.method,
|
|
664
|
+
headers,
|
|
665
|
+
body: split.body === undefined ? undefined : JSON.stringify(split.body, wireValue)
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
function wireValue(_key, value) {
|
|
669
|
+
return typeof value === "bigint" ? value.toString() : value;
|
|
670
|
+
}
|
|
671
|
+
function readString(source, key) {
|
|
672
|
+
if (source === null || typeof source !== "object") {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const value = Reflect.get(source, key);
|
|
676
|
+
return typeof value === "string" ? value : undefined;
|
|
677
|
+
}
|
|
678
|
+
function sdkErrorFromEnvelope(payload, response) {
|
|
679
|
+
const envelope = payload !== null && typeof payload === "object" ? payload : {};
|
|
680
|
+
const rawData = Reflect.get(envelope, "data");
|
|
681
|
+
const data = rawData !== null && typeof rawData === "object" ? rawData : {};
|
|
682
|
+
const retryable = Reflect.get(data, "retryable");
|
|
683
|
+
const retryAfter = response.headers.get("retry-after");
|
|
684
|
+
let retryAfterSeconds;
|
|
685
|
+
if (retryAfter !== null) {
|
|
686
|
+
if (/^\d+$/.test(retryAfter)) {
|
|
687
|
+
retryAfterSeconds = Number(retryAfter);
|
|
688
|
+
} else if (Number.isNaN(Number(retryAfter))) {
|
|
689
|
+
retryAfterSeconds = Math.max(0, Math.ceil((Date.parse(retryAfter) - Date.now()) / 1000));
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return new DalpSdkError2({
|
|
693
|
+
code: readString(envelope, "code") ?? `sdk.transport.HTTP_${String(response.status)}`,
|
|
694
|
+
category: readString(data, "category") ?? "dependency",
|
|
695
|
+
status: response.status,
|
|
696
|
+
retryable: typeof retryable === "boolean" ? retryable : response.status >= 500,
|
|
697
|
+
message: readString(envelope, "message") ?? `DALP API responded ${String(response.status)}`,
|
|
698
|
+
why: readString(data, "why") ?? `The platform answered ${String(response.status)}.`,
|
|
699
|
+
fix: readString(data, "fix") ?? "Inspect the response body and the request id before retrying.",
|
|
700
|
+
...retryAfterSeconds !== undefined && Number.isFinite(retryAfterSeconds) ? { retryAfterSeconds } : {},
|
|
701
|
+
...Object.keys(data).length === 0 ? {} : { details: data }
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
async function readResponse(response) {
|
|
705
|
+
const text = await response.text();
|
|
706
|
+
let payload;
|
|
707
|
+
try {
|
|
708
|
+
payload = text === "" ? undefined : JSON.parse(text);
|
|
709
|
+
} catch {
|
|
710
|
+
payload = text;
|
|
711
|
+
}
|
|
712
|
+
if (!response.ok) {
|
|
713
|
+
throw makeDalpApiError2(sdkErrorFromEnvelope(payload, response));
|
|
714
|
+
}
|
|
715
|
+
return payload;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// src/proxy/sse.ts
|
|
719
|
+
var RESERVED_STREAM_FAILURE_EVENT2 = "effect/httpapi/stream/failure";
|
|
720
|
+
var STREAM_FAILED_ERROR_CODE2 = "sdk.stream.FAILED";
|
|
721
|
+
function parseFrame(raw) {
|
|
722
|
+
try {
|
|
723
|
+
return JSON.parse(raw.data);
|
|
724
|
+
} catch {
|
|
725
|
+
return raw.data;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
function dispatchFrame(raw) {
|
|
729
|
+
if (raw.event === RESERVED_STREAM_FAILURE_EVENT2) {
|
|
730
|
+
throw makeDalpApiError2(new DalpSdkError2({
|
|
731
|
+
code: STREAM_FAILED_ERROR_CODE2,
|
|
732
|
+
category: "dependency",
|
|
733
|
+
retryable: true,
|
|
734
|
+
message: "The DALP API stream failed before it completed.",
|
|
735
|
+
why: "The route ended the stream with its reserved failure event, so the remaining frames were never sent.",
|
|
736
|
+
fix: "Retry the stream. If it fails again, read the route's logs for the failure the server recorded.",
|
|
737
|
+
details: { cause: raw.data }
|
|
738
|
+
}));
|
|
739
|
+
}
|
|
740
|
+
return parseFrame(raw);
|
|
741
|
+
}
|
|
742
|
+
function readBlock(block) {
|
|
743
|
+
let event = "message";
|
|
744
|
+
const data = [];
|
|
745
|
+
for (const line of block.split(`
|
|
746
|
+
`)) {
|
|
747
|
+
if (line === "" || line.startsWith(":")) {
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
const separator = line.indexOf(":");
|
|
751
|
+
const field = separator === -1 ? line : line.slice(0, separator);
|
|
752
|
+
const rest = separator === -1 ? "" : line.slice(separator + 1).replace(/^ /u, "");
|
|
753
|
+
if (field === "event") {
|
|
754
|
+
event = rest;
|
|
755
|
+
} else if (field === "data") {
|
|
756
|
+
data.push(rest);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return data.length === 0 ? undefined : { event, data: data.join(`
|
|
760
|
+
`) };
|
|
761
|
+
}
|
|
762
|
+
async function* decodeEventStream2(body) {
|
|
763
|
+
const reader = body.getReader();
|
|
764
|
+
const decoder = new TextDecoder;
|
|
765
|
+
let buffer = "";
|
|
766
|
+
try {
|
|
767
|
+
for (;; ) {
|
|
768
|
+
const chunk = await reader.read();
|
|
769
|
+
if (chunk.done) {
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
773
|
+
buffer = buffer.replaceAll(`\r
|
|
774
|
+
`, `
|
|
775
|
+
`);
|
|
776
|
+
let boundary = buffer.indexOf(`
|
|
777
|
+
|
|
778
|
+
`);
|
|
779
|
+
while (boundary !== -1) {
|
|
780
|
+
const raw = readBlock(buffer.slice(0, boundary));
|
|
781
|
+
buffer = buffer.slice(boundary + 2);
|
|
782
|
+
if (raw !== undefined) {
|
|
783
|
+
yield dispatchFrame(raw);
|
|
784
|
+
}
|
|
785
|
+
boundary = buffer.indexOf(`
|
|
786
|
+
|
|
787
|
+
`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
const trailing = readBlock(buffer);
|
|
791
|
+
if (trailing !== undefined) {
|
|
792
|
+
yield dispatchFrame(trailing);
|
|
793
|
+
}
|
|
794
|
+
} finally {
|
|
795
|
+
await reader.cancel().catch(() => {});
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
async function* openEventStream(transport, route, input, options, signal, closed) {
|
|
799
|
+
const prepared = prepareRequest(transport, route, input, options);
|
|
800
|
+
prepared.headers.set("Accept", "text/event-stream");
|
|
801
|
+
const response = await transport.fetch(prepared.url, {
|
|
802
|
+
method: prepared.method,
|
|
803
|
+
headers: prepared.headers,
|
|
804
|
+
credentials: transport.credentials,
|
|
805
|
+
...prepared.body === undefined ? {} : { body: prepared.body },
|
|
806
|
+
signal
|
|
807
|
+
});
|
|
808
|
+
if (!response.ok) {
|
|
809
|
+
await readResponse(response);
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
if (response.body === null) {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
try {
|
|
816
|
+
yield* decodeEventStream2(response.body);
|
|
817
|
+
} catch (error) {
|
|
818
|
+
if (!closed.byConsumer) {
|
|
819
|
+
throw error;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function streamRequest(transport, route, input, options) {
|
|
824
|
+
const closing = new AbortController;
|
|
825
|
+
const callerSignal = options?.signal;
|
|
826
|
+
if (callerSignal !== undefined) {
|
|
827
|
+
if (callerSignal.aborted) {
|
|
828
|
+
closing.abort(callerSignal.reason);
|
|
829
|
+
} else {
|
|
830
|
+
callerSignal.addEventListener("abort", () => closing.abort(callerSignal.reason), { once: true });
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
const closed = { byConsumer: false };
|
|
834
|
+
const frames = openEventStream(transport, route, input, options, closing.signal, closed);
|
|
835
|
+
const close = () => {
|
|
836
|
+
closed.byConsumer = true;
|
|
837
|
+
closing.abort();
|
|
838
|
+
};
|
|
839
|
+
return {
|
|
840
|
+
[Symbol.asyncIterator]() {
|
|
841
|
+
return this;
|
|
842
|
+
},
|
|
843
|
+
next: (...args) => frames.next(...args),
|
|
844
|
+
return: (value) => {
|
|
845
|
+
close();
|
|
846
|
+
return frames.return(value);
|
|
847
|
+
},
|
|
848
|
+
throw: (error) => {
|
|
849
|
+
close();
|
|
850
|
+
return frames.throw(error);
|
|
851
|
+
},
|
|
852
|
+
async[Symbol.asyncDispose]() {
|
|
853
|
+
close();
|
|
854
|
+
await frames.return(undefined);
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// src/proxy/fetch-executor.ts
|
|
860
|
+
async function sendRequest(transport, route, input, options) {
|
|
861
|
+
const prepared = prepareRequest(transport, route, input, options);
|
|
862
|
+
const response = await transport.fetch(prepared.url, {
|
|
863
|
+
method: prepared.method,
|
|
864
|
+
headers: prepared.headers,
|
|
865
|
+
credentials: transport.credentials,
|
|
866
|
+
...prepared.body === undefined ? {} : { body: prepared.body },
|
|
867
|
+
...options?.signal === undefined ? {} : { signal: options.signal }
|
|
868
|
+
});
|
|
869
|
+
return readResponse(response);
|
|
870
|
+
}
|
|
871
|
+
function createFetchExecutor(transport) {
|
|
872
|
+
return (route, _id, input, options) => route.serializer === "sse" ? streamRequest(transport, route, input, options) : sendRequest(transport, route, input, options);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/proxy/tanstack.ts
|
|
876
|
+
function canonicalize2(value) {
|
|
877
|
+
if (Array.isArray(value)) {
|
|
878
|
+
return value.map(canonicalize2);
|
|
879
|
+
}
|
|
880
|
+
if (value === null || typeof value !== "object") {
|
|
881
|
+
return value;
|
|
882
|
+
}
|
|
883
|
+
const entries = [];
|
|
884
|
+
for (const key of Object.keys(value).sort((left, right) => left.localeCompare(right))) {
|
|
885
|
+
const entry = Reflect.get(value, key);
|
|
886
|
+
if (entry !== undefined) {
|
|
887
|
+
entries.push([key, canonicalize2(entry)]);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return Object.fromEntries(entries);
|
|
891
|
+
}
|
|
892
|
+
function queryKeyFor2(id, input) {
|
|
893
|
+
const canonical = canonicalize2(input);
|
|
894
|
+
return canonical === undefined ? ["dalp", id] : ["dalp", id, canonical];
|
|
895
|
+
}
|
|
896
|
+
var UI_SETTLEMENT_TIMEOUT_MS2 = 30000;
|
|
897
|
+
function createTanstackFactories(transport) {
|
|
898
|
+
return {
|
|
899
|
+
queryOptions: (route, id, input, options) => ({
|
|
900
|
+
queryKey: queryKeyFor2(id, input),
|
|
901
|
+
queryFn: (context) => sendRequest(transport, route, input, {
|
|
902
|
+
...options,
|
|
903
|
+
...context.signal === undefined ? {} : { signal: context.signal }
|
|
904
|
+
})
|
|
905
|
+
}),
|
|
906
|
+
mutationOptions: (route, id, options) => ({
|
|
907
|
+
mutationKey: ["dalp", id],
|
|
908
|
+
mutationFn: (input) => sendRequest(transport, route, input, { settlementTimeoutMs: UI_SETTLEMENT_TIMEOUT_MS2, ...options })
|
|
909
|
+
})
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
export { DalpApiError2, isDalpApiError2, findDalpSdkError2, makeDalpApiError2, createDalpContextHeaders2, createTransport, RESERVED_STREAM_FAILURE_EVENT2, STREAM_FAILED_ERROR_CODE2, decodeEventStream2, streamRequest, sendRequest, createFetchExecutor, canonicalize2, queryKeyFor2, UI_SETTLEMENT_TIMEOUT_MS2, createTanstackFactories };
|