Skip to content

Commit 3b51b6f

Browse files
authored
Merge pull request Azure#238 from yojagad/AnotherDuplicateData
Account for removal of duplicate data from host
2 parents ba8402b + bd1b5e4 commit 3b51b6f

File tree

6 files changed

+90
-10
lines changed

6 files changed

+90
-10
lines changed

src/WorkerChannel.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IFunctionLoader } from './FunctionLoader';
55
import { CreateContextAndInputs, LogCallback, ResultCallback } from './Context';
66
import { IEventStream } from './GrpcService';
77
import { toTypedData } from './converters';
8+
import { augmentTriggerMetadata } from './augmenters';
89
import { systemError, systemLog } from './utils/Logger';
910
import { InternalException } from './utils/InternalException';
1011

@@ -82,8 +83,10 @@ export class WorkerChannel implements IWorkerChannel {
8283
* @param msg gRPC message content
8384
*/
8485
public workerInitRequest(requestId: string, msg: rpc.WorkerInitRequest) {
85-
let capabilitiesDictionary = {};
86-
capabilitiesDictionary['RpcHttpBodyOnly'] = "true";
86+
const capabilitiesDictionary = {
87+
RpcHttpTriggerMetadataRemoved: "true",
88+
RpcHttpBodyOnly: "true"
89+
};
8790
this._eventStream.write({
8891
requestId: requestId,
8992
workerInitResponse: {
@@ -126,6 +129,9 @@ export class WorkerChannel implements IWorkerChannel {
126129
* @param msg gRPC message content
127130
*/
128131
public invocationRequest(requestId: string, msg: rpc.InvocationRequest) {
132+
// Repopulate triggerMetaData if http.
133+
augmentTriggerMetadata(msg);
134+
129135
let info = this._functionLoader.getInfo(<string>msg.functionId);
130136
let logCallback: LogCallback = (level, ...args) => {
131137
this.log({
@@ -267,4 +273,4 @@ export class WorkerChannel implements IWorkerChannel {
267273

268274
return status;
269275
}
270-
}
276+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc';
2+
3+
/**
4+
* Augments TriggerMetadata from invocation with $request.
5+
* @param request IInvocationRequest object
6+
*/
7+
export function augmentTriggerMetadata(request: rpc.IInvocationRequest) {
8+
var key: any, value: any;
9+
if (request.inputData) {
10+
request.inputData.forEach((element, _index, _array) => {
11+
let elementKeys = Object.keys(element);
12+
if (elementKeys) {
13+
for (let val of elementKeys) {
14+
if (element[val] && element[val].http) {
15+
key = element["name"];
16+
value = element[val];
17+
break;
18+
}
19+
}
20+
}
21+
});
22+
}
23+
if(request && request.triggerMetadata)
24+
{
25+
if(key && (request.triggerMetadata[key] === undefined || request.triggerMetadata[key] === null))
26+
{
27+
request.triggerMetadata[key] = value;
28+
}
29+
if((request.triggerMetadata["$request"] === undefined || request.triggerMetadata["$request"] === null))
30+
{
31+
request.triggerMetadata["$request"] = value;
32+
}
33+
}
34+
}

src/augmenters/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './InvocationRequestAugmenters';

src/converters/BindingConverters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export function getBindingDefinitions(info: FunctionInfo): BindingDefinition[] {
2424
export function getNormalizedBindingData(request: rpc.IInvocationRequest): Dict<any> {
2525
let bindingData: Dict<any> = {
2626
invocationId: request.invocationId
27-
};
27+
};
28+
2829
// node binding data is camel cased due to language convention
2930
if (request.triggerMetadata) {
3031
Object.assign(bindingData, convertKeysToCamelCase(request.triggerMetadata))

test/BindingConvertersTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Binding Converters', () => {
2020
string: "Https://mock/"
2121
}
2222
};
23+
2324
var request: rpc.IInvocationRequest = <rpc.IInvocationRequest> {
2425
triggerMetadata: triggerDataMock,
2526
invocationId: "12341"

test/WorkerChannelTests.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('WorkerChannel', () => {
2828
requestId: 'id',
2929
workerInitResponse: {
3030
capabilities: {
31-
'RpcHttpBodyOnly': "true"
31+
'RpcHttpBodyOnly': "true",
32+
'RpcHttpTriggerMetadataRemoved': "true"
3233
},
3334
result: {
3435
status: rpc.StatusResult.Status.Success
@@ -161,13 +162,45 @@ describe('WorkerChannel', () => {
161162
name: 'test',
162163
outputBindings: {}
163164
})
165+
166+
var triggerDataMock: { [k: string]: rpc.ITypedData } = {
167+
"Headers": {
168+
json: JSON.stringify({Connection: 'Keep-Alive'})
169+
},
170+
"Sys": {
171+
json: JSON.stringify({MethodName: 'test-js', UtcNow: '2018', RandGuid: '3212'})
172+
}
173+
};
174+
175+
var inputDataValue = {
176+
name: "req",
177+
data: {
178+
data: "http",
179+
http:
180+
{
181+
body:
182+
{
183+
data: "string",
184+
body: "blahh"
185+
},
186+
rawBody:
187+
{
188+
data: "string",
189+
body: "blahh"
190+
}
191+
}
192+
}
193+
};
194+
195+
var actualInvocationRequest: rpc.IInvocationRequest = <rpc.IInvocationRequest> {
196+
functionId: 'id',
197+
invocationId: '1',
198+
inputData: [inputDataValue],
199+
triggerMetadata: triggerDataMock,
200+
};
164201

165202
stream.addTestMessage({
166-
invocationRequest: {
167-
functionId: 'id',
168-
invocationId: '1',
169-
inputData: []
170-
}
203+
invocationRequest: actualInvocationRequest
171204
});
172205

173206
sinon.assert.calledWithMatch(stream.written, <rpc.IStreamingMessage> {
@@ -179,6 +212,10 @@ describe('WorkerChannel', () => {
179212
outputData: []
180213
}
181214
});
215+
216+
// triggerMedata will be augmented with inpuDataValue since "RpcHttpTriggerMetadataRemoved" capability is set to true and therefore not populated by the host.
217+
expect(JSON.stringify(actualInvocationRequest.triggerMetadata!.$request)).to.equal(JSON.stringify(inputDataValue.data));
218+
expect(JSON.stringify(actualInvocationRequest.triggerMetadata!.req)).to.equal(JSON.stringify(inputDataValue.data));
182219
});
183220

184221
it ('throws for malformed messages', () => {

0 commit comments

Comments
 (0)