-
Notifications
You must be signed in to change notification settings - Fork 130
/
grpc-web-format.ts
313 lines (266 loc) · 10.4 KB
/
grpc-web-format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {base64decode, base64encode} from "@protobuf-ts/runtime";
import {RpcError, RpcMetadata} from "@protobuf-ts/runtime-rpc";
import {GrpcStatusCode} from "./goog-grpc-status-code";
/**
* Create fetch API headers for a grpc-web request.
*/
export function createGrpcWebRequestHeader(headers: Headers, format: GrpcWebFormat, deadline: Date | number | undefined, meta?: RpcMetadata, userAgent?: string): Headers {
// add meta as headers
if (meta) {
for (let [k, v] of Object.entries(meta)) {
if (typeof v == "string")
headers.append(k, v);
else
for (let i of v)
headers.append(k, i);
}
}
// set standard headers (possibly overwriting meta)
headers.set('Content-Type', format === "text" ? "application/grpc-web-text" : "application/grpc-web+proto");
headers.set('X-Grpc-Web', "1");
if (userAgent)
headers.set("X-User-Agent", userAgent);
// deadline
let timeout = typeof deadline == "number" ? deadline : deadline instanceof Date ? (deadline.getTime() - Date.now()) : 0;
if (timeout > 0) {
headers.set('grpc-timeout', timeout + 'm');
}
return headers;
}
/**
* Create a fetch API request body for a grpc-web request.
*
* Packs the serialized message into a data frame, and base64 encodes if
* format is "text".
*/
export function createGrpcWebRequestBody(message: Uint8Array, format: GrpcWebFormat): Uint8Array | string {
let body = new Uint8Array(5 + message.length); // we need 5 bytes for frame type + message length
body[0] = GrpcWebFrame.DATA; // first byte is frame type
// 4 bytes message length
for (let msgLen = message.length, i = 4; i > 0; i--) {
body[i] = (msgLen % 256);
msgLen >>>= 8;
}
body.set(message, 5); // reset is message
return format === "binary" ? body : base64encode(body);
}
/**
* Parses a grpc status (code and optional text) and meta data from response
* headers.
*
* If given a fetch response, checks for fetch-specific error information
* ("type" property) and whether the "body" is null and throws a RpcError.
*/
export function readGrpcWebResponseHeader(fetchResponse: Response): [GrpcStatusCode, string | undefined, RpcMetadata];
export function readGrpcWebResponseHeader(headers: HttpHeaders, httpStatus: number, httpStatusText: string): [GrpcStatusCode, string | undefined, RpcMetadata];
export function readGrpcWebResponseHeader(headersOrFetchResponse: HttpHeaders | Response, httpStatus?: number, httpStatusText?: string): [GrpcStatusCode, string | undefined, RpcMetadata] {
if (arguments.length === 1) {
let fetchResponse = headersOrFetchResponse as Response;
switch (fetchResponse.type) {
case "error":
case "opaque":
case "opaqueredirect":
// see https://developer.mozilla.org/en-US/docs/Web/API/Response/type
throw new RpcError(`fetch response type ${fetchResponse.type}`, GrpcStatusCode[GrpcStatusCode.UNKNOWN]);
}
if (!fetchResponse.body)
throw new RpcError('premature end of response', GrpcStatusCode[GrpcStatusCode.DATA_LOSS]);
return readGrpcWebResponseHeader(
fetchHeadersToHttp(fetchResponse.headers),
fetchResponse.status,
fetchResponse.statusText
);
}
let
headers = headersOrFetchResponse as HttpHeaders,
httpOk = httpStatus! >= 200 && httpStatus! < 300,
responseMeta = parseMetadataFromHttpHeaders(headers),
[statusCode, statusDetail] = parseStatusFromHttpHeaders(headers);
if (statusCode === GrpcStatusCode.OK && !httpOk) {
statusCode = grpcStatusCodeFromHttp(httpStatus!);
statusDetail = httpStatusText;
}
return [statusCode, statusDetail, responseMeta];
}
/**
* Parses a grpc status (code and optional text) and meta data from response
* trailers.
*
* Response trailers are expected as a byte array, but are actually just an
* ASCII string with HTTP headers. Just pass the data of a grpc-web trailer
* frame.
*/
export function readGrpcWebResponseTrailer(data: Uint8Array): [GrpcStatusCode, string | undefined, RpcMetadata] {
let
headers = parseTrailerToHttpHeaders(data),
[code, detail] = parseStatusFromHttpHeaders(headers),
meta = parseMetadataFromHttpHeaders(headers);
return [code, detail, meta];
}
/**
* A grpc-frame type. Can be used to determine type of frame emitted by
* `readGrpcWebResponseBody()`.
*/
export enum GrpcWebFrame { DATA = 0x00, TRAILER = 0x80 }
/**
* Parses a grpc-web response (unary or server streaming) from a fetch API
* stream.
*
* Emits grpc-web frames.
*
* The returned promise resolves when the response is complete.
*/
export async function readGrpcWebResponseBody(stream: ReadableStream<Uint8Array>, format: GrpcWebFormat, onFrame: (type: GrpcWebFrame, data: Uint8Array) => void): Promise<void> {
let streamReader = stream.getReader(),
base64queue = "",
byteQueue: Uint8Array = new Uint8Array(0);
while (true) {
let result = await streamReader.read();
if (result.value !== undefined) {
if (format === "text") {
// the statements below just decode base64 and append to `bytesUnread`
// add incoming base64 to queue
for (let i = 0; i < result.value.length; i++)
base64queue += String.fromCharCode(result.value[i]);
// if the base64 queue is not a multiple of 4,
// we have to wait for more data
let safeLen = base64queue.length - base64queue.length % 4;
if (safeLen === 0)
continue;
// decode safe chunk of base64 and add to byte queue
byteQueue = concatBytes(byteQueue, base64decode(base64queue.substring(0, safeLen)));
base64queue = base64queue.substring(safeLen);
} else {
byteQueue = concatBytes(byteQueue, result.value);
}
// read all fully available data frames
while (byteQueue.length >= 5 && byteQueue[0] === GrpcWebFrame.DATA) {
let msgLen = 0;
for (let i = 1; i < 5; i++)
msgLen = (msgLen << 8) + byteQueue[i];
if (byteQueue.length - 5 >= msgLen) {
// we have the entire message
onFrame(GrpcWebFrame.DATA, byteQueue.subarray(5, 5 + msgLen));
byteQueue = byteQueue.subarray(5 + msgLen)
} else
break; // wait for more data
}
}
// exit, but emit trailer if exists
if (result.done) {
if (byteQueue.length === 0)
break;
if (byteQueue[0] !== GrpcWebFrame.TRAILER || byteQueue.length < 5)
throw new RpcError("premature EOF", GrpcStatusCode[GrpcStatusCode.DATA_LOSS]);
onFrame(GrpcWebFrame.TRAILER, byteQueue.subarray(5));
break;
}
}
}
// internal
type GrpcWebFormat = "text" | "binary";
// internal short cut type for http headers
type HttpHeaders = { [key: string]: string | string[]; }
// internal
function concatBytes(a: Uint8Array, b: Uint8Array): Uint8Array {
let n = new Uint8Array(a.length + b.length);
n.set(a);
n.set(b, a.length);
return n;
}
// returns error code on parse failure, uses OK as default code
function parseStatusFromHttpHeaders(headers: HttpHeaders): [GrpcStatusCode, string | undefined] {
let code = GrpcStatusCode.OK,
message: string | undefined;
let m = headers['grpc-message'];
if (m !== undefined) {
if (Array.isArray(m))
return [GrpcStatusCode.INTERNAL, "invalid grpc-web message"];
message = m;
}
let s = headers['grpc-status'];
if (s !== undefined) {
if (Array.isArray(m) || GrpcStatusCode[code] === undefined)
return [GrpcStatusCode.INTERNAL, "invalid grpc-web status"];
code = parseInt(s as string);
}
return [code, message];
}
// skips grpc-web headers
function parseMetadataFromHttpHeaders(headers: HttpHeaders): RpcMetadata {
let meta: RpcMetadata = {};
for (let [k, v] of Object.entries(headers))
switch (k) {
case 'grpc-message':
case 'grpc-status':
case 'content-type':
break;
default:
meta[k] = v;
}
return meta;
}
// parse trailer data (ASCII) to our headers rep
function parseTrailerToHttpHeaders(trailerData: Uint8Array): HttpHeaders {
let headers: HttpHeaders = {};
for (let chunk of String.fromCharCode.apply(String, trailerData as unknown as number[]).trim().split("\r\n")) {
let [key, value] = chunk.split(":", 2);
key = key.trim();
value = value.trim();
let e = headers[key];
if (typeof e == "string")
headers[key] = [e, value];
else if (Array.isArray(e))
e.push(value);
else
headers[key] = value;
}
return headers;
}
// fetch API to our headers rep
function fetchHeadersToHttp(fetchHeaders: Headers): HttpHeaders {
let headers: HttpHeaders = {};
fetchHeaders.forEach((value, key) => {
let e = headers[key];
if (typeof e == "string")
headers[key] = [e, value];
else if (Array.isArray(e))
e.push(value);
else
headers[key] = value;
});
return headers;
}
// internal
function grpcStatusCodeFromHttp(httpStatus: number): GrpcStatusCode {
switch (httpStatus) {
case 200:
return GrpcStatusCode.OK;
case 400:
return GrpcStatusCode.INVALID_ARGUMENT;
case 401:
return GrpcStatusCode.UNAUTHENTICATED;
case 403:
return GrpcStatusCode.PERMISSION_DENIED;
case 404:
return GrpcStatusCode.NOT_FOUND;
case 409:
return GrpcStatusCode.ABORTED;
case 412:
return GrpcStatusCode.FAILED_PRECONDITION;
case 429:
return GrpcStatusCode.RESOURCE_EXHAUSTED;
case 499:
return GrpcStatusCode.CANCELLED;
case 500:
return GrpcStatusCode.UNKNOWN;
case 501:
return GrpcStatusCode.UNIMPLEMENTED;
case 503:
return GrpcStatusCode.UNAVAILABLE;
case 504:
return GrpcStatusCode.DEADLINE_EXCEEDED;
default:
return GrpcStatusCode.UNKNOWN;
}
}