@jimp/wasm-jxl 0.0.0-stage → 2.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 +107 -2
- package/dist/browser/header.d.ts +27 -0
- package/dist/browser/header.d.ts.map +1 -0
- package/dist/browser/header.js +265 -0
- package/dist/browser/header.js.map +1 -0
- package/dist/browser/index.d.ts +52 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +156 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/load-wasm-browser.d.mts.map +1 -0
- package/dist/browser/load-wasm-browser.mjs.map +1 -0
- package/dist/browser/load-wasm.d.ts +3 -0
- package/dist/browser/load-wasm.js +6 -0
- package/dist/browser/package.json +3 -0
- package/dist/esm/header.d.ts +27 -0
- package/dist/esm/header.d.ts.map +1 -0
- package/dist/esm/header.js +265 -0
- package/dist/esm/header.js.map +1 -0
- package/dist/esm/index.d.ts +52 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +156 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/load-wasm.d.ts +6 -0
- package/dist/esm/load-wasm.d.ts.map +1 -0
- package/dist/esm/load-wasm.js +12 -0
- package/dist/esm/load-wasm.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +76 -4
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
import decode, { init as initDecoder } from "@jsquash/jxl/decode.js";
|
|
3
|
+
import encode, { init as initEncoder } from "@jsquash/jxl/encode.js";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { readJxlHeader, TransferFunction } from "./header.js";
|
|
6
|
+
import { loadWasm } from "./load-wasm.js";
|
|
7
|
+
const JxlOptionsSchema = z
|
|
8
|
+
.object({
|
|
9
|
+
/**
|
|
10
|
+
* Image quality, between 0 and 100. 90 (the `cjxl` default) is
|
|
11
|
+
* visually lossless for most images. 100 is lossless, like `lossless`.
|
|
12
|
+
* @default 90
|
|
13
|
+
*/
|
|
14
|
+
quality: z.number().optional().default(90),
|
|
15
|
+
/**
|
|
16
|
+
* Store every pixel exactly, including alpha. `quality` is ignored.
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
lossless: z.boolean().optional().default(false),
|
|
20
|
+
/**
|
|
21
|
+
* How hard the encoder tries to make the file smaller, between 1 (fastest)
|
|
22
|
+
* and 9 (slowest).
|
|
23
|
+
* @default 7
|
|
24
|
+
*/
|
|
25
|
+
effort: z.number().optional().default(7),
|
|
26
|
+
/**
|
|
27
|
+
* Order the data so a partially downloaded file can be shown at a lower
|
|
28
|
+
* quality first. Makes files a little larger.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
progressive: z.boolean().optional().default(false),
|
|
32
|
+
/**
|
|
33
|
+
* Use the modular mode for lossy images too, rather than the DCT based
|
|
34
|
+
* VarDCT mode. It can do better on graphics, and worse on photos.
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
lossyModular: z.boolean().optional().default(false),
|
|
38
|
+
/**
|
|
39
|
+
* Strength of the edge-preserving filter applied when decoding lossy
|
|
40
|
+
* images, between 0 (off) and 3. -1 picks one from the quality.
|
|
41
|
+
* @default -1
|
|
42
|
+
*/
|
|
43
|
+
epf: z.number().optional().default(-1),
|
|
44
|
+
/**
|
|
45
|
+
* Make lossy images faster to decode, between 0 (slowest, smallest
|
|
46
|
+
* files) and 4 (fastest).
|
|
47
|
+
* @default 0
|
|
48
|
+
*/
|
|
49
|
+
decodingSpeedTier: z.number().optional().default(0),
|
|
50
|
+
/**
|
|
51
|
+
* Add synthetic film grain to lossy images, like a photo taken at this
|
|
52
|
+
* ISO. 0 adds none.
|
|
53
|
+
* @default 0
|
|
54
|
+
*/
|
|
55
|
+
photonNoiseIso: z.number().optional().default(0),
|
|
56
|
+
})
|
|
57
|
+
.strict();
|
|
58
|
+
let decoderReady;
|
|
59
|
+
let encoderReady;
|
|
60
|
+
function initDecoderOnce(module) {
|
|
61
|
+
decoderReady ??= Promise.resolve(module ?? loadWasm("@jsquash/jxl/codec/dec/jxl_dec.wasm")).then(initDecoder);
|
|
62
|
+
return decoderReady;
|
|
63
|
+
}
|
|
64
|
+
function initEncoderOnce(module) {
|
|
65
|
+
// @jsquash/jxl only uses the multi-threaded encoder outside of Node
|
|
66
|
+
encoderReady ??= Promise.resolve(module ?? loadWasm("@jsquash/jxl/codec/enc/jxl_enc.wasm")).then(initEncoder);
|
|
67
|
+
return encoderReady;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The codecs return null for data they can't handle. Anything else they
|
|
71
|
+
* throw is a wasm trap, almost always from running out of memory (wasm has
|
|
72
|
+
* at most 2 GB), which can leave the codec in a bad state.
|
|
73
|
+
*/
|
|
74
|
+
function isTrap(error) {
|
|
75
|
+
return !(error instanceof Error && /^(De|En)coding error/.test(error.message));
|
|
76
|
+
}
|
|
77
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
78
|
+
async function decodeJxl(data, { maxResolutionInMP = 100 }, wasm) {
|
|
79
|
+
const header = readJxlHeader(data);
|
|
80
|
+
if (header) {
|
|
81
|
+
const { width, height, transferFunction } = header;
|
|
82
|
+
if (width * height > maxResolutionInMP * 1_000_000) {
|
|
83
|
+
throw new Error(`JPEG XL dimensions ${width}x${height} exceed the maximum resolution of ${maxResolutionInMP} megapixels`);
|
|
84
|
+
}
|
|
85
|
+
if (transferFunction === TransferFunction.PQ ||
|
|
86
|
+
transferFunction === TransferFunction.HLG) {
|
|
87
|
+
throw new Error(`Unsupported JPEG XL image: HDR images (with the ${transferFunction === TransferFunction.PQ ? "PQ" : "HLG"} transfer function) can't be decoded, because the codec would convert them to 8-bit sRGB without tone mapping`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
await initDecoderOnce(wasm);
|
|
91
|
+
let result;
|
|
92
|
+
try {
|
|
93
|
+
result = await decode(new Uint8Array(data).buffer);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (!isTrap(error)) {
|
|
97
|
+
throw new Error("Failed to decode the JPEG XL image: it's invalid, or uses a feature the codec doesn't support", { cause: error });
|
|
98
|
+
}
|
|
99
|
+
// Start again with a fresh instance
|
|
100
|
+
decoderReady = undefined;
|
|
101
|
+
throw new Error(`Failed to decode the ${header ? `${header.width}x${header.height} ` : ""}JPEG XL image: the codec ran out of memory`, { cause: error });
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
data: Buffer.from(result.data.buffer, result.data.byteOffset, result.data.byteLength),
|
|
105
|
+
width: result.width,
|
|
106
|
+
height: result.height,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async function encodeJxl({ width, height, data }, options, wasm) {
|
|
110
|
+
const { quality, lossless, effort, progressive, lossyModular, epf, decodingSpeedTier, photonNoiseIso, } = JxlOptionsSchema.parse(options);
|
|
111
|
+
await initEncoderOnce(wasm);
|
|
112
|
+
try {
|
|
113
|
+
const arrayBuffer = await encode({
|
|
114
|
+
width,
|
|
115
|
+
height,
|
|
116
|
+
data: new Uint8ClampedArray(data.buffer, data.byteOffset, data.length),
|
|
117
|
+
// ImageData needs one, but the codec only reads the pixels
|
|
118
|
+
colorSpace: "srgb",
|
|
119
|
+
}, {
|
|
120
|
+
// Quality 100 is what makes the codec lossless. Its own lossless
|
|
121
|
+
// option does the same, but warns about the other options.
|
|
122
|
+
quality: lossless ? 100 : clamp(quality, 0, 100),
|
|
123
|
+
lossless: false,
|
|
124
|
+
lossyModular: lossless ? false : lossyModular,
|
|
125
|
+
// Larger values make the codec fail
|
|
126
|
+
effort: clamp(Math.round(effort), 1, 9),
|
|
127
|
+
epf: clamp(Math.round(epf), -1, 3),
|
|
128
|
+
decodingSpeedTier: clamp(Math.round(decodingSpeedTier), 0, 4),
|
|
129
|
+
photonNoiseIso: Math.max(photonNoiseIso, 0),
|
|
130
|
+
progressive,
|
|
131
|
+
// Makes files much larger, and isn't lossless, in this build of libjxl
|
|
132
|
+
lossyPalette: false,
|
|
133
|
+
});
|
|
134
|
+
return Buffer.from(arrayBuffer);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (isTrap(error)) {
|
|
138
|
+
encoderReady = undefined;
|
|
139
|
+
throw new Error(`Failed to encode the ${width}x${height} JPEG XL image: the codec ran out of memory`, { cause: error });
|
|
140
|
+
}
|
|
141
|
+
throw new Error("Failed to encode the JPEG XL image", { cause: error });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* JPEG XL images (.jxl), as bare codestreams or in the ISOBMFF container.
|
|
146
|
+
* Images are decoded to 8-bit sRGB, and written as 8-bit sRGB with alpha.
|
|
147
|
+
*/
|
|
148
|
+
export default function jxl({ wasm } = {}) {
|
|
149
|
+
return {
|
|
150
|
+
mime: "image/jxl",
|
|
151
|
+
hasAlpha: true,
|
|
152
|
+
encode: (bitmap, options = {}) => encodeJxl(bitmap, options, wasm?.encoder),
|
|
153
|
+
decode: (data, options = {}) => decodeJxl(data, options, wasm?.decoder),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,MAAM,EAAE,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,MAAM,EAAE,EAAE,IAAI,IAAI,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAErE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,MAAM,gB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;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,EAAE,IAAI,EAAE,GAAqB,EAAE;IACzD,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,GAAwB,EAAE,EAAE,EAAE,CACpD,SAAS,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;QAC3C,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,GAAqB,EAAE,EAAE,EAAE,CAC/C,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC;KACZ,CAAC;AAClC,CAAC","sourcesContent":["import { Buffer } from \"buffer\";\nimport decode, { init as initDecoder } from \"@jsquash/jxl/decode.js\";\nimport encode, { init as initEncoder } from \"@jsquash/jxl/encode.js\";\nimport { Bitmap, Format } from \"@jimp/types\";\nimport { z } from \"zod\";\n\nimport { readJxlHeader, TransferFunction } from \"./header.js\";\nimport { loadWasm } from \"./load-wasm.js\";\n\nconst JxlOptionsSchema = z\n .object({\n /**\n * Image quality, between 0 and 100. 90 (the `cjxl` default) is\n * visually lossless for most images. 100 is lossless, like `lossless`.\n * @default 90\n */\n quality: z.number().optional().default(90),\n /**\n * Store every pixel exactly, including alpha. `quality` is ignored.\n * @default false\n */\n lossless: z.boolean().optional().default(false),\n /**\n * How hard the encoder tries to make the file smaller, between 1 (fastest)\n * and 9 (slowest).\n * @default 7\n */\n effort: z.number().optional().default(7),\n /**\n * Order the data so a partially downloaded file can be shown at a lower\n * quality first. Makes files a little larger.\n * @default false\n */\n progressive: z.boolean().optional().default(false),\n /**\n * Use the modular mode for lossy images too, rather than the DCT based\n * VarDCT mode. It can do better on graphics, and worse on photos.\n * @default false\n */\n lossyModular: z.boolean().optional().default(false),\n /**\n * Strength of the edge-preserving filter applied when decoding lossy\n * images, between 0 (off) and 3. -1 picks one from the quality.\n * @default -1\n */\n epf: z.number().optional().default(-1),\n /**\n * Make lossy images faster to decode, between 0 (slowest, smallest\n * files) and 4 (fastest).\n * @default 0\n */\n decodingSpeedTier: z.number().optional().default(0),\n /**\n * Add synthetic film grain to lossy images, like a photo taken at this\n * ISO. 0 adds none.\n * @default 0\n */\n photonNoiseIso: z.number().optional().default(0),\n })\n .strict();\n\ntype JxlOptions = z.infer<typeof JxlOptionsSchema>;\n\nexport interface DecodeJxlOptions {\n /**\n * Largest image, in megapixels, that will be decoded. Larger images are\n * rejected before any pixel memory is allocated.\n * @default 100\n */\n maxResolutionInMP?: number;\n}\n\nexport interface JxlWasm {\n /** The compiled `@jsquash/jxl/codec/dec/jxl_dec.wasm` */\n decoder?: WebAssembly.Module;\n /**\n * The compiled `@jsquash/jxl/codec/enc/jxl_enc.wasm`, the single-threaded\n * encoder that @jsquash/jxl uses in Node and Cloudflare Workers\n */\n encoder?: WebAssembly.Module;\n}\n\nexport interface JxlFormatOptions {\n /**\n * Precompiled wasm modules for runtimes where the codecs can't load their\n * own wasm, like Cloudflare Workers.\n * Each codec is initialized once, by the first instance that encodes or\n * decodes, so pass the same modules to every instance.\n */\n wasm?: JxlWasm;\n}\n\nlet decoderReady: Promise<unknown> | undefined;\nlet encoderReady: Promise<unknown> | undefined;\n\nfunction initDecoderOnce(module?: WebAssembly.Module) {\n decoderReady ??= Promise.resolve(\n module ?? loadWasm(\"@jsquash/jxl/codec/dec/jxl_dec.wasm\"),\n ).then(initDecoder);\n return decoderReady;\n}\n\nfunction initEncoderOnce(module?: WebAssembly.Module) {\n // @jsquash/jxl only uses the multi-threaded encoder outside of Node\n encoderReady ??= Promise.resolve(\n module ?? loadWasm(\"@jsquash/jxl/codec/enc/jxl_enc.wasm\"),\n ).then(initEncoder);\n return encoderReady;\n}\n\n/**\n * The codecs return null for data they can't handle. Anything else they\n * throw is a wasm trap, almost always from running out of memory (wasm has\n * at most 2 GB), which can leave the codec in a bad state.\n */\nfunction isTrap(error: unknown) {\n return !(\n error instanceof Error && /^(De|En)coding error/.test(error.message)\n );\n}\n\nconst clamp = (value: number, min: number, max: number) =>\n Math.min(Math.max(value, min), max);\n\nasync function decodeJxl(\n data: Buffer,\n { maxResolutionInMP = 100 }: DecodeJxlOptions,\n wasm?: WebAssembly.Module,\n): Promise<Bitmap> {\n const header = readJxlHeader(data);\n\n if (header) {\n const { width, height, transferFunction } = header;\n\n if (width * height > maxResolutionInMP * 1_000_000) {\n throw new Error(\n `JPEG XL dimensions ${width}x${height} exceed the maximum resolution of ${maxResolutionInMP} megapixels`,\n );\n }\n\n if (\n transferFunction === TransferFunction.PQ ||\n transferFunction === TransferFunction.HLG\n ) {\n throw new Error(\n `Unsupported JPEG XL image: HDR images (with the ${transferFunction === TransferFunction.PQ ? \"PQ\" : \"HLG\"} transfer function) can't be decoded, because the codec would convert them to 8-bit sRGB without tone mapping`,\n );\n }\n }\n\n await initDecoderOnce(wasm);\n\n let result: ImageData;\n\n try {\n result = await decode(new Uint8Array(data).buffer);\n } catch (error) {\n if (!isTrap(error)) {\n throw new Error(\n \"Failed to decode the JPEG XL image: it's invalid, or uses a feature the codec doesn't support\",\n { cause: error },\n );\n }\n\n // Start again with a fresh instance\n decoderReady = undefined;\n throw new Error(\n `Failed to decode the ${header ? `${header.width}x${header.height} ` : \"\"}JPEG XL image: the codec ran out of memory`,\n { cause: error },\n );\n }\n\n return {\n data: Buffer.from(\n result.data.buffer,\n result.data.byteOffset,\n result.data.byteLength,\n ),\n width: result.width,\n height: result.height,\n };\n}\n\nasync function encodeJxl(\n { width, height, data }: Bitmap,\n options: Partial<JxlOptions>,\n wasm?: WebAssembly.Module,\n) {\n const {\n quality,\n lossless,\n effort,\n progressive,\n lossyModular,\n epf,\n decodingSpeedTier,\n photonNoiseIso,\n } = JxlOptionsSchema.parse(options);\n\n await initEncoderOnce(wasm);\n\n try {\n const arrayBuffer = await encode(\n {\n width,\n height,\n data: new Uint8ClampedArray(\n data.buffer as ArrayBuffer,\n data.byteOffset,\n data.length,\n ),\n // ImageData needs one, but the codec only reads the pixels\n colorSpace: \"srgb\",\n },\n {\n // Quality 100 is what makes the codec lossless. Its own lossless\n // option does the same, but warns about the other options.\n quality: lossless ? 100 : clamp(quality, 0, 100),\n lossless: false,\n lossyModular: lossless ? false : lossyModular,\n // Larger values make the codec fail\n effort: clamp(Math.round(effort), 1, 9),\n epf: clamp(Math.round(epf), -1, 3),\n decodingSpeedTier: clamp(Math.round(decodingSpeedTier), 0, 4),\n photonNoiseIso: Math.max(photonNoiseIso, 0),\n progressive,\n // Makes files much larger, and isn't lossless, in this build of libjxl\n lossyPalette: false,\n },\n );\n\n return Buffer.from(arrayBuffer);\n } catch (error) {\n if (isTrap(error)) {\n encoderReady = undefined;\n throw new Error(\n `Failed to encode the ${width}x${height} JPEG XL image: the codec ran out of memory`,\n { cause: error },\n );\n }\n\n throw new Error(\"Failed to encode the JPEG XL image\", { cause: error });\n }\n}\n\n/**\n * JPEG XL images (.jxl), as bare codestreams or in the ISOBMFF container.\n * Images are decoded to 8-bit sRGB, and written as 8-bit sRGB with alpha.\n */\nexport default function jxl({ wasm }: JxlFormatOptions = {}) {\n return {\n mime: \"image/jxl\",\n hasAlpha: true,\n encode: (bitmap, options: Partial<JxlOptions> = {}) =>\n encodeJxl(bitmap, options, wasm?.encoder),\n decode: (data, options: DecodeJxlOptions = {}) =>\n decodeJxl(data, options, wasm?.decoder),\n } satisfies Format<\"image/jxl\">;\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The @jsquash codecs fetch their wasm from a file: URL, which Node's fetch
|
|
3
|
+
* can't load, so read and compile it from disk instead.
|
|
4
|
+
*/
|
|
5
|
+
export declare function loadWasm(specifier: string): Promise<WebAssembly.Module | undefined>;
|
|
6
|
+
//# sourceMappingURL=load-wasm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-wasm.d.ts","sourceRoot":"","sources":["../../src/load-wasm.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,wBAAsB,QAAQ,CAC5B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,WAAW,CAAC,MAAM,GAAG,SAAS,CAAC,CAGzC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
/**
|
|
5
|
+
* The @jsquash codecs fetch their wasm from a file: URL, which Node's fetch
|
|
6
|
+
* can't load, so read and compile it from disk instead.
|
|
7
|
+
*/
|
|
8
|
+
export async function loadWasm(specifier) {
|
|
9
|
+
const wasm = await readFile(require.resolve(specifier));
|
|
10
|
+
return WebAssembly.compile(new Uint8Array(wasm));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=load-wasm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-wasm.js","sourceRoot":"","sources":["../../src/load-wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,SAAiB;IAEjB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;IACxD,OAAO,WAAW,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { createRequire } from \"node:module\";\n\nconst require = createRequire(import.meta.url);\n\n/**\n * The @jsquash codecs fetch their wasm from a file: URL, which Node's fetch\n * can't load, so read and compile it from disk instead.\n */\nexport async function loadWasm(\n specifier: string,\n): Promise<WebAssembly.Module | undefined> {\n const wasm = await readFile(require.resolve(specifier));\n return WebAssembly.compile(new Uint8Array(wasm));\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,78 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jimp/wasm-jxl",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"repository": "jimp-dev/jimp",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"lint": "eslint .",
|
|
13
|
+
"test": "vitest",
|
|
14
|
+
"test:browser": "vitest --config vitest.config.browser.mjs",
|
|
15
|
+
"build": "tshy",
|
|
16
|
+
"dev": "tshy --watch",
|
|
17
|
+
"clean": "rimraf node_modules .tshy .tshy-build dist .turbo"
|
|
18
|
+
},
|
|
19
|
+
"author": "Andrew Lisowski <lisowski54@gmail.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@jimp/config-eslint": "2.1.0",
|
|
23
|
+
"@jimp/config-typescript": "2.1.0",
|
|
24
|
+
"@jimp/config-vitest": "2.1.0",
|
|
25
|
+
"@jimp/core": "2.1.0",
|
|
26
|
+
"@jimp/js-jpeg": "2.1.0",
|
|
27
|
+
"@jimp/js-png": "2.1.0",
|
|
28
|
+
"@jimp/test-utils": "2.1.0",
|
|
29
|
+
"@jimp/types": "2.1.0",
|
|
30
|
+
"@types/node": "^22.20.5",
|
|
31
|
+
"@vitest/browser": "^5.1.1",
|
|
32
|
+
"eslint": "^10.15.0",
|
|
33
|
+
"tshy": "~4.0.0",
|
|
34
|
+
"typescript": "^7.0.2",
|
|
35
|
+
"vite-plugin-node-polyfills": "^0.28.0",
|
|
36
|
+
"vitest": "^5.1.1"
|
|
37
|
+
},
|
|
38
|
+
"tshy": {
|
|
39
|
+
"exclude": [
|
|
40
|
+
"**/*.test.ts"
|
|
41
|
+
],
|
|
42
|
+
"dialects": [
|
|
43
|
+
"esm"
|
|
44
|
+
],
|
|
45
|
+
"esmDialects": [
|
|
46
|
+
"browser"
|
|
47
|
+
],
|
|
48
|
+
"exports": {
|
|
49
|
+
"./package.json": "./package.json",
|
|
50
|
+
".": "./src/index.ts"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"exports": {
|
|
54
|
+
"./package.json": "./package.json",
|
|
55
|
+
".": {
|
|
56
|
+
"browser": {
|
|
57
|
+
"types": "./dist/browser/index.d.ts",
|
|
58
|
+
"default": "./dist/browser/index.js"
|
|
59
|
+
},
|
|
60
|
+
"import": {
|
|
61
|
+
"types": "./dist/esm/index.d.ts",
|
|
62
|
+
"default": "./dist/esm/index.js"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"type": "module",
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"sideEffects": false,
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@jsquash/jxl": "1.3.0",
|
|
73
|
+
"buffer": "^6.0.3",
|
|
74
|
+
"zod": "^4.6.7"
|
|
75
|
+
},
|
|
76
|
+
"module": "./dist/esm/index.js",
|
|
77
|
+
"gitHead": "8346e2329163e0c3ee26aaf8fe1001bac8b25a21"
|
|
78
|
+
}
|