UNPKG

@difizen/mana-app

Version:

487 lines (465 loc) 24.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileType = exports.FileSystemProviderErrorCode = exports.FileSystemProviderError = exports.FileSystemProviderCapabilities = exports.FileSystemProvider = exports.FileStat = exports.FileOperationResult = exports.FileOperationEvent = exports.FileOperationError = exports.FileOperation = exports.FileChangesEvent = exports.FileChangeType = exports.ETAG_DISABLED = exports.BinarySize = exports.BaseStat = void 0; exports.createFileSystemProviderError = createFileSystemProviderError; exports.ensureFileSystemProviderError = ensureFileSystemProviderError; exports.etag = etag; exports.hasAccessCapability = hasAccessCapability; exports.hasFileFolderCopyCapability = hasFileFolderCopyCapability; exports.hasOpenReadWriteCloseCapability = hasOpenReadWriteCloseCapability; exports.hasReadWriteCapability = hasReadWriteCapability; exports.markAsFileSystemProviderError = markAsFileSystemProviderError; exports.toFileOperationResult = toFileOperationResult; exports.toFileSystemProviderErrorCode = toFileSystemProviderErrorCode; var _manaCommon = require("@difizen/mana-common"); var _BinarySize; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _classCallCheck(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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* eslint-disable @typescript-eslint/no-use-before-define */ /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ // based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/common/files.ts var FileOperation = exports.FileOperation = /*#__PURE__*/function (FileOperation) { FileOperation[FileOperation["CREATE"] = 0] = "CREATE"; FileOperation[FileOperation["DELETE"] = 1] = "DELETE"; FileOperation[FileOperation["MOVE"] = 2] = "MOVE"; FileOperation[FileOperation["COPY"] = 3] = "COPY"; return FileOperation; }({}); var FileOperationEvent = exports.FileOperationEvent = /*#__PURE__*/function () { function FileOperationEvent(resource, operation, target) { _classCallCheck(this, FileOperationEvent); this.resource = resource; this.operation = operation; this.target = target; } _createClass(FileOperationEvent, [{ key: "isOperation", value: function isOperation(operation) { return this.operation === operation; } }]); return FileOperationEvent; }(); /** * Possible changes that can occur to a file. */ var FileChangeType = exports.FileChangeType = /*#__PURE__*/function (FileChangeType) { FileChangeType[FileChangeType["UPDATED"] = 0] = "UPDATED"; FileChangeType[FileChangeType["ADDED"] = 1] = "ADDED"; FileChangeType[FileChangeType["DELETED"] = 2] = "DELETED"; return FileChangeType; }({}); /** * Identifies a single change in a file. */ var FileChangesEvent = exports.FileChangesEvent = /*#__PURE__*/function () { function FileChangesEvent(changes) { _classCallCheck(this, FileChangesEvent); this.changes = changes; } /** * Returns true if this change event contains the provided file with the given change type (if provided). In case of * type DELETED, this method will also return true if a folder got deleted that is the parent of the * provided file path. */ _createClass(FileChangesEvent, [{ key: "contains", value: function contains(resource, type) { if (!resource) { return false; } var checkForChangeType = typeof type === 'number'; return this.changes.some(function (change) { if (checkForChangeType && change.type !== type) { return false; } // For deleted also return true when deleted folder is parent of target path if (change.type === FileChangeType.DELETED) { return resource.includes(change.resource); } return resource.toString() === change.resource.toString(); }); } /** * Returns the changes that describe added files. */ }, { key: "getAdded", value: function getAdded() { return this.getOfType(FileChangeType.ADDED); } /** * Returns if this event contains added files. */ }, { key: "gotAdded", value: function gotAdded() { return this.hasType(FileChangeType.ADDED); } /** * Returns the changes that describe deleted files. */ }, { key: "getDeleted", value: function getDeleted() { return this.getOfType(FileChangeType.DELETED); } /** * Returns if this event contains deleted files. */ }, { key: "gotDeleted", value: function gotDeleted() { return this.hasType(FileChangeType.DELETED); } /** * Returns the changes that describe updated files. */ }, { key: "getUpdated", value: function getUpdated() { return this.getOfType(FileChangeType.UPDATED); } /** * Returns if this event contains updated files. */ }, { key: "gotUpdated", value: function gotUpdated() { return this.hasType(FileChangeType.UPDATED); } }, { key: "getOfType", value: function getOfType(type) { return this.changes.filter(function (change) { return change.type === type; }); } }, { key: "hasType", value: function hasType(type) { return this.changes.some(function (change) { return change.type === type; }); } }]); return FileChangesEvent; }(); var BaseStat; (function (_BaseStat) { function is(arg) { return !!arg && _typeof(arg) === 'object' && // eslint-disable-next-line @typescript-eslint/no-explicit-any 'resource' in arg && arg.resource instanceof _manaCommon.URI && 'name' in arg && typeof arg.name === 'string'; } _BaseStat.is = is; })(BaseStat || (exports.BaseStat = BaseStat = {})); /** * A file resource with meta information. */ var FileStat; (function (_FileStat) { function is(arg) { if (BaseStat.is(arg) && 'isFile' in arg) { return 'isFile' in arg && typeof arg.isFile === 'boolean' && 'isDirectory' in arg && typeof arg.isDirectory === 'boolean' && 'isSymbolicLink' in arg && typeof arg.isSymbolicLink === 'boolean'; } return false; } _FileStat.is = is; function asFileType(stat) { var res = 0; if (stat.isFile) { res += FileType.File; } else if (stat.isDirectory) { res += FileType.Directory; } if (stat.isSymbolicLink) { res += FileType.SymbolicLink; } return res; } _FileStat.asFileType = asFileType; function toStat(stat) { return { type: asFileType(stat), ctime: stat.ctime, mtime: stat.mtime, size: stat.size }; } _FileStat.toStat = toStat; function fromStat(resource, stat) { return { resource: resource, name: resource.path.base || resource.path.toString(), isFile: (stat.type & FileType.File) !== 0, isDirectory: (stat.type & FileType.Directory) !== 0, isSymbolicLink: (stat.type & FileType.SymbolicLink) !== 0, mtime: stat.mtime, ctime: stat.ctime, size: stat.size, etag: etag({ mtime: stat.mtime, size: stat.size }) }; } _FileStat.fromStat = fromStat; function dir(resource, stat) { return fromStat(resource instanceof _manaCommon.URI ? resource : new _manaCommon.URI(resource), _objectSpread({ type: FileType.Directory }, stat)); } _FileStat.dir = dir; function file(resource, stat) { return fromStat(resource instanceof _manaCommon.URI ? resource : new _manaCommon.URI(resource), _objectSpread({ type: FileType.File }, stat)); } _FileStat.file = file; })(FileStat || (exports.FileStat = FileStat = {})); var FileOperationError = exports.FileOperationError = /*#__PURE__*/function (_Error) { _inherits(FileOperationError, _Error); var _super = _createSuper(FileOperationError); function FileOperationError(message, fileOperationResult, options) { var _this; _classCallCheck(this, FileOperationError); _this = _super.call(this, message); _this.fileOperationResult = fileOperationResult; _this.options = options; Object.setPrototypeOf(_assertThisInitialized(_this), FileOperationError.prototype); return _this; } return _createClass(FileOperationError); }( /*#__PURE__*/_wrapNativeSuper(Error)); var FileOperationResult = exports.FileOperationResult = /*#__PURE__*/function (FileOperationResult) { FileOperationResult[FileOperationResult["FILE_IS_DIRECTORY"] = 0] = "FILE_IS_DIRECTORY"; FileOperationResult[FileOperationResult["FILE_NOT_FOUND"] = 1] = "FILE_NOT_FOUND"; FileOperationResult[FileOperationResult["FILE_NOT_MODIFIED_SINCE"] = 2] = "FILE_NOT_MODIFIED_SINCE"; FileOperationResult[FileOperationResult["FILE_MODIFIED_SINCE"] = 3] = "FILE_MODIFIED_SINCE"; FileOperationResult[FileOperationResult["FILE_MOVE_CONFLICT"] = 4] = "FILE_MOVE_CONFLICT"; FileOperationResult[FileOperationResult["FILE_READ_ONLY"] = 5] = "FILE_READ_ONLY"; FileOperationResult[FileOperationResult["FILE_PERMISSION_DENIED"] = 6] = "FILE_PERMISSION_DENIED"; FileOperationResult[FileOperationResult["FILE_TOO_LARGE"] = 7] = "FILE_TOO_LARGE"; FileOperationResult[FileOperationResult["FILE_INVALID_PATH"] = 8] = "FILE_INVALID_PATH"; FileOperationResult[FileOperationResult["FILE_EXCEEDS_MEMORY_LIMIT"] = 9] = "FILE_EXCEEDS_MEMORY_LIMIT"; FileOperationResult[FileOperationResult["FILE_NOT_DIRECTORY"] = 10] = "FILE_NOT_DIRECTORY"; FileOperationResult[FileOperationResult["FILE_OTHER_ERROR"] = 11] = "FILE_OTHER_ERROR"; return FileOperationResult; }({}); var FileType = exports.FileType = /*#__PURE__*/function (FileType) { FileType[FileType["Unknown"] = 0] = "Unknown"; FileType[FileType["File"] = 1] = "File"; FileType[FileType["Directory"] = 2] = "Directory"; FileType[FileType["SymbolicLink"] = 64] = "SymbolicLink"; return FileType; }({}); var FileSystemProviderCapabilities = exports.FileSystemProviderCapabilities = /*#__PURE__*/function (FileSystemProviderCapabilities) { FileSystemProviderCapabilities[FileSystemProviderCapabilities["FileReadWrite"] = 2] = "FileReadWrite"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["FileOpenReadWriteClose"] = 4] = "FileOpenReadWriteClose"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["FileReadStream"] = 16] = "FileReadStream"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["FileFolderCopy"] = 8] = "FileFolderCopy"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["PathCaseSensitive"] = 1024] = "PathCaseSensitive"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["Readonly"] = 2048] = "Readonly"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["Trash"] = 4096] = "Trash"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["Access"] = 16777216] = "Access"; FileSystemProviderCapabilities[FileSystemProviderCapabilities["Update"] = 33554432] = "Update"; return FileSystemProviderCapabilities; }({}); var FileSystemProviderErrorCode = exports.FileSystemProviderErrorCode = /*#__PURE__*/function (FileSystemProviderErrorCode) { FileSystemProviderErrorCode["FileExists"] = "EntryExists"; FileSystemProviderErrorCode["FileNotFound"] = "EntryNotFound"; FileSystemProviderErrorCode["FileNotADirectory"] = "EntryNotADirectory"; FileSystemProviderErrorCode["FileIsADirectory"] = "EntryIsADirectory"; FileSystemProviderErrorCode["FileExceedsMemoryLimit"] = "EntryExceedsMemoryLimit"; FileSystemProviderErrorCode["FileTooLarge"] = "EntryTooLarge"; FileSystemProviderErrorCode["NoPermissions"] = "NoPermissions"; FileSystemProviderErrorCode["Unavailable"] = "Unavailable"; FileSystemProviderErrorCode["Unknown"] = "Unknown"; return FileSystemProviderErrorCode; }({}); var FileSystemProviderError = exports.FileSystemProviderError = /*#__PURE__*/function (_Error2) { _inherits(FileSystemProviderError, _Error2); var _super2 = _createSuper(FileSystemProviderError); function FileSystemProviderError(message, code) { var _this2; _classCallCheck(this, FileSystemProviderError); _this2 = _super2.call(this, message); _this2.code = code; Object.setPrototypeOf(_assertThisInitialized(_this2), FileSystemProviderError.prototype); return _this2; } return _createClass(FileSystemProviderError); }( /*#__PURE__*/_wrapNativeSuper(Error)); function createFileSystemProviderError(error, code) { var providerError = new FileSystemProviderError(error.toString(), code); markAsFileSystemProviderError(providerError, code); return providerError; } function ensureFileSystemProviderError(error) { if (!error) { return createFileSystemProviderError('Unknown Error', FileSystemProviderErrorCode.Unknown); // https://github.com/Microsoft/vscode/issues/72798 } return error; } var FileSystemProvider = exports.FileSystemProvider = Symbol('FileSystemProvider'); /** * A {@link FileSystemProvider} provides the capabilities to read, write, discover, and to manage files and folders * of the underlying (potentially virtual) file system. {@link FileSystemProvider}s can be used to serve files from both the * local disk as well as remote locations like ftp-servers, REST-services etc. A {@link FileSystemProvider} is registered for a certain * scheme and can handle all resources whose uri does conform to that scheme. */ /** * Subtype of {@link FileSystemProvider} that ensures that the optional functions needed for providers, that should be * able access files, are implemented. */ function hasAccessCapability(provider) { return !!(provider.capabilities & FileSystemProviderCapabilities.Access); } /** * Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers * that should be able to read & write files, are implemented. */ function hasReadWriteCapability(provider) { return !!(provider.capabilities & FileSystemProviderCapabilities.FileReadWrite); } /** * Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers that should be able to copy * file folders, are implemented. */ function hasFileFolderCopyCapability(provider) { return !!(provider.capabilities & FileSystemProviderCapabilities.FileFolderCopy); } /** * Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers that should be able to open,read, write * or close files, are implemented. */ function hasOpenReadWriteCloseCapability(provider) { return !!(provider.capabilities & FileSystemProviderCapabilities.FileOpenReadWriteClose); } function markAsFileSystemProviderError(error, code) { error.name = code ? "".concat(code, " (FileSystemError)") : 'FileSystemError'; return error; } function toFileSystemProviderErrorCode(error) { // Guard against abuse if (!error) { return FileSystemProviderErrorCode.Unknown; } // FileSystemProviderError comes with the code if (error instanceof FileSystemProviderError) { return error.code; } // Any other error, check for name match by assuming that the error // went through the markAsFileSystemProviderError() method var match = /^(.+) \(FileSystemError\)$/.exec(error.name); if (!match) { return FileSystemProviderErrorCode.Unknown; } // eslint-disable-next-line default-case switch (match[1]) { case FileSystemProviderErrorCode.FileExists: return FileSystemProviderErrorCode.FileExists; case FileSystemProviderErrorCode.FileIsADirectory: return FileSystemProviderErrorCode.FileIsADirectory; case FileSystemProviderErrorCode.FileNotADirectory: return FileSystemProviderErrorCode.FileNotADirectory; case FileSystemProviderErrorCode.FileNotFound: return FileSystemProviderErrorCode.FileNotFound; case FileSystemProviderErrorCode.FileExceedsMemoryLimit: return FileSystemProviderErrorCode.FileExceedsMemoryLimit; case FileSystemProviderErrorCode.FileTooLarge: return FileSystemProviderErrorCode.FileTooLarge; case FileSystemProviderErrorCode.NoPermissions: return FileSystemProviderErrorCode.NoPermissions; case FileSystemProviderErrorCode.Unavailable: return FileSystemProviderErrorCode.Unavailable; } return FileSystemProviderErrorCode.Unknown; } function toFileOperationResult(error) { // FileSystemProviderError comes with the result already if (error instanceof FileOperationError) { return error.fileOperationResult; } // Otherwise try to find from code switch (toFileSystemProviderErrorCode(error)) { case FileSystemProviderErrorCode.FileNotFound: return FileOperationResult.FILE_NOT_FOUND; case FileSystemProviderErrorCode.FileIsADirectory: return FileOperationResult.FILE_IS_DIRECTORY; case FileSystemProviderErrorCode.FileNotADirectory: return FileOperationResult.FILE_NOT_DIRECTORY; case FileSystemProviderErrorCode.NoPermissions: return FileOperationResult.FILE_PERMISSION_DENIED; case FileSystemProviderErrorCode.FileExists: return FileOperationResult.FILE_MOVE_CONFLICT; case FileSystemProviderErrorCode.FileExceedsMemoryLimit: return FileOperationResult.FILE_EXCEEDS_MEMORY_LIMIT; case FileSystemProviderErrorCode.FileTooLarge: return FileOperationResult.FILE_TOO_LARGE; default: return FileOperationResult.FILE_OTHER_ERROR; } } /** * A hint to disable etag checking for reading/writing. */ var ETAG_DISABLED = exports.ETAG_DISABLED = ''; function etag(stat) { if (typeof stat.size !== 'number' || typeof stat.mtime !== 'number') { return undefined; } return stat.mtime.toString(29) + stat.size.toString(31); } /** * Helper to format a raw byte size into a human readable label. */ var BinarySize = exports.BinarySize = /*#__PURE__*/function () { function BinarySize() { _classCallCheck(this, BinarySize); } _createClass(BinarySize, null, [{ key: "formatSize", value: function formatSize(size) { if (size < BinarySize.KB) { return "".concat(size, "B"); } if (size < BinarySize.MB) { return "".concat((size / BinarySize.KB).toFixed(2), "KB"); } if (size < BinarySize.GB) { return "".concat((size / BinarySize.MB).toFixed(2), "MB"); } if (size < BinarySize.TB) { return "".concat((size / BinarySize.GB).toFixed(2), "GB"); } return "".concat((size / BinarySize.TB).toFixed(2), "TB"); } }]); return BinarySize; }(); _BinarySize = BinarySize; BinarySize.KB = 1024; BinarySize.MB = _BinarySize.KB * _BinarySize.KB; BinarySize.GB = _BinarySize.MB * _BinarySize.KB; BinarySize.TB = _BinarySize.GB * _BinarySize.KB;