Skip to content

Commit 3aa049b

Browse files
committed
1 parent 19ee0fa commit 3aa049b

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from 'node:assert/strict'
2+
import { it, describe } from 'node:test'
3+
import { ImportFromFolderNameError, packLambda } from './packLambda.js'
4+
import path, { dirname } from 'node:path'
5+
import { fileURLToPath } from 'node:url'
6+
import fs from 'node:fs/promises'
7+
import os from 'node:os'
8+
9+
const tmpDir = os.tmpdir()
10+
11+
void describe('packLambda()', () => {
12+
// See https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
13+
void it('should fail if it imports from a folder that has the same name as the handler module', async () =>
14+
assert.rejects(
15+
async () =>
16+
packLambda({
17+
sourceFile: path.join(
18+
dirname(fileURLToPath(import.meta.url)),
19+
'test-data',
20+
'module-folder-named-like-handler-bug',
21+
'acme.ts',
22+
),
23+
zipFile: path.join(
24+
await fs.mkdtemp(`${tmpDir}${path.sep}`),
25+
'acme.zip',
26+
),
27+
}),
28+
ImportFromFolderNameError,
29+
))
30+
})

src/packLambda.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { checkSumOfFiles } from './checksumOfFiles.js'
66
import { commonParent } from './commonParent.js'
77
import { findDependencies } from './findDependencies.js'
88
import { fileURLToPath } from 'node:url'
9+
import path from 'node:path'
910

1011
export type PackedLambda = {
1112
id: string
@@ -43,12 +44,21 @@ export const packLambda = async ({
4344
debug?: (label: string, info: string) => void
4445
progress?: (label: string, info: string) => void
4546
}): Promise<{ handler: string; hash: string }> => {
46-
const lambdaFiles = [sourceFile, ...findDependencies(sourceFile)]
47+
const deps = findDependencies(sourceFile)
48+
const lambdaFiles = [sourceFile, ...deps]
4749

4850
const zipfile = new yazl.ZipFile()
4951

5052
const stripCommon = removeCommonAncestor(commonParent(lambdaFiles))
5153

54+
const handler = stripCommon(sourceFile)
55+
56+
const folderNames = new Set(deps.map(stripCommon).map((s) => s.split('/')[0]))
57+
const handlerName = path.parse(handler).name
58+
if (folderNames.has(handlerName)) {
59+
throw new ImportFromFolderNameError(handlerName)
60+
}
61+
5262
for (const file of lambdaFiles) {
5363
const compiled = (
5464
await swc.transformFile(file, {
@@ -91,3 +101,17 @@ export const packLambda = async ({
91101

92102
return { handler: stripCommon(sourceFile), hash }
93103
}
104+
105+
/**
106+
* @see https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
107+
*/
108+
export class ImportFromFolderNameError extends Error {
109+
public readonly folderName: string
110+
constructor(folderName: string) {
111+
super(
112+
`Import from folder with same name as handler ("${folderName}") not allowed!`,
113+
)
114+
this.name = 'ImportFromFolderNameError'
115+
this.folderName = folderName
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Lambda's will fail if they import from a folder that has the same name as the
2+
handler.
3+
4+
`packLambda` should fail in this case.
5+
6+
See
7+
https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { hello } from './acme/lib.js'
2+
3+
export const handler = (): string => hello()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const hello = (): string => 'Hello World!'

0 commit comments

Comments
 (0)