varlink-js 0.1.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/.github/workflows/bun.yml +18 -0
- package/.github/workflows/nodejs.yml +18 -0
- package/LICENSE +7 -0
- package/README.md +93 -0
- package/eslint.config.cjs +17 -0
- package/package.json +30 -0
- package/src/client/client.test.ts +138 -0
- package/src/client/client.ts +119 -0
- package/src/protocol/protocol.ts +124 -0
- package/src/schemas/org.varlink.certification.varlink +86 -0
- package/src/schemas/org.varlink.certification.varlink.ts +32 -0
- package/src/schemas/org.varlink.resolver.varlink +18 -0
- package/src/schemas/org.varlink.resolver.varlink.ts +25 -0
- package/src/schemas/org.varlink.service.varlink +29 -0
- package/src/schemas/org.varlink.service.varlink.ts +25 -0
- package/src/server/reference-server.ts +436 -0
- package/src/server/run-reference-server.ts +11 -0
- package/src/server/server.test.ts +41 -0
- package/src/server/server.ts +180 -0
- package/src/transport/node-socket.ts +174 -0
- package/src/transport/transport.ts +58 -0
- package/tsconfig.json +27 -0
- package/tsup.config.ts +19 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: Build and test on Bun
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: oven-sh/setup-bun@v2
|
|
14
|
+
with:
|
|
15
|
+
bun-version: "1.1.19"
|
|
16
|
+
- run: bun install
|
|
17
|
+
- run: bun run build
|
|
18
|
+
- run: bun run test
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: Build on Node.js
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: "20.x"
|
|
16
|
+
- run: npm install
|
|
17
|
+
- run: npm run build
|
|
18
|
+
- run: npm pack ./dist
|
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright © 2024 Pawel Zmarzly
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Varlink protocol implemented in JS/TS/NodeJS
|
|
2
|
+
|
|
3
|
+
- ✅ client and server code
|
|
4
|
+
- ✅ no external dependencies
|
|
5
|
+
- ✅ passes official certification tests
|
|
6
|
+
- ✅ type-safe
|
|
7
|
+
- ✅ async-first
|
|
8
|
+
- ✅ connection pooling
|
|
9
|
+
- ✅ compatible with Node.js and Bun.sh
|
|
10
|
+
- ✅ supports TCP and Unix sockets
|
|
11
|
+
- ❌ no input validation
|
|
12
|
+
- ❌ no parsing and generating of schema IDL
|
|
13
|
+
- ❌ writing servers is a bit clunky (you have to implement `org.varlink.service` by hand)
|
|
14
|
+
- ❌ no socket activation
|
|
15
|
+
- ❌ no proxy mode or HTTP mode (but should be easy to add)
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Installation:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install varlink-js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Client:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { VarlinkClient } from "varlink-js/client/client";
|
|
29
|
+
import { SocketClientSideTransport } from "varlink-js/transport/node-socket";
|
|
30
|
+
|
|
31
|
+
const transport = new SocketClientSideTransport({
|
|
32
|
+
host: "127.0.0.1",
|
|
33
|
+
port: 12345,
|
|
34
|
+
timeout: 10_000,
|
|
35
|
+
});
|
|
36
|
+
const client = new VarlinkClient(transport);
|
|
37
|
+
|
|
38
|
+
// Call any method by name (no type safety)
|
|
39
|
+
import { VarlinkDynamicMethod } from "varlink-js/protocol/protocol";
|
|
40
|
+
const method = new VarlinkDynamicMethod("org.varlink.service.GetInterfaceDescription");
|
|
41
|
+
const response1 = await client.call(method, { interface: "org.varlink.service" });
|
|
42
|
+
console.log(response1.description); // any
|
|
43
|
+
|
|
44
|
+
// Or use type-safe wrappers
|
|
45
|
+
// (Currently NOT autogenerated)
|
|
46
|
+
import { OrgVarlinkService } from "varlink-js/schemas/org.varlink.service.varlink";
|
|
47
|
+
const response2 = await client.call(OrgVarlinkService.GetInterfaceDescription, { interface: "org.varlink.service" });
|
|
48
|
+
console.log(response2.description); // string
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Server:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { VarlinkServer } from "varlink-js/server/server";
|
|
55
|
+
import { SocketServerSideTransport } from "varlink-js/transport/node-socket";
|
|
56
|
+
import { OrgVarlinkService } from "varlink-js/schemas/org.varlink.service.varlink";
|
|
57
|
+
|
|
58
|
+
const transport = new SocketServerSideTransport({
|
|
59
|
+
timeout: 10_000,
|
|
60
|
+
port: 12345,
|
|
61
|
+
});
|
|
62
|
+
const server = new VarlinkServer(transport);
|
|
63
|
+
server.registerCall(OrgVarlinkService.GetInfo, async (_input) => ({
|
|
64
|
+
vendor: "varlink-js",
|
|
65
|
+
product: "varlink-js",
|
|
66
|
+
version: "0.0.1",
|
|
67
|
+
url: "https://example.com",
|
|
68
|
+
interfaces: ["org.varlink.service"],
|
|
69
|
+
}));
|
|
70
|
+
await server.start();
|
|
71
|
+
console.log(transport.address());
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
See [`client.test.ts`](src/client/client.test.ts) and [`reference-server.ts`](src/server/reference-server.ts) for full usage.
|
|
75
|
+
|
|
76
|
+
## Certification
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# JS client, JS server
|
|
80
|
+
bun run test
|
|
81
|
+
|
|
82
|
+
# JS client, Python server
|
|
83
|
+
python3 -m varlink.tests.test_certification --varlink=tcp:127.0.0.1:12345
|
|
84
|
+
PORT=12345 bun run test
|
|
85
|
+
|
|
86
|
+
# JS server, Python client
|
|
87
|
+
PORT=12345 bun run src/server/run-reference-server.ts
|
|
88
|
+
python3 -m varlink.tests.test_certification --client --varlink=tcp:127.0.0.1:12345
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = [
|
|
2
|
+
{
|
|
3
|
+
files: ["**/*.js", "**/*.cjs", "**/*.mjs", "**/*.ts"],
|
|
4
|
+
},
|
|
5
|
+
{
|
|
6
|
+
ignores: ["dist/", "node_modules/"],
|
|
7
|
+
},
|
|
8
|
+
require("eslint-config-love"),
|
|
9
|
+
require("eslint-plugin-prettier/recommended"),
|
|
10
|
+
{
|
|
11
|
+
rules: {
|
|
12
|
+
"@typescript-eslint/class-methods-use-this": "off",
|
|
13
|
+
"@typescript-eslint/consistent-type-assertions": "off",
|
|
14
|
+
"@typescript-eslint/no-non-null-assertion": "off",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
];
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "varlink-js",
|
|
3
|
+
"description": "Varlink client and server for Node.js and Bun.sh",
|
|
4
|
+
"author": "Pawel Zmarzly <pawo2500@gmail.com>",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"varlink"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/pzmarzly/varlink-js",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/pzmarzly/varlink-js"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"version": "0.1.0",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bun": "^1.1.6",
|
|
17
|
+
"@types/node": "^20.14.11",
|
|
18
|
+
"eslint": "^8.57.0",
|
|
19
|
+
"eslint-config-love": "^56.0.0",
|
|
20
|
+
"eslint-config-prettier": "^9.1.0",
|
|
21
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
22
|
+
"tsup": "^8.2.2",
|
|
23
|
+
"typescript": "^5.5.3"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc && eslint . && tsup && cp README.md package.json LICENSE dist",
|
|
27
|
+
"test": "bun test",
|
|
28
|
+
"fmt": "eslint --fix ."
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { test, expect, beforeAll } from "bun:test";
|
|
2
|
+
import { SocketClientSideTransport } from "../transport/node-socket";
|
|
3
|
+
import { VarlinkDynamicMethod } from "../protocol/protocol";
|
|
4
|
+
import { OrgVarlinkService } from "../schemas/org.varlink.service.varlink.ts";
|
|
5
|
+
import { VarlinkClient } from "./client";
|
|
6
|
+
import { OrgVarlinkCertification } from "../schemas/org.varlink.certification.varlink.ts";
|
|
7
|
+
import { ReferenceServer } from "../server/reference-server.ts";
|
|
8
|
+
|
|
9
|
+
let client: VarlinkClient;
|
|
10
|
+
beforeAll(async () => {
|
|
11
|
+
let transport;
|
|
12
|
+
if (process.env.PORT !== undefined) {
|
|
13
|
+
transport = new SocketClientSideTransport({
|
|
14
|
+
host: "127.0.0.1",
|
|
15
|
+
// python3 -m varlink.tests.test_certification --varlink=tcp:127.0.0.1:12345
|
|
16
|
+
port: Number(process.env.PORT),
|
|
17
|
+
timeout: 10_000,
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
const server = new ReferenceServer();
|
|
21
|
+
await server.start();
|
|
22
|
+
const address = server.address();
|
|
23
|
+
if (address === null || typeof address !== "object") {
|
|
24
|
+
throw new Error(`unknown address type ${address}`);
|
|
25
|
+
}
|
|
26
|
+
transport = new SocketClientSideTransport({
|
|
27
|
+
timeout: 10_000,
|
|
28
|
+
host: address.address,
|
|
29
|
+
port: address.port,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
client = new VarlinkClient(transport);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("connects to the reference server (dynamic)", async () => {
|
|
36
|
+
const GetInfo = new VarlinkDynamicMethod("org.varlink.service.GetInfo");
|
|
37
|
+
const info = await client.call(GetInfo, {});
|
|
38
|
+
expect(info).toBeObject();
|
|
39
|
+
expect(info.interfaces).toBeArray();
|
|
40
|
+
expect(info.interfaces.toSorted()).toEqual([
|
|
41
|
+
"org.varlink.certification",
|
|
42
|
+
"org.varlink.service",
|
|
43
|
+
]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("connects to the reference server (typed)", async () => {
|
|
47
|
+
const info = await client.call(OrgVarlinkService.GetInfo, {});
|
|
48
|
+
const interfaces = info.interfaces;
|
|
49
|
+
interfaces.sort();
|
|
50
|
+
expect(interfaces).toEqual([
|
|
51
|
+
"org.varlink.certification",
|
|
52
|
+
"org.varlink.service",
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("exposes error details", async () => {
|
|
57
|
+
const UnknownMethod = new VarlinkDynamicMethod(
|
|
58
|
+
"org.varlink.unknown.UnknownMethod",
|
|
59
|
+
);
|
|
60
|
+
expect(async () => await client.call(UnknownMethod, {})).toThrow(
|
|
61
|
+
"org.varlink.unknown",
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("passes reference tests", async () => {
|
|
66
|
+
const returnValueStart = await client.call(OrgVarlinkCertification.Start, {});
|
|
67
|
+
|
|
68
|
+
const returnValue01 = await client.call(OrgVarlinkCertification.Test01, {
|
|
69
|
+
client_id: returnValueStart.client_id,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const returnValue02 = await client.call(OrgVarlinkCertification.Test02, {
|
|
73
|
+
client_id: returnValueStart.client_id,
|
|
74
|
+
bool: returnValue01.bool,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const returnValue03 = await client.call(OrgVarlinkCertification.Test03, {
|
|
78
|
+
client_id: returnValueStart.client_id,
|
|
79
|
+
int: returnValue02.int,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const returnValue04 = await client.call(OrgVarlinkCertification.Test04, {
|
|
83
|
+
client_id: returnValueStart.client_id,
|
|
84
|
+
float: returnValue03.float,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const returnValue05 = await client.call(OrgVarlinkCertification.Test05, {
|
|
88
|
+
client_id: returnValueStart.client_id,
|
|
89
|
+
string: returnValue04.string,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const returnValue06 = await client.call(OrgVarlinkCertification.Test06, {
|
|
93
|
+
client_id: returnValueStart.client_id,
|
|
94
|
+
bool: returnValue05.bool,
|
|
95
|
+
int: returnValue05.int,
|
|
96
|
+
float: returnValue05.float,
|
|
97
|
+
string: returnValue05.string,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const returnValue07 = await client.call(OrgVarlinkCertification.Test07, {
|
|
101
|
+
client_id: returnValueStart.client_id,
|
|
102
|
+
struct: returnValue06.struct,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const returnValue08 = await client.call(OrgVarlinkCertification.Test08, {
|
|
106
|
+
client_id: returnValueStart.client_id,
|
|
107
|
+
map: returnValue07.map,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const returnValue09 = await client.call(OrgVarlinkCertification.Test09, {
|
|
111
|
+
client_id: returnValueStart.client_id,
|
|
112
|
+
set: returnValue08.set,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const returnValue10: any[] = [];
|
|
116
|
+
await client.callStream(
|
|
117
|
+
OrgVarlinkCertification.Test10,
|
|
118
|
+
{
|
|
119
|
+
client_id: returnValueStart.client_id,
|
|
120
|
+
mytype: returnValue09.mytype,
|
|
121
|
+
},
|
|
122
|
+
async (error, data) => {
|
|
123
|
+
expect(error).toBeUndefined();
|
|
124
|
+
returnValue10.push(data);
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
await client.callOneshot(OrgVarlinkCertification.Test11, {
|
|
129
|
+
client_id: returnValueStart.client_id,
|
|
130
|
+
last_more_replies: returnValue10.map((x) => x.string),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const returnValueEnd = await client.call(
|
|
134
|
+
OrgVarlinkCertification.End,
|
|
135
|
+
returnValueStart,
|
|
136
|
+
);
|
|
137
|
+
expect(returnValueEnd.all_ok).toBeTrue();
|
|
138
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type VarlinkClientSideTransport,
|
|
3
|
+
type VarlinkClientSideTransportChannel,
|
|
4
|
+
} from "../transport/transport";
|
|
5
|
+
import {
|
|
6
|
+
VarlinkError,
|
|
7
|
+
type VarlinkMethod,
|
|
8
|
+
type VarlinkMethodGetInput,
|
|
9
|
+
type VarlinkMethodGetOutput,
|
|
10
|
+
} from "../protocol/protocol";
|
|
11
|
+
|
|
12
|
+
export class VarlinkClient {
|
|
13
|
+
private pool: VarlinkClientSideTransportChannel[] = [];
|
|
14
|
+
constructor(private readonly transport: VarlinkClientSideTransport) {}
|
|
15
|
+
|
|
16
|
+
async connect(): Promise<void> {
|
|
17
|
+
const chan = await this.#takeFromPool();
|
|
18
|
+
await this.#backToPool(chan);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async disconnect(): Promise<void> {
|
|
22
|
+
await this.#drainPool();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async call<M extends VarlinkMethod<string, any, any>>(
|
|
26
|
+
method: M,
|
|
27
|
+
input: VarlinkMethodGetInput<M>,
|
|
28
|
+
): Promise<VarlinkMethodGetOutput<M>> {
|
|
29
|
+
const chan = await this.#takeFromPool();
|
|
30
|
+
await chan.send({
|
|
31
|
+
method: method.name,
|
|
32
|
+
parameters: input,
|
|
33
|
+
oneway: false,
|
|
34
|
+
more: false,
|
|
35
|
+
upgrade: false,
|
|
36
|
+
});
|
|
37
|
+
const resp = await chan.recv();
|
|
38
|
+
await this.#backToPool(chan);
|
|
39
|
+
|
|
40
|
+
if (resp.error !== undefined) {
|
|
41
|
+
throw new VarlinkError(resp.error, resp.parameters);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return resp.parameters as VarlinkMethodGetOutput<M>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async callOneshot<M extends VarlinkMethod<string, any, any>>(
|
|
48
|
+
method: M,
|
|
49
|
+
input: VarlinkMethodGetInput<M>,
|
|
50
|
+
): Promise<void> {
|
|
51
|
+
const chan = await this.#takeFromPool();
|
|
52
|
+
await chan.send({
|
|
53
|
+
method: method.name,
|
|
54
|
+
parameters: input,
|
|
55
|
+
oneway: true,
|
|
56
|
+
more: false,
|
|
57
|
+
upgrade: false,
|
|
58
|
+
});
|
|
59
|
+
await this.#backToPool(chan);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async callStream<M extends VarlinkMethod<string, any, any>>(
|
|
63
|
+
method: M,
|
|
64
|
+
input: VarlinkMethodGetInput<M>,
|
|
65
|
+
callbackFunction: (
|
|
66
|
+
error: VarlinkError | undefined,
|
|
67
|
+
data: VarlinkMethodGetOutput<M>,
|
|
68
|
+
) => Promise<void>,
|
|
69
|
+
): Promise<void> {
|
|
70
|
+
const chan = await this.#takeFromPool();
|
|
71
|
+
await chan.send({
|
|
72
|
+
method: method.name,
|
|
73
|
+
parameters: input,
|
|
74
|
+
oneway: false,
|
|
75
|
+
more: true,
|
|
76
|
+
upgrade: false,
|
|
77
|
+
});
|
|
78
|
+
let continues = true;
|
|
79
|
+
while (continues) {
|
|
80
|
+
const resp = await chan.recv();
|
|
81
|
+
if (resp.error !== undefined) {
|
|
82
|
+
await callbackFunction(
|
|
83
|
+
new VarlinkError(resp.error, resp.parameters),
|
|
84
|
+
{} as VarlinkMethodGetOutput<M>,
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
await callbackFunction(
|
|
88
|
+
undefined,
|
|
89
|
+
resp.parameters as VarlinkMethodGetOutput<M>,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
continues = resp.continues ?? false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
await this.#backToPool(chan);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async #takeFromPool(): Promise<VarlinkClientSideTransportChannel> {
|
|
99
|
+
if (this.pool.length === 0) {
|
|
100
|
+
return await this.transport.connect();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return this.pool.shift()!;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async #backToPool(chan: VarlinkClientSideTransportChannel): Promise<void> {
|
|
107
|
+
this.pool.push(chan);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async #drainPool(): Promise<void> {
|
|
111
|
+
const pool = this.pool;
|
|
112
|
+
this.pool = [];
|
|
113
|
+
await Promise.all(
|
|
114
|
+
pool.map(async (chan) => {
|
|
115
|
+
await chan.close();
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
export type VarlinkDictionary = Record<string, any>;
|
|
2
|
+
|
|
3
|
+
export class VarlinkError extends Error {
|
|
4
|
+
constructor(
|
|
5
|
+
public type_: string,
|
|
6
|
+
public parameters: VarlinkDictionary,
|
|
7
|
+
) {
|
|
8
|
+
super(`${type_} ${JSON.stringify(parameters)}`);
|
|
9
|
+
this.name = this.constructor.name;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class VarlinkMethod<
|
|
14
|
+
Name extends string,
|
|
15
|
+
Input extends VarlinkDictionary,
|
|
16
|
+
Output extends VarlinkDictionary,
|
|
17
|
+
> {
|
|
18
|
+
constructor(
|
|
19
|
+
public name: Name,
|
|
20
|
+
private readonly inputPhantom: Input,
|
|
21
|
+
private readonly outputPhantom: Output,
|
|
22
|
+
) {}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type VarlinkMethodGetName<M> =
|
|
26
|
+
M extends VarlinkMethod<infer Name, any, any> ? Name : never;
|
|
27
|
+
export type VarlinkMethodGetInput<M> =
|
|
28
|
+
M extends VarlinkMethod<any, infer Input, any> ? Input : never;
|
|
29
|
+
export type VarlinkMethodGetOutput<M> =
|
|
30
|
+
M extends VarlinkMethod<any, any, infer Output> ? Output : never;
|
|
31
|
+
|
|
32
|
+
export class VarlinkDynamicMethod extends VarlinkMethod<
|
|
33
|
+
string,
|
|
34
|
+
VarlinkDictionary,
|
|
35
|
+
VarlinkDictionary
|
|
36
|
+
> {
|
|
37
|
+
constructor(name: string) {
|
|
38
|
+
super(name, {}, {});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface VarlinkRequest {
|
|
43
|
+
method: string;
|
|
44
|
+
parameters: VarlinkDictionary;
|
|
45
|
+
oneway: boolean;
|
|
46
|
+
more: boolean;
|
|
47
|
+
upgrade: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function serializeVarlinkRequest(request: VarlinkRequest): Uint8Array {
|
|
51
|
+
const partial: Partial<VarlinkRequest> = { method: request.method };
|
|
52
|
+
|
|
53
|
+
if (Object.keys(request.parameters).length > 0) {
|
|
54
|
+
partial.parameters = request.parameters;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (request.oneway) {
|
|
58
|
+
partial.oneway = request.oneway;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (request.more) {
|
|
62
|
+
partial.more = request.more;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (request.upgrade) {
|
|
66
|
+
partial.upgrade = request.upgrade;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return new TextEncoder().encode(JSON.stringify(partial));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function deserializeVarlinkRequest(buffer: Uint8Array): VarlinkRequest {
|
|
73
|
+
const partial = JSON.parse(
|
|
74
|
+
new TextDecoder().decode(buffer),
|
|
75
|
+
) as VarlinkRequest;
|
|
76
|
+
partial.parameters ||= {};
|
|
77
|
+
partial.oneway ||= false;
|
|
78
|
+
partial.more ||= false;
|
|
79
|
+
partial.upgrade ||= false;
|
|
80
|
+
return partial;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface VarlinkSuccessResponse {
|
|
84
|
+
error: undefined;
|
|
85
|
+
parameters: VarlinkDictionary;
|
|
86
|
+
continues: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface VarlinkErrorResponse {
|
|
90
|
+
error: string;
|
|
91
|
+
parameters: VarlinkDictionary;
|
|
92
|
+
continues: undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type VarlinkResponse = VarlinkSuccessResponse | VarlinkErrorResponse;
|
|
96
|
+
|
|
97
|
+
export function serializeVarlinkResponse(
|
|
98
|
+
response: VarlinkResponse,
|
|
99
|
+
): Uint8Array {
|
|
100
|
+
const partial: Partial<VarlinkResponse> = { parameters: response.parameters };
|
|
101
|
+
|
|
102
|
+
if (response.error !== undefined) {
|
|
103
|
+
partial.error = response.error;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (response.continues === true) {
|
|
107
|
+
partial.continues = response.continues;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return new TextEncoder().encode(JSON.stringify(response));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function deserializeVarlinkResponse(
|
|
114
|
+
buffer: Uint8Array,
|
|
115
|
+
): VarlinkResponse {
|
|
116
|
+
const partial = JSON.parse(
|
|
117
|
+
new TextDecoder().decode(buffer),
|
|
118
|
+
) as VarlinkResponse;
|
|
119
|
+
if (partial.error === undefined) {
|
|
120
|
+
partial.continues ||= false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return partial;
|
|
124
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Interface to test varlink implementations against.
|
|
2
|
+
# First you write a varlink client calling:
|
|
3
|
+
# Start, Test01, Test02, …, Test09, End
|
|
4
|
+
# The return value of the previous call should be the argument of the next call.
|
|
5
|
+
# Then you test this client against well known servers like python or rust from
|
|
6
|
+
# https://github.com/varlink/
|
|
7
|
+
#
|
|
8
|
+
# Next you write a varlink server providing the same service as the well known ones.
|
|
9
|
+
# Now run your client against it and run well known clients like python or rust
|
|
10
|
+
# from https://github.com/varlink/ against your server. If all works out, then
|
|
11
|
+
# your new language bindings should be varlink certified.
|
|
12
|
+
interface org.varlink.certification
|
|
13
|
+
|
|
14
|
+
type Interface (
|
|
15
|
+
foo: ?[]?[string](foo, bar, baz),
|
|
16
|
+
anon: (foo: bool, bar: bool)
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
type MyType (
|
|
20
|
+
object: object,
|
|
21
|
+
enum: (one, two, three),
|
|
22
|
+
struct: (first: int, second: string),
|
|
23
|
+
array: []string,
|
|
24
|
+
dictionary: [string]string,
|
|
25
|
+
stringset: [string](),
|
|
26
|
+
nullable: ?string,
|
|
27
|
+
nullable_array_struct: ?[](first: int, second: string),
|
|
28
|
+
interface: Interface
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
method Start() -> (client_id: string)
|
|
32
|
+
|
|
33
|
+
method Test01(client_id: string) -> (bool: bool)
|
|
34
|
+
|
|
35
|
+
method Test02(client_id: string, bool: bool) -> (int: int)
|
|
36
|
+
|
|
37
|
+
method Test03(client_id: string, int: int) -> (float: float)
|
|
38
|
+
|
|
39
|
+
method Test04(client_id: string, float: float) -> (string: string)
|
|
40
|
+
|
|
41
|
+
method Test05(client_id: string, string: string) -> (
|
|
42
|
+
bool: bool,
|
|
43
|
+
int: int,
|
|
44
|
+
float: float,
|
|
45
|
+
string: string
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
method Test06(
|
|
49
|
+
client_id: string,
|
|
50
|
+
bool: bool,
|
|
51
|
+
int: int,
|
|
52
|
+
float: float,
|
|
53
|
+
string: string
|
|
54
|
+
) -> (
|
|
55
|
+
struct: (
|
|
56
|
+
bool: bool,
|
|
57
|
+
int: int,
|
|
58
|
+
float: float,
|
|
59
|
+
string: string
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
method Test07(
|
|
64
|
+
client_id: string,
|
|
65
|
+
struct: (
|
|
66
|
+
bool: bool,
|
|
67
|
+
int: int,
|
|
68
|
+
float: float,
|
|
69
|
+
string: string
|
|
70
|
+
)
|
|
71
|
+
) -> (map: [string]string)
|
|
72
|
+
|
|
73
|
+
method Test08(client_id: string, map: [string]string) -> (set: [string]())
|
|
74
|
+
|
|
75
|
+
method Test09(client_id: string, set: [string]()) -> (mytype: MyType)
|
|
76
|
+
|
|
77
|
+
# returns more than one reply with "continues"
|
|
78
|
+
method Test10(client_id: string, mytype: MyType) -> (string: string)
|
|
79
|
+
|
|
80
|
+
# must be called as "oneway"
|
|
81
|
+
method Test11(client_id: string, last_more_replies: []string) -> ()
|
|
82
|
+
|
|
83
|
+
method End(client_id: string) -> (all_ok: bool)
|
|
84
|
+
|
|
85
|
+
error ClientIdError ()
|
|
86
|
+
error CertificationError (wants: object, got: object)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { VarlinkDynamicMethod } from "../protocol/protocol";
|
|
2
|
+
|
|
3
|
+
const Start = new VarlinkDynamicMethod("org.varlink.certification.Start");
|
|
4
|
+
const Test01 = new VarlinkDynamicMethod("org.varlink.certification.Test01");
|
|
5
|
+
const Test02 = new VarlinkDynamicMethod("org.varlink.certification.Test02");
|
|
6
|
+
const Test03 = new VarlinkDynamicMethod("org.varlink.certification.Test03");
|
|
7
|
+
const Test04 = new VarlinkDynamicMethod("org.varlink.certification.Test04");
|
|
8
|
+
const Test05 = new VarlinkDynamicMethod("org.varlink.certification.Test05");
|
|
9
|
+
const Test06 = new VarlinkDynamicMethod("org.varlink.certification.Test06");
|
|
10
|
+
const Test07 = new VarlinkDynamicMethod("org.varlink.certification.Test07");
|
|
11
|
+
const Test08 = new VarlinkDynamicMethod("org.varlink.certification.Test08");
|
|
12
|
+
const Test09 = new VarlinkDynamicMethod("org.varlink.certification.Test09");
|
|
13
|
+
const Test10 = new VarlinkDynamicMethod("org.varlink.certification.Test10");
|
|
14
|
+
const Test11 = new VarlinkDynamicMethod("org.varlink.certification.Test11");
|
|
15
|
+
const End = new VarlinkDynamicMethod("org.varlink.certification.End");
|
|
16
|
+
|
|
17
|
+
export const OrgVarlinkCertification = {
|
|
18
|
+
interface: "org.varlink.certification",
|
|
19
|
+
Start,
|
|
20
|
+
Test01,
|
|
21
|
+
Test02,
|
|
22
|
+
Test03,
|
|
23
|
+
Test04,
|
|
24
|
+
Test05,
|
|
25
|
+
Test06,
|
|
26
|
+
Test07,
|
|
27
|
+
Test08,
|
|
28
|
+
Test09,
|
|
29
|
+
Test10,
|
|
30
|
+
Test11,
|
|
31
|
+
End,
|
|
32
|
+
};
|