@meonode/canvas 1.7.0 → 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.
- package/dist/cjs/canvas/image.canvas.util.d.ts +42 -2
- package/dist/cjs/canvas/image.canvas.util.d.ts.map +1 -1
- package/dist/cjs/canvas/image.canvas.util.js +145 -7
- package/dist/cjs/canvas/image.canvas.util.js.map +1 -1
- package/dist/cjs/canvas/root.canvas.util.d.ts +2 -0
- package/dist/cjs/canvas/root.canvas.util.d.ts.map +1 -1
- package/dist/cjs/canvas/root.canvas.util.js +59 -40
- package/dist/cjs/canvas/root.canvas.util.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/util/disk.cache.d.ts +4 -0
- package/dist/cjs/util/disk.cache.d.ts.map +1 -0
- package/dist/cjs/util/disk.cache.js +40 -0
- package/dist/cjs/util/disk.cache.js.map +1 -0
- package/dist/cjs/worker/render.worker.d.ts.map +1 -0
- package/dist/cjs/{render.worker.js → worker/render.worker.js} +1 -1
- package/dist/cjs/worker/render.worker.js.map +1 -0
- package/dist/{esm/canvas → cjs/worker}/worker.types.d.ts.map +1 -1
- package/dist/esm/canvas/image.canvas.util.d.ts +42 -2
- package/dist/esm/canvas/image.canvas.util.d.ts.map +1 -1
- package/dist/esm/canvas/image.canvas.util.js +143 -8
- package/dist/esm/canvas/root.canvas.util.d.ts +2 -0
- package/dist/esm/canvas/root.canvas.util.d.ts.map +1 -1
- package/dist/esm/canvas/root.canvas.util.js +60 -41
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/util/disk.cache.d.ts +4 -0
- package/dist/esm/util/disk.cache.d.ts.map +1 -0
- package/dist/esm/util/disk.cache.js +35 -0
- package/dist/esm/worker/render.worker.d.ts.map +1 -0
- package/dist/esm/{render.worker.js → worker/render.worker.js} +1 -1
- package/dist/{cjs/canvas → esm/worker}/worker.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/render.worker.d.ts.map +0 -1
- package/dist/cjs/render.worker.js.map +0 -1
- package/dist/esm/render.worker.d.ts.map +0 -1
- /package/dist/cjs/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
- /package/dist/cjs/{canvas → worker}/worker.types.d.ts +0 -0
- /package/dist/esm/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
- /package/dist/esm/{canvas → worker}/worker.types.d.ts +0 -0
|
@@ -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 } 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}\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 (_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 // 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 }\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 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 new RootNode(props).render()\n}\n"],"names":["cpus","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;AASzC;;;AAGG;AACG,SAAU,SAAS,CAAC,OAA2B,EAAA;AACnD,IAAA,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS;AAAE,QAAA,WAAW,GAAG,OAAO,CAAC,UAAU;AACtE,IAAA,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS;AAAE,QAAA,eAAe,GAAG,OAAO,CAAC,OAAO;IACpE,IAAI,WAAW,EAAE;AACf,QAAA,WAAW,GAAG,IAAI,UAAU,CAAC,eAAe,CAAC;IAC/C;AACF;AAeA;;;;AAIG;AACH,MAAM,YAAY,CAAA;AACP,IAAA,KAAK;AACL,IAAA,MAAM;IACE,OAAO,CAAQ;AACf,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C,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC;AACvC,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC;;QAGtC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;AAE5B,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC;QAC3C;QAEA,OAAO,IAAI,CAAC,MAAM;IACpB;AACD;AAED;;;;;;AAMG;MACU,IAAI,GAAG,OAAO,KAAgB,KAAqB;IAC9D,IAAI,WAAW,EAAE;QACf,IAAI,CAAC,WAAW,EAAE;AAChB,YAAA,WAAW,GAAG,IAAI,UAAU,CAAC,eAAe,CAAC;QAC/C;QACA,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC;AAC9C,QAAA,OAAO,IAAI,YAAY,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAsB;IAChF;IACA,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE;AACrC;;;;;;;"}
|
|
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","f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BAAU;;;AAGR,YAAAjB,mCAAiB,EAAE;QACrB;IACF;AACD;AAED;;;;;;AAMG;MACU,IAAI,GAAG,OAAO,KAAgB,KAAqB;AAC9D,IAAA,IAAI;QACF,IAAI,WAAW,EAAE;YACf,IAAI,CAAC,WAAW,EAAE;AAChB,gBAAA,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;;;;;;;"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './constant/common.const.js';
|
|
2
2
|
export * from './canvas/canvas.type.js';
|
|
3
3
|
export { Box, Column, Row, type BoxNode } from './canvas/layout.canvas.util.js';
|
|
4
|
-
export { Image } from './canvas/image.canvas.util.js';
|
|
4
|
+
export { Image, disposeImageCache } from './canvas/image.canvas.util.js';
|
|
5
5
|
export { Text } from './canvas/text.canvas.util.js';
|
|
6
6
|
export { Root, configure } from './canvas/root.canvas.util.js';
|
|
7
7
|
export { GridItem } from './canvas/grid.canvas.util.js';
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,yBAAyB,CAAA;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAC/E,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,yBAAyB,CAAA;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAC/E,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAA;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAA;AACnD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAA;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAA"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -20,6 +20,7 @@ exports.Box = layout_canvas_util.Box;
|
|
|
20
20
|
exports.Column = layout_canvas_util.Column;
|
|
21
21
|
exports.Row = layout_canvas_util.Row;
|
|
22
22
|
exports.Image = image_canvas_util.Image;
|
|
23
|
+
exports.disposeImageCache = image_canvas_util.disposeImageCache;
|
|
23
24
|
exports.Text = text_canvas_util.Text;
|
|
24
25
|
exports.Root = root_canvas_util.Root;
|
|
25
26
|
exports.configure = root_canvas_util.configure;
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -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('
|
|
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,OAAO,GAAkB,KAAI;AACpD,IAAA,IAAI,GAAG,CAAC,IAAI,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AK,QAAQ;oBACX,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC;oBACzC;AACF,gBAAA,KAAK,SAAS;;AAEZ,oBAAA,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE;oBACrD;;YAEJ,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QACvC;QAAE,OAAO,GAAG,EAAE;AACZ,YAAA,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.types.d.ts","sourceRoot":"","sources":["../../../src/
|
|
1
|
+
{"version":3,"file":"worker.types.d.ts","sourceRoot":"","sources":["../../../src/worker/worker.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC1F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE;QAAE,IAAI,EAAE,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;IAClE,KAAK,EAAE;QAAE,IAAI,EAAE,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;IAC/D,MAAM,EAAE;QAAE,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;Q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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"}
|
|
@@ -6,6 +6,36 @@ import { BoxNode } from '../canvas/layout.canvas.util.js';
|
|
|
6
6
|
* Scoped to a single RootNode.render() call; discarded after rendering.
|
|
7
7
|
*/
|
|
8
8
|
export type RenderImageCache = Map<string, Promise<CanvasImage>>;
|
|
9
|
+
/**
|
|
10
|
+
* A simple LRU cache for resolved `CanvasImage` objects.
|
|
11
|
+
*
|
|
12
|
+
* - Persists across render passes so repeated renders of the same tree don't
|
|
13
|
+
* re-fetch every image.
|
|
14
|
+
* - Bounded by `maxSize` entries; least-recently-used entries are evicted first.
|
|
15
|
+
* - Call `dispose()` to eagerly release all held images, or rely on the
|
|
16
|
+
* automatic `process.on('exit')` hook that clears the singleton.
|
|
17
|
+
*/
|
|
18
|
+
export declare class ImageLRUCache {
|
|
19
|
+
private readonly map;
|
|
20
|
+
readonly maxSize: number;
|
|
21
|
+
constructor(maxSize: number);
|
|
22
|
+
get(key: string): CanvasImage | undefined;
|
|
23
|
+
set(key: string, image: CanvasImage): void;
|
|
24
|
+
has(key: string): boolean;
|
|
25
|
+
get size(): number;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns the singleton `ImageLRUCache`, creating it on first access.
|
|
30
|
+
* Registers a one-time process cleanup hook to clear the cache
|
|
31
|
+
* so native image buffers are freed when the process shuts down.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getImageCache(maxSize?: number): ImageLRUCache;
|
|
34
|
+
/**
|
|
35
|
+
* Explicitly disposes the global image cache.
|
|
36
|
+
* Useful in tests or when tearing down the rendering engine.
|
|
37
|
+
*/
|
|
38
|
+
export declare function disposeImageCache(): void;
|
|
9
39
|
/**
|
|
10
40
|
* Renders images with configurable sizing, positioning, and effects.
|
|
11
41
|
* Supports object-fit modes, positioning, border radius, and saturation filters.
|
|
@@ -21,11 +51,21 @@ export declare class ImageNode extends BoxNode {
|
|
|
21
51
|
/**
|
|
22
52
|
* Fetches and processes the image source into a CanvasImage.
|
|
23
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).
|
|
24
57
|
*/
|
|
25
58
|
private _fetchCanvasImage;
|
|
26
59
|
/**
|
|
27
|
-
* Loads and processes an image
|
|
28
|
-
*
|
|
60
|
+
* Loads and processes an image.
|
|
61
|
+
*
|
|
62
|
+
* Resolution order:
|
|
63
|
+
* 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
|
|
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.
|
|
67
|
+
*
|
|
68
|
+
* Buffer sources use a SHA-256 hash as their cache key (same as string sources).
|
|
29
69
|
*/
|
|
30
70
|
private _loadImage;
|
|
31
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;
|
|
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;AAuBxD;;;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,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.
|
|
@@ -20,6 +21,86 @@ function calculateOffsetFromValue(positionValue, availableSpace) {
|
|
|
20
21
|
console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`);
|
|
21
22
|
return availableSpace * 0.5;
|
|
22
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* A simple LRU cache for resolved `CanvasImage` objects.
|
|
26
|
+
*
|
|
27
|
+
* - Persists across render passes so repeated renders of the same tree don't
|
|
28
|
+
* re-fetch every image.
|
|
29
|
+
* - Bounded by `maxSize` entries; least-recently-used entries are evicted first.
|
|
30
|
+
* - Call `dispose()` to eagerly release all held images, or rely on the
|
|
31
|
+
* automatic `process.on('exit')` hook that clears the singleton.
|
|
32
|
+
*/
|
|
33
|
+
class ImageLRUCache {
|
|
34
|
+
map = new Map();
|
|
35
|
+
maxSize;
|
|
36
|
+
constructor(maxSize) {
|
|
37
|
+
this.maxSize = maxSize;
|
|
38
|
+
}
|
|
39
|
+
get(key) {
|
|
40
|
+
const entry = this.map.get(key);
|
|
41
|
+
if (!entry)
|
|
42
|
+
return undefined;
|
|
43
|
+
// Move to end (most-recently used)
|
|
44
|
+
this.map.delete(key);
|
|
45
|
+
this.map.set(key, entry);
|
|
46
|
+
return entry.image;
|
|
47
|
+
}
|
|
48
|
+
set(key, image) {
|
|
49
|
+
// If key already exists, refresh it
|
|
50
|
+
if (this.map.has(key)) {
|
|
51
|
+
this.map.delete(key);
|
|
52
|
+
}
|
|
53
|
+
// Evict oldest if at capacity
|
|
54
|
+
while (this.map.size >= this.maxSize) {
|
|
55
|
+
const oldest = this.map.keys().next().value;
|
|
56
|
+
this.map.delete(oldest);
|
|
57
|
+
}
|
|
58
|
+
this.map.set(key, { image, key });
|
|
59
|
+
}
|
|
60
|
+
has(key) {
|
|
61
|
+
return this.map.has(key);
|
|
62
|
+
}
|
|
63
|
+
get size() {
|
|
64
|
+
return this.map.size;
|
|
65
|
+
}
|
|
66
|
+
dispose() {
|
|
67
|
+
this.map.clear();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Module-level singleton — lazily created on first render. */
|
|
71
|
+
let _globalImageCache = null;
|
|
72
|
+
const DEFAULT_CACHE_SIZE = 128;
|
|
73
|
+
// Symbol key on process to track hook registration across module reloads (e.g. Jest resetModules)
|
|
74
|
+
const HOOK_KEY = Symbol.for('__meonode_canvas_image_cache_hook__');
|
|
75
|
+
/**
|
|
76
|
+
* Returns the singleton `ImageLRUCache`, creating it on first access.
|
|
77
|
+
* Registers a one-time process cleanup hook to clear the cache
|
|
78
|
+
* so native image buffers are freed when the process shuts down.
|
|
79
|
+
*/
|
|
80
|
+
function getImageCache(maxSize = DEFAULT_CACHE_SIZE) {
|
|
81
|
+
if (!_globalImageCache) {
|
|
82
|
+
_globalImageCache = new ImageLRUCache(maxSize);
|
|
83
|
+
}
|
|
84
|
+
if (!globalThis[HOOK_KEY]) {
|
|
85
|
+
globalThis[HOOK_KEY] = true;
|
|
86
|
+
const cleanup = () => {
|
|
87
|
+
_globalImageCache?.dispose();
|
|
88
|
+
_globalImageCache = null;
|
|
89
|
+
};
|
|
90
|
+
process.once('exit', cleanup);
|
|
91
|
+
process.once('SIGINT', cleanup);
|
|
92
|
+
process.once('SIGTERM', cleanup);
|
|
93
|
+
}
|
|
94
|
+
return _globalImageCache;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Explicitly disposes the global image cache.
|
|
98
|
+
* Useful in tests or when tearing down the rendering engine.
|
|
99
|
+
*/
|
|
100
|
+
function disposeImageCache() {
|
|
101
|
+
_globalImageCache?.dispose();
|
|
102
|
+
_globalImageCache = null;
|
|
103
|
+
}
|
|
23
104
|
/**
|
|
24
105
|
* Renders images with configurable sizing, positioning, and effects.
|
|
25
106
|
* Supports object-fit modes, positioning, border radius, and saturation filters.
|
|
@@ -48,8 +129,11 @@ class ImageNode extends BoxNode {
|
|
|
48
129
|
/**
|
|
49
130
|
* Fetches and processes the image source into a CanvasImage.
|
|
50
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).
|
|
51
135
|
*/
|
|
52
|
-
async _fetchCanvasImage() {
|
|
136
|
+
async _fetchCanvasImage(diskCacheKey) {
|
|
53
137
|
const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type');
|
|
54
138
|
let finalSource = this.props.src;
|
|
55
139
|
let isSvg = false;
|
|
@@ -108,11 +192,24 @@ class ImageNode extends BoxNode {
|
|
|
108
192
|
const modifiedSvgString = svgString.replace(/fill="[^"]*"/g, `fill="${this.props.color}"`);
|
|
109
193
|
finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer;
|
|
110
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
|
+
}
|
|
111
201
|
return loadImage(finalSource);
|
|
112
202
|
}
|
|
113
203
|
/**
|
|
114
|
-
* Loads and processes an image
|
|
115
|
-
*
|
|
204
|
+
* Loads and processes an image.
|
|
205
|
+
*
|
|
206
|
+
* Resolution order:
|
|
207
|
+
* 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
|
|
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.
|
|
211
|
+
*
|
|
212
|
+
* Buffer sources use a SHA-256 hash as their cache key (same as string sources).
|
|
116
213
|
*/
|
|
117
214
|
_loadImage(cache) {
|
|
118
215
|
if (!this.props.src) {
|
|
@@ -125,18 +222,56 @@ class ImageNode extends BoxNode {
|
|
|
125
222
|
return new Promise(resolve => {
|
|
126
223
|
const load = async () => {
|
|
127
224
|
try {
|
|
225
|
+
const lru = getImageCache();
|
|
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);
|
|
233
|
+
// 1. Check persistent LRU cache
|
|
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;
|
|
245
|
+
}
|
|
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)
|
|
128
262
|
let imagePromise;
|
|
129
|
-
if (cache
|
|
130
|
-
const cacheKey = this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src;
|
|
263
|
+
if (cache) {
|
|
131
264
|
if (!cache.has(cacheKey)) {
|
|
132
|
-
cache.set(cacheKey, this._fetchCanvasImage());
|
|
265
|
+
cache.set(cacheKey, this._fetchCanvasImage(cacheKey));
|
|
133
266
|
}
|
|
134
267
|
imagePromise = cache.get(cacheKey);
|
|
135
268
|
}
|
|
136
269
|
else {
|
|
137
|
-
imagePromise = this._fetchCanvasImage();
|
|
270
|
+
imagePromise = this._fetchCanvasImage(cacheKey);
|
|
138
271
|
}
|
|
139
272
|
const img = await imagePromise;
|
|
273
|
+
// 4. Store in persistent LRU cache
|
|
274
|
+
lru.set(cacheKey, img);
|
|
140
275
|
this.loadedImage = img;
|
|
141
276
|
this.naturalWidth = img.width;
|
|
142
277
|
this.naturalHeight = img.height;
|
|
@@ -317,4 +452,4 @@ const Image = (props) => ({
|
|
|
317
452
|
props: props,
|
|
318
453
|
});
|
|
319
454
|
|
|
320
|
-
export { Image, ImageNode };
|
|
455
|
+
export { Image, ImageLRUCache, ImageNode, disposeImageCache, getImageCache };
|
|
@@ -7,6 +7,8 @@ export interface CanvasEngineConfig {
|
|
|
7
7
|
workerMode?: boolean;
|
|
8
8
|
/** Number of worker threads in the pool (default: os.cpus().length - 1) */
|
|
9
9
|
workers?: number;
|
|
10
|
+
/** Maximum number of resolved images to keep in the persistent LRU cache (default: 128) */
|
|
11
|
+
imageCacheSize?: number;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* Configure the canvas rendering engine.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"root.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/root.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAA8C,MAAM,aAAa,CAAA;AAEhF,OAAO,EAAE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