Skip to content

Commit

Permalink
ADD: Custom Docker support - PR dherault#1246
Browse files Browse the repository at this point in the history
  • Loading branch information
eek committed Mar 23, 2022
1 parent dc510b3 commit 4079113
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk'
import { parse as semverParse } from 'semver'
import debugLog from './debugLog.js'
import serverlessLog, { logWarning, setLog } from './serverlessLog.js'
import { satisfiesVersionRange } from './utils/index.js'
import { satisfiesVersionRange, getHandlerName } from './utils/index.js'
import {
commandOptions,
CUSTOM_OPTION,
Expand Down Expand Up @@ -319,10 +319,11 @@ export default class ServerlessOffline {
events.forEach((event) => {
const { http, httpApi, schedule, websocket } = event

if ((http || httpApi) && functionDefinition.handler) {
const handlerName = getHandlerName(functionDefinition)
if ((http || httpApi) && handlerName) {
const httpEvent = {
functionKey,
handler: functionDefinition.handler,
handler: handlerName,
http: http || httpApi,
}

Expand Down
9 changes: 7 additions & 2 deletions src/lambda/LambdaFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
DEFAULT_LAMBDA_TIMEOUT,
supportedRuntimes,
} from '../config/index.js'
import { createUniqueId, splitHandlerPathAndName } from '../utils/index.js'
import {
createUniqueId,
splitHandlerPathAndName,
getHandlerName,
} from '../utils/index.js'

const { keys } = Object
const { ceil } = Math
Expand Down Expand Up @@ -56,7 +60,8 @@ export default class LambdaFunction {
// TODO FIXME look into better way to work with serverless-webpack
const _servicePath = resolve(servicePath, options.location || '')

const { handler, name, package: functionPackage = {} } = functionDefinition
const { name, package: functionPackage = {} } = functionDefinition
const handler = getHandlerName(functionDefinition)
const [handlerPath, handlerName, handlerModuleNesting] =
splitHandlerPathAndName(handler)

Expand Down
44 changes: 44 additions & 0 deletions src/utils/__tests__/getHandlerName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import getHandlerName from '../getHandlerName.js'

const tests = [
{
input: {
image: {
command: ['app.handler'],
name: 'node14',
},
events: [
{
httpApi: {
method: 'get',
path: '/',
},
},
],
},
expected: 'app.handler',
},
{
input: {
handler: 'app.handler',
events: [
{
httpApi: {
method: 'get',
path: '/',
},
},
],
},
expected: 'app.handler',
},
]

describe('getHandlerName', () => {
tests.forEach(({ input, expected }) => {
test('Should return the correct handler name', () => {
const handlerValue = getHandlerName(input)
expect(handlerValue).toEqual(expected)
})
})
})
17 changes: 17 additions & 0 deletions src/utils/getHandlerName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function getHandlerName(functionDefinition) {
const {
handler,
image,
} = functionDefinition;

if (handler) {
return handler;
}

const { command } = image;
if (!command || command.length < 1) {
throw new Error('Unable to determine handler name. Be sure to include a "command" property when using a docker image as a custom runtime. See https://www.serverless.com/blog/container-support-for-lambda');
}

return command[0];
}
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { default as splitHandlerPathAndName } from './splitHandlerPathAndName.js
export { default as checkDockerDaemon } from './checkDockerDaemon.js'
export { default as checkGoVersion } from './checkGoVersion.js'
export { default as generateHapiPath } from './generateHapiPath.js'
export { default as getHandlerName } from './getHandlerName.js'
// export { default as baseImage } from './baseImage.js'

// Detect the toString encoding from the request headers content-type
Expand Down

0 comments on commit 4079113

Please sign in to comment.