Skip to content

Commit

Permalink
Remove browser file and use index.js instead
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 22, 2024
1 parent e560649 commit 40a28ff
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 105 deletions.
4 changes: 3 additions & 1 deletion packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand All @@ -75,7 +77,7 @@ xmpp.on("online", async (address) => {
await xmpp.send(message);
});

xmpp.start().catch(console.error);
await xmpp.start();
```

## xml
Expand Down
92 changes: 0 additions & 92 deletions packages/client/browser.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/client/example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { client, xml } from "@xmpp/client";

// eslint-disable-next-line n/no-extraneous-import
import debug from "@xmpp/debug";

Expand Down
19 changes: 15 additions & 4 deletions packages/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand All @@ -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 }),
Expand Down
8 changes: 6 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ xmpp.on("online", async (address) => {
await xmpp.send(message);
});

xmpp.start().catch(console.error);
await xmpp.start();
```

## xml
Expand Down
2 changes: 1 addition & 1 deletion packages/sasl/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ test("prefers SCRAM-SHA-1", async () => {
<features xmlns="http://etherx.jabber.org/streams">
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
<mechanism>ANONYMOUS</mechanism>
<mechanism>PLAIN</mechanism>
<mechanism>SCRAM-SHA-1</mechanism>
<mechanism>PLAIN</mechanism>
</mechanisms>
</features>,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/sasl2/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ test("prefers SCRAM-SHA-1", async () => {
<features xmlns="http://etherx.jabber.org/streams">
<authentication xmlns="urn:xmpp:sasl:2">
<mechanism>ANONYMOUS</mechanism>
<mechanism>PLAIN</mechanism>
<mechanism>SCRAM-SHA-1</mechanism>
<mechanism>PLAIN</mechanism>
</authentication>
</features>,
);
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 40a28ff

Please sign in to comment.