@digitaldefiance/secrets 2.0.3

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 (79) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +445 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/browser-types.d.ts +112 -0
  5. package/dist/browser-types.d.ts.map +1 -0
  6. package/dist/browser-types.js +242 -0
  7. package/dist/browser-types.js.map +1 -0
  8. package/dist/errors.d.ts +33 -0
  9. package/dist/errors.d.ts.map +1 -0
  10. package/dist/errors.js +92 -0
  11. package/dist/errors.js.map +1 -0
  12. package/dist/esm/.tsbuildinfo +1 -0
  13. package/dist/esm/browser-types.d.ts +112 -0
  14. package/dist/esm/browser-types.d.ts.map +1 -0
  15. package/dist/esm/browser-types.js +201 -0
  16. package/dist/esm/browser-types.js.map +1 -0
  17. package/dist/esm/errors.d.ts +33 -0
  18. package/dist/esm/errors.d.ts.map +1 -0
  19. package/dist/esm/errors.js +54 -0
  20. package/dist/esm/errors.js.map +1 -0
  21. package/dist/esm/node-types.d.ts +87 -0
  22. package/dist/esm/node-types.d.ts.map +1 -0
  23. package/dist/esm/node-types.js +157 -0
  24. package/dist/esm/node-types.js.map +1 -0
  25. package/dist/esm/secrets.d.ts +262 -0
  26. package/dist/esm/secrets.d.ts.map +1 -0
  27. package/dist/esm/secrets.js +894 -0
  28. package/dist/esm/secrets.js.map +1 -0
  29. package/dist/esm/types.d.ts +249 -0
  30. package/dist/esm/types.d.ts.map +1 -0
  31. package/dist/esm/types.js +61 -0
  32. package/dist/esm/types.js.map +1 -0
  33. package/dist/esm/validation.d.ts +34 -0
  34. package/dist/esm/validation.d.ts.map +1 -0
  35. package/dist/esm/validation.js +72 -0
  36. package/dist/esm/validation.js.map +1 -0
  37. package/dist/node-types.d.ts +87 -0
  38. package/dist/node-types.d.ts.map +1 -0
  39. package/dist/node-types.js +191 -0
  40. package/dist/node-types.js.map +1 -0
  41. package/dist/secrets.d.ts +262 -0
  42. package/dist/secrets.d.ts.map +1 -0
  43. package/dist/secrets.js +897 -0
  44. package/dist/secrets.js.map +1 -0
  45. package/dist/types.d.ts +249 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +67 -0
  48. package/dist/types.js.map +1 -0
  49. package/dist/umd/.tsbuildinfo +1 -0
  50. package/dist/umd/browser-types.d.ts +112 -0
  51. package/dist/umd/browser-types.d.ts.map +1 -0
  52. package/dist/umd/browser-types.js +252 -0
  53. package/dist/umd/browser-types.js.map +1 -0
  54. package/dist/umd/errors.d.ts +33 -0
  55. package/dist/umd/errors.d.ts.map +1 -0
  56. package/dist/umd/errors.js +102 -0
  57. package/dist/umd/errors.js.map +1 -0
  58. package/dist/umd/node-types.d.ts +87 -0
  59. package/dist/umd/node-types.d.ts.map +1 -0
  60. package/dist/umd/node-types.js +201 -0
  61. package/dist/umd/node-types.js.map +1 -0
  62. package/dist/umd/secrets.d.ts +262 -0
  63. package/dist/umd/secrets.d.ts.map +1 -0
  64. package/dist/umd/secrets.js +907 -0
  65. package/dist/umd/secrets.js.map +1 -0
  66. package/dist/umd/secrets.min.js +2 -0
  67. package/dist/umd/types.d.ts +249 -0
  68. package/dist/umd/types.d.ts.map +1 -0
  69. package/dist/umd/types.js +77 -0
  70. package/dist/umd/types.js.map +1 -0
  71. package/dist/umd/validation.d.ts +34 -0
  72. package/dist/umd/validation.d.ts.map +1 -0
  73. package/dist/umd/validation.js +92 -0
  74. package/dist/umd/validation.js.map +1 -0
  75. package/dist/validation.d.ts +34 -0
  76. package/dist/validation.d.ts.map +1 -0
  77. package/dist/validation.js +82 -0
  78. package/dist/validation.js.map +1 -0
  79. package/package.json +181 -0
@@ -0,0 +1,112 @@
1
+ import type { RNGFunction, CSPRNGType } from './types';
2
+ import { CryptoError } from './errors';
3
+ /**
4
+ * Enhanced browser Web Crypto API interface with comprehensive typing
5
+ */
6
+ export interface BrowserCryptoAPI {
7
+ /**
8
+ * Generates cryptographically strong random values
9
+ * @param array - Typed array to fill with random values
10
+ * @returns The same array filled with random values
11
+ * @throws {Error} If random value generation fails
12
+ */
13
+ getRandomValues<T extends ArrayBufferView>(array: T): T;
14
+ /**
15
+ * SubtleCrypto interface for advanced cryptographic operations
16
+ */
17
+ subtle?: SubtleCrypto;
18
+ }
19
+ /**
20
+ * Browser window.crypto interface
21
+ */
22
+ export interface WindowCrypto {
23
+ crypto?: BrowserCryptoAPI;
24
+ }
25
+ /**
26
+ * Type guard for Uint32Array
27
+ */
28
+ export declare function isUint32Array(value: unknown): value is Uint32Array;
29
+ /**
30
+ * Type guard for any typed array
31
+ */
32
+ export declare function isTypedArray(value: unknown): value is ArrayBufferView;
33
+ /**
34
+ * Detects if the current environment is a browser
35
+ * @returns true if running in a browser, false otherwise
36
+ */
37
+ export declare function isBrowserEnvironment(): boolean;
38
+ /**
39
+ * Attempts to access the browser Web Crypto API
40
+ * @returns BrowserCryptoAPI if available, null otherwise
41
+ */
42
+ export declare function getBrowserCrypto(): BrowserCryptoAPI | null;
43
+ /**
44
+ * Checks if browser crypto.getRandomValues is available
45
+ * @returns true if crypto.getRandomValues is available
46
+ */
47
+ export declare function hasBrowserCryptoGetRandomValues(): boolean;
48
+ /**
49
+ * Error thrown when browser crypto operations fail
50
+ */
51
+ export declare class BrowserCryptoError extends CryptoError {
52
+ readonly originalError?: Error | undefined;
53
+ constructor(operation: string, reason: string, originalError?: Error | undefined);
54
+ }
55
+ /**
56
+ * Error thrown when browser environment is required but not available
57
+ */
58
+ export declare class BrowserEnvironmentError extends CryptoError {
59
+ constructor(feature: string);
60
+ }
61
+ /**
62
+ * Error thrown when Web Crypto API is not supported
63
+ */
64
+ export declare class WebCryptoNotSupportedError extends BrowserCryptoError {
65
+ constructor();
66
+ }
67
+ /**
68
+ * Creates a browser-specific RNG function using crypto.getRandomValues
69
+ * @param crypto - Browser Web Crypto API
70
+ * @returns RNG function that generates random binary strings
71
+ * @throws {BrowserCryptoError} If random value generation fails
72
+ */
73
+ export declare function createBrowserRNG(crypto: BrowserCryptoAPI): RNGFunction;
74
+ /**
75
+ * Browser-specific crypto environment descriptor
76
+ */
77
+ export interface BrowserCryptoEnvironment {
78
+ readonly type: 'browser';
79
+ readonly crypto: BrowserCryptoAPI;
80
+ readonly rngType: CSPRNGType;
81
+ readonly rng: RNGFunction;
82
+ }
83
+ /**
84
+ * Creates a browser crypto environment descriptor
85
+ * @returns BrowserCryptoEnvironment if browser crypto is available, null otherwise
86
+ */
87
+ export declare function createBrowserCryptoEnvironment(): BrowserCryptoEnvironment | null;
88
+ /**
89
+ * Validates that a value is a valid Uint32Array
90
+ * @param value - Value to validate
91
+ * @param paramName - Parameter name for error messages
92
+ * @throws {BrowserCryptoError} If value is not a valid Uint32Array
93
+ */
94
+ export declare function validateUint32Array(value: unknown, paramName: string): asserts value is Uint32Array;
95
+ /**
96
+ * Validates that a value is a valid typed array
97
+ * @param value - Value to validate
98
+ * @param paramName - Parameter name for error messages
99
+ * @throws {BrowserCryptoError} If value is not a valid typed array
100
+ */
101
+ export declare function validateTypedArray(value: unknown, paramName: string): asserts value is ArrayBufferView;
102
+ /**
103
+ * Validates that browser crypto is available
104
+ * @throws {WebCryptoNotSupportedError} If browser crypto is not available
105
+ */
106
+ export declare function requireBrowserCrypto(): BrowserCryptoAPI;
107
+ /**
108
+ * Checks if the browser supports the Web Crypto API
109
+ * @returns true if Web Crypto API is fully supported
110
+ */
111
+ export declare function isWebCryptoSupported(): boolean;
112
+ //# sourceMappingURL=browser-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-types.d.ts","sourceRoot":"","sources":["../../src/browser-types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAMvC;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,eAAe,CAAC,CAAC,SAAS,eAAe,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wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,WAAW,CAOnG;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,eAAe,CAOtG;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,gBAAgB,CAQvD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,OAAO,CAG9C"}
@@ -0,0 +1,201 @@
1
+ // Browser-specific type definitions and error handling
2
+ // Provides enhanced typing for browser Web Crypto API and environment-specific behavior
3
+ import { CryptoError } from './errors';
4
+ /**
5
+ * Type guard for Uint32Array
6
+ */
7
+ export function isUint32Array(value) {
8
+ var _a;
9
+ return value !== null &&
10
+ typeof value === 'object' &&
11
+ ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Uint32Array';
12
+ }
13
+ /**
14
+ * Type guard for any typed array
15
+ */
16
+ export function isTypedArray(value) {
17
+ return value !== null &&
18
+ typeof value === 'object' &&
19
+ ArrayBuffer.isView(value);
20
+ }
21
+ // ============================================================================
22
+ // Browser Environment Detection
23
+ // ============================================================================
24
+ /**
25
+ * Detects if the current environment is a browser
26
+ * @returns true if running in a browser, false otherwise
27
+ */
28
+ export function isBrowserEnvironment() {
29
+ return typeof window !== 'undefined' &&
30
+ typeof window.document !== 'undefined';
31
+ }
32
+ /**
33
+ * Attempts to access the browser Web Crypto API
34
+ * @returns BrowserCryptoAPI if available, null otherwise
35
+ */
36
+ export function getBrowserCrypto() {
37
+ // Check for window.crypto
38
+ if (typeof window !== 'undefined' && window.crypto) {
39
+ const crypto = window.crypto;
40
+ if (typeof crypto.getRandomValues === 'function' ||
41
+ typeof crypto.getRandomValues === 'object') {
42
+ return crypto;
43
+ }
44
+ }
45
+ // Check for global crypto (some environments)
46
+ if (typeof global !== 'undefined' && global.crypto) {
47
+ const crypto = global.crypto;
48
+ if (typeof crypto.getRandomValues === 'function' ||
49
+ typeof crypto.getRandomValues === 'object') {
50
+ return crypto;
51
+ }
52
+ }
53
+ return null;
54
+ }
55
+ /**
56
+ * Checks if browser crypto.getRandomValues is available
57
+ * @returns true if crypto.getRandomValues is available
58
+ */
59
+ export function hasBrowserCryptoGetRandomValues() {
60
+ const crypto = getBrowserCrypto();
61
+ if (!crypto) {
62
+ return false;
63
+ }
64
+ // Check if Uint32Array is available
65
+ if (typeof Uint32Array !== 'function' && typeof Uint32Array !== 'object') {
66
+ return false;
67
+ }
68
+ return typeof crypto.getRandomValues === 'function' ||
69
+ typeof crypto.getRandomValues === 'object';
70
+ }
71
+ // ============================================================================
72
+ // Browser-Specific Error Types
73
+ // ============================================================================
74
+ /**
75
+ * Error thrown when browser crypto operations fail
76
+ */
77
+ export class BrowserCryptoError extends CryptoError {
78
+ constructor(operation, reason, originalError) {
79
+ super(operation, `Browser crypto error: ${reason}`);
80
+ this.originalError = originalError;
81
+ this.name = 'BrowserCryptoError';
82
+ if (originalError && Error.captureStackTrace) {
83
+ Error.captureStackTrace(this, BrowserCryptoError);
84
+ }
85
+ }
86
+ }
87
+ /**
88
+ * Error thrown when browser environment is required but not available
89
+ */
90
+ export class BrowserEnvironmentError extends CryptoError {
91
+ constructor(feature) {
92
+ super('environment_check', `Feature '${feature}' requires browser environment but is not available`);
93
+ this.name = 'BrowserEnvironmentError';
94
+ }
95
+ }
96
+ /**
97
+ * Error thrown when Web Crypto API is not supported
98
+ */
99
+ export class WebCryptoNotSupportedError extends BrowserCryptoError {
100
+ constructor() {
101
+ super('getRandomValues', 'Web Crypto API is not supported in this browser');
102
+ this.name = 'WebCryptoNotSupportedError';
103
+ }
104
+ }
105
+ // ============================================================================
106
+ // Browser RNG Implementation
107
+ // ============================================================================
108
+ /**
109
+ * Creates a browser-specific RNG function using crypto.getRandomValues
110
+ * @param crypto - Browser Web Crypto API
111
+ * @returns RNG function that generates random binary strings
112
+ * @throws {BrowserCryptoError} If random value generation fails
113
+ */
114
+ export function createBrowserRNG(crypto) {
115
+ return function browserCryptoGetRandomValues(bits) {
116
+ if (typeof bits !== 'number' || bits <= 0 || bits % 1 !== 0) {
117
+ throw new BrowserCryptoError('getRandomValues', `Invalid bits parameter: ${bits}. Must be a positive integer.`);
118
+ }
119
+ // Calculate number of 32-bit integers needed
120
+ const elems = Math.ceil(bits / 32);
121
+ try {
122
+ // Generate random values
123
+ const array = new Uint32Array(elems);
124
+ crypto.getRandomValues(array);
125
+ // Convert to binary string
126
+ let binary = '';
127
+ for (let i = 0; i < array.length; i++) {
128
+ const value = array[i];
129
+ const bin = value.toString(2);
130
+ // Pad to 32 bits
131
+ binary += '00000000000000000000000000000000'.substring(bin.length) + bin;
132
+ }
133
+ // Return exactly the requested number of bits
134
+ return binary.substring(0, bits);
135
+ }
136
+ catch (error) {
137
+ throw new BrowserCryptoError('getRandomValues', 'Failed to generate random values', error);
138
+ }
139
+ };
140
+ }
141
+ /**
142
+ * Creates a browser crypto environment descriptor
143
+ * @returns BrowserCryptoEnvironment if browser crypto is available, null otherwise
144
+ */
145
+ export function createBrowserCryptoEnvironment() {
146
+ const crypto = getBrowserCrypto();
147
+ if (!crypto) {
148
+ return null;
149
+ }
150
+ return {
151
+ type: 'browser',
152
+ crypto,
153
+ rngType: 'browserCryptoGetRandomValues',
154
+ rng: createBrowserRNG(crypto)
155
+ };
156
+ }
157
+ // ============================================================================
158
+ // Type Validation for Browser
159
+ // ============================================================================
160
+ /**
161
+ * Validates that a value is a valid Uint32Array
162
+ * @param value - Value to validate
163
+ * @param paramName - Parameter name for error messages
164
+ * @throws {BrowserCryptoError} If value is not a valid Uint32Array
165
+ */
166
+ export function validateUint32Array(value, paramName) {
167
+ if (!isUint32Array(value)) {
168
+ throw new BrowserCryptoError('validation', `Parameter '${paramName}' must be a Uint32Array`);
169
+ }
170
+ }
171
+ /**
172
+ * Validates that a value is a valid typed array
173
+ * @param value - Value to validate
174
+ * @param paramName - Parameter name for error messages
175
+ * @throws {BrowserCryptoError} If value is not a valid typed array
176
+ */
177
+ export function validateTypedArray(value, paramName) {
178
+ if (!isTypedArray(value)) {
179
+ throw new BrowserCryptoError('validation', `Parameter '${paramName}' must be a typed array`);
180
+ }
181
+ }
182
+ /**
183
+ * Validates that browser crypto is available
184
+ * @throws {WebCryptoNotSupportedError} If browser crypto is not available
185
+ */
186
+ export function requireBrowserCrypto() {
187
+ const crypto = getBrowserCrypto();
188
+ if (!crypto) {
189
+ throw new WebCryptoNotSupportedError();
190
+ }
191
+ return crypto;
192
+ }
193
+ /**
194
+ * Checks if the browser supports the Web Crypto API
195
+ * @returns true if Web Crypto API is fully supported
196
+ */
197
+ export function isWebCryptoSupported() {
198
+ return hasBrowserCryptoGetRandomValues() &&
199
+ (typeof Uint32Array === 'function' || typeof Uint32Array === 'object');
200
+ }
201
+ //# sourceMappingURL=browser-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-types.js","sourceRoot":"","sources":["../../src/browser-types.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,wFAAwF;AAGxF,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AA+BvC;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;;IAC1C,OAAO,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAA,MAAA,KAAK,CAAC,WAAW,0CAAE,IAAI,MAAK,aAAa,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC;AAED,+EAA+E;AAC/E,gCAAgC;AAChC,+EAA+E;AAE/E;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,OAAO,MAAM,KAAK,WAAW;QAC7B,OAAO,MAA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MAAM,UAAU,kBAAkB,CAAC,KAAc,EAAE,SAAiB;IAClE,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,kBAAkB,CAC1B,YAAY,EACZ,cAAc,SAAS,yBAAyB,CACjD,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,MAAM,MAAM,GAAG,gBAAgB,EAAE,CAAC;IAElC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,0BAA0B,EAAE,CAAC;IACzC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,+BAA+B,EAAE;QACjC,CAAC,OAAO,WAAW,KAAK,UAAU,IAAI,OAAO,WAAW,KAAK,QAAQ,CAAC,CAAC;AAChF,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Base error class for all secrets library errors
3
+ */
4
+ export declare class SecretsError extends Error {
5
+ readonly code: string;
6
+ readonly context?: Record<string, unknown> | undefined;
7
+ constructor(message: string, code: string, context?: Record<string, unknown> | undefined);
8
+ }
9
+ /**
10
+ * Error thrown when invalid parameters are provided to a function
11
+ */
12
+ export declare class InvalidParameterError extends SecretsError {
13
+ constructor(parameter: string, value: unknown, expected: string);
14
+ }
15
+ /**
16
+ * Error thrown when initialization fails
17
+ */
18
+ export declare class InitializationError extends SecretsError {
19
+ constructor(reason: string);
20
+ }
21
+ /**
22
+ * Error thrown when share data is invalid or corrupted
23
+ */
24
+ export declare class InvalidShareError extends SecretsError {
25
+ constructor(share: string, reason: string);
26
+ }
27
+ /**
28
+ * Error thrown when cryptographic operations fail
29
+ */
30
+ export declare class CryptoError extends SecretsError {
31
+ constructor(operation: string, reason: string);
32
+ }
33
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,qBAAa,YAAa,SAAQ,KAAK;aAGnB,IAAI,EAAE,MAAM;aACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;gBAFjD,OAAO,EAAE,MAAM,EACC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YAAA;CAUpD;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,YAAY;gBACzC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM;CAQhE;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,YAAY;gBACvC,MAAM,EAAE,MAAM;CAQ3B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,YAAY;gBACrC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAQ1C;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,YAAY;gBAC/B,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAQ9C"}
@@ -0,0 +1,54 @@
1
+ // Type-safe error handling for @digitaldefiance/secrets
2
+ // Provides structured error classes with context information
3
+ /**
4
+ * Base error class for all secrets library errors
5
+ */
6
+ export class SecretsError extends Error {
7
+ constructor(message, code, context) {
8
+ super(message);
9
+ this.code = code;
10
+ this.context = context;
11
+ this.name = 'SecretsError';
12
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
13
+ if (Error.captureStackTrace) {
14
+ Error.captureStackTrace(this, SecretsError);
15
+ }
16
+ }
17
+ }
18
+ /**
19
+ * Error thrown when invalid parameters are provided to a function
20
+ */
21
+ export class InvalidParameterError extends SecretsError {
22
+ constructor(parameter, value, expected) {
23
+ super(`Invalid parameter '${parameter}': expected ${expected}, got ${typeof value}`, 'INVALID_PARAMETER', { parameter, value, expected });
24
+ this.name = 'InvalidParameterError';
25
+ }
26
+ }
27
+ /**
28
+ * Error thrown when initialization fails
29
+ */
30
+ export class InitializationError extends SecretsError {
31
+ constructor(reason) {
32
+ super(`Initialization failed: ${reason}`, 'INITIALIZATION_FAILED', { reason });
33
+ this.name = 'InitializationError';
34
+ }
35
+ }
36
+ /**
37
+ * Error thrown when share data is invalid or corrupted
38
+ */
39
+ export class InvalidShareError extends SecretsError {
40
+ constructor(share, reason) {
41
+ super(`Invalid share data: ${reason}`, 'INVALID_SHARE', { share, reason });
42
+ this.name = 'InvalidShareError';
43
+ }
44
+ }
45
+ /**
46
+ * Error thrown when cryptographic operations fail
47
+ */
48
+ export class CryptoError extends SecretsError {
49
+ constructor(operation, reason) {
50
+ super(`Cryptographic operation '${operation}' failed: ${reason}`, 'CRYPTO_ERROR', { operation, reason });
51
+ this.name = 'CryptoError';
52
+ }
53
+ }
54
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,6DAA6D;AAE7D;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YACE,OAAe,EACC,IAAY,EACZ,OAAiC;QAEjD,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,SAAI,GAAJ,IAAI,CAAQ;QACZ,YAAO,GAAP,OAAO,CAA0B;QAGjD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAE3B,qFAAqF;QACrF,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAAY;IACrD,YAAY,SAAiB,EAAE,KAAc,EAAE,QAAgB;QAC7D,KAAK,CACH,sBAAsB,SAAS,eAAe,QAAQ,SAAS,OAAO,KAAK,EAAE,EAC7E,mBAAmB,EACnB,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,CAC/B,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,YAAY;IACnD,YAAY,MAAc;QACxB,KAAK,CACH,0BAA0B,MAAM,EAAE,EAClC,uBAAuB,EACvB,EAAE,MAAM,EAAE,CACX,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,YAAY;IACjD,YAAY,KAAa,EAAE,MAAc;QACvC,KAAK,CACH,uBAAuB,MAAM,EAAE,EAC/B,eAAe,EACf,EAAE,KAAK,EAAE,MAAM,EAAE,CAClB,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,WAAY,SAAQ,YAAY;IAC3C,YAAY,SAAiB,EAAE,MAAc;QAC3C,KAAK,CACH,4BAA4B,SAAS,aAAa,MAAM,EAAE,EAC1D,cAAc,EACd,EAAE,SAAS,EAAE,MAAM,EAAE,CACtB,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF"}
@@ -0,0 +1,87 @@
1
+ import type { RNGFunction, CSPRNGType } from './types';
2
+ import { CryptoError } from './errors';
3
+ /**
4
+ * Enhanced Node.js crypto module interface with comprehensive typing
5
+ */
6
+ export interface NodeCryptoModule {
7
+ /**
8
+ * Generates cryptographically strong pseudo-random data
9
+ * @param size - Number of bytes to generate
10
+ * @returns Buffer containing random bytes
11
+ * @throws {Error} If random data generation fails
12
+ */
13
+ randomBytes(size: number): Buffer;
14
+ /**
15
+ * Synchronous version of randomBytes
16
+ * @param size - Number of bytes to generate
17
+ * @returns Buffer containing random bytes
18
+ * @throws {Error} If random data generation fails
19
+ */
20
+ randomBytesSync?(size: number): Buffer;
21
+ }
22
+ /**
23
+ * Node.js Buffer type guard
24
+ */
25
+ export declare function isNodeBuffer(value: unknown): value is Buffer;
26
+ /**
27
+ * Detects if the current environment is Node.js
28
+ * @returns true if running in Node.js, false otherwise
29
+ */
30
+ export declare function isNodeEnvironment(): boolean;
31
+ /**
32
+ * Attempts to load the Node.js crypto module
33
+ * @returns NodeCryptoModule if available, null otherwise
34
+ */
35
+ export declare function getNodeCrypto(): NodeCryptoModule | null;
36
+ /**
37
+ * Checks if Node.js crypto.randomBytes is available
38
+ * @returns true if crypto.randomBytes is available
39
+ */
40
+ export declare function hasNodeCryptoRandomBytes(): boolean;
41
+ /**
42
+ * Error thrown when Node.js crypto operations fail
43
+ */
44
+ export declare class NodeCryptoError extends CryptoError {
45
+ readonly originalError?: Error | undefined;
46
+ constructor(operation: string, reason: string, originalError?: Error | undefined);
47
+ }
48
+ /**
49
+ * Error thrown when Node.js environment is required but not available
50
+ */
51
+ export declare class NodeEnvironmentError extends CryptoError {
52
+ constructor(feature: string);
53
+ }
54
+ /**
55
+ * Creates a Node.js-specific RNG function using crypto.randomBytes
56
+ * @param crypto - Node.js crypto module
57
+ * @returns RNG function that generates random binary strings
58
+ * @throws {NodeCryptoError} If random byte generation fails
59
+ */
60
+ export declare function createNodeRNG(crypto: NodeCryptoModule): RNGFunction;
61
+ /**
62
+ * Node.js-specific crypto environment descriptor
63
+ */
64
+ export interface NodeCryptoEnvironment {
65
+ readonly type: 'node';
66
+ readonly crypto: NodeCryptoModule;
67
+ readonly rngType: CSPRNGType;
68
+ readonly rng: RNGFunction;
69
+ }
70
+ /**
71
+ * Creates a Node.js crypto environment descriptor
72
+ * @returns NodeCryptoEnvironment if Node.js crypto is available, null otherwise
73
+ */
74
+ export declare function createNodeCryptoEnvironment(): NodeCryptoEnvironment | null;
75
+ /**
76
+ * Validates that a value is a valid Node.js Buffer
77
+ * @param value - Value to validate
78
+ * @param paramName - Parameter name for error messages
79
+ * @throws {NodeCryptoError} If value is not a valid Buffer
80
+ */
81
+ export declare function validateNodeBuffer(value: unknown, paramName: string): asserts value is Buffer;
82
+ /**
83
+ * Validates that Node.js crypto is available
84
+ * @throws {NodeEnvironmentError} If Node.js crypto is not available
85
+ */
86
+ export declare function requireNodeCrypto(): NodeCryptoModule;
87
+ //# sourceMappingURL=node-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-types.d.ts","sourceRoot":"","sources":["../../src/node-types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAMvC;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAElC;;;;;OAKG;IACH,eAAe,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CACxC;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAK5D;AAMD;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAI3C;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,gBAAgB,GAAG,IAAI,CAiBvD;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,IAAI,OAAO,CAGlD;AAMD;;GAEG;AACH,qBAAa,eAAgB,SAAQ,WAAW;aACiB,aAAa,CAAC,EAAE,KAAK;gBAAxE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAkB,aAAa,CAAC,EAAE,KAAK,YAAA;CAQrF;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,WAAW;gBACvC,OAAO,EAAE,MAAM;CAO5B;AAMD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,gBAAgB,GAAG,WAAW,CAwCnE;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,IAAI,qBAAqB,GAAG,IAAI,CAa1E;AAMD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAO7F;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,gBAAgB,CAQpD"}
@@ -0,0 +1,157 @@
1
+ // Node.js-specific type definitions and error handling
2
+ // Provides enhanced typing for Node.js crypto APIs and environment-specific behavior
3
+ import { CryptoError } from './errors';
4
+ /**
5
+ * Node.js Buffer type guard
6
+ */
7
+ export function isNodeBuffer(value) {
8
+ return value !== null &&
9
+ typeof value === 'object' &&
10
+ 'length' in value &&
11
+ typeof value.readUInt8 === 'function';
12
+ }
13
+ // ============================================================================
14
+ // Node.js Environment Detection
15
+ // ============================================================================
16
+ /**
17
+ * Detects if the current environment is Node.js
18
+ * @returns true if running in Node.js, false otherwise
19
+ */
20
+ export function isNodeEnvironment() {
21
+ return typeof process !== 'undefined' &&
22
+ process.versions != null &&
23
+ process.versions.node != null;
24
+ }
25
+ /**
26
+ * Attempts to load the Node.js crypto module
27
+ * @returns NodeCryptoModule if available, null otherwise
28
+ */
29
+ export function getNodeCrypto() {
30
+ if (!isNodeEnvironment()) {
31
+ return null;
32
+ }
33
+ try {
34
+ // Dynamic require to avoid bundler issues
35
+ const crypto = require('crypto');
36
+ if (typeof crypto === 'object' && typeof crypto.randomBytes === 'function') {
37
+ return crypto;
38
+ }
39
+ return null;
40
+ }
41
+ catch (error) {
42
+ return null;
43
+ }
44
+ }
45
+ /**
46
+ * Checks if Node.js crypto.randomBytes is available
47
+ * @returns true if crypto.randomBytes is available
48
+ */
49
+ export function hasNodeCryptoRandomBytes() {
50
+ const crypto = getNodeCrypto();
51
+ return crypto !== null && typeof crypto.randomBytes === 'function';
52
+ }
53
+ // ============================================================================
54
+ // Node.js-Specific Error Types
55
+ // ============================================================================
56
+ /**
57
+ * Error thrown when Node.js crypto operations fail
58
+ */
59
+ export class NodeCryptoError extends CryptoError {
60
+ constructor(operation, reason, originalError) {
61
+ super(operation, `Node.js crypto error: ${reason}`);
62
+ this.originalError = originalError;
63
+ this.name = 'NodeCryptoError';
64
+ if (originalError && Error.captureStackTrace) {
65
+ Error.captureStackTrace(this, NodeCryptoError);
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * Error thrown when Node.js environment is required but not available
71
+ */
72
+ export class NodeEnvironmentError extends CryptoError {
73
+ constructor(feature) {
74
+ super('environment_check', `Feature '${feature}' requires Node.js environment but is not available`);
75
+ this.name = 'NodeEnvironmentError';
76
+ }
77
+ }
78
+ // ============================================================================
79
+ // Node.js RNG Implementation
80
+ // ============================================================================
81
+ /**
82
+ * Creates a Node.js-specific RNG function using crypto.randomBytes
83
+ * @param crypto - Node.js crypto module
84
+ * @returns RNG function that generates random binary strings
85
+ * @throws {NodeCryptoError} If random byte generation fails
86
+ */
87
+ export function createNodeRNG(crypto) {
88
+ return function nodeCryptoRandomBytes(bits) {
89
+ if (typeof bits !== 'number' || bits <= 0 || bits % 1 !== 0) {
90
+ throw new NodeCryptoError('randomBytes', `Invalid bits parameter: ${bits}. Must be a positive integer.`);
91
+ }
92
+ const bytes = Math.ceil(bits / 8);
93
+ try {
94
+ const buffer = crypto.randomBytes(bytes);
95
+ // Convert buffer to hex string
96
+ let hex = '';
97
+ for (let i = 0; i < buffer.length; i++) {
98
+ const byte = buffer[i].toString(16);
99
+ hex += byte.length === 1 ? '0' + byte : byte;
100
+ }
101
+ // Convert hex to binary
102
+ let binary = '';
103
+ for (let i = 0; i < hex.length; i++) {
104
+ const num = parseInt(hex[i], 16);
105
+ const bin = num.toString(2);
106
+ binary += '0000'.substring(bin.length) + bin;
107
+ }
108
+ // Return exactly the requested number of bits
109
+ return binary.substring(0, bits);
110
+ }
111
+ catch (error) {
112
+ throw new NodeCryptoError('randomBytes', 'Failed to generate random bytes', error);
113
+ }
114
+ };
115
+ }
116
+ /**
117
+ * Creates a Node.js crypto environment descriptor
118
+ * @returns NodeCryptoEnvironment if Node.js crypto is available, null otherwise
119
+ */
120
+ export function createNodeCryptoEnvironment() {
121
+ const crypto = getNodeCrypto();
122
+ if (!crypto) {
123
+ return null;
124
+ }
125
+ return {
126
+ type: 'node',
127
+ crypto,
128
+ rngType: 'nodeCryptoRandomBytes',
129
+ rng: createNodeRNG(crypto)
130
+ };
131
+ }
132
+ // ============================================================================
133
+ // Type Validation for Node.js
134
+ // ============================================================================
135
+ /**
136
+ * Validates that a value is a valid Node.js Buffer
137
+ * @param value - Value to validate
138
+ * @param paramName - Parameter name for error messages
139
+ * @throws {NodeCryptoError} If value is not a valid Buffer
140
+ */
141
+ export function validateNodeBuffer(value, paramName) {
142
+ if (!isNodeBuffer(value)) {
143
+ throw new NodeCryptoError('validation', `Parameter '${paramName}' must be a Node.js Buffer`);
144
+ }
145
+ }
146
+ /**
147
+ * Validates that Node.js crypto is available
148
+ * @throws {NodeEnvironmentError} If Node.js crypto is not available
149
+ */
150
+ export function requireNodeCrypto() {
151
+ const crypto = getNodeCrypto();
152
+ if (!crypto) {
153
+ throw new NodeEnvironmentError('crypto.randomBytes');
154
+ }
155
+ return crypto;
156
+ }
157
+ //# sourceMappingURL=node-types.js.map