@trpc/server 11.0.0-rc.452 → 11.0.0-rc.455
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/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +3 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +3 -1
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +4 -1
- package/dist/adapters/ws.mjs +4 -1
- package/dist/bundle-analysis.json +86 -85
- package/dist/observable/observable.d.ts +1 -4
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/observable.js +6 -18
- package/dist/observable/observable.mjs +6 -18
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +6 -3
- package/dist/unstable-core-do-not-import/http/contentType.mjs +6 -3
- package/dist/unstable-core-do-not-import/http/types.d.ts +5 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +8 -3
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +66 -32
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +66 -32
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.d.ts +1 -0
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +3 -0
- package/dist/unstable-core-do-not-import.mjs +1 -0
- package/package.json +2 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +3 -1
- package/src/adapters/ws.ts +3 -0
- package/src/observable/observable.ts +6 -19
- package/src/unstable-core-do-not-import/http/contentType.ts +3 -0
- package/src/unstable-core-do-not-import/http/types.ts +5 -0
- package/src/unstable-core-do-not-import/stream/jsonl.ts +88 -43
- package/src/unstable-core-do-not-import/stream/utils/createServer.ts +1 -3
- package/src/unstable-core-do-not-import.ts +1 -0
|
@@ -94,18 +94,16 @@ export function isPromise(value: unknown): value is Promise<unknown> {
|
|
|
94
94
|
type Serialize = (value: any) => any;
|
|
95
95
|
type Deserialize = (value: any) => any;
|
|
96
96
|
|
|
97
|
+
type PathArray = readonly (string | number)[];
|
|
97
98
|
export type ProducerOnError = (opts: {
|
|
98
99
|
error: unknown;
|
|
99
|
-
path:
|
|
100
|
+
path: PathArray;
|
|
100
101
|
}) => void;
|
|
101
102
|
export interface ProducerOptions {
|
|
102
103
|
serialize?: Serialize;
|
|
103
104
|
data: Record<string, unknown> | unknown[];
|
|
104
105
|
onError?: ProducerOnError;
|
|
105
|
-
formatError?: (opts: {
|
|
106
|
-
error: unknown;
|
|
107
|
-
path: (string | number)[];
|
|
108
|
-
}) => unknown;
|
|
106
|
+
formatError?: (opts: { error: unknown; path: PathArray }) => unknown;
|
|
109
107
|
maxDepth?: number;
|
|
110
108
|
}
|
|
111
109
|
|
|
@@ -424,6 +422,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
424
422
|
deserialize?: Deserialize;
|
|
425
423
|
onError?: ConsumerOnError;
|
|
426
424
|
formatError?: (opts: { error: unknown }) => Error;
|
|
425
|
+
abortController: AbortController | null;
|
|
427
426
|
}) {
|
|
428
427
|
const { deserialize = (v) => v } = opts;
|
|
429
428
|
|
|
@@ -441,19 +440,38 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
441
440
|
|
|
442
441
|
type ControllerChunk = ChunkData | StreamInterruptedError;
|
|
443
442
|
type ChunkController = ReadableStreamDefaultController<ControllerChunk>;
|
|
444
|
-
|
|
445
|
-
|
|
443
|
+
type ControllerWrapper = {
|
|
444
|
+
controller: ChunkController;
|
|
445
|
+
returned: boolean;
|
|
446
|
+
};
|
|
447
|
+
const chunkDeferred = new Map<ChunkIndex, Deferred<ControllerWrapper>>();
|
|
448
|
+
const controllers = new Map<ChunkIndex, ControllerWrapper>();
|
|
449
|
+
|
|
450
|
+
const maybeAbort = () => {
|
|
451
|
+
if (
|
|
452
|
+
chunkDeferred.size === 0 &&
|
|
453
|
+
Array.from(controllers.values()).every((it) => it.returned)
|
|
454
|
+
) {
|
|
455
|
+
// nothing is listening to the stream anymore
|
|
456
|
+
opts.abortController?.abort();
|
|
457
|
+
}
|
|
458
|
+
};
|
|
446
459
|
|
|
447
460
|
function hydrateChunkDefinition(value: ChunkDefinition) {
|
|
448
461
|
const [_path, type, chunkId] = value;
|
|
449
462
|
|
|
450
463
|
const { readable, controller } = createReadableStream<ChunkData>();
|
|
451
|
-
|
|
464
|
+
|
|
465
|
+
const wrapper: ControllerWrapper = {
|
|
466
|
+
controller,
|
|
467
|
+
returned: false,
|
|
468
|
+
};
|
|
469
|
+
controllers.set(chunkId, wrapper);
|
|
452
470
|
|
|
453
471
|
// resolve chunk deferred if it exists
|
|
454
472
|
const deferred = chunkDeferred.get(chunkId);
|
|
455
473
|
if (deferred) {
|
|
456
|
-
deferred.resolve(
|
|
474
|
+
deferred.resolve(wrapper);
|
|
457
475
|
chunkDeferred.delete(chunkId);
|
|
458
476
|
}
|
|
459
477
|
|
|
@@ -478,6 +496,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
478
496
|
switch (status) {
|
|
479
497
|
case PROMISE_STATUS_FULFILLED:
|
|
480
498
|
resolve(hydrate(data));
|
|
499
|
+
|
|
481
500
|
break;
|
|
482
501
|
case PROMISE_STATUS_REJECTED:
|
|
483
502
|
reject(
|
|
@@ -490,38 +509,64 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
490
509
|
.finally(() => {
|
|
491
510
|
// reader.releaseLock();
|
|
492
511
|
controllers.delete(chunkId);
|
|
512
|
+
|
|
513
|
+
maybeAbort();
|
|
493
514
|
});
|
|
494
515
|
});
|
|
495
516
|
}
|
|
496
517
|
case CHUNK_VALUE_TYPE_ASYNC_ITERABLE: {
|
|
497
518
|
return {
|
|
498
|
-
[Symbol.asyncIterator]:
|
|
519
|
+
[Symbol.asyncIterator]: () => {
|
|
499
520
|
const reader = readable.getReader();
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const [_chunkId, status, data] = value as IterableChunk;
|
|
510
|
-
|
|
511
|
-
switch (status) {
|
|
512
|
-
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
513
|
-
yield hydrate(data);
|
|
514
|
-
break;
|
|
515
|
-
case ASYNC_ITERABLE_STATUS_DONE:
|
|
516
|
-
controllers.delete(chunkId);
|
|
517
|
-
return;
|
|
518
|
-
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
521
|
+
const iterator: AsyncIterator<unknown> = {
|
|
522
|
+
next: async () => {
|
|
523
|
+
const { done, value } = await reader.read();
|
|
524
|
+
if (value instanceof StreamInterruptedError) {
|
|
525
|
+
throw value;
|
|
526
|
+
}
|
|
527
|
+
if (done) {
|
|
519
528
|
controllers.delete(chunkId);
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
529
|
+
maybeAbort();
|
|
530
|
+
return {
|
|
531
|
+
done: true,
|
|
532
|
+
value: undefined,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const [_chunkId, status, data] = value as IterableChunk;
|
|
537
|
+
|
|
538
|
+
switch (status) {
|
|
539
|
+
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
540
|
+
return {
|
|
541
|
+
done: false,
|
|
542
|
+
value: hydrate(data),
|
|
543
|
+
};
|
|
544
|
+
case ASYNC_ITERABLE_STATUS_DONE:
|
|
545
|
+
controllers.delete(chunkId);
|
|
546
|
+
maybeAbort();
|
|
547
|
+
return {
|
|
548
|
+
done: true,
|
|
549
|
+
value: undefined,
|
|
550
|
+
};
|
|
551
|
+
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
552
|
+
controllers.delete(chunkId);
|
|
553
|
+
maybeAbort();
|
|
554
|
+
throw (
|
|
555
|
+
opts.formatError?.({ error: data }) ??
|
|
556
|
+
new AsyncError(data)
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
return: async () => {
|
|
561
|
+
wrapper.returned = true;
|
|
562
|
+
maybeAbort();
|
|
563
|
+
return {
|
|
564
|
+
done: true,
|
|
565
|
+
value: undefined,
|
|
566
|
+
};
|
|
567
|
+
},
|
|
568
|
+
};
|
|
569
|
+
return iterator;
|
|
525
570
|
},
|
|
526
571
|
};
|
|
527
572
|
}
|
|
@@ -532,14 +577,14 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
532
577
|
const [[data], ...asyncProps] = value;
|
|
533
578
|
|
|
534
579
|
for (const value of asyncProps) {
|
|
580
|
+
const [key] = value;
|
|
535
581
|
const hydrated = hydrateChunkDefinition(value);
|
|
536
582
|
|
|
537
|
-
|
|
538
|
-
if (path === null) {
|
|
583
|
+
if (key === null) {
|
|
539
584
|
return hydrated;
|
|
540
585
|
}
|
|
541
586
|
|
|
542
|
-
(data as any)[
|
|
587
|
+
(data as any)[key] = hydrated;
|
|
543
588
|
}
|
|
544
589
|
return data;
|
|
545
590
|
}
|
|
@@ -552,7 +597,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
552
597
|
deferred.reject(error);
|
|
553
598
|
}
|
|
554
599
|
chunkDeferred.clear();
|
|
555
|
-
for (const controller of controllers.values()) {
|
|
600
|
+
for (const { controller } of controllers.values()) {
|
|
556
601
|
controller.enqueue(error);
|
|
557
602
|
controller.close();
|
|
558
603
|
}
|
|
@@ -575,17 +620,17 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
575
620
|
}
|
|
576
621
|
const chunk = chunkOrHead as ChunkData;
|
|
577
622
|
const [idx] = chunk;
|
|
578
|
-
let
|
|
579
|
-
if (!
|
|
623
|
+
let wrapper = controllers.get(idx);
|
|
624
|
+
if (!wrapper) {
|
|
580
625
|
let deferred = chunkDeferred.get(idx);
|
|
581
626
|
if (!deferred) {
|
|
582
|
-
deferred = createDeferred
|
|
627
|
+
deferred = createDeferred();
|
|
583
628
|
chunkDeferred.set(idx, deferred);
|
|
584
629
|
}
|
|
585
630
|
|
|
586
|
-
|
|
631
|
+
wrapper = await deferred.promise;
|
|
587
632
|
}
|
|
588
|
-
controller.enqueue(chunk);
|
|
633
|
+
wrapper.controller.enqueue(chunk);
|
|
589
634
|
},
|
|
590
635
|
close: closeOrAbort,
|
|
591
636
|
abort: closeOrAbort,
|
|
@@ -34,6 +34,7 @@ export * from './unstable-core-do-not-import/router';
|
|
|
34
34
|
export * from './unstable-core-do-not-import/rpc';
|
|
35
35
|
export * from './unstable-core-do-not-import/stream/jsonl';
|
|
36
36
|
export * from './unstable-core-do-not-import/stream/sse';
|
|
37
|
+
export * from './unstable-core-do-not-import/stream/utils/createDeferred';
|
|
37
38
|
export * from './unstable-core-do-not-import/transformer';
|
|
38
39
|
export * from './unstable-core-do-not-import/types';
|
|
39
40
|
export * from './unstable-core-do-not-import/utils';
|