@chainfuse/helpers 4.1.8 → 4.2.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/crypto.d.mts +1 -0
- package/dist/crypto.mjs +13 -2
- package/dist/cryptoInternals.d.mts +3 -0
- package/dist/cryptoInternals.mjs +33 -0
- package/dist/db.d.mts +1 -8
- package/dist/db.mjs +3 -3
- package/package.json +3 -3
package/dist/crypto.d.mts
CHANGED
|
@@ -3,6 +3,7 @@ export declare class CryptoHelpers {
|
|
|
3
3
|
static secretBytesSync(byteSize: number): Uint8Array<ArrayBufferLike>;
|
|
4
4
|
static base16secret(secretLength: number): Promise<string>;
|
|
5
5
|
static base62secret(secretLength: number): Promise<string>;
|
|
6
|
+
static getHashStreaming(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', body: ReadableStream<Uint8Array>): Promise<string>;
|
|
6
7
|
static getHash(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', input: string | ArrayBufferLike): Promise<string>;
|
|
7
8
|
/**
|
|
8
9
|
* @returns Fully formatted (double quote encapsulated) `ETag` header value
|
package/dist/crypto.mjs
CHANGED
|
@@ -29,6 +29,9 @@ export class CryptoHelpers {
|
|
|
29
29
|
return randomText;
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
+
static getHashStreaming(algorithm, body) {
|
|
33
|
+
return CryptoHelpersInternals.node_getHashStreaming(algorithm, body).catch(() => CryptoHelpersInternals.browser_getHashStreaming(algorithm, body));
|
|
34
|
+
}
|
|
32
35
|
static getHash(algorithm, input) {
|
|
33
36
|
return CryptoHelpersInternals.node_getHash(algorithm, input).catch(() => CryptoHelpersInternals.browser_getHash(algorithm, input));
|
|
34
37
|
}
|
|
@@ -37,9 +40,17 @@ export class CryptoHelpers {
|
|
|
37
40
|
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#etag_value
|
|
38
41
|
*/
|
|
39
42
|
static generateETag(response, algorithm = 'SHA-512') {
|
|
40
|
-
return
|
|
43
|
+
return (() => {
|
|
44
|
+
const body = response.clone().body;
|
|
45
|
+
if (body) {
|
|
46
|
+
return this.getHashStreaming(algorithm, body).then((hex) => `"${hex}"`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error('Response has no body to hash');
|
|
50
|
+
}
|
|
51
|
+
})().catch(() => response
|
|
41
52
|
.clone()
|
|
42
53
|
.arrayBuffer()
|
|
43
|
-
.then((buffer) => this.getHash(algorithm, buffer).then((hex) => `"${hex}"`));
|
|
54
|
+
.then((buffer) => this.getHash(algorithm, buffer).then((hex) => `"${hex}"`)));
|
|
44
55
|
}
|
|
45
56
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export declare class CryptoHelpersInternals {
|
|
2
2
|
static node_secretBytes(byteSize: number): Promise<Uint8Array>;
|
|
3
3
|
static browser_secretBytes(byteSize: number): Uint8Array;
|
|
4
|
+
private static internal_streamAsyncIterable;
|
|
5
|
+
static node_getHashStreaming(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', body: ReadableStream<Uint8Array>): Promise<string>;
|
|
6
|
+
static browser_getHashStreaming(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', body: ReadableStream<Uint8Array>): Promise<string>;
|
|
4
7
|
static node_getHash(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', input: string | ArrayBufferLike): Promise<string>;
|
|
5
8
|
static browser_getHash(algorithm: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512', input: string | ArrayBufferLike): Promise<string>;
|
|
6
9
|
}
|
package/dist/cryptoInternals.mjs
CHANGED
|
@@ -11,6 +11,39 @@ export class CryptoHelpersInternals {
|
|
|
11
11
|
crypto.getRandomValues(randomBytes);
|
|
12
12
|
return randomBytes;
|
|
13
13
|
}
|
|
14
|
+
static async *internal_streamAsyncIterable(stream) {
|
|
15
|
+
const reader = stream.getReader();
|
|
16
|
+
try {
|
|
17
|
+
while (true) {
|
|
18
|
+
const { done, value } = await reader.read();
|
|
19
|
+
if (done)
|
|
20
|
+
return;
|
|
21
|
+
yield value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
reader.releaseLock();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
static node_getHashStreaming(algorithm, body) {
|
|
29
|
+
return import('node:crypto').then(async ({ createHash }) => {
|
|
30
|
+
const hash = createHash(algorithm.replace('-', '').toLowerCase());
|
|
31
|
+
for await (const chunk of this.internal_streamAsyncIterable(body)) {
|
|
32
|
+
hash.update(chunk);
|
|
33
|
+
}
|
|
34
|
+
return hash.digest('hex');
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
static async browser_getHashStreaming(algorithm, body) {
|
|
38
|
+
if ('DigestStream' in crypto) {
|
|
39
|
+
const digestStream = new crypto.DigestStream(algorithm);
|
|
40
|
+
await body.pipeTo(digestStream);
|
|
41
|
+
return BufferHelpers.bufferToHex(await digestStream.digest);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw new Error('`DigestStream` is not available in this environment');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
14
47
|
static node_getHash(algorithm, input) {
|
|
15
48
|
return import('node:crypto').then(async ({ createHash }) => {
|
|
16
49
|
const hash = createHash(algorithm.replace('-', '').toLowerCase());
|
package/dist/db.d.mts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
+
import type { CacheStorageLike } from '@chainfuse/types';
|
|
1
2
|
import { Cache as DrizzleCache, type MutationOption } from 'drizzle-orm/cache/core';
|
|
2
3
|
import type { CacheConfig } from 'drizzle-orm/cache/core/types';
|
|
3
4
|
import * as z from 'zod/mini';
|
|
4
|
-
/**
|
|
5
|
-
* Interface for CacheStorage-like objects that can be used as drop-in replacements.
|
|
6
|
-
* This interface ensures compatibility with the Web API CacheStorage while allowing for custom implementations that provide the same core functionality.
|
|
7
|
-
*/
|
|
8
|
-
export interface CacheStorageLike {
|
|
9
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CacheStorage/open) */
|
|
10
|
-
open(cacheName: string): Promise<Cache>;
|
|
11
|
-
}
|
|
12
5
|
/**
|
|
13
6
|
* SQLCache is a cache implementation for SQL query results, using Web CacheStorage (supports drop in replacements).
|
|
14
7
|
* It supports caching strategies for explicit or global query caching, and provides mechanisms for cache invalidation based on affected tables or tags.
|
package/dist/db.mjs
CHANGED
|
@@ -140,14 +140,14 @@ export class SQLCache extends DrizzleCache {
|
|
|
140
140
|
else if (config?.pxat) {
|
|
141
141
|
ttl = Math.floor((new Date(config.pxat).getTime() - Date.now()) / 1000);
|
|
142
142
|
}
|
|
143
|
-
const
|
|
143
|
+
const cacheResponse = new Response(JSON.stringify(response), {
|
|
144
144
|
headers: {
|
|
145
145
|
'Content-Type': 'application/json',
|
|
146
146
|
'Cache-Control': `public, max-age=${ttl}, s-maxage=${ttl}`,
|
|
147
147
|
},
|
|
148
148
|
});
|
|
149
|
-
|
|
150
|
-
await this.cache.then(async (cache) => cache.put(this.getCacheKey(isTag ? { tag: hashedQuery } : { key: hashedQuery }),
|
|
149
|
+
cacheResponse.headers.set('ETag', await CryptoHelpers.generateETag(cacheResponse));
|
|
150
|
+
await this.cache.then(async (cache) => cache.put(this.getCacheKey(isTag ? { tag: hashedQuery } : { key: hashedQuery }), cacheResponse)).then(() => console.debug('SQLCache.put', isTag ? 'tag' : 'key', hashedQuery, 'SUCCESS'));
|
|
151
151
|
for (const table of tables) {
|
|
152
152
|
const keys = this.usedTablesPerKey[table];
|
|
153
153
|
if (keys === undefined) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainfuse/helpers",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "ChainFuse",
|
|
6
6
|
"homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
},
|
|
81
81
|
"prettier": "@demosjarco/prettier-config",
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@chainfuse/types": "^4.0
|
|
83
|
+
"@chainfuse/types": "^4.1.0",
|
|
84
84
|
"@discordjs/rest": "^2.6.0",
|
|
85
85
|
"chalk": "^5.6.2",
|
|
86
86
|
"cloudflare": "^5.2.0",
|
|
@@ -93,5 +93,5 @@
|
|
|
93
93
|
"@cloudflare/workers-types": "^4.20251014.0",
|
|
94
94
|
"@types/node": "^24.6.0"
|
|
95
95
|
},
|
|
96
|
-
"gitHead": "
|
|
96
|
+
"gitHead": "f2559bafd284760bc5fce2df9e699616cf4710a3"
|
|
97
97
|
}
|