Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
224 lines (202 loc) · 7.97 KB
/
Copy pathserver.ts
File metadata and controls
224 lines (202 loc) · 7.97 KB
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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import http from 'node:http'
import { WebSocketServer } from 'ws'
/**
* Where the fixture server listens. The port is fixed because the updater
* endpoint is baked into the app at build time (see `tauri.e2e.conf.json`).
*/
export const FIXTURE_SERVER_PORT = 3004
export const FIXTURE_SERVER_URL = `http://127.0.0.1:${FIXTURE_SERVER_PORT}`
/** Version the `/updater` endpoint advertises by default. */
export const UPDATER_FIXTURE_VERSION = '2.1.0'
export const UPDATER_FIXTURE_NOTES = 'Test update from the e2e fixture server'
/** `{{target}}` values with special behavior on the `/updater` endpoint. */
export const UPDATER_TARGET_NO_UPDATE = 'e2e-no-update'
export const UPDATER_TARGET_OLDER = 'e2e-older'
export const UPDATER_FIXTURE_OLDER_VERSION = '1.0.0'
/** Body served by `GET /download`. */
export const DOWNLOAD_FIXTURE_BODY =
'hello from the plugins e2e fixture server\n'.repeat(64)
/**
* Filename `GET /raw-header` puts in its `content-disposition`, sent as raw
* UTF-8 bytes on the wire as many servers do.
*/
export const RAW_HEADER_FILENAME = '中文-EN.pdf'
/** WebSocket endpoint of the fixture server (the `ws://` twin of {@link FIXTURE_SERVER_URL}). */
export const WEBSOCKET_FIXTURE_URL = `ws://127.0.0.1:${FIXTURE_SERVER_PORT}/ws`
/** Text message that makes the `/ws` endpoint close the connection with {@link WEBSOCKET_CLOSE_CODE}. */
export const WEBSOCKET_CLOSE_REQUEST = 'close-me'
export const WEBSOCKET_CLOSE_CODE = 4000
export const WEBSOCKET_CLOSE_REASON = 'closed by the fixture server'
/** First (and only) body chunk `GET /stall-body` sends. */
export const STALL_BODY_FIRST_CHUNK = 'first chunk'
export interface FixtureServer {
close(): void
}
/**
* A tiny HTTP server the network-facing specs (updater, upload) talk to:
*
* - `GET /updater/{{target}}/{{arch}}/{{current_version}}` — an updater
* manifest in the dynamic format. Advertises {@link UPDATER_FIXTURE_VERSION},
* or {@link UPDATER_FIXTURE_OLDER_VERSION} when the target is
* {@link UPDATER_TARGET_OLDER}, and replies `204 No Content` when it is
* {@link UPDATER_TARGET_NO_UPDATE}.
* - `GET /download` — {@link DOWNLOAD_FIXTURE_BODY} with a `Content-Length`.
* - `* /echo` — a JSON description of the request (`method`, `url`, `headers`
* and the utf-8 `body`). Node decodes header values as ISO-8859-1, one char
* per byte, so `headers` shows the exact bytes the client sent.
* - `GET /raw-header` — an empty response whose `content-disposition` names
* {@link RAW_HEADER_FILENAME} in raw UTF-8 bytes.
* - `GET /empty/{204,205,304}` — a response with no body and the requested status.
* - `GET /stall[/<id>]` — never answers, so a request can be aborted while it waits for headers.
* - `GET /stall-body[/<id>]` — answers with headers and {@link STALL_BODY_FIRST_CHUNK}, then
* never sends the rest of the body, so a body read can be aborted mid-stream.
* - `GET /stall-state/<id>` — `{ "state": "open" | "closed" | null }`: whether the
* client still holds the connection of the stalled request named `<id>`.
* - `ws /ws` — a WebSocket echo endpoint: text and binary messages are sent
* back as-is, and {@link WEBSOCKET_CLOSE_REQUEST} makes the server close the
* connection with {@link WEBSOCKET_CLOSE_CODE}. On `/ws/headers` the server
* answers the first message with the upgrade request's headers as a JSON
* text message instead (not sent on connect, where it could arrive before
* the client attached a listener).
*/
export function startFixtureServer(): Promise<FixtureServer> {
// Connection state of the named `/stall` and `/stall-body` requests.
const stalls = new Map<string, 'open' | 'closed'>()
const trackStall = (id: string | undefined, res: http.ServerResponse) => {
if (!id) return
stalls.set(id, 'open')
res.on('close', () => stalls.set(id, 'closed'))
}
const server = http.createServer((req, res) => {
const chunks: Buffer[] = []
req.on('data', (chunk: Buffer) => chunks.push(chunk))
req.on('end', () => {
const body = Buffer.concat(chunks)
const url = new URL(req.url ?? '/', FIXTURE_SERVER_URL)
const [, route, ...rest] = url.pathname.split('/')
if (route === 'empty' && req.method === 'GET') {
const status = Number(rest[0])
if ([204, 205, 304].includes(status)) {
res.writeHead(status, { 'x-e2e-empty': 'present' }).end()
return
}
}
if (route === 'stall' && req.method === 'GET') {
// The connection is dropped by the client or by `close()`.
trackStall(rest[0], res)
return
}
if (route === 'stall-body' && req.method === 'GET') {
trackStall(rest[0], res)
res.writeHead(200, { 'content-type': 'text/plain' })
res.write(STALL_BODY_FIRST_CHUNK)
return
}
if (route === 'stall-state' && req.method === 'GET') {
json(res, { state: stalls.get(rest[0]) ?? null })
return
}
if (route === 'updater' && req.method === 'GET') {
const [target] = rest
if (target === UPDATER_TARGET_NO_UPDATE) {
res.writeHead(204).end()
return
}
json(res, {
version:
target === UPDATER_TARGET_OLDER
? UPDATER_FIXTURE_OLDER_VERSION
: UPDATER_FIXTURE_VERSION,
notes: UPDATER_FIXTURE_NOTES,
pub_date: '2026-03-01T14:04:20Z',
url: `${FIXTURE_SERVER_URL}/download`,
signature: ''
})
return
}
if (route === 'download' && req.method === 'GET') {
res
.writeHead(200, {
'content-type': 'text/plain',
'content-length': Buffer.byteLength(DOWNLOAD_FIXTURE_BODY)
})
.end(DOWNLOAD_FIXTURE_BODY)
return
}
if (route === 'raw-header' && req.method === 'GET') {
// Node writes header strings as ISO-8859-1, so this sends the UTF-8 bytes as-is.
const disposition = Buffer.from(
`attachment; filename="${RAW_HEADER_FILENAME}"`
).toString('latin1')
res
.writeHead(200, {
'content-disposition': disposition,
'content-length': 0
})
.end()
return
}
if (route === 'echo') {
json(res, {
method: req.method,
url: req.url,
headers: req.headers,
body: body.toString('utf8')
})
return
}
res.writeHead(404).end()
})
})
const wss = new WebSocketServer({ noServer: true })
server.on('upgrade', (req, socket, head) => {
const { pathname } = new URL(req.url ?? '/', FIXTURE_SERVER_URL)
if (pathname !== '/ws' && pathname !== '/ws/headers') {
socket.destroy()
return
}
wss.handleUpgrade(req, socket, head, (ws) => {
let sendHeaders = pathname === '/ws/headers'
ws.on('message', (data, isBinary) => {
if (sendHeaders) {
sendHeaders = false
ws.send(JSON.stringify(req.headers))
} else if (
!isBinary
&& Buffer.isBuffer(data)
&& data.toString('utf8') === WEBSOCKET_CLOSE_REQUEST
) {
ws.close(WEBSOCKET_CLOSE_CODE, WEBSOCKET_CLOSE_REASON)
} else {
ws.send(data, { binary: isBinary })
}
})
})
})
return new Promise((resolve, reject) => {
server.once('error', reject)
server.listen(FIXTURE_SERVER_PORT, '127.0.0.1', () => {
server.off('error', reject)
resolve({
close: () => {
for (const client of wss.clients) client.terminate()
wss.close()
server.closeAllConnections()
server.close()
}
})
})
})
}
function json(res: http.ServerResponse, value: unknown) {
const payload = JSON.stringify(value)
res
.writeHead(200, {
'content-type': 'application/json',
'content-length': Buffer.byteLength(payload)
})
.end(payload)
}