opfs-mock 1.0.2 → 2.1.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 +21 -21
- package/README.md +159 -159
- package/dist/index.js +183 -48
- package/package.json +4 -4
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,159 +1,159 @@
|
|
|
1
|
-
# opfs-mock
|
|
2
|
-
|
|
3
|
-
This is a pure JS 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.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```shell
|
|
8
|
-
npm install -save-dev opfs-mock
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
It replicates the behavior of origin private file system, except data is not persisted to disk.
|
|
14
|
-
|
|
15
|
-
The easiest way to use it is to import `opfs-mock`, which will polyfill OPFS API to global scope.
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import "opfs-mock";
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Alternatively, you can explicitly import `storageFactory`:
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import { storageFactory } from "opfs-mock";
|
|
25
|
-
|
|
26
|
-
test('Your test', async () => {
|
|
27
|
-
const storage = await storageFactory();
|
|
28
|
-
const root = await storage.getDirectory();
|
|
29
|
-
const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
|
|
30
|
-
// rest of your test
|
|
31
|
-
});
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
`storageFactory` has
|
|
35
|
-
In case you need specific values, you can pass both as arguments to `storageFactory`.
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
import { storageFactory } from "opfs-mock";
|
|
39
|
-
|
|
40
|
-
test('Your test', async () => {
|
|
41
|
-
const storage = await storageFactory({ quota: 1_000_000,
|
|
42
|
-
const root = await storage.getDirectory();
|
|
43
|
-
const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
|
|
44
|
-
// rest of your test
|
|
45
|
-
});
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### Vitest
|
|
49
|
-
|
|
50
|
-
To use `opfs-mock` in a single Vitest test suite, require `opfs-mock` at the beginning of the test file, as described above.
|
|
51
|
-
|
|
52
|
-
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:
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
// vite.config.ts
|
|
56
|
-
|
|
57
|
-
import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
|
|
58
|
-
import { defineConfig as defineVitestConfig } from 'vitest/config';
|
|
59
|
-
|
|
60
|
-
const viteConfig = defineViteConfig({
|
|
61
|
-
...
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const vitestConfig = defineVitestConfig({
|
|
65
|
-
test: {
|
|
66
|
-
setupFiles: ['opfs-mock'],
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
export default mergeConfig(viteConfig, vitestConfig);
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
Alternatively you can create a new setup file which then imports this module.
|
|
74
|
-
|
|
75
|
-
```ts
|
|
76
|
-
// vitest-setup.ts
|
|
77
|
-
|
|
78
|
-
import "opfs-mock";
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
Add that file to your `test.setupFiles` array:
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
// vite.config.ts
|
|
85
|
-
|
|
86
|
-
import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
|
|
87
|
-
import { defineConfig as defineVitestConfig } from 'vitest/config';
|
|
88
|
-
|
|
89
|
-
const viteConfig = defineViteConfig({
|
|
90
|
-
...
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
const vitestConfig = defineVitestConfig({
|
|
94
|
-
test: {
|
|
95
|
-
setupFiles: ['vitest-setup.ts'],
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
export default mergeConfig(viteConfig, vitestConfig);
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
### Jest
|
|
104
|
-
|
|
105
|
-
To use `opfs-mock` in a single Jest test suite, require `opfs-mock` at the beginning of the test file, as described above.
|
|
106
|
-
|
|
107
|
-
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:
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
// jest.config.js
|
|
111
|
-
|
|
112
|
-
{
|
|
113
|
-
...
|
|
114
|
-
"setupFiles": [
|
|
115
|
-
"opfs-mock"
|
|
116
|
-
]
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Alternatively you can create a new setup file which then imports this module.
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
// jest-setup.ts
|
|
124
|
-
|
|
125
|
-
import "opfs-mock";
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
Add that file to your `setupFiles` array:
|
|
129
|
-
|
|
130
|
-
```ts
|
|
131
|
-
// jest.config.js
|
|
132
|
-
|
|
133
|
-
{
|
|
134
|
-
...
|
|
135
|
-
"setupFiles": [
|
|
136
|
-
"jest-setup"
|
|
137
|
-
]
|
|
138
|
-
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## Wiping/resetting the OPFS mock for a fresh state
|
|
142
|
-
|
|
143
|
-
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.
|
|
144
|
-
|
|
145
|
-
```ts
|
|
146
|
-
import { resetMockOPFS } from 'opfs-mock';
|
|
147
|
-
|
|
148
|
-
beforeEach(() => {
|
|
149
|
-
resetMockOPFS();
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
test('First isolated test', async () => {
|
|
153
|
-
// rest of your test
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test('Second isolated test', async () => {
|
|
157
|
-
// rest of your test
|
|
158
|
-
});
|
|
159
|
-
```
|
|
1
|
+
# opfs-mock
|
|
2
|
+
|
|
3
|
+
This is a pure JS 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.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install -save-dev opfs-mock
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
It replicates the behavior of origin private file system, except data is not persisted to disk.
|
|
14
|
+
|
|
15
|
+
The easiest way to use it is to import `opfs-mock`, which will polyfill OPFS API to global scope.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import "opfs-mock";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Alternatively, you can explicitly import `storageFactory`:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { storageFactory } from "opfs-mock";
|
|
25
|
+
|
|
26
|
+
test('Your test', async () => {
|
|
27
|
+
const storage = await storageFactory();
|
|
28
|
+
const root = await storage.getDirectory();
|
|
29
|
+
const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
|
|
30
|
+
// rest of your test
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`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.
|
|
35
|
+
In case you need specific values, you can pass both as arguments to `storageFactory`.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { storageFactory } from "opfs-mock";
|
|
39
|
+
|
|
40
|
+
test('Your test', async () => {
|
|
41
|
+
const storage = await storageFactory({ quota: 1_000_000, usage: 1_000 });
|
|
42
|
+
const root = await storage.getDirectory();
|
|
43
|
+
const directoryHandle = await root.getFileHandle('test-file.txt', { create: true });
|
|
44
|
+
// rest of your test
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Vitest
|
|
49
|
+
|
|
50
|
+
To use `opfs-mock` in a single Vitest test suite, require `opfs-mock` at the beginning of the test file, as described above.
|
|
51
|
+
|
|
52
|
+
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:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// vite.config.ts
|
|
56
|
+
|
|
57
|
+
import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
|
|
58
|
+
import { defineConfig as defineVitestConfig } from 'vitest/config';
|
|
59
|
+
|
|
60
|
+
const viteConfig = defineViteConfig({
|
|
61
|
+
...
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const vitestConfig = defineVitestConfig({
|
|
65
|
+
test: {
|
|
66
|
+
setupFiles: ['opfs-mock'],
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export default mergeConfig(viteConfig, vitestConfig);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Alternatively you can create a new setup file which then imports this module.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
// vitest-setup.ts
|
|
77
|
+
|
|
78
|
+
import "opfs-mock";
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Add that file to your `test.setupFiles` array:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
// vite.config.ts
|
|
85
|
+
|
|
86
|
+
import { defineConfig as defineViteConfig, mergeConfig } from 'vite';
|
|
87
|
+
import { defineConfig as defineVitestConfig } from 'vitest/config';
|
|
88
|
+
|
|
89
|
+
const viteConfig = defineViteConfig({
|
|
90
|
+
...
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const vitestConfig = defineVitestConfig({
|
|
94
|
+
test: {
|
|
95
|
+
setupFiles: ['vitest-setup.ts'],
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export default mergeConfig(viteConfig, vitestConfig);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
### Jest
|
|
104
|
+
|
|
105
|
+
To use `opfs-mock` in a single Jest test suite, require `opfs-mock` at the beginning of the test file, as described above.
|
|
106
|
+
|
|
107
|
+
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:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
// jest.config.js
|
|
111
|
+
|
|
112
|
+
{
|
|
113
|
+
...
|
|
114
|
+
"setupFiles": [
|
|
115
|
+
"opfs-mock"
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Alternatively you can create a new setup file which then imports this module.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// jest-setup.ts
|
|
124
|
+
|
|
125
|
+
import "opfs-mock";
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Add that file to your `setupFiles` array:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
// jest.config.js
|
|
132
|
+
|
|
133
|
+
{
|
|
134
|
+
...
|
|
135
|
+
"setupFiles": [
|
|
136
|
+
"jest-setup"
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Wiping/resetting the OPFS mock for a fresh state
|
|
142
|
+
|
|
143
|
+
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.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { resetMockOPFS } from 'opfs-mock';
|
|
147
|
+
|
|
148
|
+
beforeEach(() => {
|
|
149
|
+
resetMockOPFS();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('First isolated test', async () => {
|
|
153
|
+
// rest of your test
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('Second isolated test', async () => {
|
|
157
|
+
// rest of your test
|
|
158
|
+
});
|
|
159
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
var isFileHandle = (handle) => {
|
|
3
|
+
return handle.kind === "file";
|
|
4
|
+
};
|
|
5
|
+
var isDirectoryHandle = (handle) => {
|
|
6
|
+
return handle.kind === "directory";
|
|
7
|
+
};
|
|
8
|
+
var getSizeOfDirectory = async (directory) => {
|
|
9
|
+
let totalSize = 0;
|
|
10
|
+
for await (const handle of directory.values()) {
|
|
11
|
+
if (isFileHandle(handle)) {
|
|
12
|
+
const file = await handle.getFile();
|
|
13
|
+
totalSize += file.size;
|
|
14
|
+
} else if (isDirectoryHandle(handle)) {
|
|
15
|
+
totalSize += await getSizeOfDirectory(handle);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return totalSize;
|
|
19
|
+
};
|
|
20
|
+
|
|
1
21
|
// src/opfs.ts
|
|
2
22
|
var fileSystemFileHandleFactory = (name, fileData) => {
|
|
3
23
|
return {
|
|
@@ -6,51 +26,154 @@ var fileSystemFileHandleFactory = (name, fileData) => {
|
|
|
6
26
|
isSameEntry: async (other) => {
|
|
7
27
|
return other.name === name && other.kind === "file";
|
|
8
28
|
},
|
|
9
|
-
getFile: async () => new File([fileData.content], name,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
29
|
+
getFile: async () => new File([fileData.content], name),
|
|
30
|
+
createWritable: async (options) => {
|
|
31
|
+
const keepExistingData = options?.keepExistingData;
|
|
32
|
+
let abortReason = "";
|
|
33
|
+
let isAborted = false;
|
|
34
|
+
let isClosed = false;
|
|
35
|
+
let content = keepExistingData ? fileData.content : "";
|
|
36
|
+
let cursorPosition = keepExistingData ? fileData.content.length : 0;
|
|
14
37
|
const writableStream = new WritableStream({
|
|
15
|
-
write: (
|
|
16
|
-
const newContent = new TextDecoder().decode(chunk);
|
|
17
|
-
fileData.content += newContent;
|
|
38
|
+
write: () => {
|
|
18
39
|
},
|
|
19
40
|
close: () => {
|
|
20
41
|
},
|
|
21
|
-
abort: (
|
|
42
|
+
abort: () => {
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return Object.assign(writableStream, {
|
|
46
|
+
getWriter: () => writableStream.getWriter(),
|
|
47
|
+
write: async function(chunk) {
|
|
48
|
+
if (isAborted) {
|
|
49
|
+
throw new Error(abortReason);
|
|
50
|
+
}
|
|
51
|
+
if (isClosed) {
|
|
52
|
+
throw new TypeError("Cannot write to a CLOSED writable stream");
|
|
53
|
+
}
|
|
54
|
+
if (chunk === void 0) {
|
|
55
|
+
throw new TypeError("Cannot write undefined data to the stream");
|
|
56
|
+
}
|
|
57
|
+
let chunkText;
|
|
58
|
+
if (typeof chunk === "string") {
|
|
59
|
+
chunkText = chunk;
|
|
60
|
+
} else if (chunk instanceof Blob) {
|
|
61
|
+
chunkText = await chunk.text();
|
|
62
|
+
} else if (ArrayBuffer.isView(chunk)) {
|
|
63
|
+
chunkText = new TextDecoder().decode(new Uint8Array(chunk.buffer));
|
|
64
|
+
} else if (typeof chunk === "object" && "data" in chunk) {
|
|
65
|
+
if (chunk.position !== void 0 && (typeof chunk.position !== "number" || chunk.position < 0)) {
|
|
66
|
+
throw new TypeError("Invalid position value in write parameters");
|
|
67
|
+
}
|
|
68
|
+
if (chunk.size !== void 0 && (typeof chunk.size !== "number" || chunk.size < 0)) {
|
|
69
|
+
throw new TypeError("Invalid size value in write parameters");
|
|
70
|
+
}
|
|
71
|
+
if (chunk.position !== void 0 && chunk.position !== null) {
|
|
72
|
+
cursorPosition = chunk.position;
|
|
73
|
+
}
|
|
74
|
+
if (chunk.data) {
|
|
75
|
+
if (typeof chunk.data === "string") {
|
|
76
|
+
chunkText = chunk.data;
|
|
77
|
+
} else if (chunk.data instanceof Blob) {
|
|
78
|
+
chunkText = await chunk.data.text();
|
|
79
|
+
} else {
|
|
80
|
+
chunkText = new TextDecoder().decode(new Uint8Array(chunk.data instanceof ArrayBuffer ? chunk.data : chunk.data.buffer));
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
chunkText = "";
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
throw new TypeError("Invalid data type written to the file. Data must be of type FileSystemWriteChunkType.");
|
|
87
|
+
}
|
|
88
|
+
content = content.slice(0, cursorPosition) + chunkText + content.slice(cursorPosition + chunkText.length);
|
|
89
|
+
cursorPosition += chunkText.length;
|
|
90
|
+
},
|
|
91
|
+
close: async function() {
|
|
92
|
+
if (isClosed) {
|
|
93
|
+
throw new TypeError("Cannot close a CLOSED writable stream");
|
|
94
|
+
}
|
|
95
|
+
if (isAborted) {
|
|
96
|
+
throw new TypeError("Cannot close a ERRORED writable stream");
|
|
97
|
+
}
|
|
98
|
+
isClosed = true;
|
|
99
|
+
fileData.content = content;
|
|
100
|
+
},
|
|
101
|
+
abort: async function(reason) {
|
|
102
|
+
if (isAborted) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (reason && !abortReason) {
|
|
106
|
+
abortReason = reason;
|
|
107
|
+
}
|
|
108
|
+
isAborted = true;
|
|
109
|
+
return Promise.resolve(void 0);
|
|
110
|
+
},
|
|
111
|
+
truncate: async function(size) {
|
|
112
|
+
if (size < 0) {
|
|
113
|
+
throw new DOMException("Invalid truncate size", "IndexSizeError");
|
|
114
|
+
}
|
|
115
|
+
if (size < content.length) {
|
|
116
|
+
content = content.slice(0, size);
|
|
117
|
+
} else {
|
|
118
|
+
content = content.padEnd(size, "\0");
|
|
119
|
+
}
|
|
120
|
+
cursorPosition = Math.min(cursorPosition, size);
|
|
121
|
+
},
|
|
122
|
+
seek: async function(position) {
|
|
123
|
+
if (position < 0 || position > content.length) {
|
|
124
|
+
throw new DOMException("Invalid seek position", "IndexSizeError");
|
|
125
|
+
}
|
|
126
|
+
cursorPosition = position;
|
|
22
127
|
}
|
|
23
128
|
});
|
|
24
|
-
return writableStream.getWriter();
|
|
25
129
|
},
|
|
26
|
-
createSyncAccessHandle: async () =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
130
|
+
createSyncAccessHandle: async () => {
|
|
131
|
+
let closed = false;
|
|
132
|
+
return {
|
|
133
|
+
getSize: () => {
|
|
134
|
+
if (closed) {
|
|
135
|
+
throw new DOMException("InvalidStateError", "The access handle is closed");
|
|
136
|
+
}
|
|
137
|
+
return fileData.content.length;
|
|
138
|
+
},
|
|
139
|
+
read: (buffer, { at = 0 } = {}) => {
|
|
140
|
+
if (closed) {
|
|
141
|
+
throw new DOMException("InvalidStateError", "The access handle is closed");
|
|
142
|
+
}
|
|
143
|
+
const text = new TextEncoder().encode(fileData.content);
|
|
144
|
+
const bytesRead = Math.min(buffer.length, text.length - at);
|
|
145
|
+
buffer.set(text.subarray(at, at + bytesRead));
|
|
146
|
+
return bytesRead;
|
|
147
|
+
},
|
|
148
|
+
write: (data, options) => {
|
|
149
|
+
const at = options?.at ?? 0;
|
|
150
|
+
if (closed) {
|
|
151
|
+
throw new DOMException("InvalidStateError", "The access handle is closed");
|
|
152
|
+
}
|
|
153
|
+
const newContent = new TextDecoder().decode(data);
|
|
154
|
+
if (at < fileData.content.length) {
|
|
155
|
+
fileData.content = fileData.content.slice(0, at) + newContent + fileData.content.slice(at + newContent.length);
|
|
156
|
+
} else {
|
|
157
|
+
fileData.content += newContent;
|
|
158
|
+
}
|
|
159
|
+
return data.byteLength;
|
|
160
|
+
},
|
|
161
|
+
truncate: (size) => {
|
|
162
|
+
if (closed) {
|
|
163
|
+
throw new DOMException("InvalidStateError", "The access handle is closed");
|
|
164
|
+
}
|
|
165
|
+
fileData.content = fileData.content.slice(0, size);
|
|
166
|
+
},
|
|
167
|
+
flush: async () => {
|
|
168
|
+
if (closed) {
|
|
169
|
+
throw new DOMException("InvalidStateError", "The access handle is closed");
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
close: async () => {
|
|
173
|
+
closed = true;
|
|
41
174
|
}
|
|
42
|
-
|
|
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
|
-
})
|
|
175
|
+
};
|
|
176
|
+
}
|
|
54
177
|
};
|
|
55
178
|
};
|
|
56
179
|
var fileSystemDirectoryHandleFactory = (name) => {
|
|
@@ -85,14 +208,25 @@ var fileSystemDirectoryHandleFactory = (name) => {
|
|
|
85
208
|
}
|
|
86
209
|
return directoryHandle;
|
|
87
210
|
},
|
|
88
|
-
removeEntry: async (entryName) => {
|
|
211
|
+
removeEntry: async (entryName, options) => {
|
|
89
212
|
if (files.has(entryName)) {
|
|
90
213
|
files.delete(entryName);
|
|
91
214
|
} else if (directories.has(entryName)) {
|
|
92
|
-
|
|
215
|
+
if (options?.recursive) {
|
|
216
|
+
directories.delete(entryName);
|
|
217
|
+
} else {
|
|
218
|
+
throw new DOMException(`Failed to remove directory: $1${entryName}`, "InvalidModificationError");
|
|
219
|
+
}
|
|
93
220
|
} else {
|
|
94
|
-
throw new
|
|
221
|
+
throw new DOMException(`No such file or directory: $1${entryName}`, "NotFoundError");
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
[Symbol.asyncIterator]: async function* () {
|
|
225
|
+
const entries = getJoinedMaps();
|
|
226
|
+
for (const [name2, handle] of entries) {
|
|
227
|
+
yield [name2, handle];
|
|
95
228
|
}
|
|
229
|
+
return void 0;
|
|
96
230
|
},
|
|
97
231
|
entries: async function* () {
|
|
98
232
|
const joinedMaps = getJoinedMaps();
|
|
@@ -106,19 +240,18 @@ var fileSystemDirectoryHandleFactory = (name) => {
|
|
|
106
240
|
const joinedMaps = getJoinedMaps();
|
|
107
241
|
yield* joinedMaps.values();
|
|
108
242
|
},
|
|
109
|
-
resolve: async (possibleDescendant)
|
|
243
|
+
resolve: async function(possibleDescendant) {
|
|
110
244
|
const traverseDirectory = async (directory, target, path = []) => {
|
|
111
245
|
if (await directory.isSameEntry(target)) {
|
|
112
246
|
return path;
|
|
113
247
|
}
|
|
114
248
|
for await (const [name2, handle] of directory.entries()) {
|
|
115
|
-
if (handle
|
|
116
|
-
const
|
|
117
|
-
const result = await traverseDirectory(subDirectory, target, [...path, name2]);
|
|
249
|
+
if (isDirectoryHandle(handle)) {
|
|
250
|
+
const result = await traverseDirectory(handle, target, [...path, name2]);
|
|
118
251
|
if (result) {
|
|
119
252
|
return result;
|
|
120
253
|
}
|
|
121
|
-
} else if (handle
|
|
254
|
+
} else if (isFileHandle(handle)) {
|
|
122
255
|
if (await handle.isSameEntry(target)) {
|
|
123
256
|
return [...path, name2];
|
|
124
257
|
}
|
|
@@ -126,18 +259,20 @@ var fileSystemDirectoryHandleFactory = (name) => {
|
|
|
126
259
|
}
|
|
127
260
|
return null;
|
|
128
261
|
};
|
|
129
|
-
return traverseDirectory(
|
|
262
|
+
return traverseDirectory(this, possibleDescendant);
|
|
130
263
|
}
|
|
131
264
|
};
|
|
132
265
|
};
|
|
133
266
|
|
|
134
267
|
// src/index.ts
|
|
135
|
-
var storageFactory = ({ usage = 0, quota =
|
|
268
|
+
var storageFactory = ({ usage = 0, quota = 1024 ** 3 } = {}) => {
|
|
136
269
|
const root = fileSystemDirectoryHandleFactory("root");
|
|
137
270
|
return {
|
|
138
271
|
estimate: async () => {
|
|
272
|
+
const defaultUsage = usage;
|
|
273
|
+
const calculatedUsage = await getSizeOfDirectory(root);
|
|
139
274
|
return {
|
|
140
|
-
usage,
|
|
275
|
+
usage: defaultUsage + calculatedUsage,
|
|
141
276
|
quota
|
|
142
277
|
};
|
|
143
278
|
},
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opfs-mock",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "2.1.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>",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"homepage": "https://github.com/jurerotar/opfs-mock#
|
|
8
|
+
"homepage": "https://github.com/jurerotar/opfs-mock#readme",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "git+https://github.com/jurerotar/opfs-mock.git"
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@biomejs/biome": "1.9.4",
|
|
43
|
-
"tsup": "8.
|
|
43
|
+
"tsup": "8.4.0",
|
|
44
44
|
"typescript": "5.7.3",
|
|
45
|
-
"vitest": "
|
|
45
|
+
"vitest": "3.0.7"
|
|
46
46
|
}
|
|
47
47
|
}
|