-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathvite.plugin.server.ts
176 lines (157 loc) · 6.6 KB
/
vite.plugin.server.ts
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
import fs from 'fs';
import path from 'path';
import url from 'url';
import { acceptOne } from './scripts/accept-new-reference-files.common';
import { Connect, Plugin } from 'vite';
import { ServerResponse } from 'http';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
function removeLeadingSlash(str: string): string {
return str[0] === '/' ? str.slice(1) : str;
}
function readBody(req: Connect.IncomingMessage, length: number): Promise<string> {
return new Promise((resolve, reject) => {
let chunks: string = '';
let received = 0;
req.on('data', (chunk: Buffer) => {
chunks += chunk.toString('utf-8');
received += chunk.length;
if (received >= length) {
resolve(chunks);
}
});
req.on('end', () => {
resolve(chunks);
});
req.on('error', () => {
reject();
});
});
}
export default function server(): Plugin {
return {
name: 'at-test-data-server',
configureServer(server) {
const testDataPath = path.join(__dirname, 'test-data');
server.middlewares.use('/test-results', (req, res, next) => {
try {
const response: any = [];
function crawl(d: string, name: string) {
const dir = fs.opendirSync(d);
try {
while (true) {
const entry = dir.readSync();
if (!entry) {
break;
} else if (entry.isDirectory() && entry.name !== '.' && entry.name !== '..') {
crawl(path.join(d, entry.name), name + '/' + entry.name);
} else if (entry.isFile()) {
if (entry.name.endsWith('.new.png')) {
response.push({
originalFile: name + '/' + entry.name.replace('.new.png', '.png'),
newFile: name + '/' + entry.name,
diffFile: name + '/' + entry.name.replace('.new.png', '.diff.png')
});
}
}
}
} finally {
dir.close();
}
}
crawl(testDataPath, 'test-data');
const json = Buffer.from(JSON.stringify(response), 'utf-8');
res.writeHead(200, 'OK', {
'content-type': 'application/json',
'content-length': json.length
});
res.write(json);
res.end();
} catch (e) {
const json = Buffer.from(
JSON.stringify({
message: (e as Error).message,
stack: (e as Error).stack
}),
'utf-8'
);
res.writeHead(500, 'Internal Server Error', {
'content-type': 'application/json',
'content-length': json.length
});
res.write(json);
res.end();
}
});
async function handleAsync(req: Connect.IncomingMessage, res: ServerResponse) {
if (req.method !== 'POST') {
res.writeHead(405, 'Method not allowed');
console.log('Wrong HTTP Method');
return;
}
if (!req.headers['content-length']) {
res.writeHead(411, 'Length Required');
console.log('Missing Length');
return;
}
const bodyLength = parseInt(req.headers['content-length']!);
if (bodyLength > 10_000 || !isFinite(bodyLength)) {
res.writeHead(413, 'Content Too Large');
console.log('Too long');
return;
}
const bodyText = await readBody(req, bodyLength);
const body = JSON.parse(bodyText);
// basic validation that nothing bad happens
if (typeof body.originalFile !== 'string') {
res.writeHead(400, 'Bad Request');
console.log('invalid json');
return;
}
let relativePath = removeLeadingSlash(body.newFile);
if (relativePath.startsWith('test-data/')) {
relativePath = relativePath.substring('test-data/'.length);
}
const newFile = path.normalize(path.resolve(testDataPath, relativePath));
if (!newFile.startsWith(testDataPath)) {
res.writeHead(400, 'Bad Request');
console.log('invalid path', newFile, testDataPath);
res.end();
return;
}
await acceptOne(newFile);
const json = Buffer.from(
JSON.stringify({
message: 'Accepted'
}),
'utf-8'
);
res.writeHead(200, 'OK', {
'content-type': 'application/json',
'content-length': json.length
});
res.write(json);
}
server.middlewares.use('/accept-test-result', (req, res, next) => {
handleAsync(req, res)
.then(() => {
res.end();
})
.catch(e => {
const json = Buffer.from(
JSON.stringify({
message: (e as Error).message,
stack: (e as Error).stack
}),
'utf-8'
);
res.writeHead(500, 'Internal Server Error', {
'content-type': 'application/json',
'content-length': json.length
});
res.write(json);
res.end();
});
});
}
};
}