Skip to content
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
11 changes: 9 additions & 2 deletions src/eventHandlers/invocationRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { AzureFunction } from '@azure/functions';
import { HookData, PostInvocationContext, PreInvocationContext } from '@azure/functions-core';
import { format } from 'util';
import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc';
Expand Down Expand Up @@ -82,11 +83,17 @@ export async function invocationRequest(channel: WorkerChannel, requestId: strin
});

const hookData: HookData = {};
const userFunction = channel.functionLoader.getFunc(nonNullProp(msg, 'functionId'));
const preInvocContext: PreInvocationContext = { hookData, invocationContext: context, inputs };
let userFunction = channel.functionLoader.getFunc(nonNullProp(msg, 'functionId'));
const preInvocContext: PreInvocationContext = {
hookData,
invocationContext: context,
functionCallback: <AzureFunction>userFunction,
inputs,
};

await channel.executeHooks('preInvocation', preInvocContext);
inputs = preInvocContext.inputs;
userFunction = preInvocContext.functionCallback;

let rawResult = userFunction(context, ...inputs);
resultIsPromise = rawResult && typeof rawResult.then === 'function';
Expand Down
19 changes: 19 additions & 0 deletions test/eventHandlers/invocationRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,25 @@ describe('invocationRequest', () => {
});
}

it('preInvocationHook respects change to functionCallback', async () => {
loader.getFunc.returns(async (invocContext: Context) => {
invocContext.log('old function');
});
loader.getInfo.returns(new FunctionInfo(Binding.queue));

testDisposables.push(
coreApi.registerHook('preInvocation', (context: coreTypes.PreInvocationContext) => {
expect(context.functionCallback).to.be.a('function');
context.functionCallback = async (invocContext: Context) => {
invocContext.log('new function');
};
})
);

sendInvokeMessage([InputData.string]);
await stream.assertCalledWith(Msg.receivedInvocLog(), Msg.userTestLog('new function'), Msg.invocResponse([]));
});

for (const [func, suffix] of TestFunc.logHookData) {
it('postInvocationHook' + suffix, async () => {
loader.getFunc.returns(func);
Expand Down
7 changes: 6 additions & 1 deletion types-core/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { Context } from '@azure/functions';
import { AzureFunction, Context } from '@azure/functions';

/**
* This module is shipped as a built-in part of the Azure Functions Node.js worker and is available at runtime
Expand Down Expand Up @@ -45,6 +45,11 @@ declare module '@azure/functions-core' {
* The input values for this specific invocation. Changes to this array _will_ affect the inputs passed to your function
*/
inputs: any[];

/**
* The function callback for this specific invocation. Changes to this value _will_ affect the function itself
*/
functionCallback: AzureFunction;
}

/**
Expand Down