Can I reuse a package that knows nothing about OpenFlange?
Available — v0.0.1 (experimental)javascript
Wrap an npm package
A short Node wrapper turns a library that is already on npm — here ms, which parses durations — into a pipe. The library itself is not modified.
Available in v0.0.1: the files, README and tests are in examples/npm-wrapper of the openflange-go repository. Commands run from the repository root.
Needs: Node.js 18 or newer and npm; network access once, for the install.
A pipe is a data-processing step with declared inputs and outputs.
What goes in, what comes out
Input (stdin)
"2 days"
"1.5h"
"soon"Command
(cd examples/npm-wrapper && npm ci --ignore-scripts) && printf '%s\n' '"2 days"' '"1.5h"' '"soon"' | openflange run ./examples/npm-wrapper --grant subprocessOutput (stdout)
{"text":"2 days","ms":172800000}
{"text":"1.5h","ms":5400000}
{"text":"soon","ms":null}Files and further commands
examples/npm-wrapper/parse-duration.js
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0
// parse-duration — wraps the npm package `ms` (https://www.npmjs.com/package/ms,
// MIT) as a pipe: "2 days" → {"text":"2 days","ms":172800000}.
// The wrapper is only the plumbing: read a line, call the library, write a line.
'use strict';
const readline = require('node:readline');
const ms = require('ms');
const lines = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
let n = 0;
lines.on('line', (line) => {
n += 1;
const text = JSON.parse(line);
if (typeof text !== 'string') {
console.error(`parse-duration: item ${n} is not a string`);
process.exit(1);
}
// `ms` returns undefined for text it does not understand, and throws for an
// empty or very long string. Either way the answer is an explicit null.
let value = null;
try {
const parsed = ms(text);
if (typeof parsed === 'number' && Number.isFinite(parsed)) value = parsed;
} catch (_) { /* not a duration */ }
process.stdout.write(JSON.stringify({ text, ms: value }) + '\n');
});
process.stdout.on('error', (err) => { if (err.code === 'EPIPE') process.exit(0); });What to notice
- Text that is not a duration gets an explicit
"ms": null: it is answered, not dropped. - The dependency is pinned to an exact version with a committed
package-lock.json;npm ciinstalls exactly that or fails. - Always install with
npm ci --ignore-scripts: no lifecycle script of any package is executed. OpenFlange itself never runs package lifecycle scripts. - After the install, running and testing need no network; the pipe declares only the
subprocesspermission. msis a third-party package (MIT) used as a familiar example; it is not affiliated with OpenFlange.