Changes
2 changed files (+57/-0)
-
-
@@ -0,0 +1,26 @@// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com> // // SPDX-License-Identifier: Apache-2.0 import { assertEquals, assertExists, } from "../deps/deno.land/std/assert/mod.ts"; import { MemoryFsWriter } from "./memory_fs.ts"; Deno.test("Should write to an internal key-value store", async () => { const writer = new MemoryFsWriter(); const content = new TextEncoder().encode("Hello, World!\n"); await writer.write(["foo", "bar.txt"], content); const wrote = writer.get(["foo", "bar.txt"]); assertExists(wrote); assertEquals( new TextDecoder().decode(wrote), "Hello, World!\n", ); });
-
-
-
@@ -0,0 +1,31 @@// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com> // // SPDX-License-Identifier: Apache-2.0 import type { FileSystemWriter, WriteOptions } from "./interface.ts"; const SEP = "/"; export class MemoryFsWriter implements FileSystemWriter { #files: Map<string, Uint8Array> = new Map(); async write( path: readonly string[], content: Uint8Array, opts: WriteOptions = {}, ): Promise<void> { const key = path.join(SEP); if (opts.errorOnOverwrite) { if (this.#files.has(key)) { throw new Error(`Attempt to overwrite file at "${key}"`); } } this.#files.set(key, content); } get(path: readonly string[]): Uint8Array | null { return this.#files.get(path.join(SEP)) ?? null; } }
-