@temporary-name/server 1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8
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 +24 -0
- package/dist/adapters/aws-lambda/index.d.ts +24 -0
- package/dist/adapters/aws-lambda/index.mjs +23 -0
- package/dist/adapters/fetch/index.d.mts +102 -0
- package/dist/adapters/fetch/index.d.ts +102 -0
- package/dist/adapters/fetch/index.mjs +171 -0
- package/dist/adapters/node/index.d.mts +77 -0
- package/dist/adapters/node/index.d.ts +77 -0
- package/dist/adapters/node/index.mjs +136 -0
- package/dist/adapters/standard/index.d.mts +16 -0
- package/dist/adapters/standard/index.d.ts +16 -0
- package/dist/adapters/standard/index.mjs +101 -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 +705 -0
- package/dist/index.d.ts +705 -0
- package/dist/index.mjs +618 -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.BEQrAa3A.mjs +207 -0
- package/dist/shared/server.Bo94xDTv.d.mts +73 -0
- package/dist/shared/server.Btxrgkj5.d.ts +73 -0
- package/dist/shared/server.C1YnHvvf.d.mts +192 -0
- package/dist/shared/server.C1YnHvvf.d.ts +192 -0
- package/dist/shared/server.D6K9uoPI.mjs +35 -0
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.X0YaZxSJ.mjs +13 -0
- package/package.json +89 -0
|
@@ -0,0 +1,136 @@
|
|
|
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 { r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DZ5BIITo.mjs';
|
|
5
|
+
import '@temporary-name/standard-server';
|
|
6
|
+
import '@temporary-name/contract';
|
|
7
|
+
import '../../shared/server.D6K9uoPI.mjs';
|
|
8
|
+
import { C as CompositeStandardHandlerPlugin } from '../../shared/server.X0YaZxSJ.mjs';
|
|
9
|
+
import 'node:async_hooks';
|
|
10
|
+
import 'zod';
|
|
11
|
+
import 'zod/v4/core';
|
|
12
|
+
|
|
13
|
+
class BodyLimitPlugin {
|
|
14
|
+
maxBodySize;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.maxBodySize = options.maxBodySize;
|
|
17
|
+
}
|
|
18
|
+
initRuntimeAdapter(options) {
|
|
19
|
+
options.adapterInterceptors ??= [];
|
|
20
|
+
options.adapterInterceptors.push(async (options2) => {
|
|
21
|
+
const checkHeader = once(() => {
|
|
22
|
+
if (Number(options2.request.headers["content-length"]) > this.maxBodySize) {
|
|
23
|
+
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const originalEmit = options2.request.emit;
|
|
27
|
+
let currentBodySize = 0;
|
|
28
|
+
options2.request.emit = (event, ...args) => {
|
|
29
|
+
if (event === "data") {
|
|
30
|
+
checkHeader();
|
|
31
|
+
currentBodySize += args[0].length;
|
|
32
|
+
if (currentBodySize > this.maxBodySize) {
|
|
33
|
+
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return originalEmit.call(options2.request, event, ...args);
|
|
37
|
+
};
|
|
38
|
+
try {
|
|
39
|
+
return await options2.next(options2);
|
|
40
|
+
} finally {
|
|
41
|
+
options2.request.emit = originalEmit;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class CompressionPlugin {
|
|
48
|
+
compressionHandler;
|
|
49
|
+
constructor(options = {}) {
|
|
50
|
+
this.compressionHandler = compression({
|
|
51
|
+
...options,
|
|
52
|
+
filter: (req, res) => {
|
|
53
|
+
const hasContentDisposition = res.hasHeader("content-disposition");
|
|
54
|
+
const contentType = res.getHeader("content-type")?.toString();
|
|
55
|
+
if (!hasContentDisposition && contentType?.startsWith("text/event-stream")) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return options.filter ? options.filter(req, res) : compression.filter(req, res);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
initRuntimeAdapter(options) {
|
|
63
|
+
options.adapterInterceptors ??= [];
|
|
64
|
+
options.adapterInterceptors.unshift(async (options2) => {
|
|
65
|
+
let resolve;
|
|
66
|
+
let reject;
|
|
67
|
+
const promise = new Promise((res, rej) => {
|
|
68
|
+
resolve = res;
|
|
69
|
+
reject = rej;
|
|
70
|
+
});
|
|
71
|
+
const originalWrite = options2.response.write;
|
|
72
|
+
const originalEnd = options2.response.end;
|
|
73
|
+
const originalOn = options2.response.on;
|
|
74
|
+
this.compressionHandler(options2.request, options2.response, async (err) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
reject(err);
|
|
77
|
+
} else {
|
|
78
|
+
try {
|
|
79
|
+
resolve(await options2.next());
|
|
80
|
+
} catch (error) {
|
|
81
|
+
reject(error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
try {
|
|
86
|
+
return await promise;
|
|
87
|
+
} finally {
|
|
88
|
+
options2.response.write = originalWrite;
|
|
89
|
+
options2.response.end = originalEnd;
|
|
90
|
+
options2.response.on = originalOn;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class CompositeNodeHttpHandlerPlugin extends CompositeStandardHandlerPlugin {
|
|
97
|
+
initRuntimeAdapter(options) {
|
|
98
|
+
for (const plugin of this.plugins) {
|
|
99
|
+
plugin.initRuntimeAdapter?.(options);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class NodeHttpHandler {
|
|
105
|
+
constructor(standardHandler, options = {}) {
|
|
106
|
+
this.standardHandler = standardHandler;
|
|
107
|
+
const plugin = new CompositeNodeHttpHandlerPlugin(options.plugins);
|
|
108
|
+
plugin.initRuntimeAdapter(options);
|
|
109
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
|
110
|
+
this.sendStandardResponseOptions = options;
|
|
111
|
+
}
|
|
112
|
+
sendStandardResponseOptions;
|
|
113
|
+
adapterInterceptors;
|
|
114
|
+
async handle(request, response, ...rest) {
|
|
115
|
+
return intercept(
|
|
116
|
+
this.adapterInterceptors,
|
|
117
|
+
{
|
|
118
|
+
...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
|
|
119
|
+
request,
|
|
120
|
+
response,
|
|
121
|
+
sendStandardResponseOptions: this.sendStandardResponseOptions
|
|
122
|
+
},
|
|
123
|
+
async ({ request: request2, response: response2, sendStandardResponseOptions, ...options }) => {
|
|
124
|
+
const standardRequest = toStandardLazyRequest(request2, response2);
|
|
125
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
|
126
|
+
if (!result.matched) {
|
|
127
|
+
return { matched: false };
|
|
128
|
+
}
|
|
129
|
+
await sendStandardResponse(response2, result.response, sendStandardResponseOptions);
|
|
130
|
+
return { matched: true };
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export { BodyLimitPlugin, CompositeNodeHttpHandlerPlugin, CompressionPlugin, NodeHttpHandler };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { b as StandardHandleOptions } from '../../shared/server.Bo94xDTv.mjs';
|
|
2
|
+
export { C as CompositeStandardHandlerPlugin, i as StandardCodec, e as StandardHandleResult, S as StandardHandler, c as StandardHandlerInterceptorOptions, d as StandardHandlerOptions, a as StandardHandlerPlugin, g as StandardMatchResult, h as StandardMatcher, f as StandardParams } from '../../shared/server.Bo94xDTv.mjs';
|
|
3
|
+
import { C as Context } from '../../shared/server.C1YnHvvf.mjs';
|
|
4
|
+
import '@temporary-name/contract';
|
|
5
|
+
import '@temporary-name/shared';
|
|
6
|
+
import '@temporary-name/standard-server';
|
|
7
|
+
|
|
8
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
9
|
+
context?: T;
|
|
10
|
+
} : {
|
|
11
|
+
context: T;
|
|
12
|
+
});
|
|
13
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
14
|
+
|
|
15
|
+
export { StandardHandleOptions, resolveFriendlyStandardHandleOptions };
|
|
16
|
+
export type { FriendlyStandardHandleOptions };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { b as StandardHandleOptions } from '../../shared/server.Btxrgkj5.js';
|
|
2
|
+
export { C as CompositeStandardHandlerPlugin, i as StandardCodec, e as StandardHandleResult, S as StandardHandler, c as StandardHandlerInterceptorOptions, d as StandardHandlerOptions, a as StandardHandlerPlugin, g as StandardMatchResult, h as StandardMatcher, f as StandardParams } from '../../shared/server.Btxrgkj5.js';
|
|
3
|
+
import { C as Context } from '../../shared/server.C1YnHvvf.js';
|
|
4
|
+
import '@temporary-name/contract';
|
|
5
|
+
import '@temporary-name/shared';
|
|
6
|
+
import '@temporary-name/standard-server';
|
|
7
|
+
|
|
8
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
9
|
+
context?: T;
|
|
10
|
+
} : {
|
|
11
|
+
context: T;
|
|
12
|
+
});
|
|
13
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
14
|
+
|
|
15
|
+
export { StandardHandleOptions, resolveFriendlyStandardHandleOptions };
|
|
16
|
+
export type { FriendlyStandardHandleOptions };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { flattenHeader } from '@temporary-name/standard-server';
|
|
3
|
+
import { c as createProcedureClient } from '../../shared/server.BEQrAa3A.mjs';
|
|
4
|
+
import { C as CompositeStandardHandlerPlugin } from '../../shared/server.X0YaZxSJ.mjs';
|
|
5
|
+
export { r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DZ5BIITo.mjs';
|
|
6
|
+
import '@temporary-name/contract';
|
|
7
|
+
import '../../shared/server.D6K9uoPI.mjs';
|
|
8
|
+
import 'node:async_hooks';
|
|
9
|
+
import 'zod';
|
|
10
|
+
import 'zod/v4/core';
|
|
11
|
+
|
|
12
|
+
class StandardHandler {
|
|
13
|
+
constructor(router, matcher, codec, options) {
|
|
14
|
+
this.matcher = matcher;
|
|
15
|
+
this.codec = codec;
|
|
16
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
|
17
|
+
plugins.init(options, router);
|
|
18
|
+
this.interceptors = toArray(options.interceptors);
|
|
19
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
20
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
|
21
|
+
this.matcher.init(router);
|
|
22
|
+
}
|
|
23
|
+
interceptors;
|
|
24
|
+
clientInterceptors;
|
|
25
|
+
rootInterceptors;
|
|
26
|
+
async handle(request, options) {
|
|
27
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
|
28
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
|
29
|
+
return { matched: false, response: void 0 };
|
|
30
|
+
}
|
|
31
|
+
return intercept(this.rootInterceptors, { ...options, request, prefix }, async (interceptorOptions) => {
|
|
32
|
+
return runWithSpan({ name: `${request.method} ${request.url.pathname}` }, async (span) => {
|
|
33
|
+
let step;
|
|
34
|
+
try {
|
|
35
|
+
return await intercept(
|
|
36
|
+
this.interceptors,
|
|
37
|
+
interceptorOptions,
|
|
38
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
|
39
|
+
const method = request2.method;
|
|
40
|
+
const url = request2.url;
|
|
41
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
|
42
|
+
const match = await runWithSpan(
|
|
43
|
+
{ name: "find_procedure" },
|
|
44
|
+
() => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
|
|
45
|
+
);
|
|
46
|
+
if (!match) {
|
|
47
|
+
return { matched: false, response: void 0 };
|
|
48
|
+
}
|
|
49
|
+
span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
|
|
50
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
51
|
+
span?.setAttribute("rpc.method", match.path.join("."));
|
|
52
|
+
step = "decode_input";
|
|
53
|
+
let input = await runWithSpan(
|
|
54
|
+
{ name: "decode_input" },
|
|
55
|
+
() => this.codec.decode(request2, match.params, match.procedure)
|
|
56
|
+
);
|
|
57
|
+
step = void 0;
|
|
58
|
+
if (isAsyncIteratorObject(input)) {
|
|
59
|
+
input = asyncIteratorWithSpan(
|
|
60
|
+
{ name: "consume_event_iterator_input", signal: request2.signal },
|
|
61
|
+
input
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const client = createProcedureClient(match.procedure, {
|
|
65
|
+
context,
|
|
66
|
+
path: match.path,
|
|
67
|
+
interceptors: this.clientInterceptors
|
|
68
|
+
});
|
|
69
|
+
step = "call_procedure";
|
|
70
|
+
const output = await client(input, {
|
|
71
|
+
signal: request2.signal,
|
|
72
|
+
lastEventId: flattenHeader(request2.headers["last-event-id"])
|
|
73
|
+
});
|
|
74
|
+
step = void 0;
|
|
75
|
+
const response = this.codec.encode(output, match.procedure);
|
|
76
|
+
return {
|
|
77
|
+
matched: true,
|
|
78
|
+
response
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
if (step !== "call_procedure") {
|
|
84
|
+
setSpanError(span, e);
|
|
85
|
+
}
|
|
86
|
+
const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
|
87
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
88
|
+
cause: e
|
|
89
|
+
}) : toORPCError(e);
|
|
90
|
+
const response = this.codec.encodeError(error);
|
|
91
|
+
return {
|
|
92
|
+
matched: true,
|
|
93
|
+
response
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { CompositeStandardHandlerPlugin, StandardHandler };
|
|
@@ -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 };
|