@acryldev/blends-frame 0.2.2 → 0.2.4

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/lib/contract.d.ts CHANGED
@@ -148,6 +148,8 @@ export declare const FRAME_PLUGINS_SWITCH_PATH = "/api/acryl-blends-frame/plugin
148
148
  export declare const FRAME_APP_SHARE_PLAN_PATH = "/api/acryl-blends-frame/app/share-plan";
149
149
  /** POST `{ owner?, visibility?, confirm }`: share the app exactly as the confirmed plan says, or nothing. */
150
150
  export declare const FRAME_APP_SHARE_PATH = "/api/acryl-blends-frame/app/share";
151
+ /** POST `{ png: base64 }`: the app's own icon (assets/icon.png), drawn by the page at 512 px. */
152
+ export declare const FRAME_APP_ICON_PATH = "/api/acryl-blends-frame/app/icon";
151
153
  /** Who may see a shared app, and under which account; both optional (the signed-in account, blend.yaml's visibility). */
152
154
  export interface AppShareRequest {
153
155
  readonly owner?: string;
package/lib/index.d.ts CHANGED
@@ -165,8 +165,10 @@ export interface FrameRoutesDeps {
165
165
  readonly selection?: SelectionInbox;
166
166
  /** Share the whole app to GitHub (Settings > Share), through the app's own share command. */
167
167
  readonly appShare?: AppSharePort;
168
+ /** The app's folder, where Settings > App writes its icon. */
169
+ readonly appIconRoot?: string;
168
170
  }
169
- export declare function frameRoutes({ state, report, plugins, graph, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection, appShare }: FrameRoutesDeps): Acryl;
171
+ export declare function frameRoutes({ state, report, plugins, graph, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection, appShare, appIconRoot }: FrameRoutesDeps): Acryl;
170
172
  /**
171
173
  * The app's rows, plugins first and routes after (the order they start in): each as the reloader drives it. A row's module is imported under the
172
174
  * current generation (`./app-generation.ts`) by the loader itself, so `restart` stops the row's plugin and has the loader start the entry again - not
package/lib/index.js CHANGED
@@ -111,6 +111,8 @@ const FRAME_PLUGINS_SWITCH_PATH = `${FRAME_API_PREFIX}/plugins/switch`;
111
111
  const FRAME_APP_SHARE_PLAN_PATH = `${FRAME_API_PREFIX}/app/share-plan`;
112
112
  /** POST `{ owner?, visibility?, confirm }`: share the app exactly as the confirmed plan says, or nothing. */
113
113
  const FRAME_APP_SHARE_PATH = `${FRAME_API_PREFIX}/app/share`;
114
+ /** POST `{ png: base64 }`: the app's own icon (assets/icon.png), drawn by the page at 512 px. */
115
+ const FRAME_APP_ICON_PATH = `${FRAME_API_PREFIX}/app/icon`;
114
116
  function parseAppSharePlan(value) {
115
117
  if (!isRecord$2(value)) throw new Error("app share plan: not a plan");
116
118
  const { repository, create, visibility, changes } = value;
@@ -913,6 +915,40 @@ function appShareCommand(root, env = process.env, run = runCommand(root, env)) {
913
915
  }
914
916
  };
915
917
  }
918
+ // src/app-icon.ts
919
+ /**
920
+ * The app's own icon, chosen in Settings > App: one PNG at `<app>/assets/icon.png`, the file the page serves as the tab's icon and the desktop window
921
+ * shows (its Dock icon on macOS from the window's next start). The page sends a PNG it drew at 512 px; anything else is refused.
922
+ */
923
+ const APP_ICON_FILE = join("assets", "icon.png");
924
+ const APP_ICON_MAX_BYTES = 2097152;
925
+ const PNG_SIGNATURE = [
926
+ 137,
927
+ 80,
928
+ 78,
929
+ 71,
930
+ 13,
931
+ 10,
932
+ 26,
933
+ 10
934
+ ];
935
+ var AppIconRefused = class extends Error {};
936
+ /** The PNG's bytes from the page's base64, or a refusal in words for the person. */
937
+ function iconBytes(base64) {
938
+ if (typeof base64 !== "string" || base64.length === 0) throw new AppIconRefused("choose an image first");
939
+ if (base64.length > Math.ceil(2097152 / 3) * 4) throw new AppIconRefused("that image is too large: choose one under 2 MB");
940
+ const bytes = Buffer.from(base64, "base64");
941
+ if (!PNG_SIGNATURE.every((byte, index) => bytes[index] === byte)) throw new AppIconRefused("that is not a PNG image");
942
+ return bytes;
943
+ }
944
+ /** Write the icon into the app, replacing any earlier one in one step (a half-written icon is never served). */
945
+ async function saveAppIcon(root, bytes) {
946
+ const target = join(root, APP_ICON_FILE);
947
+ await mkdir(join(root, "assets"), { recursive: true });
948
+ const temporary = `${target}.${String(process.pid)}.tmp`;
949
+ await writeFile(temporary, bytes);
950
+ await rename(temporary, target);
951
+ }
916
952
  // src/graph-contract.ts
917
953
  /**
918
954
  * The plugin graph's wire types: what the frame's Host answers and what the browser draws (Settings > Plugins > Graph). Ported from cordis-plugin-graph
@@ -3503,7 +3539,7 @@ function parseBranchName(body) {
3503
3539
  const problem = branchNameProblem(name);
3504
3540
  return problem === void 0 ? { name } : { problem };
3505
3541
  }
3506
- function frameRoutes({ state, report = () => {}, plugins, graph, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection, appShare }) {
3542
+ function frameRoutes({ state, report = () => {}, plugins, graph, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection, appShare, appIconRoot }) {
3507
3543
  const app = new Acryl();
3508
3544
  if (appShare !== void 0) {
3509
3545
  const refusal = (cause) => cause instanceof AppShareRefused ? { say: cause.message } : {
@@ -3543,6 +3579,17 @@ function frameRoutes({ state, report = () => {}, plugins, graph, feedback, branc
3543
3579
  }
3544
3580
  });
3545
3581
  }
3582
+ if (appIconRoot !== void 0) app.post(FRAME_APP_ICON_PATH, ownPage, jsonBody(APP_ICON_MAX_BYTES * 2), async (c) => {
3583
+ const body = await readJson(c);
3584
+ try {
3585
+ await saveAppIcon(appIconRoot, iconBytes(body === INVALID_JSON || body === null || typeof body !== "object" ? void 0 : body.png));
3586
+ return c.json({ ok: true });
3587
+ } catch (cause) {
3588
+ if (cause instanceof AppIconRefused) return c.json(error(cause.message), 400);
3589
+ report(cause);
3590
+ return c.json(error("the icon could not be saved"), 500);
3591
+ }
3592
+ });
3546
3593
  if (appReload !== void 0) app.get(FRAME_APP_PATH, ownPage, async (c) => {
3547
3594
  const after = Number(c.req.query("after"));
3548
3595
  return c.json(c.req.query("after") !== void 0 && Number.isInteger(after) ? await appReload.wait(after, APP_POLL_MS) : appReload.view());
@@ -4138,6 +4185,7 @@ function apply(ctx, config = {}) {
4138
4185
  plugins,
4139
4186
  graph: graph.port,
4140
4187
  appShare: appShareCommand(root),
4188
+ appIconRoot: root,
4141
4189
  feedback: feedbackPort(() => {
4142
4190
  return (ctx.get("loader")?.entries().find((row) => row.options.id === APP_PLUGINS_ROW))?.fiber?.ctx?.get("feedback") ?? ctx.get("feedback");
4143
4191
  }),