Skip to content

Commit

Permalink
chore: consolidate HTTP parsers and header maps into single files (#267)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant authored Jul 24, 2020
1 parent 1613595 commit 45850e3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/transport/http/binary_receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CloudEvent, Version } from "../..";
import { CloudEventV1, validateV1 } from "../../event/v1";
import { CloudEventV03, validateV03 } from "../../event/v03";
import { Headers, validate } from "./headers";
import { binaryParsers as v1Parsers } from "./v1/parsers";
import { binaryParsers as v03Parsers } from "./v03/parsers";
import { binaryParsers as v1Parsers } from "./v1";
import { binaryParsers as v03Parsers } from "./v03";
import { parserByContentType, MappedParser } from "../../parsers";
import { isString, isBase64, ValidationError, isStringOrObjectOrThrow } from "../../event/validation";
import CONSTANTS from "../../constants";
Expand Down
4 changes: 2 additions & 2 deletions src/transport/http/structured_receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CloudEvent, Version } from "../..";
import { Headers, sanitize } from "./headers";
import { Parser, JSONParser, MappedParser } from "../../parsers";
import { parserByContentType } from "../../parsers";
import { structuredParsers as v1Parsers } from "./v1/parsers";
import { structuredParsers as v03Parsers } from "./v03/parsers";
import { structuredParsers as v1Parsers } from "./v1";
import { structuredParsers as v03Parsers } from "./v03";
import { isString, isBase64, ValidationError, isStringOrObjectOrThrow } from "../../event/validation";
import { CloudEventV1, validateV1 } from "../../event/v1";
import { CloudEventV03, validateV03 } from "../../event/v03";
Expand Down
31 changes: 22 additions & 9 deletions src/transport/http/v03/parsers.ts → src/transport/http/v03.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { MappedParser, DateParser, PassThroughParser } from "../../../parsers";
import CONSTANTS from "../../../constants";
import { MappedParser, DateParser, PassThroughParser } from "../../parsers";
import CONSTANTS from "../../constants";

const passThrough = new PassThroughParser();
function parser(name: string, parser = passThrough): MappedParser {
return { name: name, parser: parser };
function parser(header: string, parser = new PassThroughParser()): MappedParser {
return { name: header, parser };
}

const binaryParsers: Record<string, MappedParser> = Object.freeze({
/**
* A utility Map used to retrieve the header names for a CloudEvent
* using the CloudEvent getter function.
*/
export const headerMap: Readonly<{ [key: string]: MappedParser }> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.CONTENT_TYPE]: parser(CONSTANTS.HEADER_CONTENT_TYPE),
[CONSTANTS.CE_ATTRIBUTES.SUBJECT]: parser(CONSTANTS.CE_HEADERS.SUBJECT),
[CONSTANTS.CE_ATTRIBUTES.TYPE]: parser(CONSTANTS.CE_HEADERS.TYPE),
[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]: parser(CONSTANTS.CE_HEADERS.SPEC_VERSION),
[CONSTANTS.CE_ATTRIBUTES.SOURCE]: parser(CONSTANTS.CE_HEADERS.SOURCE),
[CONSTANTS.CE_ATTRIBUTES.ID]: parser(CONSTANTS.CE_HEADERS.ID),
[CONSTANTS.CE_ATTRIBUTES.TIME]: parser(CONSTANTS.CE_HEADERS.TIME),
[CONSTANTS.STRUCTURED_ATTRS_03.CONTENT_ENCODING]: parser(CONSTANTS.BINARY_HEADERS_03.CONTENT_ENCODING),
[CONSTANTS.STRUCTURED_ATTRS_03.SCHEMA_URL]: parser(CONSTANTS.BINARY_HEADERS_03.SCHEMA_URL),
});

export const binaryParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_HEADERS.TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.TYPE),
[CONSTANTS.CE_HEADERS.SPEC_VERSION]: parser(CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION),
[CONSTANTS.CE_HEADERS.SOURCE]: parser(CONSTANTS.CE_ATTRIBUTES.SOURCE),
Expand All @@ -18,7 +33,7 @@ const binaryParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.HEADER_CONTENT_TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.CONTENT_TYPE),
});

const structuredParsers: Record<string, MappedParser> = Object.freeze({
export const structuredParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.TYPE),
[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]: parser(CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION),
[CONSTANTS.CE_ATTRIBUTES.SOURCE]: parser(CONSTANTS.CE_ATTRIBUTES.SOURCE),
Expand All @@ -30,5 +45,3 @@ const structuredParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.SUBJECT]: parser(CONSTANTS.CE_ATTRIBUTES.SUBJECT),
[CONSTANTS.CE_ATTRIBUTES.DATA]: parser(CONSTANTS.CE_ATTRIBUTES.DATA),
});

export { binaryParsers, structuredParsers };
23 changes: 0 additions & 23 deletions src/transport/http/v03/headers.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/transport/http/v03/index.ts

This file was deleted.

29 changes: 20 additions & 9 deletions src/transport/http/v1/parsers.ts → src/transport/http/v1.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { PassThroughParser, DateParser, MappedParser } from "../../../parsers";
import CONSTANTS from "../../../constants";
import { PassThroughParser, DateParser, MappedParser } from "../../parsers";
import CONSTANTS from "../../constants";

const passThrough = new PassThroughParser();

function parser(name: string, parser = passThrough): MappedParser {
function parser(name: string, parser = new PassThroughParser()): MappedParser {
return { name: name, parser: parser };
}

const binaryParsers: Record<string, MappedParser> = Object.freeze({
/**
* A utility Map used to retrieve the header names for a CloudEvent
* using the CloudEvent getter function.
*/
export const headerMap: Readonly<{ [key: string]: MappedParser }> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.CONTENT_TYPE]: parser(CONSTANTS.HEADER_CONTENT_TYPE),
[CONSTANTS.CE_ATTRIBUTES.SUBJECT]: parser(CONSTANTS.CE_HEADERS.SUBJECT),
[CONSTANTS.CE_ATTRIBUTES.TYPE]: parser(CONSTANTS.CE_HEADERS.TYPE),
[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]: parser(CONSTANTS.CE_HEADERS.SPEC_VERSION),
[CONSTANTS.CE_ATTRIBUTES.SOURCE]: parser(CONSTANTS.CE_HEADERS.SOURCE),
[CONSTANTS.CE_ATTRIBUTES.ID]: parser(CONSTANTS.CE_HEADERS.ID),
[CONSTANTS.CE_ATTRIBUTES.TIME]: parser(CONSTANTS.CE_HEADERS.TIME),
[CONSTANTS.STRUCTURED_ATTRS_1.DATA_SCHEMA]: parser(CONSTANTS.BINARY_HEADERS_1.DATA_SCHEMA),
});

export const binaryParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_HEADERS.TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.TYPE),
[CONSTANTS.CE_HEADERS.SPEC_VERSION]: parser(CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION),
[CONSTANTS.CE_HEADERS.SOURCE]: parser(CONSTANTS.CE_ATTRIBUTES.SOURCE),
Expand All @@ -19,7 +32,7 @@ const binaryParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.HEADER_CONTENT_TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.CONTENT_TYPE),
});

const structuredParsers: Record<string, MappedParser> = Object.freeze({
export const structuredParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.TYPE]: parser(CONSTANTS.CE_ATTRIBUTES.TYPE),
[CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION]: parser(CONSTANTS.CE_ATTRIBUTES.SPEC_VERSION),
[CONSTANTS.CE_ATTRIBUTES.SOURCE]: parser(CONSTANTS.CE_ATTRIBUTES.SOURCE),
Expand All @@ -31,5 +44,3 @@ const structuredParsers: Record<string, MappedParser> = Object.freeze({
[CONSTANTS.CE_ATTRIBUTES.DATA]: parser(CONSTANTS.CE_ATTRIBUTES.DATA),
[CONSTANTS.STRUCTURED_ATTRS_1.DATA_BASE64]: parser(CONSTANTS.STRUCTURED_ATTRS_1.DATA_BASE64),
});

export { structuredParsers, binaryParsers };
22 changes: 0 additions & 22 deletions src/transport/http/v1/headers.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/transport/http/v1/index.ts

This file was deleted.

0 comments on commit 45850e3

Please sign in to comment.