@@ -491,19 +491,19 @@ func (*ExecutionEnvironment) clean(doc map[string]interface{}) error {
491
491
}
492
492
493
493
func (env * ExecutionEnvironment ) addVolatileFunctions (vm * goja.Runtime ) error {
494
- err := vm .Set ("send " , func (call goja.FunctionCall ) goja.Value {
494
+ err := vm .Set ("publish " , func (call goja.FunctionCall ) goja.Value {
495
495
if len (call .Arguments ) != 3 {
496
- return vm .ToValue (Result {Content : "argument missmatch: you need 3 arguments for send(type, data, channel )" })
496
+ return vm .ToValue (Result {Content : "argument missmatch: you need 3 arguments for send(channel, type, data )" })
497
497
}
498
498
499
499
var typ , channel string
500
- if err := vm .ExportTo (call .Argument (0 ), & typ ); err != nil {
500
+ if err := vm .ExportTo (call .Argument (0 ), & channel ); err != nil {
501
501
return vm .ToValue (Result {Content : "the first argument should be a string" })
502
- } else if err := vm .ExportTo (call .Argument (2 ), & channel ); err != nil {
502
+ } else if err := vm .ExportTo (call .Argument (1 ), & typ ); err != nil {
503
503
return vm .ToValue (Result {Content : "the third argument should be a string" })
504
504
}
505
505
506
- b , err := json .Marshal (call .Argument (1 ).Export ())
506
+ b , err := json .Marshal (call .Argument (2 ).Export ())
507
507
if err != nil {
508
508
return vm .ToValue (Result {Content : fmt .Sprintf ("error converting your data: %v" , err )})
509
509
}
@@ -525,7 +525,95 @@ func (env *ExecutionEnvironment) addVolatileFunctions(vm *goja.Runtime) error {
525
525
return vm .ToValue (Result {OK : true })
526
526
})
527
527
if err != nil {
528
- return nil
528
+ return err
529
+ }
530
+
531
+ err = vm .Set ("cacheGet" , func (call goja.FunctionCall ) goja.Value {
532
+ if len (call .Arguments ) != 1 {
533
+ return vm .ToValue (Result {Content : "argument missmatch: you need 1 argument for cacheGet(key)" })
534
+ }
535
+
536
+ var key string
537
+ if err := vm .ExportTo (call .Argument (0 ), & key ); err != nil {
538
+ return vm .ToValue (Result {Content : "the first argument should be a string" })
539
+ }
540
+
541
+ val , _ := env .Volatile .Get (key )
542
+
543
+ return vm .ToValue (Result {OK : true , Content : val })
544
+ })
545
+ if err != nil {
546
+ return err
547
+ }
548
+
549
+ err = vm .Set ("cacheSet" , func (call goja.FunctionCall ) goja.Value {
550
+ if len (call .Arguments ) != 2 {
551
+ return vm .ToValue (Result {Content : "argument missmatch: you need 2 arguments for cacheSet(key, value)" })
552
+ }
553
+
554
+ var key , value string
555
+ if err := vm .ExportTo (call .Argument (0 ), & key ); err != nil {
556
+ return vm .ToValue (Result {Content : "the first argument should be a string" })
557
+ } else if err := vm .ExportTo (call .Argument (1 ), & value ); err != nil {
558
+ return vm .ToValue (Result {Content : "the 2nd argument should be a string" })
559
+ }
560
+
561
+ if err := env .Volatile .Set (key , value ); err != nil {
562
+ return vm .ToValue (Result {Content : fmt .Sprintf ("error while setting cache value: %v" , err )})
563
+ }
564
+
565
+ return vm .ToValue (Result {OK : true })
566
+ })
567
+ if err != nil {
568
+ return err
569
+ }
570
+
571
+ err = vm .Set ("inc" , func (call goja.FunctionCall ) goja.Value {
572
+ if len (call .Arguments ) != 2 {
573
+ return vm .ToValue (Result {Content : "argument missmatch: you need 2 arguments for inc(key, n)" })
574
+ }
575
+
576
+ var key string
577
+ var n int64
578
+ if err := vm .ExportTo (call .Argument (0 ), & key ); err != nil {
579
+ return vm .ToValue (Result {Content : "the first argument should be a string" })
580
+ } else if err := vm .ExportTo (call .Argument (1 ), & n ); err != nil {
581
+ return vm .ToValue (Result {Content : "the 2nd argument should be a number" })
582
+ }
583
+
584
+ total , err := env .Volatile .Inc (key , n )
585
+ if err != nil {
586
+ return vm .ToValue (Result {Content : fmt .Sprintf ("error while incrementing cache value: %v" , err )})
587
+ }
588
+
589
+ return vm .ToValue (Result {OK : true , Content : total })
590
+ })
591
+ if err != nil {
592
+ return err
593
+ }
594
+
595
+ err = vm .Set ("dec" , func (call goja.FunctionCall ) goja.Value {
596
+ if len (call .Arguments ) != 2 {
597
+ return vm .ToValue (Result {Content : "argument missmatch: you need 2 arguments for dec(key, n)" })
598
+ }
599
+
600
+ var key string
601
+ var n int64
602
+ if err := vm .ExportTo (call .Argument (0 ), & key ); err != nil {
603
+ return vm .ToValue (Result {Content : "the first argument should be a string" })
604
+ } else if err := vm .ExportTo (call .Argument (1 ), & n ); err != nil {
605
+ return vm .ToValue (Result {Content : "the 2nd argument should be a number" })
606
+ }
607
+
608
+ total , err := env .Volatile .Dec (key , n )
609
+ if err != nil {
610
+ return vm .ToValue (Result {Content : fmt .Sprintf ("error while decrementing cache value: %v" , err )})
611
+ }
612
+
613
+ return vm .ToValue (Result {OK : true , Content : total })
614
+ })
615
+ if err != nil {
616
+ return err
529
617
}
530
618
return nil
531
619
}
0 commit comments