|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import type {Server} from 'connect'; |
| 12 | + |
| 13 | +import connect from 'connect'; |
| 14 | +import fs from 'node:fs'; |
| 15 | +import http from 'node:http'; |
| 16 | +import os from 'node:os'; |
| 17 | +import path from 'node:path'; |
| 18 | + |
| 19 | +jest.useRealTimers(); |
| 20 | +jest.mock('launch-editor', () => jest.fn()); |
| 21 | + |
| 22 | +const launchEditor: JestMockFn<[string, ?string], void> = |
| 23 | + jest.requireMock('launch-editor'); |
| 24 | + |
| 25 | +const projectRoot = fs.realpathSync( |
| 26 | + fs.mkdtempSync(path.join(os.tmpdir(), 'openStackFrameMiddleware-test-')), |
| 27 | +); |
| 28 | +const appFile = path.join(projectRoot, 'App.js'); |
| 29 | +fs.writeFileSync(appFile, ''); |
| 30 | +const outsideRoot = fs.realpathSync( |
| 31 | + fs.mkdtempSync(path.join(os.tmpdir(), 'openStackFrameMiddleware-outside-')), |
| 32 | +); |
| 33 | +const outsideFile = path.join(outsideRoot, 'secret.txt'); |
| 34 | +fs.writeFileSync(outsideFile, ''); |
| 35 | +const symlinkOut = path.join(projectRoot, 'SettingsScreen.tsx'); |
| 36 | +fs.symlinkSync(outsideFile, symlinkOut); |
| 37 | + |
| 38 | +const originalEditor = process.env.REACT_EDITOR; |
| 39 | + |
| 40 | +beforeAll(() => { |
| 41 | + process.env.REACT_EDITOR = 'test-editor'; |
| 42 | +}); |
| 43 | + |
| 44 | +afterAll(() => { |
| 45 | + process.env.REACT_EDITOR = originalEditor; |
| 46 | + fs.rmSync(projectRoot, {recursive: true, force: true}); |
| 47 | + fs.rmSync(outsideRoot, {recursive: true, force: true}); |
| 48 | +}); |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + launchEditor.mockReset(); |
| 52 | +}); |
| 53 | + |
| 54 | +async function startServer( |
| 55 | + mount: (app: Server, serverBaseUrl: URL) => void, |
| 56 | +): Promise<{server: http$Server, baseUrl: string}> { |
| 57 | + const app = connect(); |
| 58 | + const server = http.createServer(app); |
| 59 | + await new Promise(resolve => server.listen(0, 'localhost', resolve)); |
| 60 | + const address = server.address(); |
| 61 | + if (address == null || typeof address === 'string') { |
| 62 | + throw new Error('Expected the server to listen on a TCP port'); |
| 63 | + } |
| 64 | + const baseUrl = `http://localhost:${address.port}`; |
| 65 | + mount(app, new URL(baseUrl)); |
| 66 | + app.use((req, res) => { |
| 67 | + res.writeHead(404); |
| 68 | + res.end(); |
| 69 | + }); |
| 70 | + return {server, baseUrl}; |
| 71 | +} |
| 72 | + |
| 73 | +function mountOpenStackFrame(app: Server, serverBaseUrl: URL) { |
| 74 | + app.use( |
| 75 | + '/open-stack-frame', |
| 76 | + require('../openStackFrameMiddleware').default({ |
| 77 | + serverBaseUrl, |
| 78 | + watchFolders: [projectRoot], |
| 79 | + }), |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +function stopServer(server: http$Server): Promise<void> { |
| 84 | + server.closeAllConnections(); |
| 85 | + return new Promise(resolve => server.close(() => resolve())); |
| 86 | +} |
| 87 | + |
| 88 | +function postFrame( |
| 89 | + baseUrl: string, |
| 90 | + body: string, |
| 91 | + headers?: {Origin?: string}, |
| 92 | +): Promise<Response> { |
| 93 | + return fetch(baseUrl + '/open-stack-frame', { |
| 94 | + method: 'POST', |
| 95 | + headers: {...headers, 'Content-Type': 'application/json'}, |
| 96 | + body, |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +describe('/open-stack-frame', () => { |
| 101 | + let server: http$Server; |
| 102 | + let baseUrl: string; |
| 103 | + |
| 104 | + beforeEach(async () => { |
| 105 | + ({server, baseUrl} = await startServer(mountOpenStackFrame)); |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => stopServer(server)); |
| 109 | + |
| 110 | + test('opens the frame in the editor', async () => { |
| 111 | + const resp = await postFrame( |
| 112 | + baseUrl, |
| 113 | + JSON.stringify({file: appFile, lineNumber: 12}), |
| 114 | + ); |
| 115 | + |
| 116 | + expect(resp.status).toBe(200); |
| 117 | + expect(await resp.text()).toBe('OK'); |
| 118 | + expect(launchEditor).toHaveBeenCalledWith(`${appFile}:12`, 'test-editor'); |
| 119 | + }); |
| 120 | + |
| 121 | + test('throttles a second launch inside the window', async () => { |
| 122 | + const body = JSON.stringify({file: appFile, lineNumber: 12}); |
| 123 | + |
| 124 | + const first = await postFrame(baseUrl, body); |
| 125 | + const second = await postFrame(baseUrl, body); |
| 126 | + |
| 127 | + expect(first.status).toBe(200); |
| 128 | + expect(second.status).toBe(429); |
| 129 | + expect(launchEditor).toHaveBeenCalledTimes(1); |
| 130 | + }); |
| 131 | + |
| 132 | + test('refuses a cross-origin request', async () => { |
| 133 | + const resp = await postFrame( |
| 134 | + baseUrl, |
| 135 | + JSON.stringify({file: appFile, lineNumber: 12}), |
| 136 | + {Origin: 'https://example.com'}, |
| 137 | + ); |
| 138 | + |
| 139 | + expect(resp.status).toBe(403); |
| 140 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + test('rejects a body that is not JSON', async () => { |
| 144 | + const resp = await postFrame(baseUrl, 'not json'); |
| 145 | + |
| 146 | + expect(resp.status).toBe(400); |
| 147 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + test('opens a path relative to the first watch folder', async () => { |
| 151 | + const resp = await postFrame( |
| 152 | + baseUrl, |
| 153 | + JSON.stringify({file: 'App.js', lineNumber: 3}), |
| 154 | + ); |
| 155 | + |
| 156 | + expect(resp.status).toBe(200); |
| 157 | + expect(launchEditor).toHaveBeenCalledWith(`${appFile}:3`, 'test-editor'); |
| 158 | + }); |
| 159 | + |
| 160 | + test.each([ |
| 161 | + ['a file outside every watch folder', outsideFile], |
| 162 | + ['a symlink out of a watch folder', symlinkOut], |
| 163 | + ['a nonexistent file', path.join(projectRoot, 'Missing.js')], |
| 164 | + ])('rejects %s', async (_, file) => { |
| 165 | + const resp = await postFrame( |
| 166 | + baseUrl, |
| 167 | + JSON.stringify({file, lineNumber: 1}), |
| 168 | + ); |
| 169 | + |
| 170 | + expect(resp.status).toBe(400); |
| 171 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 172 | + }); |
| 173 | + |
| 174 | + test.each([ |
| 175 | + ['a missing file', {lineNumber: 1}], |
| 176 | + ['null', null], |
| 177 | + ])('rejects a body with %s', async (_, body) => { |
| 178 | + const resp = await postFrame(baseUrl, JSON.stringify(body)); |
| 179 | + |
| 180 | + expect(resp.status).toBe(400); |
| 181 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 182 | + }); |
| 183 | + |
| 184 | + test('passes a GET request through', async () => { |
| 185 | + const resp = await fetch(baseUrl + '/open-stack-frame'); |
| 186 | + |
| 187 | + expect(resp.status).toBe(404); |
| 188 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe('/open-stack-frame behind a middleware that serves it', () => { |
| 193 | + let server: http$Server; |
| 194 | + let baseUrl: string; |
| 195 | + |
| 196 | + beforeEach(async () => { |
| 197 | + ({server, baseUrl} = await startServer((app, serverBaseUrl) => { |
| 198 | + app.use('/open-stack-frame', (req, res, next) => { |
| 199 | + res.end('handled upstream'); |
| 200 | + next(); |
| 201 | + }); |
| 202 | + mountOpenStackFrame(app, serverBaseUrl); |
| 203 | + })); |
| 204 | + }); |
| 205 | + |
| 206 | + afterEach(() => stopServer(server)); |
| 207 | + |
| 208 | + test('leaves the response to that middleware', async () => { |
| 209 | + const resp = await postFrame( |
| 210 | + baseUrl, |
| 211 | + JSON.stringify({file: appFile, lineNumber: 12}), |
| 212 | + ); |
| 213 | + |
| 214 | + expect(await resp.text()).toBe('handled upstream'); |
| 215 | + expect(launchEditor).not.toHaveBeenCalled(); |
| 216 | + }); |
| 217 | +}); |
0 commit comments