@jscv-solutions/node-crypto 0.0.0-stage → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +92 -2
  3. package/dist/cjs/package.json +6 -0
  4. package/dist/cjs/packages/comparison/index.d.ts +5 -0
  5. package/dist/cjs/packages/comparison/index.js +8 -0
  6. package/dist/cjs/packages/digest/index.d.ts +5 -0
  7. package/dist/cjs/packages/digest/index.js +8 -0
  8. package/dist/cjs/packages/encryption/index.d.ts +5 -0
  9. package/dist/cjs/packages/encryption/index.js +8 -0
  10. package/dist/cjs/packages/key-derivation/index.d.ts +5 -0
  11. package/dist/cjs/packages/key-derivation/index.js +8 -0
  12. package/dist/cjs/packages/message-authentication/index.d.ts +5 -0
  13. package/dist/cjs/packages/message-authentication/index.js +8 -0
  14. package/dist/cjs/packages/password-hashing/index.d.ts +5 -0
  15. package/dist/cjs/packages/password-hashing/index.js +8 -0
  16. package/dist/cjs/packages/random/index.d.ts +5 -0
  17. package/dist/cjs/packages/random/index.js +8 -0
  18. package/dist/cjs/shared/algorithms.d.ts +8 -0
  19. package/dist/cjs/shared/algorithms.js +24 -0
  20. package/dist/cjs/shared/deprecation.d.ts +23 -0
  21. package/dist/cjs/shared/deprecation.js +38 -0
  22. package/dist/cjs/shared/encodings.d.ts +30 -0
  23. package/dist/cjs/shared/encodings.js +69 -0
  24. package/dist/cjs/shared/errors.d.ts +79 -0
  25. package/dist/cjs/shared/errors.js +89 -0
  26. package/dist/cjs/shared/inputs.d.ts +9 -0
  27. package/dist/cjs/shared/inputs.js +29 -0
  28. package/dist/cjs/shared/peer-loading.d.ts +16 -0
  29. package/dist/cjs/shared/peer-loading.js +129 -0
  30. package/dist/cjs/shared/require-sync-cjs.d.ts +11 -0
  31. package/dist/cjs/shared/require-sync-cjs.js +18 -0
  32. package/dist/cjs/shared/typings/encodings.d.ts +2 -0
  33. package/dist/cjs/shared/typings/errors.d.ts +10 -0
  34. package/dist/cjs/shared/typings/inputs.d.ts +2 -0
  35. package/dist/esm/package.json +6 -0
  36. package/dist/esm/packages/comparison/index.d.ts +5 -0
  37. package/dist/esm/packages/comparison/index.js +5 -0
  38. package/dist/esm/packages/digest/index.d.ts +5 -0
  39. package/dist/esm/packages/digest/index.js +5 -0
  40. package/dist/esm/packages/encryption/index.d.ts +5 -0
  41. package/dist/esm/packages/encryption/index.js +5 -0
  42. package/dist/esm/packages/key-derivation/index.d.ts +5 -0
  43. package/dist/esm/packages/key-derivation/index.js +5 -0
  44. package/dist/esm/packages/message-authentication/index.d.ts +5 -0
  45. package/dist/esm/packages/message-authentication/index.js +5 -0
  46. package/dist/esm/packages/password-hashing/index.d.ts +5 -0
  47. package/dist/esm/packages/password-hashing/index.js +5 -0
  48. package/dist/esm/packages/random/index.d.ts +5 -0
  49. package/dist/esm/packages/random/index.js +5 -0
  50. package/dist/esm/shared/algorithms.d.ts +8 -0
  51. package/dist/esm/shared/algorithms.js +21 -0
  52. package/dist/esm/shared/deprecation.d.ts +23 -0
  53. package/dist/esm/shared/deprecation.js +34 -0
  54. package/dist/esm/shared/encodings.d.ts +30 -0
  55. package/dist/esm/shared/encodings.js +64 -0
  56. package/dist/esm/shared/errors.d.ts +79 -0
  57. package/dist/esm/shared/errors.js +81 -0
  58. package/dist/esm/shared/inputs.d.ts +9 -0
  59. package/dist/esm/shared/inputs.js +26 -0
  60. package/dist/esm/shared/peer-loading.d.ts +16 -0
  61. package/dist/esm/shared/peer-loading.js +92 -0
  62. package/dist/esm/shared/require-sync-esm.d.ts +7 -0
  63. package/dist/esm/shared/require-sync-esm.js +20 -0
  64. package/dist/esm/shared/typings/encodings.d.ts +2 -0
  65. package/dist/esm/shared/typings/errors.d.ts +10 -0
  66. package/dist/esm/shared/typings/inputs.d.ts +2 -0
  67. package/package.json +157 -3
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Algorithm identifier validation shared by every namespace.
3
+ *
4
+ * Identifiers are lowercase kebab-case strings (for example `sha-256`,
5
+ * `argon2id`). Anything a function does not support — unknown names and
6
+ * malformed identifiers alike — throws an `UnsupportedAlgorithmError`.
7
+ * Identifiers are public names and safe to include in messages.
8
+ */
9
+ import { UnsupportedAlgorithmError } from './errors.js';
10
+ /**
11
+ * Asserts the received identifier is one of the supported identifiers.
12
+ *
13
+ * @param algorithm - Identifier passed by the caller.
14
+ * @param supported - Identifiers the calling function supports.
15
+ * @throws {UnsupportedAlgorithmError} WHEN the identifier is unsupported.
16
+ */
17
+ export function assertSupportedAlgorithm(algorithm, supported) {
18
+ if (!supported.includes(algorithm)) {
19
+ throw new UnsupportedAlgorithmError(`Unsupported algorithm '${algorithm}'. Supported algorithms: ${supported.join(', ')}.`);
20
+ }
21
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Deprecation signalling shared by every namespace.
3
+ *
4
+ * A deprecated algorithm is marked with the JSDoc `@deprecated` tag at
5
+ * its declaration site and calls `emitDeprecationWarning` on first use.
6
+ * Warnings go through `process.emitWarning` (never `console.warn`) so
7
+ * they honor `--no-warnings` and `--disable-warning`, and each stable
8
+ * warning code is emitted at most once per process. The algorithm name
9
+ * is public and safe to include in the message.
10
+ */
11
+ /**
12
+ * Derives the stable process-warning code for a deprecated algorithm.
13
+ *
14
+ * @param algorithm - Lowercase kebab-case algorithm identifier.
15
+ * @returns The stable warning code for the algorithm.
16
+ */
17
+ export declare function deprecationCodeFor(algorithm: string): string;
18
+ /**
19
+ * Emits the deprecation warning for an algorithm, once per process.
20
+ *
21
+ * @param algorithm - Lowercase kebab-case algorithm identifier.
22
+ */
23
+ export declare function emitDeprecationWarning(algorithm: string): void;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Deprecation signalling shared by every namespace.
3
+ *
4
+ * A deprecated algorithm is marked with the JSDoc `@deprecated` tag at
5
+ * its declaration site and calls `emitDeprecationWarning` on first use.
6
+ * Warnings go through `process.emitWarning` (never `console.warn`) so
7
+ * they honor `--no-warnings` and `--disable-warning`, and each stable
8
+ * warning code is emitted at most once per process. The algorithm name
9
+ * is public and safe to include in the message.
10
+ */
11
+ const emittedCodes = new Set();
12
+ /**
13
+ * Derives the stable process-warning code for a deprecated algorithm.
14
+ *
15
+ * @param algorithm - Lowercase kebab-case algorithm identifier.
16
+ * @returns The stable warning code for the algorithm.
17
+ */
18
+ export function deprecationCodeFor(algorithm) {
19
+ const token = algorithm.toUpperCase().replace(/-/g, '_');
20
+ return `NODE_CRYPTO_DEPRECATED_${token}`;
21
+ }
22
+ /**
23
+ * Emits the deprecation warning for an algorithm, once per process.
24
+ *
25
+ * @param algorithm - Lowercase kebab-case algorithm identifier.
26
+ */
27
+ export function emitDeprecationWarning(algorithm) {
28
+ const code = deprecationCodeFor(algorithm);
29
+ if (emittedCodes.has(code)) {
30
+ return;
31
+ }
32
+ emittedCodes.add(code);
33
+ process.emitWarning(`The '${algorithm}' algorithm is deprecated and will be removed in a future major release.`, { code, type: 'DeprecationWarning' });
34
+ }
@@ -0,0 +1,30 @@
1
+ import type { Encoding } from './typings/encodings.js';
2
+ /**
3
+ * Encodes bytes as text with the requested encoding.
4
+ *
5
+ * @param data - Bytes to encode.
6
+ * @param encoding - Target text encoding.
7
+ * @returns The encoded text (`base64url` output is unpadded).
8
+ */
9
+ export declare function encodeBytes(data: Uint8Array, encoding: Encoding): string;
10
+ /**
11
+ * Decodes text back into bytes, strictly validating the input.
12
+ *
13
+ * @param text - Encoded text to decode.
14
+ * @param encoding - Text encoding the input claims to use.
15
+ * @returns The decoded bytes.
16
+ * @throws {InvalidEncodingError} WHEN the text breaks the encoding rules.
17
+ */
18
+ export declare function decodeText(text: string, encoding: Encoding): Buffer;
19
+ /**
20
+ * Checks text against the alphabet and shape rules of an encoding.
21
+ *
22
+ * `base64` follows the padded canonical form. `base64url` accepts the
23
+ * unpadded form the encoder emits (whose length is never one past a
24
+ * multiple of four) as well as correctly padded input.
25
+ *
26
+ * @param text - Candidate encoded text.
27
+ * @param encoding - Encoding whose rules apply.
28
+ * @returns Whether the text is well formed for the encoding.
29
+ */
30
+ export declare function isWellFormed(text: string, encoding: Encoding): boolean;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Binary-to-text encodings shared by every namespace.
3
+ *
4
+ * Supported encodings are `hex`, `base64` and `base64url`. Decoding is
5
+ * strict: input outside the encoding alphabet (or breaking its length
6
+ * and padding rules) throws an `InvalidEncodingError` instead of being
7
+ * silently truncated the way `Buffer.from` would on its own. Error
8
+ * messages never echo the offending input.
9
+ */
10
+ import { InvalidEncodingError } from './errors.js';
11
+ const HEXPattern = /^[0-9a-fA-F]*$/;
12
+ const base64Pattern = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
13
+ const base64UrlPaddedPattern = /^(?:[A-Za-z0-9\-_]{4})*(?:[A-Za-z0-9\-_]{2}==|[A-Za-z0-9\-_]{3}=)?$/;
14
+ const base64UrlUnpaddedPattern = /^[A-Za-z0-9\-_]*$/;
15
+ /**
16
+ * Encodes bytes as text with the requested encoding.
17
+ *
18
+ * @param data - Bytes to encode.
19
+ * @param encoding - Target text encoding.
20
+ * @returns The encoded text (`base64url` output is unpadded).
21
+ */
22
+ export function encodeBytes(data, encoding) {
23
+ const buffer = Buffer.from(data);
24
+ const text = buffer.toString(encoding);
25
+ return text;
26
+ }
27
+ /**
28
+ * Decodes text back into bytes, strictly validating the input.
29
+ *
30
+ * @param text - Encoded text to decode.
31
+ * @param encoding - Text encoding the input claims to use.
32
+ * @returns The decoded bytes.
33
+ * @throws {InvalidEncodingError} WHEN the text breaks the encoding rules.
34
+ */
35
+ export function decodeText(text, encoding) {
36
+ if (!isWellFormed(text, encoding)) {
37
+ throw new InvalidEncodingError(`Invalid ${encoding} string: input breaks the ${encoding} rules.`);
38
+ }
39
+ const buffer = Buffer.from(text, encoding);
40
+ return buffer;
41
+ }
42
+ /**
43
+ * Checks text against the alphabet and shape rules of an encoding.
44
+ *
45
+ * `base64` follows the padded canonical form. `base64url` accepts the
46
+ * unpadded form the encoder emits (whose length is never one past a
47
+ * multiple of four) as well as correctly padded input.
48
+ *
49
+ * @param text - Candidate encoded text.
50
+ * @param encoding - Encoding whose rules apply.
51
+ * @returns Whether the text is well formed for the encoding.
52
+ */
53
+ export function isWellFormed(text, encoding) {
54
+ if (encoding === 'hex') {
55
+ return text.length % 2 === 0 && HEXPattern.test(text);
56
+ }
57
+ if (encoding === 'base64') {
58
+ return text.length % 4 === 0 && base64Pattern.test(text);
59
+ }
60
+ if (text.includes('=')) {
61
+ return text.length % 4 === 0 && base64UrlPaddedPattern.test(text);
62
+ }
63
+ return text.length % 4 !== 1 && base64UrlUnpaddedPattern.test(text);
64
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Error model shared by every namespace of the library.
3
+ *
4
+ * Every error thrown by the library is an instance of `NodeCryptoError`
5
+ * (or a subclass) and carries a stable string `code` that callers can
6
+ * switch on. Codes are part of the public contract: never rename or
7
+ * remove one without a major release.
8
+ *
9
+ * Message hygiene: constructors accept a message built by the throwing
10
+ * code. Callers MUST NOT interpolate secrets, passwords, keys or
11
+ * plaintext into messages. Algorithm identifiers and package names are
12
+ * public and safe to include.
13
+ */
14
+ import type { ErrorCode } from './typings/errors.js';
15
+ /**
16
+ * Base class for every error thrown by the library.
17
+ */
18
+ export declare class NodeCryptoError extends Error {
19
+ /** Stable machine-readable failure identifier. */
20
+ readonly code: ErrorCode;
21
+ /**
22
+ * Creates a library error with a stable code.
23
+ *
24
+ * @param message - Human-readable description without secrets.
25
+ * @param code - Stable machine-readable failure identifier.
26
+ * @param options - Optional error options (for example `cause`).
27
+ */
28
+ constructor(message: string, code: ErrorCode, options?: ErrorOptions);
29
+ }
30
+ /**
31
+ * A function received an argument of the wrong type or range.
32
+ */
33
+ export declare class InvalidArgumentError extends NodeCryptoError {
34
+ /**
35
+ * Creates an invalid-argument error.
36
+ *
37
+ * @param message - Human-readable description without secrets.
38
+ * @param options - Optional error options (for example `cause`).
39
+ */
40
+ constructor(message: string, options?: ErrorOptions);
41
+ }
42
+ /**
43
+ * Encoded text contains characters outside its alphabet or breaks the
44
+ * encoding rules (for example odd-length hexadecimal).
45
+ */
46
+ export declare class InvalidEncodingError extends NodeCryptoError {
47
+ /**
48
+ * Creates an invalid-encoding error.
49
+ *
50
+ * @param message - Human-readable description without echoing the input.
51
+ * @param options - Optional error options (for example `cause`).
52
+ */
53
+ constructor(message: string, options?: ErrorOptions);
54
+ }
55
+ /**
56
+ * An optional peer dependency (for example `bcrypt`) is not installed,
57
+ * so the requested operation cannot run.
58
+ */
59
+ export declare class MissingPeerDependencyError extends NodeCryptoError {
60
+ /**
61
+ * Creates a missing-peer-dependency error.
62
+ *
63
+ * @param message - Description naming the package and how to install it.
64
+ * @param options - Optional error options (for example `cause`).
65
+ */
66
+ constructor(message: string, options?: ErrorOptions);
67
+ }
68
+ /**
69
+ * The caller passed an algorithm identifier the function does not support.
70
+ */
71
+ export declare class UnsupportedAlgorithmError extends NodeCryptoError {
72
+ /**
73
+ * Creates an unsupported-algorithm error.
74
+ *
75
+ * @param message - Description naming the received identifier.
76
+ * @param options - Optional error options (for example `cause`).
77
+ */
78
+ constructor(message: string, options?: ErrorOptions);
79
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Base class for every error thrown by the library.
3
+ */
4
+ export class NodeCryptoError extends Error {
5
+ /** Stable machine-readable failure identifier. */
6
+ code;
7
+ /**
8
+ * Creates a library error with a stable code.
9
+ *
10
+ * @param message - Human-readable description without secrets.
11
+ * @param code - Stable machine-readable failure identifier.
12
+ * @param options - Optional error options (for example `cause`).
13
+ */
14
+ constructor(message, code, options) {
15
+ super(message, options);
16
+ this.name = 'NodeCryptoError';
17
+ this.code = code;
18
+ }
19
+ }
20
+ /**
21
+ * A function received an argument of the wrong type or range.
22
+ */
23
+ export class InvalidArgumentError extends NodeCryptoError {
24
+ /**
25
+ * Creates an invalid-argument error.
26
+ *
27
+ * @param message - Human-readable description without secrets.
28
+ * @param options - Optional error options (for example `cause`).
29
+ */
30
+ constructor(message, options) {
31
+ super(message, 'INVALID_ARGUMENT', options);
32
+ this.name = 'InvalidArgumentError';
33
+ }
34
+ }
35
+ /**
36
+ * Encoded text contains characters outside its alphabet or breaks the
37
+ * encoding rules (for example odd-length hexadecimal).
38
+ */
39
+ export class InvalidEncodingError extends NodeCryptoError {
40
+ /**
41
+ * Creates an invalid-encoding error.
42
+ *
43
+ * @param message - Human-readable description without echoing the input.
44
+ * @param options - Optional error options (for example `cause`).
45
+ */
46
+ constructor(message, options) {
47
+ super(message, 'INVALID_ENCODING', options);
48
+ this.name = 'InvalidEncodingError';
49
+ }
50
+ }
51
+ /**
52
+ * An optional peer dependency (for example `bcrypt`) is not installed,
53
+ * so the requested operation cannot run.
54
+ */
55
+ export class MissingPeerDependencyError extends NodeCryptoError {
56
+ /**
57
+ * Creates a missing-peer-dependency error.
58
+ *
59
+ * @param message - Description naming the package and how to install it.
60
+ * @param options - Optional error options (for example `cause`).
61
+ */
62
+ constructor(message, options) {
63
+ super(message, 'MISSING_PEER_DEPENDENCY', options);
64
+ this.name = 'MissingPeerDependencyError';
65
+ }
66
+ }
67
+ /**
68
+ * The caller passed an algorithm identifier the function does not support.
69
+ */
70
+ export class UnsupportedAlgorithmError extends NodeCryptoError {
71
+ /**
72
+ * Creates an unsupported-algorithm error.
73
+ *
74
+ * @param message - Description naming the received identifier.
75
+ * @param options - Optional error options (for example `cause`).
76
+ */
77
+ constructor(message, options) {
78
+ super(message, 'UNSUPPORTED_ALGORITHM', options);
79
+ this.name = 'UnsupportedAlgorithmError';
80
+ }
81
+ }
@@ -0,0 +1,9 @@
1
+ import type { CryptoInput } from './typings/inputs.js';
2
+ /**
3
+ * Normalizes accepted input into a fresh Buffer of bytes.
4
+ *
5
+ * @param input - String (UTF-8), Buffer or Uint8Array holding the data.
6
+ * @returns A Buffer copy of the input bytes.
7
+ * @throws {InvalidArgumentError} WHEN the input has an unexpected type.
8
+ */
9
+ export declare function toBytes(input: CryptoInput): Buffer;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Input handling shared by every namespace.
3
+ *
4
+ * Wherever a function accepts data it accepts `CryptoInput`: a UTF-8
5
+ * string, a Buffer or a Uint8Array. Normalization copies the bytes, so
6
+ * later mutation of a caller-provided view cannot affect the operation.
7
+ */
8
+ import { InvalidArgumentError } from './errors.js';
9
+ /**
10
+ * Normalizes accepted input into a fresh Buffer of bytes.
11
+ *
12
+ * @param input - String (UTF-8), Buffer or Uint8Array holding the data.
13
+ * @returns A Buffer copy of the input bytes.
14
+ * @throws {InvalidArgumentError} WHEN the input has an unexpected type.
15
+ */
16
+ export function toBytes(input) {
17
+ if (typeof input === 'string') {
18
+ const buffer = Buffer.from(input, 'utf8');
19
+ return buffer;
20
+ }
21
+ if (input instanceof Uint8Array) {
22
+ const buffer = Buffer.from(input);
23
+ return buffer;
24
+ }
25
+ throw new InvalidArgumentError('Invalid input: expected a string, Buffer or Uint8Array.');
26
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Loads a peer dependency asynchronously with dynamic `import`.
3
+ *
4
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
5
+ * @returns The loaded peer module.
6
+ * @throws {MissingPeerDependencyError} WHEN the peer cannot be resolved.
7
+ */
8
+ export declare function loadPeer(specifier: string): Promise<unknown>;
9
+ /**
10
+ * Loads a peer dependency synchronously with the per-format loader.
11
+ *
12
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
13
+ * @returns The loaded peer module.
14
+ * @throws {MissingPeerDependencyError} WHEN the peer cannot be resolved.
15
+ */
16
+ export declare function loadPeerSync(specifier: string): unknown;
@@ -0,0 +1,92 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ /**
10
+ * Lazy loading for optional peer dependencies (currently `bcrypt`).
11
+ *
12
+ * Peers load only when an operation needing them is first invoked, so
13
+ * callers without the peer installed can still use every other
14
+ * namespace. Async callers use dynamic `import`; sync callers use the
15
+ * per-format `#sync-require` loader (native `require` in CJS,
16
+ * `createRequire` anchored at the loader file in ESM). A peer that
17
+ * cannot be resolved throws a `MissingPeerDependencyError` naming the
18
+ * package to install; any other load failure propagates unchanged.
19
+ */
20
+ import { loadSyncPeer } from '#sync-require';
21
+ import { MissingPeerDependencyError } from './errors.js';
22
+ /**
23
+ * Extracts the Node.js error code from an unknown load failure.
24
+ *
25
+ * @param error - Failure raised while loading the peer.
26
+ * @returns The error code when present, otherwise undefined.
27
+ */
28
+ function errorCodeOf(error) {
29
+ if (typeof error !== 'object' || error === null) {
30
+ return undefined;
31
+ }
32
+ if (!('code' in error)) {
33
+ return undefined;
34
+ }
35
+ const code = error.code;
36
+ return typeof code === 'string' ? code : undefined;
37
+ }
38
+ /**
39
+ * Reports whether a load failure means the peer is not installed.
40
+ *
41
+ * @param error - Failure raised while loading the peer.
42
+ * @returns Whether the peer module could not be resolved.
43
+ */
44
+ function isPeerMissing(error) {
45
+ const code = errorCodeOf(error);
46
+ return code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND';
47
+ }
48
+ /**
49
+ * Builds the missing-peer error naming the package to install.
50
+ *
51
+ * @param specifier - Bare peer specifier that could not be resolved.
52
+ * @param error - Original resolution failure, kept as `cause`.
53
+ * @returns The missing-peer-dependency error.
54
+ */
55
+ function missingPeerError(specifier, error) {
56
+ return new MissingPeerDependencyError(`Optional peer dependency '${specifier}' is not installed. Install '${specifier}' to use this feature.`, { cause: error });
57
+ }
58
+ /**
59
+ * Loads a peer dependency asynchronously with dynamic `import`.
60
+ *
61
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
62
+ * @returns The loaded peer module.
63
+ * @throws {MissingPeerDependencyError} WHEN the peer cannot be resolved.
64
+ */
65
+ export function loadPeer(specifier) {
66
+ const peer = import(__rewriteRelativeImportExtension(specifier)).catch((error) => {
67
+ if (isPeerMissing(error)) {
68
+ throw missingPeerError(specifier, error);
69
+ }
70
+ throw error;
71
+ });
72
+ return peer;
73
+ }
74
+ /**
75
+ * Loads a peer dependency synchronously with the per-format loader.
76
+ *
77
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
78
+ * @returns The loaded peer module.
79
+ * @throws {MissingPeerDependencyError} WHEN the peer cannot be resolved.
80
+ */
81
+ export function loadPeerSync(specifier) {
82
+ try {
83
+ const peer = loadSyncPeer(specifier);
84
+ return peer;
85
+ }
86
+ catch (error) {
87
+ if (isPeerMissing(error)) {
88
+ throw missingPeerError(specifier, error);
89
+ }
90
+ throw error;
91
+ }
92
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Loads a peer dependency synchronously for ESM callers.
3
+ *
4
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
5
+ * @returns The loaded peer module.
6
+ */
7
+ export declare function loadSyncPeer(specifier: string): unknown;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Synchronous peer loader for ECMAScript module callers.
3
+ *
4
+ * This file is compiled only in the ESM build and the typecheck program
5
+ * (see the `#sync-require` mapping and the per-build `exclude` lists), so
6
+ * `import.meta` never reaches CommonJS output. The `createRequire` anchor
7
+ * is this file, so the peer resolves from the consumer's installation.
8
+ */
9
+ import { createRequire } from 'node:module';
10
+ const peerRequire = createRequire(import.meta.url);
11
+ /**
12
+ * Loads a peer dependency synchronously for ESM callers.
13
+ *
14
+ * @param specifier - Bare peer specifier to load (for example `bcrypt`).
15
+ * @returns The loaded peer module.
16
+ */
17
+ export function loadSyncPeer(specifier) {
18
+ const peer = peerRequire(specifier);
19
+ return peer;
20
+ }
@@ -0,0 +1,2 @@
1
+ /** Text encodings the library reads and writes. */
2
+ export type Encoding = 'hex' | 'base64' | 'base64url';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Stable string codes identifying every failure of the library. Codes
3
+ * are part of the public contract: never rename or remove one without
4
+ * a major release.
5
+ */
6
+ export type ErrorCode =
7
+ | 'INVALID_ARGUMENT'
8
+ | 'INVALID_ENCODING'
9
+ | 'MISSING_PEER_DEPENDENCY'
10
+ | 'UNSUPPORTED_ALGORITHM';
@@ -0,0 +1,2 @@
1
+ /** Data accepted wherever a function takes input bytes. */
2
+ export type CryptoInput = string | Buffer | Uint8Array;