D
Deno

help

Problem with parsing `base64url` from a `Buffer (node)`

RReallyBored9/18/2023
help , it works in bun but not in Deno, I don't know if is a bug from them or it's not implemented here,it also works in node:
import BufferProto from "node:buffer"

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

//deno: ""
//bun: "{\"hello\":\"hdd/e+i\"}"
//node:"{\"hello\":\"hdd/e+i\"}"
import BufferProto from "node:buffer"

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

//deno: ""
//bun: "{\"hello\":\"hdd/e+i\"}"
//node:"{\"hello\":\"hdd/e+i\"}"
DAHDeno AI Helper9/18/2023
Based on the documentation, Deno does not have built-in support for the base64url encoding in the Buffer.from() method. The Buffer.from() method in Node.js does not support base64url encoding either. The discrepancy you're seeing might be due to differences in how the Buffer.from() method is implemented in bun, Node.js, and Deno. In Deno, you can use the built-in TextDecoder and atob functions to decode base64 strings. However, these functions do not support base64url directly. You would need to convert the base64url string to a regular base64 string first. Here's how you can do it:
const base64url = "IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig";
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
const decoded = atob(base64);
console.log(decoded);
const base64url = "IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig";
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
const decoded = atob(base64);
console.log(decoded);
Please note that this information is not explicitly stated in the documentation, but it's a common way to handle base64url in JavaScript.
RReallyBored9/18/2023
That's actually incorrect, the official types includes 'base64url'
DAHDeno AI Helper9/18/2023
I apologize for the confusion. The official Node.js documentation does not list 'base64url' as a valid encoding for the Buffer.from() method. However, if 'base64url' is working in your Node.js environment, it might be due to a third-party library or a custom implementation that extends the native Buffer functionality. In Deno, you might need to manually convert the 'base64url' string to a regular base64 string before decoding it. Here's how you can do it:
const base64url = "IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig";
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
const decoded = atob(base64);
console.log(decoded);
const base64url = "IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig";
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
const decoded = atob(base64);
console.log(decoded);
This code replaces '-' with '+' and '_' with '/' in the 'base64url' string, converting it to a regular base64 string. Then it uses the atob() function to decode the base64 string.
RReallyBored9/18/2023
Well, I will need to make a deno's only implementation that will be slower than bun's
Ppyrote9/18/2023
I'm seeing the same bug with Buffer.from and base64url. Its returning a buffer that is mostly zeros. The AI doesn't know that Deno has a module for Base64URL.
import { encode as encodeBase64url, decode as decodeBase64url } from "https://deno.land/std/encoding/base64url.ts";

const text_decode = (d) => new TextDecoder().decode(new Uint8Array(d));

const url_str = 'IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig';
const buffer = decodeBase64url(url_str);
const buffer_str = text_decode(buffer);
console.log(`Text ${buffer_str}`);

const text = '"{\\"hello\\":\\"hdd/e+i\\"}"';
const base64_text = encodeBase64url(text);
console.log(`Base64URL ${base64_text}`);
import { encode as encodeBase64url, decode as decodeBase64url } from "https://deno.land/std/encoding/base64url.ts";

const text_decode = (d) => new TextDecoder().decode(new Uint8Array(d));

const url_str = 'IntcImhlbGxvXCI6XCJoZGQvZStpXCJ9Ig';
const buffer = decodeBase64url(url_str);
const buffer_str = text_decode(buffer);
console.log(`Text ${buffer_str}`);

const text = '"{\\"hello\\":\\"hdd/e+i\\"}"';
const base64_text = encodeBase64url(text);
console.log(`Base64URL ${base64_text}`);
RReallyBored9/18/2023
That would do, I hope is fast because the other implementation works but it's super slow
Llcasdev9/19/2023
Hey folks, do you mind opening a bug about this? We'll fix it!
RReallyBored9/19/2023
I will indeed! done!

Looking for more? Join the community!

Recommended Posts
UUID of managed KV databaseHello, I am trying to follow [this doc](https://docs.deno.com/kv/manual/on_deploy#connect-to-managedError at the execution of a programmHey ! I have a problem when i run one of my programm with deno, is a programm made with typescript Can't import typescript moduleWhy can I only import the js file and not the ts file? ```js import * as zip from "https://deno.lanIs there a way to change the default Details of the compiled exeI esentialy want to change the comiling defaults of the details and the image of the resulting exe (Setting source map root with `deno_emit`Hello! I'm currently trying to use `deno_emit` so bundle some assets. The code bundles properly, howIn Deno, what is the best way to simply host static websites?What is the best way to simply host static/cached files? Without having to do a read file call everyzsh: command not found: deno on macOSplease send helpUsing esbuild with import mapsHello 👋 I'm attempting to migrate one of my front-end projects to using Deno for tooling instead ofproxy-agent not working on Deno DeployI'm trying to use a proxy for the node-fetch requests in a deno app. I chose proxy-agent npm packagExtremely high CPU usage in Windows running on Intel Pentium processorI have a simple app (about 300 lines of code, few deps). I compile it into a Windows executable. WheImpossible to use `"jsx": "preserve"` in deno.json...Well it seems that I can't use jsx preserve in deno.json, when I try to run any tasks in deno.json, integrating deno_webOk I seem to have poobrain today. Can't seem to get the deno_web extension working. The core versioCan I use Workers in an executable ?Everything is in the questions, on the doc site, it is mention that we can't use Workers when creatiWalkSync dosnt get filesIm trying to read all files of a folder with WalkSync from the deno STD libary but it dosnt see thedeno behaving differently in local dev env and production docker buildsI'm currently trying to switch from using node+yarn 2 for my blogs static files to deno. however theFront end dev needed for 6 months.Hello. I am in need of a frontend developer. dm me pls.Include platform and version in compiled binary filenameRight now when i run `deno compile --output bin/myprogram`, it works as expected. Is is possible to How to handle a user abort by Deno.serveHello, I have a question and discovered this Discord channel now. The Deno website has an example atAPI hooks during restart for deno run --watchHello! I thought it would be super fun to further minimize repetitive tasks while manually testing adeno compile -A npm:cowsay not workingHello, I am trying `deno compile -A npm:cowsay` and then following that with `./cowsay Hello World!`