wasm-html-to-image 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +326 -0
- package/dist/browser-workers.d.ts +12 -0
- package/dist/browser-workers.js +26 -0
- package/dist/child-workers.d.ts +11 -0
- package/dist/child-workers.js +19 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +87 -0
- package/dist/core.d.ts +168 -0
- package/dist/core.js +824 -0
- package/dist/diagnostics.d.ts +93 -0
- package/dist/diagnostics.js +28 -0
- package/dist/edge-light.d.ts +18 -0
- package/dist/edge-light.js +70 -0
- package/dist/encode-args.d.ts +10 -0
- package/dist/encode-args.js +30 -0
- package/dist/html-to-image-single.js +0 -0
- package/dist/html-to-image-wasm.js +3 -0
- package/dist/html-to-image.js +2 -0
- package/dist/html-to-image.wasm +0 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/input.d.ts +23 -0
- package/dist/input.js +90 -0
- package/dist/loader.d.ts +132 -0
- package/dist/loader.js +199 -0
- package/dist/preact.d.ts +10 -0
- package/dist/preact.js +13 -0
- package/dist/react.d.ts +9 -0
- package/dist/react.js +11 -0
- package/dist/single.d.ts +27 -0
- package/dist/single.js +22 -0
- package/dist/tailwind.d.ts +1 -0
- package/dist/tailwind.js +6 -0
- package/dist/wasm-payload.d.ts +2 -0
- package/dist/wasm-payload.js +39 -0
- package/dist/web-workers.js +4411 -0
- package/dist/worker-actions.d.ts +10 -0
- package/dist/worker-actions.js +13 -0
- package/dist/worker-lib-loader.d.ts +19 -0
- package/dist/worker-lib-loader.js +34 -0
- package/dist/workerd.d.ts +18 -0
- package/dist/workerd.js +65 -0
- package/dist/workers-dummy.d.ts +12 -0
- package/dist/workers-dummy.js +6 -0
- package/dist/workers-parent.js +602 -0
- package/dist/workers.d.ts +86 -0
- package/dist/workers.js +163 -0
- package/package.json +166 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type HtmlToImageOptions, type RenderResult } from "./core.js";
|
|
2
|
+
import type { HtmlToImageModule } from "./loader.js";
|
|
3
|
+
/**
|
|
4
|
+
* Shared worker action table (unified `render` only).
|
|
5
|
+
* `child-workers.ts` (Node) and `browser-workers.ts` (pre-bundled browser
|
|
6
|
+
* worker) differ only in how `getModule` resolves the WASM module.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createRenderActions(getModule: () => Promise<HtmlToImageModule>): {
|
|
9
|
+
render(options: HtmlToImageOptions): Promise<RenderResult<Uint8Array | string>>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { htmlToImage, } from "./core.js";
|
|
2
|
+
/**
|
|
3
|
+
* Shared worker action table (unified `render` only).
|
|
4
|
+
* `child-workers.ts` (Node) and `browser-workers.ts` (pre-bundled browser
|
|
5
|
+
* worker) differ only in how `getModule` resolves the WASM module.
|
|
6
|
+
*/
|
|
7
|
+
export function createRenderActions(getModule) {
|
|
8
|
+
return {
|
|
9
|
+
async render(options) {
|
|
10
|
+
return await htmlToImage(await getModule(), options);
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type * as WorkerLibTypes from "worker-lib";
|
|
2
|
+
export interface WorkerLib {
|
|
3
|
+
createWorker: typeof WorkerLibTypes.createWorker;
|
|
4
|
+
initWorker: typeof WorkerLibTypes.initWorker;
|
|
5
|
+
Worker: typeof WorkerLibTypes.Worker;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Load worker-lib, tolerating its broken native-ESM build: the published
|
|
9
|
+
* `dist/esm/*.js` uses extensionless relative imports (`./pool`), which
|
|
10
|
+
* throw `ERR_MODULE_NOT_FOUND` under plain Node (2.2.0/2.2.1; upstream
|
|
11
|
+
* dodges this via bundler resolution). Plain Node falls back to the CJS
|
|
12
|
+
* build via `createRequire`, which resolves fine.
|
|
13
|
+
*
|
|
14
|
+
* No `@vite-ignore` on the first import so bundlers still substitute the
|
|
15
|
+
* browser build for browser targets.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadWorkerLib(): Promise<WorkerLib>;
|
|
18
|
+
/** Drop the cached load (e.g. after a failed load) so it can be retried. */
|
|
19
|
+
export declare function resetWorkerLib(): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { importNode } from "./loader.js";
|
|
2
|
+
let cached;
|
|
3
|
+
/**
|
|
4
|
+
* Load worker-lib, tolerating its broken native-ESM build: the published
|
|
5
|
+
* `dist/esm/*.js` uses extensionless relative imports (`./pool`), which
|
|
6
|
+
* throw `ERR_MODULE_NOT_FOUND` under plain Node (2.2.0/2.2.1; upstream
|
|
7
|
+
* dodges this via bundler resolution). Plain Node falls back to the CJS
|
|
8
|
+
* build via `createRequire`, which resolves fine.
|
|
9
|
+
*
|
|
10
|
+
* No `@vite-ignore` on the first import so bundlers still substitute the
|
|
11
|
+
* browser build for browser targets.
|
|
12
|
+
*/
|
|
13
|
+
export function loadWorkerLib() {
|
|
14
|
+
if (!cached) {
|
|
15
|
+
cached = (async () => {
|
|
16
|
+
try {
|
|
17
|
+
return (await import("worker-lib"));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
const mod = await importNode("module");
|
|
21
|
+
return mod.createRequire(import.meta.url)("worker-lib");
|
|
22
|
+
}
|
|
23
|
+
})();
|
|
24
|
+
// A rejected load must not poison the cache; allow a later retry.
|
|
25
|
+
cached.catch(() => {
|
|
26
|
+
resetWorkerLib();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return cached;
|
|
30
|
+
}
|
|
31
|
+
/** Drop the cached load (e.g. after a failed load) so it can be retried. */
|
|
32
|
+
export function resetWorkerLib() {
|
|
33
|
+
cached = undefined;
|
|
34
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type HtmlToImageModule } from "./loader.js";
|
|
2
|
+
import { type HtmlToImageOptions, type RenderResult } from "./core.js";
|
|
3
|
+
export type { OutputFormat, RenderOptions, HtmlToImageOptions, RenderResult, DiagnosticMessage, FontDiagnostic, RenderDiagnostics, RenderLimits, ResolveResourceHook, ResourceDiagnostic, } from "./core.js";
|
|
4
|
+
export { DIAGNOSTIC_CODES, LogLevel } from "./core.js";
|
|
5
|
+
export type { HtmlToImageModule, EmscriptenModuleArg, LoadOptions, WasmInstancePtr, CreateHtmlToImageModule, } from "./loader.js";
|
|
6
|
+
/**
|
|
7
|
+
* Load (and reuse) the unified module on workerd.
|
|
8
|
+
* @param wasm The compiled WASM module (defaults to the bundled one)
|
|
9
|
+
*/
|
|
10
|
+
export declare function getDefaultModule(wasm?: WebAssembly.Module): Promise<HtmlToImageModule>;
|
|
11
|
+
/**
|
|
12
|
+
* Render HTML — or convert an image — on workerd.
|
|
13
|
+
* Returns RenderResult containing data and metadata.
|
|
14
|
+
*/
|
|
15
|
+
export declare function render(options: HtmlToImageOptions & {
|
|
16
|
+
format: "svg";
|
|
17
|
+
}, wasm?: WebAssembly.Module): Promise<RenderResult<string>>;
|
|
18
|
+
export declare function render(options: HtmlToImageOptions, wasm?: WebAssembly.Module): Promise<RenderResult<Uint8Array | string>>;
|
package/dist/workerd.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare Workers (workerd) entry point.
|
|
3
|
+
*
|
|
4
|
+
* Upstream-compatible pattern (`satoru` / `wasm-image-optimization`
|
|
5
|
+
* `workerd.ts`): the `.wasm` artifact is imported as a pre-compiled
|
|
6
|
+
* `WebAssembly.Module` and instantiation is overridden via
|
|
7
|
+
* `instantiateWasm`, because workerd cannot fetch/compile WASM at runtime.
|
|
8
|
+
*
|
|
9
|
+
* Uses the regular (non-SINGLE_FILE) `dist/html-to-image.wasm`
|
|
10
|
+
* (`SINGLE_FILE` builds cannot be used on Workers).
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import wasm from "wasm-html-to-image/html-to-image.wasm"; // optional override
|
|
14
|
+
* import { render } from "wasm-html-to-image/workerd";
|
|
15
|
+
* const png = await render({ value: "<h1>hi</h1>", width: 800, format: "png" });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
// @ts-expect-error — dist/html-to-image.js is generated at build time
|
|
19
|
+
import createHtmlToImageModule from "../dist/html-to-image.js";
|
|
20
|
+
import htmlToImageWasm from "../dist/html-to-image.wasm";
|
|
21
|
+
import { loadHtmlToImageModule, } from "./loader.js";
|
|
22
|
+
import { htmlToImage, } from "./core.js";
|
|
23
|
+
export { DIAGNOSTIC_CODES, LogLevel } from "./core.js";
|
|
24
|
+
/** Build the Emscripten module args overriding WASM instantiation. */
|
|
25
|
+
function workerdModuleArg(wasm) {
|
|
26
|
+
return {
|
|
27
|
+
instantiateWasm: (imports, successCallback) => {
|
|
28
|
+
// Cloudflare Workers requires using the pre-compiled WebAssembly.Module
|
|
29
|
+
WebAssembly.instantiate(wasm, imports)
|
|
30
|
+
.then((instance) => {
|
|
31
|
+
successCallback(instance, wasm);
|
|
32
|
+
})
|
|
33
|
+
.catch((e) => {
|
|
34
|
+
console.error("wasm-html-to-image [workerd]: Wasm instantiation failed:", e);
|
|
35
|
+
});
|
|
36
|
+
return {}; // Return empty object as emscripten expects
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Module instances keyed by WASM binary: a second call with a different
|
|
41
|
+
* `wasm` must not silently reuse the first one (previous bug). Failed loads
|
|
42
|
+
* are evicted so a later call can retry. */
|
|
43
|
+
const moduleByWasm = new Map();
|
|
44
|
+
/**
|
|
45
|
+
* Load (and reuse) the unified module on workerd.
|
|
46
|
+
* @param wasm The compiled WASM module (defaults to the bundled one)
|
|
47
|
+
*/
|
|
48
|
+
export function getDefaultModule(wasm = htmlToImageWasm) {
|
|
49
|
+
let pending = moduleByWasm.get(wasm);
|
|
50
|
+
if (!pending) {
|
|
51
|
+
pending = loadHtmlToImageModule({
|
|
52
|
+
factory: createHtmlToImageModule,
|
|
53
|
+
moduleArg: workerdModuleArg(wasm),
|
|
54
|
+
});
|
|
55
|
+
moduleByWasm.set(wasm, pending);
|
|
56
|
+
pending.catch(() => {
|
|
57
|
+
if (moduleByWasm.get(wasm) === pending)
|
|
58
|
+
moduleByWasm.delete(wasm);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return pending;
|
|
62
|
+
}
|
|
63
|
+
export async function render(options, wasm) {
|
|
64
|
+
return htmlToImage(await getDefaultModule(wasm ?? htmlToImageWasm), options);
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workerd fallback for `./workers`: no thread pool on Workers, so jobs run
|
|
3
|
+
* directly on the workerd entry point instead of being pooled.
|
|
4
|
+
*/
|
|
5
|
+
export type { HtmlToImageWorker } from "./child-workers.js";
|
|
6
|
+
export type { HtmlToImageOptions, OutputFormat, RenderOptions, } from "./core.js";
|
|
7
|
+
export { render } from "./workerd.js";
|
|
8
|
+
export declare const setLimit: (_limit: number) => void;
|
|
9
|
+
export declare const close: () => void;
|
|
10
|
+
export declare const waitAll: () => Promise<void>;
|
|
11
|
+
export declare const waitReady: (_retryTime?: number) => Promise<void>;
|
|
12
|
+
export declare const launchWorker: () => Promise<void>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { render } from "./workerd.js";
|
|
2
|
+
export const setLimit = (_limit) => { };
|
|
3
|
+
export const close = () => { };
|
|
4
|
+
export const waitAll = () => Promise.resolve();
|
|
5
|
+
export const waitReady = (_retryTime) => Promise.resolve();
|
|
6
|
+
export const launchWorker = () => Promise.resolve();
|