Skip to content

Commit 6397a07

Browse files
committed
fix: small fixes to error messages and more
1 parent e96da16 commit 6397a07

File tree

16 files changed

+71
-164
lines changed

16 files changed

+71
-164
lines changed

packages/cipher-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cipher-kit",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "🔐 Secure, Lightweight, and Cross-Platform Encryption & Decryption for Web, Node.js, Deno, and Bun",
55
"homepage": "https://github.com/WolfieLeader/npm/tree/main/packages/cipher-kit#readme",
66
"repository": {

packages/cipher-kit/src/node/encrypt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export function encrypt(data: string, secretKey: NodeKey): Result<string> {
6565
const { result: decodedTag, error: tagError } = decode(tag, 'base64url');
6666
if (ivError || encryptedError || tagError) {
6767
return $err({
68-
msg: 'Crypto NodeJS: Failed to encode IV or encrypted data',
69-
desc: `Decoding error: ${ivError || encryptedError || tagError}`,
68+
msg: 'Crypto NodeJS: Failed to convert IV or encrypted data or tag',
69+
desc: `Conversion error: ${ivError || encryptedError || tagError}`,
7070
});
7171
}
7272

@@ -101,8 +101,8 @@ export function decrypt(encrypted: string, secretKey: NodeKey): Result<string> {
101101
const { bytes: tagBytes, error: tagError } = encode(tag, 'base64url');
102102
if (ivError || encryptedError || tagError) {
103103
return $err({
104-
msg: 'Crypto NodeJS: Failed to decode IV or encrypted data',
105-
desc: `Encoding error: ${ivError || encryptedError || tagError}`,
104+
msg: 'Crypto NodeJS: Failed to convert IV or encrypted data or tag',
105+
desc: `Conversion error: ${ivError || encryptedError || tagError}`,
106106
});
107107
}
108108

packages/cipher-kit/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function isInWebApiEncryptionFormat(data: string): boolean {
2424
}
2525

2626
export function $isStr(value: unknown, min = 1): value is string {
27-
return (value !== null || value !== undefined) && typeof value === 'string' && value.trim().length >= min;
27+
return value !== null && value !== undefined && typeof value === 'string' && value.trim().length >= min;
2828
}
2929

3030
export function $isObj(value: unknown): value is Record<string, unknown> {

packages/cipher-kit/src/web/encrypt.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function encrypt(data: string, secretKey: WebApiKey): Promise<Resul
5959
}
6060

6161
if (!isWebApiKey(secretKey)) {
62-
return $err({ msg: 'Crypto Web API: Invalid encryption key', desc: 'Expected a NodeKey(crypto.KeyObject)' });
62+
return $err({ msg: 'Crypto Web API: Invalid encryption key', desc: 'Expected a WebApiKey(webcrypto.CryptoKey)' });
6363
}
6464

6565
const { bytes, error } = encode(data, 'utf8');
@@ -73,8 +73,8 @@ export async function encrypt(data: string, secretKey: WebApiKey): Promise<Resul
7373

7474
if (ivError || encryptedError) {
7575
return $err({
76-
msg: 'Crypto Web API: Failed to encode IV or encrypted data',
77-
desc: `Decoding error: ${ivError || encryptedError}`,
76+
msg: 'Crypto Web API: Failed to convert IV or encrypted data',
77+
desc: `Conversion error: ${ivError || encryptedError}`,
7878
});
7979
}
8080

@@ -88,7 +88,7 @@ export async function decrypt(encrypted: string, secretKey: WebApiKey): Promise<
8888
if (isInWebApiEncryptionFormat(encrypted) === false) {
8989
return $err({
9090
msg: 'Crypto Web API: Invalid encrypted data format',
91-
desc: 'Encrypted data must be in the format "iv.encryptedData.tag."',
91+
desc: 'Encrypted data must be in the format "iv.encryptedData."',
9292
});
9393
}
9494

@@ -108,8 +108,8 @@ export async function decrypt(encrypted: string, secretKey: WebApiKey): Promise<
108108
const { bytes: encryptedBytes, error: encryptedError } = encode(encryptedWithTag, 'base64url');
109109
if (ivError || encryptedError) {
110110
return $err({
111-
msg: 'Crypto Web API: Failed to decode IV or encrypted data',
112-
desc: `Encoding error: ${ivError || encryptedError}`,
111+
msg: 'Crypto Web API: Failed to convert IV or encrypted data',
112+
desc: `Conversion error: ${ivError || encryptedError}`,
113113
});
114114
}
115115

packages/compress-kit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "compress-kit",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "🔬 Reliable, Cross-Platform Compression & Decompression for Web, Node.js, Deno, and Bun",
55
"homepage": "https://github.com/WolfieLeader/npm/tree/main/packages/compress-kit#readme",
66
"repository": {
@@ -43,6 +43,7 @@
4343
"test": "vitest"
4444
},
4545
"dependencies": {
46+
"cipher-kit": "^0.0.10",
4647
"pako": "^2.1.0"
4748
},
4849
"devDependencies": {

packages/compress-kit/src/compress.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { decode, encode, parseToObj, stringifyObj } from 'cipher-kit/web-api';
12
import pako, { type DeflateFunctionOptions } from 'pako';
2-
import { $decode, $encode } from './encode';
33
import { $err, $ok, $stringifyError, type Result } from './error';
4-
import { $isStr, isInCompressionFormat, parseToObj, stringifyObj } from './utils';
4+
import { $isStr, isInCompressionFormat } from './utils';
55

66
const COMPRESSION_OPTIONS: DeflateFunctionOptions = {
77
level: 6,
@@ -12,19 +12,17 @@ const COMPRESSION_OPTIONS: DeflateFunctionOptions = {
1212
};
1313

1414
export function compress(data: string): Result<string> {
15-
if (!$isStr(data, 1)) {
16-
return $err({ msg: 'Empty string', desc: 'Cannot compress null or undefined string' });
17-
}
15+
try {
16+
if (!$isStr(data)) return $err({ msg: 'Empty string', desc: 'Cannot compress null or undefined string' });
1817

19-
const { bytes, error } = $encode(data, 'utf8');
20-
if (error) return $err(error);
18+
const encoded = encode(data, 'utf8');
19+
if (encoded.error) return $err({ msg: encoded.error.message, desc: encoded.error.description });
2120

22-
const decoded = $decode(bytes, 'base64url');
23-
if (decoded.error) return $err(decoded.error);
21+
const decoded = decode(encoded.bytes, 'base64url');
22+
if (decoded.error) return $err({ msg: decoded.error.message, desc: decoded.error.description });
2423

25-
try {
26-
const compressed = $decode(pako.deflate(bytes, COMPRESSION_OPTIONS), 'base64url');
27-
if (compressed.error) return $err(compressed.error);
24+
const compressed = decode(pako.deflate(encoded.bytes, COMPRESSION_OPTIONS), 'base64url');
25+
if (compressed.error) return $err({ msg: compressed.error.message, desc: compressed.error.description });
2826

2927
if (decoded.result.length <= compressed.result.length) return $ok(`${decoded.result}.0.`);
3028
return $ok(`${compressed.result}.1.`);
@@ -34,19 +32,26 @@ export function compress(data: string): Result<string> {
3432
}
3533

3634
export function decompress(data: string): Result<string> {
37-
if (isInCompressionFormat(data) === false) {
38-
return $err({ msg: 'Invalid format', desc: 'String does not match expected compressed format' });
39-
}
35+
try {
36+
if (isInCompressionFormat(data) === false) {
37+
return $err({ msg: 'Invalid format', desc: 'String does not match expected compressed format' });
38+
}
4039

41-
const str = data.slice(0, -3);
42-
if (!$isStr(str, 1)) return $err({ msg: 'Invalid input', desc: 'Input is not a valid string' });
40+
const str = data.slice(0, -3);
41+
if (!$isStr(str, 1)) return $err({ msg: 'Invalid input', desc: 'Input is not a valid string' });
4342

44-
const { bytes, error } = $encode(str, 'base64url');
45-
if (error) return $err(error);
43+
const encoded = encode(str, 'base64url');
44+
if (encoded.error) return $err({ msg: encoded.error.message, desc: encoded.error.description });
4645

47-
try {
48-
if (data.endsWith('.1.')) return $ok(pako.inflate(bytes, { to: 'string' }));
49-
if (data.endsWith('.0.')) return $decode(bytes, 'utf8');
46+
if (data.endsWith('.1.')) {
47+
return $ok(pako.inflate(encoded.bytes, { to: 'string' }));
48+
}
49+
50+
if (data.endsWith('.0.')) {
51+
const decoded = decode(encoded.bytes, 'utf8');
52+
if (decoded.error) return $err({ msg: decoded.error.message, desc: decoded.error.description });
53+
return $ok(decoded.result);
54+
}
5055

5156
return $err({ msg: 'Invalid compression type', desc: 'Expected .0. or .1. at the end of the string' });
5257
} catch (error) {

packages/compress-kit/src/encode.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

packages/compress-kit/src/tests/compress.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,10 @@ describe('Compression Utility', () => {
103103
expect(decompressed.success).toBe(true);
104104
expect(decompressed.result).toEqual(largeObj);
105105
});
106+
107+
test('Decompress invalid data', () => {
108+
const decompressed = decompressObj('invalid-compressed-data');
109+
expect(decompressed.success).toBe(false);
110+
expect(decompressed.error?.message).toMatch(/Invalid format/);
111+
});
106112
});

packages/compress-kit/src/utils.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { $err, $ok, $stringifyError, type Result } from './error';
2-
31
export const COMPRESSION_REGEX = Object.freeze({
42
GENERAL: /^[A-Za-z0-9\-_]+\.(0|1)\.$/,
53
UNCOMPRESSED: /^[A-Za-z0-9\-_]+\.0\.$/,
@@ -18,8 +16,8 @@ export function isInUncompressedFormat(data: string): boolean {
1816
return typeof data === 'string' && COMPRESSION_REGEX.UNCOMPRESSED.test(data);
1917
}
2018

21-
export function $isStr(value: unknown, min = 0): value is string {
22-
return (value !== null || value !== undefined) && typeof value === 'string' && value.trim().length >= min;
19+
export function $isStr(value: unknown, min = 1): value is string {
20+
return value !== null && value !== undefined && typeof value === 'string' && value.trim().length >= min;
2321
}
2422

2523
export function $isObj(value: unknown): value is Record<string, unknown> {
@@ -30,23 +28,3 @@ export function $isObj(value: unknown): value is Record<string, unknown> {
3028
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)
3129
);
3230
}
33-
34-
export function stringifyObj(obj: Record<string, unknown>): Result<string> {
35-
try {
36-
if (!$isObj(obj)) return $err({ msg: 'Invalid object', desc: 'Input is not a plain object' });
37-
return $ok(JSON.stringify(obj));
38-
} catch (error) {
39-
return $err({ msg: 'Stringify error', desc: $stringifyError(error) });
40-
}
41-
}
42-
43-
export function parseToObj(str: string): Result<{ result: Record<string, unknown> }> {
44-
try {
45-
if (!$isStr(str)) return $err({ msg: 'Invalid input', desc: 'Input is not a valid string' });
46-
const obj = JSON.parse(str);
47-
if (!$isObj(obj)) return $err({ msg: 'Invalid object format', desc: 'Parsed data is not a plain object' });
48-
return $ok({ result: obj });
49-
} catch (error) {
50-
return $err({ msg: 'Invalid format', desc: $stringifyError(error) });
51-
}
52-
}

packages/generate-certs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ It streamlines the process of creating `key.pem` and `cert.pem` files, supports
3333
## Installation 🔥
3434

3535
```bash
36-
npm install -D generate-certs
36+
npm install -D generate-certs@latest
3737
```
3838

3939
> 💡 Works with `npm`, `pnpm`, and `yarn`. You can use it in dev dependencies since it's typically used only for local HTTPS.

0 commit comments

Comments
 (0)