-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Telemetry] Address package name limitations - Take 2 (#14259)
* Introducing package name processing * Add trimming if string length > 100 * Change files
- Loading branch information
1 parent
d42f2a3
commit 1ccad10
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@react-native-windows-telemetry-f67eede7-8e2c-46b7-9ce9-5d664dfcd76e.json
This file contains 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,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "Address package name limitations for telemetry", | ||
"packageName": "@react-native-windows/telemetry", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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
62 changes: 62 additions & 0 deletions
62
packages/@react-native-windows/telemetry/src/test/nameUtils.test.ts
This file contains 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,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
18
packages/@react-native-windows/telemetry/src/utils/nameUtils.ts
This file contains 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,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); | ||
} |