@chainfuse/helpers 0.4.0 → 0.5.0
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 +1 -1
- package/dist/buffers.d.mts +6 -6
- package/dist/buffers.mjs +8 -5
- package/dist/crypto.d.mts +2 -8
- package/dist/crypto.mjs +4 -10
- package/dist/index.d.mts +9 -0
- package/dist/index.mjs +43 -0
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://securityscorecards.dev/viewer/?uri=github.com/ChainFuse/packages)[](https://socket.dev/npm/package/@
|
|
1
|
+
[](https://securityscorecards.dev/viewer/?uri=github.com/ChainFuse/packages)[](https://socket.dev/npm/package/@chainfuse/helpers)
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
package/dist/buffers.d.mts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { UndefinedProperties } from '@chainfuse/types';
|
|
2
|
-
import type { PrefixedUuid, UuidExport } from '@chainfuse/types/d1';
|
|
1
|
+
import type { D1Blob, PrefixedUuid, UndefinedProperties, UuidExport } from '@chainfuse/types';
|
|
3
2
|
export declare class BufferHelpers {
|
|
4
3
|
/**
|
|
5
4
|
* @deprecated
|
|
6
5
|
*/
|
|
7
6
|
static bufferFromHex(...args: Parameters<typeof this.hexToBuffer>): ReturnType<typeof this.hexToBuffer>;
|
|
8
7
|
static hexToBuffer(hex: UuidExport['hex']): Promise<UuidExport['blob']>;
|
|
9
|
-
static bufferToHex(buffer: UuidExport['blob']): Promise<UuidExport['hex']>;
|
|
8
|
+
static bufferToHex(buffer: UuidExport['blob'] | D1Blob): Promise<UuidExport['hex']>;
|
|
10
9
|
static base64ToBuffer(rawBase64: string, urlSafe: boolean): Promise<UuidExport['blob']>;
|
|
11
|
-
static bufferToBase64(buffer: UuidExport['blob'], urlSafe: boolean): Promise<string>;
|
|
10
|
+
static bufferToBase64(buffer: UuidExport['blob'] | D1Blob, urlSafe: boolean): Promise<string>;
|
|
12
11
|
static get generateUuid(): Promise<UuidExport>;
|
|
13
|
-
static uuidConvert(input:
|
|
12
|
+
static uuidConvert(input: undefined): Promise<UndefinedProperties<UuidExport>>;
|
|
14
13
|
static uuidConvert(prefixedUtf: PrefixedUuid): Promise<UuidExport>;
|
|
15
14
|
static uuidConvert(input: UuidExport['utf8']): Promise<UuidExport>;
|
|
16
15
|
static uuidConvert(input: UuidExport['hex']): Promise<UuidExport>;
|
|
17
|
-
static uuidConvert(input:
|
|
16
|
+
static uuidConvert(input: UuidExport['blob']): Promise<UuidExport>;
|
|
17
|
+
static uuidConvert(input: D1Blob): Promise<UuidExport>;
|
|
18
18
|
}
|
package/dist/buffers.mjs
CHANGED
|
@@ -19,10 +19,12 @@ export class BufferHelpers {
|
|
|
19
19
|
}
|
|
20
20
|
static bufferToHex(buffer) {
|
|
21
21
|
return (import('node:buffer')
|
|
22
|
+
// @ts-expect-error `ArrayBufferLike` or D1Blob is actually accepted and fine
|
|
22
23
|
.then(({ Buffer }) => Buffer.from(buffer).toString('hex'))
|
|
23
24
|
/**
|
|
24
25
|
* @link https://jsbm.dev/AoXo8dEke1GUg
|
|
25
26
|
*/
|
|
27
|
+
// @ts-expect-error `ArrayBufferLike` or D1Blob is actually accepted and fine
|
|
26
28
|
.catch(() => new Uint8Array(buffer).reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '')));
|
|
27
29
|
}
|
|
28
30
|
static base64ToBuffer(rawBase64, urlSafe) {
|
|
@@ -44,7 +46,8 @@ export class BufferHelpers {
|
|
|
44
46
|
});
|
|
45
47
|
}
|
|
46
48
|
static bufferToBase64(buffer, urlSafe) {
|
|
47
|
-
return import('node:buffer')
|
|
49
|
+
return (import('node:buffer')
|
|
50
|
+
// @ts-expect-error `ArrayBufferLike` or D1Blob is actually accepted and fine
|
|
48
51
|
.then(({ Buffer }) => Buffer.from(buffer).toString(urlSafe ? 'base64url' : 'base64'))
|
|
49
52
|
.catch(() => {
|
|
50
53
|
// @ts-expect-error `ArrayBufferLike` is actually accepted and fine
|
|
@@ -55,7 +58,7 @@ export class BufferHelpers {
|
|
|
55
58
|
else {
|
|
56
59
|
return raw;
|
|
57
60
|
}
|
|
58
|
-
});
|
|
61
|
+
}));
|
|
59
62
|
}
|
|
60
63
|
static get generateUuid() {
|
|
61
64
|
return Promise.all([CryptoHelpers.secretBytes(16), import('uuid')]).then(([random, { v7: uuidv7 }]) => {
|
|
@@ -94,11 +97,11 @@ export class BufferHelpers {
|
|
|
94
97
|
}
|
|
95
98
|
}
|
|
96
99
|
else {
|
|
97
|
-
|
|
98
|
-
return this.bufferToHex(blob).then((hex) => ({
|
|
100
|
+
return this.bufferToHex(input).then((hex) => ({
|
|
99
101
|
utf8: `${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${hex.substring(20)}`,
|
|
100
102
|
hex,
|
|
101
|
-
|
|
103
|
+
// @ts-expect-error `ArrayBufferLike` or D1Blob is actually accepted and fine
|
|
104
|
+
blob: new Uint8Array(input).buffer,
|
|
102
105
|
}));
|
|
103
106
|
}
|
|
104
107
|
}
|
package/dist/crypto.d.mts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
export declare class CryptoHelpers {
|
|
2
2
|
static secretBytes(byteSize: number): Promise<Uint8Array<ArrayBuffer | SharedArrayBuffer> | Uint8Array<ArrayBuffer>>;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
static base16secret(byteSize: number): Promise<string>;
|
|
7
|
-
/**
|
|
8
|
-
* @yields secret length = (`byteSize` * Math.log2(62)) / 8
|
|
9
|
-
*/
|
|
10
|
-
static base62secret(byteSize: number): Promise<string>;
|
|
3
|
+
static base16secret(secretLength: number): Promise<string>;
|
|
4
|
+
static base62secret(secretLength: number): Promise<string>;
|
|
11
5
|
static getHash(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', input: string | ArrayBufferLike): Promise<string>;
|
|
12
6
|
/**
|
|
13
7
|
* @returns Fully formatted (double quote encapsulated) `ETag` header value
|
package/dist/crypto.mjs
CHANGED
|
@@ -12,20 +12,14 @@ export class CryptoHelpers {
|
|
|
12
12
|
return randomBytes;
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
18
|
-
static base16secret(byteSize) {
|
|
19
|
-
return this.secretBytes(byteSize).then((bytes) => BufferHelpers.bufferToHex(bytes.buffer));
|
|
15
|
+
static base16secret(secretLength) {
|
|
16
|
+
return this.secretBytes(secretLength / 2).then((bytes) => BufferHelpers.bufferToHex(bytes.buffer));
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
* @yields secret length = (`byteSize` * Math.log2(62)) / 8
|
|
23
|
-
*/
|
|
24
|
-
static base62secret(byteSize) {
|
|
18
|
+
static base62secret(secretLength) {
|
|
25
19
|
const LOWER_CHAR_SET = 'abcdefghijklmnopqrstuvwxyz';
|
|
26
20
|
const NUMBER_CHAR_SET = '0123456789';
|
|
27
21
|
const CHAR_SET = `${NUMBER_CHAR_SET}${LOWER_CHAR_SET}${LOWER_CHAR_SET.toUpperCase()}`;
|
|
28
|
-
return this.secretBytes(
|
|
22
|
+
return this.secretBytes(secretLength).then((randomBytes) => {
|
|
29
23
|
/**
|
|
30
24
|
* @link https://jsbm.dev/x1F2ITy7RU8T2
|
|
31
25
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import type { Chalk } from 'chalk';
|
|
1
2
|
export * from './buffers.mjs';
|
|
2
3
|
export * from './crypto.mjs';
|
|
3
4
|
export * from './discord.mjs';
|
|
4
5
|
export * from './net.mjs';
|
|
5
6
|
export declare class Helpers {
|
|
7
|
+
/**
|
|
8
|
+
* Generates a unique RGB color based unique to the provided string ID. The RGB values are clamped to a range that ensures the resulting color is legible
|
|
9
|
+
*
|
|
10
|
+
* @param id - The input string used to generate the unique color.
|
|
11
|
+
* @returns A tuple containing the RGB values [r, g, b].
|
|
12
|
+
*/
|
|
13
|
+
static uniqueIdColor(id: string): Parameters<InstanceType<typeof Chalk>['rgb']>;
|
|
6
14
|
static precisionFloat(input: string): number;
|
|
7
15
|
/**
|
|
8
16
|
* A wrapper around `Promise.allSettled()` that filters and returns only the fulfilled results. This method behaves like `Promise.allSettled()` where one promise failing doesn't stop others.
|
|
@@ -12,4 +20,5 @@ export declare class Helpers {
|
|
|
12
20
|
* @returns A promise that resolves to an array of fulfilled values from the input promises.
|
|
13
21
|
*/
|
|
14
22
|
static getFulfilledResults<T extends unknown>(promises: PromiseLike<T>[]): Promise<Awaited<T>[]>;
|
|
23
|
+
static areArraysEqual<T>(array1: T[], array2: T[]): boolean;
|
|
15
24
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,31 @@ export * from './crypto.mjs';
|
|
|
3
3
|
export * from './discord.mjs';
|
|
4
4
|
export * from './net.mjs';
|
|
5
5
|
export class Helpers {
|
|
6
|
+
/**
|
|
7
|
+
* Generates a unique RGB color based unique to the provided string ID. The RGB values are clamped to a range that ensures the resulting color is legible
|
|
8
|
+
*
|
|
9
|
+
* @param id - The input string used to generate the unique color.
|
|
10
|
+
* @returns A tuple containing the RGB values [r, g, b].
|
|
11
|
+
*/
|
|
12
|
+
static uniqueIdColor(id) {
|
|
13
|
+
// Hash the string to a numeric value
|
|
14
|
+
let hash = 0;
|
|
15
|
+
for (let i = 0; i < id.length; i++) {
|
|
16
|
+
const char = id.charCodeAt(i);
|
|
17
|
+
hash = (hash << 5) - hash + char;
|
|
18
|
+
hash |= 0; // Convert to 32-bit integer
|
|
19
|
+
}
|
|
20
|
+
// Convert the hash to RGB components
|
|
21
|
+
let r = (hash & 0xff0000) >> 16; // Extract red
|
|
22
|
+
let g = (hash & 0x00ff00) >> 8; // Extract green
|
|
23
|
+
let b = hash & 0x0000ff; // Extract blue
|
|
24
|
+
// Clamp RGB values to a more legible range (e.g., 64-200)
|
|
25
|
+
const clamp = (value) => Math.max(100, Math.min(222, value));
|
|
26
|
+
r = clamp(r);
|
|
27
|
+
g = clamp(g);
|
|
28
|
+
b = clamp(b);
|
|
29
|
+
return [r, g, b];
|
|
30
|
+
}
|
|
6
31
|
static precisionFloat(input) {
|
|
7
32
|
if (!input.includes('.')) {
|
|
8
33
|
// No decimal point means it's an integer, just return as a float
|
|
@@ -29,4 +54,22 @@ export class Helpers {
|
|
|
29
54
|
static getFulfilledResults(promises) {
|
|
30
55
|
return Promise.allSettled(promises).then((results) => results.filter((result) => result.status === 'fulfilled').map((result) => result.value));
|
|
31
56
|
}
|
|
57
|
+
static areArraysEqual(array1, array2) {
|
|
58
|
+
// Quick length check for early exit
|
|
59
|
+
if (array1.length !== array2.length) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
// Use Set for efficient comparison if arrays are of primitive types
|
|
63
|
+
const set1 = new Set(array1);
|
|
64
|
+
const set2 = new Set(array2);
|
|
65
|
+
if (set1.size !== set2.size) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
for (const item of set1) {
|
|
69
|
+
if (!set2.has(item)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
32
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
|
|
@@ -30,8 +30,7 @@
|
|
|
30
30
|
"build": "tsc",
|
|
31
31
|
"build:clean": "npm run build -- --build --clean && npm run build",
|
|
32
32
|
"pretest": "tsc --project tsconfig.tests.json",
|
|
33
|
-
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout"
|
|
34
|
-
"test:local": "npm run test"
|
|
33
|
+
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout"
|
|
35
34
|
},
|
|
36
35
|
"type": "module",
|
|
37
36
|
"bugs": {
|
|
@@ -49,14 +48,14 @@
|
|
|
49
48
|
},
|
|
50
49
|
"prettier": "@demosjarco/prettier-config",
|
|
51
50
|
"dependencies": {
|
|
52
|
-
"@discordjs/rest": "^2.4.
|
|
53
|
-
"chalk": "^5.
|
|
51
|
+
"@discordjs/rest": "^2.4.2",
|
|
52
|
+
"chalk": "^5.4.1",
|
|
54
53
|
"cloudflare": "^3.5.0",
|
|
55
54
|
"uuid": "^11.0.3"
|
|
56
55
|
},
|
|
57
56
|
"devDependencies": {
|
|
58
|
-
"@chainfuse/types": "^1.
|
|
59
|
-
"@types/node": "^22.10.
|
|
57
|
+
"@chainfuse/types": "^1.4.0",
|
|
58
|
+
"@types/node": "^22.10.5"
|
|
60
59
|
},
|
|
61
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "13881adbb875a4b1680448205ff9e0ac55577f72"
|
|
62
61
|
}
|