buildful 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/cli.js +29 -2
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -55,13 +55,40 @@ process.on("SIGTERM", () => void shutdown("SIGTERM"));
55
55
  console.log("Starting a dedicated preview sandbox. This can take a few minutes…");
56
56
  const response = await fetch(new URL("/api/preview/resolve", api), {
57
57
  method: "POST",
58
- headers: { authorization: `Bearer ${token}` },
58
+ headers: { authorization: `Bearer ${token}`, accept: "application/x-ndjson" },
59
59
  });
60
60
  if (!response.ok) {
61
61
  const result = await response.json().catch(() => ({}));
62
62
  throw new Error(result.error || `Could not resolve preview (${response.status}).`);
63
63
  }
64
- const preview = await response.json();
64
+ let preview;
65
+ if ((response.headers.get("content-type") ?? "").includes("application/x-ndjson") && response.body) {
66
+ // The server streams progress logs (and live dev-server output) as
67
+ // newline-delimited JSON events, ending with a result or error event.
68
+ const decoder = new TextDecoder();
69
+ let buffer = "";
70
+ for await (const chunk of response.body) {
71
+ buffer += decoder.decode(chunk, { stream: true });
72
+ let index;
73
+ while ((index = buffer.indexOf("\n")) >= 0) {
74
+ const line = buffer.slice(0, index);
75
+ buffer = buffer.slice(index + 1);
76
+ if (!line.trim()) continue;
77
+ let event;
78
+ try {
79
+ event = JSON.parse(line);
80
+ } catch {
81
+ continue;
82
+ }
83
+ if (event.type === "log") console.log(event.message);
84
+ else if (event.type === "result") preview = event;
85
+ else if (event.type === "error") throw new Error(event.error || "Could not start the preview.");
86
+ }
87
+ }
88
+ if (!preview) throw new Error("The preview stream ended before the sandbox was ready.");
89
+ } else {
90
+ preview = await response.json();
91
+ }
65
92
  stopToken = preview.stopToken;
66
93
  if (shuttingDown) {
67
94
  await stopPreview();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "buildful",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Connect a local port to a Buildful sandbox preview",
5
5
  "type": "module",
6
6
  "bin": {