From bf0596b358afe6b3ddb52cf2e1f7e218ab3de08e Mon Sep 17 00:00:00 2001 From: benStre Date: Tue, 26 Nov 2024 23:44:02 +0100 Subject: [PATCH 01/11] Bump version to 0.0.1 --- deno.json | 2 +- rs-lib/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deno.json b/deno.json index f7755b7..48250c3 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@unyt/datex-core-js", - "version": "0.0.0", + "version": "0.0.1", "exports":"./src/datex.ts", "compilerOptions": { "noImplicitOverride":true, diff --git a/rs-lib/Cargo.toml b/rs-lib/Cargo.toml index f337b45..71c5dd5 100644 --- a/rs-lib/Cargo.toml +++ b/rs-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "datex-core-js" -version = "0.0.0" +version = "0.0.1" authors = ["Benedikt Strehle "] edition = "2021" From 5edbe18dd327d356c4582121560fb000ca8582ff Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Tue, 26 Nov 2024 23:54:23 +0100 Subject: [PATCH 02/11] update Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index cfe1682..c858b08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,7 +197,7 @@ dependencies = [ [[package]] name = "datex-core-js" -version = "0.0.0" +version = "0.0.1" dependencies = [ "datex-core", "lazy_static", From 8a305777dbe4b3c77a3b8e19e52afe45d8f375eb Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:02:26 +0100 Subject: [PATCH 03/11] run deno fmt --- .github/tools/create-release.ts | 8 +-- .github/workflows/create-release.yml | 90 ++++++++++++++-------------- .github/workflows/publish.yml | 8 +-- .github/workflows/release.yml | 28 ++++----- .github/workflows/test.yml | 4 +- .vscode/settings.json | 6 +- DEVELOP.md | 23 ++++--- README.md | 24 +++++--- deno.json | 31 +++++----- src/datex-core.ts | 2 +- src/mod.ts | 2 +- src/runtime/runtime.ts | 77 +++++++++++++----------- test/browser.html | 85 +++++++++++++------------- test/init.test.ts | 19 +++--- 14 files changed, 214 insertions(+), 193 deletions(-) diff --git a/.github/tools/create-release.ts b/.github/tools/create-release.ts index 4b72191..4edd13a 100644 --- a/.github/tools/create-release.ts +++ b/.github/tools/create-release.ts @@ -18,7 +18,6 @@ const denoJsonPath = "./deno.json"; const denoJsonText = await Deno.readTextFile(denoJsonPath); const denoJson = JSON.parse(denoJsonText); - // Extract versions from Cargo.toml and deno.json const cargoVersionRegex = /version\s*=\s*"(\d+)\.(\d+)\.(\d+)"/; const denoVersionRegex = /"version":\s*"(\d+\.\d+\.\d+)"/; @@ -40,7 +39,6 @@ if (denoVersion !== cargoVersion) { throw new Error("Version mismatch between Cargo.toml and deno.json"); } - switch (type) { case "major": major++; @@ -73,6 +71,8 @@ const updatedDenoJson = denoJsonText.replace( await Deno.writeTextFile(denoJsonPath, updatedDenoJson); // pass new version to the next step -await Deno.writeTextFile(ghOutput, `NEW_VERSION=${newVersion}`, { append: true }); +await Deno.writeTextFile(ghOutput, `NEW_VERSION=${newVersion}`, { + append: true, +}); -console.log(`Version updated to ${newVersion}`, ghOutput); \ No newline at end of file +console.log(`Version updated to ${newVersion}`, ghOutput); diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index ac3743d..3d73d78 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -1,54 +1,54 @@ name: Create release on: - workflow_dispatch: - inputs: - bump_type: - description: 'Select the type' - required: true - default: 'patch' - type: choice - options: - - patch - - minor - - major + workflow_dispatch: + inputs: + bump_type: + description: "Select the type" + required: true + default: "patch" + type: choice + options: + - patch + - minor + - major jobs: - version_bump: - permissions: - contents: write - pull-requests: write - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 + version_bump: + permissions: + contents: write + pull-requests: write + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 - - name: Set up Deno - uses: denoland/setup-deno@v2 - with: - deno-version: v2.x + - name: Set up Deno + uses: denoland/setup-deno@v2 + with: + deno-version: v2.x - - name: Bump version using Deno - id: bump_version - run: deno run -A .github/tools/create-release.ts ${{ inputs.bump_type }} + - name: Bump version using Deno + id: bump_version + run: deno run -A .github/tools/create-release.ts ${{ inputs.bump_type }} - # - name: Configure Git - # run: | - # git config user.name "github-actions[bot]" - # git config user.email "github-actions[bot]@users.noreply.github.com" + # - name: Configure Git + # run: | + # git config user.name "github-actions[bot]" + # git config user.email "github-actions[bot]@users.noreply.github.com" - - name: Create Pull Request - uses: peter-evans/create-pull-request@v5 - env: - NEW_VERSION: ${{ steps.bump_version.outputs.NEW_VERSION }} - with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: "Bump version to ${{ env.NEW_VERSION }}" - title: "Release (${{ env.NEW_VERSION }})" - body: "This PR bumps the Cargo.toml and deno.json version to ${{ env.NEW_VERSION }}" - draft: false - branch: "release/${{ env.NEW_VERSION }}" - delete-branch: true - labels: | - release - reviewers: jonasstrehle,benStre + - name: Create Pull Request + uses: peter-evans/create-pull-request@v5 + env: + NEW_VERSION: ${{ steps.bump_version.outputs.NEW_VERSION }} + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "Bump version to ${{ env.NEW_VERSION }}" + title: "Release (${{ env.NEW_VERSION }})" + body: "This PR bumps the Cargo.toml and deno.json version to ${{ env.NEW_VERSION }}" + draft: false + branch: "release/${{ env.NEW_VERSION }}" + delete-branch: true + labels: | + release + reviewers: jonasstrehle,benStre diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f9cd061..44c6079 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -6,11 +6,11 @@ jobs: runs-on: ubuntu-latest name: Publish permissions: - contents: read - id-token: write + contents: read + id-token: write steps: - uses: actions/checkout@v4 - + # publish to JSR - name: Publish package - run: npx jsr publish \ No newline at end of file + run: npx jsr publish diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0fad6c7..cd53e6b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,33 +1,33 @@ on: push: - branches: - - main + branches: + - main jobs: release: runs-on: ubuntu-latest name: Release steps: - uses: actions/checkout@v3 - + - name: Extract version from Cargo.toml id: get_version run: | - VERSION=$(grep -E '^version\s*=\s*".*"' ./rs-lib/Cargo.toml | sed 's/version = "\(.*\)"/\1/') - echo "Version found: $VERSION" - echo "version=$VERSION" >> $GITHUB_ENV + VERSION=$(grep -E '^version\s*=\s*".*"' ./rs-lib/Cargo.toml | sed 's/version = "\(.*\)"/\1/') + echo "Version found: $VERSION" + echo "version=$VERSION" >> $GITHUB_ENV - name: Create Tag id: create_tag run: | - git config user.name "github-actions" - git config user.email "github-actions@github.com" - git tag "v${{ env.version }}" - git push origin "v${{ env.version }}" + git config user.name "github-actions" + git config user.email "github-actions@github.com" + git tag "v${{ env.version }}" + git push origin "v${{ env.version }}" - name: Generate Release Notes uses: ncipollo/release-action@v1 with: - token: ${{ secrets.GITHUB_TOKEN }} - tag: "v${{ env.version }}" - generateReleaseNotes: true - draft: true \ No newline at end of file + token: ${{ secrets.GITHUB_TOKEN }} + tag: "v${{ env.version }}" + generateReleaseNotes: true + draft: true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fac20ad..dabb028 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@v4 - uses: denoland/setup-deno@v2 with: - deno-version: v2.x + deno-version: v2.x - name: Check formatting run: deno fmt --check @@ -18,4 +18,4 @@ jobs: run: deno lint - name: Run Tests - run: deno test -A \ No newline at end of file + run: deno test -A diff --git a/.vscode/settings.json b/.vscode/settings.json index 0006751..be67fe4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - "deno.enable": true, - "deno.unstable": true -} \ No newline at end of file + "deno.enable": true, + "deno.unstable": true +} diff --git a/DEVELOP.md b/DEVELOP.md index fb7368f..560fbb5 100644 --- a/DEVELOP.md +++ b/DEVELOP.md @@ -2,11 +2,14 @@ ## Building the rust library -The rust adapter code can be found in the `rs-lib` directory, the generated WASM and JS glue code in the `src/datex-core` directory. +The rust adapter code can be found in the `rs-lib` directory, the generated WASM +and JS glue code in the `src/datex-core` directory. -The `rs-lib` directory contains the `datex-core` submodule, which contains the complete [DATEX Core](https://github.com/unyt-org/datex-core.git) library. +The `rs-lib` directory contains the `datex-core` submodule, which contains the +complete [DATEX Core](https://github.com/unyt-org/datex-core.git) library. -To generate new WASM and JS glue code for the rust library located in `rs-lib`, run `deno task build`. +To generate new WASM and JS glue code for the rust library located in `rs-lib`, +run `deno task build`. Rust nightly is required for coroutines: @@ -17,12 +20,14 @@ rustup default nightly ## Testing -The JS runtime can be tested by running `deno task test`. -This compiles the rust library, generates the WASM and JS glue code, and runs all tests in the `test` directory. -If you only want to run the tests without rebuilding the rust library, you can run `deno task test-no-build`. +The JS runtime can be tested by running `deno task test`. This compiles the rust +library, generates the WASM and JS glue code, and runs all tests in the `test` +directory. If you only want to run the tests without rebuilding the rust +library, you can run `deno task test-no-build`. ## Running in the browser -You can test the library in the browser by running `deno task serve`. -Now, you can open `http://localhost:8042/test/browser.html` in your browser. -A new Datex runtime instance is automatically created and can be accessed in the developer console via the global `Datex` variable. \ No newline at end of file +You can test the library in the browser by running `deno task serve`. Now, you +can open `http://localhost:8042/test/browser.html` in your browser. A new Datex +runtime instance is automatically created and can be accessed in the developer +console via the global `Datex` variable. diff --git a/README.md b/README.md index 97d455d..f7a8733 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,25 @@ -The DATEX Core Library for JavaScript (DATEX Core JS) is based on the [DATEX Core](https://github.com/unyt-org/datex-core) rust library and provides a high-level API for working with DATEX in JavaScript and TypeScript. -DATEX is developed and maintained by the [unyt.org](https://unyt.org) organization and community. - -You can find the legacy version of the DATEX Core library (implemented in TypeScript) at [unyt-org/datex-core-js-legacy](https://github.com/unyt-org/datex-core-js-legacy). -The legacy version is still actively maintained and supported until this library reaches feature parity. +The DATEX Core Library for JavaScript (DATEX Core JS) is based on the +[DATEX Core](https://github.com/unyt-org/datex-core) rust library and provides a +high-level API for working with DATEX in JavaScript and TypeScript. DATEX is +developed and maintained by the [unyt.org](https://unyt.org) organization and +community. + +You can find the legacy version of the DATEX Core library (implemented in +TypeScript) at +[unyt-org/datex-core-js-legacy](https://github.com/unyt-org/datex-core-js-legacy). +The legacy version is still actively maintained and supported until this library +reaches feature parity. ## Contributing -We welcome every contribution!
-Please take a look at the [development guidelines](./DEVELOP.md) and the unyt.org [contribution guidlines](https://github.com/unyt-org/.github/blob/main/CONTRIBUTING.md). - +We welcome every contribution!
Please take a look at the +[development guidelines](./DEVELOP.md) and the unyt.org +[contribution guidlines](https://github.com/unyt-org/.github/blob/main/CONTRIBUTING.md). [Twitter badge]: https://img.shields.io/twitter/follow/unytorg.svg?style=social&label=Follow [Twitter link]: https://twitter.com/intent/follow?screen_name=unytorg [Discord badge]: https://img.shields.io/discord/928247036770390016?logo=discord&style=social -[Discord link]: https://unyt.org/discord \ No newline at end of file +[Discord link]: https://unyt.org/discord diff --git a/deno.json b/deno.json index 7806ab9..e3d10bd 100644 --- a/deno.json +++ b/deno.json @@ -1,15 +1,18 @@ { - "name": "@unyt/datex", - "version": "0.0.1", - "exports":"./src/mod.ts", - "compilerOptions": { - "noImplicitOverride":true, - "lib": ["deno.window", "dom"] - }, - "tasks": { - "build": "deno run -A jsr:@deno/wasmbuild@0.17.3 -p datex-core-js --out ./src/datex-core", - "test": "deno task build && deno test -A", - "test-no-build": "deno test -A", - "serve": "deno run -A https://raw.githubusercontent.com/unyt-org/ts-file-server/main/src/run.ts -w --minify=false --port 8042" - } -} \ No newline at end of file + "name": "@unyt/datex", + "version": "0.0.1", + "exports": "./src/mod.ts", + "compilerOptions": { + "noImplicitOverride": true, + "lib": ["deno.window", "dom"] + }, + "tasks": { + "build": "deno run -A jsr:@deno/wasmbuild@0.17.3 -p datex-core-js --out ./src/datex-core", + "test": "deno task build && deno test -A", + "test-no-build": "deno test -A", + "serve": "deno run -A https://raw.githubusercontent.com/unyt-org/ts-file-server/main/src/run.ts -w --minify=false --port 8042" + }, + "fmt": { + "indentWidth": 4 + } +} diff --git a/src/datex-core.ts b/src/datex-core.ts index bb4f47a..b0600a6 100644 --- a/src/datex-core.ts +++ b/src/datex-core.ts @@ -1,3 +1,3 @@ import { instantiate } from "./datex-core/datex_core_js.generated.js"; export * from "./datex-core/datex_core_js.generated.js"; -await instantiate(); \ No newline at end of file +await instantiate(); diff --git a/src/mod.ts b/src/mod.ts index bc70159..720a303 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -3,4 +3,4 @@ import { Runtime } from "./runtime/runtime.ts"; /** * The default instance of the Datex runtime. */ -export const Datex = new Runtime(); \ No newline at end of file +export const Datex = new Runtime(); diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 0632818..6515332 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -1,41 +1,46 @@ import { init_runtime } from "../datex-core.ts"; -import type { JSRuntime, JSMemory } from "../datex-core/datex_core_js.generated.d.ts"; - +import type { + JSMemory, + JSRuntime, +} from "../datex-core/datex_core_js.generated.d.ts"; // get version from deno.json -const VERSION = globalThis.Deno ? - // Deno - await Deno.readTextFile(new URL("../../deno.json", import.meta.url)).then(JSON.parse).then((data: { version: string }) => data.version) : - // browser - await fetch(new URL("../../deno.json", import.meta.url)).then((res) => res.json()).then((data: { version: string }) => data.version); +const VERSION = globalThis.Deno + // Deno + ? await Deno.readTextFile(new URL("../../deno.json", import.meta.url)).then( + JSON.parse, + ).then((data: { version: string }) => data.version) + // browser + : await fetch(new URL("../../deno.json", import.meta.url)).then((res) => + res.json() + ).then((data: { version: string }) => data.version); export class Runtime { - public readonly js_version = VERSION; - - #runtime: JSRuntime; - #memory: JSMemory; - - /** - * properties from #runtime - */ - get version() { - return this.#runtime.version; - } - - get memory() { - return this.#memory; - } - - /** - * @internal only used for debugging - */ - get _runtime() { - return this.#runtime; - } - - constructor() { - this.#runtime = init_runtime(); - this.#memory = this.#runtime.memory; - } - -} \ No newline at end of file + public readonly js_version = VERSION; + + #runtime: JSRuntime; + #memory: JSMemory; + + /** + * properties from #runtime + */ + get version() { + return this.#runtime.version; + } + + get memory() { + return this.#memory; + } + + /** + * @internal only used for debugging + */ + get _runtime() { + return this.#runtime; + } + + constructor() { + this.#runtime = init_runtime(); + this.#memory = this.#runtime.memory; + } +} diff --git a/test/browser.html b/test/browser.html index a94be81..c853ddd 100644 --- a/test/browser.html +++ b/test/browser.html @@ -1,46 +1,45 @@ - - - Datex Core JS Test Page - - - - - - + + + Datex Core JS Test Page + + + + + + -
-

Datex Core JS Test Page

-

Please open the developer console

-
- - - - \ No newline at end of file +
+

Datex Core JS Test Page

+

Please open the developer console

+
+ + + diff --git a/test/init.test.ts b/test/init.test.ts index 86cd483..13f0777 100644 --- a/test/init.test.ts +++ b/test/init.test.ts @@ -6,12 +6,15 @@ import { Runtime } from "../src/runtime/runtime.ts"; * match the expected versions defined in deno.json and Cargo.toml */ Deno.test("runtime version", async () => { + const actual_js_version = await Deno.readTextFile( + new URL("../deno.json", import.meta.url), + ).then(JSON.parse).then((data: { version: string }) => data.version); + const actual_version = await Deno.readTextFile( + new URL("../rs-lib/Cargo.toml", import.meta.url), + ).then((data) => data.match(/version\s*=\s*"?([^"]*)"?$/m)?.[1]); - const actual_js_version = await Deno.readTextFile(new URL("../deno.json", import.meta.url)).then(JSON.parse).then((data: { version: string }) => data.version); - const actual_version = await Deno.readTextFile(new URL("../rs-lib/Cargo.toml", import.meta.url)).then((data) => data.match(/version\s*=\s*"?([^"]*)"?$/m)?.[1]); - - const runtime = new Runtime(); - assertEquals(runtime.js_version, actual_js_version); - assertEquals(runtime.version, actual_version); - console.log(runtime) -}); \ No newline at end of file + const runtime = new Runtime(); + assertEquals(runtime.js_version, actual_js_version); + assertEquals(runtime.version, actual_version); + console.log(runtime); +}); From 524ffbfea7ce15c638869fadfdfd8776281f7b96 Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:03:27 +0100 Subject: [PATCH 04/11] exclude datex-core from formatting --- deno.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deno.json b/deno.json index e3d10bd..a3acf7a 100644 --- a/deno.json +++ b/deno.json @@ -13,6 +13,7 @@ "serve": "deno run -A https://raw.githubusercontent.com/unyt-org/ts-file-server/main/src/run.ts -w --minify=false --port 8042" }, "fmt": { - "indentWidth": 4 + "indentWidth": 4, + "exclude": ["./rs-lib/datex-core"] } } From fc082af64aefdd7c2f57bfa46297191e16345e4f Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:05:52 +0100 Subject: [PATCH 05/11] fix deno lint --- src/mod.ts | 2 +- src/runtime/runtime.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mod.ts b/src/mod.ts index 720a303..9eed834 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -3,4 +3,4 @@ import { Runtime } from "./runtime/runtime.ts"; /** * The default instance of the Datex runtime. */ -export const Datex = new Runtime(); +export const Datex: Runtime = new Runtime(); diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 6515332..eab2d33 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -5,7 +5,7 @@ import type { } from "../datex-core/datex_core_js.generated.d.ts"; // get version from deno.json -const VERSION = globalThis.Deno +const VERSION: string = globalThis.Deno // Deno ? await Deno.readTextFile(new URL("../../deno.json", import.meta.url)).then( JSON.parse, @@ -24,18 +24,18 @@ export class Runtime { /** * properties from #runtime */ - get version() { + get version(): string { return this.#runtime.version; } - get memory() { + get memory(): JSMemory { return this.#memory; } /** * @internal only used for debugging */ - get _runtime() { + get _runtime(): JSRuntime { return this.#runtime; } From 434b7b8c757dc9494646cf9135872d303654349d Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:07:32 +0100 Subject: [PATCH 06/11] add js files, allow in gitignore --- .gitignore | 4 - src/datex-core/datex_core_js.generated.js | 671 ++++++++++++++++++++++ 2 files changed, 671 insertions(+), 4 deletions(-) create mode 100644 src/datex-core/datex_core_js.generated.js diff --git a/.gitignore b/.gitignore index 1c25da2..81826e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ .dx .datex-cache -**/*.js -*.js -**/*.js.map -*.js.map target diff --git a/src/datex-core/datex_core_js.generated.js b/src/datex-core/datex_core_js.generated.js new file mode 100644 index 0000000..195beb2 --- /dev/null +++ b/src/datex-core/datex_core_js.generated.js @@ -0,0 +1,671 @@ +// @generated file from wasmbuild -- do not edit +// @ts-nocheck: generated +// deno-lint-ignore-file +// deno-fmt-ignore-file +/// + +// source-hash: 643c1630bc945a8e1cd0ef23ab4153fb5eda9b77 +let wasm; + +const heap = new Array(128).fill(undefined); + +heap.push(undefined, null, true, false); + +function getObject(idx) { + return heap[idx]; +} + +let heap_next = heap.length; + +function dropObject(idx) { + if (idx < 132) return; + heap[idx] = heap_next; + heap_next = idx; +} + +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; +} + +const cachedTextDecoder = typeof TextDecoder !== "undefined" + ? new TextDecoder("utf-8", { ignoreBOM: true, fatal: true }) + : { + decode: () => { + throw Error("TextDecoder not available"); + }, + }; + +if (typeof TextDecoder !== "undefined") cachedTextDecoder.decode(); + +let cachedUint8Memory0 = null; + +function getUint8Memory0() { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} + +function addHeapObject(obj) { + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; + + heap[idx] = obj; + return idx; +} + +let cachedInt32Memory0 = null; + +function getInt32Memory0() { + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachedInt32Memory0; +} + +let cachedUint32Memory0 = null; + +function getUint32Memory0() { + if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) { + cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer); + } + return cachedUint32Memory0; +} + +function getArrayJsValueFromWasm0(ptr, len) { + ptr = ptr >>> 0; + const mem = getUint32Memory0(); + const slice = mem.subarray(ptr / 4, ptr / 4 + len); + const result = []; + for (let i = 0; i < slice.length; i++) { + result.push(takeObject(slice[i])); + } + return result; +} +/** + * @returns {JSRuntime} + */ +export function init_runtime() { + const ret = wasm.init_runtime(); + return JSRuntime.__wrap(ret); +} + +let WASM_VECTOR_LEN = 0; + +const cachedTextEncoder = typeof TextEncoder !== "undefined" + ? new TextEncoder("utf-8") + : { + encode: () => { + throw Error("TextEncoder not available"); + }, + }; + +const encodeString = function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +}; + +function passStringToWasm0(arg, malloc, realloc) { + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} +/** + * @param {string} datex_script + */ +export function compile(datex_script) { + const ptr0 = passStringToWasm0( + datex_script, + wasm.__wbindgen_malloc, + wasm.__wbindgen_realloc, + ); + const len0 = WASM_VECTOR_LEN; + wasm.compile(ptr0, len0); +} + +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8Memory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} +/** + * @param {Uint8Array} dxb + * @param {boolean} formatted + * @param {boolean} colorized + * @param {boolean} resolve_slots + * @returns {string} + */ +export function decompile(dxb, formatted, colorized, resolve_slots) { + let deferred2_0; + let deferred2_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passArray8ToWasm0(dxb, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.decompile(retptr, ptr0, len0, formatted, colorized, resolve_slots); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred2_0 = r0; + deferred2_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +const JSMemoryFinalization = (typeof FinalizationRegistry === "undefined") + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jsmemory_free(ptr >>> 0)); +/** */ +export class JSMemory { + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSMemory.prototype); + obj.__wbg_ptr = ptr; + JSMemoryFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSMemoryFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jsmemory_free(ptr); + } + /** + * @param {Uint8Array} address + * @returns {JSPointer | undefined} + */ + get_pointer_by_id(address) { + const ret = wasm.jsmemory_get_pointer_by_id( + this.__wbg_ptr, + addHeapObject(address), + ); + return ret === 0 ? undefined : JSPointer.__wrap(ret); + } + /** + * @returns {(Uint8Array)[]} + */ + get_pointer_ids() { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jsmemory_get_pointer_ids(retptr, this.__wbg_ptr); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4, 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } + } +} + +const JSPointerFinalization = (typeof FinalizationRegistry === "undefined") + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jspointer_free(ptr >>> 0)); +/** */ +export class JSPointer { + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSPointer.prototype); + obj.__wbg_ptr = ptr; + JSPointerFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSPointerFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jspointer_free(ptr); + } +} + +const JSRuntimeFinalization = (typeof FinalizationRegistry === "undefined") + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jsruntime_free(ptr >>> 0)); +/** */ +export class JSRuntime { + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSRuntime.prototype); + obj.__wbg_ptr = ptr; + JSRuntimeFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSRuntimeFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jsruntime_free(ptr); + } + /** + * @returns {string} + */ + get version() { + let deferred1_0; + let deferred1_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jsruntime_version(retptr, this.__wbg_ptr); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred1_0 = r0; + deferred1_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * @returns {JSMemory} + */ + get memory() { + const ret = wasm.jsruntime_memory(this.__wbg_ptr); + return JSMemory.__wrap(ret); + } +} + +const imports = { + __wbindgen_placeholder__: { + __wbindgen_object_drop_ref: function (arg0) { + takeObject(arg0); + }, + __wbindgen_string_new: function (arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }, + __wbg_log_5bb5f88f245d7762: function (arg0) { + console.log(getObject(arg0)); + }, + __wbg_buffer_12d079cc21e14bdb: function (arg0) { + const ret = getObject(arg0).buffer; + return addHeapObject(ret); + }, + __wbg_newwithbyteoffsetandlength_aa4a17c33a06e5cb: function ( + arg0, + arg1, + arg2, + ) { + const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0); + return addHeapObject(ret); + }, + __wbg_new_63b92bc8671ed464: function (arg0) { + const ret = new Uint8Array(getObject(arg0)); + return addHeapObject(ret); + }, + __wbg_set_a47bac70306a19a7: function (arg0, arg1, arg2) { + getObject(arg0).set(getObject(arg1), arg2 >>> 0); + }, + __wbg_length_c20a40f15020d68a: function (arg0) { + const ret = getObject(arg0).length; + return ret; + }, + __wbindgen_throw: function (arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }, + __wbindgen_memory: function () { + const ret = wasm.memory; + return addHeapObject(ret); + }, + }, +}; + +class WasmBuildLoader { + #options; + #lastLoadPromise; + #instantiated; + + constructor(options) { + this.#options = options; + } + + get instance() { + return this.#instantiated?.instance; + } + + get module() { + return this.#instantiated?.module; + } + + load( + url, + decompress, + ) { + if (this.#instantiated) { + return Promise.resolve(this.#instantiated); + } else if (this.#lastLoadPromise == null) { + this.#lastLoadPromise = (async () => { + try { + this.#instantiated = await this.#instantiate(url, decompress); + return this.#instantiated; + } finally { + this.#lastLoadPromise = undefined; + } + })(); + } + return this.#lastLoadPromise; + } + + async #instantiate(url, decompress) { + const imports = this.#options.imports; + if (this.#options.cache != null && url.protocol !== "file:") { + try { + const result = await this.#options.cache( + url, + decompress ?? ((bytes) => bytes), + ); + if (result instanceof URL) { + url = result; + decompress = undefined; // already decompressed + } else if (result != null) { + return WebAssembly.instantiate(result, imports); + } + } catch { + // ignore if caching ever fails (ex. when on deploy) + } + } + + const isFile = url.protocol === "file:"; + + // make file urls work in Node via dnt + const isNode = globalThis.process?.versions?.node != null; + if (isFile && typeof Deno !== "object") { + throw new Error( + "Loading local files are not supported in this environment", + ); + } + if (isNode && isFile) { + // the deno global will be shimmed by dnt + const wasmCode = await Deno.readFile(url); + return WebAssembly.instantiate( + decompress ? decompress(wasmCode) : wasmCode, + imports, + ); + } + + switch (url.protocol) { + case "file:": + case "https:": + case "http:": { + const wasmResponse = await fetchWithRetries(url); + if (decompress) { + const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer()); + return WebAssembly.instantiate(decompress(wasmCode), imports); + } + if ( + isFile || + wasmResponse.headers.get("content-type")?.toLowerCase() + .startsWith("application/wasm") + ) { + return WebAssembly.instantiateStreaming(wasmResponse, imports); + } else { + return WebAssembly.instantiate( + await wasmResponse.arrayBuffer(), + imports, + ); + } + } + default: + throw new Error(`Unsupported protocol: ${url.protocol}`); + } + } +} +const isNodeOrDeno = typeof Deno === "object" || + (typeof process !== "undefined" && process.versions != null && + process.versions.node != null); + +const loader = new WasmBuildLoader({ + imports, + cache: isNodeOrDeno ? cacheToLocalDir : undefined, +}); + +export async function instantiate(opts) { + return (await instantiateWithInstance(opts)).exports; +} + +export async function instantiateWithInstance(opts) { + const { instance } = await loader.load( + opts?.url ?? new URL("datex_core_js_bg.wasm", import.meta.url), + opts?.decompress, + ); + wasm = wasm ?? instance.exports; + cachedInt32Memory0 = cachedInt32Memory0 ?? new Int32Array(wasm.memory.buffer); + cachedUint8Memory0 = cachedUint8Memory0 ?? new Uint8Array(wasm.memory.buffer); + return { + instance, + exports: getWasmInstanceExports(), + }; +} + +function getWasmInstanceExports() { + return { init_runtime, compile, decompile, JSMemory, JSPointer, JSRuntime }; +} + +export function isInstantiated() { + return loader.instance != null; +} +export async function cacheToLocalDir(url, decompress) { + const localPath = await getUrlLocalPath(url); + if (localPath == null) { + return undefined; + } + if (!await exists(localPath)) { + const fileBytes = decompress(new Uint8Array(await getUrlBytes(url))); + try { + await Deno.writeFile(localPath, fileBytes); + } catch { + // ignore and return the wasm bytes + return fileBytes; + } + } + return toFileUrl(localPath); +} +async function getUrlLocalPath(url) { + try { + const dataDirPath = await getInitializedLocalDataDirPath(); + const hash = await getUrlHash(url); + return `${dataDirPath}/${hash}.wasm`; + } catch { + return undefined; + } +} +async function getInitializedLocalDataDirPath() { + const dataDir = localDataDir(); + if (dataDir == null) { + throw new Error(`Could not find local data directory.`); + } + const dirPath = `${dataDir}/deno-wasmbuild`; + await ensureDir(dirPath); + return dirPath; +} +async function exists(filePath) { + try { + await Deno.lstat(filePath); + return true; + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + return false; + } + throw error; + } +} +async function ensureDir(dir) { + try { + const fileInfo = await Deno.lstat(dir); + if (!fileInfo.isDirectory) { + throw new Error(`Path was not a directory '${dir}'`); + } + } catch (err) { + if (err instanceof Deno.errors.NotFound) { + // if dir not exists. then create it. + await Deno.mkdir(dir, { recursive: true }); + return; + } + throw err; + } +} +async function getUrlHash(url) { + // Taken from MDN: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest + const hashBuffer = await crypto.subtle.digest( + "SHA-256", + new TextEncoder().encode(url.href), + ); + // convert buffer to byte array + const hashArray = Array.from(new Uint8Array(hashBuffer)); + // convert bytes to hex string + const hashHex = hashArray + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + return hashHex; +} +async function getUrlBytes(url) { + const response = await fetchWithRetries(url); + return await response.arrayBuffer(); +} +// the below is extracted from deno_std/path +const WHITESPACE_ENCODINGS = { + "\u0009": "%09", + "\u000A": "%0A", + "\u000B": "%0B", + "\u000C": "%0C", + "\u000D": "%0D", + "\u0020": "%20", +}; +function encodeWhitespace(string) { + return string.replaceAll(/[\s]/g, (c) => { + return WHITESPACE_ENCODINGS[c] ?? c; + }); +} +function toFileUrl(path) { + return Deno.build.os === "windows" + ? windowsToFileUrl(path) + : posixToFileUrl(path); +} +function posixToFileUrl(path) { + const url = new URL("file:///"); + url.pathname = encodeWhitespace( + path.replace(/%/g, "%25").replace(/\\/g, "%5C"), + ); + return url; +} +function windowsToFileUrl(path) { + const [, hostname, pathname] = path.match( + /^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/, + ); + const url = new URL("file:///"); + url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25")); + if (hostname != null && hostname != "localhost") { + url.hostname = hostname; + if (!url.hostname) { + throw new TypeError("Invalid hostname."); + } + } + return url; +} +export async function fetchWithRetries(url, maxRetries = 5) { + let sleepMs = 250; + let iterationCount = 0; + while (true) { + iterationCount++; + try { + const res = await fetch(url); + if (res.ok || iterationCount > maxRetries) { + return res; + } + } catch (err) { + if (iterationCount > maxRetries) { + throw err; + } + } + console.warn(`Failed fetching. Retrying in ${sleepMs}ms...`); + await new Promise((resolve) => setTimeout(resolve, sleepMs)); + sleepMs = Math.min(sleepMs * 2, 10000); + } +} +// MIT License - Copyright (c) justjavac. +// https://github.com/justjavac/deno_dirs/blob/e8c001bbef558f08fd486d444af391729b0b8068/data_local_dir/mod.ts +function localDataDir() { + switch (Deno.build.os) { + case "linux": { + const xdg = Deno.env.get("XDG_DATA_HOME"); + if (xdg) { + return xdg; + } + const home = Deno.env.get("HOME"); + if (home) { + return `${home}/.local/share`; + } + break; + } + case "darwin": { + const home = Deno.env.get("HOME"); + if (home) { + return `${home}/Library/Application Support`; + } + break; + } + case "windows": + return Deno.env.get("LOCALAPPDATA") ?? undefined; + } + return undefined; +} From 44efa1ba66c0a1ccd31d27787a86c9ab92a5e166 Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:10:35 +0100 Subject: [PATCH 07/11] fix version test --- deno.lock | 18 + src/datex-core/datex_core_js.generated.js | 1013 +++++++++++---------- test/init.test.ts | 2 +- 3 files changed, 532 insertions(+), 501 deletions(-) create mode 100644 deno.lock diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..b742530 --- /dev/null +++ b/deno.lock @@ -0,0 +1,18 @@ +{ + "version": "4", + "specifiers": { + "jsr:@std/assert@*": "1.0.8", + "jsr:@std/internal@^1.0.5": "1.0.5" + }, + "jsr": { + "@std/assert@1.0.8": { + "integrity": "ebe0bd7eb488ee39686f77003992f389a06c3da1bbd8022184804852b2fa641b", + "dependencies": [ + "jsr:@std/internal" + ] + }, + "@std/internal@1.0.5": { + "integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba" + } + } +} diff --git a/src/datex-core/datex_core_js.generated.js b/src/datex-core/datex_core_js.generated.js index 195beb2..05e2762 100644 --- a/src/datex-core/datex_core_js.generated.js +++ b/src/datex-core/datex_core_js.generated.js @@ -4,7 +4,7 @@ // deno-fmt-ignore-file /// -// source-hash: 643c1630bc945a8e1cd0ef23ab4153fb5eda9b77 +// source-hash: b6d93fb83902454473c56ac4cba2637a3b7b11a9 let wasm; const heap = new Array(128).fill(undefined); @@ -12,161 +12,161 @@ const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); function getObject(idx) { - return heap[idx]; + return heap[idx]; } let heap_next = heap.length; function dropObject(idx) { - if (idx < 132) return; - heap[idx] = heap_next; - heap_next = idx; + if (idx < 132) return; + heap[idx] = heap_next; + heap_next = idx; } function takeObject(idx) { - const ret = getObject(idx); - dropObject(idx); - return ret; + const ret = getObject(idx); + dropObject(idx); + return ret; } const cachedTextDecoder = typeof TextDecoder !== "undefined" - ? new TextDecoder("utf-8", { ignoreBOM: true, fatal: true }) - : { - decode: () => { - throw Error("TextDecoder not available"); - }, - }; + ? new TextDecoder("utf-8", { ignoreBOM: true, fatal: true }) + : { + decode: () => { + throw Error("TextDecoder not available"); + }, + }; if (typeof TextDecoder !== "undefined") cachedTextDecoder.decode(); let cachedUint8Memory0 = null; function getUint8Memory0() { - if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { - cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8Memory0; + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; } function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); } function addHeapObject(obj) { - if (heap_next === heap.length) heap.push(heap.length + 1); - const idx = heap_next; - heap_next = heap[idx]; + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; - heap[idx] = obj; - return idx; + heap[idx] = obj; + return idx; } let cachedInt32Memory0 = null; function getInt32Memory0() { - if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { - cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachedInt32Memory0; + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachedInt32Memory0; } let cachedUint32Memory0 = null; function getUint32Memory0() { - if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) { - cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer); - } - return cachedUint32Memory0; + if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) { + cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer); + } + return cachedUint32Memory0; } function getArrayJsValueFromWasm0(ptr, len) { - ptr = ptr >>> 0; - const mem = getUint32Memory0(); - const slice = mem.subarray(ptr / 4, ptr / 4 + len); - const result = []; - for (let i = 0; i < slice.length; i++) { - result.push(takeObject(slice[i])); - } - return result; + ptr = ptr >>> 0; + const mem = getUint32Memory0(); + const slice = mem.subarray(ptr / 4, ptr / 4 + len); + const result = []; + for (let i = 0; i < slice.length; i++) { + result.push(takeObject(slice[i])); + } + return result; } /** * @returns {JSRuntime} */ export function init_runtime() { - const ret = wasm.init_runtime(); - return JSRuntime.__wrap(ret); + const ret = wasm.init_runtime(); + return JSRuntime.__wrap(ret); } let WASM_VECTOR_LEN = 0; const cachedTextEncoder = typeof TextEncoder !== "undefined" - ? new TextEncoder("utf-8") - : { - encode: () => { - throw Error("TextEncoder not available"); - }, - }; + ? new TextEncoder("utf-8") + : { + encode: () => { + throw Error("TextEncoder not available"); + }, + }; const encodeString = function (arg, view) { - return cachedTextEncoder.encodeInto(arg, view); + return cachedTextEncoder.encodeInto(arg, view); }; function passStringToWasm0(arg, malloc, realloc) { - if (realloc === undefined) { - const buf = cachedTextEncoder.encode(arg); - const ptr = malloc(buf.length, 1) >>> 0; - getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); - WASM_VECTOR_LEN = buf.length; - return ptr; - } - - let len = arg.length; - let ptr = malloc(len, 1) >>> 0; + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } - const mem = getUint8Memory0(); + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; - let offset = 0; + const mem = getUint8Memory0(); - for (; offset < len; offset++) { - const code = arg.charCodeAt(offset); - if (code > 0x7F) break; - mem[ptr + offset] = code; - } + let offset = 0; - if (offset !== len) { - if (offset !== 0) { - arg = arg.slice(offset); + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; } - ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; - const view = getUint8Memory0().subarray(ptr + offset, ptr + len); - const ret = encodeString(arg, view); - offset += ret.written; - ptr = realloc(ptr, len, offset, 1) >>> 0; - } + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); - WASM_VECTOR_LEN = offset; - return ptr; + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; } /** * @param {string} datex_script */ export function compile(datex_script) { - const ptr0 = passStringToWasm0( - datex_script, - wasm.__wbindgen_malloc, - wasm.__wbindgen_realloc, - ); - const len0 = WASM_VECTOR_LEN; - wasm.compile(ptr0, len0); + const ptr0 = passStringToWasm0( + datex_script, + wasm.__wbindgen_malloc, + wasm.__wbindgen_realloc, + ); + const len0 = WASM_VECTOR_LEN; + wasm.compile(ptr0, len0); } function passArray8ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 1, 1) >>> 0; - getUint8Memory0().set(arg, ptr / 1); - WASM_VECTOR_LEN = arg.length; - return ptr; + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8Memory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; } /** * @param {Uint8Array} dxb @@ -176,496 +176,509 @@ function passArray8ToWasm0(arg, malloc) { * @returns {string} */ export function decompile(dxb, formatted, colorized, resolve_slots) { - let deferred2_0; - let deferred2_1; - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - const ptr0 = passArray8ToWasm0(dxb, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - wasm.decompile(retptr, ptr0, len0, formatted, colorized, resolve_slots); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - deferred2_0 = r0; - deferred2_1 = r1; - return getStringFromWasm0(r0, r1); - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); - wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); - } + let deferred2_0; + let deferred2_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passArray8ToWasm0(dxb, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.decompile(retptr, ptr0, len0, formatted, colorized, resolve_slots); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred2_0 = r0; + deferred2_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } } const JSMemoryFinalization = (typeof FinalizationRegistry === "undefined") - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry((ptr) => wasm.__wbg_jsmemory_free(ptr >>> 0)); + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jsmemory_free(ptr >>> 0)); /** */ export class JSMemory { - static __wrap(ptr) { - ptr = ptr >>> 0; - const obj = Object.create(JSMemory.prototype); - obj.__wbg_ptr = ptr; - JSMemoryFinalization.register(obj, obj.__wbg_ptr, obj); - return obj; - } - - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - JSMemoryFinalization.unregister(this); - return ptr; - } - - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_jsmemory_free(ptr); - } - /** - * @param {Uint8Array} address - * @returns {JSPointer | undefined} - */ - get_pointer_by_id(address) { - const ret = wasm.jsmemory_get_pointer_by_id( - this.__wbg_ptr, - addHeapObject(address), - ); - return ret === 0 ? undefined : JSPointer.__wrap(ret); - } - /** - * @returns {(Uint8Array)[]} - */ - get_pointer_ids() { - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - wasm.jsmemory_get_pointer_ids(retptr, this.__wbg_ptr); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 4, 4); - return v1; - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSMemory.prototype); + obj.__wbg_ptr = ptr; + JSMemoryFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSMemoryFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jsmemory_free(ptr); + } + /** + * @param {Uint8Array} address + * @returns {JSPointer | undefined} + */ + get_pointer_by_id(address) { + const ret = wasm.jsmemory_get_pointer_by_id( + this.__wbg_ptr, + addHeapObject(address), + ); + return ret === 0 ? undefined : JSPointer.__wrap(ret); + } + /** + * @returns {(Uint8Array)[]} + */ + get_pointer_ids() { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jsmemory_get_pointer_ids(retptr, this.__wbg_ptr); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4, 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } } - } } const JSPointerFinalization = (typeof FinalizationRegistry === "undefined") - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry((ptr) => wasm.__wbg_jspointer_free(ptr >>> 0)); + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jspointer_free(ptr >>> 0)); /** */ export class JSPointer { - static __wrap(ptr) { - ptr = ptr >>> 0; - const obj = Object.create(JSPointer.prototype); - obj.__wbg_ptr = ptr; - JSPointerFinalization.register(obj, obj.__wbg_ptr, obj); - return obj; - } - - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - JSPointerFinalization.unregister(this); - return ptr; - } + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSPointer.prototype); + obj.__wbg_ptr = ptr; + JSPointerFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSPointerFinalization.unregister(this); + return ptr; + } - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_jspointer_free(ptr); - } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jspointer_free(ptr); + } } const JSRuntimeFinalization = (typeof FinalizationRegistry === "undefined") - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry((ptr) => wasm.__wbg_jsruntime_free(ptr >>> 0)); + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry((ptr) => wasm.__wbg_jsruntime_free(ptr >>> 0)); /** */ export class JSRuntime { - static __wrap(ptr) { - ptr = ptr >>> 0; - const obj = Object.create(JSRuntime.prototype); - obj.__wbg_ptr = ptr; - JSRuntimeFinalization.register(obj, obj.__wbg_ptr, obj); - return obj; - } - - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - JSRuntimeFinalization.unregister(this); - return ptr; - } - - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_jsruntime_free(ptr); - } - /** - * @returns {string} - */ - get version() { - let deferred1_0; - let deferred1_1; - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - wasm.jsruntime_version(retptr, this.__wbg_ptr); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - deferred1_0 = r0; - deferred1_1 = r1; - return getStringFromWasm0(r0, r1); - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); - wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(JSRuntime.prototype); + obj.__wbg_ptr = ptr; + JSRuntimeFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + JSRuntimeFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_jsruntime_free(ptr); + } + /** + * @returns {string} + */ + get version() { + let deferred1_0; + let deferred1_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.jsruntime_version(retptr, this.__wbg_ptr); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred1_0 = r0; + deferred1_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * @returns {JSMemory} + */ + get memory() { + const ret = wasm.jsruntime_memory(this.__wbg_ptr); + return JSMemory.__wrap(ret); } - } - /** - * @returns {JSMemory} - */ - get memory() { - const ret = wasm.jsruntime_memory(this.__wbg_ptr); - return JSMemory.__wrap(ret); - } } const imports = { - __wbindgen_placeholder__: { - __wbindgen_object_drop_ref: function (arg0) { - takeObject(arg0); - }, - __wbindgen_string_new: function (arg0, arg1) { - const ret = getStringFromWasm0(arg0, arg1); - return addHeapObject(ret); - }, - __wbg_log_5bb5f88f245d7762: function (arg0) { - console.log(getObject(arg0)); - }, - __wbg_buffer_12d079cc21e14bdb: function (arg0) { - const ret = getObject(arg0).buffer; - return addHeapObject(ret); - }, - __wbg_newwithbyteoffsetandlength_aa4a17c33a06e5cb: function ( - arg0, - arg1, - arg2, - ) { - const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0); - return addHeapObject(ret); - }, - __wbg_new_63b92bc8671ed464: function (arg0) { - const ret = new Uint8Array(getObject(arg0)); - return addHeapObject(ret); - }, - __wbg_set_a47bac70306a19a7: function (arg0, arg1, arg2) { - getObject(arg0).set(getObject(arg1), arg2 >>> 0); - }, - __wbg_length_c20a40f15020d68a: function (arg0) { - const ret = getObject(arg0).length; - return ret; - }, - __wbindgen_throw: function (arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }, - __wbindgen_memory: function () { - const ret = wasm.memory; - return addHeapObject(ret); + __wbindgen_placeholder__: { + __wbindgen_object_drop_ref: function (arg0) { + takeObject(arg0); + }, + __wbindgen_string_new: function (arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }, + __wbg_log_5bb5f88f245d7762: function (arg0) { + console.log(getObject(arg0)); + }, + __wbg_buffer_12d079cc21e14bdb: function (arg0) { + const ret = getObject(arg0).buffer; + return addHeapObject(ret); + }, + __wbg_newwithbyteoffsetandlength_aa4a17c33a06e5cb: function ( + arg0, + arg1, + arg2, + ) { + const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0); + return addHeapObject(ret); + }, + __wbg_new_63b92bc8671ed464: function (arg0) { + const ret = new Uint8Array(getObject(arg0)); + return addHeapObject(ret); + }, + __wbg_set_a47bac70306a19a7: function (arg0, arg1, arg2) { + getObject(arg0).set(getObject(arg1), arg2 >>> 0); + }, + __wbg_length_c20a40f15020d68a: function (arg0) { + const ret = getObject(arg0).length; + return ret; + }, + __wbindgen_throw: function (arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }, + __wbindgen_memory: function () { + const ret = wasm.memory; + return addHeapObject(ret); + }, }, - }, }; class WasmBuildLoader { - #options; - #lastLoadPromise; - #instantiated; - - constructor(options) { - this.#options = options; - } - - get instance() { - return this.#instantiated?.instance; - } - - get module() { - return this.#instantiated?.module; - } - - load( - url, - decompress, - ) { - if (this.#instantiated) { - return Promise.resolve(this.#instantiated); - } else if (this.#lastLoadPromise == null) { - this.#lastLoadPromise = (async () => { - try { - this.#instantiated = await this.#instantiate(url, decompress); - return this.#instantiated; - } finally { - this.#lastLoadPromise = undefined; + #options; + #lastLoadPromise; + #instantiated; + + constructor(options) { + this.#options = options; + } + + get instance() { + return this.#instantiated?.instance; + } + + get module() { + return this.#instantiated?.module; + } + + load( + url, + decompress, + ) { + if (this.#instantiated) { + return Promise.resolve(this.#instantiated); + } else if (this.#lastLoadPromise == null) { + this.#lastLoadPromise = (async () => { + try { + this.#instantiated = await this.#instantiate( + url, + decompress, + ); + return this.#instantiated; + } finally { + this.#lastLoadPromise = undefined; + } + })(); } - })(); - } - return this.#lastLoadPromise; - } - - async #instantiate(url, decompress) { - const imports = this.#options.imports; - if (this.#options.cache != null && url.protocol !== "file:") { - try { - const result = await this.#options.cache( - url, - decompress ?? ((bytes) => bytes), - ); - if (result instanceof URL) { - url = result; - decompress = undefined; // already decompressed - } else if (result != null) { - return WebAssembly.instantiate(result, imports); + return this.#lastLoadPromise; + } + + async #instantiate(url, decompress) { + const imports = this.#options.imports; + if (this.#options.cache != null && url.protocol !== "file:") { + try { + const result = await this.#options.cache( + url, + decompress ?? ((bytes) => bytes), + ); + if (result instanceof URL) { + url = result; + decompress = undefined; // already decompressed + } else if (result != null) { + return WebAssembly.instantiate(result, imports); + } + } catch { + // ignore if caching ever fails (ex. when on deploy) + } } - } catch { - // ignore if caching ever fails (ex. when on deploy) - } - } - - const isFile = url.protocol === "file:"; - - // make file urls work in Node via dnt - const isNode = globalThis.process?.versions?.node != null; - if (isFile && typeof Deno !== "object") { - throw new Error( - "Loading local files are not supported in this environment", - ); - } - if (isNode && isFile) { - // the deno global will be shimmed by dnt - const wasmCode = await Deno.readFile(url); - return WebAssembly.instantiate( - decompress ? decompress(wasmCode) : wasmCode, - imports, - ); - } - - switch (url.protocol) { - case "file:": - case "https:": - case "http:": { - const wasmResponse = await fetchWithRetries(url); - if (decompress) { - const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer()); - return WebAssembly.instantiate(decompress(wasmCode), imports); + + const isFile = url.protocol === "file:"; + + // make file urls work in Node via dnt + const isNode = globalThis.process?.versions?.node != null; + if (isFile && typeof Deno !== "object") { + throw new Error( + "Loading local files are not supported in this environment", + ); } - if ( - isFile || - wasmResponse.headers.get("content-type")?.toLowerCase() - .startsWith("application/wasm") - ) { - return WebAssembly.instantiateStreaming(wasmResponse, imports); - } else { - return WebAssembly.instantiate( - await wasmResponse.arrayBuffer(), - imports, - ); + if (isNode && isFile) { + // the deno global will be shimmed by dnt + const wasmCode = await Deno.readFile(url); + return WebAssembly.instantiate( + decompress ? decompress(wasmCode) : wasmCode, + imports, + ); + } + + switch (url.protocol) { + case "file:": + case "https:": + case "http:": { + const wasmResponse = await fetchWithRetries(url); + if (decompress) { + const wasmCode = new Uint8Array( + await wasmResponse.arrayBuffer(), + ); + return WebAssembly.instantiate( + decompress(wasmCode), + imports, + ); + } + if ( + isFile || + wasmResponse.headers.get("content-type")?.toLowerCase() + .startsWith("application/wasm") + ) { + return WebAssembly.instantiateStreaming( + wasmResponse, + imports, + ); + } else { + return WebAssembly.instantiate( + await wasmResponse.arrayBuffer(), + imports, + ); + } + } + default: + throw new Error(`Unsupported protocol: ${url.protocol}`); } - } - default: - throw new Error(`Unsupported protocol: ${url.protocol}`); } - } } const isNodeOrDeno = typeof Deno === "object" || - (typeof process !== "undefined" && process.versions != null && - process.versions.node != null); + (typeof process !== "undefined" && process.versions != null && + process.versions.node != null); const loader = new WasmBuildLoader({ - imports, - cache: isNodeOrDeno ? cacheToLocalDir : undefined, + imports, + cache: isNodeOrDeno ? cacheToLocalDir : undefined, }); export async function instantiate(opts) { - return (await instantiateWithInstance(opts)).exports; + return (await instantiateWithInstance(opts)).exports; } export async function instantiateWithInstance(opts) { - const { instance } = await loader.load( - opts?.url ?? new URL("datex_core_js_bg.wasm", import.meta.url), - opts?.decompress, - ); - wasm = wasm ?? instance.exports; - cachedInt32Memory0 = cachedInt32Memory0 ?? new Int32Array(wasm.memory.buffer); - cachedUint8Memory0 = cachedUint8Memory0 ?? new Uint8Array(wasm.memory.buffer); - return { - instance, - exports: getWasmInstanceExports(), - }; + const { instance } = await loader.load( + opts?.url ?? new URL("datex_core_js_bg.wasm", import.meta.url), + opts?.decompress, + ); + wasm = wasm ?? instance.exports; + cachedInt32Memory0 = cachedInt32Memory0 ?? + new Int32Array(wasm.memory.buffer); + cachedUint8Memory0 = cachedUint8Memory0 ?? + new Uint8Array(wasm.memory.buffer); + return { + instance, + exports: getWasmInstanceExports(), + }; } function getWasmInstanceExports() { - return { init_runtime, compile, decompile, JSMemory, JSPointer, JSRuntime }; + return { init_runtime, compile, decompile, JSMemory, JSPointer, JSRuntime }; } export function isInstantiated() { - return loader.instance != null; + return loader.instance != null; } export async function cacheToLocalDir(url, decompress) { - const localPath = await getUrlLocalPath(url); - if (localPath == null) { - return undefined; - } - if (!await exists(localPath)) { - const fileBytes = decompress(new Uint8Array(await getUrlBytes(url))); - try { - await Deno.writeFile(localPath, fileBytes); - } catch { - // ignore and return the wasm bytes - return fileBytes; + const localPath = await getUrlLocalPath(url); + if (localPath == null) { + return undefined; } - } - return toFileUrl(localPath); + if (!await exists(localPath)) { + const fileBytes = decompress(new Uint8Array(await getUrlBytes(url))); + try { + await Deno.writeFile(localPath, fileBytes); + } catch { + // ignore and return the wasm bytes + return fileBytes; + } + } + return toFileUrl(localPath); } async function getUrlLocalPath(url) { - try { - const dataDirPath = await getInitializedLocalDataDirPath(); - const hash = await getUrlHash(url); - return `${dataDirPath}/${hash}.wasm`; - } catch { - return undefined; - } + try { + const dataDirPath = await getInitializedLocalDataDirPath(); + const hash = await getUrlHash(url); + return `${dataDirPath}/${hash}.wasm`; + } catch { + return undefined; + } } async function getInitializedLocalDataDirPath() { - const dataDir = localDataDir(); - if (dataDir == null) { - throw new Error(`Could not find local data directory.`); - } - const dirPath = `${dataDir}/deno-wasmbuild`; - await ensureDir(dirPath); - return dirPath; + const dataDir = localDataDir(); + if (dataDir == null) { + throw new Error(`Could not find local data directory.`); + } + const dirPath = `${dataDir}/deno-wasmbuild`; + await ensureDir(dirPath); + return dirPath; } async function exists(filePath) { - try { - await Deno.lstat(filePath); - return true; - } catch (error) { - if (error instanceof Deno.errors.NotFound) { - return false; + try { + await Deno.lstat(filePath); + return true; + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + return false; + } + throw error; } - throw error; - } } async function ensureDir(dir) { - try { - const fileInfo = await Deno.lstat(dir); - if (!fileInfo.isDirectory) { - throw new Error(`Path was not a directory '${dir}'`); - } - } catch (err) { - if (err instanceof Deno.errors.NotFound) { - // if dir not exists. then create it. - await Deno.mkdir(dir, { recursive: true }); - return; + try { + const fileInfo = await Deno.lstat(dir); + if (!fileInfo.isDirectory) { + throw new Error(`Path was not a directory '${dir}'`); + } + } catch (err) { + if (err instanceof Deno.errors.NotFound) { + // if dir not exists. then create it. + await Deno.mkdir(dir, { recursive: true }); + return; + } + throw err; } - throw err; - } } async function getUrlHash(url) { - // Taken from MDN: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest - const hashBuffer = await crypto.subtle.digest( - "SHA-256", - new TextEncoder().encode(url.href), - ); - // convert buffer to byte array - const hashArray = Array.from(new Uint8Array(hashBuffer)); - // convert bytes to hex string - const hashHex = hashArray - .map((b) => b.toString(16).padStart(2, "0")) - .join(""); - return hashHex; + // Taken from MDN: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest + const hashBuffer = await crypto.subtle.digest( + "SHA-256", + new TextEncoder().encode(url.href), + ); + // convert buffer to byte array + const hashArray = Array.from(new Uint8Array(hashBuffer)); + // convert bytes to hex string + const hashHex = hashArray + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + return hashHex; } async function getUrlBytes(url) { - const response = await fetchWithRetries(url); - return await response.arrayBuffer(); + const response = await fetchWithRetries(url); + return await response.arrayBuffer(); } // the below is extracted from deno_std/path const WHITESPACE_ENCODINGS = { - "\u0009": "%09", - "\u000A": "%0A", - "\u000B": "%0B", - "\u000C": "%0C", - "\u000D": "%0D", - "\u0020": "%20", + "\u0009": "%09", + "\u000A": "%0A", + "\u000B": "%0B", + "\u000C": "%0C", + "\u000D": "%0D", + "\u0020": "%20", }; function encodeWhitespace(string) { - return string.replaceAll(/[\s]/g, (c) => { - return WHITESPACE_ENCODINGS[c] ?? c; - }); + return string.replaceAll(/[\s]/g, (c) => { + return WHITESPACE_ENCODINGS[c] ?? c; + }); } function toFileUrl(path) { - return Deno.build.os === "windows" - ? windowsToFileUrl(path) - : posixToFileUrl(path); + return Deno.build.os === "windows" + ? windowsToFileUrl(path) + : posixToFileUrl(path); } function posixToFileUrl(path) { - const url = new URL("file:///"); - url.pathname = encodeWhitespace( - path.replace(/%/g, "%25").replace(/\\/g, "%5C"), - ); - return url; + const url = new URL("file:///"); + url.pathname = encodeWhitespace( + path.replace(/%/g, "%25").replace(/\\/g, "%5C"), + ); + return url; } function windowsToFileUrl(path) { - const [, hostname, pathname] = path.match( - /^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/, - ); - const url = new URL("file:///"); - url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25")); - if (hostname != null && hostname != "localhost") { - url.hostname = hostname; - if (!url.hostname) { - throw new TypeError("Invalid hostname."); - } - } - return url; + const [, hostname, pathname] = path.match( + /^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/, + ); + const url = new URL("file:///"); + url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25")); + if (hostname != null && hostname != "localhost") { + url.hostname = hostname; + if (!url.hostname) { + throw new TypeError("Invalid hostname."); + } + } + return url; } export async function fetchWithRetries(url, maxRetries = 5) { - let sleepMs = 250; - let iterationCount = 0; - while (true) { - iterationCount++; - try { - const res = await fetch(url); - if (res.ok || iterationCount > maxRetries) { - return res; - } - } catch (err) { - if (iterationCount > maxRetries) { - throw err; - } + let sleepMs = 250; + let iterationCount = 0; + while (true) { + iterationCount++; + try { + const res = await fetch(url); + if (res.ok || iterationCount > maxRetries) { + return res; + } + } catch (err) { + if (iterationCount > maxRetries) { + throw err; + } + } + console.warn(`Failed fetching. Retrying in ${sleepMs}ms...`); + await new Promise((resolve) => setTimeout(resolve, sleepMs)); + sleepMs = Math.min(sleepMs * 2, 10000); } - console.warn(`Failed fetching. Retrying in ${sleepMs}ms...`); - await new Promise((resolve) => setTimeout(resolve, sleepMs)); - sleepMs = Math.min(sleepMs * 2, 10000); - } } // MIT License - Copyright (c) justjavac. // https://github.com/justjavac/deno_dirs/blob/e8c001bbef558f08fd486d444af391729b0b8068/data_local_dir/mod.ts function localDataDir() { - switch (Deno.build.os) { - case "linux": { - const xdg = Deno.env.get("XDG_DATA_HOME"); - if (xdg) { - return xdg; - } - const home = Deno.env.get("HOME"); - if (home) { - return `${home}/.local/share`; - } - break; - } - case "darwin": { - const home = Deno.env.get("HOME"); - if (home) { - return `${home}/Library/Application Support`; - } - break; - } - case "windows": - return Deno.env.get("LOCALAPPDATA") ?? undefined; - } - return undefined; + switch (Deno.build.os) { + case "linux": { + const xdg = Deno.env.get("XDG_DATA_HOME"); + if (xdg) { + return xdg; + } + const home = Deno.env.get("HOME"); + if (home) { + return `${home}/.local/share`; + } + break; + } + case "darwin": { + const home = Deno.env.get("HOME"); + if (home) { + return `${home}/Library/Application Support`; + } + break; + } + case "windows": + return Deno.env.get("LOCALAPPDATA") ?? undefined; + } + return undefined; } diff --git a/test/init.test.ts b/test/init.test.ts index 13f0777..42de1b9 100644 --- a/test/init.test.ts +++ b/test/init.test.ts @@ -10,7 +10,7 @@ Deno.test("runtime version", async () => { new URL("../deno.json", import.meta.url), ).then(JSON.parse).then((data: { version: string }) => data.version); const actual_version = await Deno.readTextFile( - new URL("../rs-lib/Cargo.toml", import.meta.url), + new URL("../rs-lib/datex-core/Cargo.toml", import.meta.url), ).then((data) => data.match(/version\s*=\s*"?([^"]*)"?$/m)?.[1]); const runtime = new Runtime(); From 77c453118553570ad4f3e783a96c0f2ef799499b Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:11:57 +0100 Subject: [PATCH 08/11] add submodules to test action --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dabb028..a9c9f10 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,6 +7,8 @@ jobs: name: Test steps: - uses: actions/checkout@v4 + with: + submodules: 'true' - uses: denoland/setup-deno@v2 with: deno-version: v2.x From f7c0f263f03a7e4489b398a15fb2bd412d7140ca Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:12:26 +0100 Subject: [PATCH 09/11] run deno fmt --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a9c9f10..b2b6267 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: steps: - uses: actions/checkout@v4 with: - submodules: 'true' + submodules: "true" - uses: denoland/setup-deno@v2 with: deno-version: v2.x From b487f6255acdd520c53b077921686dc8f30ad75f Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:25:39 +0100 Subject: [PATCH 10/11] add MIT license --- deno.json | 1 + 1 file changed, 1 insertion(+) diff --git a/deno.json b/deno.json index a3acf7a..5f2bc7d 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,7 @@ { "name": "@unyt/datex", "version": "0.0.1", + "license": "MIT", "exports": "./src/mod.ts", "compilerOptions": { "noImplicitOverride": true, From 206ebd69e8ec043d34582e2ebc441eb2d60fb52a Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Wed, 27 Nov 2024 00:26:37 +0100 Subject: [PATCH 11/11] fmt --- deno.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deno.json b/deno.json index 5f2bc7d..71734f2 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@unyt/datex", "version": "0.0.1", - "license": "MIT", + "license": "MIT", "exports": "./src/mod.ts", "compilerOptions": { "noImplicitOverride": true,