-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
177 lines (134 loc) · 4.33 KB
/
index.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
'use strict';
const _ = require('lodash');
const Xml2js = require('xml2js');
const Request = require('request');
const Url = require('url');
const Aws4 = require('aws4');
const internals = {};
internals.formatTagName = function (name) {
// Remove 'aws:' prefix.
name = name.replace(/^aws:/, '');
// Make first char lower case.
return name.charAt(0).toLowerCase() + name.slice(1);
};
internals.walk = function (data) {
if (_.isArray(data)) {
if (data.length > 1) {
return _.map(data, (item) => {
return internals.walk(item);
});
}
else if (data.length === 1) {
return internals.walk(data[0]);
}
}
else if (_.isObject(data)) {
if (data._) {
return data._;
}
return _.reduce(data, (memo, v, k) => {
memo[internals.formatTagName(k)] = internals.walk(v);
return memo;
}, {});
}
else {
return data;
}
};
internals.parse = function (xml, req, cb) {
const action = req.Action;
const responseKey = 'aws:' + action + 'Response';
const actionResultKey = 'aws:' + action + 'Result';
const validateResponse = function (data, json) {
if (!_.isArray(data['aws:ResponseStatus'])) {
throw new Error('XML response missing aws:ResponseStatus!\n' + json);
}
if (!_.isArray(data['aws:ResponseStatus'][0]['aws:StatusCode'])) {
throw new Error('XML response missing aws:StatusCode!\n' + json);
}
const statusCode = data['aws:ResponseStatus'][0]['aws:StatusCode'][0];
if (statusCode !== 'Success') {
throw new Error(statusCode + '\n' + json);
}
if (!_.isArray(data[actionResultKey]) || !data[actionResultKey].length) {
throw new Error('XML response missing ' + actionResultKey + '!\n' + json);
}
data = data[actionResultKey][0]['aws:Alexa'][0];
return internals.walk(data);
};
Xml2js.parseString(xml, { mergeAttrs: true, trim: true }, (err, data) => {
if (err) {
return cb(err);
}
// json string representation of response data (for debugging).
const json = JSON.stringify(data, null, ' ');
// We need to know the request action in order to know the tag names.
if (data.Response && _.isArray(data.Response.Errors)) {
return cb(new Error(data.Response.Errors.reduce((memo, error) => {
if (memo) {
memo += '\n';
}
memo += error.Error[0].Code[0] + ': ' + error.Error[0].Message[0];
return memo;
}, '')));
}
if (!data[responseKey] || !_.isArray(data[responseKey]['aws:Response'])) {
return cb(new Error('XML response missing aws:Response!\n' + json));
}
// Discard top level nodes. We are only interested on whats inside
// `aws:Alexa`.
data = data[responseKey]['aws:Response'];
// Batch requests support
let all = [];
try {
for (let i = 0; i < data.length; ++i) {
// Require all responses to be valid
all.push(validateResponse(data[i], json));
}
}
catch (e) {
return cb(e);
}
// If it isn't a batch request, be backwards compatible (single response object)
if (all.length === 1) {
all = all[0];
}
cb(null, all);
});
};
module.exports = function (options) {
return function (req, cb) {
const region = options.region || 'us-west-1';
const host = (req.Action === 'TopSites') ? `ats.${region}.amazonaws.com` : `awis.${region}.amazonaws.com`;
const service = (req.Action === 'TopSites') ? 'AlexaTopSites' : 'awis';
const pathname = '/api';
const search = Object.keys(req).sort().reduce((memo, k) => {
if (memo) {
memo += '&';
}
// Manually replace single quotes with `%27` as `encodeURIComponent` doesnt
// seem to encode them, and things break:
// https://github.com/wrangr/awis/issues/3
const value = encodeURIComponent(req[k]).replace(/'/g, '%27');
return memo + encodeURIComponent(k) + '=' + value;
}, '');
const path = pathname + '?' + search;
const url = Url.format({ protocol: 'https:', host, pathname, search });
const signOpts = {
url,
host,
service,
path
};
const signRes = Aws4.sign(signOpts, {
accessKeyId: options.key,
secretAccessKey: options.secret
});
Request(signRes, (err, res) => {
if (err) {
return cb(err);
}
internals.parse(res.body, req, cb);
});
};
};