UNPKG

6.3 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.HttpExecutor = exports.HttpMethod = exports.HttpServiceType = void 0;
30/* eslint jsdoc/require-jsdoc: off */
31const binding_1 = __importDefault(require("./binding"));
32const bindingutilities_1 = require("./bindingutilities");
33const errorcontexts_1 = require("./errorcontexts");
34const events = __importStar(require("events"));
35/**
36 * @internal
37 */
38var HttpServiceType;
39(function (HttpServiceType) {
40 HttpServiceType["Management"] = "MGMT";
41 HttpServiceType["Views"] = "VIEW";
42 HttpServiceType["Query"] = "QUERY";
43 HttpServiceType["Search"] = "SEARCH";
44 HttpServiceType["Analytics"] = "ANALYTICS";
45 HttpServiceType["Eventing"] = "EVENTING";
46})(HttpServiceType || (exports.HttpServiceType = HttpServiceType = {}));
47/**
48 * @internal
49 */
50var HttpMethod;
51(function (HttpMethod) {
52 HttpMethod["Get"] = "GET";
53 HttpMethod["Post"] = "POST";
54 HttpMethod["Put"] = "PUT";
55 HttpMethod["Delete"] = "DELETE";
56})(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
57/**
58 * @internal
59 */
60class HttpExecutor {
61 /**
62 * @internal
63 */
64 constructor(conn) {
65 this._conn = conn;
66 }
67 /**
68 * @internal
69 */
70 streamRequest(options) {
71 const emitter = new events.EventEmitter();
72 let cppHttpType;
73 if (options.type === HttpServiceType.Management) {
74 cppHttpType = binding_1.default.service_type.management;
75 }
76 else if (options.type === HttpServiceType.Views) {
77 cppHttpType = binding_1.default.service_type.view;
78 }
79 else if (options.type === HttpServiceType.Query) {
80 cppHttpType = binding_1.default.service_type.query;
81 }
82 else if (options.type === HttpServiceType.Search) {
83 cppHttpType = binding_1.default.service_type.search;
84 }
85 else if (options.type === HttpServiceType.Analytics) {
86 cppHttpType = binding_1.default.service_type.analytics;
87 }
88 else if (options.type === HttpServiceType.Eventing) {
89 cppHttpType = binding_1.default.service_type.eventing;
90 }
91 else {
92 throw new Error('unexpected http request type');
93 }
94 let cppHttpMethod;
95 if (options.method === HttpMethod.Get) {
96 cppHttpMethod = 'GET';
97 }
98 else if (options.method === HttpMethod.Post) {
99 cppHttpMethod = 'POST';
100 }
101 else if (options.method === HttpMethod.Put) {
102 cppHttpMethod = 'PUT';
103 }
104 else if (options.method === HttpMethod.Delete) {
105 cppHttpMethod = 'DELETE';
106 }
107 else {
108 throw new Error('unexpected http request method');
109 }
110 const headers = {};
111 if (options.contentType) {
112 headers['Content-Type'] = options.contentType;
113 }
114 let body = '';
115 if (!options.body) {
116 // empty body is acceptable
117 }
118 else if (options.body instanceof Buffer) {
119 body = options.body.toString();
120 }
121 else if (typeof options.body === 'string') {
122 body = options.body;
123 }
124 else {
125 throw new Error('unexpected http body type');
126 }
127 this._conn.managementFreeform({
128 type: cppHttpType,
129 method: cppHttpMethod,
130 path: options.path,
131 headers: headers,
132 body: body,
133 timeout: options.timeout,
134 }, (cppErr, res) => {
135 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
136 if (err) {
137 emitter.emit('error', err);
138 return;
139 }
140 emitter.emit('meta', {
141 statusCode: res.status,
142 headers: res.headers,
143 });
144 emitter.emit('data', Buffer.from(res.body));
145 emitter.emit('end');
146 });
147 return emitter;
148 }
149 async request(options) {
150 return new Promise((resolve, reject) => {
151 const emitter = this.streamRequest(options);
152 emitter.on('error', (err) => {
153 reject(err);
154 });
155 let dataCache = Buffer.allocUnsafe(0);
156 emitter.on('data', (data) => {
157 dataCache = Buffer.concat([dataCache, data]);
158 });
159 let metaCache = null;
160 emitter.on('meta', (meta) => {
161 metaCache = meta;
162 });
163 emitter.on('end', () => {
164 resolve({
165 requestOptions: options,
166 statusCode: metaCache.statusCode,
167 headers: metaCache.headers,
168 body: dataCache,
169 });
170 });
171 });
172 }
173 static errorContextFromResponse(resp) {
174 return new errorcontexts_1.HttpErrorContext({
175 method: resp.requestOptions.method,
176 request_path: resp.requestOptions.path,
177 response_code: resp.statusCode,
178 response_body: resp.body.toString(),
179 });
180 }
181}
182exports.HttpExecutor = HttpExecutor;