1 | var Bluebird = require('bluebird');
|
2 | var Decode = require('jwt-decode');
|
3 | var Path = require('path');
|
4 | var Superagent = require('superagent');
|
5 | var Url = require('url');
|
6 |
|
7 | var defaults = require('lodash.defaults');
|
8 | var defaultsDeep = require('lodash.defaultsdeep');
|
9 | var forEach = require('lodash.foreach');
|
10 | var getFrom = require('lodash.get');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function Webtask (sandbox, token, options) {
|
18 | if (!options) options = {};
|
19 |
|
20 | if (sandbox.securityVersion === 'v1') {
|
21 | try {
|
22 | |
23 |
|
24 |
|
25 | this.claims = Decode(token);
|
26 |
|
27 | |
28 |
|
29 |
|
30 | this.token = token;
|
31 | }
|
32 | catch (_) {
|
33 | throw new Error('token must be a valid JWT');
|
34 | }
|
35 | }
|
36 |
|
37 | if (sandbox.securityVersion === 'v2') {
|
38 | if (typeof options.name !== 'string') {
|
39 | throw new Error('name must be a valid string');
|
40 | }
|
41 |
|
42 | this.claims = {
|
43 | jtn: options.name,
|
44 | ten: options.container || sandbox.container,
|
45 | }
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 | this.sandbox = sandbox;
|
52 |
|
53 | |
54 |
|
55 |
|
56 | this.meta = options.meta || {};
|
57 |
|
58 | |
59 |
|
60 |
|
61 | this.secrets = options.secrets;
|
62 |
|
63 | |
64 |
|
65 |
|
66 | this.code = options.code;
|
67 |
|
68 | |
69 |
|
70 |
|
71 | Object.defineProperty(this, 'container', {
|
72 | enumerable: true,
|
73 | get: function () {
|
74 | return options.container || this.sandbox.container;
|
75 | }
|
76 | });
|
77 |
|
78 | |
79 |
|
80 |
|
81 | Object.defineProperty(this, 'url', {
|
82 | enumerable: true,
|
83 | get: function () {
|
84 | var url = options.webtask_url;
|
85 | if (!url) {
|
86 | if (this.claims.host) {
|
87 | var surl = Url.parse(this.sandbox.url);
|
88 | url = surl.protocol + '//' + this.claims.host + (surl.port ? (':' + surl.port) : '') + '/' + this.sandbox.container;
|
89 | }
|
90 | else {
|
91 | url = this.sandbox.url + '/api/run/' + this.sandbox.container;
|
92 | }
|
93 |
|
94 | if (this.claims.jtn) url += '/' + this.claims.jtn;
|
95 | else url += '?key=' + this.token;
|
96 | }
|
97 |
|
98 | return url;
|
99 | }
|
100 | });
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | Webtask.prototype.createLogStream = function (options) {
|
113 | return this.sandbox.createLogStream(options);
|
114 | };
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | Webtask.prototype.run = function (options, cb) {
|
124 | var methodMap = {
|
125 | 'delete': 'del',
|
126 | };
|
127 | var urlData = Url.parse(this.url, true);
|
128 | var config = defaultsDeep(options || {}, {
|
129 | method: 'get',
|
130 | path: '',
|
131 | query: {},
|
132 | });
|
133 | var method = getFrom(methodMap, config.method, config.method);
|
134 | var query = defaults(urlData.query, config.query);
|
135 |
|
136 | urlData.pathname = Path.join(urlData.pathname, config.path);
|
137 | urlData.query = {};
|
138 | urlData.search = null;
|
139 |
|
140 | var url = Url.format(urlData);
|
141 | var request = Superagent[method](url)
|
142 | .query(query);
|
143 |
|
144 | if (config.body) request = request.send(config.body);
|
145 |
|
146 | forEach(config.headers, function (value, header) {
|
147 | request = request.set(header, value);
|
148 | });
|
149 |
|
150 | var promise = Bluebird.resolve(request)
|
151 | .catch(function (err) {
|
152 | if (err.response) return err.response;
|
153 |
|
154 | throw err;
|
155 | })
|
156 | .then(function (res) {
|
157 | res.statusCode = res.status;
|
158 |
|
159 | return res;
|
160 | });
|
161 |
|
162 | return cb ? promise.nodeify(cb, {spread: true}) : promise;
|
163 | };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | Webtask.prototype.createCronJob = function (options, cb) {
|
176 | if (this.token) {
|
177 | var parsedUrl = Url.parse(this.claims.url);
|
178 | var filename = parsedUrl.pathname
|
179 | ? Path.basename(parsedUrl.pathname)
|
180 | : '';
|
181 |
|
182 | options = defaultsDeep(options, {
|
183 | name: this.claims.jtn || filename,
|
184 | state: 'active',
|
185 | meta: this.meta,
|
186 | });
|
187 | }
|
188 | else {
|
189 | if (options.name && options.name !== this.claims.jtn) {
|
190 | throw new Error('Server does not support specifying CRON name that is different from webtask name.');
|
191 | }
|
192 | options = defaultsDeep(options, {
|
193 | name: this.claims.jtn,
|
194 | state: 'active',
|
195 | meta: this.meta,
|
196 | });
|
197 | }
|
198 |
|
199 | if (!options.name) throw new Error('Cron jobs must have a name.');
|
200 |
|
201 | var promise = this.sandbox.createCronJob({
|
202 | name: options.name,
|
203 | token: this.token,
|
204 | schedule: options.schedule,
|
205 | state: options.state,
|
206 | tz: options.tz,
|
207 | meta: options.meta
|
208 | });
|
209 |
|
210 | return cb ? promise.nodeify(cb, {spread: true}) : promise;
|
211 | };
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | Webtask.prototype.inspect = function (options, cb) {
|
224 | if (this.sandbox.securityVersion === 'v2') {
|
225 | throw new Error('The Webtask.inspect function is not supported in the current configuration. Consider using Sandbox.inspectWebtask instead.')
|
226 | }
|
227 |
|
228 | options.token = this.token;
|
229 |
|
230 | var promise = this.sandbox.inspectToken(options);
|
231 |
|
232 | return cb ? promise.nodeify(cb) : promise;
|
233 | };
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | Webtask.prototype.remove = function (cb) {
|
242 | var promise;
|
243 |
|
244 | if (!this.claims.jtn) {
|
245 | var err = new Error('Unnamed webtasks cannot be removed');
|
246 | err.statusCode = 400;
|
247 |
|
248 | promise = Bluebird.reject(err);
|
249 |
|
250 | return cb ? promise.nodeify(cb) : promise;
|
251 | } else {
|
252 | promise = this.sandbox.removeWebtask({
|
253 | name: this.claims.jtn,
|
254 | });
|
255 | }
|
256 |
|
257 | return cb ? promise.nodeify(cb) : promise;
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | Webtask.prototype.revoke = function (cb) {
|
267 | var promise = this.sandbox.revokeToken(this.token);
|
268 |
|
269 | return cb ? promise.nodeify(cb) : promise;
|
270 | };
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | Webtask.prototype.update = function (options, cb) {
|
280 | if (typeof options === 'function') {
|
281 | cb = options;
|
282 | options = {};
|
283 | }
|
284 |
|
285 | if (!options) {
|
286 | options = {};
|
287 | }
|
288 |
|
289 | options.name = this.claims.jtn;
|
290 |
|
291 | var promise = this.sandbox.updateWebtask(options);
|
292 |
|
293 | return cb ? promise.nodeify(cb) : promise;
|
294 | };
|
295 |
|
296 |
|
297 | Webtask.prototype.toJSON = function () {
|
298 | var data = {
|
299 | container: this.container,
|
300 | token: this.token,
|
301 | url: this.url,
|
302 | };
|
303 |
|
304 | if (this.claims.jtn) data.name = this.claims.jtn;
|
305 |
|
306 | return data;
|
307 | };
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | Webtask.prototype.updateStorage = function (storage, options, cb) {
|
322 | var promise = this.sandbox.updateStorage(storage, options);
|
323 |
|
324 | return cb ? promise.nodeify(cb) : promise;
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | Webtask.prototype.getStorage = function (options, cb) {
|
337 | var promise = this.sandbox.getStorage(options);
|
338 |
|
339 | return cb ? promise.nodeify(cb) : promise;
|
340 | }
|
341 |
|
342 | module.exports = Webtask;
|