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.mjs CHANGED
@@ -312,166 +312,161 @@ import {
312
312
  doc as doc2,
313
313
  setDoc as setDoc2,
314
314
  collection as collection2,
315
- getDoc as getDoc2,
316
- getFirestore as getFirestore2,
317
- writeBatch,
318
- enableIndexedDbPersistence
315
+ getDoc as getDoc2
319
316
  } from "firebase/firestore";
320
317
  import { pack } from "@rrweb/packer";
321
- import { initializeApp as initializeApp2 } from "firebase/app";
322
318
  var ActionLensRc = class {
323
319
  sessionId = uuidv4();
324
320
  stopFnForStore = null;
325
321
  userId = null;
326
- db;
322
+ db = null;
327
323
  prefix = "";
328
324
  userData = null;
329
325
  projectId = null;
330
326
  projectData = null;
331
327
  organizationId = null;
332
328
  originalUserId = null;
329
+ // 元のユーザーID(外部システムのIDなど)
333
330
  userMeta = {};
334
331
  metaData = {};
335
332
  constructor() {
336
- const app2 = initializeApp2(firebaseConfig);
337
- this.db = getFirestore2(app2);
338
- enableIndexedDbPersistence(this.db).catch((err) => {
339
- console.warn("Persistence error:", err.message);
340
- });
341
- }
342
- async retryOperation(operation, maxAttempts = 3, delay = 1e3) {
343
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
344
- try {
345
- return await operation();
346
- } catch (error) {
347
- if (attempt === maxAttempts) throw error;
348
- console.warn(`Retry ${attempt}/${maxAttempts} failed:`, error);
349
- await new Promise((resolve) => setTimeout(resolve, delay));
350
- }
351
- }
352
- throw new Error("Retry operation failed");
333
+ this.db = db;
353
334
  }
354
335
  async init(_userId, projectId, userMeta, metaData = {}, _db = null, prefix = "") {
355
336
  try {
356
- if (_db) {
357
- this.db = _db;
358
- }
359
- if (!this.db) {
360
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
361
- }
362
337
  this.originalUserId = _userId;
363
338
  console.log("ActionLensRc\u958B\u59CB");
339
+ if (!projectId) {
340
+ console.error("projectId\u304CNULL\u3067\u3059");
341
+ throw new Error("projectId\u304CNULL\u3067\u3059");
342
+ }
364
343
  this.projectId = projectId;
365
344
  this.prefix = prefix;
366
345
  this.userMeta = userMeta;
367
346
  this.metaData = metaData;
368
347
  if (!_userId) {
348
+ console.error("userId\u304CNULL\u3067\u3059");
369
349
  throw new Error("userId\u304CNULL\u3067\u3059");
370
350
  }
351
+ if (!this.db && _db) {
352
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
353
+ this.db = _db;
354
+ }
355
+ if (!this.db) {
356
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
357
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
358
+ }
371
359
  if (!this.projectId) {
372
- throw new Error("projectId\u304CNULL\u3067\u3059");
360
+ console.error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
361
+ throw new Error("projectId\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
373
362
  }
374
- const projectDocRef = doc2(
375
- collection2(this.db, `${this.prefix}project`),
376
- this.projectId
363
+ const _doc = await getDoc2(
364
+ doc2(collection2(this.db, `${this.prefix}project`), this.projectId)
377
365
  );
378
- const projectDoc = await this.retryOperation(() => getDoc2(projectDocRef));
379
- this.projectData = projectDoc.exists() ? projectDoc.data() : null;
366
+ this.projectData = _doc.data();
367
+ const projectData = this.projectData;
368
+ this.organizationId = projectData?.organizationId || "";
380
369
  if (!this.projectData) {
381
- await this.retryOperation(
382
- () => setDoc2(
383
- projectDocRef,
384
- {
385
- id: this.projectId,
386
- name: this.projectId,
387
- organizationId: "",
388
- isActivate: false,
389
- createAt: /* @__PURE__ */ new Date(),
390
- updateAt: /* @__PURE__ */ new Date(),
391
- createdBy: this.originalUserId,
392
- updatedBy: this.originalUserId
393
- },
394
- { merge: true }
395
- )
370
+ await setDoc2(
371
+ doc2(collection2(this.db, `${this.prefix}project`), this.projectId),
372
+ {
373
+ id: this.projectId,
374
+ name: this.projectId,
375
+ organizationId: "",
376
+ isActivate: false,
377
+ // デフォルトでアクティブに設定
378
+ createAt: /* @__PURE__ */ new Date(),
379
+ updateAt: /* @__PURE__ */ new Date(),
380
+ createdBy: this.originalUserId,
381
+ updatedBy: this.originalUserId
382
+ },
383
+ { merge: true }
396
384
  );
397
385
  throw new Error("\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");
398
386
  }
399
- if (!this.projectData.isActivate) {
387
+ if (!this.projectData?.isActivate) {
388
+ 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");
400
389
  throw new Error(
401
390
  "\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"
402
391
  );
403
392
  }
404
- this.organizationId = this.projectData.organizationId || "";
405
- this.userId = `${this.projectData.organizationId}-${this.originalUserId}`;
406
- const userDocRef = doc2(
407
- collection2(this.db, `${this.prefix}user`),
408
- this.userId
393
+ this.userId = projectData.organizationId + "-" + this.originalUserId;
394
+ this.prefix = this.prefix;
395
+ this.projectId = this.projectId;
396
+ const user = await getDoc2(
397
+ doc2(collection2(this.db, `${this.prefix}user`), this.userId)
409
398
  );
410
- const userDoc = await this.retryOperation(() => getDoc2(userDocRef));
411
- this.userData = userDoc.exists() ? userDoc.data() : null;
412
- if (!this.userData || !userDoc.exists()) {
413
- await this.retryOperation(
414
- () => setDoc2(
415
- userDocRef,
399
+ if (!this.db) {
400
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
401
+ throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
402
+ }
403
+ const userData = user.data();
404
+ if (!userData.id || !user.exists()) {
405
+ await setDoc2(
406
+ doc2(
407
+ collection2(this.db, `${this.prefix}user`),
408
+ projectData.organizationId + "-" + this.originalUserId
409
+ ),
410
+ {
411
+ ...this.userMeta || {},
412
+ metaData: this.metaData,
413
+ id: this.userId,
414
+ originalUserId: this.originalUserId,
415
+ type: "customer",
416
+ organizationId: projectData?.organizationId || "",
417
+ projectIds: [this.projectId],
418
+ createAt: /* @__PURE__ */ new Date(),
419
+ updateAt: /* @__PURE__ */ new Date(),
420
+ createdBy: this.userId,
421
+ updatedBy: this.userId,
422
+ hidden: false,
423
+ hiddenBy: ""
424
+ },
425
+ { merge: true }
426
+ );
427
+ this.userData = user.data();
428
+ } else {
429
+ this.userData = user.data();
430
+ if (!this.userData?.projectIds?.includes(this.projectId || "")) {
431
+ await setDoc2(
432
+ doc2(
433
+ collection2(this.db, `${this.prefix}user`),
434
+ projectData.organizationId + "-" + this.originalUserId
435
+ ),
416
436
  {
417
- ...this.userMeta,
418
- metaData: this.metaData,
419
- id: this.userId,
420
- originalUserId: this.originalUserId,
421
- type: "customer",
422
- organizationId: this.projectData?.organizationId || "",
423
- projectIds: [this.projectId],
424
- createAt: /* @__PURE__ */ new Date(),
437
+ projectIds: [
438
+ ...this.userData?.projectIds || [],
439
+ this.projectId
440
+ ],
425
441
  updateAt: /* @__PURE__ */ new Date(),
426
- createdBy: this.userId,
427
- updatedBy: this.userId,
428
- hidden: false,
429
- hiddenBy: ""
442
+ updatedBy: this.userId
430
443
  },
431
444
  { merge: true }
432
- )
433
- );
434
- const updatedUserDoc = await this.retryOperation(
435
- () => getDoc2(userDocRef)
436
- );
437
- this.userData = updatedUserDoc.data();
438
- } else {
439
- if (!this.userData.projectIds?.includes(this.projectId)) {
440
- await this.retryOperation(
441
- () => setDoc2(
442
- userDocRef,
443
- {
444
- projectIds: [
445
- ...this.userData?.projectIds || [],
446
- this.projectId
447
- ],
448
- updateAt: /* @__PURE__ */ new Date(),
449
- updatedBy: this.userId
450
- },
451
- { merge: true }
452
- )
453
445
  );
454
446
  }
455
- if (this.userData.organizationId !== this.projectData.organizationId) {
456
- await this.retryOperation(
457
- () => setDoc2(
458
- userDocRef,
459
- {
460
- organizationId: this.projectData?.organizationId || this.userData?.organizationId,
461
- updateAt: /* @__PURE__ */ new Date(),
462
- updatedBy: this.userId
463
- },
464
- { merge: true }
465
- )
447
+ if (this.userData?.organizationId !== projectData.organizationId) {
448
+ await setDoc2(
449
+ doc2(
450
+ collection2(this.db, `${this.prefix}user`),
451
+ projectData.organizationId + "-" + this.originalUserId
452
+ ),
453
+ {
454
+ organizationId: projectData.organizationId ? projectData.organizationId : this.userData?.organizationId,
455
+ updateAt: /* @__PURE__ */ new Date(),
456
+ updatedBy: this.userId
457
+ },
458
+ { merge: true }
466
459
  );
467
460
  }
468
461
  }
469
462
  await this.startRrwebRecordingForStore();
463
+ return;
470
464
  } catch (error) {
471
465
  console.error("\u521D\u671F\u5316\u4E2D\u306B\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F:", error);
472
466
  throw error;
473
467
  }
474
468
  }
469
+ // コンソールログをキャプチャ
475
470
  startConsoleRecording() {
476
471
  try {
477
472
  const originalConsole = { ...console };
@@ -485,15 +480,8 @@ var ActionLensRc = class {
485
480
  ];
486
481
  consoleMethods.forEach((method) => {
487
482
  console[method] = (...args) => {
488
- try {
489
- originalConsole[method](...args);
490
- record.addCustomEvent("console", { method, args });
491
- } catch (error) {
492
- originalConsole.error(
493
- "\u30B3\u30F3\u30BD\u30FC\u30EB\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:",
494
- error
495
- );
496
- }
483
+ originalConsole[method](...args);
484
+ record.addCustomEvent("console", { method: "log", args });
497
485
  };
498
486
  });
499
487
  } catch (error) {
@@ -501,39 +489,32 @@ var ActionLensRc = class {
501
489
  throw error;
502
490
  }
503
491
  }
492
+ // タイムライン(ナビゲーションとパフォーマンス)をキャプチャ
504
493
  startTimelineRecording() {
505
494
  try {
506
495
  const userId = this.userId || "unknown";
507
496
  const sessionId = this.sessionId;
508
497
  const db2 = this.db;
509
498
  const prefix = this.prefix;
510
- if (!db2) {
511
- console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
512
- return;
513
- }
514
499
  window.addEventListener("popstate", (event) => {
515
- try {
516
- record.addCustomEvent("navigation", {
517
- url: window.location.href,
518
- state: event.state
519
- });
520
- } catch (error) {
521
- console.error("\u30CA\u30D3\u30B2\u30FC\u30B7\u30E7\u30F3\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:", error);
500
+ if (!db2) {
501
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
502
+ return;
522
503
  }
504
+ record.addCustomEvent("navigation", {
505
+ url: window.location.href,
506
+ state: event.state
507
+ });
523
508
  });
524
509
  if (window.performance) {
525
510
  const observer = new PerformanceObserver((list) => {
526
511
  for (const entry of list.getEntries()) {
527
- try {
528
- record.addCustomEvent("performance", {
529
- name: entry.name,
530
- entryType: entry.entryType,
531
- startTime: entry.startTime,
532
- duration: entry.duration
533
- });
534
- } catch (error) {
535
- console.error("\u30D1\u30D5\u30A9\u30FC\u30DE\u30F3\u30B9\u30A4\u30D9\u30F3\u30C8\u8A18\u9332\u30A8\u30E9\u30FC:", error);
536
- }
512
+ record.addCustomEvent("performance", {
513
+ name: entry.name,
514
+ entryType: entry.entryType,
515
+ startTime: entry.startTime,
516
+ duration: entry.duration
517
+ });
537
518
  }
538
519
  });
539
520
  observer.observe({ entryTypes: ["resource", "navigation", "paint"] });
@@ -552,27 +533,26 @@ var ActionLensRc = class {
552
533
  const db2 = this.db;
553
534
  const prefix = this.prefix;
554
535
  if (!db2) {
555
- throw new Error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
536
+ console.error("Firestore\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");
537
+ return;
556
538
  }
557
539
  const ActionRecordSessionRef = doc2(
558
540
  collection2(db2, `${prefix}ActionRecordSession`),
559
541
  _sessionId
560
542
  );
561
- await this.retryOperation(
562
- () => setDoc2(ActionRecordSessionRef, {
563
- id: _sessionId,
564
- startTime: /* @__PURE__ */ new Date(),
565
- endTime: /* @__PURE__ */ new Date(),
566
- userId,
567
- projectId: this.projectId || "",
568
- organizationId: this.projectData?.organizationId || "",
569
- createAt: /* @__PURE__ */ new Date(),
570
- updateAt: /* @__PURE__ */ new Date(),
571
- createdBy: userId,
572
- updatedBy: userId
573
- }).then(() => console.log(`ActionRecordSession created: ${_sessionId}`))
574
- );
575
- const events = [];
543
+ await setDoc2(ActionRecordSessionRef, {
544
+ id: _sessionId,
545
+ startTime: /* @__PURE__ */ new Date(),
546
+ endTime: /* @__PURE__ */ new Date(),
547
+ userId,
548
+ projectId: this.projectId || "",
549
+ organizationId: this.projectData?.organizationId || "",
550
+ createAt: /* @__PURE__ */ new Date(),
551
+ updateAt: /* @__PURE__ */ new Date(),
552
+ createdBy: userId,
553
+ updatedBy: userId
554
+ });
555
+ const _updateActionRecordSession = async () => await this.updateActionRecordSession();
576
556
  this.stopFnForStore = record({
577
557
  packFn: pack,
578
558
  collectFonts: true,
@@ -600,38 +580,18 @@ var ActionLensRc = class {
600
580
  updatedBy: userId,
601
581
  hiddenBy: ""
602
582
  };
603
- events.push(record2);
604
- if (events.length >= 10) {
605
- const batch = writeBatch(db2);
606
- events.forEach((evt) => {
607
- const docRef = doc2(
608
- collection2(db2, `${prefix}ActionRecord`),
609
- evt.id
610
- );
611
- batch.set(docRef, evt);
612
- });
613
- await this.retryOperation(
614
- () => batch.commit().then(
615
- () => console.log(
616
- `Batch write completed for ${events.length} events`
617
- )
618
- )
619
- );
620
- events.length = 0;
621
- await this.updateActionRecordSession();
622
- }
583
+ await setDoc2(newDocRef, record2);
584
+ await _updateActionRecordSession();
623
585
  } catch (error) {
624
- console.error(
625
- `rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC (sessionId: ${_sessionId}):`,
626
- error
627
- );
586
+ console.error("rrweb\u30A4\u30D9\u30F3\u30C8\u4FDD\u5B58\u30A8\u30E9\u30FC:", error);
587
+ throw error;
628
588
  }
629
589
  },
630
590
  recordCanvas: false
631
591
  });
632
592
  this.startConsoleRecording();
633
593
  } catch (error) {
634
- console.error(`rrweb\u9332\u753B\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`, error);
594
+ console.error("rrweb\u9332\u753B\u30A8\u30E9\u30FC:", error);
635
595
  throw error;
636
596
  }
637
597
  }
@@ -640,7 +600,6 @@ var ActionLensRc = class {
640
600
  if (this.stopFnForStore) {
641
601
  this.stopFnForStore();
642
602
  this.stopFnForStore = null;
643
- console.log("rrweb\u9332\u753B\u3092\u505C\u6B62\u3057\u307E\u3057\u305F");
644
603
  }
645
604
  } catch (error) {
646
605
  console.error("rrweb\u9332\u753B\u505C\u6B62\u30A8\u30E9\u30FC:", error);
@@ -659,24 +618,19 @@ var ActionLensRc = class {
659
618
  collection2(this.db, `${this.prefix}ActionRecordSession`),
660
619
  this.sessionId
661
620
  );
662
- await this.retryOperation(
663
- () => setDoc2(
664
- ActionRecordSessionRef,
665
- {
666
- endTime: /* @__PURE__ */ new Date(),
667
- updateAt: /* @__PURE__ */ new Date(),
668
- updatedBy: this.userId
669
- },
670
- { merge: true }
671
- ).then(
672
- () => console.log(`ActionRecordSession updated: ${this.sessionId}`)
673
- )
674
- );
621
+ await setDoc2(
622
+ ActionRecordSessionRef,
623
+ {
624
+ endTime: /* @__PURE__ */ new Date(),
625
+ updateAt: /* @__PURE__ */ new Date(),
626
+ updatedBy: this.userId
627
+ },
628
+ { merge: true }
629
+ ).catch((error) => {
630
+ console.error("Error updating ActionRecordSession: ", error);
631
+ });
675
632
  } catch (error) {
676
- console.error(
677
- `ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC (sessionId: ${this.sessionId}):`,
678
- error
679
- );
633
+ console.error("ActionRecordSession\u66F4\u65B0\u30A8\u30E9\u30FC:", error);
680
634
  throw error;
681
635
  }
682
636
  }