This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev server] refactor middleware into other files (#3666)
* refactor middleware into other files * Simplify symbolicator * Update MetroDevServer.ts
- Loading branch information
Showing
4 changed files
with
80 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/dev-server/src/middleware/remoteDevtoolsCorsMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { IncomingMessage, ServerResponse } from 'http'; | ||
import { parse as parseUrl } from 'url'; | ||
|
||
// Middleware that accepts multiple Access-Control-Allow-Origin for processing *.map. | ||
// This is a hook middleware before metro processing *.map, | ||
// which originally allow only devtools://devtools | ||
export function remoteDevtoolsCorsMiddleware( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
next: (err?: Error) => void | ||
) { | ||
if (req.url) { | ||
const url = parseUrl(req.url); | ||
const origin = req.headers.origin; | ||
const isValidOrigin = | ||
origin && | ||
['devtools://devtools', 'https://chrome-devtools-frontend.appspot.com'].includes(origin); | ||
if (url.pathname?.endsWith('.map') && origin && isValidOrigin) { | ||
res.setHeader('Access-Control-Allow-Origin', origin); | ||
|
||
// Prevent metro overwrite Access-Control-Allow-Origin header | ||
const setHeader = res.setHeader.bind(res); | ||
res.setHeader = (key, ...args) => { | ||
if (key === 'Access-Control-Allow-Origin') { | ||
return; | ||
} | ||
setHeader(key, ...args); | ||
}; | ||
} | ||
} | ||
next(); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/dev-server/src/middleware/remoteDevtoolsSecurityHeadersMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { IncomingMessage, ServerResponse } from 'http'; | ||
|
||
// Like securityHeadersMiddleware but further allow cross-origin requests | ||
// from https://chrome-devtools-frontend.appspot.com/ | ||
export function remoteDevtoolsSecurityHeadersMiddleware( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
next: (err?: Error) => void | ||
) { | ||
// Block any cross origin request. | ||
if ( | ||
typeof req.headers.origin === 'string' && | ||
!req.headers.origin.match(/^https?:\/\/localhost:/) && | ||
!req.headers.origin.match(/^https:\/\/chrome-devtools-frontend\.appspot\.com/) | ||
) { | ||
next( | ||
new Error( | ||
`Unauthorized request from ${req.headers.origin}. ` + | ||
'This may happen because of a conflicting browser extension to intercept HTTP requests. ' + | ||
'Please try again without browser extensions or using incognito mode.' | ||
) | ||
); | ||
return; | ||
} | ||
|
||
// Block MIME-type sniffing. | ||
res.setHeader('X-Content-Type-Options', 'nosniff'); | ||
|
||
next(); | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/dev-server/src/middleware/replaceMiddlewareWith.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Server as ConnectServer, HandleFunction } from 'connect'; | ||
|
||
export function replaceMiddlewareWith( | ||
app: ConnectServer, | ||
sourceMiddleware: HandleFunction, | ||
targetMiddleware: HandleFunction | ||
) { | ||
const item = app.stack.find(middleware => middleware.handle === sourceMiddleware); | ||
if (item) { | ||
item.handle = targetMiddleware; | ||
} | ||
} |