denoland / docs

Deno documentation, examples and API Reference. Powered by Lume.
https://docs.deno.com
MIT License
84 stars 186 forks source link

Documented example usage of Node API crypto.hash method throws TypeError as `.hash()` is undefined #987

Open noelforte opened 1 month ago

noelforte commented 1 month ago

Possibly related: https://github.com/denoland/deno/issues/24945

Implementation and example usage of the .hash() method from node:crypto is a documented part of Deno's Node-compatible API.

However, attempting to run the following example throws an error:

import crypto from 'node:crypto';
import { Buffer } from 'node:buffer';

// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
~/Desktop => deno crypto-hash.ts
error: Uncaught (in promise) TypeError: crypto.hash is not a function
console.log(crypto.hash('sha1', string));
                   ^
    at file:///.../Desktop/crypto-hash.ts:7:20

Documented features should be supported or removed from the documentation until actually implemented.

tommyhtran commented 1 month ago

Those docs (https://docs.deno.com/api/node/) are automatically generated based on type definition files from @types/node. Perhaps it's only intended to be a reference for what Node supports in order for Deno developers to not be dependent on Node's docs.

The actual Node compatibility documentation is at https://docs.deno.com/runtime/reference/node_apis/.

Ideally what should have happened is we start with a particular @types/node version and document Deno's compatibility at that point. Then every time @types/node is bumped, review the changes and update the compatibility list. That's certainly quite a lot of work!

noelforte commented 1 month ago

That's certainly quite a lot of work!

I don't doubt it! Thanks for clearing it up. Feel free to close as a duplicate of https://github.com/denoland/deno/issues/24945.

mediaformat commented 1 week ago

Just came across this aswell, work around using https://jsr.io/@libs/crypto import { hash } from "@libs/crypto";