8000 feat: precompile cloudevent schema (#471) · cloudevents/sdk-javascript@b13bde9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b13bde9

Browse files
authored
feat: precompile cloudevent schema (#471)
* 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 <lball@redhat.com>
1 parent 4d8f03f commit b13bde9

File tree

11 files changed

+275
-133
lines changed

11 files changed

+275
-133
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",

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ index.js
1313
/bundles
1414
/dist
1515
/docs
16+
src/schema/v1.js
1617

1718
# Runtime data
1819
pids

package-lock.json

Lines changed: 117 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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
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 .",
@@ -106,13 +109,12 @@
106109
},
107110
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
108111
"dependencies": {
109-
"ajv": "~6.12.3",
112+
"ajv": "^8.6.3",
110113
"util": "^0.12.4",
111114
"uuid": "~8.3.0"
112115
},
113116
"devDependencies": {
114117
"@cucumber/cucumber": "^8.0.0-rc.1",
115-
"@types/ajv": "^1.0.0",
116118
"@types/chai": "^4.2.11",
117119
"@types/cucumber": "^6.0.1",
118120
"@types/got": "^9.6.11",
@@ -122,6 +124,8 @@
122124
"@types/uuid": "^8.0.0",
123125
"@typescript-eslint/eslint-plugin": "^4.29.0",
124126
"@typescript-eslint/parser": "^4.29.0",
127+
"ajv-cli": "^5.0.0",
128+
"ajv-formats": "^2.1.1",
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)
0