最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cloudflare Workers Buffer.from() - Stack Overflow

programmeradmin1浏览0评论

I am not that familiar with how Buffers and all that work.

In node I can do

const str = "dasjkiodjsiodjpasiodfjodifjaspiofjsdioajfpasodfjsdioa";
let buff = Buffer.from(str); // <Buffer 64 61 73 6a 6b 6 etc...
let buffHex = Buffer.from(str, 'hex');

console.log(buff)

How would I go about doing this in Cloudflare Workers, because I get ReferenceError: Buffer is not defined

I am not that familiar with how Buffers and all that work.

In node I can do

const str = "dasjkiodjsiodjpasiodfjodifjaspiofjsdioajfpasodfjsdioa";
let buff = Buffer.from(str); // <Buffer 64 61 73 6a 6b 6 etc...
let buffHex = Buffer.from(str, 'hex');

console.log(buff)

How would I go about doing this in Cloudflare Workers, because I get ReferenceError: Buffer is not defined

Share Improve this question asked Sep 29, 2021 at 9:33 DugDug 851 silver badge4 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

Buffer is a Node API. Cloudflare Workers is based on web platform APIs instead, like what you'd find in browsers. The web platform alternative to Buffer is Uint8Array. You can use the TextEncoder and TextDecoder APIs to convert between Uint8Arrays with UTF-8 encoding and text strings.

let bytes = new TextEncoder().encode(str);

To convert a Uint8Array to hex, you can use a function like:

function bytes2hex(bytes) {
  return Array.prototype.map.call(bytes,
      byte => ('0' + byte.toString(16)).slice(-2)).join('');
}

I do not recommend using a Buffer polyfill for this as it'll bloat your code size. It's better to use Uint8Array directly.

In general you should be able to find answers about how to do common operations on Uint8Array on Stack Overflow.

You can use Node.js's Buffer API with Cloudflare Workers.

See https://developers.cloudflare.com/workers/runtime-apis/nodejs/ which explains that by adding compatibility_flags = [ "nodejs_compat" ] to your wrangler.toml you can use the following Node.js APIs:

  • assert
  • AsyncLocalStorage
  • Buffer
  • EventEmitter
  • util

Here's an example of how to create a SHA-256 hash of "hello world" in Cloudflare workers, just using the globally available WebCrypto browser API. Hope it gives you some insight into how this works!

export default {
  async fetch(request)
  {
    const myText = new TextEncoder().encode('Hello world!');
    const myDigest = await crypto.subtle.digest({ name: 'SHA-256' }, myText);
    const hashArray = Array.from(new Uint8Array(myDigest));
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
    return new Response(hashHex, {status: 200});
  }
}

more info: https://developers.cloudflare.com/workers/runtime-apis/web-crypto/

Cloudflare added Node.js compatibility APIs recently.

If you're using SvelteKit, replace your adapter with my @chientrm/adapter-cloudflare.

In your code:

import { Buffer } from 'node:buffer';

Now you can use this Buffer object. Example:

const bytes = Buffer.from('<some-base64-string>', 'base64');
发布评论

评论列表(0)

  1. 暂无评论