covara 0.11.2 → 0.13.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 +4 -1
- package/dist/abuse/budget.d.ts +41 -0
- package/dist/abuse/budget.d.ts.map +1 -0
- package/dist/abuse/budget.js +122 -0
- package/dist/abuse/budget.js.map +1 -0
- package/dist/abuse/captcha.d.ts +45 -0
- package/dist/abuse/captcha.d.ts.map +1 -0
- package/dist/abuse/captcha.js +80 -0
- package/dist/abuse/captcha.js.map +1 -0
- package/dist/abuse/config.d.ts +150 -0
- package/dist/abuse/config.d.ts.map +1 -0
- package/dist/abuse/config.js +33 -0
- package/dist/abuse/config.js.map +1 -0
- package/dist/abuse/enforce.d.ts +44 -0
- package/dist/abuse/enforce.d.ts.map +1 -0
- package/dist/abuse/enforce.js +198 -0
- package/dist/abuse/enforce.js.map +1 -0
- package/dist/abuse/middleware.d.ts +26 -0
- package/dist/abuse/middleware.d.ts.map +1 -0
- package/dist/abuse/middleware.js +115 -0
- package/dist/abuse/middleware.js.map +1 -0
- package/dist/auth/routes.d.ts +12 -0
- package/dist/auth/routes.d.ts.map +1 -1
- package/dist/auth/routes.js +28 -0
- package/dist/auth/routes.js.map +1 -1
- package/dist/cli/templates/agents.d.ts.map +1 -1
- package/dist/cli/templates/agents.js +2 -0
- package/dist/cli/templates/agents.js.map +1 -1
- package/dist/cli/templates/source.d.ts +4 -4
- package/dist/cli/templates/source.d.ts.map +1 -1
- package/dist/cli/templates/source.js +5 -5
- package/dist/client/captcha.d.ts +41 -0
- package/dist/client/captcha.d.ts.map +1 -0
- package/dist/client/captcha.js +110 -0
- package/dist/client/captcha.js.map +1 -0
- package/dist/client/index.d.ts +18 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +4 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/pow.d.ts +15 -0
- package/dist/client/pow.d.ts.map +1 -0
- package/dist/client/pow.js +9 -0
- package/dist/client/pow.js.map +1 -0
- package/dist/client/react.d.ts +29 -1
- package/dist/client/react.d.ts.map +1 -1
- package/dist/client/react.js +77 -1
- package/dist/client/react.js.map +1 -1
- package/dist/client/transport.d.ts +13 -1
- package/dist/client/transport.d.ts.map +1 -1
- package/dist/client/transport.js +131 -16
- package/dist/client/transport.js.map +1 -1
- package/dist/client/types.d.ts +25 -0
- package/dist/client/types.d.ts.map +1 -1
- package/dist/index.d.ts +12 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/pow/core.d.ts +55 -0
- package/dist/pow/core.d.ts.map +1 -0
- package/dist/pow/core.js +220 -0
- package/dist/pow/core.js.map +1 -0
- package/dist/pow/server.d.ts +71 -0
- package/dist/pow/server.d.ts.map +1 -0
- package/dist/pow/server.js +152 -0
- package/dist/pow/server.js.map +1 -0
- package/dist/resource/error.d.ts +17 -0
- package/dist/resource/error.d.ts.map +1 -1
- package/dist/resource/error.js +46 -0
- package/dist/resource/error.js.map +1 -1
- package/dist/resource/filter.d.ts +1 -0
- package/dist/resource/filter.d.ts.map +1 -1
- package/dist/resource/filter.js +101 -4
- package/dist/resource/filter.js.map +1 -1
- package/dist/resource/hook.d.ts.map +1 -1
- package/dist/resource/hook.js +73 -3
- package/dist/resource/hook.js.map +1 -1
- package/dist/resource/procedures.d.ts +3 -2
- package/dist/resource/procedures.d.ts.map +1 -1
- package/dist/resource/procedures.js +1 -0
- package/dist/resource/procedures.js.map +1 -1
- package/dist/resource/relation-registry.d.ts +20 -0
- package/dist/resource/relation-registry.d.ts.map +1 -0
- package/dist/resource/relation-registry.js +12 -0
- package/dist/resource/relation-registry.js.map +1 -0
- package/dist/resource/search.d.ts +4 -0
- package/dist/resource/search.d.ts.map +1 -1
- package/dist/resource/search.js +27 -8
- package/dist/resource/search.js.map +1 -1
- package/dist/resource/secure-query.d.ts.map +1 -1
- package/dist/resource/secure-query.js +13 -0
- package/dist/resource/secure-query.js.map +1 -1
- package/dist/resource/subscription.d.ts.map +1 -1
- package/dist/resource/subscription.js +16 -0
- package/dist/resource/subscription.js.map +1 -1
- package/dist/resource/types.d.ts +18 -5
- package/dist/resource/types.d.ts.map +1 -1
- package/dist/server/app.d.ts +2 -0
- package/dist/server/app.d.ts.map +1 -1
- package/dist/server/app.js +4 -0
- package/dist/server/app.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,12 +6,12 @@ export { useResource } from "./resource/hook.js";
|
|
|
6
6
|
export { createResourceFilter } from "./resource/filter.js";
|
|
7
7
|
export { changelog, recordCreate, recordUpdate, recordDelete } from "./resource/changelog.js";
|
|
8
8
|
export { encodeCursor, decodeCursor, parseOrderBy, createPagination, setGlobalCursorSigningSecret, getGlobalCursorSigningSecret, } from "./resource/pagination.js";
|
|
9
|
-
export { ResourceError, NotFoundError, ValidationError, UnauthorizedError, ForbiddenError, RateLimitError, BatchLimitError, ConflictError, SearchError, SearchNotConfiguredError, formatErrorResponse, formatZodError, } from "./resource/error.js";
|
|
9
|
+
export { ResourceError, NotFoundError, ValidationError, UnauthorizedError, ForbiddenError, RateLimitError, PowRequiredError, CaptchaRequiredError, BatchLimitError, ConflictError, SearchError, SearchNotConfiguredError, formatErrorResponse, formatZodError, } from "./resource/error.js";
|
|
10
10
|
export { columnName, columnNames, } from "./resource/column-ref.js";
|
|
11
11
|
// Subscriptions
|
|
12
12
|
export { createSubscription as createServerSubscription, pushUpdatesToSubscriptions, pushDeletesToSubscriptions, clearAllSubscriptions, } from "./resource/subscription.js";
|
|
13
13
|
// Procedures
|
|
14
|
-
export { defineProcedure, executeProcedure, createTimestampHooks, composeHooks, } from "./resource/procedures.js";
|
|
14
|
+
export { defineProcedure, procedureBuilder, executeProcedure, createTimestampHooks, composeHooks, } from "./resource/procedures.js";
|
|
15
15
|
// Query utilities
|
|
16
16
|
export { parseSelect, applyProjection } from "./resource/query.js";
|
|
17
17
|
// Auth
|
|
@@ -30,6 +30,13 @@ export { hashPassword, verifyPassword, needsRehash } from "./auth/password.js";
|
|
|
30
30
|
export { createCsrfMiddleware, issueCsrfToken, generateCsrfToken, LoginThrottle, InMemoryVerificationTokenStore, KVVerificationTokenStore, createKVVerificationTokenStore, issueToken, verifyToken, issuePasswordResetToken, verifyPasswordResetToken, hashNewPassword, } from "./auth/index.js";
|
|
31
31
|
export { useAuth, createAuthRoutes } from "./auth/routes.js";
|
|
32
32
|
export { InMemorySessionStore } from "./auth/types.js";
|
|
33
|
+
// Abuse protection (token-bucket budget + proof-of-work)
|
|
34
|
+
export { abuseProtection, setGlobalAbuseProtection, getGlobalAbuseProtection, hasGlobalAbuseProtection, clearGlobalAbuseProtection, isBudgetEnabled, isPowEnabled, DEFAULT_POW_DIFFICULTY, DEFAULT_POW_TTL_MS, } from "./abuse/config.js";
|
|
35
|
+
export { turnstile, hcaptcha, recaptcha, customCaptcha, } from "./abuse/captcha.js";
|
|
36
|
+
export { createAbuseMiddleware } from "./abuse/middleware.js";
|
|
37
|
+
export { enforceAbuse } from "./abuse/enforce.js";
|
|
38
|
+
export { issueChallenge, verifySolution, computeFingerprint, consumeNonce, resolvePowSecret, } from "./pow/server.js";
|
|
39
|
+
export { solveChallenge, sha256Hex, leadingZeroBits, } from "./pow/core.js";
|
|
33
40
|
// Middleware
|
|
34
41
|
export { createRateLimiter } from "./middleware/rateLimit.js";
|
|
35
42
|
export { errorHandler, notFoundHandler } from "./middleware/error.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AAEtC,cAAc;AACd,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGvD,kBAAkB;AAClB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAe,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,YAAY,EA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
1
|
+
/**
|
|
2
|
+
* Isomorphic proof-of-work primitives.
|
|
3
|
+
*
|
|
4
|
+
* This module has ZERO Node.js dependencies so it can be bundled into the
|
|
5
|
+
* browser client. It provides a synchronous SHA-256, base64url codecs, the
|
|
6
|
+
* leading-zero-bit difficulty metric, the challenge payload encode/decode, and
|
|
7
|
+
* the hashcash-style solver shared by client and server.
|
|
8
|
+
*/
|
|
9
|
+
export type PowAlgorithm = "sha256";
|
|
10
|
+
export interface ChallengePayload {
|
|
11
|
+
v: number;
|
|
12
|
+
d: number;
|
|
13
|
+
fp: string;
|
|
14
|
+
iat: number;
|
|
15
|
+
exp: number;
|
|
16
|
+
n: string;
|
|
17
|
+
alg: PowAlgorithm;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Synchronous SHA-256 over raw bytes, returning the 32-byte digest.
|
|
21
|
+
*/
|
|
22
|
+
export declare const sha256Bytes: (message: Uint8Array) => Uint8Array;
|
|
23
|
+
export declare const utf8Bytes: (input: string) => Uint8Array;
|
|
24
|
+
export declare const bytesToHex: (bytes: Uint8Array) => string;
|
|
25
|
+
export declare const sha256Hex: (input: string | Uint8Array) => string;
|
|
26
|
+
export declare const base64UrlEncodeBytes: (bytes: Uint8Array) => string;
|
|
27
|
+
export declare const base64UrlDecodeBytes: (input: string) => Uint8Array;
|
|
28
|
+
export declare const base64UrlEncode: (input: string) => string;
|
|
29
|
+
export declare const base64UrlDecode: (input: string) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Count the number of leading zero bits in a byte array (most significant bit
|
|
32
|
+
* of byte 0 first).
|
|
33
|
+
*/
|
|
34
|
+
export declare const leadingZeroBits: (bytes: Uint8Array) => number;
|
|
35
|
+
export declare const encodeChallengePayload: (payload: ChallengePayload) => string;
|
|
36
|
+
export declare const decodeChallengePayload: (encoded: string) => ChallengePayload;
|
|
37
|
+
/**
|
|
38
|
+
* Hash that the solver must drive below the difficulty target. The full
|
|
39
|
+
* challenge token (payload.signature) is concatenated with the candidate nonce
|
|
40
|
+
* so a solution is bound to the exact issued challenge.
|
|
41
|
+
*/
|
|
42
|
+
export declare const solutionDigest: (token: string, nonce: string, algorithm?: PowAlgorithm) => Uint8Array;
|
|
43
|
+
export declare const meetsDifficulty: (token: string, nonce: string, difficulty: number, algorithm?: PowAlgorithm) => boolean;
|
|
44
|
+
export interface SolveOptions {
|
|
45
|
+
algorithm?: PowAlgorithm;
|
|
46
|
+
maxIterations?: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Find a nonce whose `solutionDigest` has at least `difficulty` leading zero
|
|
50
|
+
* bits. Returns the nonce as a decimal string. Difficulty 0 resolves
|
|
51
|
+
* immediately. Throws if `maxIterations` is exhausted (a safety valve against
|
|
52
|
+
* an unreasonable difficulty).
|
|
53
|
+
*/
|
|
54
|
+
export declare const solveChallenge: (token: string, difficulty: number, options?: SolveOptions) => string;
|
|
55
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/pow/core.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC;AAEpC,MAAM,WAAW,gBAAgB;IAC/B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,GAAG,EAAE,YAAY,CAAC;CACnB;AAkBD;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,SAAS,UAAU,KAAG,UAwEjD,CAAC;AAKF,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,KAAG,UAAmC,CAAC;AAI9E,eAAO,MAAM,UAAU,GAAI,OAAO,UAAU,KAAG,MAM9C,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,GAAG,UAAU,KAAG,MACwB,CAAC;AAWhF,eAAO,MAAM,oBAAoB,GAAI,OAAO,UAAU,KAAG,MAgBxD,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,UAmBpD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,OAAO,MAAM,KAAG,MACR,CAAC;AAEzC,eAAO,MAAM,eAAe,GAAI,OAAO,MAAM,KAAG,MACH,CAAC;AAY9C;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,UAAU,KAAG,MAQnD,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAI,SAAS,gBAAgB,KAAG,MACzB,CAAC;AAE3C,eAAO,MAAM,sBAAsB,GAAI,SAAS,MAAM,KAAG,gBAaxD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc,GACzB,OAAO,MAAM,EACb,OAAO,MAAM,EACb,YAAW,YAAuB,KACjC,UAKF,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,OAAO,MAAM,EACb,OAAO,MAAM,EACb,YAAY,MAAM,EAClB,YAAW,YAAuB,KACjC,OAAiF,CAAC;AAErF,MAAM,WAAW,YAAY;IAC3B,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GACzB,OAAO,MAAM,EACb,YAAY,MAAM,EAClB,UAAS,YAAiB,KACzB,MAeF,CAAC"}
|
package/dist/pow/core.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isomorphic proof-of-work primitives.
|
|
3
|
+
*
|
|
4
|
+
* This module has ZERO Node.js dependencies so it can be bundled into the
|
|
5
|
+
* browser client. It provides a synchronous SHA-256, base64url codecs, the
|
|
6
|
+
* leading-zero-bit difficulty metric, the challenge payload encode/decode, and
|
|
7
|
+
* the hashcash-style solver shared by client and server.
|
|
8
|
+
*/
|
|
9
|
+
const K = new Uint32Array([
|
|
10
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
11
|
+
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
12
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
13
|
+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
14
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
15
|
+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
16
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
17
|
+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
18
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
19
|
+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
20
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
21
|
+
]);
|
|
22
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
23
|
+
/**
|
|
24
|
+
* Synchronous SHA-256 over raw bytes, returning the 32-byte digest.
|
|
25
|
+
*/
|
|
26
|
+
export const sha256Bytes = (message) => {
|
|
27
|
+
const h = new Uint32Array([
|
|
28
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
|
|
29
|
+
0x1f83d9ab, 0x5be0cd19,
|
|
30
|
+
]);
|
|
31
|
+
const bitLen = message.length * 8;
|
|
32
|
+
const withOne = message.length + 1;
|
|
33
|
+
const totalLen = withOne + ((56 - (withOne % 64) + 64) % 64) + 8;
|
|
34
|
+
const padded = new Uint8Array(totalLen);
|
|
35
|
+
padded.set(message);
|
|
36
|
+
padded[message.length] = 0x80;
|
|
37
|
+
const view = new DataView(padded.buffer);
|
|
38
|
+
view.setUint32(totalLen - 4, bitLen >>> 0, false);
|
|
39
|
+
view.setUint32(totalLen - 8, Math.floor(bitLen / 0x100000000), false);
|
|
40
|
+
const w = new Uint32Array(64);
|
|
41
|
+
for (let offset = 0; offset < totalLen; offset += 64) {
|
|
42
|
+
for (let i = 0; i < 16; i++) {
|
|
43
|
+
w[i] = view.getUint32(offset + i * 4, false);
|
|
44
|
+
}
|
|
45
|
+
for (let i = 16; i < 64; i++) {
|
|
46
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
47
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
48
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
49
|
+
}
|
|
50
|
+
let a = h[0];
|
|
51
|
+
let b = h[1];
|
|
52
|
+
let c = h[2];
|
|
53
|
+
let d = h[3];
|
|
54
|
+
let e = h[4];
|
|
55
|
+
let f = h[5];
|
|
56
|
+
let g = h[6];
|
|
57
|
+
let hh = h[7];
|
|
58
|
+
for (let i = 0; i < 64; i++) {
|
|
59
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
60
|
+
const ch = (e & f) ^ (~e & g);
|
|
61
|
+
const t1 = (hh + S1 + ch + K[i] + w[i]) >>> 0;
|
|
62
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
63
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
64
|
+
const t2 = (S0 + maj) >>> 0;
|
|
65
|
+
hh = g;
|
|
66
|
+
g = f;
|
|
67
|
+
f = e;
|
|
68
|
+
e = (d + t1) >>> 0;
|
|
69
|
+
d = c;
|
|
70
|
+
c = b;
|
|
71
|
+
b = a;
|
|
72
|
+
a = (t1 + t2) >>> 0;
|
|
73
|
+
}
|
|
74
|
+
h[0] = (h[0] + a) >>> 0;
|
|
75
|
+
h[1] = (h[1] + b) >>> 0;
|
|
76
|
+
h[2] = (h[2] + c) >>> 0;
|
|
77
|
+
h[3] = (h[3] + d) >>> 0;
|
|
78
|
+
h[4] = (h[4] + e) >>> 0;
|
|
79
|
+
h[5] = (h[5] + f) >>> 0;
|
|
80
|
+
h[6] = (h[6] + g) >>> 0;
|
|
81
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
82
|
+
}
|
|
83
|
+
const out = new Uint8Array(32);
|
|
84
|
+
const outView = new DataView(out.buffer);
|
|
85
|
+
for (let i = 0; i < 8; i++) {
|
|
86
|
+
outView.setUint32(i * 4, h[i], false);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
};
|
|
90
|
+
const encoder = new TextEncoder();
|
|
91
|
+
const decoder = new TextDecoder();
|
|
92
|
+
export const utf8Bytes = (input) => encoder.encode(input);
|
|
93
|
+
const HEX = "0123456789abcdef";
|
|
94
|
+
export const bytesToHex = (bytes) => {
|
|
95
|
+
let out = "";
|
|
96
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
97
|
+
out += HEX[bytes[i] >>> 4] + HEX[bytes[i] & 0xf];
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
};
|
|
101
|
+
export const sha256Hex = (input) => bytesToHex(sha256Bytes(typeof input === "string" ? utf8Bytes(input) : input));
|
|
102
|
+
const B64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
103
|
+
const B64URL_LOOKUP = (() => {
|
|
104
|
+
const table = new Int16Array(128).fill(-1);
|
|
105
|
+
for (let i = 0; i < B64URL.length; i++) {
|
|
106
|
+
table[B64URL.charCodeAt(i)] = i;
|
|
107
|
+
}
|
|
108
|
+
return table;
|
|
109
|
+
})();
|
|
110
|
+
export const base64UrlEncodeBytes = (bytes) => {
|
|
111
|
+
let out = "";
|
|
112
|
+
let i = 0;
|
|
113
|
+
for (; i + 2 < bytes.length; i += 3) {
|
|
114
|
+
const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
|
|
115
|
+
out += B64URL[(n >>> 18) & 63] + B64URL[(n >>> 12) & 63] + B64URL[(n >>> 6) & 63] + B64URL[n & 63];
|
|
116
|
+
}
|
|
117
|
+
const rem = bytes.length - i;
|
|
118
|
+
if (rem === 1) {
|
|
119
|
+
const n = bytes[i] << 16;
|
|
120
|
+
out += B64URL[(n >>> 18) & 63] + B64URL[(n >>> 12) & 63];
|
|
121
|
+
}
|
|
122
|
+
else if (rem === 2) {
|
|
123
|
+
const n = (bytes[i] << 16) | (bytes[i + 1] << 8);
|
|
124
|
+
out += B64URL[(n >>> 18) & 63] + B64URL[(n >>> 12) & 63] + B64URL[(n >>> 6) & 63];
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
};
|
|
128
|
+
export const base64UrlDecodeBytes = (input) => {
|
|
129
|
+
const len = input.length;
|
|
130
|
+
const bytes = [];
|
|
131
|
+
let buffer = 0;
|
|
132
|
+
let bits = 0;
|
|
133
|
+
for (let i = 0; i < len; i++) {
|
|
134
|
+
const code = input.charCodeAt(i);
|
|
135
|
+
const value = code < 128 ? B64URL_LOOKUP[code] : -1;
|
|
136
|
+
if (value === -1) {
|
|
137
|
+
throw new Error("Invalid base64url character");
|
|
138
|
+
}
|
|
139
|
+
buffer = (buffer << 6) | value;
|
|
140
|
+
bits += 6;
|
|
141
|
+
if (bits >= 8) {
|
|
142
|
+
bits -= 8;
|
|
143
|
+
bytes.push((buffer >>> bits) & 0xff);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return new Uint8Array(bytes);
|
|
147
|
+
};
|
|
148
|
+
export const base64UrlEncode = (input) => base64UrlEncodeBytes(utf8Bytes(input));
|
|
149
|
+
export const base64UrlDecode = (input) => decoder.decode(base64UrlDecodeBytes(input));
|
|
150
|
+
const CLZ8 = (byte) => {
|
|
151
|
+
if (byte === 0)
|
|
152
|
+
return 8;
|
|
153
|
+
let n = 0;
|
|
154
|
+
for (let mask = 0x80; mask > 0; mask >>= 1) {
|
|
155
|
+
if (byte & mask)
|
|
156
|
+
break;
|
|
157
|
+
n++;
|
|
158
|
+
}
|
|
159
|
+
return n;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Count the number of leading zero bits in a byte array (most significant bit
|
|
163
|
+
* of byte 0 first).
|
|
164
|
+
*/
|
|
165
|
+
export const leadingZeroBits = (bytes) => {
|
|
166
|
+
let count = 0;
|
|
167
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
168
|
+
const z = CLZ8(bytes[i]);
|
|
169
|
+
count += z;
|
|
170
|
+
if (z !== 8)
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
return count;
|
|
174
|
+
};
|
|
175
|
+
export const encodeChallengePayload = (payload) => base64UrlEncode(JSON.stringify(payload));
|
|
176
|
+
export const decodeChallengePayload = (encoded) => {
|
|
177
|
+
const parsed = JSON.parse(base64UrlDecode(encoded));
|
|
178
|
+
if (typeof parsed !== "object" ||
|
|
179
|
+
parsed === null ||
|
|
180
|
+
typeof parsed.d !== "number" ||
|
|
181
|
+
typeof parsed.fp !== "string" ||
|
|
182
|
+
typeof parsed.exp !== "number" ||
|
|
183
|
+
typeof parsed.n !== "string") {
|
|
184
|
+
throw new Error("Malformed challenge payload");
|
|
185
|
+
}
|
|
186
|
+
return parsed;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Hash that the solver must drive below the difficulty target. The full
|
|
190
|
+
* challenge token (payload.signature) is concatenated with the candidate nonce
|
|
191
|
+
* so a solution is bound to the exact issued challenge.
|
|
192
|
+
*/
|
|
193
|
+
export const solutionDigest = (token, nonce, algorithm = "sha256") => {
|
|
194
|
+
if (algorithm !== "sha256") {
|
|
195
|
+
throw new Error(`Unsupported proof-of-work algorithm: ${algorithm}`);
|
|
196
|
+
}
|
|
197
|
+
return sha256Bytes(utf8Bytes(`${token}:${nonce}`));
|
|
198
|
+
};
|
|
199
|
+
export const meetsDifficulty = (token, nonce, difficulty, algorithm = "sha256") => leadingZeroBits(solutionDigest(token, nonce, algorithm)) >= difficulty;
|
|
200
|
+
/**
|
|
201
|
+
* Find a nonce whose `solutionDigest` has at least `difficulty` leading zero
|
|
202
|
+
* bits. Returns the nonce as a decimal string. Difficulty 0 resolves
|
|
203
|
+
* immediately. Throws if `maxIterations` is exhausted (a safety valve against
|
|
204
|
+
* an unreasonable difficulty).
|
|
205
|
+
*/
|
|
206
|
+
export const solveChallenge = (token, difficulty, options = {}) => {
|
|
207
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
208
|
+
const maxIterations = options.maxIterations ?? 1e8;
|
|
209
|
+
if (difficulty <= 0) {
|
|
210
|
+
return "0";
|
|
211
|
+
}
|
|
212
|
+
for (let nonce = 0; nonce < maxIterations; nonce++) {
|
|
213
|
+
const candidate = String(nonce);
|
|
214
|
+
if (meetsDifficulty(token, candidate, difficulty, algorithm)) {
|
|
215
|
+
return candidate;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
throw new Error(`Proof-of-work not solved within ${maxIterations} iterations (difficulty ${difficulty})`);
|
|
219
|
+
};
|
|
220
|
+
//# sourceMappingURL=core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../src/pow/core.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAcH,MAAM,CAAC,GAAG,IAAI,WAAW,CAAC;IACxB,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU;IACtE,UAAU,EAAE,UAAU,EAAE,UAAU,E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verification
|
|
3
|
+
* plus a short-TTL replay cache. Challenge verification is cryptographically
|
|
4
|
+
* stateless (HMAC); only one-time-use enforcement touches the (KV or memory)
|
|
5
|
+
* replay cache.
|
|
6
|
+
*/
|
|
7
|
+
import type { KVAdapter } from "../kv/index.js";
|
|
8
|
+
import { type ChallengePayload, type PowAlgorithm } from "./core.js";
|
|
9
|
+
export declare const POW_HEADER: {
|
|
10
|
+
readonly challenge: "Covara-PoW-Challenge";
|
|
11
|
+
readonly difficulty: "Covara-PoW-Difficulty";
|
|
12
|
+
readonly algorithm: "Covara-PoW-Algorithm";
|
|
13
|
+
readonly nonce: "Covara-PoW-Nonce";
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the HMAC secret: explicit config -> COVARA_POW_SECRET env -> a random
|
|
17
|
+
* dev-only secret (warns; a random per-process secret breaks verification
|
|
18
|
+
* across processes, so production must set one explicitly).
|
|
19
|
+
*/
|
|
20
|
+
export declare const resolvePowSecret: (configured?: string) => string;
|
|
21
|
+
export declare const resetPowSecretForTests: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* Bind a challenge to the exact request: method + path(+query) + a hash of the
|
|
24
|
+
* raw body. The client re-sends the identical request, so the fingerprint
|
|
25
|
+
* recomputes to the same value on the retry.
|
|
26
|
+
*/
|
|
27
|
+
export declare const computeFingerprint: (method: string, pathWithQuery: string, bodyText?: string) => string;
|
|
28
|
+
export interface IssueChallengeOptions {
|
|
29
|
+
secret: string;
|
|
30
|
+
difficulty: number;
|
|
31
|
+
fingerprint: string;
|
|
32
|
+
ttlMs?: number;
|
|
33
|
+
algorithm?: PowAlgorithm;
|
|
34
|
+
now?: number;
|
|
35
|
+
nonce?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface IssuedChallenge {
|
|
38
|
+
token: string;
|
|
39
|
+
difficulty: number;
|
|
40
|
+
algorithm: PowAlgorithm;
|
|
41
|
+
expiresAt: number;
|
|
42
|
+
}
|
|
43
|
+
export declare const issueChallenge: (options: IssueChallengeOptions) => IssuedChallenge;
|
|
44
|
+
export type VerifyFailure = "missing" | "malformed" | "bad_signature" | "expired" | "fingerprint_mismatch" | "insufficient_difficulty" | "replayed";
|
|
45
|
+
export interface VerifyResult {
|
|
46
|
+
ok: boolean;
|
|
47
|
+
reason?: VerifyFailure;
|
|
48
|
+
payload?: ChallengePayload;
|
|
49
|
+
}
|
|
50
|
+
export interface VerifySolutionOptions {
|
|
51
|
+
secret: string;
|
|
52
|
+
token: string | undefined | null;
|
|
53
|
+
nonce: string | undefined | null;
|
|
54
|
+
fingerprint: string;
|
|
55
|
+
now?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Stateless verification of a submitted solution. Does NOT consult the replay
|
|
59
|
+
* cache — call `consumeNonce` separately once verification passes so a failed
|
|
60
|
+
* verification never burns a nonce.
|
|
61
|
+
*/
|
|
62
|
+
export declare const verifySolution: (options: VerifySolutionOptions) => VerifyResult;
|
|
63
|
+
/**
|
|
64
|
+
* Atomically record a nonce as consumed. Returns true when the nonce is fresh
|
|
65
|
+
* (first use) and false when it has already been used within its TTL window.
|
|
66
|
+
* Uses an atomic INCR on the global KV (or an injected store); falls back to an
|
|
67
|
+
* in-process map when no KV is configured.
|
|
68
|
+
*/
|
|
69
|
+
export declare const consumeNonce: (nonce: string, expiresAt: number, store?: KVAdapter, now?: number) => Promise<boolean>;
|
|
70
|
+
export declare const clearReplayCacheForTests: () => void;
|
|
71
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/pow/server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAItC,OAAO,EAOL,KAAK,gBAAgB,EACrB,KAAK,YAAY,EAClB,MAAM,QAAQ,CAAC;AAEhB,eAAO,MAAM,UAAU;;;;;CAKb,CAAC;AAQX;;;;GAIG;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BAAqB,KAAG,YAiC/D,CAAC;AAsBF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GACvB,OAAO,MAAM,EACb,WAAW,MAAM,EACjB,QAAQ,SAAS,EACjB,YAAgB,KACf,OAAO,CAAC,OAAO,CAmBjB,CAAC;AAEF,eAAO,MAAM,wBAAwB,QAAO,IAM3C,CAAC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side proof-of-work: stateless signed challenge issuance + verification
|
|
3
|
+
* plus a short-TTL replay cache. Challenge verification is cryptographically
|
|
4
|
+
* stateless (HMAC); only one-time-use enforcement touches the (KV or memory)
|
|
5
|
+
* replay cache.
|
|
6
|
+
*/
|
|
7
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
8
|
+
import { getGlobalKV, hasGlobalKV } from "../kv/index.js";
|
|
9
|
+
import { getLogger } from "../server/logger.js";
|
|
10
|
+
import { readEnv } from "../server/env.js";
|
|
11
|
+
import { base64UrlEncodeBytes, decodeChallengePayload, encodeChallengePayload, meetsDifficulty, sha256Hex, utf8Bytes, } from "./core.js";
|
|
12
|
+
export const POW_HEADER = {
|
|
13
|
+
challenge: "Covara-PoW-Challenge",
|
|
14
|
+
difficulty: "Covara-PoW-Difficulty",
|
|
15
|
+
algorithm: "Covara-PoW-Algorithm",
|
|
16
|
+
nonce: "Covara-PoW-Nonce",
|
|
17
|
+
};
|
|
18
|
+
const CHALLENGE_VERSION = 1;
|
|
19
|
+
const REPLAY_PREFIX = "covara:pow:used:";
|
|
20
|
+
const DEFAULT_TTL_MS = 120000;
|
|
21
|
+
let resolvedSecret = null;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the HMAC secret: explicit config -> COVARA_POW_SECRET env -> a random
|
|
24
|
+
* dev-only secret (warns; a random per-process secret breaks verification
|
|
25
|
+
* across processes, so production must set one explicitly).
|
|
26
|
+
*/
|
|
27
|
+
export const resolvePowSecret = (configured) => {
|
|
28
|
+
if (configured)
|
|
29
|
+
return configured;
|
|
30
|
+
const fromEnv = readEnv("COVARA_POW_SECRET");
|
|
31
|
+
if (fromEnv)
|
|
32
|
+
return fromEnv;
|
|
33
|
+
if (!resolvedSecret) {
|
|
34
|
+
resolvedSecret = randomBytes(32).toString("hex");
|
|
35
|
+
getLogger().warn("Proof-of-work secret not configured; generated a random per-process secret. " +
|
|
36
|
+
"Set COVARA_POW_SECRET (or abuseProtection.pow.secret) so challenges verify across processes/restarts.");
|
|
37
|
+
}
|
|
38
|
+
return resolvedSecret;
|
|
39
|
+
};
|
|
40
|
+
export const resetPowSecretForTests = () => {
|
|
41
|
+
resolvedSecret = null;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Bind a challenge to the exact request: method + path(+query) + a hash of the
|
|
45
|
+
* raw body. The client re-sends the identical request, so the fingerprint
|
|
46
|
+
* recomputes to the same value on the retry.
|
|
47
|
+
*/
|
|
48
|
+
export const computeFingerprint = (method, pathWithQuery, bodyText = "") => sha256Hex(`${method.toUpperCase()}\n${pathWithQuery}\n${sha256Hex(bodyText)}`);
|
|
49
|
+
const sign = (secret, payloadEncoded) => base64UrlEncodeBytes(new Uint8Array(createHmac("sha256", secret).update(payloadEncoded).digest()));
|
|
50
|
+
export const issueChallenge = (options) => {
|
|
51
|
+
const now = options.now ?? Date.now();
|
|
52
|
+
const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
53
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
54
|
+
const payload = {
|
|
55
|
+
v: CHALLENGE_VERSION,
|
|
56
|
+
d: options.difficulty,
|
|
57
|
+
fp: options.fingerprint,
|
|
58
|
+
iat: now,
|
|
59
|
+
exp: now + ttlMs,
|
|
60
|
+
n: options.nonce ?? randomBytes(16).toString("hex"),
|
|
61
|
+
alg: algorithm,
|
|
62
|
+
};
|
|
63
|
+
const encoded = encodeChallengePayload(payload);
|
|
64
|
+
const token = `${encoded}.${sign(options.secret, encoded)}`;
|
|
65
|
+
return { token, difficulty: options.difficulty, algorithm, expiresAt: payload.exp };
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Stateless verification of a submitted solution. Does NOT consult the replay
|
|
69
|
+
* cache — call `consumeNonce` separately once verification passes so a failed
|
|
70
|
+
* verification never burns a nonce.
|
|
71
|
+
*/
|
|
72
|
+
export const verifySolution = (options) => {
|
|
73
|
+
const { secret, token, nonce, fingerprint } = options;
|
|
74
|
+
const now = options.now ?? Date.now();
|
|
75
|
+
if (!token || !nonce)
|
|
76
|
+
return { ok: false, reason: "missing" };
|
|
77
|
+
const dot = token.indexOf(".");
|
|
78
|
+
if (dot <= 0 || dot === token.length - 1)
|
|
79
|
+
return { ok: false, reason: "malformed" };
|
|
80
|
+
const encoded = token.slice(0, dot);
|
|
81
|
+
const providedSig = token.slice(dot + 1);
|
|
82
|
+
let payload;
|
|
83
|
+
try {
|
|
84
|
+
payload = decodeChallengePayload(encoded);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return { ok: false, reason: "malformed" };
|
|
88
|
+
}
|
|
89
|
+
const expectedSig = sign(secret, encoded);
|
|
90
|
+
const a = utf8Bytes(providedSig);
|
|
91
|
+
const b = utf8Bytes(expectedSig);
|
|
92
|
+
if (a.length !== b.length || !timingSafeEqual(a, b)) {
|
|
93
|
+
return { ok: false, reason: "bad_signature" };
|
|
94
|
+
}
|
|
95
|
+
if (payload.exp <= now)
|
|
96
|
+
return { ok: false, reason: "expired" };
|
|
97
|
+
if (payload.fp !== fingerprint)
|
|
98
|
+
return { ok: false, reason: "fingerprint_mismatch" };
|
|
99
|
+
if (!meetsDifficulty(token, nonce, payload.d, payload.alg)) {
|
|
100
|
+
return { ok: false, reason: "insufficient_difficulty" };
|
|
101
|
+
}
|
|
102
|
+
return { ok: true, payload };
|
|
103
|
+
};
|
|
104
|
+
const memoryReplay = new Map();
|
|
105
|
+
let memoryCleanup = null;
|
|
106
|
+
const ensureMemoryCleanup = () => {
|
|
107
|
+
if (memoryCleanup)
|
|
108
|
+
return;
|
|
109
|
+
memoryCleanup = setInterval(() => {
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
for (const [key, entry] of memoryReplay) {
|
|
112
|
+
if (entry.expiresAt <= now)
|
|
113
|
+
memoryReplay.delete(key);
|
|
114
|
+
}
|
|
115
|
+
}, 60000);
|
|
116
|
+
if (typeof memoryCleanup === "object" && "unref" in memoryCleanup) {
|
|
117
|
+
memoryCleanup.unref();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Atomically record a nonce as consumed. Returns true when the nonce is fresh
|
|
122
|
+
* (first use) and false when it has already been used within its TTL window.
|
|
123
|
+
* Uses an atomic INCR on the global KV (or an injected store); falls back to an
|
|
124
|
+
* in-process map when no KV is configured.
|
|
125
|
+
*/
|
|
126
|
+
export const consumeNonce = async (nonce, expiresAt, store, now = Date.now()) => {
|
|
127
|
+
const ttlSeconds = Math.max(1, Math.ceil((expiresAt - now) / 1000));
|
|
128
|
+
const kv = store ?? (hasGlobalKV() ? getGlobalKV() : null);
|
|
129
|
+
if (!kv) {
|
|
130
|
+
ensureMemoryCleanup();
|
|
131
|
+
const existing = memoryReplay.get(nonce);
|
|
132
|
+
if (existing && existing.expiresAt > now)
|
|
133
|
+
return false;
|
|
134
|
+
memoryReplay.set(nonce, { expiresAt });
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
const key = `${REPLAY_PREFIX}${nonce}`;
|
|
138
|
+
const count = await kv.incr(key);
|
|
139
|
+
if (count === 1) {
|
|
140
|
+
await kv.expire(key, ttlSeconds);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
export const clearReplayCacheForTests = () => {
|
|
146
|
+
memoryReplay.clear();
|
|
147
|
+
if (memoryCleanup) {
|
|
148
|
+
clearInterval(memoryCleanup);
|
|
149
|
+
memoryCleanup = null;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+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M,wBAAwB,GAAG,GAAS,EAAE;IACjD,YAAY,CAAC,KAAK,EAAE,CAAC;IACrB,IAAI,aAAa,EAAE,CAAC;QAClB,aAAa,CAAC,aAAa,CAAC,CAAC;QAC7B,aAAa,GAAG,IAAI,CAAC;IACvB,CAAC;AACH,CAAC,CAAC"}
|
package/dist/resource/error.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export declare const ERROR_TYPES: {
|
|
|
4
4
|
readonly NOT_FOUND: "/__covara/problems/not-found";
|
|
5
5
|
readonly VALIDATION_ERROR: "/__covara/problems/validation-error";
|
|
6
6
|
readonly RATE_LIMIT_EXCEEDED: "/__covara/problems/rate-limit-exceeded";
|
|
7
|
+
readonly PROOF_OF_WORK_REQUIRED: "/__covara/problems/proof-of-work-required";
|
|
8
|
+
readonly CAPTCHA_REQUIRED: "/__covara/problems/captcha-required";
|
|
7
9
|
readonly FILTER_PARSE_ERROR: "/__covara/problems/filter-parse-error";
|
|
8
10
|
readonly UNAUTHORIZED: "/__covara/problems/unauthorized";
|
|
9
11
|
readonly FORBIDDEN: "/__covara/problems/forbidden";
|
|
@@ -59,6 +61,18 @@ export declare class RateLimitError extends ResourceError {
|
|
|
59
61
|
retryAfter: number;
|
|
60
62
|
constructor(retryAfter: number, message?: string);
|
|
61
63
|
}
|
|
64
|
+
export declare class PowRequiredError extends ResourceError {
|
|
65
|
+
challenge: string;
|
|
66
|
+
difficulty: number;
|
|
67
|
+
algorithm: string;
|
|
68
|
+
constructor(challenge: string, difficulty: number, algorithm?: string, message?: string);
|
|
69
|
+
}
|
|
70
|
+
export declare class CaptchaRequiredError extends ResourceError {
|
|
71
|
+
provider: string;
|
|
72
|
+
siteKey?: string | undefined;
|
|
73
|
+
action?: string | undefined;
|
|
74
|
+
constructor(provider: string, siteKey?: string | undefined, action?: string | undefined, message?: string);
|
|
75
|
+
}
|
|
62
76
|
export declare class UnauthorizedError extends ResourceError {
|
|
63
77
|
constructor(message?: string);
|
|
64
78
|
}
|
|
@@ -98,6 +112,9 @@ export declare class SearchNotConfiguredError extends ResourceError {
|
|
|
98
112
|
export declare class SearchError extends ResourceError {
|
|
99
113
|
constructor(message: string, details?: unknown);
|
|
100
114
|
}
|
|
115
|
+
export declare class RelationPathNotInMemoryError extends Error {
|
|
116
|
+
constructor(field: string);
|
|
117
|
+
}
|
|
101
118
|
export declare const formatZodError: (error: ZodError) => FieldError[];
|
|
102
119
|
export declare const formatRFC7807Error: (error: unknown, requestId?: string) => ProblemDetail;
|
|
103
120
|
export declare const formatErrorResponse: (error: unknown) => {
|