@posthog/browser 0.0.0 → 0.1.1

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.
Files changed (121) hide show
  1. package/ARCHITECTURE.md +559 -0
  2. package/LICENSE +417 -0
  3. package/README.md +286 -28
  4. package/dist/analytics-buffer.d.ts +4 -0
  5. package/dist/analytics-buffer.js +200 -0
  6. package/dist/analytics-buffer.mjs +162 -0
  7. package/dist/analytics-delivery.d.ts +3 -0
  8. package/dist/analytics-delivery.js +231 -0
  9. package/dist/analytics-delivery.mjs +193 -0
  10. package/dist/analytics-internal.d.ts +64 -0
  11. package/dist/analytics-internal.js +52 -0
  12. package/dist/analytics-internal.mjs +5 -0
  13. package/dist/analytics-options.d.ts +14 -0
  14. package/dist/analytics-options.js +18 -0
  15. package/dist/analytics-options.mjs +1 -0
  16. package/dist/analytics.d.ts +5 -0
  17. package/dist/analytics.js +42 -0
  18. package/dist/analytics.mjs +4 -0
  19. package/dist/autocapture-options.d.ts +32 -0
  20. package/dist/autocapture-options.js +49 -0
  21. package/dist/autocapture-options.mjs +11 -0
  22. package/dist/autocapture.d.ts +5 -0
  23. package/dist/autocapture.js +108 -0
  24. package/dist/autocapture.mjs +70 -0
  25. package/dist/automatic-analytics.d.ts +3 -0
  26. package/dist/automatic-analytics.js +44 -0
  27. package/dist/automatic-analytics.mjs +6 -0
  28. package/dist/automatic-surveys.d.ts +2 -0
  29. package/dist/automatic-surveys.js +41 -0
  30. package/dist/automatic-surveys.mjs +3 -0
  31. package/dist/bot-filter.d.ts +2 -0
  32. package/dist/bot-filter.js +87 -0
  33. package/dist/bot-filter.mjs +49 -0
  34. package/dist/browser-client.d.ts +30 -0
  35. package/dist/browser-client.js +18 -0
  36. package/dist/browser-client.mjs +1 -0
  37. package/dist/capture-summary.d.ts +3 -0
  38. package/dist/capture-summary.js +63 -0
  39. package/dist/capture-summary.mjs +22 -0
  40. package/dist/capture-v1.d.ts +105 -0
  41. package/dist/capture-v1.js +781 -0
  42. package/dist/capture-v1.mjs +722 -0
  43. package/dist/core.d.ts +7 -0
  44. package/dist/core.js +49 -0
  45. package/dist/core.mjs +5 -0
  46. package/dist/event-buffer.d.ts +41 -0
  47. package/dist/event-buffer.js +158 -0
  48. package/dist/event-buffer.mjs +120 -0
  49. package/dist/extensions/registry.d.ts +16 -0
  50. package/dist/extensions/registry.js +128 -0
  51. package/dist/extensions/registry.mjs +90 -0
  52. package/dist/flags-internal.d.ts +8 -0
  53. package/dist/flags-internal.js +18 -0
  54. package/dist/flags-internal.mjs +1 -0
  55. package/dist/flags-options.d.ts +22 -0
  56. package/dist/flags-options.js +18 -0
  57. package/dist/flags-options.mjs +1 -0
  58. package/dist/flags-token.d.ts +15 -0
  59. package/dist/flags-token.js +40 -0
  60. package/dist/flags-token.mjs +2 -0
  61. package/dist/flags.d.ts +6 -0
  62. package/dist/flags.js +164 -0
  63. package/dist/flags.mjs +123 -0
  64. package/dist/id.d.ts +1 -0
  65. package/dist/id.js +61 -0
  66. package/dist/id.mjs +23 -0
  67. package/dist/index.d.ts +11 -0
  68. package/dist/index.js +119 -0
  69. package/dist/index.mjs +75 -0
  70. package/dist/lane.d.ts +57 -0
  71. package/dist/lane.js +363 -0
  72. package/dist/lane.mjs +325 -0
  73. package/dist/logger.d.ts +4 -0
  74. package/dist/logger.js +57 -0
  75. package/dist/logger.mjs +19 -0
  76. package/dist/logs-internal.d.ts +7 -0
  77. package/dist/logs-internal.js +18 -0
  78. package/dist/logs-internal.mjs +1 -0
  79. package/dist/logs-options.d.ts +11 -0
  80. package/dist/logs-options.js +57 -0
  81. package/dist/logs-options.mjs +19 -0
  82. package/dist/logs.d.ts +6 -0
  83. package/dist/logs.js +140 -0
  84. package/dist/logs.mjs +102 -0
  85. package/dist/named-error.d.ts +7 -0
  86. package/dist/named-error.js +49 -0
  87. package/dist/named-error.mjs +11 -0
  88. package/dist/posthog.d.ts +3 -0
  89. package/dist/posthog.js +829 -0
  90. package/dist/posthog.mjs +791 -0
  91. package/dist/rate-limiter.d.ts +24 -0
  92. package/dist/rate-limiter.js +95 -0
  93. package/dist/rate-limiter.mjs +57 -0
  94. package/dist/remote-config.d.ts +3 -0
  95. package/dist/remote-config.js +47 -0
  96. package/dist/remote-config.mjs +9 -0
  97. package/dist/request.d.ts +14 -0
  98. package/dist/request.js +130 -0
  99. package/dist/request.mjs +89 -0
  100. package/dist/state.d.ts +82 -0
  101. package/dist/state.js +625 -0
  102. package/dist/state.mjs +581 -0
  103. package/dist/surveys-extension.d.ts +9 -0
  104. package/dist/surveys-extension.js +272 -0
  105. package/dist/surveys-extension.mjs +234 -0
  106. package/dist/surveys-internal.d.ts +12 -0
  107. package/dist/surveys-internal.js +18 -0
  108. package/dist/surveys-internal.mjs +1 -0
  109. package/dist/surveys-options.d.ts +18 -0
  110. package/dist/surveys-options.js +42 -0
  111. package/dist/surveys-options.mjs +4 -0
  112. package/dist/surveys.d.ts +6 -0
  113. package/dist/surveys.js +45 -0
  114. package/dist/surveys.mjs +4 -0
  115. package/dist/types.d.ts +114 -0
  116. package/dist/types.js +18 -0
  117. package/dist/types.mjs +1 -0
  118. package/dist/version.d.ts +1 -0
  119. package/dist/version.js +40 -0
  120. package/dist/version.mjs +2 -0
  121. package/package.json +89 -8
package/dist/logs.mjs ADDED
@@ -0,0 +1,102 @@
1
+ import { sanitizeUrl } from "@posthog/browser-common/utils/sanitize-url";
2
+ import { PostHogLogs } from "@posthog/browser-common/logs";
3
+ import { initializeLogs, replayConsoleBuffer } from "@posthog/browser-common/console-logs";
4
+ import { FeatureFlagsCommonExtension } from "@posthog/browser-common/extension-tokens";
5
+ import { snapshotLogsOptions } from "./logs-options.mjs";
6
+ const readSdkContext = (environment, config)=>{
7
+ const client = environment.client;
8
+ const session = client.session;
9
+ const distinctId = client.distinctId;
10
+ let currentUrl;
11
+ try {
12
+ const href = environment.browserWindow?.location?.href;
13
+ if (href) currentUrl = sanitizeUrl(href, config.urlCapture ?? {});
14
+ } catch {}
15
+ const keys = client.getExtension(FeatureFlagsCommonExtension)?.getFlags();
16
+ return {
17
+ distinctId,
18
+ ...session?.sessionId ? session : {},
19
+ ...currentUrl ? {
20
+ currentUrl
21
+ } : {},
22
+ ...keys?.length ? {
23
+ activeFeatureFlags: keys
24
+ } : {}
25
+ };
26
+ };
27
+ class BrowserNextLogs extends PostHogLogs {
28
+ constructor(config, _environment){
29
+ super({
30
+ get: ()=>config,
31
+ captureHintKey: 'consoleCaptureEnabled',
32
+ remoteConfigWillArrive: true
33
+ }, ()=>readSdkContext(_environment, config)), this._environment = _environment;
34
+ }
35
+ _getConsoleLoader() {
36
+ const browserWindow = this._environment.browserWindow;
37
+ const console = browserWindow?.console;
38
+ if (!console) return;
39
+ const consoleHost = {
40
+ console,
41
+ hostname: browserWindow?.location?.host ?? '',
42
+ getCapturingLogs: ()=>!this._environment.disposed && this._environment.client?.canCapture ? this : void 0
43
+ };
44
+ return (callback)=>callback(void 0, {
45
+ initialize: ()=>initializeLogs(consoleHost),
46
+ replay: (_client, entries)=>replayConsoleBuffer(consoleHost, entries)
47
+ });
48
+ }
49
+ }
50
+ const logs = (options = {})=>{
51
+ const environment = {
52
+ client: void 0,
53
+ disposed: false
54
+ };
55
+ const subscriptions = [];
56
+ const shared = new BrowserNextLogs(snapshotLogsOptions(options), environment);
57
+ const pagehide = ()=>{
58
+ if (!environment.disposed) shared.flushLogs('sendBeacon');
59
+ };
60
+ return {
61
+ name: 'logs',
62
+ setup: async (value)=>{
63
+ environment.client = value;
64
+ try {
65
+ environment.browserWindow = globalThis.window;
66
+ } catch {}
67
+ subscriptions.push(value.onReset(()=>shared.reset()), value.onConsentChange(({ current })=>{
68
+ if ('denied' === current) shared.reset();
69
+ }));
70
+ await shared.setup(value);
71
+ if (environment.disposed) return;
72
+ environment.browserWindow?.addEventListener('pagehide', pagehide);
73
+ },
74
+ captureLog: (record)=>{
75
+ if (environment.disposed) return;
76
+ try {
77
+ if (environment.client?.canCapture) shared.captureLog(record);
78
+ } catch (error) {
79
+ environment.client?.logger.error('Log capture failed', error);
80
+ }
81
+ },
82
+ flush: async ()=>{
83
+ if (!environment.disposed) await shared.flush();
84
+ },
85
+ reset: ()=>shared.reset(),
86
+ dispose: ()=>{
87
+ environment.disposed = true;
88
+ subscriptions.splice(0).forEach((subscription)=>subscription.dispose());
89
+ try {
90
+ environment.browserWindow?.removeEventListener('pagehide', pagehide);
91
+ } finally{
92
+ try {
93
+ shared.reset();
94
+ } finally{
95
+ shared.dispose();
96
+ environment.client = void 0;
97
+ }
98
+ }
99
+ }
100
+ };
101
+ };
102
+ export { logs };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Sets `error.name` in a way that also holds on pages where a browser extension has made
3
+ * `Error.prototype.name` non-writable, where a plain `error.name = ...` throws in strict mode.
4
+ * An own property on the instance shadows the prototype property. The descriptor matches what
5
+ * a plain assignment produces, so the error is unchanged everywhere else.
6
+ */
7
+ export declare const defineErrorName: (error: Error, name: string) => void;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ const defineErrorName = (error, name)=>{
31
+ try {
32
+ Object.defineProperty(error, 'name', {
33
+ value: name,
34
+ writable: true,
35
+ enumerable: true,
36
+ configurable: true
37
+ });
38
+ } catch {}
39
+ };
40
+ __webpack_require__.d(__webpack_exports__, {}, {
41
+ defineErrorName: defineErrorName
42
+ });
43
+ exports.defineErrorName = __webpack_exports__.defineErrorName;
44
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
45
+ "defineErrorName"
46
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
47
+ Object.defineProperty(exports, '__esModule', {
48
+ value: true
49
+ });
@@ -0,0 +1,11 @@
1
+ const defineErrorName = (error, name)=>{
2
+ try {
3
+ Object.defineProperty(error, 'name', {
4
+ value: name,
5
+ writable: true,
6
+ enumerable: true,
7
+ configurable: true
8
+ });
9
+ } catch {}
10
+ };
11
+ export { defineErrorName };
@@ -0,0 +1,3 @@
1
+ import { type Extension } from '@posthog/browser-common';
2
+ import type { CorePostHogOptions, PostHog } from './types';
3
+ export declare const createPostHogCore: (options: CorePostHogOptions, configured?: readonly Extension[]) => Promise<PostHog>;