Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

fix: Check for valid index before setting name property #480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/services/packageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Serverless from "serverless";
import { FunctionMetadata } from "../shared/utils";
import { MockFactory } from "../test/mockFactory";
import { PackageService } from "./packageService";
import { Runtime } from "../config/runtime";

jest.mock("rimraf");
import rimraf from "rimraf";
Expand Down Expand Up @@ -228,6 +229,32 @@ describe("Package Service", () => {
writeFileSpy.mockRestore();
});

it("createBinding should be able to create function.json without bindings", async () => {
// Create service provider with python 3.8 runtime
const provider = MockFactory.createTestAzureServiceProvider()
provider.runtime = Runtime.PYTHON38
sls = MockFactory.createTestServerless({"service": { "provider": provider }});
sls.config.servicePath = process.cwd();
sls.service["functions"] = {
hello: MockFactory.createTestAzureFunctionConfig(functionRoute),
eventhubHandler: MockFactory.createTestEventHubFunctionConfig(),
}

const functionName = "hello";
const functionMetadata: FunctionMetadata = {
entryPoint: "handler",
handlerPath: "src/handlers/hello",
params: {
functionJson: {
bindings: []
},
},
};

packageService = new PackageService(sls, MockFactory.createTestServerlessOptions());
await packageService.createBinding(functionName, functionMetadata);
});

it("webpack copies required", async () => {
mockFs({
// Generated by webpack plugin
Expand Down
16 changes: 10 additions & 6 deletions src/services/packageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class PackageService extends BaseService {
rimraf.sync(folderPath);
}
});

// Delete function folder if empty
const items = fs.readdirSync(functionName);
if (items.length === 0) {
Expand All @@ -122,7 +122,7 @@ export class PackageService extends BaseService {
fs.unlinkSync(filePath);
}
}

for (const dir of rootFoldersToRemove) {
const dirPath = path.join(this.serverless.config.servicePath, dir);
if (fs.existsSync(dirPath)) {
Expand Down Expand Up @@ -151,22 +151,26 @@ export class PackageService extends BaseService {
if (this.configService.isPythonTarget()) {
const index = (functionJSON.bindings as any[])
.findIndex((binding) => (!binding.direction || binding.direction === "out"));
functionJSON.bindings[index].name = "$return";
} else {

if (functionJSON.bindings[index]) {
functionJSON.bindings[index].name = "$return";
}
}
functionJSON.scriptFile = handlerPath;

const functionObject = this.configService.getFunctionConfig()[functionName];
const incomingBinding = Utils.getIncomingBindingConfig(functionObject);

const bindingAzureSettings = Utils.get(incomingBinding, constants.xAzureSettings, incomingBinding);

if (bindingAzureSettings.route) {
// Find incoming binding within functionJSON and set the route
const index = (functionJSON.bindings as any[])
.findIndex((binding) => (!binding.direction || binding.direction === "in"));

functionJSON.bindings[index].route = bindingAzureSettings.route;
if (functionJSON.bindings[index]) {
functionJSON.bindings[index].route = bindingAzureSettings.route;
}
}

return functionJSON;
Expand Down