This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathas-http.js
112 lines (97 loc) · 3.46 KB
/
as-http.js
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
#!/usr/bin/env node
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var PassThrough = require('readable-stream').PassThrough;
var TChannelAsHTTP = require('tchannel/as/http');
var Transform = require('stream').Transform;
var util = require('util');
module.exports = TCurlAsHttp;
function TCurlAsHttp(options) {
if (!(this instanceof TCurlAsHttp)) {
return new TCurlAsHttp(options);
}
var self = this;
self.remoteHostPort = options.remoteHostPort;
self.serviceName = options.serviceName;
self.logger = options.logger;
self.channel = options.channel;
self.subChannel = options.subChannel;
self.asHttpClient = TChannelAsHTTP();
self.method = options.method;
self.headers = options.headers;
self.endpoint = options.endpoint;
self.body = options.body;
self.done = options.done;
self.delegate = options.delegate;
}
TCurlAsHttp.prototype.send = function send() {
var self = this;
var hreq = PassThrough();
hreq.end(self.body);
hreq.url = self.endpoint;
hreq.method = self.method;
hreq.headers = self.headers;
var hres = new HTTPResponse();
self.asHttpClient.forwardToTChannel(self.subChannel, hreq, hres, {streamed: true}, onComplete);
function onComplete(err) {
if (err) {
self.done();
self.logger.error(err);
return self.logger.exit();
}
self.done();
self.logger.log(hres.statusCode);
self.logger.log(hres.body);
self.logger.exit();
}
};
function HTTPResponse() {
var self = this;
Transform.call(self);
self.statusCode = 200;
self.body = '';
self.headers = {};
}
util.inherits(HTTPResponse, Transform);
HTTPResponse.prototype._transform = function _transform(chunk, encoding, next) {
var self = this;
self.push(chunk);
self.body += chunk;
next();
};
HTTPResponse.prototype.setHeader = function setHeader(name, value) {
var self = this;
self.headers[name.toLowerCase()] = value;
};
HTTPResponse.prototype.writeHead = function writeHead(statusCode, message, headers) {
var self = this;
if (headers === undefined && typeof message === 'object') {
headers = message;
}
self.statusCode = statusCode;
if (headers) {
for (var name in headers) {
if (headers.hasOwnProperty(name)) {
self.setHeader(name, headers[name]);
}
}
}
};