action-lens 1.0.53 → 1.0.55

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 | null;
47
+ stopFnForStore: any;
48
48
  userId: string | null;
49
- db: Firestore;
49
+ db: Firestore | null;
50
50
  prefix: string;
51
51
  userData: User | null;
52
52
  projectId: string | null;
@@ -59,7 +59,6 @@ declare class ActionLensRc {
59
59
  };
60
60
  metaData: Record<string, any>;
61
61
  constructor();
62
- private retryOperation;
63
62
  init(_userId: string | null, projectId: string | null, userMeta: {
64
63
  name: string;
65
64
  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 | null;
47
+ stopFnForStore: any;
48
48
  userId: string | null;
49
- db: Firestore;
49
+ db: Firestore | null;
50
50
  prefix: string;
51
51
  userData: User | null;
52
52
  projectId: string | null;
@@ -59,7 +59,6 @@ declare class ActionLensRc {
59
59
  };
60
60
  metaData: Record<string, any>;
61
61
  constructor();
62
- private retryOperation;
63
62
  init(_userId: string | null, projectId: string | null, userMeta: {
64
63
  name: string;
65
64
  email: string;
package/dist/index.js CHANGED
@@ -332,160 +332,158 @@ 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");
336
335
  var ActionLensRc = class {
337
336
  sessionId = (0, import_uuid.v4)();
338
337
  stopFnForStore = null;
339
338
  userId = null;
340
- db;
339
+ db = null;
341
340
  prefix = "";
342
341
  userData = null;
343
342
  projectId = null;
344
343
  projectData = null;
345
344
  organizationId = null;
346
345
  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
+ this.db = db;
367
351
  }
368
352
  async init(_userId, projectId, userMeta, metaData = {}, _db = null, prefix = "") {
369
353
  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
- }
376
354
  this.originalUserId = _userId;
377
355
  console.log("ActionLensRc\u958B\u59CB");
356
+ if (!projectId) {
357
+ console.error("projectId\u304CNULL\u3067\u3059");
358
+ throw new Error("projectId\u304CNULL\u3067\u3059");
359
+ }
378
360
  this.projectId = projectId;
379
361
  this.prefix = prefix;
380
362
  this.userMeta = userMeta;
381
363
  this.metaData = metaData;
382
364
  if (!_userId) {
365
+ console.error("userId\u304CNULL\u3067\u3059");
383
366
  throw new Error("userId\u304CNULL\u3067\u3059");
384
367
  }
368
+ if (!this.db && _db) {
369
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
370
+ this.db = _db;
371
+ }
372
+ if (!this.db) {
373
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
374
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
375
+ }
385
376
  if (!this.projectId) {
386
- throw new Error("projectId\u304CNULL\u3067\u3059");
377
+ console.error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
378
+ throw new Error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
387
379
  }
388
- const projectDocRef = (0, import_firestore3.doc)(
389
- (0, import_firestore3.collection)(this.db, `${this.prefix}project`),
390
- this.projectId
380
+ const _doc = await (0, import_firestore3.getDoc)(
381
+ (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}project`), this.projectId)
391
382
  );
392
- const projectDoc = await this.retryOperation(() => (0, import_firestore3.getDoc)(projectDocRef));
393
- this.projectData = projectDoc.exists() ? projectDoc.data() : null;
383
+ this.projectData = _doc.data();
384
+ const projectData = this.projectData;
385
+ this.organizationId = projectData?.organizationId || "";
394
386
  if (!this.projectData) {
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
- )
387
+ await (0, import_firestore3.setDoc)(
388
+ (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}project`), this.projectId),
389
+ {
390
+ id: this.projectId,
391
+ name: this.projectId,
392
+ organizationId: "",
393
+ isActivate: false,
394
+ // デフォルトでアクティブに設定
395
+ createAt: /* @__PURE__ */ new Date(),
396
+ updateAt: /* @__PURE__ */ new Date(),
397
+ createdBy: this.originalUserId,
398
+ updatedBy: this.originalUserId
399
+ },
400
+ { merge: true }
410
401
  );
411
402
  throw new Error("\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");
412
403
  }
413
- if (!this.projectData.isActivate) {
404
+ if (!this.projectData?.isActivate) {
405
+ 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");
414
406
  throw new Error(
415
407
  "\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"
416
408
  );
417
409
  }
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
410
+ this.userId = projectData.organizationId + "-" + this.originalUserId;
411
+ this.prefix = this.prefix;
412
+ this.projectId = this.projectId;
413
+ const user = await (0, import_firestore3.getDoc)(
414
+ (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}user`), this.userId)
423
415
  );
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,
416
+ if (!this.db) {
417
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
418
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
419
+ }
420
+ const userData = user.data();
421
+ if (!userData.id || !user.exists()) {
422
+ await (0, import_firestore3.setDoc)(
423
+ (0, import_firestore3.doc)(
424
+ (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
425
+ projectData.organizationId + "-" + this.originalUserId
426
+ ),
427
+ {
428
+ ...this.userMeta || {},
429
+ metaData: this.metaData,
430
+ id: this.userId,
431
+ originalUserId: this.originalUserId,
432
+ type: "customer",
433
+ organizationId: projectData?.organizationId || "",
434
+ projectIds: [this.projectId],
435
+ createAt: /* @__PURE__ */ new Date(),
436
+ updateAt: /* @__PURE__ */ new Date(),
437
+ createdBy: this.userId,
438
+ updatedBy: this.userId,
439
+ hidden: false,
440
+ hiddenBy: ""
441
+ },
442
+ { merge: true }
443
+ );
444
+ this.userData = user.data();
445
+ } else {
446
+ this.userData = user.data();
447
+ if (!this.userData?.projectIds?.includes(this.projectId || "")) {
448
+ await (0, import_firestore3.setDoc)(
449
+ (0, import_firestore3.doc)(
450
+ (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
451
+ projectData.organizationId + "-" + this.originalUserId
452
+ ),
430
453
  {
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(),
454
+ projectIds: [
455
+ ...this.userData?.projectIds || [],
456
+ this.projectId
457
+ ],
439
458
  updateAt: /* @__PURE__ */ new Date(),
440
- createdBy: this.userId,
441
- updatedBy: this.userId,
442
- hidden: false,
443
- hiddenBy: ""
459
+ updatedBy: this.userId
444
460
  },
445
461
  { 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
- )
467
462
  );
468
463
  }
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
- )
464
+ if (this.userData?.organizationId !== projectData.organizationId) {
465
+ await (0, import_firestore3.setDoc)(
466
+ (0, import_firestore3.doc)(
467
+ (0, import_firestore3.collection)(this.db, `${this.prefix}user`),
468
+ projectData.organizationId + "-" + this.originalUserId
469
+ ),
470
+ {
471
+ organizationId: projectData.organizationId ? projectData.organizationId : this.userData?.organizationId,
472
+ updateAt: /* @__PURE__ */ new Date(),
473
+ updatedBy: this.userId
474
+ },
475
+ { merge: true }
480
476
  );
481
477
  }
482
478
  }
483
479
  await this.startRrwebRecordingForStore();
480
+ return;
484
481
  } catch (error) {
485
482
  console.error("\u521D\u671F\u5316\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
486
483
  throw error;
487
484
  }
488
485
  }
486
+ // コンソールログをキャプチャ
489
487
  startConsoleRecording() {
490
488
  try {
491
489
  const originalConsole = { ...console };
@@ -499,15 +497,8 @@ var ActionLensRc = class {
499
497
  ];
500
498
  consoleMethods.forEach((method) => {
501
499
  console[method] = (...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
- }
500
+ originalConsole[method](...args);
501
+ import_rrweb2.record.addCustomEvent("console", { method: "log", args });
511
502
  };
512
503
  });
513
504
  } catch (error) {
@@ -515,39 +506,32 @@ var ActionLensRc = class {
515
506
  throw error;
516
507
  }
517
508
  }
509
+ // タイムライン(ナビゲーションとパフォーマンス)をキャプチャ
518
510
  startTimelineRecording() {
519
511
  try {
520
512
  const userId = this.userId || "unknown";
521
513
  const sessionId = this.sessionId;
522
514
  const db2 = this.db;
523
515
  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
- }
528
516
  window.addEventListener("popstate", (event) => {
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);
517
+ if (!db2) {
518
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
519
+ return;
536
520
  }
521
+ import_rrweb2.record.addCustomEvent("navigation", {
522
+ url: window.location.href,
523
+ state: event.state
524
+ });
537
525
  });
538
526
  if (window.performance) {
539
527
  const observer = new PerformanceObserver((list) => {
540
528
  for (const entry of list.getEntries()) {
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
- }
529
+ import_rrweb2.record.addCustomEvent("performance", {
530
+ name: entry.name,
531
+ entryType: entry.entryType,
532
+ startTime: entry.startTime,
533
+ duration: entry.duration
534
+ });
551
535
  }
552
536
  });
553
537
  observer.observe({ entryTypes: ["resource", "navigation", "paint"] });
@@ -566,27 +550,26 @@ var ActionLensRc = class {
566
550
  const db2 = this.db;
567
551
  const prefix = this.prefix;
568
552
  if (!db2) {
569
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
553
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
554
+ return;
570
555
  }
571
556
  const ActionRecordSessionRef = (0, import_firestore3.doc)(
572
557
  (0, import_firestore3.collection)(db2, `${prefix}ActionRecordSession`),
573
558
  _sessionId
574
559
  );
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 = [];
560
+ await (0, import_firestore3.setDoc)(ActionRecordSessionRef, {
561
+ id: _sessionId,
562
+ startTime: /* @__PURE__ */ new Date(),
563
+ endTime: /* @__PURE__ */ new Date(),
564
+ userId,
565
+ projectId: this.projectId || "",
566
+ organizationId: this.projectData?.organizationId || "",
567
+ createAt: /* @__PURE__ */ new Date(),
568
+ updateAt: /* @__PURE__ */ new Date(),
569
+ createdBy: userId,
570
+ updatedBy: userId
571
+ });
572
+ const _updateActionRecordSession = async () => await this.updateActionRecordSession();
590
573
  this.stopFnForStore = (0, import_rrweb2.record)({
591
574
  packFn: import_packer2.pack,
592
575
  collectFonts: true,
@@ -614,38 +597,18 @@ var ActionLensRc = class {
614
597
  updatedBy: userId,
615
598
  hiddenBy: ""
616
599
  };
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
- }
600
+ await (0, import_firestore3.setDoc)(newDocRef, record2);
601
+ await _updateActionRecordSession();
637
602
  } catch (error) {
638
- console.error(
639
- `rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC (sessionId: ${_sessionId}):`,
640
- error
641
- );
603
+ console.error("rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC:", error);
604
+ throw error;
642
605
  }
643
606
  },
644
607
  recordCanvas: false
645
608
  });
646
609
  this.startConsoleRecording();
647
610
  } catch (error) {
648
- console.error(`rrweb\u9332\u753B\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`, error);
611
+ console.error("rrweb\u9332\u753B\u30A8\u30E9\u30FC:", error);
649
612
  throw error;
650
613
  }
651
614
  }
@@ -654,7 +617,6 @@ var ActionLensRc = class {
654
617
  if (this.stopFnForStore) {
655
618
  this.stopFnForStore();
656
619
  this.stopFnForStore = null;
657
- console.log("rrweb\u9332\u753B\u3092\u505C\u6B62\u3057\u307E\u3057\u305F");
658
620
  }
659
621
  } catch (error) {
660
622
  console.error("rrweb\u9332\u753B\u505C\u6B62\u30A8\u30E9\u30FC:", error);
@@ -673,24 +635,19 @@ var ActionLensRc = class {
673
635
  (0, import_firestore3.collection)(this.db, `${this.prefix}ActionRecordSession`),
674
636
  this.sessionId
675
637
  );
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
- );
638
+ await (0, import_firestore3.setDoc)(
639
+ ActionRecordSessionRef,
640
+ {
641
+ endTime: /* @__PURE__ */ new Date(),
642
+ updateAt: /* @__PURE__ */ new Date(),
643
+ updatedBy: this.userId
644
+ },
645
+ { merge: true }
646
+ ).catch((error) => {
647
+ console.error("Error updating ActionRecordSession: ", error);
648
+ });
689
649
  } catch (error) {
690
- console.error(
691
- `ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`,
692
- error
693
- );
650
+ console.error("ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC:", error);
694
651
  throw error;
695
652
  }
696
653
  }