diff --git a/lib/invoker.js b/lib/invoker.js index f92d4ca..ce3a81c 100644 --- a/lib/invoker.js +++ b/lib/invoker.js @@ -26,7 +26,7 @@ module.exports = function invoker(func) { // If the response is a CloudEvent, we need to convert it // to a Message first and respond with the headers/body - if (fnReturn instanceof CloudEvent) { + if (fnReturn instanceof CloudEvent || fnReturn.constructor?.name === 'CloudEvent') { try { const message = HTTP.binary(fnReturn); payload.headers = {...payload.headers, ...message.headers}; diff --git a/lib/types.d.ts b/lib/types.d.ts index 2376003..9268ad7 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -5,12 +5,12 @@ import { CloudEvent } from 'cloudevents'; /** * CloudEventFunction describes the function signature for a function that accepts CloudEvents. */ - export interface CloudEventFunction { - (context: Context, event: CloudEvent): CloudEventFunctionReturn; +export interface CloudEventFunction { + (context: Context, event?: CloudEvent): CloudEventFunctionReturn; } // CloudEventFunctionReturn is the return type for a CloudEventFunction. -export type CloudEventFunctionReturn = Promise | CloudEvent | HTTPFunctionReturn; +export type CloudEventFunctionReturn = Promise> | CloudEvent | HTTPFunctionReturn; /** * HTTPFunction describes the function signature for a function that handles @@ -50,7 +50,7 @@ export interface Context { httpVersion: string; httpVersionMajor: number; httpVersionMinor: number; - cloudevent: CloudEvent; + cloudevent: CloudEvent; cloudEventResponse(data: string|object): CloudEventResponse; } diff --git a/package-lock.json b/package-lock.json index 1d6b38c..801f56c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4219,7 +4219,8 @@ }, "node_modules/safe-buffer": { "version": "5.1.2", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true }, "node_modules/safe-regex2": { "version": "2.0.0", @@ -4864,15 +4865,14 @@ } }, "node_modules/util": { - "version": "0.12.4", - "resolved": "https://registry.npmjs.org/util/-/util-0.12.4.tgz", - "integrity": "sha512-bxZ9qtSlGUWSOy9Qa9Xgk11kSslpuZwaxCg4sNIDj6FLucDab2JxnHwyNTCpHMtK1MjoQiWQ6DiUMZYbSrO+Sw==", + "version": "0.12.5", + "resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz", + "integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==", "dependencies": { "inherits": "^2.0.3", "is-arguments": "^1.0.4", "is-generator-function": "^1.0.7", "is-typed-array": "^1.1.3", - "safe-buffer": "^5.1.2", "which-typed-array": "^1.1.2" } }, @@ -8150,7 +8150,8 @@ }, "safe-buffer": { "version": "5.1.2", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true }, "safe-regex2": { "version": "2.0.0", @@ -8649,15 +8650,14 @@ } }, "util": { - "version": "0.12.4", - "resolved": "https://registry.npmjs.org/util/-/util-0.12.4.tgz", - "integrity": "sha512-bxZ9qtSlGUWSOy9Qa9Xgk11kSslpuZwaxCg4sNIDj6FLucDab2JxnHwyNTCpHMtK1MjoQiWQ6DiUMZYbSrO+Sw==", + "version": "0.12.5", + "resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz", + "integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==", "requires": { "inherits": "^2.0.3", "is-arguments": "^1.0.4", "is-generator-function": "^1.0.7", "is-typed-array": "^1.1.3", - "safe-buffer": "^5.1.2", "which-typed-array": "^1.1.2" } }, diff --git a/test/types/context.test-d.ts b/test/types/context.test-d.ts index 07318fa..486351b 100644 --- a/test/types/context.test-d.ts +++ b/test/types/context.test-d.ts @@ -18,7 +18,7 @@ expectType(context.method); expectType(context.httpVersion); expectType(context.httpVersionMajor); expectType(context.httpVersionMinor); -expectType(context.cloudevent); +expectType>(context.cloudevent); expectAssignable|string|undefined>(context.body); // CloudEventResponse diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index 7ce1fe4..13a65fe 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -6,10 +6,10 @@ import { CloudEvent } from 'cloudevents'; const fn: Invokable = function( context: Context, - cloudevent?: CloudEvent + cloudevent?: CloudEvent ) { expectType(context); - expectType(cloudevent); + expectType|undefined>(cloudevent); return undefined; };