summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal_binding/_utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/polyfills/internal_binding/_utils.ts')
-rw-r--r--ext/node/polyfills/internal_binding/_utils.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal_binding/_utils.ts b/ext/node/polyfills/internal_binding/_utils.ts
index 74dc3cbcd..0b1e1709d 100644
--- a/ext/node/polyfills/internal_binding/_utils.ts
+++ b/ext/node/polyfills/internal_binding/_utils.ts
@@ -7,6 +7,7 @@ import {
forgivingBase64Decode,
forgivingBase64UrlDecode,
} from "ext:deno_web/00_infra.js";
+import { op_base64_write } from "ext:core/ops";
export function asciiToBytes(str: string) {
const length = str.length;
@@ -27,6 +28,22 @@ export function base64ToBytes(str: string) {
}
}
+export function base64Write(
+ str: string,
+ buffer: Uint8Array,
+ offset: number = 0,
+ length?: number,
+): number {
+ length = length ?? buffer.byteLength - offset;
+ try {
+ return op_base64_write(str, buffer, offset, length);
+ } catch {
+ str = base64clean(str);
+ str = str.replaceAll("-", "+").replaceAll("_", "/");
+ return op_base64_write(str, buffer, offset, length);
+ }
+}
+
const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
function base64clean(str: string) {
// Node takes equal signs as end of the Base64 encoding