38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.sendChunks = exports.buildMessage = exports.createChunks = void 0;
|
|
const protobuf_1 = require("@trezor/protobuf");
|
|
const createChunks = (data, chunkHeader, chunkSize) => {
|
|
if (!chunkSize || data.byteLength <= chunkSize) {
|
|
const buffer = Buffer.alloc(Math.max(chunkSize, data.byteLength));
|
|
data.copy(buffer);
|
|
return [buffer];
|
|
}
|
|
const chunks = [data.subarray(0, chunkSize)];
|
|
let position = chunkSize;
|
|
while (position < data.byteLength) {
|
|
const sliceEnd = Math.min(position + chunkSize - chunkHeader.byteLength, data.byteLength);
|
|
const slice = data.subarray(position, sliceEnd);
|
|
const chunk = Buffer.concat([chunkHeader, slice]);
|
|
chunks.push(Buffer.alloc(chunkSize).fill(chunk, 0, chunk.byteLength));
|
|
position = sliceEnd;
|
|
}
|
|
return chunks;
|
|
};
|
|
exports.createChunks = createChunks;
|
|
const buildMessage = ({ messages, name, data, encode }) => {
|
|
const { messageType, message } = (0, protobuf_1.encodeMessage)(messages, name, data);
|
|
return encode(message, { messageType });
|
|
};
|
|
exports.buildMessage = buildMessage;
|
|
const sendChunks = async (chunks, apiWrite) => {
|
|
for (let i = 0; i < chunks.length; i++) {
|
|
const result = await apiWrite(chunks[i]);
|
|
if (!result.success) {
|
|
return result;
|
|
}
|
|
}
|
|
return { success: true, payload: undefined };
|
|
};
|
|
exports.sendChunks = sendChunks;
|
|
//# sourceMappingURL=send.js.map
|