@highstate/designer 0.4.2 → 0.4.4

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 (119) hide show
  1. package/.output/nitro.json +15 -0
  2. package/.output/public/_nuxt/B-YqONwZ.js +1 -0
  3. package/.output/public/_nuxt/COXQCz2I.js +1 -0
  4. package/.output/public/_nuxt/DNF_Kfc9.js +1 -0
  5. package/.output/public/_nuxt/DYyPqv2f.js +164 -0
  6. package/.output/public/_nuxt/builds/latest.json +1 -0
  7. package/.output/public/_nuxt/builds/meta/c81be379-d98f-426f-8eac-c45f3b97a743.json +1 -0
  8. package/.output/public/_nuxt/entry.BSAdMt6G.css +5 -0
  9. package/.output/public/_nuxt/error-404.BJkSn6RI.css +1 -0
  10. package/.output/public/_nuxt/error-500.TOCKLquH.css +1 -0
  11. package/.output/public/favicon.ico +0 -0
  12. package/.output/public/robots.txt +1 -0
  13. package/.output/server/index.mjs +12266 -0
  14. package/.output/server/index.mjs.map +1 -0
  15. package/.output/server/node_modules/@babel/parser/lib/index.js +14183 -0
  16. package/.output/server/node_modules/@babel/parser/package.json +50 -0
  17. package/.output/server/node_modules/@trpc/server/dist/http.mjs +7 -0
  18. package/.output/server/node_modules/@trpc/server/dist/index.mjs +11 -0
  19. package/.output/server/node_modules/@trpc/server/dist/observable/observable.mjs +175 -0
  20. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/createProxy.mjs +73 -0
  21. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/error/TRPCError.mjs +78 -0
  22. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/error/formatter.mjs +5 -0
  23. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/error/getErrorShape.mjs +29 -0
  24. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/contentType.mjs +224 -0
  25. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
  26. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +76 -0
  27. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/isAbortError.mjs +7 -0
  28. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/parseConnectionParams.mjs +39 -0
  29. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/http/resolveResponse.mjs +569 -0
  30. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/initTRPC.mjs +80 -0
  31. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/middleware.mjs +90 -0
  32. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/parser.mjs +40 -0
  33. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/procedureBuilder.mjs +194 -0
  34. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/rootConfig.mjs +6 -0
  35. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/router.mjs +176 -0
  36. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/rpc/codes.mjs +54 -0
  37. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/jsonl.mjs +597 -0
  38. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/sse.mjs +359 -0
  39. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/tracked.mjs +25 -0
  40. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +167 -0
  41. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/createDeferred.mjs +15 -0
  42. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/disposable.mjs +41 -0
  43. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.mjs +239 -0
  44. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.mjs +23 -0
  45. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/timerResource.mjs +23 -0
  46. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +127 -0
  47. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/transformer.mjs +101 -0
  48. package/.output/server/node_modules/@trpc/server/dist/unstable-core-do-not-import/utils.mjs +83 -0
  49. package/.output/server/node_modules/@trpc/server/dist/vendor/unpromise/unpromise.mjs +289 -0
  50. package/.output/server/node_modules/@trpc/server/package.json +156 -0
  51. package/.output/server/node_modules/@unhead/dom/dist/index.mjs +197 -0
  52. package/.output/server/node_modules/@unhead/dom/package.json +40 -0
  53. package/.output/server/node_modules/@unhead/shared/dist/index.mjs +641 -0
  54. package/.output/server/node_modules/@unhead/shared/package.json +45 -0
  55. package/.output/server/node_modules/@unhead/ssr/dist/index.mjs +95 -0
  56. package/.output/server/node_modules/@unhead/ssr/package.json +40 -0
  57. package/.output/server/node_modules/@vue/compiler-core/dist/compiler-core.cjs.prod.js +6690 -0
  58. package/.output/server/node_modules/@vue/compiler-core/package.json +58 -0
  59. package/.output/server/node_modules/@vue/compiler-dom/dist/compiler-dom.cjs.prod.js +686 -0
  60. package/.output/server/node_modules/@vue/compiler-dom/package.json +57 -0
  61. package/.output/server/node_modules/@vue/compiler-ssr/dist/compiler-ssr.cjs.js +1404 -0
  62. package/.output/server/node_modules/@vue/compiler-ssr/package.json +34 -0
  63. package/.output/server/node_modules/@vue/reactivity/dist/reactivity.cjs.prod.js +1789 -0
  64. package/.output/server/node_modules/@vue/reactivity/package.json +55 -0
  65. package/.output/server/node_modules/@vue/runtime-core/dist/runtime-core.cjs.prod.js +6654 -0
  66. package/.output/server/node_modules/@vue/runtime-core/package.json +52 -0
  67. package/.output/server/node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.prod.js +1669 -0
  68. package/.output/server/node_modules/@vue/runtime-dom/package.json +60 -0
  69. package/.output/server/node_modules/@vue/server-renderer/dist/server-renderer.cjs.prod.js +857 -0
  70. package/.output/server/node_modules/@vue/server-renderer/package.json +55 -0
  71. package/.output/server/node_modules/@vue/shared/dist/shared.cjs.prod.js +594 -0
  72. package/.output/server/node_modules/@vue/shared/package.json +47 -0
  73. package/.output/server/node_modules/devalue/index.js +3 -0
  74. package/.output/server/node_modules/devalue/package.json +34 -0
  75. package/.output/server/node_modules/devalue/src/base64.js +110 -0
  76. package/.output/server/node_modules/devalue/src/constants.js +6 -0
  77. package/.output/server/node_modules/devalue/src/parse.js +160 -0
  78. package/.output/server/node_modules/devalue/src/stringify.js +232 -0
  79. package/.output/server/node_modules/devalue/src/uneval.js +351 -0
  80. package/.output/server/node_modules/devalue/src/utils.js +113 -0
  81. package/.output/server/node_modules/entities/lib/decode.js +536 -0
  82. package/.output/server/node_modules/entities/lib/decode_codepoint.js +76 -0
  83. package/.output/server/node_modules/entities/lib/generated/decode-data-html.js +9 -0
  84. package/.output/server/node_modules/entities/lib/generated/decode-data-xml.js +9 -0
  85. package/.output/server/node_modules/entities/package.json +90 -0
  86. package/.output/server/node_modules/estree-walker/dist/umd/estree-walker.js +344 -0
  87. package/.output/server/node_modules/estree-walker/package.json +37 -0
  88. package/.output/server/node_modules/hookable/dist/index.mjs +290 -0
  89. package/.output/server/node_modules/hookable/package.json +49 -0
  90. package/.output/server/node_modules/packrup/dist/index.mjs +108 -0
  91. package/.output/server/node_modules/packrup/package.json +58 -0
  92. package/.output/server/node_modules/source-map-js/lib/array-set.js +121 -0
  93. package/.output/server/node_modules/source-map-js/lib/base64-vlq.js +140 -0
  94. package/.output/server/node_modules/source-map-js/lib/base64.js +67 -0
  95. package/.output/server/node_modules/source-map-js/lib/binary-search.js +111 -0
  96. package/.output/server/node_modules/source-map-js/lib/mapping-list.js +79 -0
  97. package/.output/server/node_modules/source-map-js/lib/quick-sort.js +132 -0
  98. package/.output/server/node_modules/source-map-js/lib/source-map-consumer.js +1188 -0
  99. package/.output/server/node_modules/source-map-js/lib/source-map-generator.js +444 -0
  100. package/.output/server/node_modules/source-map-js/lib/source-node.js +413 -0
  101. package/.output/server/node_modules/source-map-js/lib/util.js +594 -0
  102. package/.output/server/node_modules/source-map-js/package.json +71 -0
  103. package/.output/server/node_modules/source-map-js/source-map.js +8 -0
  104. package/.output/server/node_modules/ufo/dist/index.mjs +630 -0
  105. package/.output/server/node_modules/ufo/package.json +47 -0
  106. package/.output/server/node_modules/unhead/dist/index.mjs +748 -0
  107. package/.output/server/node_modules/unhead/package.json +46 -0
  108. package/.output/server/node_modules/vue/dist/vue.cjs.js +80 -0
  109. package/.output/server/node_modules/vue/dist/vue.cjs.prod.js +66 -0
  110. package/.output/server/node_modules/vue/index.js +7 -0
  111. package/.output/server/node_modules/vue/index.mjs +1 -0
  112. package/.output/server/node_modules/vue/package.json +112 -0
  113. package/.output/server/node_modules/vue/server-renderer/index.mjs +1 -0
  114. package/.output/server/node_modules/vue-bundle-renderer/dist/runtime.mjs +196 -0
  115. package/.output/server/node_modules/vue-bundle-renderer/package.json +50 -0
  116. package/.output/server/node_modules/zod/lib/index.mjs +4382 -0
  117. package/.output/server/node_modules/zod/package.json +122 -0
  118. package/.output/server/package.json +1 -0
  119. package/package.json +4 -4
@@ -0,0 +1,76 @@
1
+ import { TRPC_ERROR_CODES_BY_NUMBER } from '../rpc/codes.mjs';
2
+ import { isObject } from '../utils.mjs';
3
+
4
+ const JSONRPC2_TO_HTTP_CODE = {
5
+ PARSE_ERROR: 400,
6
+ BAD_REQUEST: 400,
7
+ UNAUTHORIZED: 401,
8
+ FORBIDDEN: 403,
9
+ NOT_FOUND: 404,
10
+ METHOD_NOT_SUPPORTED: 405,
11
+ TIMEOUT: 408,
12
+ CONFLICT: 409,
13
+ PRECONDITION_FAILED: 412,
14
+ PAYLOAD_TOO_LARGE: 413,
15
+ UNSUPPORTED_MEDIA_TYPE: 415,
16
+ UNPROCESSABLE_CONTENT: 422,
17
+ TOO_MANY_REQUESTS: 429,
18
+ CLIENT_CLOSED_REQUEST: 499,
19
+ INTERNAL_SERVER_ERROR: 500,
20
+ NOT_IMPLEMENTED: 501,
21
+ BAD_GATEWAY: 502,
22
+ SERVICE_UNAVAILABLE: 503,
23
+ GATEWAY_TIMEOUT: 504
24
+ };
25
+ const HTTP_CODE_TO_JSONRPC2 = {
26
+ 400: 'BAD_REQUEST',
27
+ 401: 'UNAUTHORIZED',
28
+ 403: 'FORBIDDEN',
29
+ 404: 'NOT_FOUND',
30
+ 405: 'METHOD_NOT_SUPPORTED',
31
+ 408: 'TIMEOUT',
32
+ 409: 'CONFLICT',
33
+ 412: 'PRECONDITION_FAILED',
34
+ 413: 'PAYLOAD_TOO_LARGE',
35
+ 415: 'UNSUPPORTED_MEDIA_TYPE',
36
+ 422: 'UNPROCESSABLE_CONTENT',
37
+ 429: 'TOO_MANY_REQUESTS',
38
+ 499: 'CLIENT_CLOSED_REQUEST',
39
+ 500: 'INTERNAL_SERVER_ERROR',
40
+ 501: 'NOT_IMPLEMENTED',
41
+ 502: 'BAD_GATEWAY',
42
+ 503: 'SERVICE_UNAVAILABLE',
43
+ 504: 'GATEWAY_TIMEOUT'
44
+ };
45
+ function getStatusCodeFromKey(code) {
46
+ return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
47
+ }
48
+ function getStatusKeyFromCode(code) {
49
+ return HTTP_CODE_TO_JSONRPC2[code] ?? 'INTERNAL_SERVER_ERROR';
50
+ }
51
+ function getHTTPStatusCode(json) {
52
+ const arr = Array.isArray(json) ? json : [
53
+ json
54
+ ];
55
+ const httpStatuses = new Set(arr.map((res)=>{
56
+ if ('error' in res && isObject(res.error.data)) {
57
+ if (typeof res.error.data?.['httpStatus'] === 'number') {
58
+ return res.error.data['httpStatus'];
59
+ }
60
+ const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
61
+ return getStatusCodeFromKey(code);
62
+ }
63
+ return 200;
64
+ }));
65
+ if (httpStatuses.size !== 1) {
66
+ return 207;
67
+ }
68
+ const httpStatus = httpStatuses.values().next().value;
69
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
+ return httpStatus;
71
+ }
72
+ function getHTTPStatusCodeFromError(error) {
73
+ return getStatusCodeFromKey(error.code);
74
+ }
75
+
76
+ export { HTTP_CODE_TO_JSONRPC2, JSONRPC2_TO_HTTP_CODE, getHTTPStatusCode, getHTTPStatusCodeFromError, getStatusCodeFromKey, getStatusKeyFromCode };
@@ -0,0 +1,7 @@
1
+ import { isObject } from '../utils.mjs';
2
+
3
+ function isAbortError(error) {
4
+ return isObject(error) && error['name'] === 'AbortError';
5
+ }
6
+
7
+ export { isAbortError };
@@ -0,0 +1,39 @@
1
+ import { TRPCError } from '../error/TRPCError.mjs';
2
+ import { isObject } from '../utils.mjs';
3
+
4
+ function parseConnectionParamsFromUnknown(parsed) {
5
+ try {
6
+ if (parsed === null) {
7
+ return null;
8
+ }
9
+ if (!isObject(parsed)) {
10
+ throw new Error('Expected object');
11
+ }
12
+ const nonStringValues = Object.entries(parsed).filter(([_key, value])=>typeof value !== 'string');
13
+ if (nonStringValues.length > 0) {
14
+ throw new Error(`Expected connectionParams to be string values. Got ${nonStringValues.map(([key, value])=>`${key}: ${typeof value}`).join(', ')}`);
15
+ }
16
+ return parsed;
17
+ } catch (cause) {
18
+ throw new TRPCError({
19
+ code: 'PARSE_ERROR',
20
+ message: 'Invalid connection params shape',
21
+ cause
22
+ });
23
+ }
24
+ }
25
+ function parseConnectionParamsFromString(str) {
26
+ let parsed;
27
+ try {
28
+ parsed = JSON.parse(str);
29
+ } catch (cause) {
30
+ throw new TRPCError({
31
+ code: 'PARSE_ERROR',
32
+ message: 'Not JSON-parsable query params',
33
+ cause
34
+ });
35
+ }
36
+ return parseConnectionParamsFromUnknown(parsed);
37
+ }
38
+
39
+ export { parseConnectionParamsFromString, parseConnectionParamsFromUnknown };
@@ -0,0 +1,569 @@
1
+ import { isObservable, observableToAsyncIterable } from '../../observable/observable.mjs';
2
+ import { getErrorShape } from '../error/getErrorShape.mjs';
3
+ import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError.mjs';
4
+ import { jsonlStreamProducer, isPromise } from '../stream/jsonl.mjs';
5
+ import { sseStreamProducer, sseHeaders } from '../stream/sse.mjs';
6
+ import { transformTRPCResponse } from '../transformer.mjs';
7
+ import { run, isAsyncIterable, isObject } from '../utils.mjs';
8
+ import { getRequestInfo } from './contentType.mjs';
9
+ import { getHTTPStatusCode } from './getHTTPStatusCode.mjs';
10
+
11
+ function errorToAsyncIterable(err) {
12
+ return run(async function*() {
13
+ throw err;
14
+ });
15
+ }
16
+ const TYPE_ACCEPTED_METHOD_MAP = {
17
+ mutation: [
18
+ 'POST'
19
+ ],
20
+ query: [
21
+ 'GET'
22
+ ],
23
+ subscription: [
24
+ 'GET'
25
+ ]
26
+ };
27
+ const TYPE_ACCEPTED_METHOD_MAP_WITH_METHOD_OVERRIDE = {
28
+ // never allow GET to do a mutation
29
+ mutation: [
30
+ 'POST'
31
+ ],
32
+ query: [
33
+ 'GET',
34
+ 'POST'
35
+ ],
36
+ subscription: [
37
+ 'GET',
38
+ 'POST'
39
+ ]
40
+ };
41
+ function initResponse(initOpts) {
42
+ const { ctx, info, responseMeta, untransformedJSON, errors = [], headers } = initOpts;
43
+ let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
44
+ const eagerGeneration = !untransformedJSON;
45
+ const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [
46
+ untransformedJSON
47
+ ];
48
+ const meta = responseMeta?.({
49
+ ctx,
50
+ info,
51
+ paths: info?.calls.map((call)=>call.path),
52
+ data,
53
+ errors,
54
+ eagerGeneration,
55
+ type: info?.calls.find((call)=>call.procedure?._def.type)?.procedure?._def.type ?? 'unknown'
56
+ }) ?? {};
57
+ if (meta.headers) {
58
+ if (meta.headers instanceof Headers) {
59
+ for (const [key, value] of meta.headers.entries()){
60
+ headers.append(key, value);
61
+ }
62
+ } else {
63
+ /**
64
+ * @deprecated, delete in v12
65
+ */ for (const [key, value] of Object.entries(meta.headers)){
66
+ if (Array.isArray(value)) {
67
+ for (const v of value){
68
+ headers.append(key, v);
69
+ }
70
+ } else if (typeof value === 'string') {
71
+ headers.set(key, value);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ if (meta.status) {
77
+ status = meta.status;
78
+ }
79
+ return {
80
+ status
81
+ };
82
+ }
83
+ function caughtErrorToData(cause, errorOpts) {
84
+ const { router, req, onError } = errorOpts.opts;
85
+ const error = getTRPCErrorFromUnknown(cause);
86
+ onError?.({
87
+ error,
88
+ path: errorOpts.path,
89
+ input: errorOpts.input,
90
+ ctx: errorOpts.ctx,
91
+ type: errorOpts.type,
92
+ req
93
+ });
94
+ const untransformedJSON = {
95
+ error: getErrorShape({
96
+ config: router._def._config,
97
+ error,
98
+ type: errorOpts.type,
99
+ path: errorOpts.path,
100
+ input: errorOpts.input,
101
+ ctx: errorOpts.ctx
102
+ })
103
+ };
104
+ const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
105
+ const body = JSON.stringify(transformedJSON);
106
+ return {
107
+ error,
108
+ untransformedJSON,
109
+ body
110
+ };
111
+ }
112
+ /**
113
+ * Check if a value is a stream-like object
114
+ * - if it's an async iterable
115
+ * - if it's an object with async iterables or promises
116
+ */ function isDataStream(v) {
117
+ if (!isObject(v)) {
118
+ return false;
119
+ }
120
+ if (isAsyncIterable(v)) {
121
+ return true;
122
+ }
123
+ return Object.values(v).some(isPromise) || Object.values(v).some(isAsyncIterable);
124
+ }
125
+ async function resolveResponse(opts) {
126
+ const { router, req } = opts;
127
+ const headers = new Headers([
128
+ [
129
+ 'vary',
130
+ 'trpc-accept'
131
+ ]
132
+ ]);
133
+ const config = router._def._config;
134
+ const url = new URL(req.url);
135
+ if (req.method === 'HEAD') {
136
+ // can be used for lambda warmup
137
+ return new Response(null, {
138
+ status: 204
139
+ });
140
+ }
141
+ const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
142
+ const allowMethodOverride = (opts.allowMethodOverride ?? false) && req.method === 'POST';
143
+ const infoTuple = run(()=>{
144
+ try {
145
+ return [
146
+ undefined,
147
+ getRequestInfo({
148
+ req,
149
+ path: decodeURIComponent(opts.path),
150
+ router,
151
+ searchParams: url.searchParams,
152
+ headers: opts.req.headers
153
+ })
154
+ ];
155
+ } catch (cause) {
156
+ return [
157
+ getTRPCErrorFromUnknown(cause),
158
+ undefined
159
+ ];
160
+ }
161
+ });
162
+ const ctxManager = run(()=>{
163
+ let result = undefined;
164
+ return {
165
+ valueOrUndefined: ()=>{
166
+ if (!result) {
167
+ return undefined;
168
+ }
169
+ return result[1];
170
+ },
171
+ value: ()=>{
172
+ const [err, ctx] = result;
173
+ if (err) {
174
+ throw err;
175
+ }
176
+ return ctx;
177
+ },
178
+ create: async (info)=>{
179
+ if (result) {
180
+ throw new Error('This should only be called once - report a bug in tRPC');
181
+ }
182
+ try {
183
+ const ctx = await opts.createContext({
184
+ info
185
+ });
186
+ result = [
187
+ undefined,
188
+ ctx
189
+ ];
190
+ } catch (cause) {
191
+ result = [
192
+ getTRPCErrorFromUnknown(cause),
193
+ undefined
194
+ ];
195
+ }
196
+ }
197
+ };
198
+ });
199
+ const methodMapper = allowMethodOverride ? TYPE_ACCEPTED_METHOD_MAP_WITH_METHOD_OVERRIDE : TYPE_ACCEPTED_METHOD_MAP;
200
+ /**
201
+ * @deprecated
202
+ */ const isStreamCall = req.headers.get('trpc-accept') === 'application/jsonl';
203
+ const experimentalIterablesAndDeferreds = config.iterablesAndDeferreds ?? true;
204
+ const experimentalSSE = config.sse?.enabled ?? true;
205
+ try {
206
+ const [infoError, info] = infoTuple;
207
+ if (infoError) {
208
+ throw infoError;
209
+ }
210
+ if (info.isBatchCall && !allowBatching) {
211
+ throw new TRPCError({
212
+ code: 'BAD_REQUEST',
213
+ message: `Batching is not enabled on the server`
214
+ });
215
+ }
216
+ /* istanbul ignore if -- @preserve */ if (isStreamCall && !info.isBatchCall) {
217
+ throw new TRPCError({
218
+ message: `Streaming requests must be batched (you can do a batch of 1)`,
219
+ code: 'BAD_REQUEST'
220
+ });
221
+ }
222
+ await ctxManager.create(info);
223
+ const rpcCalls = info.calls.map(async (call)=>{
224
+ const proc = call.procedure;
225
+ try {
226
+ if (opts.error) {
227
+ throw opts.error;
228
+ }
229
+ if (!proc) {
230
+ throw new TRPCError({
231
+ code: 'NOT_FOUND',
232
+ message: `No procedure found on path "${call.path}"`
233
+ });
234
+ }
235
+ if (!methodMapper[proc._def.type].includes(req.method)) {
236
+ throw new TRPCError({
237
+ code: 'METHOD_NOT_SUPPORTED',
238
+ message: `Unsupported ${req.method}-request to ${proc._def.type} procedure at path "${call.path}"`
239
+ });
240
+ }
241
+ if (proc._def.type === 'subscription') {
242
+ /* istanbul ignore if -- @preserve */ if (info.isBatchCall) {
243
+ throw new TRPCError({
244
+ code: 'BAD_REQUEST',
245
+ message: `Cannot batch subscription calls`
246
+ });
247
+ }
248
+ }
249
+ const data = await proc({
250
+ path: call.path,
251
+ getRawInput: call.getRawInput,
252
+ ctx: ctxManager.value(),
253
+ type: proc._def.type,
254
+ signal: opts.req.signal
255
+ });
256
+ return [
257
+ undefined,
258
+ {
259
+ data
260
+ }
261
+ ];
262
+ } catch (cause) {
263
+ const error = getTRPCErrorFromUnknown(cause);
264
+ const input = call.result();
265
+ opts.onError?.({
266
+ error,
267
+ path: call.path,
268
+ input,
269
+ ctx: ctxManager.valueOrUndefined(),
270
+ type: call.procedure?._def.type ?? 'unknown',
271
+ req: opts.req
272
+ });
273
+ return [
274
+ error,
275
+ undefined
276
+ ];
277
+ }
278
+ });
279
+ // ----------- response handlers -----------
280
+ if (!info.isBatchCall) {
281
+ const [call] = info.calls;
282
+ const [error, result] = await rpcCalls[0];
283
+ switch(info.type){
284
+ case 'unknown':
285
+ case 'mutation':
286
+ case 'query':
287
+ {
288
+ // httpLink
289
+ headers.set('content-type', 'application/json');
290
+ if (isDataStream(result?.data)) {
291
+ throw new TRPCError({
292
+ code: 'UNSUPPORTED_MEDIA_TYPE',
293
+ message: 'Cannot use stream-like response in non-streaming request - use httpBatchStreamLink'
294
+ });
295
+ }
296
+ const res = error ? {
297
+ error: getErrorShape({
298
+ config,
299
+ ctx: ctxManager.valueOrUndefined(),
300
+ error,
301
+ input: call.result(),
302
+ path: call.path,
303
+ type: info.type
304
+ })
305
+ } : {
306
+ result: {
307
+ data: result.data
308
+ }
309
+ };
310
+ const headResponse = initResponse({
311
+ ctx: ctxManager.valueOrUndefined(),
312
+ info,
313
+ responseMeta: opts.responseMeta,
314
+ errors: error ? [
315
+ error
316
+ ] : [],
317
+ headers,
318
+ untransformedJSON: [
319
+ res
320
+ ]
321
+ });
322
+ return new Response(JSON.stringify(transformTRPCResponse(config, res)), {
323
+ status: headResponse.status,
324
+ headers
325
+ });
326
+ }
327
+ case 'subscription':
328
+ {
329
+ // httpSubscriptionLink
330
+ const iterable = run(()=>{
331
+ if (error) {
332
+ return errorToAsyncIterable(error);
333
+ }
334
+ if (!experimentalSSE) {
335
+ return errorToAsyncIterable(new TRPCError({
336
+ code: 'METHOD_NOT_SUPPORTED',
337
+ message: 'Missing experimental flag "sseSubscriptions"'
338
+ }));
339
+ }
340
+ if (!isObservable(result.data) && !isAsyncIterable(result.data)) {
341
+ return errorToAsyncIterable(new TRPCError({
342
+ message: `Subscription ${call.path} did not return an observable or a AsyncGenerator`,
343
+ code: 'INTERNAL_SERVER_ERROR'
344
+ }));
345
+ }
346
+ const dataAsIterable = isObservable(result.data) ? observableToAsyncIterable(result.data, opts.req.signal) : result.data;
347
+ return dataAsIterable;
348
+ });
349
+ const stream = sseStreamProducer({
350
+ ...config.sse,
351
+ data: iterable,
352
+ serialize: (v)=>config.transformer.output.serialize(v),
353
+ formatError (errorOpts) {
354
+ const error = getTRPCErrorFromUnknown(errorOpts.error);
355
+ const input = call?.result();
356
+ const path = call?.path;
357
+ const type = call?.procedure?._def.type ?? 'unknown';
358
+ opts.onError?.({
359
+ error,
360
+ path,
361
+ input,
362
+ ctx: ctxManager.valueOrUndefined(),
363
+ req: opts.req,
364
+ type
365
+ });
366
+ const shape = getErrorShape({
367
+ config,
368
+ ctx: ctxManager.valueOrUndefined(),
369
+ error,
370
+ input,
371
+ path,
372
+ type
373
+ });
374
+ return shape;
375
+ }
376
+ });
377
+ for (const [key, value] of Object.entries(sseHeaders)){
378
+ headers.set(key, value);
379
+ }
380
+ const headResponse = initResponse({
381
+ ctx: ctxManager.valueOrUndefined(),
382
+ info,
383
+ responseMeta: opts.responseMeta,
384
+ errors: [],
385
+ headers,
386
+ untransformedJSON: null
387
+ });
388
+ return new Response(stream, {
389
+ headers,
390
+ status: headResponse.status
391
+ });
392
+ }
393
+ }
394
+ }
395
+ // batch response handlers
396
+ if (info.accept === 'application/jsonl') {
397
+ // httpBatchStreamLink
398
+ headers.set('content-type', 'application/json');
399
+ headers.set('transfer-encoding', 'chunked');
400
+ const headResponse = initResponse({
401
+ ctx: ctxManager.valueOrUndefined(),
402
+ info,
403
+ responseMeta: opts.responseMeta,
404
+ errors: [],
405
+ headers,
406
+ untransformedJSON: null
407
+ });
408
+ const stream = jsonlStreamProducer({
409
+ /**
410
+ * Example structure for `maxDepth: 4`:
411
+ * {
412
+ * // 1
413
+ * 0: {
414
+ * // 2
415
+ * result: {
416
+ * // 3
417
+ * data: // 4
418
+ * }
419
+ * }
420
+ * }
421
+ */ maxDepth: experimentalIterablesAndDeferreds ? 4 : 3,
422
+ data: rpcCalls.map(async (res)=>{
423
+ const [error, result] = await res;
424
+ const call = info.calls[0];
425
+ if (error) {
426
+ return {
427
+ error: getErrorShape({
428
+ config,
429
+ ctx: ctxManager.valueOrUndefined(),
430
+ error,
431
+ input: call.result(),
432
+ path: call.path,
433
+ type: call.procedure?._def.type ?? 'unknown'
434
+ })
435
+ };
436
+ }
437
+ /**
438
+ * Not very pretty, but we need to wrap nested data in promises
439
+ * Our stream producer will only resolve top-level async values or async values that are directly nested in another async value
440
+ */ const iterable = isObservable(result.data) ? observableToAsyncIterable(result.data, opts.req.signal) : Promise.resolve(result.data);
441
+ return {
442
+ result: Promise.resolve({
443
+ data: iterable
444
+ })
445
+ };
446
+ }),
447
+ serialize: config.transformer.output.serialize,
448
+ onError: (cause)=>{
449
+ opts.onError?.({
450
+ error: getTRPCErrorFromUnknown(cause),
451
+ path: undefined,
452
+ input: undefined,
453
+ ctx: ctxManager.valueOrUndefined(),
454
+ req: opts.req,
455
+ type: info?.type ?? 'unknown'
456
+ });
457
+ },
458
+ formatError (errorOpts) {
459
+ const call = info?.calls[errorOpts.path[0]];
460
+ const error = getTRPCErrorFromUnknown(errorOpts.error);
461
+ const input = call?.result();
462
+ const path = call?.path;
463
+ const type = call?.procedure?._def.type ?? 'unknown';
464
+ // no need to call `onError` here as it will be propagated through the stream itself
465
+ const shape = getErrorShape({
466
+ config,
467
+ ctx: ctxManager.valueOrUndefined(),
468
+ error,
469
+ input,
470
+ path,
471
+ type
472
+ });
473
+ return shape;
474
+ }
475
+ });
476
+ return new Response(stream, {
477
+ headers,
478
+ status: headResponse.status
479
+ });
480
+ }
481
+ // httpBatchLink
482
+ /**
483
+ * Non-streaming response:
484
+ * - await all responses in parallel, blocking on the slowest one
485
+ * - create headers with known response body
486
+ * - return a complete HTTPResponse
487
+ */ headers.set('content-type', 'application/json');
488
+ const results = (await Promise.all(rpcCalls)).map((res)=>{
489
+ const [error, result] = res;
490
+ if (error) {
491
+ return res;
492
+ }
493
+ if (isDataStream(result.data)) {
494
+ return [
495
+ new TRPCError({
496
+ code: 'UNSUPPORTED_MEDIA_TYPE',
497
+ message: 'Cannot use stream-like response in non-streaming request - use httpBatchStreamLink'
498
+ }),
499
+ undefined
500
+ ];
501
+ }
502
+ return res;
503
+ });
504
+ const resultAsRPCResponse = results.map(([error, result], index)=>{
505
+ const call = info.calls[index];
506
+ if (error) {
507
+ return {
508
+ error: getErrorShape({
509
+ config,
510
+ ctx: ctxManager.valueOrUndefined(),
511
+ error,
512
+ input: call.result(),
513
+ path: call.path,
514
+ type: call.procedure?._def.type ?? 'unknown'
515
+ })
516
+ };
517
+ }
518
+ return {
519
+ result: {
520
+ data: result.data
521
+ }
522
+ };
523
+ });
524
+ const errors = results.map(([error])=>error).filter(Boolean);
525
+ const headResponse = initResponse({
526
+ ctx: ctxManager.valueOrUndefined(),
527
+ info,
528
+ responseMeta: opts.responseMeta,
529
+ untransformedJSON: resultAsRPCResponse,
530
+ errors,
531
+ headers
532
+ });
533
+ return new Response(JSON.stringify(transformTRPCResponse(config, resultAsRPCResponse)), {
534
+ status: headResponse.status,
535
+ headers
536
+ });
537
+ } catch (cause) {
538
+ const [_infoError, info] = infoTuple;
539
+ const ctx = ctxManager.valueOrUndefined();
540
+ // we get here if
541
+ // - batching is called when it's not enabled
542
+ // - `createContext()` throws
543
+ // - `router._def._config.transformer.output.serialize()` throws
544
+ // - post body is too large
545
+ // - input deserialization fails
546
+ // - `errorFormatter` return value is malformed
547
+ const { error, untransformedJSON, body } = caughtErrorToData(cause, {
548
+ opts,
549
+ ctx: ctxManager.valueOrUndefined(),
550
+ type: info?.type ?? 'unknown'
551
+ });
552
+ const headResponse = initResponse({
553
+ ctx,
554
+ info,
555
+ responseMeta: opts.responseMeta,
556
+ untransformedJSON,
557
+ errors: [
558
+ error
559
+ ],
560
+ headers
561
+ });
562
+ return new Response(body, {
563
+ status: headResponse.status,
564
+ headers
565
+ });
566
+ }
567
+ }
568
+
569
+ export { resolveResponse };