UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

809 lines (807 loc) • 25.2 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var classes_native_exports = {}; __export(classes_native_exports, { Directory: () => Directory, File: () => File, FileHandle: () => FileHandle, FileMode: () => import_types.FileMode, Paths: () => Paths }); module.exports = __toCommonJS(classes_native_exports); var import_nativeError = require("../nativeError.native.js"); var import_hybrid = require("./hybrid.native.js"); var import_paths = require("./paths.native.js"); var import_streams = require("./streams.native.js"); var import_types = require("./types.native.js"); var import_validate = require("./validate.native.js"); function _assert_this_initialized(self) { if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true }); } else obj[key] = value; return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o2) { return o2.__proto__ || Object.getPrototypeOf(o2); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) return call; return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o2, p2) { o2.__proto__ = p2; return o2; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function native() { return (0, import_hybrid.fileSystemNative)(); } function sync(work) { try { return work(); } catch (error) { return (0, import_nativeError.rethrowNativeError)(error); } } function toArrayBuffer(bytes) { if (bytes instanceof ArrayBuffer) return bytes; if (bytes.buffer instanceof ArrayBuffer && bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) { return bytes.buffer; } var copy = new Uint8Array(bytes.byteLength); copy.set(bytes); return copy.buffer; } var FileHandle = /* @__PURE__ */ (function() { "use strict"; function FileHandle2(handle) { var mode = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : import_types.FileMode.ReadWrite; _class_call_check(this, FileHandle2); _define_property(this, "handleId", void 0); _define_property(this, "mode", void 0); _define_property(this, "closed", false); (0, import_validate.assertMode)(mode, "FileHandle"); this.handleId = handle; this.mode = mode; } _create_class(FileHandle2, [ { key: "offset", get: function get() { var _this = this; if (this.closed) return null; return sync(function() { return native().getHandleOffset(_this.handleId); }); }, set: function set(value) { var _this = this; this.assertOpen("offset"); (0, import_validate.assertOffset)(value, "FileHandle.offset"); sync(function() { return native().setHandleOffset(_this.handleId, value); }); } }, { key: "size", get: function get() { var _this = this; if (this.closed) return null; return sync(function() { return native().getHandleSize(_this.handleId); }); } }, { key: "close", value: function close() { var _this = this; if (this.closed) return; this.closed = true; sync(function() { return native().closeFile(_this.handleId); }); } }, { key: "readBytes", value: function readBytes(length) { this.assertOpen("readBytes"); (0, import_validate.assertReadable)(this.mode, "FileHandle.readBytes"); (0, import_validate.assertByteLength)(length, "FileHandle.readBytes"); return native().readHandleAsync(this.handleId, length).then(function(buffer) { return new Uint8Array(buffer); }).catch(import_nativeError.rethrowNativeError); } }, { key: "readBytesSync", value: function readBytesSync(length) { var _this = this; this.assertOpen("readBytesSync"); (0, import_validate.assertReadable)(this.mode, "FileHandle.readBytesSync"); (0, import_validate.assertByteLength)(length, "FileHandle.readBytesSync"); return new Uint8Array(sync(function() { return native().readHandle(_this.handleId, length); })); } }, { key: "writeBytes", value: function writeBytes(bytes) { this.assertOpen("writeBytes"); (0, import_validate.assertWritable)(this.mode, "FileHandle.writeBytes"); (0, import_validate.assertWriteBytes)(bytes, "FileHandle.writeBytes"); return native().writeHandleAsync(this.handleId, toArrayBuffer(bytes)).catch(import_nativeError.rethrowNativeError); } }, { key: "writeBytesSync", value: function writeBytesSync(bytes) { var _this = this; this.assertOpen("writeBytesSync"); (0, import_validate.assertWritable)(this.mode, "FileHandle.writeBytesSync"); (0, import_validate.assertWriteBytes)(bytes, "FileHandle.writeBytesSync"); sync(function() { return native().writeHandle(_this.handleId, toArrayBuffer(bytes)); }); } }, { key: "assertOpen", value: function assertOpen(verb) { if (this.closed) throw new Error(`FileHandle.${verb}: the handle is closed`); } } ]); return FileHandle2; })(); function resolveDestination(source, destination, verb) { (0, import_validate.assertDestination)(destination, verb); if (destination instanceof Directory) { return import_paths.PathUtilities.join(destination.uri, source.name); } if (destination instanceof File) return destination.uri; throw new Error(`${verb}: expected a File or Directory destination`); } var File = /* @__PURE__ */ (function() { "use strict"; function File2() { for (var _len = arguments.length, uris = new Array(_len), _key = 0; _key < _len; _key++) { uris[_key] = arguments[_key]; } _class_call_check(this, File2); _define_property(this, "currentUri", void 0); var joined = import_paths.PathUtilities.join(...uris); (0, import_validate.assertUri)(joined, "File"); this.currentUri = joined; } _create_class(File2, [ { key: "uri", get: function get() { return this.currentUri; } }, { key: "name", get: function get() { return import_paths.PathUtilities.basename(this.currentUri); } }, { key: "extension", get: function get() { return import_paths.PathUtilities.extname(this.currentUri); } }, { key: "parentDirectory", get: function get() { return new Directory(import_paths.PathUtilities.dirname(this.currentUri)); } }, { key: "exists", get: function get() { var _this = this; return sync(function() { return native().fileDetails(_this.currentUri); }).exists; } }, { key: "size", get: function get() { var _this = this; return sync(function() { return native().fileDetails(_this.currentUri); }).size; } }, { key: "type", get: function get() { var _this = this; return sync(function() { return native().fileDetails(_this.currentUri); }).mimeType; } }, { key: "creationTime", get: function get() { var _this = this; var _sync_creationTime; return (_sync_creationTime = sync(function() { return native().fileDetails(_this.currentUri); }).creationTime) !== null && _sync_creationTime !== void 0 ? _sync_creationTime : null; } }, { key: "lastModified", get: function get() { var _this = this; var _sync_modificationTime; return (_sync_modificationTime = sync(function() { return native().fileDetails(_this.currentUri); }).modificationTime) !== null && _sync_modificationTime !== void 0 ? _sync_modificationTime : null; } }, { key: "text", value: function text() { return native().readTextAsync(this.currentUri).catch(import_nativeError.rethrowNativeError); } }, { key: "textSync", value: function textSync() { var _this = this; return sync(function() { return native().readText(_this.currentUri); }); } }, { key: "bytes", value: function bytes() { return native().readDataAsync(this.currentUri).then(function(buffer) { return new Uint8Array(buffer); }).catch(import_nativeError.rethrowNativeError); } }, { key: "bytesSync", value: function bytesSync() { var _this = this; return new Uint8Array(sync(function() { return native().readData(_this.currentUri); })); } }, { key: "base64", value: function base64() { return native().readBase64Async(this.currentUri).catch(import_nativeError.rethrowNativeError); } }, { key: "base64Sync", value: function base64Sync() { var _this = this; return sync(function() { return native().readBase64(_this.currentUri); }); } }, { key: "arrayBuffer", value: async function arrayBuffer() { return (await this.bytes()).buffer; } }, { key: "json", value: async function json() { return JSON.parse(await this.text()); } }, { key: "info", value: function info() { var _this = this; var details = sync(function() { return native().fileDetails(_this.currentUri); }); if (!details.exists) return { exists: false, uri: this.currentUri }; return { exists: true, uri: this.currentUri, size: details.size, ...details.modificationTime !== void 0 && { modificationTime: details.modificationTime }, ...details.creationTime !== void 0 && { creationTime: details.creationTime } }; } }, { key: "digest", value: function digest(algorithm) { (0, import_validate.assertAlgorithm)(algorithm, "File.digest"); return native().fileDigest(this.currentUri, algorithm).catch(import_nativeError.rethrowNativeError); } }, { key: "write", value: function write(content, options) { var _ref; (0, import_validate.assertWriteContent)(content, "File.write"); var append = (_ref = options === null || options === void 0 ? void 0 : options.append) !== null && _ref !== void 0 ? _ref : false; if (typeof content === "string") { var _ref1; var encoding = (_ref1 = options === null || options === void 0 ? void 0 : options.encoding) !== null && _ref1 !== void 0 ? _ref1 : "utf8"; (0, import_validate.assertEncoding)(encoding, "File.write"); return native().writeTextAsync(this.currentUri, content, encoding, append).catch(import_nativeError.rethrowNativeError); } if ((options === null || options === void 0 ? void 0 : options.encoding) !== void 0) (0, import_validate.assertEncoding)(options.encoding, "File.write"); return native().writeDataAsync(this.currentUri, toArrayBuffer(content), append).catch(import_nativeError.rethrowNativeError); } }, { key: "writeSync", value: function writeSync(content, options) { var _this = this; var _ref; (0, import_validate.assertWriteContent)(content, "File.writeSync"); var append = (_ref = options === null || options === void 0 ? void 0 : options.append) !== null && _ref !== void 0 ? _ref : false; if (typeof content === "string") { var _ref1; var encoding = (_ref1 = options === null || options === void 0 ? void 0 : options.encoding) !== null && _ref1 !== void 0 ? _ref1 : "utf8"; (0, import_validate.assertEncoding)(encoding, "File.writeSync"); sync(function() { return native().writeText(_this.currentUri, content, encoding, append); }); return; } if ((options === null || options === void 0 ? void 0 : options.encoding) !== void 0) (0, import_validate.assertEncoding)(options.encoding, "File.writeSync"); sync(function() { return native().writeData(_this.currentUri, toArrayBuffer(content), append); }); } }, { key: "create", value: function create(options) { var _this = this; sync(function() { var _ref, _ref1; return native().createFileEntry(_this.currentUri, (_ref = options === null || options === void 0 ? void 0 : options.intermediates) !== null && _ref !== void 0 ? _ref : false, (_ref1 = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref1 !== void 0 ? _ref1 : false); }); } }, { key: "delete", value: function _delete() { var _this = this; sync(function() { return native().deleteEntry(_this.currentUri); }); } }, { key: "copy", value: function copy(destination, options) { var _ref; var to = resolveDestination(this, destination, "File.copy"); return native().copyEntryAsync(this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false).catch(import_nativeError.rethrowNativeError); } }, { key: "copySync", value: function copySync(destination, options) { var _this = this; var to = resolveDestination(this, destination, "File.copySync"); sync(function() { var _ref; return native().copyEntry(_this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false); }); } }, { key: "move", value: function move(destination, options) { var _this = this; var _ref; var to = resolveDestination(this, destination, "File.move"); if (to === this.currentUri) return Promise.resolve(); return native().moveEntryAsync(this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false).then(function() { _this.currentUri = to; }).catch(import_nativeError.rethrowNativeError); } }, { key: "moveSync", value: function moveSync(destination, options) { var _this = this; var to = resolveDestination(this, destination, "File.moveSync"); if (to === this.currentUri) return; sync(function() { var _ref; return native().moveEntry(_this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false); }); this.currentUri = to; } }, { key: "rename", value: function rename(newName) { var _this = this; (0, import_validate.assertName)(newName, "File.rename"); if (newName === this.name) return; this.currentUri = sync(function() { return native().renameEntry(_this.currentUri, newName); }); } }, { key: "open", value: function open() { var _this = this; var mode = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : import_types.FileMode.ReadWrite; (0, import_validate.assertMode)(mode, "File.open"); return new FileHandle(sync(function() { return native().openFile(_this.currentUri, mode); }), mode); } }, { key: "readableStream", value: function readableStream() { return new ReadableStream(new import_streams.FileSystemReadableStreamSource(this.open(import_types.FileMode.ReadOnly))); } }, { key: "writableStream", value: function writableStream() { return new WritableStream(new import_streams.FileSystemWritableSink(this.open(import_types.FileMode.WriteOnly))); } }, { key: "stream", value: function stream() { return this.readableStream(); } } ]); return File2; })(); var Directory = /* @__PURE__ */ (function() { "use strict"; function Directory2() { for (var _len = arguments.length, uris = new Array(_len), _key = 0; _key < _len; _key++) { uris[_key] = arguments[_key]; } _class_call_check(this, Directory2); _define_property(this, "currentUri", void 0); var joined = import_paths.PathUtilities.join(...uris); (0, import_validate.assertUri)(joined, "Directory"); this.currentUri = joined; } _create_class(Directory2, [ { key: "uri", get: function get() { return this.currentUri; } }, { key: "name", get: function get() { return import_paths.PathUtilities.basename(this.currentUri); } }, { key: "parentDirectory", get: function get() { return new Directory2(import_paths.PathUtilities.join(this.currentUri, "..")); } }, { key: "exists", get: function get() { var _this = this; return sync(function() { return native().directoryDetails(_this.currentUri); }).exists; } }, { key: "size", get: function get() { var _this = this; var _sync_size; return (_sync_size = sync(function() { return native().directoryDetails(_this.currentUri); }).size) !== null && _sync_size !== void 0 ? _sync_size : null; } }, { key: "list", value: function list() { var _this = this; return sync(function() { return native().listEntries(_this.currentUri); }).map(function(entry) { return entry.isDirectory ? new Directory2(entry.uri) : new File(entry.uri); }); } }, { key: "info", value: function info() { var _this = this; var details = sync(function() { return native().directoryDetails(_this.currentUri); }); if (!details.exists) return { exists: false, uri: this.currentUri }; return { exists: true, uri: this.currentUri, ...details.size !== void 0 && { size: details.size }, ...details.modificationTime !== void 0 && { modificationTime: details.modificationTime }, ...details.creationTime !== void 0 && { creationTime: details.creationTime }, ...details.files !== void 0 && { files: details.files } }; } }, { key: "create", value: function create(options) { var _this = this; sync(function() { var _ref, _ref1, _ref2; return native().createDirectoryEntry(_this.currentUri, (_ref = options === null || options === void 0 ? void 0 : options.intermediates) !== null && _ref !== void 0 ? _ref : false, (_ref1 = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref1 !== void 0 ? _ref1 : false, (_ref2 = options === null || options === void 0 ? void 0 : options.idempotent) !== null && _ref2 !== void 0 ? _ref2 : false); }); } }, { key: "createFile", value: function createFile(name, mimeType) { var _this = this; (0, import_validate.assertName)(name, "Directory.createFile"); if (typeof mimeType !== "string" && mimeType !== null) { throw new Error("Directory.createFile: expected a mime type string or null"); } return new File(sync(function() { return native().createFileInDirectory(_this.currentUri, name); })); } }, { key: "createDirectory", value: function createDirectory(name) { var _this = this; (0, import_validate.assertName)(name, "Directory.createDirectory"); return new Directory2(sync(function() { return native().createSubdirectory(_this.currentUri, name); })); } }, { key: "delete", value: function _delete() { var _this = this; sync(function() { return native().deleteEntry(_this.currentUri); }); } }, { key: "copy", value: function copy(destination, options) { var _ref; var to = resolveDestination(this, destination, "Directory.copy"); return native().copyEntryAsync(this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false).catch(import_nativeError.rethrowNativeError); } }, { key: "copySync", value: function copySync(destination, options) { var _this = this; var to = resolveDestination(this, destination, "Directory.copySync"); sync(function() { var _ref; return native().copyEntry(_this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false); }); } }, { key: "move", value: function move(destination, options) { var _this = this; var _ref; var to = resolveDestination(this, destination, "Directory.move"); if (to === this.currentUri) return Promise.resolve(); return native().moveEntryAsync(this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false).then(function() { _this.currentUri = to; }).catch(import_nativeError.rethrowNativeError); } }, { key: "moveSync", value: function moveSync(destination, options) { var _this = this; var to = resolveDestination(this, destination, "Directory.moveSync"); if (to === this.currentUri) return; sync(function() { var _ref; return native().moveEntry(_this.currentUri, to, (_ref = options === null || options === void 0 ? void 0 : options.overwrite) !== null && _ref !== void 0 ? _ref : false); }); this.currentUri = to; } }, { key: "rename", value: function rename(newName) { var _this = this; (0, import_validate.assertName)(newName, "Directory.rename"); if (newName === this.name) return; this.currentUri = sync(function() { return native().renameEntry(_this.currentUri, newName); }); } } ]); return Directory2; })(); var Paths = /* @__PURE__ */ (function(PathUtilities2) { "use strict"; _inherits(Paths2, PathUtilities2); function Paths2() { _class_call_check(this, Paths2); return _call_super(this, Paths2, arguments); } _create_class(Paths2, null, [ { key: "cache", get: function get() { return new Directory(sync(function() { return native().getDirectories(); }).cache); } }, { key: "document", get: function get() { return new Directory(sync(function() { return native().getDirectories(); }).documents); } }, { key: "bundle", get: function get() { return new Directory(sync(function() { return native().getBundleDirectory(); })); } }, { key: "totalDiskSpace", get: function get() { return sync(function() { return native().getDiskSpace(); }).total; } }, { key: "availableDiskSpace", get: function get() { return sync(function() { return native().getDiskSpace(); }).available; } }, { key: "info", value: function info() { for (var _len = arguments.length, uris = new Array(_len), _key = 0; _key < _len; _key++) { uris[_key] = arguments[_key]; } var _stat_isDirectory; var stat = sync(function() { return native().pathInfo(uris.join("/")); }); return { exists: stat.exists, isDirectory: (_stat_isDirectory = stat.isDirectory) !== null && _stat_isDirectory !== void 0 ? _stat_isDirectory : null }; } } ]); return Paths2; })(import_paths.PathUtilities);