UNPKG

@dschu012/casclib

Version:
133 lines 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const addon = require('../build/Release/casclib-native'); const stream_1 = require("stream"); function openFileSync(storageHandle, filePath) { return addon.openCascFileSync(storageHandle, filePath); } exports.openFileSync = openFileSync; function openFile(storageHandle, filePath, callback) { return addon.openCascFile(storageHandle, filePath, callback); } exports.openFile = openFile; function readSync(fileHandle) { return addon.cascReadSync(fileHandle); } exports.readSync = readSync; function read(fileHandle, callback) { return addon.cascRead(fileHandle, callback); } exports.read = read; function readFileSync(storageHandle, filePath) { const fileHandle = openFileSync(storageHandle, filePath); const fileData = readSync(fileHandle); closeFile(fileHandle); return fileData; } exports.readFileSync = readFileSync; function readFile(storageHandle, filePath, callback) { if (callback) { openFile(storageHandle, filePath, (error, fileHandle) => { if (error) { throw error; } read(fileHandle, (error, fileData) => { closeFile(fileHandle); callback(error, fileData); }); }); return null; } return openFile(storageHandle, filePath) .then(fileHandle => { return read(fileHandle) .then(fileData => { closeFile(fileHandle); return fileData; }); }); } exports.readFile = readFile; class FileReadable extends stream_1.Readable { constructor(options) { super(options); } _read(size) { if (!this.fileHandle) { this.openFile(() => this.getData(size)); } else { this.getData(size); } } _destroy(error, callback) { if (this.storageHandle && this.path && this.storageHandle) { try { this.closeFile(); callback(error); } catch (e) { callback(e); } } } openFile(callback) { if (!this.path) { this.error(new Error("'fileHandle' or 'storageHandle' and 'path' must be provided.")); return; } openFile(this.storageHandle, this.path, (error, fileHandle) => { if (error) { this.error(error); return; } this.fileHandle = fileHandle; callback(); }); } closeFile() { if (this.storageHandle && this.path && this.fileHandle) { closeFile(this.fileHandle); this.fileHandle = null; } } getData(size) { addon.readCascFileBuffer(this.fileHandle, size, (error, buffer) => { if (error) { this.error(error); return; } if (buffer.length === 0) { this.closeFile(); this.push(null); } else { this.push(buffer); } }); } error(error) { process.nextTick(() => this.emit('error', error)); return; } } exports.FileReadable = FileReadable; function createReadStream(handle, filePathOrOptions, options) { const path = typeof filePathOrOptions === 'string' ? filePathOrOptions : undefined; options = typeof filePathOrOptions !== 'string' ? filePathOrOptions : options; const readable = new FileReadable(options || {}); readable.path = path; if (path) { readable.storageHandle = handle; } else { readable.fileHandle = handle; } return readable; } exports.createReadStream = createReadStream; function closeFile(fileHandle) { addon.closeCascFile(fileHandle); } exports.closeFile = closeFile; //# sourceMappingURL=files.js.map