0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-21 13:39:22 +01:00

fix(hogvm): if without else, add base64 and url encoding/decoding (#22976)

This commit is contained in:
Marius Andra 2024-06-14 11:03:57 +02:00 committed by GitHub
parent 7e3a365102
commit 919d017699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 107 additions and 4 deletions

View File

@ -0,0 +1,3 @@
["_h", 42, 0, 36, 0, 32, "email", 45, 32, "", 36, 1, 11, 40, 12, 32, "ERROR - Email not found!", 2, "print", 1, 35, 32,
"3", 2, "print", 1, 35, 32, "1", 2, "print", 1, 35, 32, "", 36, 1, 11, 40, 14, 32, "ERROR - Email not found!", 2,
"print", 1, 35, 32, "3", 2, "print", 1, 35, 39, 6, 32, "else", 2, "print", 1, 35, 32, "1", 2, "print", 1, 35, 35, 35]

View File

@ -0,0 +1,3 @@
1
else
1

View File

@ -1,3 +1,9 @@
["_h", 32, "-- empty, notEmpty, length, lower, upper, reverse --", 2, "print", 1, 35, 32, "234", 2, "notEmpty", 1, 32,
"", 2, "empty", 1, 3, 2, 40, 11, 32, "123", 2, "length", 1, 2, "print", 1, 35, 32, "tdd4gh", 32, "Tdd4gh", 2, "lower",
1, 11, 40, 11, 32, "test", 2, "upper", 1, 2, "print", 1, 35, 32, "spinner", 2, "reverse", 1, 2, "print", 1, 35]
"", 2, "empty", 1, 3, 2, 40, 9, 32, "123", 2, "length", 1, 2, "print", 1, 35, 32, "tdd4gh", 32, "Tdd4gh", 2, "lower", 1,
11, 40, 9, 32, "test", 2, "upper", 1, 2, "print", 1, 35, 32, "spinner", 2, "reverse", 1, 2, "print", 1, 35, 32,
"http://www.google.com", 2, "encodeURLComponent", 1, 2, "print", 1, 35, 32, "tom & jerry", 2, "encodeURLComponent", 1,
2, "print", 1, 35, 32, "http://www.google.com", 2, "encodeURLComponent", 1, 2, "decodeURLComponent", 1, 2, "print", 1,
35, 32, "tom & jerry", 2, "encodeURLComponent", 1, 2, "decodeURLComponent", 1, 2, "print", 1, 35, 32,
"http://www.google.com", 2, "base64Encode", 1, 2, "print", 1, 35, 32, "tom & jerry", 2, "base64Encode", 1, 2, "print",
1, 35, 32, "http://www.google.com", 2, "base64Encode", 1, 2, "base64Decode", 1, 2, "print", 1, 35, 32, "tom & jerry", 2,
"base64Encode", 1, 2, "base64Decode", 1, 2, "print", 1, 35]

View File

@ -2,3 +2,11 @@
3
TEST
rennips
http%3A%2F%2Fwww.google.com
tom%20%26%20jerry
http://www.google.com
tom & jerry
aHR0cDovL3d3dy5nb29nbGUuY29t
dG9tICYgamVycnk=
http://www.google.com
tom & jerry

View File

@ -0,0 +1,20 @@
let props := {
}
let email := props.email
if (email == '') {
print('ERROR - Email not found!')
print('3')
}
print('1')
if (email == '') {
print('ERROR - Email not found!')
print('3')
} else {
print('else')
}
print('1')

View File

@ -2,3 +2,15 @@ print('-- empty, notEmpty, length, lower, upper, reverse --')
if (empty('') and notEmpty('234')) print(length('123'))
if (lower('Tdd4gh') == 'tdd4gh') print(upper('test'))
print(reverse('spinner'))
print(encodeURLComponent('http://www.google.com'))
print(encodeURLComponent('tom & jerry'))
print(decodeURLComponent(encodeURLComponent('http://www.google.com')))
print(decodeURLComponent(encodeURLComponent('tom & jerry')))
print(base64Encode('http://www.google.com'))
print(base64Encode('tom & jerry'))
print(base64Decode(base64Encode('http://www.google.com')))
print(base64Decode(base64Encode('tom & jerry')))

View File

@ -107,6 +107,34 @@ def jsonStringify(name: str, args: list[Any], team: Optional["Team"], stdout: Op
return json.dumps(args[0])
def base64Encode(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
import base64
return base64.b64encode(args[0].encode()).decode()
def base64Decode(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
import base64
return base64.b64decode(args[0].encode()).decode()
def encodeURLComponent(
name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int
) -> str:
import urllib.parse
return urllib.parse.quote(args[0], safe="")
def decodeURLComponent(
name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int
) -> str:
import urllib.parse
return urllib.parse.unquote(args[0])
STL: dict[str, Callable[[str, list[Any], Optional["Team"], list[str] | None, int], Any]] = {
"concat": concat,
"match": match,
@ -126,4 +154,8 @@ STL: dict[str, Callable[[str, list[Any], Optional["Team"], list[str] | None, int
"run": run,
"jsonParse": jsonParse,
"jsonStringify": jsonStringify,
"base64Encode": base64Encode,
"base64Decode": base64Decode,
"encodeURLComponent": encodeURLComponent,
"decodeURLComponent": decodeURLComponent,
}

View File

@ -1,6 +1,6 @@
{
"name": "@posthog/hogvm",
"version": "1.0.11",
"version": "1.0.12",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"main": "dist/index.js",

View File

@ -86,6 +86,25 @@ export const STL: Record<string, (args: any[], name: string, timeout: number) =>
}
return JSON.stringify(convert(args[0]))
},
base64Encode: (args) => {
return Buffer.from(args[0]).toString('base64')
},
base64Decode: (args) => {
return Buffer.from(args[0], 'base64').toString()
},
tryBase64Decode: (args) => {
try {
return Buffer.from(args[0], 'base64').toString()
} catch (e) {
return ''
}
},
encodeURLComponent(args) {
return encodeURIComponent(args[0])
},
decodeURLComponent(args) {
return decodeURIComponent(args[0])
},
}
export const ASYNC_STL: Record<string, (args: any[], name: string, timeout: number) => Promise<any>> = {

View File

@ -248,7 +248,7 @@ class BytecodeBuilder(Visitor):
response = []
response.extend(expr)
response.extend([Operation.JUMP_IF_FALSE, len(then) + 2]) # + else's OP_JUMP + count
response.extend([Operation.JUMP_IF_FALSE, len(then) + (2 if else_ else 0)])
response.extend(then)
if else_:
response.extend([Operation.JUMP, len(else_)])