@wasm-fmt/shfmt 0.2.0 → 0.2.2

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/README.md CHANGED
@@ -24,7 +24,6 @@ import { format } from "@wasm-fmt/shfmt";
24
24
  const source = `#!/bin/bash
25
25
  echo "hello world"
26
26
  `;
27
-
28
27
  const formatted = format(source);
29
28
  console.log(formatted);
30
29
  ```
@@ -44,6 +43,12 @@ const formatted = format(source, "script.sh", {
44
43
  });
45
44
  ```
46
45
 
46
+ ## Node.js < 22.19
47
+
48
+ ```JavaScript
49
+ import { format } from "@wasm-fmt/shfmt/node";
50
+ ```
51
+
47
52
  ## Web
48
53
 
49
54
  For web environments, you need to initialize WASM module manually:
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@wasm-fmt/shfmt",
3
3
  "description": "A wasm based shell script formatter",
4
4
  "author": "magic-akari <akari.ccino@gmail.com>",
5
- "version": "0.2.0",
5
+ "version": "0.2.2",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "wasm",
@@ -22,22 +22,23 @@
22
22
  "type": "module",
23
23
  "exports": {
24
24
  ".": {
25
- "types": "./shfmt_entry.d.ts",
26
- "webpack": "./shfmt_bundle.js",
27
- "node": "./shfmt_node.js",
25
+ "types": "./shfmt.d.ts",
26
+ "webpack": "./shfmt.js",
27
+ "deno": "./shfmt.js",
28
+ "module-sync": "./shfmt_node.js",
28
29
  "default": "./shfmt_esm.js"
29
30
  },
30
31
  "./esm": {
31
- "types": "./shfmt_entry.d.ts",
32
+ "types": "./shfmt.d.ts",
32
33
  "default": "./shfmt_esm.js"
33
34
  },
34
35
  "./node": {
35
- "types": "./shfmt_entry.d.ts",
36
+ "types": "./shfmt.d.ts",
36
37
  "default": "./shfmt_node.js"
37
38
  },
38
39
  "./bundler": {
39
- "types": "./shfmt_entry.d.ts",
40
- "default": "./shfmt_bundle.js"
40
+ "types": "./shfmt.d.ts",
41
+ "default": "./shfmt.js"
41
42
  },
42
43
  "./web": {
43
44
  "types": "./shfmt_web.d.ts",
@@ -1,3 +1,21 @@
1
+ /**
2
+ * A wasm based shell script formatter
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * ```javascript
7
+ * import { format } from "@wasm-fmt/shfmt";
8
+ *
9
+ * const source = `#!/bin/bash
10
+ * echo "hello world"
11
+ * `;
12
+ *
13
+ * const formatted = format(source);
14
+ * ```
15
+ *
16
+ * @module
17
+ */
18
+
1
19
  /**
2
20
  * Formats a shell script source code.
3
21
  * @param source - The shell script source code to format
package/shfmt.js CHANGED
@@ -1,61 +1,9 @@
1
- /**
2
- * @import * as WASM from "./shfmt.wasm"
3
- */
1
+ /* @ts-self-types="./shfmt.d.ts" */
2
+ import wasm from "./shfmt.wasm";
3
+ import { format as _format } from "./shfmt_binding.js";
4
4
 
5
- /**
6
- * @param {WASM} wasm
7
- * @param {string} source
8
- * @param {string} [path]
9
- * @param {object} [options]
10
- * @return {string}
11
- */
12
- export function format(wasm, source, path, options) {
13
- try {
14
- if (options) {
15
- writeStringToWasmMemory(wasm, JSON.stringify(options));
16
- wasm.set_options();
17
- }
5
+ wasm._initialize();
18
6
 
19
- if (path) {
20
- writeStringToWasmMemory(wasm, path);
21
- wasm.set_path();
22
- }
23
-
24
- writeStringToWasmMemory(wasm, source);
25
- const result = wasm.format();
26
- if (result === 0) {
27
- return source;
28
- }
29
-
30
- const ptr = wasm.output_ptr();
31
- const length = wasm.output_len();
32
- const text = readStringFromWasmMemory(wasm, ptr, length);
33
-
34
- if (result === 1) {
35
- return text;
36
- }
37
-
38
- throw new Error(text);
39
- } finally {
40
- wasm.dispose();
41
- }
7
+ export function format(source, path, options) {
8
+ return _format(wasm, source, path, options);
42
9
  }
43
-
44
- /**
45
- * @param {WASM} wasm
46
- * @param {string} str
47
- */
48
- function writeStringToWasmMemory(wasm, str) {
49
- const bytes = encoder.encode(str);
50
- const ptr = wasm.alloc(bytes.length);
51
- const memory = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
52
- memory.set(bytes);
53
- }
54
-
55
- function readStringFromWasmMemory(wasm, ptr, length) {
56
- const memory = new Uint8Array(wasm.memory.buffer, ptr, length);
57
- return decoder.decode(memory);
58
- }
59
-
60
- const encoder = new TextEncoder();
61
- const decoder = new TextDecoder();
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @import * as WASM from "./shfmt.wasm"
3
+ */
4
+
5
+ /**
6
+ * @param {WASM} wasm
7
+ * @param {string} source
8
+ * @param {string} [path]
9
+ * @param {object} [options]
10
+ * @return {string}
11
+ */
12
+ export function format(wasm, source, path, options) {
13
+ try {
14
+ if (options) {
15
+ writeStringToWasmMemory(wasm, JSON.stringify(options));
16
+ wasm.set_options();
17
+ }
18
+
19
+ if (path) {
20
+ writeStringToWasmMemory(wasm, path);
21
+ wasm.set_path();
22
+ }
23
+
24
+ writeStringToWasmMemory(wasm, source);
25
+ const result = wasm.format();
26
+ if (result === 0) {
27
+ return source;
28
+ }
29
+
30
+ const ptr = wasm.output_ptr();
31
+ const length = wasm.output_len();
32
+ const text = readStringFromWasmMemory(wasm, ptr, length);
33
+
34
+ if (result === 1) {
35
+ return text;
36
+ }
37
+
38
+ throw new Error(text);
39
+ } finally {
40
+ wasm.dispose();
41
+ }
42
+ }
43
+
44
+ /**
45
+ * @param {WASM} wasm
46
+ * @param {string} str
47
+ */
48
+ function writeStringToWasmMemory(wasm, str) {
49
+ const bytes = encoder.encode(str);
50
+ const ptr = wasm.alloc(bytes.length);
51
+ const memory = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
52
+ memory.set(bytes);
53
+ }
54
+
55
+ function readStringFromWasmMemory(wasm, ptr, length) {
56
+ const memory = new Uint8Array(wasm.memory.buffer, ptr, length);
57
+ return decoder.decode(memory);
58
+ }
59
+
60
+ const encoder = new TextEncoder();
61
+ const decoder = new TextDecoder();
package/shfmt_esm.js CHANGED
@@ -1,11 +1,7 @@
1
- /* @ts-self-types="./shfmt_entry.d.ts" */
2
- /**
3
- * Loads the Wasm module via source phase import.
4
- * @module
5
- */
1
+ /* @ts-self-types="./shfmt.d.ts" */
6
2
  // prettier-ignore
7
3
  import source wasmModule from "./shfmt.wasm";
8
- import { format as _format } from "./shfmt.js";
4
+ import { format as _format } from "./shfmt_binding.js";
9
5
  /**
10
6
  * @import * as WASM from "./shfmt.wasm"
11
7
  */
package/shfmt_node.js CHANGED
@@ -1,11 +1,6 @@
1
- /* @ts-self-types="./shfmt_entry.d.ts" */
2
- /**
3
- * Loads the Wasm module using Node's fs API.
4
- * Consider using `./esm` entry if your environment supports source phase import.
5
- * @module
6
- */
1
+ /* @ts-self-types="./shfmt.d.ts" */
7
2
  import { readFileSync } from "node:fs";
8
- import { format as _format } from "./shfmt.js";
3
+ import { format as _format } from "./shfmt_binding.js";
9
4
 
10
5
  const wasmUrl = new URL("shfmt.wasm", import.meta.url);
11
6
  const wasmBytes = readFileSync(wasmUrl);
package/shfmt_vite.js CHANGED
@@ -1,11 +1,7 @@
1
1
  /* @ts-self-types="./shfmt_web.d.ts" */
2
- /**
3
- * Loads the Wasm module for Vite and bundlers supporting `?init` imports.
4
- * @module
5
- */
6
2
  import init from "./shfmt.wasm?init";
7
3
  import initAsync from "./shfmt_web.js";
8
- import { format as _format } from "./shfmt.js";
4
+ import { format as _format } from "./shfmt_binding.js";
9
5
 
10
6
  let wasm, wasmModule;
11
7
 
package/shfmt_web.d.ts CHANGED
@@ -1,5 +1,35 @@
1
+ /**
2
+ * A wasm based shell script formatter
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * ```javascript
7
+ * import init, { format } from "@wasm-fmt/shfmt";
8
+ *
9
+ * await init();
10
+ *
11
+ * const source = `#!/bin/bash
12
+ * echo "hello world"
13
+ * `;
14
+ *
15
+ * const formatted = format(source);
16
+ * ```
17
+ *
18
+ * @module
19
+ */
20
+
21
+ /**
22
+ * Input types for asynchronous WASM initialization.
23
+ * Can be a URL/path to fetch, a Response object, raw bytes, or a pre-compiled WebAssembly.Module.
24
+ */
1
25
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
26
+
27
+ /**
28
+ * Input types for synchronous WASM initialization.
29
+ * Must be raw bytes (BufferSource) or a pre-compiled WebAssembly.Module.
30
+ */
2
31
  export type SyncInitInput = BufferSource | WebAssembly.Module;
32
+
3
33
  import type * as InitOutput from "./shfmt.wasm";
4
34
 
5
35
  /**
@@ -7,10 +37,11 @@ import type * as InitOutput from "./shfmt.wasm";
7
37
  * @param init_input - Optional URL/path to the WASM file, or any valid InitInput
8
38
  */
9
39
  export default function initAsync(init_input?: InitInput): Promise<InitOutput>;
40
+
10
41
  /**
11
42
  * Initializes the WASM module synchronously.
12
43
  * @param buffer_or_module - The WASM module or buffer source
13
44
  */
14
45
  export declare function initSync(buffer_or_module: BufferSource | WebAssembly.Module): InitOutput;
15
46
 
16
- export * from "shfmt_entry.d.ts";
47
+ export * from "./shfmt.d.ts";
package/shfmt_web.js CHANGED
@@ -1,10 +1,5 @@
1
1
  /* @ts-self-types="./shfmt_web.d.ts" */
2
- /**
3
- * Loads the Wasm module via Web Fetch API (browsers).
4
- * Requires calling init().
5
- * @module
6
- */
7
- import { format as _format } from "./shfmt.js";
2
+ import { format as _format } from "./shfmt_binding.js";
8
3
  let wasm, wasmModule;
9
4
 
10
5
  async function load(module, imports) {
package/shfmt_bundle.js DELETED
@@ -1,9 +0,0 @@
1
- /* @ts-self-types="./shfmt_entry.d.ts" */
2
- import wasm from "./shfmt.wasm";
3
- import { format as _format } from "./shfmt.js";
4
-
5
- wasm._initialize();
6
-
7
- export function format(source, path, options) {
8
- return _format(wasm, source, path, options);
9
- }