Skip to content

Commit b1cbe0f

Browse files
committed
refactor: prevert fromUint8 over typedView in more places
1 parent 47a2cc2 commit b1cbe0f

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

base64.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEmptyRest } from './assert.js'
22
import { typedView } from './array.js'
3-
import { assertU8, E_STRING } from './fallback/_utils.js'
3+
import { assertU8, fromUint8, E_STRING } from './fallback/_utils.js'
44
import { isHermes } from './fallback/platform.js'
55
import { decodeLatin1, encodeLatin1 } from './fallback/latin1.js'
66
import * as js from './fallback/base64.js'
@@ -96,7 +96,7 @@ function fromBase64common(str, isBase64url, padding, format, rest) {
9696
throw new TypeError('Invalid padding option')
9797
}
9898

99-
return typedView(fromBase64impl(str, isBase64url, padding), format)
99+
return fromBase64impl(str, isBase64url, padding, format)
100100
}
101101

102102
// ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE
@@ -114,7 +114,7 @@ function noWhitespaceSeen(str, arr) {
114114
let fromBase64impl
115115
if (Uint8Array.fromBase64) {
116116
// NOTICE: this is actually slower than our JS impl in older JavaScriptCore and (slightly) in SpiderMonkey, but faster on V8 and new JavaScriptCore
117-
fromBase64impl = (str, isBase64url, padding) => {
117+
fromBase64impl = (str, isBase64url, padding, format) => {
118118
const alphabet = isBase64url ? 'base64url' : 'base64'
119119

120120
let arr
@@ -136,20 +136,20 @@ if (Uint8Array.fromBase64) {
136136
// We don't allow whitespace in input, but that can be rechecked based on output length
137137
// All other chars are checked natively
138138
if (!noWhitespaceSeen(str, arr)) throw new SyntaxError(E_CHAR)
139-
return arr
139+
return fromUint8(arr, format)
140140
}
141141
} else if (haveNativeBuffer) {
142-
fromBase64impl = (str, isBase64url, padding) => {
142+
fromBase64impl = (str, isBase64url, padding, format) => {
143143
const arr = Buffer.from(str, 'base64')
144144
// Rechecking by re-encoding is cheaper than regexes on Node.js
145145
const got = isBase64url ? maybeUnpad(str, padding === false) : maybePad(str, padding !== true)
146146
const valid = isBase64url ? arr.base64urlSlice(0, arr.length) : arr.base64Slice(0, arr.length)
147147
if (got !== valid) throw new SyntaxError(E_PADDING)
148-
return arr // fully checked
148+
return typedView(arr, format) // fully checked
149149
}
150150
} else if (shouldUseAtob) {
151151
// atob is faster than manual parsing on Hermes
152-
fromBase64impl = (str, isBase64url, padding) => {
152+
fromBase64impl = (str, isBase64url, padding, format) => {
153153
let arr
154154
if (isBase64url) {
155155
if (/[\t\n\f\r +/]/.test(str)) throw new SyntaxError(E_CHAR) // atob verifies other invalid input
@@ -171,8 +171,9 @@ if (Uint8Array.fromBase64) {
171171
if (expected !== end) throw new SyntaxError(E_LAST)
172172
}
173173

174-
return arr
174+
return fromUint8(arr, format)
175175
}
176176
} else {
177-
fromBase64impl = (str, isBase64url, padding) => js.fromBase64(str, isBase64url) // validated in js
177+
fromBase64impl = (str, isBase64url, padding, format) =>
178+
fromUint8(js.fromBase64(str, isBase64url), format) // validated in js
178179
}

hex.node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { typedView } from './array.js'
2-
import { assertU8, E_STRING } from './fallback/_utils.js'
2+
import { assertU8, fromUint8, E_STRING } from './fallback/_utils.js'
33
import { E_HEX } from './fallback/hex.js'
44

55
if (Buffer.TYPED_ARRAY_SUPPORT) throw new Error('Unexpected Buffer polyfill')
@@ -17,7 +17,7 @@ export function toHex(arr) {
1717

1818
// Unlike Buffer.from(), throws on invalid input
1919
export const fromHex = Uint8Array.fromHex
20-
? (str, format = 'uint8') => typedView(Uint8Array.fromHex(str), format)
20+
? (str, format = 'uint8') => fromUint8(Uint8Array.fromHex(str), format)
2121
: (str, format = 'uint8') => {
2222
if (typeof str !== 'string') throw new TypeError(E_STRING)
2323
if (str.length % 2 !== 0) throw new SyntaxError(E_HEX)

0 commit comments

Comments
 (0)