test(storage): 메모리 OPFS 파일 시스템을 테스트 helper로 옮긴다
문제: OPFS worker runtime을 메모리에서 돌리는 MemoryDirectory·MemoryFile이 tests/unit/opfs-worker-runtime.test.ts 안에만 있다. 05-04를 고치려면 recipe fake와 runtime에 같은 시나리오를 돌리는 parity 테스트가 tests/recipes에 필요한데, 그 테스트는 이 파일 시스템을 쓸 수 없다. unit 테스트가 recipe를 import하면 optional-recipe 제거 드릴이 깨지므로 parity 테스트를 unit 쪽에 둘 수도 없다. 고른 방법: 그 블록(MemoryNode·MemoryFile·MemoryDirectory·fileHandle· notFound)을 그대로 tests/helpers/memory-opfs.ts로 옮기고 export한다. memory-indexeddb.ts와 같은 자리다. 코드는 바꾸지 않았다. helper는 src를 import하지 않아 browser-file-storage 제거 드릴에서도 남아도 된다. 위험·한계: 동작 변경 없음. 검증: opfs-worker-runtime.test.ts 82건 통과. check:types:test, 바꾼 파일 eslint 통과. Refs: docs/reviews/2026-11-03-maturity-review/05-adapters-storage-kernel.md 05-04
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* An in-memory Origin Private File System for the OPFS worker runtime: just
|
||||
* the `FileSystemDirectoryHandle` / `FileSystemFileHandle` surface it uses.
|
||||
* Moved out of `tests/unit/opfs-worker-runtime.test.ts` so the recipe tests
|
||||
* can run the same runtime (2026-11-03 05-04).
|
||||
*/
|
||||
|
||||
export type MemoryNode = MemoryDirectory | MemoryFile;
|
||||
|
||||
export class MemoryFile {
|
||||
readonly kind = "file";
|
||||
bytes = new Uint8Array();
|
||||
lastModified = 0;
|
||||
reads = 0;
|
||||
}
|
||||
|
||||
export class MemoryDirectory {
|
||||
readonly kind = "directory";
|
||||
readonly children = new Map<string, MemoryNode>();
|
||||
|
||||
async getDirectoryHandle(
|
||||
name: string,
|
||||
options: FileSystemGetDirectoryOptions = {},
|
||||
): Promise<FileSystemDirectoryHandle> {
|
||||
const current = this.children.get(name);
|
||||
if (current instanceof MemoryDirectory) {
|
||||
return current as unknown as FileSystemDirectoryHandle;
|
||||
}
|
||||
if (current || !options.create) throw notFound();
|
||||
const created = new MemoryDirectory();
|
||||
this.children.set(name, created);
|
||||
return created as unknown as FileSystemDirectoryHandle;
|
||||
}
|
||||
|
||||
async getFileHandle(
|
||||
name: string,
|
||||
options: FileSystemGetFileOptions = {},
|
||||
): Promise<FileSystemFileHandle> {
|
||||
const current = this.children.get(name);
|
||||
if (current instanceof MemoryFile) {
|
||||
return fileHandle(current);
|
||||
}
|
||||
if (current || !options.create) throw notFound();
|
||||
const created = new MemoryFile();
|
||||
this.children.set(name, created);
|
||||
return fileHandle(created);
|
||||
}
|
||||
|
||||
async removeEntry(
|
||||
name: string,
|
||||
options: FileSystemRemoveOptions = {},
|
||||
): Promise<void> {
|
||||
const current = this.children.get(name);
|
||||
if (!current) throw notFound();
|
||||
if (
|
||||
current instanceof MemoryDirectory &&
|
||||
current.children.size > 0 &&
|
||||
!options.recursive
|
||||
) {
|
||||
throw new DOMException("Directory is not empty.", "InvalidModificationError");
|
||||
}
|
||||
this.children.delete(name);
|
||||
}
|
||||
|
||||
async *entries(): AsyncIterableIterator<
|
||||
[string, FileSystemDirectoryHandle | FileSystemFileHandle]
|
||||
> {
|
||||
for (const [name, node] of this.children) {
|
||||
yield [
|
||||
name,
|
||||
node instanceof MemoryDirectory
|
||||
? (node as unknown as FileSystemDirectoryHandle)
|
||||
: fileHandle(node),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
has(path: readonly string[]): boolean {
|
||||
let current: MemoryNode = this;
|
||||
for (const segment of path) {
|
||||
if (!(current instanceof MemoryDirectory)) return false;
|
||||
const next = current.children.get(segment);
|
||||
if (!next) return false;
|
||||
current = next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function fileHandle(file: MemoryFile): FileSystemFileHandle {
|
||||
return {
|
||||
kind: "file",
|
||||
name: "memory",
|
||||
async getFile() {
|
||||
file.reads += 1;
|
||||
const blob = new Blob([Uint8Array.from(file.bytes)]);
|
||||
Object.defineProperty(blob, "lastModified", {
|
||||
value: file.lastModified,
|
||||
});
|
||||
return blob as File;
|
||||
},
|
||||
async createWritable() {
|
||||
let pending = Uint8Array.from(file.bytes);
|
||||
return {
|
||||
async write(data: FileSystemWriteChunkType) {
|
||||
if (!(data instanceof Uint8Array)) {
|
||||
throw new TypeError("The test writer accepts Uint8Array only.");
|
||||
}
|
||||
pending = Uint8Array.from(data);
|
||||
},
|
||||
async close() {
|
||||
file.bytes = pending;
|
||||
file.lastModified = Date.now();
|
||||
},
|
||||
async abort() {},
|
||||
} as unknown as FileSystemWritableFileStream;
|
||||
},
|
||||
} as FileSystemFileHandle;
|
||||
}
|
||||
|
||||
function notFound(): DOMException {
|
||||
return new DOMException("Entry was not found.", "NotFoundError");
|
||||
}
|
||||
@@ -38,6 +38,11 @@ import {
|
||||
type OpfsWorkerRuntime,
|
||||
} from "../../src/adapters/storage/opfs/opfs-worker-runtime.ts";
|
||||
import { MemoryIndexedDbFactory } from "../helpers/memory-indexeddb.ts";
|
||||
import {
|
||||
MemoryDirectory,
|
||||
MemoryFile,
|
||||
type MemoryNode,
|
||||
} from "../helpers/memory-opfs.ts";
|
||||
|
||||
const scopeA: OpfsStorageScope = Object.freeze({
|
||||
namespace: "durable-objects",
|
||||
@@ -71,123 +76,6 @@ const runtimePolicy = resolveOpfsRuntimePolicy({
|
||||
orphanGracePeriodMs: 60_000,
|
||||
});
|
||||
|
||||
type MemoryNode = MemoryDirectory | MemoryFile;
|
||||
|
||||
class MemoryFile {
|
||||
readonly kind = "file";
|
||||
bytes = new Uint8Array();
|
||||
lastModified = 0;
|
||||
reads = 0;
|
||||
}
|
||||
|
||||
class MemoryDirectory {
|
||||
readonly kind = "directory";
|
||||
readonly children = new Map<string, MemoryNode>();
|
||||
|
||||
async getDirectoryHandle(
|
||||
name: string,
|
||||
options: FileSystemGetDirectoryOptions = {},
|
||||
): Promise<FileSystemDirectoryHandle> {
|
||||
const current = this.children.get(name);
|
||||
if (current instanceof MemoryDirectory) {
|
||||
return current as unknown as FileSystemDirectoryHandle;
|
||||
}
|
||||
if (current || !options.create) throw notFound();
|
||||
const created = new MemoryDirectory();
|
||||
this.children.set(name, created);
|
||||
return created as unknown as FileSystemDirectoryHandle;
|
||||
}
|
||||
|
||||
async getFileHandle(
|
||||
name: string,
|
||||
options: FileSystemGetFileOptions = {},
|
||||
): Promise<FileSystemFileHandle> {
|
||||
const current = this.children.get(name);
|
||||
if (current instanceof MemoryFile) {
|
||||
return fileHandle(current);
|
||||
}
|
||||
if (current || !options.create) throw notFound();
|
||||
const created = new MemoryFile();
|
||||
this.children.set(name, created);
|
||||
return fileHandle(created);
|
||||
}
|
||||
|
||||
async removeEntry(
|
||||
name: string,
|
||||
options: FileSystemRemoveOptions = {},
|
||||
): Promise<void> {
|
||||
const current = this.children.get(name);
|
||||
if (!current) throw notFound();
|
||||
if (
|
||||
current instanceof MemoryDirectory &&
|
||||
current.children.size > 0 &&
|
||||
!options.recursive
|
||||
) {
|
||||
throw new DOMException("Directory is not empty.", "InvalidModificationError");
|
||||
}
|
||||
this.children.delete(name);
|
||||
}
|
||||
|
||||
async *entries(): AsyncIterableIterator<
|
||||
[string, FileSystemDirectoryHandle | FileSystemFileHandle]
|
||||
> {
|
||||
for (const [name, node] of this.children) {
|
||||
yield [
|
||||
name,
|
||||
node instanceof MemoryDirectory
|
||||
? (node as unknown as FileSystemDirectoryHandle)
|
||||
: fileHandle(node),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
has(path: readonly string[]): boolean {
|
||||
let current: MemoryNode = this;
|
||||
for (const segment of path) {
|
||||
if (!(current instanceof MemoryDirectory)) return false;
|
||||
const next = current.children.get(segment);
|
||||
if (!next) return false;
|
||||
current = next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function fileHandle(file: MemoryFile): FileSystemFileHandle {
|
||||
return {
|
||||
kind: "file",
|
||||
name: "memory",
|
||||
async getFile() {
|
||||
file.reads += 1;
|
||||
const blob = new Blob([Uint8Array.from(file.bytes)]);
|
||||
Object.defineProperty(blob, "lastModified", {
|
||||
value: file.lastModified,
|
||||
});
|
||||
return blob as File;
|
||||
},
|
||||
async createWritable() {
|
||||
let pending = Uint8Array.from(file.bytes);
|
||||
return {
|
||||
async write(data: FileSystemWriteChunkType) {
|
||||
if (!(data instanceof Uint8Array)) {
|
||||
throw new TypeError("The test writer accepts Uint8Array only.");
|
||||
}
|
||||
pending = Uint8Array.from(data);
|
||||
},
|
||||
async close() {
|
||||
file.bytes = pending;
|
||||
file.lastModified = Date.now();
|
||||
},
|
||||
async abort() {},
|
||||
} as unknown as FileSystemWritableFileStream;
|
||||
},
|
||||
} as FileSystemFileHandle;
|
||||
}
|
||||
|
||||
function notFound(): DOMException {
|
||||
return new DOMException("Entry was not found.", "NotFoundError");
|
||||
}
|
||||
|
||||
const PHYSICAL_GENERATION_A = "a".repeat(32) as OpfsPhysicalGenerationId;
|
||||
|
||||
function beginRequest(
|
||||
|
||||
Reference in New Issue
Block a user