appium-uiautomator2-driver 9.2.2 → 10.0.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/CHANGELOG.md +18 -0
- package/README.md +0 -2295
- package/build/lib/commands/misc.d.ts +15 -1
- package/build/lib/commands/misc.d.ts.map +1 -1
- package/build/lib/commands/misc.js +16 -0
- package/build/lib/commands/misc.js.map +1 -1
- package/build/lib/driver.d.ts.map +1 -1
- package/build/lib/driver.js +6 -1
- package/build/lib/driver.js.map +1 -1
- package/lib/commands/misc.ts +21 -1
- package/lib/driver.ts +7 -0
- package/node_modules/@appium/strongbox/LICENSE +201 -0
- package/node_modules/@appium/strongbox/README.md +110 -0
- package/node_modules/@appium/strongbox/build/lib/base-item.d.ts +52 -0
- package/node_modules/@appium/strongbox/build/lib/base-item.d.ts.map +1 -0
- package/node_modules/@appium/strongbox/build/lib/base-item.js +101 -0
- package/node_modules/@appium/strongbox/build/lib/base-item.js.map +1 -0
- package/node_modules/@appium/strongbox/build/lib/index.d.ts +228 -0
- package/node_modules/@appium/strongbox/build/lib/index.d.ts.map +1 -0
- package/node_modules/@appium/strongbox/build/lib/index.js +283 -0
- package/node_modules/@appium/strongbox/build/lib/index.js.map +1 -0
- package/node_modules/@appium/strongbox/build/lib/util.d.ts +6 -0
- package/node_modules/@appium/strongbox/build/lib/util.d.ts.map +1 -0
- package/node_modules/@appium/strongbox/build/lib/util.js +13 -0
- package/node_modules/@appium/strongbox/build/lib/util.js.map +1 -0
- package/node_modules/@appium/strongbox/lib/base-item.ts +107 -0
- package/node_modules/@appium/strongbox/lib/index.ts +430 -0
- package/node_modules/@appium/strongbox/lib/util.ts +12 -0
- package/node_modules/@appium/strongbox/package.json +69 -0
- package/node_modules/@appium/strongbox/tsconfig.json +14 -0
- package/node_modules/appium-android-driver/CHANGELOG.md +56 -0
- package/node_modules/appium-android-driver/build/lib/commands/context/helpers.d.ts.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/context/helpers.js +2 -0
- package/node_modules/appium-android-driver/build/lib/commands/context/helpers.js.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/find.js +2 -2
- package/node_modules/appium-android-driver/build/lib/commands/find.js.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/constants.d.ts +3 -0
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/constants.d.ts.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/constants.js +4 -0
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/constants.js.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/exports.d.ts +10 -4
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/exports.d.ts.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/exports.js +37 -17
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/exports.js.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/stream.d.ts +34 -2
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/stream.d.ts.map +1 -1
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/stream.js +89 -43
- package/node_modules/appium-android-driver/build/lib/commands/media-stream/stream.js.map +1 -1
- package/node_modules/appium-android-driver/build/lib/driver.d.ts +1 -0
- package/node_modules/appium-android-driver/build/lib/driver.d.ts.map +1 -1
- package/node_modules/appium-android-driver/build/lib/driver.js +1 -0
- package/node_modules/appium-android-driver/build/lib/driver.js.map +1 -1
- package/node_modules/appium-android-driver/lib/commands/context/helpers.ts +2 -0
- package/node_modules/appium-android-driver/lib/commands/find.ts +2 -2
- package/node_modules/appium-android-driver/lib/commands/media-stream/constants.ts +4 -0
- package/node_modules/appium-android-driver/lib/commands/media-stream/exports.ts +51 -17
- package/node_modules/appium-android-driver/lib/commands/media-stream/stream.ts +90 -43
- package/node_modules/appium-android-driver/lib/driver.ts +1 -0
- package/node_modules/appium-android-driver/package.json +5 -5
- package/node_modules/appium-chromedriver/CHANGELOG.md +12 -0
- package/node_modules/appium-chromedriver/README.md +26 -0
- package/node_modules/appium-chromedriver/build/lib/checksums.d.ts +28 -0
- package/node_modules/appium-chromedriver/build/lib/checksums.d.ts.map +1 -0
- package/node_modules/appium-chromedriver/build/lib/checksums.js +73 -0
- package/node_modules/appium-chromedriver/build/lib/checksums.js.map +1 -0
- package/node_modules/appium-chromedriver/build/lib/chromedriver.d.ts +1 -0
- package/node_modules/appium-chromedriver/build/lib/chromedriver.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/chromedriver.js +3 -1
- package/node_modules/appium-chromedriver/build/lib/chromedriver.js.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/commands/binary.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/commands/binary.js +26 -1
- package/node_modules/appium-chromedriver/build/lib/commands/binary.js.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/commands/types.d.ts +1 -0
- package/node_modules/appium-chromedriver/build/lib/commands/types.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/storage-client/storage-client.d.ts +1 -0
- package/node_modules/appium-chromedriver/build/lib/storage-client/storage-client.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/storage-client/storage-client.js +8 -1
- package/node_modules/appium-chromedriver/build/lib/storage-client/storage-client.js.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/types.d.ts +4 -0
- package/node_modules/appium-chromedriver/build/lib/types.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/utils.d.ts +8 -0
- package/node_modules/appium-chromedriver/build/lib/utils.d.ts.map +1 -1
- package/node_modules/appium-chromedriver/build/lib/utils.js +1 -1
- package/node_modules/appium-chromedriver/build/lib/utils.js.map +1 -1
- package/node_modules/appium-chromedriver/config/checksums.json +1665 -0
- package/node_modules/appium-chromedriver/config/edge-checksums.json +439 -0
- package/node_modules/appium-chromedriver/lib/checksums.ts +92 -0
- package/node_modules/appium-chromedriver/lib/chromedriver.ts +3 -0
- package/node_modules/appium-chromedriver/lib/commands/binary.ts +73 -42
- package/node_modules/appium-chromedriver/lib/commands/types.ts +1 -0
- package/node_modules/appium-chromedriver/lib/storage-client/storage-client.ts +9 -2
- package/node_modules/appium-chromedriver/lib/types.ts +4 -0
- package/node_modules/appium-chromedriver/lib/utils.ts +1 -1
- package/node_modules/appium-chromedriver/package.json +3 -2
- package/node_modules/appium-uiautomator2-server/CHANGELOG.md +120 -0
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk +0 -0
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk.idsig +0 -0
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v11.3.0.apk +0 -0
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v11.3.0.apk.idsig +0 -0
- package/node_modules/appium-uiautomator2-server/package.json +2 -2
- package/node_modules/asyncbox/README.md +7 -3
- package/node_modules/asyncbox/build/lib/shared/errors.d.ts +18 -2
- package/node_modules/asyncbox/build/lib/shared/errors.d.ts.map +1 -1
- package/node_modules/asyncbox/build/lib/shared/errors.js +24 -8
- package/node_modules/asyncbox/build/lib/shared/errors.js.map +1 -1
- package/node_modules/asyncbox/lib/shared/errors.ts +34 -8
- package/node_modules/asyncbox/package.json +1 -1
- package/node_modules/bare-fs/package.json +2 -2
- package/node_modules/bare-fs/prebuilds/darwin-arm64/bare-fs.bare +0 -0
- package/node_modules/bare-fs/prebuilds/linux-arm64/bare-fs.bare +0 -0
- package/node_modules/bare-fs/prebuilds/linux-x64/bare-fs.bare +0 -0
- package/node_modules/bare-fs/prebuilds/win32-x64/bare-fs.bare +0 -0
- package/node_modules/env-paths/index.d.ts +90 -0
- package/node_modules/env-paths/index.js +69 -0
- package/node_modules/env-paths/license +9 -0
- package/node_modules/env-paths/package.json +55 -0
- package/node_modules/env-paths/readme.md +100 -0
- package/node_modules/io.appium.settings/CHANGELOG.md +14 -0
- package/node_modules/io.appium.settings/apks/settings_apk-debug.apk +0 -0
- package/node_modules/io.appium.settings/package.json +2 -2
- package/node_modules/is-safe-filename/index.d.ts +59 -0
- package/node_modules/is-safe-filename/index.js +41 -0
- package/node_modules/is-safe-filename/license +9 -0
- package/node_modules/is-safe-filename/package.json +45 -0
- package/node_modules/is-safe-filename/readme.md +62 -0
- package/node_modules/plist/dist/parse-binary.d.ts.map +1 -1
- package/node_modules/plist/dist/parse-binary.js +37 -9
- package/node_modules/plist/dist/parse-binary.js.map +1 -1
- package/node_modules/plist/package.json +1 -1
- package/node_modules/xpath/package.json +4 -3
- package/node_modules/xpath/xpath.d.ts +69 -21
- package/node_modules/xpath/xpath.js +233 -81
- package/package.json +5 -5
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v11.0.8.apk +0 -0
- package/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v11.0.8.apk.idsig +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Item, ItemEncoding, Strongbox, Value } from './index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base item implementation
|
|
4
|
+
*
|
|
5
|
+
* @remarks This class is not intended to be instantiated directly
|
|
6
|
+
* @typeParam T - Type of data stored in the `Item`
|
|
7
|
+
*/
|
|
8
|
+
export declare class BaseItem<T extends Value, U extends Strongbox = Strongbox> implements Item<T> {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly encoding: ItemEncoding;
|
|
11
|
+
/**
|
|
12
|
+
* Parent Strongbox instance
|
|
13
|
+
*/
|
|
14
|
+
readonly container: string;
|
|
15
|
+
/**
|
|
16
|
+
* Unique slugified identifier
|
|
17
|
+
*/
|
|
18
|
+
readonly id: string;
|
|
19
|
+
/**
|
|
20
|
+
* {@inheritdoc Item.value}
|
|
21
|
+
*/
|
|
22
|
+
readonly value: T | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* {@inheritdoc Item.value}
|
|
25
|
+
*/
|
|
26
|
+
protected _value?: T;
|
|
27
|
+
/**
|
|
28
|
+
* Slugifies the name
|
|
29
|
+
* @param name Name of instance
|
|
30
|
+
* @param parent Parent Strongbox
|
|
31
|
+
* @param encoding Defaults to `utf8`
|
|
32
|
+
*/
|
|
33
|
+
constructor(name: string, parent: U, encoding?: ItemEncoding);
|
|
34
|
+
/**
|
|
35
|
+
* Absolute filesystem path of the file backing an item: `container` + slugified `name`.
|
|
36
|
+
* Also used to convert a `name` to an `id`.
|
|
37
|
+
*/
|
|
38
|
+
static toFilePath(container: string, name: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* {@inheritdoc Item.clear}
|
|
41
|
+
*/
|
|
42
|
+
clear(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* {@inheritdoc Item.read}
|
|
45
|
+
*/
|
|
46
|
+
read(): Promise<T | undefined>;
|
|
47
|
+
/**
|
|
48
|
+
* {@inheritdoc Item.write}
|
|
49
|
+
*/
|
|
50
|
+
write(value: T): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=base-item.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-item.d.ts","sourceRoot":"","sources":["../../lib/base-item.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAGrE;;;;;GAKG;AACH,qBAAa,QAAQ,CAAC,CAAC,SAAS,KAAK,EAAE,CAAC,SAAS,SAAS,GAAG,SAAS,CAAE,YAAW,IAAI,CAAC,CAAC,CAAC;aA0BtE,IAAI,EAAE,MAAM;aAEZ,QAAQ,EAAE,YAAY;IA3BxC;;OAEG;IACH,SAAgB,SAAS,EAAE,MAAM,CAAC;IAClC;;OAEG;IACH,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B;;OAEG;IACH,SAAgB,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC;IAErC;;OAEG;IACH,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAErB;;;;;OAKG;gBAEe,IAAI,EAAE,MAAM,EAC5B,MAAM,EAAE,CAAC,EACO,QAAQ,GAAE,YAAqB;IAkBjD;;;OAGG;WACW,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAIjE;;OAEG;IACU,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAWnC;;OAEG;IACU,IAAI,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAa3C;;OAEG;IACU,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAO5C"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { slugify } from './util.js';
|
|
4
|
+
/**
|
|
5
|
+
* Base item implementation
|
|
6
|
+
*
|
|
7
|
+
* @remarks This class is not intended to be instantiated directly
|
|
8
|
+
* @typeParam T - Type of data stored in the `Item`
|
|
9
|
+
*/
|
|
10
|
+
export class BaseItem {
|
|
11
|
+
name;
|
|
12
|
+
encoding;
|
|
13
|
+
/**
|
|
14
|
+
* Parent Strongbox instance
|
|
15
|
+
*/
|
|
16
|
+
container;
|
|
17
|
+
/**
|
|
18
|
+
* Unique slugified identifier
|
|
19
|
+
*/
|
|
20
|
+
id;
|
|
21
|
+
/**
|
|
22
|
+
* {@inheritdoc Item.value}
|
|
23
|
+
*/
|
|
24
|
+
value;
|
|
25
|
+
/**
|
|
26
|
+
* {@inheritdoc Item.value}
|
|
27
|
+
*/
|
|
28
|
+
_value;
|
|
29
|
+
/**
|
|
30
|
+
* Slugifies the name
|
|
31
|
+
* @param name Name of instance
|
|
32
|
+
* @param parent Parent Strongbox
|
|
33
|
+
* @param encoding Defaults to `utf8`
|
|
34
|
+
*/
|
|
35
|
+
constructor(name, parent, encoding = 'utf8') {
|
|
36
|
+
this.name = name;
|
|
37
|
+
this.encoding = encoding;
|
|
38
|
+
this.container = parent.container;
|
|
39
|
+
this.id = BaseItem.toFilePath(this.container, name);
|
|
40
|
+
Object.defineProperties(this, {
|
|
41
|
+
value: {
|
|
42
|
+
get() {
|
|
43
|
+
return this._value;
|
|
44
|
+
},
|
|
45
|
+
enumerable: true,
|
|
46
|
+
},
|
|
47
|
+
_value: {
|
|
48
|
+
enumerable: false,
|
|
49
|
+
writable: true,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Absolute filesystem path of the file backing an item: `container` + slugified `name`.
|
|
55
|
+
* Also used to convert a `name` to an `id`.
|
|
56
|
+
*/
|
|
57
|
+
static toFilePath(container, name) {
|
|
58
|
+
return path.join(container, slugify(name));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* {@inheritdoc Item.clear}
|
|
62
|
+
*/
|
|
63
|
+
async clear() {
|
|
64
|
+
try {
|
|
65
|
+
await unlink(this.id);
|
|
66
|
+
this._value = undefined;
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e.code !== 'ENOENT') {
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritdoc Item.read}
|
|
76
|
+
*/
|
|
77
|
+
async read() {
|
|
78
|
+
try {
|
|
79
|
+
this._value = (await readFile(this.id, {
|
|
80
|
+
encoding: this.encoding,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
if (e.code !== 'ENOENT') {
|
|
85
|
+
throw e;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return this._value;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* {@inheritdoc Item.write}
|
|
92
|
+
*/
|
|
93
|
+
async write(value) {
|
|
94
|
+
if (this._value !== value) {
|
|
95
|
+
await mkdir(path.dirname(this.id), { recursive: true });
|
|
96
|
+
await writeFile(this.id, value, this.encoding);
|
|
97
|
+
this._value = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=base-item.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-item.js","sourceRoot":"","sources":["../../lib/base-item.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AACpE,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAElC;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IA0BD;IAEA;IA3BlB;;OAEG;IACa,SAAS,CAAS;IAClC;;OAEG;IACa,EAAE,CAAS;IAC3B;;OAEG;IACa,KAAK,CAAgB;IAErC;;OAEG;IACO,MAAM,CAAK;IAErB;;;;;OAKG;IACH,YACkB,IAAY,EAC5B,MAAS,EACO,WAAyB,MAAM;QAF/B,SAAI,GAAJ,IAAI,CAAQ;QAEZ,aAAQ,GAAR,QAAQ,CAAuB;QAE/C,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,EAAE,GAAG,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QACpD,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE;YAC5B,KAAK,EAAE;gBACL,GAAG;oBACD,OAAO,IAAI,CAAC,MAAM,CAAC;gBACrB,CAAC;gBACD,UAAU,EAAE,IAAI;aACjB;YACD,MAAM,EAAE;gBACN,UAAU,EAAE,KAAK;gBACjB,QAAQ,EAAE,IAAI;aACf;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,UAAU,CAAC,SAAiB,EAAE,IAAY;QACtD,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OA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+
*
|
|
5
|
+
* `null` means the file should be read and written as a `Buffer`.
|
|
6
|
+
*/
|
|
7
|
+
export type ItemEncoding = BufferEncoding | null;
|
|
8
|
+
/**
|
|
9
|
+
* Valid value wrapped by {@linkcode Item}. Can be an encoded string or `Buffer`
|
|
10
|
+
*/
|
|
11
|
+
export type Value = string | Buffer;
|
|
12
|
+
/**
|
|
13
|
+
* An object representing a persisted item containing something of type `T` (which can be a {@linkcode Buffer} or an encoded string; see {@linkcode ItemEncoding}).
|
|
14
|
+
*
|
|
15
|
+
* A {@linkcode Item} does not know anything about where it is stored, or how it is stored.
|
|
16
|
+
* @typeParam T - Type of data stored in the item
|
|
17
|
+
*/
|
|
18
|
+
export interface Item<T extends Value> {
|
|
19
|
+
/**
|
|
20
|
+
* Encoding of underlying value
|
|
21
|
+
*/
|
|
22
|
+
encoding: ItemEncoding;
|
|
23
|
+
/**
|
|
24
|
+
* Slugified name
|
|
25
|
+
*/
|
|
26
|
+
id: string;
|
|
27
|
+
/**
|
|
28
|
+
* Name of item
|
|
29
|
+
*/
|
|
30
|
+
name: string;
|
|
31
|
+
/**
|
|
32
|
+
* Last known value (stored in memory)
|
|
33
|
+
*
|
|
34
|
+
* @remarks A custom {@linkcode Item} meant to handle very large files should probably not implement this.
|
|
35
|
+
*/
|
|
36
|
+
value?: T;
|
|
37
|
+
/**
|
|
38
|
+
* Deletes the item.
|
|
39
|
+
*/
|
|
40
|
+
clear(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Reads value
|
|
43
|
+
*/
|
|
44
|
+
read(): Promise<T | undefined>;
|
|
45
|
+
/**
|
|
46
|
+
* Writes value
|
|
47
|
+
* @param value New value
|
|
48
|
+
*/
|
|
49
|
+
write(value: T): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Default container suffix if no explicit container is provided.
|
|
53
|
+
*
|
|
54
|
+
* @see {@linkcode StrongboxOpts}
|
|
55
|
+
*/
|
|
56
|
+
export declare const DEFAULT_SUFFIX = "strongbox";
|
|
57
|
+
/**
|
|
58
|
+
* A constructor function which instantiates a {@linkcode Item}.
|
|
59
|
+
*/
|
|
60
|
+
export type ItemCtor<T extends Value, U extends StrongboxOpts = StrongboxOpts, V extends Strongbox<U> = Strongbox<U>> = new (name: string, parent: V, encoding?: ItemEncoding) => Item<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Options for {@linkcode strongbox}
|
|
63
|
+
*/
|
|
64
|
+
export interface StrongboxOpts {
|
|
65
|
+
/**
|
|
66
|
+
* Override default container, which is chosen according to environment.
|
|
67
|
+
*
|
|
68
|
+
* This must be a writable path.
|
|
69
|
+
*/
|
|
70
|
+
container: string;
|
|
71
|
+
/**
|
|
72
|
+
* Default {@linkcode Item} constructor.
|
|
73
|
+
*
|
|
74
|
+
* Unless a constructor is specified when calling {@linkcode Strongbox.createItem} or {@linkcode Strongbox.createItemWithValue}, this will be used.
|
|
75
|
+
* @defaultValue BaseItem
|
|
76
|
+
*/
|
|
77
|
+
defaultItemCtor: ItemCtor<any>;
|
|
78
|
+
/**
|
|
79
|
+
* Extra subdir to append to the auto-generated file directory hierarchy.
|
|
80
|
+
*
|
|
81
|
+
* This is ignored if `container` is provided.
|
|
82
|
+
* @defaultValue 'strongbox'
|
|
83
|
+
*/
|
|
84
|
+
suffix: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Main entry point for use of this module
|
|
88
|
+
*
|
|
89
|
+
* Manages multiple {@linkcode Item}s.
|
|
90
|
+
*/
|
|
91
|
+
export declare class Strongbox<Options extends StrongboxOpts = StrongboxOpts> implements AsyncIterable<Item<any>> {
|
|
92
|
+
readonly name: string;
|
|
93
|
+
/**
|
|
94
|
+
* Override the directory of this container.
|
|
95
|
+
*
|
|
96
|
+
* If this is present, `suffix` is ignored.
|
|
97
|
+
*/
|
|
98
|
+
readonly container: string;
|
|
99
|
+
/**
|
|
100
|
+
* Slugified name of this instance; corresponds to the directory name.
|
|
101
|
+
*
|
|
102
|
+
* If `dir` is provided, this value is unused.
|
|
103
|
+
* If `suffix` is provided, then this will be the parent directory of `suffix`.
|
|
104
|
+
*/
|
|
105
|
+
readonly id: string;
|
|
106
|
+
readonly suffix: string;
|
|
107
|
+
/**
|
|
108
|
+
* Default {@linkcode ItemCtor} to use when creating new {@linkcode Item}s
|
|
109
|
+
*/
|
|
110
|
+
protected defaultItemCtor: ItemCtor<any>;
|
|
111
|
+
/**
|
|
112
|
+
* Slugifies the name & determines the directory
|
|
113
|
+
* @param name Name of instance
|
|
114
|
+
* @param opts Options
|
|
115
|
+
*/
|
|
116
|
+
protected constructor(name: string, opts?: Partial<Options>);
|
|
117
|
+
/**
|
|
118
|
+
* Creates a new {@linkcode Strongbox}
|
|
119
|
+
* @param name Name of instance
|
|
120
|
+
* @param opts Options
|
|
121
|
+
* @returns New instance
|
|
122
|
+
*/
|
|
123
|
+
static create<Options extends StrongboxOpts = StrongboxOpts>(name: string, opts?: Partial<Options>): Strongbox<Options>;
|
|
124
|
+
/**
|
|
125
|
+
* Clears _all_ items.
|
|
126
|
+
*
|
|
127
|
+
* @param force - If `true`, will rimraf the container. Otherwise, will only delete individual items.
|
|
128
|
+
*/
|
|
129
|
+
clearAll(force?: boolean): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Create a new {@linkcode Item}.
|
|
132
|
+
*
|
|
133
|
+
* Reads the item, if it is already persisted. Does not throw if missing.
|
|
134
|
+
* @param name Unique name of item
|
|
135
|
+
* @param encoding Encoding of item; defaults to `utf8`
|
|
136
|
+
* @returns New `Item`
|
|
137
|
+
* @typeParam T - Type of data stored in the `Item`
|
|
138
|
+
*/
|
|
139
|
+
createItem<T extends Value>(name: string, ctor?: ItemCtor<T>, encoding?: ItemEncoding): Promise<Item<T>>;
|
|
140
|
+
createItem<T extends Value>(name: string, encoding?: ItemEncoding): Promise<Item<T>>;
|
|
141
|
+
/**
|
|
142
|
+
* Creates a {@linkcode Item} then immediately writes value to it.
|
|
143
|
+
*
|
|
144
|
+
* If it exists already, it will be overwritten.
|
|
145
|
+
* @param name Name of `Item`
|
|
146
|
+
* @param value File value to write
|
|
147
|
+
* @param ctor Specific {@linkcode ItemCtor} to use
|
|
148
|
+
* @param encoding File encoding
|
|
149
|
+
* @returns New `Item` w/ value of `value`
|
|
150
|
+
*/
|
|
151
|
+
createItemWithValue<T extends Value>(name: string, value: T, ctor: ItemCtor<T>, encoding?: ItemEncoding): Promise<Item<T>>;
|
|
152
|
+
createItemWithValue<T extends Value>(name: string, value: T, encoding?: ItemEncoding): Promise<Item<T>>;
|
|
153
|
+
/**
|
|
154
|
+
* Attempts to retrieve an {@linkcode Item} by its `id`.
|
|
155
|
+
* Drops stale {@linkcode WeakRef} map entries when the value was collected.
|
|
156
|
+
* @param id ID of item
|
|
157
|
+
* @returns An `Item`, if found
|
|
158
|
+
*/
|
|
159
|
+
getItem(id: string): Item<any> | undefined;
|
|
160
|
+
/**
|
|
161
|
+
* Lists persisted items by scanning the container directory (one regular file per item).
|
|
162
|
+
*
|
|
163
|
+
* Filenames are matched to items by path; if an item was already registered (e.g. via
|
|
164
|
+
* {@linkcode Strongbox.createItem}), that instance is returned and keeps its original `name`.
|
|
165
|
+
* Otherwise a new item is created using the filename as `name` (see {@linkcode BaseItem}).
|
|
166
|
+
*
|
|
167
|
+
* @remarks Builds one array of every {@linkcode Item} reference. Does not read file contents;
|
|
168
|
+
* call {@linkcode Item.read} on each item as needed. Order follows directory iteration
|
|
169
|
+
* ({@linkcode opendir}), not lexicographic sort. For many items, `for await (const item of box)`
|
|
170
|
+
* ({@linkcode Symbol.asyncIterator}) streams entries without allocating a full {@linkcode Item}[]
|
|
171
|
+
* first.
|
|
172
|
+
*
|
|
173
|
+
* @returns Items in directory iteration order; empty if the container directory does not exist yet
|
|
174
|
+
*/
|
|
175
|
+
listItems(): Promise<Item<any>[]>;
|
|
176
|
+
/**
|
|
177
|
+
* Yields each persisted item in the same order as {@linkcode Strongbox.listItems}. Use
|
|
178
|
+
* `for await (const item of box)`.
|
|
179
|
+
*
|
|
180
|
+
* @remarks Walks the container with {@linkcode opendir} and yields one {@linkcode Item} per file
|
|
181
|
+
* as basenames are seen (no full-name buffer and no sort).
|
|
182
|
+
*/
|
|
183
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<Item<any>>;
|
|
184
|
+
/**
|
|
185
|
+
* Performs runtime validation (and optionally transformation) of options.
|
|
186
|
+
*
|
|
187
|
+
* Should not set defaults.
|
|
188
|
+
*
|
|
189
|
+
* The default implementation slugifies any custom container name and suffix.
|
|
190
|
+
*
|
|
191
|
+
* Subclasses should override this method to perform additional validation as needed.
|
|
192
|
+
* @param opts - Options
|
|
193
|
+
*/
|
|
194
|
+
protected checkOptions(opts: Options): Options;
|
|
195
|
+
/**
|
|
196
|
+
* Sets defaults for options.
|
|
197
|
+
*
|
|
198
|
+
* Subclasses should override as necessary.
|
|
199
|
+
* @param opts Options
|
|
200
|
+
* @returns Options with defaults applied
|
|
201
|
+
*/
|
|
202
|
+
protected setDefaultOptions(opts?: Partial<Options>): Options;
|
|
203
|
+
/**
|
|
204
|
+
* Returns a live {@linkcode Item} or removes a stale {@linkcode WeakRef} from {@linkcode Strongbox.items}.
|
|
205
|
+
*/
|
|
206
|
+
private getLiveItem;
|
|
207
|
+
/**
|
|
208
|
+
* Streams regular-file basenames from the container using {@linkcode opendir} (order is
|
|
209
|
+
* filesystem-defined, not sorted).
|
|
210
|
+
*/
|
|
211
|
+
private iterateFileBasenames;
|
|
212
|
+
/**
|
|
213
|
+
* Registers an {@linkcode Item} for a filename on disk without reading the file (see
|
|
214
|
+
* {@linkcode Strongbox.createItem}, which loads persisted contents eagerly). Uses the same
|
|
215
|
+
* constructor arity as {@linkcode Strongbox.createItem} when no encoding is given.
|
|
216
|
+
*/
|
|
217
|
+
private registerItemWithoutRead;
|
|
218
|
+
private resolveItemForBasename;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* {@inheritdoc Strongbox.create}
|
|
222
|
+
*/
|
|
223
|
+
export declare const strongbox: typeof Strongbox.create;
|
|
224
|
+
/**
|
|
225
|
+
* This can be subclassed if needed.
|
|
226
|
+
*/
|
|
227
|
+
export { BaseItem };
|
|
228
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAGxC;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG,IAAI,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,WAAW,IAAI,CAAC,CAAC,SAAS,KAAK;IACnC;;OAEG;IACH,QAAQ,EAAE,YAAY,CAAC;IACvB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,KAAK,CAAC,EAAE,CAAC,CAAC;IAEV;;OAEG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB;;OAEG;IACH,IAAI,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAC/B;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChC;AA8BD;;;;GAIG;AACH,eAAO,MAAM,cAAc,cAAc,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS,KAAK,EACf,CAAC,SAAS,aAAa,GAAG,aAAa,EACvC,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,IACnC,KAAK,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,YAAY,KAAK,IAAI,CAAC,CAAC,C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env-paths';
|
|
4
|
+
import { BaseItem } from './base-item.js';
|
|
5
|
+
import { slugify } from './util.js';
|
|
6
|
+
/**
|
|
7
|
+
* Set of known `Item` encodings
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
const ITEM_ENCODINGS = new Set([
|
|
11
|
+
'ascii',
|
|
12
|
+
'utf8',
|
|
13
|
+
'utf-8',
|
|
14
|
+
'utf16le',
|
|
15
|
+
'ucs2',
|
|
16
|
+
'ucs-2',
|
|
17
|
+
'base64',
|
|
18
|
+
'base64url',
|
|
19
|
+
'latin1',
|
|
20
|
+
'binary',
|
|
21
|
+
'hex',
|
|
22
|
+
null,
|
|
23
|
+
]);
|
|
24
|
+
/**
|
|
25
|
+
* Type guard for encodings
|
|
26
|
+
* @param value any
|
|
27
|
+
* @returns `true` is `value` is a valid file encoding
|
|
28
|
+
*/
|
|
29
|
+
function isEncoding(value) {
|
|
30
|
+
return ITEM_ENCODINGS.has(value);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Default container suffix if no explicit container is provided.
|
|
34
|
+
*
|
|
35
|
+
* @see {@linkcode StrongboxOpts}
|
|
36
|
+
*/
|
|
37
|
+
export const DEFAULT_SUFFIX = 'strongbox';
|
|
38
|
+
/**
|
|
39
|
+
* Main entry point for use of this module
|
|
40
|
+
*
|
|
41
|
+
* Manages multiple {@linkcode Item}s.
|
|
42
|
+
*/
|
|
43
|
+
export class Strongbox {
|
|
44
|
+
name;
|
|
45
|
+
/**
|
|
46
|
+
* Override the directory of this container.
|
|
47
|
+
*
|
|
48
|
+
* If this is present, `suffix` is ignored.
|
|
49
|
+
*/
|
|
50
|
+
container;
|
|
51
|
+
/**
|
|
52
|
+
* Slugified name of this instance; corresponds to the directory name.
|
|
53
|
+
*
|
|
54
|
+
* If `dir` is provided, this value is unused.
|
|
55
|
+
* If `suffix` is provided, then this will be the parent directory of `suffix`.
|
|
56
|
+
*/
|
|
57
|
+
id;
|
|
58
|
+
suffix;
|
|
59
|
+
/**
|
|
60
|
+
* Default {@linkcode ItemCtor} to use when creating new {@linkcode Item}s
|
|
61
|
+
*/
|
|
62
|
+
defaultItemCtor;
|
|
63
|
+
/**
|
|
64
|
+
* Store of known {@linkcode Item}s
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
items;
|
|
68
|
+
/**
|
|
69
|
+
* Slugifies the name & determines the directory
|
|
70
|
+
* @param name Name of instance
|
|
71
|
+
* @param opts Options
|
|
72
|
+
*/
|
|
73
|
+
constructor(name, opts = {}) {
|
|
74
|
+
this.name = name;
|
|
75
|
+
this.id = slugify(name);
|
|
76
|
+
let newOpts = this.setDefaultOptions(opts);
|
|
77
|
+
newOpts = this.checkOptions(newOpts);
|
|
78
|
+
this.defaultItemCtor = newOpts.defaultItemCtor;
|
|
79
|
+
this.container = newOpts.container;
|
|
80
|
+
this.suffix = newOpts.suffix;
|
|
81
|
+
this.items = new Map();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new {@linkcode Strongbox}
|
|
85
|
+
* @param name Name of instance
|
|
86
|
+
* @param opts Options
|
|
87
|
+
* @returns New instance
|
|
88
|
+
*/
|
|
89
|
+
static create(name, opts) {
|
|
90
|
+
return new Strongbox(name, opts);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Clears _all_ items.
|
|
94
|
+
*
|
|
95
|
+
* @param force - If `true`, will rimraf the container. Otherwise, will only delete individual items.
|
|
96
|
+
*/
|
|
97
|
+
async clearAll(force = false) {
|
|
98
|
+
const items = [...this.items.values()].map((ref) => ref.deref()).filter(Boolean);
|
|
99
|
+
await Promise.all(items.map((item) => item.clear()));
|
|
100
|
+
if (force) {
|
|
101
|
+
await rm(this.container, { recursive: true });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async createItem(name, encodingOrCtor, encoding) {
|
|
105
|
+
if (isEncoding(encodingOrCtor)) {
|
|
106
|
+
encoding = encodingOrCtor;
|
|
107
|
+
encodingOrCtor = this.defaultItemCtor;
|
|
108
|
+
}
|
|
109
|
+
if (!slugify(name)) {
|
|
110
|
+
// the slug is the filename; an empty one would resolve to the container directory itself
|
|
111
|
+
throw new TypeError(`Item name '${name}' must contain at least one alphanumeric character`);
|
|
112
|
+
}
|
|
113
|
+
const item = new (encodingOrCtor ?? this.defaultItemCtor)(name, this, encoding);
|
|
114
|
+
if (this.getLiveItem(item.id)) {
|
|
115
|
+
throw new ReferenceError(`Item with id "${item.id}" already exists`);
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
await item.read();
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
if (e.code !== 'ENOENT') {
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
this.items.set(item.id, new WeakRef(item));
|
|
126
|
+
return item;
|
|
127
|
+
}
|
|
128
|
+
async createItemWithValue(name, value, encodingOrCtor, encoding) {
|
|
129
|
+
const item = isEncoding(encodingOrCtor)
|
|
130
|
+
? await this.createItem(name, encodingOrCtor)
|
|
131
|
+
: await this.createItem(name, encodingOrCtor, encoding);
|
|
132
|
+
await item.write(value);
|
|
133
|
+
return item;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Attempts to retrieve an {@linkcode Item} by its `id`.
|
|
137
|
+
* Drops stale {@linkcode WeakRef} map entries when the value was collected.
|
|
138
|
+
* @param id ID of item
|
|
139
|
+
* @returns An `Item`, if found
|
|
140
|
+
*/
|
|
141
|
+
getItem(id) {
|
|
142
|
+
return this.getLiveItem(id);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Lists persisted items by scanning the container directory (one regular file per item).
|
|
146
|
+
*
|
|
147
|
+
* Filenames are matched to items by path; if an item was already registered (e.g. via
|
|
148
|
+
* {@linkcode Strongbox.createItem}), that instance is returned and keeps its original `name`.
|
|
149
|
+
* Otherwise a new item is created using the filename as `name` (see {@linkcode BaseItem}).
|
|
150
|
+
*
|
|
151
|
+
* @remarks Builds one array of every {@linkcode Item} reference. Does not read file contents;
|
|
152
|
+
* call {@linkcode Item.read} on each item as needed. Order follows directory iteration
|
|
153
|
+
* ({@linkcode opendir}), not lexicographic sort. For many items, `for await (const item of box)`
|
|
154
|
+
* ({@linkcode Symbol.asyncIterator}) streams entries without allocating a full {@linkcode Item}[]
|
|
155
|
+
* first.
|
|
156
|
+
*
|
|
157
|
+
* @returns Items in directory iteration order; empty if the container directory does not exist yet
|
|
158
|
+
*/
|
|
159
|
+
async listItems() {
|
|
160
|
+
const items = [];
|
|
161
|
+
for await (const basename of this.iterateFileBasenames()) {
|
|
162
|
+
items.push(this.resolveItemForBasename(basename));
|
|
163
|
+
}
|
|
164
|
+
return items;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Yields each persisted item in the same order as {@linkcode Strongbox.listItems}. Use
|
|
168
|
+
* `for await (const item of box)`.
|
|
169
|
+
*
|
|
170
|
+
* @remarks Walks the container with {@linkcode opendir} and yields one {@linkcode Item} per file
|
|
171
|
+
* as basenames are seen (no full-name buffer and no sort).
|
|
172
|
+
*/
|
|
173
|
+
async *[Symbol.asyncIterator]() {
|
|
174
|
+
for await (const basename of this.iterateFileBasenames()) {
|
|
175
|
+
yield this.resolveItemForBasename(basename);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Performs runtime validation (and optionally transformation) of options.
|
|
180
|
+
*
|
|
181
|
+
* Should not set defaults.
|
|
182
|
+
*
|
|
183
|
+
* The default implementation slugifies any custom container name and suffix.
|
|
184
|
+
*
|
|
185
|
+
* Subclasses should override this method to perform additional validation as needed.
|
|
186
|
+
* @param opts - Options
|
|
187
|
+
*/
|
|
188
|
+
checkOptions(opts) {
|
|
189
|
+
opts.suffix = slugify(opts.suffix);
|
|
190
|
+
if (opts.container) {
|
|
191
|
+
if (!path.isAbsolute(opts.container)) {
|
|
192
|
+
throw new TypeError(`container slug ${opts.container} must be an absolute path`);
|
|
193
|
+
}
|
|
194
|
+
// slugify the folders only. the root is what makes the path absolute (a drive letter or UNC
|
|
195
|
+
// share on windows), and slugify would strip the colon. normalize first so mixed slashes
|
|
196
|
+
// still split into real segments.
|
|
197
|
+
const normalized = path.normalize(opts.container);
|
|
198
|
+
const { root } = path.parse(normalized);
|
|
199
|
+
const segments = normalized.slice(root.length).split(path.sep).filter(Boolean);
|
|
200
|
+
opts.container = path.join(root, ...segments.map(slugify));
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
opts.container = path.join(envPaths(this.id).data, opts.suffix);
|
|
204
|
+
}
|
|
205
|
+
return opts;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Sets defaults for options.
|
|
209
|
+
*
|
|
210
|
+
* Subclasses should override as necessary.
|
|
211
|
+
* @param opts Options
|
|
212
|
+
* @returns Options with defaults applied
|
|
213
|
+
*/
|
|
214
|
+
setDefaultOptions(opts = {}) {
|
|
215
|
+
const newOpts = opts;
|
|
216
|
+
newOpts.suffix = opts.suffix ?? DEFAULT_SUFFIX;
|
|
217
|
+
newOpts.defaultItemCtor = opts.defaultItemCtor ?? BaseItem;
|
|
218
|
+
return newOpts;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Returns a live {@linkcode Item} or removes a stale {@linkcode WeakRef} from {@linkcode Strongbox.items}.
|
|
222
|
+
*/
|
|
223
|
+
getLiveItem(id) {
|
|
224
|
+
const ref = this.items.get(id);
|
|
225
|
+
if (!ref) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
const item = ref.deref();
|
|
229
|
+
if (!item) {
|
|
230
|
+
this.items.delete(id);
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
return item;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Streams regular-file basenames from the container using {@linkcode opendir} (order is
|
|
237
|
+
* filesystem-defined, not sorted).
|
|
238
|
+
*/
|
|
239
|
+
async *iterateFileBasenames() {
|
|
240
|
+
let dir;
|
|
241
|
+
try {
|
|
242
|
+
dir = await opendir(this.container);
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
if (e.code === 'ENOENT') {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
throw e;
|
|
249
|
+
}
|
|
250
|
+
for await (const ent of dir) {
|
|
251
|
+
// a basename with no alphanumerics has no slug, so it cannot belong to an item
|
|
252
|
+
if (ent.isFile() && slugify(ent.name)) {
|
|
253
|
+
yield ent.name;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Registers an {@linkcode Item} for a filename on disk without reading the file (see
|
|
259
|
+
* {@linkcode Strongbox.createItem}, which loads persisted contents eagerly). Uses the same
|
|
260
|
+
* constructor arity as {@linkcode Strongbox.createItem} when no encoding is given.
|
|
261
|
+
*/
|
|
262
|
+
registerItemWithoutRead(name) {
|
|
263
|
+
const item = new this.defaultItemCtor(name, this, undefined);
|
|
264
|
+
if (this.getLiveItem(item.id)) {
|
|
265
|
+
throw new ReferenceError(`Item with id "${item.id}" already exists`);
|
|
266
|
+
}
|
|
267
|
+
this.items.set(item.id, new WeakRef(item));
|
|
268
|
+
return item;
|
|
269
|
+
}
|
|
270
|
+
resolveItemForBasename(basename) {
|
|
271
|
+
const id = BaseItem.toFilePath(this.container, basename);
|
|
272
|
+
return this.getItem(id) ?? this.registerItemWithoutRead(basename);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* {@inheritdoc Strongbox.create}
|
|
277
|
+
*/
|
|
278
|
+
export const strongbox = Strongbox.create;
|
|
279
|
+
/**
|
|
280
|
+
* This can be subclassed if needed.
|
|
281
|
+
*/
|
|
282
|
+
export { BaseItem };
|
|
283
|
+
//# sourceMappingURL=index.js.map
|