Libraries
A codec can take a library’s modules instead of carrying their code: one Tuya protocol shared by a plug’s codec and a light’s, one ONVIF client shared by every camera’s.
Using one
Section titled “Using one”The package names its libraries in device.json, by GitHub repository and
release tag:
"libraries": {"tuya": {"repo": "The1TrueJoe/pulzr-tuya", "ref": "v1.0.1"}}and the codec requires modules by id, <library>/<module>:
var run = require('tuya/run');var plug = require('tuya/plug');function encodeDownlink(input) { return run.encode(input, plug.commands); }ref is a release tag, or a commit while a library is being written. A
platform resolves the tag to its commit when it publishes the package and
keeps the commit, so a tag moved later changes nothing already published.
Writing one
Section titled “Writing one”A library is a repository with pulzr-library.json at its root:
{ "name": "tuya", "modules": { "run": "lib/run.js", "local": "lib/local.js", "plug": "lib/plug.js", "light": "lib/light.js" }}A module is CommonJS-shaped: it fills module.exports (or exports) and
requires other modules by their full id, its own library’s included:
var local = require('tuya/local');module.exports = { commands: { 'switch.on': function (steps, ip) { /* ... */ } } };Names are lower case letters, digits, - and _.
What bundling does
Section titled “What bundling does”pulzr-codec bundle <dir> (and a platform at publish time) puts the modules
the codec requires, and the modules those require, ahead of the codec, each in
a function of its own, and defines require. Nothing else goes in: a plug’s
codec carries run, local and plug, not light. Only literal ids are
followed (require('tuya/plug'), not require(name)), so the bundle is
exactly what the source says. A cycle is allowed, as in CommonJS.
The bundle is one plain script and runs as any codec does; nothing of libraries reaches the engine. It starts with a comment naming each library, its commit and the modules taken.
Testing a library with its codecs
Section titled “Testing a library with its codecs”pulzr-codec test types/plug tuya=.name=path takes a library from a directory instead of GitHub, so a change to
the library is tested against every type built on it before it is tagged.