@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.
- package/lib.d.ts +8 -1
- package/lib.js +55 -27
- package/package.json +46 -36
- package/vite.js +2 -2
package/lib.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
export
|
|
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
|
|
4
|
+
let wasm;
|
|
5
5
|
|
|
6
|
-
export default async function init(
|
|
7
|
-
if (
|
|
8
|
-
|
|
6
|
+
export default async function init(input) {
|
|
7
|
+
if (wasm) {
|
|
8
|
+
await wasm;
|
|
9
|
+
return;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
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 (
|
|
16
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
64
|
+
|
|
65
|
+
return instance;
|
|
38
66
|
}
|
|
39
67
|
|
|
40
68
|
export function format(input) {
|
|
41
|
-
const [err, result] =
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
"homepage": "https://github.com/wasm-fmt/gofmt",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/wasm-fmt/gofmt/issues"
|
|
17
19
|
},
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
}
|