I've been exploring ways to utilize the crypto
module with the async await
syntax while maintaining TypeScript declarations intact. For instance, the randomFill
function within @types/node
has three different overrides:
function randomFill<T extends NodeJS.ArrayBufferView>(
buffer: T,
callback: (err: Error | null, buf: T) => void,
): void;
function randomFill<T extends NodeJS.ArrayBufferView>(
buffer: T,
offset: number,
callback: (err: Error | null, buf: T) => void,
): void;
function randomFill<T extends NodeJS.ArrayBufferView>(
buffer: T,
offset: number,
size: number,
callback: (err: Error | null, buf: T) => void,
): void;
If I opt to use the bluebird promisify
method, only a portion of the TypeScript declarations will be retained (you can observe this when hovering over the exported randomFill
method in an IDE):
import {promisify} from 'bluebird';
import {randomFill as randomFillNative } from 'crypto';
export const randomFill = promisify<NodeJS.ArrayBufferView, NodeJS.ArrayBufferView, number>(randomFillNative)
The same issue arises with the util promisify
approach:
import {promisify} from 'util';
import {randomFill as randomFillNative } from 'crypto';
export const randomFill = promisify<NodeJS.ArrayBufferView, number, NodeJS.ArrayBufferView>(randomFillNative)
This is the result after promisifying:
https://i.sstatic.net/iNxBD.png
Is there an efficient way to wrap crypto methods into promises without compromising TypeScript declarations?