@dxos/random-access-storage 0.4.9 → 0.4.10-main.05b9ab6

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.
@@ -11,7 +11,7 @@ import { type File, type Storage, StorageType } from '../common';
11
11
 
12
12
  export const randomText = () => uuid.v4();
13
13
 
14
- export const storageTests = (testGroupName: StorageType, createStorage: () => Storage) => {
14
+ export const storageTests = (testGroupName: StorageType, createStorage: (name: string) => Storage) => {
15
15
  const writeAndCheck = async (file: File, data: Buffer, offset = 0) => {
16
16
  await file.write(offset, data);
17
17
  const bufferRead = await file.read(offset, data.length);
@@ -21,7 +21,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
21
21
 
22
22
  describe(testGroupName, () => {
23
23
  test('open & close', async () => {
24
- const storage = createStorage();
24
+ const storageName = uuid.v4();
25
+
26
+ const storage = createStorage(storageName);
25
27
  const directory = storage.createDirectory();
26
28
  const fileName = randomText();
27
29
  const file = directory.getOrCreateFile(fileName);
@@ -29,7 +31,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
29
31
  });
30
32
 
31
33
  test('open file, read & write', async () => {
32
- const storage = createStorage();
34
+ const storageName = uuid.v4();
35
+
36
+ const storage = createStorage(storageName);
33
37
  const fileName = randomText();
34
38
  const directory = storage.createDirectory();
35
39
  const file = directory.getOrCreateFile(fileName);
@@ -45,9 +49,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
45
49
  });
46
50
 
47
51
  test('list files', async () => {
48
- // await waitForDebugger();
52
+ const storageName = uuid.v4();
49
53
 
50
- const storage = createStorage();
54
+ const storage = createStorage(storageName);
51
55
  const directoryName = randomText();
52
56
  const directory = storage.createDirectory(directoryName);
53
57
 
@@ -84,7 +88,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
84
88
  });
85
89
 
86
90
  test('read from empty file', async () => {
87
- const storage = createStorage();
91
+ const storageName = uuid.v4();
92
+
93
+ const storage = createStorage(storageName);
88
94
  const directory = storage.createDirectory();
89
95
 
90
96
  const fileName = randomText();
@@ -95,7 +101,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
95
101
  });
96
102
 
97
103
  test('reopen and check if data is the same', async () => {
98
- const storage = createStorage();
104
+ const storageName = uuid.v4();
105
+
106
+ const storage = createStorage(storageName);
99
107
  const directory = storage.createDirectory();
100
108
  const fileName = randomText();
101
109
  const data1 = Buffer.from(randomText());
@@ -115,7 +123,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
115
123
  });
116
124
 
117
125
  test('destroy clears all data', async () => {
118
- const storage = createStorage();
126
+ const storageName = uuid.v4();
127
+
128
+ const storage = createStorage(storageName);
119
129
  const directory = storage.createDirectory();
120
130
  const fileName = randomText();
121
131
 
@@ -139,12 +149,14 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
139
149
  if (testGroupName !== StorageType.NODE) {
140
150
  t.skip();
141
151
  }
152
+ const storageName = uuid.v4();
153
+
142
154
  const fileName = randomText();
143
155
  let firstHandle: File;
144
156
  let secondHandle: File;
145
157
 
146
158
  {
147
- const storage = createStorage();
159
+ const storage = createStorage(storageName);
148
160
  const directory = storage.createDirectory();
149
161
  const file = directory.getOrCreateFile(fileName);
150
162
  const buffer = Buffer.from(randomText());
@@ -153,7 +165,7 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
153
165
  }
154
166
 
155
167
  {
156
- const storage = createStorage();
168
+ const storage = createStorage(storageName);
157
169
  const directory = storage.createDirectory();
158
170
  const file = directory.getOrCreateFile(fileName);
159
171
  const buffer = Buffer.from(randomText());
@@ -166,8 +178,10 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
166
178
  });
167
179
 
168
180
  test('sub-directories', async () => {
181
+ const storageName = uuid.v4();
182
+
169
183
  // 1. Create storage and two subdirectories
170
- const storage = createStorage();
184
+ const storage = createStorage(storageName);
171
185
  const dir1 = storage.createDirectory('dir1');
172
186
  const dir2 = storage.createDirectory('dir2');
173
187
 
@@ -189,7 +203,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
189
203
  });
190
204
 
191
205
  test('write in directory/sub-directory/file', async () => {
192
- const storage = createStorage();
206
+ const storageName = uuid.v4();
207
+
208
+ const storage = createStorage(storageName);
193
209
  const dir = storage.createDirectory('directory');
194
210
  const subDir = dir.createDirectory('subDirectory');
195
211
 
@@ -206,10 +222,11 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
206
222
  if (testGroupName === StorageType.RAM) {
207
223
  t.skip();
208
224
  }
225
+ const storageName = uuid.v4();
209
226
 
210
227
  const fileName = randomText();
211
228
  {
212
- const storage = createStorage();
229
+ const storage = createStorage(storageName);
213
230
  const directory = storage.createDirectory();
214
231
  const file = directory.getOrCreateFile(fileName);
215
232
  await file.write(0, Buffer.alloc(10, '0'));
@@ -220,7 +237,7 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
220
237
  }
221
238
 
222
239
  {
223
- const storage = createStorage();
240
+ const storage = createStorage(storageName);
224
241
  const directory = storage.createDirectory();
225
242
  const file = directory.getOrCreateFile(fileName);
226
243
  const allContent = await file.read(0, (await file.stat()).size);
@@ -232,10 +249,11 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
232
249
  if (testGroupName === StorageType.RAM) {
233
250
  t.skip();
234
251
  }
252
+ const storageName = uuid.v4();
235
253
 
236
254
  const fileName = randomText();
237
255
  {
238
- const storage = createStorage();
256
+ const storage = createStorage(storageName);
239
257
  const directory = storage.createDirectory();
240
258
  const file = directory.getOrCreateFile(fileName);
241
259
  await file.write(0, Buffer.alloc(3, '0'));
@@ -246,7 +264,7 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
246
264
  }
247
265
 
248
266
  {
249
- const storage = createStorage();
267
+ const storage = createStorage(storageName);
250
268
  const directory = storage.createDirectory();
251
269
  const file = directory.getOrCreateFile(fileName);
252
270
  const allContent = await file.read(0, (await file.stat()).size);
@@ -255,7 +273,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
255
273
  });
256
274
 
257
275
  test('operations on a destroyed file are rejected', async () => {
258
- const storage = createStorage();
276
+ const storageName = uuid.v4();
277
+
278
+ const storage = createStorage(storageName);
259
279
  const directory = storage.createDirectory();
260
280
  const file = directory.getOrCreateFile(randomText());
261
281
 
@@ -275,7 +295,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
275
295
  });
276
296
 
277
297
  test('delete directory', async () => {
278
- const storage = createStorage();
298
+ const storageName = uuid.v4();
299
+
300
+ const storage = createStorage(storageName);
279
301
  const directory = storage.createDirectory();
280
302
  const file = directory.getOrCreateFile(randomText());
281
303
 
@@ -291,7 +313,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
291
313
  if (testGroupName !== StorageType.NODE) {
292
314
  t.skip();
293
315
  }
294
- const storage = createStorage();
316
+ const storageName = uuid.v4();
317
+
318
+ const storage = createStorage(storageName);
295
319
 
296
320
  const directory = storage.createDirectory();
297
321
  const file = directory.getOrCreateFile(randomText());
@@ -310,14 +334,18 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
310
334
  });
311
335
 
312
336
  test('stat of new file', async () => {
313
- const storage = createStorage();
337
+ const storageName = uuid.v4();
338
+
339
+ const storage = createStorage(storageName);
314
340
  const directory = storage.createDirectory();
315
341
  const file = directory.getOrCreateFile(randomText());
316
342
  expect((await file.stat()).size).toBe(0);
317
343
  });
318
344
 
319
345
  test('call del with edge arguments', async () => {
320
- const storage = createStorage();
346
+ const storageName = uuid.v4();
347
+
348
+ const storage = createStorage(storageName);
321
349
  if (storage.type === StorageType.IDB) {
322
350
  // Not deletable.
323
351
  return;
@@ -347,42 +375,44 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
347
375
  }
348
376
  });
349
377
 
350
- test('reset', async () => {
351
- if (
352
- testGroupName === StorageType.RAM || // RAM storage does not persist data.
353
- testGroupName === StorageType.IDB // IDB storage is blocked by opened connection, and there is no handle to close it.
354
- ) {
355
- return;
378
+ test('reset', async (t) => {
379
+ if (testGroupName === StorageType.RAM) {
380
+ t.skip();
356
381
  }
382
+ const storageName = uuid.v4();
357
383
  const filename = randomText();
358
384
  const buffer = Buffer.from(randomText());
359
385
 
360
386
  {
361
- const storage = createStorage();
387
+ const storage = createStorage(storageName);
362
388
  const directory = storage.createDirectory();
363
389
  const file = directory.getOrCreateFile(filename);
364
390
  await file.write(0, buffer);
365
391
  await file.close();
392
+ await storage.close();
366
393
  }
367
394
 
368
395
  {
369
- const storage = createStorage();
396
+ const storage = createStorage(storageName);
370
397
  const directory = storage.createDirectory();
371
398
  const file = directory.getOrCreateFile(filename);
372
399
  await expect(file.read(0, buffer.length)).resolves.toEqual(buffer);
373
400
  await file.close();
401
+ await storage.close();
374
402
  }
375
403
 
376
404
  {
377
- const storage = createStorage();
405
+ const storage = createStorage(storageName);
378
406
  await asyncTimeout(storage.reset(), 1_000);
379
407
  }
380
408
 
381
409
  {
382
- const storage = createStorage();
410
+ const storage = createStorage(storageName);
383
411
  const directory = storage.createDirectory();
384
412
  const file = directory.getOrCreateFile(filename);
385
- await expect(file.read(0, buffer.length)).rejects.toThrow();
413
+ const { size } = await file.stat();
414
+ await expect(size).to.eq(0);
415
+ await storage.close();
386
416
  }
387
417
  });
388
418
 
@@ -390,10 +420,10 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
390
420
  if (testGroupName === StorageType.RAM) {
391
421
  t.skip();
392
422
  }
393
-
423
+ const storageName = uuid.v4();
394
424
  const dirname = randomText();
395
425
 
396
- const storage = createStorage();
426
+ const storage = createStorage(storageName);
397
427
  const directory = storage.createDirectory(dirname);
398
428
 
399
429
  {
@@ -413,7 +443,7 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
413
443
  }
414
444
 
415
445
  {
416
- const storage = createStorage();
446
+ const storage = createStorage(storageName);
417
447
  const directory = storage.createDirectory(dirname);
418
448
  const entries = await directory.list();
419
449
  expect(entries.length).toBe(files.length);
@@ -421,10 +451,11 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
421
451
  });
422
452
 
423
453
  test('list returns correct filenames', async () => {
454
+ const storageName = uuid.v4();
424
455
  const FILES = ['one', 'two', 'three'];
425
456
 
426
457
  // Create storage and check.
427
- const storage = createStorage();
458
+ const storage = createStorage(storageName);
428
459
  const directory = storage.createDirectory('dir');
429
460
 
430
461
  for (const file of FILES) {
@@ -436,7 +467,9 @@ export const storageTests = (testGroupName: StorageType, createStorage: () => St
436
467
  });
437
468
 
438
469
  test('getDiskInfo returns correct size', async (t) => {
439
- const storage = createStorage();
470
+ const storageName = uuid.v4();
471
+
472
+ const storage = createStorage(storageName);
440
473
  if (storage.type === StorageType.IDB) {
441
474
  t.skip();
442
475
  }
@@ -1,13 +0,0 @@
1
- import type { RandomAccessStorage } from 'random-access-storage';
2
- import { AbstractStorage } from '../common';
3
- /**
4
- * Base class for random access files based on IDB.
5
- */
6
- export declare abstract class BrowserStorage extends AbstractStorage {
7
- protected readonly _fileStorage: (filename: string, opts?: {}) => RandomAccessStorage;
8
- constructor(path: string);
9
- protected _createFile(path: string, filename: string): RandomAccessStorage;
10
- protected abstract _createFileStorage(path: string): (filename: string, opts?: {}) => RandomAccessStorage;
11
- protected _destroy(): Promise<void>;
12
- }
13
- //# sourceMappingURL=browser-storage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../../../src/browser/browser-storage.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAEjE,OAAO,EAAE,eAAe,EAAe,MAAM,WAAW,CAAC;AAEzD;;GAEG;AACH,8BAAsB,cAAe,SAAQ,eAAe;IAC1D,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,mBAAmB,CAAC;gBAE1E,IAAI,EAAE,MAAM;IAKxB,SAAS,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,mBAAmB;IAK1E,SAAS,CAAC,QAAQ,CAAC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,mBAAmB;cAEhF,QAAQ;CAkBlC"}
@@ -1,11 +0,0 @@
1
- import { BrowserStorage } from './browser-storage';
2
- import { StorageType } from '../common';
3
- /**
4
- * Storage interface implementation for Firefox browser.
5
- * https://github.com/random-access-storage/random-access-web
6
- */
7
- export declare class FirefoxStorage extends BrowserStorage {
8
- type: StorageType;
9
- protected _createFileStorage(path: string): any;
10
- }
11
- //# sourceMappingURL=firefox-storage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"firefox-storage.d.ts","sourceRoot":"","sources":["../../../../src/browser/firefox-storage.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC;;;GAGG;AACH,qBAAa,cAAe,SAAQ,cAAc;IAChC,IAAI,EAAE,WAAW,CAAuB;cAErC,kBAAkB,CAAC,IAAI,EAAE,MAAM;CAGnD"}
@@ -1,45 +0,0 @@
1
- //
2
- // Copyright 2021 DXOS.org
3
- //
4
-
5
- import type { RandomAccessStorage } from 'random-access-storage';
6
-
7
- import { AbstractStorage, getFullPath } from '../common';
8
-
9
- /**
10
- * Base class for random access files based on IDB.
11
- */
12
- export abstract class BrowserStorage extends AbstractStorage {
13
- protected readonly _fileStorage: (filename: string, opts?: {}) => RandomAccessStorage;
14
-
15
- constructor(path: string) {
16
- super(path);
17
- this._fileStorage = this._createFileStorage(path);
18
- }
19
-
20
- protected _createFile(path: string, filename: string): RandomAccessStorage {
21
- const fullPath = getFullPath(path, filename);
22
- return this._fileStorage(fullPath);
23
- }
24
-
25
- protected abstract _createFileStorage(path: string): (filename: string, opts?: {}) => RandomAccessStorage;
26
-
27
- protected override async _destroy() {
28
- // eslint-disable-next-line no-undef
29
- return new Promise<void>((resolve, reject) => {
30
- const request = indexedDB.deleteDatabase(this.path);
31
- request.onsuccess = () => {
32
- resolve();
33
- };
34
- request.onupgradeneeded = () => {
35
- reject(new Error('Upgrade needed.'));
36
- };
37
- request.onblocked = () => {
38
- reject(new Error('Blocked.'));
39
- };
40
- request.onerror = (err: any) => {
41
- reject(err);
42
- };
43
- });
44
- }
45
- }
@@ -1,23 +0,0 @@
1
- //
2
- // Copyright 2021 DXOS.org
3
- //
4
-
5
- // NOTE: Removing the .js extension here breaks usage in create-react-app.
6
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
7
- // @ts-ignore
8
- import raw from 'random-access-web/mutable-file-wrapper.js';
9
-
10
- import { BrowserStorage } from './browser-storage';
11
- import { StorageType } from '../common';
12
-
13
- /**
14
- * Storage interface implementation for Firefox browser.
15
- * https://github.com/random-access-storage/random-access-web
16
- */
17
- export class FirefoxStorage extends BrowserStorage {
18
- public override type: StorageType = StorageType.FIREFOX;
19
-
20
- protected override _createFileStorage(path: string) {
21
- return raw({ name: path });
22
- }
23
- }