-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathzfactory.js
206 lines (169 loc) · 4.93 KB
/
zfactory.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
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
const crypto = require('crypto');
const _ = require('lodash');
const request = require('request');
const { createSyncFn } = require('synckit');
// So `zapier build` doesn't forget to include request-worker.js
require('./request-worker');
// Converts WB `bundle.request` format to something `request` can use
const convertBundleRequest = (bundleOrBundleRequest) => {
bundleOrBundleRequest = _.extend({}, bundleOrBundleRequest);
// LEGACY: allow for the whole bundle to mistakingly be sent over
const bundleRequest = bundleOrBundleRequest.request
? bundleOrBundleRequest.request
: bundleOrBundleRequest;
if (
bundleRequest.auth &&
Array.isArray(bundleRequest.auth) &&
bundleRequest.auth.length === 2
) {
bundleRequest.auth = {
user: bundleRequest.auth[0],
password: bundleRequest.auth[1],
};
}
if (!bundleRequest.qs && bundleRequest.params) {
bundleRequest.qs = bundleRequest.params;
}
if (!bundleRequest.body && bundleRequest.data) {
bundleRequest.body = bundleRequest.data;
}
delete bundleRequest.params;
delete bundleRequest.data;
return bundleRequest;
};
const parseBody = (body) => {
if (body) {
if (typeof body === 'string' || body.writeInt32BE) {
return String(body);
}
return body;
}
return null;
};
// Converts `request`'s response into a simplified object
const convertResponse = (response) => {
if (response) {
return {
status_code: response.statusCode,
headers: _.extend({}, response.headers),
content: parseBody(response.body),
};
}
return {};
};
const syncRequest = createSyncFn(require.resolve('./request-worker'));
const zfactory = (zcli, app, logger) => {
const AWS = () => {
// Direct require breaks the build as the module isn't found by browserify
const moduleName = 'aws-sdk';
return require(moduleName);
};
const jsonParse = (str) => {
try {
return JSON.parse(str);
} catch (err) {
let preview = str;
if (str && str.length > 100) {
preview = str.substr(0, 100);
}
throw new Error(`Error parsing response. We got: "${preview}"`);
}
};
const jsonStringify = (obj) => {
try {
return JSON.stringify(obj);
} catch (err) {
throw new Error(err.message);
}
};
const sendHttpLog = (req, res) => {
// Log fields here intend to match the ones in createHttpPatch in core
const method = (req.method || 'GET').toUpperCase();
const url = req.url || req.uri;
const responseBody =
typeof res.content === 'string'
? res.content
: 'Could not show response content';
logger(`${res.status_code} ${method} ${url}`, {
log_type: 'http',
request_type: 'devplatform-outbound',
request_url: url,
request_method: method,
request_headers: req.headers,
request_data: req.data,
request_via_client: false,
response_status_code: res.status_code,
response_headers: res.headers,
response_content: responseBody,
});
};
const requestMethod = (bundleRequest, callback) => {
const options = convertBundleRequest(bundleRequest);
if (_.isFunction(callback)) {
return request(options, (err, response) =>
callback(err, convertResponse(response)),
);
}
const normalizedOptions = request.initParams(options);
const response = syncRequest(normalizedOptions);
const convertedResponse = convertResponse(response);
// syncRequest() is done by a worker thread, which isn't httpPatch'ed, so we
// need to explicit write the http log here
if (logger) {
sendHttpLog(normalizedOptions, convertedResponse);
}
return convertedResponse;
};
const hash = (
algorithm,
string,
encoding = 'hex',
inputEncoding = 'binary',
) => {
const hasher = crypto.createHash(algorithm);
hasher.update(string, inputEncoding);
return hasher.digest(encoding);
};
const hmac = (algorithm, key, string, encoding = 'hex') => {
const hasher = crypto.createHmac(algorithm, key);
hasher.update(string);
return hasher.digest(encoding);
};
const snipify = (string) => {
const SALT = process.env.SECRET_SALT || 'doesntmatterreally';
if (!_.isString(string)) {
return null;
}
const length = string.length;
string += SALT;
const result = hash('sha256', string);
return `:censored:${length}:${result.substr(0, 10)}:`;
};
const dehydrate = (method, bundle) => {
return zcli.dehydrate(app.hydrators.legacyMethodHydrator, {
method,
bundle,
});
};
const dehydrateFile = (url, requestOptions, meta) => {
return zcli.dehydrateFile(app.hydrators.legacyFileHydrator, {
url,
request: requestOptions,
meta,
});
};
return {
AWS,
JSON: {
parse: jsonParse,
stringify: jsonStringify,
},
request: requestMethod,
hash,
hmac,
snipify,
dehydrate,
dehydrateFile,
};
};
module.exports = zfactory;