@uru-intelligence/app 2.1.0 → 2.1.1

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/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.fetch ?? globalThis.fetch;
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.", {
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.fetch ?? globalThis.fetch)(`${base}${path}`, {
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.fetch ?? globalThis.fetch)(url, {
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 and the
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 and the
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
- const response = await args.runtimeFetch(args.url, {
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/uploads.js CHANGED
@@ -27,7 +27,7 @@
27
27
  * the grant is untouched and may be completed again.
28
28
  */
29
29
  import { UruAppRuntimeError } from './errors.js';
30
- import { resolveHeaders, runtimeBasePath, trimTrailingSlash } from './runtime.js';
30
+ import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash } from './runtime.js';
31
31
  import { isRuntimeJsonObject } from './transport-json.js';
32
32
  /** The platform's per-file ceiling. Restated so a caller is refused locally. */
33
33
  export const URU_UPLOAD_MAX_BYTES = 100 * 1024 * 1024;
@@ -142,7 +142,10 @@ function resultFrom(value) {
142
142
  }
143
143
  export function createUploadsContext(options) {
144
144
  const base = `${trimTrailingSlash(options.baseUrl ?? '')}${runtimeBasePath(options)}/apps/${encodeURIComponent(options.deploymentId)}/uploads`;
145
- const doFetch = options.fetch ?? globalThis.fetch;
145
+ // Resolved per call rather than once here, so building the context still
146
+ // costs nothing on a runtime whose `fetch` arrives later, and so the global
147
+ // is always called with the global as its receiver.
148
+ const doFetch = async (...args) => await resolveRuntimeFetch(options)(...args);
146
149
  const post = async (path, payload, signal) => {
147
150
  const headers = new Headers(await resolveHeaders(options.headers));
148
151
  if (payload !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uru-intelligence/app",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "private": false,
5
5
  "description": "Runtime client for Uru Gems: capability calls, Datasets, jobs and secrets",
6
6
  "keywords": [