faas-js-runtime
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -5,2 +5,14 @@ # Changelog | ||
## [0.5.0](https://www.github.com/boson-project/faas-js-runtime/compare/v0.4.0...v0.5.0) (2020-10-30) | ||
### Features | ||
* handle CloudEvent and Message responses from function invocation ([#68](https://www.github.com/boson-project/faas-js-runtime/issues/68)) ([351197f](https://www.github.com/boson-project/faas-js-runtime/commit/351197f7258e8612fc4ad1a1d43d3952ba87f7f6)) | ||
### Bug Fixes | ||
* handle cloudevents that have no data ([#67](https://www.github.com/boson-project/faas-js-runtime/issues/67)) ([84d402d](https://www.github.com/boson-project/faas-js-runtime/commit/84d402d32301ce379f819b670deb7348cc2a7d1b)) | ||
## [0.4.0](https://www.github.com/boson-project/faas-js-runtime/compare/v0.3.0...v0.4.0) (2020-10-06) | ||
@@ -7,0 +19,0 @@ |
@@ -1,2 +0,2 @@ | ||
const Spec = require('../lib/ce-constants.js').Spec; | ||
const { CloudEvent } = require('cloudevents'); | ||
@@ -27,10 +27,7 @@ class Context { | ||
constructor(response) { | ||
this.#response = response; | ||
if (!this.#response.headers) { | ||
this.#response.headers = []; | ||
} | ||
this.#response = { data: response }; | ||
} | ||
version(version) { | ||
this.#response.headers[Spec.version] = version; | ||
this.#response.specversion = version; | ||
return this; | ||
@@ -40,3 +37,3 @@ } | ||
id(id) { | ||
this.#response.headers[Spec.id] = id; | ||
this.#response.id = id; | ||
return this; | ||
@@ -46,3 +43,3 @@ } | ||
type(type) { | ||
this.#response.headers[Spec.type] = type; | ||
this.#response.type = type; | ||
return this; | ||
@@ -52,3 +49,3 @@ } | ||
source(source) { | ||
this.#response.headers[Spec.source] = source; | ||
this.#response.source = source; | ||
return this; | ||
@@ -58,3 +55,3 @@ } | ||
response() { | ||
return this.#response; | ||
return new CloudEvent(this.#response); | ||
} | ||
@@ -61,0 +58,0 @@ } |
'use strict'; | ||
const { CloudEvent, HTTP } = require('cloudevents'); | ||
@@ -10,6 +11,6 @@ module.exports = function invoker(func) { | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf8', | ||
'Access-Control-Allow-Methods': | ||
'content-type': 'application/json; charset=utf8', | ||
'access-control-allow-methods': | ||
'OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH', | ||
'Access-Control-Allow-Origin': '*' | ||
'access-control-allow-origin': '*' | ||
} | ||
@@ -19,3 +20,3 @@ }; | ||
try { | ||
if (context.cloudevent && context.cloudevent.data) { | ||
if (context.cloudevent) { | ||
// If there is a cloud event, provide the data | ||
@@ -30,7 +31,3 @@ // as the first parameter | ||
} catch (err) { | ||
log.error(err); | ||
payload.response = { | ||
statusCode: err.code ? err.code : 500, | ||
statusMessage: err.message | ||
}; | ||
payload.response = handleError(err, log); | ||
} | ||
@@ -45,2 +42,26 @@ | ||
// Check for user defined headers | ||
if (typeof payload.response.headers === 'object') { | ||
const headers = {}; | ||
// normalize the headers as lowercase | ||
for (const header in payload.response.headers) { | ||
headers[header.toLocaleLowerCase()] = payload.response.headers[header]; | ||
} | ||
payload.headers = { ...payload.headers, ...headers }; | ||
delete payload.response.headers; | ||
} | ||
// If the response is a CloudEvent, we need to convert it | ||
// to a Message first and respond with the headers/body | ||
if (payload.response instanceof CloudEvent) { | ||
try { | ||
const message = HTTP.binary(payload.response); | ||
payload.headers = {...payload.headers, ...message.headers}; | ||
payload.response = message.body; | ||
} catch (err) { | ||
payload.response = handleError(err, log); | ||
return payload; | ||
} | ||
} | ||
// Check for user defined status code | ||
@@ -52,6 +73,6 @@ if (payload.response.statusCode) { | ||
// Check for user defined headers | ||
if (typeof payload.response.headers === 'object') { | ||
Object.assign(payload.headers, payload.response.headers); | ||
delete payload.response.headers; | ||
// Check for user supplied body | ||
if (payload.response.body !== undefined) { | ||
payload.response = payload.response.body; | ||
delete payload.response.body; | ||
} | ||
@@ -61,1 +82,9 @@ return payload; | ||
}; | ||
function handleError(err, log) { | ||
log.error(err); | ||
return { | ||
statusCode: err.code ? err.code : 500, | ||
statusMessage: err.message | ||
}; | ||
} |
@@ -25,3 +25,4 @@ 'use strict'; | ||
function sendReply(reply, payload) { | ||
if (payload.headers['Content-Type'].startsWith('text/plain')) { | ||
const contentType = payload.headers['content-type']; | ||
if (contentType.startsWith('text/plain') && (typeof payload.response !== 'string')) { | ||
payload.response = JSON.stringify(payload.response); | ||
@@ -28,0 +29,0 @@ } |
{ | ||
"name": "faas-js-runtime", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"repository": { | ||
@@ -24,3 +24,3 @@ "type": "git", | ||
"chalk": "^4.1.0", | ||
"cloudevents": "^3.1.0", | ||
"cloudevents": "^3.2.0", | ||
"commander": "^6.1.0", | ||
@@ -27,0 +27,0 @@ "death": "^1.1.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30406
12
320
Updatedcloudevents@^3.2.0