From 13ee20f9ebb23e6b5c3d58cf163ac5b1971ed6ff Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Sun, 22 Dec 2024 23:40:03 +0100 Subject: [PATCH] Remove browser file and use index.js instead --- packages/client/README.md | 4 +- packages/client/browser.js | 92 ------------------------------------ packages/client/example.js | 1 - packages/client/index.js | 19 ++++++-- packages/client/package.json | 8 +++- packages/component/README.md | 2 +- rollup.config.js | 4 +- 7 files changed, 27 insertions(+), 103 deletions(-) delete mode 100644 packages/client/browser.js diff --git a/packages/client/README.md b/packages/client/README.md index f0d62f0d..aeab2f58 100644 --- a/packages/client/README.md +++ b/packages/client/README.md @@ -63,6 +63,8 @@ xmpp.on("stanza", async (stanza) => { }); xmpp.on("online", async (address) => { + console.log("online as", address.toString()); + // Makes itself available await xmpp.send(xml("presence")); @@ -75,7 +77,7 @@ xmpp.on("online", async (address) => { await xmpp.send(message); }); -xmpp.start().catch(console.error); +await xmpp.start(); ``` ## xml diff --git a/packages/client/browser.js b/packages/client/browser.js deleted file mode 100644 index 747ac8ce..00000000 --- a/packages/client/browser.js +++ /dev/null @@ -1,92 +0,0 @@ -import { xml, jid, Client } from "@xmpp/client-core"; -import getDomain from "./lib/getDomain.js"; -import createOnAuthenticate from "./lib/createOnAuthenticate.js"; - -import _reconnect from "@xmpp/reconnect"; -import _websocket from "@xmpp/websocket"; -import _middleware from "@xmpp/middleware"; -import _streamFeatures from "@xmpp/stream-features"; -import _iqCaller from "@xmpp/iq/caller.js"; -import _iqCallee from "@xmpp/iq/callee.js"; -import _resolve from "@xmpp/resolve"; -import _sasl2 from "@xmpp/sasl2"; -import _sasl from "@xmpp/sasl"; -import _resourceBinding from "@xmpp/resource-binding"; -import _sessionEstablishment from "@xmpp/session-establishment"; -import _streamManagement from "@xmpp/stream-management"; - -import SASLFactory from "saslmechanisms"; -import plain from "@xmpp/sasl-plain"; -import anonymous from "@xmpp/sasl-anonymous"; - -function client(options = {}) { - const { resource, credentials, username, password, ...params } = options; - const { clientId, software, device } = params; - - const { domain, service } = params; - if (!domain && service) { - params.domain = getDomain(service); - } - - const entity = new Client(params); - - const reconnect = _reconnect({ entity }); - const websocket = _websocket({ entity }); - - const middleware = _middleware({ entity }); - const streamFeatures = _streamFeatures({ middleware }); - const iqCaller = _iqCaller({ middleware, entity }); - const iqCallee = _iqCallee({ middleware, entity }); - const resolve = _resolve({ entity }); - - // SASL mechanisms - order matters and define priority - const saslFactory = new SASLFactory(); - const mechanisms = Object.entries({ - plain, - anonymous, - }).map(([k, v]) => ({ [k]: v(saslFactory) })); - - // Stream features - order matters and define priority - const sasl2 = _sasl2( - { streamFeatures, saslFactory }, - credentials || { username, password }, - { clientId, software, device }, - ); - const sasl = _sasl( - { streamFeatures, saslFactory }, - createOnAuthenticate(credentials ?? { username, password }), - ); - const streamManagement = _streamManagement({ - streamFeatures, - entity, - middleware, - }); - const resourceBinding = _resourceBinding( - { iqCaller, streamFeatures }, - resource, - ); - const sessionEstablishment = _sessionEstablishment({ - iqCaller, - streamFeatures, - }); - - return Object.assign(entity, { - entity, - reconnect, - websocket, - middleware, - streamFeatures, - iqCaller, - iqCallee, - resolve, - saslFactory, - sasl2, - sasl, - resourceBinding, - sessionEstablishment, - streamManagement, - mechanisms, - }); -} - -export { xml, jid, client }; diff --git a/packages/client/example.js b/packages/client/example.js index 7effe6cf..6bab1abc 100644 --- a/packages/client/example.js +++ b/packages/client/example.js @@ -1,5 +1,4 @@ import { client, xml } from "@xmpp/client"; - // eslint-disable-next-line n/no-extraneous-import import debug from "@xmpp/debug"; diff --git a/packages/client/index.js b/packages/client/index.js index 202a17fd..3bd18496 100644 --- a/packages/client/index.js +++ b/packages/client/index.js @@ -23,6 +23,17 @@ import scramsha1 from "@xmpp/sasl-scram-sha-1"; import plain from "@xmpp/sasl-plain"; import anonymous from "@xmpp/sasl-anonymous"; +// In browsers and react-native some packages are excluded +// see package.json and https://metrobundler.dev/docs/configuration/#resolvermainfields +// in which case the default import returns an empty object +function setupIfAvailable(module, ...args) { + if (typeof module !== "function") { + return undefined; + } + + return module(...args); +} + function client(options = {}) { const { resource, credentials, username, password, ...params } = options; const { clientId, software, device } = params; @@ -36,8 +47,8 @@ function client(options = {}) { const reconnect = _reconnect({ entity }); const websocket = _websocket({ entity }); - const tcp = _tcp({ entity }); - const tls = _tls({ entity }); + const tcp = setupIfAvailable(_tcp, { entity }); + const tls = setupIfAvailable(_tls, { entity }); const middleware = _middleware({ entity }); const streamFeatures = _streamFeatures({ middleware }); @@ -48,13 +59,13 @@ function client(options = {}) { // SASL mechanisms - order matters and define priority const saslFactory = new SASLFactory(); const mechanisms = Object.entries({ - scramsha1, + ...(typeof scramsha1 === "function" && scramsha1), plain, anonymous, }).map(([k, v]) => ({ [k]: v(saslFactory) })); // Stream features - order matters and define priority - const starttls = _starttls({ streamFeatures }); + const starttls = setupIfAvailable(_starttls, { streamFeatures }); const sasl2 = _sasl2( { streamFeatures, saslFactory }, createOnAuthenticate(credentials ?? { username, password }), diff --git a/packages/client/package.json b/packages/client/package.json index f3c7dffd..1f446c7c 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -28,8 +28,12 @@ "@xmpp/websocket": "^0.13.2", "saslmechanisms": "^0.1.1" }, - "browser": "browser.js", - "react-native": "browser.js", + "browser": { + "@xmpp/tcp": false, + "@xmpp/tls": false, + "@xmpp/starttls": false, + "@xmpp/sasl-scram-sha-1": false + }, "engines": { "node": ">= 20" }, diff --git a/packages/component/README.md b/packages/component/README.md index 5c18dd33..0a9c41e4 100644 --- a/packages/component/README.md +++ b/packages/component/README.md @@ -52,7 +52,7 @@ xmpp.on("online", async (address) => { await xmpp.send(message); }); -xmpp.start().catch(console.error); +await xmpp.start(); ``` ## xml diff --git a/rollup.config.js b/rollup.config.js index 2b81fd56..fd709d68 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -6,7 +6,7 @@ import terser from "@rollup/plugin-terser"; export default [ { - input: "packages/client/browser.js", + input: "packages/client/index.js", output: { file: "packages/client/dist/xmpp.js", format: "iife", @@ -21,7 +21,7 @@ export default [ ], }, { - input: "packages/client/browser.js", + input: "packages/client/index.js", output: { file: "packages/client/dist/xmpp.min.js", format: "iife",