| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2025-10-03 02:42:19 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2025-10-03 02:42:19 UTC |
| parent | c5ac62da5bc72705a4ec29d8459cee6126415fab |
| borogove/Caps.hx | +2 | -1 |
| borogove/Color.hx | +2 | -1 |
| borogove/Identicon.hx | +2 | -1 |
| borogove/OMEMO.hx | +4 | -3 |
| borogove/Util.hx | +14 | -0 |
| borogove/streams/XmppJsStream.hx | +2 | -1 |
diff --git a/borogove/Caps.hx b/borogove/Caps.hx index ad79b12..3df6ee0 100644 --- a/borogove/Caps.hx +++ b/borogove/Caps.hx @@ -6,6 +6,7 @@ import haxe.io.BytesData; using Lambda; import borogove.Hash; +import borogove.Util; @:expose class Caps { @@ -90,7 +91,7 @@ class Caps { for (feature in features) { s += feature + "<"; } - return Hash.sha1(Bytes.ofString(s)); + return Hash.sha1(bytesOfString(s)); } public function verRaw(): Hash { diff --git a/borogove/Color.hx b/borogove/Color.hx index 3993bbc..de894dc 100644 --- a/borogove/Color.hx +++ b/borogove/Color.hx @@ -3,10 +3,11 @@ package borogove; import hsluv.Hsluv; import haxe.io.Bytes; import haxe.crypto.Sha1; +import borogove.Util; class Color { public static function forString(s:String) { - var hash = Sha1.make(Bytes.ofString(s)); + var hash = Sha1.make(bytesOfString(s)); var hue = (hash.getUInt16(0) / 65536.0) * 360; var color = new Hsluv(); color.hsluv_h = hue; diff --git a/borogove/Identicon.hx b/borogove/Identicon.hx index b65a332..d9e9615 100644 --- a/borogove/Identicon.hx +++ b/borogove/Identicon.hx @@ -1,6 +1,7 @@ package borogove; import borogove.Color; +import borogove.Util; import haxe.crypto.Sha1; import haxe.io.Bytes; import haxe.io.BytesInput; @@ -16,7 +17,7 @@ import HaxeCBridge; #end class Identicon { public static function svg(source: String) { - final sha = Sha1.make(Bytes.ofString(source)); + final sha = Sha1.make(bytesOfString(source)); final input = new BytesInput(sha); input.bigEndian = true; final hash = input.readInt32(); diff --git a/borogove/OMEMO.hx b/borogove/OMEMO.hx index 588b653..d76f946 100644 --- a/borogove/OMEMO.hx +++ b/borogove/OMEMO.hx @@ -3,6 +3,7 @@ package borogove; import haxe.io.BytesBuffer; import borogove.EncryptedMessage; import borogove.Message; +import borogove.Util; import borogove.queries.PubsubGet; import borogove.queries.PubsubPublish; @@ -1011,7 +1012,7 @@ class OMEMO { subtle.encrypt({ name: "AES-GCM", iv: encryptedPayload.iv, - }, generatedKey, Bytes.ofString(plaintext).getData()).then((encryptionResult:BytesData) -> { + }, generatedKey, bytesOfString(plaintext).getData()).then((encryptionResult:BytesData) -> { // Process result of encryption final encryptedBytes = Bytes.ofData(encryptionResult); final ciphertextLength = encryptionResult.byteLength - 16; // Exclude GCM tag @@ -1025,7 +1026,7 @@ class OMEMO { resolve(encryptedPayload); }); }); - }); + }); }); #else throw new haxe.exceptions.NotImplementedException(); @@ -1217,7 +1218,7 @@ class OMEMO { // to a pair of bytes (since JS uses UTF-16). return Browser.window.btoa(keyStr); #else - return Base64.encode(Bytes.ofString(keyStr, RawNative)); + return Base64.encode(bytesOfString(keyStr, RawNative)); #end } diff --git a/borogove/Util.hx b/borogove/Util.hx index 6d87e08..4a54a35 100644 --- a/borogove/Util.hx +++ b/borogove/Util.hx @@ -1,5 +1,11 @@ package borogove; +import haxe.io.Bytes; +#if js +import js.html.TextEncoder; +final textEncoder = new TextEncoder(); +#end + function setupTrace() { #if js haxe.Log.trace = (v, ?infos) -> { @@ -16,6 +22,14 @@ function setupTrace() { #end } +inline function bytesOfString(s: String) { +#if js + return Bytes.ofData(textEncoder.encode(s).buffer); +#else + return Bytes.ofString(s); +#end +} + // Faster just by specializing to array inline function existsFast<A>(it:Array<A>, f:(item:A) -> Bool) { var result = false; diff --git a/borogove/streams/XmppJsStream.hx b/borogove/streams/XmppJsStream.hx index 2df63a0..5c9dbcc 100644 --- a/borogove/streams/XmppJsStream.hx +++ b/borogove/streams/XmppJsStream.hx @@ -10,6 +10,7 @@ using Lambda; import borogove.FSM; import borogove.GenericStream; import borogove.Stanza; +import borogove.Util; @:js.import(@default "@xmpp/sasl-scram-sha-1") extern class XmppJsScramSha1 { @@ -360,7 +361,7 @@ class XmppJsStream extends GenericStream { this.trigger( "sm/update", { - sm: Bytes.ofString(haxe.Json.stringify({ + sm: bytesOfString(haxe.Json.stringify({ id: client.streamManagement.id, outbound: client.streamManagement.outbound, inbound: client.streamManagement.inbound,