action-lens 1.0.51 → 1.0.53

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.d.mts CHANGED
@@ -44,9 +44,9 @@ declare class ActionLensPlayer {
44
44
 
45
45
  declare class ActionLensRc {
46
46
  sessionId: string;
47
- stopFnForStore: any;
47
+ stopFnForStore: any | null;
48
48
  userId: string | null;
49
- db: Firestore | null;
49
+ db: Firestore;
50
50
  prefix: string;
51
51
  userData: User | null;
52
52
  projectId: string | null;
@@ -59,6 +59,7 @@ declare class ActionLensRc {
59
59
  };
60
60
  metaData: Record<string, any>;
61
61
  constructor();
62
+ private retryOperation;
62
63
  init(_userId: string | null, projectId: string | null, userMeta: {
63
64
  name: string;
64
65
  email: string;
package/dist/index.d.ts CHANGED
@@ -44,9 +44,9 @@ declare class ActionLensPlayer {
44
44
 
45
45
  declare class ActionLensRc {
46
46
  sessionId: string;
47
- stopFnForStore: any;
47
+ stopFnForStore: any | null;
48
48
  userId: string | null;
49
- db: Firestore | null;
49
+ db: Firestore;
50
50
  prefix: string;
51
51
  userData: User | null;
52
52
  projectId: string | null;
@@ -59,6 +59,7 @@ declare class ActionLensRc {
59
59
  };
60
60
  metaData: Record<string, any>;
61
61
  constructor();
62
+ private retryOperation;
62
63
  init(_userId: string | null, projectId: string | null, userMeta: {
63
64
  name: string;
64
65
  email: string;
package/dist/index.js CHANGED
@@ -332,157 +332,160 @@ var import_uuid = require("uuid");
332
332
  var import_rrweb2 = require("rrweb");
333
333
  var import_firestore3 = require("firebase/firestore");
334
334
  var import_packer2 = require("@rrweb/packer");
335
+ var import_app2 = require("firebase/app");
335
336
  var ActionLensRc = class {
336
337
  sessionId = (0, import_uuid.v4)();
337
338
  stopFnForStore = null;
338
339
  userId = null;
339
- db = db;
340
+ db;
340
341
  prefix = "";
341
342
  userData = null;
342
343
  projectId = null;
343
344
  projectData = null;
344
345
  organizationId = null;
345
346
  originalUserId = null;
346
- // 元のユーザーID(外部システムのIDなど)
347
347
  userMeta = {};
348
348
  metaData = {};
349
349
  constructor() {
350
+ const app2 = (0, import_app2.initializeApp)(firebaseConfig);
351
+ this.db = (0, import_firestore3.getFirestore)(app2);
352
+ (0, import_firestore3.enableIndexedDbPersistence)(this.db).catch((err) => {
353
+ console.warn("Persistence error:", err.message);
354
+ });
355
+ }
356
+ async retryOperation(operation, maxAttempts = 3, delay = 1e3) {
357
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
358
+ try {
359
+ return await operation();
360
+ } catch (error) {
361
+ if (attempt === maxAttempts) throw error;
362
+ console.warn(`Retry ${attempt}/${maxAttempts} failed:`, error);
363
+ await new Promise((resolve) => setTimeout(resolve, delay));
364
+ }
365
+ }
366
+ throw new Error("Retry operation failed");
350
367
  }
351
368
  async init(_userId, projectId, userMeta, metaData = {}, _db = null, prefix = "") {
352
369
  try {
370
+ if (_db) {
371
+ this.db = _db;
372
+ }
373
+ if (!this.db) {
374
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
375
+ }
353
376
  this.originalUserId = _userId;
354
377
  console.log("ActionLensRc\u958B\u59CB");
355
- if (!projectId) {
356
- console.error("projectId\u304CNULL\u3067\u3059");
357
- throw new Error("projectId\u304CNULL\u3067\u3059");
358
- }
359
378
  this.projectId = projectId;
360
379
  this.prefix = prefix;
361
380
  this.userMeta = userMeta;
362
381
  this.metaData = metaData;
363
382
  if (!_userId) {
364
- console.error("userId\u304CNULL\u3067\u3059");
365
383
  throw new Error("userId\u304CNULL\u3067\u3059");
366
384
  }
367
- if (!this.db && _db) {
368
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
369
- this.db = _db;
370
- }
371
- if (!this.db) {
372
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
373
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
374
- }
375
385
  if (!this.projectId) {
376
- console.error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
377
- throw new Error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
386
+ throw new Error("projectId\u304CNULL\u3067\u3059");
378
387
  }
379
- const _doc = await (0, import_firestore3.getDoc)(
380
- (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}project`), this.projectId)
388
+ const projectDocRef = (0, import_firestore3.doc)(
389
+ (0, import_firestore3.collection)(this.db, `${this.prefix}project`),
390
+ this.projectId
381
391
  );
382
- this.projectData = _doc.data();
383
- const projectData = this.projectData;
384
- this.organizationId = projectData?.organizationId || "";
392
+ const projectDoc = await this.retryOperation(() => (0, import_firestore3.getDoc)(projectDocRef));
393
+ this.projectData = projectDoc.exists() ? projectDoc.data() : null;
385
394
  if (!this.projectData) {
386
- await (0, import_firestore3.setDoc)(
387
- (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}project`), this.projectId),
388
- {
389
- id: this.projectId,
390
- name: this.projectId,
391
- organizationId: "",
392
- isActivate: false,
393
- // デフォルトでアクティブに設定
394
- createAt: /* @__PURE__ */ new Date(),
395
- updateAt: /* @__PURE__ */ new Date(),
396
- createdBy: this.originalUserId,
397
- updatedBy: this.originalUserId
398
- },
399
- { merge: true }
395
+ await this.retryOperation(
396
+ () => (0, import_firestore3.setDoc)(
397
+ projectDocRef,
398
+ {
399
+ id: this.projectId,
400
+ name: this.projectId,
401
+ organizationId: "",
402
+ isActivate: false,
403
+ createAt: /* @__PURE__ */ new Date(),
404
+ updateAt: /* @__PURE__ */ new Date(),
405
+ createdBy: this.originalUserId,
406
+ updatedBy: this.originalUserId
407
+ },
408
+ { merge: true }
409
+ )
400
410
  );
401
411
  throw new Error("\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");
402
412
  }
403
- if (!this.projectData?.isActivate) {
404
- console.warn("\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u306F\u975E\u30A2\u30AF\u30C6\u30A3\u30D6\u3067\u3059\u3002\u9332\u753B\u3092\u958B\u59CB\u3067\u304D\u307E\u305B\u3093\u3002");
413
+ if (!this.projectData.isActivate) {
405
414
  throw new Error(
406
415
  "\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u306F\u975E\u30A2\u30AF\u30C6\u30A3\u30D6\u3067\u3059\u3002\u9332\u753B\u3092\u958B\u59CB\u3067\u304D\u307E\u305B\u3093\u3002"
407
416
  );
408
417
  }
409
- this.userId = projectData.organizationId + "-" + this.originalUserId;
410
- this.prefix = this.prefix;
411
- this.projectId = this.projectId;
412
- const user = await (0, import_firestore3.getDoc)(
413
- (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}user`), this.userId)
418
+ this.organizationId = this.projectData.organizationId || "";
419
+ this.userId = `${this.projectData.organizationId}-${this.originalUserId}`;
420
+ const userDocRef = (0, import_firestore3.doc)(
421
+ (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
422
+ this.userId
414
423
  );
415
- if (!this.db) {
416
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
417
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
418
- }
419
- const userData = user.data();
420
- if (!userData.id || !user.exists()) {
421
- await (0, import_firestore3.setDoc)(
422
- (0, import_firestore3.doc)(
423
- (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
424
- projectData.organizationId + "-" + this.originalUserId
425
- ),
426
- {
427
- ...this.userMeta || {},
428
- metaData: this.metaData,
429
- id: this.userId,
430
- originalUserId: this.originalUserId,
431
- type: "customer",
432
- organizationId: projectData?.organizationId || "",
433
- projectIds: [this.projectId],
434
- createAt: /* @__PURE__ */ new Date(),
435
- updateAt: /* @__PURE__ */ new Date(),
436
- createdBy: this.userId,
437
- updatedBy: this.userId,
438
- hidden: false,
439
- hiddenBy: ""
440
- },
441
- { merge: true }
442
- );
443
- this.userData = user.data();
444
- } else {
445
- this.userData = user.data();
446
- if (!this.userData?.projectIds?.includes(this.projectId || "")) {
447
- await (0, import_firestore3.setDoc)(
448
- (0, import_firestore3.doc)(
449
- (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
450
- projectData.organizationId + "-" + this.originalUserId
451
- ),
424
+ const userDoc = await this.retryOperation(() => (0, import_firestore3.getDoc)(userDocRef));
425
+ this.userData = userDoc.exists() ? userDoc.data() : null;
426
+ if (!this.userData || !userDoc.exists()) {
427
+ await this.retryOperation(
428
+ () => (0, import_firestore3.setDoc)(
429
+ userDocRef,
452
430
  {
453
- projectIds: [
454
- ...this.userData?.projectIds || [],
455
- this.projectId
456
- ],
431
+ ...this.userMeta,
432
+ metaData: this.metaData,
433
+ id: this.userId,
434
+ originalUserId: this.originalUserId,
435
+ type: "customer",
436
+ organizationId: this.projectData?.organizationId || "",
437
+ projectIds: [this.projectId],
438
+ createAt: /* @__PURE__ */ new Date(),
457
439
  updateAt: /* @__PURE__ */ new Date(),
458
- updatedBy: this.userId
440
+ createdBy: this.userId,
441
+ updatedBy: this.userId,
442
+ hidden: false,
443
+ hiddenBy: ""
459
444
  },
460
445
  { merge: true }
446
+ )
447
+ );
448
+ const updatedUserDoc = await this.retryOperation(
449
+ () => (0, import_firestore3.getDoc)(userDocRef)
450
+ );
451
+ this.userData = updatedUserDoc.data();
452
+ } else {
453
+ if (!this.userData.projectIds?.includes(this.projectId)) {
454
+ await this.retryOperation(
455
+ () => (0, import_firestore3.setDoc)(
456
+ userDocRef,
457
+ {
458
+ projectIds: [
459
+ ...this.userData?.projectIds || [],
460
+ this.projectId
461
+ ],
462
+ updateAt: /* @__PURE__ */ new Date(),
463
+ updatedBy: this.userId
464
+ },
465
+ { merge: true }
466
+ )
461
467
  );
462
468
  }
463
- if (this.userData?.organizationId !== projectData.organizationId) {
464
- await (0, import_firestore3.setDoc)(
465
- (0, import_firestore3.doc)(
466
- (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
467
- projectData.organizationId + "-" + this.originalUserId
468
- ),
469
- {
470
- organizationId: projectData.organizationId ? projectData.organizationId : this.userData?.organizationId,
471
- updateAt: /* @__PURE__ */ new Date(),
472
- updatedBy: this.userId
473
- },
474
- { merge: true }
469
+ if (this.userData.organizationId !== this.projectData.organizationId) {
470
+ await this.retryOperation(
471
+ () => (0, import_firestore3.setDoc)(
472
+ userDocRef,
473
+ {
474
+ organizationId: this.projectData?.organizationId || this.userData?.organizationId,
475
+ updateAt: /* @__PURE__ */ new Date(),
476
+ updatedBy: this.userId
477
+ },
478
+ { merge: true }
479
+ )
475
480
  );
476
481
  }
477
482
  }
478
483
  await this.startRrwebRecordingForStore();
479
- return;
480
484
  } catch (error) {
481
485
  console.error("\u521D\u671F\u5316\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
482
486
  throw error;
483
487
  }
484
488
  }
485
- // コンソールログをキャプチャ
486
489
  startConsoleRecording() {
487
490
  try {
488
491
  const originalConsole = { ...console };
@@ -492,33 +495,19 @@ var ActionLensRc = class {
492
495
  "warn",
493
496
  "error",
494
497
  "debug",
495
- "trace",
496
- "assert",
497
- "clear",
498
- "context",
499
- "count",
500
- "countReset",
501
- "createTask",
502
- "debug",
503
- "dir",
504
- "dirxml",
505
- "error",
506
- "group",
507
- "groupCollapsed",
508
- "groupEnd",
509
- "memory",
510
- "profile",
511
- "profileEnd",
512
- "table",
513
- "time",
514
- "timeEnd",
515
- "timeLog",
516
- "timeStamp"
498
+ "trace"
517
499
  ];
518
500
  consoleMethods.forEach((method) => {
519
501
  console[method] = (...args) => {
520
- originalConsole[method](...args);
521
- import_rrweb2.record.addCustomEvent("console", { method: "log", args });
502
+ try {
503
+ originalConsole[method](...args);
504
+ import_rrweb2.record.addCustomEvent("console", { method, args });
505
+ } catch (error) {
506
+ originalConsole.error(
507
+ "\u30B3\u30F3\u30BD\u30FC\u30EB\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:",
508
+ error
509
+ );
510
+ }
522
511
  };
523
512
  });
524
513
  } catch (error) {
@@ -526,32 +515,39 @@ var ActionLensRc = class {
526
515
  throw error;
527
516
  }
528
517
  }
529
- // タイムライン(ナビゲーションとパフォーマンス)をキャプチャ
530
518
  startTimelineRecording() {
531
519
  try {
532
520
  const userId = this.userId || "unknown";
533
521
  const sessionId = this.sessionId;
534
522
  const db2 = this.db;
535
523
  const prefix = this.prefix;
524
+ if (!db2) {
525
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
526
+ return;
527
+ }
536
528
  window.addEventListener("popstate", (event) => {
537
- if (!db2) {
538
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
539
- return;
529
+ try {
530
+ import_rrweb2.record.addCustomEvent("navigation", {
531
+ url: window.location.href,
532
+ state: event.state
533
+ });
534
+ } catch (error) {
535
+ console.error("\u30CA\u30D3\u30B2\u30FC\u30B7\u30E7\u30F3\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:", error);
540
536
  }
541
- import_rrweb2.record.addCustomEvent("navigation", {
542
- url: window.location.href,
543
- state: event.state
544
- });
545
537
  });
546
538
  if (window.performance) {
547
539
  const observer = new PerformanceObserver((list) => {
548
540
  for (const entry of list.getEntries()) {
549
- import_rrweb2.record.addCustomEvent("performance", {
550
- name: entry.name,
551
- entryType: entry.entryType,
552
- startTime: entry.startTime,
553
- duration: entry.duration
554
- });
541
+ try {
542
+ import_rrweb2.record.addCustomEvent("performance", {
543
+ name: entry.name,
544
+ entryType: entry.entryType,
545
+ startTime: entry.startTime,
546
+ duration: entry.duration
547
+ });
548
+ } catch (error) {
549
+ console.error("\u30D1\u30D5\u30A9\u30FC\u30DE\u30F3\u30B9\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:", error);
550
+ }
555
551
  }
556
552
  });
557
553
  observer.observe({ entryTypes: ["resource", "navigation", "paint"] });
@@ -570,26 +566,27 @@ var ActionLensRc = class {
570
566
  const db2 = this.db;
571
567
  const prefix = this.prefix;
572
568
  if (!db2) {
573
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
574
- return;
569
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
575
570
  }
576
571
  const ActionRecordSessionRef = (0, import_firestore3.doc)(
577
572
  (0, import_firestore3.collection)(db2, `${prefix}ActionRecordSession`),
578
573
  _sessionId
579
574
  );
580
- await (0, import_firestore3.setDoc)(ActionRecordSessionRef, {
581
- id: _sessionId,
582
- startTime: /* @__PURE__ */ new Date(),
583
- endTime: /* @__PURE__ */ new Date(),
584
- userId,
585
- projectId: this.projectId || "",
586
- organizationId: this.projectData?.organizationId || "",
587
- createAt: /* @__PURE__ */ new Date(),
588
- updateAt: /* @__PURE__ */ new Date(),
589
- createdBy: userId,
590
- updatedBy: userId
591
- });
592
- const _updateActionRecordSession = async () => await this.updateActionRecordSession();
575
+ await this.retryOperation(
576
+ () => (0, import_firestore3.setDoc)(ActionRecordSessionRef, {
577
+ id: _sessionId,
578
+ startTime: /* @__PURE__ */ new Date(),
579
+ endTime: /* @__PURE__ */ new Date(),
580
+ userId,
581
+ projectId: this.projectId || "",
582
+ organizationId: this.projectData?.organizationId || "",
583
+ createAt: /* @__PURE__ */ new Date(),
584
+ updateAt: /* @__PURE__ */ new Date(),
585
+ createdBy: userId,
586
+ updatedBy: userId
587
+ }).then(() => console.log(`ActionRecordSession created: ${_sessionId}`))
588
+ );
589
+ const events = [];
593
590
  this.stopFnForStore = (0, import_rrweb2.record)({
594
591
  packFn: import_packer2.pack,
595
592
  collectFonts: true,
@@ -617,18 +614,38 @@ var ActionLensRc = class {
617
614
  updatedBy: userId,
618
615
  hiddenBy: ""
619
616
  };
620
- await (0, import_firestore3.setDoc)(newDocRef, record2);
621
- await _updateActionRecordSession();
617
+ events.push(record2);
618
+ if (events.length >= 10) {
619
+ const batch = (0, import_firestore3.writeBatch)(db2);
620
+ events.forEach((evt) => {
621
+ const docRef = (0, import_firestore3.doc)(
622
+ (0, import_firestore3.collection)(db2, `${prefix}ActionRecord`),
623
+ evt.id
624
+ );
625
+ batch.set(docRef, evt);
626
+ });
627
+ await this.retryOperation(
628
+ () => batch.commit().then(
629
+ () => console.log(
630
+ `Batch write completed for ${events.length} events`
631
+ )
632
+ )
633
+ );
634
+ events.length = 0;
635
+ await this.updateActionRecordSession();
636
+ }
622
637
  } catch (error) {
623
- console.error("rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC:", error);
624
- throw error;
638
+ console.error(
639
+ `rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC (sessionId: ${_sessionId}):`,
640
+ error
641
+ );
625
642
  }
626
643
  },
627
644
  recordCanvas: false
628
645
  });
629
646
  this.startConsoleRecording();
630
647
  } catch (error) {
631
- console.error("rrweb\u9332\u753B\u30A8\u30E9\u30FC:", error);
648
+ console.error(`rrweb\u9332\u753B\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`, error);
632
649
  throw error;
633
650
  }
634
651
  }
@@ -637,6 +654,7 @@ var ActionLensRc = class {
637
654
  if (this.stopFnForStore) {
638
655
  this.stopFnForStore();
639
656
  this.stopFnForStore = null;
657
+ console.log("rrweb\u9332\u753B\u3092\u505C\u6B62\u3057\u307E\u3057\u305F");
640
658
  }
641
659
  } catch (error) {
642
660
  console.error("rrweb\u9332\u753B\u505C\u6B62\u30A8\u30E9\u30FC:", error);
@@ -655,19 +673,24 @@ var ActionLensRc = class {
655
673
  (0, import_firestore3.collection)(this.db, `${this.prefix}ActionRecordSession`),
656
674
  this.sessionId
657
675
  );
658
- await (0, import_firestore3.setDoc)(
659
- ActionRecordSessionRef,
660
- {
661
- endTime: /* @__PURE__ */ new Date(),
662
- updateAt: /* @__PURE__ */ new Date(),
663
- updatedBy: this.userId
664
- },
665
- { merge: true }
666
- ).catch((error) => {
667
- console.error("Error updating ActionRecordSession: ", error);
668
- });
676
+ await this.retryOperation(
677
+ () => (0, import_firestore3.setDoc)(
678
+ ActionRecordSessionRef,
679
+ {
680
+ endTime: /* @__PURE__ */ new Date(),
681
+ updateAt: /* @__PURE__ */ new Date(),
682
+ updatedBy: this.userId
683
+ },
684
+ { merge: true }
685
+ ).then(
686
+ () => console.log(`ActionRecordSession updated: ${this.sessionId}`)
687
+ )
688
+ );
669
689
  } catch (error) {
670
- console.error("ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC:", error);
690
+ console.error(
691
+ `ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`,
692
+ error
693
+ );
671
694
  throw error;
672
695
  }
673
696
  }