@trpc/server 11.0.0-rc.361 → 11.0.0-rc.362
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/dist/@trpc/server/http.d.ts +7 -5
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -2
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts +13 -0
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/getPlanner.js +143 -0
- package/dist/adapters/aws-lambda/getPlanner.mjs +141 -0
- package/dist/adapters/aws-lambda/index.d.ts +12 -9
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +7 -76
- package/dist/adapters/aws-lambda/index.mjs +9 -72
- package/dist/adapters/express.js +1 -0
- package/dist/adapters/express.mjs +1 -0
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +19 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +12 -63
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -64
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +28 -85
- package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -86
- package/dist/adapters/fetch/types.d.ts +2 -2
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.js +1 -1
- package/dist/adapters/next.mjs +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
- package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
- package/dist/adapters/node-http/index.d.ts +1 -0
- package/dist/adapters/node-http/index.d.ts.map +1 -1
- package/dist/adapters/node-http/index.js +2 -0
- package/dist/adapters/node-http/index.mjs +1 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +27 -70
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -71
- package/dist/adapters/node-http/types.d.ts +7 -8
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/ws.js +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/bundle-analysis.json +180 -277
- package/dist/http.js +4 -2
- package/dist/http.mjs +2 -1
- package/dist/index.js +2 -4
- package/dist/index.mjs +1 -2
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -20
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +180 -0
- package/dist/unstable-core-do-not-import/http/contentType.mjs +178 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +7 -43
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +119 -134
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +119 -134
- package/dist/unstable-core-do-not-import/http/types.d.ts +25 -27
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -9
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +2 -0
- package/dist/unstable-core-do-not-import/utils.mjs +2 -1
- package/dist/unstable-core-do-not-import.d.ts +9 -3
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +11 -7
- package/dist/unstable-core-do-not-import.mjs +7 -5
- package/package.json +3 -13
- package/src/@trpc/server/http.ts +7 -12
- package/src/@trpc/server/index.ts +0 -3
- package/src/adapters/aws-lambda/getPlanner.ts +191 -0
- package/src/adapters/aws-lambda/index.ts +43 -107
- package/src/adapters/express.ts +1 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +40 -89
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +35 -111
- package/src/adapters/fetch/types.ts +4 -2
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
- package/src/adapters/node-http/index.ts +1 -0
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -97
- package/src/adapters/node-http/types.ts +27 -37
- package/src/adapters/standalone.ts +1 -1
- package/src/unstable-core-do-not-import/http/contentType.ts +214 -22
- package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +135 -210
- package/src/unstable-core-do-not-import/http/types.ts +25 -30
- package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
- package/src/unstable-core-do-not-import/rootConfig.ts +0 -10
- package/src/unstable-core-do-not-import/utils.ts +4 -0
- package/src/unstable-core-do-not-import.ts +9 -3
- package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
- package/adapters/node-http/content-type/form-data/index.js +0 -1
- package/adapters/node-http/content-type/json/index.d.ts +0 -1
- package/adapters/node-http/content-type/json/index.js +0 -1
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts +0 -10
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/content-type/json/index.js +0 -62
- package/dist/adapters/aws-lambda/content-type/json/index.mjs +0 -60
- package/dist/adapters/aws-lambda/utils.d.ts +0 -31
- package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/utils.js +0 -111
- package/dist/adapters/aws-lambda/utils.mjs +0 -103
- package/dist/adapters/content-handlers/concurrentCache.d.ts +0 -7
- package/dist/adapters/content-handlers/concurrentCache.d.ts.map +0 -1
- package/dist/adapters/content-handlers/concurrentCache.js +0 -17
- package/dist/adapters/content-handlers/concurrentCache.mjs +0 -15
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +0 -10
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +0 -1
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +0 -33
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +0 -31
- package/dist/adapters/fastify/content-type/json/index.d.ts +0 -8
- package/dist/adapters/fastify/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/fastify/content-type/json/index.js +0 -62
- package/dist/adapters/fastify/content-type/json/index.mjs +0 -60
- package/dist/adapters/fastify/types.d.ts +0 -11
- package/dist/adapters/fastify/types.d.ts.map +0 -1
- package/dist/adapters/fetch/content-type/json/index.d.ts +0 -9
- package/dist/adapters/fetch/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/fetch/content-type/json/index.js +0 -55
- package/dist/adapters/fetch/content-type/json/index.mjs +0 -53
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +0 -32
- package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -30
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -45
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -43
- package/dist/adapters/node-http/content-type/json/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/index.js +0 -68
- package/dist/adapters/node-http/content-type/json/index.mjs +0 -66
- package/dist/adapters/node-http/content-type/octet/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/octet/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/octet/index.js +0 -19
- package/dist/adapters/node-http/content-type/octet/index.mjs +0 -17
- package/dist/adapters/node-http/content-type/types.d.ts +0 -8
- package/dist/adapters/node-http/content-type/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +0 -16
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/contentTypeParsers.js +0 -23
- package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +0 -21
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -11
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
- package/src/adapters/aws-lambda/content-type/json/index.ts +0 -108
- package/src/adapters/aws-lambda/utils.ts +0 -170
- package/src/adapters/content-handlers/concurrentCache.ts +0 -16
- package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +0 -45
- package/src/adapters/fastify/content-type/json/index.ts +0 -106
- package/src/adapters/fastify/types.ts +0 -22
- package/src/adapters/fetch/content-type/json/index.ts +0 -90
- package/src/adapters/node-http/content-type/form-data/index.ts +0 -37
- package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -49
- package/src/adapters/node-http/content-type/json/index.ts +0 -100
- package/src/adapters/node-http/content-type/octet/index.ts +0 -27
- package/src/adapters/node-http/content-type/types.ts +0 -19
- package/src/unstable-core-do-not-import/contentTypeParsers.ts +0 -37
- package/src/unstable-core-do-not-import/http/index.ts +0 -28
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { TRPCError } from '../../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
2
|
+
import '../../unstable-core-do-not-import/rootConfig.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert an incoming message to a body stream with a max size
|
|
6
|
+
*/ function incomingMessageToBodyStream(req, opts) {
|
|
7
|
+
let size = 0;
|
|
8
|
+
const maxBodySize = opts.maxBodySize;
|
|
9
|
+
let controller = null;
|
|
10
|
+
const stream = new ReadableStream({
|
|
11
|
+
start (c) {
|
|
12
|
+
controller = c;
|
|
13
|
+
},
|
|
14
|
+
async pull (c) {
|
|
15
|
+
const chunk = req.read();
|
|
16
|
+
if (chunk) {
|
|
17
|
+
size += chunk.length;
|
|
18
|
+
}
|
|
19
|
+
if (maxBodySize !== null && size > maxBodySize) {
|
|
20
|
+
controller.error(new TRPCError({
|
|
21
|
+
code: 'PAYLOAD_TOO_LARGE'
|
|
22
|
+
}));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (chunk === null) {
|
|
26
|
+
c.close();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
controller.enqueue(chunk);
|
|
30
|
+
},
|
|
31
|
+
cancel () {
|
|
32
|
+
req.destroy();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return stream;
|
|
36
|
+
}
|
|
37
|
+
const bodyMethods = [
|
|
38
|
+
'POST',
|
|
39
|
+
'PUT',
|
|
40
|
+
'PATCH'
|
|
41
|
+
];
|
|
42
|
+
/**
|
|
43
|
+
* Convert an [`IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage) to a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
44
|
+
*/ function incomingMessageToRequest(req, opts) {
|
|
45
|
+
const ac = new AbortController();
|
|
46
|
+
const headers = new Headers(req.headers);
|
|
47
|
+
const url = `http://${headers.get('host')}${req.url}`;
|
|
48
|
+
req.once('aborted', ()=>ac.abort());
|
|
49
|
+
const init = {
|
|
50
|
+
headers,
|
|
51
|
+
method: req.method,
|
|
52
|
+
signal: ac.signal,
|
|
53
|
+
// @ts-expect-error this is fine
|
|
54
|
+
duplex: 'half'
|
|
55
|
+
};
|
|
56
|
+
if (req.method && bodyMethods.includes(req.method)) {
|
|
57
|
+
if (!('body' in req)) {
|
|
58
|
+
init.body = incomingMessageToBodyStream(req, opts);
|
|
59
|
+
} else if (typeof req.body === 'string') {
|
|
60
|
+
init.body = req.body;
|
|
61
|
+
} else if (req.body !== undefined) {
|
|
62
|
+
init.body = JSON.stringify(req.body);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const request = new Request(url, init);
|
|
66
|
+
return request;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { incomingMessageToRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,SAAS,CAAC;AACxB,cAAc,4BAA4B,CAAC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var nodeHTTPRequestHandler = require('./nodeHTTPRequestHandler.js');
|
|
4
|
+
var incomingMessageToRequest = require('./incomingMessageToRequest.js');
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
|
|
9
|
+
exports.incomingMessageToRequest = incomingMessageToRequest.incomingMessageToRequest;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
8
8
|
* ```
|
|
9
9
|
*/
|
|
10
|
-
import type
|
|
10
|
+
import { type AnyRouter } from '../../@trpc/server';
|
|
11
11
|
import type { NodeHTTPRequest, NodeHTTPRequestHandlerOptions, NodeHTTPResponse } from './types';
|
|
12
12
|
export declare function nodeHTTPRequestHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>): Promise<void>;
|
|
13
13
|
//# sourceMappingURL=nodeHTTPRequestHandler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAA2B,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAI7E,OAAO,KAAK,EACV,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAUjB,wBAAsB,sBAAsB,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,iBA8ClE"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var TRPCError = require('../../unstable-core-do-not-import/error/TRPCError.js');
|
|
3
4
|
var resolveHTTPResponse = require('../../unstable-core-do-not-import/http/resolveHTTPResponse.js');
|
|
4
|
-
var batchStreamFormatter = require('../../unstable-core-do-not-import/http/batchStreamFormatter.js');
|
|
5
5
|
require('../../unstable-core-do-not-import/rootConfig.js');
|
|
6
|
-
var
|
|
7
|
-
var index$1 = require('./content-type/form-data/index.js');
|
|
8
|
-
var index = require('./content-type/json/index.js');
|
|
9
|
-
var index$2 = require('./content-type/octet/index.js');
|
|
6
|
+
var incomingMessageToRequest = require('./incomingMessageToRequest.js');
|
|
10
7
|
|
|
11
8
|
/**
|
|
12
9
|
* If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
|
|
@@ -16,12 +13,18 @@ var index$2 = require('./content-type/octet/index.js');
|
|
|
16
13
|
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
17
14
|
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
18
15
|
* ```
|
|
19
|
-
*/
|
|
16
|
+
*/ // @trpc/server
|
|
17
|
+
function assertAsyncIterable(value) {
|
|
18
|
+
if (!(Symbol.asyncIterator in value)) {
|
|
19
|
+
throw new Error('Expected AsyncIterable - are you using Node >= 18.0.0?');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
20
22
|
async function nodeHTTPRequestHandler(opts) {
|
|
21
23
|
const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>next());
|
|
22
24
|
return handleViaMiddleware(opts.req, opts.res, async (err)=>{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const req = incomingMessageToRequest.incomingMessageToRequest(opts.req, {
|
|
26
|
+
maxBodySize: opts.maxBodySize ?? null
|
|
27
|
+
});
|
|
25
28
|
// Build tRPC dependencies
|
|
26
29
|
const createContext = async (innerOpts)=>{
|
|
27
30
|
return await opts.createContext?.({
|
|
@@ -29,78 +32,32 @@ async function nodeHTTPRequestHandler(opts) {
|
|
|
29
32
|
...innerOpts
|
|
30
33
|
});
|
|
31
34
|
};
|
|
32
|
-
const
|
|
33
|
-
const req = {
|
|
34
|
-
method: opts.req.method,
|
|
35
|
-
headers: opts.req.headers,
|
|
36
|
-
query
|
|
37
|
-
};
|
|
38
|
-
let isStream = false;
|
|
39
|
-
let formatter;
|
|
40
|
-
const unstable_onHead = (head, isStreaming)=>{
|
|
41
|
-
if ('status' in head && (!opts.res.statusCode || opts.res.statusCode === 200)) {
|
|
42
|
-
opts.res.statusCode = head.status;
|
|
43
|
-
}
|
|
44
|
-
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
45
|
-
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
opts.res.setHeader(key, value);
|
|
49
|
-
}
|
|
50
|
-
if (isStreaming) {
|
|
51
|
-
opts.res.setHeader('Transfer-Encoding', 'chunked');
|
|
52
|
-
const vary = opts.res.getHeader('Vary');
|
|
53
|
-
opts.res.setHeader('Vary', vary ? 'trpc-batch-mode, ' + vary : 'trpc-batch-mode');
|
|
54
|
-
isStream = true;
|
|
55
|
-
formatter = batchStreamFormatter.getBatchStreamFormatter();
|
|
56
|
-
opts.res.flushHeaders();
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
const unstable_onChunk = ([index, string])=>{
|
|
60
|
-
if (index === -1) {
|
|
61
|
-
/**
|
|
62
|
-
* Full response, no streaming. This can happen
|
|
63
|
-
* - if the response is an error
|
|
64
|
-
* - if response is empty (HEAD request)
|
|
65
|
-
*/ opts.res.end(string);
|
|
66
|
-
} else {
|
|
67
|
-
opts.res.write(formatter(index, string));
|
|
68
|
-
opts.res.flush?.();
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
const [contentTypeHandler, unsupportedMediaTypeError] = selectContentHandlerOrUnsupportedMediaType.selectContentHandlerOrUnsupportedMediaType([
|
|
72
|
-
index.getNodeHTTPJSONContentTypeHandler(),
|
|
73
|
-
index$1.getFormDataContentTypeHandler(),
|
|
74
|
-
index$2.getOctetContentTypeHandler()
|
|
75
|
-
], {
|
|
76
|
-
...opts,
|
|
77
|
-
query
|
|
78
|
-
});
|
|
79
|
-
await resolveHTTPResponse.resolveHTTPResponse({
|
|
35
|
+
const response = await resolveHTTPResponse.resolveResponse({
|
|
80
36
|
...opts,
|
|
81
37
|
req,
|
|
38
|
+
error: err ? TRPCError.getTRPCErrorFromUnknown(err) : null,
|
|
82
39
|
createContext,
|
|
83
|
-
error: unsupportedMediaTypeError,
|
|
84
40
|
onError (o) {
|
|
85
41
|
opts?.onError?.({
|
|
86
42
|
...o,
|
|
87
43
|
req: opts.req
|
|
88
44
|
});
|
|
89
|
-
}
|
|
90
|
-
async getInput (inputsOpts) {
|
|
91
|
-
return await contentTypeHandler?.getInputs({
|
|
92
|
-
...opts,
|
|
93
|
-
query
|
|
94
|
-
}, inputsOpts);
|
|
95
|
-
},
|
|
96
|
-
unstable_onHead,
|
|
97
|
-
unstable_onChunk
|
|
45
|
+
}
|
|
98
46
|
});
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
opts.res.
|
|
47
|
+
if (opts.res.statusCode === 200) {
|
|
48
|
+
// if the status code is set, we assume that it's been manually overridden
|
|
49
|
+
opts.res.statusCode = response.status;
|
|
50
|
+
}
|
|
51
|
+
for (const [key, value] of response.headers){
|
|
52
|
+
opts.res.setHeader(key, value);
|
|
53
|
+
}
|
|
54
|
+
if (response.body) {
|
|
55
|
+
assertAsyncIterable(response.body);
|
|
56
|
+
for await (const chunk of response.body){
|
|
57
|
+
opts.res.write(chunk);
|
|
58
|
+
}
|
|
102
59
|
}
|
|
103
|
-
|
|
60
|
+
opts.res.end();
|
|
104
61
|
});
|
|
105
62
|
}
|
|
106
63
|
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getTRPCErrorFromUnknown } from '../../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
2
|
+
import { resolveResponse } from '../../unstable-core-do-not-import/http/resolveHTTPResponse.mjs';
|
|
3
3
|
import '../../unstable-core-do-not-import/rootConfig.mjs';
|
|
4
|
-
import {
|
|
5
|
-
import { getFormDataContentTypeHandler } from './content-type/form-data/index.mjs';
|
|
6
|
-
import { getNodeHTTPJSONContentTypeHandler } from './content-type/json/index.mjs';
|
|
7
|
-
import { getOctetContentTypeHandler } from './content-type/octet/index.mjs';
|
|
4
|
+
import { incomingMessageToRequest } from './incomingMessageToRequest.mjs';
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
7
|
* If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
|
|
@@ -14,12 +11,18 @@ import { getOctetContentTypeHandler } from './content-type/octet/index.mjs';
|
|
|
14
11
|
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
15
12
|
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
16
13
|
* ```
|
|
17
|
-
*/
|
|
14
|
+
*/ // @trpc/server
|
|
15
|
+
function assertAsyncIterable(value) {
|
|
16
|
+
if (!(Symbol.asyncIterator in value)) {
|
|
17
|
+
throw new Error('Expected AsyncIterable - are you using Node >= 18.0.0?');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
18
20
|
async function nodeHTTPRequestHandler(opts) {
|
|
19
21
|
const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>next());
|
|
20
22
|
return handleViaMiddleware(opts.req, opts.res, async (err)=>{
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const req = incomingMessageToRequest(opts.req, {
|
|
24
|
+
maxBodySize: opts.maxBodySize ?? null
|
|
25
|
+
});
|
|
23
26
|
// Build tRPC dependencies
|
|
24
27
|
const createContext = async (innerOpts)=>{
|
|
25
28
|
return await opts.createContext?.({
|
|
@@ -27,78 +30,32 @@ async function nodeHTTPRequestHandler(opts) {
|
|
|
27
30
|
...innerOpts
|
|
28
31
|
});
|
|
29
32
|
};
|
|
30
|
-
const
|
|
31
|
-
const req = {
|
|
32
|
-
method: opts.req.method,
|
|
33
|
-
headers: opts.req.headers,
|
|
34
|
-
query
|
|
35
|
-
};
|
|
36
|
-
let isStream = false;
|
|
37
|
-
let formatter;
|
|
38
|
-
const unstable_onHead = (head, isStreaming)=>{
|
|
39
|
-
if ('status' in head && (!opts.res.statusCode || opts.res.statusCode === 200)) {
|
|
40
|
-
opts.res.statusCode = head.status;
|
|
41
|
-
}
|
|
42
|
-
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
43
|
-
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
opts.res.setHeader(key, value);
|
|
47
|
-
}
|
|
48
|
-
if (isStreaming) {
|
|
49
|
-
opts.res.setHeader('Transfer-Encoding', 'chunked');
|
|
50
|
-
const vary = opts.res.getHeader('Vary');
|
|
51
|
-
opts.res.setHeader('Vary', vary ? 'trpc-batch-mode, ' + vary : 'trpc-batch-mode');
|
|
52
|
-
isStream = true;
|
|
53
|
-
formatter = getBatchStreamFormatter();
|
|
54
|
-
opts.res.flushHeaders();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
const unstable_onChunk = ([index, string])=>{
|
|
58
|
-
if (index === -1) {
|
|
59
|
-
/**
|
|
60
|
-
* Full response, no streaming. This can happen
|
|
61
|
-
* - if the response is an error
|
|
62
|
-
* - if response is empty (HEAD request)
|
|
63
|
-
*/ opts.res.end(string);
|
|
64
|
-
} else {
|
|
65
|
-
opts.res.write(formatter(index, string));
|
|
66
|
-
opts.res.flush?.();
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
const [contentTypeHandler, unsupportedMediaTypeError] = selectContentHandlerOrUnsupportedMediaType([
|
|
70
|
-
getNodeHTTPJSONContentTypeHandler(),
|
|
71
|
-
getFormDataContentTypeHandler(),
|
|
72
|
-
getOctetContentTypeHandler()
|
|
73
|
-
], {
|
|
74
|
-
...opts,
|
|
75
|
-
query
|
|
76
|
-
});
|
|
77
|
-
await resolveHTTPResponse({
|
|
33
|
+
const response = await resolveResponse({
|
|
78
34
|
...opts,
|
|
79
35
|
req,
|
|
36
|
+
error: err ? getTRPCErrorFromUnknown(err) : null,
|
|
80
37
|
createContext,
|
|
81
|
-
error: unsupportedMediaTypeError,
|
|
82
38
|
onError (o) {
|
|
83
39
|
opts?.onError?.({
|
|
84
40
|
...o,
|
|
85
41
|
req: opts.req
|
|
86
42
|
});
|
|
87
|
-
}
|
|
88
|
-
async getInput (inputsOpts) {
|
|
89
|
-
return await contentTypeHandler?.getInputs({
|
|
90
|
-
...opts,
|
|
91
|
-
query
|
|
92
|
-
}, inputsOpts);
|
|
93
|
-
},
|
|
94
|
-
unstable_onHead,
|
|
95
|
-
unstable_onChunk
|
|
43
|
+
}
|
|
96
44
|
});
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
opts.res.
|
|
45
|
+
if (opts.res.statusCode === 200) {
|
|
46
|
+
// if the status code is set, we assume that it's been manually overridden
|
|
47
|
+
opts.res.statusCode = response.status;
|
|
48
|
+
}
|
|
49
|
+
for (const [key, value] of response.headers){
|
|
50
|
+
opts.res.setHeader(key, value);
|
|
51
|
+
}
|
|
52
|
+
if (response.body) {
|
|
53
|
+
assertAsyncIterable(response.body);
|
|
54
|
+
for await (const chunk of response.body){
|
|
55
|
+
opts.res.write(chunk);
|
|
56
|
+
}
|
|
100
57
|
}
|
|
101
|
-
|
|
58
|
+
opts.res.end();
|
|
102
59
|
});
|
|
103
60
|
}
|
|
104
61
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
import type * as http from 'http';
|
|
12
|
-
import type { AnyRouter, CreateContextCallback, inferRouterContext
|
|
12
|
+
import type { AnyRouter, CreateContextCallback, inferRouterContext } from '../../@trpc/server';
|
|
13
13
|
import type { HTTPBaseHandlerOptions, TRPCRequestInfo } from '../../@trpc/server/http';
|
|
14
14
|
import type { MaybePromise } from '../../unstable-core-do-not-import';
|
|
15
15
|
interface ParsedQs {
|
|
@@ -30,12 +30,12 @@ export type NodeHTTPResponse = http.ServerResponse & {
|
|
|
30
30
|
*/
|
|
31
31
|
flush?: () => void;
|
|
32
32
|
};
|
|
33
|
-
export type
|
|
33
|
+
export type NodeHTTPCreateContextOption<TRouter extends AnyRouter, TRequest, TResponse> = CreateContextCallback<inferRouterContext<TRouter>, NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>>;
|
|
34
34
|
/**
|
|
35
35
|
* @internal
|
|
36
36
|
*/
|
|
37
37
|
type ConnectMiddleware<TRequest extends NodeHTTPRequest = NodeHTTPRequest, TResponse extends NodeHTTPResponse = NodeHTTPResponse> = (req: TRequest, res: TResponse, next: (err?: any) => any) => void;
|
|
38
|
-
|
|
38
|
+
export type NodeHTTPHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = HTTPBaseHandlerOptions<TRouter, TRequest> & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse> & {
|
|
39
39
|
/**
|
|
40
40
|
* By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
|
|
41
41
|
*
|
|
@@ -52,15 +52,14 @@ interface CoreNodeHTTPHandlerOptions<TRouter extends AnyRouter, TRequest extends
|
|
|
52
52
|
* You can also use it for other needs which a connect/node.js compatible middleware can solve,
|
|
53
53
|
* though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
|
|
54
54
|
*/
|
|
55
|
-
middleware?: ConnectMiddleware
|
|
55
|
+
middleware?: ConnectMiddleware;
|
|
56
56
|
maxBodySize?: number;
|
|
57
|
-
}
|
|
58
|
-
export type
|
|
59
|
-
export type NodeHTTPRequestHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = CoreNodeHTTPHandlerOptions<TRouter, TRequest, TResponse> & WrapCreateContext<NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>> & {
|
|
57
|
+
};
|
|
58
|
+
export type NodeHTTPRequestHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = {
|
|
60
59
|
req: TRequest;
|
|
61
60
|
res: TResponse;
|
|
62
61
|
path: string;
|
|
63
|
-
}
|
|
62
|
+
} & NodeHTTPHandlerOptions<TRouter, TRequest, TResponse>;
|
|
64
63
|
export type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
|
|
65
64
|
req: TRequest;
|
|
66
65
|
res: TResponse;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,KAAK,IAAI,MAAM,MAAM,CAAC;AAElC,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACrB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,KAAK,IAAI,MAAM,MAAM,CAAC;AAElC,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,KAAK,EACV,sBAAsB,EACtB,eAAe,EAChB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEtE,UAAU,QAAQ;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,EAAE,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,CAAC;CACtE;AAED,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,GAAG;IACnD,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AACF,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,cAAc,GAAG;IACnD;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,MAAM,IAAI,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,2BAA2B,CACrC,OAAO,SAAS,SAAS,EACzB,QAAQ,EACR,SAAS,IACP,qBAAqB,CACvB,kBAAkB,CAAC,OAAO,CAAC,EAC3B,uBAAuB,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CACtD,CAAC;AAEF;;GAEG;AACH,KAAK,iBAAiB,CACpB,QAAQ,SAAS,eAAe,GAAG,eAAe,EAClD,SAAS,SAAS,gBAAgB,GAAG,gBAAgB,IACnD,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,GAAG,KAAK,IAAI,CAAC;AAEtE,MAAM,MAAM,sBAAsB,CAChC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,IAChC,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC,GAC3C,2BAA2B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,GAAG;IAC1D;;;;;;;;;;;;;;;OAeG;IACH,UAAU,CAAC,EAAE,iBAAiB,CAAC;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEJ,MAAM,MAAM,6BAA6B,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,IAChC;IACF,GAAG,EAAE,QAAQ,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,sBAAsB,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAEzD,MAAM,MAAM,8BAA8B,CAAC,QAAQ,EAAE,SAAS,IAAI;IAChE,GAAG,EAAE,QAAQ,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;IACf,IAAI,EAAE,eAAe,CAAC;CACvB,CAAC;AACF,MAAM,MAAM,uBAAuB,CACjC,OAAO,SAAS,SAAS,EACzB,QAAQ,EACR,SAAS,IACP,CACF,IAAI,EAAE,8BAA8B,CAAC,QAAQ,EAAE,SAAS,CAAC,KACtD,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC"}
|
package/dist/adapters/ws.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var getErrorShape = require('../unstable-core-do-not-import/error/getErrorShape.js');
|
|
4
3
|
var TRPCError = require('../unstable-core-do-not-import/error/TRPCError.js');
|
|
4
|
+
var getErrorShape = require('../unstable-core-do-not-import/error/getErrorShape.js');
|
|
5
5
|
var parseTRPCMessage = require('../unstable-core-do-not-import/rpc/parseTRPCMessage.js');
|
|
6
6
|
var router = require('../unstable-core-do-not-import/router.js');
|
|
7
7
|
var transformer = require('../unstable-core-do-not-import/transformer.js');
|
package/dist/adapters/ws.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getErrorShape } from '../unstable-core-do-not-import/error/getErrorShape.mjs';
|
|
2
1
|
import { TRPCError, getTRPCErrorFromUnknown } from '../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
2
|
+
import { getErrorShape } from '../unstable-core-do-not-import/error/getErrorShape.mjs';
|
|
3
3
|
import { parseTRPCMessage } from '../unstable-core-do-not-import/rpc/parseTRPCMessage.mjs';
|
|
4
4
|
import { callProcedure } from '../unstable-core-do-not-import/router.mjs';
|
|
5
5
|
import { transformTRPCResponse } from '../unstable-core-do-not-import/transformer.mjs';
|