Skip to content

Commit

Permalink
Add function to convert Dato URL-safe Base64-encoded IDs back to vali…
Browse files Browse the repository at this point in the history
…d UUID hex strings
  • Loading branch information
arcataroger committed Mar 22, 2024
1 parent 2e1c6f7 commit b6d5642
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/cma-client/__tests__/datoIdToUuid.test.ts
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);
});
});
36 changes: 36 additions & 0 deletions packages/cma-client/src/datoIdtoUuid.ts
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;
};
1 change: 1 addition & 0 deletions packages/cma-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type { ClientConfigOptions } from './generated/Client';
export * from './buildClient';
export * from './buildBlockRecord';
export * from './generateId';
export * from './datoIdtoUuid';
export * as SchemaTypes from './generated/SchemaTypes';
export * as SimpleSchemaTypes from './generated/SimpleSchemaTypes';

0 comments on commit b6d5642

Please sign in to comment.