-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
218 lines (172 loc) · 5.73 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
exports.auth0 = require('./auth0/auth0');
exports.fromConnect = exports.fromExpress = fromConnect;
exports.fromHapi = fromHapi;
exports.fromServer = exports.fromRestify = fromServer;
/*
var app = new (require('express'))();
app.get('/', function (req, res) {
res.send('Hello, world');
});
module.exports = app;
*/
exports.express = function (options, cb) {
options.nodejsCompiler(options.script, function (error, func) {
if (error) return cb(error);
try {
func = fromConnect(func);
}
catch (e) {
return cb(e);
}
return cb(null, func);
});
};
// File rendering
exports.html = function (options, cb) {
render(options, cb, (script)=>script);
};
exports.pug = function (options, cb) {
var pug = require('pug');
render(options, cb, (script)=>pug.render(script));
};
exports.jade = exports.pug;
function render(options, cb, render) {
cb(null, (ctx, req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
var html = render(options.script);
res.end(html);
});
}
/*
async (dynamic context) => {
return "Hello, world!";
}
*/
exports.cs = function (options, cb) {
cb(null, require('edge').func(options.script));
};
const SANITIZE_RX = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
// API functions
function addAuth0(func) {
func.auth0 = function (options) {
return exports.auth0(func, options);
}
return func;
}
function fromConnect (connectFn) {
return addAuth0(function (context, req, res) {
var normalizeRouteRx = createRouteNormalizationRx(req.x_wt);
req.originalUrl = req.url;
req.url = req.url.replace(normalizeRouteRx, '/');
req.webtaskContext = attachStorageHelpers(context);
return connectFn(req, res);
});
}
function fromHapi(server) {
var webtaskContext;
server.ext('onRequest', function (request, response) {
var normalizeRouteRx = createRouteNormalizationRx(request.x_wt.jtn);
request.setUrl(request.url.replace(normalizeRouteRx, '/'));
request.webtaskContext = webtaskContext;
});
return addAuth0(function (context, req, res) {
var dispatchFn = server._dispatch();
webtaskContext = attachStorageHelpers(context);
dispatchFn(req, res);
});
}
function fromServer(httpServer) {
return addAuth0(function (context, req, res) {
var normalizeRouteRx = createRouteNormalizationRx(req.x_wt);
req.originalUrl = req.url;
req.url = req.url.replace(normalizeRouteRx, '/');
req.webtaskContext = attachStorageHelpers(context);
return httpServer.emit('request', req, res);
});
}
// Helper functions
const USE_WILDCARD_DOMAIN = 3;
const USE_CUSTOM_DOMAIN = 2;
const USE_SHARED_DOMAIN = 1;
function createRouteNormalizationRx(claims) {
var container = claims.container.replace(SANITIZE_RX, '\\$&');
var name = claims.jtn
? claims.jtn.replace(SANITIZE_RX, '\\$&')
: '';
if (claims.url_format === USE_SHARED_DOMAIN) {
return new RegExp(`^\/api/run/${container}/(?:${name}\/?)?`);
}
else if (claims.url_format === USE_CUSTOM_DOMAIN) {
return new RegExp(`^\/${container}/(?:${name}\/?)?`);
}
else if (claims.url_format === USE_WILDCARD_DOMAIN) {
return new RegExp(`^\/(?:${name}\/?)?`);
}
else {
throw new Error('Unsupported webtask URL format.');
}
}
function attachStorageHelpers(context) {
context.read = context.secrets.EXT_STORAGE_URL
? readFromPath
: readNotAvailable;
context.write = context.secrets.EXT_STORAGE_URL
? writeToPath
: writeNotAvailable;
return context;
function readNotAvailable(path, options, cb) {
var Boom = require('boom');
if (typeof options === 'function') {
cb = options;
options = {};
}
cb(Boom.preconditionFailed('Storage is not available in this context'));
}
function readFromPath(path, options, cb) {
var Boom = require('boom');
var Request = require('request');
if (typeof options === 'function') {
cb = options;
options = {};
}
Request({
uri: context.secrets.EXT_STORAGE_URL,
method: 'GET',
headers: options.headers || {},
qs: { path: path },
json: true,
}, function (err, res, body) {
if (err) return cb(Boom.boomify(err, { statusCode: 502 }));
if (res.statusCode === 404 && Object.hasOwnProperty.call(options, 'defaultValue')) return cb(null, options.defaultValue);
if (res.statusCode >= 400) return cb(new Boom(body && body.message, { statusCode: res.statusCode }));
cb(null, body);
});
}
function writeNotAvailable(path, data, options, cb) {
var Boom = require('boom');
if (typeof options === 'function') {
cb = options;
options = {};
}
cb(Boom.preconditionFailed('Storage is not available in this context'));
}
function writeToPath(path, data, options, cb) {
var Boom = require('boom');
var Request = require('request');
if (typeof options === 'function') {
cb = options;
options = {};
}
Request({
uri: context.secrets.EXT_STORAGE_URL,
method: 'PUT',
headers: options.headers || {},
qs: { path: path },
body: data,
}, function (err, res, body) {
if (err) return cb(Boom.boomify(err, { statusCode: 502 }));
if (res.statusCode >= 400) return cb(new Boom(body && body.message, { statusCode: res.statusCode }));
cb(null);
});
}
}