0xdw
0xdw2mo ago

Buffer.from equivalent in deno?

on node.js
const signerPvtKey = Buffer.from(
// process.env.PVT_KEY
PVT_KEY,
"hex"
);
const signerPvtKey = Buffer.from(
// process.env.PVT_KEY
PVT_KEY,
"hex"
);
i found the deno from medium article to do it like this: https://medium.com/deno-the-complete-reference/buffer-from-denos-equivalent-of-node-js-d42f7115af61
//Node.js
Buffer.from('48656c6c6f20576f726c64', 'hex');
//Deno
import {decodeString} from "https://deno.land/std/encoding/hex.ts";
new Buffer(decodeString('48656c6c6f20576f726c64'));
//Node.js
Buffer.from('48656c6c6f20576f726c64', 'hex');
//Deno
import {decodeString} from "https://deno.land/std/encoding/hex.ts";
new Buffer(decodeString('48656c6c6f20576f726c64'));
but it is outofdate for decodeString, so i use decodeHex
import { Buffer } from "https://deno.land/std/io/mod.ts";
// import {Buffer} from "https://deno.land/std/io/buffer.ts";
import {decodeHex} from "https://deno.land/std/encoding/hex.ts";
import { Buffer } from "https://deno.land/std/io/mod.ts";
// import {Buffer} from "https://deno.land/std/io/buffer.ts";
import {decodeHex} from "https://deno.land/std/encoding/hex.ts";
the decodeHex is working as intended but Buffer still not working, i got empty object instead and neither those 2 Buffer seems working from mod and buffer.ts
4 Replies
Hong Minhee
Hong Minhee2mo ago
As far as I know, there is no such thing in Deno as Buffer in Node.js, and you should use either ArrayBuffer or Uint8Array instead, which are web standards. (The decodeHex() function already returns a Uint8Array.) The Buffer in @std/io has the same name as the Buffer in Node.js, but is a completely different thing. Ah, there was no such thing as Buffer in Node.js back in the day, but now I see there is one in the Node.js compatibility layer.
import { Buffer } from "node:buffer";
import { Buffer } from "node:buffer";
lukeed
lukeed2mo ago
Yeah there’s the compat layer
lukeed
lukeed2mo ago
But if you wanted to emulate it with pure Web APIs then have a look at this: https://github.com/lukeed/worktop/blob/836f00bd03addf258b93ea40ebd7e6342bc0d6e7/packages/worktop/src/buffer.ts#L98
GitHub
worktop/packages/worktop/src/buffer.ts at 836f00bd03addf258b93ea40e...
The next generation web framework for Cloudflare Workers - lukeed/worktop