@autono/pinbox-toolbar 0.19.0 → 0.21.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.
@@ -1,4 +1,53 @@
1
1
  import { a as readHubToken, i as ensureHub, n as VIRTUAL_ID, r as resolveOptions, t as RESOLVED_VIRTUAL_ID } from "../options-DSrJgbJh.js";
2
+ import { execFile } from "node:child_process";
3
+ import { createHash } from "node:crypto";
4
+ import { realpathSync } from "node:fs";
5
+ //#region src/plugins/previews.ts
6
+ const PREVIEWS_PATH = "/__pinbox_previews";
7
+ function servePreviews(server, cwd, executable) {
8
+ server.middlewares.use((req, res, next) => {
9
+ const path = req.url?.split("?")[0];
10
+ if (path !== "/__pinbox_previews" && path !== `/__pinbox_previews/identity`) return next();
11
+ res.setHeader("Content-Type", "application/json");
12
+ res.setHeader("Cache-Control", "no-store");
13
+ if (req.method !== "GET" || req.headers["sec-fetch-site"] === "cross-site") {
14
+ res.statusCode = 403;
15
+ res.end(JSON.stringify({
16
+ ok: false,
17
+ error: { message: "same-origin GET required" }
18
+ }));
19
+ return;
20
+ }
21
+ if (path === `/__pinbox_previews/identity`) {
22
+ res.end(JSON.stringify({ id: createHash("sha256").update(realpathSync(cwd)).digest("hex").slice(0, 12) }));
23
+ return;
24
+ }
25
+ execFile(executable, [
26
+ "preview",
27
+ "list",
28
+ "--json"
29
+ ], {
30
+ cwd,
31
+ timeout: 6e3,
32
+ maxBuffer: 1048576
33
+ }, (error, stdout) => {
34
+ if (error) {
35
+ res.statusCode = 503;
36
+ res.end(JSON.stringify({
37
+ ok: false,
38
+ error: { message: "Preview discovery unavailable. Update the Pinbox CLI." }
39
+ }));
40
+ } else try {
41
+ JSON.parse(stdout);
42
+ res.end(stdout);
43
+ } catch {
44
+ res.statusCode = 503;
45
+ res.end("{\"ok\":false}");
46
+ }
47
+ });
48
+ });
49
+ }
50
+ //#endregion
2
51
  //#region src/plugins/snippet.ts
3
52
  /**
4
53
  * Build the module source injected into the dev page.
@@ -24,14 +73,17 @@ import { a as readHubToken, i as ensureHub, n as VIRTUAL_ID, r as resolveOptions
24
73
  * this import was dropped for real. `src/build.test.ts` bundles this exact import with Vite and
25
74
  * asserts `customElements.define` survives, because statting dist/ cannot see that class of break.
26
75
  */
27
- function buildBootstrap(hubUrl, token) {
76
+ function buildBootstrap(hubUrl, token, previews) {
28
77
  const hub = JSON.stringify(hubUrl);
78
+ const tag = JSON.stringify("pinbox-toolbar");
79
+ const tokenLiteral = token === void 0 ? "null" : JSON.stringify(token);
29
80
  return `// pinbox dev toolbar — injected by @autono/pinbox-toolbar (dev only)
30
81
  import "@autono/pinbox-toolbar";
82
+ ${previews ? "import { mountPreviewSwitcher } from \"@autono/pinbox-toolbar\";" : ""}
31
83
 
32
- const TAG = ${JSON.stringify("pinbox-toolbar")};
84
+ const TAG = ${tag};
33
85
  const HUB = ${hub};
34
- const TOKEN = ${token === void 0 ? "null" : JSON.stringify(token)};
86
+ const TOKEN = ${tokenLiteral};
35
87
 
36
88
  function mount() {
37
89
  let el = document.querySelector(TAG);
@@ -44,6 +96,7 @@ function mount() {
44
96
  if (TOKEN === null) el.removeAttribute("token");
45
97
  else el.setAttribute("token", TOKEN);
46
98
  if (created) document.body.appendChild(el);
99
+ ${previews ? `mountPreviewSwitcher(el, ${JSON.stringify(previews)});` : ""}
47
100
  }
48
101
 
49
102
  if (document.readyState === "loading") {
@@ -63,6 +116,8 @@ const PLUGIN_NAME = "pinbox";
63
116
  */
64
117
  function pinbox(options) {
65
118
  const resolved = resolveOptions(options);
119
+ const previews = options?.previews ?? process.env["PINBOX_PREVIEWS"] === "1";
120
+ const previewExecutable = options?.previewExecutable ?? process.env["PINBOX_PREVIEW_EXECUTABLE"] ?? "pinbox";
66
121
  if (resolved.disabled) return { name: PLUGIN_NAME };
67
122
  let hubPromise;
68
123
  function hub() {
@@ -72,7 +127,8 @@ function pinbox(options) {
72
127
  return {
73
128
  name: PLUGIN_NAME,
74
129
  apply: "serve",
75
- async configureServer() {
130
+ async configureServer(server) {
131
+ if (previews) servePreviews(server, resolved.projectRoot, previewExecutable);
76
132
  await hub();
77
133
  },
78
134
  resolveId(id) {
@@ -83,7 +139,7 @@ function pinbox(options) {
83
139
  if (id !== "\0virtual:pinbox-toolbar") return null;
84
140
  const status = await hub();
85
141
  if (status === void 0) return "// pinbox: hub unavailable, toolbar not mounted.\nexport {};\n";
86
- return buildBootstrap(status.url, readHubToken(resolved.projectRoot));
142
+ return buildBootstrap(status.url, readHubToken(resolved.projectRoot), previews ? PREVIEWS_PATH : void 0);
87
143
  },
88
144
  transformIndexHtml: {
89
145
  order: "pre",
package/dist/react.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as PinboxConfig } from "./index-DENhglDF.js";
1
+ import { n as PinboxConfig } from "./index-CMSOhoVs.js";
2
2
  import { ReactElement } from "react";
3
3
  //#region src/react.d.ts
4
4
  /**
package/dist/react.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as defineToolbarElement, r as PinboxToolbarElement } from "./src-BsWnyGwB.js";
1
+ import { i as PinboxToolbarElement, n as defineToolbarElement } from "./src-B134WXZ8.js";
2
2
  import { createElement, useEffect, useRef } from "react";
3
3
  //#region src/react.ts
4
4
  /**