Skip to content

Commit 57e8166

Browse files
committed
test: add test for intentional deploy failure when C++ addons / .node modules are attempted in node middleware
1 parent 9cabcc2 commit 57e8166

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/build/functions/edge.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const copyHandlerDependenciesForNodeMiddleware = async (ctx: PluginContext) => {
218218
// files are relative to location of middleware entrypoint
219219
// we need to capture all of them
220220
// they might be going to parent directories, so first we check how many directories we need to go up
221-
const { maxParentDirectoriesPath } = files.reduce(
221+
const { maxParentDirectoriesPath, unsupportedDotNodeModules } = files.reduce(
222222
(acc, file) => {
223223
let dirsUp = 0
224224
let parentDirectoriesPath = ''
@@ -231,18 +231,30 @@ const copyHandlerDependenciesForNodeMiddleware = async (ctx: PluginContext) => {
231231
}
232232
}
233233

234+
if (file.endsWith('.node')) {
235+
// C++ addons are not supported
236+
acc.unsupportedDotNodeModules.push(join(srcDir, file))
237+
}
238+
234239
if (dirsUp > acc.maxDirsUp) {
235240
return {
241+
...acc,
236242
maxDirsUp: dirsUp,
237243
maxParentDirectoriesPath: parentDirectoriesPath,
238244
}
239245
}
240246

241247
return acc
242248
},
243-
{ maxDirsUp: 0, maxParentDirectoriesPath: '' },
249+
{ maxDirsUp: 0, maxParentDirectoriesPath: '', unsupportedDotNodeModules: [] as string[] },
244250
)
245251

252+
if (unsupportedDotNodeModules.length !== 0) {
253+
throw new Error(
254+
`Usage of unsupported C++ Addon(s) found in Node.js Middleware:\n${unsupportedDotNodeModules.map((file) => `- ${file}`).join('\n')}\n\nCheck https://docs.netlify.com/build/frameworks/framework-setup-guides/nextjs/overview/#limitations for more information.`,
255+
)
256+
}
257+
246258
const commonPrefix = relative(join(srcDir, maxParentDirectoriesPath), srcDir)
247259

248260
parts.push(`const virtualModules = new Map();`)

tests/integration/middleware.test.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -698,39 +698,25 @@ for (const {
698698

699699
if (isNodeMiddleware) {
700700
describe('Node.js Middleware specific', () => {
701-
test.only<FixtureTestContext>('should fail to deploy when using unsupported C++ Addons with meaningful message about limitation', async (ctx) => {
701+
test<FixtureTestContext>('should fail to deploy when using unsupported C++ Addons with meaningful message about limitation', async (ctx) => {
702702
await createFixture('middleware-node-unsupported-cpp-addons', ctx)
703703

704704
const runPluginPromise = runPlugin(ctx)
705-
await runPluginPromise
706-
707-
const origin = await LocalServer.run(async (req, res) => {
708-
res.write(
709-
JSON.stringify({
710-
url: req.url,
711-
headers: req.headers,
712-
}),
713-
)
714-
res.end()
715-
})
716-
ctx.cleanup?.push(() => origin.stop())
717-
const response = await invokeEdgeFunction(ctx, {
718-
functions: [edgeFunctionNameRoot],
719-
origin,
720-
url: `/json`,
721-
})
722-
console.log(response)
723-
724-
// await expect(runPluginPromise).rejects.toThrow('Node.js middleware is not yet supported.')
725-
// await expect(runPluginPromise).rejects.toThrow(
726-
// 'Future @netlify/plugin-nextjs release will support node middleware with following limitations:',
727-
// )
728-
// await expect(runPluginPromise).rejects.toThrow(
729-
// ' - usage of C++ Addons (https://nodejs.org/api/addons.html) not supported (for example `bcrypt` npm module will not be supported, but `bcryptjs` will be supported)',
730-
// )
731-
// await expect(runPluginPromise).rejects.toThrow(
732-
// ' - usage of Filesystem (https://nodejs.org/api/fs.html) not supported',
733-
// )
705+
706+
await expect(
707+
runPluginPromise,
708+
'error message should describe error cause',
709+
).rejects.toThrow('Usage of unsupported C++ Addon(s) found in Node.js Middleware')
710+
await expect(
711+
runPluginPromise,
712+
'error message should mention c++ addons (.node) file names to help finding the package(s) that contain them',
713+
).rejects.toThrow('bcrypt.node')
714+
await expect(
715+
runPluginPromise,
716+
'link to documentation should be provided',
717+
).rejects.toThrow(
718+
'https://docs.netlify.com/build/frameworks/framework-setup-guides/nextjs/overview/#limitations',
719+
)
734720
})
735721
})
736722
}

0 commit comments

Comments
 (0)