@openreplay/tracker 12.0.12 → 12.1.0-beta.99

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.
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const QueueSender_js_1 = __importDefault(require("./QueueSender.js"));
7
+ var WorkerStatus;
8
+ (function (WorkerStatus) {
9
+ WorkerStatus[WorkerStatus["NotActive"] = 0] = "NotActive";
10
+ WorkerStatus[WorkerStatus["Starting"] = 1] = "Starting";
11
+ WorkerStatus[WorkerStatus["Stopping"] = 2] = "Stopping";
12
+ WorkerStatus[WorkerStatus["Active"] = 3] = "Active";
13
+ WorkerStatus[WorkerStatus["Stopped"] = 4] = "Stopped";
14
+ })(WorkerStatus || (WorkerStatus = {}));
15
+ const AUTO_SEND_INTERVAL = 10 * 1000;
16
+ const rebroadcastEvents = ['queue_empty', 'not_init', 'restart'];
17
+ class WebWorkerManager {
18
+ constructor(app, worker, onError) {
19
+ this.app = app;
20
+ this.worker = worker;
21
+ this.onError = onError;
22
+ this.sendIntervalID = null;
23
+ this.restartTimeoutID = null;
24
+ this.workerStatus = WorkerStatus.NotActive;
25
+ this.sender = null;
26
+ this.finalize = () => {
27
+ this.worker.postMessage({ type: 'writer_finalize' });
28
+ };
29
+ this.resetWebWorker = () => {
30
+ this.worker.postMessage({ type: 'reset_writer' });
31
+ };
32
+ this.resetSender = () => {
33
+ if (!this.sender) {
34
+ return;
35
+ }
36
+ this.sender.clean();
37
+ setTimeout(() => {
38
+ this.sender = null;
39
+ }, 20);
40
+ };
41
+ this.reset = () => {
42
+ this.workerStatus = WorkerStatus.Stopping;
43
+ if (this.sendIntervalID !== null) {
44
+ clearInterval(this.sendIntervalID);
45
+ this.sendIntervalID = null;
46
+ }
47
+ this.resetSender();
48
+ this.resetWebWorker();
49
+ setTimeout(() => {
50
+ this.workerStatus = WorkerStatus.NotActive;
51
+ }, 100);
52
+ };
53
+ this.initiateRestart = () => {
54
+ if (this.workerStatus === WorkerStatus.Stopped) {
55
+ return;
56
+ }
57
+ this.postMessage({ type: 'restart' });
58
+ this.reset();
59
+ };
60
+ this.initiateFailure = (reason) => {
61
+ if ([WorkerStatus.Stopped, WorkerStatus.Stopping, WorkerStatus.NotActive].includes(this.workerStatus)) {
62
+ return;
63
+ }
64
+ this.postMessage({ type: 'failure', reason });
65
+ this.reset();
66
+ };
67
+ this.processMessage = (data) => {
68
+ if (data === null) {
69
+ this.finalize();
70
+ return;
71
+ }
72
+ if (data.type === 'batch' && Array.isArray(data.data)) {
73
+ data.data.forEach((message) => {
74
+ if (message[0] === 55 /* MType.SetPageVisibility */) {
75
+ // document is hidden
76
+ if (message[1]) {
77
+ this.restartTimeoutID = setTimeout(() => this.initiateRestart(), 30 * 60 * 1000);
78
+ }
79
+ else {
80
+ if (this.restartTimeoutID) {
81
+ clearTimeout(this.restartTimeoutID);
82
+ }
83
+ }
84
+ }
85
+ });
86
+ this.worker.postMessage({ type: 'to_writer', data: data.data });
87
+ }
88
+ };
89
+ this.startWorker = (data) => {
90
+ this.sender = new QueueSender_js_1.default(data.ingestPoint, () => {
91
+ // onUnauthorised
92
+ this.initiateRestart();
93
+ }, (reason) => {
94
+ // onFailure
95
+ this.initiateFailure(reason);
96
+ }, data.connAttemptCount, data.connAttemptGap, (batch) => {
97
+ this.postMessage({ type: 'compress', batch });
98
+ });
99
+ if (this.sendIntervalID === null) {
100
+ this.sendIntervalID = setInterval(this.finalize, AUTO_SEND_INTERVAL);
101
+ }
102
+ this.worker.postMessage(data);
103
+ return;
104
+ };
105
+ this.stopWorker = () => {
106
+ this.finalize();
107
+ this.reset();
108
+ return;
109
+ };
110
+ this.authorizeWorker = (data) => {
111
+ if (!this.sender) {
112
+ console.debug('OR WebWorker: sender not initialised. Received auth.');
113
+ this.initiateRestart();
114
+ return;
115
+ }
116
+ this.sender.authorise(data.token);
117
+ if (data.beaconSizeLimit) {
118
+ this.worker.postMessage({ type: 'beacon_size_limit', data: data.beaconSizeLimit });
119
+ }
120
+ return;
121
+ };
122
+ this.sendCompressedBatch = (data) => {
123
+ if (!this.sender) {
124
+ console.debug('OR Worker: sender not init. Compressed batch');
125
+ this.initiateRestart();
126
+ return;
127
+ }
128
+ this.sender?.sendCompressed(data);
129
+ return;
130
+ };
131
+ this.sendUncompressedBatch = (data) => {
132
+ if (!this.sender) {
133
+ console.debug('OR Worker: sender not init. Compressed batch.');
134
+ this.initiateRestart();
135
+ return;
136
+ }
137
+ if (data) {
138
+ this.sender.sendUncompressed(data);
139
+ return;
140
+ }
141
+ };
142
+ this.postMessage = (data) => {
143
+ this.app.handleWorkerMsg(data);
144
+ };
145
+ this.worker.onerror = (e) => {
146
+ this.onError('webworker_error', e);
147
+ };
148
+ this.worker.onmessage = ({ data }) => {
149
+ if (rebroadcastEvents.includes(data.type)) {
150
+ this.postMessage(data);
151
+ return;
152
+ }
153
+ switch (data.type) {
154
+ case 'status':
155
+ this.workerStatus = data.data;
156
+ return;
157
+ case 'batch_ready':
158
+ if (this.sender) {
159
+ this.app.debug.log('Openreplay: msg batch to sender: ', data.data);
160
+ this.sender.push(data.data);
161
+ }
162
+ }
163
+ };
164
+ }
165
+ }
166
+ exports.default = WebWorkerManager;
@@ -3,7 +3,61 @@ export interface Options {
3
3
  connAttemptCount?: number;
4
4
  connAttemptGap?: number;
5
5
  }
6
- type Start = {
6
+ export type ToWorkerData = null | Stop | Batch | WorkerStart | BeaconSizeLimit | ToWriterData | ForceFlushBatch | CheckQueue | ResetWriter | WriterFinalize;
7
+ export type FromWorkerData = Restart | Failure | NotInit | Compress | QEmpty | Status | BatchReady;
8
+ type BatchReady = {
9
+ type: 'batch_ready';
10
+ data: Uint8Array;
11
+ };
12
+ type Status = {
13
+ type: 'status';
14
+ data: number;
15
+ };
16
+ type Compress = {
17
+ type: 'compress';
18
+ batch: Uint8Array;
19
+ };
20
+ type Restart = {
21
+ type: 'restart';
22
+ };
23
+ type NotInit = {
24
+ type: 'not_init';
25
+ };
26
+ type Stop = {
27
+ type: 'stop';
28
+ };
29
+ type Batch = {
30
+ type: 'batch';
31
+ data: Array<Message>;
32
+ };
33
+ type ForceFlushBatch = {
34
+ type: 'forceFlushBatch';
35
+ };
36
+ type CheckQueue = {
37
+ type: 'check_queue';
38
+ };
39
+ type WriterFinalize = {
40
+ type: 'writer_finalize';
41
+ };
42
+ type ResetWriter = {
43
+ type: 'reset_writer';
44
+ };
45
+ type BeaconSizeLimit = {
46
+ type: 'beacon_size_limit';
47
+ data: number;
48
+ };
49
+ type ToWriterData = {
50
+ type: 'to_writer';
51
+ data: Array<Message>;
52
+ };
53
+ type Failure = {
54
+ type: 'failure';
55
+ reason: string;
56
+ };
57
+ type QEmpty = {
58
+ type: 'queue_empty';
59
+ };
60
+ export type WorkerStart = {
7
61
  type: 'start';
8
62
  ingestPoint: string;
9
63
  pageNo: number;
@@ -11,27 +65,8 @@ type Start = {
11
65
  url: string;
12
66
  tabId: string;
13
67
  } & Options;
14
- type Auth = {
15
- type: 'auth';
68
+ export type WorkerAuth = {
16
69
  token: string;
17
70
  beaconSizeLimit?: number;
18
71
  };
19
- export type ToWorkerData = null | 'stop' | Start | Auth | Array<Message> | {
20
- type: 'compressed';
21
- batch: Uint8Array;
22
- } | {
23
- type: 'uncompressed';
24
- batch: Uint8Array;
25
- } | 'forceFlushBatch' | 'check_queue';
26
- type Failure = {
27
- type: 'failure';
28
- reason: string;
29
- };
30
- type QEmpty = {
31
- type: 'queue_empty';
32
- };
33
- export type FromWorkerData = 'a_stop' | 'a_start' | Failure | 'not_init' | {
34
- type: 'compress';
35
- batch: Uint8Array;
36
- } | QEmpty;
37
72
  export {};
package/cjs/index.js CHANGED
@@ -97,7 +97,7 @@ class API {
97
97
  const orig = this.options.ingestPoint || index_js_1.DEFAULT_INGEST_POINT;
98
98
  req.open('POST', orig + '/v1/web/not-started');
99
99
  req.send(JSON.stringify({
100
- trackerVersion: '12.0.12',
100
+ trackerVersion: '12.1.0-beta.99',
101
101
  projectKey: this.options.projectKey,
102
102
  doNotTrack,
103
103
  reason: missingApi.length ? `missing api: ${missingApi.join(',')}` : reason,
@@ -11,7 +11,7 @@ import type { Options as ObserverOptions } from './observer/top_observer.js';
11
11
  import type { Options as SanitizerOptions } from './sanitizer.js';
12
12
  import type { Options as SessOptions } from './session.js';
13
13
  import type { Options as NetworkOptions } from '../modules/network.js';
14
- import type { Options as WebworkerOptions } from '../common/interaction.js';
14
+ import type { Options as WebworkerOptions, FromWorkerData } from '../common/interaction.js';
15
15
  export interface StartOptions {
16
16
  userID?: string;
17
17
  metadata?: Record<string, string>;
@@ -90,7 +90,7 @@ export default class App {
90
90
  private readonly revID;
91
91
  private activityState;
92
92
  private readonly version;
93
- private readonly worker?;
93
+ private readonly workerManager?;
94
94
  private compressionThreshold;
95
95
  private restartAttempts;
96
96
  private readonly bc;
@@ -100,9 +100,10 @@ export default class App {
100
100
  private uxtManager;
101
101
  private conditionsManager;
102
102
  featureFlags: FeatureFlags;
103
- private tagWatcher;
103
+ private readonly tagWatcher;
104
104
  constructor(projectKey: string, sessionToken: string | undefined, options: Partial<Options>, signalError: (error: string, apis: string[]) => void);
105
- private _debug;
105
+ handleWorkerMsg(data: FromWorkerData): void;
106
+ private readonly _debug;
106
107
  private _usingOldFetchPlugin;
107
108
  send(message: Message, urgent?: boolean): void;
108
109
  /**
@@ -160,7 +161,7 @@ export default class App {
160
161
  private checkSessionToken;
161
162
  /**
162
163
  * start buffering messages without starting the actual session, which gives
163
- * user 30 seconds to "activate" and record session by calling `start()` on conditional trigger
164
+ * user 30 seconds to "activate" and record session by calling `start()` on conditional trigger,
164
165
  * and we will then send buffered batch, so it won't get lost
165
166
  * */
166
167
  coldStart(startOpts?: StartOptions, conditional?: boolean): Promise<void>;
@@ -178,7 +179,7 @@ export default class App {
178
179
  /**
179
180
  * Saves the captured messages in localStorage (or whatever is used in its place)
180
181
  *
181
- * Then when this.offlineRecording is called, it will preload this messages and clear the storage item
182
+ * Then, when this.offlineRecording is called, it will preload this messages and clear the storage item
182
183
  *
183
184
  * Keeping the size of local storage reasonable is up to the end users of this library
184
185
  * */
package/lib/app/index.js CHANGED
@@ -15,6 +15,7 @@ import AttributeSender from '../modules/attributeSender.js';
15
15
  import CanvasRecorder from './canvas.js';
16
16
  import UserTestManager from '../modules/userTesting/index.js';
17
17
  import TagWatcher from '../modules/tagWatcher.js';
18
+ import WebWorkerManager from './workerManager/index.js';
18
19
  const CANCELED = 'canceled';
19
20
  const uxtStorageKey = 'or_uxt_active';
20
21
  const bufferStorageKey = 'or_buffer_1';
@@ -51,12 +52,26 @@ export default class App {
51
52
  this.stopCallbacks = [];
52
53
  this.commitCallbacks = [];
53
54
  this.activityState = ActivityState.NotActive;
54
- this.version = '12.0.12'; // TODO: version compatability check inside each plugin.
55
+ this.version = '12.1.0-beta.99'; // TODO: version compatability check inside each plugin.
55
56
  this.compressionThreshold = 24 * 1000;
56
57
  this.restartAttempts = 0;
57
58
  this.bc = null;
58
59
  this.canvasRecorder = null;
59
60
  this.conditionsManager = null;
61
+ this._debug = (context, e) => {
62
+ if (this.options.__debug_report_edp !== null) {
63
+ void fetch(this.options.__debug_report_edp, {
64
+ method: 'POST',
65
+ headers: { 'Content-Type': 'application/json' },
66
+ body: JSON.stringify({
67
+ context,
68
+ // @ts-ignore
69
+ error: `${e}`,
70
+ }),
71
+ });
72
+ }
73
+ this.debug.error('OpenReplay error: ', context, e);
74
+ };
60
75
  this._usingOldFetchPlugin = false;
61
76
  this.coldStartCommitN = 0;
62
77
  this.delay = 0;
@@ -146,51 +161,11 @@ export default class App {
146
161
  this.session.applySessionHash(sessionToken);
147
162
  }
148
163
  try {
149
- this.worker = new Worker(URL.createObjectURL(new Blob(['"use strict";class t{constructor(t,s,i,e=10,n=250,h,r){this.onUnauthorised=s,this.onFailure=i,this.MAX_ATTEMPTS_COUNT=e,this.ATTEMPT_TIMEOUT=n,this.onCompress=h,this.pageNo=r,this.attemptsCount=0,this.busy=!1,this.queue=[],this.token=null,this.lastBatchNum=0,this.ingestURL=t+"/v1/web/i",this.isCompressing=void 0!==h}getQueueStatus(){return 0===this.queue.length&&!this.busy}authorise(t){this.token=t,this.busy||this.sendNext()}push(t){if(this.busy||!this.token)this.queue.push(t);else if(this.busy=!0,this.isCompressing&&this.onCompress)this.onCompress(t);else{const s=++this.lastBatchNum;this.sendBatch(t,!1,s)}}sendNext(){const t=this.queue.shift();if(t)if(this.busy=!0,this.isCompressing&&this.onCompress)this.onCompress(t);else{const s=++this.lastBatchNum;this.sendBatch(t,!1,s)}else this.busy=!1}retry(t,s,i){this.attemptsCount>=this.MAX_ATTEMPTS_COUNT?this.onFailure(`Failed to send batch after ${this.attemptsCount} attempts.`):(this.attemptsCount++,setTimeout((()=>this.sendBatch(t,s,i)),this.ATTEMPT_TIMEOUT*this.attemptsCount))}sendBatch(t,s,i){const e=i?.toString().replace(/^([^_]+)_([^_]+).*/,"$1_$2_$3");this.busy=!0;const n={Authorization:`Bearer ${this.token}`};s&&(n["Content-Encoding"]="gzip"),null!==this.token?fetch(`${this.ingestURL}?batch=${this.pageNo??"noPageNum"}_${e??"noBatchNum"}`,{body:t,method:"POST",headers:n,keepalive:t.length<65536}).then((e=>{if(401===e.status)return this.busy=!1,void this.onUnauthorised();e.status>=400?this.retry(t,s,`${i??"noBatchNum"}_network:${e.status}`):(this.attemptsCount=0,this.sendNext())})).catch((e=>{console.warn("OpenReplay:",e),this.retry(t,s,`${i??"noBatchNum"}_reject:${e.message}`)})):setTimeout((()=>{this.sendBatch(t,s,`${i??"noBatchNum"}_newToken`)}),500)}sendCompressed(t){const s=++this.lastBatchNum;this.sendBatch(t,!0,s)}sendUncompressed(t){const s=++this.lastBatchNum;this.sendBatch(t,!1,s)}clean(){this.sendNext(),setTimeout((()=>{this.token=null,this.queue.length=0}),10)}}const s="function"==typeof TextEncoder?new TextEncoder:{encode(t){const s=t.length,i=new Uint8Array(3*s);let e=-1;for(let n=0,h=0,r=0;r!==s;){if(n=t.charCodeAt(r),r+=1,n>=55296&&n<=56319){if(r===s){i[e+=1]=239,i[e+=1]=191,i[e+=1]=189;break}if(h=t.charCodeAt(r),!(h>=56320&&h<=57343)){i[e+=1]=239,i[e+=1]=191,i[e+=1]=189;continue}if(n=1024*(n-55296)+h-56320+65536,r+=1,n>65535){i[e+=1]=240|n>>>18,i[e+=1]=128|n>>>12&63,i[e+=1]=128|n>>>6&63,i[e+=1]=128|63&n;continue}}n<=127?i[e+=1]=0|n:n<=2047?(i[e+=1]=192|n>>>6,i[e+=1]=128|63&n):(i[e+=1]=224|n>>>12,i[e+=1]=128|n>>>6&63,i[e+=1]=128|63&n)}return i.subarray(0,e+1)}};class i{constructor(t){this.size=t,this.offset=0,this.checkpointOffset=0,this.data=new Uint8Array(t)}getCurrentOffset(){return this.offset}checkpoint(){this.checkpointOffset=this.offset}get isEmpty(){return 0===this.offset}skip(t){return this.offset+=t,this.offset<=this.size}set(t,s){this.data.set(t,s)}boolean(t){return this.data[this.offset++]=+t,this.offset<=this.size}uint(t){for((t<0||t>Number.MAX_SAFE_INTEGER)&&(t=0);t>=128;)this.data[this.offset++]=t%256|128,t=Math.floor(t/128);return this.data[this.offset++]=t,this.offset<=this.size}int(t){return t=Math.round(t),this.uint(t>=0?2*t:-2*t-1)}string(t){const i=s.encode(t),e=i.byteLength;return!(!this.uint(e)||this.offset+e>this.size)&&(this.data.set(i,this.offset),this.offset+=e,!0)}reset(){this.offset=0,this.checkpointOffset=0}flush(){const t=this.data.slice(0,this.checkpointOffset);return this.reset(),t}}class e extends i{encode(t){switch(t[0]){case 0:case 11:case 114:case 115:return this.uint(t[1]);case 4:case 44:case 47:return this.string(t[1])&&this.string(t[2])&&this.uint(t[3]);case 5:case 20:case 38:case 70:case 75:case 76:case 77:case 82:return this.uint(t[1])&&this.uint(t[2]);case 6:return this.int(t[1])&&this.int(t[2]);case 7:return!0;case 8:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.string(t[4])&&this.boolean(t[5]);case 9:case 10:case 24:case 51:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3]);case 12:case 61:case 71:return this.uint(t[1])&&this.string(t[2])&&this.string(t[3]);case 13:case 14:case 17:case 50:case 54:return this.uint(t[1])&&this.string(t[2]);case 16:return this.uint(t[1])&&this.int(t[2])&&this.int(t[3]);case 18:return this.uint(t[1])&&this.string(t[2])&&this.int(t[3]);case 19:return this.uint(t[1])&&this.boolean(t[2]);case 21:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.string(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8]);case 22:case 27:case 30:case 41:case 45:case 46:case 63:case 64:case 79:return this.string(t[1])&&this.string(t[2]);case 23:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8])&&this.uint(t[9]);case 28:case 29:case 42:case 117:case 118:return this.string(t[1]);case 37:return this.uint(t[1])&&this.string(t[2])&&this.uint(t[3]);case 39:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.uint(t[7]);case 40:return this.string(t[1])&&this.uint(t[2])&&this.string(t[3])&&this.string(t[4]);case 48:case 78:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4]);case 49:return this.int(t[1])&&this.int(t[2])&&this.uint(t[3])&&this.uint(t[4]);case 53:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.string(t[7])&&this.string(t[8]);case 55:return this.boolean(t[1]);case 57:case 60:return this.uint(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4]);case 58:case 120:return this.int(t[1]);case 59:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.string(t[5])&&this.string(t[6])&&this.string(t[7]);case 67:case 73:return this.uint(t[1])&&this.string(t[2])&&this.uint(t[3])&&this.string(t[4]);case 69:return this.uint(t[1])&&this.uint(t[2])&&this.string(t[3])&&this.string(t[4]);case 81:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.int(t[4])&&this.string(t[5]);case 83:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.string(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8])&&this.uint(t[9]);case 84:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.uint(t[4])&&this.string(t[5])&&this.string(t[6]);case 112:return this.uint(t[1])&&this.string(t[2])&&this.boolean(t[3])&&this.string(t[4])&&this.int(t[5])&&this.int(t[6]);case 113:return this.uint(t[1])&&this.uint(t[2])&&this.string(t[3]);case 116:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.string(t[7])&&this.string(t[8])&&this.uint(t[9])&&this.boolean(t[10]);case 119:return this.string(t[1])&&this.uint(t[2]);case 121:return this.string(t[1])&&this.string(t[2])&&this.uint(t[3])&&this.uint(t[4])}}}class n{constructor(t,s,i,n,h,r){this.pageNo=t,this.timestamp=s,this.url=i,this.onBatch=n,this.tabId=h,this.onOfflineEnd=r,this.nextIndex=0,this.beaconSize=2e5,this.encoder=new e(this.beaconSize),this.sizeBuffer=new Uint8Array(3),this.isEmpty=!0,this.beaconSizeLimit=1e6,this.prepare()}writeType(t){return this.encoder.uint(t[0])}writeFields(t){return this.encoder.encode(t)}writeSizeAt(t,s){for(let s=0;s<3;s++)this.sizeBuffer[s]=t>>8*s;this.encoder.set(this.sizeBuffer,s)}prepare(){if(!this.encoder.isEmpty)return;const t=[81,1,this.pageNo,this.nextIndex,this.timestamp,this.url],s=[118,this.tabId];this.writeType(t),this.writeFields(t),this.writeWithSize(s),this.isEmpty=!0}writeWithSize(t){const s=this.encoder;if(!this.writeType(t)||!s.skip(3))return!1;const i=s.getCurrentOffset(),e=this.writeFields(t);if(e){const e=s.getCurrentOffset()-i;if(e>16777215)return console.warn("OpenReplay: max message size overflow."),!1;this.writeSizeAt(e,i-3),s.checkpoint(),this.isEmpty=this.isEmpty&&0===t[0],this.nextIndex++}return e}setBeaconSizeLimit(t){this.beaconSizeLimit=t}writeMessage(t){if("q_end"===t[0])return this.finaliseBatch(),this.onOfflineEnd();0===t[0]&&(this.timestamp=t[1]),4===t[0]&&(this.url=t[1]),this.writeWithSize(t)||(this.finaliseBatch(),this.writeWithSize(t)||(this.encoder=new e(this.beaconSizeLimit),this.prepare(),this.writeWithSize(t)?this.finaliseBatch():console.warn("OpenReplay: beacon size overflow. Skipping large message.",t,this),this.encoder=new e(this.beaconSize),this.prepare()))}finaliseBatch(){if(this.isEmpty)return;const t=this.encoder.flush();this.onBatch(t),this.prepare()}clean(){this.encoder.reset()}}var h;!function(t){t[t.NotActive=0]="NotActive",t[t.Starting=1]="Starting",t[t.Stopping=2]="Stopping",t[t.Active=3]="Active",t[t.Stopped=4]="Stopped"}(h||(h={}));let r=null,a=null,u=h.NotActive;function o(){a&&a.finaliseBatch()}function c(){return new Promise((t=>{u=h.Stopping,null!==l&&(clearInterval(l),l=null),a&&(a.clean(),a=null),r&&(r.clean(),setTimeout((()=>{r=null}),20)),setTimeout((()=>{u=h.NotActive,t(null)}),100)}))}function p(){u!==h.Stopped&&(postMessage("a_stop"),c().then((()=>{postMessage("a_start")})))}let g,l=null;self.onmessage=({data:s})=>{if(null!=s){if("stop"===s)return o(),void c().then((()=>{u=h.Stopped}));if("forceFlushBatch"!==s){if(!Array.isArray(s)){if("compressed"===s.type){if(!r)return console.debug("OR WebWorker: sender not initialised. Compressed batch."),void p();s.batch&&r.sendCompressed(s.batch)}if("uncompressed"===s.type){if(!r)return console.debug("OR WebWorker: sender not initialised. Uncompressed batch."),void p();s.batch&&r.sendUncompressed(s.batch)}return"start"===s.type?(u=h.Starting,r=new t(s.ingestPoint,(()=>{p()}),(t=>{!function(t){postMessage({type:"failure",reason:t}),c()}(t)}),s.connAttemptCount,s.connAttemptGap,(t=>{postMessage({type:"compress",batch:t},[t.buffer])}),s.pageNo),a=new n(s.pageNo,s.timestamp,s.url,(t=>{r&&r.push(t)}),s.tabId,(()=>postMessage({type:"queue_empty"}))),null===l&&(l=setInterval(o,1e4)),u=h.Active):"auth"===s.type?r?a?(r.authorise(s.token),void(s.beaconSizeLimit&&a.setBeaconSizeLimit(s.beaconSizeLimit))):(console.debug("OR WebWorker: writer not initialised. Received auth."),void p()):(console.debug("OR WebWorker: sender not initialised. Received auth."),void p()):void 0}if(a){const t=a;s.forEach((s=>{55===s[0]&&(s[1]?g=setTimeout((()=>p()),18e5):clearTimeout(g)),t.writeMessage(s)}))}else postMessage("not_init"),p()}else o()}else o()};'], { type: 'text/javascript' })));
150
- this.worker.onerror = (e) => {
151
- this._debug('webworker_error', e);
152
- };
153
- this.worker.onmessage = ({ data }) => {
154
- // handling 401 auth restart (new token assignment)
155
- if (data === 'a_stop') {
156
- this.stop(false);
157
- }
158
- else if (data === 'a_start') {
159
- void this.start({}, true);
160
- }
161
- else if (data === 'not_init') {
162
- this.debug.warn('OR WebWorker: writer not initialised. Restarting tracker');
163
- }
164
- else if (data.type === 'failure') {
165
- this.stop(false);
166
- this.debug.error('worker_failed', data.reason);
167
- this._debug('worker_failed', data.reason);
168
- }
169
- else if (data.type === 'compress') {
170
- const batch = data.batch;
171
- const batchSize = batch.byteLength;
172
- if (batchSize > this.compressionThreshold) {
173
- gzip(data.batch, { mtime: 0 }, (err, result) => {
174
- if (err) {
175
- this.debug.error('Openreplay compression error:', err);
176
- this.worker?.postMessage({ type: 'uncompressed', batch: batch });
177
- }
178
- else {
179
- this.worker?.postMessage({ type: 'compressed', batch: result });
180
- }
181
- });
182
- }
183
- else {
184
- this.worker?.postMessage({ type: 'uncompressed', batch: batch });
185
- }
186
- }
187
- else if (data.type === 'queue_empty') {
188
- this.onSessionSent();
189
- }
190
- };
164
+ const webworker = new Worker(URL.createObjectURL(new Blob(['"use strict";const t="function"==typeof TextEncoder?new TextEncoder:{encode(t){const i=t.length,s=new Uint8Array(3*i);let e=-1;for(let n=0,r=0,h=0;h!==i;){if(n=t.charCodeAt(h),h+=1,n>=55296&&n<=56319){if(h===i){s[e+=1]=239,s[e+=1]=191,s[e+=1]=189;break}if(r=t.charCodeAt(h),!(r>=56320&&r<=57343)){s[e+=1]=239,s[e+=1]=191,s[e+=1]=189;continue}if(n=1024*(n-55296)+r-56320+65536,h+=1,n>65535){s[e+=1]=240|n>>>18,s[e+=1]=128|n>>>12&63,s[e+=1]=128|n>>>6&63,s[e+=1]=128|63&n;continue}}n<=127?s[e+=1]=0|n:n<=2047?(s[e+=1]=192|n>>>6,s[e+=1]=128|63&n):(s[e+=1]=224|n>>>12,s[e+=1]=128|n>>>6&63,s[e+=1]=128|63&n)}return s.subarray(0,e+1)}};class i{constructor(t){this.size=t,this.offset=0,this.checkpointOffset=0,this.data=new Uint8Array(t)}getCurrentOffset(){return this.offset}checkpoint(){this.checkpointOffset=this.offset}get isEmpty(){return 0===this.offset}skip(t){return this.offset+=t,this.offset<=this.size}set(t,i){this.data.set(t,i)}boolean(t){return this.data[this.offset++]=+t,this.offset<=this.size}uint(t){for((t<0||t>Number.MAX_SAFE_INTEGER)&&(t=0);t>=128;)this.data[this.offset++]=t%256|128,t=Math.floor(t/128);return this.data[this.offset++]=t,this.offset<=this.size}int(t){return t=Math.round(t),this.uint(t>=0?2*t:-2*t-1)}string(i){const s=t.encode(i),e=s.byteLength;return!(!this.uint(e)||this.offset+e>this.size)&&(this.data.set(s,this.offset),this.offset+=e,!0)}reset(){this.offset=0,this.checkpointOffset=0}flush(){const t=this.data.slice(0,this.checkpointOffset);return this.reset(),t}}class s extends i{encode(t){switch(t[0]){case 0:case 11:case 114:case 115:return this.uint(t[1]);case 4:case 44:case 47:return this.string(t[1])&&this.string(t[2])&&this.uint(t[3]);case 5:case 20:case 38:case 70:case 75:case 76:case 77:case 82:return this.uint(t[1])&&this.uint(t[2]);case 6:return this.int(t[1])&&this.int(t[2]);case 7:return!0;case 8:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.string(t[4])&&this.boolean(t[5]);case 9:case 10:case 24:case 51:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3]);case 12:case 61:case 71:return this.uint(t[1])&&this.string(t[2])&&this.string(t[3]);case 13:case 14:case 17:case 50:case 54:return this.uint(t[1])&&this.string(t[2]);case 16:return this.uint(t[1])&&this.int(t[2])&&this.int(t[3]);case 18:return this.uint(t[1])&&this.string(t[2])&&this.int(t[3]);case 19:return this.uint(t[1])&&this.boolean(t[2]);case 21:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.string(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8]);case 22:case 27:case 30:case 41:case 45:case 46:case 63:case 64:case 79:return this.string(t[1])&&this.string(t[2]);case 23:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8])&&this.uint(t[9]);case 28:case 29:case 42:case 117:case 118:return this.string(t[1]);case 37:return this.uint(t[1])&&this.string(t[2])&&this.uint(t[3]);case 39:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.uint(t[7]);case 40:return this.string(t[1])&&this.uint(t[2])&&this.string(t[3])&&this.string(t[4]);case 48:case 78:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4]);case 49:return this.int(t[1])&&this.int(t[2])&&this.uint(t[3])&&this.uint(t[4]);case 53:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.string(t[7])&&this.string(t[8]);case 55:return this.boolean(t[1]);case 57:case 60:return this.uint(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4]);case 58:case 120:return this.int(t[1]);case 59:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.string(t[5])&&this.string(t[6])&&this.string(t[7]);case 67:case 73:return this.uint(t[1])&&this.string(t[2])&&this.uint(t[3])&&this.string(t[4]);case 69:return this.uint(t[1])&&this.uint(t[2])&&this.string(t[3])&&this.string(t[4]);case 81:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.int(t[4])&&this.string(t[5]);case 83:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.string(t[4])&&this.string(t[5])&&this.uint(t[6])&&this.uint(t[7])&&this.uint(t[8])&&this.uint(t[9]);case 84:return this.string(t[1])&&this.string(t[2])&&this.string(t[3])&&this.uint(t[4])&&this.string(t[5])&&this.string(t[6]);case 112:return this.uint(t[1])&&this.string(t[2])&&this.boolean(t[3])&&this.string(t[4])&&this.int(t[5])&&this.int(t[6]);case 113:return this.uint(t[1])&&this.uint(t[2])&&this.string(t[3]);case 116:return this.uint(t[1])&&this.uint(t[2])&&this.uint(t[3])&&this.uint(t[4])&&this.uint(t[5])&&this.uint(t[6])&&this.string(t[7])&&this.string(t[8])&&this.uint(t[9])&&this.boolean(t[10]);case 119:return this.string(t[1])&&this.uint(t[2]);case 121:return this.string(t[1])&&this.string(t[2])&&this.uint(t[3])&&this.uint(t[4])}}}class e{constructor(t,i,e,n,r,h){this.pageNo=t,this.timestamp=i,this.url=e,this.onBatch=n,this.tabId=r,this.onOfflineEnd=h,this.nextIndex=0,this.beaconSize=2e5,this.encoder=new s(this.beaconSize),this.sizeBuffer=new Uint8Array(3),this.isEmpty=!0,this.beaconSizeLimit=1e6,this.prepare()}writeType(t){return this.encoder.uint(t[0])}writeFields(t){return this.encoder.encode(t)}writeSizeAt(t,i){for(let i=0;i<3;i++)this.sizeBuffer[i]=t>>8*i;this.encoder.set(this.sizeBuffer,i)}prepare(){if(!this.encoder.isEmpty)return;const t=[81,1,this.pageNo,this.nextIndex,this.timestamp,this.url],i=[118,this.tabId];this.writeType(t),this.writeFields(t),this.writeWithSize(i),this.isEmpty=!0}writeWithSize(t){const i=this.encoder;if(!this.writeType(t)||!i.skip(3))return!1;const s=i.getCurrentOffset(),e=this.writeFields(t);if(e){const e=i.getCurrentOffset()-s;if(e>16777215)return console.warn("OpenReplay: max message size overflow."),!1;this.writeSizeAt(e,s-3),i.checkpoint(),this.isEmpty=this.isEmpty&&0===t[0],this.nextIndex++}return e}setBeaconSizeLimit(t){this.beaconSizeLimit=t}writeMessage(t){if("q_end"===t[0])return this.finaliseBatch(),this.onOfflineEnd();0===t[0]&&(this.timestamp=t[1]),4===t[0]&&(this.url=t[1]),this.writeWithSize(t)||(this.finaliseBatch(),this.writeWithSize(t)||(this.encoder=new s(this.beaconSizeLimit),this.prepare(),this.writeWithSize(t)?this.finaliseBatch():console.warn("OpenReplay: beacon size overflow. Skipping large message.",t,this),this.encoder=new s(this.beaconSize),this.prepare()))}finaliseBatch(){if(this.isEmpty)return;const t=this.encoder.flush();this.onBatch(t),this.prepare()}clean(){this.encoder.reset()}}var n;!function(t){t[t.NotActive=0]="NotActive",t[t.Starting=1]="Starting",t[t.Stopping=2]="Stopping",t[t.Active=3]="Active",t[t.Stopped=4]="Stopped"}(n||(n={}));let r=null,h=n.NotActive;function a(t){postMessage({type:"status",data:t}),h=t}function u(){r&&r.finaliseBatch()}function c(){a(n.Stopping),null!==f&&(clearInterval(f),f=null),r&&(r.clean(),r=null),setTimeout((()=>{a(n.NotActive)}),100)}function o(){h!==n.Stopped&&(postMessage({type:"restart"}),c())}let f=null;self.onmessage=({data:t})=>{if(null!=t){if("writer_finalize"===t.type)return u(),h=n.Stopped;if("reset_writer"!==t.type){if("forceFlushBatch"!==t.type){if("to_writer"===t.type){let i=!1;t.data.forEach((t=>{r?r.writeMessage(t):i||(i=!0,postMessage({type:"not_init"}),o())}))}return"start"===t.type?(h=n.Starting,r=new e(t.pageNo,t.timestamp,t.url,(t=>{postMessage({type:"batch_ready",data:t},[t.buffer])}),t.tabId,(()=>postMessage({type:"queue_empty"}))),null===f&&(f=setInterval(u,1e4)),h=n.Active):"beacon_size_limit"===t.type?r?void(t.beaconSizeLimit&&r.setBeaconSizeLimit(t.beaconSizeLimit)):(console.debug("OR WebWorker: writer not initialised. Received auth."),void o()):void("restart"===t.type&&o())}u()}else c()}else u()};'], { type: 'text/javascript' })));
165
+ this.workerManager = new WebWorkerManager(this, webworker, this._debug);
191
166
  const alertWorker = () => {
192
- if (this.worker) {
193
- this.worker.postMessage(null);
167
+ if (this.workerManager) {
168
+ this.workerManager.processMessage(null);
194
169
  }
195
170
  };
196
171
  // keep better tactics, discard others?
@@ -209,7 +184,7 @@ export default class App {
209
184
  // yes, there are someone out there
210
185
  resp: 'never-gonna-let-you-down',
211
186
  // you stole someone's identity
212
- reg: 'never-gonna-run-around-and-desert-you',
187
+ regen: 'never-gonna-run-around-and-desert-you',
213
188
  };
214
189
  if (this.bc) {
215
190
  this.bc.postMessage({
@@ -227,7 +202,7 @@ export default class App {
227
202
  const sessionToken = ev.data.token;
228
203
  this.session.setSessionToken(sessionToken);
229
204
  }
230
- if (ev.data.line === proto.reg) {
205
+ if (ev.data.line === proto.regen) {
231
206
  const sessionToken = ev.data.token;
232
207
  this.session.regenerateTabId();
233
208
  this.session.setSessionToken(sessionToken);
@@ -236,7 +211,7 @@ export default class App {
236
211
  const token = this.session.getSessionToken();
237
212
  if (token && this.bc) {
238
213
  this.bc.postMessage({
239
- line: ev.data.source === thisTab ? proto.reg : proto.resp,
214
+ line: ev.data.source === thisTab ? proto.regen : proto.resp,
240
215
  token,
241
216
  source: thisTab,
242
217
  context: this.contextId,
@@ -246,19 +221,44 @@ export default class App {
246
221
  };
247
222
  }
248
223
  }
249
- _debug(context, e) {
250
- if (this.options.__debug_report_edp !== null) {
251
- void fetch(this.options.__debug_report_edp, {
252
- method: 'POST',
253
- headers: { 'Content-Type': 'application/json' },
254
- body: JSON.stringify({
255
- context,
256
- // @ts-ignore
257
- error: `${e}`,
258
- }),
259
- });
224
+ handleWorkerMsg(data) {
225
+ if (data.type === 'restart') {
226
+ this.stop(false);
227
+ void this.start({}, true);
228
+ }
229
+ else if (data.type === 'not_init') {
230
+ this.debug.warn('OR WebWorker: writer not initialised; restarting worker');
231
+ }
232
+ else if (data.type === 'failure') {
233
+ this.stop(false);
234
+ this.debug.error('worker_failed', data.reason);
235
+ this._debug('worker_failed', data.reason);
236
+ }
237
+ else if (data.type === 'compress') {
238
+ const batch = data.batch;
239
+ const batchSize = batch.byteLength;
240
+ if (batchSize > this.compressionThreshold) {
241
+ gzip(data.batch, { mtime: 0 }, (err, result) => {
242
+ if (err) {
243
+ this.debug.error('Openreplay compression error:', err);
244
+ this.stop(false);
245
+ if (this.restartAttempts < 3) {
246
+ this.restartAttempts += 1;
247
+ void this.start({}, true);
248
+ }
249
+ }
250
+ else {
251
+ this.workerManager?.sendCompressedBatch(result);
252
+ }
253
+ });
254
+ }
255
+ else {
256
+ this.workerManager?.sendUncompressedBatch(batch);
257
+ }
258
+ }
259
+ else if (data.type === 'queue_empty') {
260
+ this.onSessionSent();
260
261
  }
261
- this.debug.error('OpenReplay error: ', context, e);
262
262
  }
263
263
  send(message, urgent = false) {
264
264
  if (this.activityState === ActivityState.NotActive) {
@@ -298,24 +298,15 @@ export default class App {
298
298
  * every ~30ms
299
299
  * */
300
300
  _nCommit() {
301
- if (this.worker !== undefined && this.messages.length) {
302
- try {
303
- requestIdleCb(() => {
304
- this.messages.unshift(TabData(this.session.getTabId()));
305
- this.messages.unshift(Timestamp(this.timestamp()));
306
- // why I need to add opt chaining?
307
- this.worker?.postMessage(this.messages);
308
- this.commitCallbacks.forEach((cb) => cb(this.messages));
309
- this.messages.length = 0;
310
- });
311
- }
312
- catch (e) {
313
- this._debug('worker_commit', e);
314
- this.stop(true);
315
- setTimeout(() => {
316
- void this.start();
317
- }, 500);
318
- }
301
+ if (this.workerManager !== undefined && this.messages.length) {
302
+ requestIdleCb(() => {
303
+ this.messages.unshift(TabData(this.session.getTabId()));
304
+ this.messages.unshift(Timestamp(this.timestamp()));
305
+ // why I need to add opt chaining?
306
+ this.workerManager?.processMessage({ type: 'batch', data: this.messages });
307
+ this.commitCallbacks.forEach((cb) => cb(this.messages));
308
+ this.messages.length = 0;
309
+ });
319
310
  }
320
311
  }
321
312
  /**
@@ -343,7 +334,7 @@ export default class App {
343
334
  }
344
335
  }
345
336
  postToWorker(messages) {
346
- this.worker?.postMessage(messages);
337
+ this.workerManager?.processMessage({ type: 'batch', data: messages });
347
338
  this.commitCallbacks.forEach((cb) => cb(messages));
348
339
  messages.length = 0;
349
340
  }
@@ -490,7 +481,7 @@ export default class App {
490
481
  }
491
482
  /**
492
483
  * start buffering messages without starting the actual session, which gives
493
- * user 30 seconds to "activate" and record session by calling `start()` on conditional trigger
484
+ * user 30 seconds to "activate" and record session by calling `start()` on conditional trigger,
494
485
  * and we will then send buffered batch, so it won't get lost
495
486
  * */
496
487
  async coldStart(startOpts = {}, conditional) {
@@ -626,7 +617,7 @@ export default class App {
626
617
  /**
627
618
  * Saves the captured messages in localStorage (or whatever is used in its place)
628
619
  *
629
- * Then when this.offlineRecording is called, it will preload this messages and clear the storage item
620
+ * Then, when this.offlineRecording is called, it will preload this messages and clear the storage item
630
621
  *
631
622
  * Keeping the size of local storage reasonable is up to the end users of this library
632
623
  * */
@@ -655,7 +646,7 @@ export default class App {
655
646
  async uploadOfflineRecording() {
656
647
  this.stop(false);
657
648
  const timestamp = now();
658
- this.worker?.postMessage({
649
+ this.workerManager?.processMessage({
659
650
  type: 'start',
660
651
  pageNo: this.session.incPageNo(),
661
652
  ingestPoint: this.options.ingestPoint,
@@ -683,8 +674,7 @@ export default class App {
683
674
  }),
684
675
  });
685
676
  const { token, userBrowser, userCity, userCountry, userDevice, userOS, userState, beaconSizeLimit, projectID, } = await r.json();
686
- this.worker?.postMessage({
687
- type: 'auth',
677
+ this.workerManager?.authorizeWorker({
688
678
  token,
689
679
  beaconSizeLimit,
690
680
  });
@@ -708,7 +698,7 @@ export default class App {
708
698
  if (isColdStart && this.coldInterval) {
709
699
  clearInterval(this.coldInterval);
710
700
  }
711
- if (!this.worker) {
701
+ if (!this.workerManager) {
712
702
  const reason = 'No worker found: perhaps, CSP is not set.';
713
703
  this.signalError(reason, []);
714
704
  return Promise.resolve(UnsuccessfulStart(reason));
@@ -735,7 +725,7 @@ export default class App {
735
725
  metadata: startOpts.metadata,
736
726
  });
737
727
  const timestamp = now();
738
- this.worker.postMessage({
728
+ this.workerManager?.startWorker({
739
729
  type: 'start',
740
730
  pageNo: this.session.incPageNo(),
741
731
  ingestPoint: this.options.ingestPoint,
@@ -781,7 +771,7 @@ export default class App {
781
771
  }
782
772
  })
783
773
  .then(async (r) => {
784
- if (!this.worker) {
774
+ if (!this.workerManager) {
785
775
  const reason = 'no worker found after start request (this might not happen)';
786
776
  this.signalError(reason, []);
787
777
  return Promise.reject(reason);
@@ -821,8 +811,7 @@ export default class App {
821
811
  timestamp: startTimestamp || timestamp,
822
812
  projectID,
823
813
  });
824
- this.worker.postMessage({
825
- type: 'auth',
814
+ this.workerManager?.authorizeWorker({
826
815
  token,
827
816
  beaconSizeLimit,
828
817
  });
@@ -957,7 +946,7 @@ export default class App {
957
946
  }
958
947
  }
959
948
  forceFlushBatch() {
960
- this.worker?.postMessage('forceFlushBatch');
949
+ this.workerManager?.processMessage({ type: 'forceFlushBatch' });
961
950
  }
962
951
  getTabId() {
963
952
  return this.session.getTabId();
@@ -994,8 +983,8 @@ export default class App {
994
983
  this.stopCallbacks.forEach((cb) => cb());
995
984
  this.debug.log('OpenReplay tracking stopped.');
996
985
  this.tagWatcher.clear();
997
- if (this.worker && stopWorker) {
998
- this.worker.postMessage('stop');
986
+ if (this.workerManager && stopWorker) {
987
+ this.workerManager?.stopWorker();
999
988
  }
1000
989
  this.canvasRecorder?.clear();
1001
990
  }