65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build script
|
|
// +build script
|
|
|
|
package js
|
|
|
|
import (
|
|
"encoding/base64"
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
|
)
|
|
|
|
func init() {
|
|
if !teaconst.IsMain {
|
|
return
|
|
}
|
|
|
|
SharedLibraryManager.Register(&JSBase64Library{})
|
|
}
|
|
|
|
type JSBase64Library struct {
|
|
JSBaseLibrary
|
|
}
|
|
|
|
func (this *JSBase64Library) JSNamespace() string {
|
|
return "gojs.base64"
|
|
}
|
|
|
|
func (this *JSBase64Library) JSPrototype() string {
|
|
return `gojs.base64.encode = function (data) {
|
|
return $this.Encode(data)
|
|
}
|
|
|
|
gojs.base64.decode = function (data) {
|
|
return $this.Decode(data)
|
|
}
|
|
`
|
|
}
|
|
|
|
func (this *JSBase64Library) Encode(arguments *FunctionArguments) (string, error) {
|
|
s, ok := arguments.StringAt(0)
|
|
if ok {
|
|
return base64.StdEncoding.EncodeToString([]byte(s)), nil
|
|
}
|
|
|
|
b, ok := arguments.BytesAt(0)
|
|
if ok {
|
|
return base64.StdEncoding.EncodeToString(b), nil
|
|
}
|
|
|
|
return "", nil
|
|
}
|
|
|
|
func (this *JSBase64Library) Decode(arguments *FunctionArguments) (string, error) {
|
|
s, ok := arguments.StringAt(0)
|
|
if ok {
|
|
result, err := base64.StdEncoding.DecodeString(s)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(result), nil
|
|
}
|
|
|
|
return "", nil
|
|
}
|