UNPKG

@ultipa-graph/ultipa-node-sdk

Version:

NodeJS SDK for ultipa-server 4.0

97 lines 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamHelper = void 0; exports.streamHelper = { isUseStream: (streamReq) => { if ((streamReq === null || streamReq === void 0 ? void 0 : streamReq.onData) && (streamReq === null || streamReq === void 0 ? void 0 : streamReq.onEnd)) { return true; } return false; }, commonDeal: (stream, streamReq, dataFormat) => { let log = (...argv) => { // console.log("❗️ steam log ->",...argv) }; let package_limit = +(streamReq === null || streamReq === void 0 ? void 0 : streamReq.package_limit) || 0; if (exports.streamHelper.isUseStream(streamReq)) { if (streamReq.onStart) { log("start"); streamReq.onStart(); } if (streamReq.onData) { let count = 0; stream.on('data', function (chunkData) { count++; log('ondata --> ', count); stream.pause(); let data = chunkData; if (dataFormat) { try { data = dataFormat(chunkData); } catch (error) { if (streamReq.onError) { streamReq.onError(error); stream.destroy(); } else { console.log(error); } return; } } let res = streamReq.onData(data); if (package_limit > 0 && count >= package_limit) { if (res instanceof Promise) { res.finally(() => { stream.destroy(); }); } else { stream.destroy(); } return; } if (res instanceof Promise) { res.finally(() => { stream.resume(); }); } else { stream.resume(); } }); } stream.on("close", function () { var _a; log("close"); (_a = streamReq.onClose) === null || _a === void 0 ? void 0 : _a.call(streamReq); }); stream.on('end', function () { var _a; log("end"); (_a = streamReq.onEnd) === null || _a === void 0 ? void 0 : _a.call(streamReq); }); stream.on('error', function (err) { var _a; log("error"); (_a = streamReq.onError) === null || _a === void 0 ? void 0 : _a.call(streamReq, err); }); stream.on('pause', function () { var _a; log("pause"); (_a = streamReq.onPause) === null || _a === void 0 ? void 0 : _a.call(streamReq); }); // stream.on('readable', function() { // log("readable") // streamReq.onReadable && streamReq.onReadable() // }) stream.on('resume', function () { var _a; log("resume"); (_a = streamReq.onResume) === null || _a === void 0 ? void 0 : _a.call(streamReq); }); } } }; //# sourceMappingURL=stream.helper.js.map