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.
- package/chunk-QUYAB4HT.js +42 -0
- package/chunk-QUYAB4HT.js.map +1 -0
- package/chunk-XDPGXLHF.js +90 -0
- package/chunk-XDPGXLHF.js.map +1 -0
- package/client/client.cjs +139 -0
- package/client/client.cjs.map +1 -0
- package/client/client.d.cts +16 -0
- package/client/client.d.ts +16 -0
- package/client/client.js +102 -0
- package/client/client.js.map +1 -0
- package/package.json +4 -3
- package/protocol/protocol.cjs +107 -0
- package/protocol/protocol.cjs.map +1 -0
- package/protocol/protocol.d.cts +42 -0
- package/protocol/protocol.d.ts +42 -0
- package/protocol/protocol.js +19 -0
- package/protocol/protocol.js.map +1 -0
- package/schemas/org.varlink.certification.varlink.cjs +75 -0
- package/schemas/org.varlink.certification.varlink.cjs.map +1 -0
- package/schemas/org.varlink.certification.varlink.d.cts +20 -0
- package/schemas/org.varlink.certification.varlink.d.ts +20 -0
- package/schemas/org.varlink.certification.varlink.js +38 -0
- package/schemas/org.varlink.certification.varlink.js.map +1 -0
- package/schemas/org.varlink.resolver.varlink.cjs +56 -0
- package/schemas/org.varlink.resolver.varlink.cjs.map +1 -0
- package/schemas/org.varlink.resolver.varlink.d.cts +19 -0
- package/schemas/org.varlink.resolver.varlink.d.ts +19 -0
- package/schemas/org.varlink.resolver.varlink.js +24 -0
- package/schemas/org.varlink.resolver.varlink.js.map +1 -0
- package/schemas/org.varlink.service.varlink.cjs +56 -0
- package/schemas/org.varlink.service.varlink.cjs.map +1 -0
- package/schemas/org.varlink.service.varlink.d.cts +19 -0
- package/schemas/org.varlink.service.varlink.d.ts +19 -0
- package/schemas/org.varlink.service.varlink.js +24 -0
- package/schemas/org.varlink.service.varlink.js.map +1 -0
- package/server/server.cjs +168 -0
- package/server/server.cjs.map +1 -0
- package/server/server.d.cts +16 -0
- package/server/server.d.ts +16 -0
- package/server/server.js +130 -0
- package/server/server.js.map +1 -0
- package/transport/node-socket.cjs +247 -0
- package/transport/node-socket.cjs.map +1 -0
- package/transport/node-socket.d.cts +35 -0
- package/transport/node-socket.d.ts +35 -0
- package/{src/transport/node-socket.ts → transport/node-socket.js} +60 -84
- package/transport/node-socket.js.map +1 -0
- package/transport/transport.cjs +109 -0
- package/transport/transport.cjs.map +1 -0
- package/transport/transport.d.cts +31 -0
- package/transport/transport.d.ts +31 -0
- package/transport/transport.js +10 -0
- package/transport/transport.js.map +1 -0
- package/.github/workflows/bun.yml +0 -18
- package/.github/workflows/nodejs.yml +0 -18
- package/eslint.config.cjs +0 -17
- package/src/client/client.test.ts +0 -138
- package/src/client/client.ts +0 -119
- package/src/protocol/protocol.ts +0 -124
- package/src/schemas/org.varlink.certification.varlink +0 -86
- package/src/schemas/org.varlink.certification.varlink.ts +0 -32
- package/src/schemas/org.varlink.resolver.varlink +0 -18
- package/src/schemas/org.varlink.resolver.varlink.ts +0 -25
- package/src/schemas/org.varlink.service.varlink +0 -29
- package/src/schemas/org.varlink.service.varlink.ts +0 -25
- package/src/server/reference-server.ts +0 -436
- package/src/server/run-reference-server.ts +0 -11
- package/src/server/server.test.ts +0 -41
- package/src/server/server.ts +0 -180
- package/src/transport/transport.ts +0 -58
- package/tsconfig.json +0 -27
- package/tsup.config.ts +0 -19
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deserializeVarlinkRequest,
|
|
3
|
+
deserializeVarlinkResponse,
|
|
4
|
+
serializeVarlinkRequest,
|
|
5
|
+
serializeVarlinkResponse
|
|
6
|
+
} from "./chunk-XDPGXLHF.js";
|
|
7
|
+
|
|
8
|
+
// src/transport/transport.ts
|
|
9
|
+
var VarlinkClientSideTransportChannel = class {
|
|
10
|
+
constructor(chan) {
|
|
11
|
+
this.chan = chan;
|
|
12
|
+
}
|
|
13
|
+
async send(request) {
|
|
14
|
+
await this.chan.send(serializeVarlinkRequest(request));
|
|
15
|
+
}
|
|
16
|
+
async recv() {
|
|
17
|
+
return deserializeVarlinkResponse(await this.chan.recv());
|
|
18
|
+
}
|
|
19
|
+
async close() {
|
|
20
|
+
await this.chan.close();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var VarlinkServerSideTransportChannel = class {
|
|
24
|
+
constructor(chan) {
|
|
25
|
+
this.chan = chan;
|
|
26
|
+
}
|
|
27
|
+
async send(response) {
|
|
28
|
+
await this.chan.send(serializeVarlinkResponse(response));
|
|
29
|
+
}
|
|
30
|
+
async recv() {
|
|
31
|
+
return deserializeVarlinkRequest(await this.chan.recv());
|
|
32
|
+
}
|
|
33
|
+
async close() {
|
|
34
|
+
await this.chan.close();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
VarlinkClientSideTransportChannel,
|
|
40
|
+
VarlinkServerSideTransportChannel
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=chunk-QUYAB4HT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/transport/transport.ts"],"sourcesContent":["import {\n deserializeVarlinkRequest,\n deserializeVarlinkResponse,\n serializeVarlinkRequest,\n serializeVarlinkResponse,\n type VarlinkRequest,\n type VarlinkResponse,\n} from \"../protocol/protocol\";\n\nexport interface VarlinkTransportChannel {\n send: (request: Uint8Array) => Promise<void>;\n recv: () => Promise<Uint8Array>;\n close: () => Promise<void>;\n}\n\nexport class VarlinkClientSideTransportChannel {\n constructor(private readonly chan: VarlinkTransportChannel) {}\n\n async send(request: VarlinkRequest): Promise<void> {\n await this.chan.send(serializeVarlinkRequest(request));\n }\n\n async recv(): Promise<VarlinkResponse> {\n return deserializeVarlinkResponse(await this.chan.recv());\n }\n\n async close(): Promise<void> {\n await this.chan.close();\n }\n}\n\nexport class VarlinkServerSideTransportChannel {\n constructor(private readonly chan: VarlinkTransportChannel) {}\n\n async send(response: VarlinkResponse): Promise<void> {\n await this.chan.send(serializeVarlinkResponse(response));\n }\n\n async recv(): Promise<VarlinkRequest> {\n return deserializeVarlinkRequest(await this.chan.recv());\n }\n\n async close(): Promise<void> {\n await this.chan.close();\n }\n}\n\nexport interface VarlinkClientSideTransport {\n connect: () => Promise<VarlinkClientSideTransportChannel>;\n}\n\nexport interface VarlinkServerSideTransport {\n onClientConnected: (\n callback: (chan: VarlinkServerSideTransportChannel) => void,\n ) => void;\n start: () => Promise<void>;\n stop: () => Promise<void>;\n}\n"],"mappings":";;;;;;;;AAeO,IAAM,oCAAN,MAAwC;AAAA,EAC7C,YAA6B,MAA+B;AAA/B;AAAA,EAAgC;AAAA,EAE7D,MAAM,KAAK,SAAwC;AACjD,UAAM,KAAK,KAAK,KAAK,wBAAwB,OAAO,CAAC;AAAA,EACvD;AAAA,EAEA,MAAM,OAAiC;AACrC,WAAO,2BAA2B,MAAM,KAAK,KAAK,KAAK,CAAC;AAAA,EAC1D;AAAA,EAEA,MAAM,QAAuB;AAC3B,UAAM,KAAK,KAAK,MAAM;AAAA,EACxB;AACF;AAEO,IAAM,oCAAN,MAAwC;AAAA,EAC7C,YAA6B,MAA+B;AAA/B;AAAA,EAAgC;AAAA,EAE7D,MAAM,KAAK,UAA0C;AACnD,UAAM,KAAK,KAAK,KAAK,yBAAyB,QAAQ,CAAC;AAAA,EACzD;AAAA,EAEA,MAAM,OAAgC;AACpC,WAAO,0BAA0B,MAAM,KAAK,KAAK,KAAK,CAAC;AAAA,EACzD;AAAA,EAEA,MAAM,QAAuB;AAC3B,UAAM,KAAK,KAAK,MAAM;AAAA,EACxB;AACF;","names":[]}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
8
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
9
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
10
|
+
|
|
11
|
+
// src/protocol/protocol.ts
|
|
12
|
+
var VarlinkError = class extends Error {
|
|
13
|
+
constructor(type_, parameters) {
|
|
14
|
+
super(`${type_} ${JSON.stringify(parameters)}`);
|
|
15
|
+
this.type_ = type_;
|
|
16
|
+
this.parameters = parameters;
|
|
17
|
+
this.name = this.constructor.name;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var VarlinkMethod = class {
|
|
21
|
+
constructor(name, inputPhantom, outputPhantom) {
|
|
22
|
+
this.name = name;
|
|
23
|
+
this.inputPhantom = inputPhantom;
|
|
24
|
+
this.outputPhantom = outputPhantom;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var VarlinkDynamicMethod = class extends VarlinkMethod {
|
|
28
|
+
constructor(name) {
|
|
29
|
+
super(name, {}, {});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function serializeVarlinkRequest(request) {
|
|
33
|
+
const partial = { method: request.method };
|
|
34
|
+
if (Object.keys(request.parameters).length > 0) {
|
|
35
|
+
partial.parameters = request.parameters;
|
|
36
|
+
}
|
|
37
|
+
if (request.oneway) {
|
|
38
|
+
partial.oneway = request.oneway;
|
|
39
|
+
}
|
|
40
|
+
if (request.more) {
|
|
41
|
+
partial.more = request.more;
|
|
42
|
+
}
|
|
43
|
+
if (request.upgrade) {
|
|
44
|
+
partial.upgrade = request.upgrade;
|
|
45
|
+
}
|
|
46
|
+
return new TextEncoder().encode(JSON.stringify(partial));
|
|
47
|
+
}
|
|
48
|
+
function deserializeVarlinkRequest(buffer) {
|
|
49
|
+
const partial = JSON.parse(
|
|
50
|
+
new TextDecoder().decode(buffer)
|
|
51
|
+
);
|
|
52
|
+
partial.parameters || (partial.parameters = {});
|
|
53
|
+
partial.oneway || (partial.oneway = false);
|
|
54
|
+
partial.more || (partial.more = false);
|
|
55
|
+
partial.upgrade || (partial.upgrade = false);
|
|
56
|
+
return partial;
|
|
57
|
+
}
|
|
58
|
+
function serializeVarlinkResponse(response) {
|
|
59
|
+
const partial = { parameters: response.parameters };
|
|
60
|
+
if (response.error !== void 0) {
|
|
61
|
+
partial.error = response.error;
|
|
62
|
+
}
|
|
63
|
+
if (response.continues === true) {
|
|
64
|
+
partial.continues = response.continues;
|
|
65
|
+
}
|
|
66
|
+
return new TextEncoder().encode(JSON.stringify(response));
|
|
67
|
+
}
|
|
68
|
+
function deserializeVarlinkResponse(buffer) {
|
|
69
|
+
const partial = JSON.parse(
|
|
70
|
+
new TextDecoder().decode(buffer)
|
|
71
|
+
);
|
|
72
|
+
if (partial.error === void 0) {
|
|
73
|
+
partial.continues || (partial.continues = false);
|
|
74
|
+
}
|
|
75
|
+
return partial;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export {
|
|
79
|
+
__publicField,
|
|
80
|
+
__privateAdd,
|
|
81
|
+
__privateMethod,
|
|
82
|
+
VarlinkError,
|
|
83
|
+
VarlinkMethod,
|
|
84
|
+
VarlinkDynamicMethod,
|
|
85
|
+
serializeVarlinkRequest,
|
|
86
|
+
deserializeVarlinkRequest,
|
|
87
|
+
serializeVarlinkResponse,
|
|
88
|
+
deserializeVarlinkResponse
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=chunk-XDPGXLHF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/protocol/protocol.ts"],"sourcesContent":["export type VarlinkDictionary = Record<string, any>;\n\nexport class VarlinkError extends Error {\n constructor(\n public type_: string,\n public parameters: VarlinkDictionary,\n ) {\n super(`${type_} ${JSON.stringify(parameters)}`);\n this.name = this.constructor.name;\n }\n}\n\nexport class VarlinkMethod<\n Name extends string,\n Input extends VarlinkDictionary,\n Output extends VarlinkDictionary,\n> {\n constructor(\n public name: Name,\n private readonly inputPhantom: Input,\n private readonly outputPhantom: Output,\n ) {}\n}\n\nexport type VarlinkMethodGetName<M> =\n M extends VarlinkMethod<infer Name, any, any> ? Name : never;\nexport type VarlinkMethodGetInput<M> =\n M extends VarlinkMethod<any, infer Input, any> ? Input : never;\nexport type VarlinkMethodGetOutput<M> =\n M extends VarlinkMethod<any, any, infer Output> ? Output : never;\n\nexport class VarlinkDynamicMethod extends VarlinkMethod<\n string,\n VarlinkDictionary,\n VarlinkDictionary\n> {\n constructor(name: string) {\n super(name, {}, {});\n }\n}\n\nexport interface VarlinkRequest {\n method: string;\n parameters: VarlinkDictionary;\n oneway: boolean;\n more: boolean;\n upgrade: boolean;\n}\n\nexport function serializeVarlinkRequest(request: VarlinkRequest): Uint8Array {\n const partial: Partial<VarlinkRequest> = { method: request.method };\n\n if (Object.keys(request.parameters).length > 0) {\n partial.parameters = request.parameters;\n }\n\n if (request.oneway) {\n partial.oneway = request.oneway;\n }\n\n if (request.more) {\n partial.more = request.more;\n }\n\n if (request.upgrade) {\n partial.upgrade = request.upgrade;\n }\n\n return new TextEncoder().encode(JSON.stringify(partial));\n}\n\nexport function deserializeVarlinkRequest(buffer: Uint8Array): VarlinkRequest {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkRequest;\n partial.parameters ||= {};\n partial.oneway ||= false;\n partial.more ||= false;\n partial.upgrade ||= false;\n return partial;\n}\n\nexport interface VarlinkSuccessResponse {\n error: undefined;\n parameters: VarlinkDictionary;\n continues: boolean;\n}\n\nexport interface VarlinkErrorResponse {\n error: string;\n parameters: VarlinkDictionary;\n continues: undefined;\n}\n\nexport type VarlinkResponse = VarlinkSuccessResponse | VarlinkErrorResponse;\n\nexport function serializeVarlinkResponse(\n response: VarlinkResponse,\n): Uint8Array {\n const partial: Partial<VarlinkResponse> = { parameters: response.parameters };\n\n if (response.error !== undefined) {\n partial.error = response.error;\n }\n\n if (response.continues === true) {\n partial.continues = response.continues;\n }\n\n return new TextEncoder().encode(JSON.stringify(response));\n}\n\nexport function deserializeVarlinkResponse(\n buffer: Uint8Array,\n): VarlinkResponse {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkResponse;\n if (partial.error === undefined) {\n partial.continues ||= false;\n }\n\n return partial;\n}\n"],"mappings":";;;;;;;;;;;AAEO,IAAM,eAAN,cAA2B,MAAM;AAAA,EACtC,YACS,OACA,YACP;AACA,UAAM,GAAG,KAAK,IAAI,KAAK,UAAU,UAAU,CAAC,EAAE;AAHvC;AACA;AAGP,SAAK,OAAO,KAAK,YAAY;AAAA,EAC/B;AACF;AAEO,IAAM,gBAAN,MAIL;AAAA,EACA,YACS,MACU,cACA,eACjB;AAHO;AACU;AACA;AAAA,EAChB;AACL;AASO,IAAM,uBAAN,cAAmC,cAIxC;AAAA,EACA,YAAY,MAAc;AACxB,UAAM,MAAM,CAAC,GAAG,CAAC,CAAC;AAAA,EACpB;AACF;AAUO,SAAS,wBAAwB,SAAqC;AAC3E,QAAM,UAAmC,EAAE,QAAQ,QAAQ,OAAO;AAElE,MAAI,OAAO,KAAK,QAAQ,UAAU,EAAE,SAAS,GAAG;AAC9C,YAAQ,aAAa,QAAQ;AAAA,EAC/B;AAEA,MAAI,QAAQ,QAAQ;AAClB,YAAQ,SAAS,QAAQ;AAAA,EAC3B;AAEA,MAAI,QAAQ,MAAM;AAChB,YAAQ,OAAO,QAAQ;AAAA,EACzB;AAEA,MAAI,QAAQ,SAAS;AACnB,YAAQ,UAAU,QAAQ;AAAA,EAC5B;AAEA,SAAO,IAAI,YAAY,EAAE,OAAO,KAAK,UAAU,OAAO,CAAC;AACzD;AAEO,SAAS,0BAA0B,QAAoC;AAC5E,QAAM,UAAU,KAAK;AAAA,IACnB,IAAI,YAAY,EAAE,OAAO,MAAM;AAAA,EACjC;AACA,UAAQ,eAAR,QAAQ,aAAe,CAAC;AACxB,UAAQ,WAAR,QAAQ,SAAW;AACnB,UAAQ,SAAR,QAAQ,OAAS;AACjB,UAAQ,YAAR,QAAQ,UAAY;AACpB,SAAO;AACT;AAgBO,SAAS,yBACd,UACY;AACZ,QAAM,UAAoC,EAAE,YAAY,SAAS,WAAW;AAE5E,MAAI,SAAS,UAAU,QAAW;AAChC,YAAQ,QAAQ,SAAS;AAAA,EAC3B;AAEA,MAAI,SAAS,cAAc,MAAM;AAC/B,YAAQ,YAAY,SAAS;AAAA,EAC/B;AAEA,SAAO,IAAI,YAAY,EAAE,OAAO,KAAK,UAAU,QAAQ,CAAC;AAC1D;AAEO,SAAS,2BACd,QACiB;AACjB,QAAM,UAAU,KAAK;AAAA,IACnB,IAAI,YAAY,EAAE,OAAO,MAAM;AAAA,EACjC;AACA,MAAI,QAAQ,UAAU,QAAW;AAC/B,YAAQ,cAAR,QAAQ,YAAc;AAAA,EACxB;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __typeError = (msg) => {
|
|
7
|
+
throw TypeError(msg);
|
|
8
|
+
};
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
24
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
25
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
26
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
27
|
+
|
|
28
|
+
// src/client/client.ts
|
|
29
|
+
var client_exports = {};
|
|
30
|
+
__export(client_exports, {
|
|
31
|
+
VarlinkClient: () => VarlinkClient
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(client_exports);
|
|
34
|
+
|
|
35
|
+
// src/protocol/protocol.ts
|
|
36
|
+
var VarlinkError = class extends Error {
|
|
37
|
+
constructor(type_, parameters) {
|
|
38
|
+
super(`${type_} ${JSON.stringify(parameters)}`);
|
|
39
|
+
this.type_ = type_;
|
|
40
|
+
this.parameters = parameters;
|
|
41
|
+
this.name = this.constructor.name;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/client/client.ts
|
|
46
|
+
var _VarlinkClient_instances, takeFromPool_fn, backToPool_fn, drainPool_fn;
|
|
47
|
+
var VarlinkClient = class {
|
|
48
|
+
constructor(transport) {
|
|
49
|
+
this.transport = transport;
|
|
50
|
+
__privateAdd(this, _VarlinkClient_instances);
|
|
51
|
+
__publicField(this, "pool", []);
|
|
52
|
+
}
|
|
53
|
+
async connect() {
|
|
54
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
55
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
56
|
+
}
|
|
57
|
+
async disconnect() {
|
|
58
|
+
await __privateMethod(this, _VarlinkClient_instances, drainPool_fn).call(this);
|
|
59
|
+
}
|
|
60
|
+
async call(method, input) {
|
|
61
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
62
|
+
await chan.send({
|
|
63
|
+
method: method.name,
|
|
64
|
+
parameters: input,
|
|
65
|
+
oneway: false,
|
|
66
|
+
more: false,
|
|
67
|
+
upgrade: false
|
|
68
|
+
});
|
|
69
|
+
const resp = await chan.recv();
|
|
70
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
71
|
+
if (resp.error !== void 0) {
|
|
72
|
+
throw new VarlinkError(resp.error, resp.parameters);
|
|
73
|
+
}
|
|
74
|
+
return resp.parameters;
|
|
75
|
+
}
|
|
76
|
+
async callOneshot(method, input) {
|
|
77
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
78
|
+
await chan.send({
|
|
79
|
+
method: method.name,
|
|
80
|
+
parameters: input,
|
|
81
|
+
oneway: true,
|
|
82
|
+
more: false,
|
|
83
|
+
upgrade: false
|
|
84
|
+
});
|
|
85
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
86
|
+
}
|
|
87
|
+
async callStream(method, input, callbackFunction) {
|
|
88
|
+
var _a;
|
|
89
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
90
|
+
await chan.send({
|
|
91
|
+
method: method.name,
|
|
92
|
+
parameters: input,
|
|
93
|
+
oneway: false,
|
|
94
|
+
more: true,
|
|
95
|
+
upgrade: false
|
|
96
|
+
});
|
|
97
|
+
let continues = true;
|
|
98
|
+
while (continues) {
|
|
99
|
+
const resp = await chan.recv();
|
|
100
|
+
if (resp.error !== void 0) {
|
|
101
|
+
await callbackFunction(
|
|
102
|
+
new VarlinkError(resp.error, resp.parameters),
|
|
103
|
+
{}
|
|
104
|
+
);
|
|
105
|
+
} else {
|
|
106
|
+
await callbackFunction(
|
|
107
|
+
void 0,
|
|
108
|
+
resp.parameters
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
continues = (_a = resp.continues) != null ? _a : false;
|
|
112
|
+
}
|
|
113
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
_VarlinkClient_instances = new WeakSet();
|
|
117
|
+
takeFromPool_fn = async function() {
|
|
118
|
+
if (this.pool.length === 0) {
|
|
119
|
+
return await this.transport.connect();
|
|
120
|
+
}
|
|
121
|
+
return this.pool.shift();
|
|
122
|
+
};
|
|
123
|
+
backToPool_fn = async function(chan) {
|
|
124
|
+
this.pool.push(chan);
|
|
125
|
+
};
|
|
126
|
+
drainPool_fn = async function() {
|
|
127
|
+
const pool = this.pool;
|
|
128
|
+
this.pool = [];
|
|
129
|
+
await Promise.all(
|
|
130
|
+
pool.map(async (chan) => {
|
|
131
|
+
await chan.close();
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
136
|
+
0 && (module.exports = {
|
|
137
|
+
VarlinkClient
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/client.ts","../../src/protocol/protocol.ts"],"sourcesContent":["import {\n type VarlinkClientSideTransport,\n type VarlinkClientSideTransportChannel,\n} from \"../transport/transport\";\nimport {\n VarlinkError,\n type VarlinkMethod,\n type VarlinkMethodGetInput,\n type VarlinkMethodGetOutput,\n} from \"../protocol/protocol\";\n\nexport class VarlinkClient {\n private pool: VarlinkClientSideTransportChannel[] = [];\n constructor(private readonly transport: VarlinkClientSideTransport) {}\n\n async connect(): Promise<void> {\n const chan = await this.#takeFromPool();\n await this.#backToPool(chan);\n }\n\n async disconnect(): Promise<void> {\n await this.#drainPool();\n }\n\n async call<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n ): Promise<VarlinkMethodGetOutput<M>> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: false,\n more: false,\n upgrade: false,\n });\n const resp = await chan.recv();\n await this.#backToPool(chan);\n\n if (resp.error !== undefined) {\n throw new VarlinkError(resp.error, resp.parameters);\n }\n\n return resp.parameters as VarlinkMethodGetOutput<M>;\n }\n\n async callOneshot<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n ): Promise<void> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: true,\n more: false,\n upgrade: false,\n });\n await this.#backToPool(chan);\n }\n\n async callStream<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n callbackFunction: (\n error: VarlinkError | undefined,\n data: VarlinkMethodGetOutput<M>,\n ) => Promise<void>,\n ): Promise<void> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: false,\n more: true,\n upgrade: false,\n });\n let continues = true;\n while (continues) {\n const resp = await chan.recv();\n if (resp.error !== undefined) {\n await callbackFunction(\n new VarlinkError(resp.error, resp.parameters),\n {} as VarlinkMethodGetOutput<M>,\n );\n } else {\n await callbackFunction(\n undefined,\n resp.parameters as VarlinkMethodGetOutput<M>,\n );\n }\n continues = resp.continues ?? false;\n }\n\n await this.#backToPool(chan);\n }\n\n async #takeFromPool(): Promise<VarlinkClientSideTransportChannel> {\n if (this.pool.length === 0) {\n return await this.transport.connect();\n }\n\n return this.pool.shift()!;\n }\n\n async #backToPool(chan: VarlinkClientSideTransportChannel): Promise<void> {\n this.pool.push(chan);\n }\n\n async #drainPool(): Promise<void> {\n const pool = this.pool;\n this.pool = [];\n await Promise.all(\n pool.map(async (chan) => {\n await chan.close();\n }),\n );\n }\n}\n","export type VarlinkDictionary = Record<string, any>;\n\nexport class VarlinkError extends Error {\n constructor(\n public type_: string,\n public parameters: VarlinkDictionary,\n ) {\n super(`${type_} ${JSON.stringify(parameters)}`);\n this.name = this.constructor.name;\n }\n}\n\nexport class VarlinkMethod<\n Name extends string,\n Input extends VarlinkDictionary,\n Output extends VarlinkDictionary,\n> {\n constructor(\n public name: Name,\n private readonly inputPhantom: Input,\n private readonly outputPhantom: Output,\n ) {}\n}\n\nexport type VarlinkMethodGetName<M> =\n M extends VarlinkMethod<infer Name, any, any> ? Name : never;\nexport type VarlinkMethodGetInput<M> =\n M extends VarlinkMethod<any, infer Input, any> ? Input : never;\nexport type VarlinkMethodGetOutput<M> =\n M extends VarlinkMethod<any, any, infer Output> ? Output : never;\n\nexport class VarlinkDynamicMethod extends VarlinkMethod<\n string,\n VarlinkDictionary,\n VarlinkDictionary\n> {\n constructor(name: string) {\n super(name, {}, {});\n }\n}\n\nexport interface VarlinkRequest {\n method: string;\n parameters: VarlinkDictionary;\n oneway: boolean;\n more: boolean;\n upgrade: boolean;\n}\n\nexport function serializeVarlinkRequest(request: VarlinkRequest): Uint8Array {\n const partial: Partial<VarlinkRequest> = { method: request.method };\n\n if (Object.keys(request.parameters).length > 0) {\n partial.parameters = request.parameters;\n }\n\n if (request.oneway) {\n partial.oneway = request.oneway;\n }\n\n if (request.more) {\n partial.more = request.more;\n }\n\n if (request.upgrade) {\n partial.upgrade = request.upgrade;\n }\n\n return new TextEncoder().encode(JSON.stringify(partial));\n}\n\nexport function deserializeVarlinkRequest(buffer: Uint8Array): VarlinkRequest {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkRequest;\n partial.parameters ||= {};\n partial.oneway ||= false;\n partial.more ||= false;\n partial.upgrade ||= false;\n return partial;\n}\n\nexport interface VarlinkSuccessResponse {\n error: undefined;\n parameters: VarlinkDictionary;\n continues: boolean;\n}\n\nexport interface VarlinkErrorResponse {\n error: string;\n parameters: VarlinkDictionary;\n continues: undefined;\n}\n\nexport type VarlinkResponse = VarlinkSuccessResponse | VarlinkErrorResponse;\n\nexport function serializeVarlinkResponse(\n response: VarlinkResponse,\n): Uint8Array {\n const partial: Partial<VarlinkResponse> = { parameters: response.parameters };\n\n if (response.error !== undefined) {\n partial.error = response.error;\n }\n\n if (response.continues === true) {\n partial.continues = response.continues;\n }\n\n return new TextEncoder().encode(JSON.stringify(response));\n}\n\nexport function deserializeVarlinkResponse(\n buffer: Uint8Array,\n): VarlinkResponse {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkResponse;\n if (partial.error === undefined) {\n partial.continues ||= false;\n }\n\n return partial;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,eAAN,cAA2B,MAAM;AAAA,EACtC,YACS,OACA,YACP;AACA,UAAM,GAAG,KAAK,IAAI,KAAK,UAAU,UAAU,CAAC,EAAE;AAHvC;AACA;AAGP,SAAK,OAAO,KAAK,YAAY;AAAA,EAC/B;AACF;;;ADVA;AAWO,IAAM,gBAAN,MAAoB;AAAA,EAEzB,YAA6B,WAAuC;AAAvC;AAFxB;AACL,wBAAQ,QAA4C,CAAC;AAAA,EACgB;AAAA,EAErE,MAAM,UAAyB;AAC7B,UAAM,OAAO,MAAM,sBAAK,2CAAL;AACnB,UAAM,sBAAK,yCAAL,WAAiB;AAAA,EACzB;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,sBAAK,wCAAL;AAAA,EACR;AAAA,EAEA,MAAM,KACJ,QACA,OACoC;AACpC,UAAM,OAAO,MAAM,sBAAK,2CAAL;AACnB,UAAM,KAAK,KAAK;AAAA,MACd,QAAQ,OAAO;AAAA,MACf,YAAY;AAAA,MACZ,QAAQ;AAAA,MACR,MAAM;AAAA,MACN,SAAS;AAAA,I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,KAAK,KAAK,IAAI;AACrB;AAEM,eAAU,iBAAkB;AAChC,QAAM,OAAO,KAAK;AAClB,OAAK,OAAO,CAAC;AACb,QAAM,QAAQ;AAAA,IACZ,KAAK,IAAI,OAAO,SAAS;AACvB,YAAM,KAAK,MAAM;AAAA,IACnB,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { VarlinkClientSideTransport } from '../transport/transport.cjs';
|
|
2
|
+
import { VarlinkMethod, VarlinkMethodGetInput, VarlinkMethodGetOutput, VarlinkError } from '../protocol/protocol.cjs';
|
|
3
|
+
|
|
4
|
+
declare class VarlinkClient {
|
|
5
|
+
#private;
|
|
6
|
+
private readonly transport;
|
|
7
|
+
private pool;
|
|
8
|
+
constructor(transport: VarlinkClientSideTransport);
|
|
9
|
+
connect(): Promise<void>;
|
|
10
|
+
disconnect(): Promise<void>;
|
|
11
|
+
call<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>): Promise<VarlinkMethodGetOutput<M>>;
|
|
12
|
+
callOneshot<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>): Promise<void>;
|
|
13
|
+
callStream<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>, callbackFunction: (error: VarlinkError | undefined, data: VarlinkMethodGetOutput<M>) => Promise<void>): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { VarlinkClient };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { VarlinkClientSideTransport } from '../transport/transport.js';
|
|
2
|
+
import { VarlinkMethod, VarlinkMethodGetInput, VarlinkMethodGetOutput, VarlinkError } from '../protocol/protocol.js';
|
|
3
|
+
|
|
4
|
+
declare class VarlinkClient {
|
|
5
|
+
#private;
|
|
6
|
+
private readonly transport;
|
|
7
|
+
private pool;
|
|
8
|
+
constructor(transport: VarlinkClientSideTransport);
|
|
9
|
+
connect(): Promise<void>;
|
|
10
|
+
disconnect(): Promise<void>;
|
|
11
|
+
call<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>): Promise<VarlinkMethodGetOutput<M>>;
|
|
12
|
+
callOneshot<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>): Promise<void>;
|
|
13
|
+
callStream<M extends VarlinkMethod<string, any, any>>(method: M, input: VarlinkMethodGetInput<M>, callbackFunction: (error: VarlinkError | undefined, data: VarlinkMethodGetOutput<M>) => Promise<void>): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { VarlinkClient };
|
package/client/client.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import "../chunk-QUYAB4HT.js";
|
|
2
|
+
import {
|
|
3
|
+
VarlinkError,
|
|
4
|
+
__privateAdd,
|
|
5
|
+
__privateMethod,
|
|
6
|
+
__publicField
|
|
7
|
+
} from "../chunk-XDPGXLHF.js";
|
|
8
|
+
|
|
9
|
+
// src/client/client.ts
|
|
10
|
+
var _VarlinkClient_instances, takeFromPool_fn, backToPool_fn, drainPool_fn;
|
|
11
|
+
var VarlinkClient = class {
|
|
12
|
+
constructor(transport) {
|
|
13
|
+
this.transport = transport;
|
|
14
|
+
__privateAdd(this, _VarlinkClient_instances);
|
|
15
|
+
__publicField(this, "pool", []);
|
|
16
|
+
}
|
|
17
|
+
async connect() {
|
|
18
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
19
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
20
|
+
}
|
|
21
|
+
async disconnect() {
|
|
22
|
+
await __privateMethod(this, _VarlinkClient_instances, drainPool_fn).call(this);
|
|
23
|
+
}
|
|
24
|
+
async call(method, input) {
|
|
25
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
26
|
+
await chan.send({
|
|
27
|
+
method: method.name,
|
|
28
|
+
parameters: input,
|
|
29
|
+
oneway: false,
|
|
30
|
+
more: false,
|
|
31
|
+
upgrade: false
|
|
32
|
+
});
|
|
33
|
+
const resp = await chan.recv();
|
|
34
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
35
|
+
if (resp.error !== void 0) {
|
|
36
|
+
throw new VarlinkError(resp.error, resp.parameters);
|
|
37
|
+
}
|
|
38
|
+
return resp.parameters;
|
|
39
|
+
}
|
|
40
|
+
async callOneshot(method, input) {
|
|
41
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
42
|
+
await chan.send({
|
|
43
|
+
method: method.name,
|
|
44
|
+
parameters: input,
|
|
45
|
+
oneway: true,
|
|
46
|
+
more: false,
|
|
47
|
+
upgrade: false
|
|
48
|
+
});
|
|
49
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
50
|
+
}
|
|
51
|
+
async callStream(method, input, callbackFunction) {
|
|
52
|
+
var _a;
|
|
53
|
+
const chan = await __privateMethod(this, _VarlinkClient_instances, takeFromPool_fn).call(this);
|
|
54
|
+
await chan.send({
|
|
55
|
+
method: method.name,
|
|
56
|
+
parameters: input,
|
|
57
|
+
oneway: false,
|
|
58
|
+
more: true,
|
|
59
|
+
upgrade: false
|
|
60
|
+
});
|
|
61
|
+
let continues = true;
|
|
62
|
+
while (continues) {
|
|
63
|
+
const resp = await chan.recv();
|
|
64
|
+
if (resp.error !== void 0) {
|
|
65
|
+
await callbackFunction(
|
|
66
|
+
new VarlinkError(resp.error, resp.parameters),
|
|
67
|
+
{}
|
|
68
|
+
);
|
|
69
|
+
} else {
|
|
70
|
+
await callbackFunction(
|
|
71
|
+
void 0,
|
|
72
|
+
resp.parameters
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
continues = (_a = resp.continues) != null ? _a : false;
|
|
76
|
+
}
|
|
77
|
+
await __privateMethod(this, _VarlinkClient_instances, backToPool_fn).call(this, chan);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
_VarlinkClient_instances = new WeakSet();
|
|
81
|
+
takeFromPool_fn = async function() {
|
|
82
|
+
if (this.pool.length === 0) {
|
|
83
|
+
return await this.transport.connect();
|
|
84
|
+
}
|
|
85
|
+
return this.pool.shift();
|
|
86
|
+
};
|
|
87
|
+
backToPool_fn = async function(chan) {
|
|
88
|
+
this.pool.push(chan);
|
|
89
|
+
};
|
|
90
|
+
drainPool_fn = async function() {
|
|
91
|
+
const pool = this.pool;
|
|
92
|
+
this.pool = [];
|
|
93
|
+
await Promise.all(
|
|
94
|
+
pool.map(async (chan) => {
|
|
95
|
+
await chan.close();
|
|
96
|
+
})
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
export {
|
|
100
|
+
VarlinkClient
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/client.ts"],"sourcesContent":["import {\n type VarlinkClientSideTransport,\n type VarlinkClientSideTransportChannel,\n} from \"../transport/transport\";\nimport {\n VarlinkError,\n type VarlinkMethod,\n type VarlinkMethodGetInput,\n type VarlinkMethodGetOutput,\n} from \"../protocol/protocol\";\n\nexport class VarlinkClient {\n private pool: VarlinkClientSideTransportChannel[] = [];\n constructor(private readonly transport: VarlinkClientSideTransport) {}\n\n async connect(): Promise<void> {\n const chan = await this.#takeFromPool();\n await this.#backToPool(chan);\n }\n\n async disconnect(): Promise<void> {\n await this.#drainPool();\n }\n\n async call<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n ): Promise<VarlinkMethodGetOutput<M>> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: false,\n more: false,\n upgrade: false,\n });\n const resp = await chan.recv();\n await this.#backToPool(chan);\n\n if (resp.error !== undefined) {\n throw new VarlinkError(resp.error, resp.parameters);\n }\n\n return resp.parameters as VarlinkMethodGetOutput<M>;\n }\n\n async callOneshot<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n ): Promise<void> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: true,\n more: false,\n upgrade: false,\n });\n await this.#backToPool(chan);\n }\n\n async callStream<M extends VarlinkMethod<string, any, any>>(\n method: M,\n input: VarlinkMethodGetInput<M>,\n callbackFunction: (\n error: VarlinkError | undefined,\n data: VarlinkMethodGetOutput<M>,\n ) => Promise<void>,\n ): Promise<void> {\n const chan = await this.#takeFromPool();\n await chan.send({\n method: method.name,\n parameters: input,\n oneway: false,\n more: true,\n upgrade: false,\n });\n let continues = true;\n while (continues) {\n const resp = await chan.recv();\n if (resp.error !== undefined) {\n await callbackFunction(\n new VarlinkError(resp.error, resp.parameters),\n {} as VarlinkMethodGetOutput<M>,\n );\n } else {\n await callbackFunction(\n undefined,\n resp.parameters as VarlinkMethodGetOutput<M>,\n );\n }\n continues = resp.continues ?? false;\n }\n\n await this.#backToPool(chan);\n }\n\n async #takeFromPool(): Promise<VarlinkClientSideTransportChannel> {\n if (this.pool.length === 0) {\n return await this.transport.connect();\n }\n\n return this.pool.shift()!;\n }\n\n async #backToPool(chan: VarlinkClientSideTransportChannel): Promise<void> {\n this.pool.push(chan);\n }\n\n async #drainPool(): Promise<void> {\n const pool = this.pool;\n this.pool = [];\n await Promise.all(\n pool.map(async (chan) => {\n await chan.close();\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;;AAAA;AAWO,IAAM,gBAAN,MAAoB;AAAA,EAEzB,YAA6B,WAAuC;AAAvC;AAFxB;AACL,wBAAQ,QAA4C,CAAC;AAAA,EACgB;AAAA,EAErE,MAAM,UAAyB;AAC7B,UAAM,OAAO,MAAM,sBAAK,2CAAL;AACnB,UAAM,sBAAK,yCAAL,WAAiB;AAAA,EACzB;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,sBAAK,wCAAL;AAAA,EACR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B;AAuBF;AA3GO;AAsFC,kBAAa,iBAA+C;AAChE,MAAI,KAAK,KAAK,WAAW,GAAG;AAC1B,WAAO,MAAM,KAAK,UAAU,QAAQ;AAAA,EACtC;AAEA,SAAO,KAAK,KAAK,MAAM;AACzB;AAEM,gBAAW,eAAC,MAAwD;AACxE,OAAK,KAAK,KAAK,IAAI;AACrB;AAEM,eAAU,iBAAkB;AAChC,QAAM,OAAO,KAAK;AAClB,OAAK,OAAO,CAAC;AACb,QAAM,QAAQ;AAAA,IACZ,KAAK,IAAI,OAAO,SAAS;AACvB,YAAM,KAAK,MAAM;AAAA,IACnB,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "https://github.com/pzmarzly/varlink-js"
|
|
11
11
|
},
|
|
12
12
|
"license": "MIT",
|
|
13
|
-
"version": "0.1.
|
|
13
|
+
"version": "0.1.2",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/bun": "^1.1.6",
|
|
@@ -23,8 +23,9 @@
|
|
|
23
23
|
"typescript": "^5.5.3"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
|
-
"build": "tsc && eslint . && tsup
|
|
26
|
+
"build": "tsc && eslint . && tsup",
|
|
27
27
|
"test": "bun test",
|
|
28
|
-
"fmt": "eslint --fix ."
|
|
28
|
+
"fmt": "eslint --fix .",
|
|
29
|
+
"publish": "cp README.md package.json LICENSE dist && npm publish ./dist"
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/protocol/protocol.ts
|
|
21
|
+
var protocol_exports = {};
|
|
22
|
+
__export(protocol_exports, {
|
|
23
|
+
VarlinkDynamicMethod: () => VarlinkDynamicMethod,
|
|
24
|
+
VarlinkError: () => VarlinkError,
|
|
25
|
+
VarlinkMethod: () => VarlinkMethod,
|
|
26
|
+
deserializeVarlinkRequest: () => deserializeVarlinkRequest,
|
|
27
|
+
deserializeVarlinkResponse: () => deserializeVarlinkResponse,
|
|
28
|
+
serializeVarlinkRequest: () => serializeVarlinkRequest,
|
|
29
|
+
serializeVarlinkResponse: () => serializeVarlinkResponse
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(protocol_exports);
|
|
32
|
+
var VarlinkError = class extends Error {
|
|
33
|
+
constructor(type_, parameters) {
|
|
34
|
+
super(`${type_} ${JSON.stringify(parameters)}`);
|
|
35
|
+
this.type_ = type_;
|
|
36
|
+
this.parameters = parameters;
|
|
37
|
+
this.name = this.constructor.name;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var VarlinkMethod = class {
|
|
41
|
+
constructor(name, inputPhantom, outputPhantom) {
|
|
42
|
+
this.name = name;
|
|
43
|
+
this.inputPhantom = inputPhantom;
|
|
44
|
+
this.outputPhantom = outputPhantom;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var VarlinkDynamicMethod = class extends VarlinkMethod {
|
|
48
|
+
constructor(name) {
|
|
49
|
+
super(name, {}, {});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
function serializeVarlinkRequest(request) {
|
|
53
|
+
const partial = { method: request.method };
|
|
54
|
+
if (Object.keys(request.parameters).length > 0) {
|
|
55
|
+
partial.parameters = request.parameters;
|
|
56
|
+
}
|
|
57
|
+
if (request.oneway) {
|
|
58
|
+
partial.oneway = request.oneway;
|
|
59
|
+
}
|
|
60
|
+
if (request.more) {
|
|
61
|
+
partial.more = request.more;
|
|
62
|
+
}
|
|
63
|
+
if (request.upgrade) {
|
|
64
|
+
partial.upgrade = request.upgrade;
|
|
65
|
+
}
|
|
66
|
+
return new TextEncoder().encode(JSON.stringify(partial));
|
|
67
|
+
}
|
|
68
|
+
function deserializeVarlinkRequest(buffer) {
|
|
69
|
+
const partial = JSON.parse(
|
|
70
|
+
new TextDecoder().decode(buffer)
|
|
71
|
+
);
|
|
72
|
+
partial.parameters || (partial.parameters = {});
|
|
73
|
+
partial.oneway || (partial.oneway = false);
|
|
74
|
+
partial.more || (partial.more = false);
|
|
75
|
+
partial.upgrade || (partial.upgrade = false);
|
|
76
|
+
return partial;
|
|
77
|
+
}
|
|
78
|
+
function serializeVarlinkResponse(response) {
|
|
79
|
+
const partial = { parameters: response.parameters };
|
|
80
|
+
if (response.error !== void 0) {
|
|
81
|
+
partial.error = response.error;
|
|
82
|
+
}
|
|
83
|
+
if (response.continues === true) {
|
|
84
|
+
partial.continues = response.continues;
|
|
85
|
+
}
|
|
86
|
+
return new TextEncoder().encode(JSON.stringify(response));
|
|
87
|
+
}
|
|
88
|
+
function deserializeVarlinkResponse(buffer) {
|
|
89
|
+
const partial = JSON.parse(
|
|
90
|
+
new TextDecoder().decode(buffer)
|
|
91
|
+
);
|
|
92
|
+
if (partial.error === void 0) {
|
|
93
|
+
partial.continues || (partial.continues = false);
|
|
94
|
+
}
|
|
95
|
+
return partial;
|
|
96
|
+
}
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {
|
|
99
|
+
VarlinkDynamicMethod,
|
|
100
|
+
VarlinkError,
|
|
101
|
+
VarlinkMethod,
|
|
102
|
+
deserializeVarlinkRequest,
|
|
103
|
+
deserializeVarlinkResponse,
|
|
104
|
+
serializeVarlinkRequest,
|
|
105
|
+
serializeVarlinkResponse
|
|
106
|
+
});
|
|
107
|
+
//# sourceMappingURL=protocol.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/protocol/protocol.ts"],"sourcesContent":["export type VarlinkDictionary = Record<string, any>;\n\nexport class VarlinkError extends Error {\n constructor(\n public type_: string,\n public parameters: VarlinkDictionary,\n ) {\n super(`${type_} ${JSON.stringify(parameters)}`);\n this.name = this.constructor.name;\n }\n}\n\nexport class VarlinkMethod<\n Name extends string,\n Input extends VarlinkDictionary,\n Output extends VarlinkDictionary,\n> {\n constructor(\n public name: Name,\n private readonly inputPhantom: Input,\n private readonly outputPhantom: Output,\n ) {}\n}\n\nexport type VarlinkMethodGetName<M> =\n M extends VarlinkMethod<infer Name, any, any> ? Name : never;\nexport type VarlinkMethodGetInput<M> =\n M extends VarlinkMethod<any, infer Input, any> ? Input : never;\nexport type VarlinkMethodGetOutput<M> =\n M extends VarlinkMethod<any, any, infer Output> ? Output : never;\n\nexport class VarlinkDynamicMethod extends VarlinkMethod<\n string,\n VarlinkDictionary,\n VarlinkDictionary\n> {\n constructor(name: string) {\n super(name, {}, {});\n }\n}\n\nexport interface VarlinkRequest {\n method: string;\n parameters: VarlinkDictionary;\n oneway: boolean;\n more: boolean;\n upgrade: boolean;\n}\n\nexport function serializeVarlinkRequest(request: VarlinkRequest): Uint8Array {\n const partial: Partial<VarlinkRequest> = { method: request.method };\n\n if (Object.keys(request.parameters).length > 0) {\n partial.parameters = request.parameters;\n }\n\n if (request.oneway) {\n partial.oneway = request.oneway;\n }\n\n if (request.more) {\n partial.more = request.more;\n }\n\n if (request.upgrade) {\n partial.upgrade = request.upgrade;\n }\n\n return new TextEncoder().encode(JSON.stringify(partial));\n}\n\nexport function deserializeVarlinkRequest(buffer: Uint8Array): VarlinkRequest {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkRequest;\n partial.parameters ||= {};\n partial.oneway ||= false;\n partial.more ||= false;\n partial.upgrade ||= false;\n return partial;\n}\n\nexport interface VarlinkSuccessResponse {\n error: undefined;\n parameters: VarlinkDictionary;\n continues: boolean;\n}\n\nexport interface VarlinkErrorResponse {\n error: string;\n parameters: VarlinkDictionary;\n continues: undefined;\n}\n\nexport type VarlinkResponse = VarlinkSuccessResponse | VarlinkErrorResponse;\n\nexport function serializeVarlinkResponse(\n response: VarlinkResponse,\n): Uint8Array {\n const partial: Partial<VarlinkResponse> = { parameters: response.parameters };\n\n if (response.error !== undefined) {\n partial.error = response.error;\n }\n\n if (response.continues === true) {\n partial.continues = response.continues;\n }\n\n return new TextEncoder().encode(JSON.stringify(response));\n}\n\nexport function deserializeVarlinkResponse(\n buffer: Uint8Array,\n): VarlinkResponse {\n const partial = JSON.parse(\n new TextDecoder().decode(buffer),\n ) as VarlinkResponse;\n if (partial.error === undefined) {\n partial.continues ||= false;\n }\n\n return partial;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,IAAM,eAAN,cAA2B,MAAM;AAAA,EACtC,YACS,OACA,YACP;AACA,UAAM,GAAG,KAAK,IAAI,KAAK,UAAU,UAAU,CAAC,EAAE;AAHvC;AACA;AAGP,SAAK,OAAO,KAAK,YAAY;AAAA,EAC/B;AACF;AAEO,IAAM,gBAAN,MAIL;AAAA,EACA,YACS,MACU,cACA,eACjB;AAHO;AACU;AACA;AAAA,EAChB;AACL;AASO,IAAM,uBAAN,cAAmC,cAIxC;AAAA,EACA,YAAY,MAAc;AACxB,UAA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0 +1,42 @@
|
|
|
1
|
+
type VarlinkDictionary = Record<string, any>;
|
|
2
|
+
declare class VarlinkError extends Error {
|
|
3
|
+
type_: string;
|
|
4
|
+
parameters: VarlinkDictionary;
|
|
5
|
+
constructor(type_: string, parameters: VarlinkDictionary);
|
|
6
|
+
}
|
|
7
|
+
declare class VarlinkMethod<Name extends string, Input extends VarlinkDictionary, Output extends VarlinkDictionary> {
|
|
8
|
+
name: Name;
|
|
9
|
+
private readonly inputPhantom;
|
|
10
|
+
private readonly outputPhantom;
|
|
11
|
+
constructor(name: Name, inputPhantom: Input, outputPhantom: Output);
|
|
12
|
+
}
|
|
13
|
+
type VarlinkMethodGetName<M> = M extends VarlinkMethod<infer Name, any, any> ? Name : never;
|
|
14
|
+
type VarlinkMethodGetInput<M> = M extends VarlinkMethod<any, infer Input, any> ? Input : never;
|
|
15
|
+
type VarlinkMethodGetOutput<M> = M extends VarlinkMethod<any, any, infer Output> ? Output : never;
|
|
16
|
+
declare class VarlinkDynamicMethod extends VarlinkMethod<string, VarlinkDictionary, VarlinkDictionary> {
|
|
17
|
+
constructor(name: string);
|
|
18
|
+
}
|
|
19
|
+
interface VarlinkRequest {
|
|
20
|
+
method: string;
|
|
21
|
+
parameters: VarlinkDictionary;
|
|
22
|
+
oneway: boolean;
|
|
23
|
+
more: boolean;
|
|
24
|
+
upgrade: boolean;
|
|
25
|
+
}
|
|
26
|
+
declare function serializeVarlinkRequest(request: VarlinkRequest): Uint8Array;
|
|
27
|
+
declare function deserializeVarlinkRequest(buffer: Uint8Array): VarlinkRequest;
|
|
28
|
+
interface VarlinkSuccessResponse {
|
|
29
|
+
error: undefined;
|
|
30
|
+
parameters: VarlinkDictionary;
|
|
31
|
+
continues: boolean;
|
|
32
|
+
}
|
|
33
|
+
interface VarlinkErrorResponse {
|
|
34
|
+
error: string;
|
|
35
|
+
parameters: VarlinkDictionary;
|
|
36
|
+
continues: undefined;
|
|
37
|
+
}
|
|
38
|
+
type VarlinkResponse = VarlinkSuccessResponse | VarlinkErrorResponse;
|
|
39
|
+
declare function serializeVarlinkResponse(response: VarlinkResponse): Uint8Array;
|
|
40
|
+
declare function deserializeVarlinkResponse(buffer: Uint8Array): VarlinkResponse;
|
|
41
|
+
|
|
42
|
+
export { type VarlinkDictionary, VarlinkDynamicMethod, VarlinkError, type VarlinkErrorResponse, VarlinkMethod, type VarlinkMethodGetInput, type VarlinkMethodGetName, type VarlinkMethodGetOutput, type VarlinkRequest, type VarlinkResponse, type VarlinkSuccessResponse, deserializeVarlinkRequest, deserializeVarlinkResponse, serializeVarlinkRequest, serializeVarlinkResponse };
|