git » sdk » commit 6a40e73

Package works both in browser and nodejs

author Stephen Paul Weber
2024-10-18 02:28:44 UTC
committer Stephen Paul Weber
2024-10-18 02:28:44 UTC
parent 4b030324a5c504f79ba15c1bfbfebd1b0478fb7b

Package works both in browser and nodejs

.gitignore +4 -0
Main.hx +0 -33
Makefile +17 -4
README.md +1 -0
browserjs.hxml +16 -0
js.hxml => nodejs.hxml +2 -0
npm/index.ts +3 -3
npm/package.json +4 -0
snikket/persistence/browser.js +2 -2
snikket/streams/XmppJsStream.hx +13 -8

diff --git a/.gitignore b/.gitignore
index bed90d4..d5856fa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,11 @@
 npm/package-lock.json
 npm/*.d.ts
+npm/browser.js
 npm/snikket.js
 npm/snikket-enums.ts
 npm/snikket-enums.js
+npm/snikket-browser.js
+npm/snikket-browser-enums.ts
+npm/snikket-browser-enums.js
 npm/index.js
 node_modules
diff --git a/Main.hx b/Main.hx
deleted file mode 100644
index 989a94c..0000000
--- a/Main.hx
+++ /dev/null
@@ -1,33 +0,0 @@
-import xmpp.Client;
-import xmpp.EventHandler;
-
-class Main {
-	static public function main():Void {
-		var client = new Client("user@example.com");
-
-		client.on("status/online", function (data) {
-			trace("CONNECTED CLIENT!");
-
-			var chat = client.getDirectChat("user2@example.com");
-			chat.getMessages(function (result) {
-				trace('${result.messages.length} messages received:');
-				for (message in result.messages) {
-					trace('[${message.isIncoming()?"incoming":"outgoing"}]: ${message.text}');
-				}
-				trace("complete: " + !result.sync.hasMore());
-			});
-			chat.onMessage((msg) -> {
-				trace("live message: ", msg);
-			});
-
-			return EventHandled;
-		});
-
-		client.on("auth/password-needed", function (data) {
-			client.usePassword("secret-password");
-			return EventHandled;
-		});
-
-		client.start();
-	}
-}
diff --git a/Makefile b/Makefile
index c3a267f..d24f144 100644
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,40 @@
 HAXE_PATH=$$HOME/Software/haxe-4.3.1/hxnodejs/12,1,0/src
 
-.PHONY: all test cpp/output.dso npm/snikket.js
+.PHONY: all test cpp/output.dso npm/snikket-browser.js npm/snikket.js
 
 all: npm libsnikket.so
 
 test:
 	haxe test.hxml
 
+npm/snikket-browser.js:
+	haxe browserjs.hxml
+	sed -i 's/import { snikket }/import { snikket as enums }/' npm/snikket-browser.d.ts
+	sed -i 's/snikket\.UiState/enums.UiState/g' npm/snikket-browser.d.ts
+	sed -i 's/snikket\.MessageStatus/enums.MessageStatus/g' npm/snikket-browser.d.ts
+	sed -i 's/snikket\.MessageDirection/enums.MessageDirection/g' npm/snikket-browser.d.ts
+	sed -i 's/snikket\.UserState/enums.UserState/g' npm/snikket-browser.d.ts
+	sed -i 's/_Push.Push_Fields_/Push/g' npm/snikket-browser.d.ts
+	sed -i '1ivar exports = {};' npm/snikket-browser.js
+	echo "export const snikket = exports.snikket;" >> npm/snikket-browser.js
+
 npm/snikket.js:
-	haxe js.hxml
+	haxe nodejs.hxml
 	sed -i 's/import { snikket }/import { snikket as enums }/' npm/snikket.d.ts
 	sed -i 's/snikket\.UiState/enums.UiState/g' npm/snikket.d.ts
 	sed -i 's/snikket\.MessageStatus/enums.MessageStatus/g' npm/snikket.d.ts
 	sed -i 's/snikket\.MessageDirection/enums.MessageDirection/g' npm/snikket.d.ts
 	sed -i 's/snikket\.UserState/enums.UserState/g' npm/snikket.d.ts
 	sed -i 's/_Push.Push_Fields_/Push/g' npm/snikket.d.ts
+	sed -i '1iimport { createRequire } from "module";' npm/snikket.js
+	sed -i '1iglobal.require = createRequire(import.meta.url);' npm/snikket.js
 	sed -i '1ivar exports = {};' npm/snikket.js
 	echo "export const snikket = exports.snikket;" >> npm/snikket.js
 
-npm: npm/snikket.js snikket/persistence/browser.js
+npm: npm/snikket-browser.js npm/snikket.js snikket/persistence/browser.js
 	cp snikket/persistence/browser.js npm
 	cd npm && npx tsc --esModuleInterop --lib esnext,dom --target esnext --preserveConstEnums -d index.ts
-	sed -i '1iimport { snikket as enums } from "./snikket-enums";' npm/index.js
+	sed -i '1iimport { snikket as enums } from "./snikket-enums.js";' npm/index.js
 
 cpp/output.dso:
 	haxe cpp.hxml
diff --git a/README.md b/README.md
index 7a53a1c..3417b27 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,7 @@ https://github.com/snikket-im/snikket-sdk
 
 Working towards simplicity in developing Snikket-compatible apps.
 
+    haxelib git jsImport https://github.com/back2dos/jsImport
     haxelib install datetime
     haxelib install haxe-strings
     haxelib install hsluv
diff --git a/browserjs.hxml b/browserjs.hxml
new file mode 100644
index 0000000..829cf6d
--- /dev/null
+++ b/browserjs.hxml
@@ -0,0 +1,16 @@
+--library haxe-strings
+--library hxtsdgen
+--library hsluv
+--library tink_http
+--library sha
+--library thenshim
+--library HtmlParser
+--library jsImport
+
+snikket.Client
+snikket.Push
+
+-D js-es=6
+-D hxtsdgen_enums_ts
+-D hxtsdgen_namespaced
+--js npm/snikket-browser.js
diff --git a/js.hxml b/nodejs.hxml
similarity index 86%
rename from js.hxml
rename to nodejs.hxml
index 2d892c0..c00a82d 100644
--- a/js.hxml
+++ b/nodejs.hxml
@@ -5,6 +5,8 @@
 --library sha
 --library thenshim
 --library HtmlParser
+--library hxnodejs
+--library jsImport
 
 snikket.Client
 snikket.Push
diff --git a/npm/index.ts b/npm/index.ts
index a79b831..938e6aa 100644
--- a/npm/index.ts
+++ b/npm/index.ts
@@ -1,6 +1,6 @@
-import browserp from "./browser";
-import { snikket as enums } from "./snikket-enums";
-import { snikket } from "./snikket";
+import browserp from "./browser.js";
+import { snikket as enums } from "./snikket-enums.js";
+import { snikket } from "./snikket.js";
 
 // TODO: should we autogenerate this?
 export import AvailableChat = snikket.AvailableChat;
diff --git a/npm/package.json b/npm/package.json
index 7068fb9..9ab64a9 100644
--- a/npm/package.json
+++ b/npm/package.json
@@ -3,10 +3,14 @@
   "version": "0.0.0",
   "description": "Chat SDK",
   "main": "index.js",
+  "type": "module",
   "files": [
     "*.js",
     "*.ts"
   ],
+  "browser": {
+    "./snikket.js": "./snikket-browser.js"
+  },
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
diff --git a/snikket/persistence/browser.js b/snikket/persistence/browser.js
index caf89bb..e7ff5fb 100644
--- a/snikket/persistence/browser.js
+++ b/snikket/persistence/browser.js
@@ -1,8 +1,8 @@
 // This example persistence driver is written in JavaScript
 // so that SDK users can easily see how to write their own
 
-import { snikket as enums } from "./snikket-enums";
-import { snikket } from "./snikket";
+import { snikket as enums } from "./snikket-enums.js";
+import { snikket } from "./snikket.js";
 
 const browser = (dbname, tokenize, stemmer) => {
 	if (!tokenize) tokenize = function(s) { return s.split(" "); }
diff --git a/snikket/streams/XmppJsStream.hx b/snikket/streams/XmppJsStream.hx
index 1beaf92..aceb75f 100644
--- a/snikket/streams/XmppJsStream.hx
+++ b/snikket/streams/XmppJsStream.hx
@@ -11,13 +11,13 @@ import snikket.FSM;
 import snikket.GenericStream;
 import snikket.Stanza;
 
-@:jsRequire("@xmpp/sasl-scram-sha-1")
+@:js.import(@default "@xmpp/sasl-scram-sha-1")
 extern class XmppJsScramSha1 {
 	@:selfCall
 	function new(sasl: Dynamic);
 }
 
-@:jsRequire("@xmpp/client", "client")
+@:js.import("@xmpp/client", "client")
 extern class XmppJsClient {
 	function new(options:Dynamic);
 	function start():Promise<Dynamic>;
@@ -37,7 +37,7 @@ extern class XmppJsClient {
 	var sasl2: Dynamic;
 }
 
-@:jsRequire("@xmpp/jid", "jid")
+@:js.import("@xmpp/jid", "jid")
 extern class XmppJsJID {
 	function new(jid:String);
 	function toString():String;
@@ -47,13 +47,13 @@ extern class XmppJsJID {
 	var resource(default, set):String;
 }
 
-@:jsRequire("@xmpp/debug")
+@:js.import(@default "@xmpp/debug")
 extern class XmppJsDebug {
 	@:selfCall
 	function new(client:XmppJsClient, force:Bool):Void;
 }
 
-@:jsRequire("@xmpp/xml")
+@:js.import(@default "@xmpp/xml")
 extern class XmppJsXml {
 	@:selfCall
 	@:overload(function(tagName:String, ?attr:Dynamic):XmppJsXml { })
@@ -67,7 +67,7 @@ extern class XmppJsXml {
 	var children:Array<Dynamic>;
 }
 
-@:jsRequire("ltx") // The default XML library used by xmpp.js
+@:js.import(@star "ltx") // The default XML library used by xmpp.js
 extern class XmppJsLtx {
 	static function isNode(el:Dynamic):Bool;
 	static function isElement(el:Dynamic):Bool;
@@ -75,13 +75,13 @@ extern class XmppJsLtx {
 	static function parse(input:String):XmppJsXml;
 }
 
-@:jsRequire("@xmpp/id")
+@:js.import(@default "@xmpp/id")
 extern class XmppJsId {
 	@:selfCall
 	static function id():String;
 }
 
-@:jsRequire("@xmpp/error")
+@:js.import(@default "@xmpp/error")
 extern class XmppJsError {
 	public final name: String;
 	public final condition: String;
@@ -129,6 +129,10 @@ class XmppJsStream extends GenericStream {
 	}
 
 	static private function resolveConnectionURI(domain:String, callback:(String)->Void):Void {
+		#if nodejs
+		callback("xmpp://" + domain);
+		return;
+		#else
 		var request = new Http('https://$domain/.well-known/host-meta.json');
 		request.onData = function (data:String) {
 			try {
@@ -146,6 +150,7 @@ class XmppJsStream extends GenericStream {
 			callback(null);
 		}
 		request.request(false);
+		#end
 	}
 
 	private function connectWithURI(uri:String) {