1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(ext/node): fix TypeError in Buffer.from with base64url encoding. (#20705)

For the following example, if I set the encoding to `base64url`, it'll
throw an unexpected TypeError:

```ts
import { Buffer } from "node:buffer";

Buffer.from("IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig", "base64url").toString();

// error: Uncaught TypeError: src.subarray is not a function
// const buf = Buffer.from(
//                    ^
//     at blitBuffer (ext:deno_node/internal/buffer.mjs:1779:15)
//     at Uint8Array.base64urlWrite (ext:deno_node/internal/buffer.mjs:691:10)
//     at Object.write (ext:deno_node/internal/buffer.mjs:2195:11)
//     at Uint8Array.write (ext:deno_node/internal/buffer.mjs:794:14)
//     at fromString (ext:deno_node/internal/buffer.mjs:214:22)
//     at _from (ext:deno_node/internal/buffer.mjs:119:12)
//     at Function.from (ext:deno_node/internal/buffer.mjs:157:10)
//     at file:///Users/foodieats/temp/buffer1.ts:3:20
```

The error caused by `base64urlWrite` function, it should call
`forgivingBase64UrlDecode` not `forgivingBase64UrlEncode`

Also fixed #20563 .
This commit is contained in:
Chen Su 2023-09-27 21:54:19 +08:00 committed by Bartek Iwańczuk
parent 16419c44b8
commit ae84e01e8a
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -5,7 +5,7 @@
import {
forgivingBase64Decode,
forgivingBase64UrlEncode,
forgivingBase64UrlDecode,
} from "ext:deno_web/00_infra.js";
export function asciiToBytes(str: string) {
@ -51,7 +51,7 @@ function base64clean(str: string) {
export function base64UrlToBytes(str: string) {
str = base64clean(str);
str = str.replaceAll("+", "-").replaceAll("/", "_");
return forgivingBase64UrlEncode(str);
return forgivingBase64UrlDecode(str);
}
export function hexToBytes(str: string) {