-
Notifications
You must be signed in to change notification settings - Fork 452
chore(shared): replace telemetry postinstall with runtime notice #8549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacekradko
wants to merge
6
commits into
main
Choose a base branch
from
jacek/sdk-84-remove-telemetry-postinstall-notice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c69eeb
chore(shared): replace telemetry postinstall with runtime notice
jacekradko 6592f97
fix(shared): evade bundler static analysis for node: imports
jacekradko 8d23466
fix(shared): drop fs persistence and new Function from telemetry notice
jacekradko 2e0d12a
refactor(shared): scope telemetry notice to server-side only
jacekradko 554a192
fix(shared): detect server runtime without reading process.versions
jacekradko 9e5fe52
fix(shared): add curly braces to satisfy eslint curly rule
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Remove the `postinstall` lifecycle script that displayed the telemetry disclosure notice. The same one-time disclosure is now surfaced server-side at runtime when the telemetry collector activates on a development instance, deduped per process via a `globalThis` flag. The notice is intentionally not emitted in browser consoles to keep the noise profile in line with the original postinstall (terminal-only). This removes install-time code from the published package and drops the `std-env` dependency. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { __resetTelemetryNoticeForTests, maybeShowTelemetryNotice } from '../telemetry/notice'; | ||
|
|
||
| const CI_VARS = [ | ||
| 'CI', | ||
| 'CONTINUOUS_INTEGRATION', | ||
| 'BUILD_NUMBER', | ||
| 'GITHUB_ACTIONS', | ||
| 'GITLAB_CI', | ||
| 'CIRCLECI', | ||
| 'TRAVIS', | ||
| 'BUILDKITE', | ||
| 'JENKINS_URL', | ||
| 'TF_BUILD', | ||
| 'DRONE', | ||
| 'CODEBUILD_BUILD_ID', | ||
| ]; | ||
|
|
||
| function clearCIEnv() { | ||
| for (const name of CI_VARS) { | ||
| delete process.env[name]; | ||
| } | ||
| } | ||
|
|
||
| describe('maybeShowTelemetryNotice', () => { | ||
| let logSpy: ReturnType<typeof vi.spyOn>; | ||
| let originalCIEnv: Record<string, string | undefined>; | ||
|
|
||
| beforeEach(() => { | ||
| originalCIEnv = Object.fromEntries(CI_VARS.map(name => [name, process.env[name]])); | ||
| clearCIEnv(); | ||
| __resetTelemetryNoticeForTests(); | ||
| logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| logSpy.mockRestore(); | ||
| for (const [name, value] of Object.entries(originalCIEnv)) { | ||
| if (typeof value === 'string') { | ||
| process.env[name] = value; | ||
| } else { | ||
| delete process.env[name]; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('prints the disclosure on Node', () => { | ||
| maybeShowTelemetryNotice(); | ||
|
|
||
| expect(logSpy).toHaveBeenCalled(); | ||
| const printed = logSpy.mock.calls.map(call => String(call[0])).join('\n'); | ||
| expect(printed).toMatch(/Clerk collects telemetry/); | ||
| expect(printed).toMatch(/clerk\.com\/docs\/telemetry/); | ||
| }); | ||
|
|
||
| test('does not print again on subsequent calls in the same process', () => { | ||
| maybeShowTelemetryNotice(); | ||
| maybeShowTelemetryNotice(); | ||
| maybeShowTelemetryNotice(); | ||
|
|
||
| const disclosureCalls = logSpy.mock.calls.filter(call => /Clerk collects telemetry/.test(String(call[0]))); | ||
| expect(disclosureCalls).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('skips entirely when skip:true is passed', () => { | ||
| maybeShowTelemetryNotice({ skip: true }); | ||
|
|
||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('skips when a CI env var is set', () => { | ||
| // eslint-disable-next-line turbo/no-undeclared-env-vars | ||
| process.env.CI = 'true'; | ||
|
|
||
| maybeShowTelemetryNotice(); | ||
|
|
||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test.each(CI_VARS)('skips when %s is set', name => { | ||
| process.env[name] = '1'; | ||
|
|
||
| maybeShowTelemetryNotice(); | ||
|
|
||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('skips in a browser-like environment', () => { | ||
| const original = (globalThis as { window?: unknown }).window; | ||
| (globalThis as { window?: unknown }).window = {}; | ||
|
|
||
| try { | ||
| maybeShowTelemetryNotice(); | ||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| } finally { | ||
| if (typeof original === 'undefined') { | ||
| delete (globalThis as { window?: unknown }).window; | ||
| } else { | ||
| (globalThis as { window?: unknown }).window = original; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('skips in Next.js Edge Runtime', () => { | ||
| (globalThis as { EdgeRuntime?: string }).EdgeRuntime = 'edge-runtime'; | ||
|
|
||
| try { | ||
| maybeShowTelemetryNotice(); | ||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| } finally { | ||
| delete (globalThis as { EdgeRuntime?: string }).EdgeRuntime; | ||
| } | ||
| }); | ||
|
|
||
| test('does not throw if console.log fails', () => { | ||
| logSpy.mockImplementation(() => { | ||
| throw new Error('console broken'); | ||
| }); | ||
|
|
||
| expect(() => maybeShowTelemetryNotice()).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * One-time runtime disclosure that Clerk collects telemetry from development instances. | ||
| * | ||
| * Replaces the previous `postinstall` script. Disclosure is intentionally surfaced | ||
| * only on Node (server-side) so the noise profile matches the original postinstall | ||
| * (terminal-only, dev-eyes-only). Browser consoles are not used because they are | ||
| * frequently observed by non-developers (QA, screenshots, demos), and adding another | ||
| * console warning is a common source of customer complaints. | ||
| * | ||
| * Persistence is in-process via a `globalThis` Symbol, which survives Next.js HMR | ||
| * module reloads. No filesystem access, no `node:` imports, no dynamic-code APIs, so | ||
| * the module remains safe to bundle for Edge Runtime, Workers, and any browser path. | ||
| * | ||
| * All work is wrapped in try/catch. Failure to display the notice must never affect | ||
| * the SDK. | ||
| */ | ||
|
|
||
| import { isTruthy } from '../underscore'; | ||
|
|
||
| const PROCESS_FLAG = Symbol.for('@clerk/shared.telemetryNoticeShown'); | ||
|
|
||
| const NOTICE_LINES = [ | ||
| 'Attention: Clerk collects telemetry data from its SDKs when connected to development instances.', | ||
| "The data collected is used to inform Clerk's product roadmap.", | ||
| 'To learn more, including how to opt-out from the telemetry program, visit: https://clerk.com/docs/telemetry.', | ||
| ]; | ||
|
|
||
| const CI_ENV_VARS = [ | ||
| 'CI', | ||
| 'CONTINUOUS_INTEGRATION', | ||
| 'BUILD_NUMBER', | ||
| 'GITHUB_ACTIONS', | ||
| 'GITLAB_CI', | ||
| 'CIRCLECI', | ||
| 'TRAVIS', | ||
| 'BUILDKITE', | ||
| 'JENKINS_URL', | ||
| 'TF_BUILD', | ||
| 'DRONE', | ||
| 'CODEBUILD_BUILD_ID', | ||
| ]; | ||
|
|
||
| function isServerRuntime(): boolean { | ||
| // Skip in browsers. | ||
| if (typeof window !== 'undefined') { | ||
| return false; | ||
| } | ||
| // Skip in Next.js Edge Runtime, which exposes a global `EdgeRuntime` marker. We detect via | ||
| // this marker (rather than checking `process.versions`) because the Edge Runtime build-time | ||
| // analyzer flags any reachable read of `process.versions` even when it sits behind a guard. | ||
| if (typeof (globalThis as { EdgeRuntime?: string }).EdgeRuntime !== 'undefined') { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function isCI(): boolean { | ||
| if (typeof process === 'undefined' || !process.env) { | ||
| return false; | ||
| } | ||
| return CI_ENV_VARS.some(name => isTruthy(process.env[name])); | ||
| } | ||
|
|
||
| function hasSeen(): boolean { | ||
| return Boolean((globalThis as Record<symbol, unknown>)[PROCESS_FLAG]); | ||
| } | ||
|
|
||
| function markSeen(): void { | ||
| (globalThis as Record<symbol, unknown>)[PROCESS_FLAG] = true; | ||
| } | ||
|
|
||
| function printNotice(): void { | ||
| if (typeof console === 'undefined' || typeof console.log !== 'function') { | ||
| return; | ||
| } | ||
| for (const line of NOTICE_LINES) { | ||
| console.log(line); | ||
| } | ||
| console.log(''); | ||
| } | ||
|
|
||
| export type MaybeShowTelemetryNoticeOptions = { | ||
| /** | ||
| * Skip the notice entirely. Used when the caller has already determined that no | ||
| * telemetry will be sent (e.g. opt-out, non-development instance), in which case | ||
| * there is nothing to disclose. | ||
| */ | ||
| skip?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Display the one-time telemetry disclosure on server runtimes if it has not already been | ||
| * shown in this process. Browser and Edge Runtime callers are silently skipped. Never throws. | ||
| */ | ||
| export function maybeShowTelemetryNotice(options: MaybeShowTelemetryNoticeOptions = {}): void { | ||
| if (options.skip) { | ||
| return; | ||
| } | ||
| try { | ||
| if (!isServerRuntime()) { | ||
| return; | ||
| } | ||
| if (isCI()) { | ||
| return; | ||
| } | ||
| if (hasSeen()) { | ||
| return; | ||
| } | ||
| printNotice(); | ||
| markSeen(); | ||
| } catch { | ||
| // never let disclosure break the SDK | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test-only: clear the in-process flag so the next call re-runs the gating logic. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function __resetTelemetryNoticeForTests(): void { | ||
| delete (globalThis as Record<symbol, unknown>)[PROCESS_FLAG]; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.