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.
@@ -0,0 +1,18 @@
1
+ # Interface to resolve reverse-domain interface names to
2
+ # service adresses
3
+ interface org.varlink.resolver
4
+
5
+ # Get a list of all resolvable interfaces and information
6
+ # about the resolver's identity.
7
+ method GetInfo() -> (
8
+ vendor: string,
9
+ product: string,
10
+ version: string,
11
+ url: string,
12
+ interfaces: []string
13
+ )
14
+
15
+ # Resolve an interface name to a registered varlink service address
16
+ method Resolve(interface: string) -> (address: string)
17
+
18
+ error InterfaceNotFound (interface: string)
@@ -0,0 +1,25 @@
1
+ import { VarlinkMethod } from "../protocol/protocol";
2
+
3
+ const GetInfo = new VarlinkMethod(
4
+ "org.varlink.resolver.GetInfo",
5
+ {} as object,
6
+ {} as {
7
+ interfaces: string[];
8
+ vendor: string;
9
+ product: string;
10
+ version: string;
11
+ url: string;
12
+ },
13
+ );
14
+
15
+ const Resolve = new VarlinkMethod(
16
+ "org.varlink.resolver.Resolve",
17
+ {} as { interface: string },
18
+ {} as { address: string },
19
+ );
20
+
21
+ export const OrgVarlinkResolver = {
22
+ interface: "org.varlink.resolver",
23
+ GetInfo,
24
+ Resolve,
25
+ };
@@ -0,0 +1,29 @@
1
+ # The Varlink Service Interface is provided by every varlink service. It
2
+ # describes the service and the interfaces it implements.
3
+ interface org.varlink.service
4
+
5
+ # Get a list of all the interfaces a service provides and information
6
+ # about the implementation.
7
+ method GetInfo() -> (
8
+ vendor: string,
9
+ product: string,
10
+ version: string,
11
+ url: string,
12
+ interfaces: []string
13
+ )
14
+
15
+ # Get the description of an interface that is implemented by this service.
16
+ method GetInterfaceDescription(interface: string) -> (description: string)
17
+
18
+ # The requested interface was not found.
19
+ error InterfaceNotFound (interface: string)
20
+
21
+ # The requested method was not found
22
+ error MethodNotFound (method: string)
23
+
24
+ # The interface defines the requested method, but the service does not
25
+ # implement it.
26
+ error MethodNotImplemented (method: string)
27
+
28
+ # One of the passed parameters is invalid.
29
+ error InvalidParameter (parameter: string)
@@ -0,0 +1,25 @@
1
+ import { VarlinkMethod } from "../protocol/protocol";
2
+
3
+ const GetInfo = new VarlinkMethod(
4
+ "org.varlink.service.GetInfo",
5
+ {} as object,
6
+ {} as {
7
+ interfaces: string[];
8
+ vendor: string;
9
+ product: string;
10
+ version: string;
11
+ url: string;
12
+ },
13
+ );
14
+
15
+ const GetInterfaceDescription = new VarlinkMethod(
16
+ "org.varlink.service.GetInterfaceDescription",
17
+ {} as { interface: string },
18
+ {} as { description: string },
19
+ );
20
+
21
+ export const OrgVarlinkService = {
22
+ interface: "org.varlink.service",
23
+ GetInfo,
24
+ GetInterfaceDescription,
25
+ };
@@ -0,0 +1,436 @@
1
+ import type { AddressInfo } from "node:net";
2
+ import { OrgVarlinkService } from "../schemas/org.varlink.service.varlink.ts";
3
+ import { SocketServerSideTransport } from "../transport/node-socket";
4
+ import { VarlinkServer } from "./server";
5
+ import { OrgVarlinkCertification } from "../schemas/org.varlink.certification.varlink.ts";
6
+ import { randomBytes } from "node:crypto";
7
+ import { VarlinkError, type VarlinkDictionary } from "../protocol/protocol.ts";
8
+ import { readFileSync } from "node:fs";
9
+
10
+ class ClientIdError extends VarlinkError {
11
+ constructor() {
12
+ super("org.varlink.certification.ClientIdError", {});
13
+ }
14
+ }
15
+
16
+ class CertificationError extends VarlinkError {
17
+ constructor(wants: VarlinkDictionary, got: VarlinkDictionary) {
18
+ super("org.varlink.certification.CertificationError", { wants, got });
19
+ }
20
+ }
21
+
22
+ export class ReferenceServer {
23
+ private readonly transport: SocketServerSideTransport;
24
+ private readonly server: VarlinkServer;
25
+ private readonly clientStates = new Map<string, string>();
26
+
27
+ constructor(port: number = 0) {
28
+ this.transport = new SocketServerSideTransport({
29
+ timeout: 10_000,
30
+ port,
31
+ });
32
+ this.server = new VarlinkServer(this.transport);
33
+
34
+ this.server.registerCall(
35
+ OrgVarlinkService.GetInfo,
36
+ this.#getInfo.bind(this),
37
+ );
38
+
39
+ this.server.registerCall(
40
+ OrgVarlinkService.GetInterfaceDescription,
41
+ this.#getInterfaceDescription.bind(this),
42
+ );
43
+
44
+ this.server.registerCall(
45
+ OrgVarlinkCertification.Start,
46
+ this.#start.bind(this),
47
+ );
48
+
49
+ this.server.registerCall(
50
+ OrgVarlinkCertification.Test01,
51
+ this.#test01.bind(this),
52
+ );
53
+
54
+ this.server.registerCall(
55
+ OrgVarlinkCertification.Test02,
56
+ this.#test02.bind(this),
57
+ );
58
+
59
+ this.server.registerCall(
60
+ OrgVarlinkCertification.Test03,
61
+ this.#test03.bind(this),
62
+ );
63
+
64
+ this.server.registerCall(
65
+ OrgVarlinkCertification.Test04,
66
+ this.#test04.bind(this),
67
+ );
68
+
69
+ this.server.registerCall(
70
+ OrgVarlinkCertification.Test05,
71
+ this.#test05.bind(this),
72
+ );
73
+
74
+ this.server.registerCall(
75
+ OrgVarlinkCertification.Test06,
76
+ this.#test06.bind(this),
77
+ );
78
+
79
+ this.server.registerCall(
80
+ OrgVarlinkCertification.Test07,
81
+ this.#test07.bind(this),
82
+ );
83
+
84
+ this.server.registerCall(
85
+ OrgVarlinkCertification.Test08,
86
+ this.#test08.bind(this),
87
+ );
88
+
89
+ this.server.registerCall(
90
+ OrgVarlinkCertification.Test09,
91
+ this.#test09.bind(this),
92
+ );
93
+
94
+ this.server.registerCallStream(
95
+ OrgVarlinkCertification.Test10,
96
+ this.#test10.bind(this),
97
+ );
98
+
99
+ this.server.registerCallOneshot(
100
+ OrgVarlinkCertification.Test11,
101
+ this.#test11.bind(this),
102
+ );
103
+
104
+ this.server.registerCall(OrgVarlinkCertification.End, this.#end.bind(this));
105
+ }
106
+
107
+ async start(): Promise<void> {
108
+ await this.server.start();
109
+ }
110
+
111
+ async stop(): Promise<void> {
112
+ await this.server.stop();
113
+ }
114
+
115
+ address(): AddressInfo | string | null {
116
+ return this.transport.address();
117
+ }
118
+
119
+ async #getInfo(input: object): Promise<{
120
+ interfaces: string[];
121
+ vendor: string;
122
+ product: string;
123
+ version: string;
124
+ url: string;
125
+ }> {
126
+ return {
127
+ vendor: "varlink-js",
128
+ product: "varlink-js",
129
+ version: "0.0.1",
130
+ url: "https://example.com",
131
+ interfaces: ["org.varlink.certification", "org.varlink.service"],
132
+ };
133
+ }
134
+
135
+ async #getInterfaceDescription(input: {
136
+ interface: string;
137
+ }): Promise<{ description: string }> {
138
+ const description = readFileSync(
139
+ new URL(`../schemas/${input.interface}.varlink`, import.meta.url),
140
+ "utf-8",
141
+ );
142
+ return {
143
+ description,
144
+ };
145
+ }
146
+
147
+ async #start(input: object): Promise<{ client_id: string }> {
148
+ const clientId = randomBytes(20).toString("hex");
149
+ this.clientStates.set(clientId, "Start");
150
+ return {
151
+ client_id: clientId,
152
+ };
153
+ }
154
+
155
+ async #test01(input: VarlinkDictionary): Promise<VarlinkDictionary> {
156
+ if (!this.clientStates.has(input.client_id as string)) {
157
+ throw new ClientIdError();
158
+ }
159
+ if (this.clientStates.get(input.client_id as string) !== "Start") {
160
+ throw new ClientIdError();
161
+ }
162
+
163
+ const wants = { client_id: input.client_id };
164
+ if (!isEqual(wants, input)) {
165
+ throw new CertificationError(wants, input);
166
+ }
167
+
168
+ this.clientStates.set(input.client_id as string, "Test01");
169
+ return { bool: true };
170
+ }
171
+
172
+ async #test02(input: VarlinkDictionary): Promise<VarlinkDictionary> {
173
+ if (!this.clientStates.has(input.client_id as string)) {
174
+ throw new ClientIdError();
175
+ }
176
+ if (this.clientStates.get(input.client_id as string) !== "Test01") {
177
+ throw new ClientIdError();
178
+ }
179
+
180
+ const wants = { client_id: input.client_id, bool: true };
181
+ if (!isEqual(wants, input)) {
182
+ throw new CertificationError(wants, input);
183
+ }
184
+
185
+ this.clientStates.set(input.client_id as string, "Test02");
186
+ return { int: 42 };
187
+ }
188
+
189
+ async #test03(input: VarlinkDictionary): Promise<VarlinkDictionary> {
190
+ if (!this.clientStates.has(input.client_id as string)) {
191
+ throw new ClientIdError();
192
+ }
193
+ if (this.clientStates.get(input.client_id as string) !== "Test02") {
194
+ throw new ClientIdError();
195
+ }
196
+
197
+ const wants = { client_id: input.client_id, int: 42 };
198
+ if (!isEqual(wants, input)) {
199
+ throw new CertificationError(wants, input);
200
+ }
201
+
202
+ this.clientStates.set(input.client_id as string, "Test03");
203
+ return { float: Math.PI };
204
+ }
205
+
206
+ async #test04(input: VarlinkDictionary): Promise<VarlinkDictionary> {
207
+ if (!this.clientStates.has(input.client_id as string)) {
208
+ throw new ClientIdError();
209
+ }
210
+ if (this.clientStates.get(input.client_id as string) !== "Test03") {
211
+ throw new ClientIdError();
212
+ }
213
+
214
+ const wants = { client_id: input.client_id, float: Math.PI };
215
+ if (!isEqual(wants, input)) {
216
+ throw new CertificationError(wants, input);
217
+ }
218
+
219
+ this.clientStates.set(input.client_id as string, "Test04");
220
+ return { string: "varlink" };
221
+ }
222
+
223
+ async #test05(input: VarlinkDictionary): Promise<VarlinkDictionary> {
224
+ if (!this.clientStates.has(input.client_id as string)) {
225
+ throw new ClientIdError();
226
+ }
227
+ if (this.clientStates.get(input.client_id as string) !== "Test04") {
228
+ throw new ClientIdError();
229
+ }
230
+
231
+ const wants = { client_id: input.client_id, string: "varlink" };
232
+ if (!isEqual(wants, input)) {
233
+ throw new CertificationError(wants, input);
234
+ }
235
+
236
+ this.clientStates.set(input.client_id as string, "Test05");
237
+ return { bool: true, int: 42, float: Math.PI, string: "varlink" };
238
+ }
239
+
240
+ async #test06(input: VarlinkDictionary): Promise<VarlinkDictionary> {
241
+ if (!this.clientStates.has(input.client_id as string)) {
242
+ throw new ClientIdError();
243
+ }
244
+ if (this.clientStates.get(input.client_id as string) !== "Test05") {
245
+ throw new ClientIdError();
246
+ }
247
+
248
+ const wants = {
249
+ client_id: input.client_id,
250
+ bool: true,
251
+ int: 42,
252
+ float: Math.PI,
253
+ string: "varlink",
254
+ };
255
+ if (!isEqual(wants, input)) {
256
+ throw new CertificationError(wants, input);
257
+ }
258
+
259
+ this.clientStates.set(input.client_id as string, "Test06");
260
+ return {
261
+ struct: { bool: true, int: 42, float: Math.PI, string: "varlink" },
262
+ };
263
+ }
264
+
265
+ async #test07(input: VarlinkDictionary): Promise<VarlinkDictionary> {
266
+ if (!this.clientStates.has(input.client_id as string)) {
267
+ throw new ClientIdError();
268
+ }
269
+ if (this.clientStates.get(input.client_id as string) !== "Test06") {
270
+ throw new ClientIdError();
271
+ }
272
+
273
+ const wants = {
274
+ client_id: input.client_id,
275
+ struct: {
276
+ bool: true,
277
+ int: 42,
278
+ float: Math.PI,
279
+ string: "varlink",
280
+ },
281
+ };
282
+ if (!isEqual(wants, input)) {
283
+ throw new CertificationError(wants, input);
284
+ }
285
+
286
+ this.clientStates.set(input.client_id as string, "Test07");
287
+ return {
288
+ map: { varlink: "awesome", dbus: "not awesome" },
289
+ };
290
+ }
291
+
292
+ async #test08(input: VarlinkDictionary): Promise<VarlinkDictionary> {
293
+ if (!this.clientStates.has(input.client_id as string)) {
294
+ throw new ClientIdError();
295
+ }
296
+ if (this.clientStates.get(input.client_id as string) !== "Test07") {
297
+ throw new ClientIdError();
298
+ }
299
+
300
+ const wants = {
301
+ client_id: input.client_id,
302
+ map: { varlink: "awesome", dbus: "not awesome" },
303
+ };
304
+ if (!isEqual(wants, input)) {
305
+ throw new CertificationError(wants, input);
306
+ }
307
+
308
+ this.clientStates.set(input.client_id as string, "Test08");
309
+ return {
310
+ set: { awesome: {}, "not awesome": {} },
311
+ };
312
+ }
313
+
314
+ async #test09(input: VarlinkDictionary): Promise<VarlinkDictionary> {
315
+ if (!this.clientStates.has(input.client_id as string)) {
316
+ throw new ClientIdError();
317
+ }
318
+ if (this.clientStates.get(input.client_id as string) !== "Test08") {
319
+ throw new ClientIdError();
320
+ }
321
+
322
+ const wants = {
323
+ client_id: input.client_id,
324
+ set: { awesome: {}, "not awesome": {} },
325
+ };
326
+ if (!isEqual(wants, input)) {
327
+ throw new CertificationError(wants, input);
328
+ }
329
+
330
+ this.clientStates.set(input.client_id as string, "Test09");
331
+ return {
332
+ mytype: {
333
+ object: {},
334
+ enum: "one",
335
+ struct: { first: 42, second: "42" },
336
+ array: ["a", "b", "c"],
337
+ dictionary: { first: "42", second: "21" },
338
+ stringset: { a: {}, b: {}, c: {} },
339
+ nullable: undefined,
340
+ nullable_array_struct: undefined,
341
+ interface: {
342
+ foo: undefined,
343
+ anon: { foo: true, bar: false },
344
+ },
345
+ },
346
+ };
347
+ }
348
+
349
+ async #test10(
350
+ input: VarlinkDictionary,
351
+ callback: (output: VarlinkDictionary, continues: boolean) => Promise<void>,
352
+ ): Promise<void> {
353
+ if (!this.clientStates.has(input.client_id as string)) {
354
+ throw new ClientIdError();
355
+ }
356
+ if (this.clientStates.get(input.client_id as string) !== "Test09") {
357
+ throw new ClientIdError();
358
+ }
359
+
360
+ const wants = {
361
+ client_id: input.client_id,
362
+ mytype: {
363
+ object: {},
364
+ enum: "one",
365
+ struct: { first: 42, second: "42" },
366
+ array: ["a", "b", "c"],
367
+ dictionary: { first: "42", second: "21" },
368
+ stringset: { a: {}, b: {}, c: {} },
369
+ interface: {
370
+ anon: { foo: true, bar: false },
371
+ },
372
+ },
373
+ };
374
+ if (!isEqual(wants, input)) {
375
+ throw new CertificationError(wants, input);
376
+ }
377
+
378
+ this.clientStates.set(input.client_id as string, "Test10");
379
+ await callback({ string: "a" }, true);
380
+ await callback({ string: "b" }, true);
381
+ await callback({ string: "c" }, false);
382
+ }
383
+
384
+ async #test11(input: VarlinkDictionary): Promise<void> {
385
+ if (!this.clientStates.has(input.client_id as string)) {
386
+ throw new ClientIdError();
387
+ }
388
+ if (this.clientStates.get(input.client_id as string) !== "Test10") {
389
+ throw new ClientIdError();
390
+ }
391
+
392
+ const wants = {
393
+ client_id: input.client_id,
394
+ last_more_replies: ["a", "b", "c"],
395
+ };
396
+ if (!isEqual(wants, input)) {
397
+ throw new CertificationError(wants, input);
398
+ }
399
+
400
+ this.clientStates.set(input.client_id as string, "Test11");
401
+ }
402
+
403
+ async #end(input: VarlinkDictionary): Promise<VarlinkDictionary> {
404
+ if (!this.clientStates.has(input.client_id as string)) {
405
+ throw new ClientIdError();
406
+ }
407
+ if (this.clientStates.get(input.client_id as string) !== "Test11") {
408
+ throw new ClientIdError();
409
+ }
410
+
411
+ const wants = {
412
+ client_id: input.client_id,
413
+ };
414
+ if (!isEqual(wants, input)) {
415
+ throw new CertificationError(wants, input);
416
+ }
417
+
418
+ this.clientStates.set(input.client_id as string, "End");
419
+ return { all_ok: true };
420
+ }
421
+ }
422
+
423
+ function isEqual<T>(a: T, b: T): boolean {
424
+ if (a === b) {
425
+ return true;
426
+ }
427
+
428
+ const bothAreObjects =
429
+ a !== null && b !== null && typeof a === "object" && typeof b === "object";
430
+
431
+ return Boolean(
432
+ bothAreObjects &&
433
+ Object.keys(a).length === Object.keys(b).length &&
434
+ Object.entries(a).every(([k, v]) => isEqual(v, b[k as keyof T])),
435
+ );
436
+ }
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bun run
2
+ import { ReferenceServer } from "./reference-server";
3
+
4
+ let port = 0;
5
+ if (process.env.PORT !== undefined) {
6
+ port = Number(process.env.PORT);
7
+ }
8
+
9
+ const server = new ReferenceServer(port);
10
+ await server.start();
11
+ console.log(server.address());
@@ -0,0 +1,41 @@
1
+ import { test } from "bun:test";
2
+ import { SocketServerSideTransport } from "../transport/node-socket";
3
+ import { VarlinkServer } from "./server";
4
+ import { OrgVarlinkService } from "../schemas/org.varlink.service.varlink.ts";
5
+
6
+ test("transport starts and stops", async () => {
7
+ const transport = new SocketServerSideTransport({
8
+ timeout: 10_000,
9
+ port: 12346,
10
+ });
11
+ transport.onClientConnected((chan) => {
12
+ console.log(chan);
13
+ });
14
+ await transport.start();
15
+ await transport.stop();
16
+ });
17
+
18
+ test("server starts and stops", async () => {
19
+ const transport = new SocketServerSideTransport({
20
+ timeout: 10_000,
21
+ port: 12346,
22
+ });
23
+ const server = new VarlinkServer(transport);
24
+ await server.start();
25
+ await server.stop();
26
+ });
27
+
28
+ test("serves interface", async () => {
29
+ const transport = new SocketServerSideTransport({
30
+ timeout: 10_000,
31
+ port: 12346,
32
+ });
33
+ const server = new VarlinkServer(transport);
34
+ server.registerCall(OrgVarlinkService.GetInfo, async (_input) => ({
35
+ vendor: "varlink-js",
36
+ product: "varlink-js",
37
+ version: "0.0.1",
38
+ url: "https://example.com",
39
+ interfaces: [],
40
+ }));
41
+ });