|
| 1 | +package nodes |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "encoding/base32" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/hex" |
| 8 | + "html" |
| 9 | + "net/url" |
| 10 | + "regexp" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/actionforge/actrun-cli/core" |
| 15 | + ni "github.com/actionforge/actrun-cli/node_interfaces" |
| 16 | + |
| 17 | + "golang.org/x/text/encoding/unicode" |
| 18 | + "golang.org/x/text/encoding/unicode/utf32" |
| 19 | +) |
| 20 | + |
| 21 | +//go:embed string-decode@v1.yml |
| 22 | +var stringDecodeDefinition string |
| 23 | + |
| 24 | +type StringDecode struct { |
| 25 | + core.NodeBaseComponent |
| 26 | + core.Inputs |
| 27 | + core.Outputs |
| 28 | +} |
| 29 | + |
| 30 | +func (n *StringDecode) OutputValueById(c *core.ExecutionState, outputId core.OutputId) (any, error) { |
| 31 | + input, err := core.InputValueById[string](c, n, ni.Core_string_decode_v1_Input_input) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + op, err := core.InputValueById[string](c, n, ni.Core_string_decode_v1_Input_op) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + var result string |
| 42 | + inputBytes := []byte(input) |
| 43 | + |
| 44 | + switch op { |
| 45 | + case "base64": |
| 46 | + decoded, err := base64.StdEncoding.DecodeString(input) |
| 47 | + if err != nil { |
| 48 | + return nil, core.CreateErr(c, err, "failed to decode base64") |
| 49 | + } |
| 50 | + result = string(decoded) |
| 51 | + case "base64url": |
| 52 | + decoded, err := base64.URLEncoding.DecodeString(input) |
| 53 | + if err != nil { |
| 54 | + return nil, core.CreateErr(c, err, "failed to decode base64url") |
| 55 | + } |
| 56 | + result = string(decoded) |
| 57 | + case "base32": |
| 58 | + decoded, err := base32.StdEncoding.DecodeString(input) |
| 59 | + if err != nil { |
| 60 | + return nil, core.CreateErr(c, err, "failed to decode base32") |
| 61 | + } |
| 62 | + result = string(decoded) |
| 63 | + case "hex": |
| 64 | + decoded, err := hex.DecodeString(input) |
| 65 | + if err != nil { |
| 66 | + return nil, core.CreateErr(c, err, "failed to decode hex") |
| 67 | + } |
| 68 | + result = string(decoded) |
| 69 | + |
| 70 | + case "utf8": |
| 71 | + result = input // No-op, it's already a UTF-8 string |
| 72 | + case "utf16le": |
| 73 | + decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder() |
| 74 | + utf8Bytes, err := decoder.Bytes(inputBytes) |
| 75 | + if err != nil { |
| 76 | + return nil, core.CreateErr(c, err, "failed to decode utf16le") |
| 77 | + } |
| 78 | + result = string(utf8Bytes) |
| 79 | + case "utf16be": |
| 80 | + decoder := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder() |
| 81 | + utf8Bytes, err := decoder.Bytes(inputBytes) |
| 82 | + if err != nil { |
| 83 | + return nil, core.CreateErr(c, err, "failed to decode utf16be") |
| 84 | + } |
| 85 | + result = string(utf8Bytes) |
| 86 | + case "utf32le": |
| 87 | + decoder := utf32.UTF32(utf32.LittleEndian, utf32.IgnoreBOM).NewDecoder() |
| 88 | + utf8Bytes, err := decoder.Bytes(inputBytes) |
| 89 | + if err != nil { |
| 90 | + return nil, core.CreateErr(c, err, "failed to decode utf32le") |
| 91 | + } |
| 92 | + result = string(utf8Bytes) |
| 93 | + case "utf32be": |
| 94 | + decoder := utf32.UTF32(utf32.BigEndian, utf32.IgnoreBOM).NewDecoder() |
| 95 | + utf8Bytes, err := decoder.Bytes(inputBytes) |
| 96 | + if err != nil { |
| 97 | + return nil, core.CreateErr(c, err, "failed to decode utf32be") |
| 98 | + } |
| 99 | + result = string(utf8Bytes) |
| 100 | + |
| 101 | + case "html": |
| 102 | + result = html.UnescapeString(input) |
| 103 | + case "url": |
| 104 | + decoded, err := url.QueryUnescape(input) |
| 105 | + if err != nil { |
| 106 | + return nil, core.CreateErr(c, err, "failed to decode url") |
| 107 | + } |
| 108 | + result = decoded |
| 109 | + case "urlpath": |
| 110 | + decoded, err := url.PathUnescape(input) |
| 111 | + if err != nil { |
| 112 | + return nil, core.CreateErr(c, err, "failed to decode url path") |
| 113 | + } |
| 114 | + result = decoded |
| 115 | + case "json": |
| 116 | + result = unescapeJSON(input) |
| 117 | + case "xml": |
| 118 | + result = unescapeXML(input) |
| 119 | + default: |
| 120 | + return nil, core.CreateErr(c, nil, "unknown operation '%s'", op) |
| 121 | + } |
| 122 | + |
| 123 | + return result, nil |
| 124 | +} |
| 125 | + |
| 126 | +var jsonEscapeRegex = regexp.MustCompile(`\\(["\\bfnrt/]|u[0-9a-fA-F]{4})`) |
| 127 | + |
| 128 | +func unescapeJSON(s string) string { |
| 129 | + return jsonEscapeRegex.ReplaceAllStringFunc(s, func(match string) string { |
| 130 | + switch match { |
| 131 | + case `\"`: |
| 132 | + return `"` |
| 133 | + case `\\`: |
| 134 | + return `\` |
| 135 | + case `\b`: |
| 136 | + return "\b" |
| 137 | + case `\f`: |
| 138 | + return "\f" |
| 139 | + case `\n`: |
| 140 | + return "\n" |
| 141 | + case `\r`: |
| 142 | + return "\r" |
| 143 | + case `\t`: |
| 144 | + return "\t" |
| 145 | + case `\/`: |
| 146 | + return "/" |
| 147 | + default: |
| 148 | + // Handle \uXXXX |
| 149 | + if strings.HasPrefix(match, `\u`) && len(match) == 6 { |
| 150 | + code, err := strconv.ParseInt(match[2:], 16, 32) |
| 151 | + if err == nil { |
| 152 | + return string(rune(code)) |
| 153 | + } |
| 154 | + } |
| 155 | + return match |
| 156 | + } |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +func unescapeXML(s string) string { |
| 161 | + replacements := []struct { |
| 162 | + escaped string |
| 163 | + unescaped string |
| 164 | + }{ |
| 165 | + {"<", "<"}, |
| 166 | + {">", ">"}, |
| 167 | + {"&", "&"}, |
| 168 | + {"'", "'"}, |
| 169 | + {""", `"`}, |
| 170 | + } |
| 171 | + |
| 172 | + result := s |
| 173 | + for _, r := range replacements { |
| 174 | + result = strings.ReplaceAll(result, r.escaped, r.unescaped) |
| 175 | + } |
| 176 | + |
| 177 | + // Handle numeric character references like < or < |
| 178 | + numericRegex := regexp.MustCompile(`&#(x[0-9a-fA-F]+|\d+);`) |
| 179 | + result = numericRegex.ReplaceAllStringFunc(result, func(match string) string { |
| 180 | + inner := match[2 : len(match)-1] |
| 181 | + var code int64 |
| 182 | + var err error |
| 183 | + if strings.HasPrefix(inner, "x") { |
| 184 | + code, err = strconv.ParseInt(inner[1:], 16, 32) |
| 185 | + } else { |
| 186 | + code, err = strconv.ParseInt(inner, 10, 32) |
| 187 | + } |
| 188 | + if err == nil { |
| 189 | + return string(rune(code)) |
| 190 | + } |
| 191 | + return match |
| 192 | + }) |
| 193 | + |
| 194 | + return result |
| 195 | +} |
| 196 | + |
| 197 | +func init() { |
| 198 | + err := core.RegisterNodeFactory(stringDecodeDefinition, func(ctx any, parent core.NodeBaseInterface, parentId string, nodeDef map[string]any, validate bool) (core.NodeBaseInterface, []error) { |
| 199 | + return &StringDecode{}, nil |
| 200 | + }) |
| 201 | + if err != nil { |
| 202 | + panic(err) |
| 203 | + } |
| 204 | +} |
0 commit comments