Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ npm install @azure/functions
- _**Runtime**_: Defines underlying behavior of Azure Functions and is shared across all languages.
- The programming model version is strictly tied to the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package, and is versioned independently of the [runtime](https://learn.microsoft.com/azure/azure-functions/functions-versions?pivots=programming-language-javascript). Both the runtime and the programming model use "4" as their latest major version, but that is purely a coincidence.
- You can't mix the v3 and v4 programming models in the same function app. As soon as you register one v4 function in your app, any v3 functions registered in _function.json_ files are ignored.
- By default, trigger metadata strings will be converted to JSON if they can successfully be interpreted as JSON. This behavior can be disabled by setting the `enablePlainRpcStrings` option to `true` in the setup.

## Usage

Expand Down
7 changes: 7 additions & 0 deletions src/converters/fromRpcTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import { RpcTypedData } from '@azure/functions-core';
import { ResourceFactoryResolver } from '@azure/functions-extensions-base';
import { HttpRequest } from '../http/HttpRequest';
import { enablePlainRpcStrings } from '../setup';
import { isDefined } from '../utils/nonNull';

export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown {
if (!data) {
return undefined;
} else if (isDefined(data.string)) {
if (enablePlainRpcStrings) {
return data.string;
}
return tryJsonParse(data.string);
} else if (isDefined(data.json)) {
return JSON.parse(data.json);
Expand All @@ -26,6 +30,9 @@ export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown
} else if (data.collectionBytes && isDefined(data.collectionBytes.bytes)) {
return data.collectionBytes.bytes.map((d) => Buffer.from(d));
} else if (data.collectionString && isDefined(data.collectionString.string)) {
if (enablePlainRpcStrings) {
return data.collectionString.string;
}
return data.collectionString.string.map(tryJsonParse);
} else if (data.collectionDouble && isDefined(data.collectionDouble.double)) {
return data.collectionDouble.double;
Expand Down
5 changes: 5 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function lockSetup(): void {
}

export let enableHttpStream = false;
export let enablePlainRpcStrings = false;
export const capabilities: Record<string, string> = {};

export function setup(opts: SetupOptions): void {
Expand All @@ -33,6 +34,10 @@ export function setup(opts: SetupOptions): void {
enableHttpStream = opts.enableHttpStream;
}

if (isDefined(opts.enablePlainRpcStrings)) {
enablePlainRpcStrings = opts.enablePlainRpcStrings;
}

if (opts.capabilities) {
for (let [key, val] of Object.entries(opts.capabilities)) {
if (isDefined(val)) {
Expand Down
33 changes: 33 additions & 0 deletions test/converters/fromRpcTypedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Long = require('long');
import { ResourceFactoryResolver } from '@azure/functions-extensions-base';
import sinon = require('sinon');
import { RpcTypedData } from '@azure/functions-core';
import { setup } from '../../src/setup';

describe('fromRpcTypedData', () => {
it('null', () => {
Expand All @@ -26,6 +27,20 @@ describe('fromRpcTypedData', () => {
expect(fromRpcTypedData({ string: 'false' })).to.equal(false);
expect(fromRpcTypedData({ string: '1' })).to.equal(1);
expect(fromRpcTypedData({ string: '0' })).to.equal(0);

setup({ enablePlainRpcStrings: true });
try {
expect(fromRpcTypedData({ string: 'test' })).to.equal('test');
expect(fromRpcTypedData({ string: '{ "a": "b" }' })).to.equal('{ "a": "b" }');
expect(fromRpcTypedData({ string: '{ "a": "b" ' })).to.equal('{ "a": "b" ');
expect(fromRpcTypedData({ string: '[1,2]' })).to.equal('[1,2]');
expect(fromRpcTypedData({ string: 'true' })).to.equal('true');
expect(fromRpcTypedData({ string: 'false' })).to.equal('false');
expect(fromRpcTypedData({ string: '1' })).to.equal('1');
expect(fromRpcTypedData({ string: '0' })).to.equal('0');
} finally {
setup({ enablePlainRpcStrings: false });
}
});

it('json', () => {
Expand Down Expand Up @@ -99,6 +114,24 @@ describe('fromRpcTypedData', () => {
'{"a": "b"',
'test2',
]);

setup({ enablePlainRpcStrings: true });
try {
expect(fromRpcTypedData({ collectionString: { string: ['test1', 'test2'] } })).to.deep.equal([
'test1',
'test2',
]);
expect(fromRpcTypedData({ collectionString: { string: ['{"a": "b"}', 'test2'] } })).to.deep.equal([
'{"a": "b"}',
'test2',
]);
expect(fromRpcTypedData({ collectionString: { string: ['{"a": "b"', 'test2'] } })).to.deep.equal([
'{"a": "b"',
'test2',
]);
} finally {
setup({ enablePlainRpcStrings: false });
}
});

it('collectionDouble', () => {
Expand Down
21 changes: 20 additions & 1 deletion test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import 'mocha';
import { expect } from 'chai';
import { capabilities, enableHttpStream, setup } from '../src/setup';
import { capabilities, enableHttpStream, enablePlainRpcStrings, setup } from '../src/setup';

describe('setup', () => {
it('enableHttpStream', () => {
Expand All @@ -25,6 +25,25 @@ describe('setup', () => {
expect(enableHttpStream).to.equal(false);
});

it('enablePlainRpcStrings', () => {
// default
expect(enablePlainRpcStrings).to.equal(false);

// set to true
setup({ enablePlainRpcStrings: true });
expect(enablePlainRpcStrings).to.equal(true);

// don't change if not explicitly set
setup({});
expect(enablePlainRpcStrings).to.equal(true);
setup({ capabilities: {} });
expect(enablePlainRpcStrings).to.equal(true);

// set to false
setup({ enablePlainRpcStrings: false });
expect(enablePlainRpcStrings).to.equal(false);
});

it('capabilities', () => {
// default
expect(capabilities).to.deep.equal({});
Expand Down
10 changes: 10 additions & 0 deletions test/types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import type { CosmosDBv4ChangeFeedMode } from '../../types';
import type { SetupOptions } from '../../types';

// This file will be compiled by multiple versions of TypeScript as decribed in ./test/TypesTests.ts to verify there are no errors
interface TodoItem {
Expand Down Expand Up @@ -92,6 +93,13 @@ type IsInvalidHandlerAssignableToAvad = InvalidAvadHandler extends ExpectedAvadH
const validHandlerAssignableToAvad: IsValidHandlerAssignableToAvad = true;
const invalidHandlerAssignableToAvad: IsInvalidHandlerAssignableToAvad = false;

const setupOptions: SetupOptions = {
enableHttpStream: false,
capabilities: { a: 'b', c: false },
enablePlainRpcStrings: false,
};
const emptySetupOptions: SetupOptions = {};

void latestVersionOptions;
void allVersionsAndDeletesOptions;
void deprecatedFullFidelityOptions;
Expand All @@ -101,3 +109,5 @@ void invalidServiceBusTopicTriggerOptions;
void registerServiceBusTopic;
void validHandlerAssignableToAvad;
void invalidHandlerAssignableToAvad;
void setupOptions;
void emptySetupOptions;
5 changes: 5 additions & 0 deletions types/setup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export interface SetupOptions {
* This will be merged with existing capabilities specified by the Node.js worker and library.
*/
capabilities?: Record<string, string | boolean>;

/**
* Don't convert RPC strings to JSON just because they have the shape of valid JSON.
*/
enablePlainRpcStrings?: boolean;
}