Skip to content
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

[Telemetry] Address package name limitations - Take 2 #14259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Address package name limitations for telemetry",
"packageName": "@react-native-windows/telemetry",
"email": "[email protected]",
"dependentChangeType": "patch"
}
10 changes: 8 additions & 2 deletions packages/@react-native-windows/telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as basePropUtils from './utils/basePropUtils';
import * as versionUtils from './utils/versionUtils';
import * as errorUtils from './utils/errorUtils';
import * as projectUtils from './utils/projectUtils';
import * as nameUtils from './utils/nameUtils';

export interface TelemetryOptions {
setupString: string;
Expand Down Expand Up @@ -246,10 +247,15 @@ export class Telemetry {
return true;
}

if (forceRefresh === true || !Telemetry.versionsProp[name]) {
// Process the package name to comply with the backend requirements
const packageName = nameUtils.isValidTelemetryPackageName(name)
? name
: nameUtils.cleanTelemetryPackageName(name);

if (forceRefresh === true || !Telemetry.versionsProp[packageName]) {
const value = await getValue();
if (value) {
Telemetry.versionsProp[name] = value;
Telemetry.versionsProp[packageName] = value;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* @format
*/

import * as nameUtils from '../utils/nameUtils';

test('Verify telemetry package name is valid', () => {
expect(nameUtils.isValidTelemetryPackageName('package')).toBe(true);
expect(nameUtils.isValidTelemetryPackageName('@react')).toBe(false);
expect(nameUtils.isValidTelemetryPackageName('react-native')).toBe(false);
expect(nameUtils.isValidTelemetryPackageName('react_native')).toBe(true);
expect(nameUtils.isValidTelemetryPackageName('react_native/cli')).toBe(false);

// Check for size limits. A valid package name has 100 characters or less.
expect(
nameUtils.isValidTelemetryPackageName(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nat',
),
).toBe(true);
expect(
nameUtils.isValidTelemetryPackageName(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nati',
),
).toBe(false);
});

test('Verify telemetry package name cleaning', () => {
expect(nameUtils.cleanTelemetryPackageName('package')).toBe('package');
expect(nameUtils.cleanTelemetryPackageName('@react')).toBe('_react');
expect(nameUtils.cleanTelemetryPackageName('react-native')).toBe(
'react_native',
);
expect(nameUtils.cleanTelemetryPackageName('react_native')).toBe(
'react_native',
);
expect(nameUtils.cleanTelemetryPackageName('react_native/cli')).toBe(
'react_native_cli',
);
expect(nameUtils.cleanTelemetryPackageName('@react-native-windows/cli')).toBe(
'_react_native_windows_cli',
);

expect(
nameUtils.cleanTelemetryPackageName(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nat',
),
).toBe(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nat',
);

// Truncate a package name with 101 characters, to the first 100.
expect(
nameUtils.cleanTelemetryPackageName(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nati',
),
).toBe(
'react_native_react_native_react_native_react_native_react_native_react_native_react_native_react_nat',
);
});
18 changes: 18 additions & 0 deletions packages/@react-native-windows/telemetry/src/utils/nameUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
* @format
*/

export function isValidTelemetryPackageName(name: string): boolean {
// Accepted characters: alphanumeric, underscore, dot, starts with letter.
// Size: 1-100 characters.
if (name.match(/^[a-zA-Z][a-zA-Z0-9_.]{0,99}$/gi)) {
return true;
}
return false;
}

export function cleanTelemetryPackageName(str: string): string {
return str.replace(/[^a-zA-Z0-9_.]/g, '_').slice(0, 100);
}
Loading