UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

212 lines (211 loc) 8.49 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const FileBase_1 = __importDefault(require("./FileBase")); const StorageUtilities_1 = __importStar(require("./StorageUtilities")); const axios_1 = __importDefault(require("axios")); const Log_1 = __importDefault(require("../core/Log")); class HttpFile extends FileBase_1.default { _name; _parentFolder; _pendingLoadRequests = []; _isLoading = false; get name() { return this._name; } get isContentLoaded() { return this.lastLoadedOrSaved !== null; } get parentFolder() { return this._parentFolder; } get fullPath() { return this._parentFolder.fullPath + this.name; } constructor(parentFolder, folderName) { super(); this._parentFolder = parentFolder; this._name = folderName; } async exists() { await this.loadContent(false); return this._content !== null; } async scanForChanges() { await this.loadContent(true); } async loadContent(force) { // Log.assert(this.fullPath.startsWith("/"), "Expecting a full absolute path"); if (force || this.lastLoadedOrSaved === null) { if (this._isLoading) { const pendingLoad = this._pendingLoadRequests; const prom = (resolve, reject) => { pendingLoad.push(resolve); }; await new Promise(prom); if (this.lastLoadedOrSaved === null) { throw new Error(); } return this.lastLoadedOrSaved; } else { this._content = null; const path = this.fullPath; if (StorageUtilities_1.default.getEncodingByFileName(this.name) === StorageUtilities_1.EncodingType.ByteBuffer) { try { const headers = {}; if (this._parentFolder.storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._parentFolder.storage.authToken}`; } const response = await axios_1.default.get(path, { responseType: "arraybuffer", headers, }); this._content = new Uint8Array(response.data); } catch (e) { } } else { let result = null; try { const headers = {}; if (this._parentFolder.storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._parentFolder.storage.authToken}`; } const response = await axios_1.default.get(path, { headers, }); result = response.data; if (typeof result === "object") { try { result = JSON.stringify(result, null, 2); } catch (e) { Log_1.default.fail("Could not convert file to JSON"); } } if (response.status !== 200) { Log_1.default.verbose("Could not retrieve file from '" + path + "' - response code is " + response.status); } if (result === null || result === "null") { Log_1.default.verbose("Could not retrieve file from '" + path + "' - result is null."); } } catch (e) { Log_1.default.verbose("Could not retrieve file from '" + path + "' - " + e + " - " + e?.stack); } this._content = result; } this.lastLoadedOrSaved = new Date(); this._isLoading = false; const pendingLoad = this._pendingLoadRequests; this._pendingLoadRequests = []; for (const prom of pendingLoad) { prom(undefined); } } } return this.lastLoadedOrSaved; } async deleteThisFile(skipRemoveFromParent) { if (this._parentFolder.storage.readOnly) { throw new Error("HttpFile is read-only."); } try { const path = this.fullPath; const headers = {}; if (this._parentFolder.storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._parentFolder.storage.authToken}`; } await axios_1.default.delete(path, { headers }); if (!skipRemoveFromParent) { this._parentFolder.removeFile(this.name); } return true; } catch (e) { Log_1.default.debug("Failed to delete file: " + e); return false; } } async moveTo(newStorageRelativePath) { throw new Error("HttpFile does not support move operations."); } setContent(newContent) { if (this._parentFolder.storage.readOnly) { throw new Error("HttpFile is read-only."); } this._content = newContent; this.modified = new Date(); return true; } async saveContent() { if (this._parentFolder.storage.readOnly) { throw new Error("HttpFile is read-only."); } if (this._content === null) { throw new Error("Cannot save file with null content"); } try { const path = this.fullPath; const headers = {}; if (this._parentFolder.storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._parentFolder.storage.authToken}`; } // Determine content type based on file type const encoding = StorageUtilities_1.default.getEncodingByFileName(this.name); if (encoding === StorageUtilities_1.EncodingType.ByteBuffer) { headers["Content-Type"] = "application/octet-stream"; } else { headers["Content-Type"] = "text/plain; charset=utf-8"; } await axios_1.default.put(path, this._content, { headers }); this.lastLoadedOrSaved = new Date(); this.modified = null; return this.lastLoadedOrSaved; } catch (e) { Log_1.default.debug("Failed to save file: " + e); throw new Error("Failed to save file: " + e); } } } exports.default = HttpFile;