@orpc/server 2.0.0-beta.49 → 2.0.0-beta.50
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/adapters/aws-lambda/handler.d.ts +17 -15
- package/dist/adapters/aws-lambda/handler.js +23 -23
- package/dist/adapters/aws-lambda/index.d.ts +4 -16
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +9 -9
- package/dist/adapters/aws-lambda/rpc-handler.js +20 -17
- package/dist/adapters/crossws/handler.d.ts +44 -42
- package/dist/adapters/crossws/handler.js +53 -60
- package/dist/adapters/crossws/index.d.ts +3 -2
- package/dist/adapters/crossws/index.js +2 -2
- package/dist/adapters/crossws/rpc-handler.d.ts +10 -10
- package/dist/adapters/crossws/rpc-handler.js +17 -14
- package/dist/adapters/fastify/handler.d.ts +17 -15
- package/dist/adapters/fastify/handler.js +20 -20
- package/dist/adapters/fastify/index.d.ts +3 -2
- package/dist/adapters/fastify/index.js +2 -2
- package/dist/adapters/fastify/rpc-handler.d.ts +9 -9
- package/dist/adapters/fastify/rpc-handler.js +16 -13
- package/dist/adapters/fetch/handler.d.ts +19 -17
- package/dist/adapters/fetch/handler.js +22 -22
- package/dist/adapters/fetch/index.d.ts +4 -7
- package/dist/adapters/fetch/index.js +3 -7
- package/dist/adapters/fetch/rpc-handler.d.ts +9 -9
- package/dist/adapters/fetch/rpc-handler.js +18 -15
- package/dist/adapters/message-port/handler.d.ts +58 -60
- package/dist/adapters/message-port/handler.js +72 -86
- package/dist/adapters/message-port/index.d.ts +3 -2
- package/dist/adapters/message-port/index.js +2 -2
- package/dist/adapters/message-port/rpc-handler.d.ts +9 -9
- package/dist/adapters/message-port/rpc-handler.js +17 -14
- package/dist/adapters/node/handler.d.ts +17 -15
- package/dist/adapters/node/handler.js +20 -20
- package/dist/adapters/node/index.d.ts +4 -7
- package/dist/adapters/node/index.js +3 -7
- package/dist/adapters/node/rpc-handler.d.ts +9 -9
- package/dist/adapters/node/rpc-handler.js +17 -14
- package/dist/adapters/standard/codec.d.ts +13 -13
- package/dist/adapters/standard/handler.d.ts +49 -49
- package/dist/adapters/standard/handler.js +205 -205
- package/dist/adapters/standard/index.d.ts +7 -6
- package/dist/adapters/standard/index.js +4 -5
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-handler-codec.d.ts +39 -43
- package/dist/adapters/standard/rpc-handler-codec.js +50 -51
- package/dist/adapters/standard/rpc-matcher.d.ts +40 -40
- package/dist/adapters/standard/rpc-matcher.js +103 -149
- package/dist/adapters/standard/utils.d.ts +8 -8
- package/dist/adapters/standard/utils.js +7 -5
- package/dist/adapters/standard-peer/index.d.ts +2 -1
- package/dist/adapters/standard-peer/index.js +1 -1
- package/dist/adapters/standard-peer/utils.d.ts +6 -4
- package/dist/adapters/standard-peer/utils.js +12 -6
- package/dist/adapters/websocket/handler.d.ts +52 -50
- package/dist/adapters/websocket/handler.js +75 -85
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-handler.d.ts +9 -9
- package/dist/adapters/websocket/rpc-handler.js +17 -14
- package/dist/builder-variants.d.ts +195 -195
- package/dist/builder.d.ts +103 -104
- package/dist/builder.js +198 -224
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +4 -2
- package/dist/context.d.ts +3 -3
- package/dist/extensions/callable.d.ts +11 -11
- package/dist/extensions/callable.js +6 -5
- package/dist/helpers/base64url.d.ts +1 -1
- package/dist/helpers/base64url.js +44 -49
- package/dist/helpers/cookie.d.ts +10 -11
- package/dist/helpers/cookie.js +56 -63
- package/dist/helpers/encryption.d.ts +1 -1
- package/dist/helpers/encryption.js +80 -70
- package/dist/helpers/index.d.ts +5 -4
- package/dist/helpers/index.js +4 -4
- package/dist/helpers/signing.d.ts +1 -1
- package/dist/helpers/signing.js +72 -82
- package/dist/implementer-procedure.d.ts +12 -13
- package/dist/implementer-procedure.js +37 -44
- package/dist/implementer-router.d.ts +30 -36
- package/dist/implementer-router.js +92 -111
- package/dist/implementer.d.ts +8 -8
- package/dist/implementer.js +28 -35
- package/dist/index.d.ts +22 -61
- package/dist/index.js +16 -48
- package/dist/lazy.d.ts +14 -14
- package/dist/lazy.js +19 -30
- package/dist/middleware-decorated.d.ts +8 -9
- package/dist/middleware-decorated.js +66 -70
- package/dist/middleware.d.ts +34 -34
- package/dist/plugins/batch.d.ts +75 -75
- package/dist/plugins/batch.js +345 -379
- package/dist/plugins/cors.d.ts +60 -64
- package/dist/plugins/cors.js +118 -111
- package/dist/plugins/get-method-csrf-protection.d.ts +8 -6
- package/dist/plugins/get-method-csrf-protection.js +47 -63
- package/dist/plugins/head-method.d.ts +11 -9
- package/dist/plugins/head-method.js +97 -94
- package/dist/plugins/index.d.ts +15 -39
- package/dist/plugins/index.js +14 -29
- package/dist/plugins/method-override.d.ts +30 -28
- package/dist/plugins/method-override.js +53 -49
- package/dist/plugins/request-compression.d.ts +11 -9
- package/dist/plugins/request-compression.js +52 -79
- package/dist/plugins/request-headers.d.ts +10 -8
- package/dist/plugins/request-headers.js +27 -29
- package/dist/plugins/request-limit.d.ts +21 -19
- package/dist/plugins/request-limit.js +60 -67
- package/dist/plugins/response-compression.d.ts +42 -39
- package/dist/plugins/response-compression.js +150 -202
- package/dist/plugins/response-headers.d.ts +15 -13
- package/dist/plugins/response-headers.js +43 -46
- package/dist/plugins/rethrow.d.ts +25 -23
- package/dist/plugins/rethrow.js +46 -58
- package/dist/plugins/signal-sync.d.ts +7 -5
- package/dist/plugins/signal-sync.js +71 -86
- package/dist/plugins/timeout.d.ts +29 -27
- package/dist/plugins/timeout.js +61 -58
- package/dist/procedure-client.d.ts +14 -15
- package/dist/procedure-client.js +149 -166
- package/dist/procedure-decorated.d.ts +9 -9
- package/dist/procedure-decorated.js +36 -44
- package/dist/procedure-utils.d.ts +10 -30
- package/dist/procedure-utils.js +39 -40
- package/dist/procedure.d.ts +60 -60
- package/dist/procedure.js +18 -24
- package/dist/router-client.d.ts +9 -11
- package/dist/router-client.js +33 -41
- package/dist/router-hidden.js +10 -12
- package/dist/router-utils.d.ts +13 -30
- package/dist/router-utils.js +146 -191
- package/dist/router.d.ts +12 -26
- package/package.json +10 -10
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/builder-variants.js +0 -1
- package/dist/context.js +0 -1
- package/dist/middleware.js +0 -1
- package/dist/router-hidden.d.ts +0 -5
- package/dist/router.js +0 -1
package/dist/helpers/cookie.js
CHANGED
|
@@ -1,69 +1,62 @@
|
|
|
1
|
-
import { parseCookie, stringifySetCookie } from
|
|
1
|
+
import { parseCookie, stringifySetCookie } from "cookie";
|
|
2
|
+
// src/helpers/cookie.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
...options,
|
|
28
|
-
name, // prioritize
|
|
29
|
-
value, // prioritize
|
|
30
|
-
}, options);
|
|
31
|
-
headers.append('Set-Cookie', cookieString);
|
|
4
|
+
* Sets a cookie in the response headers.
|
|
5
|
+
*
|
|
6
|
+
* Does nothing if `headers` is `undefined`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const headers = new Headers()
|
|
11
|
+
*
|
|
12
|
+
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
13
|
+
*
|
|
14
|
+
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; Max-Age=3600; Path=/; HttpOnly')
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
18
|
+
*/
|
|
19
|
+
function setCookie(headers, name, value, options = {}) {
|
|
20
|
+
if (headers === void 0) return;
|
|
21
|
+
const cookieString = stringifySetCookie({
|
|
22
|
+
path: "/",
|
|
23
|
+
...options,
|
|
24
|
+
name,
|
|
25
|
+
value
|
|
26
|
+
}, options);
|
|
27
|
+
headers.append("Set-Cookie", cookieString);
|
|
32
28
|
}
|
|
33
29
|
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (cookieHeader === null) {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
return parseCookie(cookieHeader, options)[name];
|
|
30
|
+
* Gets a cookie value from request headers
|
|
31
|
+
*
|
|
32
|
+
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
37
|
+
*
|
|
38
|
+
* const sessionId = getCookie(headers, 'sessionId')
|
|
39
|
+
*
|
|
40
|
+
* expect(sessionId).toEqual('abc123')
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
44
|
+
*/
|
|
45
|
+
function getCookie(headers, name, options = {}) {
|
|
46
|
+
if (headers === void 0) return;
|
|
47
|
+
const cookieHeader = headers.get("cookie");
|
|
48
|
+
if (cookieHeader === null) return;
|
|
49
|
+
return parseCookie(cookieHeader, options)[name];
|
|
58
50
|
}
|
|
59
51
|
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
52
|
+
* Deletes a cookie by marking it expired.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
55
|
+
*/
|
|
56
|
+
function deleteCookie(headers, name, options = {}) {
|
|
57
|
+
return setCookie(headers, name, "", {
|
|
58
|
+
...options,
|
|
59
|
+
maxAge: 0
|
|
60
|
+
});
|
|
69
61
|
}
|
|
62
|
+
export { deleteCookie, getCookie, setCookie };
|
|
@@ -25,4 +25,4 @@ export declare function encrypt(value: string, secret: string): Promise<string>;
|
|
|
25
25
|
*
|
|
26
26
|
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
27
27
|
*/
|
|
28
|
-
export declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
28
|
+
export declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
@@ -1,80 +1,90 @@
|
|
|
1
|
-
import { decodeBase64url, encodeBase64url } from
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from "./base64url.js";
|
|
2
|
+
// src/helpers/encryption.ts
|
|
2
3
|
const PBKDF2_CONFIG = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
name: "PBKDF2",
|
|
5
|
+
/**
|
|
6
|
+
* Below the OWASP Password Storage Cheat Sheet recommendation of 600,000 iterations
|
|
7
|
+
* for PBKDF2-HMAC-SHA256. The output does not record the iteration count, so changing
|
|
8
|
+
* it would make existing ciphertexts undecryptable; raising it needs a versioned format.
|
|
9
|
+
*/
|
|
10
|
+
iterations: 6e4,
|
|
11
|
+
hash: "SHA-256"
|
|
11
12
|
};
|
|
12
13
|
const AES_GCM_CONFIG = {
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
name: "AES-GCM",
|
|
15
|
+
length: 256
|
|
15
16
|
};
|
|
16
17
|
const CRYPTO_CONSTANTS = {
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
SALT_LENGTH: 16,
|
|
19
|
+
IV_LENGTH: 12
|
|
19
20
|
};
|
|
20
21
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
22
|
+
* Encrypts a string using AES-GCM with a secret key.
|
|
23
|
+
* The output is base64url encoded to be URL-safe.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
28
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
29
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
33
|
+
*/
|
|
34
|
+
async function encrypt(value, secret) {
|
|
35
|
+
const encoder = new TextEncoder();
|
|
36
|
+
const data = encoder.encode(value);
|
|
37
|
+
const salt = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.SALT_LENGTH));
|
|
38
|
+
const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(secret), PBKDF2_CONFIG.name, false, ["deriveKey"]);
|
|
39
|
+
const key = await crypto.subtle.deriveKey({
|
|
40
|
+
...PBKDF2_CONFIG,
|
|
41
|
+
salt
|
|
42
|
+
}, keyMaterial, AES_GCM_CONFIG, false, ["encrypt"]);
|
|
43
|
+
const iv = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.IV_LENGTH));
|
|
44
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
45
|
+
name: AES_GCM_CONFIG.name,
|
|
46
|
+
iv
|
|
47
|
+
}, key, data);
|
|
48
|
+
const result = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
|
|
49
|
+
result.set(salt, 0);
|
|
50
|
+
result.set(iv, salt.length);
|
|
51
|
+
result.set(new Uint8Array(encrypted), salt.length + iv.length);
|
|
52
|
+
return encodeBase64url(result);
|
|
46
53
|
}
|
|
47
54
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
55
|
+
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
56
|
+
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
61
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
62
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
66
|
+
*/
|
|
67
|
+
async function decrypt(encrypted, secret) {
|
|
68
|
+
try {
|
|
69
|
+
const data = decodeBase64url(encrypted);
|
|
70
|
+
if (data === void 0) return;
|
|
71
|
+
const encoder = new TextEncoder();
|
|
72
|
+
const decoder = new TextDecoder();
|
|
73
|
+
const salt = data.slice(0, CRYPTO_CONSTANTS.SALT_LENGTH);
|
|
74
|
+
const iv = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH, CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
75
|
+
const encryptedData = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
76
|
+
const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(secret), PBKDF2_CONFIG.name, false, ["deriveKey"]);
|
|
77
|
+
const key = await crypto.subtle.deriveKey({
|
|
78
|
+
...PBKDF2_CONFIG,
|
|
79
|
+
salt
|
|
80
|
+
}, keyMaterial, AES_GCM_CONFIG, false, ["decrypt"]);
|
|
81
|
+
const decrypted = await crypto.subtle.decrypt({
|
|
82
|
+
name: AES_GCM_CONFIG.name,
|
|
83
|
+
iv
|
|
84
|
+
}, key, encryptedData);
|
|
85
|
+
return decoder.decode(decrypted);
|
|
86
|
+
} catch {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
80
89
|
}
|
|
90
|
+
export { decrypt, encrypt };
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from "./base64url.js";
|
|
2
|
+
import { GetCookieOptions, SetCookieOptions, deleteCookie, getCookie, setCookie } from "./cookie.js";
|
|
3
|
+
import { decrypt, encrypt } from "./encryption.js";
|
|
4
|
+
import { getSignedValue, sign, unsign } from "./signing.js";
|
|
5
|
+
export { GetCookieOptions, SetCookieOptions, decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
package/dist/helpers/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
export { decodeBase64url, encodeBase64url } from "./base64url.js";
|
|
2
|
+
export { deleteCookie, getCookie, setCookie } from "./cookie.js";
|
|
3
|
+
export { decrypt, encrypt } from "./encryption.js";
|
|
4
|
+
export { getSignedValue, sign, unsign } from "./signing.js";
|
|
@@ -47,4 +47,4 @@ export declare function unsign(signedValue: string | undefined | null, secret: s
|
|
|
47
47
|
*
|
|
48
48
|
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
49
49
|
*/
|
|
50
|
-
export declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
50
|
+
export declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
package/dist/helpers/signing.js
CHANGED
|
@@ -1,88 +1,78 @@
|
|
|
1
|
-
import { decodeBase64url, encodeBase64url } from
|
|
2
|
-
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from "./base64url.js";
|
|
2
|
+
// src/helpers/signing.ts
|
|
3
|
+
const ALGORITHM = {
|
|
4
|
+
name: "HMAC",
|
|
5
|
+
hash: "SHA-256"
|
|
6
|
+
};
|
|
3
7
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const signature = await crypto.subtle.sign(ALGORITHM, key, encoder.encode(value));
|
|
24
|
-
return `${value}.${encodeBase64url(new Uint8Array(signature))}`;
|
|
8
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
9
|
+
*
|
|
10
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
11
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
12
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
17
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
21
|
+
*/
|
|
22
|
+
async function sign(value, secret) {
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), ALGORITHM, false, ["sign"]);
|
|
25
|
+
const signature = await crypto.subtle.sign(ALGORITHM, key, encoder.encode(value));
|
|
26
|
+
return `${value}.${encodeBase64url(new Uint8Array(signature))}`;
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (signature === undefined) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
const encoder = new TextEncoder();
|
|
57
|
-
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), ALGORITHM, false, [
|
|
58
|
-
'verify',
|
|
59
|
-
]);
|
|
60
|
-
const isValid = await crypto.subtle.verify(ALGORITHM, key, signature, encoder.encode(value));
|
|
61
|
-
return isValid ? value : undefined;
|
|
29
|
+
* Verifies and extracts the original value from a signed string.
|
|
30
|
+
*
|
|
31
|
+
* This function validates the signature of a previously signed value using the same
|
|
32
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
33
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
38
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
39
|
+
* expect(originalValue).toEqual("user123")
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
43
|
+
*/
|
|
44
|
+
async function unsign(signedValue, secret) {
|
|
45
|
+
if (typeof signedValue !== "string") return;
|
|
46
|
+
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
47
|
+
if (lastDotIndex === -1) return;
|
|
48
|
+
const value = signedValue.slice(0, lastDotIndex);
|
|
49
|
+
const signatureBase64url = signedValue.slice(lastDotIndex + 1);
|
|
50
|
+
const signature = decodeBase64url(signatureBase64url);
|
|
51
|
+
if (signature === void 0) return;
|
|
52
|
+
const encoder = new TextEncoder();
|
|
53
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), ALGORITHM, false, ["verify"]);
|
|
54
|
+
return await crypto.subtle.verify(ALGORITHM, key, signature, encoder.encode(value)) ? value : void 0;
|
|
62
55
|
}
|
|
63
56
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (lastDotIndex === -1) {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
return signedValue.slice(0, lastDotIndex);
|
|
57
|
+
* Extracts the value part from a signed string without verification.
|
|
58
|
+
*
|
|
59
|
+
* This function simply extracts the original value from a signed string
|
|
60
|
+
* without performing any signature verification. It's useful when you need
|
|
61
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
66
|
+
* const value = getSignedValue(signedValue)
|
|
67
|
+
* expect(value).toEqual("user123")
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
71
|
+
*/
|
|
72
|
+
function getSignedValue(signedValue) {
|
|
73
|
+
if (typeof signedValue !== "string") return;
|
|
74
|
+
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
75
|
+
if (lastDotIndex === -1) return;
|
|
76
|
+
return signedValue.slice(0, lastDotIndex);
|
|
88
77
|
}
|
|
78
|
+
export { getSignedValue, sign, unsign };
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import { Procedure } from './procedure.ts';
|
|
1
|
+
import { BuilderDefinition } from "./builder.js";
|
|
2
|
+
import { Context, MergedContext, MergedInitialContext } from "./context.js";
|
|
3
|
+
import { Middleware } from "./middleware.js";
|
|
4
|
+
import { Procedure, ProcedureHandler } from "./procedure.js";
|
|
5
|
+
import { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorConstructorMap } from "@orpc/contract";
|
|
6
|
+
import { IntersectPick } from "@orpc/shared";
|
|
8
7
|
export declare class ProcedureImplementer<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
"~orpc": BuilderDefinition<TInputSchema, TOutputSchema, TErrorMap>;
|
|
9
|
+
"constructor"(definition: BuilderDefinition<TInputSchema, TOutputSchema, TErrorMap>);
|
|
10
|
+
"use"<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): ProcedureImplementer<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, TErrorMap>;
|
|
11
|
+
"handler"(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, ORPCErrorConstructorMap<TErrorMap>>): ImplementedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
13
12
|
}
|
|
14
13
|
export declare class ImplementedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap> {
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
use<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): ImplementedProcedure<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, TErrorMap>;
|
|
15
|
+
}
|
|
@@ -1,44 +1,37 @@
|
|
|
1
|
-
import { Procedure } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
inputSchemasLengthAtUse: this['~orpc'].inputSchemas?.length,
|
|
39
|
-
outputSchemasLengthAtUse: this['~orpc'].outputSchemas?.length,
|
|
40
|
-
},
|
|
41
|
-
],
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import { Procedure } from "./procedure.js";
|
|
2
|
+
// src/implementer-procedure.ts
|
|
3
|
+
var ProcedureImplementer = class ProcedureImplementer {
|
|
4
|
+
"~orpc";
|
|
5
|
+
"constructor"(definition) {
|
|
6
|
+
this["~orpc"] = definition;
|
|
7
|
+
}
|
|
8
|
+
"use"(middleware) {
|
|
9
|
+
return new ProcedureImplementer({
|
|
10
|
+
...this["~orpc"],
|
|
11
|
+
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
12
|
+
middleware,
|
|
13
|
+
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
14
|
+
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
15
|
+
}]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
"handler"(handler) {
|
|
19
|
+
return new ImplementedProcedure({
|
|
20
|
+
...this["~orpc"],
|
|
21
|
+
handler
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var ImplementedProcedure = class ImplementedProcedure extends Procedure {
|
|
26
|
+
use(middleware) {
|
|
27
|
+
return new ImplementedProcedure({
|
|
28
|
+
...this["~orpc"],
|
|
29
|
+
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
30
|
+
middleware,
|
|
31
|
+
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
32
|
+
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
33
|
+
}]
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export { ImplementedProcedure, ProcedureImplementer };
|