| 49 | } |
| 50 | |
| 51 | export class Telemetry { |
| 52 | readonly sessionId: string |
| 53 | |
| 54 | private conf: Conf<any> | null |
| 55 | private distDir: string |
| 56 | private loadProjectId: undefined | string | Promise<string> |
| 57 | private NEXT_TELEMETRY_DISABLED: any |
| 58 | private NEXT_TELEMETRY_DEBUG: any |
| 59 | |
| 60 | private queue: Set<Promise<RecordObject>> |
| 61 | |
| 62 | constructor({ distDir }: { distDir: string }) { |
| 63 | // Read in the constructor so that .env can be loaded before reading |
| 64 | const { NEXT_TELEMETRY_DISABLED, NEXT_TELEMETRY_DEBUG } = process.env |
| 65 | this.NEXT_TELEMETRY_DISABLED = NEXT_TELEMETRY_DISABLED |
| 66 | this.NEXT_TELEMETRY_DEBUG = NEXT_TELEMETRY_DEBUG |
| 67 | this.distDir = distDir |
| 68 | const storageDirectory = getStorageDirectory(distDir) |
| 69 | |
| 70 | try { |
| 71 | // `conf` incorrectly throws a permission error during initialization |
| 72 | // instead of waiting for first use. We need to handle it, otherwise the |
| 73 | // process may crash. |
| 74 | this.conf = new Conf({ projectName: 'nextjs', cwd: storageDirectory }) |
| 75 | } catch (_) { |
| 76 | this.conf = null |
| 77 | } |
| 78 | this.sessionId = randomBytes(32).toString('hex') |
| 79 | this.queue = new Set() |
| 80 | |
| 81 | this.notify() |
| 82 | } |
| 83 | |
| 84 | private notify = () => { |
| 85 | if (this.isDisabled || !this.conf) { |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | // The end-user has already been notified about our telemetry integration. We |
| 90 | // don't need to constantly annoy them about it. |
| 91 | // We will re-inform users about the telemetry if significant changes are |
| 92 | // ever made. |
| 93 | if (this.conf.get(TELEMETRY_KEY_NOTIFY_DATE, '')) { |
| 94 | return |
| 95 | } |
| 96 | this.conf.set(TELEMETRY_KEY_NOTIFY_DATE, Date.now().toString()) |
| 97 | |
| 98 | console.log( |
| 99 | `${magenta( |
| 100 | bold('Attention') |
| 101 | )}: Next.js now collects completely anonymous telemetry regarding usage.` |
| 102 | ) |
| 103 | console.log( |
| 104 | `This information is used to shape Next.js' roadmap and prioritize features.` |
| 105 | ) |
| 106 | console.log( |
| 107 | `You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:` |
| 108 | ) |
nothing calls this directly
no test coverage detected