node-red-contrib-dynamorse-file-io
Version:
Dynamorse File IO nodes for Node-RED
52 lines (43 loc) • 1.64 kB
JavaScript
/* Copyright 2018 Streampunk Media Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const Grain = require('node-red-contrib-dynamorse-core').Grain;
const codecadon = require('codecadon');
const { Transform } = require('stream');
module.exports = function(node) {
let flipper = null;
if (node.flip.h || node.flip.v) {
flipper = new codecadon.Flipper(() => {
node.log('Flipper exiting');
});
flipper.setInfo(node.tags, node.flip);
}
const transform = new Transform({
objectMode: true,
transform(x, encoding, cb) {
if (flipper && Grain.isGrain(x)) {
var dstBuf = Buffer.allocUnsafe(x.buffers[0].length);
flipper.flip(x.buffers, dstBuf, (err, result) => {
if (err) {
cb(err);
} else if (result) {
cb(null, new Grain([result], x.ptpSync, x.ptpOrigin,
x.timecode, x.flow_id, x.source_id, x.duration));
}
});
} else {
cb(null, x);
}
}
});
transform.on('end', () => { if (flipper) flipper.quit(() => {}); });
return transform;
};