[blake2b-wasm] Add types (#63132)

This commit is contained in:
Dimitri B
2022-11-07 20:25:24 +01:00
committed by GitHub
parent 7c86edf069
commit 68ba69e52f
4 changed files with 229 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
/// <reference types="node" />
import blake2b = require('blake2b-wasm');
// test type exports
type Blake2b = blake2b.Blake2b;
type Blake2bWasm = blake2b.Blake2bWasm;
blake2b.BYTES_MIN; // $ExpectType 16
blake2b.BYTES_MAX; // $ExpectType 64
blake2b.BYTES; // $ExpectType 32
blake2b.KEYBYTES_MIN; // $ExpectType 16
blake2b.KEYBYTES_MAX; // $ExpectType 64
blake2b.KEYBYTES; // $ExpectType 32
blake2b.SALTBYTES; // $ExpectType 16
blake2b.PERSONALBYTES; // $ExpectType 16
const wasm = blake2b.WASM; // $ExpectType Blake2bWasm | null
blake2b.SUPPORTED; // $ExpectType boolean
blake2b.ready(); // $ExpectType Promise<void>
// $ExpectType Promise<void>
blake2b.ready(err => {
err; // $ExpectType Error | undefined
});
const hash = blake2b(); // $ExpectType Blake2b
blake2b(32, new Uint8Array(8)); // $ExpectType Blake2b
blake2b(32, new Uint8Array(8), new Uint8Array(8)); // $ExpectType Blake2b
blake2b(32, new Uint8Array(8), new Uint8Array(8), new Uint8Array(8)); // $ExpectType Blake2b
blake2b(32, new Uint8Array(8), new Uint8Array(8), new Uint8Array(8), true); // $ExpectType Blake2b
new blake2b(); // $ExpectType Blake2b
new blake2b(32, new Uint8Array(8)); // $ExpectType Blake2b
new blake2b(32, new Uint8Array(8), new Uint8Array(8)); // $ExpectType Blake2b
new blake2b(32, new Uint8Array(8), new Uint8Array(8), new Uint8Array(8)); // $ExpectType Blake2b
new blake2b(32, new Uint8Array(8), new Uint8Array(8), new Uint8Array(8), true); // $ExpectType Blake2b
hash.digestLength; // $ExpectType number
hash.finalized; // $ExpectType boolean
hash.pointer; // $ExpectType number
hash.update(new Uint8Array(10)); // $ExpectType Blake2b
hash.digest(); // $ExpectType Uint8Array
hash.digest('binary'); // $ExpectType Uint8Array
hash.digest('hex'); // $ExpectType string
hash.digest(new Uint8Array(10)); // $ExpectType Uint8Array
hash.digest(Buffer.alloc(10)); // $ExpectType Buffer
hash.final(); // $ExpectType Uint8Array
hash.final('binary'); // $ExpectType Uint8Array
hash.final('hex'); // $ExpectType string
hash.final(new Uint8Array(10)); // $ExpectType Uint8Array
hash.final(Buffer.alloc(10)); // $ExpectType Buffer
hash.ready(); // $ExpectType Promise<void>
// $ExpectType Promise<void>
hash.ready(err => {
err; // $ExpectType Error | undefined
});
hash.getPartialHash(); // $ExpectType Uint8Array
hash.setPartialHash([1]); // $ExpectType void
hash.setPartialHash(new Uint8Array(1)); // $ExpectType void
if (wasm) {
wasm.memory; // $ExpectType Memory
wasm.blake2b_init(1, 1); // $ExpectType void
wasm.blake2b_update(1, 1, 1); // $ExpectType void
wasm.blake2b_final(1); // $ExpectType void
wasm.blake2b_compress(1); // $ExpectType void
}

139
types/blake2b-wasm/index.d.ts vendored Normal file
View File

@@ -0,0 +1,139 @@
// Type definitions for blake2b-wasm 2.4
// Project: https://github.com/mafintosh/blake2b-wasm
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference lib="dom" />
export = Blake2b;
declare const Blake2b: Blake2bCtor;
interface Blake2bCtor {
/**
* Create a new hash instance.
*
* @param [digestLength=32] Length of the digest.
* @param key The key to use.
* @param salt The salt to use.
* @param personal The personal data to use.
* @param noAssert Disables all input checks.
*
* @example
* import blake2b = require('blake2b-wasm')
*
* if (!blake2b.SUPPORTED) {
* console.log('WebAssembly not supported by your runtime')
* }
*
* blake2b.ready((err) => {
* if (err) throw err
*
* const hash = blake2b()
* .update(Buffer.from('hello')) // pass in a buffer or uint8array
* .update(Buffer.from(' '))
* .update(Buffer.from('world'))
* .digest('hex')
*
* console.log('Blake2b hash of "hello world" is %s', hash)
* })
*/
(
digestLength?: number,
key?: Uint8Array,
salt?: Uint8Array,
personal?: Uint8Array,
noAssert?: boolean,
): Blake2b.Blake2b;
/**
* Create a new hash instance.
*
* @param [digestLength=32] Length of the digest.
* @param key The key to use.
* @param salt The salt to use.
* @param personal The personal data to use.
* @param noAssert Disables all input checks.
*
* @example
* import blake2b = require('blake2b-wasm')
*
* if (!blake2b.SUPPORTED) {
* console.log('WebAssembly not supported by your runtime')
* }
*
* blake2b.ready((err) => {
* if (err) throw err
*
* const hash = new blake2b()
* .update(Buffer.from('hello')) // pass in a buffer or uint8array
* .update(Buffer.from(' '))
* .update(Buffer.from('world'))
* .digest('hex')
*
* console.log('Blake2b hash of "hello world" is %s', hash)
* })
*/
new (
digestLength?: number,
key?: Uint8Array,
salt?: Uint8Array,
personal?: Uint8Array,
noAssert?: boolean,
): Blake2b.Blake2b;
readonly BYTES_MIN: 16;
readonly BYTES_MAX: 64;
readonly BYTES: 32;
readonly KEYBYTES_MIN: 16;
readonly KEYBYTES_MAX: 64;
readonly KEYBYTES: 32;
readonly SALTBYTES: 16;
readonly PERSONALBYTES: 16;
readonly WASM: Blake2b.Blake2bWasm | null;
readonly SUPPORTED: boolean;
/**
* Wait for the WASM code to load. Returns the WebAssembly instance promise as well for convenience.
* You have to call this at least once before instantiating the hash.
*/
ready(cb?: (err: Error | undefined) => void): Promise<void>;
}
declare namespace Blake2b {
interface Blake2b {
digestLength: number;
finalized: boolean;
pointer: number;
/**
* Update the hash with a new piece of data.
*/
update(input: Uint8Array): this;
/**
* Digest the hash.
*/
digest(enc?: 'binary'): Uint8Array;
digest(enc: string): string;
digest<T extends Uint8Array>(enc: T): T;
/**
* Digest the hash.
*/
final: Blake2b['digest'];
ready: Blake2bCtor['ready'];
/**
* @returns The current partial hash.
*/
getPartialHash(): Uint8Array;
/**
* Set the hash to a previously set hash.
*
* @param data Should be the result of `getPartialHash()`.
*/
setPartialHash(data: ArrayLike<number>): void;
}
interface Blake2bWasm {
memory: WebAssembly.Memory;
blake2b_init(pointer: number, digestLength: number): void;
blake2b_update(pointer: number, start: number, end: number): void;
blake2b_final(pointer: number): void;
blake2b_compress(pointer: number): void;
}
}

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"blake2b-wasm-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "@definitelytyped/dtslint/dt.json" }