@sailfish-ai/recorder 1.2.4 → 1.2.6

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/index.js CHANGED
@@ -158,63 +158,56 @@ export function matchUrlWithWildcard(url, patterns) {
158
158
  }
159
159
  // Updated XMLHttpRequest interceptor with single check function
160
160
  // Updated XMLHttpRequest interceptor to bypass for CORS-sensitive domains
161
+ // Updated XMLHttpRequest interceptor with exclusion handling
161
162
  function setupXMLHttpRequestInterceptor(domainsToNotPropagateHeaderTo, domainsToPropagateHeadersTo = []) {
162
163
  const originalOpen = XMLHttpRequest.prototype.open;
163
164
  const originalSend = XMLHttpRequest.prototype.send;
164
165
  const sessionId = getOrSetSessionId();
165
- // Combine default and passed ignore domains
166
+ // Combined ignore and propagate logic
166
167
  const combinedIgnoreDomains = [
167
168
  ...DOMAINS_TO_NOT_PROPAGATE_HEADER_TO_DEFAULT,
168
169
  ...domainsToNotPropagateHeaderTo,
169
170
  ];
170
171
  // Store URL during open()
171
172
  XMLHttpRequest.prototype.open = function (method, url, ...args) {
172
- // Ensure the URL is a string, otherwise handle gracefully
173
- if (typeof url === "string" && url.length > 0) {
174
- this._requestUrl = url; // Capture the valid URL
175
- }
176
- else {
177
- console.warn("Invalid or non-string URL passed to XMLHttpRequest:", url);
178
- this._requestUrl = null; // Handle invalid or non-string URL
179
- }
173
+ this._requestUrl = typeof url === "string" && url.length > 0 ? url : null;
180
174
  return originalOpen.apply(this, [method, url, ...args]);
181
175
  };
182
176
  // Intercept send()
183
177
  XMLHttpRequest.prototype.send = function (...args) {
184
178
  const url = this._requestUrl;
185
- // If no valid URL was captured, proceed without modification
186
- if (!url) {
179
+ if (!url)
187
180
  return originalSend.apply(this, args);
188
- }
189
- // Bypass logic for domains listed in the combinedIgnoreDomains
181
+ // Check if domain should be ignored based on combined ignore list
190
182
  if (matchUrlWithWildcard(url, combinedIgnoreDomains)) {
183
+ console.log([`[XML] [InIgnoreDomains] IGNORE --> ${url}`]);
191
184
  return originalSend.apply(this, args);
192
185
  }
193
- // Check if the domain should propagate headers
186
+ // Check if domain should propagate headers
194
187
  const shouldPropagateHeader = domainsToPropagateHeadersTo.length === 0 ||
195
188
  matchUrlWithWildcard(url, domainsToPropagateHeadersTo);
196
189
  if (sessionId && shouldPropagateHeader) {
190
+ console.log([`[XML] [InPropagateDomans] PROPAGATE FOR --> ${url}`]);
197
191
  this.setRequestHeader("X-Sf3-Rid", sessionId);
198
192
  }
193
+ console.log([`[XML] [NOT InPropagateDomans] IGNORE --> ${url}`]);
199
194
  return originalSend.apply(this, args);
200
195
  };
201
196
  }
202
- // Updated fetch interceptor to bypass for CORS-sensitive domains
203
- function setupFetchInterceptor(domainsToNotPropagateHeaderTo, domainsToPropagateHeadersTo = []) {
197
+ // Updated fetch interceptor with exclusion handling
198
+ function setupFetchInterceptor(domainsToNotPropagateHeadersTo, domainsToPropagateHeadersTo = []) {
204
199
  const originalFetch = window.fetch;
205
200
  const sessionId = getOrSetSessionId();
206
- // Combine default and passed ignore domains
207
201
  const combinedIgnoreDomains = [
208
202
  ...DOMAINS_TO_NOT_PROPAGATE_HEADER_TO_DEFAULT,
209
- ...domainsToNotPropagateHeaderTo,
203
+ ...domainsToNotPropagateHeadersTo,
210
204
  ];
211
- // Proxy to conditionally intercept fetch based on domains
205
+ const cache = new Map();
212
206
  window.fetch = new Proxy(originalFetch, {
213
207
  apply: (target, thisArg, args) => {
214
208
  let input = args[0];
215
209
  let init = args[1] || {};
216
210
  let url;
217
- // Handle different types of `input` for fetch
218
211
  if (typeof input === "string") {
219
212
  url = input;
220
213
  }
@@ -225,45 +218,65 @@ function setupFetchInterceptor(domainsToNotPropagateHeaderTo, domainsToPropagate
225
218
  url = input.href;
226
219
  }
227
220
  else {
228
- // Unsupported type, skip interception
229
- console.warn("Unsupported input type for fetch:", input);
230
- return target.apply(thisArg, args);
221
+ return target.apply(thisArg, args); // Skip unsupported inputs
231
222
  }
232
- // ** Check if we should skip patching fetch entirely for this domain **
223
+ // Cache check
224
+ if (cache.has(url)) {
225
+ const cachedResult = cache.get(url);
226
+ if (cachedResult === "ignore") {
227
+ console.log(`[CACHE] IGNORE --> ${url}`);
228
+ return target.apply(thisArg, args);
229
+ }
230
+ if (cachedResult === "propagate") {
231
+ console.log(`[CACHE] PROPAGATE FOR --> ${url}`);
232
+ return injectHeader(target, thisArg, args, input, init, sessionId);
233
+ }
234
+ }
235
+ // Check domain exclusion
233
236
  if (matchUrlWithWildcard(url, combinedIgnoreDomains)) {
237
+ cache.set(url, "ignore");
238
+ console.log(`[InIgnoreDomains] IGNORE --> ${url}`);
234
239
  return target.apply(thisArg, args);
235
240
  }
236
- // Check if the domain should propagate the header
241
+ // Check domain propagation
237
242
  const shouldPropagateHeader = domainsToPropagateHeadersTo.length === 0 ||
238
243
  matchUrlWithWildcard(url, domainsToPropagateHeadersTo);
239
- // ** Skip CORS-sensitive requests **
240
- // Ensure we don't modify headers for cross-origin requests to non-whitelisted domains
241
244
  if (!shouldPropagateHeader) {
245
+ cache.set(url, "ignore");
246
+ console.log(`[NOT InPropagateDomans] IGNORE --> ${url}`);
242
247
  return target.apply(thisArg, args);
243
248
  }
244
- // ** Add X-Sf3-Rid header if needed **
245
- if (sessionId && shouldPropagateHeader) {
246
- if (input instanceof Request) {
247
- const clonedRequest = input.clone();
248
- const newHeaders = new Headers(clonedRequest.headers);
249
- newHeaders.set("X-Sf3-Rid", sessionId);
250
- const modifiedRequest = new Request(clonedRequest, {
251
- headers: newHeaders,
252
- });
253
- return target.call(thisArg, modifiedRequest, init);
254
- }
255
- else {
256
- const modifiedInit = { ...init };
257
- const newHeaders = new Headers(init.headers || {});
258
- newHeaders.set("X-Sf3-Rid", sessionId);
259
- modifiedInit.headers = newHeaders;
260
- return target.call(thisArg, input, modifiedInit);
261
- }
262
- }
263
- // If no header propagation, return the original fetch call
264
- return target.apply(thisArg, args);
249
+ cache.set(url, "propagate");
250
+ console.log(`[InPropagateDomans] PROPAGATE FOR --> ${url}`);
251
+ return injectHeader(target, thisArg, args, input, init, sessionId);
265
252
  },
266
253
  });
254
+ // Helper function to inject the X-Sf3-Rid header
255
+ function injectHeader(target, thisArg, args, input, init, sessionId) {
256
+ if (sessionId) {
257
+ if (input instanceof Request) {
258
+ // Clone the Request and modify headers
259
+ const clonedRequest = input.clone();
260
+ const newHeaders = new Headers(clonedRequest.headers);
261
+ newHeaders.set("X-Sf3-Rid", sessionId);
262
+ const modifiedRequest = new Request(clonedRequest, {
263
+ headers: newHeaders,
264
+ });
265
+ return target.call(thisArg, modifiedRequest, init);
266
+ }
267
+ else {
268
+ // For string or URL input, modify init to add headers
269
+ const modifiedInit = { ...init };
270
+ const newHeaders = new Headers(init.headers || {});
271
+ newHeaders.set("X-Sf3-Rid", sessionId);
272
+ modifiedInit.headers = newHeaders;
273
+ return target.call(thisArg, input, modifiedInit);
274
+ }
275
+ }
276
+ else {
277
+ return target.apply(thisArg, args);
278
+ }
279
+ }
267
280
  }
268
281
  // Main Recording Function
269
282
  export async function startRecording({ apiKey, backendApi, domainsToPropagateHeaderTo = [], domainsToNotPropagateHeaderTo = [], }) {