forked from mswjs/msw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(browser integration): patch upstream
@open-draft/test-server
t…
…o fix IPv6 issue - Fix bug in IPv6 URL serialization that isn't available upstream - Source is not available, so waiting on new release Same reasoning as: mswjs/interceptors#283
- Loading branch information
1 parent
a129a2f
commit 7ccba4f
Showing
23 changed files
with
141 additions
and
22 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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,119 @@ | ||
import { | ||
HttpServer as OrigHttpServer, | ||
httpsAgent, | ||
} from '@open-draft/test-server/http' | ||
|
||
export const HttpServer: typeof OrigHttpServer = OrigHttpServer | ||
|
||
export type HttpServer = OrigHttpServer | ||
|
||
const PATCHED_IPV6: unique symbol = Symbol('isPatchedIPv6Compat') | ||
|
||
type PatchedForIPv6Compat<Method> = Method & { | ||
[PATCHED_IPV6]?: true | ||
} | ||
|
||
type PatchedGetServerAddress = PatchedForIPv6Compat< | ||
typeof OrigHttpServer.getServerAddress | ||
> | ||
type PatchedBuildHttpServerApi = PatchedForIPv6Compat< | ||
typeof OrigHttpServer.prototype['buildHttpServerApi'] | ||
> | ||
|
||
/** | ||
* Patch `HttpServer` from `../patched/OpenDraftTestServer`, to fix bug in URL | ||
* serialization of IPv6 addresses, by surrounding the host with brackets, e.g.: | ||
* | ||
* correct: { port: 37007, host: '::1', url: 'http://[::1]:37007' } | ||
* incorrect: { port: 37007, host: '::1', url: 'http://::1:37007' } | ||
* ^^^^^ missing [ ] | ||
* | ||
* This bug appears in Node v18 due to new behavior of inheriting operating system | ||
* DNS resolution order, so some dual-stack hosts will resolve addresses including | ||
* `localhost` to `::1`, causing `HttpServer` to bind to `::1` instead of `127.0.0.1`. | ||
* | ||
* This patch is a work-around until the upstream bug is fixed in the | ||
* `../patched/OpenDraftTestServer` package. | ||
* | ||
* To use this patch, change every import of `HttpServer` and `httpsAgent` to | ||
* import from this file, instead of upstream `../patched/OpenDraftTestServer` | ||
* | ||
* To deprecate this patch once the upstream bug is fixed, revert all the | ||
* imports to use `../patched/OpenDraftTestServer`, and delete this file. | ||
*/ | ||
const applyPatchFixingIPv6URISerialization = ( | ||
HttpServer: typeof OrigHttpServer, | ||
): void => { | ||
if ((HttpServer.getServerAddress as PatchedGetServerAddress)[PATCHED_IPV6]) { | ||
console.log('DUPLICATE PATCH: HttpServer.getServerAddress') | ||
} | ||
|
||
// eslint-disable-next-line no-var | ||
var origGetServerAddress = HttpServer.getServerAddress | ||
/** | ||
* Patch `static HttpServer.getServer` to fix URI serialization of IPv6 hosts, | ||
* by calling the original method and then modifying the return value. | ||
* | ||
* @see HttpServer.getServerAddress | ||
*/ | ||
HttpServer.getServerAddress = function () { | ||
const address = origGetServerAddress.apply( | ||
{}, // we're patching a static method so we expect no valid `this` binding | ||
// eslint-disable-next-line prefer-rest-params | ||
arguments as unknown as Parameters<typeof HttpServer.getServerAddress>, | ||
) | ||
|
||
// Copy the same behavior of the original function, but fix the ternary | ||
Object.defineProperty(address, 'href', { | ||
get() { | ||
// assume: host with `:` is definitely not valid IPv4, likely valid IPv6 | ||
return new URL( | ||
`${this.protocol}//${ | ||
this.host.includes(':') && | ||
!this.host.startsWith('[') && | ||
!this.host.endsWith(']') | ||
? `[${this.host}]` | ||
: this.host | ||
}:${this.port}`, | ||
).href | ||
}, | ||
enumerable: true, | ||
}) | ||
|
||
return address | ||
} as PatchedGetServerAddress | ||
;(HttpServer.getServerAddress as PatchedGetServerAddress)[PATCHED_IPV6] = true | ||
|
||
if ( | ||
// @ts-ignore accessing private method .buildHttpServerApi | ||
HttpServer.prototype.buildHttpServerApi[PATCHED_IPV6] | ||
) { | ||
console.log('DUPLICATE PATCH: HttpServer.prototype.buildHttpServerApi') | ||
} | ||
|
||
/** | ||
* Patch `HttpServer.buildHttpServerApi` (private) class method, to fix | ||
* URI serialization of IPv6 hosts. | ||
* | ||
* This patch re-implements the original behavior of the function, but it's necessary | ||
* so that `buildHttpServerApi` can call the newly patched `HttpServer.getServerAddress` | ||
*/ | ||
// @ts-ignore patching a private method | ||
HttpServer.prototype.buildHttpServerApi = (( | ||
server: Parameters<typeof HttpServer.getServerAddress>[0], | ||
) => { | ||
const address = HttpServer.getServerAddress(server) | ||
|
||
return { | ||
address, | ||
url(path = '/') { | ||
return new URL(path, address.href).href | ||
}, | ||
} | ||
}) as PatchedBuildHttpServerApi | ||
HttpServer.prototype['buildHttpServerApi'][PATCHED_IPV6] = true | ||
} | ||
|
||
applyPatchFixingIPv6URISerialization(HttpServer) | ||
|
||
export { httpsAgent } |
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
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
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
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
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