action-lens 1.0.52 → 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 };
@@ -496,8 +499,15 @@ var ActionLensRc = class {
496
499
  ];
497
500
  consoleMethods.forEach((method) => {
498
501
  console[method] = (...args) => {
499
- originalConsole[method](...args);
500
- 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
+ }
501
511
  };
502
512
  });
503
513
  } catch (error) {
@@ -505,32 +515,39 @@ var ActionLensRc = class {
505
515
  throw error;
506
516
  }
507
517
  }
508
- // タイムライン(ナビゲーションとパフォーマンス)をキャプチャ
509
518
  startTimelineRecording() {
510
519
  try {
511
520
  const userId = this.userId || "unknown";
512
521
  const sessionId = this.sessionId;
513
522
  const db2 = this.db;
514
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
+ }
515
528
  window.addEventListener("popstate", (event) => {
516
- if (!db2) {
517
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
518
- 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);
519
536
  }
520
- import_rrweb2.record.addCustomEvent("navigation", {
521
- url: window.location.href,
522
- state: event.state
523
- });
524
537
  });
525
538
  if (window.performance) {
526
539
  const observer = new PerformanceObserver((list) => {
527
540
  for (const entry of list.getEntries()) {
528
- import_rrweb2.record.addCustomEvent("performance", {
529
- name: entry.name,
530
- entryType: entry.entryType,
531
- startTime: entry.startTime,
532
- duration: entry.duration
533
- });
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
+ }
534
551
  }
535
552
  });
536
553
  observer.observe({ entryTypes: ["resource", "navigation", "paint"] });
@@ -549,26 +566,27 @@ var ActionLensRc = class {
549
566
  const db2 = this.db;
550
567
  const prefix = this.prefix;
551
568
  if (!db2) {
552
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
553
- return;
569
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
554
570
  }
555
571
  const ActionRecordSessionRef = (0, import_firestore3.doc)(
556
572
  (0, import_firestore3.collection)(db2, `${prefix}ActionRecordSession`),
557
573
  _sessionId
558
574
  );
559
- await (0, import_firestore3.setDoc)(ActionRecordSessionRef, {
560
- id: _sessionId,
561
- startTime: /* @__PURE__ */ new Date(),
562
- endTime: /* @__PURE__ */ new Date(),
563
- userId,
564
- projectId: this.projectId || "",
565
- organizationId: this.projectData?.organizationId || "",
566
- createAt: /* @__PURE__ */ new Date(),
567
- updateAt: /* @__PURE__ */ new Date(),
568
- createdBy: userId,
569
- updatedBy: userId
570
- });
571
- 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 = [];
572
590
  this.stopFnForStore = (0, import_rrweb2.record)({
573
591
  packFn: import_packer2.pack,
574
592
  collectFonts: true,
@@ -596,18 +614,38 @@ var ActionLensRc = class {
596
614
  updatedBy: userId,
597
615
  hiddenBy: ""
598
616
  };
599
- await (0, import_firestore3.setDoc)(newDocRef, record2);
600
- 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
+ }
601
637
  } catch (error) {
602
- console.error("rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC:", error);
603
- throw error;
638
+ console.error(
639
+ `rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC (sessionId: ${_sessionId}):`,
640
+ error
641
+ );
604
642
  }
605
643
  },
606
644
  recordCanvas: false
607
645
  });
608
646
  this.startConsoleRecording();
609
647
  } catch (error) {
610
- console.error("rrweb\u9332\u753B\u30A8\u30E9\u30FC:", error);
648
+ console.error(`rrweb\u9332\u753B\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`, error);
611
649
  throw error;
612
650
  }
613
651
  }
@@ -616,6 +654,7 @@ var ActionLensRc = class {
616
654
  if (this.stopFnForStore) {
617
655
  this.stopFnForStore();
618
656
  this.stopFnForStore = null;
657
+ console.log("rrweb\u9332\u753B\u3092\u505C\u6B62\u3057\u307E\u3057\u305F");
619
658
  }
620
659
  } catch (error) {
621
660
  console.error("rrweb\u9332\u753B\u505C\u6B62\u30A8\u30E9\u30FC:", error);
@@ -634,19 +673,24 @@ var ActionLensRc = class {
634
673
  (0, import_firestore3.collection)(this.db, `${this.prefix}ActionRecordSession`),
635
674
  this.sessionId
636
675
  );
637
- await (0, import_firestore3.setDoc)(
638
- ActionRecordSessionRef,
639
- {
640
- endTime: /* @__PURE__ */ new Date(),
641
- updateAt: /* @__PURE__ */ new Date(),
642
- updatedBy: this.userId
643
- },
644
- { merge: true }
645
- ).catch((error) => {
646
- console.error("Error updating ActionRecordSession: ", error);
647
- });
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
+ );
648
689
  } catch (error) {
649
- 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
+ );
650
694
  throw error;
651
695
  }
652
696
  }