@pgxsinkit/pgwasm 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +111 -0
- package/README.md +56 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +21 -0
- package/dist/build/index.js.map +9 -0
- package/dist/build/seam.d.ts +174 -0
- package/dist/build/seam.d.ts.map +1 -0
- package/dist/chunk-0w227vw2.js +233 -0
- package/dist/chunk-0w227vw2.js.map +11 -0
- package/dist/chunk-74gcrk47.js +563 -0
- package/dist/chunk-74gcrk47.js.map +14 -0
- package/dist/chunk-at1py5s5.js +373 -0
- package/dist/chunk-at1py5s5.js.map +12 -0
- package/dist/chunk-cpsnmzrf.js +126 -0
- package/dist/chunk-cpsnmzrf.js.map +10 -0
- package/dist/chunk-d82gg1hx.js +306 -0
- package/dist/chunk-d82gg1hx.js.map +10 -0
- package/dist/chunk-gjf102c9.js +1005 -0
- package/dist/chunk-gjf102c9.js.map +16 -0
- package/dist/chunk-kb3rsr7n.js +19 -0
- package/dist/chunk-kb3rsr7n.js.map +9 -0
- package/dist/chunk-tdvrvgt5.js +83 -0
- package/dist/chunk-tdvrvgt5.js.map +11 -0
- package/dist/chunk-z0c40tja.js +50 -0
- package/dist/chunk-z0c40tja.js.map +10 -0
- package/dist/core/data-dir-archive.d.ts +17 -0
- package/dist/core/data-dir-archive.d.ts.map +1 -0
- package/dist/core/internals.d.ts +59 -0
- package/dist/core/internals.d.ts.map +1 -0
- package/dist/core/marker.d.ts +35 -0
- package/dist/core/marker.d.ts.map +1 -0
- package/dist/core/mutex.d.ts +9 -0
- package/dist/core/mutex.d.ts.map +1 -0
- package/dist/core/names.d.ts +11 -0
- package/dist/core/names.d.ts.map +1 -0
- package/dist/core/parse.d.ts +11 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/core/pgwasm.d.ts +53 -0
- package/dist/core/pgwasm.d.ts.map +1 -0
- package/dist/core/storage.d.ts +14 -0
- package/dist/core/storage.d.ts.map +1 -0
- package/dist/create.d.ts +10 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/drizzle/codecs.d.ts +9 -0
- package/dist/drizzle/codecs.d.ts.map +1 -0
- package/dist/drizzle/driver.d.ts +50 -0
- package/dist/drizzle/driver.d.ts.map +1 -0
- package/dist/drizzle/index.d.ts +4 -0
- package/dist/drizzle/index.d.ts.map +1 -0
- package/dist/drizzle/index.js +261 -0
- package/dist/drizzle/index.js.map +12 -0
- package/dist/drizzle/session.d.ts +39 -0
- package/dist/drizzle/session.d.ts.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fs/base-filesystem.d.ts +118 -0
- package/dist/fs/base-filesystem.d.ts.map +1 -0
- package/dist/fs/index.d.ts +2 -0
- package/dist/fs/index.d.ts.map +1 -0
- package/dist/fs/index.js +11 -0
- package/dist/fs/index.js.map +9 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +9 -0
- package/dist/interface.d.ts +165 -0
- package/dist/interface.d.ts.map +1 -0
- package/dist/live/debounce-mutex.d.ts +6 -0
- package/dist/live/debounce-mutex.d.ts.map +1 -0
- package/dist/live/format-query.d.ts +8 -0
- package/dist/live/format-query.d.ts.map +1 -0
- package/dist/live/index.d.ts +9 -0
- package/dist/live/index.d.ts.map +1 -0
- package/dist/live/index.js +601 -0
- package/dist/live/index.js.map +12 -0
- package/dist/live/interface.d.ts +86 -0
- package/dist/live/interface.d.ts.map +1 -0
- package/dist/opfs/broker/client.d.ts +163 -0
- package/dist/opfs/broker/client.d.ts.map +1 -0
- package/dist/opfs/broker/protocol.d.ts +326 -0
- package/dist/opfs/broker/protocol.d.ts.map +1 -0
- package/dist/opfs/broker/server.d.ts +84 -0
- package/dist/opfs/broker/server.d.ts.map +1 -0
- package/dist/opfs/core/checksum.d.ts +3 -0
- package/dist/opfs/core/checksum.d.ts.map +1 -0
- package/dist/opfs/core/codec.d.ts +73 -0
- package/dist/opfs/core/codec.d.ts.map +1 -0
- package/dist/opfs/core/errors.d.ts +80 -0
- package/dist/opfs/core/errors.d.ts.map +1 -0
- package/dist/opfs/core/limits.d.ts +35 -0
- package/dist/opfs/core/limits.d.ts.map +1 -0
- package/dist/opfs/core/memory-port.d.ts +51 -0
- package/dist/opfs/core/memory-port.d.ts.map +1 -0
- package/dist/opfs/core/metadata-store.d.ts +19 -0
- package/dist/opfs/core/metadata-store.d.ts.map +1 -0
- package/dist/opfs/core/mounted-vfs.d.ts +119 -0
- package/dist/opfs/core/mounted-vfs.d.ts.map +1 -0
- package/dist/opfs/core/path.d.ts +14 -0
- package/dist/opfs/core/path.d.ts.map +1 -0
- package/dist/opfs/core/port.d.ts +30 -0
- package/dist/opfs/core/port.d.ts.map +1 -0
- package/dist/opfs/core/repacked-vfs.d.ts +138 -0
- package/dist/opfs/core/repacked-vfs.d.ts.map +1 -0
- package/dist/opfs/core/state-machine.d.ts +296 -0
- package/dist/opfs/core/state-machine.d.ts.map +1 -0
- package/dist/opfs/create.d.ts +50 -0
- package/dist/opfs/create.d.ts.map +1 -0
- package/dist/opfs/file-port.d.ts +14 -0
- package/dist/opfs/file-port.d.ts.map +1 -0
- package/dist/opfs/index.d.ts +37 -0
- package/dist/opfs/index.d.ts.map +1 -0
- package/dist/opfs/index.js +6671 -0
- package/dist/opfs/index.js.map +28 -0
- package/dist/opfs/opfs-port.d.ts +34 -0
- package/dist/opfs/opfs-port.d.ts.map +1 -0
- package/dist/opfs/opfs-repacked-fs.d.ts +55 -0
- package/dist/opfs/opfs-repacked-fs.d.ts.map +1 -0
- package/dist/opfs/wasi/preview1.d.ts +228 -0
- package/dist/opfs/wasi/preview1.d.ts.map +1 -0
- package/dist/protocol/index.d.ts +15 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +23 -0
- package/dist/protocol/index.js.map +10 -0
- package/dist/protocol/wire/buffer-reader.d.ts +13 -0
- package/dist/protocol/wire/buffer-reader.d.ts.map +1 -0
- package/dist/protocol/wire/buffer-writer.d.ts +12 -0
- package/dist/protocol/wire/buffer-writer.d.ts.map +1 -0
- package/dist/protocol/wire/index.d.ts +5 -0
- package/dist/protocol/wire/index.d.ts.map +1 -0
- package/dist/protocol/wire/messages.d.ts +194 -0
- package/dist/protocol/wire/messages.d.ts.map +1 -0
- package/dist/protocol/wire/parser.d.ts +12 -0
- package/dist/protocol/wire/parser.d.ts.map +1 -0
- package/dist/protocol/wire/serializer.d.ts +61 -0
- package/dist/protocol/wire/serializer.d.ts.map +1 -0
- package/dist/protocol/wire/string-utils.d.ts +6 -0
- package/dist/protocol/wire/string-utils.d.ts.map +1 -0
- package/dist/protocol/wire/types.d.ts +7 -0
- package/dist/protocol/wire/types.d.ts.map +1 -0
- package/dist/tar/gzip.d.ts +7 -0
- package/dist/tar/gzip.d.ts.map +1 -0
- package/dist/tar/tar.d.ts +39 -0
- package/dist/tar/tar.d.ts.map +1 -0
- package/dist/templating.d.ts +42 -0
- package/dist/templating.d.ts.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
- package/skills/opfs/SKILL.md +138 -0
- package/src/build/index.ts +23 -0
- package/src/build/seam.ts +179 -0
- package/src/core/data-dir-archive.ts +68 -0
- package/src/core/internals.ts +87 -0
- package/src/core/marker.ts +94 -0
- package/src/core/mutex.ts +16 -0
- package/src/core/names.ts +26 -0
- package/src/core/parse.ts +79 -0
- package/src/core/pgwasm.ts +876 -0
- package/src/core/storage.ts +70 -0
- package/src/create.ts +134 -0
- package/src/drizzle/codecs.ts +114 -0
- package/src/drizzle/driver.ts +141 -0
- package/src/drizzle/index.ts +15 -0
- package/src/drizzle/session.ts +133 -0
- package/src/errors.ts +201 -0
- package/src/fs/base-filesystem.ts +149 -0
- package/src/fs/index.ts +7 -0
- package/src/index.ts +44 -0
- package/src/interface.ts +186 -0
- package/src/live/debounce-mutex.ts +45 -0
- package/src/live/format-query.ts +47 -0
- package/src/live/index.ts +707 -0
- package/src/live/interface.ts +109 -0
- package/src/opfs/broker/client.ts +482 -0
- package/src/opfs/broker/protocol.ts +624 -0
- package/src/opfs/broker/server.ts +522 -0
- package/src/opfs/core/checksum.ts +85 -0
- package/src/opfs/core/codec.ts +1051 -0
- package/src/opfs/core/errors.ts +134 -0
- package/src/opfs/core/limits.ts +68 -0
- package/src/opfs/core/memory-port.ts +459 -0
- package/src/opfs/core/metadata-store.ts +512 -0
- package/src/opfs/core/mounted-vfs.ts +407 -0
- package/src/opfs/core/path.ts +82 -0
- package/src/opfs/core/port.ts +114 -0
- package/src/opfs/core/repacked-vfs.ts +1464 -0
- package/src/opfs/core/state-machine.ts +1699 -0
- package/src/opfs/create.ts +125 -0
- package/src/opfs/file-port.ts +183 -0
- package/src/opfs/index.ts +113 -0
- package/src/opfs/opfs-port.ts +90 -0
- package/src/opfs/opfs-repacked-fs.ts +210 -0
- package/src/opfs/wasi/preview1.ts +1016 -0
- package/src/protocol/index.ts +24 -0
- package/src/protocol/wire/buffer-reader.ts +60 -0
- package/src/protocol/wire/buffer-writer.ts +93 -0
- package/src/protocol/wire/index.ts +15 -0
- package/src/protocol/wire/messages.ts +332 -0
- package/src/protocol/wire/parser.ts +373 -0
- package/src/protocol/wire/serializer.ts +233 -0
- package/src/protocol/wire/string-utils.ts +18 -0
- package/src/protocol/wire/types.ts +12 -0
- package/src/tar/gzip.ts +36 -0
- package/src/tar/tar.ts +253 -0
- package/src/templating.ts +103 -0
- package/src/types.ts +283 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export const FS_ERRNO = {
|
|
2
|
+
EBADF: 8,
|
|
3
|
+
EEXIST: 20,
|
|
4
|
+
EINVAL: 28,
|
|
5
|
+
/** The core reports this only as {@link StoreFailedError}'s `code`: the store failed, not the caller. */
|
|
6
|
+
EIO: 29,
|
|
7
|
+
EISDIR: 31,
|
|
8
|
+
ELOOP: 32,
|
|
9
|
+
ENOENT: 44,
|
|
10
|
+
ENOTDIR: 54,
|
|
11
|
+
ENOTEMPTY: 55,
|
|
12
|
+
EXDEV: 75,
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
export type FsErrorName = keyof typeof FS_ERRNO;
|
|
16
|
+
|
|
17
|
+
/** A normal virtual-filesystem rejection. The live store remains usable. */
|
|
18
|
+
export class FsError extends Error {
|
|
19
|
+
readonly code: number;
|
|
20
|
+
readonly operation: string | undefined;
|
|
21
|
+
readonly path: string | undefined;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
name: FsErrorName,
|
|
25
|
+
message: string,
|
|
26
|
+
options: { operation?: string; path?: string; cause?: unknown } = {},
|
|
27
|
+
) {
|
|
28
|
+
super(message, { cause: options.cause });
|
|
29
|
+
this.name = "FsError";
|
|
30
|
+
this.code = FS_ERRNO[name];
|
|
31
|
+
this.operation = options.operation;
|
|
32
|
+
this.path = options.path;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A configured or format hard limit was reached. */
|
|
37
|
+
export class StoreLimitError extends Error {
|
|
38
|
+
readonly storeCode = "STORE_LIMIT";
|
|
39
|
+
|
|
40
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
41
|
+
super(message, options);
|
|
42
|
+
this.name = "StoreLimitError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Activated bytes violate format integrity or semantic invariants. */
|
|
47
|
+
export class CorruptStoreError extends Error {
|
|
48
|
+
readonly storeCode = "CORRUPT_STORE";
|
|
49
|
+
|
|
50
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
51
|
+
super(message, options);
|
|
52
|
+
this.name = "CorruptStoreError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The store identifies a different format and must be deleted in full. */
|
|
57
|
+
export class StoreRecreationRequiredError extends Error {
|
|
58
|
+
readonly storeCode = "STORE_RECREATION_REQUIRED";
|
|
59
|
+
|
|
60
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
61
|
+
super(message, options);
|
|
62
|
+
this.name = "StoreRecreationRequiredError";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A supplied creation extent size disagrees with the existing store identity. */
|
|
67
|
+
export class ExtentSizeMismatchError extends Error {
|
|
68
|
+
readonly storeCode = "EXTENT_SIZE_MISMATCH";
|
|
69
|
+
|
|
70
|
+
constructor(expected: number, actual: number) {
|
|
71
|
+
super(`configured extent size ${expected} does not match persisted extent size ${actual}`);
|
|
72
|
+
this.name = "ExtentSizeMismatchError";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Another live instance owns at least one required exclusive OPFS handle. */
|
|
77
|
+
export class StoreOwnedError extends Error {
|
|
78
|
+
readonly storeCode = "STORE_OWNED";
|
|
79
|
+
|
|
80
|
+
constructor(options?: ErrorOptions) {
|
|
81
|
+
super("the OPFS repacked store is already exclusively owned by another live instance", options);
|
|
82
|
+
this.name = "StoreOwnedError";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The dedicated store directory contains an entry outside the exact owned set. */
|
|
87
|
+
export class UnexpectedStoreEntryError extends Error {
|
|
88
|
+
readonly storeCode = "UNEXPECTED_STORE_ENTRY";
|
|
89
|
+
|
|
90
|
+
constructor(entryName: string) {
|
|
91
|
+
super(`the OPFS repacked store contains an unexpected entry: ${entryName}`);
|
|
92
|
+
this.name = "UnexpectedStoreEntryError";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The host attempted a non-awaited sync, proving the factory wiring was bypassed. */
|
|
97
|
+
export class DurabilityModeMismatchError extends Error {
|
|
98
|
+
readonly storeCode = "DURABILITY_MODE_MISMATCH";
|
|
99
|
+
|
|
100
|
+
constructor() {
|
|
101
|
+
super("the PGlite host attempted a non-awaited OPFS repacked sync");
|
|
102
|
+
this.name = "DurabilityModeMismatchError";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The adapter was used after all owned handles were closed. */
|
|
107
|
+
export class StoreClosedError extends Error {
|
|
108
|
+
readonly storeCode = "STORE_CLOSED";
|
|
109
|
+
|
|
110
|
+
constructor() {
|
|
111
|
+
super("the OPFS repacked store is closed");
|
|
112
|
+
this.name = "StoreClosedError";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The live instance is poisoned and retains its first terminal cause.
|
|
118
|
+
*
|
|
119
|
+
* It carries a numeric `code` (`EIO`), as `FsError` does, so an engine's filesystem bridge that maps
|
|
120
|
+
* coded errors to errnos (PGlite's) reports an I/O error to the engine instead of letting an exception
|
|
121
|
+
* unwind through it. It is deliberately NOT an `FsError`: an `FsError` leaves the store usable, a
|
|
122
|
+
* `StoreFailedError` means every later call fails the same way until close.
|
|
123
|
+
*/
|
|
124
|
+
export class StoreFailedError extends Error {
|
|
125
|
+
readonly storeCode = "STORE_FAILED";
|
|
126
|
+
readonly code: number = FS_ERRNO.EIO;
|
|
127
|
+
override readonly cause: unknown;
|
|
128
|
+
|
|
129
|
+
constructor(cause: unknown) {
|
|
130
|
+
super(`the OPFS repacked store is poisoned and must be reopened: ${String(cause)}`, { cause });
|
|
131
|
+
this.name = "StoreFailedError";
|
|
132
|
+
this.cause = cause;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { StoreLimitError } from "./errors";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The on-disk format identity. Bumped to 2 by the symlink entry kind: a format-1 reader meets an
|
|
5
|
+
* inode kind and a transaction record it cannot decode, and the recreate-only version policy
|
|
6
|
+
* (ADR-0048 §3) is the store's ONLY evolution mechanism — one build reads and writes exactly one
|
|
7
|
+
* version, and a store identifying another one is rejected with `StoreRecreationRequiredError`
|
|
8
|
+
* before any content mutation. There is no migration and never will be.
|
|
9
|
+
*/
|
|
10
|
+
export const FORMAT_VERSION = 2;
|
|
11
|
+
export const LIMITS_PROFILE_VERSION = 1;
|
|
12
|
+
export const ARENA_HEADER_BYTES = 8192;
|
|
13
|
+
export const MIN_EXTENT_BYTES = 8192;
|
|
14
|
+
export const MAX_EXTENT_BYTES = 16 * 1024 * 1024;
|
|
15
|
+
export const DEFAULT_EXTENT_BYTES = 64 * 1024;
|
|
16
|
+
export const MAX_COMPONENT_BYTES = 255;
|
|
17
|
+
export const MAX_PATH_BYTES = 1024;
|
|
18
|
+
export const MAX_PATH_DEPTH = 32;
|
|
19
|
+
/** How many symbolic links one path resolution may traverse before it reports `ELOOP`. */
|
|
20
|
+
export const MAX_SYMLINK_HOPS = 32;
|
|
21
|
+
export const MAX_INODES = 65_536;
|
|
22
|
+
export const MAX_EXTENTS_PER_INODE = 2 ** 20;
|
|
23
|
+
export const MAX_TOTAL_EXTENTS = 2 ** 22;
|
|
24
|
+
export const MAX_METADATA_BASE_READER_BYTES = 64 * 1024 * 1024;
|
|
25
|
+
export const MAX_METADATA_BASE_WRITER_BYTES = 32 * 1024 * 1024;
|
|
26
|
+
export const MAX_FRAME_PAYLOAD_BYTES = 1024 * 1024;
|
|
27
|
+
export const MAX_ACTIVE_LOG_BYTES = 16 * 1024 * 1024;
|
|
28
|
+
export const SOFT_ACTIVE_LOG_BYTES = 8 * 1024 * 1024;
|
|
29
|
+
export const MAX_ACTIVE_LOG_FRAMES = 32_768;
|
|
30
|
+
export const SOFT_ACTIVE_LOG_FRAMES = 16_384;
|
|
31
|
+
export const MAX_U64 = (1n << 64n) - 1n;
|
|
32
|
+
|
|
33
|
+
export function validateExtentSize(value: number): number {
|
|
34
|
+
if (
|
|
35
|
+
!Number.isSafeInteger(value) ||
|
|
36
|
+
value < MIN_EXTENT_BYTES ||
|
|
37
|
+
value > MAX_EXTENT_BYTES ||
|
|
38
|
+
value % ARENA_HEADER_BYTES !== 0
|
|
39
|
+
) {
|
|
40
|
+
throw new TypeError(
|
|
41
|
+
`extentSize must be an ${ARENA_HEADER_BYTES}-byte multiple between ${MIN_EXTENT_BYTES} and ${MAX_EXTENT_BYTES}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function checkedU64(value: bigint, label: string): bigint {
|
|
48
|
+
if (value < 0n || value > MAX_U64) {
|
|
49
|
+
throw new StoreLimitError(`${label} is outside the unsigned 64-bit range`);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function checkedSafeNumber(value: bigint, label: string): number {
|
|
55
|
+
checkedU64(value, label);
|
|
56
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
57
|
+
throw new StoreLimitError(`${label} exceeds JavaScript's safe integer range`);
|
|
58
|
+
}
|
|
59
|
+
return Number(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function checkedAdd(left: bigint, right: bigint, label: string): bigint {
|
|
63
|
+
return checkedU64(left + right, label);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function checkedMultiply(left: bigint, right: bigint, label: string): bigint {
|
|
67
|
+
return checkedU64(left * right, label);
|
|
68
|
+
}
|
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An in-memory `RepackedPort` with the same exclusive-ownership, volatile/durable, and fault-injection
|
|
3
|
+
* behaviour as the real OPFS port. It is production source, not test support: the sync broker
|
|
4
|
+
* (`../broker/`) and any engine-agnostic host need a port that works off the main thread and outside a
|
|
5
|
+
* browser, and every store-level test in the repo already diffs real behaviour against it.
|
|
6
|
+
*/
|
|
7
|
+
import { StoreOwnedError } from "./errors";
|
|
8
|
+
import { OWNED_FILE_NAMES } from "./port";
|
|
9
|
+
import type { OwnedFileName, RepackedFileHandle, RepackedPort, RepackedPortEntry } from "./port";
|
|
10
|
+
|
|
11
|
+
export type MemoryOperation = "enumerate" | "acquire" | "getSize" | "read" | "write" | "truncate" | "flush";
|
|
12
|
+
type FaultOutcome = "short" | "throw-before" | "throw-after" | "quota";
|
|
13
|
+
|
|
14
|
+
export interface MemoryFault {
|
|
15
|
+
operation: MemoryOperation;
|
|
16
|
+
outcome: FaultOutcome;
|
|
17
|
+
file?: string;
|
|
18
|
+
label?: string;
|
|
19
|
+
bytes?: number;
|
|
20
|
+
occurrence?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface WriteEffect {
|
|
24
|
+
id: number;
|
|
25
|
+
file: OwnedFileName;
|
|
26
|
+
kind: "write";
|
|
27
|
+
at: number;
|
|
28
|
+
data: Uint8Array;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface TruncateEffect {
|
|
32
|
+
id: number;
|
|
33
|
+
file: OwnedFileName;
|
|
34
|
+
kind: "truncate";
|
|
35
|
+
size: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type MemoryEffect = WriteEffect | TruncateEffect;
|
|
39
|
+
export type TerminationDecision = "absent" | "full" | number;
|
|
40
|
+
|
|
41
|
+
export interface MemoryEffectSummary {
|
|
42
|
+
id: number;
|
|
43
|
+
file: OwnedFileName;
|
|
44
|
+
operation: "write" | "truncate";
|
|
45
|
+
bytes: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface MemoryOperationSummary {
|
|
49
|
+
readonly operation: MemoryOperation;
|
|
50
|
+
readonly file: OwnedFileName | undefined;
|
|
51
|
+
readonly label: string;
|
|
52
|
+
/**
|
|
53
|
+
* Bytes this operation copied into the DURABLE image. Recorded on `flush` only, where it is the
|
|
54
|
+
* deterministic stand-in for flush cost: a flush must copy the bytes written since the previous
|
|
55
|
+
* flush, never the size of the file. Absent on every other operation.
|
|
56
|
+
*/
|
|
57
|
+
readonly copiedBytes?: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The private, still-writable form of a record: `handleFlush` fills `copiedBytes` in after the copy. */
|
|
61
|
+
interface MemoryOperationRecord {
|
|
62
|
+
readonly operation: MemoryOperation;
|
|
63
|
+
readonly file: OwnedFileName | undefined;
|
|
64
|
+
readonly label: string;
|
|
65
|
+
copiedBytes?: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function clone(bytes: Uint8Array): Uint8Array {
|
|
69
|
+
return bytes.slice();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Replay a write onto a MATERIALIZED copy, for `terminate()`. Pure, and deliberately so: the
|
|
74
|
+
* termination model rebuilds a fresh image from the durable bytes plus the effect log, and nothing in
|
|
75
|
+
* it may alias the live volatile buffer.
|
|
76
|
+
*/
|
|
77
|
+
function applyWrite(current: Uint8Array, at: number, source: Uint8Array): Uint8Array {
|
|
78
|
+
const required = at + source.byteLength;
|
|
79
|
+
const next = required <= current.byteLength ? clone(current) : new Uint8Array(required);
|
|
80
|
+
if (next.byteLength > current.byteLength) next.set(current);
|
|
81
|
+
next.set(source, at);
|
|
82
|
+
return next;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The `terminate()` twin of `applyWrite`, and pure for the same reason. */
|
|
86
|
+
function applyTruncate(current: Uint8Array, size: number): Uint8Array {
|
|
87
|
+
if (size === current.byteLength) return clone(current);
|
|
88
|
+
const next = new Uint8Array(size);
|
|
89
|
+
next.set(current.subarray(0, Math.min(size, current.byteLength)));
|
|
90
|
+
return next;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Resize a LIVE image — the volatile file or the durable one — keeping the bytes it already holds and
|
|
95
|
+
* reusing spare capacity.
|
|
96
|
+
*
|
|
97
|
+
* A live image is always `new Uint8Array(buffer, 0, length)`, so its backing buffer may be larger
|
|
98
|
+
* than the file and `bytes.buffer.byteLength` is its capacity. That separation is what keeps this port
|
|
99
|
+
* usable at real store sizes: replacing the whole array on every write and every truncate — which is
|
|
100
|
+
* what the pure `apply*` helpers above do — makes each operation cost the size of the FILE rather than
|
|
101
|
+
* the size of the operation, so an in-memory store grows QUADRATICALLY in the bytes written. Seeding a
|
|
102
|
+
* 41 MB Postgres datadir (1,477 files, ~3,000 arena writes and growth truncates) took 98 seconds that
|
|
103
|
+
* way and copied roughly 80 GB. Nothing needed those copies: the effect log holds the CALLER's source
|
|
104
|
+
* bytes rather than the file's, `durableBytes()` clones on the way out, and the two images never share
|
|
105
|
+
* a buffer — so both are safe to mutate in place, and the same seed takes about a second.
|
|
106
|
+
*
|
|
107
|
+
* Growth doubles, and any newly exposed region is zeroed: capacity left behind by an earlier truncate
|
|
108
|
+
* still holds that truncated file's bytes, and a hole must read as zeros.
|
|
109
|
+
*/
|
|
110
|
+
function resizeImage(current: Uint8Array, size: number): Uint8Array {
|
|
111
|
+
if (size <= current.byteLength) return new Uint8Array(current.buffer, 0, size);
|
|
112
|
+
if (size <= current.buffer.byteLength) {
|
|
113
|
+
const grown = new Uint8Array(current.buffer, 0, size);
|
|
114
|
+
grown.fill(0, current.byteLength, size);
|
|
115
|
+
return grown;
|
|
116
|
+
}
|
|
117
|
+
const capacity = Math.max(size, current.buffer.byteLength * 2, 64);
|
|
118
|
+
const grown = new Uint8Array(new ArrayBuffer(capacity), 0, size);
|
|
119
|
+
grown.set(current);
|
|
120
|
+
return grown;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function isOwnedFileName(name: string): name is OwnedFileName {
|
|
124
|
+
return (OWNED_FILE_NAMES as readonly string[]).includes(name);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class MemoryHandle implements RepackedFileHandle {
|
|
128
|
+
readonly name: OwnedFileName;
|
|
129
|
+
readonly #port: MemoryRepackedPort;
|
|
130
|
+
readonly #epoch: number;
|
|
131
|
+
#closed = false;
|
|
132
|
+
|
|
133
|
+
constructor(port: MemoryRepackedPort, name: OwnedFileName, epoch: number) {
|
|
134
|
+
this.#port = port;
|
|
135
|
+
this.name = name;
|
|
136
|
+
this.#epoch = epoch;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
getSize(label: string): number {
|
|
140
|
+
this.#assertOpen();
|
|
141
|
+
return this.#port.handleGetSize(this.name, label);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
read(target: Uint8Array, at: number, label: string): number {
|
|
145
|
+
this.#assertOpen();
|
|
146
|
+
return this.#port.handleRead(this.name, target, at, label);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
write(source: Uint8Array, at: number, label: string): number {
|
|
150
|
+
this.#assertOpen();
|
|
151
|
+
return this.#port.handleWrite(this.name, source, at, label);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
truncate(size: number, label: string): void {
|
|
155
|
+
this.#assertOpen();
|
|
156
|
+
this.#port.handleTruncate(this.name, size, label);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
flush(label: string): void {
|
|
160
|
+
this.#assertOpen();
|
|
161
|
+
this.#port.handleFlush(this.name, label);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
close(): void {
|
|
165
|
+
if (this.#closed) return;
|
|
166
|
+
this.#closed = true;
|
|
167
|
+
if (this.#epoch === this.#port.epoch) this.#port.handleClose(this.name);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#assertOpen(): void {
|
|
171
|
+
if (this.#closed || this.#epoch !== this.#port.epoch) {
|
|
172
|
+
throw new Error(`memory handle ${this.name} is closed`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export class MemoryRepackedPort implements RepackedPort {
|
|
178
|
+
readonly #durable = new Map<OwnedFileName, Uint8Array>();
|
|
179
|
+
readonly #volatile = new Map<OwnedFileName, Uint8Array>();
|
|
180
|
+
readonly #entryKinds = new Map<string, "file" | "directory">();
|
|
181
|
+
readonly #open = new Set<OwnedFileName>();
|
|
182
|
+
readonly #faults: MemoryFault[] = [];
|
|
183
|
+
readonly #operations: MemoryOperationRecord[] = [];
|
|
184
|
+
#effects: MemoryEffect[] = [];
|
|
185
|
+
#nextEffectId = 1;
|
|
186
|
+
#epoch = 1;
|
|
187
|
+
|
|
188
|
+
get epoch(): number {
|
|
189
|
+
return this.#epoch;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async enumerate(label: string): Promise<readonly RepackedPortEntry[]> {
|
|
193
|
+
this.#recordOperation("enumerate", undefined, label);
|
|
194
|
+
const fault = this.#takeFault("enumerate", undefined, label);
|
|
195
|
+
if (fault !== undefined) throw new Error(`injected enumerate failure ${fault.outcome}`);
|
|
196
|
+
return [...this.#entryKinds]
|
|
197
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
198
|
+
.map(([name, kind]) => ({
|
|
199
|
+
name,
|
|
200
|
+
kind,
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async acquire(name: OwnedFileName, label: string): Promise<RepackedFileHandle> {
|
|
205
|
+
this.#recordOperation("acquire", name, label);
|
|
206
|
+
const fault = this.#takeFault("acquire", name, label);
|
|
207
|
+
if (fault?.outcome === "throw-before") throw new Error("injected acquire failure before effect");
|
|
208
|
+
if (this.#open.has(name)) throw new StoreOwnedError();
|
|
209
|
+
if (this.#entryKinds.get(name) === "directory") throw new Error(`memory entry ${name} is not a file`);
|
|
210
|
+
if (!this.#volatile.has(name)) {
|
|
211
|
+
this.#volatile.set(name, new Uint8Array());
|
|
212
|
+
this.#durable.set(name, new Uint8Array());
|
|
213
|
+
this.#entryKinds.set(name, "file");
|
|
214
|
+
}
|
|
215
|
+
if (fault !== undefined) throw new Error(`injected acquire failure ${fault.outcome}`);
|
|
216
|
+
this.#open.add(name);
|
|
217
|
+
return new MemoryHandle(this, name, this.#epoch);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
injectEntry(name: string, kind: "file" | "directory"): void {
|
|
221
|
+
if (isOwnedFileName(name) && this.#open.has(name)) {
|
|
222
|
+
throw new Error(`cannot replace acquired memory entry ${name}`);
|
|
223
|
+
}
|
|
224
|
+
this.#entryKinds.set(name, kind);
|
|
225
|
+
if (isOwnedFileName(name)) {
|
|
226
|
+
// Replacing an owned entry replaces BOTH images, so anything still pending for it describes a
|
|
227
|
+
// file that no longer exists; keeping it would let a later flush or termination replay it onto
|
|
228
|
+
// the replacement and break `durable + pending effects == volatile`.
|
|
229
|
+
this.#effects = this.#effects.filter((effect) => effect.file !== name);
|
|
230
|
+
if (kind === "file") {
|
|
231
|
+
this.#volatile.set(name, new Uint8Array());
|
|
232
|
+
this.#durable.set(name, new Uint8Array());
|
|
233
|
+
} else {
|
|
234
|
+
this.#volatile.delete(name);
|
|
235
|
+
this.#durable.delete(name);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
injectFault(fault: MemoryFault): void {
|
|
241
|
+
if (fault.outcome === "quota" && fault.operation !== "truncate") {
|
|
242
|
+
throw new TypeError("quota faults are supported only for arena growth truncates");
|
|
243
|
+
}
|
|
244
|
+
if (fault.bytes !== undefined && (!Number.isSafeInteger(fault.bytes) || fault.bytes < 0)) {
|
|
245
|
+
throw new TypeError("fault byte count must be a non-negative safe integer");
|
|
246
|
+
}
|
|
247
|
+
if (fault.occurrence !== undefined && (!Number.isSafeInteger(fault.occurrence) || fault.occurrence < 0)) {
|
|
248
|
+
throw new TypeError("fault occurrence must be a non-negative safe integer");
|
|
249
|
+
}
|
|
250
|
+
this.#faults.push({ ...fault });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
pendingEffects(): readonly MemoryEffectSummary[] {
|
|
254
|
+
return this.#effects.map((effect) => ({
|
|
255
|
+
id: effect.id,
|
|
256
|
+
file: effect.file,
|
|
257
|
+
operation: effect.kind,
|
|
258
|
+
bytes: effect.kind === "write" ? effect.data.byteLength : effect.size,
|
|
259
|
+
}));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
openHandleCount(): number {
|
|
263
|
+
return this.#open.size;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
observedOperations(): readonly MemoryOperationSummary[] {
|
|
267
|
+
return Object.freeze(this.#operations.map((operation) => Object.freeze({ ...operation })));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
clearObservedOperations(): void {
|
|
271
|
+
this.#operations.length = 0;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
durableBytes(name: OwnedFileName): Uint8Array {
|
|
275
|
+
return clone(this.#durable.get(name) ?? new Uint8Array());
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
terminate(decisions: Readonly<Record<number, TerminationDecision>> = {}): void {
|
|
279
|
+
const materialized = new Map<OwnedFileName, Uint8Array>();
|
|
280
|
+
for (const [name, bytes] of this.#durable) materialized.set(name, clone(bytes));
|
|
281
|
+
for (const effect of this.#effects) {
|
|
282
|
+
const decision = decisions[effect.id] ?? "absent";
|
|
283
|
+
if (decision === "absent") continue;
|
|
284
|
+
const current = materialized.get(effect.file) ?? new Uint8Array();
|
|
285
|
+
if (effect.kind === "truncate") {
|
|
286
|
+
if (decision !== "full") {
|
|
287
|
+
throw new TypeError("truncate termination decisions must be absent or full");
|
|
288
|
+
}
|
|
289
|
+
materialized.set(effect.file, applyTruncate(current, effect.size));
|
|
290
|
+
} else {
|
|
291
|
+
const bytes = decision === "full" ? effect.data.byteLength : decision;
|
|
292
|
+
if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > effect.data.byteLength) {
|
|
293
|
+
throw new TypeError("write termination prefix is outside the effect");
|
|
294
|
+
}
|
|
295
|
+
if (bytes > 0) materialized.set(effect.file, applyWrite(current, effect.at, effect.data.subarray(0, bytes)));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
this.#durable.clear();
|
|
299
|
+
this.#volatile.clear();
|
|
300
|
+
for (const [name, bytes] of materialized) {
|
|
301
|
+
this.#durable.set(name, clone(bytes));
|
|
302
|
+
this.#volatile.set(name, clone(bytes));
|
|
303
|
+
}
|
|
304
|
+
this.#effects = [];
|
|
305
|
+
this.#faults.length = 0;
|
|
306
|
+
this.#open.clear();
|
|
307
|
+
this.#epoch += 1;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
handleGetSize(name: OwnedFileName, label: string): number {
|
|
311
|
+
this.#recordOperation("getSize", name, label);
|
|
312
|
+
const fault = this.#takeFault("getSize", name, label);
|
|
313
|
+
if (fault !== undefined) throw new Error(`injected getSize failure ${fault.outcome}`);
|
|
314
|
+
return this.#file(name).byteLength;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
handleRead(name: OwnedFileName, target: Uint8Array, at: number, label: string): number {
|
|
318
|
+
this.#validateRange(at, target.byteLength);
|
|
319
|
+
this.#recordOperation("read", name, label);
|
|
320
|
+
const fault = this.#takeFault("read", name, label);
|
|
321
|
+
if (fault?.outcome === "throw-before" || fault?.outcome === "throw-after") {
|
|
322
|
+
throw new Error(`injected read failure ${fault.outcome === "throw-before" ? "before" : "after"} effect`);
|
|
323
|
+
}
|
|
324
|
+
const source = this.#file(name);
|
|
325
|
+
const available = Math.max(0, Math.min(target.byteLength, source.byteLength - at));
|
|
326
|
+
const count = fault?.outcome === "short" ? Math.min(available, fault.bytes ?? 0) : available;
|
|
327
|
+
target.set(source.subarray(at, at + count));
|
|
328
|
+
return count;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
handleWrite(name: OwnedFileName, source: Uint8Array, at: number, label: string): number {
|
|
332
|
+
this.#validateRange(at, source.byteLength);
|
|
333
|
+
this.#recordOperation("write", name, label);
|
|
334
|
+
const fault = this.#takeFault("write", name, label);
|
|
335
|
+
if (fault?.outcome === "throw-before") throw new Error("injected write failure before effect");
|
|
336
|
+
const count =
|
|
337
|
+
fault?.outcome === "short" || fault?.outcome === "throw-after"
|
|
338
|
+
? Math.min(source.byteLength, fault.bytes ?? source.byteLength)
|
|
339
|
+
: source.byteLength;
|
|
340
|
+
if (count > 0) this.#recordWrite(name, at, source.subarray(0, count));
|
|
341
|
+
if (fault?.outcome === "throw-after") throw new Error("injected write failure after effect");
|
|
342
|
+
return count;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
handleTruncate(name: OwnedFileName, size: number, label: string): void {
|
|
346
|
+
this.#validateRange(size, 0);
|
|
347
|
+
this.#recordOperation("truncate", name, label);
|
|
348
|
+
const fault = this.#takeFault("truncate", name, label);
|
|
349
|
+
if (fault?.outcome === "quota") throw new DOMException("injected arena quota exhaustion", "QuotaExceededError");
|
|
350
|
+
if (fault?.outcome === "throw-before") throw new Error("injected truncate failure before effect");
|
|
351
|
+
this.#volatile.set(name, resizeImage(this.#file(name), size));
|
|
352
|
+
this.#effects.push({ id: this.#nextEffectId++, file: name, kind: "truncate", size });
|
|
353
|
+
if (fault?.outcome === "throw-after") throw new Error("injected truncate failure after effect");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
handleFlush(name: OwnedFileName, label: string): void {
|
|
357
|
+
const record = this.#recordOperation("flush", name, label);
|
|
358
|
+
record.copiedBytes = 0;
|
|
359
|
+
const fault = this.#takeFault("flush", name, label);
|
|
360
|
+
// A throw-before flush made nothing durable and left every effect pending, so it copies nothing.
|
|
361
|
+
if (fault?.outcome === "throw-before") throw new Error("injected flush failure before effect");
|
|
362
|
+
record.copiedBytes = this.#makeDurable(name);
|
|
363
|
+
if (fault?.outcome === "throw-after") throw new Error("injected flush failure after effect");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
handleClose(name: OwnedFileName): void {
|
|
367
|
+
this.#open.delete(name);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
#file(name: OwnedFileName): Uint8Array {
|
|
371
|
+
const bytes = this.#volatile.get(name);
|
|
372
|
+
if (bytes === undefined) throw new Error(`memory file ${name} was not acquired`);
|
|
373
|
+
return bytes;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
#recordWrite(name: OwnedFileName, at: number, source: Uint8Array): void {
|
|
377
|
+
// The effect log owns its own copy: it is replayed against a MATERIALIZED image in `terminate()`,
|
|
378
|
+
// long after the live buffer moved on.
|
|
379
|
+
const data = clone(source);
|
|
380
|
+
const current = this.#file(name);
|
|
381
|
+
const required = at + data.byteLength;
|
|
382
|
+
const target = required > current.byteLength ? resizeImage(current, required) : current;
|
|
383
|
+
target.set(data, at);
|
|
384
|
+
this.#volatile.set(name, target);
|
|
385
|
+
this.#effects.push({ id: this.#nextEffectId++, file: name, kind: "write", at, data });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Apply this file's pending effects onto the durable image, in order, and drop them. Returns the
|
|
390
|
+
* bytes copied.
|
|
391
|
+
*
|
|
392
|
+
* The old implementation made a file durable by cloning the whole volatile image, so every flush
|
|
393
|
+
* cost the size of the FILE. That is what the broker charges to every committing statement: a guest
|
|
394
|
+
* `fd_sync` becomes a store-wide `strictSync()`, and on a seeded Postgres datadir the arena is tens
|
|
395
|
+
* of MB, which showed up as a flat ~25 ms per writing statement in the browser benchmark. The effect
|
|
396
|
+
* log already IS the dirty set — `durable + pending effects == volatile` for every file, at all
|
|
397
|
+
* times — so replaying it costs the bytes written since the previous flush instead, and reproduces
|
|
398
|
+
* the volatile image byte for byte because both sides take the same steps through `resizeImage`.
|
|
399
|
+
*/
|
|
400
|
+
#makeDurable(name: OwnedFileName): number {
|
|
401
|
+
let image = this.#durable.get(name) ?? new Uint8Array();
|
|
402
|
+
const remaining: MemoryEffect[] = [];
|
|
403
|
+
let copied = 0;
|
|
404
|
+
for (const effect of this.#effects) {
|
|
405
|
+
if (effect.file !== name) {
|
|
406
|
+
remaining.push(effect);
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (effect.kind === "truncate") {
|
|
410
|
+
const resized = resizeImage(image, effect.size);
|
|
411
|
+
// Growth past the current capacity carries the bytes already held into a new buffer; every
|
|
412
|
+
// other resize is a view onto the same one. A truncate copies nothing of its own.
|
|
413
|
+
if (resized.buffer !== image.buffer) copied += image.byteLength;
|
|
414
|
+
image = resized;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const required = effect.at + effect.data.byteLength;
|
|
418
|
+
if (required > image.byteLength) {
|
|
419
|
+
const grown = resizeImage(image, required);
|
|
420
|
+
if (grown.buffer !== image.buffer) copied += image.byteLength;
|
|
421
|
+
image = grown;
|
|
422
|
+
}
|
|
423
|
+
image.set(effect.data, effect.at);
|
|
424
|
+
copied += effect.data.byteLength;
|
|
425
|
+
}
|
|
426
|
+
this.#durable.set(name, image);
|
|
427
|
+
this.#effects = remaining;
|
|
428
|
+
return copied;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
#takeFault(operation: MemoryOperation, file: string | undefined, label: string): MemoryFault | undefined {
|
|
432
|
+
const index = this.#faults.findIndex(
|
|
433
|
+
(fault) =>
|
|
434
|
+
fault.operation === operation &&
|
|
435
|
+
(fault.file === undefined || fault.file === file) &&
|
|
436
|
+
(fault.label === undefined || fault.label === label),
|
|
437
|
+
);
|
|
438
|
+
if (index < 0) return undefined;
|
|
439
|
+
const fault = this.#faults[index]!;
|
|
440
|
+
if ((fault.occurrence ?? 0) > 0) {
|
|
441
|
+
this.#faults[index] = { ...fault, occurrence: fault.occurrence! - 1 };
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
return this.#faults.splice(index, 1)[0];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
#recordOperation(operation: MemoryOperation, file: OwnedFileName | undefined, label: string): MemoryOperationRecord {
|
|
448
|
+
const record: MemoryOperationRecord = { operation, file, label };
|
|
449
|
+
this.#operations.push(record);
|
|
450
|
+
return record;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
#validateRange(at: number, length: number): void {
|
|
454
|
+
if (!Number.isSafeInteger(at) || !Number.isSafeInteger(length) || at < 0 || length < 0) {
|
|
455
|
+
throw new RangeError("memory port range is invalid");
|
|
456
|
+
}
|
|
457
|
+
if (!Number.isSafeInteger(at + length)) throw new RangeError("memory port range exceeds safe integers");
|
|
458
|
+
}
|
|
459
|
+
}
|