@smallpen/local-package 0.1.0-alpha.1 → 0.1.0-alpha.11

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 (37) hide show
  1. package/assets/fonts/SourceSansPro-OFL.txt +46 -0
  2. package/assets/fonts/WorkSans-OFL.txt +46 -0
  3. package/package.json +4 -4
  4. package/runtime/fonts/OFL.txt +46 -0
  5. package/runtime/fonts/sourcesanspro-black.ttf +0 -0
  6. package/runtime/fonts/sourcesanspro-blackitalic.ttf +0 -0
  7. package/runtime/fonts/sourcesanspro-bold.ttf +0 -0
  8. package/runtime/fonts/sourcesanspro-bolditalic.ttf +0 -0
  9. package/runtime/fonts/sourcesanspro-extralight.ttf +0 -0
  10. package/runtime/fonts/sourcesanspro-extralightitalic.ttf +0 -0
  11. package/runtime/fonts/sourcesanspro-italic.ttf +0 -0
  12. package/runtime/fonts/sourcesanspro-light.ttf +0 -0
  13. package/runtime/fonts/sourcesanspro-lightitalic.ttf +0 -0
  14. package/runtime/fonts/sourcesanspro-regular.ttf +0 -0
  15. package/runtime/fonts/sourcesanspro-semibold.ttf +0 -0
  16. package/runtime/fonts/sourcesanspro-semibolditalic.ttf +0 -0
  17. package/runtime/manifest.json +7 -0
  18. package/runtime/render-wasm.js +15 -0
  19. package/runtime/render-wasm.wasm +0 -0
  20. package/runtime/renderer.mjs +169652 -0
  21. package/src/draft-import.mjs +1 -11
  22. package/src/font-convert.mjs +23 -1
  23. package/src/fs-utils.mjs +95 -0
  24. package/src/index.mjs +19 -3
  25. package/src/initialize.mjs +363 -136
  26. package/src/kiwi-runtime.mjs +38 -36
  27. package/src/library-network.mjs +189 -0
  28. package/src/local-package-backend.mjs +215 -24
  29. package/src/local-package.mjs +589 -177
  30. package/src/local-workspace-session.mjs +278 -96
  31. package/src/migrate-themes.mjs +116 -0
  32. package/src/remote-library.mjs +167 -28
  33. package/src/render-fonts.mjs +172 -0
  34. package/src/render-shared.mjs +346 -0
  35. package/src/render.mjs +71 -3557
  36. package/src/workspace.mjs +178 -17
  37. package/src/webp-decode.mjs +0 -73
@@ -1,6 +1,5 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import {
3
- lstat,
4
3
  mkdir,
5
4
  mkdtemp,
6
5
  rename,
@@ -21,6 +20,7 @@ import {
21
20
  SmallPenError,
22
21
  } from "@smallpen/core";
23
22
 
23
+ import { pathExists } from "./fs-utils.mjs";
24
24
  import {
25
25
  ByteBuffer,
26
26
  compileSchema,
@@ -170,16 +170,6 @@ function inspectSvg(bytes) {
170
170
  return { height, width };
171
171
  }
172
172
 
173
- async function pathExists(path) {
174
- try {
175
- await lstat(path);
176
- return true;
177
- } catch (error) {
178
- if (error?.code === "ENOENT") return false;
179
- throw error;
180
- }
181
- }
182
-
183
173
  export async function writeDraftPackage(outputValue, values) {
184
174
  const output = resolve(outputValue);
185
175
  if (!output.endsWith(".smallpen")) {
@@ -20,7 +20,9 @@ function matches(bytes, signature) {
20
20
  }
21
21
 
22
22
  export function inspectFont(bytes, mimeType) {
23
- const signature = FONT_SIGNATURES[mimeType];
23
+ const signature = Object.hasOwn(FONT_SIGNATURES, mimeType)
24
+ ? FONT_SIGNATURES[mimeType]
25
+ : undefined;
24
26
  if (!signature || !matches(bytes, signature)) {
25
27
  failFont(
26
28
  "invalid_font_blob",
@@ -108,6 +110,26 @@ export function sfntToWoff(bytes, mimeType) {
108
110
  return output;
109
111
  }
110
112
 
113
+ // Penpot names a Font Variant without a stored name from its weight and
114
+ // style (app.common.media/font-display-variant), e.g. "Bold Italic".
115
+ const FONT_WEIGHT_NAMES = {
116
+ 100: "Hairline",
117
+ 200: "Extra Light",
118
+ 300: "Light",
119
+ 400: "Regular",
120
+ 500: "Medium",
121
+ 600: "Semi Bold",
122
+ 700: "Bold",
123
+ 800: "Extra Bold",
124
+ 900: "Black",
125
+ 950: "Extra Black",
126
+ };
127
+
128
+ export function fontVariantName(weight, style) {
129
+ const base = FONT_WEIGHT_NAMES[Number(weight)] ?? String(weight);
130
+ return style === "italic" ? `${base} Italic` : base;
131
+ }
132
+
111
133
  export function prepareFontFiles(input) {
112
134
  const files = {};
113
135
  for (const [mimeType, bytes] of Object.entries(input)) {
@@ -0,0 +1,95 @@
1
+ import { constants } from "node:fs";
2
+ import { lstat, open } from "node:fs/promises";
3
+ import { isAbsolute, relative } from "node:path";
4
+
5
+ export function isWithinRoot(rootPath, candidatePath) {
6
+ const child = relative(rootPath, candidatePath);
7
+ return child === "" || (!child.startsWith("..") && !isAbsolute(child));
8
+ }
9
+
10
+ export async function pathExists(path) {
11
+ try {
12
+ await lstat(path);
13
+ return true;
14
+ } catch (error) {
15
+ if (error?.code === "ENOENT") return false;
16
+ throw error;
17
+ }
18
+ }
19
+
20
+ // Reads a file only when it is a regular file. A FIFO or device in a Package
21
+ // (an archive can carry one) would otherwise block the read, the write lock
22
+ // and a thread-pool thread forever. Opening without blocking and checking the
23
+ // opened handle leaves no gap between the check and the read.
24
+ //
25
+ // With `maxBytes`, a file larger than the limit fails with code EFILETOOBIG
26
+ // (and `size`/`maxBytes` on the error) instead of being buffered whole; the
27
+ // read itself is bounded too, so a file growing after the size check cannot
28
+ // pass the limit either.
29
+ export async function readRegularFile(path, encoding, { maxBytes } = {}) {
30
+ const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
31
+ try {
32
+ const info = await handle.stat();
33
+ if (!info.isFile()) {
34
+ const error = new Error(`Not a regular file: ${path}`);
35
+ error.code = "ENOTREGULAR";
36
+ throw error;
37
+ }
38
+ if (maxBytes === undefined) return await handle.readFile(encoding);
39
+ const tooBig = (size) =>
40
+ Object.assign(new Error(`File exceeds ${maxBytes} bytes: ${path}`), {
41
+ code: "EFILETOOBIG",
42
+ maxBytes,
43
+ size,
44
+ });
45
+ if (info.size > maxBytes) throw tooBig(info.size);
46
+ const chunks = [];
47
+ let length = 0;
48
+ while (length <= maxBytes) {
49
+ const chunk = Buffer.alloc(Math.min(Math.max(info.size - length, 0), maxBytes - length) + 1);
50
+ const { bytesRead } = await handle.read(chunk, 0, chunk.length, length);
51
+ if (bytesRead === 0) break;
52
+ chunks.push(chunk.subarray(0, bytesRead));
53
+ length += bytesRead;
54
+ }
55
+ if (length > maxBytes) throw tooBig(length);
56
+ const bytes = Buffer.concat(chunks, length);
57
+ return encoding ? bytes.toString(encoding) : bytes;
58
+ } finally {
59
+ await handle.close();
60
+ }
61
+ }
62
+
63
+ // Writes a new file and flushes it to stable storage before returning. The
64
+ // file must not exist yet: commits never truncate an inode in place because
65
+ // unchanged candidate files are hard links shared with the live Package.
66
+ export async function writeFileDurable(path, data, encoding) {
67
+ const handle = await open(path, "wx");
68
+ try {
69
+ await handle.writeFile(data, encoding);
70
+ await handle.sync();
71
+ } finally {
72
+ await handle.close();
73
+ }
74
+ }
75
+
76
+ // Flushes directory entries (creates, renames, unlinks). Some platforms and
77
+ // file systems (notably Windows) cannot open or sync a directory; durability
78
+ // there relies on the file-level syncs alone.
79
+ export async function syncDirectory(path) {
80
+ let handle;
81
+ try {
82
+ handle = await open(path, "r");
83
+ await handle.sync();
84
+ } catch (error) {
85
+ if (
86
+ !["EISDIR", "EPERM", "EACCES", "EINVAL", "ENOTSUP", "EBADF"].includes(
87
+ error?.code,
88
+ )
89
+ ) {
90
+ throw error;
91
+ }
92
+ } finally {
93
+ await handle?.close();
94
+ }
95
+ }
package/src/index.mjs CHANGED
@@ -3,18 +3,32 @@ export {
3
3
  LocalWorkspaceSession,
4
4
  reconcilePackageViewState,
5
5
  } from "./local-workspace-session.mjs";
6
- export { openWorkspace, resolveWorkspace } from "./workspace.mjs";
6
+ export {
7
+ localLibraryLocator,
8
+ openWorkspace,
9
+ resolveWorkspace,
10
+ } from "./workspace.mjs";
7
11
  export {
8
12
  defaultLibraryCacheRoot,
9
13
  openRemoteLibrary,
10
14
  } from "./remote-library.mjs";
11
- export { createEvidence, renderProjection } from "./render.mjs";
15
+ export {
16
+ createEvidence,
17
+ measureProjectionText,
18
+ renderProjection,
19
+ } from "./render.mjs";
20
+ export { renderAppProjection } from "./render-shared.mjs";
12
21
  export {
13
22
  decodeFigmaClipboard,
14
23
  importDraft,
15
24
  writeDraftPackage,
16
25
  } from "./draft-import.mjs";
17
- export { createBlankPackage, initializeWorkspace } from "./initialize.mjs";
26
+ export {
27
+ createBlankPackage,
28
+ initializeBlankWorkspace,
29
+ initializeWorkspace,
30
+ } from "./initialize.mjs";
31
+ export { migrateThemesByCopy } from "./migrate-themes.mjs";
18
32
  export {
19
33
  applyOperationBatch,
20
34
  deleteFontFamily,
@@ -22,12 +36,14 @@ export {
22
36
  importFontVariant,
23
37
  importMedia,
24
38
  openPackage,
39
+ readBatchHistory,
25
40
  removeMedia,
26
41
  replayRecordedBatch,
27
42
  updateFontFamily,
28
43
  } from "./local-package.mjs";
29
44
  export { detectMediaType, inspectMedia } from "./media-inspect.mjs";
30
45
  export {
46
+ fontVariantName,
31
47
  inspectFont,
32
48
  prepareFontFiles,
33
49
  sfntToWoff,