UNPKG

@btfuse/filesystem

Version:

Filesystem plugin for the Fuse framework

134 lines (131 loc) 5.09 kB
"use strict"; /* Copyright 2023 Breautek Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FuseFilesystem = void 0; const core_1 = require("@btfuse/core"); const FuseFileObject_1 = require("./FuseFileObject"); const FuseFileType_1 = require("./FuseFileType"); const FuseDirectory_1 = require("./FuseDirectory"); const TAG = 'FuseFilesystem'; class FuseFilesystem extends core_1.FusePlugin { _getID() { return TAG; } get(path) { return new FuseFileObject_1.FuseFileObject(this, path); } getDirectory(path) { let obj = this.get(path); return new FuseDirectory_1.FuseDirectory(obj); } async getFileType(file) { let response = await this._exec('file/type', core_1.ContentType.TEXT, file.getPath()); if (response.isError()) { throw await response.readAsError(); } let serializedType = parseInt(await response.readAsText()); if (isNaN(serializedType) || serializedType < FuseFileType_1.FuseFileType.FILE || serializedType > FuseFileType_1.FuseFileType.DIRECTORY) { throw new core_1.FuseError(TAG, `Invalid FileType value: ${serializedType}`); } return serializedType; } async getSize(file) { let response = await this._exec('file/size', core_1.ContentType.TEXT, file.getPath()); if (response.isError()) { throw await response.readAsError(); } let size = parseInt(await response.readAsText()); if (isNaN(size)) { size = -1; } return size; } async mkdir(file, recursive) { let response = await this._exec('file/mkdir', core_1.ContentType.JSON, { path: file.getPath(), recursive: !!recursive }); if (response.isError()) { throw await response.readAsError(); } let result = await response.readAsText(); return result === 'true'; } async read(file, length, offset) { let response = await this._exec('file/read', core_1.ContentType.JSON, { path: file.getPath(), length: length, offset: offset }); if (response.isError()) { throw await response.readAsError(); } return response.readAsArrayBuffer(); } $createDataPacket(headerData, data) { let encoder = new TextEncoder(); let serializedHeaderData = encoder.encode(headerData); return new Blob([ new Uint32Array([serializedHeaderData.byteLength]), serializedHeaderData, data ]); } async truncate(file, data) { let payload = this.$createDataPacket(file.getPath(), this._getAPI().getSerializer().serialize(data)); let response = await this._exec('file/truncate', core_1.ContentType.BINARY, payload); if (response.isError()) { throw await response.readAsError(); } return parseInt(await response.readAsText()); } async append(file, data) { let payload = this.$createDataPacket(file.getPath(), this._getAPI().getSerializer().serialize(data)); let response = await this._exec('file/append', core_1.ContentType.BINARY, payload); if (response.isError()) { throw await response.readAsError(); } return parseInt(await response.readAsText()); } async write(file, data, offset = 0) { let payload = this.$createDataPacket(JSON.stringify({ path: file.getPath(), offset: offset }), this._getAPI().getSerializer().serialize(data)); let response = await this._exec('file/write', core_1.ContentType.BINARY, payload); if (response.isError()) { throw await response.readAsError(); } return parseInt(await response.readAsText()); } async remove(file, recursive = false) { let response = await this._exec('file/remove', core_1.ContentType.JSON, { path: file.getPath(), recursive: recursive }); if (response.isError()) { throw await response.readAsError(); } } async exists(file) { let response = await this._exec('file/exists', core_1.ContentType.TEXT, file.getPath()); if (response.isError()) { throw await response.readAsError(); } let value = await response.readAsText(); return value === '1'; } } exports.FuseFilesystem = FuseFilesystem; //# sourceMappingURL=FuseFilesystem.js.map