@wasm-fmt/gofmt 0.6.1 → 0.7.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/README.md CHANGED
@@ -32,6 +32,12 @@ const formatted = format(source);
32
32
  console.log(formatted);
33
33
  ```
34
34
 
35
+ ## Node.js < 22.19
36
+
37
+ ```JavaScript
38
+ import { format } from "@wasm-fmt/gofmt/node";
39
+ ```
40
+
35
41
  ## Web
36
42
 
37
43
  For web environments, you need to initialize WASM module manually:
package/gofmt.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A wasm based golang formatter
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * ```javascript
7
+ * import { format } from "@wasm-fmt/gofmt";
8
+ *
9
+ * const source = `
10
+ * package main
11
+ * import "fmt"
12
+ * func main(){fmt.Println("Hello, 世界")
13
+ * }
14
+ * `;
15
+ *
16
+ * const formatted = format(source);
17
+ * ```
18
+ *
19
+ * @module
20
+ */
21
+
22
+ /**
23
+ * Formats a Go source code.
24
+ * @param input - The Go source code to format
25
+ * @returns The formatted Go source code
26
+ */
27
+ export declare function format(input: string): string;
package/gofmt.js CHANGED
@@ -1,49 +1,9 @@
1
- /**
2
- * @import * as WASM from "./gofmt.wasm"
3
- */
1
+ /* @ts-self-types="./gofmt.d.ts" */
2
+ import wasm from "./gofmt.wasm";
3
+ import { format as _format } from "./gofmt_binding.js";
4
4
 
5
- /**
6
- * @param {WASM} wasm
7
- * @param {string} source
8
- * @return {string}
9
- */
10
- export function format(wasm, source) {
11
- try {
12
- writeStringToWasmMemory(wasm, source);
13
- const result = wasm.format();
14
- if (result === 0) {
15
- return source;
16
- }
5
+ wasm._initialize();
17
6
 
18
- const ptr = wasm.output_ptr();
19
- const length = wasm.output_len();
20
- const text = readStringFromWasmMemory(wasm, ptr, length);
21
-
22
- if (result === 1) {
23
- return text;
24
- }
25
-
26
- throw new Error(text);
27
- } finally {
28
- wasm.dispose();
29
- }
30
- }
31
-
32
- /**
33
- * @param {WASM} wasm
34
- * @param {string} str
35
- */
36
- function writeStringToWasmMemory(wasm, str) {
37
- const bytes = encoder.encode(str);
38
- const ptr = wasm.alloc(bytes.length);
39
- const memory = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
40
- memory.set(bytes);
7
+ export function format(source) {
8
+ return _format(wasm, source);
41
9
  }
42
-
43
- function readStringFromWasmMemory(wasm, ptr, length) {
44
- const memory = new Uint8Array(wasm.memory.buffer, ptr, length);
45
- return decoder.decode(memory);
46
- }
47
-
48
- const encoder = new TextEncoder();
49
- const decoder = new TextDecoder();
package/gofmt.wasm CHANGED
Binary file
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @import * as WASM from "./gofmt.wasm"
3
+ */
4
+
5
+ /**
6
+ * @param {WASM} wasm
7
+ * @param {string} source
8
+ * @return {string}
9
+ */
10
+ export function format(wasm, source) {
11
+ try {
12
+ writeStringToWasmMemory(wasm, source);
13
+ const result = wasm.format();
14
+ if (result === 0) {
15
+ return source;
16
+ }
17
+
18
+ const ptr = wasm.output_ptr();
19
+ const length = wasm.output_len();
20
+ const text = readStringFromWasmMemory(wasm, ptr, length);
21
+
22
+ if (result === 1) {
23
+ return text;
24
+ }
25
+
26
+ throw new Error(text);
27
+ } finally {
28
+ wasm.dispose();
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @param {WASM} wasm
34
+ * @param {string} str
35
+ */
36
+ function writeStringToWasmMemory(wasm, str) {
37
+ const bytes = encoder.encode(str);
38
+ const ptr = wasm.alloc(bytes.length);
39
+ const memory = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
40
+ memory.set(bytes);
41
+ }
42
+
43
+ function readStringFromWasmMemory(wasm, ptr, length) {
44
+ const memory = new Uint8Array(wasm.memory.buffer, ptr, length);
45
+ return decoder.decode(memory);
46
+ }
47
+
48
+ const encoder = new TextEncoder();
49
+ const decoder = new TextDecoder();
package/gofmt_esm.js CHANGED
@@ -1,11 +1,7 @@
1
- /* @ts-self-types="./gofmt_entry.d.ts" */
2
- /**
3
- * Loads the Wasm module via source phase import.
4
- * @module
5
- */
1
+ /* @ts-self-types="./gofmt.d.ts" */
6
2
  // prettier-ignore
7
3
  import source wasmModule from "./gofmt.wasm";
8
- import { format as _format } from "./gofmt.js";
4
+ import { format as _format } from "./gofmt_binding.js";
9
5
  /**
10
6
  * @import * as WASM from "./gofmt.wasm"
11
7
  */
package/gofmt_node.js CHANGED
@@ -1,11 +1,6 @@
1
- /* @ts-self-types="./gofmt_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="./gofmt.d.ts" */
7
2
  import { readFileSync } from "node:fs";
8
- import { format as _format } from "./gofmt.js";
3
+ import { format as _format } from "./gofmt_binding.js";
9
4
 
10
5
  const wasmUrl = new URL("gofmt.wasm", import.meta.url);
11
6
  const wasmBytes = readFileSync(wasmUrl);
package/gofmt_vite.js CHANGED
@@ -1,11 +1,7 @@
1
1
  /* @ts-self-types="./gofmt_web.d.ts" */
2
- /**
3
- * Loads the Wasm module for Vite and bundlers supporting `?init` imports.
4
- * @module
5
- */
6
2
  import init from "./gofmt.wasm?init";
7
3
  import initAsync from "./gofmt_web.js";
8
- import { format as _format } from "./gofmt.js";
4
+ import { format as _format } from "./gofmt_binding.js";
9
5
 
10
6
  let wasm, wasmModule;
11
7
 
package/gofmt_web.d.ts CHANGED
@@ -1,9 +1,38 @@
1
1
  /**
2
- * gofmt entry with initialization functions.
2
+ * A wasm based golang formatter
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * ```javascript
7
+ * import init, { format } from "@wasm-fmt/gofmt";
8
+ *
9
+ * await init();
10
+ *
11
+ * const source = `
12
+ * package main
13
+ * import "fmt"
14
+ * func main(){fmt.Println("Hello, 世界")
15
+ * }
16
+ * `;
17
+ *
18
+ * const formatted = format(source);
19
+ * ```
20
+ *
3
21
  * @module
4
22
  */
23
+
24
+ /**
25
+ * Input types for asynchronous WASM initialization.
26
+ * Can be a URL/path to fetch, a Response object, raw bytes, or a pre-compiled WebAssembly.Module.
27
+ */
5
28
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
29
+
30
+ /**
31
+ * Input types for synchronous WASM initialization.
32
+ * Must be raw bytes (BufferSource) or a pre-compiled WebAssembly.Module.
33
+ */
6
34
  export type SyncInitInput = BufferSource | WebAssembly.Module;
35
+
7
36
  import type * as InitOutput from "./gofmt.wasm";
8
37
 
9
38
  /**
@@ -17,4 +46,4 @@ export default function initAsync(init_input?: InitInput): Promise<InitOutput>;
17
46
  */
18
47
  export declare function initSync(buffer_or_module: BufferSource | WebAssembly.Module): InitOutput;
19
48
 
20
- export * from "./gofmt_entry.d.ts";
49
+ export * from "./gofmt.d.ts";
package/gofmt_web.js CHANGED
@@ -1,10 +1,5 @@
1
1
  /* @ts-self-types="./gofmt_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 "./gofmt.js";
2
+ import { format as _format } from "./gofmt_binding.js";
8
3
  let wasm, wasmModule;
9
4
 
10
5
  async function load(module, imports) {
@@ -55,32 +50,32 @@ function finalize_init(instance, module) {
55
50
  return wasm;
56
51
  }
57
52
 
58
- export function initSync(module) {
53
+ export function initSync(buffer_or_module) {
59
54
  if (wasm !== void 0) return wasm;
60
55
 
61
- if (!(module instanceof WebAssembly.Module)) {
62
- module = new WebAssembly.Module(module);
56
+ if (!(buffer_or_module instanceof WebAssembly.Module)) {
57
+ buffer_or_module = new WebAssembly.Module(buffer_or_module);
63
58
  }
64
- const instance = new WebAssembly.Instance(module);
65
- return finalize_init(instance, module);
59
+ const instance = new WebAssembly.Instance(buffer_or_module);
60
+ return finalize_init(instance, buffer_or_module);
66
61
  }
67
62
 
68
- export default async function initAsync(module_or_path) {
63
+ export default async function initAsync(init_input) {
69
64
  if (wasm !== void 0) return wasm;
70
65
 
71
- if (module_or_path === void 0) {
72
- module_or_path = new URL("gofmt.wasm", import.meta.url);
66
+ if (init_input === void 0) {
67
+ init_input = new URL("gofmt.wasm", import.meta.url);
73
68
  }
74
69
 
75
70
  if (
76
- typeof module_or_path === "string" ||
77
- (typeof Request === "function" && module_or_path instanceof Request) ||
78
- (typeof URL === "function" && module_or_path instanceof URL)
71
+ typeof init_input === "string" ||
72
+ (typeof Request === "function" && init_input instanceof Request) ||
73
+ (typeof URL === "function" && init_input instanceof URL)
79
74
  ) {
80
- module_or_path = fetch(module_or_path);
75
+ init_input = fetch(init_input);
81
76
  }
82
77
 
83
- const { instance, module } = await load(await module_or_path);
78
+ const { instance, module } = await load(await init_input);
84
79
 
85
80
  return finalize_init(instance, module);
86
81
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@wasm-fmt/gofmt",
3
3
  "description": "A wasm based golang formatter",
4
4
  "author": "magic-akari <akari.ccino@gamil.com>",
5
- "version": "0.6.1",
5
+ "version": "0.7.0",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "wasm",
@@ -20,22 +20,23 @@
20
20
  "type": "module",
21
21
  "exports": {
22
22
  ".": {
23
- "types": "./gofmt_entry.d.ts",
24
- "webpack": "./gofmt_bundle.js",
25
- "node": "./gofmt_node.js",
23
+ "types": "./gofmt.d.ts",
24
+ "webpack": "./gofmt.js",
25
+ "deno": "./gofmt.js",
26
+ "module-sync": "./gofmt_node.js",
26
27
  "default": "./gofmt_esm.js"
27
28
  },
28
29
  "./esm": {
29
- "types": "./gofmt_entry.d.ts",
30
+ "types": "./gofmt.d.ts",
30
31
  "default": "./gofmt_esm.js"
31
32
  },
32
33
  "./node": {
33
- "types": "./gofmt_entry.d.ts",
34
+ "types": "./gofmt.d.ts",
34
35
  "default": "./gofmt_node.js"
35
36
  },
36
37
  "./bundler": {
37
- "types": "./gofmt_entry.d.ts",
38
- "default": "./gofmt_bundle.js"
38
+ "types": "./gofmt.d.ts",
39
+ "default": "./gofmt.js"
39
40
  },
40
41
  "./web": {
41
42
  "types": "./gofmt_web.d.ts",
package/gofmt_bundle.js DELETED
@@ -1,16 +0,0 @@
1
- import wasm from "./gofmt.wasm";
2
- import { format as _format } from "./gofmt.js";
3
-
4
- wasm._initialize();
5
-
6
- export function initSync() {
7
- return wasm;
8
- }
9
-
10
- export default async function initAsync() {
11
- return wasm;
12
- }
13
-
14
- export function format(source) {
15
- return _format(wasm, source);
16
- }
package/gofmt_entry.d.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * gofmt default entry for formatting Go source code.
3
- * @module
4
- */
5
-
6
- /**
7
- * Formats a Go source code.
8
- * @param input - The Go source code to format
9
- * @returns The formatted Go source code
10
- */
11
- export declare function format(input: string): string;