@orpc/server 0.0.0-next.a4bc49e → 0.0.0-next.a55d49f
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 +7 -2
- package/dist/adapters/aws-lambda/index.d.mts +5 -4
- package/dist/adapters/aws-lambda/index.d.ts +5 -4
- package/dist/adapters/aws-lambda/index.mjs +4 -3
- package/dist/adapters/bun-ws/index.d.mts +5 -4
- package/dist/adapters/bun-ws/index.d.ts +5 -4
- package/dist/adapters/bun-ws/index.mjs +10 -10
- package/dist/adapters/crossws/index.d.mts +5 -4
- package/dist/adapters/crossws/index.d.ts +5 -4
- package/dist/adapters/crossws/index.mjs +5 -7
- package/dist/adapters/fastify/index.d.mts +53 -0
- package/dist/adapters/fastify/index.d.ts +53 -0
- package/dist/adapters/fastify/index.mjs +54 -0
- package/dist/adapters/fetch/index.d.mts +54 -8
- package/dist/adapters/fetch/index.d.ts +54 -8
- package/dist/adapters/fetch/index.mjs +74 -4
- package/dist/adapters/message-port/index.d.mts +35 -8
- package/dist/adapters/message-port/index.d.ts +35 -8
- package/dist/adapters/message-port/index.mjs +29 -15
- package/dist/adapters/node/index.d.mts +31 -9
- package/dist/adapters/node/index.d.ts +31 -9
- package/dist/adapters/node/index.mjs +70 -13
- package/dist/adapters/standard/index.d.mts +8 -14
- package/dist/adapters/standard/index.d.ts +8 -14
- package/dist/adapters/standard/index.mjs +2 -2
- package/dist/adapters/standard-peer/index.d.mts +9 -5
- package/dist/adapters/standard-peer/index.d.ts +9 -5
- package/dist/adapters/standard-peer/index.mjs +1 -1
- package/dist/adapters/websocket/index.d.mts +9 -6
- package/dist/adapters/websocket/index.d.ts +9 -6
- package/dist/adapters/websocket/index.mjs +5 -7
- package/dist/adapters/ws/index.d.mts +5 -4
- package/dist/adapters/ws/index.d.ts +5 -4
- package/dist/adapters/ws/index.mjs +5 -7
- package/dist/helpers/index.d.mts +149 -0
- package/dist/helpers/index.d.ts +149 -0
- package/dist/helpers/index.mjs +198 -0
- package/dist/hibernation/index.d.mts +11 -11
- package/dist/hibernation/index.d.ts +11 -11
- package/dist/hibernation/index.mjs +8 -8
- package/dist/index.d.mts +9 -40
- package/dist/index.d.ts +9 -40
- package/dist/index.mjs +12 -8
- package/dist/plugins/index.d.mts +20 -4
- package/dist/plugins/index.d.ts +20 -4
- package/dist/plugins/index.mjs +86 -58
- package/dist/shared/{server.6ohwBdwx.d.mts → server.B4BGqy3Y.d.mts} +6 -5
- package/dist/shared/{server.6ohwBdwx.d.ts → server.B4BGqy3Y.d.ts} +6 -5
- package/dist/shared/{server.CB8Snncu.mjs → server.Bxx6tqNe.mjs} +74 -41
- package/dist/shared/server.COL12UTb.d.ts +32 -0
- package/dist/shared/{server.B3dVpAsJ.d.mts → server.CVKCo60T.d.mts} +2 -2
- package/dist/shared/server.Cb6yD7DZ.d.ts +42 -0
- package/dist/shared/server.Ck-gOLzq.d.mts +32 -0
- package/dist/shared/{server.BtQsqpPB.d.mts → server.DBCUJijK.d.mts} +3 -3
- package/dist/shared/{server.CeW2jMCj.d.ts → server.DNtJ-p60.d.ts} +2 -2
- package/dist/shared/{server.BE3B4vij.d.ts → server.DPIFWpxG.d.ts} +3 -3
- package/dist/shared/{server.DLJzqnSX.mjs → server.Ds4HPpvH.mjs} +114 -73
- package/dist/shared/server.DzV1hr3z.d.mts +42 -0
- package/dist/shared/{server.BW-nUGgA.mjs → server.TEVCLCFC.mjs} +3 -0
- package/dist/shared/server.UVMTOWrk.mjs +26 -0
- package/package.json +28 -12
- package/dist/shared/server.B6GspgNq.d.ts +0 -12
- package/dist/shared/server.C6Q5sqYw.mjs +0 -20
- package/dist/shared/server.CKafa5G2.d.mts +0 -12
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { ServerPeer, EncodedMessage } from '@orpc/standard-server-peer';
|
|
2
|
-
import { C as Context } from '../../shared/server.
|
|
3
|
-
import {
|
|
4
|
-
import { F as FriendlyStandardHandleOptions } from '../../shared/server.
|
|
1
|
+
import { ServerPeer, EncodedMessage, ServerPeerHandleRequestFn } from '@orpc/standard-server-peer';
|
|
2
|
+
import { C as Context } from '../../shared/server.B4BGqy3Y.mjs';
|
|
3
|
+
import { f as StandardHandler } from '../../shared/server.DBCUJijK.mjs';
|
|
4
|
+
import { F as FriendlyStandardHandleOptions } from '../../shared/server.CVKCo60T.mjs';
|
|
5
5
|
import '@orpc/client';
|
|
6
6
|
import '@orpc/contract';
|
|
7
7
|
import '@orpc/shared';
|
|
8
8
|
import '@orpc/standard-server';
|
|
9
9
|
|
|
10
10
|
type HandleStandardServerPeerMessageOptions<T extends Context> = Omit<FriendlyStandardHandleOptions<T>, 'prefix'>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use `createServerPeerRequestHandleFn` instead.
|
|
13
|
+
*/
|
|
11
14
|
declare function handleStandardServerPeerMessage<T extends Context>(handler: StandardHandler<T>, peer: ServerPeer, message: EncodedMessage, options: HandleStandardServerPeerMessageOptions<T>): Promise<void>;
|
|
15
|
+
declare function createServerPeerHandleRequestFn<T extends Context>(handler: StandardHandler<T>, options: HandleStandardServerPeerMessageOptions<T>): ServerPeerHandleRequestFn;
|
|
12
16
|
|
|
13
|
-
export { handleStandardServerPeerMessage };
|
|
17
|
+
export { createServerPeerHandleRequestFn, handleStandardServerPeerMessage };
|
|
14
18
|
export type { HandleStandardServerPeerMessageOptions };
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { ServerPeer, EncodedMessage } from '@orpc/standard-server-peer';
|
|
2
|
-
import { C as Context } from '../../shared/server.
|
|
3
|
-
import {
|
|
4
|
-
import { F as FriendlyStandardHandleOptions } from '../../shared/server.
|
|
1
|
+
import { ServerPeer, EncodedMessage, ServerPeerHandleRequestFn } from '@orpc/standard-server-peer';
|
|
2
|
+
import { C as Context } from '../../shared/server.B4BGqy3Y.js';
|
|
3
|
+
import { f as StandardHandler } from '../../shared/server.DPIFWpxG.js';
|
|
4
|
+
import { F as FriendlyStandardHandleOptions } from '../../shared/server.DNtJ-p60.js';
|
|
5
5
|
import '@orpc/client';
|
|
6
6
|
import '@orpc/contract';
|
|
7
7
|
import '@orpc/shared';
|
|
8
8
|
import '@orpc/standard-server';
|
|
9
9
|
|
|
10
10
|
type HandleStandardServerPeerMessageOptions<T extends Context> = Omit<FriendlyStandardHandleOptions<T>, 'prefix'>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use `createServerPeerRequestHandleFn` instead.
|
|
13
|
+
*/
|
|
11
14
|
declare function handleStandardServerPeerMessage<T extends Context>(handler: StandardHandler<T>, peer: ServerPeer, message: EncodedMessage, options: HandleStandardServerPeerMessageOptions<T>): Promise<void>;
|
|
15
|
+
declare function createServerPeerHandleRequestFn<T extends Context>(handler: StandardHandler<T>, options: HandleStandardServerPeerMessageOptions<T>): ServerPeerHandleRequestFn;
|
|
12
16
|
|
|
13
|
-
export { handleStandardServerPeerMessage };
|
|
17
|
+
export { createServerPeerHandleRequestFn, handleStandardServerPeerMessage };
|
|
14
18
|
export type { HandleStandardServerPeerMessageOptions };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { h as handleStandardServerPeerMessage } from '../../shared/server.
|
|
1
|
+
export { c as createServerPeerHandleRequestFn, h as handleStandardServerPeerMessage } from '../../shared/server.UVMTOWrk.mjs';
|
|
2
2
|
import '@orpc/client';
|
|
3
3
|
import '@orpc/shared';
|
|
4
4
|
import '@orpc/standard-server';
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
-
import { C as Context, R as Router } from '../../shared/server.
|
|
3
|
-
import {
|
|
2
|
+
import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.mjs';
|
|
3
|
+
import { f as StandardHandler } from '../../shared/server.DBCUJijK.mjs';
|
|
4
4
|
import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.mjs';
|
|
5
|
-
import { S as StandardRPCHandlerOptions } from '../../shared/server.
|
|
5
|
+
import { S as StandardRPCHandlerOptions } from '../../shared/server.Ck-gOLzq.mjs';
|
|
6
6
|
import '@orpc/client';
|
|
7
7
|
import '@orpc/contract';
|
|
8
8
|
import '@orpc/standard-server';
|
|
9
9
|
import '@orpc/standard-server-peer';
|
|
10
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.CVKCo60T.mjs';
|
|
11
11
|
import '@orpc/client/standard';
|
|
12
|
+
import '../../shared/server.DzV1hr3z.mjs';
|
|
12
13
|
|
|
13
14
|
type MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
|
|
14
15
|
declare class WebsocketHandler<T extends Context> {
|
|
@@ -39,6 +40,8 @@ declare class WebsocketHandler<T extends Context> {
|
|
|
39
40
|
close(ws: MinimalWebsocket): void;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T> {
|
|
44
|
+
}
|
|
42
45
|
/**
|
|
43
46
|
* RPC Handler for Websocket adapter
|
|
44
47
|
*
|
|
@@ -46,8 +49,8 @@ declare class WebsocketHandler<T extends Context> {
|
|
|
46
49
|
* @see {@link https://orpc.unnoq.com/docs/adapters/websocket Websocket Adapter Docs}
|
|
47
50
|
*/
|
|
48
51
|
declare class RPCHandler<T extends Context> extends WebsocketHandler<T> {
|
|
49
|
-
constructor(router: Router<any, T>, options?: NoInfer<
|
|
52
|
+
constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
export { RPCHandler, WebsocketHandler };
|
|
53
|
-
export type { MinimalWebsocket };
|
|
56
|
+
export type { MinimalWebsocket, RPCHandlerOptions };
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
-
import { C as Context, R as Router } from '../../shared/server.
|
|
3
|
-
import {
|
|
2
|
+
import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.js';
|
|
3
|
+
import { f as StandardHandler } from '../../shared/server.DPIFWpxG.js';
|
|
4
4
|
import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.js';
|
|
5
|
-
import { S as StandardRPCHandlerOptions } from '../../shared/server.
|
|
5
|
+
import { S as StandardRPCHandlerOptions } from '../../shared/server.COL12UTb.js';
|
|
6
6
|
import '@orpc/client';
|
|
7
7
|
import '@orpc/contract';
|
|
8
8
|
import '@orpc/standard-server';
|
|
9
9
|
import '@orpc/standard-server-peer';
|
|
10
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.DNtJ-p60.js';
|
|
11
11
|
import '@orpc/client/standard';
|
|
12
|
+
import '../../shared/server.Cb6yD7DZ.js';
|
|
12
13
|
|
|
13
14
|
type MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
|
|
14
15
|
declare class WebsocketHandler<T extends Context> {
|
|
@@ -39,6 +40,8 @@ declare class WebsocketHandler<T extends Context> {
|
|
|
39
40
|
close(ws: MinimalWebsocket): void;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T> {
|
|
44
|
+
}
|
|
42
45
|
/**
|
|
43
46
|
* RPC Handler for Websocket adapter
|
|
44
47
|
*
|
|
@@ -46,8 +49,8 @@ declare class WebsocketHandler<T extends Context> {
|
|
|
46
49
|
* @see {@link https://orpc.unnoq.com/docs/adapters/websocket Websocket Adapter Docs}
|
|
47
50
|
*/
|
|
48
51
|
declare class RPCHandler<T extends Context> extends WebsocketHandler<T> {
|
|
49
|
-
constructor(router: Router<any, T>, options?: NoInfer<
|
|
52
|
+
constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
export { RPCHandler, WebsocketHandler };
|
|
53
|
-
export type { MinimalWebsocket };
|
|
56
|
+
export type { MinimalWebsocket, RPCHandlerOptions };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { readAsBuffer, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { ServerPeer } from '@orpc/standard-server-peer';
|
|
3
|
-
import {
|
|
3
|
+
import { c as createServerPeerHandleRequestFn } from '../../shared/server.UVMTOWrk.mjs';
|
|
4
4
|
import '@orpc/client';
|
|
5
5
|
import '@orpc/standard-server';
|
|
6
6
|
import '@orpc/contract';
|
|
7
|
-
import { b as StandardRPCHandler } from '../../shared/server.
|
|
7
|
+
import { b as StandardRPCHandler } from '../../shared/server.Bxx6tqNe.mjs';
|
|
8
8
|
import '@orpc/client/standard';
|
|
9
9
|
import '../../shared/server.DZ5BIITo.mjs';
|
|
10
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.Ds4HPpvH.mjs';
|
|
11
11
|
|
|
12
12
|
class WebsocketHandler {
|
|
13
13
|
#peers = /* @__PURE__ */ new WeakMap();
|
|
@@ -40,11 +40,9 @@ class WebsocketHandler {
|
|
|
40
40
|
this.#peers.set(ws, peer = new ServerPeer(ws.send.bind(ws)));
|
|
41
41
|
}
|
|
42
42
|
const message = data instanceof Blob ? await readAsBuffer(data) : data;
|
|
43
|
-
await
|
|
44
|
-
this.#handler,
|
|
45
|
-
peer,
|
|
43
|
+
await peer.message(
|
|
46
44
|
message,
|
|
47
|
-
resolveMaybeOptionalOptions(rest)
|
|
45
|
+
createServerPeerHandleRequestFn(this.#handler, resolveMaybeOptionalOptions(rest))
|
|
48
46
|
);
|
|
49
47
|
}
|
|
50
48
|
/**
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { WebSocket } from 'ws';
|
|
3
|
-
import { C as Context, R as Router } from '../../shared/server.
|
|
4
|
-
import {
|
|
3
|
+
import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.mjs';
|
|
4
|
+
import { f as StandardHandler } from '../../shared/server.DBCUJijK.mjs';
|
|
5
5
|
import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.mjs';
|
|
6
|
-
import { S as StandardRPCHandlerOptions } from '../../shared/server.
|
|
6
|
+
import { S as StandardRPCHandlerOptions } from '../../shared/server.Ck-gOLzq.mjs';
|
|
7
7
|
import '@orpc/client';
|
|
8
8
|
import '@orpc/contract';
|
|
9
9
|
import '@orpc/standard-server';
|
|
10
10
|
import '@orpc/standard-server-peer';
|
|
11
|
-
import '../../shared/server.
|
|
11
|
+
import '../../shared/server.CVKCo60T.mjs';
|
|
12
12
|
import '@orpc/client/standard';
|
|
13
|
+
import '../../shared/server.DzV1hr3z.mjs';
|
|
13
14
|
|
|
14
15
|
declare class WsHandler<T extends Context> {
|
|
15
16
|
private readonly standardHandler;
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { WebSocket } from 'ws';
|
|
3
|
-
import { C as Context, R as Router } from '../../shared/server.
|
|
4
|
-
import {
|
|
3
|
+
import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.js';
|
|
4
|
+
import { f as StandardHandler } from '../../shared/server.DPIFWpxG.js';
|
|
5
5
|
import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.js';
|
|
6
|
-
import { S as StandardRPCHandlerOptions } from '../../shared/server.
|
|
6
|
+
import { S as StandardRPCHandlerOptions } from '../../shared/server.COL12UTb.js';
|
|
7
7
|
import '@orpc/client';
|
|
8
8
|
import '@orpc/contract';
|
|
9
9
|
import '@orpc/standard-server';
|
|
10
10
|
import '@orpc/standard-server-peer';
|
|
11
|
-
import '../../shared/server.
|
|
11
|
+
import '../../shared/server.DNtJ-p60.js';
|
|
12
12
|
import '@orpc/client/standard';
|
|
13
|
+
import '../../shared/server.Cb6yD7DZ.js';
|
|
13
14
|
|
|
14
15
|
declare class WsHandler<T extends Context> {
|
|
15
16
|
private readonly standardHandler;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { readAsBuffer, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { ServerPeer } from '@orpc/standard-server-peer';
|
|
3
|
-
import {
|
|
3
|
+
import { c as createServerPeerHandleRequestFn } from '../../shared/server.UVMTOWrk.mjs';
|
|
4
4
|
import '@orpc/client';
|
|
5
5
|
import '@orpc/standard-server';
|
|
6
6
|
import '@orpc/contract';
|
|
7
|
-
import { b as StandardRPCHandler } from '../../shared/server.
|
|
7
|
+
import { b as StandardRPCHandler } from '../../shared/server.Bxx6tqNe.mjs';
|
|
8
8
|
import '@orpc/client/standard';
|
|
9
9
|
import '../../shared/server.DZ5BIITo.mjs';
|
|
10
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.Ds4HPpvH.mjs';
|
|
11
11
|
|
|
12
12
|
class WsHandler {
|
|
13
13
|
constructor(standardHandler) {
|
|
@@ -17,11 +17,9 @@ class WsHandler {
|
|
|
17
17
|
const peer = new ServerPeer(ws.send.bind(ws));
|
|
18
18
|
ws.addEventListener("message", async (event) => {
|
|
19
19
|
const message = Array.isArray(event.data) ? await readAsBuffer(new Blob(event.data)) : event.data;
|
|
20
|
-
await
|
|
21
|
-
this.standardHandler,
|
|
22
|
-
peer,
|
|
20
|
+
await peer.message(
|
|
23
21
|
message,
|
|
24
|
-
resolveMaybeOptionalOptions(rest)
|
|
22
|
+
createServerPeerHandleRequestFn(this.standardHandler, resolveMaybeOptionalOptions(rest))
|
|
25
23
|
);
|
|
26
24
|
});
|
|
27
25
|
ws.addEventListener("close", () => {
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { SerializeOptions, ParseOptions } from 'cookie';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encodes a Uint8Array to base64url format
|
|
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
|
+
declare function encodeBase64url(data: Uint8Array): string;
|
|
16
|
+
/**
|
|
17
|
+
* Decodes a base64url string to Uint8Array
|
|
18
|
+
* Returns undefined if the input is invalid
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const text = "Hello World"
|
|
23
|
+
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
24
|
+
* const decoded = decodeBase64url(encoded)
|
|
25
|
+
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function decodeBase64url(base64url: string | undefined | null): Uint8Array | undefined;
|
|
29
|
+
|
|
30
|
+
interface SetCookieOptions extends SerializeOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
33
|
+
*
|
|
34
|
+
* @default '/'
|
|
35
|
+
*/
|
|
36
|
+
path?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets a cookie in the response headers,
|
|
40
|
+
*
|
|
41
|
+
* Does nothing if `headers` is `undefined`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const headers = new Headers()
|
|
46
|
+
*
|
|
47
|
+
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
48
|
+
*
|
|
49
|
+
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; HttpOnly; Max-Age=3600')
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
|
|
54
|
+
interface GetCookieOptions extends ParseOptions {
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets a cookie value from request headers
|
|
58
|
+
*
|
|
59
|
+
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
64
|
+
*
|
|
65
|
+
* const sessionId = getCookie(headers, 'sessionId')
|
|
66
|
+
*
|
|
67
|
+
* expect(sessionId).toEqual('abc123')
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Deletes a cookie by marking it expired.
|
|
73
|
+
*/
|
|
74
|
+
declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, 'maxAge'>): void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Encrypts a string using AES-GCM with a secret key.
|
|
78
|
+
* The output is base64url encoded to be URL-safe.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
83
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
84
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function encrypt(value: string, secret: string): Promise<string>;
|
|
88
|
+
/**
|
|
89
|
+
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
90
|
+
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
95
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
96
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
103
|
+
*
|
|
104
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
105
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
106
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
107
|
+
*
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
112
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function sign(value: string, secret: string): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* Verifies and extracts the original value from a signed string.
|
|
118
|
+
*
|
|
119
|
+
* This function validates the signature of a previously signed value using the same
|
|
120
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
121
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
122
|
+
*
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
127
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
128
|
+
* expect(originalValue).toEqual("user123")
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
132
|
+
/**
|
|
133
|
+
* Extracts the value part from a signed string without verification.
|
|
134
|
+
*
|
|
135
|
+
* This function simply extracts the original value from a signed string
|
|
136
|
+
* without performing any signature verification. It's useful when you need
|
|
137
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
142
|
+
* const value = getSignedValue(signedValue)
|
|
143
|
+
* expect(value).toEqual("user123")
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
147
|
+
|
|
148
|
+
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
|
149
|
+
export type { GetCookieOptions, SetCookieOptions };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { SerializeOptions, ParseOptions } from 'cookie';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encodes a Uint8Array to base64url format
|
|
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
|
+
declare function encodeBase64url(data: Uint8Array): string;
|
|
16
|
+
/**
|
|
17
|
+
* Decodes a base64url string to Uint8Array
|
|
18
|
+
* Returns undefined if the input is invalid
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const text = "Hello World"
|
|
23
|
+
* const encoded = encodeBase64url(new TextEncoder().encode(text))
|
|
24
|
+
* const decoded = decodeBase64url(encoded)
|
|
25
|
+
* expect(new TextDecoder().decode(decoded)).toEqual(text)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function decodeBase64url(base64url: string | undefined | null): Uint8Array | undefined;
|
|
29
|
+
|
|
30
|
+
interface SetCookieOptions extends SerializeOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
33
|
+
*
|
|
34
|
+
* @default '/'
|
|
35
|
+
*/
|
|
36
|
+
path?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets a cookie in the response headers,
|
|
40
|
+
*
|
|
41
|
+
* Does nothing if `headers` is `undefined`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const headers = new Headers()
|
|
46
|
+
*
|
|
47
|
+
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
|
|
48
|
+
*
|
|
49
|
+
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; HttpOnly; Max-Age=3600')
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
|
|
54
|
+
interface GetCookieOptions extends ParseOptions {
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets a cookie value from request headers
|
|
58
|
+
*
|
|
59
|
+
* Returns `undefined` if the cookie is not found or headers are `undefined`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
|
|
64
|
+
*
|
|
65
|
+
* const sessionId = getCookie(headers, 'sessionId')
|
|
66
|
+
*
|
|
67
|
+
* expect(sessionId).toEqual('abc123')
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Deletes a cookie by marking it expired.
|
|
73
|
+
*/
|
|
74
|
+
declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, 'maxAge'>): void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Encrypts a string using AES-GCM with a secret key.
|
|
78
|
+
* The output is base64url encoded to be URL-safe.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
83
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
84
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function encrypt(value: string, secret: string): Promise<string>;
|
|
88
|
+
/**
|
|
89
|
+
* Decrypts a base64url encoded string using AES-GCM with a secret key.
|
|
90
|
+
* Returns the original string if decryption is successful, or undefined if it fails.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
|
|
95
|
+
* const decrypted = await decrypt(encrypted, "test-secret-key")
|
|
96
|
+
* expect(decrypted).toBe("Hello, World!")
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Signs a string value using HMAC-SHA256 with a secret key.
|
|
103
|
+
*
|
|
104
|
+
* This function creates a cryptographic signature that can be used to verify
|
|
105
|
+
* the integrity and authenticity of the data. The signature is appended to
|
|
106
|
+
* the original value, separated by a dot, using base64url encoding (no padding).
|
|
107
|
+
*
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const signedValue = await sign("user123", "my-secret-key")
|
|
112
|
+
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function sign(value: string, secret: string): Promise<string>;
|
|
116
|
+
/**
|
|
117
|
+
* Verifies and extracts the original value from a signed string.
|
|
118
|
+
*
|
|
119
|
+
* This function validates the signature of a previously signed value using the same
|
|
120
|
+
* secret key. If the signature is valid, it returns the original value. If the
|
|
121
|
+
* signature is invalid or the format is incorrect, it returns undefined.
|
|
122
|
+
*
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
127
|
+
* const originalValue = await unsign(signedValue, "my-secret-key")
|
|
128
|
+
* expect(originalValue).toEqual("user123")
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
|
|
132
|
+
/**
|
|
133
|
+
* Extracts the value part from a signed string without verification.
|
|
134
|
+
*
|
|
135
|
+
* This function simply extracts the original value from a signed string
|
|
136
|
+
* without performing any signature verification. It's useful when you need
|
|
137
|
+
* to access the value quickly without the overhead of cryptographic verification.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
|
|
142
|
+
* const value = getSignedValue(signedValue)
|
|
143
|
+
* expect(value).toEqual("user123")
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
|
|
147
|
+
|
|
148
|
+
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
|
|
149
|
+
export type { GetCookieOptions, SetCookieOptions };
|