-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to convert Dato URL-safe Base64-encoded IDs back to vali…
…d UUID hex strings
- Loading branch information
1 parent
2e1c6f7
commit b6d5642
Showing
3 changed files
with
58 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 @@ | ||
import { validate } from 'uuid'; | ||
import { datoIdToUuid } from '../src/datoIdToUuid'; | ||
import { generateId } from '../src/generateId'; | ||
|
||
describe('datoIdToUuid', () => { | ||
it('converts an example URL-safe Base64 DatoID to a valid UUID hex string', () => { | ||
const base64url = 'WTyssHtyTzu9_EbszSVhPw'; | ||
const expectedUuid = '593cacb0-7b72-4f3b-bdfc-46eccd25613f'; | ||
|
||
const result = datoIdToUuid(base64url); | ||
|
||
expect(result).toBe(expectedUuid); | ||
expect(validate(result)).toBe(true); | ||
}); | ||
|
||
it('expects an ID from generateId() to validate as a v4 UUID', () => { | ||
const base64Id = generateId(); | ||
const uuid = datoIdToUuid(base64Id); | ||
expect(validate(uuid)).toBe(true); | ||
}); | ||
}); |
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,36 @@ | ||
import { stringify } from 'uuid'; | ||
function fromBase64toUint8Array(base64String: string) { | ||
// Check if Buffer is available, indicating a Node.js environment | ||
if (typeof Buffer !== 'undefined') { | ||
// Use Buffer to convert base64 to a Uint8Array | ||
return new Uint8Array(Buffer.from(base64String, 'base64')); | ||
} else { | ||
// Assuming a browser environment if Buffer is not available | ||
// Use atob to decode base64 to a binary string | ||
const binaryString = atob(base64String); | ||
|
||
// Create a Uint8Array of the same length as the binary string | ||
const bytes = new Uint8Array(binaryString.length); | ||
|
||
// Convert each character to its byte value | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
return bytes; | ||
} | ||
} | ||
|
||
export const datoIdToUuid = (b64url: string): string => { | ||
// Undo the URL encoding we did in generateId() | ||
const reversedUrlEncoding: string = `${b64url | ||
.replace(/-/g, '+') | ||
.replace(/_/g, '/')}==`; | ||
|
||
// Decode the B64 back into a Uint8Array | ||
const encodedAsBuffer = fromBase64toUint8Array(reversedUrlEncoding); | ||
|
||
// Stringify the UUID into a hex string | ||
const stringified = stringify(encodedAsBuffer); | ||
|
||
return stringified; | ||
}; |
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