Skip to content

Commit 2e8cc9a

Browse files
committed
feat: precompile cloudevent schema
This commit modifies the build pipleline so that the cloudevent schema is precompiled for runtime validation. This eliminates the need to compile the schema at runtime, improving both performance and security. Fixes: #423 Signed-off-by: Lance Ball <[email protected]>
1 parent c3d9f39 commit 2e8cc9a

File tree

13 files changed

+275
-286
lines changed

13 files changed

+275
-286
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"plugins": [
1616
"header"
1717
],
18+
"ignorePatterns": ["**/schema/*"],
1819
"rules": {
1920
"no-var": "error",
2021
"standard/no-callback-literal": "off",

package-lock.json

Lines changed: 116 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"watch": "tsc --project tsconfig.json --watch",
8-
"build": "tsc --project tsconfig.json && tsc --project tsconfig.browser.json && webpack",
8+
"build:src": "tsc --project tsconfig.json",
9+
"build:browser": "tsc --project tsconfig.browser.json && webpack",
10+
"build:schema": "ajv compile -c ./src/schema/formats.js -s src/schema/cloudevent.json --strict-types false -o src/schema/v1.js",
11+
"build": "npm run build:schema && npm run build:src && npm run build:browser",
912
"lint": "npm run lint:md && npm run lint:js",
1013
"lint:js": "eslint 'src/**/*.{js,ts}' 'test/**/*.{js,ts}' cucumber.js",
1114
"lint:md": "remark .",
1215
"lint:fix": "eslint 'src/**/*.{js,ts}' 'test/**/*.{js,ts}' --fix",
13-
"pretest": "npm run lint && npm run conformance",
16+
"pretest": "npm run lint",
1417
"test": "mocha --require ts-node/register ./test/integration/**/*.ts",
1518
"conformance": "cucumber-js ./conformance/features/*-protocol-binding.feature -p default",
1619
"coverage": "nyc --reporter=lcov --reporter=text npm run test",
@@ -106,13 +109,13 @@
106109
},
107110
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
108111
"dependencies": {
109-
"ajv": "~6.12.3",
112+
"ajv": "^8.6.3",
113+
"ajv-formats": "^2.1.1",
110114
"util": "^0.12.4",
111115
"uuid": "~8.3.0"
112116
},
113117
"devDependencies": {
114118
"@cucumber/cucumber": "^8.0.0-rc.1",
115-
"@types/ajv": "^1.0.0",
116119
"@types/chai": "^4.2.11",
117120
"@types/cucumber": "^6.0.1",
118121
"@types/got": "^9.6.11",
@@ -122,6 +125,7 @@
122125
"@types/uuid": "^8.0.0",
123126
"@typescript-eslint/eslint-plugin": "^4.29.0",
124127
"@typescript-eslint/parser": "^4.29.0",
128+
"ajv-cli": "^5.0.0",
125129
"axios": "^0.21.3",
126130
"chai": "~4.2.0",
127131
"eslint": "^7.32.0",

src/event/schemas.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/event/spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import Ajv, { Options } from "ajv";
76
import { ValidationError } from "./validation";
87

98
import { CloudEventV1 } from "./interfaces";
10-
import { schemaV1 } from "./schemas";
119
import { Version } from "./cloudevent";
10+
import validate from "../schema/v1";
1211

13-
const ajv = new Ajv({ extendRefs: true } as Options);
14-
15-
// handle date-time format specially because a user could pass
16-
// Date().toString(), which is not spec compliant date-time format
17-
ajv.addFormat("js-date-time", function (dateTimeString) {
18-
const date = new Date(Date.parse(dateTimeString));
19-
return date.toString() !== "Invalid Date";
20-
});
21-
22-
const isValidAgainstSchemaV1 = ajv.compile(schemaV1);
2312

2413
export function validateCloudEvent<T>(event: CloudEventV1<T>): boolean {
2514
if (event.specversion === Version.V1) {
26-
if (!isValidAgainstSchemaV1(event)) {
27-
throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors);
15+
if (!validate(event)) {
16+
throw new ValidationError("invalid payload", (validate as any).errors);
2817
}
2918
} else {
3019
return false;

0 commit comments

Comments
 (0)