-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
163 lines (146 loc) · 4.37 KB
/
main.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
const process = require('process');
const core = require('@actions/core');
const server = require('./server.js');
if (process.argv.length === 3 && process.argv[2] === 'serve') {
process.on('SIGTERM', () => {
process.exit(0);
});
process.on('message', (msg) => {
core.debug(`child: starting server with ${JSON.stringify(msg.config, null, 2)}`);
server.deploy(msg.config, () => {
core.debug(`child: server started`);
process.send({
state: 'serving',
pid: process.pid,
});
});
})
core.debug('child: server is ready');
process.send({
state: 'ready',
pid: process.pid,
});
return;
}
let config = {
root: null,
port: null,
noCache: null,
indexFiles: null,
allowedMethods: null,
contentTypes: null,
log: null,
logTime: null,
custom404Page: null
};
config.root = core.getInput('directory');
if (config.root === null || config.root.length == 0) {
config.root = '.';
}
config.port = core.getInput('port');
if (config.port === null || config.port.length == 0) {
config.port = 8080;
} else {
const parsed = Number.parseInt(config.port);
if (Number.isNaN(parsed)) {
core.error(`Error: unable to parse input port "${config.port}"`);
return;
}
config.port = parsed;
}
config.noCache = core.getInput('no-cache');
if (config.noCache === null || config.noCache.length == 0) {
config.noCache = false;
} else {
config.noCache = config.noCache === 'true';
}
config.indexFiles = core.getInput('index-files');
if (config.indexFiles === null || config.indexFiles.length == 0) {
config.indexFiles = [];
} else {
config.indexFiles = JSON.parse(config.indexFiles);
}
config.contentTypes = core.getInput('content-types');
if (config.contentTypes === null || config.contentTypes.length == 0) {
config.contentTypes = {
appcache: 'text/cache-manifest',
css: 'text/css',
gif: 'image/gif',
html: 'text/html',
ico: 'image/x-icon',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
js: 'text/javascript',
json: 'application/json',
png: 'image/png',
txt: 'text/plain',
xml: 'text/xml'
};
} else {
config.contentTypes = JSON.parse(config.contentTypes);
}
config.allowedMethods = core.getInput('allowed-methods');
if (config.allowedMethods === null || config.allowedMethods.length == 0) {
config.allowedMethods = ['GET', 'HEAD'];
} else {
config.allowedMethods = JSON.parse(config.allowedMethods);
}
config.log = core.getInput("log");
config.logTime = core.getInput('logTime');
if (config.logTime === null || config.logTime.length == 0) {
config.logTime = true;
} else {
config.logTime = config.logTime === 'true';
}
config.custom404Page = core.getInput("custom404Page");
const cp = require('child_process');
const child = cp.fork(__filename, ['serve'], { detached: true, silent: true });
child.on('error', (err) => {
core.error(`Error: unable to spawn server: ${err}`);
process.exit(1);
return;
});
child.on('message', (msg) => {
if (msg.state === undefined || msg.state === null || msg.pid === undefined || msg.pid === null) {
core.error(`Error: invalid message`);
child.kill();
process.exit(1);
return;
}
if (msg.pid != child.pid) {
core.error(`Error: expected pid ${child.pid}, but got ${msg.pid}`);
child.kill();
process.exit(1);
return;
}
switch (msg.state) {
case 'ready':
core.debug(`master: server ready at ${msg.pid}`);
core.debug(`master: starting server with ${JSON.stringify(config, null, 2)}`);
child.send({
config: config,
});
break;
case 'serving':
core.saveState('pid', msg.pid);
core.info('server running');
process.exit(0);
break;
}
return;
});
// test if the server was started
setTimeout(() => {
try {
process.kill(child.pid, 0);
// process is alive but did not send an message
core.error(`Error: server was started but never notified its presence.`);
child.kill();
process.exit(1);
return;
} catch (e) {
// process is dead
core.error(`Error: server is dead`);
process.exit(1);
}
}, 5000);