Got a multiplatform library where resources need t...
# javascript
m
Got a multiplatform library where resources need to be extracted to the local file system at runtime (executable that will be run in its own process). Trying to add support for JS (node only), but have no clue how to retrieve them from
jsMain
in order to uncompress and persist to FS. Resources in question are in
src/jsMain/resources
directory. With java you'd just call
Copy code
javaClass.getResourcesAsStream("/path/myresource.txt.gz")
1. Is there a similar way to java's for accessing them at runtime? 2. The files should not be served as content, would there be a better way to distribute them then as resources?
b
Node has no built-in way of distributing resources. Common practice is to simply store them in the same folder and retrieve them via regular fs
What you could try though is making a self-extracting archive (only works on unix shells) that has some bash initialisation code prepended to the archive containing your js files and resources.
Here's the strategy outlined for jars, but can be adopted to extract and run embedded archive as well https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
Although the linked article inside looks to be exactly what you need https://www.linuxjournal.com/content/add-binary-payload-your-shell-scripts
m
Hmmm... bummer. Won't be able to support windows that way. Does node have the concept of plugins (like gradle), where I can create a plugin and they can configure it and it'll download the necessary files, uncompress them, and then install on the FS?
b
Node doesn't, but yarn 2 does
Webpack also support plugins
Another way you could go about it is a good old install script that would download and setup all required files
As for Windows support with install scripts, you still have options. Have a look at how sdkman handles it https://sdkman.io/install
m
I wonder if I could just create a gradle plugin where it'll extract the files to the project's
build
directory...
b
That works too, but it still requires the end user to have extra tooling installed
Hold on. Why don't you have your node app handle fetching and caching of resources?
On each resource access check for its existence on fs and if it's not present yet, download it and store it for future use.
That way your end user only needs node to install and run the app. Plus you get an added bonus of single binary distribution
m
Hmm 🤔
yeah I don't want to pigeonhole myself here b/c I'd like to distribute the lib via npm, as well as a klib...
b
By the way, how did you initially imagine the "installation"? npm install?
Ah! That makes things a lot easier! 😀
Have a look at #npm-publish for publishing
m
Well, here's the project: Wrapper for Tor: https://github.com/05nelsonm/kmp-tor Binary distribution: https://github.com/05nelsonm/kmp-tor-binary
Have a look at #npm-publish for publishing
Definitely, thanks!
b
Klib distribution goes through regular maven-publish, but it will not distribute your resources for js target...
m
Klib distribution goes through regular maven-publish, but it will not distribute your resources for js target...
Yeah, so a gradle plugin will definitely be needed Actually, could I distribute the binary files via npm and then add them as a dependency to the
kmp-tor
wrapper project???? 🤔
b
Then to get to your package resources after installation use this https://stackoverflow.com/a/10121471/10938638
🔥 1
Re: kmp-tor Yes, that's how people are going around this kjs limitation. e.g. https://github.com/rjaros/kvision/tree/master/kvision-assets
Not required if you only target npm, but a must for maven distribution
m
So: • Publish
kmp-tor-binary
assets to npm •
kmp-tor
library consumer would also add
npm
dependency for assets (whatever platform/architecture they wish to support)
b
Not quite. You can add kmp-tor-binary as an npm dependency in kmp-tor gradle dependencies for js target. That way it will get automatically downloaded when installing kmp-tor via npm
Have a look at how kvision-assets are used in kvision framework
m
Yeah, checking it out now
Was able to play around with this finally via a dummy project to publish the files to npm. Everything seems to be working in terms of obtaining the path to the file (see pic). Now to uncompress and extract to specified location. Thanks for your help!