-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
204 lines (167 loc) · 5.37 KB
/
server.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 url = require('url'),
fs = require('fs'),
http = require('http'),
bee = require('beeline'),
SC_CLIENT_ID = '1288146c708a6fa789f74748fe960337';
var makeChain = function (args, start) {
start = start || 0;
var chain = Array.prototype.slice.call(args, start + 2);
chain.unshift(args[start]);
return chain;
};
var chainCallbacks = function (args, start) {
start = start || 0;
var chain = makeChain(args, start);
var callback = args[start + 1].apply(this, chain);
return callback;
};
var errorHandler = function (err, response) {
console.log(err);
response.writeHead(500);
response.end();
};
var serveError = function (response) {
return function (err) {
errorHandler(err, response);
};
};
// creates a response handler that verifies the response prior to executing a callback
var handleResponse = function (response, callback, errorStatusCode, errorHeaders) {
var responseHandler = function (res) {
if (!res.headers.location) {
res.headers['Content-Length'] = 0;
errorStatusCode = errorStatusCode || res.statusCode;
errorHeaders = errorHeaders || res.headers;
response.writeHead(errorStatusCode, errorHeaders);
response.end();
} else {
callback && callback(res);
}
};
return responseHandler;
};
// resolves 'resource' from the SC API resolve endpoint, and passes the response to a callback
var scResolve = function (resource, response) {
var callback = chainCallbacks(arguments, 1);
var reqOptions = {
host: 'api.soundcloud.com',
port: 80,
path: '/resolve.json?client_id=' + SC_CLIENT_ID + '&url=http://soundcloud.com/' + resource,
method: 'GET',
headers: {
'User-Agent': 'AudioJedit'
}
};
var req = http.request(reqOptions, handleResponse(response, callback));
req.on('error', serveError(response));
req.end();
return req;
};
// creates a response handler which presumes the response is the track JSON, and requests the track itself from the stream_url
var requestTrack = function (response) {
var callback = chainCallbacks(arguments);
var responseHandler = function (track) {
track = JSON.parse(track);
if (!track.stream_url) {
response.writeHead(404, { 'Content-Type': 'application/octet-stream' });
response.end();
return;
}
var reqOptions = url.parse(track.stream_url);
reqOptions = {
host: reqOptions.host,
path: reqOptions.pathname + '?client_id=' + SC_CLIENT_ID,
headers: {
'User-Agent': 'AudioJedit'
}
};
var req = http.get(reqOptions, handleResponse(response, callback, 404, { 'Content-Type': 'application/octet-stream' }));
req.on('error', serveError(response));
return req;
};
return responseHandler;
};
// creates a response handler that writes to the response
var writeResponse = function (response) {
var responseHandler = function (res) {
response.writeHead(res.statusCode, res.headers);
res.on('data', function (chunk) {
response.write(chunk);
});
res.on('end', function () {
response.end();
});
};
return responseHandler;
};
// creates a response handler that makes a subsequent request with options based on the response.headers.location
var makeRequest = function (response) {
var callback = chainCallbacks(arguments);
var responseHandler = function (res) {
var reqOptions = url.parse(res.headers.location);
reqOptions = {
host: reqOptions.host,
port: 80,
path: reqOptions.pathname + reqOptions.search,
headers: {
'User-Agent': 'AudioJedit'
}
};
var req = http.get(reqOptions, callback);
req.on('error', serveError(response));
return req;
};
return responseHandler;
};
// creates a response handler that gets chunked data, then passes that [buffered] data to a callback
var getChunks = function (response) {
var callback = chainCallbacks(arguments);
var responseHandler = function (res) {
res.setEncoding('utf-8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
callback(data);
});
};
return responseHandler;
};
var serveIndex = function (response) {
return fs.readFile('./public/index.html', function (err, data) {
if (err) {
console.log(err);
} else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(data);
}
});
};
var router = bee.route({
'r`^/(.*)`': bee.staticDir('./public/', {
'.html': 'text/html',
'.css': 'text/css',
'.gif': 'image/gif',
'.png': 'image/png',
'.ico': 'image/png',
'.js': 'text/javascript'
}),
'r`^/(index(\\.html?)?)?(\\?.*)?$`': function (req, response, matches) {
return serveIndex(response);
},
'r`^/([\\w-_]+)/([\\w-_]+)/audio`': function (req, response, matches) {
var resource = matches.join('/');
return scResolve(resource, response, makeRequest, getChunks, requestTrack, makeRequest, writeResponse);
},
'r`^/([\\w-_]+)/([\\w-_]+)(\\.\\w+)?`': function (req, response, matches) {
var format = matches.filter(Boolean).length == 3 ? matches.pop().substring(1) : 'html';
var resource = matches.join('/');
// only json is permitted, else serve index
if (format != 'json') {
return serveIndex(response);
}
return scResolve(resource, response, makeRequest, writeResponse);
}
});
http.createServer(router).listen(process.env.PORT || 8181);