UNPKG

snapshot-fs

Version:

Create a filesystem snapshot for use with memfs

139 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JSON_KIND = exports.CJSON_KIND = exports.CBOR_KIND = void 0; exports.readCBORSnapshot = readCBORSnapshot; exports.readCJSONSnapshot = readCJSONSnapshot; exports.readDirectoryJSONSnapshot = readDirectoryJSONSnapshot; exports.readSnapshot = readSnapshot; const memfs_1 = require("memfs"); const index_js_1 = require("memfs/lib/snapshot/index.js"); /** * CBOR snapshot kind */ exports.CBOR_KIND = 'cbor'; /** * Compact JSON snapshot kind */ exports.CJSON_KIND = 'cjson'; /** * {@link DirectoryJSON} snapshot kind */ exports.JSON_KIND = 'json'; /** * Reads a CBOR-encoded snapshot and populates a virtual file system. * * @param data - The CBOR-encoded data representing the snapshot. * @param options - Options for reading the snapshot. * @param options.fs - The file system to populate. Defaults to an in-memory file system. * @param options.separator - The path separator to use. * @param options.source - The root path of the source. * @returns A promise that resolves to the populated file system API. */ async function readCBORSnapshot(data, { fs = (0, memfs_1.memfs)().vol, separator, source: root } = {}) { await (0, index_js_1.fromBinarySnapshot)(data, { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment fs: fs.promises, path: root, separator, }); return fs; } /** * Reads a CJSON snapshot and populates a filesystem with its contents. * * @param data - The JSON data representing the snapshot. * @param options - Options for reading the snapshot. * @param options.fs - The filesystem to populate. Defaults to an in-memory filesystem. * @param options.separator - The path separator to use. * @param options.source - The root path for the filesystem. * @returns A promise that resolves to the populated filesystem API. */ async function readCJSONSnapshot(data, { fs = (0, memfs_1.memfs)().vol, separator, source: root } = {}) { await (0, index_js_1.fromJsonSnapshot)(data, { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment fs: fs.promises, path: root, separator, }); return fs; } /** * Reads a directory JSON snapshot and populates a filesystem with its contents. * * @param data - The JSON data representing the directory structure. Can be a string or Uint8Array. * @param options - Options for reading the snapshot. * @param options.fs - The filesystem to populate. If not provided, a new in-memory filesystem will be created. * @param options.separator - The path separator to use. * @param options.source - The root path for the source filesystem. * * @returns A promise that resolves to the populated filesystem. * * @throws Will throw an error if the JSON data is invalid. */ async function readDirectoryJSONSnapshot(data, { fs, separator, source: root } = {}) { const dirJson = (typeof data === 'string' ? JSON.parse(data) : JSON.parse(new TextDecoder().decode(data))); const { vol } = (0, memfs_1.memfs)(); vol.fromJSON(dirJson); // if the user wanted a new filesystem, we're done. if (!fs) { return vol; } // otherwise we need to create a snapshot from the temp filesystem, // then write it out to the real filesystem (which may be real or virtual). // unfortunately there is no such `fs.fromJSON()` method :D try { // CBOR is faster and smaller than CJSON const snapshot = await (0, index_js_1.toBinarySnapshot)({ fs: vol.promises, separator }); await (0, index_js_1.fromBinarySnapshot)(snapshot, { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment fs: fs.promises, path: root, separator, }); } finally { vol.reset(); } return fs; } /** * Reads a snapshot into a new virtual filesystem or existing filesystem * * @param data Raw snapshot data in `DirectoryJSON`, CSJON or CBOR format * @param options Options * @returns The value of `options.fs` populated with the snapshot contents, or a new `memfs` `Volume`. */ async function readSnapshot(kind, data, options) { await Promise.resolve(); switch (kind) { case exports.CBOR_KIND: { try { return await readCBORSnapshot(data, options); } catch (err) { throw new Error(`Failed to read snapshot as ${kind}: ${err.message}`); } } case exports.CJSON_KIND: { try { return await readCJSONSnapshot(data, options); } catch (err) { throw new Error(`Failed to read snapshot as ${kind}: ${err.message}`); } } case exports.JSON_KIND: { try { return await readDirectoryJSONSnapshot(data, options); } catch (err) { throw new Error(`Failed to read snapshot as ${kind}: ${err.message}`); } } default: throw new TypeError(`Unknown snapshot kind: ${kind}`); } } //# sourceMappingURL=read.js.map