@ailura/nestjs-hono-adapter 1.0.0
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 +411 -0
- package/dist/body.d.ts +35 -0
- package/dist/body.d.ts.map +1 -0
- package/dist/body.js +180 -0
- package/dist/body.js.map +1 -0
- package/dist/bridge.d.ts +64 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +168 -0
- package/dist/bridge.js.map +1 -0
- package/dist/closing.d.ts +13 -0
- package/dist/closing.d.ts.map +1 -0
- package/dist/closing.js +30 -0
- package/dist/closing.js.map +1 -0
- package/dist/context.d.ts +22 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +2 -0
- package/dist/context.js.map +1 -0
- package/dist/cors-middleware.d.ts +64 -0
- package/dist/cors-middleware.d.ts.map +1 -0
- package/dist/cors-middleware.js +211 -0
- package/dist/cors-middleware.js.map +1 -0
- package/dist/handler-bridge.d.ts +51 -0
- package/dist/handler-bridge.d.ts.map +1 -0
- package/dist/handler-bridge.js +122 -0
- package/dist/handler-bridge.js.map +1 -0
- package/dist/hono-lifecycle.d.ts +90 -0
- package/dist/hono-lifecycle.d.ts.map +1 -0
- package/dist/hono-lifecycle.js +169 -0
- package/dist/hono-lifecycle.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/path.d.ts +3 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +144 -0
- package/dist/path.js.map +1 -0
- package/dist/query.d.ts +19 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +238 -0
- package/dist/query.js.map +1 -0
- package/dist/response-helpers.d.ts +16 -0
- package/dist/response-helpers.d.ts.map +1 -0
- package/dist/response-helpers.js +45 -0
- package/dist/response-helpers.js.map +1 -0
- package/dist/response-writer.d.ts +28 -0
- package/dist/response-writer.d.ts.map +1 -0
- package/dist/response-writer.js +52 -0
- package/dist/response-writer.js.map +1 -0
- package/dist/route-adapter.d.ts +53 -0
- package/dist/route-adapter.d.ts.map +1 -0
- package/dist/route-adapter.js +141 -0
- package/dist/route-adapter.js.map +1 -0
- package/dist/server-adapter.d.ts +106 -0
- package/dist/server-adapter.d.ts.map +1 -0
- package/dist/server-adapter.js +153 -0
- package/dist/server-adapter.js.map +1 -0
- package/dist/sse.d.ts +67 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +211 -0
- package/dist/sse.js.map +1 -0
- package/dist/static-assets.d.ts +39 -0
- package/dist/static-assets.d.ts.map +1 -0
- package/dist/static-assets.js +155 -0
- package/dist/static-assets.js.map +1 -0
- package/dist/version-filter.d.ts +24 -0
- package/dist/version-filter.d.ts.map +1 -0
- package/dist/version-filter.js +107 -0
- package/dist/version-filter.js.map +1 -0
- package/dist/versioned-route.d.ts +21 -0
- package/dist/versioned-route.d.ts.map +1 -0
- package/dist/versioned-route.js +15 -0
- package/dist/versioned-route.js.map +1 -0
- package/dist/views.d.ts +42 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +110 -0
- package/dist/views.js.map +1 -0
- package/dist/ws-adapter.d.ts +81 -0
- package/dist/ws-adapter.d.ts.map +1 -0
- package/dist/ws-adapter.js +214 -0
- package/dist/ws-adapter.js.map +1 -0
- package/dist/ws-client.d.ts +68 -0
- package/dist/ws-client.d.ts.map +1 -0
- package/dist/ws-client.js +135 -0
- package/dist/ws-client.js.map +1 -0
- package/dist/ws-server.d.ts +23 -0
- package/dist/ws-server.d.ts.map +1 -0
- package/dist/ws-server.js +37 -0
- package/dist/ws-server.js.map +1 -0
- package/dist/ws.d.ts +14 -0
- package/dist/ws.d.ts.map +1 -0
- package/dist/ws.js +12 -0
- package/dist/ws.js.map +1 -0
- package/package.json +99 -0
- package/src/body.ts +251 -0
- package/src/bridge.ts +308 -0
- package/src/closing.ts +38 -0
- package/src/context.ts +25 -0
- package/src/cors-middleware.ts +347 -0
- package/src/handler-bridge.ts +226 -0
- package/src/hono-lifecycle.ts +259 -0
- package/src/index.ts +27 -0
- package/src/path.ts +169 -0
- package/src/query.ts +304 -0
- package/src/response-helpers.ts +60 -0
- package/src/response-writer.ts +100 -0
- package/src/route-adapter.ts +261 -0
- package/src/server-adapter.ts +294 -0
- package/src/sse.ts +274 -0
- package/src/static-assets.ts +247 -0
- package/src/version-filter.ts +170 -0
- package/src/versioned-route.ts +30 -0
- package/src/views.ts +188 -0
- package/src/ws-adapter.ts +329 -0
- package/src/ws-client.ts +190 -0
- package/src/ws-server.ts +40 -0
- package/src/ws.ts +13 -0
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { NestContext } from './context.ts';
|
|
3
|
+
import type { ParsedQuery } from './query.ts';
|
|
4
|
+
/**
|
|
5
|
+
* The request properties Nest's core reads. Every property is
|
|
6
|
+
* declared, and the ones that can legitimately be absent are
|
|
7
|
+
* written as `| undefined`, so `exactOptionalPropertyTypes`
|
|
8
|
+
* cannot hide a field that was never populated.
|
|
9
|
+
*/
|
|
10
|
+
interface NestRequest {
|
|
11
|
+
method: string;
|
|
12
|
+
url: string;
|
|
13
|
+
originalUrl: string;
|
|
14
|
+
path: string;
|
|
15
|
+
hostname: string;
|
|
16
|
+
protocol: string;
|
|
17
|
+
secure: boolean;
|
|
18
|
+
ip: string | undefined;
|
|
19
|
+
ips: string[];
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
query: ParsedQuery;
|
|
22
|
+
params: Record<string, string>;
|
|
23
|
+
hosts: Record<string, string>;
|
|
24
|
+
socket: IncomingMessage['socket'];
|
|
25
|
+
raw: IncomingMessage;
|
|
26
|
+
body: unknown;
|
|
27
|
+
rawBody: Buffer | undefined;
|
|
28
|
+
session: unknown;
|
|
29
|
+
files: Record<string, unknown> | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Continues to whatever handles the request next. The adapter
|
|
33
|
+
* contract types it as returning `void` while Hono returns a
|
|
34
|
+
* promise, so it is kept as `unknown` and the result ignored.
|
|
35
|
+
*/
|
|
36
|
+
type NextHandler = () => unknown;
|
|
37
|
+
/**
|
|
38
|
+
* A handler as Nest registers it: the route proxy, a middleware
|
|
39
|
+
* or the not-found proxy all share this shape.
|
|
40
|
+
*/
|
|
41
|
+
type NestHandler = (request: NestRequest, response: NestContext, next: NextHandler) => unknown;
|
|
42
|
+
/** How much of a proxy's word the deployment believes. */
|
|
43
|
+
interface RequestOptions {
|
|
44
|
+
readonly trustProxy: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Maps a Hono context onto the request object Nest expects.
|
|
48
|
+
*
|
|
49
|
+
* Nest reads the request as a bag of properties rather than
|
|
50
|
+
* through an interface, so this is the single place where the
|
|
51
|
+
* Web request is translated into that bag.
|
|
52
|
+
*/
|
|
53
|
+
declare function toNestRequest(context: NestContext, options: RequestOptions): NestRequest;
|
|
54
|
+
/**
|
|
55
|
+
* Builds the Web response for a value Nest returned.
|
|
56
|
+
*
|
|
57
|
+
* The headers already recorded on the context are copied first:
|
|
58
|
+
* `@Header()` reaches the adapter through `setHeader()`, which
|
|
59
|
+
* stores them on the context, and replacing the response would
|
|
60
|
+
* otherwise drop them.
|
|
61
|
+
*/
|
|
62
|
+
declare function buildResponse(context: NestContext, body: unknown, status?: number): Response;
|
|
63
|
+
export { buildResponse, toNestRequest, type NestHandler, type NestRequest, type NextHandler, type RequestOptions, };
|
|
64
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAKjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,KAAK,EAAE,W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aAAa,CACpB,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,OAAO,EACb,MAAM,GAAE,MAAsB,GAC7B,QAAQ,CAMV;AAED,OAAO,EACL,aAAa,EACb,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,CAAC"}
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { HttpStatus, StreamableFile } from '@nestjs/common';
|
|
3
|
+
import { parseQuery } from './query.js';
|
|
4
|
+
const JSON_CONTENT_TYPE = 'application/json; charset=UTF-8';
|
|
5
|
+
const TEXT_CONTENT_TYPE = 'text/plain; charset=UTF-8';
|
|
6
|
+
/** The header a proxy sets with the protocol it received. */
|
|
7
|
+
const FORWARDED_PROTO = 'x-forwarded-proto';
|
|
8
|
+
/** The header a proxy sets with the host it received. */
|
|
9
|
+
const FORWARDED_HOST = 'x-forwarded-host';
|
|
10
|
+
/** The header a proxy sets with the address it received from. */
|
|
11
|
+
const FORWARDED_FOR = 'x-forwarded-for';
|
|
12
|
+
function isPrimitive(body) {
|
|
13
|
+
const kind = typeof body;
|
|
14
|
+
return (kind === 'string' ||
|
|
15
|
+
kind === 'number' ||
|
|
16
|
+
kind === 'boolean' ||
|
|
17
|
+
kind === 'bigint');
|
|
18
|
+
}
|
|
19
|
+
function isBinaryBody(body) {
|
|
20
|
+
return (body instanceof Uint8Array ||
|
|
21
|
+
body instanceof ArrayBuffer ||
|
|
22
|
+
body instanceof ReadableStream);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reads one forwarded header as the list it is: a proxy may
|
|
26
|
+
* append to it, and the first entry is the one closest to the
|
|
27
|
+
* client.
|
|
28
|
+
*/
|
|
29
|
+
function forwarded(context, name) {
|
|
30
|
+
const header = context.req.header(name);
|
|
31
|
+
if (header === undefined) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
return header
|
|
35
|
+
.split(',')
|
|
36
|
+
.map((value) => value.trim())
|
|
37
|
+
.filter((value) => value !== '');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Reads a forwarded header only when the deployment says a
|
|
41
|
+
* proxy sets it: a client that can write it can claim any
|
|
42
|
+
* address it likes.
|
|
43
|
+
*/
|
|
44
|
+
function trustedValues(context, name, trustProxy) {
|
|
45
|
+
if (!trustProxy) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
return forwarded(context, name);
|
|
49
|
+
}
|
|
50
|
+
function forwardedValues(context, options) {
|
|
51
|
+
const { trustProxy } = options;
|
|
52
|
+
return {
|
|
53
|
+
addresses: trustedValues(context, FORWARDED_FOR, trustProxy),
|
|
54
|
+
hosts: trustedValues(context, FORWARDED_HOST, trustProxy),
|
|
55
|
+
protocols: trustedValues(context, FORWARDED_PROTO, trustProxy),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Maps a Hono context onto the request object Nest expects.
|
|
60
|
+
*
|
|
61
|
+
* Nest reads the request as a bag of properties rather than
|
|
62
|
+
* through an interface, so this is the single place where the
|
|
63
|
+
* Web request is translated into that bag.
|
|
64
|
+
*/
|
|
65
|
+
function toNestRequest(context, options) {
|
|
66
|
+
const target = new URL(context.req.url);
|
|
67
|
+
const route = `${target.pathname}${target.search}`;
|
|
68
|
+
const { incoming } = context.env;
|
|
69
|
+
const values = forwardedValues(context, options);
|
|
70
|
+
const [forwardedProtocol] = values.protocols;
|
|
71
|
+
const scheme = forwardedProtocol ?? target.protocol.replace(':', '');
|
|
72
|
+
const [host] = values.hosts;
|
|
73
|
+
const [address] = values.addresses;
|
|
74
|
+
return {
|
|
75
|
+
body: undefined,
|
|
76
|
+
files: undefined,
|
|
77
|
+
headers: context.req.header(),
|
|
78
|
+
hostname: host ?? target.hostname,
|
|
79
|
+
hosts: {},
|
|
80
|
+
ip: address ?? incoming.socket.remoteAddress,
|
|
81
|
+
ips: values.addresses,
|
|
82
|
+
method: context.req.method,
|
|
83
|
+
originalUrl: route,
|
|
84
|
+
params: context.req.param(),
|
|
85
|
+
path: target.pathname,
|
|
86
|
+
protocol: scheme,
|
|
87
|
+
query: parseQuery(target.search),
|
|
88
|
+
raw: incoming,
|
|
89
|
+
rawBody: undefined,
|
|
90
|
+
secure: scheme === 'https',
|
|
91
|
+
session: undefined,
|
|
92
|
+
socket: incoming.socket,
|
|
93
|
+
url: route,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function toTextResponse(body, status, headers) {
|
|
97
|
+
if (!headers.has('content-type')) {
|
|
98
|
+
headers.set('content-type', TEXT_CONTENT_TYPE);
|
|
99
|
+
}
|
|
100
|
+
return new Response(String(body), { headers, status });
|
|
101
|
+
}
|
|
102
|
+
function toList(value) {
|
|
103
|
+
if (typeof value === 'string') {
|
|
104
|
+
return [value];
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Writes the disposition a file declares. Nest accepts a list,
|
|
110
|
+
* which Node treats as one header line per entry, so each entry
|
|
111
|
+
* is appended rather than overwritten.
|
|
112
|
+
*/
|
|
113
|
+
function setDisposition(headers, disposition) {
|
|
114
|
+
for (const value of toList(disposition)) {
|
|
115
|
+
headers.append('content-disposition', value);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Streams a file Nest built, keeping the headers it declares.
|
|
120
|
+
* `StreamableFile` is the one response type that reaches the
|
|
121
|
+
* adapter as a Node stream, so it is converted here rather than
|
|
122
|
+
* buffered.
|
|
123
|
+
*/
|
|
124
|
+
function toFileResponse(file, status, headers) {
|
|
125
|
+
const { type, disposition, length } = file.getHeaders();
|
|
126
|
+
if (type !== '') {
|
|
127
|
+
headers.set('content-type', type);
|
|
128
|
+
}
|
|
129
|
+
if (disposition !== undefined) {
|
|
130
|
+
setDisposition(headers, disposition);
|
|
131
|
+
}
|
|
132
|
+
if (length !== undefined) {
|
|
133
|
+
headers.set('content-length', String(length));
|
|
134
|
+
}
|
|
135
|
+
return new Response(Readable.toWeb(file.getStream()), {
|
|
136
|
+
headers,
|
|
137
|
+
status,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function toResponse(body, status, headers) {
|
|
141
|
+
if (body === undefined || body === null) {
|
|
142
|
+
return new Response(undefined, { headers, status });
|
|
143
|
+
}
|
|
144
|
+
if (body instanceof StreamableFile) {
|
|
145
|
+
return toFileResponse(body, status, headers);
|
|
146
|
+
}
|
|
147
|
+
if (isBinaryBody(body)) {
|
|
148
|
+
return new Response(body, { headers, status });
|
|
149
|
+
}
|
|
150
|
+
if (isPrimitive(body)) {
|
|
151
|
+
return toTextResponse(body, status, headers);
|
|
152
|
+
}
|
|
153
|
+
headers.set('content-type', JSON_CONTENT_TYPE);
|
|
154
|
+
return Response.json(body, { headers, status });
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Builds the Web response for a value Nest returned.
|
|
158
|
+
*
|
|
159
|
+
* The headers already recorded on the context are copied first:
|
|
160
|
+
* `@Header()` reaches the adapter through `setHeader()`, which
|
|
161
|
+
* stores them on the context, and replacing the response would
|
|
162
|
+
* otherwise drop them.
|
|
163
|
+
*/
|
|
164
|
+
function buildResponse(context, body, status = HttpStatus.OK) {
|
|
165
|
+
return toResponse(body, status, new Headers(context.res.headers));
|
|
166
|
+
}
|
|
167
|
+
export { buildResponse, toNestRequest, };
|
|
168
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAG5D,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,iBAAiB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,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;AAClD,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,aAAa,CACpB,OAAoB,EACpB,IAAa,EACb,MAAM,GAAW,UAAU,CAAC,EAAE;IAE9B,OAAO,UAAU,CACf,IAAI,EACJ,MAAM,EACN,IAAI,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CACjC,CAAC;AACJ,CAAC;AAED,OAAO,EACL,aAAa,EACb,aAAa,GAKd,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from 'hono';
|
|
2
|
+
import type { NodeEnv } from './context.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Answers the requests that arrive while the server is closing.
|
|
5
|
+
*
|
|
6
|
+
* A request already in flight has passed this step and is left
|
|
7
|
+
* to finish; one that arrives afterwards is refused instead of
|
|
8
|
+
* being accepted and then dropped when the socket goes away.
|
|
9
|
+
* The step does nothing unless the application asked for it.
|
|
10
|
+
*/
|
|
11
|
+
declare function closingBridge(isClosing: () => boolean): MiddlewareHandler<NodeEnv>;
|
|
12
|
+
export { closingBridge };
|
|
13
|
+
//# sourceMappingURL=closing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"closing.d.ts","sourceRoot":"","sources":["../src/closing.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAE9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAe5C;;;;;;;GAOG;AACH,iBAAS,aAAa,CACpB,SAAS,EAAE,MAAM,OAAO,GACvB,iBAAiB,CAAC,OAAO,CAAC,CAO5B;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
package/dist/closing.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { HttpStatus } from '@nestjs/common';
|
|
2
|
+
/** The answer Nest's own adapters give while shutting down. */
|
|
3
|
+
const CLOSING_BODY = {
|
|
4
|
+
message: 'Service Unavailable',
|
|
5
|
+
statusCode: HttpStatus.SERVICE_UNAVAILABLE,
|
|
6
|
+
};
|
|
7
|
+
/** The answer a request that arrives while closing receives. */
|
|
8
|
+
function closingResponse() {
|
|
9
|
+
return Response.json(CLOSING_BODY, {
|
|
10
|
+
status: HttpStatus.SERVICE_UNAVAILABLE,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Answers the requests that arrive while the server is closing.
|
|
15
|
+
*
|
|
16
|
+
* A request already in flight has passed this step and is left
|
|
17
|
+
* to finish; one that arrives afterwards is refused instead of
|
|
18
|
+
* being accepted and then dropped when the socket goes away.
|
|
19
|
+
* The step does nothing unless the application asked for it.
|
|
20
|
+
*/
|
|
21
|
+
function closingBridge(isClosing) {
|
|
22
|
+
return (_context, next) => {
|
|
23
|
+
if (isClosing()) {
|
|
24
|
+
return Promise.resolve(closingResponse());
|
|
25
|
+
}
|
|
26
|
+
return next();
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export { closingBridge };
|
|
30
|
+
//# sourceMappingURL=closing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"closing.js","sourceRoot":"","sources":["../src/closing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAK5C,+DAA+D;AAC/D,MAAM,YAAY,GAAG;IACnB,OAAO,EAAE,qBAAqB;IAC9B,UAAU,EAAE,UAAU,CAAC,mBAAmB;CAClC,CAAC;AAEX,gEAAgE;AAChE,SAAS,eAAe;IACtB,OAAO,QAAQ,CAAC,IAAI,CAAC,YAAY,EAAE;QACjC,MAAM,EAAE,UAAU,CAAC,mBAAmB;KACvC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,aAAa,CACpB,SAAwB;IAExB,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE;QACxB,IAAI,SAAS,EAAE,EAAE,CAAC;YAChB,OAAO,OAAO,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HttpBindings } from '@hono/node-server';
|
|
2
|
+
import type { Context, Hono } from 'hono';
|
|
3
|
+
/**
|
|
4
|
+
* Environment `@hono/node-server` attaches to every request. It
|
|
5
|
+
* carries the raw Node request and response, which is how the
|
|
6
|
+
* adapter reaches the client socket without depending on them
|
|
7
|
+
* anywhere else.
|
|
8
|
+
*/
|
|
9
|
+
interface NodeEnv {
|
|
10
|
+
Bindings: HttpBindings;
|
|
11
|
+
}
|
|
12
|
+
/** The Hono application, typed with its Node bindings. */
|
|
13
|
+
type NestHono = Hono<NodeEnv>;
|
|
14
|
+
/**
|
|
15
|
+
* A Hono context. It doubles as the response object Nest writes
|
|
16
|
+
* to: keeping the transport in a Web `Response` is what makes
|
|
17
|
+
* the adapter independent of the platform the request arrived
|
|
18
|
+
* on.
|
|
19
|
+
*/
|
|
20
|
+
type NestContext = Context<NodeEnv>;
|
|
21
|
+
export type { NestContext, NestHono, NodeEnv };
|
|
22
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE1C;;;;;GAKG;AACH,UAAU,OAAO;IACf,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED,0DAA0D;AAC1D,KAAK,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;AAE9B;;;;;GAKG;AACH,KAAK,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEpC,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from 'hono';
|
|
2
|
+
import type { NodeEnv } from './context.ts';
|
|
3
|
+
/** The origins a deployment may allow. */
|
|
4
|
+
type AllowedOrigins = boolean | string | RegExp | readonly (string | RegExp)[];
|
|
5
|
+
/**
|
|
6
|
+
* The callback Nest's own options also allow, which decides an
|
|
7
|
+
* origin per request. It is the same shape the `cors` package
|
|
8
|
+
* takes, and the bridge awaits its answer before the middleware
|
|
9
|
+
* runs.
|
|
10
|
+
*/
|
|
11
|
+
type OriginCallback = (requestOrigin: string | undefined, answer: OriginAnswer) => void;
|
|
12
|
+
/** What a callback reports back: an error, or what it allows. */
|
|
13
|
+
type OriginAnswer = (error?: Error | null, origin?: AllowedOrigins) => void;
|
|
14
|
+
/**
|
|
15
|
+
* The CORS options the adapter honours.
|
|
16
|
+
*
|
|
17
|
+
* They are declared here because Nest types the parameter as
|
|
18
|
+
* `any` on both the application and the adapter, so nothing can
|
|
19
|
+
* be taken from a signature. `optionsSuccessStatus` is accepted
|
|
20
|
+
* only when it names the status a preflight is answered with,
|
|
21
|
+
* because the middleware answers it the way the current
|
|
22
|
+
* specification asks for; an application that continues a
|
|
23
|
+
* preflight answers it itself, so the option says nothing
|
|
24
|
+
* then.
|
|
25
|
+
*/
|
|
26
|
+
interface CorsOptions {
|
|
27
|
+
readonly allowedHeaders?: string | readonly string[];
|
|
28
|
+
readonly credentials?: boolean;
|
|
29
|
+
readonly exposedHeaders?: string | readonly string[];
|
|
30
|
+
readonly maxAge?: number;
|
|
31
|
+
readonly methods?: string | readonly string[];
|
|
32
|
+
readonly optionsSuccessStatus?: number;
|
|
33
|
+
readonly origin?: AllowedOrigins | OriginCallback;
|
|
34
|
+
readonly preflightContinue?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Translates the options Nest was given into the middleware the
|
|
38
|
+
* adapter runs. An origin a callback resolved for this request
|
|
39
|
+
* is handed in, because the middleware reads the option once.
|
|
40
|
+
*
|
|
41
|
+
* An answer status other than the one the middleware sends is
|
|
42
|
+
* refused rather than ignored: the adapter answers a preflight
|
|
43
|
+
* itself, and quietly doing something other than what was asked
|
|
44
|
+
* is worse than failing at startup.
|
|
45
|
+
*/
|
|
46
|
+
declare function corsMiddleware(options: CorsOptions, resolved?: AllowedOrigins): MiddlewareHandler<NodeEnv>;
|
|
47
|
+
/**
|
|
48
|
+
* Runs CORS once a deployment has turned it on, and once the
|
|
49
|
+
* origin for this request is known.
|
|
50
|
+
*
|
|
51
|
+
* The options are read when the application is configured,
|
|
52
|
+
* which happens after the chain it belongs to already exists,
|
|
53
|
+
* so the chain holds a step that looks them up instead. A
|
|
54
|
+
* service that never enables CORS simply continues, and no
|
|
55
|
+
* origin is allowed because no header is ever written.
|
|
56
|
+
*
|
|
57
|
+
* A preflight asked to continue is written with its headers and
|
|
58
|
+
* then travels on, which is what an application that answers
|
|
59
|
+
* OPTIONS itself expects.
|
|
60
|
+
*/
|
|
61
|
+
declare function corsBridge(handler: () => CorsOptions | undefined): MiddlewareHandler<NodeEnv>;
|
|
62
|
+
export { corsBridge, corsMiddleware };
|
|
63
|
+
export type { CorsOptions };
|
|
64
|
+
//# sourceMappingURL=cors-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-middleware.d.ts","sourceRoot":"","sources":["../src/cors-middleware.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAG9C,OAAO,KAAK,EAAe,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AAE,CAAC;AACtC,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { promisify } from 'node:util';
|
|
2
|
+
import { cors } from 'hono/cors';
|
|
3
|
+
/**
|
|
4
|
+
* What a browser may send. The CSRF header is named because a
|
|
5
|
+
* request that carries it stops being a simple one: a foreign
|
|
6
|
+
* site cannot make it without a preflight, and the preflight is
|
|
7
|
+
* where this list is read.
|
|
8
|
+
*/
|
|
9
|
+
const ALLOWED_HEADERS = [
|
|
10
|
+
'authorization',
|
|
11
|
+
'content-type',
|
|
12
|
+
'x-csrf-token',
|
|
13
|
+
];
|
|
14
|
+
/** What a browser may call. */
|
|
15
|
+
const ALLOWED_METHODS = [
|
|
16
|
+
'DELETE',
|
|
17
|
+
'GET',
|
|
18
|
+
'HEAD',
|
|
19
|
+
'OPTIONS',
|
|
20
|
+
'PATCH',
|
|
21
|
+
'POST',
|
|
22
|
+
'PUT',
|
|
23
|
+
];
|
|
24
|
+
/** How long a browser may reuse a preflight answer. */
|
|
25
|
+
const MAX_AGE_SECONDS = 600;
|
|
26
|
+
/**
|
|
27
|
+
* The status a preflight is answered with. The specification
|
|
28
|
+
* asks for no content, and the middleware always sends it.
|
|
29
|
+
*/
|
|
30
|
+
const PREFLIGHT_STATUS = 204;
|
|
31
|
+
const NO_HEADERS = [];
|
|
32
|
+
function matchesRule(origin, rule) {
|
|
33
|
+
if (rule instanceof RegExp) {
|
|
34
|
+
return rule.test(origin);
|
|
35
|
+
}
|
|
36
|
+
return rule === origin;
|
|
37
|
+
}
|
|
38
|
+
function isAllowedOrigin(origin, allowed) {
|
|
39
|
+
if (origin === '' ||
|
|
40
|
+
allowed === false ||
|
|
41
|
+
allowed === undefined) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (allowed === true) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (typeof allowed === 'string') {
|
|
48
|
+
return allowed === '*' || allowed === origin;
|
|
49
|
+
}
|
|
50
|
+
if (allowed instanceof RegExp) {
|
|
51
|
+
return allowed.test(origin);
|
|
52
|
+
}
|
|
53
|
+
return allowed.some((candidate) => matchesRule(origin, candidate));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Reads the origin a browser may call from: the origin itself
|
|
57
|
+
* when it is allowed, and nothing when it is not.
|
|
58
|
+
*
|
|
59
|
+
* Hono writes no header for nothing, and a browser that finds
|
|
60
|
+
* no matching header keeps a foreign page from reading the
|
|
61
|
+
* answer. Nothing here refuses a request either — CORS is a
|
|
62
|
+
* rule a browser follows, and a service that enforced it would
|
|
63
|
+
* have to guess at callers that are not browsers at all.
|
|
64
|
+
*/
|
|
65
|
+
function allowedOrigin(allowed) {
|
|
66
|
+
return (origin) => {
|
|
67
|
+
if (isAllowedOrigin(origin, allowed)) {
|
|
68
|
+
return origin;
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Reads a list of header names. Nest accepts the same list as
|
|
75
|
+
* an array or as one comma-separated string, and the string
|
|
76
|
+
* form is what the platform adapters are given, so both are
|
|
77
|
+
* split.
|
|
78
|
+
*/
|
|
79
|
+
/**
|
|
80
|
+
* Reads a list that Nest accepts either as an array or as one
|
|
81
|
+
* comma-separated string. The string form is what the platform
|
|
82
|
+
* adapters are given, so both end up as the same list.
|
|
83
|
+
*/
|
|
84
|
+
function asList(value) {
|
|
85
|
+
if (typeof value === 'string') {
|
|
86
|
+
return [value];
|
|
87
|
+
}
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
function headerList(value, fallback) {
|
|
91
|
+
if (value === undefined) {
|
|
92
|
+
return [...fallback];
|
|
93
|
+
}
|
|
94
|
+
return asList(value)
|
|
95
|
+
.flatMap((entry) => entry.split(','))
|
|
96
|
+
.map((entry) => entry.trim())
|
|
97
|
+
.filter((entry) => entry !== '');
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The origin rules a middleware reads on its own. A callback is
|
|
101
|
+
* not one of them: it answers per request, which the bridge
|
|
102
|
+
* does before the middleware runs.
|
|
103
|
+
*/
|
|
104
|
+
function staticOrigin(origin) {
|
|
105
|
+
if (typeof origin === 'function') {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return origin;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reads the origin a callback decides for one request.
|
|
112
|
+
*
|
|
113
|
+
* The callback answers through a second argument rather than by
|
|
114
|
+
* returning, so its answer is awaited here. An error it reports
|
|
115
|
+
* is thrown, which leaves the request on the path the exception
|
|
116
|
+
* layer already owns.
|
|
117
|
+
*/
|
|
118
|
+
function askOrigin(origin, requestOrigin, answer) {
|
|
119
|
+
origin(requestOrigin, answer);
|
|
120
|
+
}
|
|
121
|
+
const askResolvedOrigin = promisify(askOrigin);
|
|
122
|
+
/**
|
|
123
|
+
* The origin this request is allowed: the configured value
|
|
124
|
+
* itself, unless a callback decides it per request, in which
|
|
125
|
+
* case the callback is asked and its answer awaited.
|
|
126
|
+
*/
|
|
127
|
+
function resolveOrigin(origin, requestOrigin) {
|
|
128
|
+
if (typeof origin !== 'function') {
|
|
129
|
+
return Promise.resolve(origin);
|
|
130
|
+
}
|
|
131
|
+
return askResolvedOrigin(origin, requestOrigin);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Translates the options Nest was given into the middleware the
|
|
135
|
+
* adapter runs. An origin a callback resolved for this request
|
|
136
|
+
* is handed in, because the middleware reads the option once.
|
|
137
|
+
*
|
|
138
|
+
* An answer status other than the one the middleware sends is
|
|
139
|
+
* refused rather than ignored: the adapter answers a preflight
|
|
140
|
+
* itself, and quietly doing something other than what was asked
|
|
141
|
+
* is worse than failing at startup.
|
|
142
|
+
*/
|
|
143
|
+
function corsMiddleware(options, resolved) {
|
|
144
|
+
const named = resolved ?? staticOrigin(options.origin);
|
|
145
|
+
if (options.preflightContinue !== true &&
|
|
146
|
+
options.optionsSuccessStatus !== undefined &&
|
|
147
|
+
options.optionsSuccessStatus !== PREFLIGHT_STATUS) {
|
|
148
|
+
throw new TypeError('The Hono adapter answers CORS preflight requests with ' +
|
|
149
|
+
`${PREFLIGHT_STATUS}, so optionsSuccessStatus cannot ` +
|
|
150
|
+
'name another status.');
|
|
151
|
+
}
|
|
152
|
+
return cors({
|
|
153
|
+
allowHeaders: headerList(options.allowedHeaders, ALLOWED_HEADERS),
|
|
154
|
+
allowMethods: headerList(options.methods, ALLOWED_METHODS),
|
|
155
|
+
credentials: options.credentials ?? false,
|
|
156
|
+
exposeHeaders: headerList(options.exposedHeaders, NO_HEADERS),
|
|
157
|
+
maxAge: options.maxAge ?? MAX_AGE_SECONDS,
|
|
158
|
+
origin: allowedOrigin(named),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** A step that ends the chain, for reading a preflight answer. */
|
|
162
|
+
const NOOP_NEXT = () => Promise.resolve();
|
|
163
|
+
/** Says whether this request is a preflight the router answers. */
|
|
164
|
+
function isContinuedPreflight(options, context) {
|
|
165
|
+
return (options.preflightContinue === true &&
|
|
166
|
+
context.req.method === 'OPTIONS');
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Writes the headers a preflight would have carried, so the
|
|
170
|
+
* request can travel on to the route that answers it.
|
|
171
|
+
*/
|
|
172
|
+
async function forwardPreflight(context, middleware) {
|
|
173
|
+
const answer = await middleware(context, NOOP_NEXT);
|
|
174
|
+
if (answer === undefined) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
for (const [name, value] of answer.headers) {
|
|
178
|
+
context.header(name, value);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Runs CORS once a deployment has turned it on, and once the
|
|
183
|
+
* origin for this request is known.
|
|
184
|
+
*
|
|
185
|
+
* The options are read when the application is configured,
|
|
186
|
+
* which happens after the chain it belongs to already exists,
|
|
187
|
+
* so the chain holds a step that looks them up instead. A
|
|
188
|
+
* service that never enables CORS simply continues, and no
|
|
189
|
+
* origin is allowed because no header is ever written.
|
|
190
|
+
*
|
|
191
|
+
* A preflight asked to continue is written with its headers and
|
|
192
|
+
* then travels on, which is what an application that answers
|
|
193
|
+
* OPTIONS itself expects.
|
|
194
|
+
*/
|
|
195
|
+
function corsBridge(handler) {
|
|
196
|
+
return async (context, next) => {
|
|
197
|
+
const options = handler();
|
|
198
|
+
if (options === undefined) {
|
|
199
|
+
return next();
|
|
200
|
+
}
|
|
201
|
+
const allowed = await resolveOrigin(options.origin, context.req.header('origin'));
|
|
202
|
+
const middleware = corsMiddleware(options, allowed);
|
|
203
|
+
if (isContinuedPreflight(options, context)) {
|
|
204
|
+
await forwardPreflight(context, middleware);
|
|
205
|
+
return next();
|
|
206
|
+
}
|
|
207
|
+
return middleware(context, next);
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export { corsBridge, corsMiddleware };
|
|
211
|
+
//# sourceMappingURL=cors-middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-middleware.js","sourceRoot":"","sources":["../src/cors-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAmDjC;;;;;GAKG;AACH,MAAM,eAAe,GAAG;IACtB,eAAe;IACf,cAAc;IACd,cAAc;CACf,CAAC;AAEF,+BAA+B;AAC/B,MAAM,eAAe,GAAG;IACtB,QAAQ;IACR,KAAK;IACL,MAAM;IACN,SAAS;IACT,OAAO;IACP,MAAM;IACN,KAAK;CACN,CAAC;AAEF,uDAAuD;AACvD,MAAM,eAAe,GAAG,GAAG,CAAC;AAE5B;;;GAGG;AACH,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,MAAM,UAAU,GAAsB,EAAE,CAAC;AAEzC,SAAS,WAAW,CAClB,MAAc,EACd,IAAqB;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