@ethercorps/sveltekit-og 4.3.0-next.4 → 4.3.0-next.6

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.
@@ -1 +1,2 @@
1
1
  export declare const formatBytes: (bytes: number, decimals?: number) => string;
2
+ export declare function importWasm(input: any): Promise<any>;
@@ -1,9 +1,25 @@
1
- const sizeFormats = ['Bytes', 'KB', 'MB', 'GB'];
1
+ const sizeFormats = ["Bytes", "KB", "MB", "GB"];
2
2
  const kbSize = 1024;
3
3
  export const formatBytes = (bytes, decimals = 2) => {
4
4
  if (bytes === 0)
5
- return '0 Bytes';
5
+ return "0 Bytes";
6
6
  const decimalPoint = decimals < 0 ? 0 : decimals;
7
7
  const sizeIndex = Math.min(Math.floor(Math.log(bytes) / Math.log(kbSize)), 3);
8
- return parseFloat((bytes / Math.pow(kbSize, sizeIndex)).toFixed(decimalPoint)) + ' ' + sizeFormats[sizeIndex];
8
+ return (parseFloat((bytes / Math.pow(kbSize, sizeIndex)).toFixed(decimalPoint)) +
9
+ " " +
10
+ sizeFormats[sizeIndex]);
9
11
  };
12
+ export async function importWasm(input) {
13
+ // may be a nested await for some reason
14
+ const _input = await input;
15
+ const _module = _input.default || _input;
16
+ // this is from rollup/wasm, it does some magic we need to recover from
17
+ if (typeof _module === "function") {
18
+ // empty input is to avoid instantiating the wasm module
19
+ // this will just compile it
20
+ const fnRes = await _module();
21
+ const _instance = fnRes.instance || fnRes;
22
+ return _instance.exports || _instance || _module;
23
+ }
24
+ return _module;
25
+ }
@@ -6,28 +6,14 @@ import { formatBytes } from "./helpers/utils.js";
6
6
  export class ImageResponse extends Response {
7
7
  constructor(element, options, props) {
8
8
  const extended_options = Object.assign({ ...DEFAULT_OPTIONS }, options);
9
- const isDebug = extended_options.debug ?? false;
10
- const log = createLogger(isDebug);
11
- log.debug("ImageResponse created");
12
9
  const create_image_function = extended_options.format === "png" ? createPng : createSvg;
13
10
  const body = new ReadableStream({
14
11
  async start(controller) {
15
- try {
16
- const buffer = (await handleAsync(() => create_image_function(element, extended_options, {
17
- props,
18
- }), ErrorCodes.UNKNOWN_ERROR, `Failed to generate ${extended_options.format?.toUpperCase()}`));
19
- log.debug(buffer.length.toLocaleString());
20
- log.info(`Generated ${extended_options.format.toUpperCase()}: ${formatBytes(buffer.length)}`);
21
- controller.enqueue(buffer);
22
- controller.close();
23
- }
24
- catch (error) {
25
- const err = error instanceof ImageResponseError
26
- ? error
27
- : new ImageResponseError(error instanceof Error ? error.message : String(error), ErrorCodes.UNKNOWN_ERROR, error instanceof Error ? error : new Error(String(error)));
28
- log.error("Failed to create image response:", err.message);
29
- controller.error(err);
30
- }
12
+ const buffer = await create_image_function(element, extended_options, {
13
+ props,
14
+ });
15
+ controller.enqueue(buffer);
16
+ controller.close();
31
17
  },
32
18
  });
33
19
  super(body, {
@@ -15,20 +15,17 @@ export async function useResvg(debug = false) {
15
15
  if (resvgInstance.instance) {
16
16
  return resvgInstance.instance.Resvg;
17
17
  }
18
- log.debug("Initializing Resvg WASM");
18
+ log.debug("Initializing ReSVG WASM");
19
19
  const isWorkerLikeRuntime = isEdgeLight || isWorkerd;
20
20
  log.info(`Detected runtime: ${isWorkerLikeRuntime ? "Edge Light or Workerd" : "Node.js"}`);
21
- const moduleImport = await handleAsync(async () => {
22
- if (isWorkerLikeRuntime) {
23
- return import("./resvg/edge.js");
24
- }
25
- return import("./resvg/node.js");
26
- }, ErrorCodes.RESVG_INIT_FAILED, "Failed to import ReSVG module");
27
- resvgInstance.instance = await handleAsync(async () => {
28
- const mod = await moduleImport;
29
- return mod.default;
30
- }, ErrorCodes.RESVG_INIT_FAILED, "Failed to load ReSVG default export");
31
- await handleAsync(async () => resvgInstance.instance.initWasmPromise, ErrorCodes.RESVG_INIT_FAILED, "Failed to initialize ReSVG WASM");
21
+ // Keep both dynamic imports as direct expressions so the bundler (unwasm/Rollup)
22
+ // can statically analyse them and emit the `?module` wasm chunk correctly.
23
+ // Burying these inside nested async callbacks breaks wasm bundling on Cloudflare.
24
+ const moduleImport = isWorkerLikeRuntime
25
+ ? import("./resvg/edge.js")
26
+ : import("./resvg/node.js");
27
+ resvgInstance.instance = await handleAsync(() => moduleImport.then((m) => m.default), ErrorCodes.RESVG_INIT_FAILED, "Failed to import ReSVG module");
28
+ await handleAsync(() => resvgInstance.instance.initWasmPromise, ErrorCodes.RESVG_INIT_FAILED, "Failed to initialize ReSVG WASM");
32
29
  return resvgInstance.instance.Resvg;
33
30
  }
34
31
  export async function useSatori(debug = false) {
@@ -36,11 +33,13 @@ export async function useSatori(debug = false) {
36
33
  if (satoriInstance.instance) {
37
34
  return satoriInstance.instance.satori;
38
35
  }
39
- log.debug("Initializing Satori WASM");
40
- satoriInstance.instance = await handleAsync(async () => {
41
- const mod = await import("./satori/node.js");
42
- return mod.default;
43
- }, ErrorCodes.SATORI_INIT_FAILED, "Failed to load Satori module");
44
- await handleAsync(async () => satoriInstance.instance.initWasmPromise, ErrorCodes.SATORI_INIT_FAILED, "Failed to initialize Satori WASM");
36
+ log.debug("Initializing Satori");
37
+ const isWorkerLikeRuntime = isEdgeLight || isWorkerd;
38
+ log.info(`Detected runtime: ${isWorkerLikeRuntime ? "Edge Light or Workerd" : "Node.js"}`);
39
+ const moduleImport = isWorkerLikeRuntime
40
+ ? import("./satori/edge.js")
41
+ : import("./satori/node.js");
42
+ satoriInstance.instance = await handleAsync(() => moduleImport.then((m) => m.default), ErrorCodes.SATORI_INIT_FAILED, "Failed to import Satori module");
43
+ await handleAsync(() => satoriInstance.instance.initWasmPromise, ErrorCodes.SATORI_INIT_FAILED, "Failed to initialize Satori WASM");
45
44
  return satoriInstance.instance.satori;
46
45
  }
@@ -3,9 +3,6 @@ import { Resvg as _Resvg, initWasm } from "@resvg/resvg-wasm";
3
3
  export default {
4
4
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
5
  // @ts-ignore
6
- initWasmPromise: initWasm(
7
- // @ts-ignore
8
- import("@resvg/resvg-wasm/index_bg.wasm?module").then((r) => r.default || r)
9
- ),
6
+ initWasmPromise: initWasm(import("./resvg.wasm?module").then((r) => r.default || r)),
10
7
  Resvg: _Resvg,
11
8
  };
Binary file
@@ -0,0 +1,6 @@
1
+ declare namespace _default {
2
+ export let initWasmPromise: Promise<void>;
3
+ export { _satori as satori };
4
+ }
5
+ export default _default;
6
+ import _satori from "satori/standalone";
@@ -0,0 +1,16 @@
1
+ import _satori, { init } from "satori/standalone";
2
+
3
+ // On worker-like runtimes (Cloudflare Workers, Edge) the default `satori` entry
4
+ // fails: it bundles yoga's layout wasm as base64 and compiles it at runtime,
5
+ // which Workers block ("Wasm code generation disallowed by embedder").
6
+ //
7
+ // `satori/standalone` exposes `init()` so we can hand it a pre-compiled
8
+ // WebAssembly.Module instead. Importing the vendored yoga.wasm with `?module`
9
+ // makes the consumer bundler emit a real CompiledWasm module, so no runtime
10
+ // byte compilation happens. Mirrors providers/resvg/edge.js.
11
+ export default {
12
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13
+ // @ts-ignore
14
+ initWasmPromise: init(import("./yoga.wasm?module").then((r) => r.default || r)),
15
+ satori: _satori,
16
+ };
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "4.3.0-next.4",
3
+ "version": "4.3.0-next.6",
4
4
  "license": "MIT",
5
5
  "homepage": "https://sveltekit-og.dev",
6
6
  "repository": "github:ethercorps/sveltekit-og",
@@ -91,7 +91,7 @@
91
91
  "@resvg/resvg-wasm": "^2.6.2",
92
92
  "satori": "^0.25.0",
93
93
  "satori-html": "0.3.2",
94
- "std-env": "^3.10.0",
94
+ "std-env": "^4.1.0",
95
95
  "unwasm": "^0.5.3"
96
96
  },
97
97
  "peerDependencies": {