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