node/test/parallel/test-webcrypto-methods-not-async.js
Filip Skokan b8fc182a0f crypto: remove async from WebCrypto methods
Remove async function wrappers from SubtleCrypto methods while keeping
their public promise-returning behaviour.

Route method entry points through a shared helper that converts
synchronous validation errors into rejected promises. Let the internal
implementations return native job promises directly.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63363
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
2026-05-23 15:45:31 +00:00

43 lines
876 B
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = globalThis.crypto;
const AsyncFunction = async function() {}.constructor;
const methods = [
'decrypt',
'decapsulateBits',
'decapsulateKey',
'deriveBits',
'deriveKey',
'digest',
'encapsulateBits',
'encapsulateKey',
'encrypt',
'exportKey',
'generateKey',
'getPublicKey',
'importKey',
'sign',
'unwrapKey',
'verify',
'wrapKey',
];
(async function() {
for (const name of methods) {
assert.notStrictEqual(subtle[name].constructor, AsyncFunction);
const promise = subtle[name].call({});
assert.strictEqual(Object.getPrototypeOf(promise), Promise.prototype);
await assert.rejects(promise, {
code: 'ERR_INVALID_THIS',
});
}
})().then(common.mustCall());