Importing Buffer Type
Hi there, I am having issue typing a hash. I am trying to create an encryption key and signature key, but don't know what the return types should be in Deno. In node.js, the type appears to be
Buffer
, but this comes up with an error in Deno. Here's the code I'm referring to:
How should I type this? I'm new to Deno, so any help is appreciated. Thanks a lot4 Replies
Hello! In Deno, the
Buffer
type from Node.js is not available by default, as Deno does not include the Node.js globals. However, you can still work with similar types in Deno, such as Uint8Array
, which is the equivalent of Node.js's Buffer
for most use cases.
To resolve the type error, you can replace the Buffer
type with Uint8Array
. Here's how you can modify your class definition:
This change should resolve the type errors you're encountering. The Uint8Array
type is a built-in JavaScript typed array that represents an array of 8-bit unsigned integers, and it's suitable for handling binary data like encryption keys.
If you specifically need to include Node types like Buffer
in your Deno project, the documentation states that you can add a types reference directive to the @types/node
package. However, note that Deno aims to align with web standards, and using web standard APIs and types is generally recommended when possible. 🦕Helpful Links:
Hashing - Deno by Example
Hex and Base64 Encoding - Deno by Example
Deno 1.14 Release Notes
Data Modeling in TypeScript | Deno Docs
Announcing native npm support on Deno Deploy
Deno Docs
How to publish Deno modules to NPM
npm: specifiers | Deno Docs
Node API Compatibility List | Deno Docs
Could anyone confirm/deny the AI response here?
Yeah AI looks to be about correct about the Buffer vs Uint8Array. But I'm not crypto expert, nor TS expert, so no comments whether Uint8Array is best in this particular case, or should it be ArrayBuffer, some kind of Stream or what. Uint8Array is used a lot for simple buckets of in-memory bytes, sounds like a sensible Deno/webstandard option. IIRC Uint8Array is actually a view into a backing ArrayBuffer that contains the actual data.
Great, thanks @raunioroo