@temporary-name/server 1.9.3-alpha.03e17aa3340f4b261a549bf9e3fe948b084e2309
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/LICENSE +21 -0
- package/README.md +31 -0
- package/dist/adapters/aws-lambda/index.d.mts +30 -0
- package/dist/adapters/aws-lambda/index.d.ts +30 -0
- package/dist/adapters/aws-lambda/index.mjs +33 -0
- package/dist/adapters/fetch/index.d.mts +108 -0
- package/dist/adapters/fetch/index.d.ts +108 -0
- package/dist/adapters/fetch/index.mjs +174 -0
- package/dist/adapters/node/index.d.mts +83 -0
- package/dist/adapters/node/index.d.ts +83 -0
- package/dist/adapters/node/index.mjs +139 -0
- package/dist/adapters/standard/index.d.mts +42 -0
- package/dist/adapters/standard/index.d.ts +42 -0
- package/dist/adapters/standard/index.mjs +11 -0
- package/dist/helpers/index.d.mts +149 -0
- package/dist/helpers/index.d.ts +149 -0
- package/dist/helpers/index.mjs +168 -0
- package/dist/index.d.mts +603 -0
- package/dist/index.d.ts +603 -0
- package/dist/index.mjs +617 -0
- package/dist/openapi/index.d.mts +220 -0
- package/dist/openapi/index.d.ts +220 -0
- package/dist/openapi/index.mjs +776 -0
- package/dist/plugins/index.d.mts +160 -0
- package/dist/plugins/index.d.ts +160 -0
- package/dist/plugins/index.mjs +288 -0
- package/dist/shared/server.BEHw7Eyx.mjs +247 -0
- package/dist/shared/server.BKSOrA6h.d.mts +192 -0
- package/dist/shared/server.BKSOrA6h.d.ts +192 -0
- package/dist/shared/server.BKh8I1Ny.mjs +239 -0
- package/dist/shared/server.BeuTpcmO.d.mts +23 -0
- package/dist/shared/server.C1fnTLq0.d.mts +57 -0
- package/dist/shared/server.CQyYNJ1H.d.ts +57 -0
- package/dist/shared/server.DLsti1Pv.mjs +293 -0
- package/dist/shared/server.SLLuK6_v.d.ts +23 -0
- package/package.json +95 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { C as Context, R as Router } from '../../shared/server.BKSOrA6h.js';
|
|
2
|
+
import { Interceptor, MaybeOptionalOptions } from '@temporary-name/shared';
|
|
3
|
+
import { SendStandardResponseOptions, NodeHttpRequest, NodeHttpResponse } from '@temporary-name/standard-server-node';
|
|
4
|
+
import { a as StandardHandlerPlugin, C as CompositeStandardHandlerPlugin, b as StandardHandlerOptions, c as StandardHandleOptions } from '../../shared/server.CQyYNJ1H.js';
|
|
5
|
+
import { F as FriendlyStandardHandleOptions } from '../../shared/server.SLLuK6_v.js';
|
|
6
|
+
import compression from '@temporary-name/interop/compression';
|
|
7
|
+
import '@temporary-name/contract';
|
|
8
|
+
import '@temporary-name/standard-server';
|
|
9
|
+
|
|
10
|
+
interface NodeHttpHandlerPlugin<T extends Context> extends StandardHandlerPlugin<T> {
|
|
11
|
+
initRuntimeAdapter?(options: NodeHttpHandlerOptions<T>): void;
|
|
12
|
+
}
|
|
13
|
+
declare class CompositeNodeHttpHandlerPlugin<T extends Context, TPlugin extends NodeHttpHandlerPlugin<T>> extends CompositeStandardHandlerPlugin<T, TPlugin> implements NodeHttpHandlerPlugin<T> {
|
|
14
|
+
initRuntimeAdapter(options: NodeHttpHandlerOptions<T>): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type NodeHttpHandleResult = {
|
|
18
|
+
matched: true;
|
|
19
|
+
} | {
|
|
20
|
+
matched: false;
|
|
21
|
+
};
|
|
22
|
+
interface NodeHttpHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
|
23
|
+
request: NodeHttpRequest;
|
|
24
|
+
response: NodeHttpResponse;
|
|
25
|
+
sendStandardResponseOptions: SendStandardResponseOptions;
|
|
26
|
+
}
|
|
27
|
+
interface NodeHttpHandlerOptions<T extends Context> extends SendStandardResponseOptions, Omit<StandardHandlerOptions<T>, 'plugins'> {
|
|
28
|
+
adapterInterceptors?: Interceptor<NodeHttpHandlerInterceptorOptions<T>, Promise<NodeHttpHandleResult>>[];
|
|
29
|
+
plugins?: NodeHttpHandlerPlugin<T>[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* OpenAPI Handler for Node Server
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-handler OpenAPI Handler Docs}
|
|
35
|
+
* @see {@link https://orpc.unnoq.com/docs/adapters/http HTTP Adapter Docs}
|
|
36
|
+
*/
|
|
37
|
+
declare class NodeHttpHandler<T extends Context> implements NodeHttpHandler<T> {
|
|
38
|
+
private readonly sendStandardResponseOptions;
|
|
39
|
+
private readonly adapterInterceptors;
|
|
40
|
+
private readonly standardHandler;
|
|
41
|
+
constructor(router: Router<any, T>, options?: NoInfer<NodeHttpHandlerOptions<T>>);
|
|
42
|
+
handle(request: NodeHttpRequest, response: NodeHttpResponse, ...rest: MaybeOptionalOptions<FriendlyStandardHandleOptions<T>>): Promise<NodeHttpHandleResult>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface BodyLimitPluginOptions {
|
|
46
|
+
/**
|
|
47
|
+
* The maximum size of the body in bytes.
|
|
48
|
+
*/
|
|
49
|
+
maxBodySize: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The Body Limit Plugin restricts the size of the request body for the Node.js HTTP Server.
|
|
53
|
+
*
|
|
54
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/body-limit Body Limit Plugin Docs}
|
|
55
|
+
*/
|
|
56
|
+
declare class BodyLimitPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
57
|
+
private readonly maxBodySize;
|
|
58
|
+
constructor(options: BodyLimitPluginOptions);
|
|
59
|
+
initRuntimeAdapter(options: NodeHttpHandlerOptions<T>): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface CompressionPluginOptions extends compression.CompressionOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Override the default content-type filter used to determine which responses should be compressed.
|
|
65
|
+
*
|
|
66
|
+
* @warning [Event Iterator](https://orpc.unnoq.com/docs/event-iterator) responses are never compressed, regardless of this filter's return value.
|
|
67
|
+
* @default only responses with compressible content types are compressed.
|
|
68
|
+
*/
|
|
69
|
+
filter?: (req: NodeHttpRequest, res: NodeHttpResponse) => boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The Compression Plugin adds response compression to the Node.js HTTP Server.
|
|
73
|
+
*
|
|
74
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/compression Compression Plugin Docs}
|
|
75
|
+
*/
|
|
76
|
+
declare class CompressionPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
77
|
+
private readonly compressionHandler;
|
|
78
|
+
constructor(options?: CompressionPluginOptions);
|
|
79
|
+
initRuntimeAdapter(options: NodeHttpHandlerOptions<T>): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { BodyLimitPlugin, CompositeNodeHttpHandlerPlugin, CompressionPlugin, NodeHttpHandler };
|
|
83
|
+
export type { BodyLimitPluginOptions, CompressionPluginOptions, NodeHttpHandleResult, NodeHttpHandlerInterceptorOptions, NodeHttpHandlerOptions, NodeHttpHandlerPlugin };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { once, ORPCError, toArray, intercept, resolveMaybeOptionalOptions } from '@temporary-name/shared';
|
|
2
|
+
import compression from '@temporary-name/interop/compression';
|
|
3
|
+
import { toStandardLazyRequest, sendStandardResponse } from '@temporary-name/standard-server-node';
|
|
4
|
+
import { C as CompositeStandardHandlerPlugin, S as StandardHandler, r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DLsti1Pv.mjs';
|
|
5
|
+
import '@temporary-name/contract';
|
|
6
|
+
import '@temporary-name/standard-server';
|
|
7
|
+
import '@temporary-name/server';
|
|
8
|
+
import 'rou3';
|
|
9
|
+
import '../../shared/server.BKh8I1Ny.mjs';
|
|
10
|
+
import '../../shared/server.BEHw7Eyx.mjs';
|
|
11
|
+
import 'node:async_hooks';
|
|
12
|
+
import 'zod';
|
|
13
|
+
import 'zod/v4/core';
|
|
14
|
+
|
|
15
|
+
class BodyLimitPlugin {
|
|
16
|
+
maxBodySize;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.maxBodySize = options.maxBodySize;
|
|
19
|
+
}
|
|
20
|
+
initRuntimeAdapter(options) {
|
|
21
|
+
options.adapterInterceptors ??= [];
|
|
22
|
+
options.adapterInterceptors.push(async (options2) => {
|
|
23
|
+
const checkHeader = once(() => {
|
|
24
|
+
if (Number(options2.request.headers["content-length"]) > this.maxBodySize) {
|
|
25
|
+
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const originalEmit = options2.request.emit;
|
|
29
|
+
let currentBodySize = 0;
|
|
30
|
+
options2.request.emit = (event, ...args) => {
|
|
31
|
+
if (event === "data") {
|
|
32
|
+
checkHeader();
|
|
33
|
+
currentBodySize += args[0].length;
|
|
34
|
+
if (currentBodySize > this.maxBodySize) {
|
|
35
|
+
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return originalEmit.call(options2.request, event, ...args);
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
return await options2.next(options2);
|
|
42
|
+
} finally {
|
|
43
|
+
options2.request.emit = originalEmit;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class CompressionPlugin {
|
|
50
|
+
compressionHandler;
|
|
51
|
+
constructor(options = {}) {
|
|
52
|
+
this.compressionHandler = compression({
|
|
53
|
+
...options,
|
|
54
|
+
filter: (req, res) => {
|
|
55
|
+
const hasContentDisposition = res.hasHeader("content-disposition");
|
|
56
|
+
const contentType = res.getHeader("content-type")?.toString();
|
|
57
|
+
if (!hasContentDisposition && contentType?.startsWith("text/event-stream")) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return options.filter ? options.filter(req, res) : compression.filter(req, res);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
initRuntimeAdapter(options) {
|
|
65
|
+
options.adapterInterceptors ??= [];
|
|
66
|
+
options.adapterInterceptors.unshift(async (options2) => {
|
|
67
|
+
let resolve;
|
|
68
|
+
let reject;
|
|
69
|
+
const promise = new Promise((res, rej) => {
|
|
70
|
+
resolve = res;
|
|
71
|
+
reject = rej;
|
|
72
|
+
});
|
|
73
|
+
const originalWrite = options2.response.write;
|
|
74
|
+
const originalEnd = options2.response.end;
|
|
75
|
+
const originalOn = options2.response.on;
|
|
76
|
+
this.compressionHandler(options2.request, options2.response, async (err) => {
|
|
77
|
+
if (err) {
|
|
78
|
+
reject(err);
|
|
79
|
+
} else {
|
|
80
|
+
try {
|
|
81
|
+
resolve(await options2.next());
|
|
82
|
+
} catch (error) {
|
|
83
|
+
reject(error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
return await promise;
|
|
89
|
+
} finally {
|
|
90
|
+
options2.response.write = originalWrite;
|
|
91
|
+
options2.response.end = originalEnd;
|
|
92
|
+
options2.response.on = originalOn;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
class CompositeNodeHttpHandlerPlugin extends CompositeStandardHandlerPlugin {
|
|
99
|
+
initRuntimeAdapter(options) {
|
|
100
|
+
for (const plugin of this.plugins) {
|
|
101
|
+
plugin.initRuntimeAdapter?.(options);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class NodeHttpHandler {
|
|
107
|
+
sendStandardResponseOptions;
|
|
108
|
+
adapterInterceptors;
|
|
109
|
+
standardHandler;
|
|
110
|
+
constructor(router, options = {}) {
|
|
111
|
+
this.standardHandler = new StandardHandler(router, options);
|
|
112
|
+
const plugin = new CompositeNodeHttpHandlerPlugin(options.plugins);
|
|
113
|
+
plugin.initRuntimeAdapter(options);
|
|
114
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
|
115
|
+
this.sendStandardResponseOptions = options;
|
|
116
|
+
}
|
|
117
|
+
async handle(request, response, ...rest) {
|
|
118
|
+
return intercept(
|
|
119
|
+
this.adapterInterceptors,
|
|
120
|
+
{
|
|
121
|
+
...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
|
|
122
|
+
request,
|
|
123
|
+
response,
|
|
124
|
+
sendStandardResponseOptions: this.sendStandardResponseOptions
|
|
125
|
+
},
|
|
126
|
+
async ({ request: request2, response: response2, sendStandardResponseOptions, ...options }) => {
|
|
127
|
+
const standardRequest = toStandardLazyRequest(request2, response2);
|
|
128
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
|
129
|
+
if (!result.matched) {
|
|
130
|
+
return { matched: false };
|
|
131
|
+
}
|
|
132
|
+
await sendStandardResponse(response2, result.response, sendStandardResponseOptions);
|
|
133
|
+
return { matched: true };
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { BodyLimitPlugin, CompositeNodeHttpHandlerPlugin, CompressionPlugin, NodeHttpHandler };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { C as CompositeStandardHandlerPlugin, c as StandardHandleOptions, d as StandardHandleResult, e as StandardHandler, S as StandardHandlerInterceptorOptions, b as StandardHandlerOptions, a as StandardHandlerPlugin } from '../../shared/server.C1fnTLq0.mjs';
|
|
2
|
+
import { AnyProcedure, AnyRouter } from '@temporary-name/server';
|
|
3
|
+
import { StandardCodec as StandardCodec$1, StandardParams as StandardParams$1, StandardMatcher as StandardMatcher$1, StandardMatchResult as StandardMatchResult$1 } from '@temporary-name/server/standard';
|
|
4
|
+
import { ORPCError, HTTPPath } from '@temporary-name/shared';
|
|
5
|
+
import { StandardLazyRequest, StandardResponse } from '@temporary-name/standard-server';
|
|
6
|
+
import { h as AnyProcedure$1, f as AnyRouter$1 } from '../../shared/server.BKSOrA6h.mjs';
|
|
7
|
+
export { F as FriendlyStandardHandleOptions, d as decodeParams, r as resolveFriendlyStandardHandleOptions, t as toRou3Pattern } from '../../shared/server.BeuTpcmO.mjs';
|
|
8
|
+
import '@temporary-name/contract';
|
|
9
|
+
|
|
10
|
+
declare class StandardOpenAPICodec implements StandardCodec$1 {
|
|
11
|
+
#private;
|
|
12
|
+
constructor();
|
|
13
|
+
decode(request: StandardLazyRequest, params: StandardParams$1 | undefined, procedure: AnyProcedure): Promise<unknown>;
|
|
14
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
|
15
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare class StandardOpenAPIMatcher implements StandardMatcher$1 {
|
|
19
|
+
private readonly tree;
|
|
20
|
+
private pendingRouters;
|
|
21
|
+
init(router: AnyRouter, path?: readonly string[]): void;
|
|
22
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult$1>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type StandardParams = Record<string, string>;
|
|
26
|
+
type StandardMatchResult = {
|
|
27
|
+
path: readonly string[];
|
|
28
|
+
procedure: AnyProcedure$1;
|
|
29
|
+
params?: StandardParams;
|
|
30
|
+
} | undefined;
|
|
31
|
+
interface StandardMatcher {
|
|
32
|
+
init(router: AnyRouter$1): void;
|
|
33
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
|
34
|
+
}
|
|
35
|
+
interface StandardCodec {
|
|
36
|
+
encode(output: unknown, procedure: AnyProcedure$1): StandardResponse;
|
|
37
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
|
38
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure$1): Promise<unknown>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { StandardOpenAPICodec, StandardOpenAPIMatcher };
|
|
42
|
+
export type { StandardCodec, StandardMatchResult, StandardMatcher, StandardParams };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { C as CompositeStandardHandlerPlugin, c as StandardHandleOptions, d as StandardHandleResult, e as StandardHandler, S as StandardHandlerInterceptorOptions, b as StandardHandlerOptions, a as StandardHandlerPlugin } from '../../shared/server.CQyYNJ1H.js';
|
|
2
|
+
import { AnyProcedure, AnyRouter } from '@temporary-name/server';
|
|
3
|
+
import { StandardCodec as StandardCodec$1, StandardParams as StandardParams$1, StandardMatcher as StandardMatcher$1, StandardMatchResult as StandardMatchResult$1 } from '@temporary-name/server/standard';
|
|
4
|
+
import { ORPCError, HTTPPath } from '@temporary-name/shared';
|
|
5
|
+
import { StandardLazyRequest, StandardResponse } from '@temporary-name/standard-server';
|
|
6
|
+
import { h as AnyProcedure$1, f as AnyRouter$1 } from '../../shared/server.BKSOrA6h.js';
|
|
7
|
+
export { F as FriendlyStandardHandleOptions, d as decodeParams, r as resolveFriendlyStandardHandleOptions, t as toRou3Pattern } from '../../shared/server.SLLuK6_v.js';
|
|
8
|
+
import '@temporary-name/contract';
|
|
9
|
+
|
|
10
|
+
declare class StandardOpenAPICodec implements StandardCodec$1 {
|
|
11
|
+
#private;
|
|
12
|
+
constructor();
|
|
13
|
+
decode(request: StandardLazyRequest, params: StandardParams$1 | undefined, procedure: AnyProcedure): Promise<unknown>;
|
|
14
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
|
15
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare class StandardOpenAPIMatcher implements StandardMatcher$1 {
|
|
19
|
+
private readonly tree;
|
|
20
|
+
private pendingRouters;
|
|
21
|
+
init(router: AnyRouter, path?: readonly string[]): void;
|
|
22
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult$1>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type StandardParams = Record<string, string>;
|
|
26
|
+
type StandardMatchResult = {
|
|
27
|
+
path: readonly string[];
|
|
28
|
+
procedure: AnyProcedure$1;
|
|
29
|
+
params?: StandardParams;
|
|
30
|
+
} | undefined;
|
|
31
|
+
interface StandardMatcher {
|
|
32
|
+
init(router: AnyRouter$1): void;
|
|
33
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
|
34
|
+
}
|
|
35
|
+
interface StandardCodec {
|
|
36
|
+
encode(output: unknown, procedure: AnyProcedure$1): StandardResponse;
|
|
37
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
|
38
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure$1): Promise<unknown>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { StandardOpenAPICodec, StandardOpenAPIMatcher };
|
|
42
|
+
export type { StandardCodec, StandardMatchResult, StandardMatcher, StandardParams };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { C as CompositeStandardHandlerPlugin, S as StandardHandler, a as StandardOpenAPICodec, b as StandardOpenAPIMatcher, d as decodeParams, r as resolveFriendlyStandardHandleOptions, t as toRou3Pattern } from '../../shared/server.DLsti1Pv.mjs';
|
|
2
|
+
import '@temporary-name/shared';
|
|
3
|
+
import '@temporary-name/standard-server';
|
|
4
|
+
import '../../shared/server.BKh8I1Ny.mjs';
|
|
5
|
+
import '@temporary-name/contract';
|
|
6
|
+
import 'node:async_hooks';
|
|
7
|
+
import 'zod';
|
|
8
|
+
import 'zod/v4/core';
|
|
9
|
+
import '../../shared/server.BEHw7Eyx.mjs';
|
|
10
|
+
import '@temporary-name/server';
|
|
11
|
+
import 'rou3';
|
|
@@ -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<ArrayBuffer> | 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<ArrayBuffer> | 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 };
|