opfs-mock 2.2.0 → 2.4.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.md CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright © 2024 Jure Rotar
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024 Jure Rotar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md CHANGED
@@ -1,168 +1,168 @@
1
- # opfs-mock
2
-
3
- In-memory implementation of the [origin private file system](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system). Its main utility is for testing OPFS-dependent code in Node.js. It's tested
4
- on Node.js versions 20-24.
5
-
6
- ## Installation
7
-
8
- ```shell
9
- npm install -save-dev opfs-mock
10
- ```
11
-
12
- ## Limitations
13
-
14
- - `opfs-mock` requires **Node.js v20.0.0** or higher. It can work on Node v18.0.0 with either `--experimental-fetch` flag enabled or a global
15
- `File` polyfill.
16
-
17
- - `jsdom` testing environment is missing `File.prototype.text()` method, which is required for reading opfs files. Ensure your opfs-dependant tests are ran
18
- in `node` or `happy-dom` environment.
19
-
20
- ## Usage
21
-
22
- It replicates the behavior of origin private file system, except data is not persisted to disk.
23
-
24
- The easiest way to use it is to import `opfs-mock`, which will polyfill OPFS API to global scope.
25
-
26
- ```ts
27
- import "opfs-mock";
28
- ```
29
-
30
- Alternatively, you can explicitly import `storageFactory`:
31
-
32
- ```ts
33
- import { storageFactory } from "opfs-mock";
34
-
35
- test('Your test', async () => {
36
- const storage = await storageFactory();
37
- const root = await storage.getDirectory();
38
- const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
39
- // rest of your test
40
- });
41
- ```
42
-
43
- `storageFactory` has `quota` and `usage` values set to `1024 ** 3 (1 GB)` and `0` respectively. When calling `storage.estimate()`, `usage` is dynamically calculated by summing the predefined usage value and any additional computed storage consumption.
44
- In case you need specific values, you can pass both as arguments to `storageFactory`.
45
-
46
- ```ts
47
- import { storageFactory } from "opfs-mock";
48
-
49
- test('Your test', async () => {
50
- const storage = await storageFactory({ quota: 1_000_000, usage: 1_000 });
51
- const root = await storage.getDirectory();
52
- const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
53
- // rest of your test
54
- });
55
- ```
56
-
57
- ### Vitest
58
-
59
- To use `opfs-mock` in a single Vitest test suite, require `opfs-mock` at the beginning of the test file, as described above.
60
-
61
- To use it on all Vitest tests without having to include it in each file, add the auto setup script to the `test.setupFiles` in your Vite config:
62
-
63
- ```ts
64
- // vite.config.ts
65
-
66
- import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
67
- import { defineConfig as defineVitestConfig } from 'vitest/config';
68
-
69
- const viteConfig = defineViteConfig({
70
- ...
71
- });
72
-
73
- const vitestConfig = defineVitestConfig({
74
- test: {
75
- setupFiles: ['opfs-mock'],
76
- },
77
- });
78
-
79
- export default mergeConfig(viteConfig, vitestConfig);
80
- ```
81
-
82
- Alternatively you can create a new setup file which then imports this module.
83
-
84
- ```ts
85
- // vitest-setup.ts
86
-
87
- import "opfs-mock";
88
- ```
89
-
90
- Add that file to your `test.setupFiles` array:
91
-
92
- ```ts
93
- // vite.config.ts
94
-
95
- import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
96
- import { defineConfig as defineVitestConfig } from 'vitest/config';
97
-
98
- const viteConfig = defineViteConfig({
99
- ...
100
- });
101
-
102
- const vitestConfig = defineVitestConfig({
103
- test: {
104
- setupFiles: ['vitest-setup.ts'],
105
- },
106
- });
107
-
108
- export default mergeConfig(viteConfig, vitestConfig);
109
- ```
110
-
111
-
112
- ### Jest
113
-
114
- To use `opfs-mock` in a single Jest test suite, require `opfs-mock` at the beginning of the test file, as described above.
115
-
116
- To use it on all Jest tests without having to include it in each file, add the auto setup script to the `setupFiles` in your Jest config:
117
-
118
- ```ts
119
- // jest.config.js
120
-
121
- {
122
- ...
123
- "setupFiles": [
124
- "opfs-mock"
125
- ]
126
- }
127
- ```
128
-
129
- Alternatively you can create a new setup file which then imports this module.
130
-
131
- ```ts
132
- // jest-setup.ts
133
-
134
- import "opfs-mock";
135
- ```
136
-
137
- Add that file to your `setupFiles` array:
138
-
139
- ```ts
140
- // jest.config.js
141
-
142
- {
143
- ...
144
- "setupFiles": [
145
- "jest-setup"
146
- ]
147
- }
148
- ```
149
-
150
- ## Wiping/resetting the OPFS mock for a fresh state
151
-
152
- If you are keeping your tests completely isolated you might want to "reset" the state of the mocked OPFS. You can do this by using `resetMockOPFS` function, which creates a completely new instance of the mock.
153
-
154
- ```ts
155
- import { resetMockOPFS } from 'opfs-mock';
156
-
157
- beforeEach(() => {
158
- resetMockOPFS();
159
- });
160
-
161
- test('First isolated test', async () => {
162
- // rest of your test
163
- });
164
-
165
- test('Second isolated test', async () => {
166
- // rest of your test
167
- });
168
- ```
1
+ # opfs-mock
2
+
3
+ In-memory implementation of the [origin private file system](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system). Its main utility is for testing OPFS-dependent code in Node.js. It's tested
4
+ on Node.js versions 20-25.
5
+
6
+ ## Installation
7
+
8
+ ```shell
9
+ npm install -save-dev opfs-mock
10
+ ```
11
+
12
+ ## Limitations
13
+
14
+ - `opfs-mock` requires **Node.js v20.0.0** or higher. It can work on Node v18.0.0 with either `--experimental-fetch` flag enabled or a global
15
+ `File` polyfill.
16
+
17
+ - `jsdom` testing environment is missing `File.prototype.text()` method, which is required for reading opfs files. Ensure your opfs-dependant tests are ran
18
+ in `node` or `happy-dom` environment.
19
+
20
+ ## Usage
21
+
22
+ It replicates the behavior of origin private file system, except data is not persisted to disk.
23
+
24
+ The easiest way to use it is to import `opfs-mock`, which will polyfill OPFS API to global scope.
25
+
26
+ ```ts
27
+ import "opfs-mock";
28
+ ```
29
+
30
+ Alternatively, you can explicitly import `storageFactory`:
31
+
32
+ ```ts
33
+ import { storageFactory } from "opfs-mock";
34
+
35
+ test('Your test', async () => {
36
+ const storage = await storageFactory();
37
+ const root = await storage.getDirectory();
38
+ const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
39
+ // rest of your test
40
+ });
41
+ ```
42
+
43
+ `storageFactory` has `quota` and `usage` values set to `1024 ** 3 (1 GB)` and `0` respectively. When calling `storage.estimate()`, `usage` is dynamically calculated by summing the predefined usage value and any additional computed storage consumption.
44
+ In case you need specific values, you can pass both as arguments to `storageFactory`.
45
+
46
+ ```ts
47
+ import { storageFactory } from "opfs-mock";
48
+
49
+ test('Your test', async () => {
50
+ const storage = await storageFactory({ quota: 1_000_000, usage: 1_000 });
51
+ const root = await storage.getDirectory();
52
+ const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
53
+ // rest of your test
54
+ });
55
+ ```
56
+
57
+ ### Vitest
58
+
59
+ To use `opfs-mock` in a single Vitest test suite, require `opfs-mock` at the beginning of the test file, as described above.
60
+
61
+ To use it on all Vitest tests without having to include it in each file, add the auto setup script to the `test.setupFiles` in your Vite config:
62
+
63
+ ```ts
64
+ // vite.config.ts
65
+
66
+ import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
67
+ import { defineConfig as defineVitestConfig } from 'vitest/config';
68
+
69
+ const viteConfig = defineViteConfig({
70
+ ...
71
+ });
72
+
73
+ const vitestConfig = defineVitestConfig({
74
+ test: {
75
+ setupFiles: ['opfs-mock'],
76
+ },
77
+ });
78
+
79
+ export default mergeConfig(viteConfig, vitestConfig);
80
+ ```
81
+
82
+ Alternatively you can create a new setup file which then imports this module.
83
+
84
+ ```ts
85
+ // vitest-setup.ts
86
+
87
+ import "opfs-mock";
88
+ ```
89
+
90
+ Add that file to your `test.setupFiles` array:
91
+
92
+ ```ts
93
+ // vite.config.ts
94
+
95
+ import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
96
+ import { defineConfig as defineVitestConfig } from 'vitest/config';
97
+
98
+ const viteConfig = defineViteConfig({
99
+ ...
100
+ });
101
+
102
+ const vitestConfig = defineVitestConfig({
103
+ test: {
104
+ setupFiles: ['vitest-setup.ts'],
105
+ },
106
+ });
107
+
108
+ export default mergeConfig(viteConfig, vitestConfig);
109
+ ```
110
+
111
+
112
+ ### Jest
113
+
114
+ To use `opfs-mock` in a single Jest test suite, require `opfs-mock` at the beginning of the test file, as described above.
115
+
116
+ To use it on all Jest tests without having to include it in each file, add the auto setup script to the `setupFiles` in your Jest config:
117
+
118
+ ```ts
119
+ // jest.config.js
120
+
121
+ {
122
+ ...
123
+ "setupFiles": [
124
+ "opfs-mock"
125
+ ]
126
+ }
127
+ ```
128
+
129
+ Alternatively you can create a new setup file which then imports this module.
130
+
131
+ ```ts
132
+ // jest-setup.ts
133
+
134
+ import "opfs-mock";
135
+ ```
136
+
137
+ Add that file to your `setupFiles` array:
138
+
139
+ ```ts
140
+ // jest.config.js
141
+
142
+ {
143
+ ...
144
+ "setupFiles": [
145
+ "jest-setup"
146
+ ]
147
+ }
148
+ ```
149
+
150
+ ## Wiping/resetting the OPFS mock for a fresh state
151
+
152
+ If you are keeping your tests completely isolated you might want to "reset" the state of the mocked OPFS. You can do this by using `resetMockOPFS` function, which creates a completely new instance of the mock.
153
+
154
+ ```ts
155
+ import { resetMockOPFS } from 'opfs-mock';
156
+
157
+ beforeEach(() => {
158
+ resetMockOPFS();
159
+ });
160
+
161
+ test('First isolated test', async () => {
162
+ // rest of your test
163
+ });
164
+
165
+ test('Second isolated test', async () => {
166
+ // rest of your test
167
+ });
168
+ ```
package/dist/index.d.ts CHANGED
@@ -5,6 +5,5 @@ declare const storageFactory: ({
5
5
  }?: StorageEstimate) => StorageManager;
6
6
  declare const mockOPFS: () => void;
7
7
  declare const resetMockOPFS: () => void;
8
-
9
8
  //#endregion
10
9
  export { mockOPFS, resetMockOPFS, storageFactory };
package/dist/index.js CHANGED
@@ -16,20 +16,23 @@ const getSizeOfDirectory = async (directory) => {
16
16
 
17
17
  //#endregion
18
18
  //#region src/opfs.ts
19
- const fileSystemFileHandleFactory = (name, fileData) => {
19
+ const fileSystemFileHandleFactory = (name, fileData, exists) => {
20
20
  return {
21
21
  kind: "file",
22
22
  name,
23
23
  isSameEntry: async (other) => {
24
24
  return other.name === name && other.kind === "file";
25
25
  },
26
- getFile: async () => new File([fileData.content], name),
26
+ getFile: async () => {
27
+ if (!exists()) throw new DOMException("A requested file or directory could not be found at the time an operation was processed.", "NotFoundError");
28
+ return new File([fileData.content], name);
29
+ },
27
30
  createWritable: async (options) => {
28
31
  const keepExistingData = options?.keepExistingData;
29
32
  let abortReason = "";
30
33
  let isAborted = false;
31
34
  let isClosed = false;
32
- let content = keepExistingData ? fileData.content : "";
35
+ let content = keepExistingData ? new Uint8Array(fileData.content) : new Uint8Array();
33
36
  let cursorPosition = keepExistingData ? fileData.content.length : 0;
34
37
  const writableStream = new WritableStream({
35
38
  write: () => {},
@@ -42,21 +45,46 @@ const fileSystemFileHandleFactory = (name, fileData) => {
42
45
  if (isAborted) throw new Error(abortReason);
43
46
  if (isClosed) throw new TypeError("Cannot write to a CLOSED writable stream");
44
47
  if (chunk === void 0) throw new TypeError("Cannot write undefined data to the stream");
45
- let chunkText;
46
- if (typeof chunk === "string") chunkText = chunk;
47
- else if (chunk instanceof Blob) chunkText = await chunk.text();
48
- else if (ArrayBuffer.isView(chunk)) chunkText = new TextDecoder().decode(new Uint8Array(chunk.buffer));
48
+ if (typeof chunk === "object" && "type" in chunk && chunk.type === "truncate") {
49
+ if (typeof chunk.size !== "number" || chunk.size < 0) throw new TypeError("Invalid size value in truncate parameters");
50
+ if (chunk.size < content.length) content = content.slice(0, chunk.size);
51
+ else {
52
+ const extended = new Uint8Array(chunk.size);
53
+ extended.set(content);
54
+ content = extended;
55
+ }
56
+ cursorPosition = Math.min(cursorPosition, chunk.size);
57
+ return;
58
+ }
59
+ let encoded;
60
+ if (typeof chunk === "string") encoded = new TextEncoder().encode(chunk);
61
+ else if (chunk instanceof Blob) {
62
+ const text = await chunk.text();
63
+ encoded = new TextEncoder().encode(text);
64
+ } else if (ArrayBuffer.isView(chunk)) encoded = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
65
+ else if (chunk instanceof ArrayBuffer) encoded = new Uint8Array(chunk);
49
66
  else if (typeof chunk === "object" && "data" in chunk) {
50
67
  if (chunk.position !== void 0 && (typeof chunk.position !== "number" || chunk.position < 0)) throw new TypeError("Invalid position value in write parameters");
51
68
  if (chunk.size !== void 0 && (typeof chunk.size !== "number" || chunk.size < 0)) throw new TypeError("Invalid size value in write parameters");
52
69
  if (chunk.position !== void 0 && chunk.position !== null) cursorPosition = chunk.position;
53
- if (chunk.data) if (typeof chunk.data === "string") chunkText = chunk.data;
54
- else if (chunk.data instanceof Blob) chunkText = await chunk.data.text();
55
- else chunkText = new TextDecoder().decode(new Uint8Array(chunk.data instanceof ArrayBuffer ? chunk.data : chunk.data.buffer));
56
- else chunkText = "";
70
+ const data = chunk.data;
71
+ if (data === void 0 || data === null) encoded = new Uint8Array();
72
+ else if (typeof data === "string") encoded = new TextEncoder().encode(data);
73
+ else if (data instanceof Blob) {
74
+ const text = await data.text();
75
+ encoded = new TextEncoder().encode(text);
76
+ } else if (ArrayBuffer.isView(data)) encoded = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
77
+ else if (data instanceof ArrayBuffer) encoded = new Uint8Array(data);
78
+ else throw new TypeError("Invalid data in WriteParams");
57
79
  } else throw new TypeError("Invalid data type written to the file. Data must be of type FileSystemWriteChunkType.");
58
- content = content.slice(0, cursorPosition) + chunkText + content.slice(cursorPosition + chunkText.length);
59
- cursorPosition += chunkText.length;
80
+ const requiredSize = cursorPosition + encoded.length;
81
+ if (content.length < requiredSize) {
82
+ const extended = new Uint8Array(requiredSize);
83
+ extended.set(content);
84
+ content = extended;
85
+ }
86
+ content.set(encoded, cursorPosition);
87
+ cursorPosition += encoded.length;
60
88
  },
61
89
  close: async function() {
62
90
  if (isClosed) throw new TypeError("Cannot close a CLOSED writable stream");
@@ -73,11 +101,15 @@ const fileSystemFileHandleFactory = (name, fileData) => {
73
101
  truncate: async function(size) {
74
102
  if (size < 0) throw new DOMException("Invalid truncate size", "IndexSizeError");
75
103
  if (size < content.length) content = content.slice(0, size);
76
- else content = content.padEnd(size, "\0");
104
+ else if (size > content.length) {
105
+ const newBuffer = new Uint8Array(size);
106
+ newBuffer.set(content);
107
+ content = newBuffer;
108
+ }
77
109
  cursorPosition = Math.min(cursorPosition, size);
78
110
  },
79
111
  seek: async function(position) {
80
- if (position < 0 || position > content.length) throw new DOMException("Invalid seek position", "IndexSizeError");
112
+ if (position < 0) throw new DOMException("Invalid seek position", "IndexSizeError");
81
113
  cursorPosition = position;
82
114
  }
83
115
  });
@@ -87,26 +119,41 @@ const fileSystemFileHandleFactory = (name, fileData) => {
87
119
  return {
88
120
  getSize: () => {
89
121
  if (closed) throw new DOMException("InvalidStateError", "The access handle is closed");
90
- return fileData.content.length;
122
+ return fileData.content.byteLength;
91
123
  },
92
124
  read: (buffer, { at = 0 } = {}) => {
93
125
  if (closed) throw new DOMException("InvalidStateError", "The access handle is closed");
94
- const text = new TextEncoder().encode(fileData.content);
95
- const bytesRead = Math.min(buffer.length, text.length - at);
96
- buffer.set(text.subarray(at, at + bytesRead));
97
- return bytesRead;
126
+ const content = fileData.content;
127
+ if (at >= content.length) return 0;
128
+ const available = content.length - at;
129
+ const writable = buffer instanceof DataView ? buffer.byteLength : buffer.length;
130
+ const bytesToRead = Math.min(writable, available);
131
+ const slice = content.subarray(at, at + bytesToRead);
132
+ if (buffer instanceof DataView) for (let i = 0; i < slice.length; i++) buffer.setUint8(i, slice[i]);
133
+ else buffer.set(slice, 0);
134
+ return bytesToRead;
98
135
  },
99
- write: (data, options) => {
100
- const at = options?.at ?? 0;
136
+ write: (data, { at = 0 } = {}) => {
101
137
  if (closed) throw new DOMException("InvalidStateError", "The access handle is closed");
102
- const newContent = new TextDecoder().decode(data);
103
- if (at < fileData.content.length) fileData.content = fileData.content.slice(0, at) + newContent + fileData.content.slice(at + newContent.length);
104
- else fileData.content += newContent;
105
- return data.byteLength;
138
+ const writeLength = data instanceof DataView ? data.byteLength : data.length;
139
+ const requiredSize = at + writeLength;
140
+ if (fileData.content.length < requiredSize) {
141
+ const newBuffer = new Uint8Array(requiredSize);
142
+ newBuffer.set(fileData.content);
143
+ fileData.content = newBuffer;
144
+ }
145
+ if (data instanceof DataView) for (let i = 0; i < data.byteLength; i++) fileData.content[at + i] = data.getUint8(i);
146
+ else fileData.content.set(data, at);
147
+ return writeLength;
106
148
  },
107
149
  truncate: (size) => {
108
150
  if (closed) throw new DOMException("InvalidStateError", "The access handle is closed");
109
- fileData.content = fileData.content.slice(0, size);
151
+ if (size < fileData.content.length) fileData.content = fileData.content.slice(0, size);
152
+ else if (size > fileData.content.length) {
153
+ const newBuffer = new Uint8Array(size);
154
+ newBuffer.set(fileData.content);
155
+ fileData.content = newBuffer;
156
+ }
110
157
  },
111
158
  flush: async () => {
112
159
  if (closed) throw new DOMException("InvalidStateError", "The access handle is closed");
@@ -119,8 +166,8 @@ const fileSystemFileHandleFactory = (name, fileData) => {
119
166
  };
120
167
  };
121
168
  const fileSystemDirectoryHandleFactory = (name) => {
122
- const files = new Map();
123
- const directories = new Map();
169
+ const files = /* @__PURE__ */ new Map();
170
+ const directories = /* @__PURE__ */ new Map();
124
171
  const getJoinedMaps = () => {
125
172
  return new Map([...files, ...directories]);
126
173
  };
@@ -131,7 +178,7 @@ const fileSystemDirectoryHandleFactory = (name) => {
131
178
  return other.name === name && other.kind === "directory";
132
179
  },
133
180
  getFileHandle: async (fileName, options) => {
134
- if (!files.has(fileName) && options?.create) files.set(fileName, fileSystemFileHandleFactory(fileName, { content: "" }));
181
+ if (!files.has(fileName) && options?.create) files.set(fileName, fileSystemFileHandleFactory(fileName, { content: new Uint8Array() }, () => files.has(fileName)));
135
182
  const fileHandle = files.get(fileName);
136
183
  if (!fileHandle) throw new DOMException(`File not found: ${fileName}`, "NotFoundError");
137
184
  return fileHandle;
@@ -154,16 +201,13 @@ const fileSystemDirectoryHandleFactory = (name) => {
154
201
  return void 0;
155
202
  },
156
203
  entries: async function* () {
157
- const joinedMaps = getJoinedMaps();
158
- yield* joinedMaps.entries();
204
+ yield* getJoinedMaps().entries();
159
205
  },
160
206
  keys: async function* () {
161
- const joinedMaps = getJoinedMaps();
162
- yield* joinedMaps.keys();
207
+ yield* getJoinedMaps().keys();
163
208
  },
164
209
  values: async function* () {
165
- const joinedMaps = getJoinedMaps();
166
- yield* joinedMaps.values();
210
+ yield* getJoinedMaps().values();
167
211
  },
168
212
  resolve: async function(possibleDescendant) {
169
213
  const traverseDirectory = async (directory, target, path = []) => {
@@ -187,10 +231,8 @@ const storageFactory = ({ usage = 0, quota = 1024 ** 3 } = {}) => {
187
231
  const root = fileSystemDirectoryHandleFactory("root");
188
232
  return {
189
233
  estimate: async () => {
190
- const defaultUsage = usage;
191
- const calculatedUsage = await getSizeOfDirectory(root);
192
234
  return {
193
- usage: defaultUsage + calculatedUsage,
235
+ usage: usage + await getSizeOfDirectory(root),
194
236
  quota
195
237
  };
196
238
  },
@@ -210,13 +252,10 @@ const mockOPFS = () => {
210
252
  value: {},
211
253
  writable: true
212
254
  });
213
- if (!globalThis.navigator.storage) {
214
- const { getDirectory } = storageFactory();
215
- Object.defineProperty(globalThis.navigator, "storage", {
216
- value: { getDirectory },
217
- writable: true
218
- });
219
- }
255
+ if (!globalThis.navigator.storage) Object.defineProperty(globalThis.navigator, "storage", {
256
+ value: storageFactory(),
257
+ writable: true
258
+ });
220
259
  };
221
260
  const resetMockOPFS = () => {
222
261
  const root = fileSystemDirectoryHandleFactory("root");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opfs-mock",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
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>",
@@ -13,7 +13,16 @@
13
13
  "bugs": {
14
14
  "url": "https://github.com/jurerotar/opfs-mock/issues"
15
15
  },
16
- "keywords": ["vitest", "jest", "test", "mock", "opfs", "storage", "node", "browser"],
16
+ "keywords": [
17
+ "vitest",
18
+ "jest",
19
+ "test",
20
+ "mock",
21
+ "opfs",
22
+ "storage",
23
+ "node",
24
+ "browser"
25
+ ],
17
26
  "publishConfig": {
18
27
  "access": "public"
19
28
  },
@@ -25,7 +34,9 @@
25
34
  },
26
35
  "main": "dist/index.js",
27
36
  "module": "dist/index.js",
28
- "files": ["dist"],
37
+ "files": [
38
+ "dist"
39
+ ],
29
40
  "engines": {
30
41
  "node": ">=20.0.0"
31
42
  },
@@ -45,12 +56,12 @@
45
56
  "release": "npm publish --access public"
46
57
  },
47
58
  "devDependencies": {
48
- "@biomejs/biome": "1.9.4",
59
+ "@biomejs/biome": "2.2.6",
49
60
  "@web-std/file": "3.0.3",
50
- "happy-dom": "17.4.7",
51
- "jsdom": "26.1.0",
52
- "tsdown": "0.12.3",
53
- "typescript": "5.8.3",
54
- "vitest": "3.1.4"
61
+ "happy-dom": "20.0.5",
62
+ "jsdom": "27.0.1",
63
+ "tsdown": "0.15.7",
64
+ "typescript": "5.9.3",
65
+ "vitest": "3.2.4"
55
66
  }
56
67
  }