@trpc/server 11.0.0-rc.693 → 11.0.0-rc.694

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.
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var unpromise = require('../../vendor/unpromise/unpromise.js');
4
3
  var utils = require('../utils.js');
5
4
  var asyncIterable = require('./utils/asyncIterable.js');
6
5
  var createDeferred = require('./utils/createDeferred.js');
7
6
  var disposable = require('./utils/disposable.js');
7
+ var mergeAsyncIterables = require('./utils/mergeAsyncIterables.js');
8
8
  var readableStreamFrom = require('./utils/readableStreamFrom.js');
9
9
 
10
10
  function _define_property(obj, key, value) {
@@ -103,223 +103,189 @@ class MaxDepthError extends Error {
103
103
  }
104
104
  }
105
105
  async function* createBatchStreamProducer(opts) {
106
- const env = {
107
- stack: [],
108
- error: void 0,
109
- hasError: false
110
- };
111
- try {
112
- const { data } = opts;
113
- let counter = 0;
114
- const placeholder = 0;
115
- const queue = _ts_add_disposable_resource(env, disposable.makeAsyncResource(new Set(), async ()=>{
116
- await Promise.all(Array.from(queue).map((it)=>it.iterator.return?.()));
117
- }), true);
118
- ;
119
- function registerAsync(callback) {
120
- const idx = counter++;
121
- const iterator = callback(idx)[Symbol.asyncIterator]();
122
- const nextPromise = iterator.next();
123
- nextPromise.catch(()=>{
124
- // prevent unhandled promise rejection
125
- });
126
- queue.add({
127
- iterator,
128
- nextPromise
129
- });
130
- return idx;
131
- }
132
- function encodePromise(promise, path) {
133
- return registerAsync(async function*(idx) {
106
+ const { data } = opts;
107
+ let counter = 0;
108
+ const placeholder = 0;
109
+ const mergedIterables = mergeAsyncIterables.mergeAsyncIterables();
110
+ function registerAsync(callback) {
111
+ const idx = counter++;
112
+ const iterable = callback(idx);
113
+ mergedIterables.add(iterable);
114
+ return idx;
115
+ }
116
+ function encodePromise(promise, path) {
117
+ return registerAsync(async function*(idx) {
118
+ const error = checkMaxDepth(path);
119
+ if (error) {
120
+ // Catch any errors from the original promise to ensure they're reported
121
+ promise.catch((cause)=>{
122
+ opts.onError?.({
123
+ error: cause,
124
+ path
125
+ });
126
+ });
127
+ // Replace the promise with a rejected one containing the max depth error
128
+ promise = Promise.reject(error);
129
+ }
130
+ try {
131
+ const next = await promise;
132
+ yield [
133
+ idx,
134
+ PROMISE_STATUS_FULFILLED,
135
+ encode(next, path)
136
+ ];
137
+ } catch (cause) {
138
+ opts.onError?.({
139
+ error: cause,
140
+ path
141
+ });
142
+ yield [
143
+ idx,
144
+ PROMISE_STATUS_REJECTED,
145
+ opts.formatError?.({
146
+ error: cause,
147
+ path
148
+ })
149
+ ];
150
+ }
151
+ });
152
+ }
153
+ function encodeAsyncIterable(iterable, path) {
154
+ return registerAsync(async function*(idx) {
155
+ const env = {
156
+ stack: [],
157
+ error: void 0,
158
+ hasError: false
159
+ };
160
+ try {
134
161
  const error = checkMaxDepth(path);
135
162
  if (error) {
136
- // Catch any errors from the original promise to ensure they're reported
137
- promise.catch((cause)=>{
138
- opts.onError?.({
139
- error: cause,
140
- path
141
- });
142
- });
143
- // Replace the promise with a rejected one containing the max depth error
144
- promise = Promise.reject(error);
163
+ throw error;
145
164
  }
165
+ const iterator = _ts_add_disposable_resource(env, asyncIterable.iteratorResource(iterable), true);
166
+ ;
146
167
  try {
147
- const next = await promise;
148
- return [
149
- idx,
150
- PROMISE_STATUS_FULFILLED,
151
- encode(next, path)
152
- ];
168
+ while(true){
169
+ const next = await iterator.next();
170
+ if (next.done) {
171
+ yield [
172
+ idx,
173
+ ASYNC_ITERABLE_STATUS_RETURN,
174
+ encode(next.value, path)
175
+ ];
176
+ break;
177
+ }
178
+ yield [
179
+ idx,
180
+ ASYNC_ITERABLE_STATUS_YIELD,
181
+ encode(next.value, path)
182
+ ];
183
+ }
153
184
  } catch (cause) {
154
185
  opts.onError?.({
155
186
  error: cause,
156
187
  path
157
188
  });
158
- return [
189
+ yield [
159
190
  idx,
160
- PROMISE_STATUS_REJECTED,
191
+ ASYNC_ITERABLE_STATUS_ERROR,
161
192
  opts.formatError?.({
162
193
  error: cause,
163
194
  path
164
195
  })
165
196
  ];
166
197
  }
167
- });
198
+ } catch (e) {
199
+ env.error = e;
200
+ env.hasError = true;
201
+ } finally{
202
+ const result = _ts_dispose_resources(env);
203
+ if (result) await result;
204
+ }
205
+ });
206
+ }
207
+ function checkMaxDepth(path) {
208
+ if (opts.maxDepth && path.length > opts.maxDepth) {
209
+ return new MaxDepthError(path);
168
210
  }
169
- function encodeAsyncIterable(iterable, path) {
170
- return registerAsync(async function*(idx) {
171
- const env = {
172
- stack: [],
173
- error: void 0,
174
- hasError: false
175
- };
176
- try {
177
- const error = checkMaxDepth(path);
178
- if (error) {
179
- throw error;
180
- }
181
- const iterator = _ts_add_disposable_resource(env, asyncIterable.iteratorResource(iterable), true);
182
- ;
183
- try {
184
- while(true){
185
- const next = await iterator.next();
186
- if (next.done) {
187
- return [
188
- idx,
189
- ASYNC_ITERABLE_STATUS_RETURN,
190
- encode(next.value, path)
191
- ];
192
- }
193
- yield [
194
- idx,
195
- ASYNC_ITERABLE_STATUS_YIELD,
196
- encode(next.value, path)
197
- ];
198
- }
199
- } catch (cause) {
200
- opts.onError?.({
201
- error: cause,
202
- path
203
- });
204
- return [
205
- idx,
206
- ASYNC_ITERABLE_STATUS_ERROR,
207
- opts.formatError?.({
208
- error: cause,
209
- path
210
- })
211
- ];
212
- }
213
- } catch (e) {
214
- env.error = e;
215
- env.hasError = true;
216
- } finally{
217
- const result = _ts_dispose_resources(env);
218
- if (result) await result;
219
- }
220
- });
211
+ return null;
212
+ }
213
+ function encodeAsync(value, path) {
214
+ if (isPromise(value)) {
215
+ return [
216
+ CHUNK_VALUE_TYPE_PROMISE,
217
+ encodePromise(value, path)
218
+ ];
221
219
  }
222
- function checkMaxDepth(path) {
223
- if (opts.maxDepth && path.length > opts.maxDepth) {
224
- return new MaxDepthError(path);
220
+ if (utils.isAsyncIterable(value)) {
221
+ if (opts.maxDepth && path.length >= opts.maxDepth) {
222
+ throw new Error('Max depth reached');
225
223
  }
226
- return null;
224
+ return [
225
+ CHUNK_VALUE_TYPE_ASYNC_ITERABLE,
226
+ encodeAsyncIterable(value, path)
227
+ ];
227
228
  }
228
- function encodeAsync(value, path) {
229
- if (isPromise(value)) {
230
- return [
231
- CHUNK_VALUE_TYPE_PROMISE,
232
- encodePromise(value, path)
233
- ];
234
- }
235
- if (utils.isAsyncIterable(value)) {
236
- if (opts.maxDepth && path.length >= opts.maxDepth) {
237
- throw new Error('Max depth reached');
238
- }
239
- return [
240
- CHUNK_VALUE_TYPE_ASYNC_ITERABLE,
241
- encodeAsyncIterable(value, path)
242
- ];
243
- }
244
- return null;
229
+ return null;
230
+ }
231
+ function encode(value, path) {
232
+ if (value === undefined) {
233
+ return [
234
+ []
235
+ ];
245
236
  }
246
- function encode(value, path) {
247
- if (value === undefined) {
248
- return [
249
- []
250
- ];
251
- }
252
- if (!utils.isObject(value)) {
253
- return [
254
- [
255
- value
256
- ]
257
- ];
258
- }
259
- const reg = encodeAsync(value, path);
260
- if (reg) {
261
- return [
262
- [
263
- placeholder
264
- ],
265
- [
266
- null,
267
- ...reg
268
- ]
269
- ];
270
- }
271
- const newObj = {};
272
- const asyncValues = [];
273
- for (const [key, item] of Object.entries(value)){
274
- const transformed = encodeAsync(item, [
275
- ...path,
276
- key
277
- ]);
278
- if (!transformed) {
279
- newObj[key] = item;
280
- continue;
281
- }
282
- newObj[key] = placeholder;
283
- asyncValues.push([
284
- key,
285
- ...transformed
286
- ]);
287
- }
237
+ if (!utils.isObject(value)) {
238
+ return [
239
+ [
240
+ value
241
+ ]
242
+ ];
243
+ }
244
+ const reg = encodeAsync(value, path);
245
+ if (reg) {
288
246
  return [
289
247
  [
290
- newObj
248
+ placeholder
291
249
  ],
292
- ...asyncValues
250
+ [
251
+ null,
252
+ ...reg
253
+ ]
293
254
  ];
294
255
  }
295
- const newHead = {};
296
- for (const [key, item] of Object.entries(data)){
297
- newHead[key] = encode(item, [
256
+ const newObj = {};
257
+ const asyncValues = [];
258
+ for (const [key, item] of Object.entries(value)){
259
+ const transformed = encodeAsync(item, [
260
+ ...path,
298
261
  key
299
262
  ]);
300
- }
301
- yield newHead;
302
- // Process all async iterables in parallel by racing their next values
303
- while(queue.size > 0){
304
- // Race all iterators to get the next value from any of them
305
- const [entry, res] = await unpromise.Unpromise.race(Array.from(queue).map(async (it)=>[
306
- it,
307
- await it.nextPromise
308
- ]));
309
- yield res.value;
310
- // Remove current iterator and re-add if not done
311
- queue.delete(entry);
312
- if (!res.done) {
313
- entry.nextPromise = entry.iterator.next();
314
- queue.add(entry);
263
+ if (!transformed) {
264
+ newObj[key] = item;
265
+ continue;
315
266
  }
267
+ newObj[key] = placeholder;
268
+ asyncValues.push([
269
+ key,
270
+ ...transformed
271
+ ]);
316
272
  }
317
- } catch (e) {
318
- env.error = e;
319
- env.hasError = true;
320
- } finally{
321
- const result = _ts_dispose_resources(env);
322
- if (result) await result;
273
+ return [
274
+ [
275
+ newObj
276
+ ],
277
+ ...asyncValues
278
+ ];
279
+ }
280
+ const newHead = {};
281
+ for (const [key, item] of Object.entries(data)){
282
+ newHead[key] = encode(item, [
283
+ key
284
+ ]);
285
+ }
286
+ yield newHead;
287
+ for await (const value of mergedIterables){
288
+ yield value;
323
289
  }
324
290
  }
325
291
  /**