@trpc/server 11.0.0-next-beta.206 → 11.0.0-next-beta.216
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/aws-lambda/index.js +14 -105
- package/dist/adapters/aws-lambda/index.mjs +1 -90
- package/dist/adapters/aws-lambda/utils.js +100 -0
- package/dist/adapters/aws-lambda/utils.mjs +93 -0
- package/dist/adapters/express.js +1 -7
- package/dist/adapters/express.mjs +1 -5
- package/dist/adapters/fastify/fastifyRequestHandler.js +81 -0
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +79 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +51 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +49 -0
- package/dist/adapters/fastify/index.js +4 -128
- package/dist/adapters/fastify/index.mjs +2 -128
- package/dist/adapters/fetch/fetchRequestHandler.js +118 -0
- package/dist/adapters/fetch/fetchRequestHandler.mjs +116 -0
- package/dist/adapters/fetch/index.js +2 -115
- package/dist/adapters/fetch/index.mjs +1 -116
- package/dist/adapters/next.js +1 -6
- package/dist/adapters/next.mjs +1 -4
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +161 -0
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +157 -0
- package/dist/adapters/node-http/content-type/form-data/index.js +20 -646
- package/dist/adapters/node-http/content-type/form-data/index.mjs +9 -631
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +29 -0
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +27 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +46 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +44 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +30 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +26 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.js +42 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +40 -0
- package/dist/adapters/node-http/content-type/json/index.js +3 -42
- package/dist/adapters/node-http/content-type/json/index.mjs +2 -39
- package/dist/adapters/node-http/index.js +1 -7
- package/dist/adapters/node-http/index.mjs +1 -5
- package/dist/{contentType-72ed9df5.mjs → adapters/node-http/internals/contentType.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-83441c73.js → adapters/node-http/nodeHTTPRequestHandler.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-0223fac5.mjs → adapters/node-http/nodeHTTPRequestHandler.mjs} +2 -2
- package/dist/adapters/standalone.js +2 -12
- package/dist/adapters/standalone.mjs +1 -5
- package/dist/adapters/ws.js +0 -2
- package/dist/bundle-analysis.json +97 -97
- package/dist/http.js +1 -3
- package/dist/index.js +10 -12
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +203 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +201 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +167 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +163 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +35 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +30 -0
- package/dist/observable.js +1 -3
- package/dist/rpc.js +1 -3
- package/dist/shared.js +2 -4
- package/package.json +4 -4
- package/dist/contentType-24c44bba.js +0 -5
- package/dist/nodeHTTPRequestHandler-aa0dce4e.js +0 -105
- /package/dist/{contentType-d9d22104.js → adapters/node-http/internals/contentType.js} +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ws = require('../ws.js');
|
|
4
|
+
var fastifyRequestHandler = require('./fastifyRequestHandler.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
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`
|
|
8
|
+
*
|
|
9
|
+
* Do **not** import from `@trpc/core`
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
13
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
14
|
+
* ```
|
|
15
|
+
*/ /// <reference types="@fastify/websocket" />
|
|
16
|
+
function fastifyTRPCPlugin(fastify, opts, done) {
|
|
17
|
+
fastify.removeContentTypeParser('application/json');
|
|
18
|
+
fastify.addContentTypeParser('application/json', {
|
|
19
|
+
parseAs: 'string'
|
|
20
|
+
}, function(_, body, _done) {
|
|
21
|
+
_done(null, body);
|
|
22
|
+
});
|
|
23
|
+
let prefix = opts.prefix ?? '';
|
|
24
|
+
// https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
|
|
25
|
+
// @ts-expect-error property 'default' does not exists on type ...
|
|
26
|
+
if (typeof fastifyTRPCPlugin.default !== 'function') {
|
|
27
|
+
prefix = ''; // handled by fastify internally
|
|
28
|
+
}
|
|
29
|
+
fastify.all(`${prefix}/:path`, async (req, res)=>{
|
|
30
|
+
const path = req.params.path;
|
|
31
|
+
await fastifyRequestHandler.fastifyRequestHandler({
|
|
32
|
+
...opts.trpcOptions,
|
|
33
|
+
req,
|
|
34
|
+
res,
|
|
35
|
+
path
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
if (opts.useWSS) {
|
|
39
|
+
ws.applyWSSHandler({
|
|
40
|
+
...opts.trpcOptions,
|
|
41
|
+
wss: fastify.websocketServer
|
|
42
|
+
});
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
44
|
+
fastify.get(prefix ?? '/', {
|
|
45
|
+
websocket: true
|
|
46
|
+
}, ()=>{});
|
|
47
|
+
}
|
|
48
|
+
done();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.fastifyTRPCPlugin = fastifyTRPCPlugin;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { applyWSSHandler } from '../ws.mjs';
|
|
2
|
+
import { fastifyRequestHandler } from './fastifyRequestHandler.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 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`
|
|
6
|
+
*
|
|
7
|
+
* Do **not** import from `@trpc/core`
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
11
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
12
|
+
* ```
|
|
13
|
+
*/ /// <reference types="@fastify/websocket" />
|
|
14
|
+
function fastifyTRPCPlugin(fastify, opts, done) {
|
|
15
|
+
fastify.removeContentTypeParser('application/json');
|
|
16
|
+
fastify.addContentTypeParser('application/json', {
|
|
17
|
+
parseAs: 'string'
|
|
18
|
+
}, function(_, body, _done) {
|
|
19
|
+
_done(null, body);
|
|
20
|
+
});
|
|
21
|
+
let prefix = opts.prefix ?? '';
|
|
22
|
+
// https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
|
|
23
|
+
// @ts-expect-error property 'default' does not exists on type ...
|
|
24
|
+
if (typeof fastifyTRPCPlugin.default !== 'function') {
|
|
25
|
+
prefix = ''; // handled by fastify internally
|
|
26
|
+
}
|
|
27
|
+
fastify.all(`${prefix}/:path`, async (req, res)=>{
|
|
28
|
+
const path = req.params.path;
|
|
29
|
+
await fastifyRequestHandler({
|
|
30
|
+
...opts.trpcOptions,
|
|
31
|
+
req,
|
|
32
|
+
res,
|
|
33
|
+
path
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
if (opts.useWSS) {
|
|
37
|
+
applyWSSHandler({
|
|
38
|
+
...opts.trpcOptions,
|
|
39
|
+
wss: fastify.websocketServer
|
|
40
|
+
});
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
42
|
+
fastify.get(prefix ?? '/', {
|
|
43
|
+
websocket: true
|
|
44
|
+
}, ()=>{});
|
|
45
|
+
}
|
|
46
|
+
done();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { fastifyTRPCPlugin };
|
|
@@ -1,133 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var fastifyRequestHandler = require('./fastifyRequestHandler.js');
|
|
4
|
+
var fastifyTRPCPlugin = require('./fastifyTRPCPlugin.js');
|
|
4
5
|
|
|
5
|
-
var node_stream = require('node:stream');
|
|
6
|
-
var http = require('@trpc/core/http');
|
|
7
|
-
var adapters_ws = require('../ws.js');
|
|
8
|
-
require('@trpc/core/observable');
|
|
9
|
-
require('@trpc/core/rpc');
|
|
10
|
-
require('@trpc/core');
|
|
11
6
|
|
|
12
|
-
async function fastifyRequestHandler(opts) {
|
|
13
|
-
const createContext = async (innerOpts)=>{
|
|
14
|
-
return await opts.createContext?.({
|
|
15
|
-
...opts,
|
|
16
|
-
...innerOpts
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
|
|
20
|
-
const req = {
|
|
21
|
-
query,
|
|
22
|
-
method: opts.req.method,
|
|
23
|
-
headers: opts.req.headers,
|
|
24
|
-
body: opts.req.body ?? 'null'
|
|
25
|
-
};
|
|
26
|
-
let resolve;
|
|
27
|
-
const promise = new Promise((r)=>resolve = r);
|
|
28
|
-
let isStream = false;
|
|
29
|
-
let stream;
|
|
30
|
-
let formatter;
|
|
31
|
-
const unstable_onHead = (head, isStreaming)=>{
|
|
32
|
-
if (!opts.res.statusCode || opts.res.statusCode === 200) {
|
|
33
|
-
opts.res.statusCode = head.status;
|
|
34
|
-
}
|
|
35
|
-
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
36
|
-
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
void opts.res.header(key, value);
|
|
40
|
-
}
|
|
41
|
-
if (isStreaming) {
|
|
42
|
-
void opts.res.header('Transfer-Encoding', 'chunked');
|
|
43
|
-
void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
|
|
44
|
-
stream = new node_stream.Readable();
|
|
45
|
-
stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
46
|
-
resolve(opts.res.send(stream));
|
|
47
|
-
isStream = true;
|
|
48
|
-
formatter = http.getBatchStreamFormatter();
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
const unstable_onChunk = ([index, string])=>{
|
|
52
|
-
if (index === -1) {
|
|
53
|
-
// full response, no streaming
|
|
54
|
-
resolve(opts.res.send(string));
|
|
55
|
-
} else {
|
|
56
|
-
stream.push(formatter(index, string));
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
http.resolveHTTPResponse({
|
|
60
|
-
req,
|
|
61
|
-
createContext,
|
|
62
|
-
path: opts.path,
|
|
63
|
-
router: opts.router,
|
|
64
|
-
batching: opts.batching,
|
|
65
|
-
responseMeta: opts.responseMeta,
|
|
66
|
-
onError (o) {
|
|
67
|
-
opts?.onError?.({
|
|
68
|
-
...o,
|
|
69
|
-
req: opts.req
|
|
70
|
-
});
|
|
71
|
-
},
|
|
72
|
-
unstable_onHead,
|
|
73
|
-
unstable_onChunk
|
|
74
|
-
}).then(()=>{
|
|
75
|
-
if (isStream) {
|
|
76
|
-
stream.push(formatter.end());
|
|
77
|
-
stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
78
|
-
}
|
|
79
|
-
}).catch(()=>{
|
|
80
|
-
if (isStream) {
|
|
81
|
-
stream.push(null);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
return promise;
|
|
85
|
-
}
|
|
86
7
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
* Do **not** import from `@trpc/core`
|
|
91
|
-
* @example
|
|
92
|
-
* ```ts
|
|
93
|
-
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
94
|
-
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
95
|
-
* ```
|
|
96
|
-
*/ /// <reference types="@fastify/websocket" />
|
|
97
|
-
function fastifyTRPCPlugin(fastify, opts, done) {
|
|
98
|
-
fastify.removeContentTypeParser('application/json');
|
|
99
|
-
fastify.addContentTypeParser('application/json', {
|
|
100
|
-
parseAs: 'string'
|
|
101
|
-
}, function(_, body, _done) {
|
|
102
|
-
_done(null, body);
|
|
103
|
-
});
|
|
104
|
-
let prefix = opts.prefix ?? '';
|
|
105
|
-
// https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
|
|
106
|
-
// @ts-expect-error property 'default' does not exists on type ...
|
|
107
|
-
if (typeof fastifyTRPCPlugin.default !== 'function') {
|
|
108
|
-
prefix = ''; // handled by fastify internally
|
|
109
|
-
}
|
|
110
|
-
fastify.all(`${prefix}/:path`, async (req, res)=>{
|
|
111
|
-
const path = req.params.path;
|
|
112
|
-
await fastifyRequestHandler({
|
|
113
|
-
...opts.trpcOptions,
|
|
114
|
-
req,
|
|
115
|
-
res,
|
|
116
|
-
path
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
if (opts.useWSS) {
|
|
120
|
-
adapters_ws.applyWSSHandler({
|
|
121
|
-
...opts.trpcOptions,
|
|
122
|
-
wss: fastify.websocketServer
|
|
123
|
-
});
|
|
124
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
125
|
-
fastify.get(prefix ?? '/', {
|
|
126
|
-
websocket: true
|
|
127
|
-
}, ()=>{});
|
|
128
|
-
}
|
|
129
|
-
done();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
exports.fastifyRequestHandler = fastifyRequestHandler;
|
|
133
|
-
exports.fastifyTRPCPlugin = fastifyTRPCPlugin;
|
|
8
|
+
exports.fastifyRequestHandler = fastifyRequestHandler.fastifyRequestHandler;
|
|
9
|
+
exports.fastifyTRPCPlugin = fastifyTRPCPlugin.fastifyTRPCPlugin;
|
|
@@ -1,128 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { applyWSSHandler } from '../ws.mjs';
|
|
4
|
-
import '@trpc/core/observable';
|
|
5
|
-
import '@trpc/core/rpc';
|
|
6
|
-
import '@trpc/core';
|
|
7
|
-
|
|
8
|
-
async function fastifyRequestHandler(opts) {
|
|
9
|
-
const createContext = async (innerOpts)=>{
|
|
10
|
-
return await opts.createContext?.({
|
|
11
|
-
...opts,
|
|
12
|
-
...innerOpts
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
|
|
16
|
-
const req = {
|
|
17
|
-
query,
|
|
18
|
-
method: opts.req.method,
|
|
19
|
-
headers: opts.req.headers,
|
|
20
|
-
body: opts.req.body ?? 'null'
|
|
21
|
-
};
|
|
22
|
-
let resolve;
|
|
23
|
-
const promise = new Promise((r)=>resolve = r);
|
|
24
|
-
let isStream = false;
|
|
25
|
-
let stream;
|
|
26
|
-
let formatter;
|
|
27
|
-
const unstable_onHead = (head, isStreaming)=>{
|
|
28
|
-
if (!opts.res.statusCode || opts.res.statusCode === 200) {
|
|
29
|
-
opts.res.statusCode = head.status;
|
|
30
|
-
}
|
|
31
|
-
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
32
|
-
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
void opts.res.header(key, value);
|
|
36
|
-
}
|
|
37
|
-
if (isStreaming) {
|
|
38
|
-
void opts.res.header('Transfer-Encoding', 'chunked');
|
|
39
|
-
void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
|
|
40
|
-
stream = new Readable();
|
|
41
|
-
stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
42
|
-
resolve(opts.res.send(stream));
|
|
43
|
-
isStream = true;
|
|
44
|
-
formatter = getBatchStreamFormatter();
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const unstable_onChunk = ([index, string])=>{
|
|
48
|
-
if (index === -1) {
|
|
49
|
-
// full response, no streaming
|
|
50
|
-
resolve(opts.res.send(string));
|
|
51
|
-
} else {
|
|
52
|
-
stream.push(formatter(index, string));
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
resolveHTTPResponse({
|
|
56
|
-
req,
|
|
57
|
-
createContext,
|
|
58
|
-
path: opts.path,
|
|
59
|
-
router: opts.router,
|
|
60
|
-
batching: opts.batching,
|
|
61
|
-
responseMeta: opts.responseMeta,
|
|
62
|
-
onError (o) {
|
|
63
|
-
opts?.onError?.({
|
|
64
|
-
...o,
|
|
65
|
-
req: opts.req
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
unstable_onHead,
|
|
69
|
-
unstable_onChunk
|
|
70
|
-
}).then(()=>{
|
|
71
|
-
if (isStream) {
|
|
72
|
-
stream.push(formatter.end());
|
|
73
|
-
stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
74
|
-
}
|
|
75
|
-
}).catch(()=>{
|
|
76
|
-
if (isStream) {
|
|
77
|
-
stream.push(null);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
return promise;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* 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`
|
|
85
|
-
*
|
|
86
|
-
* Do **not** import from `@trpc/core`
|
|
87
|
-
* @example
|
|
88
|
-
* ```ts
|
|
89
|
-
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
90
|
-
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
91
|
-
* ```
|
|
92
|
-
*/ /// <reference types="@fastify/websocket" />
|
|
93
|
-
function fastifyTRPCPlugin(fastify, opts, done) {
|
|
94
|
-
fastify.removeContentTypeParser('application/json');
|
|
95
|
-
fastify.addContentTypeParser('application/json', {
|
|
96
|
-
parseAs: 'string'
|
|
97
|
-
}, function(_, body, _done) {
|
|
98
|
-
_done(null, body);
|
|
99
|
-
});
|
|
100
|
-
let prefix = opts.prefix ?? '';
|
|
101
|
-
// https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
|
|
102
|
-
// @ts-expect-error property 'default' does not exists on type ...
|
|
103
|
-
if (typeof fastifyTRPCPlugin.default !== 'function') {
|
|
104
|
-
prefix = ''; // handled by fastify internally
|
|
105
|
-
}
|
|
106
|
-
fastify.all(`${prefix}/:path`, async (req, res)=>{
|
|
107
|
-
const path = req.params.path;
|
|
108
|
-
await fastifyRequestHandler({
|
|
109
|
-
...opts.trpcOptions,
|
|
110
|
-
req,
|
|
111
|
-
res,
|
|
112
|
-
path
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
if (opts.useWSS) {
|
|
116
|
-
applyWSSHandler({
|
|
117
|
-
...opts.trpcOptions,
|
|
118
|
-
wss: fastify.websocketServer
|
|
119
|
-
});
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
121
|
-
fastify.get(prefix ?? '/', {
|
|
122
|
-
websocket: true
|
|
123
|
-
}, ()=>{});
|
|
124
|
-
}
|
|
125
|
-
done();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export { fastifyRequestHandler, fastifyTRPCPlugin };
|
|
1
|
+
export { fastifyRequestHandler } from './fastifyRequestHandler.mjs';
|
|
2
|
+
export { fastifyTRPCPlugin } from './fastifyTRPCPlugin.mjs';
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var http = require('@trpc/core/http');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 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`
|
|
7
|
+
*
|
|
8
|
+
* Do **not** import from `@trpc/core`
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
12
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
13
|
+
* ```
|
|
14
|
+
*/ // @trpc/server
|
|
15
|
+
const trimSlashes = (path)=>{
|
|
16
|
+
path = path.startsWith('/') ? path.slice(1) : path;
|
|
17
|
+
path = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
18
|
+
return path;
|
|
19
|
+
};
|
|
20
|
+
async function fetchRequestHandler(opts) {
|
|
21
|
+
const resHeaders = new Headers();
|
|
22
|
+
const createContext = async (innerOpts)=>{
|
|
23
|
+
return opts.createContext?.({
|
|
24
|
+
req: opts.req,
|
|
25
|
+
resHeaders,
|
|
26
|
+
...innerOpts
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const url = new URL(opts.req.url);
|
|
30
|
+
const pathname = trimSlashes(url.pathname);
|
|
31
|
+
const endpoint = trimSlashes(opts.endpoint);
|
|
32
|
+
const path = trimSlashes(pathname.slice(endpoint.length));
|
|
33
|
+
const req = {
|
|
34
|
+
query: url.searchParams,
|
|
35
|
+
method: opts.req.method,
|
|
36
|
+
headers: Object.fromEntries(opts.req.headers),
|
|
37
|
+
body: opts.req.headers.get('content-type')?.startsWith('application/json') ? await opts.req.text() : ''
|
|
38
|
+
};
|
|
39
|
+
let resolve;
|
|
40
|
+
const promise = new Promise((r)=>resolve = r);
|
|
41
|
+
let status = 200;
|
|
42
|
+
let isStream = false;
|
|
43
|
+
let controller;
|
|
44
|
+
let encoder;
|
|
45
|
+
let formatter;
|
|
46
|
+
const unstable_onHead = (head, isStreaming)=>{
|
|
47
|
+
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
48
|
+
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (typeof value === 'string') {
|
|
52
|
+
resHeaders.set(key, value);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
for (const v of value){
|
|
56
|
+
resHeaders.append(key, v);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
status = head.status;
|
|
60
|
+
if (isStreaming) {
|
|
61
|
+
resHeaders.set('Transfer-Encoding', 'chunked');
|
|
62
|
+
resHeaders.append('Vary', 'trpc-batch-mode');
|
|
63
|
+
const stream = new ReadableStream({
|
|
64
|
+
start (c) {
|
|
65
|
+
controller = c;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const response = new Response(stream, {
|
|
69
|
+
status,
|
|
70
|
+
headers: resHeaders
|
|
71
|
+
});
|
|
72
|
+
resolve(response);
|
|
73
|
+
encoder = new TextEncoder();
|
|
74
|
+
formatter = http.getBatchStreamFormatter();
|
|
75
|
+
isStream = true;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const unstable_onChunk = ([index, string])=>{
|
|
79
|
+
if (index === -1) {
|
|
80
|
+
// full response, no streaming
|
|
81
|
+
const response = new Response(string || null, {
|
|
82
|
+
status,
|
|
83
|
+
headers: resHeaders
|
|
84
|
+
});
|
|
85
|
+
resolve(response);
|
|
86
|
+
} else {
|
|
87
|
+
controller.enqueue(encoder.encode(formatter(index, string)));
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
http.resolveHTTPResponse({
|
|
91
|
+
req,
|
|
92
|
+
createContext,
|
|
93
|
+
path,
|
|
94
|
+
router: opts.router,
|
|
95
|
+
batching: opts.batching,
|
|
96
|
+
responseMeta: opts.responseMeta,
|
|
97
|
+
onError (o) {
|
|
98
|
+
opts?.onError?.({
|
|
99
|
+
...o,
|
|
100
|
+
req: opts.req
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
unstable_onHead,
|
|
104
|
+
unstable_onChunk
|
|
105
|
+
}).then(()=>{
|
|
106
|
+
if (isStream) {
|
|
107
|
+
controller.enqueue(encoder.encode(formatter.end()));
|
|
108
|
+
controller.close();
|
|
109
|
+
}
|
|
110
|
+
}).catch(()=>{
|
|
111
|
+
if (isStream) {
|
|
112
|
+
controller.close();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return promise;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
exports.fetchRequestHandler = fetchRequestHandler;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { resolveHTTPResponse, getBatchStreamFormatter } from '@trpc/core/http';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 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`
|
|
5
|
+
*
|
|
6
|
+
* Do **not** import from `@trpc/core`
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
10
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
11
|
+
* ```
|
|
12
|
+
*/ // @trpc/server
|
|
13
|
+
const trimSlashes = (path)=>{
|
|
14
|
+
path = path.startsWith('/') ? path.slice(1) : path;
|
|
15
|
+
path = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
16
|
+
return path;
|
|
17
|
+
};
|
|
18
|
+
async function fetchRequestHandler(opts) {
|
|
19
|
+
const resHeaders = new Headers();
|
|
20
|
+
const createContext = async (innerOpts)=>{
|
|
21
|
+
return opts.createContext?.({
|
|
22
|
+
req: opts.req,
|
|
23
|
+
resHeaders,
|
|
24
|
+
...innerOpts
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
const url = new URL(opts.req.url);
|
|
28
|
+
const pathname = trimSlashes(url.pathname);
|
|
29
|
+
const endpoint = trimSlashes(opts.endpoint);
|
|
30
|
+
const path = trimSlashes(pathname.slice(endpoint.length));
|
|
31
|
+
const req = {
|
|
32
|
+
query: url.searchParams,
|
|
33
|
+
method: opts.req.method,
|
|
34
|
+
headers: Object.fromEntries(opts.req.headers),
|
|
35
|
+
body: opts.req.headers.get('content-type')?.startsWith('application/json') ? await opts.req.text() : ''
|
|
36
|
+
};
|
|
37
|
+
let resolve;
|
|
38
|
+
const promise = new Promise((r)=>resolve = r);
|
|
39
|
+
let status = 200;
|
|
40
|
+
let isStream = false;
|
|
41
|
+
let controller;
|
|
42
|
+
let encoder;
|
|
43
|
+
let formatter;
|
|
44
|
+
const unstable_onHead = (head, isStreaming)=>{
|
|
45
|
+
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
46
|
+
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (typeof value === 'string') {
|
|
50
|
+
resHeaders.set(key, value);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
for (const v of value){
|
|
54
|
+
resHeaders.append(key, v);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
status = head.status;
|
|
58
|
+
if (isStreaming) {
|
|
59
|
+
resHeaders.set('Transfer-Encoding', 'chunked');
|
|
60
|
+
resHeaders.append('Vary', 'trpc-batch-mode');
|
|
61
|
+
const stream = new ReadableStream({
|
|
62
|
+
start (c) {
|
|
63
|
+
controller = c;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const response = new Response(stream, {
|
|
67
|
+
status,
|
|
68
|
+
headers: resHeaders
|
|
69
|
+
});
|
|
70
|
+
resolve(response);
|
|
71
|
+
encoder = new TextEncoder();
|
|
72
|
+
formatter = getBatchStreamFormatter();
|
|
73
|
+
isStream = true;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const unstable_onChunk = ([index, string])=>{
|
|
77
|
+
if (index === -1) {
|
|
78
|
+
// full response, no streaming
|
|
79
|
+
const response = new Response(string || null, {
|
|
80
|
+
status,
|
|
81
|
+
headers: resHeaders
|
|
82
|
+
});
|
|
83
|
+
resolve(response);
|
|
84
|
+
} else {
|
|
85
|
+
controller.enqueue(encoder.encode(formatter(index, string)));
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
resolveHTTPResponse({
|
|
89
|
+
req,
|
|
90
|
+
createContext,
|
|
91
|
+
path,
|
|
92
|
+
router: opts.router,
|
|
93
|
+
batching: opts.batching,
|
|
94
|
+
responseMeta: opts.responseMeta,
|
|
95
|
+
onError (o) {
|
|
96
|
+
opts?.onError?.({
|
|
97
|
+
...o,
|
|
98
|
+
req: opts.req
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
unstable_onHead,
|
|
102
|
+
unstable_onChunk
|
|
103
|
+
}).then(()=>{
|
|
104
|
+
if (isStream) {
|
|
105
|
+
controller.enqueue(encoder.encode(formatter.end()));
|
|
106
|
+
controller.close();
|
|
107
|
+
}
|
|
108
|
+
}).catch(()=>{
|
|
109
|
+
if (isStream) {
|
|
110
|
+
controller.close();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return promise;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { fetchRequestHandler };
|