@seriousme/opifex 1.11.1 → 1.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ import { exit } from "node:process";
2
+ import { importClientClass } from "./utils.ts";
3
+
4
+ const { TcpClient } = await importClientClass();
5
+ const client = new TcpClient();
6
+ client.onError = (err) => {
7
+ console.log("client error", err);
8
+ exit(1);
9
+ };
10
+
11
+ await client.connect({
12
+ url: new URL("mqtt://localhost:1883"),
13
+ });
14
+
15
+ await client.subscribe({ subscriptions: [{ topicFilter: "test", qos: 0 }] });
16
+
17
+ let counter = 0;
18
+ const interval = 5000;
19
+
20
+ function count() {
21
+ console.log("received/s", (counter / interval) * 1000);
22
+ counter = 0;
23
+ }
24
+
25
+ setInterval(count, interval);
26
+
27
+ // // Consume using a callback
28
+ // client.onPacket = async (packet) => {
29
+ // counter++;
30
+ // };
31
+
32
+ // Consume using an AsyncIterator
33
+ for await (const _ of client.messages()) {
34
+ counter++;
35
+ }
@@ -0,0 +1,32 @@
1
+ import { exit } from "node:process";
2
+ import { runAsap } from "../dev_utils/timers.ts";
3
+ import { importClientClass } from "./utils.ts";
4
+
5
+ const { TcpClient } = await importClientClass();
6
+ const client = new TcpClient();
7
+ client.onError = (err) => {
8
+ console.log("client error", err);
9
+ exit(1);
10
+ };
11
+
12
+ await client.connect({
13
+ url: new URL("mqtt://localhost:1883"),
14
+ });
15
+
16
+ const payload = new TextEncoder().encode("payload");
17
+ let sent = 0;
18
+ const interval = 5000;
19
+
20
+ const loop = () => {
21
+ sent++;
22
+ client.publish({ topic: "test", payload })
23
+ .then(() => runAsap(loop));
24
+ };
25
+
26
+ function count() {
27
+ console.log("sent/s", (sent / interval) * 1000);
28
+ sent = 0;
29
+ }
30
+
31
+ setInterval(count, interval);
32
+ loop();
@@ -1,32 +1,9 @@
1
- import type { Client } from "../client/client.ts";
2
-
3
- export type ClientClass = new () => Client;
4
-
5
- export type Main = (clientClass: ClientClass) => void | Promise<void>;
6
-
7
- // @ts-ignore: the Deno global variable is not defined in @types/node,
8
- // therefore we need to ignore this error because this file
9
- // is meant to be used in both environments.
10
- const isDeno = typeof Deno !== "undefined";
11
-
12
- // @ts-ignore: the Deno global variable is not defined in @types/node,
13
- // therefore we need to ignore this error because this file
14
- // is meant to be used in both environments.
15
- // deno-lint-ignore no-process-global
16
- const exit = isDeno ? Deno.exit : process.exit;
17
-
18
- const importClientClass = async (): Promise<ClientClass> => {
1
+ export async function importClientClass() {
2
+ // @ts-ignore: the Deno global variable is not defined in @types/node,
3
+ // therefore we need to ignore this error because this file
4
+ // is meant to be used in both environments.
5
+ const isDeno = typeof Deno !== "undefined";
19
6
  return isDeno
20
- ? (await import("../deno/tcpClient.ts")).TcpClient
21
- : (await import("../node/tcpClient.ts")).TcpClient;
22
- };
23
-
24
- export const loader = async (main: Main) => {
25
- const TcpClient = await importClientClass();
26
- try {
27
- await main(TcpClient);
28
- } catch (err) {
29
- console.error(err);
30
- exit(1);
31
- }
32
- };
7
+ ? (await import("../deno/tcpClient.ts"))
8
+ : (await import("../node/tcpClient.ts"));
9
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"handlePublish.d.ts","sourceRoot":"","sources":["../../../server/handlers/handlePublish.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAS,MAAM,YAAY,CAAC;AAkBvD;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,IAAI,CAAC,CAiCf"}
1
+ {"version":3,"file":"handlePublish.d.ts","sourceRoot":"","sources":["../../../server/handlers/handlePublish.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAS,MAAM,YAAY,CAAC;AAkBvD;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,IAAI,CAAC,CAiDf"}
@@ -43,9 +43,24 @@ export async function handlePublish(ctx, packet) {
43
43
  });
44
44
  return;
45
45
  }
46
- // qos 2
46
+ /*
47
+ In the QoS 2 delivery protocol, the Receiver
48
+
49
+ - MUST respond with a PUBREC containing the Packet Identifier from the incoming PUBLISH Packet,
50
+ having accepted ownership of the Application Message.
51
+ - Until it has received the corresponding PUBREL packet, the Receiver MUST acknowledge any subsequent
52
+ PUBLISH packet with the same Packet Identifier by sending a PUBREC. It MUST NOT cause duplicate
53
+ messages to be delivered to any onward recipients in this case.
54
+ - MUST respond to a PUBREL packet by sending a PUBCOMP packet containing the same Packet Identifier as the PUBREL.
55
+ - After it has sent a PUBCOMP, the receiver MUST treat any subsequent PUBLISH packet that contains that Packet
56
+ Identifier as being a new publication.
57
+ [MQTT-4.3.3-2].
58
+ */
47
59
  if (ctx.store) {
48
- ctx.store.pendingIncoming.set(packet.id, packet);
60
+ if (!ctx.store.pendingIncoming.has(packet.id)) {
61
+ ctx.store.pendingIncoming.set(packet.id, packet);
62
+ ctx.persistence.publish(packet.topic, packet);
63
+ }
49
64
  await ctx.send({
50
65
  type: PacketType.pubrec,
51
66
  protocolLevel: ctx.protocolLevel,
@@ -1 +1 @@
1
- {"version":3,"file":"handlePublish.js","sourceRoot":"","sources":["../../../server/handlers/handlePublish.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,GAAY,EAAE,KAAY;IACrD,IAAI,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,GAAG,CAAC,QAAQ,C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AC;YAC9C,MAAM,GAAG,CAAC,IAAI,CAAC;gBACb,IAAI,EAAE,UAAU,CAAC,MAAM;gBACvB,aAAa,EAAE,GAAG,CAAC,aAAa;gBAChC,EAAE;aACH,CAAC,CAAC;YACH,OAAO;QACT,CAAC;QAED;;;;;;;;;;;;UAYE;QACF,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBAC9C,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;gBACjD,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAChD,CAAC;YACD,MAAM,GAAG,CAAC,IAAI,CAAC;gBACb,IAAI,EAAE,UAAU,CAAC,MAAM;gBACvB,aAAa,EAAE,GAAG,CAAC,aAAa;gBAChC,EAAE,EAAE,MAAM,CAAC,EAAE;aACd,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"handlePubrel.d.ts","sourceRoot":"","sources":["../../../server/handlers/handlePubrel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,IAAI,CAAC,CAcf"}
1
+ {"version":3,"file":"handlePubrel.d.ts","sourceRoot":"","sources":["../../../server/handlers/handlePubrel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,IAAI,CAAC,CAUf"}
@@ -14,16 +14,12 @@ import { PacketType } from "../deps.js";
14
14
  export async function handlePubrel(ctx, packet) {
15
15
  const id = packet.id;
16
16
  if (ctx.store?.pendingIncoming.has(id)) {
17
- const storedPacket = ctx.store.pendingIncoming.get(id);
18
- if (storedPacket) {
19
- ctx.persistence.publish(storedPacket.topic, storedPacket);
20
- ctx.store.pendingIncoming.delete(id);
21
- await ctx.send({
22
- type: PacketType.pubcomp,
23
- protocolLevel: ctx.protocolLevel,
24
- id,
25
- });
26
- }
17
+ ctx.store.pendingIncoming.delete(id);
27
18
  }
19
+ await ctx.send({
20
+ type: PacketType.pubcomp,
21
+ protocolLevel: ctx.protocolLevel,
22
+ id,
23
+ });
28
24
  }
29
25
  //# sourceMappingURL=handlePubrel.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"handlePubrel.js","sourceRoot":"","sources":["../../../server/handlers/handlePubrel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,GAAY,EACZ,MAAoB;IAEpB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,IAAI,GAAG,CAAC,KAAK,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;QACvC,MAAM,YAAY,GAAG,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACvD,IAAI,YAAY,EAAE,CAAC;YACjB,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;YAC1D,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACrC,MAAM,GAAG,CAAC,IAAI,CAAC;gBACb,IAAI,EAAE,UAAU,CAAC,OAAO;gBACxB,aAAa,EAAE,GAAG,CAAC,aAAa;gBAChC,EAAE;aACH,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"handlePubrel.js","sourceRoot":"","sources":["../../../server/handlers/handlePubrel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,GAAY,EACZ,MAAoB;IAEpB,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACrB,IAAI,GAAG,CAAC,KAAK,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;QACvC,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACvC,CAAC;IACD,MAAM,GAAG,CAAC,IAAI,CAAC;QACb,IAAI,EAAE,UAAU,CAAC,OAAO;QACxB,aAAa,EAAE,GAAG,CAAC,aAAa;QAChC,EAAE;KACH,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seriousme/opifex",
3
- "version": "1.11.1",
3
+ "version": "1.11.2",
4
4
  "description": "MQTT client & server for Deno & NodeJS",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -52,9 +52,25 @@ export async function handlePublish(
52
52
  });
53
53
  return;
54
54
  }
55
- // qos 2
55
+
56
+ /*
57
+ In the QoS 2 delivery protocol, the Receiver
58
+
59
+ - MUST respond with a PUBREC containing the Packet Identifier from the incoming PUBLISH Packet,
60
+ having accepted ownership of the Application Message.
61
+ - Until it has received the corresponding PUBREL packet, the Receiver MUST acknowledge any subsequent
62
+ PUBLISH packet with the same Packet Identifier by sending a PUBREC. It MUST NOT cause duplicate
63
+ messages to be delivered to any onward recipients in this case.
64
+ - MUST respond to a PUBREL packet by sending a PUBCOMP packet containing the same Packet Identifier as the PUBREL.
65
+ - After it has sent a PUBCOMP, the receiver MUST treat any subsequent PUBLISH packet that contains that Packet
66
+ Identifier as being a new publication.
67
+ [MQTT-4.3.3-2].
68
+ */
56
69
  if (ctx.store) {
57
- ctx.store.pendingIncoming.set(packet.id, packet);
70
+ if (!ctx.store.pendingIncoming.has(packet.id)) {
71
+ ctx.store.pendingIncoming.set(packet.id, packet);
72
+ ctx.persistence.publish(packet.topic, packet);
73
+ }
58
74
  await ctx.send({
59
75
  type: PacketType.pubrec,
60
76
  protocolLevel: ctx.protocolLevel,
@@ -20,15 +20,11 @@ export async function handlePubrel(
20
20
  ): Promise<void> {
21
21
  const id = packet.id;
22
22
  if (ctx.store?.pendingIncoming.has(id)) {
23
- const storedPacket = ctx.store.pendingIncoming.get(id);
24
- if (storedPacket) {
25
- ctx.persistence.publish(storedPacket.topic, storedPacket);
26
- ctx.store.pendingIncoming.delete(id);
27
- await ctx.send({
28
- type: PacketType.pubcomp,
29
- protocolLevel: ctx.protocolLevel,
30
- id,
31
- });
32
- }
23
+ ctx.store.pendingIncoming.delete(id);
33
24
  }
25
+ await ctx.send({
26
+ type: PacketType.pubcomp,
27
+ protocolLevel: ctx.protocolLevel,
28
+ id,
29
+ });
34
30
  }
@@ -1,35 +0,0 @@
1
- import { loader } from "./utils.ts";
2
- import { runAsap } from "../dev_utils/timers.ts";
3
-
4
- loader((ClientClass) => {
5
- const client = new ClientClass();
6
-
7
- client.connect({
8
- url: new URL("mqtt://127.0.0.1:1884"),
9
- });
10
-
11
- client.onConnected = () => {
12
- const payload = new TextEncoder().encode("payload");
13
-
14
- let sent = 0;
15
- const interval = 5000;
16
-
17
- const loop = () => {
18
- sent++;
19
- client.publish({ topic: "test", payload })
20
- .then(() => runAsap(loop));
21
- };
22
-
23
- function count() {
24
- console.log("sent/s", (sent / interval) * 1000);
25
- sent = 0;
26
- }
27
-
28
- setInterval(count, interval);
29
- loop();
30
- };
31
-
32
- client.onError = (err) => {
33
- console.log("client error", err);
34
- };
35
- });
@@ -1,31 +0,0 @@
1
- import { loader } from "./utils.ts";
2
-
3
- loader(async (ClientClass) => {
4
- const client = new ClientClass();
5
-
6
- await client.connect({
7
- url: new URL("mqtt://127.0.0.1:1884"),
8
- });
9
-
10
- await client.subscribe({ subscriptions: [{ topicFilter: "test", qos: 0 }] });
11
-
12
- let counter = 0;
13
- const interval = 5000;
14
-
15
- function count() {
16
- console.log("received/s", (counter / interval) * 1000);
17
- counter = 0;
18
- }
19
-
20
- setInterval(count, interval);
21
-
22
- // // Consume using a callback
23
- // client.onPacket = async (packet) => {
24
- // counter++;
25
- // };
26
-
27
- // Consume using an AsyncIterator
28
- for await (const _ of client.messages()) {
29
- counter++;
30
- }
31
- });