Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
hsorbo committed Oct 10, 2023
1 parent 84abe1c commit 75ccba4
Show file tree
Hide file tree
Showing 5 changed files with 1,343 additions and 828 deletions.
3 changes: 1 addition & 2 deletions bplistParser.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
declare namespace bPlistParser {
export declare namespace bPlistParser {
type CallbackFunction<T = any> = (error: Error|null, result: [T]) => void
export function parseFile<T = any>(fileNameOrBuffer: string|Buffer, callback?: CallbackFunction<T>): Promise<[T]>
export function parseFileSync<T = any>(fileNameOrBuffer: string|Buffer): [T]
export function parseBuffer<T = any>(buffer: string|Buffer): [T]
}

export = bPlistParser
43 changes: 21 additions & 22 deletions bplistParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

// adapted from https://github.com/3breadt/dd-plist

const fs = require('fs');
const bigInt = require('big-integer');
import fs from 'fs';
const debug = false;

exports.maxObjectSize = 100 * 1000 * 1000; // 100Meg
exports.maxObjectCount = 32768;
export const maxObjectSize = 100 * 1000 * 1000; // 100Meg
export const maxObjectCount = 32768;

// EPOCH = new SimpleDateFormat("yyyy MM dd zzz").parse("2001 01 01 GMT").getTime();
// ...but that's annoying in a static initializer because it can throw exceptions, ick.
// So we just hardcode the correct value.
const EPOCH = 978307200000;

// UID object definition
const UID = exports.UID = function(id) {
export const UID = function(id) {
this.UID = id;
};

exports.parseFile = function (fileNameOrBuffer, callback) {
export const parseFile = function (fileNameOrBuffer, callback) {
return new Promise(function (resolve, reject) {
function tryParseBuffer(buffer) {
let err = null;
Expand Down Expand Up @@ -50,14 +49,14 @@ exports.parseFile = function (fileNameOrBuffer, callback) {
});
};

exports.parseFileSync = function (fileNameOrBuffer) {
export const parseFileSync = function (fileNameOrBuffer) {
if (!Buffer.isBuffer(fileNameOrBuffer)) {
fileNameOrBuffer = fs.readFileSync(fileNameOrBuffer);
}
return parseBuffer(fileNameOrBuffer);
};

const parseBuffer = exports.parseBuffer = function (buffer) {
export const parseBuffer = function (buffer) {
// check header
const header = buffer.slice(0, 'bplist'.length).toString('utf8');
if (header !== 'bplist') {
Expand Down Expand Up @@ -88,7 +87,7 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
console.log("offsetTableOffset: " + offsetTableOffset);
}

if (numObjects > exports.maxObjectCount) {
if (numObjects > maxObjectCount) {
throw new Error("maxObjectCount exceeded");
}

Expand Down Expand Up @@ -170,33 +169,33 @@ const parseBuffer = exports.parseBuffer = function (buffer) {

function parseInteger() {
const length = Math.pow(2, objInfo);
if (length < exports.maxObjectSize) {
if (length < maxObjectSize) {
const data = buffer.slice(offset + 1, offset + 1 + length);
if (length === 16) {
const str = bufferToHexString(data);
return bigInt(str, 16);
return BigInt(`0x${str}`);
}
return data.reduce((acc, curr) => {
acc <<= 8;
acc |= curr & 255;
return acc;
});
}
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + exports.maxObjectSize + " are available.");
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");

}

function parseUID() {
const length = objInfo + 1;
if (length < exports.maxObjectSize) {
if (length < maxObjectSize) {
return new UID(readUInt(buffer.slice(offset + 1, offset + 1 + length)));
}
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + exports.maxObjectSize + " are available.");
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
}

function parseReal() {
const length = Math.pow(2, objInfo);
if (length < exports.maxObjectSize) {
if (length < maxObjectSize) {
const realBuffer = buffer.slice(offset + 1, offset + 1 + length);
if (length === 4) {
return realBuffer.readFloatBE(0);
Expand All @@ -205,7 +204,7 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
return realBuffer.readDoubleBE(0);
}
} else {
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + exports.maxObjectSize + " are available.");
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
}
}

Expand Down Expand Up @@ -235,10 +234,10 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength));
}
}
if (length < exports.maxObjectSize) {
if (length < maxObjectSize) {
return buffer.slice(offset + dataoffset, offset + dataoffset + length);
}
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + exports.maxObjectSize + " are available.");
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
}

function parsePlistString (isUtf16) {
Expand All @@ -263,15 +262,15 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
}
// length is String length -> to get byte length multiply by 2, as 1 character takes 2 bytes in UTF-16
length *= (isUtf16 + 1);
if (length < exports.maxObjectSize) {
if (length < maxObjectSize) {
let plistString = Buffer.from(buffer.slice(offset + stroffset, offset + stroffset + length));
if (isUtf16) {
plistString = swapBytes(plistString);
enc = "ucs2";
}
return plistString.toString(enc);
}
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + exports.maxObjectSize + " are available.");
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
}

function parseArray() {
Expand All @@ -292,7 +291,7 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength));
}
}
if (length * objectRefSize > exports.maxObjectSize) {
if (length * objectRefSize > maxObjectSize) {
throw new Error("Too little heap space available!");
}
const array = [];
Expand Down Expand Up @@ -321,7 +320,7 @@ const parseBuffer = exports.parseBuffer = function (buffer) {
length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength));
}
}
if (length * 2 * objectRefSize > exports.maxObjectSize) {
if (length * 2 * objectRefSize > maxObjectSize) {
throw new Error("Too little heap space available!");
}
if (debug) {
Expand Down
Loading

0 comments on commit 75ccba4

Please sign in to comment.