Changes
2 changed files (+39/-1)
-
-
@@ -2,7 +2,10 @@ // SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>// // SPDX-License-Identifier: Apache-2.0 import { assertEquals } from "../deps/deno.land/std/assert/mod.ts"; import { assertEquals, assertRejects, } from "../deps/deno.land/std/assert/mod.ts"; import { DenoFsWriter } from "./deno_fs.ts";
-
@@ -42,3 +45,25 @@ } finally {await Deno.remove(root, { recursive: true }); } }); Deno.test("Should abort on attempt to write different content at same path", { // Skip this test if write permission is not granted. // Without this, simple `deno test` would fail or prompt permissions, which is annoying. ignore: writePermission.state !== "granted" || readPermission.state !== "granted", }, async () => { await Deno.mkdir(root, { recursive: true }); try { const writer = new DenoFsWriter(root); const enc = new TextEncoder(); await writer.write(["hash-test", "foo.txt"], enc.encode("Foo")); await assertRejects(() => writer.write(["hash-test", "foo.txt"], enc.encode("Bar")) ); } finally { await Deno.remove(root, { recursive: true }); } });
-
-
-
@@ -10,6 +10,7 @@ import type { FileSystemWriter } from "./interface.ts";export class DenoFsWriter implements FileSystemWriter { #root: string; #wroteHash: Map<string, Uint8Array> = new Map(); constructor(rootDirectory: string | URL) { this.#root = typeof rootDirectory === "string"
-
@@ -49,6 +50,18 @@ path: readonly string[],content: Uint8Array, ) { const resolvedPath = this.#resolve(path); const hash = new Uint8Array(await crypto.subtle.digest("SHA-1", content)); const wrote = this.#wroteHash.get(resolvedPath); if (wrote) { if (!wrote.every((b, i) => hash[i] === b)) { throw new Error( `Attempt to write different content at ${resolvedPath}`, ); } return; } this.#wroteHash.set(resolvedPath, hash); logger().debug(`Writing file at ${path.join(SEPARATOR)}`, { path,
-