@orpc/server 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/adapters/aws-lambda/handler.d.mts +25 -0
- package/dist/adapters/aws-lambda/handler.mjs +21 -0
- package/dist/adapters/aws-lambda/index.d.mts +14 -49
- package/dist/adapters/aws-lambda/index.mjs +2 -39
- package/dist/adapters/aws-lambda/rpc-handler.d.mts +18 -0
- package/dist/adapters/aws-lambda/rpc-handler.mjs +10 -0
- package/dist/adapters/crossws/handler.d.mts +41 -0
- package/dist/adapters/crossws/handler.mjs +38 -0
- package/dist/adapters/crossws/index.d.mts +2 -63
- package/dist/adapters/crossws/index.mjs +2 -68
- package/dist/adapters/crossws/rpc-handler.d.mts +15 -0
- package/dist/adapters/crossws/rpc-handler.mjs +10 -0
- package/dist/adapters/fastify/handler.d.mts +21 -0
- package/dist/adapters/fastify/handler.mjs +20 -0
- package/dist/adapters/fastify/index.d.mts +2 -42
- package/dist/adapters/fastify/index.mjs +2 -39
- package/dist/adapters/fastify/rpc-handler.d.mts +14 -0
- package/dist/adapters/fastify/rpc-handler.mjs +10 -0
- package/dist/adapters/fetch/handler.d.mts +23 -0
- package/dist/adapters/fetch/handler.mjs +22 -0
- package/dist/adapters/fetch/index.d.mts +6 -46
- package/dist/adapters/fetch/index.mjs +4 -41
- package/dist/adapters/fetch/rpc-handler.d.mts +16 -0
- package/dist/adapters/fetch/rpc-handler.mjs +10 -0
- package/dist/adapters/message-port/handler.d.mts +57 -0
- package/dist/adapters/message-port/handler.mjs +62 -0
- package/dist/adapters/message-port/index.d.mts +2 -77
- package/dist/adapters/message-port/index.mjs +2 -87
- package/dist/adapters/message-port/rpc-handler.d.mts +15 -0
- package/dist/adapters/message-port/rpc-handler.mjs +10 -0
- package/dist/adapters/node/handler.d.mts +21 -0
- package/dist/adapters/node/handler.mjs +20 -0
- package/dist/adapters/node/index.d.mts +6 -43
- package/dist/adapters/node/index.mjs +4 -40
- package/dist/adapters/node/rpc-handler.d.mts +15 -0
- package/dist/adapters/node/rpc-handler.mjs +10 -0
- package/dist/adapters/standard/codec.d.mts +16 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/handler.d.mts +68 -0
- package/dist/adapters/standard/handler.mjs +185 -0
- package/dist/adapters/standard/index.d.mts +6 -95
- package/dist/adapters/standard/index.mjs +5 -8
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-handler-codec.d.mts +42 -0
- package/dist/adapters/standard/rpc-handler-codec.mjs +51 -0
- package/dist/adapters/standard/rpc-matcher.d.mts +53 -0
- package/dist/adapters/standard/rpc-matcher.mjs +140 -0
- package/dist/adapters/standard/utils.d.mts +10 -0
- package/dist/adapters/standard/utils.mjs +6 -0
- package/dist/adapters/standard-peer/index.d.mts +1 -11
- package/dist/adapters/standard-peer/index.mjs +1 -10
- package/dist/adapters/standard-peer/utils.d.mts +4 -0
- package/dist/adapters/standard-peer/utils.mjs +11 -0
- package/dist/adapters/websocket/handler.d.mts +55 -0
- package/dist/adapters/websocket/handler.mjs +55 -0
- package/dist/adapters/websocket/index.d.mts +2 -77
- package/dist/adapters/websocket/index.mjs +2 -87
- package/dist/adapters/websocket/rpc-handler.d.mts +15 -0
- package/dist/adapters/websocket/rpc-handler.mjs +10 -0
- package/dist/builder-variants.d.mts +267 -0
- package/dist/builder-variants.mjs +1 -0
- package/dist/builder.d.mts +141 -0
- package/dist/builder.mjs +145 -0
- package/dist/constants.d.mts +2 -0
- package/dist/constants.mjs +2 -0
- package/dist/context.d.mts +13 -0
- package/dist/context.mjs +1 -0
- package/dist/extensions/callable.d.mts +16 -9
- package/dist/extensions/callable.mjs +5 -9
- package/dist/helpers/base64url.d.mts +31 -0
- package/dist/helpers/base64url.mjs +25 -0
- package/dist/helpers/cookie.d.mts +50 -0
- package/dist/helpers/cookie.mjs +35 -0
- package/dist/helpers/encryption.d.mts +28 -0
- package/dist/helpers/encryption.mjs +62 -0
- package/dist/helpers/index.d.mts +4 -167
- package/dist/helpers/index.mjs +4 -200
- package/dist/helpers/signing.d.mts +50 -0
- package/dist/helpers/signing.mjs +43 -0
- package/dist/implementer-procedure.d.mts +36 -0
- package/dist/implementer-procedure.mjs +38 -0
- package/dist/implementer-router.d.mts +78 -0
- package/dist/implementer-router.mjs +77 -0
- package/dist/implementer.d.mts +22 -0
- package/dist/implementer.mjs +30 -0
- package/dist/index.d.mts +57 -476
- package/dist/index.mjs +25 -458
- package/dist/lazy.d.mts +20 -0
- package/dist/lazy.mjs +22 -0
- package/dist/middleware-decorated.d.mts +27 -0
- package/dist/middleware-decorated.mjs +65 -0
- package/dist/middleware.d.mts +70 -0
- package/dist/middleware.mjs +1 -0
- package/dist/plugins/batch.d.mts +104 -0
- package/dist/plugins/batch.mjs +373 -0
- package/dist/plugins/cors.d.mts +67 -0
- package/dist/plugins/cors.mjs +134 -0
- package/dist/plugins/get-method-csrf-protection.d.mts +23 -0
- package/dist/plugins/get-method-csrf-protection.mjs +45 -0
- package/dist/plugins/head-method.d.mts +23 -0
- package/dist/plugins/head-method.mjs +95 -0
- package/dist/plugins/index.d.mts +39 -432
- package/dist/plugins/index.mjs +23 -965
- package/dist/plugins/method-override.d.mts +38 -0
- package/dist/plugins/method-override.mjs +50 -0
- package/dist/plugins/request-compression.d.mts +16 -0
- package/dist/plugins/request-compression.mjs +67 -0
- package/dist/plugins/request-headers.d.mts +22 -0
- package/dist/plugins/request-headers.mjs +21 -0
- package/dist/plugins/request-limit.d.mts +30 -0
- package/dist/plugins/request-limit.mjs +53 -0
- package/dist/plugins/response-compression.d.mts +59 -0
- package/dist/plugins/response-compression.mjs +172 -0
- package/dist/plugins/response-headers.d.mts +30 -0
- package/dist/plugins/response-headers.mjs +34 -0
- package/dist/plugins/rethrow.d.mts +33 -0
- package/dist/plugins/rethrow.mjs +46 -0
- package/dist/plugins/signal-sync.d.mts +13 -0
- package/dist/plugins/signal-sync.mjs +84 -0
- package/dist/plugins/timeout.d.mts +37 -0
- package/dist/plugins/timeout.mjs +56 -0
- package/dist/procedure-client.d.mts +41 -0
- package/dist/procedure-client.mjs +157 -0
- package/dist/procedure-decorated.d.mts +20 -0
- package/dist/procedure-decorated.mjs +41 -0
- package/dist/procedure-utils.d.mts +52 -0
- package/dist/procedure-utils.mjs +33 -0
- package/dist/procedure.d.mts +93 -0
- package/dist/procedure.mjs +20 -0
- package/dist/router-client.d.mts +27 -0
- package/dist/router-client.mjs +35 -0
- package/dist/router-hidden.d.mts +5 -0
- package/dist/router-hidden.mjs +12 -0
- package/dist/router-utils.d.mts +59 -0
- package/dist/router-utils.mjs +191 -0
- package/dist/router.d.mts +61 -0
- package/dist/router.mjs +1 -0
- package/package.json +11 -11
- package/dist/adapters/aws-lambda/index.d.ts +0 -51
- package/dist/adapters/crossws/index.d.ts +0 -63
- package/dist/adapters/fastify/index.d.ts +0 -42
- package/dist/adapters/fetch/index.d.ts +0 -47
- package/dist/adapters/message-port/index.d.ts +0 -77
- package/dist/adapters/node/index.d.ts +0 -44
- package/dist/adapters/standard/index.d.ts +0 -95
- package/dist/adapters/standard-peer/index.d.ts +0 -11
- package/dist/adapters/websocket/index.d.ts +0 -77
- package/dist/extensions/callable.d.ts +0 -10
- package/dist/helpers/index.d.ts +0 -167
- package/dist/index.d.ts +0 -476
- package/dist/plugins/index.d.ts +0 -432
- package/dist/shared/server.1ExWFMze.mjs +0 -69
- package/dist/shared/server.9B1jEm5a.d.mts +0 -105
- package/dist/shared/server.BB464y-6.mjs +0 -207
- package/dist/shared/server.BC5BIu02.d.mts +0 -73
- package/dist/shared/server.BSYGskId.d.ts +0 -14
- package/dist/shared/server.BvG3boBj.d.mts +0 -14
- package/dist/shared/server.CTV_mC2P.d.mts +0 -34
- package/dist/shared/server.CYbIpHbH.mjs +0 -239
- package/dist/shared/server.CgBF3Vj1.d.ts +0 -34
- package/dist/shared/server.Chq5FpXf.mjs +0 -353
- package/dist/shared/server.DFPxP2v3.d.mts +0 -166
- package/dist/shared/server.DFPxP2v3.d.ts +0 -166
- package/dist/shared/server.DOg7ke6y.d.ts +0 -105
- package/dist/shared/server.Dz7j9BEh.d.ts +0 -73
- package/dist/shared/server.W91HSRkE.mjs +0 -9
- package/dist/shared/server.sBQSgqK3.mjs +0 -41
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { encodeBase64 } from "@orpc/shared";
|
|
2
|
+
|
|
3
|
+
export function encodeBase64url(data) {
|
|
4
|
+
return encodeBase64(data).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function decodeBase64url(base64url) {
|
|
8
|
+
try {
|
|
9
|
+
if (typeof base64url !== "string") {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
let base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
13
|
+
while (base64.length % 4) {
|
|
14
|
+
base64 += "=";
|
|
15
|
+
}
|
|
16
|
+
const binaryString = atob(base64);
|
|
17
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
18
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
19
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
20
|
+
}
|
|
21
|
+
return encodeBase64url(bytes) === base64url ? bytes : undefined;
|
|
22
|
+
} catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ParseOptions, SetCookie, StringifyOptions } from "cookie";
|
|
2
|
+
export interface SetCookieOptions extends Omit<SetCookie, "name" | "value">, StringifyOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
5
|
+
*
|
|
6
|
+
* @default '/'
|
|
7
|
+
*/
|
|
8
|
+
path?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Sets a cookie in the response headers.
|
|
12
|
+
*
|
|
13
|
+
* Does nothing if `headers` is `undefined`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const headers = new Headers()
|
|
18
|
+
*
|
|
19
|
+
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
20
|
+
*
|
|
21
|
+
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; Max-Age=3600; Path=/; HttpOnly')
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
25
|
+
*/
|
|
26
|
+
export declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
|
|
27
|
+
export interface GetCookieOptions extends ParseOptions {}
|
|
28
|
+
/**
|
|
29
|
+
* Gets a cookie value from request headers
|
|
30
|
+
*
|
|
31
|
+
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
36
|
+
*
|
|
37
|
+
* const sessionId = getCookie(headers, 'sessionId')
|
|
38
|
+
*
|
|
39
|
+
* expect(sessionId).toEqual('abc123')
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
43
|
+
*/
|
|
44
|
+
export declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Deletes a cookie by marking it expired.
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
49
|
+
*/
|
|
50
|
+
export declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, "maxAge">): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parseCookie, stringifySetCookie } from "cookie";
|
|
2
|
+
|
|
3
|
+
export function setCookie(headers, name, value, options = {}) {
|
|
4
|
+
if (headers === undefined) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const cookieString = stringifySetCookie({
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
path: "/",
|
|
12
|
+
...options,
|
|
13
|
+
name,
|
|
14
|
+
value
|
|
15
|
+
}, options);
|
|
16
|
+
headers.append("Set-Cookie", cookieString);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getCookie(headers, name, options = {}) {
|
|
20
|
+
if (headers === undefined) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const cookieHeader = headers.get("cookie");
|
|
24
|
+
if (cookieHeader === null) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return parseCookie(cookieHeader, options)[name];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function deleteCookie(headers, name, options = {}) {
|
|
31
|
+
return setCookie(headers, name, "", {
|
|
32
|
+
...options,
|
|
33
|
+
maxAge: 0
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encrypts a string using AES-GCM with a secret key.
|
|
3
|
+
* The output is base64url encoded to be URL-safe.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
8
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
9
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
13
|
+
*/
|
|
14
|
+
export declare function encrypt(value: string, secret: string): Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
17
|
+
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
22
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
23
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
27
|
+
*/
|
|
28
|
+
export declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from "./base64url.mjs";
|
|
2
|
+
const PBKDF2_CONFIG = {
|
|
3
|
+
name: "PBKDF2",
|
|
4
|
+
iterations: 6e4,
|
|
5
|
+
hash: "SHA-256"
|
|
6
|
+
};
|
|
7
|
+
const AES_GCM_CONFIG = {
|
|
8
|
+
name: "AES-GCM",
|
|
9
|
+
length: 256
|
|
10
|
+
};
|
|
11
|
+
const CRYPTO_CONSTANTS = {
|
|
12
|
+
SALT_LENGTH: 16,
|
|
13
|
+
IV_LENGTH: 12
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export async function encrypt(value, secret) {
|
|
17
|
+
const encoder = new TextEncoder();
|
|
18
|
+
const data = encoder.encode(value);
|
|
19
|
+
const salt = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.SALT_LENGTH));
|
|
20
|
+
const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(secret), PBKDF2_CONFIG.name, false, ["deriveKey"]);
|
|
21
|
+
const key = await crypto.subtle.deriveKey({
|
|
22
|
+
...PBKDF2_CONFIG,
|
|
23
|
+
salt
|
|
24
|
+
}, keyMaterial, AES_GCM_CONFIG, false, ["encrypt"]);
|
|
25
|
+
const iv = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.IV_LENGTH));
|
|
26
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
27
|
+
name: AES_GCM_CONFIG.name,
|
|
28
|
+
iv
|
|
29
|
+
}, key, data);
|
|
30
|
+
const result = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
|
|
31
|
+
result.set(salt, 0);
|
|
32
|
+
result.set(iv, salt.length);
|
|
33
|
+
result.set(new Uint8Array(encrypted), salt.length + iv.length);
|
|
34
|
+
return encodeBase64url(result);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function decrypt(encrypted, secret) {
|
|
38
|
+
try {
|
|
39
|
+
const data = decodeBase64url(encrypted);
|
|
40
|
+
if (data === undefined) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const encoder = new TextEncoder();
|
|
44
|
+
const decoder = new TextDecoder();
|
|
45
|
+
const salt = data.slice(0, CRYPTO_CONSTANTS.SALT_LENGTH);
|
|
46
|
+
const iv = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH, CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
47
|
+
const encryptedData = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
48
|
+
const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(secret), PBKDF2_CONFIG.name, false, ["deriveKey"]);
|
|
49
|
+
const key = await crypto.subtle.deriveKey({
|
|
50
|
+
...PBKDF2_CONFIG,
|
|
51
|
+
salt
|
|
52
|
+
}, keyMaterial, AES_GCM_CONFIG, false, ["decrypt"]);
|
|
53
|
+
const decrypted = await crypto.subtle.decrypt({
|
|
54
|
+
name: AES_GCM_CONFIG.name,
|
|
55
|
+
iv
|
|
56
|
+
}, key, encryptedData);
|
|
57
|
+
return decoder.decode(decrypted);
|
|
58
|
+
} catch {
|
|
59
|
+
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/dist/helpers/index.d.mts
CHANGED
|
@@ -1,167 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* Base64url is URL-safe and doesn't use padding
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* const text = "Hello World"
|
|
10
|
-
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
11
|
-
* const decoded = decodeBase64url(encoded)
|
|
12
|
-
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
13
|
-
* ```
|
|
14
|
-
*
|
|
15
|
-
* @see {@link https://orpc.dev/docs/helpers/base64url | Base64Url Helpers}
|
|
16
|
-
*/
|
|
17
|
-
declare function encodeBase64url(data: Uint8Array): string;
|
|
18
|
-
/**
|
|
19
|
-
* Decodes a base64url string to Uint8Array
|
|
20
|
-
* Only accepts canonical, unpadded base64url (the form produced by `encodeBase64url`),
|
|
21
|
-
* returns undefined for anything else, including padded or standard base64 input
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* const text = "Hello World"
|
|
26
|
-
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
27
|
-
* const decoded = decodeBase64url(encoded)
|
|
28
|
-
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @see {@link https://orpc.dev/docs/helpers/base64url | Base64Url Helpers}
|
|
32
|
-
*/
|
|
33
|
-
declare function decodeBase64url(base64url: string | undefined | null): Uint8Array<ArrayBuffer> | undefined;
|
|
34
|
-
|
|
35
|
-
interface SetCookieOptions extends Omit<SetCookie, 'name' | 'value'>, StringifyOptions {
|
|
36
|
-
/**
|
|
37
|
-
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
38
|
-
*
|
|
39
|
-
* @default '/'
|
|
40
|
-
*/
|
|
41
|
-
path?: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Sets a cookie in the response headers.
|
|
45
|
-
*
|
|
46
|
-
* Does nothing if `headers` is `undefined`.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* const headers = new Headers()
|
|
51
|
-
*
|
|
52
|
-
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
53
|
-
*
|
|
54
|
-
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; Max-Age=3600; Path=/; HttpOnly')
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
58
|
-
*/
|
|
59
|
-
declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
|
|
60
|
-
interface GetCookieOptions extends ParseOptions {
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Gets a cookie value from request headers
|
|
64
|
-
*
|
|
65
|
-
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
70
|
-
*
|
|
71
|
-
* const sessionId = getCookie(headers, 'sessionId')
|
|
72
|
-
*
|
|
73
|
-
* expect(sessionId).toEqual('abc123')
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
77
|
-
*/
|
|
78
|
-
declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
|
|
79
|
-
/**
|
|
80
|
-
* Deletes a cookie by marking it expired.
|
|
81
|
-
*
|
|
82
|
-
* @see {@link https://orpc.dev/docs/helpers/cookie | Cookie Helpers}
|
|
83
|
-
*/
|
|
84
|
-
declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, 'maxAge'>): void;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Encrypts a string using AES-GCM with a secret key.
|
|
88
|
-
* The output is base64url encoded to be URL-safe.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
93
|
-
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
94
|
-
* expect(decrypted).toBe("Hello, World!")
|
|
95
|
-
* ```
|
|
96
|
-
*
|
|
97
|
-
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
98
|
-
*/
|
|
99
|
-
declare function encrypt(value: string, secret: string): Promise<string>;
|
|
100
|
-
/**
|
|
101
|
-
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
102
|
-
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
107
|
-
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
108
|
-
* expect(decrypted).toBe("Hello, World!")
|
|
109
|
-
* ```
|
|
110
|
-
*
|
|
111
|
-
* @see {@link https://orpc.dev/docs/helpers/encryption | Encryption Helpers}
|
|
112
|
-
*/
|
|
113
|
-
declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
117
|
-
*
|
|
118
|
-
* This function creates a cryptographic signature that can be used to verify
|
|
119
|
-
* the integrity and authenticity of the data. The signature is appended to
|
|
120
|
-
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```ts
|
|
124
|
-
* const signedValue = await sign("user123", "my-secret-key")
|
|
125
|
-
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
126
|
-
* ```
|
|
127
|
-
*
|
|
128
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
129
|
-
*/
|
|
130
|
-
declare function sign(value: string, secret: string): Promise<string>;
|
|
131
|
-
/**
|
|
132
|
-
* Verifies and extracts the original value from a signed string.
|
|
133
|
-
*
|
|
134
|
-
* This function validates the signature of a previously signed value using the same
|
|
135
|
-
* secret key. If the signature is valid, it returns the original value. If the
|
|
136
|
-
* signature is invalid or the format is incorrect, it returns undefined.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* ```ts
|
|
140
|
-
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
141
|
-
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
142
|
-
* expect(originalValue).toEqual("user123")
|
|
143
|
-
* ```
|
|
144
|
-
*
|
|
145
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
146
|
-
*/
|
|
147
|
-
declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
148
|
-
/**
|
|
149
|
-
* Extracts the value part from a signed string without verification.
|
|
150
|
-
*
|
|
151
|
-
* This function simply extracts the original value from a signed string
|
|
152
|
-
* without performing any signature verification. It's useful when you need
|
|
153
|
-
* to access the value quickly without the overhead of cryptographic verification.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```ts
|
|
157
|
-
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
158
|
-
* const value = getSignedValue(signedValue)
|
|
159
|
-
* expect(value).toEqual("user123")
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
163
|
-
*/
|
|
164
|
-
declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
165
|
-
|
|
166
|
-
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
|
167
|
-
export type { GetCookieOptions, SetCookieOptions };
|
|
1
|
+
export * from "./base64url.mjs";
|
|
2
|
+
export * from "./cookie.mjs";
|
|
3
|
+
export * from "./encryption.mjs";
|
|
4
|
+
export * from "./signing.mjs";
|
package/dist/helpers/index.mjs
CHANGED
|
@@ -1,200 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return encodeBase64(data).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
6
|
-
}
|
|
7
|
-
function decodeBase64url(base64url) {
|
|
8
|
-
try {
|
|
9
|
-
if (typeof base64url !== "string") {
|
|
10
|
-
return void 0;
|
|
11
|
-
}
|
|
12
|
-
let base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
13
|
-
while (base64.length % 4) {
|
|
14
|
-
base64 += "=";
|
|
15
|
-
}
|
|
16
|
-
const binaryString = atob(base64);
|
|
17
|
-
const bytes = new Uint8Array(binaryString.length);
|
|
18
|
-
for (let i = 0; i < binaryString.length; i++) {
|
|
19
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
20
|
-
}
|
|
21
|
-
return encodeBase64url(bytes) === base64url ? bytes : void 0;
|
|
22
|
-
} catch {
|
|
23
|
-
return void 0;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function setCookie(headers, name, value, options = {}) {
|
|
28
|
-
if (headers === void 0) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
const cookieString = stringifySetCookie({
|
|
32
|
-
// Force path to '/' by default so the cookie is available across the
|
|
33
|
-
// entire app, not just under the directory of the request that set it
|
|
34
|
-
// (which is what browsers do per RFC 6265 §5.1.4 when Path is omitted).
|
|
35
|
-
path: "/",
|
|
36
|
-
...options,
|
|
37
|
-
name,
|
|
38
|
-
// prioritize
|
|
39
|
-
value
|
|
40
|
-
// prioritize
|
|
41
|
-
}, options);
|
|
42
|
-
headers.append("Set-Cookie", cookieString);
|
|
43
|
-
}
|
|
44
|
-
function getCookie(headers, name, options = {}) {
|
|
45
|
-
if (headers === void 0) {
|
|
46
|
-
return void 0;
|
|
47
|
-
}
|
|
48
|
-
const cookieHeader = headers.get("cookie");
|
|
49
|
-
if (cookieHeader === null) {
|
|
50
|
-
return void 0;
|
|
51
|
-
}
|
|
52
|
-
return parseCookie(cookieHeader, options)[name];
|
|
53
|
-
}
|
|
54
|
-
function deleteCookie(headers, name, options = {}) {
|
|
55
|
-
return setCookie(headers, name, "", {
|
|
56
|
-
...options,
|
|
57
|
-
maxAge: 0
|
|
58
|
-
// prioritize
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const PBKDF2_CONFIG = {
|
|
63
|
-
name: "PBKDF2",
|
|
64
|
-
iterations: 6e4,
|
|
65
|
-
// Recommended minimum iterations per current OWASP guidelines
|
|
66
|
-
hash: "SHA-256"
|
|
67
|
-
};
|
|
68
|
-
const AES_GCM_CONFIG = {
|
|
69
|
-
name: "AES-GCM",
|
|
70
|
-
length: 256
|
|
71
|
-
};
|
|
72
|
-
const CRYPTO_CONSTANTS = {
|
|
73
|
-
SALT_LENGTH: 16,
|
|
74
|
-
IV_LENGTH: 12
|
|
75
|
-
};
|
|
76
|
-
async function encrypt(value, secret) {
|
|
77
|
-
const encoder = new TextEncoder();
|
|
78
|
-
const data = encoder.encode(value);
|
|
79
|
-
const salt = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.SALT_LENGTH));
|
|
80
|
-
const keyMaterial = await crypto.subtle.importKey(
|
|
81
|
-
"raw",
|
|
82
|
-
encoder.encode(secret),
|
|
83
|
-
PBKDF2_CONFIG.name,
|
|
84
|
-
false,
|
|
85
|
-
["deriveKey"]
|
|
86
|
-
);
|
|
87
|
-
const key = await crypto.subtle.deriveKey(
|
|
88
|
-
{ ...PBKDF2_CONFIG, salt },
|
|
89
|
-
keyMaterial,
|
|
90
|
-
AES_GCM_CONFIG,
|
|
91
|
-
false,
|
|
92
|
-
["encrypt"]
|
|
93
|
-
);
|
|
94
|
-
const iv = crypto.getRandomValues(new Uint8Array(CRYPTO_CONSTANTS.IV_LENGTH));
|
|
95
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
96
|
-
{ name: AES_GCM_CONFIG.name, iv },
|
|
97
|
-
key,
|
|
98
|
-
data
|
|
99
|
-
);
|
|
100
|
-
const result = new Uint8Array(salt.length + iv.length + encrypted.byteLength);
|
|
101
|
-
result.set(salt, 0);
|
|
102
|
-
result.set(iv, salt.length);
|
|
103
|
-
result.set(new Uint8Array(encrypted), salt.length + iv.length);
|
|
104
|
-
return encodeBase64url(result);
|
|
105
|
-
}
|
|
106
|
-
async function decrypt(encrypted, secret) {
|
|
107
|
-
try {
|
|
108
|
-
const data = decodeBase64url(encrypted);
|
|
109
|
-
if (data === void 0) {
|
|
110
|
-
return void 0;
|
|
111
|
-
}
|
|
112
|
-
const encoder = new TextEncoder();
|
|
113
|
-
const decoder = new TextDecoder();
|
|
114
|
-
const salt = data.slice(0, CRYPTO_CONSTANTS.SALT_LENGTH);
|
|
115
|
-
const iv = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH, CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
116
|
-
const encryptedData = data.slice(CRYPTO_CONSTANTS.SALT_LENGTH + CRYPTO_CONSTANTS.IV_LENGTH);
|
|
117
|
-
const keyMaterial = await crypto.subtle.importKey(
|
|
118
|
-
"raw",
|
|
119
|
-
encoder.encode(secret),
|
|
120
|
-
PBKDF2_CONFIG.name,
|
|
121
|
-
false,
|
|
122
|
-
["deriveKey"]
|
|
123
|
-
);
|
|
124
|
-
const key = await crypto.subtle.deriveKey(
|
|
125
|
-
{ ...PBKDF2_CONFIG, salt },
|
|
126
|
-
keyMaterial,
|
|
127
|
-
AES_GCM_CONFIG,
|
|
128
|
-
false,
|
|
129
|
-
["decrypt"]
|
|
130
|
-
);
|
|
131
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
132
|
-
{ name: AES_GCM_CONFIG.name, iv },
|
|
133
|
-
key,
|
|
134
|
-
encryptedData
|
|
135
|
-
);
|
|
136
|
-
return decoder.decode(decrypted);
|
|
137
|
-
} catch {
|
|
138
|
-
return void 0;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const ALGORITHM = { name: "HMAC", hash: "SHA-256" };
|
|
143
|
-
async function sign(value, secret) {
|
|
144
|
-
const encoder = new TextEncoder();
|
|
145
|
-
const key = await crypto.subtle.importKey(
|
|
146
|
-
"raw",
|
|
147
|
-
encoder.encode(secret),
|
|
148
|
-
ALGORITHM,
|
|
149
|
-
false,
|
|
150
|
-
["sign"]
|
|
151
|
-
);
|
|
152
|
-
const signature = await crypto.subtle.sign(
|
|
153
|
-
ALGORITHM,
|
|
154
|
-
key,
|
|
155
|
-
encoder.encode(value)
|
|
156
|
-
);
|
|
157
|
-
return `${value}.${encodeBase64url(new Uint8Array(signature))}`;
|
|
158
|
-
}
|
|
159
|
-
async function unsign(signedValue, secret) {
|
|
160
|
-
if (typeof signedValue !== "string") {
|
|
161
|
-
return void 0;
|
|
162
|
-
}
|
|
163
|
-
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
164
|
-
if (lastDotIndex === -1) {
|
|
165
|
-
return void 0;
|
|
166
|
-
}
|
|
167
|
-
const value = signedValue.slice(0, lastDotIndex);
|
|
168
|
-
const signatureBase64url = signedValue.slice(lastDotIndex + 1);
|
|
169
|
-
const signature = decodeBase64url(signatureBase64url);
|
|
170
|
-
if (signature === void 0) {
|
|
171
|
-
return void 0;
|
|
172
|
-
}
|
|
173
|
-
const encoder = new TextEncoder();
|
|
174
|
-
const key = await crypto.subtle.importKey(
|
|
175
|
-
"raw",
|
|
176
|
-
encoder.encode(secret),
|
|
177
|
-
ALGORITHM,
|
|
178
|
-
false,
|
|
179
|
-
["verify"]
|
|
180
|
-
);
|
|
181
|
-
const isValid = await crypto.subtle.verify(
|
|
182
|
-
ALGORITHM,
|
|
183
|
-
key,
|
|
184
|
-
signature,
|
|
185
|
-
encoder.encode(value)
|
|
186
|
-
);
|
|
187
|
-
return isValid ? value : void 0;
|
|
188
|
-
}
|
|
189
|
-
function getSignedValue(signedValue) {
|
|
190
|
-
if (typeof signedValue !== "string") {
|
|
191
|
-
return void 0;
|
|
192
|
-
}
|
|
193
|
-
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
194
|
-
if (lastDotIndex === -1) {
|
|
195
|
-
return void 0;
|
|
196
|
-
}
|
|
197
|
-
return signedValue.slice(0, lastDotIndex);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
|
1
|
+
export * from "./base64url.mjs";
|
|
2
|
+
export * from "./cookie.mjs";
|
|
3
|
+
export * from "./encryption.mjs";
|
|
4
|
+
export * from "./signing.mjs";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
3
|
+
*
|
|
4
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
5
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
6
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
11
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
15
|
+
*/
|
|
16
|
+
export declare function sign(value: string, secret: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Verifies and extracts the original value from a signed string.
|
|
19
|
+
*
|
|
20
|
+
* This function validates the signature of a previously signed value using the same
|
|
21
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
22
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
27
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
28
|
+
* expect(originalValue).toEqual("user123")
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
32
|
+
*/
|
|
33
|
+
export declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Extracts the value part from a signed string without verification.
|
|
36
|
+
*
|
|
37
|
+
* This function simply extracts the original value from a signed string
|
|
38
|
+
* without performing any signature verification. It's useful when you need
|
|
39
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
44
|
+
* const value = getSignedValue(signedValue)
|
|
45
|
+
* expect(value).toEqual("user123")
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/helpers/signing | Signing Helpers}
|
|
49
|
+
*/
|
|
50
|
+
export declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { decodeBase64url, encodeBase64url } from "./base64url.mjs";
|
|
2
|
+
const ALGORITHM = {
|
|
3
|
+
name: "HMAC",
|
|
4
|
+
hash: "SHA-256"
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export async function sign(value, secret) {
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), ALGORITHM, false, ["sign"]);
|
|
10
|
+
const signature = await crypto.subtle.sign(ALGORITHM, key, encoder.encode(value));
|
|
11
|
+
return `${value}.${encodeBase64url(new Uint8Array(signature))}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function unsign(signedValue, secret) {
|
|
15
|
+
if (typeof signedValue !== "string") {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
19
|
+
if (lastDotIndex === -1) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const value = signedValue.slice(0, lastDotIndex);
|
|
23
|
+
const signatureBase64url = signedValue.slice(lastDotIndex + 1);
|
|
24
|
+
const signature = decodeBase64url(signatureBase64url);
|
|
25
|
+
if (signature === undefined) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const encoder = new TextEncoder();
|
|
29
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), ALGORITHM, false, ["verify"]);
|
|
30
|
+
const isValid = await crypto.subtle.verify(ALGORITHM, key, signature, encoder.encode(value));
|
|
31
|
+
return isValid ? value : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getSignedValue(signedValue) {
|
|
35
|
+
if (typeof signedValue !== "string") {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const lastDotIndex = signedValue.lastIndexOf(".");
|
|
39
|
+
if (lastDotIndex === -1) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return signedValue.slice(0, lastDotIndex);
|
|
43
|
+
}
|