@chainfuse/helpers 0.3.1 → 0.4.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.
@@ -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: UuidExport['blob']): Promise<UuidExport>;
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: undefined): Promise<UndefinedProperties<UuidExport>>;
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,9 +46,11 @@ 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(() => {
53
+ // @ts-expect-error `ArrayBufferLike` is actually accepted and fine
50
54
  const raw = btoa(new TextDecoder().decode(buffer));
51
55
  if (urlSafe) {
52
56
  return raw.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
@@ -54,7 +58,7 @@ export class BufferHelpers {
54
58
  else {
55
59
  return raw;
56
60
  }
57
- });
61
+ }));
58
62
  }
59
63
  static get generateUuid() {
60
64
  return Promise.all([CryptoHelpers.secretBytes(16), import('uuid')]).then(([random, { v7: uuidv7 }]) => {
@@ -93,11 +97,11 @@ export class BufferHelpers {
93
97
  }
94
98
  }
95
99
  else {
96
- const blob = input;
97
- return this.bufferToHex(blob).then((hex) => ({
100
+ return this.bufferToHex(input).then((hex) => ({
98
101
  utf8: `${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${hex.substring(20)}`,
99
102
  hex,
100
- blob,
103
+ // @ts-expect-error `ArrayBufferLike` or D1Blob is actually accepted and fine
104
+ blob: new Uint8Array(input).buffer,
101
105
  }));
102
106
  }
103
107
  }
package/dist/crypto.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  export declare class CryptoHelpers {
2
- static secretBytes(byteSize: number): Promise<Buffer | Uint8Array>;
2
+ static secretBytes(byteSize: number): Promise<Uint8Array<ArrayBuffer> | Uint8Array<ArrayBuffer | SharedArrayBuffer>>;
3
3
  /**
4
4
  * @yields secret length = (`byteSize` * Math.log2(16)) / 8
5
5
  */
package/dist/crypto.mjs CHANGED
@@ -2,7 +2,10 @@ import { BufferHelpers } from './buffers.mjs';
2
2
  export class CryptoHelpers {
3
3
  static secretBytes(byteSize) {
4
4
  return import('node:crypto')
5
- .then(({ randomBytes }) => randomBytes(byteSize))
5
+ .then(({ randomBytes }) => {
6
+ const mainBuffer = randomBytes(byteSize);
7
+ return new Uint8Array(mainBuffer.buffer.slice(mainBuffer.byteOffset, mainBuffer.byteOffset + mainBuffer.byteLength));
8
+ })
6
9
  .catch(() => {
7
10
  const randomBytes = new Uint8Array(byteSize);
8
11
  crypto.getRandomValues(randomBytes);
@@ -13,7 +16,7 @@ export class CryptoHelpers {
13
16
  * @yields secret length = (`byteSize` * Math.log2(16)) / 8
14
17
  */
15
18
  static base16secret(byteSize) {
16
- return this.secretBytes(byteSize).then((bytes) => BufferHelpers.bufferToHex(bytes));
19
+ return this.secretBytes(byteSize).then((bytes) => BufferHelpers.bufferToHex(bytes.buffer));
17
20
  }
18
21
  /**
19
22
  * @yields secret length = (`byteSize` * Math.log2(62)) / 8
@@ -36,7 +39,7 @@ export class CryptoHelpers {
36
39
  });
37
40
  }
38
41
  static getHash(algorithm, input) {
39
- return import('node:crypto')
42
+ return (import('node:crypto')
40
43
  .then(async ({ createHash }) => {
41
44
  const hash = createHash(algorithm.replace('-', '').toLowerCase());
42
45
  if (typeof input === 'string') {
@@ -47,7 +50,8 @@ export class CryptoHelpers {
47
50
  }
48
51
  return hash.digest('hex');
49
52
  })
50
- .catch(() => crypto.subtle.digest(algorithm, typeof input === 'string' ? new TextEncoder().encode(input) : input).then((hashBuffer) => BufferHelpers.bufferToHex(hashBuffer)));
53
+ // @ts-expect-error `ArrayBufferLike` is actually accepted and fine
54
+ .catch(() => crypto.subtle.digest(algorithm, typeof input === 'string' ? new TextEncoder().encode(input) : input).then((hashBuffer) => BufferHelpers.bufferToHex(hashBuffer))));
51
55
  }
52
56
  /**
53
57
  * @returns Fully formatted (double quote encapsulated) `ETag` header value
@@ -1,6 +1,6 @@
1
1
  import type { CustomLoging } from '@chainfuse/types';
2
2
  import type { ExecutionContext } from '@cloudflare/workers-types/experimental';
3
- import { REST } from '@discordjs/rest';
3
+ import { REST, type RESTOptions } from '@discordjs/rest';
4
4
  export declare class DiscordHelpers {
5
5
  /**
6
6
  * Discord Epoch, the first second of 2015 or 1420070400000
@@ -17,5 +17,5 @@ export declare class DiscordHelpers {
17
17
  * @link https://discord.com/developers/docs/reference#snowflakes-snowflake-id-format-structure-left-to-right
18
18
  */
19
19
  static discordSnowflakeToDate(snowflakeRaw?: bigint | string): Date;
20
- static discordRest(apiKey: string, cacheTtl?: number, forceCache?: boolean, executionContext?: ExecutionContext, logger?: CustomLoging): REST;
20
+ static discordRest(apiKey: string, cacheTtl?: number, forceCache?: boolean, executionContext?: ExecutionContext, logger?: CustomLoging, restOptions?: Partial<Omit<RESTOptions, 'agent' | 'authPrefix' | 'makeRequest'>>): REST;
21
21
  }
package/dist/discord.mjs CHANGED
@@ -30,8 +30,9 @@ export class DiscordHelpers {
30
30
  const snowflake = BigInt(snowflakeRaw);
31
31
  return new Date(Number((snowflake >> BigInt(22)) + this.discordEpoch));
32
32
  }
33
- static discordRest(apiKey, cacheTtl = 24 * 60 * 60, forceCache = false, executionContext, logger = false) {
33
+ static discordRest(apiKey, cacheTtl = 24 * 60 * 60, forceCache = false, executionContext, logger = false, restOptions) {
34
34
  return new REST({
35
+ ...restOptions,
35
36
  agent: null,
36
37
  authPrefix: 'Bot',
37
38
  makeRequest: (url, rawInit) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/helpers",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
@@ -55,8 +55,8 @@
55
55
  "uuid": "^11.0.3"
56
56
  },
57
57
  "devDependencies": {
58
- "@chainfuse/types": "^1.1.8",
59
- "@types/node": "^22.9.1"
58
+ "@chainfuse/types": "^1.3.0",
59
+ "@types/node": "^22.10.0"
60
60
  },
61
- "gitHead": "50cb4ae5092e0c748de2c9e8a74f1afc79e0102e"
61
+ "gitHead": "ea93b05607f7e9687526434591370bfad1ad8605"
62
62
  }