Skip to content

Commit

Permalink
Add TypeScript typings
Browse files Browse the repository at this point in the history
  • Loading branch information
yassernasc committed Jan 13, 2025
1 parent a02cdd5 commit 716fce2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
59 changes: 59 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Transform, TransformOptions } from 'bare-stream'
import { Buffer, BufferEncoding } from 'bare-buffer'

type Algorithm = 'MD5' | 'SHA1' | 'SHA256' | 'SHA512' | 'BLAKE2B256'

export const constants: { hash: Record<Algorithm, number> }

declare class CryptoError extends Error {
static UNSUPPORTED_DIGEST_METHOD(msg: string): CryptoError
}

export class Hash extends Transform {
constructor(
algorithm: Algorithm | Lowercase<Algorithm> | number,
opts?: TransformOptions
)

update(data: string | Buffer | DataView, encoding?: BufferEncoding): this

digest(encoding?: BufferEncoding): string | Buffer
}

export function createHash(
algorithm: Algorithm | Lowercase<Algorithm> | number,
opts?: TransformOptions
): Hash

export function randomBytes(
size: number,
callback: (error: null, buffer: Buffer) => void
): void

export function randomBytes(size: number): Buffer

export function randomFill<B extends ArrayBuffer | Buffer | DataView>(
buffer: B,
offset?: number,
size?: number
): B

export function randomFill<B extends ArrayBuffer | Buffer | DataView>(
buffer: B,
offset: number,
size: number,
callback: (err: null, buffer: B) => void
): void

export function randomFill<B extends ArrayBuffer | Buffer | DataView>(
buffer: B,
offset: number,
callback: (err: null, buffer: B) => void
): void

export function randomFill<B extends ArrayBuffer | Buffer | DataView>(
buffer: B,
callback: (err: null, buffer: B) => void
): void

export { CryptoError as errors, randomFill as randomFillSync }
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
"version": "1.3.1",
"description": "Cryptographic primitives for JavaScript",
"exports": {
".": "./index.js",
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./package": "./package.json",
"./constants": "./lib/constants.js",
"./errors": "./lib/errors.js"
},
"files": [
"index.js",
"index.d.ts",
"binding.c",
"binding.js",
"CMakeLists.txt",
Expand All @@ -34,10 +38,14 @@
"bare-stream": "^2.0.0"
},
"devDependencies": {
"bare-buffer": "^3.0.1",
"brittle": "^3.5.0",
"cmake-bare": "^1.1.6",
"cmake-fetch": "^1.0.0",
"prettier": "^3.4.2",
"prettier-config-standard": "^7.0.0"
},
"peerDependencies": {
"bare-buffer": "*"
}
}

0 comments on commit 716fce2

Please sign in to comment.