Skip to content

Commit 0652596

Browse files
committed
feat: b64urlFromBuffer()
Encode to URL-safe base64, from ArrayBuffer or Uint8Array
1 parent cc45c72 commit 0652596

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "@waiting/base64",
33
"author": "waiting",
44
"version": "4.1.0",
5-
"description": "Base64 encoding/decoding in pure JS on both modern Browsers and Node.js",
5+
"description": "Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64",
66
"keywords": [
77
"base64",
8+
"URL-safe",
89
"browser",
910
"Node.js",
1011
"rfc4648",

src/lib/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ export function b64urlEncode(
8181
}
8282

8383

84+
/**
85+
* Encode to URL-safe base64, source from ArrayBuffer or Uint8Array
86+
*
87+
* @see https://en.wikipedia.org/wiki/Base64#URL_applications
88+
*/
89+
export function b64urlFromBuffer(buffer: ArrayBuffer | Uint8Array): string {
90+
const b64 = b64fromBuffer(buffer)
91+
return b64toURLSafe(b64)
92+
}
93+
94+
8495
/**
8596
* Decode URL-safe base64 to original string.
8697
*

test/10_index.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ import * as assert from 'power-assert'
99
import rewire = require('rewire')
1010
import { TextDecoder, TextEncoder } from 'util'
1111

12-
import { b64decode, b64encode, b64fromBuffer, b64urlDecode, b64urlEncode } from '../src/index'
12+
import { b64decode, b64encode, b64fromBuffer, b64urlDecode, b64urlEncode, b64urlFromBuffer } from '../src/index'
1313
import { defaultConfig, ErrMsg } from '../src/lib/config'
1414

15-
import { input1, input2, input3, input4, input44, input5, input8, inputURLSafe, inputURLSafe2 } from './config'
15+
import {
16+
input1,
17+
input2,
18+
input3,
19+
input4,
20+
input44,
21+
input5,
22+
input8,
23+
inputURLSafe,
24+
inputURLSafe2,
25+
inputURLSafe3,
26+
} from './config'
1627

1728

1829
const filename = basename(__filename)
@@ -89,6 +100,7 @@ describe(filename, () => {
89100
assert(actual === expected, `Ensure that ${u8arr} serialise to ${expected}`)
90101
})
91102
})
103+
92104
})
93105

94106

@@ -141,4 +153,20 @@ describe(filename, () => {
141153
})
142154

143155

156+
describe('Should b64urlFromBuffer() works', () => {
157+
it('with valid input', () => {
158+
inputURLSafe3.forEach(row => {
159+
const u8arr = Uint8Array.from(row[0])
160+
const ret = b64urlFromBuffer(u8arr);
161+
162+
[...ret].forEach((ch, idx) => {
163+
const expect = row[1][idx]
164+
assert(expect === ch, `${expect}, ${ch}, ${idx}`)
165+
})
166+
})
167+
})
168+
169+
})
170+
171+
144172
})

test/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export const input8: Input8Item[] = [
115115
[ [0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87], '5Lit5paH'],
116116
]
117117

118-
119118
export const input9 = [
120119
' ',
121120
'*',
@@ -207,3 +206,7 @@ export const inputBase64URLInvalid = [
207206
'Q',
208207
'AABB\nAABB',
209208
]
209+
210+
export const inputURLSafe3: Array<[number[], string]> = [
211+
[ [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff], '__--_--_--__'],
212+
]

0 commit comments

Comments
 (0)