Skip to content

Commit 6eb04a8

Browse files
committed
arg -> str
1 parent 1ae8ccd commit 6eb04a8

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

base64.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,55 @@ export function toBase64url(arr) {
4343
// Unlike Uint8Array.fromBase64(), accepts both base64 and base64url
4444
// NOTE: Always operates in strict mode for last chunk
4545

46-
export function fromBase64(arg, format = 'uint8') {
47-
if (typeof arg !== 'string') throw new TypeError('Input is not a string')
46+
export function fromBase64(str, format = 'uint8') {
47+
if (typeof str !== 'string') throw new TypeError('Input is not a string')
4848

4949
// These checks should be needed only for Buffer path, not Uint8Array.fromBase64 path, but JSC lacks proper checks
50-
assert(arg.length % 4 !== 1, 'Invalid base64 length') // JSC misses this in fromBase64
51-
if (arg.endsWith('=')) {
52-
assert(arg.length % 4 === 0, 'Invalid padded length') // JSC misses this too
53-
assert(arg[arg.length - 3] !== '=', 'Excessive padding') // no more than two = at the end
50+
assert(str.length % 4 !== 1, 'Invalid base64 length') // JSC misses this in fromBase64
51+
if (str.endsWith('=')) {
52+
assert(str.length % 4 === 0, 'Invalid padded length') // JSC misses this too
53+
assert(str[str.length - 3] !== '=', 'Excessive padding') // no more than two = at the end
5454
}
5555

56-
assert(!/[^0-9a-z=+/]/iu.test(arg), 'Invalid character in base64 input')
57-
return fromTypedArray(fromBase64common(arg, false), format)
56+
assert(!/[^0-9a-z=+/]/iu.test(str), 'Invalid character in base64 input')
57+
return fromTypedArray(fromBase64common(str, false), format)
5858
}
5959

60-
export function fromBase64url(arg, format = 'uint8') {
61-
if (typeof arg !== 'string') throw new TypeError('Input is not a string')
60+
export function fromBase64url(str, format = 'uint8') {
61+
if (typeof str !== 'string') throw new TypeError('Input is not a string')
6262

6363
// These checks should be needed only for Buffer path, not Uint8Array.fromBase64 path, but JSC lacks proper checks
64-
assert(arg.length % 4 !== 1, 'Invalid base64 length') // JSC misses this in fromBase64
65-
assert(!arg.includes('='), 'Did not expect padding in base64url input')
64+
assert(str.length % 4 !== 1, 'Invalid base64 length') // JSC misses this in fromBase64
65+
assert(!str.includes('='), 'Did not expect padding in base64url input')
6666

67-
assert(!/[^0-9a-z_-]/iu.test(arg), 'Invalid character in base64url input')
68-
return fromTypedArray(fromBase64common(arg, true), format)
67+
assert(!/[^0-9a-z_-]/iu.test(str), 'Invalid character in base64url input')
68+
return fromTypedArray(fromBase64common(str, true), format)
6969
}
7070

7171
const { atob } = globalThis
7272

73-
function fromBase64common(arg, isBase64url) {
73+
function fromBase64common(str, isBase64url) {
7474
if (Uint8Array.fromBase64) {
7575
const options = { alphabet: isBase64url ? 'base64url' : 'base64', lastChunkHandling: 'strict' }
76-
const padded = arg.length % 4 > 0 ? `${arg}${'='.repeat(4 - (arg.length % 4))}` : arg
76+
const padded = str.length % 4 > 0 ? `${str}${'='.repeat(4 - (str.length % 4))}` : str
7777
return Uint8Array.fromBase64(padded, options)
7878
}
7979

8080
let arr
8181
if (!haveNativeBuffer && atob) {
8282
// atob is faster than manual parsing on Hermes
83-
const str = atob(isBase64url ? arg.replaceAll('-', '+').replaceAll('_', '/') : arg)
84-
arr = new Uint8Array(str.length)
85-
for (let i = 0; i < str.length; i++) arr[i] = str.charCodeAt(i)
83+
const raw = atob(isBase64url ? str.replaceAll('-', '+').replaceAll('_', '/') : str)
84+
arr = new Uint8Array(raw.length)
85+
for (let i = 0; i < raw.length; i++) arr[i] = raw.charCodeAt(i)
8686
} else {
87-
assert(!arg.includes('=') || !/=[^=]/iu.test(arg), 'Invalid input after padding')
88-
arr = haveNativeBuffer ? Buffer.from(arg, 'base64') : fromBase64js(arg)
87+
assert(!str.includes('=') || !/=[^=]/iu.test(str), 'Invalid input after padding')
88+
arr = haveNativeBuffer ? Buffer.from(str, 'base64') : fromBase64js(str)
8989
}
9090

9191
if (arr.length % 3 !== 0) {
9292
// Check last chunk to be strict if it was incomplete
9393
const expected = toBase64(arr.subarray(-(arr.length % 3)))
94-
const last = arg.length % 4 === 0 ? arg.slice(-4) : arg.slice(-(arg.length % 4)).padEnd(4, '=')
94+
const last = str.length % 4 === 0 ? str.slice(-4) : str.slice(-(str.length % 4)).padEnd(4, '=')
9595
const actual = isBase64url ? last.replaceAll('-', '+').replaceAll('_', '/') : last
9696
if (expected !== actual) throw new Error('Invalid last chunk')
9797
}

hex.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export function toHex(arr) {
2424
}
2525

2626
// Unlike Buffer.from(), throws on invalid input
27-
export function fromHex(arg, format = 'uint8') {
28-
if (Uint8Array.fromHex) return fromTypedArray(Uint8Array.fromHex(arg), format)
29-
if (typeof arg !== 'string') throw new TypeError('Input is not a string')
30-
assert(arg.length % 2 === 0, 'Input is not a hex string')
27+
export function fromHex(str, format = 'uint8') {
28+
if (Uint8Array.fromHex) return fromTypedArray(Uint8Array.fromHex(str), format)
29+
if (typeof str !== 'string') throw new TypeError('Input is not a string')
30+
assert(str.length % 2 === 0, 'Input is not a hex string')
3131
if (haveNativeBuffer) {
32-
assert(!/[^0-9a-f]/iu.test(arg), 'Input is not a hex string')
33-
return fromTypedArray(Buffer.from(arg, 'hex'), format)
32+
assert(!/[^0-9a-f]/iu.test(str), 'Input is not a hex string')
33+
return fromTypedArray(Buffer.from(str, 'hex'), format)
3434
}
3535

3636
if (!dehexArray) {
@@ -41,10 +41,10 @@ export function fromHex(arg, format = 'uint8') {
4141
}
4242
}
4343

44-
const arr = new Uint8Array(arg.length / 2)
44+
const arr = new Uint8Array(str.length / 2)
4545
let j = 0
4646
for (let i = 0; i < arr.length; i++) {
47-
const a = dehexArray[arg.charCodeAt(j++)] * 16 + dehexArray[arg.charCodeAt(j++)]
47+
const a = dehexArray[str.charCodeAt(j++)] * 16 + dehexArray[str.charCodeAt(j++)]
4848
if (!a && Number.isNaN(a)) throw new Error('Input is not a hex string')
4949
arr[i] = a
5050
}

0 commit comments

Comments
 (0)