@wasm-fmt/gofmt 0.3.1 → 0.4.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.
Files changed (4) hide show
  1. package/lib.d.ts +8 -1
  2. package/lib.js +55 -27
  3. package/package.json +46 -36
  4. package/vite.js +2 -2
package/lib.d.ts CHANGED
@@ -1,2 +1,9 @@
1
- export default function init(wasm_url?: string | URL): Promise<void>;
1
+ export type InitInput =
2
+ | RequestInfo
3
+ | URL
4
+ | Response
5
+ | BufferSource
6
+ | WebAssembly.Module;
7
+
8
+ export default function init(wasm_url?: InitInput): Promise<void>;
2
9
  export declare function format(input: string): string;
package/lib.js CHANGED
@@ -1,44 +1,72 @@
1
1
  import { Go } from "./go_wasm.js";
2
2
  const go = new Go();
3
3
 
4
- let inst;
4
+ let wasm;
5
5
 
6
- export default async function init(wasm_url) {
7
- if (inst) {
8
- return await inst;
6
+ export default async function init(input) {
7
+ if (wasm) {
8
+ await wasm;
9
+ return;
9
10
  }
10
11
 
11
- if (!wasm_url) {
12
- wasm_url = new URL("lib.wasm", import.meta.url);
12
+ wasm = load(input, go.importObject);
13
+ wasm = await wasm;
14
+ go.run(wasm.instance);
15
+ }
16
+
17
+ async function load(module, importObject) {
18
+ switch (typeof module) {
19
+ case "undefined":
20
+ module = "lib.wasm";
21
+ case "string":
22
+ module = new URL(module, import.meta.url);
23
+ }
24
+
25
+ if (module instanceof URL || module instanceof Request) {
26
+ if (
27
+ typeof __webpack_require__ !== "function" &&
28
+ module.protocol === "file:"
29
+ ) {
30
+ const fs = await import("node:fs/promises");
31
+ module = await fs.readFile(module);
32
+ } else {
33
+ module = await fetch(module);
34
+ }
13
35
  }
14
36
 
15
- if (typeof wasm_url === "string") {
16
- wasm_url = new URL(wasm_url);
37
+ if (module instanceof Response) {
38
+ if ("instantiateStreaming" in WebAssembly) {
39
+ try {
40
+ return await WebAssembly.instantiateStreaming(
41
+ module,
42
+ importObject
43
+ );
44
+ } catch (e) {
45
+ if (module.headers.get("Content-Type") != "application/wasm") {
46
+ console.warn(
47
+ "`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
48
+ e
49
+ );
50
+ } else {
51
+ throw e;
52
+ }
53
+ }
54
+ }
55
+
56
+ const bytes = await module.arrayBuffer();
57
+ return await WebAssembly.instantiate(bytes, importObject);
17
58
  }
59
+ const instance = await WebAssembly.instantiate(module, importObject);
18
60
 
19
- if (
20
- typeof __webpack_require__ !== "function" &&
21
- wasm_url.protocol === "file:"
22
- ) {
23
- inst = import("node:fs/promises")
24
- .then((fs) => fs.readFile(wasm_url))
25
- .then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
26
- } else if ("instantiateStreaming" in WebAssembly) {
27
- inst = WebAssembly.instantiateStreaming(
28
- fetch(wasm_url),
29
- go.importObject
30
- );
31
- } else {
32
- inst = fetch(wasm_url)
33
- .then((response) => response.arrayBuffer())
34
- .then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
61
+ if (instance instanceof WebAssembly.Instance) {
62
+ return { instance, module };
35
63
  }
36
- inst = (await inst).instance;
37
- go.run(inst);
64
+
65
+ return instance;
38
66
  }
39
67
 
40
68
  export function format(input) {
41
- const [err, result] = inst.format(input);
69
+ const [err, result] = wasm.instance.format(input);
42
70
  if (err) {
43
71
  throw new Error(result);
44
72
  }
package/package.json CHANGED
@@ -1,40 +1,50 @@
1
1
  {
2
- "name": "@wasm-fmt/gofmt",
3
- "author": "magic-akari <akari.ccino@gamil.com>",
4
- "version": "0.3.1",
5
- "description": "wasm based gofmt",
6
- "main": "lib.js",
7
- "types": "lib.d.ts",
8
- "type": "module",
9
- "exports": {
10
- ".": {
11
- "types": "./lib.d.ts",
12
- "default": "./lib.js"
2
+ "name": "@wasm-fmt/gofmt",
3
+ "description": "A wasm based golang formatter",
4
+ "author": "magic-akari <akari.ccino@gamil.com>",
5
+ "version": "0.4.0",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "wasm",
9
+ "golang",
10
+ "formatter"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/wasm-fmt/gofmt"
13
15
  },
14
- "./vite": {
15
- "types": "./lib.d.ts",
16
- "default": "./vite.js"
16
+ "homepage": "https://github.com/wasm-fmt/gofmt",
17
+ "bugs": {
18
+ "url": "https://github.com/wasm-fmt/gofmt/issues"
17
19
  },
18
- "./package.json": "./package.json",
19
- "./*": "./*"
20
- },
21
- "scripts": {
22
- "go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
23
- "patch": "git apply ./go_wasm.patch",
24
- "build": "tinygo build -o=lib.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
25
- "postbuild": "npm run go_wasm && npm run patch",
26
- "test": "node --test"
27
- },
28
- "engines": {
29
- "node": ">=16.17.0"
30
- },
31
- "keywords": [
32
- "wasm",
33
- "golang",
34
- "formatter"
35
- ],
36
- "license": "MIT",
37
- "publishConfig": {
38
- "access": "public"
39
- }
20
+ "type": "module",
21
+ "main": "lib.js",
22
+ "module": "lib.js",
23
+ "types": "lib.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./lib.d.ts",
27
+ "default": "./lib.js"
28
+ },
29
+ "./vite": {
30
+ "types": "./lib.d.ts",
31
+ "default": "./vite.js"
32
+ },
33
+ "./package.json": "./package.json",
34
+ "./*": "./*"
35
+ },
36
+ "scripts": {
37
+ "go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
38
+ "patch": "git apply ./go_wasm.patch",
39
+ "build": "tinygo build -o=lib.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
40
+ "postbuild": "npm run go_wasm && npm run patch",
41
+ "test": "node --test"
42
+ },
43
+ "engines": {
44
+ "node": ">=16.17.0"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public",
48
+ "provenance": true
49
+ }
40
50
  }
package/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import init from "./lib.js";
2
- import wasm from "./lib.wasm?url";
2
+ import wasm_url from "./lib.wasm?url";
3
3
 
4
- export default function __wbg_init(input = wasm) {
4
+ export default function (input = wasm_url) {
5
5
  return init(input);
6
6
  }
7
7