-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow pages dev to proxy websockets (#2212)
* feat: allow pages dev to proxy websockets * add tests for websocket requests * add new line at EOF Co-authored-by: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com> * improve test logic * rename ws-app to pages-ws-app * Update packages/wrangler/src/miniflare-cli/assets.ts Co-authored-by: Greg Brimble <gbrimble@cloudflare.com> * style: formatted files in pages-ws-app * chore: update changeset to minor * undo unintended changes to lockfile Reverted the file back to c725ce0 * rerun npm install Co-authored-by: Skye <46286597+Skye-31@users.noreply.github.com> Co-authored-by: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com> Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>
- Loading branch information
1 parent
7da8f0e
commit b24c2b2
Showing
8 changed files
with
204 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: Allow pages dev to proxy websocket requests |
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,36 @@ | ||
{ | ||
"name": "pages-ws-app", | ||
"version": "0.0.0", | ||
"private": true, | ||
"sideEffects": false, | ||
"main": "server/index.js", | ||
"scripts": { | ||
"build": "esbuild --bundle --platform=node server/index.ts --outfile=dist/index.js", | ||
"dev": "npx wrangler pages dev --port 8790 --proxy 8791 -- npm run server", | ||
"server": "node dist/index.js", | ||
"test": "npm run build && npx jest --forceExit", | ||
"test:ci": "npm run build && npx jest --forceExit" | ||
}, | ||
"jest": { | ||
"restoreMocks": true, | ||
"testRegex": ".*.(test|spec)\\.[jt]sx?$", | ||
"testTimeout": 30000, | ||
"transform": { | ||
"^.+\\.c?(t|j)sx?$": [ | ||
"esbuild-jest", | ||
{ | ||
"sourcemap": true | ||
} | ||
] | ||
}, | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)" | ||
] | ||
}, | ||
"devDependencies": { | ||
"ws": "^8.8.0" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
} | ||
} |
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,24 @@ | ||
import { createServer } from "http"; | ||
import { WebSocketServer } from "ws"; | ||
|
||
const server = createServer(); | ||
const wsServer = new WebSocketServer({ noServer: true }); | ||
|
||
wsServer.on("connection", function connection(ws) { | ||
ws.on("message", (msg) => console.log(msg)); | ||
}); | ||
|
||
server.on("upgrade", (request, socket, head) => { | ||
wsServer.handleUpgrade(request, socket, head, (ws) => { | ||
wsServer.emit("connection", ws, request); | ||
}); | ||
}); | ||
|
||
server.on("request", (_request, res) => { | ||
res.writeHead(200, { "Content-Type": "text/plain" }); | ||
res.writeHead(200, { "X-Proxied": "true" }); | ||
res.write("Hello, world!"); | ||
res.end(); | ||
}); | ||
|
||
server.listen(8791); |
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,72 @@ | ||
import { spawn } from "child_process"; | ||
import * as path from "path"; | ||
import { upgradingFetch } from "@miniflare/web-sockets"; | ||
import type { ChildProcess } from "child_process"; | ||
import type { Response } from "miniflare"; | ||
import type { RequestInit } from "undici"; | ||
|
||
const waitUntilReady = async ( | ||
url: string, | ||
requestInit?: RequestInit | ||
): Promise<Response> => { | ||
let response: Response | undefined = undefined; | ||
|
||
while (response === undefined) { | ||
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500)); | ||
|
||
try { | ||
response = await upgradingFetch(url, requestInit); | ||
if (response.status === 502) response = undefined; | ||
} catch {} | ||
} | ||
|
||
return response as Response; | ||
}; | ||
|
||
const isWindows = process.platform === "win32"; | ||
|
||
describe("Pages Functions", () => { | ||
let wranglerProcess: ChildProcess; | ||
|
||
beforeAll(() => { | ||
wranglerProcess = spawn("npm", ["run", "dev"], { | ||
shell: isWindows, | ||
cwd: path.resolve(__dirname, "../"), | ||
env: { BROWSER: "none", ...process.env }, | ||
}); | ||
wranglerProcess.stdout?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
wranglerProcess.stderr?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
wranglerProcess.kill("SIGTERM"); | ||
}); | ||
}); | ||
|
||
it("understands normal fetches", async () => { | ||
const response = await waitUntilReady("http://localhost:8790/"); | ||
expect(response.headers.get("x-proxied")).toBe("true"); | ||
const text = await response.text(); | ||
expect(text).toContain("Hello, world!"); | ||
}); | ||
|
||
it("understands websocket fetches", async () => { | ||
const response = await waitUntilReady("http://localhost:8790/ws", { | ||
headers: { Upgrade: "websocket" }, | ||
}); | ||
expect(response.status).toBe(101); | ||
expect(response.webSocket).toBeDefined(); | ||
}); | ||
}); |
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,10 @@ | ||
{ | ||
"include": ["server"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"], | ||
"moduleResolution": "node" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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