Buffer.from equivalent in deno?
on node.js
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
but it is outofdate for decodeString, so i use decodeHex
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
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.
Yeah there’s the compat layer
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
Example usage: https://github.com/lukeed/worktop/blob/836f00bd03addf258b93ea40ebd7e6342bc0d6e7/packages/worktop/src/buffer.test.ts#L374
GitHub
worktop/packages/worktop/src/buffer.test.ts at 836f00bd03addf258b93...
The next generation web framework for Cloudflare Workers - lukeed/worktop