Akuze
Akuze•9mo ago

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:
class myClass {
_encryptionKey: Buffer; // Errors: Cannot find name 'Buffer'.deno-ts(2580)
_signatureKey: Buffer; // Errors: Cannot find name 'Buffer'.deno-ts(2580)
constructor(secret: string, domain: string) {
const keyMaterial = crypto.createHash("sha256").update(secret).digest();
this._encryptionKey = keyMaterial.subarray(0, BLOCK_SIZE);
this._signatureKey = keyMaterial.subarray(BLOCK_SIZE, 32);
}
}
class myClass {
_encryptionKey: Buffer; // Errors: Cannot find name 'Buffer'.deno-ts(2580)
_signatureKey: Buffer; // Errors: Cannot find name 'Buffer'.deno-ts(2580)
constructor(secret: string, domain: string) {
const keyMaterial = crypto.createHash("sha256").update(secret).digest();
this._encryptionKey = keyMaterial.subarray(0, BLOCK_SIZE);
this._signatureKey = keyMaterial.subarray(BLOCK_SIZE, 32);
}
}
How should I type this? I'm new to Deno, so any help is appreciated. Thanks a lot
4 Replies
Deno AI Helper
Deno AI Helper•9mo ago
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:
class myClass {
_encryptionKey: Uint8Array;
_signatureKey: Uint8Array;
constructor(secret: string, domain: string) {
const keyMaterial = crypto.createHash("sha256").update(secret).digest();
this._encryptionKey = keyMaterial.subarray(0, BLOCK_SIZE);
this._signatureKey = keyMaterial.subarray(BLOCK_SIZE, 32);
}
}
class myClass {
_encryptionKey: Uint8Array;
_signatureKey: Uint8Array;
constructor(secret: string, domain: string) {
const keyMaterial = crypto.createHash("sha256").update(secret).digest();
this._encryptionKey = keyMaterial.subarray(0, BLOCK_SIZE);
this._signatureKey = keyMaterial.subarray(BLOCK_SIZE, 32);
}
}
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. 🦕
Akuze
AkuzeOP•9mo ago
Could anyone confirm/deny the AI response here?
raunioroo
raunioroo•9mo ago
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.
Akuze
AkuzeOP•9mo ago
Great, thanks @raunioroo