@dxos/random-access-storage 0.8.4-main.406dc2a → 0.8.4-main.59c2e9b

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.
@@ -0,0 +1,105 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ AbstractStorage,
4
+ Directory,
5
+ MemoryStorage,
6
+ StorageType,
7
+ getFullPath,
8
+ stringDiff,
9
+ wrapFile
10
+ } from "../chunk-OW63I6TJ.mjs";
11
+
12
+ // src/node/node-storage.ts
13
+ import { existsSync } from "node:fs";
14
+ import { readdir, stat } from "node:fs/promises";
15
+ import { join } from "node:path";
16
+ import del from "del";
17
+ import raf from "random-access-file";
18
+ var NodeStorage = class extends AbstractStorage {
19
+ type = StorageType.NODE;
20
+ _initialized = false;
21
+ async _loadFiles(path) {
22
+ if (!existsSync(path)) {
23
+ return;
24
+ }
25
+ const dir = await readdir(path);
26
+ for (const entry of dir) {
27
+ const fullPath = join(path, entry);
28
+ if (this._files.has(fullPath)) {
29
+ continue;
30
+ }
31
+ const entryInfo = await stat(fullPath);
32
+ if (entryInfo.isDirectory()) {
33
+ await this._loadFiles(fullPath);
34
+ } else if (entryInfo.isFile()) {
35
+ const file = this._createFile(path, entry);
36
+ this._files.set(fullPath, wrapFile(file, this.type));
37
+ }
38
+ }
39
+ }
40
+ _createFile(path, filename, opts = {}) {
41
+ const file = raf(filename, {
42
+ directory: path,
43
+ ...opts
44
+ });
45
+ file.write(0, Buffer.from(""));
46
+ return file;
47
+ }
48
+ async _destroy() {
49
+ await del(this.path, {
50
+ force: true
51
+ });
52
+ }
53
+ async _getFiles(path) {
54
+ if (!this._initialized) {
55
+ await this._loadFiles(this.path);
56
+ this._initialized = true;
57
+ }
58
+ return super._getFiles(path);
59
+ }
60
+ async getDiskInfo() {
61
+ let used = 0;
62
+ const recurse = async (path) => {
63
+ const pathStats = await stat(path);
64
+ if (pathStats.isDirectory()) {
65
+ const entries = await readdir(path);
66
+ await Promise.all(entries.map((entry) => recurse(join(path, entry))));
67
+ } else {
68
+ used += pathStats.size;
69
+ }
70
+ };
71
+ await recurse(this.path);
72
+ return {
73
+ used
74
+ };
75
+ }
76
+ };
77
+
78
+ // src/node/storage.ts
79
+ var createStorage = ({ type, root = "/tmp/dxos/testing" } = {}) => {
80
+ if (type === void 0) {
81
+ return new NodeStorage(root);
82
+ }
83
+ switch (type) {
84
+ case StorageType.RAM: {
85
+ return new MemoryStorage(root);
86
+ }
87
+ case StorageType.NODE: {
88
+ return new NodeStorage(root);
89
+ }
90
+ default: {
91
+ throw new Error(`Invalid type: ${type}`);
92
+ }
93
+ }
94
+ };
95
+ export {
96
+ AbstractStorage,
97
+ Directory,
98
+ MemoryStorage,
99
+ StorageType,
100
+ createStorage,
101
+ getFullPath,
102
+ stringDiff,
103
+ wrapFile
104
+ };
105
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/node/node-storage.ts", "../../../../src/node/storage.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2021 DXOS.org\n//\n\nimport { existsSync } from 'node:fs';\nimport { readdir, stat } from 'node:fs/promises';\nimport { join } from 'node:path';\n\nimport del from 'del';\nimport raf from 'random-access-file';\nimport { type RandomAccessStorage } from 'random-access-storage';\n\nimport { AbstractStorage, type DiskInfo, type Storage, StorageType, wrapFile } from '../common';\n\n/**\n * Storage interface implementation for Node.\n */\nexport class NodeStorage extends AbstractStorage implements Storage {\n public override type: StorageType = StorageType.NODE;\n private _initialized = false;\n\n private async _loadFiles(path: string): Promise<void> {\n // TODO(mykola): Do not load all files at once. It is a quick fix.\n if (!existsSync(path)) {\n return;\n }\n\n // Preload all files in a directory.\n const dir = await readdir(path);\n for (const entry of dir) {\n const fullPath = join(path, entry);\n if (this._files.has(fullPath)) {\n continue;\n }\n const entryInfo = await stat(fullPath);\n if (entryInfo.isDirectory()) {\n await this._loadFiles(fullPath);\n } else if (entryInfo.isFile()) {\n const file = this._createFile(path, entry);\n this._files.set(fullPath, wrapFile(file, this.type));\n }\n }\n }\n\n protected override _createFile(path: string, filename: string, opts: any = {}): RandomAccessStorage {\n const file = raf(filename, { directory: path, ...opts });\n\n // Empty write to create file on a drive.\n file.write(0, Buffer.from(''));\n\n return file;\n }\n\n protected override async _destroy(): Promise<void> {\n await del(this.path, { force: true });\n }\n\n protected override async _getFiles(path: string) {\n if (!this._initialized) {\n await this._loadFiles(this.path);\n this._initialized = true;\n }\n\n return super._getFiles(path);\n }\n\n async getDiskInfo(): Promise<DiskInfo> {\n let used = 0;\n\n const recurse = async (path: string) => {\n const pathStats = await stat(path);\n\n if (pathStats.isDirectory()) {\n const entries = await readdir(path);\n await Promise.all(entries.map((entry) => recurse(join(path, entry))));\n } else {\n used += pathStats.size;\n }\n };\n\n await recurse(this.path);\n\n return {\n used,\n };\n }\n}\n", "//\n// Copyright 2021 DXOS.org\n//\n\nimport { MemoryStorage, type Storage, type StorageConstructor, StorageType } from '../common';\n\nimport { NodeStorage } from './node-storage';\n\nexport const createStorage: StorageConstructor = ({ type, root = '/tmp/dxos/testing' } = {}): Storage => {\n if (type === undefined) {\n return new NodeStorage(root);\n }\n\n switch (type) {\n case StorageType.RAM: {\n return new MemoryStorage(root);\n }\n\n case StorageType.NODE: {\n return new NodeStorage(root);\n }\n\n default: {\n throw new Error(`Invalid type: ${type}`);\n }\n }\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;AAIA,SAASA,kBAAkB;AAC3B,SAASC,SAASC,YAAY;AAC9B,SAASC,YAAY;AAErB,OAAOC,SAAS;AAChB,OAAOC,SAAS;AAQT,IAAMC,cAAN,cAA0BC,gBAAAA;EACfC,OAAoBC,YAAYC;EACxCC,eAAe;EAEvB,MAAcC,WAAWC,MAA6B;AAEpD,QAAI,CAACC,WAAWD,IAAAA,GAAO;AACrB;IACF;AAGA,UAAME,MAAM,MAAMC,QAAQH,IAAAA;AAC1B,eAAWI,SAASF,K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AAEvB,WAAO;MACL6B;IACF;EACF;AACF;;;AC9EO,IAAMQ,gBAAoC,CAAC,EAAEC,MAAMC,OAAO,oBAAmB,IAAK,CAAC,MAAC;AACzF,MAAID,SAASE,QAAW;AACtB,WAAO,IAAIC,YAAYF,IAAAA;EACzB;AAEA,UAAQD,MAAAA;IACN,KAAKI,YAAYC,KAAK;AACpB,aAAO,IAAIC,cAAcL,IAAAA;IAC3B;IAEA,KAAKG,YAAYG,MAAM;AACrB,aAAO,IAAIJ,YAAYF,IAAAA;IACzB;IAEA,SAAS;AACP,YAAM,IAAIO,MAAM,iBAAiBR,IAAAA,EAAM;IACzC;EACF;AACF;",
6
+ "names": ["existsSync", "readdir", "stat", "join", "del", "raf", "NodeStorage", "AbstractStorage", "type", "StorageType", "NODE", "_initialized", "_loadFiles", "path", "existsSync", "dir", "readdir", "entry", "fullPath", "join", "_files", "has", "entryInfo", "stat", "isDirectory", "isFile", "file", "_createFile", "set", "wrapFile", "filename", "opts", "raf", "directory", "write", "Buffer", "from", "_destroy", "del", "force", "_getFiles", "getDiskInfo", "used", "recurse", "pathStats", "entries", "Promise", "all", "map", "size", "createStorage", "type", "root", "undefined", "NodeStorage", "StorageType", "RAM", "MemoryStorage", "NODE", "Error"]
7
+ }
@@ -1,6 +1,6 @@
1
1
  import { type File } from './file';
2
2
  import { type StorageType } from './storage';
3
- export type DirectoryParams = {
3
+ export type DirectoryProps = {
4
4
  type: StorageType;
5
5
  path: string;
6
6
  list: (path: string) => Promise<string[]>;
@@ -18,7 +18,7 @@ export declare class Directory {
18
18
  private readonly _getOrCreateFile;
19
19
  private readonly _remove;
20
20
  private readonly _onFlush?;
21
- constructor({ type, path, list, getOrCreateFile, remove, onFlush }: DirectoryParams);
21
+ constructor({ type, path, list, getOrCreateFile, remove, onFlush }: DirectoryProps);
22
22
  toString(): string;
23
23
  /**
24
24
  * Create a new sub-directory.
@@ -1 +1 @@
1
- {"version":3,"file":"directory.d.ts","sourceRoot":"","sources":["../../../../src/common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ACH,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,IAAI;IAI7C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAG9B"}
@@ -1,2 +1,2 @@
1
- export * from './node';
1
+ export * from '#storage';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,UAAU,CAAC"}