Skip to content

Commit

Permalink
[Telemetry] Address package name limitations - Take 2 (#14259)
Browse files Browse the repository at this point in the history
* Introducing package name processing

* Add trimming if string length > 100

* Change files
  • Loading branch information
danielayala94 authored Jan 8, 2025
1 parent d42f2a3 commit 1ccad10
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
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);
}

0 comments on commit 1ccad10

Please sign in to comment.