@zenfs/core 0.10.0 → 0.11.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.
Files changed (63) hide show
  1. package/dist/backends/Index.d.ts +3 -3
  2. package/dist/backends/Index.js +4 -4
  3. package/dist/backends/backend.d.ts +1 -2
  4. package/dist/backends/backend.js +0 -5
  5. package/dist/backends/{Fetch.d.ts → fetch.d.ts} +4 -4
  6. package/dist/backends/{Fetch.js → fetch.js} +6 -7
  7. package/dist/backends/{Locked.d.ts → locked.d.ts} +2 -2
  8. package/dist/backends/{Locked.js → locked.js} +4 -5
  9. package/dist/backends/{InMemory.d.ts → memory.d.ts} +7 -9
  10. package/dist/backends/memory.js +38 -0
  11. package/dist/backends/{Overlay.d.ts → overlay.d.ts} +12 -13
  12. package/dist/backends/{Overlay.js → overlay.js} +98 -103
  13. package/dist/backends/port/fs.d.ts +15 -16
  14. package/dist/backends/port/fs.js +20 -22
  15. package/dist/backends/store/fs.d.ts +169 -0
  16. package/dist/backends/store/fs.js +743 -0
  17. package/dist/backends/store/simple.d.ts +64 -0
  18. package/dist/backends/store/simple.js +111 -0
  19. package/dist/backends/store/store.d.ts +111 -0
  20. package/dist/backends/store/store.js +62 -0
  21. package/dist/browser.min.js +4 -4
  22. package/dist/browser.min.js.map +4 -4
  23. package/dist/config.d.ts +1 -1
  24. package/dist/config.js +1 -1
  25. package/dist/emulation/shared.js +1 -1
  26. package/dist/error.d.ts +0 -1
  27. package/dist/error.js +0 -1
  28. package/dist/file.js +1 -1
  29. package/dist/filesystem.d.ts +3 -3
  30. package/dist/filesystem.js +26 -29
  31. package/dist/index.d.ts +7 -7
  32. package/dist/index.js +7 -7
  33. package/dist/inode.d.ts +1 -1
  34. package/package.json +1 -1
  35. package/src/backends/Index.ts +4 -4
  36. package/src/backends/backend.ts +3 -7
  37. package/src/backends/{Fetch.ts → fetch.ts} +13 -14
  38. package/src/backends/{Locked.ts → locked.ts} +5 -6
  39. package/src/backends/memory.ts +44 -0
  40. package/src/backends/{Overlay.ts → overlay.ts} +99 -105
  41. package/src/backends/port/fs.ts +24 -26
  42. package/src/backends/store/fs.ts +881 -0
  43. package/src/backends/store/readme.md +9 -0
  44. package/src/backends/store/simple.ts +144 -0
  45. package/src/backends/store/store.ts +164 -0
  46. package/src/config.ts +3 -3
  47. package/src/emulation/shared.ts +1 -1
  48. package/src/error.ts +0 -1
  49. package/src/file.ts +1 -1
  50. package/src/filesystem.ts +29 -32
  51. package/src/index.ts +7 -7
  52. package/src/inode.ts +1 -1
  53. package/dist/backends/AsyncStore.d.ts +0 -204
  54. package/dist/backends/AsyncStore.js +0 -509
  55. package/dist/backends/InMemory.js +0 -49
  56. package/dist/backends/SyncStore.d.ts +0 -213
  57. package/dist/backends/SyncStore.js +0 -445
  58. package/dist/backends/port/store.d.ts +0 -30
  59. package/dist/backends/port/store.js +0 -142
  60. package/src/backends/AsyncStore.ts +0 -655
  61. package/src/backends/InMemory.ts +0 -56
  62. package/src/backends/SyncStore.ts +0 -589
  63. package/src/backends/port/store.ts +0 -187
@@ -1,187 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { ErrnoError, Errno } from '../../error.js';
3
- import { type AsyncStore, type AsyncTransaction, type AsyncStoreOptions, AsyncStoreFS } from '../AsyncStore.js';
4
- import type { SyncTransaction, SyncStore } from '../SyncStore.js';
5
- import type { Backend } from '../backend.js';
6
- import * as RPC from './rpc.js';
7
- import type { ExtractProperties } from 'utilium';
8
-
9
- export class PortStore implements AsyncStore {
10
- public readonly port: RPC.Port;
11
- public constructor(
12
- public readonly options: RPC.Options,
13
- public readonly name: string = 'port'
14
- ) {
15
- this.port = options.port;
16
- RPC.attach<RPC.Response>(this.port, RPC.handleResponse);
17
- }
18
-
19
- public clear(): Promise<void> {
20
- return RPC.request(
21
- {
22
- scope: 'store',
23
- method: 'clear',
24
- args: [],
25
- },
26
- this.options
27
- );
28
- }
29
-
30
- public beginTransaction(): PortTransaction {
31
- const id = RPC.request<RPC.Request, number>(
32
- {
33
- scope: 'store',
34
- method: 'beginTransaction',
35
- args: [],
36
- },
37
- this.options
38
- );
39
- return new PortTransaction(this, id);
40
- }
41
- }
42
-
43
- type TxMethods = ExtractProperties<AsyncTransaction, (...args: any[]) => Promise<any>>;
44
- type TxMethod = keyof TxMethods;
45
- interface TxRequest<TMethod extends TxMethod = TxMethod> extends RPC.Request<'transaction', TMethod | 'close', Parameters<TxMethods[TMethod]>> {
46
- tx: number;
47
- }
48
-
49
- export class PortTransaction implements AsyncTransaction {
50
- constructor(
51
- public readonly store: PortStore,
52
- public readonly id: number | Promise<number>
53
- ) {}
54
-
55
- public async rpc<const T extends TxMethod>(method: T, ...args: Parameters<TxMethods[T]>): Promise<Awaited<ReturnType<TxMethods[T]>>> {
56
- return RPC.request<TxRequest<T>, Awaited<ReturnType<TxMethods[T]>>>(
57
- {
58
- scope: 'transaction',
59
- tx: await this.id,
60
- method,
61
- args,
62
- },
63
- this.store.options
64
- );
65
- }
66
-
67
- public get(key: bigint): Promise<Uint8Array> {
68
- return this.rpc('get', key);
69
- }
70
-
71
- public async put(key: bigint, data: Uint8Array, overwrite: boolean): Promise<boolean> {
72
- return await this.rpc('put', key, data, overwrite);
73
- }
74
-
75
- public async remove(key: bigint): Promise<void> {
76
- return await this.rpc('remove', key);
77
- }
78
-
79
- public async commit(): Promise<void> {
80
- return await this.rpc('commit');
81
- }
82
-
83
- public async abort(): Promise<void> {
84
- return await this.rpc('abort');
85
- }
86
- }
87
-
88
- let nextTx = 0;
89
-
90
- const transactions: Map<number, AsyncTransaction | SyncTransaction> = new Map();
91
-
92
- type StoreOrTxRequest = TxRequest | RPC.Request<'store', keyof ExtractProperties<AsyncStore, (...args: any[]) => any>>;
93
-
94
- async function handleRequest(port: RPC.Port, store: AsyncStore | SyncStore, request: StoreOrTxRequest): Promise<void> {
95
- if (!RPC.isMessage(request)) {
96
- return;
97
- }
98
- const { method, args, id, scope, stack } = request;
99
-
100
- let value,
101
- error: boolean = false;
102
-
103
- try {
104
- switch (scope) {
105
- case 'store':
106
- // @ts-expect-error 2556
107
- value = await store[method](...args);
108
- if (method == 'beginTransaction') {
109
- transactions.set(++nextTx, value!);
110
- value = nextTx;
111
- }
112
- break;
113
- case 'transaction':
114
- const { tx } = request as TxRequest;
115
- if (!transactions.has(tx)) {
116
- throw new ErrnoError(Errno.EBADF);
117
- }
118
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
119
- // @ts-ignore 2556
120
- value = await transactions.get(tx)![method](...args);
121
- if (method == 'close') {
122
- transactions.delete(tx);
123
- }
124
- break;
125
- default:
126
- return;
127
- }
128
- } catch (e) {
129
- value = e;
130
- error = true;
131
- }
132
-
133
- port.postMessage({
134
- _zenfs: true,
135
- scope,
136
- id,
137
- error,
138
- method,
139
- stack,
140
- value: value instanceof ErrnoError ? value.toJSON() : value,
141
- });
142
- }
143
-
144
- export function attachStore(port: RPC.Port, store: SyncStore | AsyncStore): void {
145
- RPC.attach(port, (request: StoreOrTxRequest) => handleRequest(port, store, request));
146
- }
147
-
148
- export function detachStore(port: RPC.Port, store: SyncStore | AsyncStore): void {
149
- RPC.detach(port, (request: StoreOrTxRequest) => handleRequest(port, store, request));
150
- }
151
-
152
- export const PortStoreBackend: Backend = {
153
- name: 'PortStore',
154
-
155
- options: {
156
- port: {
157
- type: 'object',
158
- description: 'The target port that you want to connect to',
159
- validator(port: RPC.Port) {
160
- // Check for a `postMessage` function.
161
- if (typeof port?.postMessage != 'function') {
162
- throw new ErrnoError(Errno.EINVAL, 'option must be a port.');
163
- }
164
- },
165
- },
166
- },
167
-
168
- async isAvailable(): Promise<boolean> {
169
- if ('WorkerGlobalScope' in globalThis && globalThis instanceof (globalThis as typeof globalThis & { WorkerGlobalScope: any }).WorkerGlobalScope) {
170
- // Web Worker
171
- return true;
172
- }
173
-
174
- try {
175
- const worker_threads = await import('node:worker_threads');
176
-
177
- // NodeJS worker
178
- return 'Worker' in worker_threads;
179
- } catch (e) {
180
- return false;
181
- }
182
- },
183
-
184
- create(options: RPC.Options & AsyncStoreOptions & { name?: string }) {
185
- return new AsyncStoreFS({ ...options, store: new PortStore(options, options?.name) });
186
- },
187
- };