@aml-org/amf-custom-validator 1.2.1 → 1.2.3
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 +31 -0
- package/lib/main.wasm.gz +0 -0
- package/lib/wasm_exec.js +51 -13
- package/package.json +1 -1
- package/test/simple.js +37 -2
package/index.js
CHANGED
|
@@ -23,6 +23,35 @@ const validateCustomProfile = function(profile, data, debug, cb) {
|
|
|
23
23
|
cb(undefined,new Error("WASM/GO not initialized"))
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
const runGenerateRego = function(profile) {
|
|
28
|
+
const res = __AMF__generateRego(profile);
|
|
29
|
+
return res;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const generateRego = function(profile, cb) {
|
|
33
|
+
if (initialized) {
|
|
34
|
+
let res = runGenerateRego(profile);
|
|
35
|
+
cb(res,undefined);
|
|
36
|
+
} else {
|
|
37
|
+
cb(undefined,new Error("WASM/GO not initialized"))
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const runNormalizeInput = function(data) {
|
|
42
|
+
const res = __AMF__normalizeInput(data);
|
|
43
|
+
return res;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const normalizeInput = function(data, cb) {
|
|
47
|
+
if (initialized) {
|
|
48
|
+
let res = runNormalizeInput(data);
|
|
49
|
+
cb(res,undefined);
|
|
50
|
+
} else {
|
|
51
|
+
cb(undefined,new Error("WASM/GO not initialized"))
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
26
55
|
const initialize = function(cb) {
|
|
27
56
|
if (initialized === true) {
|
|
28
57
|
cb(undefined)
|
|
@@ -53,4 +82,6 @@ const exit = function() {
|
|
|
53
82
|
|
|
54
83
|
module.exports.initialize = initialize;
|
|
55
84
|
module.exports.validate = validateCustomProfile;
|
|
85
|
+
module.exports.generateRego = generateRego;
|
|
86
|
+
module.exports.normalizeInput = normalizeInput;
|
|
56
87
|
module.exports.exit = exit;
|
package/lib/main.wasm.gz
CHANGED
|
Binary file
|
package/lib/wasm_exec.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
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
|
-
// ---------------------------------------------------
|
|
6
1
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
7
2
|
// Use of this source code is governed by a BSD-style
|
|
8
3
|
// license that can be found in the LICENSE file.
|
|
@@ -107,7 +102,7 @@
|
|
|
107
102
|
}
|
|
108
103
|
}
|
|
109
104
|
|
|
110
|
-
if (!global.crypto) {
|
|
105
|
+
if (!global.crypto && global.require) {
|
|
111
106
|
const nodeCrypto = require("crypto");
|
|
112
107
|
global.crypto = {
|
|
113
108
|
getRandomValues(b) {
|
|
@@ -115,6 +110,9 @@
|
|
|
115
110
|
},
|
|
116
111
|
};
|
|
117
112
|
}
|
|
113
|
+
if (!global.crypto) {
|
|
114
|
+
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
|
|
115
|
+
}
|
|
118
116
|
|
|
119
117
|
if (!global.performance) {
|
|
120
118
|
global.performance = {
|
|
@@ -125,13 +123,19 @@
|
|
|
125
123
|
};
|
|
126
124
|
}
|
|
127
125
|
|
|
128
|
-
if (!global.TextEncoder) {
|
|
126
|
+
if (!global.TextEncoder && global.require) {
|
|
129
127
|
global.TextEncoder = require("util").TextEncoder;
|
|
130
128
|
}
|
|
129
|
+
if (!global.TextEncoder) {
|
|
130
|
+
throw new Error("global.TextEncoder is not available, polyfill required");
|
|
131
|
+
}
|
|
131
132
|
|
|
132
|
-
if (!global.TextDecoder) {
|
|
133
|
+
if (!global.TextDecoder && global.require) {
|
|
133
134
|
global.TextDecoder = require("util").TextDecoder;
|
|
134
135
|
}
|
|
136
|
+
if (!global.TextDecoder) {
|
|
137
|
+
throw new Error("global.TextDecoder is not available, polyfill required");
|
|
138
|
+
}
|
|
135
139
|
|
|
136
140
|
// End of polyfills for common API.
|
|
137
141
|
|
|
@@ -260,6 +264,7 @@
|
|
|
260
264
|
|
|
261
265
|
// func wasmExit(code int32)
|
|
262
266
|
"runtime.wasmExit": (sp) => {
|
|
267
|
+
sp >>>= 0;
|
|
263
268
|
const code = this.mem.getInt32(sp + 8, true);
|
|
264
269
|
this.exited = true;
|
|
265
270
|
delete this._inst;
|
|
@@ -272,6 +277,7 @@
|
|
|
272
277
|
|
|
273
278
|
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
|
274
279
|
"runtime.wasmWrite": (sp) => {
|
|
280
|
+
sp >>>= 0;
|
|
275
281
|
const fd = getInt64(sp + 8);
|
|
276
282
|
const p = getInt64(sp + 16);
|
|
277
283
|
const n = this.mem.getInt32(sp + 24, true);
|
|
@@ -280,16 +286,19 @@
|
|
|
280
286
|
|
|
281
287
|
// func resetMemoryDataView()
|
|
282
288
|
"runtime.resetMemoryDataView": (sp) => {
|
|
289
|
+
sp >>>= 0;
|
|
283
290
|
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
284
291
|
},
|
|
285
292
|
|
|
286
293
|
// func nanotime1() int64
|
|
287
294
|
"runtime.nanotime1": (sp) => {
|
|
295
|
+
sp >>>= 0;
|
|
288
296
|
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
|
289
297
|
},
|
|
290
298
|
|
|
291
299
|
// func walltime1() (sec int64, nsec int32)
|
|
292
300
|
"runtime.walltime1": (sp) => {
|
|
301
|
+
sp >>>= 0;
|
|
293
302
|
const msec = (new Date).getTime();
|
|
294
303
|
setInt64(sp + 8, msec / 1000);
|
|
295
304
|
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
|
@@ -297,6 +306,7 @@
|
|
|
297
306
|
|
|
298
307
|
// func scheduleTimeoutEvent(delay int64) int32
|
|
299
308
|
"runtime.scheduleTimeoutEvent": (sp) => {
|
|
309
|
+
sp >>>= 0;
|
|
300
310
|
const id = this._nextCallbackTimeoutID;
|
|
301
311
|
this._nextCallbackTimeoutID++;
|
|
302
312
|
this._scheduledTimeouts.set(id, setTimeout(
|
|
@@ -316,6 +326,7 @@
|
|
|
316
326
|
|
|
317
327
|
// func clearTimeoutEvent(id int32)
|
|
318
328
|
"runtime.clearTimeoutEvent": (sp) => {
|
|
329
|
+
sp >>>= 0;
|
|
319
330
|
const id = this.mem.getInt32(sp + 8, true);
|
|
320
331
|
clearTimeout(this._scheduledTimeouts.get(id));
|
|
321
332
|
this._scheduledTimeouts.delete(id);
|
|
@@ -323,11 +334,13 @@
|
|
|
323
334
|
|
|
324
335
|
// func getRandomData(r []byte)
|
|
325
336
|
"runtime.getRandomData": (sp) => {
|
|
337
|
+
sp >>>= 0;
|
|
326
338
|
crypto.getRandomValues(loadSlice(sp + 8));
|
|
327
339
|
},
|
|
328
340
|
|
|
329
341
|
// func finalizeRef(v ref)
|
|
330
342
|
"syscall/js.finalizeRef": (sp) => {
|
|
343
|
+
sp >>>= 0;
|
|
331
344
|
const id = this.mem.getUint32(sp + 8, true);
|
|
332
345
|
this._goRefCounts[id]--;
|
|
333
346
|
if (this._goRefCounts[id] === 0) {
|
|
@@ -340,44 +353,51 @@
|
|
|
340
353
|
|
|
341
354
|
// func stringVal(value string) ref
|
|
342
355
|
"syscall/js.stringVal": (sp) => {
|
|
356
|
+
sp >>>= 0;
|
|
343
357
|
storeValue(sp + 24, loadString(sp + 8));
|
|
344
358
|
},
|
|
345
359
|
|
|
346
360
|
// func valueGet(v ref, p string) ref
|
|
347
361
|
"syscall/js.valueGet": (sp) => {
|
|
362
|
+
sp >>>= 0;
|
|
348
363
|
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
|
349
|
-
sp = this._inst.exports.getsp(); // see comment above
|
|
364
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
350
365
|
storeValue(sp + 32, result);
|
|
351
366
|
},
|
|
352
367
|
|
|
353
368
|
// func valueSet(v ref, p string, x ref)
|
|
354
369
|
"syscall/js.valueSet": (sp) => {
|
|
370
|
+
sp >>>= 0;
|
|
355
371
|
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
|
356
372
|
},
|
|
357
373
|
|
|
358
374
|
// func valueDelete(v ref, p string)
|
|
359
375
|
"syscall/js.valueDelete": (sp) => {
|
|
376
|
+
sp >>>= 0;
|
|
360
377
|
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
|
361
378
|
},
|
|
362
379
|
|
|
363
380
|
// func valueIndex(v ref, i int) ref
|
|
364
381
|
"syscall/js.valueIndex": (sp) => {
|
|
382
|
+
sp >>>= 0;
|
|
365
383
|
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
|
366
384
|
},
|
|
367
385
|
|
|
368
386
|
// valueSetIndex(v ref, i int, x ref)
|
|
369
387
|
"syscall/js.valueSetIndex": (sp) => {
|
|
388
|
+
sp >>>= 0;
|
|
370
389
|
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
|
371
390
|
},
|
|
372
391
|
|
|
373
392
|
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
374
393
|
"syscall/js.valueCall": (sp) => {
|
|
394
|
+
sp >>>= 0;
|
|
375
395
|
try {
|
|
376
396
|
const v = loadValue(sp + 8);
|
|
377
397
|
const m = Reflect.get(v, loadString(sp + 16));
|
|
378
398
|
const args = loadSliceOfValues(sp + 32);
|
|
379
399
|
const result = Reflect.apply(m, v, args);
|
|
380
|
-
sp = this._inst.exports.getsp(); // see comment above
|
|
400
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
381
401
|
storeValue(sp + 56, result);
|
|
382
402
|
this.mem.setUint8(sp + 64, 1);
|
|
383
403
|
} catch (err) {
|
|
@@ -388,11 +408,12 @@
|
|
|
388
408
|
|
|
389
409
|
// func valueInvoke(v ref, args []ref) (ref, bool)
|
|
390
410
|
"syscall/js.valueInvoke": (sp) => {
|
|
411
|
+
sp >>>= 0;
|
|
391
412
|
try {
|
|
392
413
|
const v = loadValue(sp + 8);
|
|
393
414
|
const args = loadSliceOfValues(sp + 16);
|
|
394
415
|
const result = Reflect.apply(v, undefined, args);
|
|
395
|
-
sp = this._inst.exports.getsp(); // see comment above
|
|
416
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
396
417
|
storeValue(sp + 40, result);
|
|
397
418
|
this.mem.setUint8(sp + 48, 1);
|
|
398
419
|
} catch (err) {
|
|
@@ -403,11 +424,12 @@
|
|
|
403
424
|
|
|
404
425
|
// func valueNew(v ref, args []ref) (ref, bool)
|
|
405
426
|
"syscall/js.valueNew": (sp) => {
|
|
427
|
+
sp >>>= 0;
|
|
406
428
|
try {
|
|
407
429
|
const v = loadValue(sp + 8);
|
|
408
430
|
const args = loadSliceOfValues(sp + 16);
|
|
409
431
|
const result = Reflect.construct(v, args);
|
|
410
|
-
sp = this._inst.exports.getsp(); // see comment above
|
|
432
|
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
|
411
433
|
storeValue(sp + 40, result);
|
|
412
434
|
this.mem.setUint8(sp + 48, 1);
|
|
413
435
|
} catch (err) {
|
|
@@ -418,11 +440,13 @@
|
|
|
418
440
|
|
|
419
441
|
// func valueLength(v ref) int
|
|
420
442
|
"syscall/js.valueLength": (sp) => {
|
|
443
|
+
sp >>>= 0;
|
|
421
444
|
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
|
422
445
|
},
|
|
423
446
|
|
|
424
447
|
// valuePrepareString(v ref) (ref, int)
|
|
425
448
|
"syscall/js.valuePrepareString": (sp) => {
|
|
449
|
+
sp >>>= 0;
|
|
426
450
|
const str = encoder.encode(String(loadValue(sp + 8)));
|
|
427
451
|
storeValue(sp + 16, str);
|
|
428
452
|
setInt64(sp + 24, str.length);
|
|
@@ -430,17 +454,20 @@
|
|
|
430
454
|
|
|
431
455
|
// valueLoadString(v ref, b []byte)
|
|
432
456
|
"syscall/js.valueLoadString": (sp) => {
|
|
457
|
+
sp >>>= 0;
|
|
433
458
|
const str = loadValue(sp + 8);
|
|
434
459
|
loadSlice(sp + 16).set(str);
|
|
435
460
|
},
|
|
436
461
|
|
|
437
462
|
// func valueInstanceOf(v ref, t ref) bool
|
|
438
463
|
"syscall/js.valueInstanceOf": (sp) => {
|
|
464
|
+
sp >>>= 0;
|
|
439
465
|
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
|
|
440
466
|
},
|
|
441
467
|
|
|
442
468
|
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
|
443
469
|
"syscall/js.copyBytesToGo": (sp) => {
|
|
470
|
+
sp >>>= 0;
|
|
444
471
|
const dst = loadSlice(sp + 8);
|
|
445
472
|
const src = loadValue(sp + 32);
|
|
446
473
|
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
|
@@ -455,6 +482,7 @@
|
|
|
455
482
|
|
|
456
483
|
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
|
457
484
|
"syscall/js.copyBytesToJS": (sp) => {
|
|
485
|
+
sp >>>= 0;
|
|
458
486
|
const dst = loadValue(sp + 8);
|
|
459
487
|
const src = loadSlice(sp + 16);
|
|
460
488
|
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
|
@@ -475,6 +503,9 @@
|
|
|
475
503
|
}
|
|
476
504
|
|
|
477
505
|
async run(instance) {
|
|
506
|
+
if (!(instance instanceof WebAssembly.Instance)) {
|
|
507
|
+
throw new Error("Go.run: WebAssembly.Instance expected");
|
|
508
|
+
}
|
|
478
509
|
this._inst = instance;
|
|
479
510
|
this.mem = new DataView(this._inst.exports.mem.buffer);
|
|
480
511
|
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
|
@@ -533,6 +564,13 @@
|
|
|
533
564
|
offset += 8;
|
|
534
565
|
});
|
|
535
566
|
|
|
567
|
+
// The linker guarantees global data starts from at least wasmMinDataAddr.
|
|
568
|
+
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
|
|
569
|
+
const wasmMinDataAddr = 4096 + 8192;
|
|
570
|
+
if (offset >= wasmMinDataAddr) {
|
|
571
|
+
throw new Error("total length of command line and environment variables exceeds limit");
|
|
572
|
+
}
|
|
573
|
+
|
|
536
574
|
this._inst.exports.run(argc, argv);
|
|
537
575
|
if (this.exited) {
|
|
538
576
|
this._resolveExitPromise();
|
|
@@ -592,4 +630,4 @@
|
|
|
592
630
|
process.exit(1);
|
|
593
631
|
});
|
|
594
632
|
}
|
|
595
|
-
})();
|
|
633
|
+
})();
|
package/package.json
CHANGED
package/test/simple.js
CHANGED
|
@@ -12,9 +12,7 @@ describe('validator', () => {
|
|
|
12
12
|
it("should load the WASM code, validate a profile, exit", (done) => {
|
|
13
13
|
const profile = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/profile.yaml").toString()
|
|
14
14
|
const data = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/negative.data.jsonld").toString()
|
|
15
|
-
|
|
16
15
|
const validator = require(__dirname + "/../index")
|
|
17
|
-
|
|
18
16
|
validator.initialize(() => {
|
|
19
17
|
validator.validate(profile, data, false, (r, err) => {
|
|
20
18
|
if (err) {
|
|
@@ -36,5 +34,42 @@ describe('validator', () => {
|
|
|
36
34
|
});
|
|
37
35
|
})
|
|
38
36
|
});
|
|
37
|
+
|
|
38
|
+
it ("should generate the Rego code for a profile and exit", (done) => {
|
|
39
|
+
const profile = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/profile.yaml").toString()
|
|
40
|
+
|
|
41
|
+
const validator = require(__dirname + "/../index")
|
|
42
|
+
|
|
43
|
+
validator.initialize(() => {
|
|
44
|
+
validator.generateRego(profile, (r, err) => {
|
|
45
|
+
if (err) {
|
|
46
|
+
done(err);
|
|
47
|
+
} else {
|
|
48
|
+
assert.ok(r.indexOf("package profile_kiali") > -1)
|
|
49
|
+
validator.exit();
|
|
50
|
+
done();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it ("should normalize input data", (done) => {
|
|
57
|
+
const data = fs.readFileSync(__dirname + "/../../../test/data/integration/profile10/negative.data.jsonld").toString()
|
|
58
|
+
|
|
59
|
+
const validator = require(__dirname + "/../index")
|
|
60
|
+
|
|
61
|
+
validator.initialize(() => {
|
|
62
|
+
validator.normalizeInput(data, (r, err) => {
|
|
63
|
+
if (err) {
|
|
64
|
+
done(err);
|
|
65
|
+
} else {
|
|
66
|
+
assert.ok(r.indexOf("@ids") > -1)
|
|
67
|
+
validator.exit();
|
|
68
|
+
done();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
39
74
|
})
|
|
40
75
|
})
|