@meonode/canvas 1.7.1 → 1.7.2

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 (32) hide show
  1. package/dist/cjs/canvas/image.canvas.util.d.ts +7 -3
  2. package/dist/cjs/canvas/image.canvas.util.d.ts.map +1 -1
  3. package/dist/cjs/canvas/image.canvas.util.js +54 -26
  4. package/dist/cjs/canvas/image.canvas.util.js.map +1 -1
  5. package/dist/cjs/canvas/root.canvas.util.js +1 -1
  6. package/dist/cjs/canvas/root.canvas.util.js.map +1 -1
  7. package/dist/cjs/util/disk.cache.d.ts +4 -0
  8. package/dist/cjs/util/disk.cache.d.ts.map +1 -0
  9. package/dist/cjs/util/disk.cache.js +40 -0
  10. package/dist/cjs/util/disk.cache.js.map +1 -0
  11. package/dist/cjs/worker/render.worker.d.ts.map +1 -0
  12. package/dist/cjs/{render.worker.js → worker/render.worker.js} +1 -1
  13. package/dist/cjs/worker/render.worker.js.map +1 -0
  14. package/dist/{esm/canvas → cjs/worker}/worker.types.d.ts.map +1 -1
  15. package/dist/esm/canvas/image.canvas.util.d.ts +7 -3
  16. package/dist/esm/canvas/image.canvas.util.d.ts.map +1 -1
  17. package/dist/esm/canvas/image.canvas.util.js +54 -26
  18. package/dist/esm/canvas/root.canvas.util.js +1 -1
  19. package/dist/esm/util/disk.cache.d.ts +4 -0
  20. package/dist/esm/util/disk.cache.d.ts.map +1 -0
  21. package/dist/esm/util/disk.cache.js +35 -0
  22. package/dist/esm/worker/render.worker.d.ts.map +1 -0
  23. package/dist/esm/{render.worker.js → worker/render.worker.js} +1 -1
  24. package/dist/{cjs/canvas → esm/worker}/worker.types.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/dist/cjs/render.worker.d.ts.map +0 -1
  27. package/dist/cjs/render.worker.js.map +0 -1
  28. package/dist/esm/render.worker.d.ts.map +0 -1
  29. /package/dist/cjs/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
  30. /package/dist/cjs/{canvas → worker}/worker.types.d.ts +0 -0
  31. /package/dist/esm/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
  32. /package/dist/esm/{canvas → worker}/worker.types.d.ts +0 -0
@@ -51,6 +51,9 @@ export declare class ImageNode extends BoxNode {
51
51
  /**
52
52
  * Fetches and processes the image source into a CanvasImage.
53
53
  * Does not touch node state — pure fetch logic.
54
+ *
55
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
56
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
54
57
  */
55
58
  private _fetchCanvasImage;
56
59
  /**
@@ -58,10 +61,11 @@ export declare class ImageNode extends BoxNode {
58
61
  *
59
62
  * Resolution order:
60
63
  * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
61
- * 2. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
62
- * 3. Fresh fetch via `_fetchCanvasImage()`.
64
+ * 2. Disk cache at `.cache/files/<hash>`survives process restarts.
65
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
66
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
63
67
  *
64
- * Resolved images are stored back into the LRU cache for future renders.
68
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
65
69
  */
66
70
  private _loadImage;
67
71
  getLoadingPromise(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"image.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/image.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACnF,OAAO,EAAE,KAAK,wBAAwB,EAAE,KAAK,IAAI,WAAW,EAAa,MAAM,aAAa,CAAA;AAC5F,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAsBxD;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,GAAG,CAAC,MAAM,EAAE,OAAO,C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GAAG,OAAO,CAAC,IAAI,CAAC;IAOpD;;;;;;OAMG;YACW,iBAAiB;IAyE/B;;;;;;;;;;OAUG;IACH,OAAO,CAAC,UAAU;IA2FX,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIzC;;;OAGG;cACgB,cAAc,CAAC,GAAG,EAAE,wBAAwB,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAoJrH;AAED;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,UAAU,KAAG,aAGxC,CAAA"}
@@ -5,6 +5,7 @@ var layout_canvas_util = require('./layout.canvas.util.js');
5
5
  var canvas_helper = require('./canvas.helper.js');
6
6
  var fs = require('fs');
7
7
  var common_const = require('../constant/common.const.js');
8
+ var disk_cache = require('../util/disk.cache.js');
8
9
 
9
10
  /**
10
11
  * Calculates pixel offset for image positioning based on percentage or pixel values.
@@ -130,8 +131,11 @@ class ImageNode extends layout_canvas_util.BoxNode {
130
131
  /**
131
132
  * Fetches and processes the image source into a CanvasImage.
132
133
  * Does not touch node state — pure fetch logic.
134
+ *
135
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
136
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
133
137
  */
134
- async _fetchCanvasImage() {
138
+ async _fetchCanvasImage(diskCacheKey) {
135
139
  const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type');
136
140
  let finalSource = this.props.src;
137
141
  let isSvg = false;
@@ -190,6 +194,12 @@ class ImageNode extends layout_canvas_util.BoxNode {
190
194
  const modifiedSvgString = svgString.replace(/fill="[^"]*"/g, `fill="${this.props.color}"`);
191
195
  finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer;
192
196
  }
197
+ // Write resolved buffer to disk cache (fire-and-forget, non-fatal)
198
+ if (diskCacheKey) {
199
+ const cacheBuffer = Buffer.isBuffer(finalSource) ? finalSource : contentBuffer;
200
+ if (cacheBuffer)
201
+ disk_cache.writeDiskCache(diskCacheKey, cacheBuffer);
202
+ }
193
203
  return skiaCanvas.loadImage(finalSource);
194
204
  }
195
205
  /**
@@ -197,10 +207,11 @@ class ImageNode extends layout_canvas_util.BoxNode {
197
207
  *
198
208
  * Resolution order:
199
209
  * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
200
- * 2. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
201
- * 3. Fresh fetch via `_fetchCanvasImage()`.
210
+ * 2. Disk cache at `.cache/files/<hash>`survives process restarts.
211
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
212
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
202
213
  *
203
- * Resolved images are stored back into the LRU cache for future renders.
214
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
204
215
  */
205
216
  _loadImage(cache) {
206
217
  if (!this.props.src) {
@@ -214,38 +225,55 @@ class ImageNode extends layout_canvas_util.BoxNode {
214
225
  const load = async () => {
215
226
  try {
216
227
  const lru = getImageCache();
217
- const cacheKey = typeof this.props.src === 'string' ? (this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src) : null;
228
+ const cacheKey = typeof this.props.src === 'string'
229
+ ? this.props.color
230
+ ? `${this.props.src}|${this.props.color}`
231
+ : this.props.src
232
+ : this.props.color
233
+ ? `${disk_cache.hashBuffer(this.props.src)}|${this.props.color}`
234
+ : disk_cache.hashBuffer(this.props.src);
218
235
  // 1. Check persistent LRU cache
219
- if (cacheKey) {
220
- const cached = lru.get(cacheKey);
221
- if (cached) {
222
- this.loadedImage = cached;
223
- this.naturalWidth = cached.width;
224
- this.naturalHeight = cached.height;
225
- const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined;
226
- const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
227
- this.node.setAspectRatio(finalAspectRatio);
228
- this.props.onLoad?.();
229
- resolve();
230
- return;
231
- }
236
+ const cached = lru.get(cacheKey);
237
+ if (cached) {
238
+ this.loadedImage = cached;
239
+ this.naturalWidth = cached.width;
240
+ this.naturalHeight = cached.height;
241
+ const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined;
242
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
243
+ this.node.setAspectRatio(finalAspectRatio);
244
+ this.props.onLoad?.();
245
+ resolve();
246
+ return;
232
247
  }
233
- // 2. Per-render dedup cache or fresh fetch
248
+ // 2. Check disk cache (persists across process restarts)
249
+ const diskBuffer = await disk_cache.readDiskCache(cacheKey);
250
+ if (diskBuffer) {
251
+ const img = await skiaCanvas.loadImage(diskBuffer);
252
+ lru.set(cacheKey, img);
253
+ this.loadedImage = img;
254
+ this.naturalWidth = img.width;
255
+ this.naturalHeight = img.height;
256
+ const calculatedAspectRatio = img.width > 0 && img.height > 0 ? img.width / img.height : undefined;
257
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
258
+ this.node.setAspectRatio(finalAspectRatio);
259
+ this.props.onLoad?.();
260
+ resolve();
261
+ return;
262
+ }
263
+ // 3. Per-render dedup cache or fresh fetch (writes to disk internally)
234
264
  let imagePromise;
235
- if (cache && cacheKey) {
265
+ if (cache) {
236
266
  if (!cache.has(cacheKey)) {
237
- cache.set(cacheKey, this._fetchCanvasImage());
267
+ cache.set(cacheKey, this._fetchCanvasImage(cacheKey));
238
268
  }
239
269
  imagePromise = cache.get(cacheKey);
240
270
  }
241
271
  else {
242
- imagePromise = this._fetchCanvasImage();
272
+ imagePromise = this._fetchCanvasImage(cacheKey);
243
273
  }
244
274
  const img = await imagePromise;
245
- // 3. Store in persistent LRU cache
246
- if (cacheKey) {
247
- lru.set(cacheKey, img);
248
- }
275
+ // 4. Store in persistent LRU cache
276
+ lru.set(cacheKey, img);
249
277
  this.loadedImage = img;
250
278
  this.naturalWidth = img.width;
251
279
  this.naturalHeight = img.height;
@@ -1 +1 @@
1
- {"version":3,"file":"image.canvas.util.js","sources":["../../../../src/canvas/image.canvas.util.ts"],"sourcesContent":["import type { BaseProps, ImageProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport { type CanvasRenderingContext2D, Image as CanvasImage, loadImage } from 'skia-canvas'\nimport { BoxNode } from '@/canvas/layout.canvas.util.js'\nimport { drawRoundedRectPath, parseBorderRadius } from '@/canvas/canvas.helper.js'\nimport { promises as fs } from 'fs'\nimport { Style } from '@/constant/common.const.js'\n\n/**\n * Calculates pixel offset for image positioning based on percentage or pixel values.\n * This handles centering, edge alignment, and percentage-based positioning.\n */\nfunction calculateOffsetFromValue(positionValue: number | `${number}%` | undefined, availableSpace: number): number {\n const value = positionValue ?? '50%'\n if (typeof value === 'number') {\n return value\n }\n if (typeof value === 'string' && value.endsWith('%')) {\n const percentage = parseFloat(value) / 100\n return availableSpace * percentage\n }\n console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`)\n return availableSpace * 0.5\n}\n\n/**\n * Per-render image cache — keyed by `src|color` for string sources.\n * Scoped to a single RootNode.render() call; discarded after rendering.\n */\nexport type RenderImageCache = Map<string, Promise<CanvasImage>>\n\n// ---------------------------------------------------------------------------\n// Persistent LRU image cache — survives across render passes\n// ---------------------------------------------------------------------------\n\ninterface LRUEntry {\n image: CanvasImage\n key: string\n}\n\n/**\n * A simple LRU cache for resolved `CanvasImage` objects.\n *\n * - Persists across render passes so repeated renders of the same tree don't\n * re-fetch every image.\n * - Bounded by `maxSize` entries; least-recently-used entries are evicted first.\n * - Call `dispose()` to eagerly release all held images, or rely on the\n * automatic `process.on('exit')` hook that clears the singleton.\n */\nexport class ImageLRUCache {\n private readonly map = new Map<string, LRUEntry>()\n readonly maxSize: number\n\n constructor(maxSize: number) {\n this.maxSize = maxSize\n }\n\n get(key: string): CanvasImage | undefined {\n const entry = this.map.get(key)\n if (!entry) return undefined\n // Move to end (most-recently used)\n this.map.delete(key)\n this.map.set(key, entry)\n return entry.image\n }\n\n set(key: string, image: CanvasImage): void {\n // If key already exists, refresh it\n if (this.map.has(key)) {\n this.map.delete(key)\n }\n // Evict oldest if at capacity\n while (this.map.size >= this.maxSize) {\n const oldest = this.map.keys().next().value!\n this.map.delete(oldest)\n }\n this.map.set(key, { image, key })\n }\n\n has(key: string): boolean {\n return this.map.has(key)\n }\n\n get size(): number {\n return this.map.size\n }\n\n dispose(): void {\n this.map.clear()\n }\n}\n\n/** Module-level singleton — lazily created on first render. */\nlet _globalImageCache: ImageLRUCache | null = null\n\nconst DEFAULT_CACHE_SIZE = 128\n\n// Symbol key on process to track hook registration across module reloads (e.g. Jest resetModules)\nconst HOOK_KEY = Symbol.for('__meonode_canvas_image_cache_hook__')\n\n/**\n * Returns the singleton `ImageLRUCache`, creating it on first access.\n * Registers a one-time process cleanup hook to clear the cache\n * so native image buffers are freed when the process shuts down.\n */\nexport function getImageCache(maxSize: number = DEFAULT_CACHE_SIZE): ImageLRUCache {\n if (!_globalImageCache) {\n _globalImageCache = new ImageLRUCache(maxSize)\n }\n if (!(globalThis as any)[HOOK_KEY]) {\n ;(globalThis as any)[HOOK_KEY] = true\n const cleanup = () => {\n _globalImageCache?.dispose()\n _globalImageCache = null\n }\n process.once('exit', cleanup)\n process.once('SIGINT', cleanup)\n process.once('SIGTERM', cleanup)\n }\n return _globalImageCache\n}\n\n/**\n * Explicitly disposes the global image cache.\n * Useful in tests or when tearing down the rendering engine.\n */\nexport function disposeImageCache(): void {\n _globalImageCache?.dispose()\n _globalImageCache = null\n}\n\n/**\n * Renders images with configurable sizing, positioning, and effects.\n * Supports object-fit modes, positioning, border radius, and saturation filters.\n */\nexport class ImageNode extends BoxNode {\n declare props: ImageProps & BaseProps\n private loadedImage: CanvasImage | null = null\n private naturalWidth = 0\n private naturalHeight = 0\n private loadingPromise: Promise<void> | null = null\n\n constructor(props: ImageProps) {\n super({ name: 'Image', ...props, children: undefined })\n\n this.props = {\n objectFit: 'fill',\n overflow: Style.Overflow.Hidden,\n saturate: 1,\n objectPosition: { Left: '50%', Top: '50%' },\n ...props,\n }\n }\n\n public load(cache?: RenderImageCache): Promise<void> {\n if (!this.loadingPromise) {\n this.loadingPromise = this._loadImage(cache)\n }\n return this.loadingPromise\n }\n\n /**\n * Fetches and processes the image source into a CanvasImage.\n * Does not touch node state — pure fetch logic.\n */\n private async _fetchCanvasImage(): Promise<CanvasImage> {\n const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type')\n let finalSource: string | Buffer = this.props.src\n let isSvg = false\n let contentBuffer: Buffer | null = null\n let detectedMime: string | undefined\n\n if (typeof this.props.src === 'string') {\n if (this.props.src.startsWith('http')) {\n const response = await fetch(this.props.src)\n if (!response.ok) {\n throw new Error(`HTTP error ${response.status} fetching image: ${this.props.src}`)\n }\n const imageArrayBuffer = await response.arrayBuffer()\n contentBuffer = Buffer.from(imageArrayBuffer)\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && contentBuffer.toString('utf-8').includes('<svg')) {\n isSvg = true\n }\n } else {\n finalSource = this.props.src\n const filePath = this.props.src\n\n try {\n const fileTypeResult = await fileTypeFromFile(filePath)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && filePath.toLowerCase().endsWith('.svg')) {\n isSvg = true\n }\n } catch {\n isSvg = filePath.toLowerCase().endsWith('.svg')\n }\n\n if (isSvg && this.props.color) {\n try {\n contentBuffer = await fs.readFile(filePath)\n } catch {\n isSvg = false\n contentBuffer = null\n }\n }\n }\n } else {\n contentBuffer = this.props.src\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n }\n\n if (isSvg && this.props.color && contentBuffer) {\n const svgString = contentBuffer.toString('utf-8')\n const modifiedSvgString = svgString.replace(/fill=\"[^\"]*\"/g, `fill=\"${this.props.color}\"`)\n finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer\n }\n\n return loadImage(finalSource as never)\n }\n\n /**\n * Loads and processes an image.\n *\n * Resolution order:\n * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.\n * 2. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.\n * 3. Fresh fetch via `_fetchCanvasImage()`.\n *\n * Resolved images are stored back into the LRU cache for future renders.\n */\n private _loadImage(cache?: RenderImageCache): Promise<void> {\n if (!this.props.src) {\n const aspectRatioFromProps = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(aspectRatioFromProps)\n this.naturalWidth = 0\n this.naturalHeight = 0\n return Promise.resolve()\n }\n\n return new Promise(resolve => {\n const load = async () => {\n try {\n const lru = getImageCache()\n const cacheKey = typeof this.props.src === 'string' ? (this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src) : null\n\n // 1. Check persistent LRU cache\n if (cacheKey) {\n const cached = lru.get(cacheKey)\n if (cached) {\n this.loadedImage = cached\n this.naturalWidth = cached.width\n this.naturalHeight = cached.height\n const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n return\n }\n }\n\n // 2. Per-render dedup cache or fresh fetch\n let imagePromise: Promise<CanvasImage>\n if (cache && cacheKey) {\n if (!cache.has(cacheKey)) {\n cache.set(cacheKey, this._fetchCanvasImage())\n }\n imagePromise = cache.get(cacheKey)!\n } else {\n imagePromise = this._fetchCanvasImage()\n }\n\n const img = await imagePromise\n\n // 3. Store in persistent LRU cache\n if (cacheKey) {\n lru.set(cacheKey, img)\n }\n\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n\n const calculatedAspectRatio = this.naturalWidth > 0 && this.naturalHeight > 0 ? this.naturalWidth / this.naturalHeight : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n } catch (error: any) {\n this.naturalWidth = 0\n this.naturalHeight = 0\n const finalAspectRatioOnError = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(finalAspectRatioOnError)\n this.props.onError?.(error)\n resolve()\n }\n }\n load()\n })\n }\n\n public getLoadingPromise(): Promise<void> {\n return this.loadingPromise ?? this.load()\n }\n\n /**\n * Renders the image with correct sizing, clipping, and positioning.\n * Handles object-fit, object-position, and visual effects like saturation.\n */\n protected override _renderContent(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number) {\n super._renderContent(ctx, x, y, width, height)\n\n if (!this.loadedImage || width <= 0 || height <= 0) return\n const img = this.loadedImage\n const imgW = this.naturalWidth\n const imgH = this.naturalHeight\n if (imgW <= 0 || imgH <= 0) return\n\n // Calculate content box accounting for padding and borders\n const paddingLeft = this.node.getComputedPadding(Style.Edge.Left)\n const paddingTop = this.node.getComputedPadding(Style.Edge.Top)\n const paddingRight = this.node.getComputedPadding(Style.Edge.Right)\n const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom)\n const borderLeft = this.node.getComputedBorder(Style.Edge.Left)\n const borderTop = this.node.getComputedBorder(Style.Edge.Top)\n const borderRight = this.node.getComputedBorder(Style.Edge.Right)\n const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom)\n const contentX = x + borderLeft + paddingLeft\n const contentY = y + borderTop + paddingTop\n const contentWidth = Math.max(0, width - borderLeft - paddingLeft - borderRight - paddingRight)\n const contentHeight = Math.max(0, height - borderTop - paddingTop - borderBottom - paddingBottom)\n\n if (contentWidth <= 0 || contentHeight <= 0) return\n\n // Apply clipping for border radius\n ctx.save()\n const outerRadii = parseBorderRadius(this.props.borderRadius)\n const innerBorderRadii = {\n TopLeft: Math.max(0, outerRadii.TopLeft - borderTop),\n TopRight: Math.max(0, outerRadii.TopRight - borderTop),\n BottomRight: Math.max(0, outerRadii.BottomRight - borderBottom),\n BottomLeft: Math.max(0, outerRadii.BottomLeft - borderBottom),\n }\n const contentRadii = {\n TopLeft: Math.max(0, innerBorderRadii.TopLeft - Math.max(paddingLeft, paddingTop)),\n TopRight: Math.max(0, innerBorderRadii.TopRight - Math.max(paddingRight, paddingTop)),\n BottomRight: Math.max(0, innerBorderRadii.BottomRight - Math.max(paddingRight, paddingBottom)),\n BottomLeft: Math.max(0, innerBorderRadii.BottomLeft - Math.max(paddingLeft, paddingBottom)),\n }\n drawRoundedRectPath(ctx, contentX, contentY, contentWidth, contentHeight, contentRadii)\n ctx.clip()\n\n // Calculate image dimensions based on object-fit\n const nodeRatio = contentWidth / contentHeight\n const imgRatio = imgW / imgH\n const objectFit = this.props.objectFit\n let dw = contentWidth\n let dh = contentHeight\n\n if (objectFit === 'contain') {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n } else if (objectFit === 'cover') {\n if (imgRatio > nodeRatio) {\n dh = contentHeight\n dw = contentHeight * imgRatio\n } else {\n dw = contentWidth\n dh = contentWidth / imgRatio\n }\n } else if (objectFit === 'none') {\n dw = imgW\n dh = imgH\n } else if (objectFit === 'scale-down') {\n if (imgW <= contentWidth && imgH <= contentHeight) {\n dw = imgW\n dh = imgH\n } else {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n }\n }\n\n // Calculate image position based on object-position\n const sx = 0\n const sy = 0\n const sw = imgW\n const sh = imgH\n\n const availableWidth = contentWidth - dw\n const availableHeight = contentHeight - dh\n const posProps = this.props.objectPosition || {}\n const horizontalValue = posProps.Left !== undefined ? posProps.Left : posProps.Right !== undefined ? posProps.Right : '50%'\n const verticalValue = posProps.Top !== undefined ? posProps.Top : posProps.Bottom !== undefined ? posProps.Bottom : '50%'\n\n let offsetX = calculateOffsetFromValue(horizontalValue, availableWidth)\n let offsetY = calculateOffsetFromValue(verticalValue, availableHeight)\n\n if (posProps.Left === undefined && posProps.Right !== undefined) {\n offsetX = availableWidth - offsetX\n }\n if (posProps.Top === undefined && posProps.Bottom !== undefined) {\n offsetY = availableHeight - offsetY\n }\n\n const dx = contentX + offsetX\n const dy = contentY + offsetY\n\n // Draw image with filters\n ctx.save()\n try {\n if (this.props.dropShadow) {\n const shadow = this.props.dropShadow\n const shadowBlur = Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0)\n ctx.shadowOffsetX = shadow.offsetX ?? 0\n ctx.shadowOffsetY = shadow.offsetY ?? 0\n ctx.shadowBlur = Math.max(0, shadow.blur ?? shadowBlur)\n ctx.shadowColor = shadow.color ?? 'black'\n }\n\n const saturateValue = this.props.saturate ?? 1\n let filterString = ''\n if (saturateValue !== 1) {\n filterString += `saturate(${saturateValue * 100}%) `\n }\n\n if (filterString) {\n const currentFilter = ctx.filter && ctx.filter !== 'none' ? ctx.filter + ' ' : ''\n ctx.filter = currentFilter + filterString.trim()\n }\n\n const finalDX = Math.floor(dx)\n const finalDY = Math.floor(dy)\n const finalDW = Math.ceil(dw + (dx - finalDX))\n const finalDH = Math.ceil(dh + (dy - finalDY))\n\n if (finalDW > 0 && finalDH > 0) {\n ctx.drawImage(img, sx, sy, sw, sh, finalDX, finalDY, finalDW, finalDH)\n }\n } catch (drawError) {\n console.error('[ImageNode] Error drawing image:', drawError)\n } finally {\n ctx.restore()\n }\n\n ctx.restore()\n }\n}\n\n/**\n * Factory function to create ImageNode instances\n */\nexport const Image = (props: ImageProps): CanvasElement => ({\n __type: 'Image',\n props: props as Omit<ImageProps, 'onLoad' | 'onError'>,\n})\n"],"names":["BoxNode","Style","fs","loadImage","parseBorderRadius","drawRoundedRectPath"],"mappings":";;;;;;;;AAOA;;;AAGG;AACH,SAAS,wBAAwB,CAAC,aAAgD,EAAE,cAAsB,EAAA;AACxG,IAAA,MAAM,KAAK,GAAG,aAAa,IAAI,KAAK;AACpC,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,OAAO,KAAK;IACd;AACA,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACpD,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,GAAG;QAC1C,OAAO,cAAc,GAAG,UAAU;IACpC;AACA,IAAA,OAAO,CAAC,IAAI,CAAC,oDAAoD,KAAK,CAAA,oBAAA,CAAsB,CAAC;IAC7F,OAAO,cAAc,GAAG,GAAG;AAC7B;AAiBA;;;;;;;;AAQG;MACU,aAAa,CAAA;AACP,IAAA,GAAG,GAAG,IAAI,GAAG,EAAoB;AACzC,IAAA,OAAO;AAEhB,IAAA,WAAA,CAAY,OAAe,EAAA;AACzB,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;IACxB;AAEA,IAAA,GAAG,CAAC,GAAW,EAAA;QACb,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;AAC/B,QAAA,IAAI,CAAC,KAAK;AAAE,YAAA,OAAO,SAAS;;AAE5B,QAAA,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC;QACpB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC;QACxB,OAAO,KAAK,CAAC,KAAK;I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,EAAE;gBACjF,GAAG,CAAC,MAAM,GAAG,aAAa,GAAG,YAAY,CAAC,IAAI,EAAE;YAClD;YAEA,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9B,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;AAC9B,YAAA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,CAAC;AAC9C,YAAA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,CAAC;YAE9C,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC,EAAE;gBAC9B,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC;YACxE;QACF;QAAE,OAAO,SAAS,EAAE;AAClB,YAAA,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,SAAS,CAAC;QAC9D;gBAAU;YACR,GAAG,CAAC,OAAO,EAAE;QACf;QAEA,GAAG,CAAC,OAAO,EAAE;IACf;AACD;AAED;;AAEG;MACU,KAAK,GAAG,CAAC,KAAiB,MAAqB;AAC1D,IAAA,MAAM,EAAE,OAAO;AACf,IAAA,KAAK,EAAE,KAA+C;AACvD,CAAA;;;;;;;;"}
1
+ {"version":3,"file":"image.canvas.util.js","sources":["../../../../src/canvas/image.canvas.util.ts"],"sourcesContent":["import type { BaseProps, ImageProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport { type CanvasRenderingContext2D, Image as CanvasImage, loadImage } from 'skia-canvas'\nimport { BoxNode } from '@/canvas/layout.canvas.util.js'\nimport { drawRoundedRectPath, parseBorderRadius } from '@/canvas/canvas.helper.js'\nimport { promises as fs } from 'fs'\nimport { Style } from '@/constant/common.const.js'\nimport { hashBuffer, readDiskCache, writeDiskCache } from '@/util/disk.cache.js'\n\n/**\n * Calculates pixel offset for image positioning based on percentage or pixel values.\n * This handles centering, edge alignment, and percentage-based positioning.\n */\nfunction calculateOffsetFromValue(positionValue: number | `${number}%` | undefined, availableSpace: number): number {\n const value = positionValue ?? '50%'\n if (typeof value === 'number') {\n return value\n }\n if (typeof value === 'string' && value.endsWith('%')) {\n const percentage = parseFloat(value) / 100\n return availableSpace * percentage\n }\n console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`)\n return availableSpace * 0.5\n}\n\n/**\n * Per-render image cache — keyed by `src|color` for string sources.\n * Scoped to a single RootNode.render() call; discarded after rendering.\n */\nexport type RenderImageCache = Map<string, Promise<CanvasImage>>\n\n// ---------------------------------------------------------------------------\n// Persistent LRU image cache — survives across render passes\n// ---------------------------------------------------------------------------\n\ninterface LRUEntry {\n image: CanvasImage\n key: string\n}\n\n/**\n * A simple LRU cache for resolved `CanvasImage` objects.\n *\n * - Persists across render passes so repeated renders of the same tree don't\n * re-fetch every image.\n * - Bounded by `maxSize` entries; least-recently-used entries are evicted first.\n * - Call `dispose()` to eagerly release all held images, or rely on the\n * automatic `process.on('exit')` hook that clears the singleton.\n */\nexport class ImageLRUCache {\n private readonly map = new Map<string, LRUEntry>()\n readonly maxSize: number\n\n constructor(maxSize: number) {\n this.maxSize = maxSize\n }\n\n get(key: string): CanvasImage | undefined {\n const entry = this.map.get(key)\n if (!entry) return undefined\n // Move to end (most-recently used)\n this.map.delete(key)\n this.map.set(key, entry)\n return entry.image\n }\n\n set(key: string, image: CanvasImage): void {\n // If key already exists, refresh it\n if (this.map.has(key)) {\n this.map.delete(key)\n }\n // Evict oldest if at capacity\n while (this.map.size >= this.maxSize) {\n const oldest = this.map.keys().next().value!\n this.map.delete(oldest)\n }\n this.map.set(key, { image, key })\n }\n\n has(key: string): boolean {\n return this.map.has(key)\n }\n\n get size(): number {\n return this.map.size\n }\n\n dispose(): void {\n this.map.clear()\n }\n}\n\n/** Module-level singleton — lazily created on first render. */\nlet _globalImageCache: ImageLRUCache | null = null\n\nconst DEFAULT_CACHE_SIZE = 128\n\n// Symbol key on process to track hook registration across module reloads (e.g. Jest resetModules)\nconst HOOK_KEY = Symbol.for('__meonode_canvas_image_cache_hook__')\n\n/**\n * Returns the singleton `ImageLRUCache`, creating it on first access.\n * Registers a one-time process cleanup hook to clear the cache\n * so native image buffers are freed when the process shuts down.\n */\nexport function getImageCache(maxSize: number = DEFAULT_CACHE_SIZE): ImageLRUCache {\n if (!_globalImageCache) {\n _globalImageCache = new ImageLRUCache(maxSize)\n }\n if (!(globalThis as any)[HOOK_KEY]) {\n ;(globalThis as any)[HOOK_KEY] = true\n const cleanup = () => {\n _globalImageCache?.dispose()\n _globalImageCache = null\n }\n process.once('exit', cleanup)\n process.once('SIGINT', cleanup)\n process.once('SIGTERM', cleanup)\n }\n return _globalImageCache\n}\n\n/**\n * Explicitly disposes the global image cache.\n * Useful in tests or when tearing down the rendering engine.\n */\nexport function disposeImageCache(): void {\n _globalImageCache?.dispose()\n _globalImageCache = null\n}\n\n/**\n * Renders images with configurable sizing, positioning, and effects.\n * Supports object-fit modes, positioning, border radius, and saturation filters.\n */\nexport class ImageNode extends BoxNode {\n declare props: ImageProps & BaseProps\n private loadedImage: CanvasImage | null = null\n private naturalWidth = 0\n private naturalHeight = 0\n private loadingPromise: Promise<void> | null = null\n\n constructor(props: ImageProps) {\n super({ name: 'Image', ...props, children: undefined })\n\n this.props = {\n objectFit: 'fill',\n overflow: Style.Overflow.Hidden,\n saturate: 1,\n objectPosition: { Left: '50%', Top: '50%' },\n ...props,\n }\n }\n\n public load(cache?: RenderImageCache): Promise<void> {\n if (!this.loadingPromise) {\n this.loadingPromise = this._loadImage(cache)\n }\n return this.loadingPromise\n }\n\n /**\n * Fetches and processes the image source into a CanvasImage.\n * Does not touch node state — pure fetch logic.\n *\n * If `diskCacheKey` is provided, the resolved image buffer is written to the\n * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).\n */\n private async _fetchCanvasImage(diskCacheKey?: string): Promise<CanvasImage> {\n const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type')\n let finalSource: string | Buffer = this.props.src\n let isSvg = false\n let contentBuffer: Buffer | null = null\n let detectedMime: string | undefined\n\n if (typeof this.props.src === 'string') {\n if (this.props.src.startsWith('http')) {\n const response = await fetch(this.props.src)\n if (!response.ok) {\n throw new Error(`HTTP error ${response.status} fetching image: ${this.props.src}`)\n }\n const imageArrayBuffer = await response.arrayBuffer()\n contentBuffer = Buffer.from(imageArrayBuffer)\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && contentBuffer.toString('utf-8').includes('<svg')) {\n isSvg = true\n }\n } else {\n finalSource = this.props.src\n const filePath = this.props.src\n\n try {\n const fileTypeResult = await fileTypeFromFile(filePath)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && filePath.toLowerCase().endsWith('.svg')) {\n isSvg = true\n }\n } catch {\n isSvg = filePath.toLowerCase().endsWith('.svg')\n }\n\n if (isSvg && this.props.color) {\n try {\n contentBuffer = await fs.readFile(filePath)\n } catch {\n isSvg = false\n contentBuffer = null\n }\n }\n }\n } else {\n contentBuffer = this.props.src\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n }\n\n if (isSvg && this.props.color && contentBuffer) {\n const svgString = contentBuffer.toString('utf-8')\n const modifiedSvgString = svgString.replace(/fill=\"[^\"]*\"/g, `fill=\"${this.props.color}\"`)\n finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer\n }\n\n // Write resolved buffer to disk cache (fire-and-forget, non-fatal)\n if (diskCacheKey) {\n const cacheBuffer = Buffer.isBuffer(finalSource) ? finalSource : contentBuffer\n if (cacheBuffer) writeDiskCache(diskCacheKey, cacheBuffer)\n }\n\n return loadImage(finalSource as never)\n }\n\n /**\n * Loads and processes an image.\n *\n * Resolution order:\n * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.\n * 2. Disk cache at `.cache/files/<hash>` — survives process restarts.\n * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.\n * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.\n *\n * Buffer sources use a SHA-256 hash as their cache key (same as string sources).\n */\n private _loadImage(cache?: RenderImageCache): Promise<void> {\n if (!this.props.src) {\n const aspectRatioFromProps = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(aspectRatioFromProps)\n this.naturalWidth = 0\n this.naturalHeight = 0\n return Promise.resolve()\n }\n\n return new Promise(resolve => {\n const load = async () => {\n try {\n const lru = getImageCache()\n const cacheKey =\n typeof this.props.src === 'string'\n ? this.props.color\n ? `${this.props.src}|${this.props.color}`\n : this.props.src\n : this.props.color\n ? `${hashBuffer(this.props.src)}|${this.props.color}`\n : hashBuffer(this.props.src)\n\n // 1. Check persistent LRU cache\n const cached = lru.get(cacheKey)\n if (cached) {\n this.loadedImage = cached\n this.naturalWidth = cached.width\n this.naturalHeight = cached.height\n const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n return\n }\n\n // 2. Check disk cache (persists across process restarts)\n const diskBuffer = await readDiskCache(cacheKey)\n if (diskBuffer) {\n const img = await loadImage(diskBuffer as never)\n lru.set(cacheKey, img)\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n const calculatedAspectRatio = img.width > 0 && img.height > 0 ? img.width / img.height : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n return\n }\n\n // 3. Per-render dedup cache or fresh fetch (writes to disk internally)\n let imagePromise: Promise<CanvasImage>\n if (cache) {\n if (!cache.has(cacheKey)) {\n cache.set(cacheKey, this._fetchCanvasImage(cacheKey))\n }\n imagePromise = cache.get(cacheKey)!\n } else {\n imagePromise = this._fetchCanvasImage(cacheKey)\n }\n\n const img = await imagePromise\n\n // 4. Store in persistent LRU cache\n lru.set(cacheKey, img)\n\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n\n const calculatedAspectRatio = this.naturalWidth > 0 && this.naturalHeight > 0 ? this.naturalWidth / this.naturalHeight : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n } catch (error: any) {\n this.naturalWidth = 0\n this.naturalHeight = 0\n const finalAspectRatioOnError = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(finalAspectRatioOnError)\n this.props.onError?.(error)\n resolve()\n }\n }\n load()\n })\n }\n\n public getLoadingPromise(): Promise<void> {\n return this.loadingPromise ?? this.load()\n }\n\n /**\n * Renders the image with correct sizing, clipping, and positioning.\n * Handles object-fit, object-position, and visual effects like saturation.\n */\n protected override _renderContent(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number) {\n super._renderContent(ctx, x, y, width, height)\n\n if (!this.loadedImage || width <= 0 || height <= 0) return\n const img = this.loadedImage\n const imgW = this.naturalWidth\n const imgH = this.naturalHeight\n if (imgW <= 0 || imgH <= 0) return\n\n // Calculate content box accounting for padding and borders\n const paddingLeft = this.node.getComputedPadding(Style.Edge.Left)\n const paddingTop = this.node.getComputedPadding(Style.Edge.Top)\n const paddingRight = this.node.getComputedPadding(Style.Edge.Right)\n const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom)\n const borderLeft = this.node.getComputedBorder(Style.Edge.Left)\n const borderTop = this.node.getComputedBorder(Style.Edge.Top)\n const borderRight = this.node.getComputedBorder(Style.Edge.Right)\n const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom)\n const contentX = x + borderLeft + paddingLeft\n const contentY = y + borderTop + paddingTop\n const contentWidth = Math.max(0, width - borderLeft - paddingLeft - borderRight - paddingRight)\n const contentHeight = Math.max(0, height - borderTop - paddingTop - borderBottom - paddingBottom)\n\n if (contentWidth <= 0 || contentHeight <= 0) return\n\n // Apply clipping for border radius\n ctx.save()\n const outerRadii = parseBorderRadius(this.props.borderRadius)\n const innerBorderRadii = {\n TopLeft: Math.max(0, outerRadii.TopLeft - borderTop),\n TopRight: Math.max(0, outerRadii.TopRight - borderTop),\n BottomRight: Math.max(0, outerRadii.BottomRight - borderBottom),\n BottomLeft: Math.max(0, outerRadii.BottomLeft - borderBottom),\n }\n const contentRadii = {\n TopLeft: Math.max(0, innerBorderRadii.TopLeft - Math.max(paddingLeft, paddingTop)),\n TopRight: Math.max(0, innerBorderRadii.TopRight - Math.max(paddingRight, paddingTop)),\n BottomRight: Math.max(0, innerBorderRadii.BottomRight - Math.max(paddingRight, paddingBottom)),\n BottomLeft: Math.max(0, innerBorderRadii.BottomLeft - Math.max(paddingLeft, paddingBottom)),\n }\n drawRoundedRectPath(ctx, contentX, contentY, contentWidth, contentHeight, contentRadii)\n ctx.clip()\n\n // Calculate image dimensions based on object-fit\n const nodeRatio = contentWidth / contentHeight\n const imgRatio = imgW / imgH\n const objectFit = this.props.objectFit\n let dw = contentWidth\n let dh = contentHeight\n\n if (objectFit === 'contain') {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n } else if (objectFit === 'cover') {\n if (imgRatio > nodeRatio) {\n dh = contentHeight\n dw = contentHeight * imgRatio\n } else {\n dw = contentWidth\n dh = contentWidth / imgRatio\n }\n } else if (objectFit === 'none') {\n dw = imgW\n dh = imgH\n } else if (objectFit === 'scale-down') {\n if (imgW <= contentWidth && imgH <= contentHeight) {\n dw = imgW\n dh = imgH\n } else {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n }\n }\n\n // Calculate image position based on object-position\n const sx = 0\n const sy = 0\n const sw = imgW\n const sh = imgH\n\n const availableWidth = contentWidth - dw\n const availableHeight = contentHeight - dh\n const posProps = this.props.objectPosition || {}\n const horizontalValue = posProps.Left !== undefined ? posProps.Left : posProps.Right !== undefined ? posProps.Right : '50%'\n const verticalValue = posProps.Top !== undefined ? posProps.Top : posProps.Bottom !== undefined ? posProps.Bottom : '50%'\n\n let offsetX = calculateOffsetFromValue(horizontalValue, availableWidth)\n let offsetY = calculateOffsetFromValue(verticalValue, availableHeight)\n\n if (posProps.Left === undefined && posProps.Right !== undefined) {\n offsetX = availableWidth - offsetX\n }\n if (posProps.Top === undefined && posProps.Bottom !== undefined) {\n offsetY = availableHeight - offsetY\n }\n\n const dx = contentX + offsetX\n const dy = contentY + offsetY\n\n // Draw image with filters\n ctx.save()\n try {\n if (this.props.dropShadow) {\n const shadow = this.props.dropShadow\n const shadowBlur = Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0)\n ctx.shadowOffsetX = shadow.offsetX ?? 0\n ctx.shadowOffsetY = shadow.offsetY ?? 0\n ctx.shadowBlur = Math.max(0, shadow.blur ?? shadowBlur)\n ctx.shadowColor = shadow.color ?? 'black'\n }\n\n const saturateValue = this.props.saturate ?? 1\n let filterString = ''\n if (saturateValue !== 1) {\n filterString += `saturate(${saturateValue * 100}%) `\n }\n\n if (filterString) {\n const currentFilter = ctx.filter && ctx.filter !== 'none' ? ctx.filter + ' ' : ''\n ctx.filter = currentFilter + filterString.trim()\n }\n\n const finalDX = Math.floor(dx)\n const finalDY = Math.floor(dy)\n const finalDW = Math.ceil(dw + (dx - finalDX))\n const finalDH = Math.ceil(dh + (dy - finalDY))\n\n if (finalDW > 0 && finalDH > 0) {\n ctx.drawImage(img, sx, sy, sw, sh, finalDX, finalDY, finalDW, finalDH)\n }\n } catch (drawError) {\n console.error('[ImageNode] Error drawing image:', drawError)\n } finally {\n ctx.restore()\n }\n\n ctx.restore()\n }\n}\n\n/**\n * Factory function to create ImageNode instances\n */\nexport const Image = (props: ImageProps): CanvasElement => ({\n __type: 'Image',\n props: props as Omit<ImageProps, 'onLoad' | 'onError'>,\n})\n"],"names":["BoxNode","Style","fs","writeDiskCache","loadImage","hashBuffer","readDiskCache","parseBorderRadius","drawRoundedRectPath"],"mappings":";;;;;;;;;AAQA;;;AAGG;AACH,SAAS,wBAAwB,CAAC,aAAgD,EAAE,cAAsB,EAAA;AACxG,IAAA,MAAM,KAAK,GAAG,aAAa,IAAI,KAAK;AACpC,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,OAAO,KAAK;IACd;AACA,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACpD,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,GAAG;QAC1C,OAAO,cAAc,GAAG,UAAU;IACpC;AACA,IAAA,OAAO,CAAC,IAAI,CAAC,oDAAoD,KAAK,CAAA,oBAAA,CAAsB,CAAC;IAC7F,OAAO,cAAc,GAAG,GAAG;AAC7B;AAiBA;;;;;;;;AAQG;MACU,aAAa,CAAA;AACP,IAAA,GAAG,GAAG,IAAI,GAAG,EAAoB;AACzC,IAAA,OAAO;AAEhB,IAAA,WAAA,CAAY,OAAe,EAAA;AACzB,QAAA,IAAI,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ACvD,CAAA;;;;;;;;"}
@@ -141,7 +141,7 @@ class WorkerPool {
141
141
  this.init(size);
142
142
  }
143
143
  init(size) {
144
- const workerFile = path__namespace.join(path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('canvas/root.canvas.util.js', document.baseURI).href)))), '../render.worker.js');
144
+ const workerFile = path__namespace.join(path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('canvas/root.canvas.util.js', document.baseURI).href)))), '../worker/render.worker.js');
145
145
  for (let i = 0; i < size; i++) {
146
146
  const workerIdx = i;
147
147
  const worker = new node_worker_threads.Worker(workerFile);
@@ -1 +1 @@
1
- {"version":3,"file":"root.canvas.util.js","sources":["../../../../src/canvas/root.canvas.util.ts"],"sourcesContent":["import { Canvas, FontLibrary, type CanvasRenderingContext2D } from 'skia-canvas'\nimport type { ExportFormat, ExportOptions, SaveOptions, RenderOptions } from 'skia-canvas'\nimport { ColumnNode, BoxNode, RowNode } from '@/canvas/layout.canvas.util.js'\nimport type { BaseProps, RootProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport type { CanvasCallMethod, CallArgs, CallResult, WorkerCallRequest, WorkerResponse, WorkerRequest } from '@/canvas/worker.types.js'\nimport { ImageNode, type RenderImageCache, disposeImageCache, getImageCache } from '@/canvas/image.canvas.util.js'\nimport { TextNode } from '@/canvas/text.canvas.util.js'\nimport { ChartNode } from '@/canvas/chart.canvas.util.js'\nimport { GridNode, GridItemNode } from '@/canvas/grid.canvas.util.js'\nimport { Style } from '@/constant/common.const.js'\nimport { WorkerPreProcessor } from '@/canvas/canvas.helper.js'\nimport * as path from 'node:path'\nimport * as fs from 'node:fs'\nimport { cpus } from 'node:os'\nimport { Worker } from 'node:worker_threads'\nimport { fileURLToPath } from 'node:url'\n\n/** Registry to track fonts that have already been loaded */\nconst registeredFonts = new Map<string, Set<string>>()\n\n// Exported for testing purposes only\nexport const _clearRegisteredFonts = () => {\n registeredFonts.clear()\n}\n\n/** Engine configuration */\nlet _workerMode = true\nlet _workerPoolSize = Math.max(1, cpus().length - 1)\nlet _workerPool: WorkerPool | null = null\n\nexport interface CanvasEngineConfig {\n /** Run rendering in worker threads to avoid blocking the event loop (default: true) */\n workerMode?: boolean\n /** Number of worker threads in the pool (default: os.cpus().length - 1) */\n workers?: number\n /** Maximum number of resolved images to keep in the persistent LRU cache (default: 128) */\n imageCacheSize?: number\n}\n\n/**\n * Configure the canvas rendering engine.\n * Call this once at application startup before rendering.\n */\nexport function configure(options: CanvasEngineConfig) {\n if (options.workerMode !== undefined) _workerMode = options.workerMode\n if (options.workers !== undefined) _workerPoolSize = options.workers\n if (options.imageCacheSize !== undefined) {\n // Dispose existing cache and create a new one with the specified size\n disposeImageCache()\n getImageCache(options.imageCacheSize)\n }\n if (_workerMode) {\n _workerPool = new WorkerPool(_workerPoolSize)\n }\n}\n\ninterface PendingTask {\n resolve: (value: unknown) => void\n reject: (err: Error) => void\n}\n\ninterface PoolRenderResult {\n buffer: Buffer\n canvasId: number\n workerIdx: number\n width: number\n height: number\n}\n\n/**\n * Proxies all skia-canvas Canvas APIs to a Canvas instance living inside a worker thread.\n * Sync methods (toBufferSync, toURLSync) return from a pre-encoded PNG buffer.\n * Async methods (toBuffer, toURL, toFile, getters) delegate to the worker.\n */\nclass WorkerCanvas {\n readonly width: number\n readonly height: number\n private readonly _buffer: Buffer // pre-encoded PNG for sync use\n private readonly _pool: WorkerPool\n private readonly _workerIdx: number\n private readonly _canvasId: number\n\n constructor(opts: PoolRenderResult & { pool: WorkerPool }) {\n this._buffer = opts.buffer\n this.width = opts.width\n this.height = opts.height\n this._pool = opts.pool\n this._workerIdx = opts.workerIdx\n this._canvasId = opts.canvasId\n }\n\n private _call<M extends CanvasCallMethod>(method: M, ...args: CallArgs<M>): Promise<CallResult<M>> {\n return this._pool.callOnCanvas(this._workerIdx, this._canvasId, method, args)\n }\n\n // --- Sync methods: return from pre-encoded PNG buffer ---\n\n toBufferSync(_format?: ExportFormat, _options?: ExportOptions): Buffer {\n return this._buffer\n }\n\n toURLSync(_format?: ExportFormat, _options?: ExportOptions): string {\n return `data:image/png;base64,${this._buffer.toString('base64')}`\n }\n\n // --- Async methods: delegate to worker ---\n\n toBuffer(format: ExportFormat, options?: ExportOptions): Promise<Buffer> {\n return this._call('toBuffer', format, options)\n }\n\n toURL(format: ExportFormat, options?: ExportOptions): Promise<string> {\n return this._call('toURL', format, options)\n }\n\n toFile(filename: string, options?: SaveOptions): Promise<void> {\n return this._call('toFile', filename, options)\n }\n\n /** Returns a Buffer (Sharp instance cannot be transferred across threads) */\n toSharp(options?: RenderOptions): Promise<Buffer> {\n return this._call('toSharp', options)\n }\n\n toSharpSync(_options?: RenderOptions): never {\n throw new Error('[canvas] toSharpSync() is not available in worker mode — use toSharp() instead')\n }\n\n // --- Async convenience getters ---\n\n get png(): Promise<Buffer> {\n return this._call('toBuffer', 'png')\n }\n get webp(): Promise<Buffer> {\n return this._call('toBuffer', 'webp')\n }\n get jpg(): Promise<Buffer> {\n return this._call('toBuffer', 'jpg')\n }\n get svg(): Promise<Buffer> {\n return this._call('toBuffer', 'svg')\n }\n get pdf(): Promise<Buffer> {\n return this._call('toBuffer', 'pdf')\n }\n get raw(): Promise<Buffer> {\n return this._call('toBuffer', 'raw')\n }\n\n /** Release the Canvas from worker memory. Call when done with this object. */\n release(): void {\n this._pool.releaseCanvas(this._workerIdx, this._canvasId)\n }\n}\n\n/** Worker thread pool — routes render and canvas-call messages */\nclass WorkerPool {\n private workers: Worker[] = []\n private idle: Worker[] = []\n private queue: Array<{ id: number; props: RootProps }> = []\n private pending = new Map<number, PendingTask>()\n private nextId = 0\n\n constructor(size: number) {\n this.init(size)\n }\n\n private init(size: number) {\n const workerFile = path.join(path.dirname(fileURLToPath(import.meta.url)), '../render.worker.js')\n\n for (let i = 0; i < size; i++) {\n const workerIdx = i\n const worker = new Worker(workerFile)\n worker.on('message', (msg: WorkerResponse) => {\n const task = this.pending.get(msg.taskId)\n if (!task) return\n this.pending.delete(msg.taskId)\n\n if ('error' in msg) {\n task.reject(new Error(msg.error))\n return\n }\n\n if ('canvasId' in msg) {\n // Render complete — put worker back to idle\n this.idle.push(worker)\n this.drain()\n const result: PoolRenderResult = { buffer: msg.buffer, canvasId: msg.canvasId, workerIdx, width: msg.width, height: msg.height }\n task.resolve(result)\n } else {\n // Canvas method call complete\n task.resolve(msg.result)\n }\n })\n this.workers.push(worker)\n this.idle.push(worker)\n }\n }\n\n private drain() {\n while (this.queue.length > 0 && this.idle.length > 0) {\n const task = this.queue.shift()!\n const worker = this.idle.pop()!\n const request: WorkerRequest = { type: 'render', taskId: task.id, props: task.props }\n worker.postMessage(request)\n }\n }\n\n render(props: RootProps): Promise<PoolRenderResult> {\n const sanitizedProps = WorkerPreProcessor.process(props)\n return new Promise<PoolRenderResult>((resolve, reject) => {\n const id = this.nextId++\n this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject })\n if (this.idle.length > 0) {\n const worker = this.idle.pop()!\n const request: WorkerRequest = { type: 'render', taskId: id, props: sanitizedProps }\n worker.postMessage(request)\n } else {\n this.queue.push({ id, props: sanitizedProps })\n }\n })\n }\n\n callOnCanvas<M extends CanvasCallMethod>(workerIdx: number, canvasId: number, method: M, args: CallArgs<M>): Promise<CallResult<M>> {\n return new Promise<CallResult<M>>((resolve, reject) => {\n const id = this.nextId++\n this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject })\n const request = { type: 'call' as const, taskId: id, canvasId, method, args } as WorkerCallRequest\n this.workers[workerIdx].postMessage(request)\n })\n }\n\n releaseCanvas(workerIdx: number, canvasId: number): void {\n const request: WorkerRequest = { type: 'release', canvasId }\n this.workers[workerIdx]?.postMessage(request)\n }\n\n terminate() {\n this.workers.forEach(w => w.terminate())\n }\n}\n\n/**\n * Converts a CanvasElement tree into actual BoxNode instances.\n * Used both for non-worker rendering (inline tree building) and inside\n * the render worker (reconstructing the tree from serialized descriptors).\n */\nexport function buildTree(descriptor: CanvasElement): BoxNode {\n switch (descriptor.__type) {\n case 'Box':\n return new BoxNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Column':\n return new ColumnNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Row':\n return new RowNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Grid':\n return new GridNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) as any })\n case 'GridItem':\n return new GridItemNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) as any })\n case 'Image':\n return new ImageNode(descriptor.props as any)\n case 'Text':\n return new TextNode(descriptor.text, descriptor.props)\n case 'Chart':\n return new ChartNode(descriptor.props as any)\n }\n}\n\n/**\n * Root node that manages the canvas rendering context and coordinates overall layout and drawing.\n * Inherits from ColumnNode to provide vertical layout capabilities.\n */\nexport class RootNode extends ColumnNode {\n /** The canvas instance used for rendering */\n private canvas: Canvas | undefined\n /** The 2D rendering context for the canvas */\n private ctx: CanvasRenderingContext2D | null = null\n /** Target width for the canvas in pixels */\n private readonly targetWidth: number\n /** Target height for the canvas in pixels */\n private readonly targetHeight: number\n /** Scale factor for rendering (e.g. 2 for 2x resolution) */\n private readonly scale: number\n\n /**\n * Creates a new root node for canvas rendering\n * @param props Configuration properties for the root node\n * @throws Error if width property is not provided\n */\n constructor(props: RootProps & BaseProps) {\n // Call the parent constructor with root name and props\n super({ name: 'Root', ...props })\n\n this.props = props\n\n // Validate the required width property\n if (!props.width) {\n throw new Error('Width and height are required for Root')\n }\n\n // Register provided fonts with caching\n if (props.fonts?.length) {\n for (const font of props.fonts) {\n const family = font.family\n const paths = font.paths.map(p => path.resolve(p))\n\n if (!registeredFonts.has(family)) {\n registeredFonts.set(family, new Set())\n }\n\n const cachedPaths = registeredFonts.get(family)!\n const newPaths = paths.filter(p => !cachedPaths.has(p) && fs.existsSync(p))\n\n if (newPaths.length > 0) {\n FontLibrary.use({ [family]: newPaths })\n newPaths.forEach(p => cachedPaths.add(p))\n }\n }\n }\n\n // Set up scale and width\n this.scale = props.scale || 1\n this.targetWidth = props.width\n this.targetHeight = props.height\n this.node.setWidth(this.targetWidth)\n\n // Convert any CanvasElement children to actual BoxNode instances\n if (this.props.children) {\n const childArray = Array.isArray(this.props.children) ? this.props.children : [this.props.children]\n this.props.children = childArray.map(child => {\n if (child && typeof child === 'object' && '__type' in child) {\n return buildTree(child as CanvasElement)\n }\n return child\n }) as any\n }\n\n // Initialize children nodes\n this.processInitialChildren()\n }\n\n /**\n * Traverses the node tree to find all ImageNode instances using breadth-first search\n * @returns Array of all ImageNode instances found in the tree\n */\n private findAllImageNodes(): ImageNode[] {\n const imageNodes: ImageNode[] = []\n const queue: BoxNode[] = [this]\n while (queue.length > 0) {\n const node = queue.shift()!\n if (node instanceof ImageNode) {\n imageNodes.push(node)\n }\n queue.push(...node.children)\n }\n return imageNodes\n }\n\n /**\n * Renders the entire node tree to a canvas, handling image loading, layout calculation,\n * and final drawing\n * @returns Promise resolving to the rendered Canvas instance\n */\n async render(): Promise<Canvas> {\n try {\n // Step 1: Load all images with a concurrency limit to avoid overwhelming remote sources.\n // A per-render cache deduplicates identical src+color combinations within this render pass.\n const imageNodes = this.findAllImageNodes()\n if (imageNodes.length > 0) {\n const imageCache: RenderImageCache = new Map()\n const CONCURRENCY = 5\n const queue = [...imageNodes]\n const workers = Array.from({ length: Math.min(CONCURRENCY, queue.length) }, async () => {\n while (queue.length > 0) {\n const node = queue.shift()!\n await node.load(imageCache)\n }\n })\n await Promise.allSettled(workers)\n }\n\n // Step 2: Calculate initial layout\n this.node.calculateLayout(this.targetWidth, undefined, Style.Direction.LTR)\n\n // Step 3: Allow nodes to finalize their layout\n const needRecalculate = this.finalizeLayout()\n if (needRecalculate) {\n this.node.calculateLayout(this.targetWidth, undefined, Style.Direction.LTR)\n }\n\n // Step 4: Create a canvas with calculated dimensions\n const calculatedContentHeight = this.node.getComputedHeight()\n const finalCanvasWidth = Math.ceil(this.targetWidth * this.scale)\n const finalCanvasHeight = this.targetHeight ? Math.ceil(this.targetHeight * this.scale) : Math.max(1, Math.ceil(calculatedContentHeight * this.scale))\n\n // Step 5: Set up canvas context\n this.canvas = new Canvas(finalCanvasWidth, finalCanvasHeight)\n this.ctx = this.canvas.getContext('2d')\n this.ctx.scale(this.scale, this.scale)\n\n // Step 6: Render content\n super.render(this.ctx, 0, 0)\n\n if (!this.canvas) {\n throw new Error('Canvas not initialized')\n }\n\n return this.canvas\n } finally {\n // Always clear the persistent image cache after render (success or error)\n // so resolved CanvasImage references don't outlive the render pass.\n disposeImageCache()\n }\n }\n}\n\n/**\n * Creates and renders a new root node with the given properties.\n * When worker mode is enabled via configure(), rendering runs in a worker thread\n * and the returned object implements the same toBuffer/toBufferSync interface.\n * @param props Configuration properties for the root node\n * @returns Promise resolving to the rendered Canvas (or WorkerCanvas in worker mode)\n */\nexport const Root = async (props: RootProps): Promise<Canvas> => {\n try {\n if (_workerMode) {\n if (!_workerPool) {\n _workerPool = new WorkerPool(_workerPoolSize)\n }\n const result = await _workerPool.render(props)\n return new WorkerCanvas({ ...result, pool: _workerPool }) as unknown as Canvas\n }\n return await new RootNode(props).render()\n } catch (err) {\n // Ensure cache is cleared even if Root-level orchestration fails\n disposeImageCache()\n throw err\n }\n}\n"],"names":["cpus","disposeImageCache","getImageCache","path","fileURLToPath","Worker","WorkerPreProcessor","BoxNode","ColumnNode","RowNode","GridNode","GridItemNode","ImageNode","TextNode","ChartNode","fs","FontLibrary","Style","Canvas"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA;AACA,MAAM,eAAe,GAAG,IAAI,GAAG,EAAuB;AAOtD;AACA,IAAI,WAAW,GAAG,IAAI;AACtB,IAAI,eAAe,GAAG,IAAI,C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AA,WAAW,GAAG,IAAI,UAAU,CAAC,eAAe,CAAC;YAC/C;YACA,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC;AAC9C,YAAA,OAAO,IAAI,YAAY,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAsB;QAChF;QACA,OAAO,MAAM,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE;IAC3C;IAAE,OAAO,GAAG,EAAE;;AAEZ,QAAAA,mCAAiB,EAAE;AACnB,QAAA,MAAM,GAAG;IACX;AACF;;;;;;;"}
1
+ {"version":3,"file":"root.canvas.util.js","sources":["../../../../src/canvas/root.canvas.util.ts"],"sourcesContent":["import { Canvas, FontLibrary, type CanvasRenderingContext2D } from 'skia-canvas'\nimport type { ExportFormat, ExportOptions, SaveOptions, RenderOptions } from 'skia-canvas'\nimport { ColumnNode, BoxNode, RowNode } from '@/canvas/layout.canvas.util.js'\nimport type { BaseProps, RootProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport type { CanvasCallMethod, CallArgs, CallResult, WorkerCallRequest, WorkerResponse, WorkerRequest } from '@/worker/worker.types.js'\nimport { ImageNode, type RenderImageCache, disposeImageCache, getImageCache } from '@/canvas/image.canvas.util.js'\nimport { TextNode } from '@/canvas/text.canvas.util.js'\nimport { ChartNode } from '@/canvas/chart.canvas.util.js'\nimport { GridNode, GridItemNode } from '@/canvas/grid.canvas.util.js'\nimport { Style } from '@/constant/common.const.js'\nimport { WorkerPreProcessor } from '@/canvas/canvas.helper.js'\nimport * as path from 'node:path'\nimport * as fs from 'node:fs'\nimport { cpus } from 'node:os'\nimport { Worker } from 'node:worker_threads'\nimport { fileURLToPath } from 'node:url'\n\n/** Registry to track fonts that have already been loaded */\nconst registeredFonts = new Map<string, Set<string>>()\n\n// Exported for testing purposes only\nexport const _clearRegisteredFonts = () => {\n registeredFonts.clear()\n}\n\n/** Engine configuration */\nlet _workerMode = true\nlet _workerPoolSize = Math.max(1, cpus().length - 1)\nlet _workerPool: WorkerPool | null = null\n\nexport interface CanvasEngineConfig {\n /** Run rendering in worker threads to avoid blocking the event loop (default: true) */\n workerMode?: boolean\n /** Number of worker threads in the pool (default: os.cpus().length - 1) */\n workers?: number\n /** Maximum number of resolved images to keep in the persistent LRU cache (default: 128) */\n imageCacheSize?: number\n}\n\n/**\n * Configure the canvas rendering engine.\n * Call this once at application startup before rendering.\n */\nexport function configure(options: CanvasEngineConfig) {\n if (options.workerMode !== undefined) _workerMode = options.workerMode\n if (options.workers !== undefined) _workerPoolSize = options.workers\n if (options.imageCacheSize !== undefined) {\n // Dispose existing cache and create a new one with the specified size\n disposeImageCache()\n getImageCache(options.imageCacheSize)\n }\n if (_workerMode) {\n _workerPool = new WorkerPool(_workerPoolSize)\n }\n}\n\ninterface PendingTask {\n resolve: (value: unknown) => void\n reject: (err: Error) => void\n}\n\ninterface PoolRenderResult {\n buffer: Buffer\n canvasId: number\n workerIdx: number\n width: number\n height: number\n}\n\n/**\n * Proxies all skia-canvas Canvas APIs to a Canvas instance living inside a worker thread.\n * Sync methods (toBufferSync, toURLSync) return from a pre-encoded PNG buffer.\n * Async methods (toBuffer, toURL, toFile, getters) delegate to the worker.\n */\nclass WorkerCanvas {\n readonly width: number\n readonly height: number\n private readonly _buffer: Buffer // pre-encoded PNG for sync use\n private readonly _pool: WorkerPool\n private readonly _workerIdx: number\n private readonly _canvasId: number\n\n constructor(opts: PoolRenderResult & { pool: WorkerPool }) {\n this._buffer = opts.buffer\n this.width = opts.width\n this.height = opts.height\n this._pool = opts.pool\n this._workerIdx = opts.workerIdx\n this._canvasId = opts.canvasId\n }\n\n private _call<M extends CanvasCallMethod>(method: M, ...args: CallArgs<M>): Promise<CallResult<M>> {\n return this._pool.callOnCanvas(this._workerIdx, this._canvasId, method, args)\n }\n\n // --- Sync methods: return from pre-encoded PNG buffer ---\n\n toBufferSync(_format?: ExportFormat, _options?: ExportOptions): Buffer {\n return this._buffer\n }\n\n toURLSync(_format?: ExportFormat, _options?: ExportOptions): string {\n return `data:image/png;base64,${this._buffer.toString('base64')}`\n }\n\n // --- Async methods: delegate to worker ---\n\n toBuffer(format: ExportFormat, options?: ExportOptions): Promise<Buffer> {\n return this._call('toBuffer', format, options)\n }\n\n toURL(format: ExportFormat, options?: ExportOptions): Promise<string> {\n return this._call('toURL', format, options)\n }\n\n toFile(filename: string, options?: SaveOptions): Promise<void> {\n return this._call('toFile', filename, options)\n }\n\n /** Returns a Buffer (Sharp instance cannot be transferred across threads) */\n toSharp(options?: RenderOptions): Promise<Buffer> {\n return this._call('toSharp', options)\n }\n\n toSharpSync(_options?: RenderOptions): never {\n throw new Error('[canvas] toSharpSync() is not available in worker mode — use toSharp() instead')\n }\n\n // --- Async convenience getters ---\n\n get png(): Promise<Buffer> {\n return this._call('toBuffer', 'png')\n }\n get webp(): Promise<Buffer> {\n return this._call('toBuffer', 'webp')\n }\n get jpg(): Promise<Buffer> {\n return this._call('toBuffer', 'jpg')\n }\n get svg(): Promise<Buffer> {\n return this._call('toBuffer', 'svg')\n }\n get pdf(): Promise<Buffer> {\n return this._call('toBuffer', 'pdf')\n }\n get raw(): Promise<Buffer> {\n return this._call('toBuffer', 'raw')\n }\n\n /** Release the Canvas from worker memory. Call when done with this object. */\n release(): void {\n this._pool.releaseCanvas(this._workerIdx, this._canvasId)\n }\n}\n\n/** Worker thread pool — routes render and canvas-call messages */\nclass WorkerPool {\n private workers: Worker[] = []\n private idle: Worker[] = []\n private queue: Array<{ id: number; props: RootProps }> = []\n private pending = new Map<number, PendingTask>()\n private nextId = 0\n\n constructor(size: number) {\n this.init(size)\n }\n\n private init(size: number) {\n const workerFile = path.join(path.dirname(fileURLToPath(import.meta.url)), '../worker/render.worker.js')\n\n for (let i = 0; i < size; i++) {\n const workerIdx = i\n const worker = new Worker(workerFile)\n worker.on('message', (msg: WorkerResponse) => {\n const task = this.pending.get(msg.taskId)\n if (!task) return\n this.pending.delete(msg.taskId)\n\n if ('error' in msg) {\n task.reject(new Error(msg.error))\n return\n }\n\n if ('canvasId' in msg) {\n // Render complete — put worker back to idle\n this.idle.push(worker)\n this.drain()\n const result: PoolRenderResult = { buffer: msg.buffer, canvasId: msg.canvasId, workerIdx, width: msg.width, height: msg.height }\n task.resolve(result)\n } else {\n // Canvas method call complete\n task.resolve(msg.result)\n }\n })\n this.workers.push(worker)\n this.idle.push(worker)\n }\n }\n\n private drain() {\n while (this.queue.length > 0 && this.idle.length > 0) {\n const task = this.queue.shift()!\n const worker = this.idle.pop()!\n const request: WorkerRequest = { type: 'render', taskId: task.id, props: task.props }\n worker.postMessage(request)\n }\n }\n\n render(props: RootProps): Promise<PoolRenderResult> {\n const sanitizedProps = WorkerPreProcessor.process(props)\n return new Promise<PoolRenderResult>((resolve, reject) => {\n const id = this.nextId++\n this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject })\n if (this.idle.length > 0) {\n const worker = this.idle.pop()!\n const request: WorkerRequest = { type: 'render', taskId: id, props: sanitizedProps }\n worker.postMessage(request)\n } else {\n this.queue.push({ id, props: sanitizedProps })\n }\n })\n }\n\n callOnCanvas<M extends CanvasCallMethod>(workerIdx: number, canvasId: number, method: M, args: CallArgs<M>): Promise<CallResult<M>> {\n return new Promise<CallResult<M>>((resolve, reject) => {\n const id = this.nextId++\n this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject })\n const request = { type: 'call' as const, taskId: id, canvasId, method, args } as WorkerCallRequest\n this.workers[workerIdx].postMessage(request)\n })\n }\n\n releaseCanvas(workerIdx: number, canvasId: number): void {\n const request: WorkerRequest = { type: 'release', canvasId }\n this.workers[workerIdx]?.postMessage(request)\n }\n\n terminate() {\n this.workers.forEach(w => w.terminate())\n }\n}\n\n/**\n * Converts a CanvasElement tree into actual BoxNode instances.\n * Used both for non-worker rendering (inline tree building) and inside\n * the render worker (reconstructing the tree from serialized descriptors).\n */\nexport function buildTree(descriptor: CanvasElement): BoxNode {\n switch (descriptor.__type) {\n case 'Box':\n return new BoxNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Column':\n return new ColumnNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Row':\n return new RowNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) })\n case 'Grid':\n return new GridNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) as any })\n case 'GridItem':\n return new GridItemNode({ ...descriptor.props, children: descriptor.children?.map(buildTree) as any })\n case 'Image':\n return new ImageNode(descriptor.props as any)\n case 'Text':\n return new TextNode(descriptor.text, descriptor.props)\n case 'Chart':\n return new ChartNode(descriptor.props as any)\n }\n}\n\n/**\n * Root node that manages the canvas rendering context and coordinates overall layout and drawing.\n * Inherits from ColumnNode to provide vertical layout capabilities.\n */\nexport class RootNode extends ColumnNode {\n /** The canvas instance used for rendering */\n private canvas: Canvas | undefined\n /** The 2D rendering context for the canvas */\n private ctx: CanvasRenderingContext2D | null = null\n /** Target width for the canvas in pixels */\n private readonly targetWidth: number\n /** Target height for the canvas in pixels */\n private readonly targetHeight: number\n /** Scale factor for rendering (e.g. 2 for 2x resolution) */\n private readonly scale: number\n\n /**\n * Creates a new root node for canvas rendering\n * @param props Configuration properties for the root node\n * @throws Error if width property is not provided\n */\n constructor(props: RootProps & BaseProps) {\n // Call the parent constructor with root name and props\n super({ name: 'Root', ...props })\n\n this.props = props\n\n // Validate the required width property\n if (!props.width) {\n throw new Error('Width and height are required for Root')\n }\n\n // Register provided fonts with caching\n if (props.fonts?.length) {\n for (const font of props.fonts) {\n const family = font.family\n const paths = font.paths.map(p => path.resolve(p))\n\n if (!registeredFonts.has(family)) {\n registeredFonts.set(family, new Set())\n }\n\n const cachedPaths = registeredFonts.get(family)!\n const newPaths = paths.filter(p => !cachedPaths.has(p) && fs.existsSync(p))\n\n if (newPaths.length > 0) {\n FontLibrary.use({ [family]: newPaths })\n newPaths.forEach(p => cachedPaths.add(p))\n }\n }\n }\n\n // Set up scale and width\n this.scale = props.scale || 1\n this.targetWidth = props.width\n this.targetHeight = props.height\n this.node.setWidth(this.targetWidth)\n\n // Convert any CanvasElement children to actual BoxNode instances\n if (this.props.children) {\n const childArray = Array.isArray(this.props.children) ? this.props.children : [this.props.children]\n this.props.children = childArray.map(child => {\n if (child && typeof child === 'object' && '__type' in child) {\n return buildTree(child as CanvasElement)\n }\n return child\n }) as any\n }\n\n // Initialize children nodes\n this.processInitialChildren()\n }\n\n /**\n * Traverses the node tree to find all ImageNode instances using breadth-first search\n * @returns Array of all ImageNode instances found in the tree\n */\n private findAllImageNodes(): ImageNode[] {\n const imageNodes: ImageNode[] = []\n const queue: BoxNode[] = [this]\n while (queue.length > 0) {\n const node = queue.shift()!\n if (node instanceof ImageNode) {\n imageNodes.push(node)\n }\n queue.push(...node.children)\n }\n return imageNodes\n }\n\n /**\n * Renders the entire node tree to a canvas, handling image loading, layout calculation,\n * and final drawing\n * @returns Promise resolving to the rendered Canvas instance\n */\n async render(): Promise<Canvas> {\n try {\n // Step 1: Load all images with a concurrency limit to avoid overwhelming remote sources.\n // A per-render cache deduplicates identical src+color combinations within this render pass.\n const imageNodes = this.findAllImageNodes()\n if (imageNodes.length > 0) {\n const imageCache: RenderImageCache = new Map()\n const CONCURRENCY = 5\n const queue = [...imageNodes]\n const workers = Array.from({ length: Math.min(CONCURRENCY, queue.length) }, async () => {\n while (queue.length > 0) {\n const node = queue.shift()!\n await node.load(imageCache)\n }\n })\n await Promise.allSettled(workers)\n }\n\n // Step 2: Calculate initial layout\n this.node.calculateLayout(this.targetWidth, undefined, Style.Direction.LTR)\n\n // Step 3: Allow nodes to finalize their layout\n const needRecalculate = this.finalizeLayout()\n if (needRecalculate) {\n this.node.calculateLayout(this.targetWidth, undefined, Style.Direction.LTR)\n }\n\n // Step 4: Create a canvas with calculated dimensions\n const calculatedContentHeight = this.node.getComputedHeight()\n const finalCanvasWidth = Math.ceil(this.targetWidth * this.scale)\n const finalCanvasHeight = this.targetHeight ? Math.ceil(this.targetHeight * this.scale) : Math.max(1, Math.ceil(calculatedContentHeight * this.scale))\n\n // Step 5: Set up canvas context\n this.canvas = new Canvas(finalCanvasWidth, finalCanvasHeight)\n this.ctx = this.canvas.getContext('2d')\n this.ctx.scale(this.scale, this.scale)\n\n // Step 6: Render content\n super.render(this.ctx, 0, 0)\n\n if (!this.canvas) {\n throw new Error('Canvas not initialized')\n }\n\n return this.canvas\n } finally {\n // Always clear the persistent image cache after render (success or error)\n // so resolved CanvasImage references don't outlive the render pass.\n disposeImageCache()\n }\n }\n}\n\n/**\n * Creates and renders a new root node with the given properties.\n * When worker mode is enabled via configure(), rendering runs in a worker thread\n * and the returned object implements the same toBuffer/toBufferSync interface.\n * @param props Configuration properties for the root node\n * @returns Promise resolving to the rendered Canvas (or WorkerCanvas in worker mode)\n */\nexport const Root = async (props: RootProps): Promise<Canvas> => {\n try {\n if (_workerMode) {\n if (!_workerPool) {\n _workerPool = new WorkerPool(_workerPoolSize)\n }\n const result = await _workerPool.render(props)\n return new WorkerCanvas({ ...result, pool: _workerPool }) as unknown as Canvas\n }\n return await new RootNode(props).render()\n } catch (err) {\n // Ensure cache is cleared even if Root-level orchestration fails\n disposeImageCache()\n throw err\n }\n}\n"],"names":["cpus","disposeImageCache","getImageCache","path","fileURLToPath","Worker","WorkerPreProcessor","BoxNode","ColumnNode","RowNode","GridNode","GridItemNode","ImageNode","TextNode","ChartNode","fs","FontLibrary","Style","Canvas"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA;AACA,MAAM,eAAe,GAAG,IAAI,GAAG,EAAuB;AAOtD;AACA,IAAI,WAAW,GAAG,IAAI;AACtB,IAAI,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAEA,YAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;AACpD,IAAI,WAAW,GAAsB,IAAI;AAWzC;;;AAGG;AACG,SAAU,SAAS,CAAC,OAA2B,EAAA;AACnD,IAAA,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS;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AAsB;QAChF;QACA,OAAO,MAAM,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE;IAC3C;IAAE,OAAO,GAAG,EAAE;;AAEZ,QAAAA,mCAAiB,EAAE;AACnB,QAAA,MAAM,GAAG;IACX;AACF;;;;;;;"}
@@ -0,0 +1,4 @@
1
+ export declare function hashBuffer(buf: Buffer): string;
2
+ export declare function readDiskCache(key: string): Promise<Buffer | null>;
3
+ export declare function writeDiskCache(key: string, data: Buffer): Promise<void>;
4
+ //# sourceMappingURL=disk.cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disk.cache.d.ts","sourceRoot":"","sources":["../../../src/util/disk.cache.ts"],"names":[],"mappings":"AAaA,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOvE;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAO7E"}
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ var fs = require('fs');
5
+ var path = require('path');
6
+
7
+ const CACHE_DIR = path.join(process.cwd(), '.cache', 'files');
8
+ let _dirEnsured = false;
9
+ async function ensureDir() {
10
+ if (_dirEnsured)
11
+ return;
12
+ await fs.promises.mkdir(CACHE_DIR, { recursive: true });
13
+ _dirEnsured = true;
14
+ }
15
+ function hashBuffer(buf) {
16
+ return crypto.createHash('sha256').update(buf).digest('hex');
17
+ }
18
+ async function readDiskCache(key) {
19
+ try {
20
+ await ensureDir();
21
+ return await fs.promises.readFile(path.join(CACHE_DIR, key));
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ }
27
+ async function writeDiskCache(key, data) {
28
+ try {
29
+ await ensureDir();
30
+ await fs.promises.writeFile(path.join(CACHE_DIR, key), data);
31
+ }
32
+ catch {
33
+ // best-effort — cache write failures are non-fatal
34
+ }
35
+ }
36
+
37
+ exports.hashBuffer = hashBuffer;
38
+ exports.readDiskCache = readDiskCache;
39
+ exports.writeDiskCache = writeDiskCache;
40
+ //# sourceMappingURL=disk.cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disk.cache.js","sources":["../../../../src/util/disk.cache.ts"],"sourcesContent":["import { createHash } from 'crypto'\nimport { promises as fs } from 'fs'\nimport { join } from 'path'\n\nconst CACHE_DIR = join(process.cwd(), '.cache', 'files')\nlet _dirEnsured = false\n\nasync function ensureDir(): Promise<void> {\n if (_dirEnsured) return\n await fs.mkdir(CACHE_DIR, { recursive: true })\n _dirEnsured = true\n}\n\nexport function hashBuffer(buf: Buffer): string {\n return createHash('sha256').update(buf).digest('hex')\n}\n\nexport async function readDiskCache(key: string): Promise<Buffer | null> {\n try {\n await ensureDir()\n return await fs.readFile(join(CACHE_DIR, key))\n } catch {\n return null\n }\n}\n\nexport async function writeDiskCache(key: string, data: Buffer): Promise<void> {\n try {\n await ensureDir()\n await fs.writeFile(join(CACHE_DIR, key), data)\n } catch {\n // best-effort — cache write failures are non-fatal\n }\n}\n"],"names":["join","fs","createHash"],"mappings":";;;;;;AAIA,MAAM,SAAS,GAAGA,SAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC;AACxD,IAAI,WAAW,GAAG,KAAK;AAEvB,eAAe,SAAS,GAAA;AACtB,IAAA,IAAI,WAAW;QAAE;AACjB,IAAA,MAAMC,WAAE,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC9C,WAAW,GAAG,IAAI;AACpB;AAEM,SAAU,UAAU,CAAC,GAAW,EAAA;AACpC,IAAA,OAAOC,iBAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;AACvD;AAEO,eAAe,aAAa,CAAC,GAAW,EAAA;AAC7C,IAAA,IAAI;QACF,MAAM,SAAS,EAAE;AACjB,QAAA,OAAO,MAAMD,WAAE,CAAC,QAAQ,CAACD,SAAI,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAChD;AAAE,IAAA,MAAM;AACN,QAAA,OAAO,IAAI;IACb;AACF;AAEO,eAAe,cAAc,CAAC,GAAW,EAAE,IAAY,EAAA;AAC5D,IAAA,IAAI;QACF,MAAM,SAAS,EAAE;AACjB,QAAA,MAAMC,WAAE,CAAC,SAAS,CAACD,SAAI,CAAC,SAAS,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC;IAChD;AAAE,IAAA,MAAM;;IAER;AACF;;;;;;"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.worker.d.ts","sourceRoot":"","sources":["../../../src/worker/render.worker.ts"],"names":[],"mappings":""}
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var worker_threads = require('worker_threads');
4
- var root_canvas_util = require('./canvas/root.canvas.util.js');
4
+ var root_canvas_util = require('../canvas/root.canvas.util.js');
5
5
 
6
6
  /**
7
7
  * Worker thread entry point for off-main-thread canvas rendering.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.worker.js","sources":["../../../../src/worker/render.worker.ts"],"sourcesContent":["/**\n * Worker thread entry point for off-main-thread canvas rendering.\n *\n * Message protocol (main → worker):\n * { type: 'render', taskId, props } — render and keep Canvas alive\n * { type: 'call', taskId, canvasId, method, args } — call a method on a live Canvas\n * { type: 'release', canvasId } — free Canvas from memory\n *\n * Responses (worker → main):\n * WorkerRenderResponse — render complete (includes pre-encoded PNG buffer)\n * WorkerCallResponse — method call result\n * WorkerErrorResponse — any failure\n */\nimport { parentPort } from 'worker_threads'\nimport { RootNode } from '@/canvas/root.canvas.util.js'\nimport type { Canvas } from 'skia-canvas'\nimport type { WorkerRequest, WorkerRenderResponse, WorkerCallResponse, WorkerErrorResponse } from '@/worker/worker.types.js'\n\nif (!parentPort) {\n throw new Error('[render.worker] Must be run as a worker thread')\n}\n\nconst canvases = new Map<number, Canvas>()\nlet nextCanvasId = 0\n\nfunction reply(msg: WorkerRenderResponse | WorkerCallResponse | WorkerErrorResponse) {\n parentPort!.postMessage(msg)\n}\n\nparentPort.on('message', async (msg: WorkerRequest) => {\n if (msg.type === 'render') {\n try {\n const canvas = await new RootNode(msg.props).render()\n const canvasId = nextCanvasId++\n canvases.set(canvasId, canvas)\n reply({ taskId: msg.taskId, canvasId, buffer: canvas.toBufferSync('png'), width: canvas.width, height: canvas.height })\n } catch (err) {\n reply({ taskId: msg.taskId, error: String(err) })\n }\n } else if (msg.type === 'call') {\n const canvas = canvases.get(msg.canvasId)\n if (!canvas) {\n reply({ taskId: msg.taskId, error: `[render.worker] Canvas ${msg.canvasId} not found` })\n return\n }\n try {\n let result: Buffer | string | void\n switch (msg.method) {\n case 'toBuffer':\n result = await canvas.toBuffer(...msg.args)\n break\n case 'toURL':\n result = await canvas.toURL(...msg.args)\n break\n case 'toFile':\n result = await canvas.toFile(...msg.args)\n break\n case 'toSharp':\n // Sharp instances can't be transferred across threads — serialize to buffer\n result = await canvas.toSharp(...msg.args).toBuffer()\n break\n }\n reply({ taskId: msg.taskId, result })\n } catch (err) {\n reply({ taskId: msg.taskId, error: String(err) })\n }\n } else {\n // type === 'release'\n canvases.delete(msg.canvasId)\n }\n})\n"],"names":["parentPort","RootNode"],"mappings":";;;;;AAAA;;;;;;;;;;;;AAYG;AAMH,IAAI,CAACA,yBAAU,EAAE;AACf,IAAA,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC;AACnE;AAEA,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB;AAC1C,IAAI,YAAY,GAAG,CAAC;AAEpB,SAAS,KAAK,CAAC,GAAoE,EAAA;AACjF,IAAAA,yBAAW,CAAC,WAAW,CAAC,GAAG,CAAC;AAC9B;AAEAA,yBAAU,CAAC,EAAE,CAAC,SAAS,EAAE,OA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M,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;CAAE,CAAA;AAEpG,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,SAAS,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,MAAM,aAAa,GAAG,mBAAmB,GAAG,iBAAiB,GAAG,oBAAoB,CAAA;AAM1F,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAC/B;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,MAAM,cAAc,GAAG,oBAAoB,GAAG,kBAAkB,GAAG,mBAAmB,CAAA"}
@@ -51,6 +51,9 @@ export declare class ImageNode extends BoxNode {
51
51
  /**
52
52
  * Fetches and processes the image source into a CanvasImage.
53
53
  * Does not touch node state — pure fetch logic.
54
+ *
55
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
56
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
54
57
  */
55
58
  private _fetchCanvasImage;
56
59
  /**
@@ -58,10 +61,11 @@ export declare class ImageNode extends BoxNode {
58
61
  *
59
62
  * Resolution order:
60
63
  * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
61
- * 2. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
62
- * 3. Fresh fetch via `_fetchCanvasImage()`.
64
+ * 2. Disk cache at `.cache/files/<hash>`survives process restarts.
65
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
66
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
63
67
  *
64
- * Resolved images are stored back into the LRU cache for future renders.
68
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
65
69
  */
66
70
  private _loadImage;
67
71
  getLoadingPromise(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"image.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/image.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACnF,OAAO,EAAE,KAAK,wBAAwB,EAAE,KAAK,IAAI,WAAW,EAAa,MAAM,aAAa,CAAA;AAC5F,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAsBxD;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAA;AAWhE;;;;;;;;GAQG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA8B;IAClD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;gBAEZ,OAAO,EAAE,MAAM;IAI3B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IASzC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI;IAa1C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,IAAI,IAAI;CAGhB;AAUD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,MAA2B,GAAG,aAAa,CAejF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAGxC;AAED;;;GAGG;AACH,qBAAa,SAAU,SAAQ,O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,WAAW,GAAG,SAAS;IASzC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI;IAa1C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,IAAI,IAAI;CAGhB;AAUD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,MAA2B,GAAG,aAAa,CAejF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAGxC;AAED;;;GAGG;AACH,qBAAa,SAAU,SAAQ,OAAO;IAC5B,KAAK,EAAE,UAAU,GAAG,SAAS,CAAA;IACrC,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,YAAY,CAAI;IACxB,OAAO,CAAC,aAAa,CAAI;IACzB,OAAO,CAAC,cAAc,CAA6B;gBAEvC,KAAK,EAAE,UAAU;IAYtB,IAAI,CAAC,KAAK,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOpD;;;;;;OAMG;YACW,iBAAiB;IAyE/B;;;;;;;;;;OAUG;IACH,OAAO,CAAC,UAAU;IA2FX,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIzC;;;OAGG;cACgB,cAAc,CAAC,GAAG,EAAE,wBAAwB,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAoJrH;AAED;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,UAAU,KAAG,aAGxC,CAAA"}
@@ -3,6 +3,7 @@ import { BoxNode } from './layout.canvas.util.js';
3
3
  import { parseBorderRadius, drawRoundedRectPath } from './canvas.helper.js';
4
4
  import { promises } from 'fs';
5
5
  import { Style } from '../constant/common.const.js';
6
+ import { writeDiskCache, hashBuffer, readDiskCache } from '../util/disk.cache.js';
6
7
 
7
8
  /**
8
9
  * Calculates pixel offset for image positioning based on percentage or pixel values.
@@ -128,8 +129,11 @@ class ImageNode extends BoxNode {
128
129
  /**
129
130
  * Fetches and processes the image source into a CanvasImage.
130
131
  * Does not touch node state — pure fetch logic.
132
+ *
133
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
134
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
131
135
  */
132
- async _fetchCanvasImage() {
136
+ async _fetchCanvasImage(diskCacheKey) {
133
137
  const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type');
134
138
  let finalSource = this.props.src;
135
139
  let isSvg = false;
@@ -188,6 +192,12 @@ class ImageNode extends BoxNode {
188
192
  const modifiedSvgString = svgString.replace(/fill="[^"]*"/g, `fill="${this.props.color}"`);
189
193
  finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer;
190
194
  }
195
+ // Write resolved buffer to disk cache (fire-and-forget, non-fatal)
196
+ if (diskCacheKey) {
197
+ const cacheBuffer = Buffer.isBuffer(finalSource) ? finalSource : contentBuffer;
198
+ if (cacheBuffer)
199
+ writeDiskCache(diskCacheKey, cacheBuffer);
200
+ }
191
201
  return loadImage(finalSource);
192
202
  }
193
203
  /**
@@ -195,10 +205,11 @@ class ImageNode extends BoxNode {
195
205
  *
196
206
  * Resolution order:
197
207
  * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
198
- * 2. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
199
- * 3. Fresh fetch via `_fetchCanvasImage()`.
208
+ * 2. Disk cache at `.cache/files/<hash>`survives process restarts.
209
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
210
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
200
211
  *
201
- * Resolved images are stored back into the LRU cache for future renders.
212
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
202
213
  */
203
214
  _loadImage(cache) {
204
215
  if (!this.props.src) {
@@ -212,38 +223,55 @@ class ImageNode extends BoxNode {
212
223
  const load = async () => {
213
224
  try {
214
225
  const lru = getImageCache();
215
- const cacheKey = typeof this.props.src === 'string' ? (this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src) : null;
226
+ const cacheKey = typeof this.props.src === 'string'
227
+ ? this.props.color
228
+ ? `${this.props.src}|${this.props.color}`
229
+ : this.props.src
230
+ : this.props.color
231
+ ? `${hashBuffer(this.props.src)}|${this.props.color}`
232
+ : hashBuffer(this.props.src);
216
233
  // 1. Check persistent LRU cache
217
- if (cacheKey) {
218
- const cached = lru.get(cacheKey);
219
- if (cached) {
220
- this.loadedImage = cached;
221
- this.naturalWidth = cached.width;
222
- this.naturalHeight = cached.height;
223
- const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined;
224
- const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
225
- this.node.setAspectRatio(finalAspectRatio);
226
- this.props.onLoad?.();
227
- resolve();
228
- return;
229
- }
234
+ const cached = lru.get(cacheKey);
235
+ if (cached) {
236
+ this.loadedImage = cached;
237
+ this.naturalWidth = cached.width;
238
+ this.naturalHeight = cached.height;
239
+ const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined;
240
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
241
+ this.node.setAspectRatio(finalAspectRatio);
242
+ this.props.onLoad?.();
243
+ resolve();
244
+ return;
230
245
  }
231
- // 2. Per-render dedup cache or fresh fetch
246
+ // 2. Check disk cache (persists across process restarts)
247
+ const diskBuffer = await readDiskCache(cacheKey);
248
+ if (diskBuffer) {
249
+ const img = await loadImage(diskBuffer);
250
+ lru.set(cacheKey, img);
251
+ this.loadedImage = img;
252
+ this.naturalWidth = img.width;
253
+ this.naturalHeight = img.height;
254
+ const calculatedAspectRatio = img.width > 0 && img.height > 0 ? img.width / img.height : undefined;
255
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
256
+ this.node.setAspectRatio(finalAspectRatio);
257
+ this.props.onLoad?.();
258
+ resolve();
259
+ return;
260
+ }
261
+ // 3. Per-render dedup cache or fresh fetch (writes to disk internally)
232
262
  let imagePromise;
233
- if (cache && cacheKey) {
263
+ if (cache) {
234
264
  if (!cache.has(cacheKey)) {
235
- cache.set(cacheKey, this._fetchCanvasImage());
265
+ cache.set(cacheKey, this._fetchCanvasImage(cacheKey));
236
266
  }
237
267
  imagePromise = cache.get(cacheKey);
238
268
  }
239
269
  else {
240
- imagePromise = this._fetchCanvasImage();
270
+ imagePromise = this._fetchCanvasImage(cacheKey);
241
271
  }
242
272
  const img = await imagePromise;
243
- // 3. Store in persistent LRU cache
244
- if (cacheKey) {
245
- lru.set(cacheKey, img);
246
- }
273
+ // 4. Store in persistent LRU cache
274
+ lru.set(cacheKey, img);
247
275
  this.loadedImage = img;
248
276
  this.naturalWidth = img.width;
249
277
  this.naturalHeight = img.height;
@@ -118,7 +118,7 @@ class WorkerPool {
118
118
  this.init(size);
119
119
  }
120
120
  init(size) {
121
- const workerFile = path.join(path.dirname(fileURLToPath(import.meta.url)), '../render.worker.js');
121
+ const workerFile = path.join(path.dirname(fileURLToPath(import.meta.url)), '../worker/render.worker.js');
122
122
  for (let i = 0; i < size; i++) {
123
123
  const workerIdx = i;
124
124
  const worker = new Worker(workerFile);
@@ -0,0 +1,4 @@
1
+ export declare function hashBuffer(buf: Buffer): string;
2
+ export declare function readDiskCache(key: string): Promise<Buffer | null>;
3
+ export declare function writeDiskCache(key: string, data: Buffer): Promise<void>;
4
+ //# sourceMappingURL=disk.cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disk.cache.d.ts","sourceRoot":"","sources":["../../../src/util/disk.cache.ts"],"names":[],"mappings":"AAaA,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOvE;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAO7E"}
@@ -0,0 +1,35 @@
1
+ import { createHash } from 'crypto';
2
+ import { promises } from 'fs';
3
+ import { join } from 'path';
4
+
5
+ const CACHE_DIR = join(process.cwd(), '.cache', 'files');
6
+ let _dirEnsured = false;
7
+ async function ensureDir() {
8
+ if (_dirEnsured)
9
+ return;
10
+ await promises.mkdir(CACHE_DIR, { recursive: true });
11
+ _dirEnsured = true;
12
+ }
13
+ function hashBuffer(buf) {
14
+ return createHash('sha256').update(buf).digest('hex');
15
+ }
16
+ async function readDiskCache(key) {
17
+ try {
18
+ await ensureDir();
19
+ return await promises.readFile(join(CACHE_DIR, key));
20
+ }
21
+ catch {
22
+ return null;
23
+ }
24
+ }
25
+ async function writeDiskCache(key, data) {
26
+ try {
27
+ await ensureDir();
28
+ await promises.writeFile(join(CACHE_DIR, key), data);
29
+ }
30
+ catch {
31
+ // best-effort — cache write failures are non-fatal
32
+ }
33
+ }
34
+
35
+ export { hashBuffer, readDiskCache, writeDiskCache };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.worker.d.ts","sourceRoot":"","sources":["../../../src/worker/render.worker.ts"],"names":[],"mappings":""}
@@ -1,5 +1,5 @@
1
1
  import { parentPort } from 'worker_threads';
2
- import { RootNode } from './canvas/root.canvas.util.js';
2
+ import { RootNode } from '../canvas/root.canvas.util.js';
3
3
 
4
4
  /**
5
5
  * Worker thread entry point for off-main-thread canvas rendering.
@@ -1 +1 @@
1
- {"version":3,"file":"worker.types.d.ts","sourceRoot":"","sources":["../../../src/canvas/worker.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meonode/canvas",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "description": "A declarative, component-based library for server-side canvas image generation. Write complex visuals with simple functions, similar to the composition style of @meonode/ui.",
5
5
  "keywords": [
6
6
  "canvas",
@@ -1 +0,0 @@
1
- {"version":3,"file":"render.worker.d.ts","sourceRoot":"","sources":["../../src/render.worker.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"render.worker.js","sources":["../../../src/render.worker.ts"],"sourcesContent":["/**\n * Worker thread entry point for off-main-thread canvas rendering.\n *\n * Message protocol (main → worker):\n * { type: 'render', taskId, props } — render and keep Canvas alive\n * { type: 'call', taskId, canvasId, method, args } — call a method on a live Canvas\n * { type: 'release', canvasId } — free Canvas from memory\n *\n * Responses (worker → main):\n * WorkerRenderResponse — render complete (includes pre-encoded PNG buffer)\n * WorkerCallResponse — method call result\n * WorkerErrorResponse — any failure\n */\nimport { parentPort } from 'worker_threads'\nimport { RootNode } from '@/canvas/root.canvas.util.js'\nimport type { Canvas } from 'skia-canvas'\nimport type { WorkerRequest, WorkerRenderResponse, WorkerCallResponse, WorkerErrorResponse } from '@/canvas/worker.types.js'\n\nif (!parentPort) {\n throw new Error('[render.worker] Must be run as a worker thread')\n}\n\nconst canvases = new Map<number, Canvas>()\nlet nextCanvasId = 0\n\nfunction reply(msg: WorkerRenderResponse | WorkerCallResponse | WorkerErrorResponse) {\n parentPort!.postMessage(msg)\n}\n\nparentPort.on('message', async (msg: WorkerRequest) => {\n if (msg.type === 'render') {\n try {\n const canvas = await new RootNode(msg.props).render()\n const canvasId = nextCanvasId++\n canvases.set(canvasId, canvas)\n reply({ taskId: msg.taskId, canvasId, buffer: canvas.toBufferSync('png'), width: canvas.width, height: canvas.height })\n } catch (err) {\n reply({ taskId: msg.taskId, error: String(err) })\n }\n } else if (msg.type === 'call') {\n const canvas = canvases.get(msg.canvasId)\n if (!canvas) {\n reply({ taskId: msg.taskId, error: `[render.worker] Canvas ${msg.canvasId} not found` })\n return\n }\n try {\n let result: Buffer | string | void\n switch (msg.method) {\n case 'toBuffer':\n result = await canvas.toBuffer(...msg.args)\n break\n case 'toURL':\n result = await canvas.toURL(...msg.args)\n break\n case 'toFile':\n result = await canvas.toFile(...msg.args)\n break\n case 'toSharp':\n // Sharp instances can't be transferred across threads — serialize to buffer\n result = await canvas.toSharp(...msg.args).toBuffer()\n break\n }\n reply({ taskId: msg.taskId, result })\n } catch (err) {\n reply({ taskId: msg.taskId, error: String(err) })\n }\n } else {\n // type === 'release'\n canvases.delete(msg.canvasId)\n }\n})\n"],"names":["parentPort","RootNode"],"mappings":";;;;;AAAA;;;;;;;;;;;;AAYG;AAMH,IAAI,CAACA,yBAAU,EAAE;AACf,IAAA,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC;AACnE;AAEA,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB;AAC1C,IAAI,YAAY,GAAG,CAAC;AAEpB,SAAS,KAAK,CAAC,GAAoE,EAAA;AACjF,IAAAA,yBAAW,CAAC,WAAW,CAAC,GAAG,CAAC;AAC9B;AAEAA,yBAAU,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,GAAkB,KAAI;AACpD,IAAA,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE;AACzB,QAAA,IAAI;AACF,YAAA,MAAM,MAAM,GAAG,MAAM,IAAIC,yBAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE;AACrD,YAAA,MAAM,QAAQ,GAAG,YAAY,EAAE;AAC/B,YAAA,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC;AAC9B,YAAA,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,MAAM,E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E,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QACnD;IACF;SAAO;;AAEL,QAAA,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAC/B;AACF,CAAC,CAAC;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"render.worker.d.ts","sourceRoot":"","sources":["../../src/render.worker.ts"],"names":[],"mappings":""}
File without changes
File without changes