forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change workspace id to 6 random characters (#100)
* change workspace id size to 6 Signed-off-by: Yulong Ruan <ruanyl@amazon.com> * added license header Signed-off-by: Yulong Ruan <ruanyl@amazon.com> --------- Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
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,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { generateRandomId } from './utils'; | ||
|
||
describe('workspace utils', () => { | ||
it('should generate id with the specified size', () => { | ||
expect(generateRandomId(6)).toHaveLength(6); | ||
}); | ||
|
||
it('should generate random IDs', () => { | ||
const NUM_OF_ID = 10000; | ||
const ids = new Set<string>(); | ||
for (let i = 0; i < NUM_OF_ID; i++) { | ||
ids.add(generateRandomId(6)); | ||
} | ||
expect(ids.size).toBe(NUM_OF_ID); | ||
}); | ||
}); |
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,13 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import crypto from 'crypto'; | ||
|
||
/** | ||
* Generate URL friendly random ID | ||
*/ | ||
export const generateRandomId = (size: number) => { | ||
return crypto.randomBytes(size).toString('base64url').slice(0, size); | ||
}; |
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