-
Notifications
You must be signed in to change notification settings - Fork 49
/
app.js
237 lines (227 loc) · 7.74 KB
/
app.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Main runnable file.
*/
import compression from 'compression';
import cors from 'cors';
import express from 'express';
import fileUpload from 'express-fileupload';
import { writeFile } from 'fs';
import http from 'http';
import insafe from 'insafe';
import morgan from 'morgan';
import { Server } from 'socket.io';
import tmp from 'tmp';
import * as api from './lib/api.js';
import * as l10n from './lib/l10n.js';
import { Sink } from './lib/sink.js';
import { allProfiles, importJSON } from './lib/util.js';
import { Specberus } from './lib/validator.js';
import * as views from './lib/views.js';
const { version } = importJSON('./package.json', import.meta.url);
// Settings:
const DEFAULT_PORT = 80;
if (!process.env.BASE_URI || process.env.BASE_URI.length < 1) {
console.warn(
`Environment variable “BASE_URI” not defined; assuming that Pubrules lives at “/”`
);
}
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Middleware:
app.use(morgan('combined'));
app.use(compression());
app.use('/badterms.json', cors());
app.use(
fileUpload({
createParentPath: true,
useTempFiles: true,
tempFileDir: '/tmp/',
})
);
app.use(express.static('public'));
api.setUp(app);
views.setUp(app);
// @TODO Localize this properly when messages are translated; hard-coded British English for now.
l10n.setLanguage('en_GB');
server.listen(process.argv[2] || process.env.PORT || DEFAULT_PORT);
io.on('connection', socket => {
socket.emit('handshake', { version });
socket.on('extractMetadata', data => {
if (!data.url && !data.file)
return socket.emit('exception', {
message: 'URL or file not provided.',
});
const specberus = new Specberus();
const handler = new Sink();
handler.on('err', (type, data) => {
try {
socket.emit(
'err',
l10n.message(null, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('warning', (type, data) => {
try {
socket.emit(
'warning',
l10n.message(null, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('info', (type, data) => {
try {
socket.emit(
'info',
l10n.message(null, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('end-all', metadata => {
metadata.url = data.url;
socket.emit('finishedExtraction', metadata);
});
handler.on('exception', data => {
socket.emit('exception', data);
});
data.events = handler;
specberus.extractMetadata(data);
});
socket.on('validate', async data => {
if (!data.url && !data.file)
return socket.emit('exception', {
message: 'URL or file not provided.',
});
if (!data.profile)
return socket.emit('exception', {
message: 'Profile not provided.',
});
const profilePath = allProfiles.find(p =>
p.endsWith(`/${data.profile}.js`)
);
let profile;
try {
profile = await import(`./lib/profiles/${profilePath}`);
} catch (err) {
return socket.emit('exception', {
message: `Failed to get profile ${profilePath}.`,
});
}
const specberus = new Specberus();
const handler = new Sink();
const profileCode = profile.name;
socket.emit('start', {
rules: (profile.rules || []).map(rule => rule.name),
});
handler.on('err', (type, data) => {
try {
socket.emit(
'err',
l10n.message(profileCode, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('warning', (type, data) => {
try {
socket.emit(
'warning',
l10n.message(profileCode, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('info', (type, data) => {
try {
socket.emit(
'info',
l10n.message(profileCode, type, data.key, data.extra)
);
} catch (err) {
socket.emit('exception', err.message);
}
});
handler.on('done', name => {
socket.emit('done', { name });
});
handler.on('end-all', () => {
socket.emit('finished');
});
handler.on('exception', data => {
socket.emit('exception', data);
});
if (data.url) {
insafe
.check({
url: data.url,
statusCodesAccepted: ['301', '406'],
})
.then(res => {
if (res.status) {
try {
specberus.validate({
url: data.url,
profile,
events: handler,
validation: data.validation,
informativeOnly: data.informativeOnly,
echidnaReady: data.echidnaReady,
patentPolicy: data.patentPolicy,
});
} catch (e) {
socket.emit('exception', {
message: `Validation blew up: ${e}`,
});
socket.emit('finished');
}
} else {
const message = `Error while resolving <a href="${data.url}"><code>${data.url}</code></a>;
check the spelling of the host, the protocol (<code>HTTP</code>, <code>HTTPS</code>)
and ensure that the page is accessible from the public internet.`;
socket.emit('exception', { message });
}
})
.catch(e => {
socket.emit('exception', {
message: `Insafe check blew up: ${e}`,
});
socket.emit('finished');
});
} else {
try {
specberus.validate({
file: data.file,
profile,
events: handler,
validation: data.validation,
informativeOnly: data.informativeOnly,
echidnaReady: data.echidnaReady,
patentPolicy: data.patentPolicy,
});
} catch (e) {
socket.emit('exception', {
message: `Validation blew up: ${e}`,
});
socket.emit('finished');
}
}
});
socket.on('upload', (file, callback) => {
const tmpfile = tmp.fileSync().name;
writeFile(tmpfile, file, err => {
callback({
status: err ? 'failure' : 'success',
filename: tmpfile,
});
});
});
});