UNPKG

2.27 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2021 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.decodeResponse = exports.encodeRequest = void 0;
19// proto-over-HTTP request encoding and decoding
20const fallback_1 = require("./fallback");
21const googleError_1 = require("./googleError");
22function encodeRequest(rpc, protocol, servicePath, servicePort, request) {
23 const protoNamespaces = [];
24 let currNamespace = rpc.parent;
25 while (currNamespace.name !== '') {
26 protoNamespaces.unshift(currNamespace.name);
27 currNamespace = currNamespace.parent;
28 }
29 const protoServiceName = protoNamespaces.join('.');
30 const rpcName = rpc.name;
31 const headers = {
32 'Content-Type': 'application/x-protobuf',
33 };
34 const method = 'post';
35 const requestMessage = rpc.resolvedRequestType.fromObject(request);
36 const body = rpc.resolvedRequestType.encode(requestMessage).finish();
37 const url = `${protocol}://${servicePath}:${servicePort}/$rpc/${protoServiceName}/${rpcName}`;
38 return {
39 method,
40 url,
41 headers,
42 body,
43 };
44}
45exports.encodeRequest = encodeRequest;
46function decodeResponse(rpc, ok, response) {
47 if (!ok) {
48 const statusDecoder = new googleError_1.GoogleErrorDecoder();
49 const error = statusDecoder.decodeErrorFromBuffer(response);
50 throw error;
51 }
52 const buffer = response instanceof ArrayBuffer ? new Uint8Array(response) : response;
53 const message = rpc.resolvedResponseType.decode(buffer);
54 return rpc.resolvedResponseType.toObject(message, fallback_1.defaultToObjectOptions);
55}
56exports.decodeResponse = decodeResponse;
57//# sourceMappingURL=fallbackProto.js.map
\No newline at end of file