From a3884160be55423f33a0162ba6dfd3af48455c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 9 Jun 2019 21:04:41 -0300 Subject: [PATCH] Umarchaller impl start MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/unmarshaller_0_2.js | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/bindings/http/unmarshaller_0_2.js diff --git a/lib/bindings/http/unmarshaller_0_2.js b/lib/bindings/http/unmarshaller_0_2.js new file mode 100644 index 0000000..6fdea1e --- /dev/null +++ b/lib/bindings/http/unmarshaller_0_2.js @@ -0,0 +1,40 @@ + +const content_type_header = "content-type"; +const ce_structured_content_type = "application/cloudevents"; + +/** + * Level 0 of validation: is that string? is that JSON? + */ +function validate_and_parse_as_json(payload) { + var json = payload; + + if( (typeof payload) === "string"){ + try { + json = JSON.parse(payload); + + }catch(e) { + throw {message: "Invalid payload", errors: e}; + } + + } else if( (typeof payload) !== "object"){ + // anything else + throw {message: "Invalid payload type, allowed are: string or object"}; + } + + return json; +} + +var Unmarshaller = function() { + +} + +Unmarshaller.prototype.unmarshall = function(payload, headers) { + + var valid0 = validate_and_parse_as_json(payload); + + //TODO binary or structured ? + //"Content-Type" + +} + +module.exports = Unmarshaller;