forked from apostrophecms/oembetter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (188 loc) · 5.34 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
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
var oembed = require('./oembed.js');
var async = require('async');
var filters = require('./filters.js');
var urls = require('url');
module.exports = function(options) {
var self = {};
if (!options) {
options = {};
}
self.before = filters.before.concat(options.before || []);
self.after = filters.after.concat(options.after || []);
self.fallback = filters.fallback.concat(options.fallback || []);
self.fetch = function(url, options, callback) {
if (arguments.length === 2) {
callback = options;
options = {};
}
if (url.match(/^\/\//)) {
// Protocol-relative URLs are commonly found
// in markup these days and can be upgraded
// to https so that they work
url = 'https:' + url;
}
var response;
var warnings = [];
var parsed = urls.parse(url);
if (!parsed) {
return callback(new Error('oembetter: invalid URL: ' + url));
}
if ((parsed.protocol !== 'http:') && (parsed.protocol !== 'https:'))
{
return callback(new Error('oembetter: URL is neither http nor https: ' + url));
}
if (self._whitelist) {
var i;
var good = false;
for (i = 0; (i < self._whitelist.length); i++) {
if (self.inDomain(self._whitelist[i], parsed.hostname)) {
good = true;
break;
}
}
if (!good) {
return callback(new Error('oembetter: ' + url + ' is not in a whitelisted domain.'));
}
}
var endpoint = false;
if (self._endpoints) {
for (i = 0; i < self._endpoints.length; i++) {
if (self.inDomain(self._endpoints[i].domain, parsed.hostname)) {
endpoint = self._endpoints[i].endpoint;
break;
}
}
}
return async.series({
before: function(callback) {
return async.eachSeries(self.before, function(before, callback) {
return before(url, options, response, function(err, _url, _options, _response) {
// Nonfatal
if (err) {
warnings.push(err);
return callback(null);
}
url = _url || url;
options = _options || options;
response = _response || response;
return callback(null);
});
}, callback);
},
fetch: function(callback) {
if (response) {
// Preempted by a before
return callback(null);
}
return oembed(url, options, endpoint, function (err, result) {
response = result;
if (err) {
// not necessarily fatal
warnings.push(err);
}
return callback(null);
});
},
fallback: function(fallbackCallback) {
if (response) {
return setImmediate(fallbackCallback);
}
return async.eachSeries(self.fallback, function(fallback, callback) {
return fallback(url, options, function(err, _response) {
if (err) {
warnings.push(err);
return callback(err);
}
response = _response || response;
if (response) {
// Stop trying fallbacks, we got one
return fallbackCallback(null);
}
return callback(null);
});
}, fallbackCallback);
},
after: function(callback) {
if (!response) {
return setImmediate(callback);
}
return async.eachSeries(self.after, function(after, callback) {
return after(url, options, response, function(err, _response) {
if (err) {
warnings.push(err);
return callback(err);
}
response = _response || response;
return callback(null);
});
}, callback);
}
}, function(err) {
// Handle fatal errors
if (err) {
return callback(err);
}
// If there is no response, treat the first
// warning as a fatal error
if (!response) {
if (warnings.length) {
return callback(warnings[0], warnings);
}
}
// If there is a response, make the warnings available as the
// third argument
return callback(null, response, warnings);
});
};
self.addBefore = function(fn) {
self.before.push(fn);
};
self.addAfter = function(fn) {
self.after.push(fn);
};
self.addFallback = function(fn) {
self.fallback.push(fn);
};
self.inDomain = function(domain, hostname) {
hostname = hostname.toLowerCase();
domain = domain.toLowerCase();
if (hostname === domain) {
return true;
}
if (hostname.substr(-domain.length - 1) === ('.' + domain)) {
return true;
}
return false;
};
self.whitelist = function(_whitelist) {
self._whitelist = _whitelist;
};
self.suggestedWhitelist = [
'youtube.com',
'youtu.be',
'blip.tv',
'dailymotion.com',
'flickr.com',
'hulu.com',
'nfb.ca',
'qik.com',
'revision3.com',
'scribd.com',
'viddler.com',
'vimeo.com',
'youtube.com',
'dotsub.com',
'yfrog.com',
'photobucket.com',
'soundcloud.com',
'instagram.com',
'twitter.com'
];
self.suggestedEndpoints = [
{domain: 'instagram.com', endpoint: 'http://api.instagram.com/oembed' }
];
self.endpoints = function(_endpoints) {
self._endpoints = _endpoints;
};
return self;
};