mq-bridge 0.2.0
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.
- package/README.md +41 -0
- package/index.d.ts +45 -0
- package/index.js +144 -0
- package/mq_bridge_node.node +0 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# mq-bridge Node bindings
|
|
2
|
+
|
|
3
|
+
Native Node.js bindings for `mq-bridge`, built with `napi-rs`.
|
|
4
|
+
|
|
5
|
+
This package is server-side only. It loads a native `.node` addon and does not run in browsers.
|
|
6
|
+
|
|
7
|
+
## Build
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install
|
|
11
|
+
npm run build:basic
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
For the fastest local smoke test, build only the HTTP + middleware surface used
|
|
15
|
+
by the example:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm run build:ci
|
|
19
|
+
npm run example
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Handler shape
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { Message, Route } from "@mq-bridge/node";
|
|
26
|
+
|
|
27
|
+
const route = Route.fromYamlStr(config, "orders");
|
|
28
|
+
|
|
29
|
+
route.withHandler(async (message) => {
|
|
30
|
+
const data = message.json();
|
|
31
|
+
return Message.fromJson({ ok: true, data });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
route.addHandler("order.created", async (data) => {
|
|
35
|
+
return Message.fromJson({ seen: data });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
route.start();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Middleware is configured through the normal mq-bridge route config.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type JsonValue =
|
|
2
|
+
| null
|
|
3
|
+
| boolean
|
|
4
|
+
| number
|
|
5
|
+
| string
|
|
6
|
+
| JsonValue[]
|
|
7
|
+
| { [key: string]: JsonValue };
|
|
8
|
+
|
|
9
|
+
export type Metadata = Record<string, string>;
|
|
10
|
+
export type HandlerResult = Message | null | undefined;
|
|
11
|
+
export type MessageHandler = (message: Message) => HandlerResult | Promise<HandlerResult>;
|
|
12
|
+
export type JsonHandler = (data: JsonValue) => HandlerResult | Promise<HandlerResult>;
|
|
13
|
+
|
|
14
|
+
export class Message {
|
|
15
|
+
constructor(payload: Buffer | Uint8Array, metadata?: Metadata | null, id?: string | null);
|
|
16
|
+
static fromJson(data: JsonValue, metadata?: Metadata | null, id?: string | null): Message;
|
|
17
|
+
get payload(): Buffer;
|
|
18
|
+
get metadata(): Metadata;
|
|
19
|
+
get id(): string | null;
|
|
20
|
+
json(): JsonValue;
|
|
21
|
+
text(): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Publisher {
|
|
25
|
+
static fromYaml(path: string, name: string): Publisher;
|
|
26
|
+
static fromYamlStr(text: string, name: string): Publisher;
|
|
27
|
+
static fromConfig(config: JsonValue, name: string): Publisher;
|
|
28
|
+
send(message: Message): void;
|
|
29
|
+
request(message: Message): Message;
|
|
30
|
+
sendJson(data: JsonValue, metadata?: Metadata | null, id?: string | null): void;
|
|
31
|
+
requestJson(data: JsonValue, metadata?: Metadata | null, id?: string | null): Message;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class Route {
|
|
35
|
+
static fromYaml(path: string, name: string): Route;
|
|
36
|
+
static fromYamlStr(text: string, name: string): Route;
|
|
37
|
+
static fromConfig(config: JsonValue, name: string): Route;
|
|
38
|
+
withHandler(handler: MessageHandler): void;
|
|
39
|
+
addHandler(kind: string, handler: JsonHandler): void;
|
|
40
|
+
start(): void;
|
|
41
|
+
stop(): void;
|
|
42
|
+
join(): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const version: string;
|
package/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const native = require("./mq_bridge_node.node");
|
|
4
|
+
|
|
5
|
+
class Message {
|
|
6
|
+
constructor(payload, metadata = null, id = null) {
|
|
7
|
+
this._native = native.createMessage(Buffer.from(payload), metadata, id);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static fromJson(data, metadata = null, id = null) {
|
|
11
|
+
return Message._fromNative(native.messageFromJson(data, metadata, id));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static _fromNative(raw) {
|
|
15
|
+
const message = Object.create(Message.prototype);
|
|
16
|
+
message._native = raw;
|
|
17
|
+
return message;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static _toNative(message) {
|
|
21
|
+
if (message instanceof Message) {
|
|
22
|
+
return message._native;
|
|
23
|
+
}
|
|
24
|
+
return message;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static _toNativeResult(result) {
|
|
28
|
+
if (result == null) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return Message._toNative(result);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get payload() {
|
|
35
|
+
return Buffer.from(this._native.payload);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get metadata() {
|
|
39
|
+
return { ...(this._native.metadata || {}) };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get id() {
|
|
43
|
+
return this._native.id || null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
json() {
|
|
47
|
+
return native.messageJson(this._native);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
text() {
|
|
51
|
+
return native.messageText(this._native);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class Publisher {
|
|
56
|
+
constructor(nativePublisher) {
|
|
57
|
+
this._native = nativePublisher;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static fromYaml(path, name) {
|
|
61
|
+
return new Publisher(native.Publisher.fromYaml(path, name));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static fromYamlStr(text, name) {
|
|
65
|
+
return new Publisher(native.Publisher.fromYamlStr(text, name));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static fromConfig(config, name) {
|
|
69
|
+
return new Publisher(native.Publisher.fromConfig(config, name));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
send(message) {
|
|
73
|
+
this._native.send(Message._toNative(message));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
request(message) {
|
|
77
|
+
return Message._fromNative(this._native.request(Message._toNative(message)));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
sendJson(data, metadata = null, id = null) {
|
|
81
|
+
this._native.sendJson(data, metadata, id);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
requestJson(data, metadata = null, id = null) {
|
|
85
|
+
return Message._fromNative(this._native.requestJson(data, metadata, id));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
class Route {
|
|
90
|
+
constructor(nativeRoute) {
|
|
91
|
+
this._native = nativeRoute;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static fromYaml(path, name) {
|
|
95
|
+
return new Route(native.Route.fromYaml(path, name));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static fromYamlStr(text, name) {
|
|
99
|
+
return new Route(native.Route.fromYamlStr(text, name));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static fromConfig(config, name) {
|
|
103
|
+
return new Route(native.Route.fromConfig(config, name));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
withHandler(handler) {
|
|
107
|
+
this._native.withHandler(async (error, message) => {
|
|
108
|
+
if (error) {
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
const result = await handler(Message._fromNative(message));
|
|
112
|
+
return Message._toNativeResult(result);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
addHandler(kind, handler) {
|
|
117
|
+
this._native.addHandler(kind, async (error, data) => {
|
|
118
|
+
if (error) {
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
const result = await handler(data);
|
|
122
|
+
return Message._toNativeResult(result);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
start() {
|
|
127
|
+
this._native.start();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
stop() {
|
|
131
|
+
this._native.stop();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
join() {
|
|
135
|
+
this._native.join();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
Message,
|
|
141
|
+
Publisher,
|
|
142
|
+
Route,
|
|
143
|
+
version: native.version,
|
|
144
|
+
};
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mq-bridge",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Node.js bindings for mq-bridge",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/marcomq/mq-bridge.git",
|
|
9
|
+
"directory": "node/mq-bridge-node"
|
|
10
|
+
},
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"types": "index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"*.node"
|
|
17
|
+
],
|
|
18
|
+
"napi": {
|
|
19
|
+
"binaryName": "mq_bridge_node"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "napi build --release",
|
|
23
|
+
"build:basic": "napi build --release --no-default-features --features \"basic middleware\" --dts native.d.ts",
|
|
24
|
+
"build:ci": "napi build --no-default-features --features \"http middleware\" --dts native.d.ts",
|
|
25
|
+
"build:debug": "napi build",
|
|
26
|
+
"example": "node examples/http-handler.js",
|
|
27
|
+
"test": "node --test test/*.test.js",
|
|
28
|
+
"artifacts": "napi artifacts"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@napi-rs/cli": "^3.0.0"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18"
|
|
35
|
+
}
|
|
36
|
+
}
|