wechaty-puppet-service
Version:
Puppet Service for Wechaty
67 lines • 2.33 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.packFileBoxToChunk = exports.unpackFileBoxFromChunk = void 0;
const file_box_1 = require("file-box");
const stream_1 = require("stream");
const stronger_typed_streams_1 = require("stronger-typed-streams");
const wechaty_grpc_1 = require("wechaty-grpc");
const next_data_js_1 = require("./next-data.js");
/**
* @deprecated Will be removed after Dec 31, 2022
*/
const decoder = () => new stronger_typed_streams_1.Transform({
objectMode: true,
transform: (chunk, _, callback) => {
if (!chunk.hasData()) {
callback(new Error('no data'));
return;
}
const data = chunk.getData();
callback(null, data);
},
});
/**
* @deprecated Will be removed after Dec 31, 2022
*/
async function unpackFileBoxFromChunk(stream) {
const chunk = await (0, next_data_js_1.nextData)(stream);
if (!chunk.hasName()) {
throw new Error('no name');
}
const fileName = chunk.getName();
const fileStream = new stream_1.PassThrough({ objectMode: true });
const transformedStream = stream.pipe(decoder());
transformedStream.pipe(fileStream);
stream.on('error', e => fileStream.emit('error', e));
transformedStream.on('error', e => fileStream.emit('error', e));
const fileBox = file_box_1.FileBox.fromStream(fileStream, fileName);
return fileBox;
}
exports.unpackFileBoxFromChunk = unpackFileBoxFromChunk;
/**
* @deprecated Will be removed after Dec 31, 2022
*/
const encoder = () => new stronger_typed_streams_1.Transform({
objectMode: true,
transform: (chunk, _, callback) => {
const fileBoxChunk = new wechaty_grpc_1.puppet.FileBoxChunk();
fileBoxChunk.setData(chunk);
callback(null, fileBoxChunk);
},
});
/**
* @deprecated Will be removed after Dec 31, 2022
*/
async function packFileBoxToChunk(fileBox) {
const stream = new stream_1.PassThrough({ objectMode: true });
const chunk = new wechaty_grpc_1.puppet.FileBoxChunk();
chunk.setName(fileBox.name);
// FIXME: Huan(202010) write might return false
stream.write(chunk);
fileBox
.pipe(encoder())
.pipe(stream);
return stream;
}
exports.packFileBoxToChunk = packFileBoxToChunk;
//# sourceMappingURL=file-box-chunk.js.map
;