JonathanWilbur / asn1-ts

ASN.1 TypeScript library, including codecs for Basic Encoding Rules (BER) and Distinguished Encoding Rules (DER).
MIT License
32 stars 6 forks source link

Buffer error with Cloudflare Workers #32

Open mistial-dev opened 4 months ago

mistial-dev commented 4 months ago

Attempting to use this with Cloudflare Workers, I'm getting an error that buffer isn't defined. I suspect it's because Buffer isn't imported explicitly.

(base) mistial@Mistials-MacBook-Pro xxx % npx wrangler deploy                                            
 ⛅️ wrangler 3.52.0
-------------------
Your worker has access to the following bindings:
- Queues:
  - QUEUE: xxx
- D1 Databases:
  - DB: xxx (xxx-xxx-xxx-xxx-xxx)
Total Upload: xxx KiB / gzip: xxx KiB

✘ [ERROR] A request to the Cloudflare API (/accounts/xxx/workers/scripts/xxx) failed.

  Uncaught ReferenceError: Buffer is not defined
    at null.<anonymous>
  (file:///Users/mistial/Projects/xxx/node_modules/asn1-ts/source/asn1.ts:88:9) in toBytes
    at null.<anonymous> (file:///Users/mistial/Projects/xxx/src/router.ts:12:26)
   [code: 10021]

I am using nodejs compatibility mode:

compatibility_flags = [ "nodejs_compat" ]

Looking through the CloudFlare Documentation, it states:

To ensure compatibility with a wider set of npm packages, and make it easier for you to run existing 
applications on Cloudflare Workers, the following APIs from the Node.js runtime are available directly 
as Workers runtime APIs, with no need to add polyfills to your own code

They show the need to add an import like import { Buffer } from 'node:buffer'; This seems to align with nodejs recommendations:

While the Buffer class is available within the global scope, it is still recommended to explicitly reference
it via an import or require statement.