@zthun/crumbtrail-fs 2.4.6 → 2.4.9
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/dist/file-system/file-system-node.d.mts +2 -2
- package/dist/index.cjs +359 -481
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +358 -477
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
package/dist/index.cjs
CHANGED
|
@@ -1,496 +1,373 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const path = require('path');
|
|
17
|
-
const promises$1 = require('fs/promises');
|
|
18
|
-
|
|
19
|
-
function _define_property$3(obj, key, value) {
|
|
20
|
-
if (key in obj) {
|
|
21
|
-
Object.defineProperty(obj, key, {
|
|
22
|
-
value: value,
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
writable: true
|
|
26
|
-
});
|
|
27
|
-
} else {
|
|
28
|
-
obj[key] = value;
|
|
29
|
-
}
|
|
30
|
-
return obj;
|
|
31
|
-
}
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
let _zthun_helpful_fn = require("@zthun/helpful-fn");
|
|
4
|
+
let _zthun_helpful_query = require("@zthun/helpful-query");
|
|
5
|
+
let lodash_es = require("lodash-es");
|
|
6
|
+
let minimatch = require("minimatch");
|
|
7
|
+
let node_fs_promises = require("node:fs/promises");
|
|
8
|
+
let node_process = require("node:process");
|
|
9
|
+
let glob = require("glob");
|
|
10
|
+
let chokidar = require("chokidar");
|
|
11
|
+
let rxjs = require("rxjs");
|
|
12
|
+
let node_url = require("node:url");
|
|
13
|
+
let path = require("path");
|
|
14
|
+
let fs_promises = require("fs/promises");
|
|
15
|
+
//#region src/file-system/file-system-node.mts
|
|
32
16
|
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
17
|
+
* Represents a type of a file system node.
|
|
18
|
+
*/ var ZFileSystemNodeType = /* @__PURE__ */ function(ZFileSystemNodeType) {
|
|
19
|
+
ZFileSystemNodeType["File"] = "file";
|
|
20
|
+
ZFileSystemNodeType["Folder"] = "folder";
|
|
21
|
+
return ZFileSystemNodeType;
|
|
38
22
|
}({});
|
|
39
23
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
_define_property$3(this, "folder", this.type.bind(this, "folder"));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
24
|
+
* Represents a builder for a file system node.
|
|
25
|
+
*/ var ZFileSystemNodeBuilder = class {
|
|
26
|
+
_node = {
|
|
27
|
+
path: "/",
|
|
28
|
+
type: "folder",
|
|
29
|
+
extension: "",
|
|
30
|
+
name: "",
|
|
31
|
+
parent: "",
|
|
32
|
+
title: ""
|
|
33
|
+
};
|
|
34
|
+
created(at) {
|
|
35
|
+
this._node.created = at instanceof Date ? at.toJSON() : at;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
updated(at) {
|
|
39
|
+
this._node.updated = at instanceof Date ? at.toJSON() : at;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
size(bytes) {
|
|
43
|
+
this._node.size = typeof bytes == "number" ? BigInt(bytes) : bytes;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
path(path) {
|
|
47
|
+
this._node.path = path;
|
|
48
|
+
return this.decorate();
|
|
49
|
+
}
|
|
50
|
+
type(type) {
|
|
51
|
+
this._node.type = type;
|
|
52
|
+
return this.decorate();
|
|
53
|
+
}
|
|
54
|
+
file = this.type.bind(this, "file");
|
|
55
|
+
folder = this.type.bind(this, "folder");
|
|
56
|
+
stats(s) {
|
|
57
|
+
const builder = this.size(s.size).created(s.birthtime).updated(s.mtime);
|
|
58
|
+
return s.isFile() ? builder.file() : builder.folder();
|
|
59
|
+
}
|
|
60
|
+
build() {
|
|
61
|
+
return (0, lodash_es.omitBy)(structuredClone(this._node), lodash_es.isUndefined);
|
|
62
|
+
}
|
|
63
|
+
decorate() {
|
|
64
|
+
const { path, type } = this._node;
|
|
65
|
+
this._node.name = (0, node_path.basename)(path);
|
|
66
|
+
this._node.extension = "";
|
|
67
|
+
this._node.title = this._node.name;
|
|
68
|
+
this._node.parent = (0, node_path.dirname)(path);
|
|
69
|
+
if (type === "file") {
|
|
70
|
+
const extension = (0, node_path.extname)(path);
|
|
71
|
+
this._node.extension = extension;
|
|
72
|
+
this._node.title = (0, node_path.basename)(path, extension);
|
|
73
|
+
}
|
|
74
|
+
if (!path || path === "/" || this._node.parent === ".") this._node.parent = "";
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/file-system/file-system-service.mts
|
|
100
80
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
dir = dir === stop ? "" : node_path.dirname(dir);
|
|
144
|
-
}while (dir.length)
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function _define_property$2(obj, key, value) {
|
|
150
|
-
if (key in obj) {
|
|
151
|
-
Object.defineProperty(obj, key, {
|
|
152
|
-
value: value,
|
|
153
|
-
enumerable: true,
|
|
154
|
-
configurable: true,
|
|
155
|
-
writable: true
|
|
156
|
-
});
|
|
157
|
-
} else {
|
|
158
|
-
obj[key] = value;
|
|
159
|
-
}
|
|
160
|
-
return obj;
|
|
161
|
-
}
|
|
81
|
+
* Represents a node implementation of a file system service.
|
|
82
|
+
*/ var ZFileSystemService = class {
|
|
83
|
+
async info(path) {
|
|
84
|
+
const [file] = await this.search(path);
|
|
85
|
+
return file;
|
|
86
|
+
}
|
|
87
|
+
async search(pattern, options) {
|
|
88
|
+
return (await (0, glob.glob)(pattern, {
|
|
89
|
+
stat: true,
|
|
90
|
+
...options,
|
|
91
|
+
withFileTypes: true
|
|
92
|
+
})).map((path) => {
|
|
93
|
+
let info = new ZFileSystemNodeBuilder().path(path.fullpath()).created(path.birthtime).updated(path.atime).size(path.size);
|
|
94
|
+
info = path.isFile() ? info.file() : info.folder().size(void 0);
|
|
95
|
+
return info.build();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async walk(search, options) {
|
|
99
|
+
const start = options?.start || (0, node_process.cwd)();
|
|
100
|
+
const stop = options?.stop || "/";
|
|
101
|
+
const mode = options?.mode;
|
|
102
|
+
const _test = async (path) => {
|
|
103
|
+
try {
|
|
104
|
+
await (0, node_fs_promises.access)(path, mode);
|
|
105
|
+
return true;
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
if ((0, node_path.isAbsolute)(search)) return await _test(search) ? search : null;
|
|
111
|
+
let dir = start;
|
|
112
|
+
do {
|
|
113
|
+
const path = (0, node_path.resolve)(dir, search);
|
|
114
|
+
if (await _test(path)) return path;
|
|
115
|
+
dir = dir === stop ? "" : (0, node_path.dirname)(dir);
|
|
116
|
+
} while (dir.length);
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/file-watch/file-watch.mts
|
|
162
122
|
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function _define_property$1(obj, key, value) {
|
|
215
|
-
if (key in obj) {
|
|
216
|
-
Object.defineProperty(obj, key, {
|
|
217
|
-
value: value,
|
|
218
|
-
enumerable: true,
|
|
219
|
-
configurable: true,
|
|
220
|
-
writable: true
|
|
221
|
-
});
|
|
222
|
-
} else {
|
|
223
|
-
obj[key] = value;
|
|
224
|
-
}
|
|
225
|
-
return obj;
|
|
226
|
-
}
|
|
123
|
+
* An implementation of the IZFileWatch.
|
|
124
|
+
*/ var ZFileWatch = class {
|
|
125
|
+
path;
|
|
126
|
+
_add = new rxjs.Subject();
|
|
127
|
+
_update = new rxjs.Subject();
|
|
128
|
+
_remove = new rxjs.Subject();
|
|
129
|
+
_watcher;
|
|
130
|
+
/**
|
|
131
|
+
* Initializes a new instance of this object.
|
|
132
|
+
*
|
|
133
|
+
* @param path -
|
|
134
|
+
* The path to watch.
|
|
135
|
+
*/ constructor(path) {
|
|
136
|
+
this.path = path;
|
|
137
|
+
}
|
|
138
|
+
async start() {
|
|
139
|
+
await this.stop();
|
|
140
|
+
this._watcher = (0, chokidar.watch)(this.path, {
|
|
141
|
+
usePolling: true,
|
|
142
|
+
alwaysStat: true,
|
|
143
|
+
ignoreInitial: true,
|
|
144
|
+
awaitWriteFinish: {
|
|
145
|
+
stabilityThreshold: 500,
|
|
146
|
+
pollInterval: 50
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
function next(observable, file, stats) {
|
|
150
|
+
const node = new ZFileSystemNodeBuilder().path((0, node_path.resolve)(this.path, file));
|
|
151
|
+
observable.next(stats ? node.stats(stats).build() : node.build());
|
|
152
|
+
}
|
|
153
|
+
this._watcher.on("add", next.bind(this, this._add)).on("addDir", next.bind(this, this._add)).on("change", next.bind(this, this._update)).on("unlink", next.bind(this, this._remove)).on("unlinkDir", next.bind(this, this._remove));
|
|
154
|
+
return new Promise((resolve) => this._watcher?.on("ready", () => resolve()));
|
|
155
|
+
}
|
|
156
|
+
async stop() {
|
|
157
|
+
await this._watcher?.close();
|
|
158
|
+
delete this._watcher;
|
|
159
|
+
}
|
|
160
|
+
add() {
|
|
161
|
+
return this._add.asObservable();
|
|
162
|
+
}
|
|
163
|
+
update() {
|
|
164
|
+
return this._update.asObservable();
|
|
165
|
+
}
|
|
166
|
+
remove() {
|
|
167
|
+
return this._remove.asObservable();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/file-repository/file-repository.mts
|
|
227
172
|
/**
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
this._globs = [
|
|
335
|
-
"**"
|
|
336
|
-
];
|
|
337
|
-
this._nodes = Promise.resolve(new helpfulQuery.ZDataSourceStatic([]));
|
|
338
|
-
}
|
|
339
|
-
async get(path) {
|
|
340
|
-
const nodes = await this._nodes;
|
|
341
|
-
const items = await nodes.items();
|
|
342
|
-
const node = lodashEs.find(items, (f)=>f.path === path);
|
|
343
|
-
return helpfulFn.firstDefined(null, node);
|
|
344
|
-
}
|
|
345
|
-
async count(request) {
|
|
346
|
-
const source = await this._nodes;
|
|
347
|
-
return source.count(request);
|
|
348
|
-
}
|
|
349
|
-
async retrieve(request) {
|
|
350
|
-
const source = await this._nodes;
|
|
351
|
-
return source.retrieve(request);
|
|
352
|
-
}
|
|
353
|
-
constructor(){
|
|
354
|
-
_define_property$1(this, "_service", new ZFileSystemService());
|
|
355
|
-
_define_property$1(this, "_watcher", void 0);
|
|
356
|
-
_define_property$1(this, "_nodes", Promise.resolve(new helpfulQuery.ZDataSourceStatic([])));
|
|
357
|
-
_define_property$1(this, "_globs", [
|
|
358
|
-
"**"
|
|
359
|
-
]);
|
|
360
|
-
_define_property$1(this, "_path", "");
|
|
361
|
-
_define_property$1(this, "_add", void 0);
|
|
362
|
-
_define_property$1(this, "_remove", void 0);
|
|
363
|
-
_define_property$1(this, "_update", void 0);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
173
|
+
* Represents an implementation of a ZFileRepository that watches
|
|
174
|
+
* a given path scope.
|
|
175
|
+
*
|
|
176
|
+
* This repository is only concerned about files. Folders
|
|
177
|
+
* will not be retrieved from this repository.
|
|
178
|
+
*/ var ZFileRepository = class {
|
|
179
|
+
_service = new ZFileSystemService();
|
|
180
|
+
_watcher;
|
|
181
|
+
_nodes = Promise.resolve(new _zthun_helpful_query.ZDataSourceStatic([]));
|
|
182
|
+
_globs = ["**"];
|
|
183
|
+
_path = "";
|
|
184
|
+
_add;
|
|
185
|
+
_remove;
|
|
186
|
+
_update;
|
|
187
|
+
/**
|
|
188
|
+
* Gets the path that this repository is watching.
|
|
189
|
+
*/ get path() {
|
|
190
|
+
return this._path;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Gets the globs matchers that this repository is matching against.
|
|
194
|
+
*/ get globs() {
|
|
195
|
+
return this._globs.slice();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Initializes the repository with the given root and glob filter.
|
|
199
|
+
*
|
|
200
|
+
* This will initially scan the file system and setup watches.
|
|
201
|
+
* If you call this method again, then the repository will be
|
|
202
|
+
* {@link reset} and a new set of files will be added to the
|
|
203
|
+
* repository.
|
|
204
|
+
*
|
|
205
|
+
* @param path -
|
|
206
|
+
* The root path of the repository.
|
|
207
|
+
* @param globs -
|
|
208
|
+
* The optional relative globs of the repository. If this is falsy,
|
|
209
|
+
* then ** is used. You can achieve the same kind of operation
|
|
210
|
+
* using filters, but filtering out unwanted files frees up the internal
|
|
211
|
+
* memory being used.
|
|
212
|
+
*/ async initialize(path, globs = ["**"]) {
|
|
213
|
+
await this.reset();
|
|
214
|
+
this._path = path;
|
|
215
|
+
this._globs = globs;
|
|
216
|
+
const matches = globs.map((g) => this._service.search(g, {
|
|
217
|
+
cwd: path,
|
|
218
|
+
stat: true
|
|
219
|
+
}));
|
|
220
|
+
this._nodes = Promise.all(matches).then((nodes) => {
|
|
221
|
+
return (0, lodash_es.uniqBy)((0, lodash_es.flatten)(nodes), (n) => n.path).filter((n) => n.type === ZFileSystemNodeType.File);
|
|
222
|
+
}).then((files) => {
|
|
223
|
+
return new _zthun_helpful_query.ZDataSourceStatic(files);
|
|
224
|
+
});
|
|
225
|
+
this._watcher = new ZFileWatch(path);
|
|
226
|
+
this._add = this._watcher.add().subscribe((node) => {
|
|
227
|
+
(async () => {
|
|
228
|
+
if (node.type === ZFileSystemNodeType.File && this._globs.some((g) => (0, minimatch.minimatch)(node.path, (0, node_path.resolve)(this._path, g)))) this._nodes = (await this._nodes).insert(node);
|
|
229
|
+
})();
|
|
230
|
+
});
|
|
231
|
+
this._remove = this._watcher.remove().subscribe((node) => {
|
|
232
|
+
(async () => {
|
|
233
|
+
let nodes = await this._nodes;
|
|
234
|
+
const path = (0, lodash_es.trimEnd)(node.path, node_path.sep);
|
|
235
|
+
const next = async () => (0, lodash_es.findIndex)(await nodes.items(), (n) => {
|
|
236
|
+
return n.path === path || n.path.startsWith(path) && n.path.substring(path.length).charAt(0) === node_path.sep;
|
|
237
|
+
});
|
|
238
|
+
for (let index = await next(); index >= 0; index = await next()) nodes = await nodes.removeAt(index);
|
|
239
|
+
this._nodes = Promise.resolve(nodes);
|
|
240
|
+
})();
|
|
241
|
+
});
|
|
242
|
+
this._update = this._watcher.update().subscribe((node) => {
|
|
243
|
+
(async () => {
|
|
244
|
+
const nodes = await this._nodes;
|
|
245
|
+
const index = (0, lodash_es.findIndex)(await nodes.items(), (n) => n.path === node.path);
|
|
246
|
+
if (index >= 0) this._nodes = (await nodes.removeAt(index)).insert(node, index);
|
|
247
|
+
})();
|
|
248
|
+
});
|
|
249
|
+
await (0, _zthun_helpful_fn.sleep)(100);
|
|
250
|
+
await this._watcher?.start();
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Resets the repository as if it was initially created.
|
|
254
|
+
*/ async reset() {
|
|
255
|
+
await this._watcher?.stop();
|
|
256
|
+
this._add?.unsubscribe();
|
|
257
|
+
this._remove?.unsubscribe();
|
|
258
|
+
this._update?.unsubscribe();
|
|
259
|
+
delete this._add;
|
|
260
|
+
delete this._remove;
|
|
261
|
+
delete this._update;
|
|
262
|
+
delete this._watcher;
|
|
263
|
+
this._path = "";
|
|
264
|
+
this._globs = ["**"];
|
|
265
|
+
this._nodes = Promise.resolve(new _zthun_helpful_query.ZDataSourceStatic([]));
|
|
266
|
+
}
|
|
267
|
+
async get(path) {
|
|
268
|
+
return (0, _zthun_helpful_fn.firstDefined)(null, (0, lodash_es.find)(await (await this._nodes).items(), (f) => f.path === path));
|
|
269
|
+
}
|
|
270
|
+
async count(request) {
|
|
271
|
+
return (await this._nodes).count(request);
|
|
272
|
+
}
|
|
273
|
+
async retrieve(request) {
|
|
274
|
+
return (await this._nodes).retrieve(request);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/resolve-path-like/resolve-path-like.ts
|
|
367
279
|
function resolvePathLike(path) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (path instanceof URL) {
|
|
372
|
-
return node_url.fileURLToPath(path);
|
|
373
|
-
}
|
|
374
|
-
return node_path.resolve(path.toString());
|
|
280
|
+
if (typeof path === "string") return (0, node_path.resolve)(path);
|
|
281
|
+
if (path instanceof URL) return (0, node_url.fileURLToPath)(path);
|
|
282
|
+
return (0, node_path.resolve)(path.toString());
|
|
375
283
|
}
|
|
376
|
-
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/sleep-watch-delay/sleep-watch-delay.mts
|
|
377
286
|
/**
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
287
|
+
* The actual delay time that you should wait for a watch
|
|
288
|
+
* operation to pick up your file changes.
|
|
289
|
+
*/ var ZWatchDelay = 1500;
|
|
381
290
|
/**
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
291
|
+
* Performs a sleep long enough to allow a watch operation
|
|
292
|
+
* to pick up a change.
|
|
293
|
+
*
|
|
294
|
+
* You should never really need this. Where it is useful
|
|
295
|
+
* is for unit testing with IO operations and you want to
|
|
296
|
+
* wait for your write operations to get picked up by
|
|
297
|
+
* a file watch operation.
|
|
298
|
+
*/ function sleepWatchDelay() {
|
|
299
|
+
return (0, _zthun_helpful_fn.sleep)(ZWatchDelay);
|
|
391
300
|
}
|
|
392
|
-
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region src/stream/stream-folder.mts
|
|
393
303
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
function _define_property(obj, key, value) {
|
|
409
|
-
if (key in obj) {
|
|
410
|
-
Object.defineProperty(obj, key, {
|
|
411
|
-
value: value,
|
|
412
|
-
enumerable: true,
|
|
413
|
-
configurable: true,
|
|
414
|
-
writable: true
|
|
415
|
-
});
|
|
416
|
-
} else {
|
|
417
|
-
obj[key] = value;
|
|
418
|
-
}
|
|
419
|
-
return obj;
|
|
420
|
-
}
|
|
304
|
+
* A stream that writes folders.
|
|
305
|
+
*
|
|
306
|
+
* Writing folders is idempotent. If the folder you
|
|
307
|
+
* are trying to write already exists, then the write
|
|
308
|
+
* operation still succeeds
|
|
309
|
+
*/ var ZStreamFolder = class {
|
|
310
|
+
async write(path) {
|
|
311
|
+
await (0, fs_promises.mkdir)(path, { recursive: true });
|
|
312
|
+
return 0;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/stream/stream-file.mts
|
|
421
317
|
/**
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
this._cache.delete(itr.next().value);
|
|
476
|
-
}
|
|
477
|
-
return item.buffer;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Initializes a new instance of this object.
|
|
481
|
-
*
|
|
482
|
-
* @param options -
|
|
483
|
-
* The options for file read and writes.
|
|
484
|
-
*/ constructor(options = {}){
|
|
485
|
-
_define_property(this, "options", void 0);
|
|
486
|
-
_define_property(this, "_cache", void 0);
|
|
487
|
-
this.options = options;
|
|
488
|
-
this._cache = new Map();
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
_define_property(ZStreamFile, "FileSizeDefault", 102400); // One Hundred KibiBytes
|
|
492
|
-
_define_property(ZStreamFile, "MaxFilesDefault", 1000);
|
|
493
|
-
|
|
318
|
+
* A stream that can read and write to files.
|
|
319
|
+
*
|
|
320
|
+
* This is mostly a wrapper to readFile and writeFile from node, but
|
|
321
|
+
* with some optimizations.
|
|
322
|
+
*
|
|
323
|
+
* First, this class supports file content caching so reading in a file
|
|
324
|
+
* that's under a certain size threshold will store it in memory for
|
|
325
|
+
* later retrieval.
|
|
326
|
+
*/ var ZStreamFile = class ZStreamFile {
|
|
327
|
+
options;
|
|
328
|
+
static FileSizeDefault = 102400;
|
|
329
|
+
static MaxFilesDefault = 1e3;
|
|
330
|
+
_cache = /* @__PURE__ */ new Map();
|
|
331
|
+
/**
|
|
332
|
+
* Initializes a new instance of this object.
|
|
333
|
+
*
|
|
334
|
+
* @param options -
|
|
335
|
+
* The options for file read and writes.
|
|
336
|
+
*/ constructor(options = {}) {
|
|
337
|
+
this.options = options;
|
|
338
|
+
}
|
|
339
|
+
async read(path$1, options) {
|
|
340
|
+
const _path = resolvePathLike(path$1);
|
|
341
|
+
const stats = await (0, node_fs_promises.stat)(_path);
|
|
342
|
+
const cached = this._cache.get(_path);
|
|
343
|
+
const buffer = cached && cached.stats.mtimeMs >= stats.mtimeMs ? cached.buffer : (0, node_fs_promises.readFile)(_path, options);
|
|
344
|
+
return this._addToCache(_path, {
|
|
345
|
+
stats,
|
|
346
|
+
buffer
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async write(path$2, { buffer = Buffer.from("") } = {}) {
|
|
350
|
+
const _path = resolvePathLike(path$2);
|
|
351
|
+
const directory = (0, path.dirname)(_path);
|
|
352
|
+
await new ZStreamFolder().write(directory);
|
|
353
|
+
await (0, node_fs_promises.writeFile)(_path, buffer, { flush: true });
|
|
354
|
+
this._cache.delete(_path);
|
|
355
|
+
return buffer.length;
|
|
356
|
+
}
|
|
357
|
+
_addToCache(path$3, item) {
|
|
358
|
+
const { cache = {} } = this.options;
|
|
359
|
+
const { fileSize = ZStreamFile.FileSizeDefault, maxFiles = ZStreamFile.MaxFilesDefault } = cache;
|
|
360
|
+
if (item.stats.size > fileSize) return item.buffer;
|
|
361
|
+
this._cache.delete(path$3);
|
|
362
|
+
this._cache.set(path$3, item);
|
|
363
|
+
if (this._cache.size > maxFiles) {
|
|
364
|
+
const itr = this._cache.keys();
|
|
365
|
+
this._cache.delete(itr.next().value);
|
|
366
|
+
}
|
|
367
|
+
return item.buffer;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
//#endregion
|
|
494
371
|
exports.ZFileRepository = ZFileRepository;
|
|
495
372
|
exports.ZFileSystemNodeBuilder = ZFileSystemNodeBuilder;
|
|
496
373
|
exports.ZFileSystemNodeType = ZFileSystemNodeType;
|
|
@@ -501,4 +378,5 @@ exports.ZStreamFolder = ZStreamFolder;
|
|
|
501
378
|
exports.ZWatchDelay = ZWatchDelay;
|
|
502
379
|
exports.resolvePathLike = resolvePathLike;
|
|
503
380
|
exports.sleepWatchDelay = sleepWatchDelay;
|
|
504
|
-
|
|
381
|
+
|
|
382
|
+
//# sourceMappingURL=index.cjs.map
|