|
| 1 | +import path from 'path'; |
| 2 | +import express from 'express'; |
| 3 | + |
| 4 | +type AvailableUpdate = |
| 5 | + | { |
| 6 | + version: string; |
| 7 | + notes: string; |
| 8 | + } |
| 9 | + | undefined; |
| 10 | + |
| 11 | +export function startAutoUpdateServer({ |
| 12 | + expectedPlatform, |
| 13 | + expectedChannel, |
| 14 | + expectedVersion, |
| 15 | + newVersion, |
| 16 | + filename, |
| 17 | + mockedDistDir, |
| 18 | + newNotes, |
| 19 | + port, |
| 20 | +}: { |
| 21 | + // the fake update will only apply to this exact platform |
| 22 | + expectedPlatform: string; |
| 23 | + // which channel the version applies to |
| 24 | + expectedChannel: 'dev' | 'beta' | 'stable'; |
| 25 | + // the fake update will only apply to versions matching exactly this string |
| 26 | + expectedVersion: string; |
| 27 | + // the fake version number of what we'll allow updating to |
| 28 | + newVersion: string; |
| 29 | + // the of the (probably fake) file containing the new version |
| 30 | + filename: string; |
| 31 | + // where to read the file from |
| 32 | + mockedDistDir: string; |
| 33 | + // the release notes of the new version |
| 34 | + newNotes: string; |
| 35 | + // port number for the this fake update server we're starting |
| 36 | + port: number; |
| 37 | +}) { |
| 38 | + const app = express(); |
| 39 | + |
| 40 | + app.use((req, res, next) => { |
| 41 | + console.log(req.method, req.path); |
| 42 | + next(); |
| 43 | + }); |
| 44 | + |
| 45 | + const updatablePlatforms = [ |
| 46 | + 'linux-x64', |
| 47 | + 'win32-x64', |
| 48 | + 'darwin-x64', |
| 49 | + 'darwin-arm64', |
| 50 | + ].join('|'); |
| 51 | + |
| 52 | + // http://localhost:3000/api/v2/update/compass/dev/darwin-arm64/1.45.0/check |
| 53 | + app.get( |
| 54 | + `/api/v2/update/:distribution/:channel/:platform(${updatablePlatforms})/:version/check`, |
| 55 | + (req, res) => { |
| 56 | + const match = { |
| 57 | + route: 'check', |
| 58 | + params: req.params, |
| 59 | + }; |
| 60 | + console.log(match); |
| 61 | + |
| 62 | + let availableUpdate: AvailableUpdate = undefined; |
| 63 | + |
| 64 | + if ( |
| 65 | + req.params.channel === expectedChannel && |
| 66 | + req.params.platform === expectedPlatform && |
| 67 | + req.params.version === expectedVersion |
| 68 | + ) { |
| 69 | + availableUpdate = { |
| 70 | + version: newVersion, |
| 71 | + notes: newNotes, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + if (!availableUpdate) { |
| 76 | + console.log( |
| 77 | + 'no update for', |
| 78 | + req.params.channel, |
| 79 | + req.params.platform, |
| 80 | + req.params.version |
| 81 | + ); |
| 82 | + res.status(204).send(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const response = { |
| 87 | + name: availableUpdate.version, |
| 88 | + from: req.params.version, |
| 89 | + to: availableUpdate.version, |
| 90 | + }; |
| 91 | + |
| 92 | + console.log('response:', response); |
| 93 | + res.json(response); |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + // http://localhost:3000/api/v2/update/compass/dev/win32-x64/1.45.0/RELEASES |
| 98 | + app.get( |
| 99 | + '/api/v2/update/:distribution/:channel/:platform(windows|win32-x64)/:version/RELEASES', |
| 100 | + (req, res) => { |
| 101 | + const match = { |
| 102 | + route: 'windows-releases', |
| 103 | + params: req.params, |
| 104 | + }; |
| 105 | + console.log(match); |
| 106 | + // TODO: implement this |
| 107 | + res.send(match); |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + // http://localhost:3000/api/v2/update/compass/dev/win32-x64/1.45.0/something.nupkg |
| 112 | + app.get( |
| 113 | + '/api/v2/update/:distribution/:channel/:platform(windows|win32-x64)/:version/:nupkg', |
| 114 | + (req, res) => { |
| 115 | + const match = { |
| 116 | + route: 'windows-nupkg', |
| 117 | + params: req.params, |
| 118 | + }; |
| 119 | + console.log(match); |
| 120 | + // TODO: implement this |
| 121 | + res.send(match); |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + // http://localhost:3000/api/v2/update/compass/dev/darwin-arm64/1.45.0 |
| 126 | + app.get( |
| 127 | + '/api/v2/update/:distribution/:channel/:platform(osx|darwin-x64|darwin-arm64)/:version', |
| 128 | + (req, res) => { |
| 129 | + const match = { |
| 130 | + route: 'mac-version', |
| 131 | + params: req.params, |
| 132 | + }; |
| 133 | + console.log(match); |
| 134 | + //res.send(match); |
| 135 | + |
| 136 | + let availableUpdate: AvailableUpdate = undefined; |
| 137 | + |
| 138 | + if ( |
| 139 | + req.params.channel === expectedChannel && |
| 140 | + req.params.platform === expectedPlatform && |
| 141 | + req.params.version === expectedVersion |
| 142 | + ) { |
| 143 | + availableUpdate = { |
| 144 | + version: newVersion, |
| 145 | + notes: newNotes, |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + if (!availableUpdate) { |
| 150 | + console.log( |
| 151 | + 'no update for', |
| 152 | + req.params.channel, |
| 153 | + req.params.platform, |
| 154 | + req.params.version |
| 155 | + ); |
| 156 | + res.status(204).send(); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + const response = { |
| 161 | + url: `http://localhost:${port}/download/${filename}`, |
| 162 | + name: availableUpdate.version, |
| 163 | + notes: availableUpdate.notes || '', |
| 164 | + from: req.params.version, |
| 165 | + to: availableUpdate.version, |
| 166 | + }; |
| 167 | + |
| 168 | + console.log('response:', response); |
| 169 | + res.json(response); |
| 170 | + } |
| 171 | + ); |
| 172 | + |
| 173 | + // Usually the update file would be on S3 somewhere. We're not uploading the |
| 174 | + // mocked compass anywhere on the internet, we're serving it ourselves. So |
| 175 | + // this server just tells Compass to download the new update from here. |
| 176 | + app.get('/download/:filename', (req, res) => { |
| 177 | + const match = { |
| 178 | + route: 'fake-download', |
| 179 | + params: req.params, |
| 180 | + }; |
| 181 | + console.log(match); |
| 182 | + |
| 183 | + if (req.params.filename !== filename) { |
| 184 | + console.log('filename does not match', req.params.filename, filename); |
| 185 | + res.status(404).send(); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + res.sendFile(path.join(mockedDistDir, filename)); |
| 190 | + }); |
| 191 | + |
| 192 | + // fallback to 404 for anything unknown, with some debugging |
| 193 | + app.get('/*', (req, res) => { |
| 194 | + res.status(404).send(req.path); |
| 195 | + }); |
| 196 | + |
| 197 | + const server = app.listen(port, () => { |
| 198 | + console.log(`Listening on port ${port}`); |
| 199 | + }); |
| 200 | + |
| 201 | + return { server, app }; |
| 202 | +} |
| 203 | + |
| 204 | +/* |
| 205 | +const { server } = start({ |
| 206 | + expectedPlatform: 'darwin-arm64', |
| 207 | + expectedVersion: '0.0.1-dev.0', |
| 208 | + newVersion: '1.46.0', |
| 209 | + newURL: 'TODO', |
| 210 | + newNotes: 'release notes go here', |
| 211 | + port: 3000, |
| 212 | +}); |
| 213 | +*/ |
0 commit comments