@trpc/client 11.0.0-rc.370 → 11.0.0-rc.374
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/bundle-analysis.json +60 -106
- package/dist/index.js +0 -1
- package/dist/index.mjs +1 -1
- package/dist/internals/dataLoader.d.ts +3 -4
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/dataLoader.js +14 -13
- package/dist/internals/dataLoader.mjs +14 -13
- package/dist/links/httpBatchLink.d.ts +6 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +95 -30
- package/dist/links/httpBatchLink.mjs +96 -31
- package/dist/links/httpBatchStreamLink.d.ts +9 -6
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpBatchStreamLink.js +132 -34
- package/dist/links/httpBatchStreamLink.mjs +132 -34
- package/dist/links/httpLink.d.ts +4 -7
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +72 -46
- package/dist/links/httpLink.mjs +74 -47
- package/dist/links/internals/httpUtils.d.ts +2 -4
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.js +4 -32
- package/dist/links/internals/httpUtils.mjs +4 -32
- package/package.json +4 -4
- package/src/internals/dataLoader.ts +21 -19
- package/src/links/httpBatchLink.ts +132 -46
- package/src/links/httpBatchStreamLink.ts +173 -48
- package/src/links/httpLink.ts +100 -60
- package/src/links/internals/httpUtils.ts +5 -41
- package/dist/links/internals/createHTTPBatchLink.d.ts +0 -20
- package/dist/links/internals/createHTTPBatchLink.d.ts.map +0 -1
- package/dist/links/internals/createHTTPBatchLink.js +0 -85
- package/dist/links/internals/createHTTPBatchLink.mjs +0 -83
- package/dist/links/internals/getTextDecoder.d.ts +0 -3
- package/dist/links/internals/getTextDecoder.d.ts.map +0 -1
- package/dist/links/internals/getTextDecoder.js +0 -16
- package/dist/links/internals/getTextDecoder.mjs +0 -14
- package/dist/links/internals/parseJSONStream.d.ts +0 -39
- package/dist/links/internals/parseJSONStream.d.ts.map +0 -1
- package/dist/links/internals/parseJSONStream.js +0 -118
- package/dist/links/internals/parseJSONStream.mjs +0 -115
- package/dist/links/internals/streamingUtils.d.ts +0 -7
- package/dist/links/internals/streamingUtils.d.ts.map +0 -1
- package/src/links/internals/createHTTPBatchLink.ts +0 -133
- package/src/links/internals/getTextDecoder.ts +0 -19
- package/src/links/internals/parseJSONStream.ts +0 -166
- package/src/links/internals/streamingUtils.ts +0 -6
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// Stream parsing adapted from https://www.loginradius.com/blog/engineering/guest-post/http-streaming-with-nodejs-and-fetch-api/
|
|
2
|
-
import type { TRPCResponse } from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
-
import type {
|
|
4
|
-
NodeJSReadableStreamEsque,
|
|
5
|
-
WebReadableStreamEsque,
|
|
6
|
-
} from '../../internals/types';
|
|
7
|
-
import type { HTTPHeaders } from '../types';
|
|
8
|
-
import type { HTTPBaseRequestOptions, HTTPResult } from './httpUtils';
|
|
9
|
-
import { fetchHTTPResponse, getBody, getUrl } from './httpUtils';
|
|
10
|
-
import type { TextDecoderEsque } from './streamingUtils';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* @description Take a stream of bytes and call `onLine` with
|
|
15
|
-
* a JSON object for each line in the stream. Expected stream
|
|
16
|
-
* format is:
|
|
17
|
-
* ```json
|
|
18
|
-
* {"1": {...}
|
|
19
|
-
* ,"0": {...}
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export async function parseJSONStream<TReturn>(opts: {
|
|
24
|
-
/**
|
|
25
|
-
* As given by `(await fetch(url)).body`
|
|
26
|
-
*/
|
|
27
|
-
readableStream: NodeJSReadableStreamEsque | WebReadableStreamEsque;
|
|
28
|
-
/**
|
|
29
|
-
* Called for each line of the stream
|
|
30
|
-
*/
|
|
31
|
-
onSingle: (index: number, res: TReturn) => void;
|
|
32
|
-
/**
|
|
33
|
-
* Transform text into useable data object (defaults to JSON.parse)
|
|
34
|
-
*/
|
|
35
|
-
parse?: (text: string) => TReturn;
|
|
36
|
-
signal?: AbortSignal;
|
|
37
|
-
textDecoder: TextDecoderEsque;
|
|
38
|
-
}): Promise<void> {
|
|
39
|
-
const parse = opts.parse ?? JSON.parse;
|
|
40
|
-
|
|
41
|
-
const onLine = (line: string) => {
|
|
42
|
-
if (opts.signal?.aborted) return;
|
|
43
|
-
if (!line || line === '}') {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* At this point, `line` can be one of two things:
|
|
48
|
-
* - The first line of the stream `{"2":{...}`
|
|
49
|
-
* - A line in the middle of the stream `,"2":{...}`
|
|
50
|
-
*/
|
|
51
|
-
const indexOfColon = line.indexOf(':');
|
|
52
|
-
const indexAsStr = line.substring(2, indexOfColon - 1);
|
|
53
|
-
const text = line.substring(indexOfColon + 1);
|
|
54
|
-
|
|
55
|
-
opts.onSingle(Number(indexAsStr), parse(text));
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
await readLines(opts.readableStream, onLine, opts.textDecoder);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Handle transforming a stream of bytes into lines of text.
|
|
63
|
-
* To avoid using AsyncIterators / AsyncGenerators,
|
|
64
|
-
* we use a callback for each line.
|
|
65
|
-
*
|
|
66
|
-
* @param readableStream can be a NodeJS stream or a WebAPI stream
|
|
67
|
-
* @param onLine will be called for every line ('\n' delimited) in the stream
|
|
68
|
-
*/
|
|
69
|
-
async function readLines(
|
|
70
|
-
readableStream: NodeJSReadableStreamEsque | WebReadableStreamEsque,
|
|
71
|
-
onLine: (line: string) => void,
|
|
72
|
-
textDecoder: TextDecoderEsque,
|
|
73
|
-
) {
|
|
74
|
-
let partOfLine = '';
|
|
75
|
-
|
|
76
|
-
const onChunk = (chunk: Uint8Array) => {
|
|
77
|
-
const chunkText = textDecoder.decode(chunk);
|
|
78
|
-
const chunkLines = chunkText.split('\n');
|
|
79
|
-
if (chunkLines.length === 1) {
|
|
80
|
-
partOfLine += chunkLines[0];
|
|
81
|
-
} else if (chunkLines.length > 1) {
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
|
|
83
|
-
onLine(partOfLine + chunkLines[0]!);
|
|
84
|
-
for (let i = 1; i < chunkLines.length - 1; i++) {
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
|
|
86
|
-
onLine(chunkLines[i]!);
|
|
87
|
-
}
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length doesn't change, so is necessarily > 1
|
|
89
|
-
partOfLine = chunkLines[chunkLines.length - 1]!;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// we handle 2 different types of streams, this if where we figure out which one we have
|
|
94
|
-
if ('getReader' in readableStream) {
|
|
95
|
-
await readStandardChunks(readableStream, onChunk);
|
|
96
|
-
} else {
|
|
97
|
-
await readNodeChunks(readableStream, onChunk);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
onLine(partOfLine);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Handle NodeJS stream
|
|
105
|
-
*/
|
|
106
|
-
function readNodeChunks(
|
|
107
|
-
stream: NodeJSReadableStreamEsque,
|
|
108
|
-
onChunk: (chunk: Uint8Array) => void,
|
|
109
|
-
) {
|
|
110
|
-
return new Promise<void>((resolve) => {
|
|
111
|
-
stream.on('data', onChunk);
|
|
112
|
-
stream.on('end', resolve);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Handle WebAPI stream
|
|
118
|
-
*/
|
|
119
|
-
async function readStandardChunks(
|
|
120
|
-
stream: WebReadableStreamEsque,
|
|
121
|
-
onChunk: (chunk: Uint8Array) => void,
|
|
122
|
-
) {
|
|
123
|
-
const reader = stream.getReader();
|
|
124
|
-
let readResult = await reader.read();
|
|
125
|
-
while (!readResult.done) {
|
|
126
|
-
onChunk(readResult.value);
|
|
127
|
-
readResult = await reader.read();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export const streamingJsonHttpRequester = (
|
|
132
|
-
opts: HTTPBaseRequestOptions & {
|
|
133
|
-
headers: () => HTTPHeaders | Promise<HTTPHeaders>;
|
|
134
|
-
textDecoder: TextDecoderEsque;
|
|
135
|
-
},
|
|
136
|
-
onSingle: (index: number, res: HTTPResult) => void,
|
|
137
|
-
) => {
|
|
138
|
-
const ac = opts.AbortController ? new opts.AbortController() : null;
|
|
139
|
-
const responsePromise = fetchHTTPResponse(
|
|
140
|
-
{
|
|
141
|
-
...opts,
|
|
142
|
-
contentTypeHeader: 'application/json',
|
|
143
|
-
batchModeHeader: 'stream',
|
|
144
|
-
getUrl,
|
|
145
|
-
getBody,
|
|
146
|
-
},
|
|
147
|
-
ac,
|
|
148
|
-
);
|
|
149
|
-
const cancel = () => ac?.abort();
|
|
150
|
-
const promise = responsePromise.then(async (res) => {
|
|
151
|
-
if (!res.body) throw new Error('Received response without body');
|
|
152
|
-
const meta: HTTPResult['meta'] = { response: res };
|
|
153
|
-
return parseJSONStream<HTTPResult>({
|
|
154
|
-
readableStream: res.body,
|
|
155
|
-
onSingle,
|
|
156
|
-
parse: (string) => ({
|
|
157
|
-
json: JSON.parse(string) as TRPCResponse,
|
|
158
|
-
meta,
|
|
159
|
-
}),
|
|
160
|
-
signal: ac?.signal,
|
|
161
|
-
textDecoder: opts.textDecoder,
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
return { cancel, promise };
|
|
166
|
-
};
|