UNPKG

@shapediver/sdk.sdtf-v1

Version:
85 lines 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SdtfBinarySdtf = void 0; const sdk_sdtf_core_1 = require("@shapediver/sdk.sdtf-core"); const ISdtfComponentList_1 = require("../structure/ISdtfComponentList"); class SdtfBinarySdtf { constructor() { this.binaryHeaderLength = 20; } constructBinarySdtf(componentList) { var _a; const binaryBody = (_a = componentList.binaryBody) !== null && _a !== void 0 ? _a : new ArrayBuffer(0); const jsonContent = this.createJsonContent(componentList); const header = this.createHeader(jsonContent.byteLength, binaryBody.byteLength); return new Uint8Array([ ...new Uint8Array(header), ...new Uint8Array(jsonContent), ...new Uint8Array(binaryBody), ]).buffer; } parseBinarySdtf(sdtf) { const header = new DataView(sdtf, 0, this.binaryHeaderLength); const [contentLength, totalLength] = this.readHeader(header); const jsonContent = new DataView(sdtf, this.binaryHeaderLength, contentLength); const binaryBody = new DataView(sdtf, this.binaryHeaderLength + contentLength, totalLength - this.binaryHeaderLength - contentLength); return [jsonContent, binaryBody]; } readHeader(header) { if (header instanceof ArrayBuffer) header = new DataView(header); const magic = String.fromCharCode(header.getUint8(0)) + String.fromCharCode(header.getUint8(1)) + String.fromCharCode(header.getUint8(2)) + String.fromCharCode(header.getUint8(3)); if (magic !== 'sdtf') throw new sdk_sdtf_core_1.SdtfError(`Invalid identifier: Unknown file type for sdTF identifier '${magic}'.`); const version = header.getUint32(4, true); if (version !== 1) throw new sdk_sdtf_core_1.SdtfError(`Invalid version: Unsupported sdTF version '${version}'.`); const totalLength = header.getUint32(8, true); const contentLength = header.getUint32(12, true); const contentFormat = header.getUint32(16, true); if (contentFormat !== 0) throw new sdk_sdtf_core_1.SdtfError(`Invalid content: Unsupported sdTF content format '${contentFormat}'.`); return [contentLength, totalLength]; } createHeader(contentLength, bodyLength) { const buffer = new Uint8Array(20); const headerDataView = new DataView(buffer.buffer, 0, buffer.byteLength); const magic = 'sdtf'; headerDataView.setUint8(0, magic.codePointAt(0)); headerDataView.setUint8(1, magic.codePointAt(1)); headerDataView.setUint8(2, magic.codePointAt(2)); headerDataView.setUint8(3, magic.codePointAt(3)); const version = 1; headerDataView.setUint32(4, version, true); const totalLength = this.binaryHeaderLength + contentLength + bodyLength; headerDataView.setUint32(8, totalLength, true); headerDataView.setUint32(12, contentLength, true); const format = 0; headerDataView.setUint32(16, format, true); return headerDataView.buffer; } readJsonContent(jsonContent) { if (jsonContent instanceof ArrayBuffer) jsonContent = new DataView(jsonContent); try { return JSON.parse(new TextDecoder().decode(jsonContent)); } catch (e) { throw new sdk_sdtf_core_1.SdtfError(`Invalid content: Cannot parse sdTF JSON content. ${e.message}`); } } createJsonContent(componentList) { const jsonContent = (0, ISdtfComponentList_1.toJsonContent)(componentList); try { return new TextEncoder().encode(JSON.stringify(jsonContent, undefined, 0)); } catch (e) { throw new sdk_sdtf_core_1.SdtfError(`Invalid content: Cannot create sdTF JSON content. ${e.message}`); } } } exports.SdtfBinarySdtf = SdtfBinarySdtf; //# sourceMappingURL=SdtfBinarySdtf.js.map