| | import type { BinaryLike } from 'crypto' |
| | import { bold, cyan, magenta } from '../lib/picocolors' |
| | import Conf from 'next/dist/compiled/conf' |
| | import { createHash, randomBytes } from 'crypto' |
| | import isDockerFunction from 'next/dist/compiled/is-docker' |
| | import path from 'path' |
| |
|
| | import { getAnonymousMeta } from './anonymous-meta' |
| | import * as ciEnvironment from '../server/ci-info' |
| | import { postNextTelemetryPayload } from './post-telemetry-payload' |
| | import { getRawProjectId } from './project-id' |
| | import { AbortController } from 'next/dist/compiled/@edge-runtime/ponyfill' |
| | import fs from 'fs' |
| |
|
| | |
| | const TELEMETRY_KEY_ENABLED = 'telemetry.enabled' |
| |
|
| | |
| | |
| | const TELEMETRY_KEY_NOTIFY_DATE = 'telemetry.notifiedAt' |
| |
|
| | |
| | |
| | const TELEMETRY_KEY_ID = `telemetry.anonymousId` |
| |
|
| | |
| | |
| | |
| | |
| | const TELEMETRY_KEY_SALT = `telemetry.salt` |
| |
|
| | export type TelemetryEvent = { eventName: string; payload: object } |
| |
|
| | type RecordObject = { |
| | isFulfilled: boolean |
| | isRejected: boolean |
| | value?: any |
| | reason?: any |
| | } |
| |
|
| | function getStorageDirectory(distDir: string): string | undefined { |
| | const isLikelyEphemeral = ciEnvironment.isCI || isDockerFunction() |
| |
|
| | if (isLikelyEphemeral) { |
| | return path.join(distDir, 'cache') |
| | } |
| |
|
| | return undefined |
| | } |
| |
|
| | export class Telemetry { |
| | readonly sessionId: string |
| |
|
| | private conf: Conf<any> | null |
| | private distDir: string |
| | private loadProjectId: undefined | string | Promise<string> |
| | private NEXT_TELEMETRY_DISABLED: any |
| | private NEXT_TELEMETRY_DEBUG: any |
| |
|
| | private queue: Set<Promise<RecordObject>> |
| |
|
| | constructor({ distDir }: { distDir: string }) { |
| | |
| | const { NEXT_TELEMETRY_DISABLED, NEXT_TELEMETRY_DEBUG } = process.env |
| | this.NEXT_TELEMETRY_DISABLED = NEXT_TELEMETRY_DISABLED |
| | this.NEXT_TELEMETRY_DEBUG = NEXT_TELEMETRY_DEBUG |
| | this.distDir = distDir |
| | const storageDirectory = getStorageDirectory(distDir) |
| |
|
| | try { |
| | |
| | |
| | |
| | this.conf = new Conf({ projectName: 'nextjs', cwd: storageDirectory }) |
| | } catch (_) { |
| | this.conf = null |
| | } |
| | this.sessionId = randomBytes(32).toString('hex') |
| | this.queue = new Set() |
| |
|
| | this.notify() |
| | } |
| |
|
| | private notify = () => { |
| | if (this.isDisabled || !this.conf) { |
| | return |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | if (this.conf.get(TELEMETRY_KEY_NOTIFY_DATE, '')) { |
| | return |
| | } |
| | this.conf.set(TELEMETRY_KEY_NOTIFY_DATE, Date.now().toString()) |
| |
|
| | console.log( |
| | `${magenta( |
| | bold('Attention') |
| | )}: Next.js now collects completely anonymous telemetry regarding usage.` |
| | ) |
| | console.log( |
| | `This information is used to shape Next.js' roadmap and prioritize features.` |
| | ) |
| | console.log( |
| | `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:` |
| | ) |
| | console.log(cyan('https://nextjs.org/telemetry')) |
| | console.log() |
| | } |
| |
|
| | get anonymousId(): string { |
| | const val = this.conf && this.conf.get(TELEMETRY_KEY_ID) |
| | if (val) { |
| | return val |
| | } |
| |
|
| | const generated = randomBytes(32).toString('hex') |
| | this.conf && this.conf.set(TELEMETRY_KEY_ID, generated) |
| | return generated |
| | } |
| |
|
| | get salt(): string { |
| | const val = this.conf && this.conf.get(TELEMETRY_KEY_SALT) |
| | if (val) { |
| | return val |
| | } |
| |
|
| | const generated = randomBytes(16).toString('hex') |
| | this.conf && this.conf.set(TELEMETRY_KEY_SALT, generated) |
| | return generated |
| | } |
| |
|
| | private get isDisabled(): boolean { |
| | if (!!this.NEXT_TELEMETRY_DISABLED || !this.conf) { |
| | return true |
| | } |
| | return this.conf.get(TELEMETRY_KEY_ENABLED, true) === false |
| | } |
| |
|
| | setEnabled = (_enabled: boolean) => { |
| | const enabled = !!_enabled |
| | this.conf && this.conf.set(TELEMETRY_KEY_ENABLED, enabled) |
| | return this.conf && this.conf.path |
| | } |
| |
|
| | get isEnabled(): boolean { |
| | return ( |
| | !this.NEXT_TELEMETRY_DISABLED && |
| | !!this.conf && |
| | this.conf.get(TELEMETRY_KEY_ENABLED, true) !== false |
| | ) |
| | } |
| |
|
| | oneWayHash = (payload: BinaryLike): string => { |
| | const hash = createHash('sha256') |
| |
|
| | |
| | |
| | hash.update(this.salt) |
| |
|
| | |
| | |
| | hash.update(payload) |
| | return hash.digest('hex') |
| | } |
| |
|
| | private async getProjectId(): Promise<string> { |
| | this.loadProjectId = this.loadProjectId || getRawProjectId() |
| | return this.oneWayHash(await this.loadProjectId) |
| | } |
| |
|
| | record = ( |
| | _events: TelemetryEvent | TelemetryEvent[], |
| | deferred?: boolean |
| | ): Promise<RecordObject> => { |
| | const prom = ( |
| | deferred |
| | ? |
| | |
| | |
| | new Promise((resolve) => |
| | resolve({ |
| | isFulfilled: true, |
| | isRejected: false, |
| | value: _events, |
| | }) |
| | ) |
| | : this.submitRecord(_events) |
| | ) |
| | .then((value) => ({ |
| | isFulfilled: true, |
| | isRejected: false, |
| | value, |
| | })) |
| | .catch((reason) => ({ |
| | isFulfilled: false, |
| | isRejected: true, |
| | reason, |
| | })) |
| | |
| | .then((res) => { |
| | |
| | if (!deferred) { |
| | this.queue.delete(prom) |
| | } |
| | return res |
| | }) |
| |
|
| | ;(prom as any)._events = Array.isArray(_events) ? _events : [_events] |
| | ;(prom as any)._controller = (prom as any)._controller |
| | |
| | this.queue.add(prom) |
| |
|
| | return prom |
| | } |
| |
|
| | flush = async () => Promise.all(this.queue).catch(() => null) |
| |
|
| | |
| | |
| | |
| | flushDetached = (mode: 'dev', dir: string) => { |
| | const allEvents: TelemetryEvent[] = [] |
| |
|
| | this.queue.forEach((item: any) => { |
| | try { |
| | item._controller?.abort() |
| | allEvents.push(...item._events) |
| | } catch (_) { |
| | |
| | } |
| | }) |
| | fs.mkdirSync(this.distDir, { recursive: true }) |
| | fs.writeFileSync( |
| | path.join(this.distDir, '_events.json'), |
| | JSON.stringify(allEvents) |
| | ) |
| |
|
| | |
| | |
| | const child_process = |
| | require('child_process') as typeof import('child_process') |
| |
|
| | |
| | |
| | const spawn = this.NEXT_TELEMETRY_DEBUG |
| | ? child_process.spawnSync |
| | : child_process.spawn |
| |
|
| | spawn(process.execPath, [require.resolve('./detached-flush'), mode, dir], { |
| | detached: !this.NEXT_TELEMETRY_DEBUG, |
| | windowsHide: true, |
| | shell: false, |
| | ...(this.NEXT_TELEMETRY_DEBUG |
| | ? { |
| | stdio: 'inherit', |
| | } |
| | : {}), |
| | }) |
| | } |
| |
|
| | private submitRecord = async ( |
| | _events: TelemetryEvent | TelemetryEvent[] |
| | ): Promise<any> => { |
| | let events: TelemetryEvent[] |
| | if (Array.isArray(_events)) { |
| | events = _events |
| | } else { |
| | events = [_events] |
| | } |
| |
|
| | if (events.length < 1) { |
| | return Promise.resolve() |
| | } |
| |
|
| | if (this.NEXT_TELEMETRY_DEBUG) { |
| | |
| | events.forEach(({ eventName, payload }) => |
| | console.error( |
| | `[telemetry] ` + JSON.stringify({ eventName, payload }, null, 2) |
| | ) |
| | ) |
| | |
| | |
| | return Promise.resolve() |
| | } |
| |
|
| | |
| | if (this.isDisabled) { |
| | return Promise.resolve() |
| | } |
| |
|
| | const postController = new AbortController() |
| | const res = postNextTelemetryPayload( |
| | { |
| | context: { |
| | anonymousId: this.anonymousId, |
| | projectId: await this.getProjectId(), |
| | sessionId: this.sessionId, |
| | }, |
| | meta: getAnonymousMeta(), |
| | events: events.map(({ eventName, payload }) => ({ |
| | eventName, |
| | fields: payload, |
| | })), |
| | }, |
| | postController.signal |
| | ) |
| | res._controller = postController |
| | return res |
| | } |
| | } |
| |
|