cipher-kit 2.0.0 → 2.1.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.
- package/README.md +26 -28
- package/dist/{chunk-N2EW2FDZ.cjs → chunk-56PVVFVM.cjs} +187 -152
- package/dist/chunk-56PVVFVM.cjs.map +1 -0
- package/dist/{chunk-FKSYSPJR.js → chunk-6C4NIWQ4.js} +24 -22
- package/dist/chunk-6C4NIWQ4.js.map +1 -0
- package/dist/{chunk-4MFF6V3R.js → chunk-FSEA3UXJ.js} +80 -45
- package/dist/chunk-FSEA3UXJ.js.map +1 -0
- package/dist/{chunk-CVCDAHDW.cjs → chunk-LTVOIZP5.cjs} +193 -158
- package/dist/chunk-LTVOIZP5.cjs.map +1 -0
- package/dist/{chunk-ACFPMIXO.js → chunk-PWTFVMW6.js} +79 -44
- package/dist/chunk-PWTFVMW6.js.map +1 -0
- package/dist/{chunk-3UX5MZ2P.cjs → chunk-X4CS7UXE.cjs} +25 -22
- package/dist/chunk-X4CS7UXE.cjs.map +1 -0
- package/dist/{export-55tHE0Bw.d.cts → export-C4DbO5zM.d.ts} +200 -26
- package/dist/{export-llM6c7Do.d.ts → export-CpZ7s25O.d.cts} +200 -26
- package/dist/{export-CQNsJFh_.d.cts → export-DO9n7Np-.d.cts} +200 -26
- package/dist/{export-BMvZq46v.d.ts → export-DUgIcobC.d.ts} +200 -26
- package/dist/index.cjs +12 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +33 -33
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-EHuJC5QQ.d.cts → validate-cJEdGlj1.d.cts} +54 -67
- package/dist/{validate-EHuJC5QQ.d.ts → validate-cJEdGlj1.d.ts} +54 -67
- package/dist/web-api.cjs +33 -33
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +6 -7
- package/dist/chunk-3UX5MZ2P.cjs.map +0 -1
- package/dist/chunk-4MFF6V3R.js.map +0 -1
- package/dist/chunk-ACFPMIXO.js.map +0 -1
- package/dist/chunk-CVCDAHDW.cjs.map +0 -1
- package/dist/chunk-FKSYSPJR.js.map +0 -1
- package/dist/chunk-N2EW2FDZ.cjs.map +0 -1
|
@@ -4,10 +4,27 @@ import nodeCrypto, { webcrypto } from 'node:crypto';
|
|
|
4
4
|
* Standardized error object for the `Result` type.
|
|
5
5
|
* Always has a brief `message` and a more detailed `description`.
|
|
6
6
|
*/
|
|
7
|
-
interface
|
|
7
|
+
interface ErrorStruct {
|
|
8
8
|
readonly message: string;
|
|
9
9
|
readonly description: string;
|
|
10
10
|
}
|
|
11
|
+
type ReservedWords<Obj extends object> = "success" extends keyof Obj ? never : "error" extends keyof Obj ? never : Obj;
|
|
12
|
+
type OkType<T> = {
|
|
13
|
+
readonly success: true;
|
|
14
|
+
readonly error?: undefined;
|
|
15
|
+
} & (T extends object ? ReservedWords<{
|
|
16
|
+
readonly [K in keyof T]: T[K];
|
|
17
|
+
}> : {
|
|
18
|
+
readonly result: T;
|
|
19
|
+
});
|
|
20
|
+
type ErrType<T> = {
|
|
21
|
+
readonly success: false;
|
|
22
|
+
readonly error: ErrorStruct;
|
|
23
|
+
} & (T extends object ? ReservedWords<{
|
|
24
|
+
readonly [K in keyof T]?: undefined;
|
|
25
|
+
}> : {
|
|
26
|
+
readonly result?: undefined;
|
|
27
|
+
});
|
|
11
28
|
/**
|
|
12
29
|
* Discriminated union for functions that can succeed or fail.
|
|
13
30
|
*
|
|
@@ -34,25 +51,7 @@ interface ResultErr {
|
|
|
34
51
|
* if (r2.success) console.log(r2.name, r2.age); // 'Alice' 30
|
|
35
52
|
* ```
|
|
36
53
|
*/
|
|
37
|
-
type Result<T
|
|
38
|
-
readonly [K in keyof T]: T[K];
|
|
39
|
-
} & {
|
|
40
|
-
readonly success: true;
|
|
41
|
-
readonly error?: undefined;
|
|
42
|
-
}) | ({
|
|
43
|
-
readonly [K in keyof T]?: undefined;
|
|
44
|
-
} & {
|
|
45
|
-
readonly success: false;
|
|
46
|
-
readonly error: E;
|
|
47
|
-
}) : {
|
|
48
|
-
readonly success: true;
|
|
49
|
-
readonly result: T;
|
|
50
|
-
readonly error?: undefined;
|
|
51
|
-
} | {
|
|
52
|
-
readonly success: false;
|
|
53
|
-
readonly error: E;
|
|
54
|
-
readonly result?: undefined;
|
|
55
|
-
};
|
|
54
|
+
type Result<T> = OkType<T> | ErrType<T>;
|
|
56
55
|
|
|
57
56
|
declare const ENCODING: readonly ["base64", "base64url", "hex", "utf8", "latin1"];
|
|
58
57
|
declare const CIPHER_ENCODING: readonly ["base64", "base64url", "hex"];
|
|
@@ -131,10 +130,10 @@ type DigestAlgorithm = keyof typeof DIGEST_ALGORITHMS;
|
|
|
131
130
|
* You can choose how strong the lock is (algorithm), how to make the key (digest),
|
|
132
131
|
* and add some extra secret stuff (salt and info) to make your key unique.
|
|
133
132
|
*
|
|
134
|
-
* - `algorithm
|
|
135
|
-
* - `digest
|
|
136
|
-
* - `salt
|
|
137
|
-
* - `info
|
|
133
|
+
* - `algorithm`: Encryption algorithm to use (default: `'aes256gcm'`)
|
|
134
|
+
* - `digest`: Digest algorithm for HKDF (default: `'sha256'`)
|
|
135
|
+
* - `salt`: Salt for HKDF (default: `'cipher-kit-salt'`, must be ≥ 8 chars)
|
|
136
|
+
* - `info`: Added context for HKDF (default: `'cipher-kit'`)
|
|
138
137
|
*/
|
|
139
138
|
interface CreateSecretKeyOptions {
|
|
140
139
|
/** Encryption algorithm to use (default: `'aes256gcm'`). */
|
|
@@ -152,11 +151,11 @@ interface CreateSecretKeyOptions {
|
|
|
152
151
|
* ### 🍼 Explain Like I'm Five
|
|
153
152
|
* After locking your message, how should we write the locked message down?
|
|
154
153
|
*
|
|
155
|
-
* - `
|
|
154
|
+
* - `outputEncoding`: Output ciphertext encoding (`'base64' | 'base64url' | 'hex'`) (default: `'base64url'`)
|
|
156
155
|
*/
|
|
157
156
|
interface EncryptOptions {
|
|
158
157
|
/** Encoding format for the output ciphertext (default: `'base64url'`). */
|
|
159
|
-
|
|
158
|
+
outputEncoding?: CipherEncoding;
|
|
160
159
|
}
|
|
161
160
|
/**
|
|
162
161
|
* Options for decryption.
|
|
@@ -164,11 +163,11 @@ interface EncryptOptions {
|
|
|
164
163
|
* ### 🍼 Explain Like I'm Five
|
|
165
164
|
* To unlock the message, we must know how it was written down.
|
|
166
165
|
*
|
|
167
|
-
* - `
|
|
166
|
+
* - `inputEncoding`: Input ciphertext encoding (`'base64' | 'base64url' | 'hex'`) (default: `'base64url'`)
|
|
168
167
|
*/
|
|
169
168
|
interface DecryptOptions {
|
|
170
169
|
/** Encoding format for the input ciphertext (default: `'base64url'`). */
|
|
171
|
-
|
|
170
|
+
inputEncoding?: CipherEncoding;
|
|
172
171
|
}
|
|
173
172
|
/**
|
|
174
173
|
* Options for hashing arbitrary data.
|
|
@@ -178,13 +177,13 @@ interface DecryptOptions {
|
|
|
178
177
|
* You can choose how to create the fingerprint (digest) and how to write it down (encoding).
|
|
179
178
|
*
|
|
180
179
|
* - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha256'`)
|
|
181
|
-
* - `
|
|
180
|
+
* - `outputEncoding`: Output ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
|
|
182
181
|
*/
|
|
183
182
|
interface HashOptions {
|
|
184
183
|
/** Digest algorithm to use (default: `'sha256'`). */
|
|
185
184
|
digest?: DigestAlgorithm;
|
|
186
185
|
/** Encoding format for the output hash (default: `'base64url'`). */
|
|
187
|
-
|
|
186
|
+
outputEncoding?: CipherEncoding;
|
|
188
187
|
}
|
|
189
188
|
/**
|
|
190
189
|
* Options for password hashing (PBKDF2).
|
|
@@ -194,16 +193,16 @@ interface HashOptions {
|
|
|
194
193
|
* stirring many times (iterations), and making a long result (keyLength).
|
|
195
194
|
*
|
|
196
195
|
* - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha512'`)
|
|
197
|
-
* - `
|
|
198
|
-
* - `saltLength`:
|
|
199
|
-
* - `iterations`:
|
|
200
|
-
* - `keyLength`:
|
|
196
|
+
* - `outputEncoding`: Output ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
|
|
197
|
+
* - `saltLength`: Size of the random salt in bytes (default: `16` bytes)
|
|
198
|
+
* - `iterations`: Number of iterations (default: `320000`)
|
|
199
|
+
* - `keyLength`: Length of the derived key in bytes (default: `64` bytes)
|
|
201
200
|
*/
|
|
202
201
|
interface HashPasswordOptions {
|
|
203
202
|
/** Digest algorithm to use (default: `'sha512'`). */
|
|
204
203
|
digest?: DigestAlgorithm;
|
|
205
204
|
/** Encoding format for the output hash (default: `'base64url'`). */
|
|
206
|
-
|
|
205
|
+
outputEncoding?: CipherEncoding;
|
|
207
206
|
/** Length of the salt in bytes (default: `16` bytes, min: `8` bytes). */
|
|
208
207
|
saltLength?: number;
|
|
209
208
|
/** Number of iterations for key derivation (default: `320000`, min: `1000`). */
|
|
@@ -219,15 +218,15 @@ interface HashPasswordOptions {
|
|
|
219
218
|
* same size—so the new result matches the old one.
|
|
220
219
|
*
|
|
221
220
|
* - `digest`: `'sha256' | 'sha384' | 'sha512'` (default: `'sha512'`)
|
|
222
|
-
* - `
|
|
223
|
-
* - `iterations`:
|
|
224
|
-
* - `keyLength`:
|
|
221
|
+
* - `inputEncoding`: Input ciphertext encoding for the hash (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`)
|
|
222
|
+
* - `iterations`: Number of iterations (default: `320000`)
|
|
223
|
+
* - `keyLength`: Length of the derived key in bytes (default: `64` bytes)
|
|
225
224
|
*/
|
|
226
225
|
interface VerifyPasswordOptions {
|
|
227
226
|
/** Digest algorithm to use (default: `'sha512'`). */
|
|
228
227
|
digest?: DigestAlgorithm;
|
|
229
228
|
/** Encoding format of the input hash (default: `'base64url'`). */
|
|
230
|
-
|
|
229
|
+
inputEncoding?: CipherEncoding;
|
|
231
230
|
/** Number of iterations for key derivation (default: `320000`). */
|
|
232
231
|
iterations?: number;
|
|
233
232
|
/** Length of the derived key in bytes (default: `64` bytes). */
|
|
@@ -250,12 +249,10 @@ interface VerifyPasswordOptions {
|
|
|
250
249
|
*
|
|
251
250
|
* @example
|
|
252
251
|
* ```ts
|
|
253
|
-
* const
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* console.error(res.error.message, res.error.description);
|
|
258
|
-
* }
|
|
252
|
+
* const { result, error, success } = tryStringifyObj({ a: 1 });
|
|
253
|
+
*
|
|
254
|
+
* if (success) console.log(result); // "{\"a\":1}"
|
|
255
|
+
* else console.error(error); // { message: "...", description: "..." }
|
|
259
256
|
* ```
|
|
260
257
|
*/
|
|
261
258
|
declare function tryStringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string>;
|
|
@@ -276,11 +273,7 @@ declare function tryStringifyObj<T extends object = Record<string, unknown>>(obj
|
|
|
276
273
|
*
|
|
277
274
|
* @example
|
|
278
275
|
* ```ts
|
|
279
|
-
*
|
|
280
|
-
* const json = stringifyObj({ a: 1 }); // {"a":1}
|
|
281
|
-
* } catch (error: unknown) {
|
|
282
|
-
* console.error(error);
|
|
283
|
-
* }
|
|
276
|
+
* const json = stringifyObj({ a: 1 }); // "{\"a\":1}"
|
|
284
277
|
* ```
|
|
285
278
|
*/
|
|
286
279
|
declare function stringifyObj<T extends object = Record<string, unknown>>(obj: T): string;
|
|
@@ -298,12 +291,10 @@ declare function stringifyObj<T extends object = Record<string, unknown>>(obj: T
|
|
|
298
291
|
*
|
|
299
292
|
* @example
|
|
300
293
|
* ```ts
|
|
301
|
-
* const
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
* console.error(res.error.message, res.error.description);
|
|
306
|
-
* }
|
|
294
|
+
* const {result, error, success} = tryParseToObj<{ a: number }>('{"a":1}');
|
|
295
|
+
*
|
|
296
|
+
* if (success) console.log(result); // { a: 1 }
|
|
297
|
+
* else console.error(error) // { message: "...", description: "..." }
|
|
307
298
|
* ```
|
|
308
299
|
*/
|
|
309
300
|
declare function tryParseToObj<T extends object = Record<string, unknown>>(str: string): Result<{
|
|
@@ -324,11 +315,7 @@ declare function tryParseToObj<T extends object = Record<string, unknown>>(str:
|
|
|
324
315
|
*
|
|
325
316
|
* @example
|
|
326
317
|
* ```ts
|
|
327
|
-
*
|
|
328
|
-
* const obj = parseToObj<{ a: number }>('{"a":1}'); // obj.a === 1
|
|
329
|
-
* } catch (error: unknown) {
|
|
330
|
-
* console.error(error);
|
|
331
|
-
* }
|
|
318
|
+
* const obj = parseToObj<{ a: number }>('{"a":1}'); // obj.a === 1
|
|
332
319
|
* ```
|
|
333
320
|
*/
|
|
334
321
|
declare function parseToObj<T extends object = Record<string, unknown>>(str: string): T;
|
|
@@ -373,12 +360,12 @@ declare const ENCRYPTED_REGEX: Readonly<{
|
|
|
373
360
|
*
|
|
374
361
|
* @example
|
|
375
362
|
* ```ts
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
363
|
+
* matchEncryptedPattern("abc.def.ghi.", "node"); // true
|
|
364
|
+
* matchEncryptedPattern("abc.def.", "web"); // true
|
|
365
|
+
* matchEncryptedPattern("abc.def.", "node"); // false
|
|
366
|
+
* matchEncryptedPattern("abc.def.ghi.", "general"); // true
|
|
380
367
|
* ```
|
|
381
368
|
*/
|
|
382
|
-
declare function
|
|
369
|
+
declare function matchEncryptedPattern(data: string, format: "node" | "web" | "general"): boolean;
|
|
383
370
|
|
|
384
|
-
export { type CipherEncoding as C, type DigestAlgorithm as D, ENCRYPTED_REGEX as E, type HashOptions as H, type Result as R, type SecretKey as S, type VerifyPasswordOptions as V, tryStringifyObj as a, type Encoding as b, type EncryptionAlgorithm as c, type CreateSecretKeyOptions as d, type EncryptOptions as e, type DecryptOptions as f, type HashPasswordOptions as g,
|
|
371
|
+
export { type CipherEncoding as C, type DigestAlgorithm as D, ENCRYPTED_REGEX as E, type HashOptions as H, type Result as R, type SecretKey as S, type VerifyPasswordOptions as V, tryStringifyObj as a, type Encoding as b, type EncryptionAlgorithm as c, type CreateSecretKeyOptions as d, type EncryptOptions as e, type DecryptOptions as f, type HashPasswordOptions as g, matchEncryptedPattern as m, parseToObj as p, stringifyObj as s, tryParseToObj as t };
|
package/dist/web-api.cjs
CHANGED
|
@@ -1,129 +1,129 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkLTVOIZP5_cjs = require('./chunk-LTVOIZP5.cjs');
|
|
4
|
+
var chunkX4CS7UXE_cjs = require('./chunk-X4CS7UXE.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "convertBytesToStr", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkLTVOIZP5_cjs.convertBytesToStr; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "convertEncoding", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkLTVOIZP5_cjs.convertEncoding; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "convertStrToBytes", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkLTVOIZP5_cjs.convertStrToBytes; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "createSecretKey", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkLTVOIZP5_cjs.createSecretKey; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "decrypt", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkLTVOIZP5_cjs.decrypt; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "decryptObj", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunkLTVOIZP5_cjs.decryptObj; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "encrypt", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunkLTVOIZP5_cjs.encrypt; }
|
|
35
35
|
});
|
|
36
36
|
Object.defineProperty(exports, "encryptObj", {
|
|
37
37
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
38
|
+
get: function () { return chunkLTVOIZP5_cjs.encryptObj; }
|
|
39
39
|
});
|
|
40
40
|
Object.defineProperty(exports, "generateUuid", {
|
|
41
41
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunkLTVOIZP5_cjs.generateUuid; }
|
|
43
43
|
});
|
|
44
44
|
Object.defineProperty(exports, "hash", {
|
|
45
45
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunkLTVOIZP5_cjs.hash; }
|
|
47
47
|
});
|
|
48
48
|
Object.defineProperty(exports, "hashPassword", {
|
|
49
49
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
50
|
+
get: function () { return chunkLTVOIZP5_cjs.hashPassword; }
|
|
51
51
|
});
|
|
52
52
|
Object.defineProperty(exports, "isWebSecretKey", {
|
|
53
53
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
54
|
+
get: function () { return chunkLTVOIZP5_cjs.isWebSecretKey; }
|
|
55
55
|
});
|
|
56
56
|
Object.defineProperty(exports, "tryConvertBytesToStr", {
|
|
57
57
|
enumerable: true,
|
|
58
|
-
get: function () { return
|
|
58
|
+
get: function () { return chunkLTVOIZP5_cjs.tryConvertBytesToStr; }
|
|
59
59
|
});
|
|
60
60
|
Object.defineProperty(exports, "tryConvertEncoding", {
|
|
61
61
|
enumerable: true,
|
|
62
|
-
get: function () { return
|
|
62
|
+
get: function () { return chunkLTVOIZP5_cjs.tryConvertEncoding; }
|
|
63
63
|
});
|
|
64
64
|
Object.defineProperty(exports, "tryConvertStrToBytes", {
|
|
65
65
|
enumerable: true,
|
|
66
|
-
get: function () { return
|
|
66
|
+
get: function () { return chunkLTVOIZP5_cjs.tryConvertStrToBytes; }
|
|
67
67
|
});
|
|
68
68
|
Object.defineProperty(exports, "tryCreateSecretKey", {
|
|
69
69
|
enumerable: true,
|
|
70
|
-
get: function () { return
|
|
70
|
+
get: function () { return chunkLTVOIZP5_cjs.tryCreateSecretKey; }
|
|
71
71
|
});
|
|
72
72
|
Object.defineProperty(exports, "tryDecrypt", {
|
|
73
73
|
enumerable: true,
|
|
74
|
-
get: function () { return
|
|
74
|
+
get: function () { return chunkLTVOIZP5_cjs.tryDecrypt; }
|
|
75
75
|
});
|
|
76
76
|
Object.defineProperty(exports, "tryDecryptObj", {
|
|
77
77
|
enumerable: true,
|
|
78
|
-
get: function () { return
|
|
78
|
+
get: function () { return chunkLTVOIZP5_cjs.tryDecryptObj; }
|
|
79
79
|
});
|
|
80
80
|
Object.defineProperty(exports, "tryEncrypt", {
|
|
81
81
|
enumerable: true,
|
|
82
|
-
get: function () { return
|
|
82
|
+
get: function () { return chunkLTVOIZP5_cjs.tryEncrypt; }
|
|
83
83
|
});
|
|
84
84
|
Object.defineProperty(exports, "tryEncryptObj", {
|
|
85
85
|
enumerable: true,
|
|
86
|
-
get: function () { return
|
|
86
|
+
get: function () { return chunkLTVOIZP5_cjs.tryEncryptObj; }
|
|
87
87
|
});
|
|
88
88
|
Object.defineProperty(exports, "tryGenerateUuid", {
|
|
89
89
|
enumerable: true,
|
|
90
|
-
get: function () { return
|
|
90
|
+
get: function () { return chunkLTVOIZP5_cjs.tryGenerateUuid; }
|
|
91
91
|
});
|
|
92
92
|
Object.defineProperty(exports, "tryHash", {
|
|
93
93
|
enumerable: true,
|
|
94
|
-
get: function () { return
|
|
94
|
+
get: function () { return chunkLTVOIZP5_cjs.tryHash; }
|
|
95
95
|
});
|
|
96
96
|
Object.defineProperty(exports, "tryHashPassword", {
|
|
97
97
|
enumerable: true,
|
|
98
|
-
get: function () { return
|
|
98
|
+
get: function () { return chunkLTVOIZP5_cjs.tryHashPassword; }
|
|
99
99
|
});
|
|
100
100
|
Object.defineProperty(exports, "verifyPassword", {
|
|
101
101
|
enumerable: true,
|
|
102
|
-
get: function () { return
|
|
102
|
+
get: function () { return chunkLTVOIZP5_cjs.verifyPassword; }
|
|
103
103
|
});
|
|
104
104
|
Object.defineProperty(exports, "ENCRYPTED_REGEX", {
|
|
105
105
|
enumerable: true,
|
|
106
|
-
get: function () { return
|
|
106
|
+
get: function () { return chunkX4CS7UXE_cjs.ENCRYPTED_REGEX; }
|
|
107
107
|
});
|
|
108
|
-
Object.defineProperty(exports, "
|
|
108
|
+
Object.defineProperty(exports, "matchEncryptedPattern", {
|
|
109
109
|
enumerable: true,
|
|
110
|
-
get: function () { return
|
|
110
|
+
get: function () { return chunkX4CS7UXE_cjs.matchEncryptedPattern; }
|
|
111
111
|
});
|
|
112
112
|
Object.defineProperty(exports, "parseToObj", {
|
|
113
113
|
enumerable: true,
|
|
114
|
-
get: function () { return
|
|
114
|
+
get: function () { return chunkX4CS7UXE_cjs.parseToObj; }
|
|
115
115
|
});
|
|
116
116
|
Object.defineProperty(exports, "stringifyObj", {
|
|
117
117
|
enumerable: true,
|
|
118
|
-
get: function () { return
|
|
118
|
+
get: function () { return chunkX4CS7UXE_cjs.stringifyObj; }
|
|
119
119
|
});
|
|
120
120
|
Object.defineProperty(exports, "tryParseToObj", {
|
|
121
121
|
enumerable: true,
|
|
122
|
-
get: function () { return
|
|
122
|
+
get: function () { return chunkX4CS7UXE_cjs.tryParseToObj; }
|
|
123
123
|
});
|
|
124
124
|
Object.defineProperty(exports, "tryStringifyObj", {
|
|
125
125
|
enumerable: true,
|
|
126
|
-
get: function () { return
|
|
126
|
+
get: function () { return chunkX4CS7UXE_cjs.tryStringifyObj; }
|
|
127
127
|
});
|
|
128
128
|
//# sourceMappingURL=web-api.cjs.map
|
|
129
129
|
//# sourceMappingURL=web-api.cjs.map
|
package/dist/web-api.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { C as CipherEncoding, d as CreateSecretKeyOptions, f as DecryptOptions, D as DigestAlgorithm, E as ENCRYPTED_REGEX, b as Encoding, e as EncryptOptions, c as EncryptionAlgorithm, H as HashOptions, g as HashPasswordOptions, S as SecretKey, V as VerifyPasswordOptions, m as
|
|
2
|
-
export { u as convertBytesToStr, y as convertEncoding, r as convertStrToBytes, c as createSecretKey, f as decrypt, l as decryptObj, e as encrypt, j as encryptObj, g as generateUuid, n as hash, p as hashPassword, i as isWebSecretKey, s as tryConvertBytesToStr, x as tryConvertEncoding, q as tryConvertStrToBytes, a as tryCreateSecretKey, d as tryDecrypt, k as tryDecryptObj, b as tryEncrypt, h as tryEncryptObj, t as tryGenerateUuid, m as tryHash, o as tryHashPassword, v as verifyPassword } from './export-
|
|
1
|
+
export { C as CipherEncoding, d as CreateSecretKeyOptions, f as DecryptOptions, D as DigestAlgorithm, E as ENCRYPTED_REGEX, b as Encoding, e as EncryptOptions, c as EncryptionAlgorithm, H as HashOptions, g as HashPasswordOptions, S as SecretKey, V as VerifyPasswordOptions, m as matchEncryptedPattern, p as parseToObj, s as stringifyObj, t as tryParseToObj, a as tryStringifyObj } from './validate-cJEdGlj1.cjs';
|
|
2
|
+
export { u as convertBytesToStr, y as convertEncoding, r as convertStrToBytes, c as createSecretKey, f as decrypt, l as decryptObj, e as encrypt, j as encryptObj, g as generateUuid, n as hash, p as hashPassword, i as isWebSecretKey, s as tryConvertBytesToStr, x as tryConvertEncoding, q as tryConvertStrToBytes, a as tryCreateSecretKey, d as tryDecrypt, k as tryDecryptObj, b as tryEncrypt, h as tryEncryptObj, t as tryGenerateUuid, m as tryHash, o as tryHashPassword, v as verifyPassword } from './export-CpZ7s25O.cjs';
|
|
3
3
|
import 'node:crypto';
|
package/dist/web-api.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { C as CipherEncoding, d as CreateSecretKeyOptions, f as DecryptOptions, D as DigestAlgorithm, E as ENCRYPTED_REGEX, b as Encoding, e as EncryptOptions, c as EncryptionAlgorithm, H as HashOptions, g as HashPasswordOptions, S as SecretKey, V as VerifyPasswordOptions, m as
|
|
2
|
-
export { u as convertBytesToStr, y as convertEncoding, r as convertStrToBytes, c as createSecretKey, f as decrypt, l as decryptObj, e as encrypt, j as encryptObj, g as generateUuid, n as hash, p as hashPassword, i as isWebSecretKey, s as tryConvertBytesToStr, x as tryConvertEncoding, q as tryConvertStrToBytes, a as tryCreateSecretKey, d as tryDecrypt, k as tryDecryptObj, b as tryEncrypt, h as tryEncryptObj, t as tryGenerateUuid, m as tryHash, o as tryHashPassword, v as verifyPassword } from './export-
|
|
1
|
+
export { C as CipherEncoding, d as CreateSecretKeyOptions, f as DecryptOptions, D as DigestAlgorithm, E as ENCRYPTED_REGEX, b as Encoding, e as EncryptOptions, c as EncryptionAlgorithm, H as HashOptions, g as HashPasswordOptions, S as SecretKey, V as VerifyPasswordOptions, m as matchEncryptedPattern, p as parseToObj, s as stringifyObj, t as tryParseToObj, a as tryStringifyObj } from './validate-cJEdGlj1.js';
|
|
2
|
+
export { u as convertBytesToStr, y as convertEncoding, r as convertStrToBytes, c as createSecretKey, f as decrypt, l as decryptObj, e as encrypt, j as encryptObj, g as generateUuid, n as hash, p as hashPassword, i as isWebSecretKey, s as tryConvertBytesToStr, x as tryConvertEncoding, q as tryConvertStrToBytes, a as tryCreateSecretKey, d as tryDecrypt, k as tryDecryptObj, b as tryEncrypt, h as tryEncryptObj, t as tryGenerateUuid, m as tryHash, o as tryHashPassword, v as verifyPassword } from './export-C4DbO5zM.js';
|
|
3
3
|
import 'node:crypto';
|
package/dist/web-api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, isWebSecretKey, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword } from './chunk-
|
|
2
|
-
export { ENCRYPTED_REGEX,
|
|
1
|
+
export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, isWebSecretKey, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword } from './chunk-FSEA3UXJ.js';
|
|
2
|
+
export { ENCRYPTED_REGEX, matchEncryptedPattern, parseToObj, stringifyObj, tryParseToObj, tryStringifyObj } from './chunk-6C4NIWQ4.js';
|
|
3
3
|
//# sourceMappingURL=web-api.js.map
|
|
4
4
|
//# sourceMappingURL=web-api.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cipher-kit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "🔐 Secure, Modern, and Cross-Platform Cryptography Helpers for Web, Node.js, Deno, Bun, and Cloudflare Workers",
|
|
5
5
|
"homepage": "https://github.com/WolfieLeader/npm/tree/main/packages/cipher-kit#readme",
|
|
6
6
|
"repository": {
|
|
@@ -68,9 +68,9 @@
|
|
|
68
68
|
"test": "vitest"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
|
-
"@types/node": "^24.
|
|
71
|
+
"@types/node": "^24.6.2",
|
|
72
72
|
"tsup": "^8.5.0",
|
|
73
|
-
"typescript": "^5.9.
|
|
73
|
+
"typescript": "^5.9.3",
|
|
74
74
|
"vitest": "^3.2.4"
|
|
75
75
|
},
|
|
76
76
|
"author": "WolfieLeader",
|
|
@@ -81,13 +81,12 @@
|
|
|
81
81
|
"cipher",
|
|
82
82
|
"security",
|
|
83
83
|
"hash",
|
|
84
|
-
"
|
|
84
|
+
"password",
|
|
85
|
+
"uuid",
|
|
85
86
|
"web",
|
|
86
87
|
"node",
|
|
87
88
|
"deno",
|
|
88
89
|
"bun",
|
|
89
|
-
"cloudflare"
|
|
90
|
-
"workers",
|
|
91
|
-
"password"
|
|
90
|
+
"cloudflare"
|
|
92
91
|
]
|
|
93
92
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/helpers/consts.ts","../src/helpers/validate.ts","../src/helpers/error.ts","../src/helpers/object.ts"],"names":["nodeCrypto","title"],"mappings":";;;;;;;;;;;;;;;AAAO,IAAM,QAAA,GAAW,OAAO,MAAA,CAAO,CAAC,UAAU,WAAA,EAAa,KAAA,EAAO,MAAA,EAAQ,QAAQ,CAAU;AAExF,IAAM,kBAAkB,MAAA,CAAO,MAAA,CAAO,CAAC,QAAA,EAAU,WAAA,EAAa,KAAK,CAAU;AAE7E,IAAM,iBAAA,GAAoB,OAAO,MAAA,CAAO;AAAA,EAC7C,MAAA,EAAQ,EAAE,IAAA,EAAM,QAAA,EAAU,KAAK,SAAA,EAAU;AAAA,EACzC,MAAA,EAAQ,EAAE,IAAA,EAAM,QAAA,EAAU,KAAK,SAAA,EAAU;AAAA,EACzC,MAAA,EAAQ,EAAE,IAAA,EAAM,QAAA,EAAU,KAAK,SAAA;AACjC,CAAU;AAEH,IAAM,qBAAA,GAAwB,OAAO,MAAA,CAAO;AAAA,EACjD,SAAA,EAAW,EAAE,QAAA,EAAU,EAAA,EAAI,UAAU,EAAA,EAAI,IAAA,EAAM,aAAA,EAAe,GAAA,EAAK,SAAA,EAAU;AAAA,EAC7E,SAAA,EAAW,EAAE,QAAA,EAAU,EAAA,EAAI,UAAU,EAAA,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AA,EAAM,GAAI,cAAc,GAAG,CAAA;AAC3C,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAEO,SAAS,YAAwD,GAAA,EAAoC;AAC1G,EAAA,IAAI;AACF,IAAA,IAAI,CAAC,MAAA,CAAO,GAAG,CAAA,EAAG,OAAO,IAAA,CAAK,EAAE,GAAA,EAAK,wBAAA,EAA0B,IAAA,EAAM,6BAAA,EAA+B,CAAA;AACpG,IAAA,MAAM,GAAA,GAAM,IAAA,CAAK,KAAA,CAAM,GAAG,CAAA;AAE1B,IAAA,IAAI,CAAC,MAAA,CAAO,GAAG,CAAA,EAAG,OAAO,IAAA,CAAK,EAAE,GAAA,EAAK,gCAAA,EAAkC,IAAA,EAAM,mCAAA,EAAqC,CAAA;AAClH,IAAA,OAAO,GAAA,CAAI,EAAE,MAAA,EAAQ,GAAA,EAAU,CAAA;AAAA,EACjC,SAAS,KAAA,EAAO;AACd,IAAA,OAAO,IAAA,CAAK,EAAE,GAAA,EAAK,yBAAA,EAA2B,MAAM,SAAA,CAAU,KAAK,GAAG,CAAA;AAAA,EACxE;AACF;AAwBO,SAAS,cAA0D,GAAA,EAAoC;AAC5G,EAAA,OAAO,YAAe,GAAG,CAAA;AAC3B;AAwBO,SAAS,WAAuD,GAAA,EAAgB;AACrF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,YAAe,GAAG,CAAA;AAC5C,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-3UX5MZ2P.cjs","sourcesContent":["export const ENCODING = Object.freeze([\"base64\", \"base64url\", \"hex\", \"utf8\", \"latin1\"] as const);\r\n\r\nexport const CIPHER_ENCODING = Object.freeze([\"base64\", \"base64url\", \"hex\"] as const);\r\n\r\nexport const DIGEST_ALGORITHMS = Object.freeze({\r\n sha256: { node: \"sha256\", web: \"SHA-256\" },\r\n sha384: { node: \"sha384\", web: \"SHA-384\" },\r\n sha512: { node: \"sha512\", web: \"SHA-512\" },\r\n} as const);\r\n\r\nexport const ENCRYPTION_ALGORITHMS = Object.freeze({\r\n aes256gcm: { keyBytes: 32, ivLength: 12, node: \"aes-256-gcm\", web: \"AES-GCM\" },\r\n aes192gcm: { keyBytes: 24, ivLength: 12, node: \"aes-192-gcm\", web: \"AES-GCM\" },\r\n aes128gcm: { keyBytes: 16, ivLength: 12, node: \"aes-128-gcm\", web: \"AES-GCM\" },\r\n} as const);\r\n","import nodeCrypto from \"node:crypto\";\r\nimport type { SecretKey } from \"~/helpers/types\";\r\nimport { DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from \"./consts\";\r\n\r\nexport function $isStr(x: unknown, min = 1): x is string {\r\n return x !== null && x !== undefined && typeof x === \"string\" && x.trim().length >= min;\r\n}\r\n\r\nexport function $isObj(x: unknown): x is Record<string, unknown> {\r\n if (typeof x !== \"object\" || x === null || x === undefined) return false;\r\n const proto = Object.getPrototypeOf(x);\r\n return proto === Object.prototype || proto === null;\r\n}\r\n\r\nexport function $isLooseObj(x: unknown): x is Record<string, unknown> {\r\n return typeof x === \"object\" && x !== null && x !== undefined;\r\n}\r\n\r\ntype InjectedSecretKey<Platform extends \"web\" | \"node\"> = SecretKey<Platform> & {\r\n readonly injected: (typeof ENCRYPTION_ALGORITHMS)[keyof typeof ENCRYPTION_ALGORITHMS];\r\n};\r\n\r\nconst expectedKeys = new Set([\"platform\", \"digest\", \"algorithm\", \"key\"]);\r\n\r\nexport function $isSecretKey<Platform extends \"node\" | \"web\">(\r\n x: unknown,\r\n platform: Platform,\r\n): InjectedSecretKey<Platform> | null {\r\n if (!$isLooseObj(x) || (platform !== \"node\" && platform !== \"web\") || x.platform !== platform) return null;\r\n\r\n const keys = Object.keys(x);\r\n if (keys.length !== expectedKeys.size) return null;\r\n for (const key of keys) if (!expectedKeys.has(key)) return null;\r\n for (const key of expectedKeys) if (!Object.hasOwn(x, key)) return null;\r\n\r\n if (\r\n typeof x.digest !== \"string\" ||\r\n !(x.digest in DIGEST_ALGORITHMS) ||\r\n typeof x.algorithm !== \"string\" ||\r\n !(x.algorithm in ENCRYPTION_ALGORITHMS) ||\r\n !$isLooseObj(x.key) ||\r\n x.key.type !== \"secret\"\r\n ) {\r\n return null;\r\n }\r\n\r\n const algorithm = ENCRYPTION_ALGORITHMS[x.algorithm as keyof typeof ENCRYPTION_ALGORITHMS];\r\n\r\n if (platform === \"node\") {\r\n if (\r\n !(x.key instanceof nodeCrypto.KeyObject) ||\r\n (typeof x.key.symmetricKeySize === \"number\" && x.key.symmetricKeySize !== algorithm.keyBytes)\r\n ) {\r\n return null;\r\n }\r\n return Object.freeze({ ...x, injected: algorithm }) as InjectedSecretKey<Platform>;\r\n }\r\n\r\n if (\r\n !$isLooseObj(x.key.algorithm) ||\r\n x.key.algorithm.name !== algorithm.web ||\r\n (typeof x.key.algorithm.length === \"number\" && x.key.algorithm.length !== algorithm.keyBytes * 8) ||\r\n typeof x.key.extractable !== \"boolean\" ||\r\n !Array.isArray(x.key.usages) ||\r\n x.key.usages.length !== 2 ||\r\n !(x.key.usages.includes(\"encrypt\") && x.key.usages.includes(\"decrypt\"))\r\n ) {\r\n return null;\r\n }\r\n return Object.freeze({ ...x, injected: algorithm }) as InjectedSecretKey<Platform>;\r\n}\r\n\r\n/**\r\n * Regular expressions for encrypted data patterns.\r\n *\r\n * - **node**: `\"iv.cipher.tag.\"` (three dot-separated parts plus a trailing dot)\r\n * - **web**: `\"iv.cipherWithTag.\"` (two parts plus a trailing dot)\r\n * - **general**: accepts both shapes (2 or 3 parts) with a trailing dot\r\n *\r\n * Each part is any non-empty string without dots.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You have a secret code you want to check. Before you check it,\r\n * you make sure it looks how a secret code should look.\r\n */\r\nexport const ENCRYPTED_REGEX = Object.freeze({\r\n node: /^([^.]+)\\.([^.]+)\\.([^.]+)\\.$/,\r\n web: /^([^.]+)\\.([^.]+)\\.$/,\r\n general: /^([^.]+)\\.([^.]+)(?:\\.([^.]+))?\\.$/,\r\n});\r\n\r\n/**\r\n * Checks if a string matches an expected encrypted payload shape.\r\n *\r\n * - **node**: `\"iv.cipher.tag.\"` (three dot-separated parts plus a trailing dot)\r\n * - **web**: `\"iv.cipherWithTag.\"` (two parts plus a trailing dot)\r\n * - **general**: accepts both shapes (2 or 3 parts) with a trailing dot\r\n *\r\n * Each part is any non-empty string without dots.\r\n *\r\n * This validates only the **shape**, not whether content is valid base64/hex, etc.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You have a secret code you want to check. Before you check it,\r\n * you make sure it looks how a secret code should look.\r\n *\r\n * @param data - The string to test.\r\n * @param format - Which layout to check: `'node'`, `'web'`, or `'general'`.\r\n * @returns `true` if the string matches the pattern; otherwise `false`.\r\n * @throws {Error} If an unknown `format` is provided.\r\n *\r\n * @example\r\n * ```ts\r\n * matchPattern(\"abc.def.ghi.\", \"node\"); // true\r\n * matchPattern(\"abc.def.\", \"web\"); // true\r\n * matchPattern(\"abc.def.\", \"node\"); // false\r\n * matchPattern(\"abc.def.ghi.\", \"general\"); // true\r\n * ```\r\n */\r\nexport function matchPattern(data: string, format: \"general\" | \"node\" | \"web\"): boolean {\r\n if (typeof data !== \"string\") return false;\r\n if (!(format in ENCRYPTED_REGEX)) throw new Error(`Unknown format: ${format}`);\r\n return ENCRYPTED_REGEX[format].test(data);\r\n}\r\n","import { $isObj } from \"./validate\";\r\n\r\n/**\r\n * Standardized error object for the `Result` type.\r\n * Always has a brief `message` and a more detailed `description`.\r\n */\r\nexport interface ResultErr {\r\n readonly message: string;\r\n readonly description: string;\r\n}\r\n\r\n/**\r\n * Discriminated union for functions that can succeed or fail.\r\n *\r\n * - On **success**:\r\n * - If `T` is an object - properties of `T` are **spread** into the result\r\n * along with `success: true`.\r\n * - Otherwise - `{ success: true, result: T }`.\r\n * - On **failure**: `{ success: false, error: E }`.\r\n *\r\n * @example\r\n * ```ts\r\n * // Primitive result\r\n * function getNum(): Result<number> {\r\n * return $ok(42);\r\n * }\r\n * const r1 = getNum();\r\n * if (r1.success) console.log(r1.result); // 42\r\n *\r\n * // Object result (spread)\r\n * function getObject(): Result<{ name: string; age: number }> {\r\n * return $ok({ name: 'Alice', age: 30 });\r\n * }\r\n * const r2 = getObject();\r\n * if (r2.success) console.log(r2.name, r2.age); // 'Alice' 30\r\n * ```\r\n */\r\nexport type Result<T, E = ResultErr> = T extends object\r\n ?\r\n | ({ readonly [K in keyof T]: T[K] } & { readonly success: true; readonly error?: undefined })\r\n | ({ readonly [K in keyof T]?: undefined } & { readonly success: false; readonly error: E })\r\n :\r\n | { readonly success: true; readonly result: T; readonly error?: undefined }\r\n | { readonly success: false; readonly error: E; readonly result?: undefined };\r\n\r\nexport function $ok<T>(result?: T): Result<T> {\r\n if ($isObj(result)) return { success: true, ...(result as T & object) } as Result<T>;\r\n return { success: true, result } as Result<T>;\r\n}\r\n\r\nexport function $err(err: { msg: string; desc: string }): Result<never, ResultErr>;\r\nexport function $err(err: ResultErr): Result<never, ResultErr>;\r\nexport function $err(err: { msg: string; desc: string } | ResultErr): Result<never, ResultErr> {\r\n return {\r\n success: false,\r\n error: {\r\n message: \"msg\" in err ? err.msg : err.message,\r\n description: \"desc\" in err ? err.desc : err.description,\r\n },\r\n } as Result<never, ResultErr>;\r\n}\r\n\r\nexport function $fmtError(error: unknown): string {\r\n if (typeof error === \"string\") return error;\r\n if (error instanceof Error) return error.message;\r\n return String(error);\r\n}\r\n\r\nexport function $fmtResultErr(err: ResultErr | undefined): string {\r\n if (!err) return \"Unknown error\";\r\n return `${err.message} - ${err.description}`;\r\n}\r\n\r\nexport function title(platform: \"web\" | \"node\", title: string): string {\r\n return `${platform === \"web\" ? \"Crypto Web API\" : \"Crypto NodeJS API\"} - ${title}`;\r\n}\r\n","import { $err, $fmtError, $fmtResultErr, $ok, type Result } from \"./error\";\r\nimport { $isObj, $isStr } from \"./validate\";\r\n\r\nexport function $stringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n try {\r\n if (!$isObj(obj)) return $err({ msg: \"Invalid object\", desc: \"Input is not a plain object\" });\r\n return $ok(JSON.stringify(obj));\r\n } catch (error) {\r\n return $err({ msg: \"Utility: Stringify error\", desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Safely serializes a plain object to JSON without throwing.\r\n *\r\n * Wraps `JSON.stringify` and returns a `Result` containing the JSON string or an error.\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You have a box of toys (your object) and take a photo of it (a JSON string)\r\n * so you can send it to a friend.\r\n *\r\n * @template T - Plain object type to serialize.\r\n * @param obj - The object to stringify (must be a plain object).\r\n * @returns A `Result` with the JSON string on success, or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const res = tryStringifyObj({ a: 1 });\r\n * if (res.success) {\r\n * console.log(res.result); // {\"a\":1}\r\n * } else {\r\n * console.error(res.error.message, res.error.description);\r\n * }\r\n * ```\r\n */\r\nexport function tryStringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n return $stringifyObj(obj);\r\n}\r\n\r\n/**\r\n * Serializes a plain object to JSON (throwing).\r\n *\r\n * Wraps `JSON.stringify` and returns the result or throws an error.\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You have a box of toys (your object) and take a photo of it (a JSON string)\r\n * so you can send it to a friend.\r\n *\r\n * @template T - Plain object type to serialize.\r\n * @param obj - The object to stringify (must be a plain object).\r\n * @returns JSON string representation of the object.\r\n * @throws {Error} If `obj` is not a plain object or serialization fails.\r\n *\r\n * @example\r\n * ```ts\r\n * try {\r\n * const json = stringifyObj({ a: 1 }); // {\"a\":1}\r\n * } catch (error: unknown) {\r\n * console.error(error);\r\n * }\r\n * ```\r\n */\r\nexport function stringifyObj<T extends object = Record<string, unknown>>(obj: T): string {\r\n const { result, error } = $stringifyObj(obj);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\nexport function $parseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n try {\r\n if (!$isStr(str)) return $err({ msg: \"Utility: Invalid input\", desc: \"Input is not a valid string\" });\r\n const obj = JSON.parse(str);\r\n\r\n if (!$isObj(obj)) return $err({ msg: \"Utility: Invalid object format\", desc: \"Parsed data is not a plain object\" });\r\n return $ok({ result: obj as T });\r\n } catch (error) {\r\n return $err({ msg: \"Utility: Invalid format\", desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Safely parses a JSON string to a plain object (non-throwing).\r\n *\r\n * Wraps `JSON.parse` and returns a `Result` containing the parsed object, or an error.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) from a photo you took (a JSON string).\r\n *\r\n * @template T - The expected object type.\r\n * @param str - The JSON string to parse.\r\n * @returns A `Result` with the parsed object on success, or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const res = tryParseToObj<{ a: number }>('{\"a\":1}');\r\n * if (res.success) {\r\n * console.log(res.result.a); // 1\r\n * } else {\r\n * console.error(res.error.message, res.error.description);\r\n * }\r\n * ```\r\n */\r\nexport function tryParseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n return $parseToObj<T>(str);\r\n}\r\n\r\n/**\r\n * Parses a JSON string to a plain object (throwing).\r\n *\r\n * Wraps `JSON.parse` and returns the parsed object, or throws on failure.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) from a photo you took (a JSON string).\r\n *\r\n * @template T - The expected object type.\r\n * @param str - The JSON string to parse.\r\n * @returns The parsed plain object.\r\n * @throws {Error} If the string can’t be parsed or doesn’t represent a plain object.\r\n *\r\n * @example\r\n * ```ts\r\n * try {\r\n * const obj = parseToObj<{ a: number }>('{\"a\":1}'); // obj.a === 1\r\n * } catch (error: unknown) {\r\n * console.error(error);\r\n * }\r\n * ```\r\n */\r\nexport function parseToObj<T extends object = Record<string, unknown>>(str: string): T {\r\n const { result, error } = $parseToObj<T>(str);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n"]}
|