varlink-js 0.1.0 → 0.1.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.
Files changed (72) hide show
  1. package/chunk-QUYAB4HT.js +42 -0
  2. package/chunk-QUYAB4HT.js.map +1 -0
  3. package/chunk-XDPGXLHF.js +90 -0
  4. package/chunk-XDPGXLHF.js.map +1 -0
  5. package/client/client.cjs +139 -0
  6. package/client/client.cjs.map +1 -0
  7. package/client/client.d.cts +16 -0
  8. package/client/client.d.ts +16 -0
  9. package/client/client.js +102 -0
  10. package/client/client.js.map +1 -0
  11. package/package.json +4 -3
  12. package/protocol/protocol.cjs +107 -0
  13. package/protocol/protocol.cjs.map +1 -0
  14. package/protocol/protocol.d.cts +42 -0
  15. package/protocol/protocol.d.ts +42 -0
  16. package/protocol/protocol.js +19 -0
  17. package/protocol/protocol.js.map +1 -0
  18. package/schemas/org.varlink.certification.varlink.cjs +75 -0
  19. package/schemas/org.varlink.certification.varlink.cjs.map +1 -0
  20. package/schemas/org.varlink.certification.varlink.d.cts +20 -0
  21. package/schemas/org.varlink.certification.varlink.d.ts +20 -0
  22. package/schemas/org.varlink.certification.varlink.js +38 -0
  23. package/schemas/org.varlink.certification.varlink.js.map +1 -0
  24. package/schemas/org.varlink.resolver.varlink.cjs +56 -0
  25. package/schemas/org.varlink.resolver.varlink.cjs.map +1 -0
  26. package/schemas/org.varlink.resolver.varlink.d.cts +19 -0
  27. package/schemas/org.varlink.resolver.varlink.d.ts +19 -0
  28. package/schemas/org.varlink.resolver.varlink.js +24 -0
  29. package/schemas/org.varlink.resolver.varlink.js.map +1 -0
  30. package/schemas/org.varlink.service.varlink.cjs +56 -0
  31. package/schemas/org.varlink.service.varlink.cjs.map +1 -0
  32. package/schemas/org.varlink.service.varlink.d.cts +19 -0
  33. package/schemas/org.varlink.service.varlink.d.ts +19 -0
  34. package/schemas/org.varlink.service.varlink.js +24 -0
  35. package/schemas/org.varlink.service.varlink.js.map +1 -0
  36. package/server/server.cjs +168 -0
  37. package/server/server.cjs.map +1 -0
  38. package/server/server.d.cts +16 -0
  39. package/server/server.d.ts +16 -0
  40. package/server/server.js +130 -0
  41. package/server/server.js.map +1 -0
  42. package/transport/node-socket.cjs +247 -0
  43. package/transport/node-socket.cjs.map +1 -0
  44. package/transport/node-socket.d.cts +35 -0
  45. package/transport/node-socket.d.ts +35 -0
  46. package/{src/transport/node-socket.ts → transport/node-socket.js} +60 -84
  47. package/transport/node-socket.js.map +1 -0
  48. package/transport/transport.cjs +109 -0
  49. package/transport/transport.cjs.map +1 -0
  50. package/transport/transport.d.cts +31 -0
  51. package/transport/transport.d.ts +31 -0
  52. package/transport/transport.js +10 -0
  53. package/transport/transport.js.map +1 -0
  54. package/.github/workflows/bun.yml +0 -18
  55. package/.github/workflows/nodejs.yml +0 -18
  56. package/eslint.config.cjs +0 -17
  57. package/src/client/client.test.ts +0 -138
  58. package/src/client/client.ts +0 -119
  59. package/src/protocol/protocol.ts +0 -124
  60. package/src/schemas/org.varlink.certification.varlink +0 -86
  61. package/src/schemas/org.varlink.certification.varlink.ts +0 -32
  62. package/src/schemas/org.varlink.resolver.varlink +0 -18
  63. package/src/schemas/org.varlink.resolver.varlink.ts +0 -25
  64. package/src/schemas/org.varlink.service.varlink +0 -29
  65. package/src/schemas/org.varlink.service.varlink.ts +0 -25
  66. package/src/server/reference-server.ts +0 -436
  67. package/src/server/run-reference-server.ts +0 -11
  68. package/src/server/server.test.ts +0 -41
  69. package/src/server/server.ts +0 -180
  70. package/src/transport/transport.ts +0 -58
  71. package/tsconfig.json +0 -27
  72. package/tsup.config.ts +0 -19
@@ -1,138 +0,0 @@
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
- });
@@ -1,119 +0,0 @@
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
- }
@@ -1,124 +0,0 @@
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
- }
@@ -1,86 +0,0 @@
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)
@@ -1,32 +0,0 @@
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
- };
@@ -1,18 +0,0 @@
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)
@@ -1,25 +0,0 @@
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
- };
@@ -1,29 +0,0 @@
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)
@@ -1,25 +0,0 @@
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
- };