keynote-parser2
Version:
A library for parsing Apple Keynote file
55 lines (54 loc) • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseIwaProtobufArchiveInfo = void 0;
const varint_1 = __importDefault(require("varint"));
const proto_1 = __importDefault(require("../../proto/proto"));
const utils_1 = require("../utils");
/**
* Parse IWA protobuf archive info
*
* @see https://github.com/obriensp/iWorkFileFormat/blob/master/Docs/index.md#protobuf
*/
const parseIwaProtobufArchiveInfo = (data, cursor) => {
// get the first varint, which is the length of the archive info buffer
const archiveInfoBufferLength = varint_1.default.decode(data, cursor);
// get the archive info buffer
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const archiveInfoBufferStart = cursor + varint_1.default.decode.bytes;
const archiveInfoBufferEnd = archiveInfoBufferStart + archiveInfoBufferLength;
const archiveInfoBuffer = data.subarray(archiveInfoBufferStart, archiveInfoBufferEnd);
// decode to get the archive info
const archiveInfo = proto_1.default.TSP.ArchiveInfo.decode(archiveInfoBuffer, archiveInfoBufferLength);
// decode the messages
const messages = [];
let nextCursor = archiveInfoBufferEnd;
for (const messageInfo of archiveInfo.messageInfos) {
// TODO: handle messageInfo.type = 0
// @see https://github.com/psobot/keynote-parser/blob/48d4204ab943fc7ee75fd14881a54ad5264680f5/keynote_parser/codec.py#L189-L193
const messageBufferStart = nextCursor;
const messageBufferEnd = nextCursor + messageInfo.length;
const messageBuffer = data.subarray(messageBufferStart, messageBufferEnd);
nextCursor = messageBufferEnd;
try {
const { messageProtoName, messageProto } = (0, utils_1.getMessageProto)(messageInfo);
const message = messageProto.decode(messageBuffer);
messages.push({ messageProtoName, message });
}
catch (error) {
if (!(error instanceof Error))
throw error;
console.warn('Skip message, reason:', error.message);
}
}
return {
archiveInfoData: {
archiveInfo,
messages,
},
nextCursor,
};
};
exports.parseIwaProtobufArchiveInfo = parseIwaProtobufArchiveInfo;