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

Load wasm and data file as object url #86

Open
SOULPWRD opened this issue Oct 25, 2024 · 0 comments
Open

Load wasm and data file as object url #86

SOULPWRD opened this issue Oct 25, 2024 · 0 comments

Comments

@SOULPWRD
Copy link

Hi :)

Is it somehow possible to load .wasm and .data file from URL.createObjectURL? I'm working on the offline app and I need to store wasm and data file as base64 file locally.

I've edited your example a bit:


<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript"
        src="https://cdn.jsdelivr.net/npm/[email protected]/dist/package/gdal3.js"
        integrity="sha384-yW4c2Jx7lsREjJg58+ZI5U6gAso2bRAPw3LdzPWm7z8+rMJ24R7AS+EFyXDPxgYM"
        crossorigin="anonymous"
    ></script>
</head>
<body>
    Loading...
    <script type="text/javascript">
        async function start() {

            const [wasm, data, js] = await Promise.all([
                fetch('https://cdn.jsdelivr.net/npm/[email protected]/dist/package/gdal3WebAssembly.wasm'),
                fetch('https://cdn.jsdelivr.net/npm/[email protected]/dist/package/gdal3WebAssembly.data'),
                fetch('https://cdn.jsdelivr.net/npm/[email protected]/dist/package/gdal3.js')
            ]).then(function (data) {
                return Promise.all(data.map(function (d) {
                    return d.blob();
                }));
            }).then(function (blobs) {
                return blobs.map(window.URL.createObjectURL);
            });

            const paths = {
                wasm,
                data,
                js
            };

            const Gdal = await initGdalJs({paths});
            const numberOfDrivers = Object.keys(Gdal.drivers.raster).length + Object.keys(Gdal.drivers.vector).length;
            document.write(`Number of driver: ${numberOfDrivers}`);
        }

        start();
    </script>
</body>
</html>

but I'm getting XMLHttpRequest errors.

If it's not possible, would it be possible to support it? For instance SQL.js supports a locate function.

const SQL = await initSqlJs({
  // Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
  // You can omit locateFile completely when running in node
  locateFile: file => `https://sql.js.org/dist/${file}`
});

// and you can turn it into

const SQL = await initSqlJs({
  locateFile: file => URL.createObjectURL(blob)
});

// which works well.

Thank you in advance.
Best, Martin

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

1 participant