@uru-intelligence/app 2.1.0 → 2.1.2
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 +26 -0
- package/dist/dataset-boundary.js +15 -0
- package/dist/datasets.js +6 -12
- package/dist/files.js +2 -2
- package/dist/jobs.js +2 -2
- package/dist/runtime.d.ts +23 -2
- package/dist/runtime.js +36 -2
- package/dist/transport.js +6 -14
- package/dist/types.d.ts +28 -4
- package/dist/uploads.d.ts +8 -0
- package/dist/uploads.js +78 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -298,3 +298,29 @@ Building Gems is documented at
|
|
|
298
298
|
|
|
299
299
|
Proprietary. See [LICENSE](./LICENSE). You may use and redistribute this package
|
|
300
300
|
as part of an application that runs on the Uru platform.
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
### Conditional Dataset saves
|
|
304
|
+
|
|
305
|
+
Native row queries return `revision`. Pass it as `expectedDatasetRevision` with
|
|
306
|
+
an `idempotencyKey` to reject changes made since that read:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
const page = await uru.datasets.queryRows('contacts', { limit: 1 });
|
|
310
|
+
if (page.revision === undefined) throw new Error('Native revision required');
|
|
311
|
+
await uru.datasets.upsertRows('contacts', [{ id: rowId, name: 'Updated' }],
|
|
312
|
+
undefined, { expectedDatasetRevision: page.revision, idempotencyKey: saveId });
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Keep the key for that save across retries. Hosted Gems scope it to the verified
|
|
316
|
+
Gem and viewer. A repeated request can report a conflict if preparing the row
|
|
317
|
+
would now produce different values. Read the saved record before retrying;
|
|
318
|
+
confirm the intended fields were saved or ask the user to resolve the conflict.
|
|
319
|
+
Never assign a new key merely to bypass a conflict. Browser conditional saves
|
|
320
|
+
require a built Gem runtime, including GitHub previews. External datasets do
|
|
321
|
+
not supply a native revision and remain read-only.
|
|
322
|
+
|
|
323
|
+
The fourth argument is the mutation options object. The optional third argument
|
|
324
|
+
remains the result decoder. Native query responses also include `runtimeContext`
|
|
325
|
+
with the server-verified `artifactId` and `viewerUserId` for local recovery state.
|
|
326
|
+
These returned identifiers do not grant access to other resources.
|
package/dist/dataset-boundary.js
CHANGED
|
@@ -101,6 +101,21 @@ const decodedRows = (value, decodeRow, label) => {
|
|
|
101
101
|
};
|
|
102
102
|
const rowsResultFields = (body, rows, label) => {
|
|
103
103
|
const output = { rows };
|
|
104
|
+
const revision = body.revision;
|
|
105
|
+
if (revision !== undefined) {
|
|
106
|
+
if (typeof revision !== "string" || !/^(0|[1-9][0-9]{0,19})$/.test(revision))
|
|
107
|
+
return invalidResponse(label, body, "revision");
|
|
108
|
+
output.revision = revision;
|
|
109
|
+
}
|
|
110
|
+
const runtimeContext = body.runtimeContext;
|
|
111
|
+
if (runtimeContext !== undefined) {
|
|
112
|
+
if (runtimeContext === null || typeof runtimeContext !== "object" || Array.isArray(runtimeContext) ||
|
|
113
|
+
typeof runtimeContext.artifactId !== "string" ||
|
|
114
|
+
(runtimeContext.viewerUserId !== null && typeof runtimeContext.viewerUserId !== "string")) {
|
|
115
|
+
return invalidResponse(label, body, "runtimeContext");
|
|
116
|
+
}
|
|
117
|
+
output.runtimeContext = { artifactId: runtimeContext.artifactId, viewerUserId: runtimeContext.viewerUserId };
|
|
118
|
+
}
|
|
104
119
|
const total = optionalTotal(body, label);
|
|
105
120
|
if (total !== undefined) {
|
|
106
121
|
output.total = total;
|
package/dist/datasets.js
CHANGED
|
@@ -8,15 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { datasetIdsForRequest, datasetRowIdsForRequest, datasetRowsForRequest, decodeDatasetMutationResult, decodeDatasetRowsResult, decodeDefaultDatasetMutationResult, decodeDefaultDatasetRowsResult, datasetWriteErrorMessage, serializeDatasetRequest, } from "./dataset-boundary.js";
|
|
10
10
|
import { UruAppRuntimeError } from "./errors.js";
|
|
11
|
-
import { requireCompletedRuntimeResponse, resolveHeaders, runtimeBasePath, trimTrailingSlash, } from "./runtime.js";
|
|
11
|
+
import { requireCompletedRuntimeResponse, resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash, } from "./runtime.js";
|
|
12
12
|
const fetchDatasetWrite = async (options, subpath, body) => {
|
|
13
|
-
const runtimeFetch = options
|
|
14
|
-
if (typeof runtimeFetch !== "function") {
|
|
15
|
-
throw new UruAppRuntimeError("Uru app runtime fetch is not available.", {
|
|
16
|
-
body: null,
|
|
17
|
-
status: 0,
|
|
18
|
-
});
|
|
19
|
-
}
|
|
13
|
+
const runtimeFetch = resolveRuntimeFetch(options);
|
|
20
14
|
const deploymentId = typeof options.deploymentId === "string" ? options.deploymentId.trim() : "";
|
|
21
15
|
if (!deploymentId) {
|
|
22
16
|
throw new UruAppRuntimeError("Uru app runtime deployment id is required.", {
|
|
@@ -108,8 +102,8 @@ const createQueryRowsInvoker = (app) => {
|
|
|
108
102
|
return queryRows;
|
|
109
103
|
};
|
|
110
104
|
const createRowMutationInvoker = (options) => {
|
|
111
|
-
async function mutate(datasetRef, rows, decodeResult) {
|
|
112
|
-
const body = upsertBody(rows);
|
|
105
|
+
async function mutate(datasetRef, rows, decodeResult, mutationOptions) {
|
|
106
|
+
const body = { ...upsertBody(rows), ...mutationOptions };
|
|
113
107
|
const path = datasetMutationPath(datasetRef);
|
|
114
108
|
if (decodeResult === undefined) {
|
|
115
109
|
return await postProviderDatasetWrite(options, path, body);
|
|
@@ -119,8 +113,8 @@ const createRowMutationInvoker = (options) => {
|
|
|
119
113
|
return mutate;
|
|
120
114
|
};
|
|
121
115
|
const createIdMutationInvoker = (options, operation) => {
|
|
122
|
-
async function mutate(datasetRef, rowIds, decodeResult) {
|
|
123
|
-
const body = idMutationBody(operation, rowIds);
|
|
116
|
+
async function mutate(datasetRef, rowIds, decodeResult, mutationOptions) {
|
|
117
|
+
const body = { ...idMutationBody(operation, rowIds), ...mutationOptions };
|
|
124
118
|
const path = datasetMutationPath(datasetRef);
|
|
125
119
|
if (decodeResult === undefined) {
|
|
126
120
|
return await postProviderDatasetWrite(options, path, body);
|
package/dist/files.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { UruAppRuntimeError } from './errors.js';
|
|
2
|
-
import { resolveHeaders, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
2
|
+
import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
3
3
|
import { isRuntimeJsonObject } from './transport-json.js';
|
|
4
4
|
const LIBRARY_READ_BYTES = 5 * 1024 * 1024;
|
|
5
5
|
const OUTPUT_BYTES = 10 * 1024 * 1024;
|
|
@@ -91,7 +91,7 @@ export function createFilesContext(options) {
|
|
|
91
91
|
const headers = new Headers(await resolveHeaders(options.headers));
|
|
92
92
|
if (payload !== undefined)
|
|
93
93
|
headers.set('content-type', 'application/json');
|
|
94
|
-
const response = await (options
|
|
94
|
+
const response = await resolveRuntimeFetch(options)(`${base}${path}`, {
|
|
95
95
|
method: payload === undefined ? 'GET' : 'POST', headers,
|
|
96
96
|
...(payload === undefined ? {} : { body: JSON.stringify(payload) }),
|
|
97
97
|
signal: signal === undefined ? AbortSignal.timeout(30_000) : AbortSignal.any([signal, AbortSignal.timeout(30_000)]),
|
package/dist/jobs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { UruAppRuntimeError } from './errors.js';
|
|
2
|
-
import { resolveHeaders, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
2
|
+
import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
3
3
|
import { isRuntimeJsonObject } from './transport-json.js';
|
|
4
4
|
const postJobUpdate = async (options, env, operation, payload) => {
|
|
5
5
|
const runId = Object.hasOwn(env, 'URU_JOB_RUN_ID') ? env['URU_JOB_RUN_ID']?.trim() : undefined;
|
|
@@ -10,7 +10,7 @@ const postJobUpdate = async (options, env, operation, payload) => {
|
|
|
10
10
|
const headers = new Headers(await resolveHeaders(options.headers));
|
|
11
11
|
headers.set('content-type', 'application/json');
|
|
12
12
|
const url = `${trimTrailingSlash(options.baseUrl ?? '')}${runtimeBasePath(options)}/apps/${encodeURIComponent(options.deploymentId)}/jobs/executions/${encodeURIComponent(runId)}/attempts/${encodeURIComponent(attemptId)}/${operation}`;
|
|
13
|
-
const response = await (options
|
|
13
|
+
const response = await resolveRuntimeFetch(options)(url, {
|
|
14
14
|
method: 'POST', headers, body: JSON.stringify(payload), signal: AbortSignal.timeout(30_000),
|
|
15
15
|
});
|
|
16
16
|
if (!response.ok) {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared runtime primitives: URL assembly, header resolution
|
|
3
|
-
* completed-response assertion
|
|
2
|
+
* Shared runtime primitives: URL assembly, header resolution, the
|
|
3
|
+
* completed-response assertion, and the one `fetch` resolver every module here
|
|
4
|
+
* calls the network through.
|
|
4
5
|
*
|
|
5
6
|
* INTERNAL. Nothing here is re-exported by `index.ts`. These four functions
|
|
6
7
|
* used to be public exports of the barrel for one reason only — `transport.ts`
|
|
@@ -30,3 +31,23 @@ export declare const resolveHeaders: (headers: CreateUruAppRuntimeClientOptions[
|
|
|
30
31
|
* than a state to render.
|
|
31
32
|
*/
|
|
32
33
|
export declare const requireCompletedRuntimeResponse: (responsePromise: Promise<UruAppRuntimeInvocationResponse>) => Promise<unknown>;
|
|
34
|
+
/** The `fetch` shape the transport calls. Exactly the caller-supplied option. */
|
|
35
|
+
export type RuntimeFetch = NonNullable<CreateUruAppRuntimeClientOptions["fetch"]>;
|
|
36
|
+
/**
|
|
37
|
+
* The one `fetch` every runtime call in this package goes through.
|
|
38
|
+
*
|
|
39
|
+
* The returned function MUST stay a wrapper around `globalThis.fetch` rather
|
|
40
|
+
* than the global itself. Holding the global in a variable and calling it from
|
|
41
|
+
* there drops its receiver, and a host that checks the receiver refuses the
|
|
42
|
+
* call: Cloudflare Workers answers
|
|
43
|
+
*
|
|
44
|
+
* Illegal invocation: function called with incorrect `this` reference
|
|
45
|
+
*
|
|
46
|
+
* and browsers throw the same kind of TypeError. The wrapper reads
|
|
47
|
+
* `globalThis.fetch` at CALL time, so `this` is the global object, which is
|
|
48
|
+
* what the host demands.
|
|
49
|
+
*
|
|
50
|
+
* `.bind` is deliberately not used: it would read the global once, at resolve
|
|
51
|
+
* time, and a runtime that installs `fetch` later would be captured as missing.
|
|
52
|
+
*/
|
|
53
|
+
export declare const resolveRuntimeFetch: (options: CreateUruAppRuntimeClientOptions) => RuntimeFetch;
|
package/dist/runtime.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared runtime primitives: URL assembly, header resolution
|
|
3
|
-
* completed-response assertion
|
|
2
|
+
* Shared runtime primitives: URL assembly, header resolution, the
|
|
3
|
+
* completed-response assertion, and the one `fetch` resolver every module here
|
|
4
|
+
* calls the network through.
|
|
4
5
|
*
|
|
5
6
|
* INTERNAL. Nothing here is re-exported by `index.ts`. These four functions
|
|
6
7
|
* used to be public exports of the barrel for one reason only — `transport.ts`
|
|
@@ -69,3 +70,36 @@ export const requireCompletedRuntimeResponse = async (responsePromise) => {
|
|
|
69
70
|
}
|
|
70
71
|
return response.result;
|
|
71
72
|
};
|
|
73
|
+
/**
|
|
74
|
+
* The one `fetch` every runtime call in this package goes through.
|
|
75
|
+
*
|
|
76
|
+
* The returned function MUST stay a wrapper around `globalThis.fetch` rather
|
|
77
|
+
* than the global itself. Holding the global in a variable and calling it from
|
|
78
|
+
* there drops its receiver, and a host that checks the receiver refuses the
|
|
79
|
+
* call: Cloudflare Workers answers
|
|
80
|
+
*
|
|
81
|
+
* Illegal invocation: function called with incorrect `this` reference
|
|
82
|
+
*
|
|
83
|
+
* and browsers throw the same kind of TypeError. The wrapper reads
|
|
84
|
+
* `globalThis.fetch` at CALL time, so `this` is the global object, which is
|
|
85
|
+
* what the host demands.
|
|
86
|
+
*
|
|
87
|
+
* `.bind` is deliberately not used: it would read the global once, at resolve
|
|
88
|
+
* time, and a runtime that installs `fetch` later would be captured as missing.
|
|
89
|
+
*/
|
|
90
|
+
export const resolveRuntimeFetch = (options) => {
|
|
91
|
+
// A caller's own `fetch` is returned exactly as given, EXCEPT when the caller
|
|
92
|
+
// handed us the global itself — `{ fetch: globalThis.fetch }` is the obvious
|
|
93
|
+
// thing for a Gem to write, and it needs the same receiver the global always
|
|
94
|
+
// needs.
|
|
95
|
+
if (options.fetch !== undefined && options.fetch !== globalThis.fetch) {
|
|
96
|
+
return options.fetch;
|
|
97
|
+
}
|
|
98
|
+
if (typeof globalThis.fetch !== "function") {
|
|
99
|
+
throw new UruAppRuntimeError("Uru app runtime fetch is not available.", {
|
|
100
|
+
body: null,
|
|
101
|
+
status: 0,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return async (...args) => await globalThis.fetch(...args);
|
|
105
|
+
};
|
package/dist/transport.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
import { computeRateLimitBackoffMs, parseRetryAfterSeconds, } from "./dampers.js";
|
|
19
19
|
import { runtimeErrorMessage } from "./error-response.js";
|
|
20
20
|
import { UruAppRuntimeError } from "./errors.js";
|
|
21
|
-
import { resolveHeaders, runtimeBasePath, trimTrailingSlash, } from "./runtime.js";
|
|
21
|
+
import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash, } from "./runtime.js";
|
|
22
22
|
import { isRuntimeJsonObject } from "./transport-json.js";
|
|
23
23
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
24
|
const httpRecord = (value, label) => {
|
|
@@ -286,18 +286,6 @@ const emitRuntimeEvent = (options, event) => {
|
|
|
286
286
|
options.onStatus(event);
|
|
287
287
|
}
|
|
288
288
|
};
|
|
289
|
-
const resolveRuntimeFetch = (options) => {
|
|
290
|
-
if (options.fetch !== undefined) {
|
|
291
|
-
return options.fetch;
|
|
292
|
-
}
|
|
293
|
-
if (typeof globalThis.fetch !== "function") {
|
|
294
|
-
throw new UruAppRuntimeError("Uru app runtime fetch is not available.", {
|
|
295
|
-
body: null,
|
|
296
|
-
status: 0,
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
return globalThis.fetch;
|
|
300
|
-
};
|
|
301
289
|
const invocationIdentity = (options, name) => {
|
|
302
290
|
const capabilityName = name.trim();
|
|
303
291
|
const deploymentId = options.deploymentId.trim();
|
|
@@ -364,7 +352,11 @@ const invocationRequestBody = (transport, runtimeInput, effectiveForce, damper,
|
|
|
364
352
|
};
|
|
365
353
|
const responseBody = async (response) => await response.json().catch(() => null);
|
|
366
354
|
const fetchInvocation = async (args, attempt = 0) => {
|
|
367
|
-
|
|
355
|
+
// Read out of `args` first. `args.runtimeFetch(...)` is a METHOD call, so it
|
|
356
|
+
// would hand the fetch `args` as its receiver — the same class of mistake
|
|
357
|
+
// that broke the global one.
|
|
358
|
+
const { runtimeFetch } = args;
|
|
359
|
+
const response = await runtimeFetch(args.url, {
|
|
368
360
|
body: args.requestBody,
|
|
369
361
|
headers: args.headers,
|
|
370
362
|
method: "POST",
|
package/dist/types.d.ts
CHANGED
|
@@ -151,6 +151,11 @@ export interface UruDatasetQuery {
|
|
|
151
151
|
/** One page of Dataset rows, plus the total when `includeTotal` asked for it. */
|
|
152
152
|
export interface UruDatasetRowsResult<TRow = UruDatasetRow> {
|
|
153
153
|
rows: TRow[];
|
|
154
|
+
revision?: string;
|
|
155
|
+
runtimeContext?: {
|
|
156
|
+
artifactId: string;
|
|
157
|
+
viewerUserId: string | null;
|
|
158
|
+
};
|
|
154
159
|
total?: number | null;
|
|
155
160
|
limit?: number;
|
|
156
161
|
offset?: number;
|
|
@@ -186,13 +191,18 @@ export interface UruDatasetQueryRows {
|
|
|
186
191
|
<TRow>(datasetRef: string, query: UruDatasetQuery | undefined, decodeRow: UruResultDecoder<TRow>): Promise<UruDatasetRowsResult<TRow>>;
|
|
187
192
|
(datasetRef: string, query?: UruDatasetQuery): Promise<UruDatasetRowsResult>;
|
|
188
193
|
}
|
|
194
|
+
/** Public wire contract: kept local because this package has no private workspace dependencies. */
|
|
195
|
+
export interface UruDatasetMutationOptions {
|
|
196
|
+
expectedDatasetRevision?: string;
|
|
197
|
+
idempotencyKey?: string;
|
|
198
|
+
}
|
|
189
199
|
export interface UruDatasetRowMutation {
|
|
190
|
-
<TResult>(datasetRef: string, rows: readonly unknown[], decodeResult: UruResultDecoder<TResult
|
|
191
|
-
(datasetRef: string, rows: readonly unknown[]): Promise<UruDatasetMutationResult>;
|
|
200
|
+
<TResult>(datasetRef: string, rows: readonly unknown[], decodeResult: UruResultDecoder<TResult>, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult<TResult>>;
|
|
201
|
+
(datasetRef: string, rows: readonly unknown[], decodeResult?: undefined, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult>;
|
|
192
202
|
}
|
|
193
203
|
export interface UruDatasetIdMutation {
|
|
194
|
-
<TResult>(datasetRef: string, rowIds: readonly string[], decodeResult: UruResultDecoder<TResult
|
|
195
|
-
(datasetRef: string, rowIds: readonly string[]): Promise<UruDatasetMutationResult>;
|
|
204
|
+
<TResult>(datasetRef: string, rowIds: readonly string[], decodeResult: UruResultDecoder<TResult>, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult<TResult>>;
|
|
205
|
+
(datasetRef: string, rowIds: readonly string[], decodeResult?: undefined, options?: UruDatasetMutationOptions): Promise<UruDatasetMutationResult>;
|
|
196
206
|
}
|
|
197
207
|
export interface UruDatasetSqlWrite {
|
|
198
208
|
<TResult>(sql: string, options: UruDatasetSqlWriteOptions, decodeResult: UruResultDecoder<TResult>): Promise<UruDatasetMutationResult<TResult>>;
|
|
@@ -295,6 +305,20 @@ export interface UruUploadsContext {
|
|
|
295
305
|
upload(file: File, options?: {
|
|
296
306
|
signal?: AbortSignal;
|
|
297
307
|
}): Promise<UruUploadResult>;
|
|
308
|
+
/**
|
|
309
|
+
* Read one VERIFIED upload back, as a `Blob`.
|
|
310
|
+
*
|
|
311
|
+
* A Blob rather than a URL because the read is AUTHENTICATED: the platform
|
|
312
|
+
* serves the bytes under the same authority that minted the grant, and an
|
|
313
|
+
* `<img src>` or an `<a href>` carries none of the runtime's headers. Hand
|
|
314
|
+
* the Blob to `URL.createObjectURL` for a preview or a download link.
|
|
315
|
+
*
|
|
316
|
+
* Rejects when the upload is not the caller's, was never verified, or no
|
|
317
|
+
* longer exists.
|
|
318
|
+
*/
|
|
319
|
+
read(uploadId: string, options?: {
|
|
320
|
+
signal?: AbortSignal;
|
|
321
|
+
}): Promise<Blob>;
|
|
298
322
|
}
|
|
299
323
|
export interface UruJobProgress {
|
|
300
324
|
readonly completed: number;
|
package/dist/uploads.d.ts
CHANGED
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
*
|
|
18
18
|
* `upload(file)` performs all three, which is what almost every caller wants.
|
|
19
19
|
*
|
|
20
|
+
* ## And one hop back
|
|
21
|
+
*
|
|
22
|
+
* `read(uploadId)` returns the stored file as a `Blob`. The bytes come back
|
|
23
|
+
* THROUGH the platform, not from a storage link: the read is authorized per
|
|
24
|
+
* request under the same authority that minted the grant, so there is no
|
|
25
|
+
* shareable URL to leak. Keep the `uploadId` a verified upload gave you --
|
|
26
|
+
* `uru.storage` is the durable place for it -- and pass it here.
|
|
27
|
+
*
|
|
20
28
|
* ## The refusals are values, not exceptions
|
|
21
29
|
*
|
|
22
30
|
* A grant that is refused (`size_mismatch`, `type_mismatch`, `unverifiable`) or
|
package/dist/uploads.js
CHANGED
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
*
|
|
18
18
|
* `upload(file)` performs all three, which is what almost every caller wants.
|
|
19
19
|
*
|
|
20
|
+
* ## And one hop back
|
|
21
|
+
*
|
|
22
|
+
* `read(uploadId)` returns the stored file as a `Blob`. The bytes come back
|
|
23
|
+
* THROUGH the platform, not from a storage link: the read is authorized per
|
|
24
|
+
* request under the same authority that minted the grant, so there is no
|
|
25
|
+
* shareable URL to leak. Keep the `uploadId` a verified upload gave you --
|
|
26
|
+
* `uru.storage` is the durable place for it -- and pass it here.
|
|
27
|
+
*
|
|
20
28
|
* ## The refusals are values, not exceptions
|
|
21
29
|
*
|
|
22
30
|
* A grant that is refused (`size_mismatch`, `type_mismatch`, `unverifiable`) or
|
|
@@ -27,7 +35,7 @@
|
|
|
27
35
|
* the grant is untouched and may be completed again.
|
|
28
36
|
*/
|
|
29
37
|
import { UruAppRuntimeError } from './errors.js';
|
|
30
|
-
import { resolveHeaders, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
38
|
+
import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
31
39
|
import { isRuntimeJsonObject } from './transport-json.js';
|
|
32
40
|
/** The platform's per-file ceiling. Restated so a caller is refused locally. */
|
|
33
41
|
export const URU_UPLOAD_MAX_BYTES = 100 * 1024 * 1024;
|
|
@@ -140,9 +148,54 @@ function resultFrom(value) {
|
|
|
140
148
|
cleanupFailed: row['cleanupFailed'] === true,
|
|
141
149
|
};
|
|
142
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Read the response body into a `Blob`, refusing anything above the platform's
|
|
153
|
+
* own per-file ceiling.
|
|
154
|
+
*
|
|
155
|
+
* Bounded for the same reason every other response in this SDK is: a runtime
|
|
156
|
+
* that answered with more bytes than a grant can ever hold would otherwise
|
|
157
|
+
* exhaust the Gem's memory through a call the Gem made on purpose.
|
|
158
|
+
*/
|
|
159
|
+
async function readBlob(response) {
|
|
160
|
+
const declared = Number(response.headers.get('content-length') ?? '');
|
|
161
|
+
if (Number.isFinite(declared) && declared > URU_UPLOAD_MAX_BYTES) {
|
|
162
|
+
await response.body?.cancel();
|
|
163
|
+
invalid();
|
|
164
|
+
}
|
|
165
|
+
const reader = response.body?.getReader();
|
|
166
|
+
if (reader === undefined)
|
|
167
|
+
invalid();
|
|
168
|
+
const parts = [];
|
|
169
|
+
let size = 0;
|
|
170
|
+
try {
|
|
171
|
+
for (;;) {
|
|
172
|
+
const chunk = await reader.read();
|
|
173
|
+
if (chunk.done)
|
|
174
|
+
break;
|
|
175
|
+
size += chunk.value.byteLength;
|
|
176
|
+
if (size > URU_UPLOAD_MAX_BYTES) {
|
|
177
|
+
await reader.cancel();
|
|
178
|
+
invalid();
|
|
179
|
+
}
|
|
180
|
+
parts.push(chunk.value);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
reader.releaseLock();
|
|
185
|
+
}
|
|
186
|
+
// The platform's stored media type, which is the one it verified the object
|
|
187
|
+
// against. A Blob that carried no type would render as a download of unknown
|
|
188
|
+
// kind wherever the Gem put it.
|
|
189
|
+
return new Blob(parts, {
|
|
190
|
+
type: response.headers.get('content-type') ?? 'application/octet-stream',
|
|
191
|
+
});
|
|
192
|
+
}
|
|
143
193
|
export function createUploadsContext(options) {
|
|
144
194
|
const base = `${trimTrailingSlash(options.baseUrl ?? '')}${runtimeBasePath(options)}/apps/${encodeURIComponent(options.deploymentId)}/uploads`;
|
|
145
|
-
|
|
195
|
+
// Resolved per call rather than once here, so building the context still
|
|
196
|
+
// costs nothing on a runtime whose `fetch` arrives later, and so the global
|
|
197
|
+
// is always called with the global as its receiver.
|
|
198
|
+
const doFetch = async (...args) => await resolveRuntimeFetch(options)(...args);
|
|
146
199
|
const post = async (path, payload, signal) => {
|
|
147
200
|
const headers = new Headers(await resolveHeaders(options.headers));
|
|
148
201
|
if (payload !== undefined)
|
|
@@ -187,9 +240,32 @@ export function createUploadsContext(options) {
|
|
|
187
240
|
invalid();
|
|
188
241
|
return resultFrom(await post(`/${uploadId}/complete`, undefined, settings?.signal));
|
|
189
242
|
};
|
|
243
|
+
const read = async (uploadId, settings) => {
|
|
244
|
+
if (typeof uploadId !== 'string' || !UUID.test(uploadId))
|
|
245
|
+
invalid();
|
|
246
|
+
const response = await doFetch(`${base}/${uploadId}/content`, {
|
|
247
|
+
method: 'GET',
|
|
248
|
+
headers: new Headers(await resolveHeaders(options.headers)),
|
|
249
|
+
// Five minutes, not the thirty seconds the JSON hops use: a grant admits
|
|
250
|
+
// 104,857,600 bytes, and thirty seconds would need 3.5 MB/s to finish a
|
|
251
|
+
// file the platform was happy to store.
|
|
252
|
+
signal: settings?.signal === undefined
|
|
253
|
+
? AbortSignal.timeout(300_000)
|
|
254
|
+
: AbortSignal.any([settings.signal, AbortSignal.timeout(300_000)]),
|
|
255
|
+
});
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
await response.body?.cancel();
|
|
258
|
+
throw new UruAppRuntimeError('Upload could not be read.', {
|
|
259
|
+
status: response.status,
|
|
260
|
+
body: null,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return readBlob(response);
|
|
264
|
+
};
|
|
190
265
|
return {
|
|
191
266
|
request,
|
|
192
267
|
complete,
|
|
268
|
+
read,
|
|
193
269
|
upload: async (file, settings) => {
|
|
194
270
|
// The media type is the browser's, lower-cased by it already. A file the
|
|
195
271
|
// OS could not type at all gets the generic one rather than an empty
|