@aml-org/amf-custom-validator 0.1.0-SNAPSHOT.4 → 0.1.0-SNAPSHOT.40
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/index.js +26 -20
- package/lib/main.wasm.gz +0 -0
- package/package.json +1 -1
- package/test/simple.js +25 -18
package/index.js
CHANGED
|
@@ -4,8 +4,8 @@ const pako = require("pako");
|
|
|
4
4
|
let wasm_gz
|
|
5
5
|
let wasm
|
|
6
6
|
|
|
7
|
-
let
|
|
8
|
-
let go =
|
|
7
|
+
let initialized = false
|
|
8
|
+
let go = null;
|
|
9
9
|
|
|
10
10
|
const run = function(profile, data, debug) {
|
|
11
11
|
let before = new Date()
|
|
@@ -16,35 +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
21
|
cb(res,null);
|
|
22
22
|
} else {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
23
|
+
cb(null,new Error("WASM/GO not initialized"))
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const initialize = function(cb) {
|
|
27
|
+
if (initialized === true) {
|
|
28
|
+
cb(null)
|
|
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(null);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
cb(new Error("WebAssembly is not supported in your JS environment"));
|
|
37
43
|
}
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const exit = function() {
|
|
41
|
-
if(
|
|
47
|
+
if(initialized) {
|
|
42
48
|
__AMF__terminateValidator()
|
|
43
49
|
go.exit(0)
|
|
44
|
-
|
|
45
|
-
go = new Go();
|
|
50
|
+
initialized = false;
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
|
|
54
|
+
module.exports.initialize = initialize;
|
|
49
55
|
module.exports.validate = validateCustomProfile;
|
|
50
56
|
module.exports.exit = exit;
|
package/lib/main.wasm.gz
CHANGED
|
Binary file
|
package/package.json
CHANGED
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
|
})
|