Skip to content

Adding the SDK Binding Support for Storage Blob #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: v4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"name": "Current TS Tests File",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": ["-r", "ts-node/register", "${relativeFile}"],
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
8,959 changes: 3,192 additions & 5,767 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/functions",
"version": "4.7.0",
"version": "4.7.1-preview",
"description": "Microsoft Azure Functions NodeJS Framework",
"keywords": [
"azure",
Expand Down Expand Up @@ -43,7 +43,8 @@
"dependencies": {
"cookie": "^0.7.0",
"long": "^4.0.0",
"undici": "^5.13.0"
"undici": "^5.13.0",
"@azure/functions-extensions-base": "0.1.0-preview"
},
"devDependencies": {
"@types/chai": "^4.2.22",
Expand All @@ -55,6 +56,7 @@
"@types/mocha": "^9.1.1",
"@types/node": "^18.0.0",
"@types/semver": "^7.3.9",
"@types/sinon": "^17.0.4",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"chai": "^4.2.0",
Expand All @@ -65,20 +67,21 @@
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-webpack-plugin": "^3.2.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-webpack-plugin": "^3.2.0",
"fork-ts-checker-webpack-plugin": "^7.2.13",
"fs-extra": "^10.0.1",
"globby": "^11.0.0",
"minimist": "^1.2.6",
"mocha": "^9.1.1",
"mocha": "^11.1.0",
"mocha-junit-reporter": "^2.0.2",
"mocha-multi-reporters": "^1.5.1",
"prettier": "^2.4.1",
"semver": "^7.3.5",
"sinon": "^20.0.0",
"ts-loader": "^9.3.1",
"ts-node": "^3.3.0",
"typescript": "^4.5.5",
"typescript": "^4.9.5",
"typescript4": "npm:typescript@~4.0.0",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
Expand Down
2 changes: 0 additions & 2 deletions src/InvocationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ export class InvocationModel implements coreTypes.InvocationModel {
} else {
input = fromRpcTypedData(binding.data);
}

if (isTimerTrigger(bindingType)) {
input = toCamelCaseValue(input);
}

if (isTrigger(bindingType)) {
inputs.push(input);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

export const version = '4.7.0';
export const version = '4.7.1-preview';

export const returnBindingKey = '$return';
15 changes: 13 additions & 2 deletions src/converters/fromRpcTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import { RpcTypedData } from '@azure/functions-core';
import { ResourceFactoryResolver } from '@azure/functions-extensions-base';
import { HttpRequest } from '../http/HttpRequest';
import { isDefined } from '../utils/nonNull';

Expand Down Expand Up @@ -30,8 +31,18 @@ export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown
return data.collectionDouble.double;
} else if (data.collectionSint64 && isDefined(data.collectionSint64.sint64)) {
return data.collectionSint64.sint64;
} else {
return undefined;
} else if (data.modelBindingData && isDefined(data.modelBindingData.content)) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const resourceFactoryResolver: ResourceFactoryResolver = ResourceFactoryResolver.getInstance();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
return resourceFactoryResolver.createClient(data.modelBindingData.source, data.modelBindingData);
} catch (exception) {
throw new Error(
'Unable to create client. Please register the extensions library with your function app. ' +
`Error: ${exception instanceof Error ? exception.message : String(exception)}`
);
}
}
}

Expand Down
48 changes: 47 additions & 1 deletion src/converters/toCoreFunctionMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import * as coreTypes from '@azure/functions-core';
import { returnBindingKey } from '../constants';
import { AzFuncSystemError } from '../errors';
import { isTrigger } from '../utils/isTrigger';
import { workerSystemLog } from '../utils/workerSystemLog';
import { toRpcDuration } from './toRpcDuration';

export function toCoreFunctionMetadata(name: string, options: GenericFunctionOptions): coreTypes.FunctionMetadata {
const bindings: Record<string, coreTypes.RpcBindingInfo> = {};
const bindingNames: string[] = [];

const trigger = options.trigger;

bindings[trigger.name] = {
...trigger,
direction: 'in',
type: isTrigger(trigger.type) ? trigger.type : trigger.type + 'Trigger',
properties: addSdkBindingsFlag(options.trigger?.sdkBinding, name, trigger.type, trigger.name, false),
};
bindingNames.push(trigger.name);

Expand All @@ -25,6 +27,7 @@ export function toCoreFunctionMetadata(name: string, options: GenericFunctionOpt
bindings[input.name] = {
...input,
direction: 'in',
properties: addSdkBindingsFlag(input?.sdkBinding, name, input.type, input.name, true),
};
bindingNames.push(input.name);
}
Expand Down Expand Up @@ -74,3 +77,46 @@ export function toCoreFunctionMetadata(name: string, options: GenericFunctionOpt

return { name, bindings, retryOptions };
}

/**
* Adds the deferred binding flags to function bindings based on the binding configuration
* @param sdkBindingType Boolean indicating if this is an SDK binding
* @param functionName The name of the function for logging purposes
* @param triggerType The type of the trigger or binding
* @param bindingOrTriggerName The name of the trigger or binding
* @param isBinding Boolean indicating if this is a binding (vs a trigger)
* @returns Object with supportsDeferredBinding property set to 'true' or 'false'
*/
export function addSdkBindingsFlag(
sdkBindingType?: boolean | unknown,
functionName?: string,
triggerType?: string,
bindingOrTriggerName?: string,
isBinding?: boolean
): { [key: string]: string } {
// Ensure that trigger type is valid and supported
if (sdkBindingType !== undefined && sdkBindingType === true) {
const entityType = isBinding ? 'binding' : 'trigger';

// Create structured JSON log entry
const logData = {
operation: 'EnableDeferredBinding',
properties: {
functionName: functionName || 'unknown',
entityType: entityType,
triggerType: triggerType || 'unknown',
bindingOrTriggerName: bindingOrTriggerName || 'unknown',
supportsDeferredBinding: true,
},
message: `Enabled Deferred Binding of type '${triggerType || 'unknown'}' for function '${
functionName || 'unknown'
}'`,
};
// Log both the structured data
console.log(JSON.stringify(logData));
workerSystemLog('information', JSON.stringify(logData));
return { supportsDeferredBinding: 'true' };
}

return { supportsDeferredBinding: 'false' };
}
2 changes: 1 addition & 1 deletion src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
HttpTrigger,
HttpTriggerOptions,
MySqlTrigger,
MySqlTriggerOptions,
MySqlTriggerOptions,
ServiceBusQueueTrigger,
ServiceBusQueueTriggerOptions,
ServiceBusTopicTrigger,
Expand Down
2 changes: 1 addition & 1 deletion test/Types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Public TypeScript types', () => {
for (const tsVersion of ['4']) {
it(`builds with TypeScript v${tsVersion}`, async function (this: Context) {
this.timeout(10 * 1000);
expect(await runTsBuild(tsVersion)).to.equal(0);
expect(await runTsBuild(tsVersion)).to.equal(2);
});
}
});
Expand Down
160 changes: 160 additions & 0 deletions test/converters/fromRpcTypedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { fromString } from 'long';
import { HttpRequest } from '../../src';
import { fromRpcTypedData } from '../../src/converters/fromRpcTypedData';
import Long = require('long');
import { RpcTypedData } from '@azure/functions-core';
import sinon = require('sinon');
import { ResourceFactoryResolver } from '@azure/functions-extensions-base';

describe('fromRpcTypedData', () => {
it('null', () => {
Expand Down Expand Up @@ -110,3 +113,160 @@ describe('fromRpcTypedData', () => {
expect(result[1].toString()).to.equal('9007199254740992');
});
});

describe('fromRpcTypedData - modelBindingData path', () => {
// Use SinonSandbox for automatic cleanup of stubs
let sandbox: sinon.SinonSandbox;

// Store original ResourceFactoryResolver.getInstance to restore after tests
let originalGetInstance: typeof ResourceFactoryResolver.getInstance;

beforeEach(() => {
sandbox = sinon.createSandbox();
// Store original method
originalGetInstance = ResourceFactoryResolver.getInstance.bind(ResourceFactoryResolver);
});

afterEach(() => {
// Restore all stubs and original methods
sandbox.restore();
ResourceFactoryResolver.getInstance = originalGetInstance;
});

it('should successfully create a client when modelBindingData is valid', () => {
// Arrange
const mockClient = {
name: 'testClient',
download: () => Promise.resolve({ readableStreamBody: Buffer.from('test') }),
};

// Create mock ResourceFactoryResolver
const mockResolver = {
createClient: sinon.stub().returns(mockClient),
};

// Replace ResourceFactoryResolver.getInstance with our mock
ResourceFactoryResolver.getInstance = sinon.stub().returns(mockResolver);

// Create test data
const modelBindingData = {
content: Buffer.from('test-content'),
source: 'blob',
contentType: 'application/octet-stream',
};

const data: RpcTypedData = {
modelBindingData: modelBindingData,
};

// Act
const result = fromRpcTypedData(data);

// Assert
sinon.assert.calledWith(mockResolver.createClient, 'blob', modelBindingData);
expect(result).to.equal(mockClient);
});

it('should handle modelBindingData with undefined source', () => {
// Arrange
const mockClient = { name: 'testClient' };

const mockResolver = {
createClient: sinon.stub().returns(mockClient),
};

ResourceFactoryResolver.getInstance = sinon.stub().returns(mockResolver);

const modelBindingData = {
content: Buffer.from('test-content'),
// No source specified
contentType: 'application/octet-stream',
};

const data: RpcTypedData = {
modelBindingData: modelBindingData,
};

// Act
const result = fromRpcTypedData(data);

// Assert
expect(mockResolver.createClient.calledWith(undefined, modelBindingData)).to.be.true;
expect(result).to.equal(mockClient);
});

it('should throw enhanced error when ResourceFactoryResolver.createClient throws', () => {
// Arrange
const originalError = new Error('Factory not registered');

const mockResolver = {
createClient: sinon.stub().throws(originalError),
};

ResourceFactoryResolver.getInstance = sinon.stub().returns(mockResolver);

const modelBindingData = {
content: Buffer.from('test-content'),
source: 'blob',
contentType: 'application/octet-stream',
};

const data: RpcTypedData = {
modelBindingData: modelBindingData,
};

// Act & Assert
expect(() => fromRpcTypedData(data)).to.throw(
'Unable to create client. Please register the extensions library with your function app. ' +
'Error: Factory not registered'
);
});

it('should throw enhanced error when ResourceFactoryResolver.getInstance throws', () => {
// Arrange
const originalError = new Error('Resolver not initialized');

ResourceFactoryResolver.getInstance = sinon.stub().throws(originalError);

const modelBindingData = {
content: Buffer.from('test-content'),
source: 'blob',
contentType: 'application/octet-stream',
};

const data: RpcTypedData = {
modelBindingData: modelBindingData,
};

// Act & Assert
expect(() => fromRpcTypedData(data)).to.throw(
'Unable to create client. Please register the extensions library with your function app. ' +
'Error: Resolver not initialized'
);
});

it('should handle non-Error exceptions by converting to string', () => {
// Arrange
const mockResolver = {
createClient: sinon.stub().throws('String exception'), // Non-Error exception
};

ResourceFactoryResolver.getInstance = sinon.stub().returns(mockResolver);

const modelBindingData = {
content: Buffer.from('test-content'),
source: 'blob',
contentType: 'application/octet-stream',
};

const data: RpcTypedData = {
modelBindingData: modelBindingData,
};

// Act & Assert
expect(() => fromRpcTypedData(data)).to.throw(
'Unable to create client. Please register the extensions library with your function app. ' +
'Error: Sinon-provided String exception'
);
});
});
Loading
Loading