@chainfuse/helpers 0.0.10 → 0.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/dist/buffers.d.mts +7 -1
- package/dist/buffers.mjs +38 -1
- package/package.json +4 -4
package/dist/buffers.d.mts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { UndefinedProperties } from '@chainfuse/types';
|
|
2
2
|
import type { PrefixedUuid, UuidExport } from '@chainfuse/types/d1';
|
|
3
3
|
export declare class BufferHelpers {
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
7
|
+
static bufferFromHex(...args: Parameters<typeof this.hexToBuffer>): ReturnType<typeof this.hexToBuffer>;
|
|
8
|
+
static hexToBuffer(hex: UuidExport['hex']): Promise<UuidExport['blob']>;
|
|
5
9
|
static bufferToHex(buffer: UuidExport['blob']): Promise<UuidExport['hex']>;
|
|
10
|
+
static base64ToBuffer(rawBase64: string, urlSafe: boolean): Promise<UuidExport['blob']>;
|
|
11
|
+
static bufferToBase64(buffer: UuidExport['blob'], urlSafe: boolean): Promise<string>;
|
|
6
12
|
static get generateUuid(): Promise<UuidExport>;
|
|
7
13
|
static uuidConvert(input: UuidExport['blob']): Promise<UuidExport>;
|
|
8
14
|
static uuidConvert(prefixedUtf: PrefixedUuid): Promise<UuidExport>;
|
package/dist/buffers.mjs
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { CryptoHelpers } from './crypto.mjs';
|
|
2
2
|
export class BufferHelpers {
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated
|
|
5
|
+
*/
|
|
6
|
+
static bufferFromHex(...args) {
|
|
7
|
+
return this.hexToBuffer(...args);
|
|
8
|
+
}
|
|
9
|
+
static hexToBuffer(hex) {
|
|
4
10
|
return (import('node:buffer')
|
|
5
11
|
.then(({ Buffer }) => {
|
|
6
12
|
const mainBuffer = Buffer.from(hex, 'hex');
|
|
@@ -19,6 +25,37 @@ export class BufferHelpers {
|
|
|
19
25
|
*/
|
|
20
26
|
.catch(() => new Uint8Array(buffer).reduce((output, elem) => output + ('0' + elem.toString(16)).slice(-2), '')));
|
|
21
27
|
}
|
|
28
|
+
static base64ToBuffer(rawBase64, urlSafe) {
|
|
29
|
+
return import('node:buffer')
|
|
30
|
+
.then(({ Buffer }) => {
|
|
31
|
+
const mainBuffer = Buffer.from(rawBase64, urlSafe ? 'base64url' : 'base64');
|
|
32
|
+
return mainBuffer.buffer.slice(mainBuffer.byteOffset, mainBuffer.byteOffset + mainBuffer.byteLength);
|
|
33
|
+
})
|
|
34
|
+
.catch(() => {
|
|
35
|
+
let base64 = rawBase64;
|
|
36
|
+
if (urlSafe) {
|
|
37
|
+
base64 = rawBase64.replaceAll('-', '+').replaceAll('_', '/');
|
|
38
|
+
// Add padding back to make length a multiple of 4
|
|
39
|
+
while (base64.length % 4 !== 0) {
|
|
40
|
+
base64 += '=';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return new TextEncoder().encode(atob(base64)).buffer;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
static bufferToBase64(buffer, urlSafe) {
|
|
47
|
+
return import('node:buffer')
|
|
48
|
+
.then(({ Buffer }) => Buffer.from(buffer).toString(urlSafe ? 'base64url' : 'base64'))
|
|
49
|
+
.catch(() => {
|
|
50
|
+
const raw = btoa(new TextDecoder().decode(buffer));
|
|
51
|
+
if (urlSafe) {
|
|
52
|
+
return raw.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return raw;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
22
59
|
static get generateUuid() {
|
|
23
60
|
return Promise.all([CryptoHelpers.secretBytes(16), import('uuid')]).then(([random, { v7: uuidv7 }]) => {
|
|
24
61
|
const uuid = uuidv7({ random });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
|
|
@@ -53,9 +53,9 @@
|
|
|
53
53
|
"uuid": "^10.0.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@chainfuse/types": "^1.0.
|
|
57
|
-
"@types/node": "^20.16.
|
|
56
|
+
"@chainfuse/types": "^1.0.1",
|
|
57
|
+
"@types/node": "^20.16.11",
|
|
58
58
|
"@types/uuid": "^10.0.0"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "86443611d415eb0a5a533dd5fa4c0818c04c3cec"
|
|
61
61
|
}
|