112 lines
5.1 KiB
JavaScript
112 lines
5.1 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.FetchRPC = exports.wrapErrorResponseToTwirpError = exports.NodeHttpRPC = void 0;
|
|
const http = __importStar(require("http"));
|
|
const https = __importStar(require("https"));
|
|
const url_1 = require("url");
|
|
const errors_1 = require("./errors");
|
|
/**
|
|
* a node HTTP RPC implementation
|
|
* @param options
|
|
* @constructor
|
|
*/
|
|
const NodeHttpRPC = (options) => ({
|
|
request(service, method, contentType, data) {
|
|
let client;
|
|
return new Promise((resolve, rejected) => {
|
|
const responseChunks = [];
|
|
const requestData = contentType === "application/protobuf"
|
|
? Buffer.from(data)
|
|
: JSON.stringify(data);
|
|
const url = new url_1.URL(options.baseUrl);
|
|
const isHttps = url.protocol === "https:";
|
|
if (isHttps) {
|
|
client = https;
|
|
}
|
|
else {
|
|
client = http;
|
|
}
|
|
const prefix = url.pathname !== "/" ? url.pathname : "";
|
|
const req = client
|
|
.request(Object.assign(Object.assign({}, (options ? options : {})), { method: "POST", protocol: url.protocol, host: url.hostname, port: url.port ? url.port : isHttps ? 443 : 80, path: `${prefix}/${service}/${method}`, headers: Object.assign(Object.assign({}, (options.headers ? options.headers : {})), { "Content-Type": contentType, "Content-Length": contentType === "application/protobuf"
|
|
? Buffer.byteLength(requestData)
|
|
: Buffer.from(requestData).byteLength }) }), (res) => {
|
|
res.on("data", (chunk) => responseChunks.push(chunk));
|
|
res.on("end", () => {
|
|
const data = Buffer.concat(responseChunks);
|
|
if (res.statusCode != 200) {
|
|
rejected(wrapErrorResponseToTwirpError(data.toString()));
|
|
}
|
|
else {
|
|
if (contentType === "application/json") {
|
|
resolve(JSON.parse(data.toString()));
|
|
}
|
|
else {
|
|
resolve(data);
|
|
}
|
|
}
|
|
});
|
|
res.on("error", (err) => {
|
|
rejected(err);
|
|
});
|
|
})
|
|
.on("error", (err) => {
|
|
rejected(err);
|
|
});
|
|
req.end(requestData);
|
|
});
|
|
},
|
|
});
|
|
exports.NodeHttpRPC = NodeHttpRPC;
|
|
function wrapErrorResponseToTwirpError(errorResponse) {
|
|
return errors_1.TwirpError.fromObject(JSON.parse(errorResponse));
|
|
}
|
|
exports.wrapErrorResponseToTwirpError = wrapErrorResponseToTwirpError;
|
|
/**
|
|
* a browser fetch RPC implementation
|
|
*/
|
|
const FetchRPC = (options) => ({
|
|
request(service, method, contentType, data) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const headers = new Headers(options.headers);
|
|
headers.set("content-type", contentType);
|
|
const response = yield fetch(`${options.baseUrl}/${service}/${method}`, Object.assign(Object.assign({}, options), { method: "POST", headers, body: data instanceof Uint8Array ? data : JSON.stringify(data) }));
|
|
if (response.status === 200) {
|
|
if (contentType === "application/json") {
|
|
return yield response.json();
|
|
}
|
|
return new Uint8Array(yield response.arrayBuffer());
|
|
}
|
|
throw errors_1.TwirpError.fromObject(yield response.json());
|
|
});
|
|
},
|
|
});
|
|
exports.FetchRPC = FetchRPC;
|