Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/builder/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference lib="webworker" />
/// <reference types="node" />

/**
* A module that provides a cross-platform cryptographic interface.
* This module uses the Web Crypto API in browsers and falls back to Node.js's crypto module
* when running in a Node.js environment.
*
* @module crypto
*/

const isomorphicCrypto =
globalThis.crypto ?? (await import('node:crypto')).webcrypto;

/**
* A cryptographic interface that provides methods for generating random values
* and accessing subtle cryptographic operations.
*/
export const crypto = {
/**
* Fills the given typed array with cryptographically secure random values.
*
* @param {T} array - The typed array to fill with random values.
* @returns {T} The filled typed array.
* @template T - The type of the typed array (e.g., Uint8Array).
*/
getRandomValues<T extends Uint8Array>(array: T): T {
return isomorphicCrypto.getRandomValues(array);
},

/**
* Provides access to subtle cryptographic operations.
*
* @type {SubtleCrypto} The subtle cryptographic interface.
*/
subtle: isomorphicCrypto.subtle,
};
3 changes: 3 additions & 0 deletions packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"files": ["dist/lib/**/*.js", "dist/lib/**/*.d.ts"],
"scripts": {
"build": "tsc --build"
},
"devDependencies": {
"@types/node": "^22.14.1"
}
}
18 changes: 17 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.