format-png 0.1.2 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0
4
+
5
+ The WebAssembly module is now embedded in the package, and `init()` with no
6
+ argument uses it, in browsers, bundlers, Web Workers and Node alike. Nothing is
7
+ fetched: the package makes no network requests.
8
+
9
+ ### Breaking
10
+
11
+ - The `.wasm` file is no longer in the package, so the
12
+ `format-png/format_png_wasm_bg.wasm` export is gone.
13
+ - `init` no longer takes a URL or string to fetch the module from. It still
14
+ takes a compiled `WebAssembly.Module`.
15
+
16
+ ### Upgrading
17
+
18
+ - Node: replace `init(await readFile(...format_png_wasm_bg.wasm...))` with
19
+ `init()`.
20
+ - Browsers and bundlers: `init()` keeps working as before. With Vite,
21
+ `optimizeDeps: { exclude: ["format-png"] }` is no longer needed.
22
+ - If you passed a URL to `init`, call `init()` instead.
23
+
3
24
  ## 0.1.2
4
25
 
5
26
  - Built against the `format-png` crate 0.1.1. The API and behavior are unchanged.
package/README.md CHANGED
@@ -21,9 +21,7 @@ npm install format-png
21
21
 
22
22
  ## Load the module
23
23
 
24
- Call `init()` once before anything else.
25
-
26
- **Browsers and bundlers** (Vite, webpack, Rollup, esbuild): the `.wasm` file is fetched from next to the module.
24
+ Call `init()` once before anything else. It works the same in browsers, bundlers (Vite, webpack, Rollup, esbuild), Web Workers and Node 20.6 and later: the WebAssembly module is embedded in the package, so nothing is fetched and no bundler setup is needed.
27
25
 
28
26
  ```js
29
27
  import { init } from "format-png";
@@ -31,23 +29,7 @@ import { init } from "format-png";
31
29
  await init();
32
30
  ```
33
31
 
34
- With Vite, exclude the package from dependency pre-bundling, so the module stays next to its `.wasm` file:
35
-
36
- ```js
37
- // vite.config.js
38
- export default { optimizeDeps: { exclude: ["format-png"] } };
39
- ```
40
-
41
- **Node 20.6 and later**: pass the file's bytes.
42
-
43
- ```js
44
- import { readFile } from "node:fs/promises";
45
- import { init } from "format-png";
46
-
47
- await init(await readFile(new URL(import.meta.resolve("format-png/format_png_wasm_bg.wasm"))));
48
- ```
49
-
50
- `init` also takes a URL to fetch the module from, or a compiled `WebAssembly.Module`.
32
+ `init` also takes a compiled `WebAssembly.Module`, for example one a page compiled once and posted to its workers.
51
33
 
52
34
  ## Display a PNG
53
35
 
package/dist/index.d.ts CHANGED
@@ -300,17 +300,12 @@ export declare class PngError extends Error {
300
300
  * Loads the WebAssembly module. Call it once before anything else; later calls
301
301
  * return the same promise.
302
302
  *
303
- * In browsers and bundlers, call it with no argument: the `.wasm` file is
304
- * fetched from next to this module. In Node, pass its bytes:
305
- *
306
- * ```js
307
- * import { readFile } from "node:fs/promises";
308
- * await init(await readFile(new URL(import.meta.resolve("format-png/format_png_wasm_bg.wasm"))));
309
- * ```
310
- *
311
- * You can also pass a URL to fetch it from, or a compiled `WebAssembly.Module`.
303
+ * With no argument it uses the copy embedded in this package, in browsers,
304
+ * bundlers and Node alike: nothing is fetched. You can instead pass a
305
+ * `WebAssembly.Module` compiled from it, for example one a page compiled once
306
+ * and posted to its workers.
312
307
  */
313
- export declare function init(source?: BufferSource | WebAssembly.Module | URL | string): Promise<void>;
308
+ export declare function init(source?: BufferSource | WebAssembly.Module | Response | Promise<Response>): Promise<void>;
314
309
  /**
315
310
  * Decodes a PNG in its own format, without conversion: every bit depth and
316
311
  * color type as stored, with its palette and transparency. Pass the result to
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import initWasm, * as wasm from "./wasm/format_png_wasm.js";
2
+ import wasmBase64 from "./wasm/format_png_wasm_bg.wasm.base64.js";
2
3
  /** Thrown for malformed PNGs; `message` says what's wrong. */
3
4
  export class PngError extends Error {
4
5
  name = "PngError";
@@ -9,18 +10,13 @@ let initialized = false;
9
10
  * Loads the WebAssembly module. Call it once before anything else; later calls
10
11
  * return the same promise.
11
12
  *
12
- * In browsers and bundlers, call it with no argument: the `.wasm` file is
13
- * fetched from next to this module. In Node, pass its bytes:
14
- *
15
- * ```js
16
- * import { readFile } from "node:fs/promises";
17
- * await init(await readFile(new URL(import.meta.resolve("format-png/format_png_wasm_bg.wasm"))));
18
- * ```
19
- *
20
- * You can also pass a URL to fetch it from, or a compiled `WebAssembly.Module`.
13
+ * With no argument it uses the copy embedded in this package, in browsers,
14
+ * bundlers and Node alike: nothing is fetched. You can instead pass a
15
+ * `WebAssembly.Module` compiled from it, for example one a page compiled once
16
+ * and posted to its workers.
21
17
  */
22
18
  export function init(source) {
23
- ready ??= initWasm(source === undefined ? undefined : { module_or_path: source })
19
+ ready ??= initWasm({ module_or_path: source ?? decodeBase64(wasmBase64) })
24
20
  .then(() => {
25
21
  initialized = true;
26
22
  })
@@ -30,6 +26,13 @@ export function init(source) {
30
26
  });
31
27
  return ready;
32
28
  }
29
+ function decodeBase64(text) {
30
+ const binary = atob(text);
31
+ const bytes = new Uint8Array(binary.length);
32
+ for (let i = 0; i < binary.length; i++)
33
+ bytes[i] = binary.charCodeAt(i);
34
+ return bytes;
35
+ }
33
36
  function call(fn) {
34
37
  if (!initialized)
35
38
  throw new PngError("format-png: call init() first");
@@ -1501,13 +1501,10 @@ async function __wbg_init(module_or_path) {
1501
1501
  }
1502
1502
 
1503
1503
  if (module_or_path === undefined) {
1504
- module_or_path = new URL('format_png_wasm_bg.wasm', import.meta.url);
1504
+ throw new Error("format-png: no WebAssembly module given");
1505
1505
  }
1506
1506
  const imports = __wbg_get_imports();
1507
1507
 
1508
- if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
1509
- module_or_path = fetch(module_or_path);
1510
- }
1511
1508
 
1512
1509
  const { instance, module } = await __wbg_load(await module_or_path, imports);
1513
1510
 
@@ -0,0 +1,3 @@
1
+ /** `format_png_wasm_bg.wasm`, base64-encoded. */
2
+ declare const wasm: string;
3
+ export default wasm;