@posthog/browser 0.4.1 → 0.5.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/ARCHITECTURE.md +34 -46
- package/README.md +56 -63
- package/dist/composition/npm/core.d.ts +2 -3
- package/dist/composition/npm/core.js +2 -1
- package/dist/composition/npm/core.mjs +2 -1
- package/dist/composition/npm/index.d.ts +3 -4
- package/dist/composition/npm/index.js +2 -1
- package/dist/composition/npm/index.mjs +2 -1
- package/dist/composition/npm/root-composition.d.ts +4 -4
- package/dist/composition/npm/root-composition.js +0 -16
- package/dist/composition/npm/root-composition.mjs +0 -16
- package/dist/core/context/browser-client.d.ts +4 -35
- package/dist/core/extensions/registry.d.ts +15 -36
- package/dist/core/extensions/registry.js +67 -198
- package/dist/core/extensions/registry.mjs +67 -198
- package/dist/core/posthog.d.ts +3 -2
- package/dist/core/posthog.js +104 -161
- package/dist/core/posthog.mjs +105 -162
- package/dist/core/state/state.d.ts +1 -0
- package/dist/core/state/state.js +7 -0
- package/dist/core/state/state.mjs +5 -1
- package/dist/core/types.d.ts +50 -12
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +76 -0
- package/dist/products/analytics/analytics-core.js +472 -0
- package/dist/products/analytics/analytics-core.mjs +425 -0
- package/dist/products/analytics/analytics-internal.d.ts +14 -36
- package/dist/products/analytics/analytics-internal.js +2 -10
- package/dist/products/analytics/analytics-internal.mjs +1 -3
- package/dist/products/analytics/analytics-options.d.ts +0 -9
- package/dist/products/analytics/analytics-options.js +1 -38
- package/dist/products/analytics/analytics-options.mjs +1 -17
- package/dist/products/analytics/groups.d.ts +8 -0
- package/dist/products/analytics/groups.js +84 -0
- package/dist/products/analytics/groups.mjs +40 -0
- package/dist/products/analytics/person.d.ts +11 -0
- package/dist/products/analytics/person.js +91 -0
- package/dist/products/analytics/person.mjs +41 -0
- package/dist/products/autocapture/autocapture.js +2 -6
- package/dist/products/autocapture/autocapture.mjs +2 -6
- package/dist/products/flags/flags-internal.d.ts +1 -2
- package/dist/products/flags/flags-token.d.ts +3 -2
- package/dist/products/flags/flags.js +2 -7
- package/dist/products/flags/flags.mjs +2 -7
- package/dist/products/logs/logs.js +2 -5
- package/dist/products/logs/logs.mjs +2 -5
- package/dist/products/surveys/surveys-extension.js +4 -21
- package/dist/products/surveys/surveys-extension.mjs +4 -21
- package/package.json +3 -8
- package/dist/products/analytics/analytics-api.d.ts +0 -32
- package/dist/products/analytics/analytics-api.js +0 -18
- package/dist/products/analytics/analytics-api.mjs +0 -1
- package/dist/products/analytics/analytics-buffer.d.ts +0 -4
- package/dist/products/analytics/analytics-buffer.js +0 -41
- package/dist/products/analytics/analytics-buffer.mjs +0 -3
- package/dist/products/analytics/analytics-extension.d.ts +0 -87
- package/dist/products/analytics/analytics-extension.js +0 -649
- package/dist/products/analytics/analytics-extension.mjs +0 -611
- package/dist/products/analytics/analytics.d.ts +0 -6
- package/dist/products/analytics/analytics.js +0 -42
- package/dist/products/analytics/analytics.mjs +0 -4
- package/dist/products/analytics/automatic-analytics.d.ts +0 -3
- package/dist/products/analytics/automatic-analytics.js +0 -44
- package/dist/products/analytics/automatic-analytics.mjs +0 -6
|
@@ -1,137 +1,89 @@
|
|
|
1
1
|
class ExtensionRegistry {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
2
|
+
constructor(_host, _logger, _loaders = {}, _canInstall = ()=>true){
|
|
3
|
+
this._host = _host;
|
|
4
4
|
this._logger = _logger;
|
|
5
5
|
this._loaders = _loaders;
|
|
6
|
-
this._setup = _setup;
|
|
7
6
|
this._canInstall = _canInstall;
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this._sharedReservations = new Set();
|
|
7
|
+
this._installed = new Map();
|
|
8
|
+
this._settingUp = new Set();
|
|
9
|
+
this._lookup = new Map();
|
|
10
|
+
this._reserved = new Set();
|
|
13
11
|
this._pending = new Map();
|
|
14
|
-
this.
|
|
15
|
-
this._views = new Set();
|
|
16
|
-
this._clients = new Map();
|
|
17
|
-
this._publications = new WeakMap();
|
|
12
|
+
this._retired = new WeakSet();
|
|
18
13
|
this._disposed = false;
|
|
19
14
|
}
|
|
20
|
-
expose(target) {
|
|
21
|
-
if (this._disposed || this._views.has(target)) return;
|
|
22
|
-
const publications = new Map();
|
|
23
|
-
this._publications.set(target, publications);
|
|
24
|
-
try {
|
|
25
|
-
for (const extension of this._extensions.values())this._publish(target, extension.name);
|
|
26
|
-
} catch (error) {
|
|
27
|
-
for (const name of publications.keys())this._removePublication(target, name);
|
|
28
|
-
this._publications.delete(target);
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
this._views.add(target);
|
|
32
|
-
}
|
|
33
15
|
get(name) {
|
|
34
|
-
return this.
|
|
35
|
-
}
|
|
36
|
-
getShared(name) {
|
|
37
|
-
return this._shared.get(name);
|
|
16
|
+
return this._lookup.get(name);
|
|
38
17
|
}
|
|
39
18
|
resolve(name) {
|
|
40
19
|
if (this._disposed || !this._canInstall()) return Promise.resolve(void 0);
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
20
|
+
const installed = this._installed.get(name);
|
|
21
|
+
if (installed) return Promise.resolve(installed);
|
|
22
|
+
const pending = this._pending.get(name);
|
|
23
|
+
if (pending) return pending;
|
|
43
24
|
const load = Object.hasOwn(this._loaders, name) ? this._loaders[name] : void 0;
|
|
44
25
|
if (!load) return Promise.resolve(void 0);
|
|
45
|
-
const loading = Promise.resolve().then(
|
|
26
|
+
const loading = Promise.resolve().then(load).then(async (extension)=>{
|
|
27
|
+
if (this._disposed || !this._canInstall()) return void await extension.dispose?.();
|
|
28
|
+
if (extension.name !== name) {
|
|
29
|
+
await extension.dispose?.();
|
|
30
|
+
throw new Error(`Loader for "${name}" returned extension "${extension.name}"`);
|
|
31
|
+
}
|
|
32
|
+
await this.install(extension);
|
|
33
|
+
return this._installed.get(name);
|
|
34
|
+
}).catch((error)=>{
|
|
35
|
+
this._logger.error(`Extension "${name}" loading failed`, error);
|
|
36
|
+
}).finally(()=>this._pending.delete(name));
|
|
46
37
|
this._pending.set(name, loading);
|
|
47
|
-
loading.then(()=>{
|
|
48
|
-
if (this._pending.get(name) === loading) this._pending.delete(name);
|
|
49
|
-
});
|
|
50
38
|
return loading;
|
|
51
39
|
}
|
|
52
40
|
async activate({ name, activate }) {
|
|
53
|
-
if (this._disposed || !this._canInstall() || this.
|
|
41
|
+
if (this._disposed || !this._canInstall() || this._installed.has(name)) return;
|
|
54
42
|
try {
|
|
55
|
-
|
|
56
|
-
this._assertNameAvailable(name);
|
|
57
|
-
await activate(host, ()=>this.resolve(name));
|
|
43
|
+
await activate(this._host, ()=>this.resolve(name));
|
|
58
44
|
} catch (error) {
|
|
59
45
|
this._logger.error(`Extension "${name}" activation failed`, error);
|
|
60
46
|
}
|
|
61
47
|
}
|
|
62
48
|
async install(extension) {
|
|
63
|
-
|
|
64
|
-
|
|
49
|
+
if (this._disposed) throw new Error('The extension registry is disposed');
|
|
50
|
+
if (this._retired.has(extension)) throw new Error('The extension instance has already been disposed');
|
|
51
|
+
const names = [
|
|
52
|
+
extension.name,
|
|
53
|
+
...Object.keys(extension.bindings ?? {})
|
|
54
|
+
];
|
|
55
|
+
for (const key of names)if (!key || this._lookup.has(key) || this._reserved.has(key) || key in this._host) throw new Error(`Extension name "${key}" is invalid or already in use`);
|
|
56
|
+
for (const key of names)this._reserved.add(key);
|
|
57
|
+
this._settingUp.add(extension);
|
|
65
58
|
try {
|
|
66
|
-
|
|
67
|
-
this.
|
|
68
|
-
await this._setup(extension, host);
|
|
69
|
-
this._publishInstallation(installation);
|
|
59
|
+
await extension.setup(this._host);
|
|
60
|
+
if (this._disposed || !this._canInstall()) throw new Error('Extension installation stopped during setup');
|
|
70
61
|
} catch (error) {
|
|
71
|
-
this.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
62
|
+
if (!this._retired.has(extension)) {
|
|
63
|
+
this._retired.add(extension);
|
|
64
|
+
try {
|
|
65
|
+
await extension.dispose?.();
|
|
66
|
+
} catch (disposeError) {
|
|
67
|
+
this._logger.error('Extension cleanup failed after setup failed', disposeError);
|
|
68
|
+
}
|
|
77
69
|
}
|
|
78
70
|
throw error;
|
|
79
71
|
} finally{
|
|
80
|
-
this.
|
|
72
|
+
this._settingUp.delete(extension);
|
|
73
|
+
for (const key of names)this._reserved.delete(key);
|
|
81
74
|
}
|
|
82
|
-
}
|
|
83
|
-
_existingResolution(name) {
|
|
84
|
-
const installed = this.get(name);
|
|
85
|
-
if (installed) return Promise.resolve(installed);
|
|
86
|
-
return this._pending.get(name) ?? this._records.get(name)?.ready;
|
|
87
|
-
}
|
|
88
|
-
_reserveInstallation(extension) {
|
|
89
|
-
if (this._disposed) throw new Error('The extension registry is disposed');
|
|
90
|
-
if (this._retired.has(extension)) throw new Error('The extension instance has already been disposed');
|
|
91
75
|
const { name } = extension;
|
|
92
|
-
|
|
93
|
-
this.
|
|
94
|
-
const bindings
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
ready: new Promise((resolve)=>{
|
|
101
|
-
finish = resolve;
|
|
102
|
-
}),
|
|
103
|
-
finish: (value)=>finish(value)
|
|
104
|
-
};
|
|
105
|
-
this._records.set(name, record);
|
|
106
|
-
for (const [key] of bindings)this._sharedReservations.add(key);
|
|
107
|
-
return {
|
|
108
|
-
name,
|
|
109
|
-
bindings,
|
|
110
|
-
record
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
_bindingEntries(extension) {
|
|
114
|
-
const bindings = Object.entries(extension.bindings ?? {});
|
|
115
|
-
for (const [key] of bindings)if (!key || this._sharedReservations.has(key)) throw new Error(`A shared extension named "${key}" is already installed or the name is invalid`);
|
|
116
|
-
return bindings;
|
|
117
|
-
}
|
|
118
|
-
_publishInstallation({ name, bindings, record }) {
|
|
119
|
-
if (this._disposed || record.disposed || !this._canInstall()) throw new Error('Extension installation stopped during setup');
|
|
120
|
-
this._extensions.set(name, record.extension);
|
|
121
|
-
for (const view of this._views)this._publish(view, name);
|
|
122
|
-
for (const [key, target] of bindings)this._shared.set(key, target);
|
|
123
|
-
record.finish(record.extension);
|
|
124
|
-
}
|
|
125
|
-
async rollback(extension) {
|
|
126
|
-
const record = this._records.get(extension.name);
|
|
127
|
-
if (record?.extension === extension) {
|
|
128
|
-
this._removeRecord(record);
|
|
129
|
-
record.finish(void 0);
|
|
130
|
-
await this._disposeRecord(record);
|
|
131
|
-
}
|
|
76
|
+
this._installed.set(name, extension);
|
|
77
|
+
this._lookup.set(name, extension);
|
|
78
|
+
for (const [key, target] of Object.entries(extension.bindings ?? {}))this._lookup.set(key, target);
|
|
79
|
+
Object.defineProperty(this._host, name, {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
get: ()=>this._installed.get(name)
|
|
83
|
+
});
|
|
132
84
|
}
|
|
133
85
|
async flush(reason = 'flush') {
|
|
134
|
-
await Promise.all(Array.from(this.
|
|
86
|
+
await Promise.all(Array.from(this._installed.values()).map(async (extension)=>{
|
|
135
87
|
try {
|
|
136
88
|
await extension.flush?.(reason);
|
|
137
89
|
} catch (error) {
|
|
@@ -142,106 +94,23 @@ class ExtensionRegistry {
|
|
|
142
94
|
async dispose() {
|
|
143
95
|
if (this._disposed) return;
|
|
144
96
|
this._disposed = true;
|
|
145
|
-
const
|
|
146
|
-
this.
|
|
147
|
-
|
|
148
|
-
this.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
97
|
+
const installed = Array.from(this._installed.values()).reverse();
|
|
98
|
+
for (const { name } of installed)Reflect.deleteProperty(this._host, name);
|
|
99
|
+
this._installed.clear();
|
|
100
|
+
this._lookup.clear();
|
|
101
|
+
const extensions = [
|
|
102
|
+
...installed,
|
|
103
|
+
...this._settingUp
|
|
104
|
+
];
|
|
105
|
+
await Promise.all(extensions.map(async (extension)=>{
|
|
106
|
+
if (this._retired.has(extension)) return;
|
|
107
|
+
this._retired.add(extension);
|
|
156
108
|
try {
|
|
157
|
-
await
|
|
109
|
+
await extension.dispose?.();
|
|
158
110
|
} catch (error) {
|
|
159
|
-
this._logger.error(`Extension "${
|
|
111
|
+
this._logger.error(`Extension "${extension.name}" cleanup failed`, error);
|
|
160
112
|
}
|
|
161
113
|
}));
|
|
162
114
|
}
|
|
163
|
-
_client(name) {
|
|
164
|
-
const existing = this._clients.get(name);
|
|
165
|
-
if (void 0 !== existing) return existing;
|
|
166
|
-
const host = this._createClient(name, (target)=>this._resolveDependency(name, target));
|
|
167
|
-
if (!this._disposed) this._clients.set(name, host);
|
|
168
|
-
return host;
|
|
169
|
-
}
|
|
170
|
-
async _load(name, load) {
|
|
171
|
-
try {
|
|
172
|
-
const host = this._client(name);
|
|
173
|
-
this._assertNameAvailable(name);
|
|
174
|
-
const extension = await load(host);
|
|
175
|
-
if (this._disposed || extension.name !== name) {
|
|
176
|
-
await extension.dispose?.();
|
|
177
|
-
if (!this._disposed) throw new Error(`Loader for "${name}" returned extension "${extension.name}"`);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
await this.install(extension);
|
|
181
|
-
return this.get(name);
|
|
182
|
-
} catch (error) {
|
|
183
|
-
this._logger.error(`Extension "${name}" loading failed`, error);
|
|
184
|
-
return;
|
|
185
|
-
} finally{
|
|
186
|
-
this._dependencies.delete(name);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
_resolveDependency(owner, target) {
|
|
190
|
-
if (this.get(owner) || this.get(target)) return this.resolve(target);
|
|
191
|
-
if (owner === target || this._reaches(target, owner, new Set())) {
|
|
192
|
-
this._logger.error(`Circular extension dependency: "${owner}" -> "${target}"`, new Error('Circular extension dependency'));
|
|
193
|
-
return Promise.resolve(void 0);
|
|
194
|
-
}
|
|
195
|
-
let dependencies = this._dependencies.get(owner);
|
|
196
|
-
if (!dependencies) this._dependencies.set(owner, dependencies = new Set());
|
|
197
|
-
dependencies.add(target);
|
|
198
|
-
return this.resolve(target);
|
|
199
|
-
}
|
|
200
|
-
_reaches(from, target, visited) {
|
|
201
|
-
if (from === target) return true;
|
|
202
|
-
if (visited.has(from)) return false;
|
|
203
|
-
visited.add(from);
|
|
204
|
-
for (const next of this._dependencies.get(from) ?? [])if (this._reaches(next, target, visited)) return true;
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
_removeRecord(record) {
|
|
208
|
-
const name = record.extension.name;
|
|
209
|
-
if (this._records.get(name) !== record) return;
|
|
210
|
-
this._records.delete(name);
|
|
211
|
-
this._extensions.delete(name);
|
|
212
|
-
this._unpublish(name);
|
|
213
|
-
for (const key of record.sharedNames){
|
|
214
|
-
this._shared.delete(key);
|
|
215
|
-
this._sharedReservations.delete(key);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
_assertNameAvailable(name) {
|
|
219
|
-
for (const view of this._views)if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
|
|
220
|
-
}
|
|
221
|
-
_publish(view, name) {
|
|
222
|
-
if (name in view) throw new Error(`Extension name "${name}" conflicts with a client capability`);
|
|
223
|
-
const read = ()=>this.get(name);
|
|
224
|
-
Object.defineProperty(view, name, {
|
|
225
|
-
enumerable: true,
|
|
226
|
-
configurable: true,
|
|
227
|
-
get: read
|
|
228
|
-
});
|
|
229
|
-
this._publications.get(view)?.set(name, read);
|
|
230
|
-
}
|
|
231
|
-
_removePublication(view, name) {
|
|
232
|
-
const read = this._publications.get(view)?.get(name);
|
|
233
|
-
if (read && Object.getOwnPropertyDescriptor(view, name)?.get === read) Reflect.deleteProperty(view, name);
|
|
234
|
-
this._publications.get(view)?.delete(name);
|
|
235
|
-
}
|
|
236
|
-
_unpublish(name) {
|
|
237
|
-
for (const view of this._views)this._removePublication(view, name);
|
|
238
|
-
}
|
|
239
|
-
async _disposeRecord(record) {
|
|
240
|
-
if (!record.disposed) {
|
|
241
|
-
record.disposed = true;
|
|
242
|
-
this._retired.add(record.extension);
|
|
243
|
-
await record.extension.dispose?.();
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
115
|
}
|
|
247
116
|
export { ExtensionRegistry };
|
package/dist/core/posthog.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BrowserExtension as Extension } from './context/browser-client';
|
|
2
|
+
import type { AnalyticsDeliveryFactory } from '../products/analytics/analytics-internal';
|
|
2
3
|
import { type AutomaticExtension, type ExtensionLoader } from './extensions/registry';
|
|
3
4
|
import type { CorePostHogOptions, IPostHog } from './types';
|
|
4
|
-
export declare const createPostHogCore: (options: CorePostHogOptions, configured?: readonly Extension[], automaticLoaders?: Readonly<Record<string, ExtensionLoader<
|
|
5
|
+
export declare const createPostHogCore: (options: CorePostHogOptions, createDelivery: AnalyticsDeliveryFactory, configured?: readonly Extension[], automaticLoaders?: Readonly<Record<string, ExtensionLoader<IPostHog>>>, automatic?: readonly AutomaticExtension<IPostHog>[]) => Promise<IPostHog>;
|