@ricsam/quickjs-fs 0.0.1 → 0.2.1
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/README.md +70 -43
- package/dist/cjs/directory-handle.cjs +166 -0
- package/dist/cjs/directory-handle.cjs.map +10 -0
- package/dist/cjs/file-handle.cjs +125 -0
- package/dist/cjs/file-handle.cjs.map +10 -0
- package/dist/cjs/index.cjs +43 -0
- package/dist/cjs/index.cjs.map +10 -0
- package/dist/cjs/memory-adapter.cjs +269 -0
- package/dist/cjs/memory-adapter.cjs.map +10 -0
- package/dist/cjs/node-adapter.cjs +221 -0
- package/dist/cjs/node-adapter.cjs.map +10 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/setup.cjs +140 -0
- package/dist/cjs/setup.cjs.map +10 -0
- package/dist/cjs/types.cjs +26 -0
- package/dist/cjs/types.cjs.map +9 -0
- package/dist/cjs/writable-stream.cjs +119 -0
- package/dist/cjs/writable-stream.cjs.map +10 -0
- package/dist/mjs/directory-handle.mjs +135 -0
- package/dist/mjs/directory-handle.mjs.map +10 -0
- package/dist/mjs/file-handle.mjs +94 -0
- package/dist/mjs/file-handle.mjs.map +10 -0
- package/dist/mjs/index.mjs +12 -0
- package/dist/mjs/index.mjs.map +10 -0
- package/dist/mjs/memory-adapter.mjs +237 -0
- package/dist/mjs/memory-adapter.mjs.map +10 -0
- package/dist/mjs/node-adapter.mjs +189 -0
- package/dist/mjs/node-adapter.mjs.map +10 -0
- package/dist/mjs/package.json +5 -0
- package/dist/mjs/setup.mjs +112 -0
- package/dist/mjs/setup.mjs.map +10 -0
- package/dist/mjs/types.mjs +3 -0
- package/dist/mjs/types.mjs.map +9 -0
- package/dist/mjs/writable-stream.mjs +88 -0
- package/dist/mjs/writable-stream.mjs.map +10 -0
- package/dist/types/handles/directory-handle.d.ts +12 -0
- package/dist/types/handles/file-handle.d.ts +14 -0
- package/dist/types/handles/writable-stream.d.ts +6 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/memory-adapter.d.ts +18 -0
- package/dist/types/node-adapter.d.ts +23 -0
- package/dist/types/setup.d.ts +56 -0
- package/dist/types/types.d.ts +85 -0
- package/package.json +51 -6
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// packages/fs/src/memory-adapter.ts
|
|
31
|
+
var exports_memory_adapter = {};
|
|
32
|
+
__export(exports_memory_adapter, {
|
|
33
|
+
createMemoryDirectoryHandle: () => createMemoryDirectoryHandle
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(exports_memory_adapter);
|
|
36
|
+
|
|
37
|
+
class MemoryFileHandle {
|
|
38
|
+
name;
|
|
39
|
+
entry;
|
|
40
|
+
kind = "file";
|
|
41
|
+
constructor(name, entry) {
|
|
42
|
+
this.name = name;
|
|
43
|
+
this.entry = entry;
|
|
44
|
+
}
|
|
45
|
+
async getFile() {
|
|
46
|
+
const content = this.entry.content ?? new Uint8Array;
|
|
47
|
+
return new File([content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength)], this.name, {
|
|
48
|
+
type: this.entry.type || "application/octet-stream",
|
|
49
|
+
lastModified: this.entry.lastModified
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async createWritable(options) {
|
|
53
|
+
return new MemoryWritableFileStream(this.entry, options?.keepExistingData ?? false);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class MemoryWritableFileStream {
|
|
58
|
+
entry;
|
|
59
|
+
buffer;
|
|
60
|
+
position = 0;
|
|
61
|
+
constructor(entry, keepExistingData) {
|
|
62
|
+
this.entry = entry;
|
|
63
|
+
if (keepExistingData && this.entry.content) {
|
|
64
|
+
this.buffer = new Uint8Array(this.entry.content);
|
|
65
|
+
} else {
|
|
66
|
+
this.buffer = new Uint8Array(0);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async write(data) {
|
|
70
|
+
let bytes;
|
|
71
|
+
if (typeof data === "string") {
|
|
72
|
+
bytes = new TextEncoder().encode(data);
|
|
73
|
+
} else if (data instanceof ArrayBuffer) {
|
|
74
|
+
bytes = new Uint8Array(data);
|
|
75
|
+
} else if (data instanceof Uint8Array) {
|
|
76
|
+
bytes = data;
|
|
77
|
+
} else if (data instanceof Blob) {
|
|
78
|
+
bytes = new Uint8Array(await data.arrayBuffer());
|
|
79
|
+
} else {
|
|
80
|
+
if (data.type === "seek") {
|
|
81
|
+
this.position = data.position ?? 0;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (data.type === "truncate") {
|
|
85
|
+
this.buffer = this.buffer.slice(0, data.size ?? 0);
|
|
86
|
+
if (this.position > this.buffer.length) {
|
|
87
|
+
this.position = this.buffer.length;
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (data.data === undefined)
|
|
92
|
+
return;
|
|
93
|
+
return this.write(data.data);
|
|
94
|
+
}
|
|
95
|
+
const neededSize = this.position + bytes.length;
|
|
96
|
+
if (neededSize > this.buffer.length) {
|
|
97
|
+
const newBuffer = new Uint8Array(neededSize);
|
|
98
|
+
newBuffer.set(this.buffer);
|
|
99
|
+
this.buffer = newBuffer;
|
|
100
|
+
}
|
|
101
|
+
this.buffer.set(bytes, this.position);
|
|
102
|
+
this.position += bytes.length;
|
|
103
|
+
}
|
|
104
|
+
async seek(position) {
|
|
105
|
+
this.position = position;
|
|
106
|
+
}
|
|
107
|
+
async truncate(size) {
|
|
108
|
+
if (size < this.buffer.length) {
|
|
109
|
+
this.buffer = this.buffer.slice(0, size);
|
|
110
|
+
} else if (size > this.buffer.length) {
|
|
111
|
+
const newBuffer = new Uint8Array(size);
|
|
112
|
+
newBuffer.set(this.buffer);
|
|
113
|
+
this.buffer = newBuffer;
|
|
114
|
+
}
|
|
115
|
+
if (this.position > size) {
|
|
116
|
+
this.position = size;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async close() {
|
|
120
|
+
this.entry.content = this.buffer;
|
|
121
|
+
this.entry.lastModified = Date.now();
|
|
122
|
+
}
|
|
123
|
+
async abort(_reason) {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
class MemoryDirectoryHandle {
|
|
127
|
+
name;
|
|
128
|
+
entry;
|
|
129
|
+
kind = "directory";
|
|
130
|
+
constructor(name, entry) {
|
|
131
|
+
this.name = name;
|
|
132
|
+
this.entry = entry;
|
|
133
|
+
}
|
|
134
|
+
async getFileHandle(name, options) {
|
|
135
|
+
if (!this.entry.children) {
|
|
136
|
+
this.entry.children = new Map;
|
|
137
|
+
}
|
|
138
|
+
let child = this.entry.children.get(name);
|
|
139
|
+
if (!child) {
|
|
140
|
+
if (!options?.create) {
|
|
141
|
+
throw new DOMException(`File not found: ${name}`, "NotFoundError");
|
|
142
|
+
}
|
|
143
|
+
child = {
|
|
144
|
+
kind: "file",
|
|
145
|
+
name,
|
|
146
|
+
content: new Uint8Array(0),
|
|
147
|
+
lastModified: Date.now()
|
|
148
|
+
};
|
|
149
|
+
this.entry.children.set(name, child);
|
|
150
|
+
}
|
|
151
|
+
if (child.kind !== "file") {
|
|
152
|
+
throw new DOMException(`${name} is not a file`, "TypeMismatchError");
|
|
153
|
+
}
|
|
154
|
+
return new MemoryFileHandle(name, child);
|
|
155
|
+
}
|
|
156
|
+
async getDirectoryHandle(name, options) {
|
|
157
|
+
if (!this.entry.children) {
|
|
158
|
+
this.entry.children = new Map;
|
|
159
|
+
}
|
|
160
|
+
let child = this.entry.children.get(name);
|
|
161
|
+
if (!child) {
|
|
162
|
+
if (!options?.create) {
|
|
163
|
+
throw new DOMException(`Directory not found: ${name}`, "NotFoundError");
|
|
164
|
+
}
|
|
165
|
+
child = {
|
|
166
|
+
kind: "directory",
|
|
167
|
+
name,
|
|
168
|
+
children: new Map,
|
|
169
|
+
lastModified: Date.now()
|
|
170
|
+
};
|
|
171
|
+
this.entry.children.set(name, child);
|
|
172
|
+
}
|
|
173
|
+
if (child.kind !== "directory") {
|
|
174
|
+
throw new DOMException(`${name} is not a directory`, "TypeMismatchError");
|
|
175
|
+
}
|
|
176
|
+
return new MemoryDirectoryHandle(name, child);
|
|
177
|
+
}
|
|
178
|
+
async removeEntry(name, options) {
|
|
179
|
+
if (!this.entry.children) {
|
|
180
|
+
throw new DOMException(`Entry not found: ${name}`, "NotFoundError");
|
|
181
|
+
}
|
|
182
|
+
const child = this.entry.children.get(name);
|
|
183
|
+
if (!child) {
|
|
184
|
+
throw new DOMException(`Entry not found: ${name}`, "NotFoundError");
|
|
185
|
+
}
|
|
186
|
+
if (child.kind === "directory" && child.children?.size && !options?.recursive) {
|
|
187
|
+
throw new DOMException(`Directory is not empty: ${name}`, "InvalidModificationError");
|
|
188
|
+
}
|
|
189
|
+
this.entry.children.delete(name);
|
|
190
|
+
}
|
|
191
|
+
async resolve(possibleDescendant) {
|
|
192
|
+
const descendantName = possibleDescendant.name;
|
|
193
|
+
if (this.entry.children?.has(descendantName)) {
|
|
194
|
+
return [descendantName];
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
async* entries() {
|
|
199
|
+
if (!this.entry.children) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
for (const [name, child] of this.entry.children) {
|
|
203
|
+
if (child.kind === "file") {
|
|
204
|
+
yield [name, new MemoryFileHandle(name, child)];
|
|
205
|
+
} else {
|
|
206
|
+
yield [name, new MemoryDirectoryHandle(name, child)];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async* keys() {
|
|
211
|
+
if (!this.entry.children) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
for (const name of this.entry.children.keys()) {
|
|
215
|
+
yield name;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async* values() {
|
|
219
|
+
for await (const [, handle] of this.entries()) {
|
|
220
|
+
yield handle;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function createMemoryDirectoryHandle(initialFiles) {
|
|
225
|
+
const root = {
|
|
226
|
+
kind: "directory",
|
|
227
|
+
name: "",
|
|
228
|
+
children: new Map,
|
|
229
|
+
lastModified: Date.now()
|
|
230
|
+
};
|
|
231
|
+
if (initialFiles) {
|
|
232
|
+
for (const [path, content] of Object.entries(initialFiles)) {
|
|
233
|
+
const segments = path.split("/").filter(Boolean);
|
|
234
|
+
let current = root;
|
|
235
|
+
for (let i = 0;i < segments.length - 1; i++) {
|
|
236
|
+
const segment = segments[i];
|
|
237
|
+
if (!current.children) {
|
|
238
|
+
current.children = new Map;
|
|
239
|
+
}
|
|
240
|
+
let child = current.children.get(segment);
|
|
241
|
+
if (!child) {
|
|
242
|
+
child = {
|
|
243
|
+
kind: "directory",
|
|
244
|
+
name: segment,
|
|
245
|
+
children: new Map,
|
|
246
|
+
lastModified: Date.now()
|
|
247
|
+
};
|
|
248
|
+
current.children.set(segment, child);
|
|
249
|
+
}
|
|
250
|
+
current = child;
|
|
251
|
+
}
|
|
252
|
+
const fileName = segments[segments.length - 1];
|
|
253
|
+
if (!current.children) {
|
|
254
|
+
current.children = new Map;
|
|
255
|
+
}
|
|
256
|
+
const fileContent = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
257
|
+
current.children.set(fileName, {
|
|
258
|
+
kind: "file",
|
|
259
|
+
name: fileName,
|
|
260
|
+
content: fileContent,
|
|
261
|
+
lastModified: Date.now()
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return new MemoryDirectoryHandle("", root);
|
|
266
|
+
}
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
//# debugId=EE454A6223A555BF64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/memory-adapter.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {\n HostDirectoryHandle,\n HostFileHandle,\n HostWritableFileStream,\n WriteParams,\n} from \"./types.ts\";\n\ninterface MemoryEntry {\n kind: \"file\" | \"directory\";\n name: string;\n content?: Uint8Array;\n children?: Map<string, MemoryEntry>;\n lastModified: number;\n type?: string;\n}\n\n/**\n * In-memory file handle implementation\n */\nclass MemoryFileHandle implements HostFileHandle {\n readonly kind = \"file\" as const;\n\n constructor(\n public readonly name: string,\n private entry: MemoryEntry\n ) {}\n\n async getFile(): Promise<File> {\n const content = this.entry.content ?? new Uint8Array();\n return new File([content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength) as ArrayBuffer], this.name, {\n type: this.entry.type || \"application/octet-stream\",\n lastModified: this.entry.lastModified,\n });\n }\n\n async createWritable(\n options?: { keepExistingData?: boolean }\n ): Promise<HostWritableFileStream> {\n return new MemoryWritableFileStream(\n this.entry,\n options?.keepExistingData ?? false\n );\n }\n}\n\n/**\n * In-memory writable file stream implementation\n */\nclass MemoryWritableFileStream implements HostWritableFileStream {\n private buffer: Uint8Array;\n private position = 0;\n\n constructor(\n private entry: MemoryEntry,\n keepExistingData: boolean\n ) {\n if (keepExistingData && this.entry.content) {\n this.buffer = new Uint8Array(this.entry.content);\n } else {\n this.buffer = new Uint8Array(0);\n }\n }\n\n async write(\n data: string | ArrayBuffer | Uint8Array | Blob | WriteParams\n ): Promise<void> {\n let bytes: Uint8Array;\n\n if (typeof data === \"string\") {\n bytes = new TextEncoder().encode(data);\n } else if (data instanceof ArrayBuffer) {\n bytes = new Uint8Array(data);\n } else if (data instanceof Uint8Array) {\n bytes = data;\n } else if (data instanceof Blob) {\n bytes = new Uint8Array(await data.arrayBuffer());\n } else {\n // WriteParams\n if (data.type === \"seek\") {\n this.position = data.position ?? 0;\n return;\n }\n if (data.type === \"truncate\") {\n this.buffer = this.buffer.slice(0, data.size ?? 0);\n if (this.position > this.buffer.length) {\n this.position = this.buffer.length;\n }\n return;\n }\n // type === \"write\"\n if (data.data === undefined) return;\n return this.write(data.data);\n }\n\n // Expand buffer if needed\n const neededSize = this.position + bytes.length;\n if (neededSize > this.buffer.length) {\n const newBuffer = new Uint8Array(neededSize);\n newBuffer.set(this.buffer);\n this.buffer = newBuffer;\n }\n\n this.buffer.set(bytes, this.position);\n this.position += bytes.length;\n }\n\n async seek(position: number): Promise<void> {\n this.position = position;\n }\n\n async truncate(size: number): Promise<void> {\n if (size < this.buffer.length) {\n this.buffer = this.buffer.slice(0, size);\n } else if (size > this.buffer.length) {\n const newBuffer = new Uint8Array(size);\n newBuffer.set(this.buffer);\n this.buffer = newBuffer;\n }\n if (this.position > size) {\n this.position = size;\n }\n }\n\n async close(): Promise<void> {\n this.entry.content = this.buffer;\n this.entry.lastModified = Date.now();\n }\n\n async abort(_reason?: unknown): Promise<void> {\n // Don't save, just discard\n }\n}\n\n/**\n * In-memory directory handle implementation\n */\nclass MemoryDirectoryHandle implements HostDirectoryHandle {\n readonly kind = \"directory\" as const;\n\n constructor(\n public readonly name: string,\n private entry: MemoryEntry\n ) {}\n\n async getFileHandle(\n name: string,\n options?: { create?: boolean }\n ): Promise<HostFileHandle> {\n if (!this.entry.children) {\n this.entry.children = new Map();\n }\n\n let child = this.entry.children.get(name);\n\n if (!child) {\n if (!options?.create) {\n throw new DOMException(`File not found: ${name}`, \"NotFoundError\");\n }\n child = {\n kind: \"file\",\n name,\n content: new Uint8Array(0),\n lastModified: Date.now(),\n };\n this.entry.children.set(name, child);\n }\n\n if (child.kind !== \"file\") {\n throw new DOMException(\n `${name} is not a file`,\n \"TypeMismatchError\"\n );\n }\n\n return new MemoryFileHandle(name, child);\n }\n\n async getDirectoryHandle(\n name: string,\n options?: { create?: boolean }\n ): Promise<HostDirectoryHandle> {\n if (!this.entry.children) {\n this.entry.children = new Map();\n }\n\n let child = this.entry.children.get(name);\n\n if (!child) {\n if (!options?.create) {\n throw new DOMException(\n `Directory not found: ${name}`,\n \"NotFoundError\"\n );\n }\n child = {\n kind: \"directory\",\n name,\n children: new Map(),\n lastModified: Date.now(),\n };\n this.entry.children.set(name, child);\n }\n\n if (child.kind !== \"directory\") {\n throw new DOMException(\n `${name} is not a directory`,\n \"TypeMismatchError\"\n );\n }\n\n return new MemoryDirectoryHandle(name, child);\n }\n\n async removeEntry(\n name: string,\n options?: { recursive?: boolean }\n ): Promise<void> {\n if (!this.entry.children) {\n throw new DOMException(`Entry not found: ${name}`, \"NotFoundError\");\n }\n\n const child = this.entry.children.get(name);\n if (!child) {\n throw new DOMException(`Entry not found: ${name}`, \"NotFoundError\");\n }\n\n if (child.kind === \"directory\" && child.children?.size && !options?.recursive) {\n throw new DOMException(\n `Directory is not empty: ${name}`,\n \"InvalidModificationError\"\n );\n }\n\n this.entry.children.delete(name);\n }\n\n async resolve(\n possibleDescendant: HostFileHandle | HostDirectoryHandle\n ): Promise<string[] | null> {\n // Simplified implementation - just check if it's a direct child\n const descendantName = possibleDescendant.name;\n if (this.entry.children?.has(descendantName)) {\n return [descendantName];\n }\n return null;\n }\n\n async *entries(): AsyncIterable<\n [string, HostFileHandle | HostDirectoryHandle]\n > {\n if (!this.entry.children) {\n return;\n }\n\n for (const [name, child] of this.entry.children) {\n if (child.kind === \"file\") {\n yield [name, new MemoryFileHandle(name, child)];\n } else {\n yield [name, new MemoryDirectoryHandle(name, child)];\n }\n }\n }\n\n async *keys(): AsyncIterable<string> {\n if (!this.entry.children) {\n return;\n }\n for (const name of this.entry.children.keys()) {\n yield name;\n }\n }\n\n async *values(): AsyncIterable<HostFileHandle | HostDirectoryHandle> {\n for await (const [, handle] of this.entries()) {\n yield handle;\n }\n }\n}\n\n/**\n * Create an in-memory directory handle\n * Useful for testing or fully sandboxed environments\n *\n * @example\n * import { createMemoryDirectoryHandle } from \"@ricsam/quickjs-fs\";\n *\n * const memFs = createMemoryDirectoryHandle({\n * \"config.json\": JSON.stringify({ debug: true }),\n * \"data/users.json\": JSON.stringify([]),\n * });\n *\n * const handle = setupFs(context, {\n * getDirectory: async (path) => memFs\n * });\n */\nexport function createMemoryDirectoryHandle(\n initialFiles?: Record<string, string | Uint8Array>\n): HostDirectoryHandle {\n const root: MemoryEntry = {\n kind: \"directory\",\n name: \"\",\n children: new Map(),\n lastModified: Date.now(),\n };\n\n if (initialFiles) {\n for (const [path, content] of Object.entries(initialFiles)) {\n const segments = path.split(\"/\").filter(Boolean);\n let current = root;\n\n // Create directories as needed\n for (let i = 0; i < segments.length - 1; i++) {\n const segment = segments[i]!;\n if (!current.children) {\n current.children = new Map();\n }\n\n let child = current.children.get(segment);\n if (!child) {\n child = {\n kind: \"directory\",\n name: segment,\n children: new Map(),\n lastModified: Date.now(),\n };\n current.children.set(segment, child);\n }\n current = child;\n }\n\n // Create the file\n const fileName = segments[segments.length - 1]!;\n if (!current.children) {\n current.children = new Map();\n }\n\n const fileContent =\n typeof content === \"string\"\n ? new TextEncoder().encode(content)\n : content;\n\n current.children.set(fileName, {\n kind: \"file\",\n name: fileName,\n content: fileContent,\n lastModified: Date.now(),\n });\n }\n }\n\n return new MemoryDirectoryHandle(\"\", root);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAM,iBAA2C;AAAA,EAI7B;AAAA,EACR;AAAA,EAJD,OAAO;AAAA,EAEhB,WAAW,CACO,MACR,OACR;AAAA,IAFgB;AAAA,IACR;AAAA;AAAA,OAGJ,QAAO,GAAkB;AAAA,IAC7B,MAAM,UAAU,KAAK,MAAM,WAAW,IAAI;AAAA,IAC1C,OAAO,IAAI,KAAK,CAAC,QAAQ,OAAO,MAAM,QAAQ,YAAY,QAAQ,aAAa,QAAQ,UAAU,CAAgB,GAAG,KAAK,MAAM;AAAA,MAC7H,MAAM,KAAK,MAAM,QAAQ;AAAA,MACzB,cAAc,KAAK,MAAM;AAAA,IAC3B,CAAC;AAAA;AAAA,OAGG,eAAc,CAClB,SACiC;AAAA,IACjC,OAAO,IAAI,yBACT,KAAK,OACL,SAAS,oBAAoB,KAC/B;AAAA;AAEJ;AAAA;AAKA,MAAM,yBAA2D;AAAA,EAKrD;AAAA,EAJF;AAAA,EACA,WAAW;AAAA,EAEnB,WAAW,CACD,OACR,kBACA;AAAA,IAFQ;AAAA,IAGR,IAAI,oBAAoB,KAAK,MAAM,SAAS;AAAA,MAC1C,KAAK,SAAS,IAAI,WAAW,KAAK,MAAM,OAAO;AAAA,IACjD,EAAO;AAAA,MACL,KAAK,SAAS,IAAI,WAAW,CAAC;AAAA;AAAA;AAAA,OAI5B,MAAK,CACT,MACe;AAAA,IACf,IAAI;AAAA,IAEJ,IAAI,OAAO,SAAS,UAAU;AAAA,MAC5B,QAAQ,IAAI,YAAY,EAAE,OAAO,IAA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AAAA,UACA,QAAQ,SAAS,IAAI,SAAS,KAAK;AAAA,QACrC;AAAA,QACA,UAAU;AAAA,MACZ;AAAA,MAGA,MAAM,WAAW,SAAS,SAAS,SAAS;AAAA,MAC5C,IAAI,CAAC,QAAQ,UAAU;AAAA,QACrB,QAAQ,WAAW,IAAI;AAAA,MACzB;AAAA,MAEA,MAAM,cACJ,OAAO,YAAY,WACf,IAAI,YAAY,EAAE,OAAO,OAAO,IAChC;AAAA,MAEN,QAAQ,SAAS,IAAI,UAAU;AAAA,QAC7B,MAAM;AAAA,QACN,MAAM;AAAA,QACN,SAAS;AAAA,QACT,cAAc,KAAK,IAAI;AAAA,MACzB,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,OAAO,IAAI,sBAAsB,IAAI,IAAI;AAAA;",
|
|
8
|
+
"debugId": "EE454A6223A555BF64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// packages/fs/src/node-adapter.ts
|
|
31
|
+
var exports_node_adapter = {};
|
|
32
|
+
__export(exports_node_adapter, {
|
|
33
|
+
createNodeDirectoryHandle: () => createNodeDirectoryHandle
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(exports_node_adapter);
|
|
36
|
+
|
|
37
|
+
class NodeFileHandle {
|
|
38
|
+
name;
|
|
39
|
+
fullPath;
|
|
40
|
+
kind = "file";
|
|
41
|
+
constructor(name, fullPath) {
|
|
42
|
+
this.name = name;
|
|
43
|
+
this.fullPath = fullPath;
|
|
44
|
+
}
|
|
45
|
+
async getFile() {
|
|
46
|
+
const file = Bun.file(this.fullPath);
|
|
47
|
+
const buffer = await file.arrayBuffer();
|
|
48
|
+
const stat = await Bun.file(this.fullPath).stat();
|
|
49
|
+
return new File([buffer], this.name, {
|
|
50
|
+
type: file.type,
|
|
51
|
+
lastModified: stat?.mtime?.getTime() ?? Date.now()
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async createWritable(options) {
|
|
55
|
+
return new NodeWritableFileStream(this.fullPath, options?.keepExistingData ?? false);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class NodeWritableFileStream {
|
|
60
|
+
path;
|
|
61
|
+
buffer;
|
|
62
|
+
position = 0;
|
|
63
|
+
constructor(path, keepExistingData) {
|
|
64
|
+
this.path = path;
|
|
65
|
+
if (keepExistingData) {
|
|
66
|
+
try {
|
|
67
|
+
const file = Bun.file(this.path);
|
|
68
|
+
this.buffer = new Uint8Array(0);
|
|
69
|
+
file.arrayBuffer().then((ab) => {
|
|
70
|
+
this.buffer = new Uint8Array(ab);
|
|
71
|
+
});
|
|
72
|
+
} catch {
|
|
73
|
+
this.buffer = new Uint8Array(0);
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
this.buffer = new Uint8Array(0);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async write(data) {
|
|
80
|
+
let bytes;
|
|
81
|
+
if (typeof data === "string") {
|
|
82
|
+
bytes = new TextEncoder().encode(data);
|
|
83
|
+
} else if (data instanceof ArrayBuffer) {
|
|
84
|
+
bytes = new Uint8Array(data);
|
|
85
|
+
} else if (data instanceof Uint8Array) {
|
|
86
|
+
bytes = data;
|
|
87
|
+
} else if (data instanceof Blob) {
|
|
88
|
+
bytes = new Uint8Array(await data.arrayBuffer());
|
|
89
|
+
} else {
|
|
90
|
+
if (data.type === "seek") {
|
|
91
|
+
this.position = data.position ?? 0;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (data.type === "truncate") {
|
|
95
|
+
this.buffer = this.buffer.slice(0, data.size ?? 0);
|
|
96
|
+
if (this.position > this.buffer.length) {
|
|
97
|
+
this.position = this.buffer.length;
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (data.data === undefined)
|
|
102
|
+
return;
|
|
103
|
+
return this.write(data.data);
|
|
104
|
+
}
|
|
105
|
+
const neededSize = this.position + bytes.length;
|
|
106
|
+
if (neededSize > this.buffer.length) {
|
|
107
|
+
const newBuffer = new Uint8Array(neededSize);
|
|
108
|
+
newBuffer.set(this.buffer);
|
|
109
|
+
this.buffer = newBuffer;
|
|
110
|
+
}
|
|
111
|
+
this.buffer.set(bytes, this.position);
|
|
112
|
+
this.position += bytes.length;
|
|
113
|
+
}
|
|
114
|
+
async seek(position) {
|
|
115
|
+
this.position = position;
|
|
116
|
+
}
|
|
117
|
+
async truncate(size) {
|
|
118
|
+
if (size < this.buffer.length) {
|
|
119
|
+
this.buffer = this.buffer.slice(0, size);
|
|
120
|
+
} else if (size > this.buffer.length) {
|
|
121
|
+
const newBuffer = new Uint8Array(size);
|
|
122
|
+
newBuffer.set(this.buffer);
|
|
123
|
+
this.buffer = newBuffer;
|
|
124
|
+
}
|
|
125
|
+
if (this.position > size) {
|
|
126
|
+
this.position = size;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async close() {
|
|
130
|
+
await Bun.write(this.path, this.buffer);
|
|
131
|
+
}
|
|
132
|
+
async abort(_reason) {
|
|
133
|
+
this.buffer = new Uint8Array(0);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class NodeDirectoryHandle {
|
|
138
|
+
name;
|
|
139
|
+
fullPath;
|
|
140
|
+
kind = "directory";
|
|
141
|
+
constructor(name, fullPath) {
|
|
142
|
+
this.name = name;
|
|
143
|
+
this.fullPath = fullPath;
|
|
144
|
+
}
|
|
145
|
+
async getFileHandle(name, options) {
|
|
146
|
+
const path = `${this.fullPath}/${name}`;
|
|
147
|
+
const file = Bun.file(path);
|
|
148
|
+
const exists = await file.exists();
|
|
149
|
+
if (!exists && !options?.create) {
|
|
150
|
+
throw new DOMException(`File not found: ${name}`, "NotFoundError");
|
|
151
|
+
}
|
|
152
|
+
if (!exists && options?.create) {
|
|
153
|
+
await Bun.write(path, "");
|
|
154
|
+
}
|
|
155
|
+
return new NodeFileHandle(name, path);
|
|
156
|
+
}
|
|
157
|
+
async getDirectoryHandle(name, options) {
|
|
158
|
+
const path = `${this.fullPath}/${name}`;
|
|
159
|
+
try {
|
|
160
|
+
const stat = await Bun.file(path).stat();
|
|
161
|
+
if (!stat) {
|
|
162
|
+
throw new Error("Not found");
|
|
163
|
+
}
|
|
164
|
+
} catch {
|
|
165
|
+
if (!options?.create) {
|
|
166
|
+
throw new DOMException(`Directory not found: ${name}`, "NotFoundError");
|
|
167
|
+
}
|
|
168
|
+
await Bun.$`mkdir -p ${path}`;
|
|
169
|
+
}
|
|
170
|
+
return new NodeDirectoryHandle(name, path);
|
|
171
|
+
}
|
|
172
|
+
async removeEntry(name, options) {
|
|
173
|
+
const path = `${this.fullPath}/${name}`;
|
|
174
|
+
if (options?.recursive) {
|
|
175
|
+
await Bun.$`rm -rf ${path}`;
|
|
176
|
+
} else {
|
|
177
|
+
await Bun.$`rm ${path}`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
async resolve(possibleDescendant) {
|
|
181
|
+
const descendantPath = possibleDescendant instanceof NodeFileHandle || possibleDescendant instanceof NodeDirectoryHandle ? possibleDescendant.fullPath : null;
|
|
182
|
+
if (!descendantPath || !descendantPath.startsWith(this.fullPath)) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const relativePath = descendantPath.slice(this.fullPath.length);
|
|
186
|
+
return relativePath.split("/").filter(Boolean);
|
|
187
|
+
}
|
|
188
|
+
async* entries() {
|
|
189
|
+
const result = await Bun.$`ls -1 ${this.fullPath}`.text();
|
|
190
|
+
const names = result.trim().split(`
|
|
191
|
+
`).filter(Boolean);
|
|
192
|
+
for (const name of names) {
|
|
193
|
+
const path = `${this.fullPath}/${name}`;
|
|
194
|
+
try {
|
|
195
|
+
const stat = await Bun.file(path).stat();
|
|
196
|
+
if (stat && typeof stat.isDirectory === "function" && stat.isDirectory()) {
|
|
197
|
+
yield [name, new NodeDirectoryHandle(name, path)];
|
|
198
|
+
} else {
|
|
199
|
+
yield [name, new NodeFileHandle(name, path)];
|
|
200
|
+
}
|
|
201
|
+
} catch {}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async* keys() {
|
|
205
|
+
for await (const [name] of this.entries()) {
|
|
206
|
+
yield name;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async* values() {
|
|
210
|
+
for await (const [, handle] of this.entries()) {
|
|
211
|
+
yield handle;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function createNodeDirectoryHandle(rootPath) {
|
|
216
|
+
const name = rootPath.split("/").pop() || "";
|
|
217
|
+
return new NodeDirectoryHandle(name, rootPath);
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
//# debugId=30BC4BE3F0DE509364756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/node-adapter.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {\n HostDirectoryHandle,\n HostFileHandle,\n HostWritableFileStream,\n WriteParams,\n} from \"./types.ts\";\n\n/**\n * Node/Bun file handle implementation\n */\nclass NodeFileHandle implements HostFileHandle {\n readonly kind = \"file\" as const;\n\n constructor(\n public readonly name: string,\n private readonly fullPath: string\n ) {}\n\n async getFile(): Promise<File> {\n const file = Bun.file(this.fullPath);\n const buffer = await file.arrayBuffer();\n const stat = await Bun.file(this.fullPath).stat();\n\n return new File([buffer], this.name, {\n type: file.type,\n lastModified: stat?.mtime?.getTime() ?? Date.now(),\n });\n }\n\n async createWritable(\n options?: { keepExistingData?: boolean }\n ): Promise<HostWritableFileStream> {\n return new NodeWritableFileStream(\n this.fullPath,\n options?.keepExistingData ?? false\n );\n }\n}\n\n/**\n * Node/Bun writable file stream implementation\n */\nclass NodeWritableFileStream implements HostWritableFileStream {\n private buffer: Uint8Array;\n private position = 0;\n\n constructor(\n private readonly path: string,\n keepExistingData: boolean\n ) {\n if (keepExistingData) {\n // Read existing file synchronously for simplicity\n try {\n const file = Bun.file(this.path);\n // Note: This is simplified; in production you'd want async\n this.buffer = new Uint8Array(0);\n file.arrayBuffer().then((ab) => {\n this.buffer = new Uint8Array(ab);\n });\n } catch {\n this.buffer = new Uint8Array(0);\n }\n } else {\n this.buffer = new Uint8Array(0);\n }\n }\n\n async write(\n data: string | ArrayBuffer | Uint8Array | Blob | WriteParams\n ): Promise<void> {\n let bytes: Uint8Array;\n\n if (typeof data === \"string\") {\n bytes = new TextEncoder().encode(data);\n } else if (data instanceof ArrayBuffer) {\n bytes = new Uint8Array(data);\n } else if (data instanceof Uint8Array) {\n bytes = data;\n } else if (data instanceof Blob) {\n bytes = new Uint8Array(await data.arrayBuffer());\n } else {\n // WriteParams\n if (data.type === \"seek\") {\n this.position = data.position ?? 0;\n return;\n }\n if (data.type === \"truncate\") {\n this.buffer = this.buffer.slice(0, data.size ?? 0);\n if (this.position > this.buffer.length) {\n this.position = this.buffer.length;\n }\n return;\n }\n // type === \"write\"\n if (data.data === undefined) return;\n return this.write(data.data);\n }\n\n // Expand buffer if needed\n const neededSize = this.position + bytes.length;\n if (neededSize > this.buffer.length) {\n const newBuffer = new Uint8Array(neededSize);\n newBuffer.set(this.buffer);\n this.buffer = newBuffer;\n }\n\n this.buffer.set(bytes, this.position);\n this.position += bytes.length;\n }\n\n async seek(position: number): Promise<void> {\n this.position = position;\n }\n\n async truncate(size: number): Promise<void> {\n if (size < this.buffer.length) {\n this.buffer = this.buffer.slice(0, size);\n } else if (size > this.buffer.length) {\n const newBuffer = new Uint8Array(size);\n newBuffer.set(this.buffer);\n this.buffer = newBuffer;\n }\n if (this.position > size) {\n this.position = size;\n }\n }\n\n async close(): Promise<void> {\n await Bun.write(this.path, this.buffer);\n }\n\n async abort(_reason?: unknown): Promise<void> {\n // Don't write, just discard\n this.buffer = new Uint8Array(0);\n }\n}\n\n/**\n * Node/Bun directory handle implementation\n */\nclass NodeDirectoryHandle implements HostDirectoryHandle {\n readonly kind = \"directory\" as const;\n\n constructor(\n public readonly name: string,\n private readonly fullPath: string\n ) {}\n\n async getFileHandle(\n name: string,\n options?: { create?: boolean }\n ): Promise<HostFileHandle> {\n const path = `${this.fullPath}/${name}`;\n const file = Bun.file(path);\n const exists = await file.exists();\n\n if (!exists && !options?.create) {\n throw new DOMException(`File not found: ${name}`, \"NotFoundError\");\n }\n\n if (!exists && options?.create) {\n await Bun.write(path, \"\");\n }\n\n return new NodeFileHandle(name, path);\n }\n\n async getDirectoryHandle(\n name: string,\n options?: { create?: boolean }\n ): Promise<HostDirectoryHandle> {\n const path = `${this.fullPath}/${name}`;\n\n try {\n const stat = await Bun.file(path).stat();\n if (!stat) {\n throw new Error(\"Not found\");\n }\n } catch {\n if (!options?.create) {\n throw new DOMException(`Directory not found: ${name}`, \"NotFoundError\");\n }\n // Create directory\n await Bun.$`mkdir -p ${path}`;\n }\n\n return new NodeDirectoryHandle(name, path);\n }\n\n async removeEntry(\n name: string,\n options?: { recursive?: boolean }\n ): Promise<void> {\n const path = `${this.fullPath}/${name}`;\n\n if (options?.recursive) {\n await Bun.$`rm -rf ${path}`;\n } else {\n await Bun.$`rm ${path}`;\n }\n }\n\n async resolve(\n possibleDescendant: HostFileHandle | HostDirectoryHandle\n ): Promise<string[] | null> {\n // Get the paths and compare\n const descendantPath =\n possibleDescendant instanceof NodeFileHandle ||\n possibleDescendant instanceof NodeDirectoryHandle\n ? (possibleDescendant as unknown as { fullPath: string }).fullPath\n : null;\n\n if (!descendantPath || !descendantPath.startsWith(this.fullPath)) {\n return null;\n }\n\n const relativePath = descendantPath.slice(this.fullPath.length);\n return relativePath.split(\"/\").filter(Boolean);\n }\n\n async *entries(): AsyncIterable<\n [string, HostFileHandle | HostDirectoryHandle]\n > {\n const result = await Bun.$`ls -1 ${this.fullPath}`.text();\n const names = result.trim().split(\"\\n\").filter(Boolean);\n\n for (const name of names) {\n const path = `${this.fullPath}/${name}`;\n try {\n const stat = await Bun.file(path).stat();\n if (stat && typeof stat.isDirectory === 'function' && stat.isDirectory()) {\n yield [name, new NodeDirectoryHandle(name, path)];\n } else {\n yield [name, new NodeFileHandle(name, path)];\n }\n } catch {\n // Skip entries we can't stat\n }\n }\n }\n\n async *keys(): AsyncIterable<string> {\n for await (const [name] of this.entries()) {\n yield name;\n }\n }\n\n async *values(): AsyncIterable<HostFileHandle | HostDirectoryHandle> {\n for await (const [, handle] of this.entries()) {\n yield handle;\n }\n }\n}\n\n/**\n * Create a directory handle backed by Node.js/Bun fs\n * Useful for server-side implementations\n *\n * @param rootPath - Absolute path to the directory on disk\n * @returns A HostDirectoryHandle implementation\n *\n * @example\n * import { createNodeDirectoryHandle } from \"@ricsam/quickjs-fs\";\n *\n * const handle = setupFs(context, {\n * getDirectory: async (path) => {\n * // Only allow access to /sandbox\n * if (!path.startsWith(\"/sandbox\")) {\n * throw new Error(\"Access denied\");\n * }\n * const realPath = path.replace(\"/sandbox\", \"/var/app/sandbox\");\n * return createNodeDirectoryHandle(realPath);\n * }\n * });\n */\nexport function createNodeDirectoryHandle(\n rootPath: string\n): HostDirectoryHandle {\n const name = rootPath.split(\"/\").pop() || \"\";\n return new NodeDirectoryHandle(name, rootPath);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,MAAM,eAAyC;AAAA,EAI3B;AAAA,EACC;AAAA,EAJV,OAAO;AAAA,EAEhB,WAAW,CACO,MACC,UACjB;AAAA,IAFgB;AAAA,IACC;AAAA;AAAA,OAGb,QAAO,GAAkB;AAAA,IAC7B,MAAM,OAAO,IAAI,KAAK,KAAK,QAAQ;AAAA,IACnC,MAAM,SAAS,MAAM,KAAK,YAAY;AAAA,IACtC,MAAM,OAAO,MAAM,IAAI,KAAK,KAAK,QAAQ,EAAE,KAAK;AAAA,IAEhD,OAAO,IAAI,KAAK,CAAC,MAAM,GAAG,KAAK,MAAM;AAAA,MACnC,MAAM,KAAK;AAAA,MACX,cAAc,MAAM,OAAO,QAAQ,KAAK,KAAK,IAAI;AAAA,IACnD,CAAC;AAAA;AAAA,OAGG,eAAc,CAClB,SACiC;AAAA,IACjC,OAAO,IAAI,uBACT,KAAK,UACL,SAAS,oBAAoB,KAC/B;AAAA;AAEJ;AAAA;AAKA,MAAM,uBAAyD;AAAA,EAK1C;AAAA,EAJX;AAAA,EACA,WAAW;AAAA,EAEnB,WAAW,CACQ,MACjB,kBACA;AAAA,IAFiB;AAAA,IAGjB,IAAI,kBAAkB;AAAA,MAEpB,IAAI;AAAA,QACF,MA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