@wasm-fmt/gofmt 0.4.6 → 0.4.8

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
@@ -1,16 +1,23 @@
1
1
  [![Test](https://github.com/wasm-fmt/gofmt/actions/workflows/test.yml/badge.svg)](https://github.com/wasm-fmt/gofmt/actions/workflows/test.yml)
2
- [![npm](https://img.shields.io/npm/v/@wasm-fmt/gofmt)](https://www.npmjs.com/package/@wasm-fmt/gofmt)
3
2
 
4
3
  # Install
5
4
 
5
+ [![npm](https://img.shields.io/npm/v/@wasm-fmt/gofmt)](https://www.npmjs.com/package/@wasm-fmt/gofmt)
6
+
6
7
  ```bash
7
8
  npm install @wasm-fmt/gofmt
8
9
  ```
9
10
 
11
+ [![jsr.io](https://jsr.io/badges/@fmt/gofmt)](https://jsr.io/@fmt/gofmt)
12
+
13
+ ```bash
14
+ npx jsr add @fmt/gofmt
15
+ ```
16
+
10
17
  # Usage
11
18
 
12
19
  ```JavaScript
13
- import init, { format } from '@wasm-fmt/gofmt';
20
+ import init, { format } from "@wasm-fmt/gofmt";
14
21
 
15
22
  await init();
16
23
 
@@ -28,7 +35,7 @@ console.log(formatted);
28
35
  Vite users tip:
29
36
 
30
37
  ```JavaScript
31
- import init, { format } from '@wasm-fmt/gofmt/vite';
38
+ import init, { format } from "@wasm-fmt/gofmt/vite";
32
39
  ```
33
40
 
34
41
  # Build from source
package/gofmt.d.ts CHANGED
@@ -5,5 +5,6 @@ export type InitInput =
5
5
  | BufferSource
6
6
  | WebAssembly.Module;
7
7
 
8
- export default function init(wasm_url?: InitInput): Promise<void>;
8
+ export default function initAsync(wasm_url?: InitInput): Promise<void>;
9
+ export declare function initSync(module: BufferSource | WebAssembly.Module): void;
9
10
  export declare function format(input: string): string;
package/gofmt.js CHANGED
@@ -3,6 +3,7 @@
3
3
  // license that can be found in the LICENSE file.
4
4
  //
5
5
  // This file has been modified for use by the TinyGo compiler.
6
+
6
7
  const encoder = new TextEncoder("utf-8");
7
8
  const decoder = new TextDecoder("utf-8");
8
9
  let reinterpretBuf = new DataView(new ArrayBuffer(8));
@@ -114,7 +115,7 @@
114
115
  this.importObject = {
115
116
  wasi_snapshot_preview1: {
116
117
  // https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write
117
- fd_write() {},
118
+ fd_write: () => 0, // dummy
118
119
  },
119
120
  gojs: {
120
121
  // func ticks() float64
@@ -283,69 +284,69 @@
283
284
  };
284
285
  }
285
286
  }
287
+
286
288
  /**
287
289
  * ================== End of wasm_exec.js ==================
288
290
  */
289
291
  /**/let wasm;
290
- /**/export default async function init(input) {
291
- /**/ if (wasm) {
292
- /**/ await wasm;
293
- /**/ return;
294
- /**/ }
295
- /**/ const go = new Go();
296
- /**/ wasm = load(input, go.importObject);
297
- /**/ wasm = await wasm;
298
- /**/ go.run(wasm.instance);
299
- /**/}
300
- /**/
301
- /**/async function load(module, importObject) {
302
- /**/ switch (typeof module) {
303
- /**/ case "undefined":
304
- /**/ module = "gofmt.wasm";
305
- /**/ case "string":
306
- /**/ module = new URL(module, import.meta.url);
307
- /**/ }
308
- /**/ if (module instanceof URL || module instanceof Request) {
309
- /**/ if (
310
- /**/ typeof __webpack_require__ !== "function" &&
311
- /**/ module.protocol === "file:"
312
- /**/ ) {
313
- /**/ const fs = await import("node:fs/promises");
314
- /**/ module = await fs.readFile(module);
315
- /**/ } else {
316
- /**/ module = await fetch(module);
317
- /**/ }
318
- /**/ }
292
+ /**/async function __load(module, imports) {
319
293
  /**/ if (typeof Response === 'function' && module instanceof Response) {
320
- /**/ if ("instantiateStreaming" in WebAssembly) {
321
- /**/ try {
322
- /**/ return await WebAssembly.instantiateStreaming(
323
- /**/ module,
324
- /**/ importObject
325
- /**/ );
326
- /**/ } catch (e) {
327
- /**/ if (module.headers.get("Content-Type") != "application/wasm") {
328
- /**/ console.warn(
329
- /**/ "`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",
330
- /**/ e
331
- /**/ );
332
- /**/ } else {
333
- /**/ throw e;
334
- /**/ }
294
+ /**/ if (typeof WebAssembly.instantiateStreaming === 'function') {
295
+ /**/ try { return await WebAssembly.instantiateStreaming(module, imports); }
296
+ /**/ catch (e) {
297
+ /**/ if (module.headers.get('Content-Type') != 'application/wasm') {
298
+ /**/ console.warn("`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", e);
299
+ /**/ } else { throw e; }
335
300
  /**/ }
336
301
  /**/ }
337
302
  /**/ const bytes = await module.arrayBuffer();
338
- /**/ return await WebAssembly.instantiate(bytes, importObject);
339
- /**/ }
340
- /**/ const instance = await WebAssembly.instantiate(module, importObject);
341
- /**/ if (instance instanceof WebAssembly.Instance) {
342
- /**/ return { instance, module };
303
+ /**/ return await WebAssembly.instantiate(bytes, imports);
304
+ /**/ } else {
305
+ /**/ const instance = await WebAssembly.instantiate(module, imports);
306
+ /**/ if (instance instanceof WebAssembly.Instance) return { instance, module };
307
+ /**/ else return instance;
343
308
  /**/ }
344
- /**/ return instance;
345
309
  /**/}
310
+ /**/function __finalize_init(instance) {
311
+ /**/ return wasm = instance;
312
+ /**/}
313
+ /**/function __init_memory(imports, maybe_memory) { }
314
+ /**/export function initSync(module) {
315
+ /**/ if (wasm !== undefined) return wasm;
316
+ /**/
317
+ /**/ const go = new Go;
318
+ /**/ const imports = go.importObject;
319
+ /**/
320
+ /**/ __init_memory(imports);
346
321
  /**/
322
+ /**/ if (!(module instanceof WebAssembly.Module)) module = new WebAssembly.Module(module);
323
+ /**/
324
+ /**/ const instance = new WebAssembly.Instance(module, imports);
325
+ /**/
326
+ /**/ go.run(instance);
327
+ /**/ return __finalize_init(instance, module);
328
+ /**/}
329
+ /**/export default async function initAsync(input) {
330
+ /**/ if (wasm !== undefined) return wasm;
331
+ /**/
332
+ /**/ if (typeof input === 'undefined') input = new URL('gofmt.wasm', import.meta.url);
333
+ /**/
334
+ /**/ const go = new Go;
335
+ /**/ const imports = go.importObject;
336
+ /**/
337
+ /**/ if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
338
+ /**/ input = fetch(input);
339
+ /**/ }
340
+ /**/
341
+ /**/ __init_memory(imports);
342
+ /**/
343
+ /**/ const { instance, module } = await __load(await input, imports);
344
+ /**/
345
+ /**/ go.run(instance);
346
+ /**/ return __finalize_init(instance, module);
347
+ /**/}
347
348
  /**/export function format(input) {
348
- /**/ const [err, result] = wasm.instance.format(input);
349
+ /**/ const [err, result] = wasm.format(input);
349
350
  /**/ if (err) {
350
351
  /**/ throw new Error(result);
351
352
  /**/ }
package/gofmt.wasm CHANGED
Binary file
package/gofmt_node.js ADDED
@@ -0,0 +1,10 @@
1
+ import fs from "node:fs/promises";
2
+ import initAsync from "./gofmt.js";
3
+
4
+ const wasm = new URL("./gofmt.wasm", import.meta.url);
5
+
6
+ export default function (init = fs.readFile(wasm)) {
7
+ return initAsync(init);
8
+ }
9
+
10
+ export * from "./gofmt.js";
@@ -1,8 +1,8 @@
1
- import init from "./gofmt.js";
1
+ import initAsync from "./gofmt.js";
2
2
  import wasm_url from "./gofmt.wasm?url";
3
3
 
4
4
  export default function (input = wasm_url) {
5
- return init(input);
5
+ return initAsync(input);
6
6
  }
7
7
 
8
8
  export * from "./gofmt.js";
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.4.6",
5
+ "version": "0.4.8",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "wasm",
@@ -24,11 +24,12 @@
24
24
  "exports": {
25
25
  ".": {
26
26
  "types": "./gofmt.d.ts",
27
+ "node": "./gofmt_node.js",
27
28
  "default": "./gofmt.js"
28
29
  },
29
30
  "./vite": {
30
31
  "types": "./gofmt.d.ts",
31
- "default": "./vite.js"
32
+ "default": "./gofmt_vite.js"
32
33
  },
33
34
  "./package.json": "./package.json",
34
35
  "./*": "./*"
package/go.mod DELETED
@@ -1,3 +0,0 @@
1
- module github.com/wasm-fmt/gofmt
2
-
3
- go 1.20