From 507f24c4746e24ea60dcebdffacd0b66e3d4bfc1 Mon Sep 17 00:00:00 2001 From: Chen Su Date: Wed, 27 Sep 2023 21:54:19 +0800 Subject: [PATCH] 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 . --- ext/node/polyfills/internal_binding/_utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/node/polyfills/internal_binding/_utils.ts b/ext/node/polyfills/internal_binding/_utils.ts index ab174608be..00993732f4 100644 --- a/ext/node/polyfills/internal_binding/_utils.ts +++ b/ext/node/polyfills/internal_binding/_utils.ts @@ -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) {