Skip to content

Swapnil/sdk binding blob with extensions #342

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

Closed
wants to merge 9 commits into from
Closed
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
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@
"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"
},
{
"name": "mocha tests",
"type": "node",
"protocol": "inspector",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"--require",
"ts-node/register",
"${workspaceRoot}/test/**/toCoreFunctionMetadata.test.ts",
"--no-timeouts"
],
"cwd": "${workspaceRoot}"
}
]
}
1,238 changes: 923 additions & 315 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 +66,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
17 changes: 17 additions & 0 deletions src/converters/fromRpcTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the MIT License.

import { RpcTypedData } from '@azure/functions-core';
import { StorageBlobClientOptions } from '../../types';
import { HttpRequest } from '../http/HttpRequest';
import { isModelBindingData, parseConnectionDetails } from '../sdk-binding/connectionDetails';
import { StorageBlobClientFactoryResolver } from '../storageBlobClientFactoryResolver';
import { isDefined } from '../utils/nonNull';

export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown {
Expand Down Expand Up @@ -30,6 +33,20 @@ 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 if (data.modelBindingData && isDefined(data.modelBindingData.content)) {
if (isModelBindingData(data.modelBindingData)) {
const blobConnectionDetails = parseConnectionDetails(data.modelBindingData.content);

const storageBlobClientOptions: StorageBlobClientOptions = {
connection: blobConnectionDetails.Connection,
containerName: blobConnectionDetails.ContainerName,
blobName: blobConnectionDetails.BlobName,
};
const storageBlobClientFactoryResolver = StorageBlobClientFactoryResolver.getInstance();
return storageBlobClientFactoryResolver.createClient(storageBlobClientOptions);
}
//TODO determine if we need to throw error.
return data.modelBindingData;
} else {
return undefined;
}
Expand Down
13 changes: 12 additions & 1 deletion src/converters/toCoreFunctionMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ 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),
};
bindingNames.push(trigger.name);

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

return { name, bindings, retryOptions };
}

function addSdkBindingsFlag(sdkBindingType?: boolean | unknown): { [key: string]: string } {
//Ensure that trigger type that is passed is valid and supported.
if (sdkBindingType !== undefined && sdkBindingType === true) {
return { supportsDeferredBinding: 'true' };
}

return { supportsDeferredBinding: 'false' };
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { HttpResponse } from './http/HttpResponse';
export * as input from './input';
export { InvocationContext } from './InvocationContext';
export * as output from './output';
export * from './storageBlobClientFactoryResolver';
export * as trigger from './trigger';
export { Disposable } from './utils/Disposable';

Expand Down
90 changes: 90 additions & 0 deletions src/sdk-binding/connectionDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { ModelBindingData } from '@azure/functions-core';

export type BlobConnectionDetails = {
Connection: string;
ContainerName: string;
BlobName: string;
};

// Define the `ServiceBusConnectionInfo` type that extends `ConnectionInfo`
//TODO Define other connectionInfo example ServiceBusConnectionDetails

/**
* Type Guard to check if an object is of type BlobConnectionInfo
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
function isBlobConnectionDetails(obj: unknown): obj is BlobConnectionDetails {
return (
obj !== null &&
typeof obj === 'object' &&
'Connection' in obj &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
typeof (obj as any).Connection === 'string' &&
'ContainerName' in obj &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
typeof (obj as any).ContainerName === 'string' &&
'BlobName' in obj &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
typeof (obj as any).BlobName === 'string'
);
}

/**
* Function to parse JSON and determine its type
* @param jsonBuffer Bufer that holds the JSON string to parse
* @returns Either `BlobConnectionDetails` or `ServiceBusConnectionDetails`
*/
export function parseConnectionDetails(jsonBuffer: Buffer | null | undefined): BlobConnectionDetails {
if (jsonBuffer === null || jsonBuffer === undefined) {
throw new Error('Connection details content is null or undefined');
}
const parsedObject: unknown = JSON.parse(jsonBuffer.toString());

if (isBlobConnectionDetails(parsedObject)) {
return parsedObject;
}
//TODO add other parser for different resource types
else {
throw new Error('Invalid connection info type');
}
}

/**
* Type guard to check if an object conforms to the ModelBindingData interface
* @param obj Object to check
* @returns True if object is ModelBindingData
*/
export function isModelBindingData(obj: unknown): obj is ModelBindingData {
if (!obj || typeof obj !== 'object') {
return false;
}

const candidate = obj as Record<string, unknown>;

// Check content property if it exists
if (
!('content' in candidate) ||
candidate.content === null ||
candidate.content === undefined ||
!(candidate.content instanceof Buffer)
) {
return false;
}

// Check string properties if they exist
const stringProps = ['contentType', 'source', 'version'];
for (const prop of stringProps) {
if (
prop in candidate &&
candidate[prop] !== null &&
candidate[prop] !== undefined &&
typeof candidate[prop] !== 'string'
) {
return false;
}
}
return true;
}
89 changes: 89 additions & 0 deletions src/storageBlobClientFactoryResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { StorageBlobClientFactory, StorageBlobClientOptions } from '../types';

/**
* Registry for Storage Blob Client factory implementations.
* Manages the registration and retrieval of blob client factories across the application.
*
* @example
* ```typescript
* // Register a factory
* StorageBlobClientRegistry.getInstance().registerFactory((options) => {
* return new MyCustomBlobClient(options.connection, options.path);
* });
*
* // Create a blob client
* const blobClient = StorageBlobClientRegistry.getInstance().createClient({
* connection: "MyStorageConnection",
* path: "container/blob.txt"
* });
* ```
*/
export class StorageBlobClientFactoryResolver {
private static instance: StorageBlobClientFactoryResolver;
private factory: StorageBlobClientFactory | undefined;

/**
* Private constructor to enforce singleton pattern
*/
private constructor() {
// Initialize as needed
}

/**
* Gets the singleton instance of the registry
* @returns The singleton instance
*/
static getInstance(): StorageBlobClientFactoryResolver {
if (!StorageBlobClientFactoryResolver.instance) {
StorageBlobClientFactoryResolver.instance = new StorageBlobClientFactoryResolver();
}
return StorageBlobClientFactoryResolver.instance;
}

/**
* Registers a factory implementation to create Storage Blob Clients
* @param factory - The factory function implementation
* @throws Error if a factory is already registered
*/
registerFactory(factory: StorageBlobClientFactory): void {
if (this.factory) {
throw new Error(
'A StorageBlobClient factory is already registered. Unregister the existing factory first.'
);
}
this.factory = factory;
}

/**
* Unregisters the current factory implementation
*/
unregisterFactory(): void {
this.factory = undefined;
}

/**
* Creates a Storage Blob Client using the registered factory
* @param options - Options for creating the Storage Blob Client
* @returns The created Storage Blob Client
* @throws Error if no factory is registered
*/
createClient(options: StorageBlobClientOptions): unknown {
if (!this.factory) {
throw new Error(
'StorageBlobClient factory is not registered. Register a factory implementation before creating clients.'
);
}
return this.factory(options);
}

/**
* Checks if a factory is registered
* @returns true if a factory is registered, false otherwise
*/
hasFactory(): boolean {
return this.factory !== undefined;
}
}
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
Loading