opfs-mock 1.0.1 → 1.0.2
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/dist/index.d.ts +5 -3
- package/dist/index.js +157 -99
- package/package.json +6 -6
- package/dist/opfs.d.ts +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare const storageFactory: ({ usage, quota }?: StorageEstimate) => StorageManager;
|
|
2
|
+
declare const mockOPFS: () => void;
|
|
3
|
+
declare const resetMockOPFS: () => void;
|
|
4
|
+
|
|
5
|
+
export { mockOPFS, resetMockOPFS, storageFactory };
|
package/dist/index.js
CHANGED
|
@@ -1,128 +1,186 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// @ts-ignore TODO: Implement options, add missing properties
|
|
9
|
-
createWritable: async (t) => new WritableStream({
|
|
10
|
-
write: (e) => {
|
|
11
|
-
const r = new TextDecoder().decode(e);
|
|
12
|
-
n.content += r;
|
|
13
|
-
},
|
|
14
|
-
close: () => {
|
|
15
|
-
},
|
|
16
|
-
abort: (e) => {
|
|
17
|
-
}
|
|
18
|
-
}).getWriter(),
|
|
19
|
-
createSyncAccessHandle: async () => ({
|
|
20
|
-
getSize: () => n.content.length,
|
|
21
|
-
read: (t, { at: s = 0 } = {}) => {
|
|
22
|
-
const e = new TextEncoder().encode(n.content), r = Math.min(t.length, e.length - s);
|
|
23
|
-
return t.set(e.subarray(s, s + r)), r;
|
|
24
|
-
},
|
|
25
|
-
write: (t, { at: s = 0 } = {}) => {
|
|
26
|
-
const e = new TextDecoder().decode(t), r = n.content.length;
|
|
27
|
-
return s < r ? n.content = n.content.slice(0, s) + e + n.content.slice(s + e.length) : n.content += e, t.byteLength;
|
|
28
|
-
},
|
|
29
|
-
// Flush is a no-op in memory
|
|
30
|
-
flush: async () => {
|
|
1
|
+
// src/opfs.ts
|
|
2
|
+
var fileSystemFileHandleFactory = (name, fileData) => {
|
|
3
|
+
return {
|
|
4
|
+
kind: "file",
|
|
5
|
+
name,
|
|
6
|
+
isSameEntry: async (other) => {
|
|
7
|
+
return other.name === name && other.kind === "file";
|
|
31
8
|
},
|
|
32
|
-
|
|
33
|
-
|
|
9
|
+
getFile: async () => new File([fileData.content], name, {
|
|
10
|
+
type: "application/json"
|
|
11
|
+
}),
|
|
12
|
+
// @ts-ignore TODO: Implement options, add missing properties
|
|
13
|
+
createWritable: async (_options) => {
|
|
14
|
+
const writableStream = new WritableStream({
|
|
15
|
+
write: (chunk) => {
|
|
16
|
+
const newContent = new TextDecoder().decode(chunk);
|
|
17
|
+
fileData.content += newContent;
|
|
18
|
+
},
|
|
19
|
+
close: () => {
|
|
20
|
+
},
|
|
21
|
+
abort: (_reason) => {
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return writableStream.getWriter();
|
|
34
25
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
createSyncAccessHandle: async () => ({
|
|
27
|
+
getSize: () => fileData.content.length,
|
|
28
|
+
read: (buffer, { at = 0 } = {}) => {
|
|
29
|
+
const text = new TextEncoder().encode(fileData.content);
|
|
30
|
+
const bytesRead = Math.min(buffer.length, text.length - at);
|
|
31
|
+
buffer.set(text.subarray(at, at + bytesRead));
|
|
32
|
+
return bytesRead;
|
|
33
|
+
},
|
|
34
|
+
write: (data, { at = 0 } = {}) => {
|
|
35
|
+
const newContent = new TextDecoder().decode(data);
|
|
36
|
+
const originalLength = fileData.content.length;
|
|
37
|
+
if (at < originalLength) {
|
|
38
|
+
fileData.content = fileData.content.slice(0, at) + newContent + fileData.content.slice(at + newContent.length);
|
|
39
|
+
} else {
|
|
40
|
+
fileData.content += newContent;
|
|
41
|
+
}
|
|
42
|
+
return data.byteLength;
|
|
43
|
+
},
|
|
44
|
+
// Flush is a no-op in memory
|
|
45
|
+
flush: async () => {
|
|
46
|
+
},
|
|
47
|
+
// Close is a no-op in memory
|
|
48
|
+
close: async () => {
|
|
49
|
+
},
|
|
50
|
+
truncate: async (size) => {
|
|
51
|
+
fileData.content = fileData.content.slice(0, size);
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
var fileSystemDirectoryHandleFactory = (name) => {
|
|
57
|
+
const files = /* @__PURE__ */ new Map();
|
|
58
|
+
const directories = /* @__PURE__ */ new Map();
|
|
59
|
+
const getJoinedMaps = () => {
|
|
60
|
+
return new Map([...files, ...directories]);
|
|
61
|
+
};
|
|
41
62
|
return {
|
|
42
63
|
kind: "directory",
|
|
43
|
-
name
|
|
44
|
-
isSameEntry: async (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!
|
|
49
|
-
|
|
50
|
-
|
|
64
|
+
name,
|
|
65
|
+
isSameEntry: async (other) => {
|
|
66
|
+
return other.name === name && other.kind === "directory";
|
|
67
|
+
},
|
|
68
|
+
getFileHandle: async (fileName, options) => {
|
|
69
|
+
if (!files.has(fileName) && options?.create) {
|
|
70
|
+
files.set(fileName, fileSystemFileHandleFactory(fileName, { content: "" }));
|
|
71
|
+
}
|
|
72
|
+
const fileHandle = files.get(fileName);
|
|
73
|
+
if (!fileHandle) {
|
|
74
|
+
throw new Error(`File not found: ${fileName}`);
|
|
75
|
+
}
|
|
76
|
+
return fileHandle;
|
|
51
77
|
},
|
|
52
|
-
getDirectoryHandle: async (
|
|
53
|
-
!
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
78
|
+
getDirectoryHandle: async (dirName, options) => {
|
|
79
|
+
if (!directories.has(dirName) && options?.create) {
|
|
80
|
+
directories.set(dirName, fileSystemDirectoryHandleFactory(dirName));
|
|
81
|
+
}
|
|
82
|
+
const directoryHandle = directories.get(dirName);
|
|
83
|
+
if (!directoryHandle) {
|
|
84
|
+
throw new Error(`Directory not found: ${dirName}`);
|
|
85
|
+
}
|
|
86
|
+
return directoryHandle;
|
|
58
87
|
},
|
|
59
|
-
removeEntry: async (
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
else if (
|
|
63
|
-
|
|
64
|
-
else
|
|
65
|
-
throw new Error(`Entry not found: ${
|
|
88
|
+
removeEntry: async (entryName) => {
|
|
89
|
+
if (files.has(entryName)) {
|
|
90
|
+
files.delete(entryName);
|
|
91
|
+
} else if (directories.has(entryName)) {
|
|
92
|
+
directories.delete(entryName);
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error(`Entry not found: ${entryName}`);
|
|
95
|
+
}
|
|
66
96
|
},
|
|
67
97
|
entries: async function* () {
|
|
68
|
-
|
|
98
|
+
const joinedMaps = getJoinedMaps();
|
|
99
|
+
yield* joinedMaps.entries();
|
|
69
100
|
},
|
|
70
101
|
keys: async function* () {
|
|
71
|
-
|
|
102
|
+
const joinedMaps = getJoinedMaps();
|
|
103
|
+
yield* joinedMaps.keys();
|
|
72
104
|
},
|
|
73
105
|
values: async function* () {
|
|
74
|
-
|
|
106
|
+
const joinedMaps = getJoinedMaps();
|
|
107
|
+
yield* joinedMaps.values();
|
|
75
108
|
},
|
|
76
|
-
resolve: async (
|
|
77
|
-
const
|
|
78
|
-
if (await
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
109
|
+
resolve: async (possibleDescendant) => {
|
|
110
|
+
const traverseDirectory = async (directory, target, path = []) => {
|
|
111
|
+
if (await directory.isSameEntry(target)) {
|
|
112
|
+
return path;
|
|
113
|
+
}
|
|
114
|
+
for await (const [name2, handle] of directory.entries()) {
|
|
115
|
+
if (handle.kind === "directory") {
|
|
116
|
+
const subDirectory = handle;
|
|
117
|
+
const result = await traverseDirectory(subDirectory, target, [...path, name2]);
|
|
118
|
+
if (result) {
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
} else if (handle.kind === "file") {
|
|
122
|
+
if (await handle.isSameEntry(target)) {
|
|
123
|
+
return [...path, name2];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
87
127
|
return null;
|
|
88
128
|
};
|
|
89
|
-
return
|
|
129
|
+
return traverseDirectory(void 0, possibleDescendant);
|
|
90
130
|
}
|
|
91
131
|
};
|
|
92
|
-
}
|
|
93
|
-
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// src/index.ts
|
|
135
|
+
var storageFactory = ({ usage = 0, quota = 0 } = {}) => {
|
|
136
|
+
const root = fileSystemDirectoryHandleFactory("root");
|
|
94
137
|
return {
|
|
95
|
-
estimate: async () =>
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
138
|
+
estimate: async () => {
|
|
139
|
+
return {
|
|
140
|
+
usage,
|
|
141
|
+
quota
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
getDirectory: async () => {
|
|
145
|
+
return root;
|
|
146
|
+
},
|
|
147
|
+
persist: async () => {
|
|
148
|
+
return true;
|
|
149
|
+
},
|
|
150
|
+
persisted: async () => {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
102
153
|
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
154
|
+
};
|
|
155
|
+
var mockOPFS = () => {
|
|
156
|
+
if (!("navigator" in globalThis)) {
|
|
157
|
+
Object.defineProperty(globalThis, "navigator", {
|
|
158
|
+
value: {},
|
|
159
|
+
writable: true
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (!globalThis.navigator.storage) {
|
|
163
|
+
const { getDirectory } = storageFactory();
|
|
109
164
|
Object.defineProperty(globalThis.navigator, "storage", {
|
|
110
165
|
value: {
|
|
111
|
-
getDirectory
|
|
166
|
+
getDirectory
|
|
112
167
|
},
|
|
113
|
-
writable:
|
|
168
|
+
writable: true
|
|
114
169
|
});
|
|
115
170
|
}
|
|
116
|
-
}
|
|
117
|
-
|
|
171
|
+
};
|
|
172
|
+
var resetMockOPFS = () => {
|
|
173
|
+
const root = fileSystemDirectoryHandleFactory("root");
|
|
118
174
|
Object.defineProperty(globalThis.navigator.storage, "getDirectory", {
|
|
119
|
-
value: () =>
|
|
120
|
-
writable:
|
|
175
|
+
value: () => root,
|
|
176
|
+
writable: true
|
|
121
177
|
});
|
|
122
178
|
};
|
|
123
|
-
typeof globalThis
|
|
179
|
+
if (typeof globalThis !== "undefined") {
|
|
180
|
+
mockOPFS();
|
|
181
|
+
}
|
|
124
182
|
export {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
183
|
+
mockOPFS,
|
|
184
|
+
resetMockOPFS,
|
|
185
|
+
storageFactory
|
|
128
186
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opfs-mock",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Mock all origin private file system APIs for your Jest or Vitest tests",
|
|
6
6
|
"author": "Jure Rotar <hello@jurerotar.com>",
|
|
@@ -23,25 +23,25 @@
|
|
|
23
23
|
"import": "./dist/index.js"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
|
+
"main": "dist/index.js",
|
|
26
27
|
"module": "dist/index.js",
|
|
27
28
|
"files": ["dist"],
|
|
28
29
|
"scripts": {
|
|
29
|
-
"dev": "
|
|
30
|
-
"build": "
|
|
30
|
+
"dev": "tsup --watch",
|
|
31
|
+
"build": "tsup",
|
|
31
32
|
"lint:check": "npx @biomejs/biome lint",
|
|
32
33
|
"lint": "npx @biomejs/biome lint --fix",
|
|
33
34
|
"format:check": "npx @biomejs/biome format",
|
|
34
35
|
"format": "npx @biomejs/biome format --write",
|
|
35
36
|
"type-check": "tsc --noEmit",
|
|
36
37
|
"test": "vitest",
|
|
37
|
-
"
|
|
38
|
-
"prepublishOnly": "npm run build && npm run build:types",
|
|
38
|
+
"prepublishOnly": "npm run build",
|
|
39
39
|
"release": "npm publish --access public"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@biomejs/biome": "1.9.4",
|
|
43
|
+
"tsup": "8.3.5",
|
|
43
44
|
"typescript": "5.7.3",
|
|
44
|
-
"vite": "6.0.7",
|
|
45
45
|
"vitest": "2.1.8"
|
|
46
46
|
}
|
|
47
47
|
}
|
package/dist/opfs.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const fileSystemDirectoryHandleFactory: (name: string) => FileSystemDirectoryHandle;
|