Skip to content

Commit

Permalink
[node] Fix exposing WebSocket to edge runtime and legacy next tests (#…
Browse files Browse the repository at this point in the history
…9871)

This ensures we expose WebSocket correctly for edge runtime for Node.js
v16 and v18 properly. This also fixes failing legacy Next.js tests due
to a bad `@babel/compat-data` package publish from 8 hours ago.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04R82GSDBN/p1682709850008189?thread_ts=1682707749.424459&cid=C04R82GSDBN)
x-ref: vercel/next.js#48924
x-ref: #9860
Closes: #9870
  • Loading branch information
ijjk committed Apr 29, 2023
1 parent 09ac96a commit cc140d6
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 318 deletions.
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/00-index-isr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"next": "10.1.3",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"next": "11.1.0",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/00-legacy-404/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.5.5",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.5.3",
"react": "16.13.1",
"react-dom": "16.13.1"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"next": "8.0.3",
"react": "latest",
"react-dom": "latest"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/00-non-server-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "10.0.5",
"react": "latest",
"react-dom": "latest"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"next": "10.0.8",
"react": "latest",
"react-dom": "latest"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "10.0.8",
"react": "latest",
"react-dom": "latest"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/00-shared-lambdas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "10.0.8",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"next": "10.0.8",
"react": "17.0.1",
"react-dom": "17.0.1"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/03-next-8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "8.x.x",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/05-spr-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.1.6-canary.1",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
},
"devDependencies": {
"@babel/plugin-syntax-class-properties": "^7.7.4",
"@types/next": "^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.2.3-canary.13",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/20-pages-404-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.2.3-canary.4",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"next": "9.3.4",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"next": "9.3.4",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/fixtures/27-non-word-param/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9.4.4",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"next": "9.5.1",
"react": "16.13.1",
"react-dom": "16.13.1"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 3 additions & 0 deletions packages/next/test/integration/public-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"next": "9",
"react": "16",
"react-dom": "16"
},
"resolutions": {
"@babel/compat-data": "7.21.4"
}
}
3 changes: 2 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"ts-morph": "12.0.0",
"ts-node": "10.9.1",
"typescript": "4.9.5",
"undici": "5.22.0"
"undici": "5.22.0",
"ws": "8.13.0"
},
"devDependencies": {
"@babel/core": "7.5.0",
Expand Down
13 changes: 12 additions & 1 deletion packages/node/src/edge-functions/edge-handler.mts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,25 @@ async function createEdgeRuntimeServer(params?: {
const wasmBindings = await params.wasmAssets.getContext();
const nodeCompatBindings = params.nodeCompatBindings.getContext();

let WebSocket: any;

// undici's WebSocket handling is only available in Node.js >= 18
// so fallback to using ws for v16
if (Number(process.version.split('.')[0].substring(1)) < 18) {

This comment has been minimized.

Copy link
@styfle

styfle Apr 29, 2023

Member

No need for substring

if (Number(process.versions.node.split('.')[0]) < 18) {
// @ts-ignore
WebSocket = (await import('ws')).WebSocket;
} else {
WebSocket = (await import('undici')).WebSocket;
}

const runtime = new EdgeRuntime({
initialCode: params.userCode,
extend: context => {
Object.assign(context, {
// This is required for esbuild wrapping logic to resolve
module: {},

WebSocket: require('undici').WebSocket,
WebSocket,

// This is required for environment variable access.
// In production, env var access is provided by static analysis
Expand Down
47 changes: 21 additions & 26 deletions packages/node/test/unit/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import fetch from 'node-fetch';

jest.setTimeout(20 * 1000);

const [nodeMajor] = process.versions.node.split('.').map(v => Number(v));

function testForkDevServer(entrypoint: string) {
const ext = extname(entrypoint);
const isTypeScript = ext === '.ts';
Expand All @@ -26,33 +24,30 @@ function testForkDevServer(entrypoint: string) {
});
}

(nodeMajor >= 18 ? test : test.skip)(
'runs an edge function that uses `WebSocket`',
async () => {
const child = testForkDevServer('./edge-websocket.js');
try {
const result = await readMessage(child);
if (result.state !== 'message') {
throw new Error('Exited. error: ' + JSON.stringify(result.value));
}
test('runs an edge function that uses `WebSocket`', async () => {
const child = testForkDevServer('./edge-websocket.js');
try {
const result = await readMessage(child);
if (result.state !== 'message') {
throw new Error('Exited. error: ' + JSON.stringify(result.value));
}

const { address, port } = result.value;
const response = await fetch(
`http://${address}:${port}/api/edge-websocket`
);
const { address, port } = result.value;
const response = await fetch(
`http://${address}:${port}/api/edge-websocket`
);

expect({
status: response.status,
body: await response.text(),
}).toEqual({
status: 200,
body: '3210',
});
} finally {
child.kill(9);
}
expect({
status: response.status,
body: await response.text(),
}).toEqual({
status: 200,
body: '3210',
});
} finally {
child.kill(9);
}
);
});

test('runs an edge function that uses `buffer`', async () => {
const child = testForkDevServer('./edge-buffer.js');
Expand Down

0 comments on commit cc140d6

Please sign in to comment.