@chainfuse/helpers 0.3.0 → 0.4.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/dist/buffers.mjs CHANGED
@@ -47,6 +47,7 @@ export class BufferHelpers {
47
47
  return import('node:buffer')
48
48
  .then(({ Buffer }) => Buffer.from(buffer).toString(urlSafe ? 'base64url' : 'base64'))
49
49
  .catch(() => {
50
+ // @ts-expect-error `ArrayBufferLike` is actually accepted and fine
50
51
  const raw = btoa(new TextDecoder().decode(buffer));
51
52
  if (urlSafe) {
52
53
  return raw.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
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 | SharedArrayBuffer> | Uint8Array<ArrayBuffer>>;
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.0",
3
+ "version": "0.4.0",
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.7",
59
- "@types/node": "^22.9.0"
58
+ "@chainfuse/types": "^1.2.1",
59
+ "@types/node": "^22.10.0"
60
60
  },
61
- "gitHead": "8818545b1a8c724cf0a9d1698d00edc45eb489b3"
61
+ "gitHead": "1bdc9ab8d4604ec581e390361a78f5dcc615dbbf"
62
62
  }