-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff1b62
commit 80d9429
Showing
6 changed files
with
99 additions
and
65 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 @@ | ||
--- | ||
"next-ws-cli": patch | ||
--- | ||
|
||
Add support for Next.js version 15 |
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 |
---|---|---|
@@ -1,37 +1,47 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import logger from '~/helpers/logger'; | ||
import { patchNextNodeServer } from './patch-1'; | ||
import { findNextDirectory } from '~/helpers/next'; | ||
import { patchNextNodeServer } from './patch-1'; | ||
|
||
// Add `SOCKET?: Function` to the page module interface check field thing in | ||
// next/dist/build/webpack/plugins/next-types-plugin.js | ||
// REMARK: The file for 'next-types-plugin' was moved in 13.4.9 | ||
|
||
const NextTypesFilePath = path.join( | ||
const NextTypesFilePath = join( | ||
findNextDirectory(), | ||
'dist/build/webpack/plugins/next-types-plugin/index.js', | ||
); | ||
|
||
// Add `SOCKET?: Function` to the page module interface check field thing in | ||
// `next/dist/build/webpack/plugins/next-types-plugin/index.js` | ||
export function patchNextTypesPlugin() { | ||
export async function patchNextTypesPlugin() { | ||
logger.info("Adding 'SOCKET' to the page module interface type..."); | ||
|
||
let content = fs.readFileSync(NextTypesFilePath, 'utf8'); | ||
if (content.includes('SOCKET?: Function')) return; | ||
const source = await readFile(NextTypesFilePath, 'utf8'); | ||
if (source.includes('SOCKET?: Function')) | ||
return logger.warn( | ||
"'SOCKET' already exists in page module interface, skipping.", | ||
); | ||
|
||
const toFind = '.map((method)=>`${method}?: Function`).join("\\n ")'; | ||
const replaceWith = `${toFind} + "; SOCKET?: Function"`; | ||
content = content.replace(toFind, replaceWith); | ||
const toFind = | ||
/\.map\(\(method\)=>`\${method}\?: Function`\).join\(['"]\\n +['"]\)/; | ||
const replaceWith = `.map((method)=>\`\${method}?: Function\`).join('\\n ') + "; SOCKET?: Function"`; | ||
const newSource = source.replace(toFind, replaceWith); | ||
if (!newSource.includes('SOCKET?: Function')) | ||
throw 'Failed to add SOCKET to page module interface type.'; | ||
|
||
fs.writeFileSync(NextTypesFilePath, content); | ||
await writeFile(NextTypesFilePath, newSource); | ||
logger.info("'SOCKET' added to page module interface type."); | ||
} | ||
|
||
// | ||
|
||
export default Object.assign( | ||
() => { | ||
patchNextNodeServer(); | ||
patchNextTypesPlugin(); | ||
async () => { | ||
await patchNextNodeServer(); | ||
await patchNextTypesPlugin(); | ||
}, | ||
{ | ||
date: '2023-07-15' as const, | ||
// The file for 'next-types-plugin' was moved in 13.4.9 | ||
supported: '>=13.4.9 <=13.4.12' as const, | ||
date: '2023-06-16' as const, | ||
supported: '>=13.1.1 <=13.4.8' as const, | ||
}, | ||
); |
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 |
---|---|---|
@@ -1,40 +1,41 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import logger from '~/helpers/logger'; | ||
import { findNextDirectory } from '~/helpers/next'; | ||
import { patchNextNodeServer } from './patch-1'; | ||
import { patchNextTypesPlugin } from './patch-2'; | ||
|
||
const RouterServerFilePath = path.join( | ||
// If Next.js receives a WebSocket connection on a matched route, it will | ||
// close it immediately. This patch prevents that from happening. | ||
// REMARK: This patch is only necessary for Next.js versions greater than 13.5.1 | ||
|
||
const RouterServerFilePath = join( | ||
findNextDirectory(), | ||
'dist/server/lib/router-server.js', | ||
); | ||
|
||
// If Next.js receives a WebSocket connection on a matched route, it will | ||
// close it immediately. This patch prevents that from happening. | ||
export function patchRouterServer() { | ||
export async function patchRouterServer() { | ||
logger.info( | ||
'Preventing Next.js from immediately closing WebSocket connections...', | ||
); | ||
|
||
let content = fs.readFileSync(RouterServerFilePath, 'utf8'); | ||
|
||
if (content.includes('return socket.end();')) | ||
content = content.replace('return socket.end();', ''); | ||
const toFind = /(\/\/ [a-zA-Z .]+\s+)socket\.end\(\);/; | ||
if (toFind.test(content)) content = content.replace(toFind, ''); | ||
const source = await readFile(RouterServerFilePath, 'utf8'); | ||
const newSource = source | ||
.replace('return socket.end();', '') | ||
.replace(/(\/\/ [a-zA-Z .]+\s+)socket\.end\(\);/, ''); | ||
|
||
fs.writeFileSync(RouterServerFilePath, content); | ||
await writeFile(RouterServerFilePath, newSource); | ||
logger.info('WebSocket connection closing prevention patch applied.'); | ||
} | ||
|
||
export default Object.assign( | ||
() => { | ||
patchNextNodeServer(); | ||
patchRouterServer(); | ||
patchNextTypesPlugin(); | ||
async () => { | ||
await patchNextNodeServer(); | ||
await patchRouterServer(); | ||
await patchNextTypesPlugin(); | ||
}, | ||
{ | ||
date: '2023-11-01' as const, | ||
supported: '>=13.5.1 <=14.2.16' as const, | ||
supported: '>=13.5.1 <=15.0.1' as const, | ||
}, | ||
); |