UNPKG

clvm_tools

Version:

Javascript implementation of clvm_tools

179 lines (178 loc) 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readdirSync = exports.statSync = exports.existsSync = exports.writeFileSync = exports.readFileSync = exports.parseFileContent = exports.getFileObj = exports.createFileContent = void 0; const __type_compatibility__1 = require("clvm/__type_compatibility__"); function createFileContent(data, option) { let fileObj; const now = Date.now(); if (typeof data === "string") { fileObj = { mTimeMs: now, encode: "string", data, }; } else if (option && /^utf[-]?8$/i.test(option.encode)) { const decoder = new TextDecoder(option.encode); data = decoder.decode(data); fileObj = { mTimeMs: now, encode: "string", data, }; } else { data = (new __type_compatibility__1.Bytes(data)).hex(); fileObj = { mTimeMs: now, encode: "hex", data, }; } return JSON.stringify(fileObj); } exports.createFileContent = createFileContent; function getFileObj(data) { try { let fileObj; if (typeof data === "string") { fileObj = JSON.parse(data); } else if (data && typeof data === "object") { fileObj = data; } else { return false; } const isFileObj = Object.hasOwnProperty.call(fileObj, "mTimeMs") && Object.hasOwnProperty.call(fileObj, "encode") && Object.hasOwnProperty.call(fileObj, "data") && (fileObj.encode === "string" || fileObj.encode === "hex") && typeof fileObj.data === "string"; if (isFileObj) { return fileObj; } return false; } catch (e) { return false; } } exports.getFileObj = getFileObj; function parseFileContent(data, option) { const fileObj = getFileObj(data); if (!fileObj) { const errMsg = "Not a valid file object"; // printError(`Error: ${errMsg}`); throw new Error(errMsg); } if (option) { if (fileObj.encode === "hex") { const uint8 = __type_compatibility__1.Bytes.from(fileObj.data, "hex").raw(); const decoder = new TextDecoder(option.encode); return decoder.decode(uint8); } return fileObj.data; } else if (fileObj.encode === "hex") { return __type_compatibility__1.Bytes.from(fileObj.data, "hex").raw(); } const encoder = new TextEncoder(); return encoder.encode(fileObj.data); } exports.parseFileContent = parseFileContent; /** * When `option.encode` is set, it returns `string`. Otherwise it returns `Uint8Array`. * @param {string} path * @param {TFileReadWriteOption} option? */ function readFileSync(path, option) { const data = window.localStorage.getItem(path); if (data === null) { const errMsg = `File not found at: ${path}`; // printError(`Error: ${errMsg}`); throw new Error(errMsg); } return parseFileContent(data, option); } exports.readFileSync = readFileSync; function writeFileSync(path, data, option) { window.localStorage.setItem(path, createFileContent(data, option)); } exports.writeFileSync = writeFileSync; function existsSync(path) { const data = window.localStorage.getItem(path); if (data === null) { return false; } const fileObj = getFileObj(data); return Boolean(fileObj); } exports.existsSync = existsSync; function statSync(path) { const data = window.localStorage.getItem(path); if (data === null) { const errMsg = `File not found at: ${path}`; // printError(`Error: ${errMsg}`); throw new Error(errMsg); } const fileObj = getFileObj(data); if (!fileObj) { const errMsg = "Not a valid file object"; // printError(`Error: ${errMsg}`); throw new Error(errMsg); } return { isFile: () => { return true; }, mtimeMs: fileObj.mTimeMs, }; } exports.statSync = statSync; function readdirSync(path) { const n = window.localStorage.length; const dirEntries = []; for (let i = 0; i < n; i++) { const key = window.localStorage.key(i); if (!key) { continue; } const item = window.localStorage.getItem(key); if (!item) { continue; } // Check forbidden chars if (/[<>:"\\|?*]/.test(path)) { const errMsg = "path contains invalid character"; // printError(`Error: ${errMsg}`); throw new Error(errMsg); } // Remove trailing '/' path = path.replace(/[/]+$/, ""); // escape '.' path = path.replace(/[.]/g, "[.]"); const isDescendantRegex = new RegExp(`^${path}/`); if (!isDescendantRegex.test(key)) { continue; } const isDirectChildFileRegex = new RegExp(`^${path}/[^/]+$`); if (isDirectChildFileRegex.test(key)) { dirEntries.push({ name: key, isDirectory: () => false, }); continue; } const isDirectChildDirRegex = new RegExp(`^(${path}/[^/]+)/[^/]+`); const dirname = isDirectChildDirRegex.exec(key); if (dirname) { dirEntries.push({ name: dirname[1], isDirectory: () => true, }); } } return dirEntries; } exports.readdirSync = readdirSync;