-
Notifications
You must be signed in to change notification settings - Fork 0
/
canon-td.js
94 lines (83 loc) · 3.4 KB
/
canon-td.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require("fs");
const Ajv = require("ajv");
const canonifyJSON = require("canonical-json");
if (process.argv.length < 3) {
console.error("Please provide a file containing TD to canonicalize as an argument");
process.exit(-1);
}
const td = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const schema = require("./td-json-schema-validation.json");
const ajv = new Ajv();
if (!ajv.validate(schema, td)) {
// Concatenate error messages in the following format and throw an error
// 1) Error 1...
// 2) Error 2...
throw Error(ajv.errors.map(
(error, index) => `${index + 1}) ${error["message"]}`)
.join("\n"));
}
// Interactions
const PROPERTIES = "properties";
const ACTIONS = "actions";
const EVENTS = "events";
function addSecurityDefaults(schemes, key, value) {
for (const val of Object.values(td["securityDefinitions"])) {
if (schemes.includes(val["scheme"]) && val[key] === undefined) {
val[key] = value;
}
}
}
function addFormDefaults(interactions) {
for (const interaction of Object.values(td[interactions] ?? {})) {
for (const form of Object.values(interaction["forms"])) {
// Add contentType
if (form["contentType"] === undefined) {
form["contentType"] = "application/json";
}
// Add interaction-specific values
if (interactions === "properties") {
if (interaction["readOnly"] === undefined) {
interaction["readOnly"] = false;
}
if (interaction["writeOnly"] === undefined) {
interaction["writeOnly"] = false;
}
if (interaction["observable"] === undefined) {
interaction["observable"] = false;
}
if (form["op"] === undefined) {
if (!interaction["readOnly"] && !interaction["writeOnly"]) {
form["op"] = ["readproperty", "writeproperty"];
} else if (interaction["readOnly"]) {
form["op"] = "readproperty";
} else if (interaction["writeOnly"]) {
form["op"] = "writeproperty";
}
}
} else if (interactions === "actions") {
if (interaction["safe"] === undefined) {
interaction["safe"] = false;
}
if (interaction["idempotent"] === undefined) {
interaction["idempotent"] = false;
}
if (form["op"] === undefined) {
form["op"] = "invokeaction";
}
} else if (interactions === "events") {
if (form["op"] === undefined) {
form["op"] = ["subscribeevent", "unsubscribeevent"];
}
}
}
}
}
// Populate security default values
addSecurityDefaults(["basic", "digest", "bearer"], "in", "header");
addSecurityDefaults(["apikey"], "in", "query");
addSecurityDefaults(["digest"], "qop", "auth");
addSecurityDefaults(["bearer"], "alg", "ES256");
addSecurityDefaults(["bearer"], "format", "jwt");
// Populate form default values
[PROPERTIES, ACTIONS, EVENTS].forEach((interactions) => addFormDefaults(interactions));
fs.writeFileSync("canonical-td.jsonld", canonifyJSON(td, undefined, parseInt(process.argv[3])));