fix: do not modify incoming event's specversion (#419)
Even if the specversion is totally invalid, we should not change the value received in an incoming `Message`. Previously we defaulted to 1.0 if we did not recognize the version number. This commit changes that, leaving the value unmodified. We default to parsing this mystery event with the 1.0 spec. When the event is validated with `event.validate()` we return `false`. One additional small change to eliminate a prettier warning about `parer` being previously declared. Fixes: https://github.com/cloudevents/sdk-javascript/issues/332 Fixes: https://github.com/cloudevents/sdk-javascript/issues/333 Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
parent
7f6b658858
commit
7c05adee7b
|
@ -87,11 +87,8 @@ export function isEvent(message: Message): boolean {
|
||||||
export function deserialize(message: Message): CloudEvent {
|
export function deserialize(message: Message): CloudEvent {
|
||||||
const cleanHeaders: Headers = sanitize(message.headers);
|
const cleanHeaders: Headers = sanitize(message.headers);
|
||||||
const mode: Mode = getMode(cleanHeaders);
|
const mode: Mode = getMode(cleanHeaders);
|
||||||
let version = getVersion(mode, cleanHeaders, message.body);
|
const version = getVersion(mode, cleanHeaders, message.body);
|
||||||
if (version !== Version.V03 && version !== Version.V1) {
|
|
||||||
console.error(`Unknown spec version ${version}. Default to ${Version.V1}`);
|
|
||||||
version = Version.V1;
|
|
||||||
}
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case Mode.BINARY:
|
case Mode.BINARY:
|
||||||
return parseBinary(message, version);
|
return parseBinary(message, version);
|
||||||
|
@ -160,7 +157,7 @@ function parseBinary(message: Message, version: Version): CloudEvent {
|
||||||
const sanitizedHeaders = sanitize(headers);
|
const sanitizedHeaders = sanitize(headers);
|
||||||
|
|
||||||
const eventObj: { [key: string]: unknown | string | Record<string, unknown> } = {};
|
const eventObj: { [key: string]: unknown | string | Record<string, unknown> } = {};
|
||||||
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1binaryParsers : v03binaryParsers;
|
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03binaryParsers : v1binaryParsers;
|
||||||
|
|
||||||
for (const header in parserMap) {
|
for (const header in parserMap) {
|
||||||
if (sanitizedHeaders[header]) {
|
if (sanitizedHeaders[header]) {
|
||||||
|
@ -218,13 +215,13 @@ function parseStructured(message: Message, version: Version): CloudEvent {
|
||||||
const incoming = { ...(parser.parse(payload as string) as Record<string, unknown>) };
|
const incoming = { ...(parser.parse(payload as string) as Record<string, unknown>) };
|
||||||
|
|
||||||
const eventObj: { [key: string]: unknown } = {};
|
const eventObj: { [key: string]: unknown } = {};
|
||||||
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1structuredParsers : v03structuredParsers;
|
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03structuredParsers : v1structuredParsers;
|
||||||
|
|
||||||
for (const key in parserMap) {
|
for (const key in parserMap) {
|
||||||
const property = incoming[key];
|
const property = incoming[key];
|
||||||
if (property) {
|
if (property) {
|
||||||
const parser: MappedParser = parserMap[key];
|
const mappedParser: MappedParser = parserMap[key];
|
||||||
eventObj[parser.name] = parser.parser.parse(property as string);
|
eventObj[mappedParser.name] = mappedParser.parser.parse(property as string);
|
||||||
}
|
}
|
||||||
delete incoming[key];
|
delete incoming[key];
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,9 @@ describe("HTTP transport", () => {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
expect(HTTP.isEvent(message)).to.be.true;
|
expect(HTTP.isEvent(message)).to.be.true;
|
||||||
expect(HTTP.toEvent(message)).not.to.throw;
|
const event: CloudEvent = HTTP.toEvent(message);
|
||||||
|
expect(event.specversion).to.equal("11.8");
|
||||||
|
expect(event.validate()).to.be.false;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Can detect CloudEvent structured Messages with weird versions", () => {
|
it("Can detect CloudEvent structured Messages with weird versions", () => {
|
||||||
|
|
Loading…
Reference in New Issue