Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
Arvid E. Picciani edited this page May 11, 2020 · 1 revision

using zz in nodejs is easy-ish. to get started, simply add the following to zz.toml

[[artifacts]]
name = "mything"
type = "npm"
main = "mything::mymodule"                                      

zz will then build your project into a standalone npm module available at target/$stage/npm/mything/ which you could upload to npm without the need for zz as a dependency. or you can use that module directly in another nodejs project with something like this in package.json

  "dependencies": {
    "mything": "file:/home/me/mything/target/release/npm/mything/"
  }

hitting npm install will automatically build the module you specified in main and all of its dependencies as C files into a native nodejs module. use node-gyp rebuild to rebuild all the stuff.

you can then use it like so

var zz = require('mything');
let mystruct = new zz.mything_mymodule.Mystruct(100);

the exact import pathing is up to change, but currently it is root.full_module_path.type. every struct has a new constructor, which takes as single argument the tail size. You can omit that parameter, which means tail size 0.

here's an example how to use the std err type

var zz = require('mything');

let e = new zz.err.Err(1000);
e.new();
e.abort("index.js","thisfunction",0);

Err::new() does require a tail size in the c api, but in zz tails are implicitly passed, so that semantic is also adapted into nodejs, although in this case at a runtime cost.

since the default js mappings are very inconvenient, you might want to provide your own index.js for the module to export a nicer more nodejs like api.

in zz.toml, specifiy "indexjs"

[[artifacts]]
name = "mything"
type = "npm"
main = "mything::mymodule"              
indexjs = "src/index.js"                        

this is the default index.js:

module.exports = require('bindings')('mything');

so change that to your liking.

Clone this wiki locally