Zum Inhalt springen

Hook-Referenz: Formate und Diagnose

In Hooks sprichst du diese fünf Pakete über context an, z. B. context.base64.encode(...) oder context.json.parse(...). Die Signaturen stammen aus den // args:-Kommentaren in internal/hook/context_*.go im tibi-server; wo so ein Kommentar fehlt, ist die Signatur aus Funktionsrumpf und _test-Datei abgeleitet und entsprechend markiert. Jede Funktion zeigt genau einen Fehlerfall mit dem originalen Error-Text aus Code oder Tests (Status-Code aus den Hook-Tests, meist 500).

Quelle: context_base64.go, registriert per RegisterPackageFunctions("base64", ...) mit encode, decode.

Signatur: base64.encode(data: string | []byte | []interface{}): string — aus // args: data:string|[]byte|[]interface{} (context_base64.go:17). Nimmt einen String, Bytes oder ein JS-Zahlenarray und liefert Standard-Base64.

base64.encode – Beispiel
;(function () {
const encoded = context.base64.encode("Hallo Welt")
throw { status: 200, json: { encoded: encoded } }
})()

Fehlerfall (Error): Ohne Argument brichst du mit missing data in base64.encode function ab (context_base64.go:20), belegt mit Status 500 (error - missing data in encode, context_base64_test.go:94).

Signatur: base64.decode(encoded: string, options?: { returnBytes?: boolean }): string | []byte — aus // args: encoded:string, options:map[string]interface{} mit returnBytes:bool (context_base64.go:53). Ohne Option kommt ein String zurück, mit { returnBytes: true } Bytes.

base64.decode – Beispiel
;(function () {
const text = context.base64.decode("SGVsbG8gV2VsdA==")
throw { status: 200, json: { text: text } }
})()

Fehlerfall (Error): Ein ungültiger Base64-String scheitert mit invalid base64 string in base64.decode function (context_base64.go:86), belegt mit Status 500 (error - invalid base64 string, context_base64_test.go:105).

Quelle: context_json.go, registriert per RegisterPackageFunctions("json", ...) mit parse, stringify.

Signatur: json.parse(str: string): any — aus // args: str:string (context_json.go:17). Wandelt einen JSON-String in ein JS-Objekt um.

json.parse – Beispiel
;(function () {
const obj = context.json.parse('{"name":"Test","wert":42}')
throw { status: 200, json: { name: obj.name, wert: obj.wert } }
})()

Fehlerfall (Error): Ohne Argument meldet der Code invalid number of arguments (context_json.go:20), belegt mit Status 500 (error - parse missing argument, context_json_test.go:104).

Signatur: json.stringify(obj: any): string — aus // args: obj:any (context_json.go:35). Wandelt ein JS-Objekt in einen JSON-String um.

json.stringify – Beispiel
;(function () {
const str = context.json.stringify({ name: "Test", wert: 42 })
throw { status: 200, json: { str: str } }
})()

Fehlerfall (Error): Ohne Argument meldet der Code invalid number of arguments (context_json.go:38); ein nicht serialisierbares Objekt reicht den Marshal-Error durch.

Quelle: context_xml.go, registriert per RegisterPackageFunctions("xml", ...) mit create, parse.

Signatur: xml.create(data: object, options?: { rootElement?: string, includeHeader?: boolean }): string — aus dem // args:-Block mit data: map, rootElement: string und includeHeader: bool (context_xml.go:19). Mit includeHeader: true stellst du <?xml version="1.0" encoding="UTF-8"?> voran.

xml.create – Beispiel
;(function () {
const out = context.xml.create({ user: { name: "Anna", ort: "Berlin" } })
throw { status: 200, json: { xml: out } }
})()

Fehlerfall (Error): Ohne Daten brichst du mit missing data in xml.create function ab (context_xml.go:27), belegt mit Status 500 (xml creation error - missing data, context_xml_test.go:61).

Signatur: xml.parse(xml: string, options?: object): object — aus // args: xml:string options:map[string]interface{} (context_xml.go:110). Attribute landen unter --Präfix (z. B. "-id").

xml.parse – Beispiel
;(function () {
const obj = context.xml.parse("<root><name>Anna</name></root>")
throw { status: 200, json: { name: obj.root.name } }
})()

Fehlerfall (Error): Ohne Argument brichst du mit missing xml string in xml.parse function ab (context_xml.go:113), belegt mit Status 500 (xml parsing error - missing xml, context_xml_test.go:190).

Quelle: context_charset.go, registriert per RegisterPackageFunctions("charset", ...) mit decode, iso8859ToUtf8.

Signatur: charset.decode(input: string | []byte | []int64, encoding: string): string — aus // args: input:string|[]byte|[]int64, encoding:string (context_charset.go:118). Versteht utf-8, iso-8859-1/latin1 sowie alle Labels aus htmlindex (z. B. windows-1252, shift_jis); UTF-8 mit BOM wird entstückt.

charset.decode – Beispiel
;(function () {
const text = context.charset.decode([228, 246, 252], "iso-8859-1")
throw { status: 200, json: { text: text } }
})()

Fehlerfall (Error): Ungültige UTF-8-Bytes scheitern mit invalid utf-8 bytes in charset.decode (Error-String invalid utf-8 bytes in context_charset.go:86), belegt mit Status 500 (decode utf8 invalid bytes, context_charset_test.go:133).

Signatur: charset.iso8859ToUtf8(iso8859: string | []byte | []int64): string — aus // args: iso8859:string (context_charset.go:60). Kurzform für den häufigsten Altbestand-Fall (entspricht decode mit iso-8859-1).

charset.iso8859ToUtf8 – Beispiel
;(function () {
const text = context.charset.iso8859ToUtf8([72, 228])
throw { status: 200, json: { text: text } }
})()

Fehlerfall (Error): Ohne Argument brichst du mit missing iso8859 string in charset.iso8859ToUtf8 ab (context_charset.go:63), belegt mit Status 500 (error missing arg, context_charset_test.go:155).

Quelle: context_debug.go, registriert per RegisterPackageFunctions("debug", ...) mit dump, sentryTraceId, sleep.

Signatur: debug.dump(toDump?: any): void — aus // args: toDump:any (context_debug.go:21). Schreibt die Argumente per spew auf die Server-Konsole und gibt immer null zurück.

debug.dump – Beispiel
;(function () {
context.debug.dump({ hinweis: "Stand prüfen", wert: 42 })
throw { status: 200, json: { message: "dump completed" } }
})()

Fehlerfall: Kein Fehlerfall — dump nimmt beliebige (auch keine) Argumente und liefert stets ohne Beanstandung zurück (context_debug.go:49, dokumentierte Abwesenheit, kein Versäumnis).

Signatur (abgeleitet): debug.sentryTraceId(): string — kein args:-Kommentar, nur // get sentry trace_id (context_debug.go:52); aus Rumpf (FuncDebugSentryTraceId, context_debug.go:53) und Hook-Kontext abgeleitet. Liefert die Sentry-Trace-ID der laufenden Transaktion oder einen leeren String ohne Transaktion.

debug.sentryTraceId – Beispiel
;(function () {
const traceId = context.debug.sentryTraceId()
throw { status: 200, json: { traceId: traceId } }
})()

Fehlerfall (Error): Außerhalb von Hooks (kein Hook-Kontext) scheitert der Aufruf mit sentry functions are only valid in hooks (context_debug.go:55).

Signatur: debug.sleep(duration: int): void — aus // args: duration:int (context_debug.go:68). Hält den Hook für duration Millisekunden an; nur für Diagnose gedacht.

debug.sleep – Beispiel
;(function () {
context.debug.sleep(50)
throw { status: 200, json: { slept: true } }
})()

Fehlerfall (Error): Ohne genau ein Argument meldet der Code sleep function requires 1 argument (context_debug.go:71), belegt mit Status 500 (error - sleep missing argument, context_debug_test.go:44).

Weiter: Hook-Kontexte als Paket-Index, Hook-Referenz: Daten für db, cache, request, response und fs.

Verifiziert am 68b4278 2026-10-16 (tibi-server)