Skip to content

Commit d836b06

Browse files
authored
lib: expose constants as a top-level export (#161)
This commit pulls the constants up from the lib/bindings/http/constants.js and exports them in the top level index.js. There are some elements of the API where we expect users to provide constant values, and this makes it easier for them to be sure the values they provide are what is expected. I've also added two new constants: `BINARY` and `STRUCTURED`. Signed-off-by: Lance Ball <[email protected]>
1 parent db42ad8 commit d836b06

File tree

7 files changed

+211
-12
lines changed

7 files changed

+211
-12
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const CloudEvent = require("./lib/cloudevent.js");
22
const HTTPReceiver = require("./lib/bindings/http/http_receiver.js");
33
const HTTPEmitter = require("./lib/bindings/http/http_emitter.js");
4+
const Constants = require("./lib/bindings/http/constants.js");
45

56
module.exports = {
67
CloudEvent,
78
HTTPReceiver,
8-
HTTPEmitter
9+
HTTPEmitter,
10+
Constants
911
};

lib/bindings/http/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module.exports = Object.freeze({
33
HEADERS: "headers",
44
CHARSET_DEFAULT: "utf-8",
55

6+
BINARY: "binary",
7+
STRUCTURED: "structured",
8+
69
SPEC_V03: "0.3",
710
SPEC_V1: "1.0",
811

lib/bindings/http/http_receiver.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const V1Binary = require("./receiver_binary_1.js");
44
const V1Structured = require("./receiver_structured_1.js");
55
const ValidationError = require("../../validation_error.js");
66
const {
7+
BINARY,
8+
STRUCTURED,
79
SPEC_V03,
810
SPEC_V1,
911
HEADER_CONTENT_TYPE,
@@ -59,16 +61,16 @@ class HTTPReceiver {
5961
function getMode(headers) {
6062
const contentType = headers[HEADER_CONTENT_TYPE];
6163
if (contentType && contentType.startsWith(MIME_CE)) {
62-
return "structured";
64+
return STRUCTURED;
6365
}
6466
if (headers[BINARY_HEADERS_1.ID]) {
65-
return "binary";
67+
return BINARY;
6668
}
6769
throw new ValidationError("no cloud event detected");
6870
}
6971

7072
function getVersion(mode, headers, body) {
71-
if (mode === "binary") {
73+
if (mode === BINARY) {
7274
// Check the headers for the version
7375
const versionHeader = headers[DEFAULT_SPEC_VERSION_HEADER];
7476
if (versionHeader) {

lib/bindings/http/unmarshaller.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ const {
33
MIME_CE,
44
MIME_CE_JSON,
55
MIME_JSON,
6-
MIME_OCTET_STREAM
6+
MIME_OCTET_STREAM,
7+
BINARY,
8+
STRUCTURED
79
} = require("./constants.js");
810
const Commons = require("./commons.js");
911
const ValidationError = require("../../validation_error.js");
1012

11-
const STRUCTURED = "structured";
12-
const BINARY = "binary";
13-
1413
const allowedBinaryContentTypes = [
1514
MIME_JSON,
1615
MIME_OCTET_STREAM

test/bindings/http/unmarshaller_0_3_tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const schemaurl = "http://cloudevents.io/schema.json";
1111
const subject = "subject.ext";
1212
const {
1313
BINARY_HEADERS_03,
14-
HEADER_CONTENT_TYPE
14+
HEADER_CONTENT_TYPE,
15+
BINARY
1516
} = require("../../../lib/bindings/http/constants.js");
1617

1718
const ceContentType = "application/json";
@@ -182,7 +183,7 @@ describe("HTTP Transport Binding Unmarshaller for CloudEvents v0.3", () => {
182183
[BINARY_HEADERS_03.TIME]: "2019-06-16T11:42:00Z",
183184
[BINARY_HEADERS_03.SCHEMA_URL]: "http://schema.registry/v1",
184185
[HEADER_CONTENT_TYPE]: "application/json",
185-
[BINARY_HEADERS_03.CONTENT_ENCONDING]: "binary"
186+
[BINARY_HEADERS_03.CONTENT_ENCONDING]: BINARY
186187
};
187188

188189
expect(() => un.unmarshall(payload, attributes)).to

test/constants_test.js

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
const expect = require("chai").expect;
2+
3+
const {
4+
HEADERS,
5+
CHARSET_DEFAULT,
6+
BINARY,
7+
STRUCTURED,
8+
SPEC_V03,
9+
SPEC_V1,
10+
DEFAULT_SPEC_VERSION_HEADER,
11+
ENCODING_BASE64,
12+
DATA_ATTRIBUTE,
13+
MIME_JSON,
14+
MIME_OCTET_STREAM,
15+
MIME_CE,
16+
MIME_CE_JSON,
17+
HEADER_CONTENT_TYPE,
18+
DEFAULT_CONTENT_TYPE,
19+
DEFAULT_CE_CONTENT_TYPE,
20+
BINARY_HEADERS_03,
21+
STRUCTURED_ATTRS_03,
22+
BINARY_HEADERS_1,
23+
STRUCTURED_ATTRS_1
24+
} = require("../").Constants;
25+
26+
describe("Constants exposed by top level exports", () => {
27+
it("Exports a HEADERS constant", () => {
28+
expect(HEADERS).to.equal("headers");
29+
});
30+
it("Exports a CHARSET_DEFAULT constant", () => {
31+
expect(CHARSET_DEFAULT).to.equal("utf-8");
32+
});
33+
it("Exports a BINARY constant", () => {
34+
expect(BINARY).to.equal("binary");
35+
});
36+
it("Exports a STRUCTURED constant", () => {
37+
expect(STRUCTURED).to.equal("structured");
38+
});
39+
it("Exports a SPEC_V03 constant", () => {
40+
expect(SPEC_V03).to.equal("0.3");
41+
});
42+
it("Exports a SPEC_V1 constant", () => {
43+
expect(SPEC_V1).to.equal("1.0");
44+
});
45+
it("Exports a DEFAULT_SPEC_VERSION_HEADER constant", () => {
46+
expect(DEFAULT_SPEC_VERSION_HEADER).to.equal("ce-specversion");
47+
});
48+
it("Exports an ENCODING_BASE64 constant", () => {
49+
expect(ENCODING_BASE64).to.equal("base64");
50+
});
51+
it("Exports a DATA_ATTRIBUTE constant", () => {
52+
expect(DATA_ATTRIBUTE).to.equal("data");
53+
});
54+
it("Exports a MIME_JSON constant", () => {
55+
expect(MIME_JSON).to.equal("application/json");
56+
});
57+
it("Exports a MIME_OCTET_STREAM constant", () => {
58+
expect(MIME_OCTET_STREAM).to.equal("application/octet-stream");
59+
});
60+
it("Exports a MIME_CE constant", () => {
61+
expect(MIME_CE).to.equal("application/cloudevents");
62+
});
63+
it("Exports a MIME_CE_JSON constant", () => {
64+
expect(MIME_CE_JSON).to.equal("application/cloudevents+json");
65+
});
66+
it("Exports a HEADER_CONTENT_TYPE constant", () => {
67+
expect(HEADER_CONTENT_TYPE).to.equal("content-type");
68+
});
69+
it("Exports a DEFAULT_CONTENT_TYPE constant", () => {
70+
expect(DEFAULT_CONTENT_TYPE).to.equal(`${MIME_JSON}; charset=${CHARSET_DEFAULT}`);
71+
});
72+
it("Exports a DEFAULT_CE_CONTENT_TYPE constant", () => {
73+
expect(DEFAULT_CE_CONTENT_TYPE).to.equal(`${MIME_CE_JSON}; charset=${CHARSET_DEFAULT}`);
74+
});
75+
describe("V0.3 binary headers constants", () => {
76+
it("Provides a TYPE header", () => {
77+
expect(BINARY_HEADERS_03.TYPE).to.equal("ce-type");
78+
});
79+
it("Provides a SPEC_VERSION header", () => {
80+
expect(BINARY_HEADERS_03.SPEC_VERSION).to.equal("ce-specversion");
81+
});
82+
it("Provides a SOURCE header", () => {
83+
expect(BINARY_HEADERS_03.SOURCE).to.equal("ce-source");
84+
});
85+
it("Provides an ID header", () => {
86+
expect(BINARY_HEADERS_03.ID).to.equal("ce-id");
87+
});
88+
it("Provides a TIME header", () => {
89+
expect(BINARY_HEADERS_03.TIME).to.equal("ce-time");
90+
});
91+
it("Provides a SCHEMA_URL header", () => {
92+
expect(BINARY_HEADERS_03.SCHEMA_URL).to.equal("ce-schemaurl");
93+
});
94+
it("Provides a CONTENT_ENCODING header", () => {
95+
expect(BINARY_HEADERS_03.CONTENT_ENCONDING).to.equal("ce-datacontentencoding");
96+
});
97+
it("Provides a SUBJECT header", () => {
98+
expect(BINARY_HEADERS_03.SUBJECT).to.equal("ce-subject");
99+
});
100+
it("Provides an EXTENSIONS_PREFIX constant", () => {
101+
expect(BINARY_HEADERS_03.EXTENSIONS_PREFIX).to.equal("ce-");
102+
});
103+
});
104+
describe("V0.3 structured attributes constants", () => {
105+
it("Provides a TYPE attribute", () => {
106+
expect(STRUCTURED_ATTRS_03.TYPE).to.equal("type");
107+
});
108+
it("Provides a SPEC_VERSION attribute", () => {
109+
expect(STRUCTURED_ATTRS_03.SPEC_VERSION).to.equal("specversion");
110+
});
111+
it("Provides a SOURCE attribute", () => {
112+
expect(STRUCTURED_ATTRS_03.SOURCE).to.equal("source");
113+
});
114+
it("Provides an ID attribute", () => {
115+
expect(STRUCTURED_ATTRS_03.ID).to.equal("id");
116+
});
117+
it("Provides a TIME attribute", () => {
118+
expect(STRUCTURED_ATTRS_03.TIME).to.equal("time");
119+
});
120+
it("Provides a SCHEMA_URL attribute", () => {
121+
expect(STRUCTURED_ATTRS_03.SCHEMA_URL).to.equal("schemaurl");
122+
});
123+
it("Provides a CONTENT_ENCODING attribute", () => {
124+
expect(STRUCTURED_ATTRS_03.CONTENT_ENCONDING).to.equal("datacontentencoding");
125+
});
126+
it("Provides a SUBJECT attribute", () => {
127+
expect(STRUCTURED_ATTRS_03.SUBJECT).to.equal("subject");
128+
});
129+
it("Provides a DATA attribute", () => {
130+
expect(STRUCTURED_ATTRS_03.DATA).to.equal("data");
131+
});
132+
});
133+
describe("V01 binary headers constants", () => {
134+
it("Provides a TYPE header", () => {
135+
expect(BINARY_HEADERS_1.TYPE).to.equal("ce-type");
136+
});
137+
it("Provides a SPEC_VERSION header", () => {
138+
expect(BINARY_HEADERS_1.SPEC_VERSION).to.equal("ce-specversion");
139+
});
140+
it("Provides a SOURCE header", () => {
141+
expect(BINARY_HEADERS_1.SOURCE).to.equal("ce-source");
142+
});
143+
it("Provides an ID header", () => {
144+
expect(BINARY_HEADERS_1.ID).to.equal("ce-id");
145+
});
146+
it("Provides a TIME header", () => {
147+
expect(BINARY_HEADERS_1.TIME).to.equal("ce-time");
148+
});
149+
it("Provides a DATA_SCHEMA header", () => {
150+
expect(BINARY_HEADERS_1.DATA_SCHEMA).to.equal("ce-dataschema");
151+
});
152+
it("Provides a SUBJECT header", () => {
153+
expect(BINARY_HEADERS_1.SUBJECT).to.equal("ce-subject");
154+
});
155+
it("Provides an EXTENSIONS_PREFIX constant", () => {
156+
expect(BINARY_HEADERS_1.EXTENSIONS_PREFIX).to.equal("ce-");
157+
});
158+
});
159+
describe("V1 structured attributes constants", () => {
160+
it("Provides a TYPE attribute", () => {
161+
expect(STRUCTURED_ATTRS_1.TYPE).to.equal("type");
162+
});
163+
it("Provides a SPEC_VERSION attribute", () => {
164+
expect(STRUCTURED_ATTRS_1.SPEC_VERSION).to.equal("specversion");
165+
});
166+
it("Provides a SOURCE attribute", () => {
167+
expect(STRUCTURED_ATTRS_1.SOURCE).to.equal("source");
168+
});
169+
it("Provides an ID attribute", () => {
170+
expect(STRUCTURED_ATTRS_1.ID).to.equal("id");
171+
});
172+
it("Provides a TIME attribute", () => {
173+
expect(STRUCTURED_ATTRS_1.TIME).to.equal("time");
174+
});
175+
it("Provides a DATA_SCHEMA attribute", () => {
176+
expect(STRUCTURED_ATTRS_1.DATA_SCHEMA).to.equal("dataschema");
177+
});
178+
it("Provides a CONTENT_TYPE attribute", () => {
179+
expect(STRUCTURED_ATTRS_1.CONTENT_TYPE).to.equal("datacontenttype");
180+
});
181+
it("Provides a SUBJECT attribute", () => {
182+
expect(STRUCTURED_ATTRS_1.SUBJECT).to.equal("subject");
183+
});
184+
it("Provides a DATA attribute", () => {
185+
expect(STRUCTURED_ATTRS_1.DATA).to.equal("data");
186+
});
187+
it("Provides a DATA_BASE64 attribute", () => {
188+
expect(STRUCTURED_ATTRS_1.DATA_BASE64).to.equal("data_base64");
189+
});
190+
});
191+
});

test/spec_0_3_tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const { CloudEvent } = require("../index.js");
44
const {
55
MIME_JSON,
66
ENCODING_BASE64,
7-
SPEC_V03
7+
SPEC_V03,
8+
BINARY
89
} = require("../lib/bindings/http/constants.js");
910
const ValidationError = require("../lib/validation_error.js");
1011

@@ -155,7 +156,7 @@ describe("CloudEvents Spec v0.3", () => {
155156
it("should throw an error when is a unsupported encoding", () => {
156157
cloudevent
157158
.data("Y2xvdWRldmVudHMK")
158-
.dataContentEncoding("binary");
159+
.dataContentEncoding(BINARY);
159160
expect(cloudevent.format.bind(cloudevent))
160161
.to.throw(ValidationError, "invalid payload");
161162
delete cloudevent.spec.payload.datacontentencoding;

0 commit comments

Comments
 (0)