@trpc/server 11.0.0-rc.566 → 11.0.0-rc.569
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/adapters/express.js +4 -4
- package/dist/adapters/express.mjs +4 -4
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +26 -32
- package/dist/adapters/next.mjs +27 -33
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +2 -1
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +2 -1
- package/dist/adapters/node-http/index.js +1 -0
- package/dist/adapters/node-http/index.mjs +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +5 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +91 -56
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +91 -57
- package/dist/adapters/standalone.d.ts +5 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +25 -14
- package/dist/adapters/standalone.mjs +26 -15
- package/dist/bundle-analysis.json +103 -98
- package/dist/unstable-core-do-not-import/http/toURL.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/toURL.js +12 -2
- package/dist/unstable-core-do-not-import/http/toURL.mjs +12 -2
- package/package.json +2 -2
- package/src/adapters/express.ts +4 -4
- package/src/adapters/next.ts +33 -35
- package/src/adapters/node-http/incomingMessageToRequest.ts +2 -1
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +109 -62
- package/src/adapters/standalone.ts +32 -16
- package/src/unstable-core-do-not-import/http/toURL.ts +14 -4
package/dist/adapters/express.js
CHANGED
|
@@ -4,13 +4,13 @@ var nodeHTTPRequestHandler = require('./node-http/nodeHTTPRequestHandler.js');
|
|
|
4
4
|
require('../unstable-core-do-not-import/rootConfig.js');
|
|
5
5
|
|
|
6
6
|
function createExpressMiddleware(opts) {
|
|
7
|
-
return
|
|
8
|
-
const
|
|
9
|
-
|
|
7
|
+
return (req, res)=>{
|
|
8
|
+
const path = req.path.slice(1);
|
|
9
|
+
nodeHTTPRequestHandler.nodeHTTPRequestHandler({
|
|
10
10
|
...opts,
|
|
11
11
|
req,
|
|
12
12
|
res,
|
|
13
|
-
path
|
|
13
|
+
path
|
|
14
14
|
});
|
|
15
15
|
};
|
|
16
16
|
}
|
|
@@ -2,13 +2,13 @@ import { nodeHTTPRequestHandler } from './node-http/nodeHTTPRequestHandler.mjs';
|
|
|
2
2
|
import '../unstable-core-do-not-import/rootConfig.mjs';
|
|
3
3
|
|
|
4
4
|
function createExpressMiddleware(opts) {
|
|
5
|
-
return
|
|
6
|
-
const
|
|
7
|
-
|
|
5
|
+
return (req, res)=>{
|
|
6
|
+
const path = req.path.slice(1);
|
|
7
|
+
nodeHTTPRequestHandler({
|
|
8
8
|
...opts,
|
|
9
9
|
req,
|
|
10
10
|
res,
|
|
11
|
-
path
|
|
11
|
+
path
|
|
12
12
|
});
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../../src/adapters/next.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAE5E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../../src/adapters/next.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAE5E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAKjD,OAAO,KAAK,EACV,8BAA8B,EAC9B,sBAAsB,EACvB,MAAM,aAAa,CAAC;AAGrB,MAAM,MAAM,wBAAwB,GAAG,8BAA8B,CACnE,cAAc,EACd,eAAe,CAChB,CAAC;AAEF;;GAEG;AACH,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAI5E,wBAAgB,oBAAoB,CAAC,OAAO,SAAS,SAAS,EAC5D,IAAI,EAAE,sBAAsB,CAAC,OAAO,EAAE,cAAc,EAAE,eAAe,CAAC,GACrE,cAAc,CAmChB"}
|
package/dist/adapters/next.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var utils = require('../unstable-core-do-not-import/utils.js');
|
|
4
4
|
var TRPCError = require('../unstable-core-do-not-import/error/TRPCError.js');
|
|
5
5
|
require('../unstable-core-do-not-import/rootConfig.js');
|
|
6
6
|
var nodeHTTPRequestHandler = require('./node-http/nodeHTTPRequestHandler.js');
|
|
@@ -15,43 +15,37 @@ var nodeHTTPRequestHandler = require('./node-http/nodeHTTPRequestHandler.js');
|
|
|
15
15
|
* ```
|
|
16
16
|
*/ // @trpc/server
|
|
17
17
|
function createNextApiHandler(opts) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (path === null) {
|
|
30
|
-
const error = getErrorShape.getErrorShape({
|
|
31
|
-
config: opts.router._def._config,
|
|
32
|
-
error: new TRPCError.TRPCError({
|
|
18
|
+
let path = '';
|
|
19
|
+
const handler = (req, res)=>{
|
|
20
|
+
try {
|
|
21
|
+
path = utils.run(()=>{
|
|
22
|
+
if (typeof req.query['trpc'] === 'string') {
|
|
23
|
+
return req.query['trpc'];
|
|
24
|
+
}
|
|
25
|
+
if (Array.isArray(req.query['trpc'])) {
|
|
26
|
+
return req.query['trpc'].join('/');
|
|
27
|
+
}
|
|
28
|
+
throw new TRPCError.TRPCError({
|
|
33
29
|
message: 'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
34
30
|
code: 'INTERNAL_SERVER_ERROR'
|
|
35
|
-
})
|
|
36
|
-
type: 'unknown',
|
|
37
|
-
ctx: undefined,
|
|
38
|
-
path: undefined,
|
|
39
|
-
input: undefined
|
|
31
|
+
});
|
|
40
32
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
nodeHTTPRequestHandler.nodeHTTPRequestHandler({
|
|
34
|
+
...opts,
|
|
35
|
+
req,
|
|
36
|
+
res,
|
|
37
|
+
path
|
|
45
38
|
});
|
|
46
|
-
|
|
39
|
+
} catch (cause) {
|
|
40
|
+
nodeHTTPRequestHandler.internal_exceptionHandler({
|
|
41
|
+
req,
|
|
42
|
+
res,
|
|
43
|
+
path,
|
|
44
|
+
...opts
|
|
45
|
+
})(cause);
|
|
47
46
|
}
|
|
48
|
-
await nodeHTTPRequestHandler.nodeHTTPRequestHandler({
|
|
49
|
-
...opts,
|
|
50
|
-
req,
|
|
51
|
-
res,
|
|
52
|
-
path
|
|
53
|
-
});
|
|
54
47
|
};
|
|
48
|
+
return handler;
|
|
55
49
|
}
|
|
56
50
|
|
|
57
51
|
exports.createNextApiHandler = createNextApiHandler;
|
package/dist/adapters/next.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { run } from '../unstable-core-do-not-import/utils.mjs';
|
|
2
2
|
import { TRPCError } from '../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
3
3
|
import '../unstable-core-do-not-import/rootConfig.mjs';
|
|
4
|
-
import { nodeHTTPRequestHandler } from './node-http/nodeHTTPRequestHandler.mjs';
|
|
4
|
+
import { nodeHTTPRequestHandler, internal_exceptionHandler } from './node-http/nodeHTTPRequestHandler.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
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`
|
|
@@ -13,43 +13,37 @@ import { nodeHTTPRequestHandler } from './node-http/nodeHTTPRequestHandler.mjs';
|
|
|
13
13
|
* ```
|
|
14
14
|
*/ // @trpc/server
|
|
15
15
|
function createNextApiHandler(opts) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (path === null) {
|
|
28
|
-
const error = getErrorShape({
|
|
29
|
-
config: opts.router._def._config,
|
|
30
|
-
error: new TRPCError({
|
|
16
|
+
let path = '';
|
|
17
|
+
const handler = (req, res)=>{
|
|
18
|
+
try {
|
|
19
|
+
path = run(()=>{
|
|
20
|
+
if (typeof req.query['trpc'] === 'string') {
|
|
21
|
+
return req.query['trpc'];
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(req.query['trpc'])) {
|
|
24
|
+
return req.query['trpc'].join('/');
|
|
25
|
+
}
|
|
26
|
+
throw new TRPCError({
|
|
31
27
|
message: 'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
32
28
|
code: 'INTERNAL_SERVER_ERROR'
|
|
33
|
-
})
|
|
34
|
-
type: 'unknown',
|
|
35
|
-
ctx: undefined,
|
|
36
|
-
path: undefined,
|
|
37
|
-
input: undefined
|
|
29
|
+
});
|
|
38
30
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
nodeHTTPRequestHandler({
|
|
32
|
+
...opts,
|
|
33
|
+
req,
|
|
34
|
+
res,
|
|
35
|
+
path
|
|
43
36
|
});
|
|
44
|
-
|
|
37
|
+
} catch (cause) {
|
|
38
|
+
internal_exceptionHandler({
|
|
39
|
+
req,
|
|
40
|
+
res,
|
|
41
|
+
path,
|
|
42
|
+
...opts
|
|
43
|
+
})(cause);
|
|
45
44
|
}
|
|
46
|
-
await nodeHTTPRequestHandler({
|
|
47
|
-
...opts,
|
|
48
|
-
req,
|
|
49
|
-
res,
|
|
50
|
-
path
|
|
51
|
-
});
|
|
52
45
|
};
|
|
46
|
+
return handler;
|
|
53
47
|
}
|
|
54
48
|
|
|
55
49
|
export { createNextApiHandler };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"incomingMessageToRequest.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/incomingMessageToRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"incomingMessageToRequest.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/incomingMessageToRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,MAAM,CAAC;AAIlC,MAAM,WAAW,uBAAwB,SAAQ,IAAI,CAAC,eAAe;IACnE;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AA+CD;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,IAAI,CAAC,eAAe,EACzB,IAAI,EAAE;IACJ;;OAEG;IACH,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,GACA,OAAO,CA4BT"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var TRPCError = require('../../unstable-core-do-not-import/error/TRPCError.js');
|
|
4
|
+
var toURL = require('../../unstable-core-do-not-import/http/toURL.js');
|
|
4
5
|
require('../../unstable-core-do-not-import/rootConfig.js');
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -48,7 +49,7 @@ const bodyMethods = [
|
|
|
48
49
|
*/ function incomingMessageToRequest(req, opts) {
|
|
49
50
|
const ac = new AbortController();
|
|
50
51
|
const headers = new Headers(req.headers);
|
|
51
|
-
const url = `http://${headers.get('host')}${req.url}
|
|
52
|
+
const url = toURL.toURL(`http://${headers.get('host')}${req.url}`);
|
|
52
53
|
req.once('aborted', ()=>{
|
|
53
54
|
ac.abort();
|
|
54
55
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TRPCError } from '../../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
2
|
+
import { toURL } from '../../unstable-core-do-not-import/http/toURL.mjs';
|
|
2
3
|
import '../../unstable-core-do-not-import/rootConfig.mjs';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -46,7 +47,7 @@ const bodyMethods = [
|
|
|
46
47
|
*/ function incomingMessageToRequest(req, opts) {
|
|
47
48
|
const ac = new AbortController();
|
|
48
49
|
const headers = new Headers(req.headers);
|
|
49
|
-
const url = `http://${headers.get('host')}${req.url}
|
|
50
|
+
const url = toURL(`http://${headers.get('host')}${req.url}`);
|
|
50
51
|
req.once('aborted', ()=>{
|
|
51
52
|
ac.abort();
|
|
52
53
|
});
|
|
@@ -5,5 +5,6 @@ var incomingMessageToRequest = require('./incomingMessageToRequest.js');
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
+
exports.internal_exceptionHandler = nodeHTTPRequestHandler.internal_exceptionHandler;
|
|
8
9
|
exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
|
|
9
10
|
exports.incomingMessageToRequest = incomingMessageToRequest.incomingMessageToRequest;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { nodeHTTPRequestHandler } from './nodeHTTPRequestHandler.mjs';
|
|
1
|
+
export { internal_exceptionHandler, nodeHTTPRequestHandler } from './nodeHTTPRequestHandler.mjs';
|
|
2
2
|
export { incomingMessageToRequest } from './incomingMessageToRequest.mjs';
|
|
@@ -9,5 +9,9 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { type AnyRouter } from '../../@trpc/server';
|
|
11
11
|
import type { NodeHTTPRequest, NodeHTTPRequestHandlerOptions, NodeHTTPResponse } from './types';
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function internal_exceptionHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>): (cause: unknown) => void;
|
|
16
|
+
export declare function nodeHTTPRequestHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>): void;
|
|
13
17
|
//# 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,
|
|
1
|
+
{"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAGL,KAAK,SAAS,EACf,MAAM,oBAAoB,CAAC;AAM5B,OAAO,KAAK,EACV,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,WAClD,OAAO,UA6BvB;AAED,wBAAgB,sBAAsB,CACpC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,QA8ElE"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var getErrorShape = require('../../unstable-core-do-not-import/error/getErrorShape.js');
|
|
3
4
|
var TRPCError = require('../../unstable-core-do-not-import/error/TRPCError.js');
|
|
5
|
+
var utils = require('../../unstable-core-do-not-import/utils.js');
|
|
4
6
|
var resolveResponse = require('../../unstable-core-do-not-import/http/resolveResponse.js');
|
|
5
7
|
require('../../unstable-core-do-not-import/rootConfig.js');
|
|
8
|
+
var transformer = require('../../unstable-core-do-not-import/transformer.js');
|
|
6
9
|
var incomingMessageToRequest = require('./incomingMessageToRequest.js');
|
|
7
10
|
|
|
8
11
|
/**
|
|
@@ -14,72 +17,104 @@ var incomingMessageToRequest = require('./incomingMessageToRequest.js');
|
|
|
14
17
|
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
15
18
|
* ```
|
|
16
19
|
*/ // @trpc/server
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/ function internal_exceptionHandler(opts) {
|
|
23
|
+
return (cause)=>{
|
|
24
|
+
const { res , req } = opts;
|
|
25
|
+
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
26
|
+
const shape = getErrorShape.getErrorShape({
|
|
27
|
+
config: opts.router._def._config,
|
|
28
|
+
error,
|
|
29
|
+
type: 'unknown',
|
|
30
|
+
path: undefined,
|
|
31
|
+
input: undefined,
|
|
32
|
+
ctx: undefined
|
|
22
33
|
});
|
|
23
|
-
|
|
24
|
-
const createContext = async (innerOpts)=>{
|
|
25
|
-
return await opts.createContext?.({
|
|
26
|
-
...opts,
|
|
27
|
-
...innerOpts
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
const response = await resolveResponse.resolveResponse({
|
|
31
|
-
...opts,
|
|
34
|
+
opts.onError?.({
|
|
32
35
|
req,
|
|
33
|
-
error
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
error,
|
|
37
|
+
type: 'unknown',
|
|
38
|
+
path: undefined,
|
|
39
|
+
input: undefined,
|
|
40
|
+
ctx: undefined
|
|
41
|
+
});
|
|
42
|
+
const transformed = transformer.transformTRPCResponse(opts.router._def._config, {
|
|
43
|
+
error: shape
|
|
41
44
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
45
|
+
res.statusCode = shape.data.httpStatus;
|
|
46
|
+
res.end(JSON.stringify(transformed));
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function nodeHTTPRequestHandler(opts) {
|
|
50
|
+
const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>next());
|
|
51
|
+
return handleViaMiddleware(opts.req, opts.res, (err)=>{
|
|
52
|
+
utils.run(async ()=>{
|
|
53
|
+
const req = incomingMessageToRequest.incomingMessageToRequest(opts.req, {
|
|
54
|
+
maxBodySize: opts.maxBodySize ?? null
|
|
55
|
+
});
|
|
56
|
+
// Build tRPC dependencies
|
|
57
|
+
const createContext = async (innerOpts)=>{
|
|
58
|
+
return await opts.createContext?.({
|
|
59
|
+
...opts,
|
|
60
|
+
...innerOpts
|
|
56
61
|
});
|
|
57
62
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
const response = await resolveResponse.resolveResponse({
|
|
64
|
+
...opts,
|
|
65
|
+
req,
|
|
66
|
+
error: err ? TRPCError.getTRPCErrorFromUnknown(err) : null,
|
|
67
|
+
createContext,
|
|
68
|
+
onError (o) {
|
|
69
|
+
opts?.onError?.({
|
|
70
|
+
...o,
|
|
71
|
+
req: opts.req
|
|
72
|
+
});
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
});
|
|
75
|
+
const { res } = opts;
|
|
76
|
+
if (res.statusCode === 200) {
|
|
77
|
+
// if the status code is set, we assume that it's been manually overridden
|
|
78
|
+
res.statusCode = response.status;
|
|
79
|
+
}
|
|
80
|
+
for (const [key, value] of response.headers){
|
|
81
|
+
res.setHeader(key, value);
|
|
82
|
+
}
|
|
83
|
+
if (response.body) {
|
|
84
|
+
const reader = response.body.getReader();
|
|
85
|
+
const onAbort = ()=>{
|
|
86
|
+
// cancelling the reader will cause the whole stream to be cancelled
|
|
87
|
+
reader.cancel().catch(()=>{
|
|
88
|
+
// console.error('reader.cancel() error', err);
|
|
72
89
|
});
|
|
90
|
+
};
|
|
91
|
+
req.signal.addEventListener('abort', onAbort, {
|
|
92
|
+
once: true
|
|
93
|
+
});
|
|
94
|
+
while(true){
|
|
95
|
+
const { done , value: value1 } = await reader.read();
|
|
96
|
+
if (done) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (!res.writable) {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
if (res.write(value1) === false) {
|
|
103
|
+
await new Promise((resolve)=>{
|
|
104
|
+
res.once('drain', resolve);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// useful for debugging chunked responses:
|
|
108
|
+
// console.log('wrote', Buffer.from(value).toString());
|
|
109
|
+
// IMPORTANT - flush the response buffer, otherwise the client will not receive the data until `.end()`
|
|
110
|
+
res.flush?.();
|
|
73
111
|
}
|
|
74
|
-
|
|
75
|
-
// console.log('wrote', Buffer.from(value).toString());
|
|
76
|
-
// IMPORTANT - flush the response buffer, otherwise the client will not receive the data until `.end()`
|
|
77
|
-
res.flush?.();
|
|
112
|
+
req.signal.removeEventListener('abort', onAbort);
|
|
78
113
|
}
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
res.end();
|
|
114
|
+
res.end();
|
|
115
|
+
}).catch(internal_exceptionHandler(opts));
|
|
82
116
|
});
|
|
83
117
|
}
|
|
84
118
|
|
|
119
|
+
exports.internal_exceptionHandler = internal_exceptionHandler;
|
|
85
120
|
exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { getErrorShape } from '../../unstable-core-do-not-import/error/getErrorShape.mjs';
|
|
1
2
|
import { getTRPCErrorFromUnknown } from '../../unstable-core-do-not-import/error/TRPCError.mjs';
|
|
3
|
+
import { run } from '../../unstable-core-do-not-import/utils.mjs';
|
|
2
4
|
import { resolveResponse } from '../../unstable-core-do-not-import/http/resolveResponse.mjs';
|
|
3
5
|
import '../../unstable-core-do-not-import/rootConfig.mjs';
|
|
6
|
+
import { transformTRPCResponse } from '../../unstable-core-do-not-import/transformer.mjs';
|
|
4
7
|
import { incomingMessageToRequest } from './incomingMessageToRequest.mjs';
|
|
5
8
|
|
|
6
9
|
/**
|
|
@@ -12,72 +15,103 @@ import { incomingMessageToRequest } from './incomingMessageToRequest.mjs';
|
|
|
12
15
|
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
13
16
|
* ```
|
|
14
17
|
*/ // @trpc/server
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/ function internal_exceptionHandler(opts) {
|
|
21
|
+
return (cause)=>{
|
|
22
|
+
const { res , req } = opts;
|
|
23
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
24
|
+
const shape = getErrorShape({
|
|
25
|
+
config: opts.router._def._config,
|
|
26
|
+
error,
|
|
27
|
+
type: 'unknown',
|
|
28
|
+
path: undefined,
|
|
29
|
+
input: undefined,
|
|
30
|
+
ctx: undefined
|
|
20
31
|
});
|
|
21
|
-
|
|
22
|
-
const createContext = async (innerOpts)=>{
|
|
23
|
-
return await opts.createContext?.({
|
|
24
|
-
...opts,
|
|
25
|
-
...innerOpts
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
const response = await resolveResponse({
|
|
29
|
-
...opts,
|
|
32
|
+
opts.onError?.({
|
|
30
33
|
req,
|
|
31
|
-
error
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
error,
|
|
35
|
+
type: 'unknown',
|
|
36
|
+
path: undefined,
|
|
37
|
+
input: undefined,
|
|
38
|
+
ctx: undefined
|
|
39
|
+
});
|
|
40
|
+
const transformed = transformTRPCResponse(opts.router._def._config, {
|
|
41
|
+
error: shape
|
|
39
42
|
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
res.statusCode = shape.data.httpStatus;
|
|
44
|
+
res.end(JSON.stringify(transformed));
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function nodeHTTPRequestHandler(opts) {
|
|
48
|
+
const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>next());
|
|
49
|
+
return handleViaMiddleware(opts.req, opts.res, (err)=>{
|
|
50
|
+
run(async ()=>{
|
|
51
|
+
const req = incomingMessageToRequest(opts.req, {
|
|
52
|
+
maxBodySize: opts.maxBodySize ?? null
|
|
53
|
+
});
|
|
54
|
+
// Build tRPC dependencies
|
|
55
|
+
const createContext = async (innerOpts)=>{
|
|
56
|
+
return await opts.createContext?.({
|
|
57
|
+
...opts,
|
|
58
|
+
...innerOpts
|
|
54
59
|
});
|
|
55
60
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
const response = await resolveResponse({
|
|
62
|
+
...opts,
|
|
63
|
+
req,
|
|
64
|
+
error: err ? getTRPCErrorFromUnknown(err) : null,
|
|
65
|
+
createContext,
|
|
66
|
+
onError (o) {
|
|
67
|
+
opts?.onError?.({
|
|
68
|
+
...o,
|
|
69
|
+
req: opts.req
|
|
70
|
+
});
|
|
66
71
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
});
|
|
73
|
+
const { res } = opts;
|
|
74
|
+
if (res.statusCode === 200) {
|
|
75
|
+
// if the status code is set, we assume that it's been manually overridden
|
|
76
|
+
res.statusCode = response.status;
|
|
77
|
+
}
|
|
78
|
+
for (const [key, value] of response.headers){
|
|
79
|
+
res.setHeader(key, value);
|
|
80
|
+
}
|
|
81
|
+
if (response.body) {
|
|
82
|
+
const reader = response.body.getReader();
|
|
83
|
+
const onAbort = ()=>{
|
|
84
|
+
// cancelling the reader will cause the whole stream to be cancelled
|
|
85
|
+
reader.cancel().catch(()=>{
|
|
86
|
+
// console.error('reader.cancel() error', err);
|
|
70
87
|
});
|
|
88
|
+
};
|
|
89
|
+
req.signal.addEventListener('abort', onAbort, {
|
|
90
|
+
once: true
|
|
91
|
+
});
|
|
92
|
+
while(true){
|
|
93
|
+
const { done , value: value1 } = await reader.read();
|
|
94
|
+
if (done) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (!res.writable) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (res.write(value1) === false) {
|
|
101
|
+
await new Promise((resolve)=>{
|
|
102
|
+
res.once('drain', resolve);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
// useful for debugging chunked responses:
|
|
106
|
+
// console.log('wrote', Buffer.from(value).toString());
|
|
107
|
+
// IMPORTANT - flush the response buffer, otherwise the client will not receive the data until `.end()`
|
|
108
|
+
res.flush?.();
|
|
71
109
|
}
|
|
72
|
-
|
|
73
|
-
// console.log('wrote', Buffer.from(value).toString());
|
|
74
|
-
// IMPORTANT - flush the response buffer, otherwise the client will not receive the data until `.end()`
|
|
75
|
-
res.flush?.();
|
|
110
|
+
req.signal.removeEventListener('abort', onAbort);
|
|
76
111
|
}
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
res.end();
|
|
112
|
+
res.end();
|
|
113
|
+
}).catch(internal_exceptionHandler(opts));
|
|
80
114
|
});
|
|
81
115
|
}
|
|
82
116
|
|
|
83
|
-
export { nodeHTTPRequestHandler };
|
|
117
|
+
export { internal_exceptionHandler, nodeHTTPRequestHandler };
|
|
@@ -8,10 +8,13 @@
|
|
|
8
8
|
* ```
|
|
9
9
|
*/
|
|
10
10
|
import http from 'http';
|
|
11
|
-
import type
|
|
11
|
+
import { type AnyRouter } from '../@trpc/server';
|
|
12
12
|
import type { NodeHTTPCreateContextFnOptions, NodeHTTPHandlerOptions } from './node-http';
|
|
13
13
|
export type CreateHTTPHandlerOptions<TRouter extends AnyRouter> = NodeHTTPHandlerOptions<TRouter, http.IncomingMessage, http.ServerResponse>;
|
|
14
14
|
export type CreateHTTPContextOptions = NodeHTTPCreateContextFnOptions<http.IncomingMessage, http.ServerResponse>;
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export declare function createHTTPHandler<TRouter extends AnyRouter>(opts: CreateHTTPHandlerOptions<TRouter>): http.RequestListener;
|
|
16
19
|
export declare function createHTTPServer<TRouter extends AnyRouter>(opts: CreateHTTPHandlerOptions<TRouter>): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
17
20
|
//# sourceMappingURL=standalone.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standalone.d.ts","sourceRoot":"","sources":["../../src/adapters/standalone.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"standalone.d.ts","sourceRoot":"","sources":["../../src/adapters/standalone.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,EACV,8BAA8B,EAC9B,sBAAsB,EACvB,MAAM,aAAa,CAAC;AAGrB,MAAM,MAAM,wBAAwB,CAAC,OAAO,SAAS,SAAS,IAC5D,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;AAE7E,MAAM,MAAM,wBAAwB,GAAG,8BAA8B,CACnE,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,cAAc,CACpB,CAAC;AAEF;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,SAAS,EACzD,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,GACtC,IAAI,CAAC,eAAe,CA6BtB;AAED,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,SAAS,EACxD,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,wEAGxC"}
|