Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected Unsupported Protocol Error #49

Open
harrysolovay opened this issue Aug 3, 2022 · 2 comments
Open

Unexpected Unsupported Protocol Error #49

harrysolovay opened this issue Aug 3, 2022 · 2 comments

Comments

@harrysolovay
Copy link

harrysolovay commented Aug 3, 2022

[EDIT]

wasm_url.protocol was an empty string (thanks to the local dev server / NextJS). Might it be worth including "" as a case here?


I'm getting the following error from my local dev server. Strangely, I use another "wasmbuilt" module in the same project, and it works as expected. Not sure why the other is producing this particular error.

Uncaught (in promise) Error: Unsupported protocol: 
    at instantiateModule (mod.generated.js:286:19)
    at eval (mod.generated.js:232:41)
    at instantiateWithInstance (mod.generated.js:245:11)
    at instantiate (mod.generated.js:212:19)
    at Ss58 (mod.js:10:90)
    at eval (submitAndWatchExtrinsic.js:24:85)
    at Runtime.eval (submitAndWatchExtrinsic.js:31:11)
    at eval (run.js:58:59)
    at async eval (run.js:66:46)
    at async Promise.all (:3000/index 2)
    at async eval (run.js:66:46)
    at async Promise.all (:3000/index 5)
    at async eval (run.js:66:46)
    at async Promise.all (:3000/index 2)
    at async eval (run.js:66:46)
    at async Module.value (run.js:24:44)

Below are both of the JS binding files:

The one that works
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 03396dbc62654de6e23aa312b7716c60b0b3ef0e
import * as dntShim from "../_dnt.shims.js";
let wasm;
const heap = new Array(32).fill(undefined);
heap.push(undefined, null, true, false);
function getObject(idx) {
    return heap[idx];
}
let heap_next = heap.length;
function dropObject(idx) {
    if (idx < 36)
        return;
    heap[idx] = heap_next;
    heap_next = idx;
}
function takeObject(idx) {
    const ret = getObject(idx);
    dropObject(idx);
    return ret;
}
const cachedTextDecoder = new TextDecoder("utf-8", {
    ignoreBOM: true,
    fatal: true,
});
cachedTextDecoder.decode();
let cachedUint8Memory0;
function getUint8Memory0() {
    if (cachedUint8Memory0.byteLength === 0) {
        cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
    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 WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
    const ptr = malloc(arg.length * 1);
    getUint8Memory0().set(arg, ptr / 1);
    WASM_VECTOR_LEN = arg.length;
    return ptr;
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function blake2_128(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.blake2_128(ptr0, len0);
    return takeObject(ret);
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function blake2_256(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.blake2_256(ptr0, len0);
    return takeObject(ret);
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function blake2_128Concat(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.blake2_128Concat(ptr0, len0);
    return takeObject(ret);
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function twox128(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.twox128(ptr0, len0);
    return takeObject(ret);
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function twox256(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.twox256(ptr0, len0);
    return takeObject(ret);
}
/**
 * @param {Uint8Array} data
 * @returns {Uint8Array}
 */
export function twox64Concat(data) {
    const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
    const len0 = WASM_VECTOR_LEN;
    const ret = wasm.twox64Concat(ptr0, len0);
    return takeObject(ret);
}
const cachedTextEncoder = new TextEncoder("utf-8");
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);
        getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
        WASM_VECTOR_LEN = buf.length;
        return ptr;
    }
    let len = arg.length;
    let ptr = malloc(len);
    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);
        const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
        const ret = encodeString(arg, view);
        offset += ret.written;
    }
    WASM_VECTOR_LEN = offset;
    return ptr;
}
let cachedInt32Memory0;
function getInt32Memory0() {
    if (cachedInt32Memory0.byteLength === 0) {
        cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
    }
    return cachedInt32Memory0;
}
const imports = {
    __wbindgen_placeholder__: {
        __wbindgen_object_drop_ref: function (arg0) {
            takeObject(arg0);
        },
        __wbg_new_693216e109162396: function () {
            const ret = new Error();
            return addHeapObject(ret);
        },
        __wbg_stack_0ddaca5d1abfb52f: function (arg0, arg1) {
            const ret = getObject(arg1).stack;
            const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
            const len0 = WASM_VECTOR_LEN;
            getInt32Memory0()[arg0 / 4 + 1] = len0;
            getInt32Memory0()[arg0 / 4 + 0] = ptr0;
        },
        __wbg_error_09919627ac0992f5: function (arg0, arg1) {
            try {
                console.error(getStringFromWasm0(arg0, arg1));
            }
            finally {
                wasm.__wbindgen_free(arg0, arg1);
            }
        },
        __wbg_buffer_de1150f91b23aa89: function (arg0) {
            const ret = getObject(arg0).buffer;
            return addHeapObject(ret);
        },
        __wbg_newwithbyteoffsetandlength_9ca61320599a2c84: function (arg0, arg1, arg2) {
            const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
            return addHeapObject(ret);
        },
        __wbg_new_97cf52648830a70d: function (arg0) {
            const ret = new Uint8Array(getObject(arg0));
            return addHeapObject(ret);
        },
        __wbindgen_throw: function (arg0, arg1) {
            throw new Error(getStringFromWasm0(arg0, arg1));
        },
        __wbindgen_memory: function () {
            const ret = wasm.memory;
            return addHeapObject(ret);
        },
    },
};
const wasm_url = new URL("mod_bg.wasm", import.meta.url);
/**
 * Decompression callback
 *
 * @callback decompressCallback
 * @param {Uint8Array} compressed
 * @return {Uint8Array} decompressed
 */
/** Instantiates an instance of the Wasm module returning its functions.
 * @remarks It is safe to call this multiple times and once successfully
 * loaded it will always return a reference to the same object.
 * @param {decompressCallback=} transform
 */
export async function instantiate(transform) {
    return (await instantiateWithInstance(transform)).exports;
}
let instanceWithExports;
let lastLoadPromise;
/** Instantiates an instance of the Wasm module along with its exports.
 * @remarks It is safe to call this multiple times and once successfully
 * loaded it will always return a reference to the same object.
 * @param {decompressCallback=} transform
 * @returns {Promise<{
 *   instance: WebAssembly.Instance;
 *   exports: { blake2_128: typeof blake2_128; blake2_256: typeof blake2_256; blake2_128Concat: typeof blake2_128Concat; twox128: typeof twox128; twox256: typeof twox256; twox64Concat: typeof twox64Concat }
 * }>}
 */
export function instantiateWithInstance(transform) {
    if (instanceWithExports != null) {
        return Promise.resolve(instanceWithExports);
    }
    if (lastLoadPromise == null) {
        lastLoadPromise = (async () => {
            try {
                const instance = (await instantiateModule(transform)).instance;
                wasm = instance.exports;
                cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
                cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
                instanceWithExports = {
                    instance,
                    exports: getWasmInstanceExports(),
                };
                return instanceWithExports;
            }
            finally {
                lastLoadPromise = null;
            }
        })();
    }
    return lastLoadPromise;
}
function getWasmInstanceExports() {
    return {
        blake2_128,
        blake2_256,
        blake2_128Concat,
        twox128,
        twox256,
        twox64Concat,
    };
}
/** Gets if the Wasm module has been instantiated. */
export function isInstantiated() {
    return instanceWithExports != null;
}
async function instantiateModule(transform) {
    switch (wasm_url.protocol) {
        case "file:": {
            if (typeof dntShim.Deno !== "object") {
                throw new Error("file urls are not supported in this environment");
            }
            if ("permissions" in dntShim.Deno) {
                await dntShim.Deno.permissions.request({ name: "read", path: wasm_url });
            }
            const wasmCode = await dntShim.Deno.readFile(wasm_url);
            return WebAssembly.instantiate(!transform ? wasmCode : transform(wasmCode), imports);
        }
        case "https:":
        case "http:": {
            if (typeof dntShim.Deno === "object" && "permissions" in dntShim.Deno) {
                await dntShim.Deno.permissions.request({ name: "net", host: wasm_url.host });
            }
            const wasmResponse = await fetch(wasm_url);
            if (transform) {
                const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer());
                return WebAssembly.instantiate(transform(wasmCode), imports);
            }
            if (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: ${wasm_url.protocol}`);
    }
}
//# sourceMappingURL=mod.generated.js.map
The one that does not work
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 2acb6246693ffbe50215ec91ed141cc7e96e8f48
import * as dntShim from "../_dnt.shims.js";
let wasm;
const cachedTextDecoder = new TextDecoder("utf-8", {
    ignoreBOM: true,
    fatal: true,
});
cachedTextDecoder.decode();
let cachedUint8Memory0;
function getUint8Memory0() {
    if (cachedUint8Memory0.byteLength === 0) {
        cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
    return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
const heap = new Array(32).fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
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;
}
function getObject(idx) {
    return heap[idx];
}
function dropObject(idx) {
    if (idx < 36)
        return;
    heap[idx] = heap_next;
    heap_next = idx;
}
function takeObject(idx) {
    const ret = getObject(idx);
    dropObject(idx);
    return ret;
}
let WASM_VECTOR_LEN = 0;
const cachedTextEncoder = new TextEncoder("utf-8");
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);
        getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
        WASM_VECTOR_LEN = buf.length;
        return ptr;
    }
    let len = arg.length;
    let ptr = malloc(len);
    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);
        const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
        const ret = encodeString(arg, view);
        offset += ret.written;
    }
    WASM_VECTOR_LEN = offset;
    return ptr;
}
let cachedInt32Memory0;
function getInt32Memory0() {
    if (cachedInt32Memory0.byteLength === 0) {
        cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
    }
    return cachedInt32Memory0;
}
/**
 * @param {string} text
 * @returns {Array<any>}
 */
export function decode(text) {
    try {
        const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
        const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
        const len0 = WASM_VECTOR_LEN;
        wasm.decode(retptr, ptr0, len0);
        var r0 = getInt32Memory0()[retptr / 4 + 0];
        var r1 = getInt32Memory0()[retptr / 4 + 1];
        var r2 = getInt32Memory0()[retptr / 4 + 2];
        if (r2) {
            throw takeObject(r1);
        }
        return takeObject(r0);
    }
    finally {
        wasm.__wbindgen_add_to_stack_pointer(16);
    }
}
/**
 * @param {number} prefix
 * @param {string} pub_key
 * @returns {string}
 */
export function encode(prefix, pub_key) {
    try {
        const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
        const ptr0 = passStringToWasm0(pub_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
        const len0 = WASM_VECTOR_LEN;
        wasm.encode(retptr, prefix, ptr0, len0);
        var r0 = getInt32Memory0()[retptr / 4 + 0];
        var r1 = getInt32Memory0()[retptr / 4 + 1];
        var r2 = getInt32Memory0()[retptr / 4 + 2];
        var r3 = getInt32Memory0()[retptr / 4 + 3];
        var ptr1 = r0;
        var len1 = r1;
        if (r3) {
            ptr1 = 0;
            len1 = 0;
            throw takeObject(r2);
        }
        return getStringFromWasm0(ptr1, len1);
    }
    finally {
        wasm.__wbindgen_add_to_stack_pointer(16);
        wasm.__wbindgen_free(ptr1, len1);
    }
}
const imports = {
    __wbindgen_placeholder__: {
        __wbindgen_error_new: function (arg0, arg1) {
            const ret = new Error(getStringFromWasm0(arg0, arg1));
            return addHeapObject(ret);
        },
        __wbindgen_number_new: function (arg0) {
            const ret = arg0;
            return addHeapObject(ret);
        },
        __wbindgen_object_drop_ref: function (arg0) {
            takeObject(arg0);
        },
        __wbindgen_string_new: function (arg0, arg1) {
            const ret = getStringFromWasm0(arg0, arg1);
            return addHeapObject(ret);
        },
        __wbg_new_693216e109162396: function () {
            const ret = new Error();
            return addHeapObject(ret);
        },
        __wbg_stack_0ddaca5d1abfb52f: function (arg0, arg1) {
            const ret = getObject(arg1).stack;
            const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
            const len0 = WASM_VECTOR_LEN;
            getInt32Memory0()[arg0 / 4 + 1] = len0;
            getInt32Memory0()[arg0 / 4 + 0] = ptr0;
        },
        __wbg_error_09919627ac0992f5: function (arg0, arg1) {
            try {
                console.error(getStringFromWasm0(arg0, arg1));
            }
            finally {
                wasm.__wbindgen_free(arg0, arg1);
            }
        },
        __wbg_new_2ab697f1555e0dbc: function () {
            const ret = new Array();
            return addHeapObject(ret);
        },
        __wbg_push_811c8b08bf4ff9d5: function (arg0, arg1) {
            const ret = getObject(arg0).push(getObject(arg1));
            return ret;
        },
        __wbindgen_throw: function (arg0, arg1) {
            throw new Error(getStringFromWasm0(arg0, arg1));
        },
    },
};
const wasm_url = new URL("mod_bg.wasm", import.meta.url);
/**
 * Decompression callback
 *
 * @callback decompressCallback
 * @param {Uint8Array} compressed
 * @return {Uint8Array} decompressed
 */
/** Instantiates an instance of the Wasm module returning its functions.
 * @remarks It is safe to call this multiple times and once successfully
 * loaded it will always return a reference to the same object.
 * @param {decompressCallback=} transform
 */
export async function instantiate(transform) {
    return (await instantiateWithInstance(transform)).exports;
}
let instanceWithExports;
let lastLoadPromise;
/** Instantiates an instance of the Wasm module along with its exports.
 * @remarks It is safe to call this multiple times and once successfully
 * loaded it will always return a reference to the same object.
 * @param {decompressCallback=} transform
 * @returns {Promise<{
 *   instance: WebAssembly.Instance;
 *   exports: { decode: typeof decode; encode: typeof encode }
 * }>}
 */
export function instantiateWithInstance(transform) {
    if (instanceWithExports != null) {
        return Promise.resolve(instanceWithExports);
    }
    if (lastLoadPromise == null) {
        lastLoadPromise = (async () => {
            try {
                const instance = (await instantiateModule(transform)).instance;
                wasm = instance.exports;
                cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
                cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
                instanceWithExports = {
                    instance,
                    exports: getWasmInstanceExports(),
                };
                return instanceWithExports;
            }
            finally {
                lastLoadPromise = null;
            }
        })();
    }
    return lastLoadPromise;
}
function getWasmInstanceExports() {
    return { decode, encode };
}
/** Gets if the Wasm module has been instantiated. */
export function isInstantiated() {
    return instanceWithExports != null;
}
async function instantiateModule(transform) {
    switch (wasm_url.protocol) {
        case "file:": {
            if (typeof dntShim.Deno !== "object") {
                throw new Error("file urls are not supported in this environment");
            }
            if ("permissions" in dntShim.Deno) {
                await dntShim.Deno.permissions.request({ name: "read", path: wasm_url });
            }
            const wasmCode = await dntShim.Deno.readFile(wasm_url);
            return WebAssembly.instantiate(!transform ? wasmCode : transform(wasmCode), imports);
        }
        case "https:":
        case "http:": {
            if (typeof dntShim.Deno === "object" && "permissions" in dntShim.Deno) {
                await dntShim.Deno.permissions.request({ name: "net", host: wasm_url.host });
            }
            const wasmResponse = await fetch(wasm_url);
            if (transform) {
                const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer());
                return WebAssembly.instantiate(transform(wasmCode), imports);
            }
            if (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: ${wasm_url.protocol}`);
    }
}
//# sourceMappingURL=mod.generated.js.map

Any tips to get this working would be greatly appreciated!

@dsherret
Copy link
Member

dsherret commented Jun 8, 2023

wasm_url.protocol was an empty string (thanks to the local dev server / NextJS). Might it be worth including "" as a case here?

What does the url look like? I feel like the protocol shouldn't be empty and perhaps this is a nextjs issue, but I'm not sure.

@harrysolovay
Copy link
Author

I'm not sure either / I've lost context on this. Please feel free to close.

hasundue pushed a commit to hasundue/wasmbuild that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants