@aml-org/amf-custom-validator 0.1.0-SNAPSHOT.6 → 1.0.0-SNAPSHOT.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +13 -0
- package/index.js +29 -19
- package/lib/main.wasm.gz +0 -0
- package/lib/wasm_exec.js +9 -2
- package/package.json +2 -2
- package/test/simple.js +25 -18
package/cli.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const validator = require("./index.js");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
|
|
6
|
+
const profile = fs.readFileSync(process.cwd() + "/" + args[0]).toString();
|
|
7
|
+
const data = fs.readFileSync(process.cwd() + "/" + args[1]).toString();
|
|
8
|
+
const debug = args[2] === 'true';
|
|
9
|
+
|
|
10
|
+
validator.validate(profile, data, debug, (result, other) => {
|
|
11
|
+
console.log(result);
|
|
12
|
+
validator.exit();
|
|
13
|
+
});
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
require(__dirname + "/lib/wasm_exec");
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const pako = require("pako");
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
let wasm_gz
|
|
5
|
+
let wasm
|
|
6
6
|
|
|
7
|
-
let
|
|
8
|
-
let go =
|
|
7
|
+
let initialized = false
|
|
8
|
+
let go = undefined;
|
|
9
9
|
|
|
10
10
|
const run = function(profile, data, debug) {
|
|
11
11
|
let before = new Date()
|
|
@@ -16,31 +16,41 @@ const run = function(profile, data, debug) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const validateCustomProfile = function(profile, data, debug, cb) {
|
|
19
|
-
if (
|
|
19
|
+
if (initialized) {
|
|
20
20
|
let res = run(profile, data, debug);
|
|
21
|
-
cb(res,
|
|
21
|
+
cb(res,undefined);
|
|
22
22
|
} else {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
cb(undefined,new Error("WASM/GO not initialized"))
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const initialize = function(cb) {
|
|
27
|
+
if (initialized === true) {
|
|
28
|
+
cb(undefined)
|
|
29
|
+
}
|
|
30
|
+
go = new Go();
|
|
31
|
+
if(!wasm_gz || !wasm) {
|
|
32
|
+
wasm_gz = fs.readFileSync(__dirname + "/lib/main.wasm.gz")
|
|
33
|
+
wasm = pako.ungzip(wasm_gz)
|
|
34
|
+
}
|
|
35
|
+
if (WebAssembly) {
|
|
36
|
+
WebAssembly.instantiate(wasm, go.importObject).then((result) => {
|
|
37
|
+
go.run(result.instance);
|
|
38
|
+
initialized = true;
|
|
39
|
+
cb(undefined);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
cb(new Error("WebAssembly is not supported in your JS environment"));
|
|
33
43
|
}
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
const exit = function() {
|
|
37
|
-
if(
|
|
47
|
+
if(initialized) {
|
|
38
48
|
__AMF__terminateValidator()
|
|
39
49
|
go.exit(0)
|
|
40
|
-
|
|
41
|
-
go = new Go();
|
|
50
|
+
initialized = false;
|
|
42
51
|
}
|
|
43
52
|
}
|
|
44
53
|
|
|
54
|
+
module.exports.initialize = initialize;
|
|
45
55
|
module.exports.validate = validateCustomProfile;
|
|
46
56
|
module.exports.exit = exit;
|
package/lib/main.wasm.gz
CHANGED
|
Binary file
|
package/lib/wasm_exec.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// wasm_exec.js obtained from https://github.com/golang/go/blob/758ac371ab930734053ed226ac62681e62ab8eea/misc/wasm/wasm_exec.js
|
|
2
|
+
// Log with custom changes (we should keep track if we diverge from original content):
|
|
3
|
+
// -
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------
|
|
1
6
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
2
7
|
// Use of this source code is governed by a BSD-style
|
|
3
8
|
// license that can be found in the LICENSE file.
|
|
@@ -11,6 +16,7 @@
|
|
|
11
16
|
// - Node.js
|
|
12
17
|
// - Electron
|
|
13
18
|
// - Parcel
|
|
19
|
+
// - Webpack
|
|
14
20
|
|
|
15
21
|
if (typeof global !== "undefined") {
|
|
16
22
|
// global already exists
|
|
@@ -28,7 +34,7 @@
|
|
|
28
34
|
|
|
29
35
|
if (!global.fs && global.require) {
|
|
30
36
|
const fs = require("fs");
|
|
31
|
-
if (Object.keys(fs) !== 0) {
|
|
37
|
+
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
|
|
32
38
|
global.fs = fs;
|
|
33
39
|
}
|
|
34
40
|
}
|
|
@@ -556,6 +562,7 @@
|
|
|
556
562
|
}
|
|
557
563
|
|
|
558
564
|
if (
|
|
565
|
+
typeof module !== "undefined" &&
|
|
559
566
|
global.require &&
|
|
560
567
|
global.require.main === module &&
|
|
561
568
|
global.process &&
|
|
@@ -585,4 +592,4 @@
|
|
|
585
592
|
process.exit(1);
|
|
586
593
|
});
|
|
587
594
|
}
|
|
588
|
-
})();
|
|
595
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aml-org/amf-custom-validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0-SNAPSHOT.56",
|
|
4
4
|
"description": "AMF validator backed by OPA Rego",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,6 +12,6 @@
|
|
|
12
12
|
"mocha": "^8.4.0"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"pako": "^2.0.
|
|
15
|
+
"pako": "^2.0.4"
|
|
16
16
|
}
|
|
17
17
|
}
|
package/test/simple.js
CHANGED
|
@@ -4,30 +4,37 @@ var assert = require('assert');
|
|
|
4
4
|
describe('validator', () => {
|
|
5
5
|
|
|
6
6
|
describe('validate', () => {
|
|
7
|
+
|
|
8
|
+
function invalidReport(report) {
|
|
9
|
+
return report[0]["doc:encodes"][0]["conforms"] === false
|
|
10
|
+
}
|
|
11
|
+
|
|
7
12
|
it("should load the WASM code, validate a profile, exit", (done) => {
|
|
8
13
|
const profile = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/profile.yaml").toString()
|
|
9
14
|
const data = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/negative.data.jsonld").toString()
|
|
10
15
|
|
|
11
16
|
const validator = require(__dirname + "/../index")
|
|
12
17
|
|
|
13
|
-
validator.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
validator.initialize(() => {
|
|
19
|
+
validator.validate(profile, data, false, (r, err) => {
|
|
20
|
+
if (err) {
|
|
21
|
+
done(err);
|
|
22
|
+
} else {
|
|
23
|
+
let report = JSON.parse(r)
|
|
24
|
+
assert.ok(invalidReport(report))
|
|
25
|
+
validator.validate(profile, data, false, (r, err) => {
|
|
26
|
+
if (err) {
|
|
27
|
+
done(err)
|
|
28
|
+
} else {
|
|
29
|
+
let report = JSON.parse(r)
|
|
30
|
+
assert.ok(invalidReport(report))
|
|
31
|
+
validator.exit();
|
|
32
|
+
done();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
})
|
|
31
38
|
});
|
|
32
39
|
})
|
|
33
40
|
})
|