-
Notifications
You must be signed in to change notification settings - Fork 156
feat(commons): add fromBase64 helper function #2190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,35 @@ | ||
const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/; | ||
|
||
/** | ||
* Convert a base64 string to a Uint8Array. | ||
* | ||
* The input string must be a valid base64 string, otherwise an error will be thrown. | ||
* | ||
* The encoding parameter is optional and defaults to 'utf-8'. | ||
* | ||
* @example | ||
* ```ts | ||
* import { fromBase64 } from '@aws-lambda-powertools/commons/utils/base64'; | ||
* | ||
* const encodedValue = 'aGVsbG8gd29ybGQ='; | ||
* | ||
* const decoded = fromBase64(encodedValue); | ||
* // new Uint8Array([ 97, 71, 86, 115, 98, 71, 56, 103, 100, 50, 57, 121, 98, 71, 81, 61 ]); | ||
* ``` | ||
* | ||
* @param input The base64 string to convert to a Uint8Array | ||
* @param encoding The encoding of the input string (optional) | ||
*/ | ||
const fromBase64 = (input: string, encoding?: BufferEncoding): Uint8Array => { | ||
if ((input.length * 3) % 4 !== 0) { | ||
throw new TypeError(`Incorrect padding on base64 string.`); | ||
} | ||
if (!BASE64_REGEX.exec(input)) { | ||
throw new TypeError(`Invalid base64 string.`); | ||
} | ||
const buffer = encoding ? Buffer.from(input, encoding) : Buffer.from(input); | ||
|
||
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
}; | ||
|
||
export { fromBase64 }; |
This file contains hidden or 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,66 @@ | ||
/** | ||
* Test fromBase64 function | ||
* | ||
* @group unit/commons/fromBase64 | ||
*/ | ||
import { fromBase64 } from '../../src/fromBase64.js'; | ||
|
||
describe('Function: fromBase64', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
it('returns the Uint8Array from a base64 string', () => { | ||
// Prepare | ||
const base64 = 'aGVsbG8gd29ybGQ='; | ||
const expected = new Uint8Array([ | ||
97, 71, 86, 115, 98, 71, 56, 103, 100, 50, 57, 121, 98, 71, 81, 61, | ||
]); | ||
|
||
// Act | ||
const result = fromBase64(base64); | ||
|
||
// Assess | ||
expect(result).toStrictEqual(expected); | ||
}); | ||
|
||
it('throws a TypeError when the base64 string has incorrect padding', () => { | ||
// Prepare | ||
const base64 = 'aGVsbG8gd29ybGQ'; | ||
|
||
// Act | ||
const result = (): Uint8Array => fromBase64(base64); | ||
|
||
// Assess | ||
expect(result).toThrow(TypeError); | ||
expect(result).toThrow(`Incorrect padding on base64 string.`); | ||
}); | ||
|
||
it('throws a TypeError when the base64 string is invalid', () => { | ||
// Prepare | ||
const base64 = 'a-VsbG8gd29ybGQ='; | ||
|
||
// Act | ||
const result = (): Uint8Array => fromBase64(base64); | ||
|
||
// Assess | ||
expect(result).toThrow(TypeError); | ||
expect(result).toThrow(`Invalid base64 string.`); | ||
}); | ||
|
||
it('uses the provided encoding to create the Uint8Array', () => { | ||
// Prepare | ||
const base64 = 'aGVsbG8gd29ybGQ='; | ||
const encoding = 'utf8'; | ||
const expected = new Uint8Array([ | ||
97, 71, 86, 115, 98, 71, 56, 103, 100, 50, 57, 121, 98, 71, 81, 61, | ||
]); | ||
|
||
// Act | ||
const result = fromBase64(base64, encoding); | ||
|
||
// Assess | ||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.