@makoai/app-sdk 2.5.0 → 2.7.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/vite.js CHANGED
@@ -206,19 +206,93 @@ export function makoData(options = {}) {
206
206
  }
207
207
  }
208
208
 
209
+ // Long builds run as jobs: the API answers 202 + a job id (it would
210
+ // otherwise hit the edge's 100 s limit and fail with a 524 while the
211
+ // build carried on), and this polls the job until it lands. An API
212
+ // without jobs just answers synchronously, which works as before.
213
+ const pollMs = options.pollIntervalMs ?? 1000;
214
+ const buildTimeoutMs = options.buildTimeoutMs ?? 30 * 60 * 1000;
215
+ // A poll that fails transiently — the network, a 429, a 5xx, the
216
+ // edge's 524 — is retried with backoff until the overall deadline; only
217
+ // a definitive 4xx (the job is gone, access was revoked) ends the wait.
218
+ const isTransient = (status) => status === 429 || status >= 500;
219
+ async function awaitJob(name, jobId) {
220
+ const url = `${appBase()}/binding-jobs/${encodeURIComponent(jobId)}`;
221
+ const deadline = Date.now() + buildTimeoutMs;
222
+ let wait = pollMs;
223
+ let lastProblem = "";
224
+ for (;;) {
225
+ let res = null;
226
+ try {
227
+ res = await apiFetch(url, { headers: await headers() });
228
+ } catch (error) {
229
+ lastProblem = error instanceof Error ? error.message : String(error);
230
+ }
231
+ if (res) {
232
+ const body = await res.json().catch(() => ({}));
233
+ if (res.ok) {
234
+ if (body.status === "ready") return body;
235
+ if (body.status === "error") {
236
+ const error = new Error(body.error || `build ${name} failed`);
237
+ error.status = body.errorStatus ?? 502;
238
+ throw error;
239
+ }
240
+ lastProblem = "";
241
+ } else if (isTransient(res.status)) {
242
+ lastProblem = `job HTTP ${res.status}`;
243
+ } else {
244
+ const error = new Error(body.error || `build ${name}: job HTTP ${res.status}`);
245
+ error.status = res.status;
246
+ throw error;
247
+ }
248
+ }
249
+ if (Date.now() > deadline) {
250
+ const error = new Error(
251
+ `build ${name} still running after ${Math.round(buildTimeoutMs / 1000)} s (job ${jobId})` +
252
+ (lastProblem ? `; last poll: ${lastProblem}` : ""),
253
+ );
254
+ error.status = 504;
255
+ throw error;
256
+ }
257
+ await new Promise((resolve) => setTimeout(resolve, wait));
258
+ wait = Math.min(wait * 2, pollMs * 5);
259
+ }
260
+ }
261
+ async function jobArtifact(name, jobId) {
262
+ const res = await apiFetch(`${appBase()}/binding-jobs/${encodeURIComponent(jobId)}/artifact`, {
263
+ headers: await headers(),
264
+ });
265
+ if (!res.ok) {
266
+ const text = await res.text().catch(() => "");
267
+ const error = new Error(`build ${name}: result HTTP ${res.status} ${text.slice(0, 300)}`);
268
+ error.status = res.status;
269
+ throw error;
270
+ }
271
+ return Buffer.from(await res.arrayBuffer());
272
+ }
273
+ // POST …/materialize, asynchronously when the API supports it.
274
+ // Resolves to the materialize answer ({ rowCount, byteSize, materializedAt }).
275
+ async function materialize(name) {
276
+ const res = await apiFetch(
277
+ `${appBase()}/bindings/${encodeURIComponent(name)}/materialize?async=1`,
278
+ { method: "POST", headers: await headers() },
279
+ );
280
+ const body = await res.json().catch(() => ({}));
281
+ if (res.status === 202 && body.jobId) return awaitJob(name, body.jobId);
282
+ if (!res.ok) {
283
+ const error = new Error(body.error || `materialize ${name}: HTTP ${res.status}`);
284
+ error.status = res.status;
285
+ throw error;
286
+ }
287
+ return body;
288
+ }
289
+
209
290
  async function fetchArtifact(name) {
210
291
  const url = `${appBase()}/bindings/${encodeURIComponent(name)}/artifact`;
211
292
  let res = await apiFetch(url, { headers: await headers() });
212
293
  if (res.status === 404 && options.materialize !== false) {
213
294
  // Never materialized (or a live binding): build it now, then read.
214
- const built = await apiFetch(
215
- `${appBase()}/bindings/${encodeURIComponent(name)}/materialize`,
216
- { method: "POST", headers: await headers() },
217
- );
218
- if (!built.ok) {
219
- const text = await built.text().catch(() => "");
220
- throw new Error(`materialize ${name}: HTTP ${built.status} ${text.slice(0, 300)}`);
221
- }
295
+ await materialize(name);
222
296
  res = await apiFetch(url, { headers: await headers() });
223
297
  }
224
298
  if (!res.ok) {
@@ -291,9 +365,16 @@ export function makoData(options = {}) {
291
365
  source,
292
366
  ...(ctx.dbtEnvironment ? { dbtEnvironment: ctx.dbtEnvironment } : {}),
293
367
  ...(refresh ? { refresh: true } : {}),
368
+ async: true,
294
369
  }),
295
370
  });
296
- if (!res.ok) {
371
+ let jobDone = null;
372
+ if (res.status === 202) {
373
+ const queued = await res.json().catch(() => ({}));
374
+ if (!queued.jobId) throw new Error(`build ${name}: HTTP 202 without a job id`);
375
+ jobDone = await awaitJob(name, queued.jobId);
376
+ }
377
+ if (!jobDone && !res.ok) {
297
378
  const text = await res.text().catch(() => "");
298
379
  let body = null;
299
380
  try {
@@ -323,16 +404,26 @@ export function makoData(options = {}) {
323
404
  error.status = res.status;
324
405
  throw error;
325
406
  }
326
- const buf = Buffer.from(await res.arrayBuffer());
327
- const meta = {
328
- fingerprint,
329
- dbtEnvironment: ctx.dbtEnvironment || null,
330
- // A redirect to the stored artifact loses the API's headers; that
331
- // answer is always the committed artifact.
332
- build: res.headers.get("x-mako-build") ?? "artifact",
333
- rowCount: Number(res.headers.get("x-mako-row-count") ?? "") || null,
334
- builtAt: res.headers.get("x-mako-materialized-at") ?? new Date().toISOString(),
335
- };
407
+ const buf = jobDone
408
+ ? await jobArtifact(name, jobDone.jobId)
409
+ : Buffer.from(await res.arrayBuffer());
410
+ const meta = jobDone
411
+ ? {
412
+ fingerprint,
413
+ dbtEnvironment: ctx.dbtEnvironment || null,
414
+ build: jobDone.build ?? "artifact",
415
+ rowCount: jobDone.rowCount ?? null,
416
+ builtAt: jobDone.materializedAt ?? new Date().toISOString(),
417
+ }
418
+ : {
419
+ fingerprint,
420
+ dbtEnvironment: ctx.dbtEnvironment || null,
421
+ // A redirect to the stored artifact loses the API's headers;
422
+ // that answer is always the committed artifact.
423
+ build: res.headers.get("x-mako-build") ?? "artifact",
424
+ rowCount: Number(res.headers.get("x-mako-row-count") ?? "") || null,
425
+ builtAt: res.headers.get("x-mako-materialized-at") ?? new Date().toISOString(),
426
+ };
336
427
  writeCache(name, buf, meta);
337
428
  return { buf, meta };
338
429
  })().finally(() => inflight.delete(key));
@@ -357,15 +448,13 @@ export function makoData(options = {}) {
357
448
  materializedAt: built.meta.builtAt,
358
449
  });
359
450
  }
360
- const materialized = await apiFetch(
361
- `${appBase()}/bindings/${encodeURIComponent(name)}/materialize`,
362
- { method: "POST", headers: await headers() },
363
- );
364
- const body = await materialized.json().catch(() => ({}));
365
- if (!materialized.ok) {
366
- return json(res, materialized.status, {
451
+ let body;
452
+ try {
453
+ body = await materialize(name);
454
+ } catch (error) {
455
+ return json(res, error?.status ?? 502, {
367
456
  success: false,
368
- error: body.error || `materialize ${name}: HTTP ${materialized.status}`,
457
+ error: error instanceof Error ? error.message : String(error),
369
458
  });
370
459
  }
371
460
  fs.rmSync(cachePath(name), { force: true });