generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
diawi.js
109 lines (91 loc) · 2.84 KB
/
diawi.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
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const util = require('util');
const EventEmitter = require('events').EventEmitter;
const UPLOAD_URL = 'https://upload.diawi.com/';
const STATUS_URL = 'https://upload.diawi.com/status';
const POLL_MAX_COUNT = 10;
const POLL_INTERVAL = 2;
const sleep = (seconds) => {
return new Promise((resolve) => {
setTimeout(resolve, (seconds * 1000));
});
};
const Diawi = function (parameters) {
if (!parameters) {
parameters = {};
}
this.token = parameters.token.trim();
this.path = parameters.path.trim();
if (!fs.existsSync(this.path)) {
throw (new Error('Could not find file at ' + this.path));
}
// Create the required form fields
this.formData = {
token: this.token,
file: fs.createReadStream(this.path),
};
// Append the optional parameters to the formData
['password', 'comment', 'callback_emails', 'wall_of_apps', 'find_by_udid', 'installation_notifications']
.forEach((key) => {
if (parameters[key]) {
this.formData[key] = parameters[key];
}
});
};
Diawi.prototype.execute = async function () {
try {
const data = new FormData();
for ( var key in this.formData ) {
data.append(key, this.formData[key]);
}
const config = {
headers: data.getHeaders() /* { 'Content-Type': 'multipart/form-data' } */,
maxContentLength: Infinity,
maxBodyLength: Infinity
};
const response = await axios.post(UPLOAD_URL, data, config);
this.job = response.data.job;
this.poll.bind(this)();
} catch (error) {
this.emit('error', new Error(error));
}
};
Diawi.prototype.poll = function (pollCount) {
if (pollCount > POLL_MAX_COUNT) {
this.emit('error', new Error('Timed out polling for job completion'));
return;
}
sleep(POLL_INTERVAL).then(function () {
const url = STATUS_URL + '?job=' + this.job + '&token=' + this.token;
return axios.get(url);
}.bind(this)).then(function (response) {
try {
switch (response.data.status) {
case 2000:
if (response.data.link) {
this.emit('complete', response.data.link, response.data.qrcode);
} else {
this.emit('error', new Error('Failed to get link from success response'));
}
return;
case 2001:
// Nothing, this just means poll again
break;
default:
this.emit('error', new Error('Error in status response - ' + response.data.message));
return;
}
this.poll(pollCount + 1);
} catch (err) {
this.emit('error', new Error(err));
return;
}
}.bind(this))
.catch(function (error) {
this.emit('error', new Error(error));
}.bind(this));
};
util.inherits(Diawi, EventEmitter);
module.exports = Diawi;