#How to import a binary file

6 messages · Page 1 of 1 (latest)

blissful vale
#

I am building a command-line tool.

Users install it via: deno install https://raw.githubusercontent.com/velipso/foo

The tool includes a binary file in the repo, that it needs to be able to read. I thought I could do Deno.readFile(new URL('../path/to/file.bin', import.meta.url)), and that works when running locally, but fails when installed from GitHub.

Then I thought, I can import JSON files -- can I also import raw binary files?

Something like:

import fileBinary from '../path/to/file.bin';

console.log(fileBinary); // Uint8Array of the bytes

But that doesn't seem to work either.

How do I distribute a command-line tool that uses a binary file of data to operate?

Thanks for your help

#

I've stumbled across this: https://github.com/txthinking/denobundle -- this literally just converts a binary file to a new Uint8Array([... the binary data ...]) so you can import it normally, which seems really ugly

modern bobcat
#

Maybe replace Deno.readFile with fetch

junior sable
#

The way I do this my project is have a build step that reads in binary files, base64 encodes them and puts them in a JSON file. That JSON file I then import allBins from “./bla.json” assert {type: “json”} from there I base64 decode the strings. It’s not pretty (and adds to the bundle size), but it works.