Failed to parse URL from cadence-parser.wasm

279 views Asked by At

I am trying to utilize Cadence Parser package in one of my project and for some reason I am getting the error from title.

I've used example provided in README:

import { CadenceParser } from "@onflow/cadence-parser";

(async () => {
    const parser = await CadenceParser.create("cadence-parser.wasm");

    const ast = parser.parse(`
        pub contract HelloWorld {
            pub fun hello() {
                log("Hello, world!")
            }
        }
`);
})();

And getting the following error

TypeError: Failed to parse URL from cadence-parser.wasm
    at Object.fetch (node:internal/deps/undici/undici:11600:11) {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL

I've tried to copy wasm file from package folder into root - thinking that maybe paths are messed up somehow, but that didn't work either.

Node.js v20.7.0

Working Solution

The only solution that was working for me is following:

import { CadenceParser } from "@onflow/cadence-parser";
import * as fs from "fs"

(async () => {
    let file = fs.readFileSync('./cadence-parser.wasm');
    const parser = await CadenceParser.create(file);

    const ast = parser.parse(`
        pub contract HelloWorld {
            pub fun hello() {
                log("Hello, world!")
            }
        }
`);

    console.log({ast})
})();

Otherwise I was getting errors about incorret URL, wrong type passed to WebAssembly.instantiate and inability to fullfil fetch.

1

There are 1 answers

0
turbolent On

The package is designed for use in browsers at the moment, usage in server runtimes like Node.JS is not supported yet. This is because the package uses the fetch function, which does not support file: URLs in Node.JS. Please open a feature request issue, PRs are also very welcome!