@khoohaoyit/image-size
Version:
A stream based image size extractor in Node
182 lines (181 loc) • 10.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamParser = void 0;
class StreamParser {
constructor(stream) {
this.stream = stream;
}
setEndianness(endianness) {
this.endianness = endianness;
}
read(offset, size) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.stream.readRange({ offset, size });
});
}
readString(offset, size, encoding) {
return __awaiter(this, void 0, void 0, function* () {
const buf = yield this.read(offset, size);
return buf.toString(encoding);
});
}
readUInt8(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 1); return buf.readUInt8(); });
}
readUint8(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 1); return buf.readUint8(); });
}
readInt8(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 1); return buf.readInt8(); });
}
readUInt16LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readUInt16LE(); });
}
readUInt16BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readUInt16BE(); });
}
readUint16LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readUint16LE(); });
}
readUint16BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readUint16BE(); });
}
readInt16LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readInt16LE(); });
}
readInt16BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 2); return buf.readInt16BE(); });
}
readUInt32LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readUInt32LE(); });
}
readUInt32BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readUInt32BE(); });
}
readUint32LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readUint32LE(); });
}
readUint32BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readUint32BE(); });
}
readInt32LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readInt32LE(); });
}
readInt32BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readInt32BE(); });
}
readBigUInt64BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigUInt64BE(); });
}
readBigUInt64LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigUInt64LE(); });
}
readBigUint64BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigUint64BE(); });
}
readBigUint64LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigUint64LE(); });
}
readBigInt64BE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigInt64BE(); });
}
readBigInt64LE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readBigInt64LE(); });
}
readFloatLE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readFloatLE(); });
}
readFloatBE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 4); return buf.readFloatBE(); });
}
readDoubleLE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readDoubleLE(); });
}
readDoubleBE(offset) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, 8); return buf.readDoubleBE(); });
}
readUInt16(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 2); return buf[`readUInt16${this.endianness}`](); });
}
readUint16(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 2); return buf[`readUint16${this.endianness}`](); });
}
readInt16(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 2); return buf[`readInt16${this.endianness}`](); });
}
readUInt32(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 4); return buf[`readUInt32${this.endianness}`](); });
}
readUint32(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 4); return buf[`readUint32${this.endianness}`](); });
}
readInt32(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 4); return buf[`readInt32${this.endianness}`](); });
}
readBigUInt64(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 8); return buf[`readBigUInt64${this.endianness}`](); });
}
readBigUint64(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 8); return buf[`readBigUint64${this.endianness}`](); });
}
readBigInt64(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 8); return buf[`readBigInt64${this.endianness}`](); });
}
readFloat(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 4); return buf[`readFloat${this.endianness}`](); });
}
readDouble(offset) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, 8); return buf[`readDouble${this.endianness}`](); });
}
readUIntLE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readUIntLE(0, byteLength); });
}
readUIntBE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readUIntBE(0, byteLength); });
}
readUintLE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readUintLE(0, byteLength); });
}
readUintBE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readUintBE(0, byteLength); });
}
readIntLE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readIntLE(0, byteLength); });
}
readIntBE(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { const buf = yield this.read(offset, byteLength); return buf.readIntBE(0, byteLength); });
}
readUInt(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, byteLength); return buf[`readUInt${this.endianness}`](0, byteLength); });
}
readUint(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, byteLength); return buf[`readUint${this.endianness}`](0, byteLength); });
}
readInt(offset, byteLength) {
return __awaiter(this, void 0, void 0, function* () { if (!this.endianness)
throw new Error(`endianness not set`); const buf = yield this.read(offset, byteLength); return buf[`readInt${this.endianness}`](0, byteLength); });
}
}
exports.StreamParser = StreamParser;