diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0f95f3..d20451b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//} - uses: actions/setup-node@v2 with: - node-version: '14.x' + node-version: '16.x' registry-url: 'https://registry.npmjs.org' - name: npm build run: deno run -A --no-check ./scripts/build_npm.ts ${{steps.get_tag_version.outputs.TAG_VERSION}} diff --git a/.gitignore b/.gitignore index 50d08d0..431cac4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -npm \ No newline at end of file +npm diff --git a/LICENSE b/LICENSE index f917d24..b641193 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2021 David Sherret +Copyright (c) 2020-2022 David Sherret Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 92889e2..042ac5c 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,10 @@ console.log(tsFormatter.formatText("file.ts", "const t = 5;")); ```ts import { createFromBuffer } from "@dprint/formatter"; +import { getBuffer } from "@dprint/json"; -const formatter = createFromBuffer(fs.readFileSync("./json.wasm")); +// or provide something like fs.readFileSync("./json.wasm") +const formatter = createFromBuffer(getBuffer()); console.log(formatter.formatText("test.json", "{test: 5}")); ``` diff --git a/bvm.json b/bvm.json index 8f986d3..dfad854 100644 --- a/bvm.json +++ b/bvm.json @@ -1,9 +1,9 @@ { "binaries": [ { - "path": "https://bvm.land/deno/1.14.3.json", - "checksum": "270150c4a72748bcb630da76d4402a0b4c99702f034cba62724f05e792116a9e", - "version": "1.14.3" + "path": "https://bvm.land/deno/1.17.2.json", + "checksum": "ca0aff221bb79cf83ac67a1abd568ed1390211589db251e148b560c7b0ea3fbe", + "version": "1.17.2" } ] } diff --git a/dprint.json b/dprint.json index efa3991..acc6d9f 100644 --- a/dprint.json +++ b/dprint.json @@ -14,8 +14,8 @@ "**/*-lock.json" ], "plugins": [ - "https://plugins.dprint.dev/typescript-0.57.0.wasm", - "https://plugins.dprint.dev/json-0.13.0.wasm", - "https://plugins.dprint.dev/markdown-0.10.0.wasm" + "https://plugins.dprint.dev/typescript-0.61.0.wasm", + "https://plugins.dprint.dev/json-0.14.0.wasm", + "https://plugins.dprint.dev/markdown-0.12.0.wasm" ] } diff --git a/mod.ts b/mod.ts index 21db6a2..391c713 100644 --- a/mod.ts +++ b/mod.ts @@ -1,7 +1,3 @@ -// Copyright 2020-2021 by David Sherret. All rights reserved. -// This work is licensed under the terms of the MIT license. -// For a copy, see . - /** Formats code. */ export interface Formatter { /** @@ -89,17 +85,40 @@ export function createImportObject(): WebAssembly.Imports { }; } +export interface ResponseLike { + arrayBuffer(): Promise; +} + /** * Creates a formatter from the specified streaming source. * @remarks This is the most efficient way to create a formatter. * @param response - The streaming source to create the formatter from. */ export function createStreaming( - response: Promise, + response: Promise, ): Promise { - return WebAssembly - .instantiateStreaming(response,createImportObject()) - .then((obj) => createFromInstance(obj.instance)); + if (typeof WebAssembly.instantiateStreaming === "function") { + return WebAssembly + // deno-lint-ignore no-explicit-any + .instantiateStreaming(response as any, createImportObject()) + .then((obj) => createFromInstance(obj.instance)); + } else { + // fallback for node.js + return getArrayBuffer() + .then((buffer) => createFromBuffer(buffer)); + } + + function getArrayBuffer() { + if (isResponse(response)) { + return response.arrayBuffer(); + } else { + return response.then((response) => response.arrayBuffer()); + } + + function isResponse(response: unknown): response is ResponseLike { + return (response as Response).arrayBuffer != null; + } + } } /** diff --git a/mod_test.ts b/mod_test.ts index 87f324b..a6d1ccf 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts"; +import { assertEquals } from "https://deno.land/std@0.113.0/testing/asserts.ts"; import { createFromBuffer, createStreaming, Formatter, GlobalConfiguration } from "./mod.ts"; Deno.test("it should create streaming", async () => { diff --git a/scripts/build_npm.ts b/scripts/build_npm.ts index 983b19a..afd9c1b 100644 --- a/scripts/build_npm.ts +++ b/scripts/build_npm.ts @@ -1,31 +1,43 @@ -import { build } from "https://deno.land/x/dnt@0.0.11/mod.ts"; - -await build({ - entryPoints: ["mod.ts"], - typeCheck: true, - test: false, - outDir: "./npm", - package: { - name: "@dprint/formatter", - version: Deno.args[0], - description: "Wasm formatter for dprint plugins.", - repository: { - type: "git", - url: "git+https://github.com/dprint/js-formatter.git", - }, - keywords: [ - "dprint", - "formatter", - "wasm", - ], - author: "David Sherret", - license: "MIT", - bugs: { - url: "https://github.com/dprint/js-formatter/issues", - }, - homepage: "https://github.com/dprint/js-formatter#readme", - }, -}); - -Deno.copyFileSync("LICENSE", "npm/LICENSE"); -Deno.copyFileSync("README.md", "npm/README.md"); +import { build } from "https://deno.land/x/dnt@0.14.0/mod.ts"; + +await build({ + entryPoints: ["mod.ts"], + typeCheck: true, + test: true, + outDir: "./npm", + shims: { + deno: { + test: "dev", + }, + customDev: [{ + globalNames: ["fetch"], + package: { + name: "undici", + version: "^4.12.1", + }, + }], + }, + package: { + name: "@dprint/formatter", + version: Deno.args[0], + description: "Wasm formatter for dprint plugins.", + repository: { + type: "git", + url: "git+https://github.com/dprint/js-formatter.git", + }, + keywords: [ + "dprint", + "formatter", + "wasm", + ], + author: "David Sherret", + license: "MIT", + bugs: { + url: "https://github.com/dprint/js-formatter/issues", + }, + homepage: "https://github.com/dprint/js-formatter#readme", + }, +}); + +Deno.copyFileSync("LICENSE", "npm/LICENSE"); +Deno.copyFileSync("README.md", "npm/README.md");