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 28 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
28 commits
Select commit Hold shift + click to select a range
c3f43cc
Adding SDK Binding for BlobTrigger
Apr 24, 2025
d5ed6aa
Adding Supported Binding Types Enum
Apr 24, 2025
0541ff5
Adding MI support
Apr 27, 2025
4e6848d
Renaming and Creating a Factory for StorageBlobClient
Apr 28, 2025
4b154d0
Adding support for input binding
Apr 28, 2025
3e98a8b
Adding unit tests
Apr 28, 2025
bde73cd
Removing Log Statements
Apr 28, 2025
80b8618
Updating the package.json for sinon testing dependencies
Apr 28, 2025
c97b5e6
Bump serialize-javascript and mocha (#332)
dependabot[bot] Apr 28, 2025
0e7efad
Original Without Singleton
May 5, 2025
cd584ba
Moving the StorageClientFactory to extensions base
May 9, 2025
a50c485
Updating package.json
May 9, 2025
81f6c60
Removing extra log
May 9, 2025
caa39e1
Adding generic call to support deferred binding
May 12, 2025
d1332ec
Code Review Comments
May 12, 2025
4dca789
Adding thje logs for Dashboard at function start
May 13, 2025
057e790
Fixing Package-loc.json
May 13, 2025
a5cc33b
Fixing Linting issue and tests
May 13, 2025
2c81f81
Ensure the release version is in the constant file
May 13, 2025
ebbc2ea
Adding SDK Binding for BlobTrigger
Apr 24, 2025
580212a
Adding Supported Binding Types Enum
Apr 24, 2025
380c3dd
Adding MI support
Apr 27, 2025
cf1be60
Renaming and Creating a Factory for StorageBlobClient
Apr 28, 2025
dfdafd9
Package.json conflict resolution
May 14, 2025
e2d7acb
Merge branch 'swapnil/SdkBindingBlob' of https://github.com/Azure/azu…
May 14, 2025
1d8c6f5
Removing the unecessory log
May 15, 2025
8c85817
Removing unecessory changes
May 15, 2025
326454d
Removing AzuriteConfig
May 15, 2025
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.

11 changes: 7 additions & 4 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 @@ -41,6 +41,7 @@
"watch": "webpack --watch --mode development"
},
"dependencies": {
"@azure/functions-extensions-base": "0.1.0-preview",
"cookie": "^0.7.0",
"long": "^4.0.0",
"undici": "^5.13.0"
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
47 changes: 46 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,45 @@ 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
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