@trpc/server 11.0.0-alpha-tmp-export-from-main.213 → 11.0.0-alpha-tmp-export-from-main.217

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