Skip to content

Commit 36be83f

Browse files
committed
add @spinframework/spin-mqtt
Signed-off-by: karthik2804 <[email protected]>
1 parent a29350f commit 36be83f

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/spin-mqtt/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@spinframework/spin-mqtt",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"type": "module",
8+
"scripts": {
9+
"build": "tsc && cp -r types/* dist/"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "Apache-2.0",
14+
"devDependencies": {
15+
"typescript": "^5.7.3"
16+
},
17+
"config": {
18+
"wasiDep": {
19+
"witDeps": [
20+
{
21+
"witPath": "./wit",
22+
"package": "fermyon:[email protected]",
23+
"world": "spin-mqtt"
24+
}
25+
]
26+
}
27+
}
28+
}

packages/spin-mqtt/src/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as spinMqtt from 'fermyon:spin/[email protected]';
2+
3+
/**
4+
* Enum representing the Quality of Service (QoS) levels for MQTT.
5+
* @enum {string}
6+
*/
7+
export enum QoS {
8+
/** Messages are delivered at most once. */
9+
AtMostOnce = 'at-most-once',
10+
/** Messages are delivered at least once. */
11+
AtLeastOnce = 'at-least-once',
12+
/** Messages are delivered exactly once. */
13+
ExactlyOnce = 'exactly-once',
14+
}
15+
16+
/**
17+
* Interface representing an MQTT connection with a method for publishing messages.
18+
* @interface MqttConnection
19+
*/
20+
export interface MqttConnection {
21+
/**
22+
* Publishes a message to the specified MQTT topic.
23+
* @param topic - The topic to publish the message to.
24+
* @param payload - The message payload as a Uint8Array.
25+
* @param qos - The Quality of Service level for message delivery.
26+
*/
27+
publish: (topic: string, payload: Uint8Array, qos: QoS) => void;
28+
}
29+
30+
/**
31+
* Opens an MQTT connection with the specified parameters.
32+
* @param {string} address - The address of the MQTT broker.
33+
* @param {string} username - The username for the MQTT connection.
34+
* @param {string} password - The password for the MQTT connection.
35+
* @param {bigint} keepAliveIntervalInSecs - The keep-alive interval in seconds.
36+
* @returns {MqttConnection} The MQTT connection object.
37+
*/
38+
export function open(
39+
address: string,
40+
username: string,
41+
password: string,
42+
keepAliveIntervalInSecs: bigint,
43+
): MqttConnection {
44+
return spinMqtt.Connection.open(
45+
address,
46+
username,
47+
password,
48+
keepAliveIntervalInSecs,
49+
);
50+
}

packages/spin-mqtt/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "ES2020",
5+
"lib": [
6+
"ES2020",
7+
"WebWorker"
8+
],
9+
"moduleResolution": "node",
10+
"declaration": true,
11+
"outDir": "dist",
12+
"strict": true,
13+
"esModuleInterop": true,
14+
},
15+
"exclude": [
16+
"node_modules",
17+
"dist"
18+
]
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
declare module 'fermyon:spin/[email protected]' {
2+
/**
3+
* Errors related to interacting with Mqtt
4+
*/
5+
export type Error = ErrorInvalidAddress | ErrorTooManyConnections | ErrorConnectionFailed | ErrorOther;
6+
/**
7+
* An invalid address string
8+
*/
9+
export interface ErrorInvalidAddress {
10+
tag: 'invalid-address',
11+
}
12+
/**
13+
* There are too many open connections
14+
*/
15+
export interface ErrorTooManyConnections {
16+
tag: 'too-many-connections',
17+
}
18+
/**
19+
* Connection failure e.g. address not allowed.
20+
*/
21+
export interface ErrorConnectionFailed {
22+
tag: 'connection-failed',
23+
val: string,
24+
}
25+
/**
26+
* Some other error occurred
27+
*/
28+
export interface ErrorOther {
29+
tag: 'other',
30+
val: string,
31+
}
32+
/**
33+
* QoS for publishing Mqtt messages
34+
* # Variants
35+
*
36+
* ## `"at-most-once"`
37+
*
38+
* ## `"at-least-once"`
39+
*
40+
* ## `"exactly-once"`
41+
*/
42+
export type Qos = 'at-most-once' | 'at-least-once' | 'exactly-once';
43+
/**
44+
* The message payload.
45+
*/
46+
export type Payload = Uint8Array;
47+
48+
export class Connection {
49+
/**
50+
* Open a connection to the Mqtt instance at `address`.
51+
*/
52+
static open(address: string, username: string, password: string, keepAliveIntervalInSecs: bigint): Connection;
53+
/**
54+
* Publish an Mqtt message to the specified `topic`.
55+
*/
56+
publish(topic: string, payload: Payload, qos: Qos): void;
57+
}
58+
59+
}

packages/spin-mqtt/wit/world.wit

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fermyon:spin@2.0.0;
2+
3+
interface mqtt {
4+
/// Errors related to interacting with Mqtt
5+
variant error {
6+
/// An invalid address string
7+
invalid-address,
8+
/// There are too many open connections
9+
too-many-connections,
10+
/// Connection failure e.g. address not allowed.
11+
connection-failed(string),
12+
/// Some other error occurred
13+
other(string),
14+
}
15+
16+
/// QoS for publishing Mqtt messages
17+
enum qos {
18+
at-most-once,
19+
at-least-once,
20+
exactly-once,
21+
}
22+
23+
resource connection {
24+
/// Open a connection to the Mqtt instance at `address`.
25+
open: static func(address: string, username: string, password: string, keep-alive-interval-in-secs: u64) -> result<connection, error>;
26+
27+
/// Publish an Mqtt message to the specified `topic`.
28+
publish: func(topic: string, payload: payload, qos: qos) -> result<_, error>;
29+
}
30+
31+
/// The message payload.
32+
type payload = list<u8>;
33+
}
34+
35+
world spin-mqtt {
36+
import mqtt;
37+
}

0 commit comments

Comments
 (0)