@@ -25,9 +25,11 @@ import type {
25
25
} from './JSON.type' ;
26
26
import type { TreeInterpreter } from './TreeInterpreter' ;
27
27
28
- import stringify from 'fast-json-stable-stringify' ;
29
- import hash from 'hash.js' ;
28
+ import { sha256 } from '@noble/hashes/sha256' ;
29
+ import { sha512 } from '@noble/hashes/sha512' ;
30
+ import { bytesToHex } from '@noble/hashes/utils' ;
30
31
import { NIL , v4 as uuidv4 , v5 as uuidv5 } from 'uuid' ;
32
+ import jsonStringify from './utils/json-serialize' ;
31
33
32
34
export enum InputArgument {
33
35
TYPE_NUMBER = 0 ,
@@ -631,28 +633,41 @@ export class Runtime {
631
633
return condition ? thenValue : elseValue ?? null ;
632
634
} ;
633
635
634
- private functionRange : RuntimeFunction < [ number , number , string ] , Array < number | string > > = ( [ start , end , prefix ] ) => {
635
- return Array . from ( { length : end - start } , ( _ , i ) => ( prefix ? `${ prefix } ${ i + start } ` : i + start ) ) ;
636
+ private functionRange : RuntimeFunction < [ number , number ?, string ?] , Array < number | string > > = ( [
637
+ start ,
638
+ end ,
639
+ prefix ,
640
+ ] ) => {
641
+ if ( end === undefined ) {
642
+ end = start ;
643
+ start = 0 ;
644
+ }
645
+
646
+ return Array . from ( { length : end - start } , ( _ , i ) => ( prefix !== undefined ? `${ prefix } ${ i + start } ` : i + start ) ) ;
636
647
} ;
637
648
638
649
private functionToObject : RuntimeFunction < [ JSONArrayKeyValuePairs ] , JSONObject > = ( [ array ] ) => {
639
650
return Object . fromEntries ( array ) ;
640
651
} ;
641
652
642
653
private functionJsonSerialize : RuntimeFunction < [ JSONValue ] , string > = ( [ inputValue ] ) => {
643
- return stringify ( inputValue ) ;
654
+ const result = jsonStringify ( inputValue ) ;
655
+ if ( result === undefined ) {
656
+ throw new Error ( 'invalid-value' ) ;
657
+ }
658
+ return result ;
644
659
} ;
645
660
646
661
private functionJsonParse : RuntimeFunction < [ string ] , JSONValue > = ( [ inputValue ] ) => {
647
662
return JSON . parse ( inputValue ) ;
648
663
} ;
649
664
650
665
private functionSha256 : RuntimeFunction < [ string ] , string > = ( [ inputValue ] ) => {
651
- return hash . sha256 ( ) . update ( inputValue ) . digest ( 'hex' ) ;
666
+ return bytesToHex ( sha256 ( inputValue ) ) ;
652
667
} ;
653
668
654
669
private functionSha512 : RuntimeFunction < [ string ] , string > = ( [ inputValue ] ) => {
655
- return hash . sha512 ( ) . update ( inputValue ) . digest ( 'hex' ) ;
670
+ return bytesToHex ( sha512 ( inputValue ) ) ;
656
671
} ;
657
672
658
673
private functionUuid : RuntimeFunction < [ string ?, string ?] , string > = ( [ name , ns ] ) => {
@@ -663,7 +678,7 @@ export class Runtime {
663
678
// Match the pattern between slashes and any flags after the last slash
664
679
const match = regexString . match ( / ^ \/ ( .* ?) \/ ( [ g i m s u y ] * ) $ / ) ;
665
680
if ( ! match ) {
666
- throw new Error ( 'Invalid regex string format ' ) ;
681
+ throw new Error ( 'invalid- regex' ) ;
667
682
}
668
683
return new RegExp ( match [ 1 ] , match [ 2 ] ) ;
669
684
}
@@ -1124,6 +1139,7 @@ export class Runtime {
1124
1139
} ,
1125
1140
{
1126
1141
types : [ InputArgument . TYPE_NUMBER ] ,
1142
+ optional : true ,
1127
1143
} ,
1128
1144
{
1129
1145
types : [ InputArgument . TYPE_STRING ] ,
0 commit comments