-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove urlsafe-base64 dependency
- Loading branch information
Showing
9 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,28 @@ | ||
import { Buffer } from 'buffer'; | ||
// base64url is supported by node Buffer, but not in buffer package for browser compatibility | ||
// https://github.com/feross/buffer/issues/309 | ||
|
||
// Instead of using a node.js-only module and forcing us to polyfill the Buffer global, | ||
// we insert code from https://gitlab.com/seangenabe/safe-base64 here | ||
|
||
export function encodeBase64Url(buffer: Buffer) { | ||
if (!Buffer.isBuffer(buffer)) { | ||
throw new TypeError('`buffer` must be a buffer.'); | ||
} | ||
return buffer | ||
.toString('base64') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+/, ''); | ||
} | ||
|
||
export function decodeBase64Url(input: string) { | ||
if (!(typeof input === 'string')) { | ||
throw new TypeError('`input` must be a string.'); | ||
} | ||
|
||
const n = input.length % 4; | ||
const padded = input + '='.repeat(n > 0 ? 4 - n : n); | ||
const base64String = padded.replace(/-/g, '+').replace(/_/g, '/'); | ||
return Buffer.from(base64String, 'base64'); | ||
} |
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
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
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
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
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,35 @@ | ||
import assert from 'node:assert'; | ||
import { decodeBase64Url, encodeBase64Url } from '../../src/keri/core/base64'; | ||
|
||
test('encode', () => { | ||
assert.equal(encodeBase64Url(Buffer.from('f')), 'Zg'); | ||
assert.equal(encodeBase64Url(Buffer.from('fi')), 'Zmk'); | ||
assert.equal(encodeBase64Url(Buffer.from('fis')), 'Zmlz'); | ||
assert.equal(encodeBase64Url(Buffer.from('fish')), 'ZmlzaA'); | ||
assert.equal(encodeBase64Url(Buffer.from([248])), '-A'); | ||
assert.equal(encodeBase64Url(Buffer.from([252])), '_A'); | ||
}); | ||
|
||
test('decode', () => { | ||
assert.equal(decodeBase64Url('Zg').toString(), 'f'); | ||
assert.equal(decodeBase64Url('Zmk').toString(), 'fi'); | ||
assert.equal(decodeBase64Url('Zmlz').toString(), 'fis'); | ||
assert.equal(decodeBase64Url('ZmlzaA').toString(), 'fish'); | ||
assert.equal(Buffer.from([248]).buffer, decodeBase64Url('-A').buffer); | ||
assert.equal(Buffer.from([252]).buffer, decodeBase64Url('_A').buffer); | ||
}); | ||
|
||
test('Test encode / decode compare with built in node Buffer', () => { | ||
const text = '🏳️🏳️'; | ||
const b64url = '8J-Ps--4j_Cfj7PvuI8'; | ||
|
||
assert.equal( | ||
Buffer.from(text).toString('base64url'), | ||
encodeBase64Url(Buffer.from(text)) | ||
); | ||
|
||
assert.equal( | ||
Buffer.from(b64url, 'base64url').buffer, | ||
decodeBase64Url(b64url).buffer | ||
); | ||
}); |
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