posthog-node 5.9.1 → 5.9.3
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/extensions/feature-flags/crypto.d.ts.map +1 -1
- package/dist/extensions/feature-flags/crypto.js +5 -10
- package/dist/extensions/feature-flags/crypto.mjs +5 -10
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +3 -4
- package/src/extensions/feature-flags/crypto.ts +6 -16
- package/src/version.ts +1 -1
- package/dist/extensions/feature-flags/crypto-helpers.d.ts +0 -3
- package/dist/extensions/feature-flags/crypto-helpers.d.ts.map +0 -1
- package/dist/extensions/feature-flags/crypto-helpers.js +0 -77
- package/dist/extensions/feature-flags/crypto-helpers.mjs +0 -22
- package/dist/extensions/feature-flags/lazy.d.ts +0 -24
- package/dist/extensions/feature-flags/lazy.d.ts.map +0 -1
- package/dist/extensions/feature-flags/lazy.js +0 -60
- package/dist/extensions/feature-flags/lazy.mjs +0 -26
- package/src/extensions/feature-flags/crypto-helpers.ts +0 -36
- package/src/extensions/feature-flags/lazy.ts +0 -55
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/extensions/feature-flags/crypto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/extensions/feature-flags/crypto.ts"],"names":[],"mappings":"AAEA,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAS5D"}
|
|
@@ -26,17 +26,12 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
hashSHA1: ()=>hashSHA1
|
|
28
28
|
});
|
|
29
|
-
const external_crypto_helpers_js_namespaceObject = require("./crypto-helpers.js");
|
|
30
29
|
async function hashSHA1(text) {
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
37
|
-
return hashArray.map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
38
|
-
}
|
|
39
|
-
throw new Error('No crypto implementation available. Tried Node Crypto API and Web SubtleCrypto API');
|
|
30
|
+
const subtle = globalThis.crypto?.subtle;
|
|
31
|
+
if (!subtle) throw new Error('SubtleCrypto API not available');
|
|
32
|
+
const hashBuffer = await subtle.digest('SHA-1', new TextEncoder().encode(text));
|
|
33
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
34
|
+
return hashArray.map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
40
35
|
}
|
|
41
36
|
exports.hashSHA1 = __webpack_exports__.hashSHA1;
|
|
42
37
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import { getNodeCrypto, getWebCrypto } from "./crypto-helpers.mjs";
|
|
2
1
|
async function hashSHA1(text) {
|
|
3
|
-
const
|
|
4
|
-
if (
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
9
|
-
return hashArray.map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
10
|
-
}
|
|
11
|
-
throw new Error('No crypto implementation available. Tried Node Crypto API and Web SubtleCrypto API');
|
|
2
|
+
const subtle = globalThis.crypto?.subtle;
|
|
3
|
+
if (!subtle) throw new Error('SubtleCrypto API not available');
|
|
4
|
+
const hashBuffer = await subtle.digest('SHA-1', new TextEncoder().encode(text));
|
|
5
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
6
|
+
return hashArray.map((byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
12
7
|
}
|
|
13
8
|
export { hashSHA1 };
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "5.9.
|
|
1
|
+
export declare const version = "5.9.3";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
|
@@ -26,7 +26,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
version: ()=>version
|
|
28
28
|
});
|
|
29
|
-
const version = '5.9.
|
|
29
|
+
const version = '5.9.3';
|
|
30
30
|
exports.version = __webpack_exports__.version;
|
|
31
31
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
32
32
|
"version"
|
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = '5.9.
|
|
1
|
+
const version = '5.9.3';
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "5.9.
|
|
3
|
+
"version": "5.9.3",
|
|
4
4
|
"description": "PostHog Node.js integration",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"module": "dist/entrypoints/index.node.mjs",
|
|
26
26
|
"types": "dist/entrypoints/index.node.d.ts",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@posthog/core": "1.2.
|
|
28
|
+
"@posthog/core": "1.2.2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^20.0.0",
|
|
@@ -75,7 +75,6 @@
|
|
|
75
75
|
"build": "rslib build",
|
|
76
76
|
"dev": "rslib build -w",
|
|
77
77
|
"package": "pnpm pack --out $PACKAGE_DEST/%s.tgz",
|
|
78
|
-
"
|
|
79
|
-
"parse-specs": "node scripts/generate-docs.mjs"
|
|
78
|
+
"generate-references": "pnpm build && pnpm exec api-extractor run --config ./api-extractor.jsonc --local && node scripts/generate-docs.mjs"
|
|
80
79
|
}
|
|
81
80
|
}
|
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
2
|
|
|
3
|
-
import { getNodeCrypto, getWebCrypto } from './crypto-helpers'
|
|
4
|
-
|
|
5
3
|
export async function hashSHA1(text: string): Promise<string> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return nodeCrypto.createHash('sha1').update(text).digest('hex')
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const webCrypto = await getWebCrypto()
|
|
13
|
-
|
|
14
|
-
// Fall back to Web Crypto API
|
|
15
|
-
if (webCrypto) {
|
|
16
|
-
const hashBuffer = await webCrypto.digest('SHA-1', new TextEncoder().encode(text))
|
|
17
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
18
|
-
return hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
4
|
+
const subtle = globalThis.crypto?.subtle
|
|
5
|
+
if (!subtle) {
|
|
6
|
+
throw new Error('SubtleCrypto API not available')
|
|
19
7
|
}
|
|
20
8
|
|
|
21
|
-
|
|
9
|
+
const hashBuffer = await subtle.digest('SHA-1', new TextEncoder().encode(text))
|
|
10
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
|
11
|
+
return hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
22
12
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.9.
|
|
1
|
+
export const version = '5.9.3'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-helpers.d.ts","sourceRoot":"","sources":["../../../src/extensions/feature-flags/crypto-helpers.ts"],"names":[],"mappings":"AAWA,wBAAsB,aAAa,IAAI,OAAO,CAAC,cAAc,QAAQ,CAAC,GAAG,SAAS,CAAC,CAElF;AAoBD,wBAAsB,YAAY,IAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC,CAEtE"}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_modules__ = {
|
|
3
|
-
crypto: function(module) {
|
|
4
|
-
module.exports = import("crypto").then(function(module) {
|
|
5
|
-
return module;
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
var __webpack_module_cache__ = {};
|
|
10
|
-
function __webpack_require__(moduleId) {
|
|
11
|
-
var cachedModule = __webpack_module_cache__[moduleId];
|
|
12
|
-
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
13
|
-
var module = __webpack_module_cache__[moduleId] = {
|
|
14
|
-
exports: {}
|
|
15
|
-
};
|
|
16
|
-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
17
|
-
return module.exports;
|
|
18
|
-
}
|
|
19
|
-
(()=>{
|
|
20
|
-
__webpack_require__.d = (exports1, definition)=>{
|
|
21
|
-
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: definition[key]
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
})();
|
|
27
|
-
(()=>{
|
|
28
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
29
|
-
})();
|
|
30
|
-
(()=>{
|
|
31
|
-
__webpack_require__.r = (exports1)=>{
|
|
32
|
-
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
33
|
-
value: 'Module'
|
|
34
|
-
});
|
|
35
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
36
|
-
value: true
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
})();
|
|
40
|
-
var __webpack_exports__ = {};
|
|
41
|
-
(()=>{
|
|
42
|
-
__webpack_require__.r(__webpack_exports__);
|
|
43
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
44
|
-
getNodeCrypto: ()=>getNodeCrypto,
|
|
45
|
-
getWebCrypto: ()=>getWebCrypto
|
|
46
|
-
});
|
|
47
|
-
const external_lazy_js_namespaceObject = require("./lazy.js");
|
|
48
|
-
const nodeCrypto = new external_lazy_js_namespaceObject.Lazy(async ()=>{
|
|
49
|
-
try {
|
|
50
|
-
return await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "crypto"));
|
|
51
|
-
} catch {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
async function getNodeCrypto() {
|
|
56
|
-
return await nodeCrypto.getValue();
|
|
57
|
-
}
|
|
58
|
-
const webCrypto = new external_lazy_js_namespaceObject.Lazy(async ()=>{
|
|
59
|
-
if (void 0 !== globalThis.crypto?.subtle) return globalThis.crypto.subtle;
|
|
60
|
-
try {
|
|
61
|
-
const crypto = await nodeCrypto.getValue();
|
|
62
|
-
if (crypto?.webcrypto?.subtle) return crypto.webcrypto.subtle;
|
|
63
|
-
} catch {}
|
|
64
|
-
});
|
|
65
|
-
async function getWebCrypto() {
|
|
66
|
-
return await webCrypto.getValue();
|
|
67
|
-
}
|
|
68
|
-
})();
|
|
69
|
-
exports.getNodeCrypto = __webpack_exports__.getNodeCrypto;
|
|
70
|
-
exports.getWebCrypto = __webpack_exports__.getWebCrypto;
|
|
71
|
-
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
72
|
-
"getNodeCrypto",
|
|
73
|
-
"getWebCrypto"
|
|
74
|
-
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
75
|
-
Object.defineProperty(exports, '__esModule', {
|
|
76
|
-
value: true
|
|
77
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Lazy } from "./lazy.mjs";
|
|
2
|
-
const nodeCrypto = new Lazy(async ()=>{
|
|
3
|
-
try {
|
|
4
|
-
return await import("crypto");
|
|
5
|
-
} catch {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
});
|
|
9
|
-
async function getNodeCrypto() {
|
|
10
|
-
return await nodeCrypto.getValue();
|
|
11
|
-
}
|
|
12
|
-
const webCrypto = new Lazy(async ()=>{
|
|
13
|
-
if (void 0 !== globalThis.crypto?.subtle) return globalThis.crypto.subtle;
|
|
14
|
-
try {
|
|
15
|
-
const crypto = await nodeCrypto.getValue();
|
|
16
|
-
if (crypto?.webcrypto?.subtle) return crypto.webcrypto.subtle;
|
|
17
|
-
} catch {}
|
|
18
|
-
});
|
|
19
|
-
async function getWebCrypto() {
|
|
20
|
-
return await webCrypto.getValue();
|
|
21
|
-
}
|
|
22
|
-
export { getNodeCrypto, getWebCrypto };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A lazy value that is only computed when needed. Inspired by C#'s Lazy<T> class.
|
|
3
|
-
*/
|
|
4
|
-
export declare class Lazy<T> {
|
|
5
|
-
private value;
|
|
6
|
-
private factory;
|
|
7
|
-
private initializationPromise;
|
|
8
|
-
constructor(factory: () => Promise<T>);
|
|
9
|
-
/**
|
|
10
|
-
* Gets the value, initializing it if necessary.
|
|
11
|
-
* Multiple concurrent calls will share the same initialization promise.
|
|
12
|
-
*/
|
|
13
|
-
getValue(): Promise<T>;
|
|
14
|
-
/**
|
|
15
|
-
* Returns true if the value has been initialized.
|
|
16
|
-
*/
|
|
17
|
-
isInitialized(): boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Returns a promise that resolves when the value is initialized.
|
|
20
|
-
* If already initialized, resolves immediately.
|
|
21
|
-
*/
|
|
22
|
-
waitForInitialization(): Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../../../src/extensions/feature-flags/lazy.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,IAAI,CAAC,CAAC;IACjB,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,qBAAqB,CAAwB;gBAEzC,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAIrC;;;OAGG;IACG,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC;IAqB5B;;OAEG;IACH,aAAa,IAAI,OAAO;IAIxB;;;OAGG;IACG,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7C"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
-
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: definition[key]
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
})();
|
|
11
|
-
(()=>{
|
|
12
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
-
})();
|
|
14
|
-
(()=>{
|
|
15
|
-
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
-
value: 'Module'
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
-
value: true
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
})();
|
|
24
|
-
var __webpack_exports__ = {};
|
|
25
|
-
__webpack_require__.r(__webpack_exports__);
|
|
26
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
-
Lazy: ()=>Lazy
|
|
28
|
-
});
|
|
29
|
-
class Lazy {
|
|
30
|
-
constructor(factory){
|
|
31
|
-
this.factory = factory;
|
|
32
|
-
}
|
|
33
|
-
async getValue() {
|
|
34
|
-
if (void 0 !== this.value) return this.value;
|
|
35
|
-
if (void 0 === this.initializationPromise) this.initializationPromise = (async ()=>{
|
|
36
|
-
try {
|
|
37
|
-
const result = await this.factory();
|
|
38
|
-
this.value = result;
|
|
39
|
-
return result;
|
|
40
|
-
} finally{
|
|
41
|
-
this.initializationPromise = void 0;
|
|
42
|
-
}
|
|
43
|
-
})();
|
|
44
|
-
return this.initializationPromise;
|
|
45
|
-
}
|
|
46
|
-
isInitialized() {
|
|
47
|
-
return void 0 !== this.value;
|
|
48
|
-
}
|
|
49
|
-
async waitForInitialization() {
|
|
50
|
-
if (this.isInitialized()) return;
|
|
51
|
-
await this.getValue();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.Lazy = __webpack_exports__.Lazy;
|
|
55
|
-
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
56
|
-
"Lazy"
|
|
57
|
-
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
58
|
-
Object.defineProperty(exports, '__esModule', {
|
|
59
|
-
value: true
|
|
60
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
class Lazy {
|
|
2
|
-
constructor(factory){
|
|
3
|
-
this.factory = factory;
|
|
4
|
-
}
|
|
5
|
-
async getValue() {
|
|
6
|
-
if (void 0 !== this.value) return this.value;
|
|
7
|
-
if (void 0 === this.initializationPromise) this.initializationPromise = (async ()=>{
|
|
8
|
-
try {
|
|
9
|
-
const result = await this.factory();
|
|
10
|
-
this.value = result;
|
|
11
|
-
return result;
|
|
12
|
-
} finally{
|
|
13
|
-
this.initializationPromise = void 0;
|
|
14
|
-
}
|
|
15
|
-
})();
|
|
16
|
-
return this.initializationPromise;
|
|
17
|
-
}
|
|
18
|
-
isInitialized() {
|
|
19
|
-
return void 0 !== this.value;
|
|
20
|
-
}
|
|
21
|
-
async waitForInitialization() {
|
|
22
|
-
if (this.isInitialized()) return;
|
|
23
|
-
await this.getValue();
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export { Lazy };
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/// <reference lib="dom" />
|
|
2
|
-
import { Lazy } from './lazy'
|
|
3
|
-
|
|
4
|
-
const nodeCrypto = new Lazy(async () => {
|
|
5
|
-
try {
|
|
6
|
-
return await import('crypto')
|
|
7
|
-
} catch {
|
|
8
|
-
return undefined
|
|
9
|
-
}
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
export async function getNodeCrypto(): Promise<typeof import('crypto') | undefined> {
|
|
13
|
-
return await nodeCrypto.getValue()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const webCrypto = new Lazy(async (): Promise<SubtleCrypto | undefined> => {
|
|
17
|
-
if (typeof globalThis.crypto?.subtle !== 'undefined') {
|
|
18
|
-
return globalThis.crypto.subtle
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
// Node.js: use built-in webcrypto and assign it if needed
|
|
23
|
-
const crypto = await nodeCrypto.getValue()
|
|
24
|
-
if (crypto?.webcrypto?.subtle) {
|
|
25
|
-
return crypto.webcrypto.subtle as SubtleCrypto
|
|
26
|
-
}
|
|
27
|
-
} catch {
|
|
28
|
-
// Ignore if not available
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return undefined
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
export async function getWebCrypto(): Promise<SubtleCrypto | undefined> {
|
|
35
|
-
return await webCrypto.getValue()
|
|
36
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A lazy value that is only computed when needed. Inspired by C#'s Lazy<T> class.
|
|
3
|
-
*/
|
|
4
|
-
export class Lazy<T> {
|
|
5
|
-
private value: T | undefined
|
|
6
|
-
private factory: () => Promise<T>
|
|
7
|
-
private initializationPromise: Promise<T> | undefined
|
|
8
|
-
|
|
9
|
-
constructor(factory: () => Promise<T>) {
|
|
10
|
-
this.factory = factory
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Gets the value, initializing it if necessary.
|
|
15
|
-
* Multiple concurrent calls will share the same initialization promise.
|
|
16
|
-
*/
|
|
17
|
-
async getValue(): Promise<T> {
|
|
18
|
-
if (this.value !== undefined) {
|
|
19
|
-
return this.value
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (this.initializationPromise === undefined) {
|
|
23
|
-
this.initializationPromise = (async () => {
|
|
24
|
-
try {
|
|
25
|
-
const result = await this.factory()
|
|
26
|
-
this.value = result
|
|
27
|
-
return result
|
|
28
|
-
} finally {
|
|
29
|
-
// Clear the promise so we can retry if needed
|
|
30
|
-
this.initializationPromise = undefined
|
|
31
|
-
}
|
|
32
|
-
})()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return this.initializationPromise
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns true if the value has been initialized.
|
|
40
|
-
*/
|
|
41
|
-
isInitialized(): boolean {
|
|
42
|
-
return this.value !== undefined
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Returns a promise that resolves when the value is initialized.
|
|
47
|
-
* If already initialized, resolves immediately.
|
|
48
|
-
*/
|
|
49
|
-
async waitForInitialization(): Promise<void> {
|
|
50
|
-
if (this.isInitialized()) {
|
|
51
|
-
return
|
|
52
|
-
}
|
|
53
|
-
await this.getValue()
|
|
54
|
-
}
|
|
55
|
-
}
|