@ethercorps/sveltekit-og 4.3.0-next.5 → 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.
- package/dist/helpers/utils.d.ts +1 -0
- package/dist/helpers/utils.js +19 -3
- package/dist/image-response.js +5 -19
- package/dist/providers/instances.js +11 -4
- package/dist/providers/resvg/edge.js +1 -8
- package/dist/providers/satori/edge.d.ts +6 -0
- package/dist/providers/satori/edge.js +16 -0
- package/dist/providers/satori/yoga.wasm +0 -0
- package/package.json +2 -2
package/dist/helpers/utils.d.ts
CHANGED
package/dist/helpers/utils.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
const sizeFormats = [
|
|
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
|
|
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)) +
|
|
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
|
+
}
|
package/dist/image-response.js
CHANGED
|
@@ -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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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,13 +15,15 @@ export async function useResvg(debug = false) {
|
|
|
15
15
|
if (resvgInstance.instance) {
|
|
16
16
|
return resvgInstance.instance.Resvg;
|
|
17
17
|
}
|
|
18
|
-
log.debug("Initializing
|
|
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
21
|
// Keep both dynamic imports as direct expressions so the bundler (unwasm/Rollup)
|
|
22
22
|
// can statically analyse them and emit the `?module` wasm chunk correctly.
|
|
23
23
|
// Burying these inside nested async callbacks breaks wasm bundling on Cloudflare.
|
|
24
|
-
const moduleImport = isWorkerLikeRuntime
|
|
24
|
+
const moduleImport = isWorkerLikeRuntime
|
|
25
|
+
? import("./resvg/edge.js")
|
|
26
|
+
: import("./resvg/node.js");
|
|
25
27
|
resvgInstance.instance = await handleAsync(() => moduleImport.then((m) => m.default), ErrorCodes.RESVG_INIT_FAILED, "Failed to import ReSVG module");
|
|
26
28
|
await handleAsync(() => resvgInstance.instance.initWasmPromise, ErrorCodes.RESVG_INIT_FAILED, "Failed to initialize ReSVG WASM");
|
|
27
29
|
return resvgInstance.instance.Resvg;
|
|
@@ -31,8 +33,13 @@ export async function useSatori(debug = false) {
|
|
|
31
33
|
if (satoriInstance.instance) {
|
|
32
34
|
return satoriInstance.instance.satori;
|
|
33
35
|
}
|
|
34
|
-
log.debug("Initializing Satori
|
|
35
|
-
|
|
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");
|
|
36
43
|
await handleAsync(() => satoriInstance.instance.initWasmPromise, ErrorCodes.SATORI_INIT_FAILED, "Failed to initialize Satori WASM");
|
|
37
44
|
return satoriInstance.instance.satori;
|
|
38
45
|
}
|
|
@@ -3,13 +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
|
-
// Vendored wasm: a relative import resolves inside this package so the
|
|
9
|
-
// consumer's bundler emits a real CompiledWasm module. A bare
|
|
10
|
-
// "@resvg/resvg-wasm/...?module" specifier falls back to runtime byte
|
|
11
|
-
// compilation, which Cloudflare Workers block.
|
|
12
|
-
import("./resvg.wasm?module").then((r) => r.default || r)
|
|
13
|
-
),
|
|
6
|
+
initWasmPromise: initWasm(import("./resvg.wasm?module").then((r) => r.default || r)),
|
|
14
7
|
Resvg: _Resvg,
|
|
15
8
|
};
|
|
@@ -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.
|
|
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": "^
|
|
94
|
+
"std-env": "^4.1.0",
|
|
95
95
|
"unwasm": "^0.5.3"
|
|
96
96
|
},
|
|
97
97
|
"peerDependencies": {
|