action-lens 1.0.53 → 1.0.54

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,157 @@ 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 = db;
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");
367
350
  }
368
351
  async init(_userId, projectId, userMeta, metaData = {}, _db = null, prefix = "") {
369
352
  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
353
  this.originalUserId = _userId;
377
354
  console.log("ActionLensRc\u958B\u59CB");
355
+ if (!projectId) {
356
+ console.error("projectId\u304CNULL\u3067\u3059");
357
+ throw new Error("projectId\u304CNULL\u3067\u3059");
358
+ }
378
359
  this.projectId = projectId;
379
360
  this.prefix = prefix;
380
361
  this.userMeta = userMeta;
381
362
  this.metaData = metaData;
382
363
  if (!_userId) {
364
+ console.error("userId\u304CNULL\u3067\u3059");
383
365
  throw new Error("userId\u304CNULL\u3067\u3059");
384
366
  }
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
+ }
385
375
  if (!this.projectId) {
386
- throw new Error("projectId\u304CNULL\u3067\u3059");
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");
387
378
  }
388
- const projectDocRef = (0, import_firestore3.doc)(
389
- (0, import_firestore3.collection)(this.db, `${this.prefix}project`),
390
- this.projectId
379
+ const _doc = await (0, import_firestore3.getDoc)(
380
+ (0, import_firestore3.doc)((0, import_firestore3.collection)(this.db, `${this.prefix}project`), this.projectId)
391
381
  );
392
- const projectDoc = await this.retryOperation(() => (0, import_firestore3.getDoc)(projectDocRef));
393
- this.projectData = projectDoc.exists() ? projectDoc.data() : null;
382
+ this.projectData = _doc.data();
383
+ const projectData = this.projectData;
384
+ this.organizationId = projectData?.organizationId || "";
394
385
  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
- )
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 }
410
400
  );
411
401
  throw new Error("\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");
412
402
  }
413
- if (!this.projectData.isActivate) {
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");
414
405
  throw new Error(
415
406
  "\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
407
  );
417
408
  }
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
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)
423
414
  );
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,
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
+ ),
430
452
  {
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(),
453
+ projectIds: [
454
+ ...this.userData?.projectIds || [],
455
+ this.projectId
456
+ ],
439
457
  updateAt: /* @__PURE__ */ new Date(),
440
- createdBy: this.userId,
441
- updatedBy: this.userId,
442
- hidden: false,
443
- hiddenBy: ""
458
+ updatedBy: this.userId
444
459
  },
445
460
  { 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
461
  );
468
462
  }
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
- )
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 }
480
475
  );
481
476
  }
482
477
  }
483
478
  await this.startRrwebRecordingForStore();
479
+ return;
484
480
  } catch (error) {
485
481
  console.error("\u521D\u671F\u5316\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
486
482
  throw error;
487
483
  }
488
484
  }
485
+ // コンソールログをキャプチャ
489
486
  startConsoleRecording() {
490
487
  try {
491
488
  const originalConsole = { ...console };
@@ -499,15 +496,8 @@ var ActionLensRc = class {
499
496
  ];
500
497
  consoleMethods.forEach((method) => {
501
498
  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
- }
499
+ originalConsole[method](...args);
500
+ import_rrweb2.record.addCustomEvent("console", { method: "log", args });
511
501
  };
512
502
  });
513
503
  } catch (error) {
@@ -515,39 +505,32 @@ var ActionLensRc = class {
515
505
  throw error;
516
506
  }
517
507
  }
508
+ // タイムライン(ナビゲーションとパフォーマンス)をキャプチャ
518
509
  startTimelineRecording() {
519
510
  try {
520
511
  const userId = this.userId || "unknown";
521
512
  const sessionId = this.sessionId;
522
513
  const db2 = this.db;
523
514
  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
515
  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);
516
+ if (!db2) {
517
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
518
+ return;
536
519
  }
520
+ import_rrweb2.record.addCustomEvent("navigation", {
521
+ url: window.location.href,
522
+ state: event.state
523
+ });
537
524
  });
538
525
  if (window.performance) {
539
526
  const observer = new PerformanceObserver((list) => {
540
527
  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
- }
528
+ import_rrweb2.record.addCustomEvent("performance", {
529
+ name: entry.name,
530
+ entryType: entry.entryType,
531
+ startTime: entry.startTime,
532
+ duration: entry.duration
533
+ });
551
534
  }
552
535
  });
553
536
  observer.observe({ entryTypes: ["resource", "navigation", "paint"] });
@@ -566,27 +549,26 @@ var ActionLensRc = class {
566
549
  const db2 = this.db;
567
550
  const prefix = this.prefix;
568
551
  if (!db2) {
569
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
552
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
553
+ return;
570
554
  }
571
555
  const ActionRecordSessionRef = (0, import_firestore3.doc)(
572
556
  (0, import_firestore3.collection)(db2, `${prefix}ActionRecordSession`),
573
557
  _sessionId
574
558
  );
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 = [];
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();
590
572
  this.stopFnForStore = (0, import_rrweb2.record)({
591
573
  packFn: import_packer2.pack,
592
574
  collectFonts: true,
@@ -614,38 +596,18 @@ var ActionLensRc = class {
614
596
  updatedBy: userId,
615
597
  hiddenBy: ""
616
598
  };
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
- }
599
+ await (0, import_firestore3.setDoc)(newDocRef, record2);
600
+ await _updateActionRecordSession();
637
601
  } catch (error) {
638
- console.error(
639
- `rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC (sessionId: ${_sessionId}):`,
640
- error
641
- );
602
+ console.error("rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC:", error);
603
+ throw error;
642
604
  }
643
605
  },
644
606
  recordCanvas: false
645
607
  });
646
608
  this.startConsoleRecording();
647
609
  } catch (error) {
648
- console.error(`rrweb\u9332\u753B\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`, error);
610
+ console.error("rrweb\u9332\u753B\u30A8\u30E9\u30FC:", error);
649
611
  throw error;
650
612
  }
651
613
  }
@@ -654,7 +616,6 @@ var ActionLensRc = class {
654
616
  if (this.stopFnForStore) {
655
617
  this.stopFnForStore();
656
618
  this.stopFnForStore = null;
657
- console.log("rrweb\u9332\u753B\u3092\u505C\u6B62\u3057\u307E\u3057\u305F");
658
619
  }
659
620
  } catch (error) {
660
621
  console.error("rrweb\u9332\u753B\u505C\u6B62\u30A8\u30E9\u30FC:", error);
@@ -673,24 +634,19 @@ var ActionLensRc = class {
673
634
  (0, import_firestore3.collection)(this.db, `${this.prefix}ActionRecordSession`),
674
635
  this.sessionId
675
636
  );
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
- );
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
+ });
689
648
  } catch (error) {
690
- console.error(
691
- `ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`,
692
- error
693
- );
649
+ console.error("ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC:", error);
694
650
  throw error;
695
651
  }
696
652
  }