@cocoar/signalarrr 4.0.0-beta.6
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/dist/index.cjs +222 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +58 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +184 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
HARRRConnection: () => HARRRConnection,
|
|
34
|
+
HARRRConnectionOptions: () => HARRRConnectionOptions
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/harrr-connection.ts
|
|
39
|
+
var signalR = __toESM(require("@microsoft/signalr"), 1);
|
|
40
|
+
|
|
41
|
+
// src/models/cancellation-token-reference.ts
|
|
42
|
+
function isCancellationTokenReference(v) {
|
|
43
|
+
return typeof v === "object" && v !== null && typeof v["Id"] === "string";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/cancellation-manager.ts
|
|
47
|
+
var CancellationManager = class {
|
|
48
|
+
_controllers = /* @__PURE__ */ new Map();
|
|
49
|
+
create(id) {
|
|
50
|
+
const controller = new AbortController();
|
|
51
|
+
this._controllers.set(id, controller);
|
|
52
|
+
return controller.signal;
|
|
53
|
+
}
|
|
54
|
+
cancel(id) {
|
|
55
|
+
const controller = this._controllers.get(id);
|
|
56
|
+
if (controller) {
|
|
57
|
+
controller.abort();
|
|
58
|
+
this._controllers.delete(id);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
remove(id) {
|
|
62
|
+
this._controllers.delete(id);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/harrr-connection.ts
|
|
67
|
+
var HARRRConnection = class _HARRRConnection {
|
|
68
|
+
_hubConnection;
|
|
69
|
+
_accessTokenFactory = () => "";
|
|
70
|
+
_serverRequestHandlers = /* @__PURE__ */ new Map();
|
|
71
|
+
_cancellationManager = new CancellationManager();
|
|
72
|
+
get baseUrl() {
|
|
73
|
+
return this._hubConnection.baseUrl;
|
|
74
|
+
}
|
|
75
|
+
set baseUrl(value) {
|
|
76
|
+
this._hubConnection.baseUrl = value;
|
|
77
|
+
}
|
|
78
|
+
get connectionId() {
|
|
79
|
+
return this._hubConnection.connectionId;
|
|
80
|
+
}
|
|
81
|
+
get state() {
|
|
82
|
+
return this._hubConnection.state;
|
|
83
|
+
}
|
|
84
|
+
get serverTimeoutInMilliseconds() {
|
|
85
|
+
return this._hubConnection.serverTimeoutInMilliseconds;
|
|
86
|
+
}
|
|
87
|
+
set serverTimeoutInMilliseconds(value) {
|
|
88
|
+
this._hubConnection.serverTimeoutInMilliseconds = value;
|
|
89
|
+
}
|
|
90
|
+
get keepAliveIntervalInMilliseconds() {
|
|
91
|
+
return this._hubConnection.keepAliveIntervalInMilliseconds;
|
|
92
|
+
}
|
|
93
|
+
set keepAliveIntervalInMilliseconds(value) {
|
|
94
|
+
this._hubConnection.keepAliveIntervalInMilliseconds = value;
|
|
95
|
+
}
|
|
96
|
+
constructor(hubConnection, _options) {
|
|
97
|
+
this._hubConnection = hubConnection;
|
|
98
|
+
const conn = hubConnection["connection"];
|
|
99
|
+
const factory = conn?.["_options"]?.["accessTokenFactory"] ?? conn?.["_accessTokenFactory"];
|
|
100
|
+
if (typeof factory === "function") {
|
|
101
|
+
this._accessTokenFactory = factory;
|
|
102
|
+
}
|
|
103
|
+
this._hubConnection.on("ChallengeAuthentication", async (req) => {
|
|
104
|
+
const token = this._accessTokenFactory();
|
|
105
|
+
await this._hubConnection.send("ReplyServerRequest", req.Id, token, null);
|
|
106
|
+
});
|
|
107
|
+
this._hubConnection.on("InvokeServerRequest", async (req) => {
|
|
108
|
+
let payload = null;
|
|
109
|
+
let error = null;
|
|
110
|
+
try {
|
|
111
|
+
payload = await this._dispatchServerMethod(req);
|
|
112
|
+
} catch (e) {
|
|
113
|
+
error = String(e);
|
|
114
|
+
}
|
|
115
|
+
await this._hubConnection.send("ReplyServerRequest", req.Id, payload, error);
|
|
116
|
+
});
|
|
117
|
+
this._hubConnection.on("InvokeServerMessage", async (req) => {
|
|
118
|
+
try {
|
|
119
|
+
await this._dispatchServerMethod(req);
|
|
120
|
+
} catch {
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
this._hubConnection.on("CancelTokenFromServer", (req) => {
|
|
124
|
+
if (req.CancellationGuid) {
|
|
125
|
+
this._cancellationManager.cancel(req.CancellationGuid);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async _dispatchServerMethod(req) {
|
|
130
|
+
const handler = this._serverRequestHandlers.get(req.Method);
|
|
131
|
+
if (!handler) return void 0;
|
|
132
|
+
const args = (req.Arguments ?? []).map((arg) => {
|
|
133
|
+
if (isCancellationTokenReference(arg) && req.CancellationGuid) {
|
|
134
|
+
return this._cancellationManager.create(req.CancellationGuid);
|
|
135
|
+
}
|
|
136
|
+
return arg;
|
|
137
|
+
});
|
|
138
|
+
return await handler(...args);
|
|
139
|
+
}
|
|
140
|
+
start() {
|
|
141
|
+
return this._hubConnection.start();
|
|
142
|
+
}
|
|
143
|
+
stop() {
|
|
144
|
+
return this._hubConnection.stop();
|
|
145
|
+
}
|
|
146
|
+
onClose(callback) {
|
|
147
|
+
this._hubConnection.onclose(callback);
|
|
148
|
+
}
|
|
149
|
+
onReconnecting(callback) {
|
|
150
|
+
this._hubConnection.onreconnecting(callback);
|
|
151
|
+
}
|
|
152
|
+
onReconnected(callback) {
|
|
153
|
+
this._hubConnection.onreconnected(callback);
|
|
154
|
+
}
|
|
155
|
+
invoke(methodName, ...args) {
|
|
156
|
+
const msg = {
|
|
157
|
+
Method: methodName,
|
|
158
|
+
Arguments: args,
|
|
159
|
+
Authorization: this._accessTokenFactory()
|
|
160
|
+
};
|
|
161
|
+
return this._hubConnection.invoke("InvokeMessageResult", msg).catch((err) => Promise.reject(this._extractException(err)));
|
|
162
|
+
}
|
|
163
|
+
send(methodName, ...args) {
|
|
164
|
+
const msg = {
|
|
165
|
+
Method: methodName,
|
|
166
|
+
Arguments: args,
|
|
167
|
+
Authorization: this._accessTokenFactory()
|
|
168
|
+
};
|
|
169
|
+
return this._hubConnection.send("InvokeMessage", msg);
|
|
170
|
+
}
|
|
171
|
+
stream(methodName, ...args) {
|
|
172
|
+
const msg = {
|
|
173
|
+
Method: methodName,
|
|
174
|
+
Arguments: args,
|
|
175
|
+
Authorization: this._accessTokenFactory()
|
|
176
|
+
};
|
|
177
|
+
return this._hubConnection.stream("StreamMessage", msg);
|
|
178
|
+
}
|
|
179
|
+
on(methodName, newMethod) {
|
|
180
|
+
this._hubConnection.on(methodName, newMethod);
|
|
181
|
+
}
|
|
182
|
+
onServerMethod(methodName, func) {
|
|
183
|
+
this._serverRequestHandlers.set(methodName, func);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
off(methodName, method) {
|
|
187
|
+
if (!method) {
|
|
188
|
+
this._hubConnection.off(methodName);
|
|
189
|
+
} else {
|
|
190
|
+
this._hubConnection.off(methodName, method);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
asSignalRHubConnection() {
|
|
194
|
+
return this._hubConnection;
|
|
195
|
+
}
|
|
196
|
+
static create(hubConnection, options) {
|
|
197
|
+
if (hubConnection instanceof Function) {
|
|
198
|
+
const builder = new signalR.HubConnectionBuilder();
|
|
199
|
+
hubConnection(builder);
|
|
200
|
+
return new _HARRRConnection(builder.build(), options);
|
|
201
|
+
}
|
|
202
|
+
return new _HARRRConnection(hubConnection, options);
|
|
203
|
+
}
|
|
204
|
+
_extractException(error) {
|
|
205
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
206
|
+
const matches = /.*\[(.*)\]\s*(.*)/m.exec(msg);
|
|
207
|
+
return {
|
|
208
|
+
type: matches?.[1] ?? "Error",
|
|
209
|
+
message: matches?.[2] ?? msg
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// src/harrr-connection-options.ts
|
|
215
|
+
var HARRRConnectionOptions = class {
|
|
216
|
+
};
|
|
217
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
218
|
+
0 && (module.exports = {
|
|
219
|
+
HARRRConnection,
|
|
220
|
+
HARRRConnectionOptions
|
|
221
|
+
});
|
|
222
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/harrr-connection.ts","../src/models/cancellation-token-reference.ts","../src/cancellation-manager.ts","../src/harrr-connection-options.ts"],"sourcesContent":["export { HARRRConnection } from './harrr-connection.js';\nexport { HARRRConnectionOptions } from './harrr-connection-options.js';\nexport type { ClientRequestMessage } from './models/client-request-message.js';\nexport type { ServerRequestMessage } from './models/server-request-message.js';\nexport type { CancellationTokenReference } from './models/cancellation-token-reference.js';\n","import * as signalR from '@microsoft/signalr';\nimport { ClientRequestMessage } from './models/client-request-message.js';\nimport { ServerRequestMessage } from './models/server-request-message.js';\nimport { isCancellationTokenReference } from './models/cancellation-token-reference.js';\nimport { HARRRConnectionOptions } from './harrr-connection-options.js';\nimport { CancellationManager } from './cancellation-manager.js';\n\nexport class HARRRConnection {\n private _hubConnection: signalR.HubConnection;\n private _accessTokenFactory: () => string = () => '';\n private _serverRequestHandlers = new Map<string, (...args: unknown[]) => unknown>();\n private _cancellationManager = new CancellationManager();\n\n public get baseUrl(): string {\n return this._hubConnection.baseUrl;\n }\n\n public set baseUrl(value: string) {\n this._hubConnection.baseUrl = value;\n }\n\n public get connectionId(): string | null {\n return this._hubConnection.connectionId;\n }\n\n public get state(): signalR.HubConnectionState {\n return this._hubConnection.state;\n }\n\n public get serverTimeoutInMilliseconds(): number {\n return this._hubConnection.serverTimeoutInMilliseconds;\n }\n\n public set serverTimeoutInMilliseconds(value: number) {\n this._hubConnection.serverTimeoutInMilliseconds = value;\n }\n\n public get keepAliveIntervalInMilliseconds(): number {\n return this._hubConnection.keepAliveIntervalInMilliseconds;\n }\n\n public set keepAliveIntervalInMilliseconds(value: number) {\n this._hubConnection.keepAliveIntervalInMilliseconds = value;\n }\n\n constructor(hubConnection: signalR.HubConnection, _options?: HARRRConnectionOptions) {\n this._hubConnection = hubConnection;\n\n const conn = (hubConnection as unknown as Record<string, unknown>)['connection'] as Record<string, unknown> | undefined;\n const factory =\n (conn?.['_options'] as Record<string, unknown> | undefined)?.['accessTokenFactory'] ??\n conn?.['_accessTokenFactory'];\n if (typeof factory === 'function') {\n this._accessTokenFactory = factory as () => string;\n }\n\n this._hubConnection.on('ChallengeAuthentication', async (req: ServerRequestMessage) => {\n const token = this._accessTokenFactory();\n await this._hubConnection.send('ReplyServerRequest', req.Id, token, null);\n });\n\n this._hubConnection.on('InvokeServerRequest', async (req: ServerRequestMessage) => {\n let payload: unknown = null;\n let error: string | null = null;\n try {\n payload = await this._dispatchServerMethod(req);\n } catch (e) {\n error = String(e);\n }\n await this._hubConnection.send('ReplyServerRequest', req.Id, payload, error);\n });\n\n this._hubConnection.on('InvokeServerMessage', async (req: ServerRequestMessage) => {\n try {\n await this._dispatchServerMethod(req);\n } catch {\n // ignored — fire-and-forget\n }\n });\n\n this._hubConnection.on('CancelTokenFromServer', (req: ServerRequestMessage) => {\n if (req.CancellationGuid) {\n this._cancellationManager.cancel(req.CancellationGuid);\n }\n });\n }\n\n private async _dispatchServerMethod(req: ServerRequestMessage): Promise<unknown> {\n const handler = this._serverRequestHandlers.get(req.Method);\n if (!handler) return undefined;\n\n const args = (req.Arguments ?? []).map(arg => {\n if (isCancellationTokenReference(arg) && req.CancellationGuid) {\n return this._cancellationManager.create(req.CancellationGuid);\n }\n return arg;\n });\n\n return await handler(...args);\n }\n\n public start(): Promise<void> {\n return this._hubConnection.start();\n }\n\n public stop(): Promise<void> {\n return this._hubConnection.stop();\n }\n\n public onClose(callback: (error?: Error) => void): void {\n this._hubConnection.onclose(callback);\n }\n\n public onReconnecting(callback: (error?: Error) => void): void {\n this._hubConnection.onreconnecting(callback);\n }\n\n public onReconnected(callback: (connectionId?: string) => void): void {\n this._hubConnection.onreconnected(callback);\n }\n\n public invoke<T>(methodName: string, ...args: unknown[]): Promise<T> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection\n .invoke<T>('InvokeMessageResult', msg)\n .catch(err => Promise.reject(this._extractException(err)));\n }\n\n public send(methodName: string, ...args: unknown[]): Promise<void> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection.send('InvokeMessage', msg);\n }\n\n public stream<T>(methodName: string, ...args: unknown[]): signalR.IStreamResult<T> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection.stream<T>('StreamMessage', msg);\n }\n\n public on(methodName: string, newMethod: (...args: any[]) => void): void {\n this._hubConnection.on(methodName, newMethod);\n }\n\n public onServerMethod(methodName: string, func: (...args: unknown[]) => unknown): this {\n this._serverRequestHandlers.set(methodName, func);\n return this;\n }\n\n public off(methodName: string): void;\n public off(methodName: string, method: (...args: any[]) => void): void;\n public off(methodName: string, method?: (...args: any[]) => void): void {\n if (!method) {\n this._hubConnection.off(methodName);\n } else {\n this._hubConnection.off(methodName, method);\n }\n }\n\n public asSignalRHubConnection(): signalR.HubConnection {\n return this._hubConnection;\n }\n\n public static create(\n hubConnection: signalR.HubConnection | ((builder: signalR.HubConnectionBuilder) => void),\n options?: HARRRConnectionOptions,\n ): HARRRConnection {\n if (hubConnection instanceof Function) {\n const builder = new signalR.HubConnectionBuilder();\n hubConnection(builder);\n return new HARRRConnection(builder.build(), options);\n }\n return new HARRRConnection(hubConnection, options);\n }\n\n private _extractException(error: unknown): { type: string; message: string } {\n const msg = error instanceof Error ? error.message : String(error);\n const matches = /.*\\[(.*)\\]\\s*(.*)/m.exec(msg);\n return {\n type: matches?.[1] ?? 'Error',\n message: matches?.[2] ?? msg,\n };\n }\n}\n","export interface CancellationTokenReference {\n Id: string;\n}\n\nexport function isCancellationTokenReference(v: unknown): v is CancellationTokenReference {\n return typeof v === 'object' && v !== null && typeof (v as Record<string, unknown>)['Id'] === 'string';\n}\n","export class CancellationManager {\n private _controllers = new Map<string, AbortController>();\n\n create(id: string): AbortSignal {\n const controller = new AbortController();\n this._controllers.set(id, controller);\n return controller.signal;\n }\n\n cancel(id: string): void {\n const controller = this._controllers.get(id);\n if (controller) {\n controller.abort();\n this._controllers.delete(id);\n }\n }\n\n remove(id: string): void {\n this._controllers.delete(id);\n }\n}\n","export class HARRRConnectionOptions {}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,cAAyB;;;ACIlB,SAAS,6BAA6B,GAA6C;AACxF,SAAO,OAAO,MAAM,YAAY,MAAM,QAAQ,OAAQ,EAA8B,IAAI,MAAM;AAChG;;;ACNO,IAAM,sBAAN,MAA0B;AAAA,EACvB,eAAe,oBAAI,IAA6B;AAAA,EAExD,OAAO,IAAyB;AAC9B,UAAM,aAAa,IAAI,gBAAgB;AACvC,SAAK,aAAa,IAAI,IAAI,UAAU;AACpC,WAAO,WAAW;AAAA,EACpB;AAAA,EAEA,OAAO,IAAkB;AACvB,UAAM,aAAa,KAAK,aAAa,IAAI,EAAE;AAC3C,QAAI,YAAY;AACd,iBAAW,MAAM;AACjB,WAAK,aAAa,OAAO,EAAE;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,OAAO,IAAkB;AACvB,SAAK,aAAa,OAAO,EAAE;AAAA,EAC7B;AACF;;;AFbO,IAAM,kBAAN,MAAM,iBAAgB;AAAA,EACnB;AAAA,EACA,sBAAoC,MAAM;AAAA,EAC1C,yBAAyB,oBAAI,IAA6C;AAAA,EAC1E,uBAAuB,IAAI,oBAAoB;AAAA,EAEvD,IAAW,UAAkB;AAC3B,WAAO,KAAK,eAAe;AAAA,EAC7B;AAAA,EAEA,IAAW,QAAQ,OAAe;AAChC,SAAK,eAAe,UAAU;AAAA,EAChC;AAAA,EAE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
5
|
+
|
|
6
|
+
declare class HARRRConnection {
|
|
7
|
+
private _hubConnection;
|
|
8
|
+
private _accessTokenFactory;
|
|
9
|
+
private _serverRequestHandlers;
|
|
10
|
+
private _cancellationManager;
|
|
11
|
+
get baseUrl(): string;
|
|
12
|
+
set baseUrl(value: string);
|
|
13
|
+
get connectionId(): string | null;
|
|
14
|
+
get state(): signalR.HubConnectionState;
|
|
15
|
+
get serverTimeoutInMilliseconds(): number;
|
|
16
|
+
set serverTimeoutInMilliseconds(value: number);
|
|
17
|
+
get keepAliveIntervalInMilliseconds(): number;
|
|
18
|
+
set keepAliveIntervalInMilliseconds(value: number);
|
|
19
|
+
constructor(hubConnection: signalR.HubConnection, _options?: HARRRConnectionOptions);
|
|
20
|
+
private _dispatchServerMethod;
|
|
21
|
+
start(): Promise<void>;
|
|
22
|
+
stop(): Promise<void>;
|
|
23
|
+
onClose(callback: (error?: Error) => void): void;
|
|
24
|
+
onReconnecting(callback: (error?: Error) => void): void;
|
|
25
|
+
onReconnected(callback: (connectionId?: string) => void): void;
|
|
26
|
+
invoke<T>(methodName: string, ...args: unknown[]): Promise<T>;
|
|
27
|
+
send(methodName: string, ...args: unknown[]): Promise<void>;
|
|
28
|
+
stream<T>(methodName: string, ...args: unknown[]): signalR.IStreamResult<T>;
|
|
29
|
+
on(methodName: string, newMethod: (...args: any[]) => void): void;
|
|
30
|
+
onServerMethod(methodName: string, func: (...args: unknown[]) => unknown): this;
|
|
31
|
+
off(methodName: string): void;
|
|
32
|
+
off(methodName: string, method: (...args: any[]) => void): void;
|
|
33
|
+
asSignalRHubConnection(): signalR.HubConnection;
|
|
34
|
+
static create(hubConnection: signalR.HubConnection | ((builder: signalR.HubConnectionBuilder) => void), options?: HARRRConnectionOptions): HARRRConnection;
|
|
35
|
+
private _extractException;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ClientRequestMessage {
|
|
39
|
+
Method: string;
|
|
40
|
+
Arguments: unknown[];
|
|
41
|
+
Authorization?: string;
|
|
42
|
+
GenericArguments?: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface ServerRequestMessage {
|
|
46
|
+
Id: string;
|
|
47
|
+
Method: string;
|
|
48
|
+
Arguments?: unknown[];
|
|
49
|
+
GenericArguments?: string[];
|
|
50
|
+
CancellationGuid?: string;
|
|
51
|
+
StreamId?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface CancellationTokenReference {
|
|
55
|
+
Id: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { type CancellationTokenReference, type ClientRequestMessage, HARRRConnection, HARRRConnectionOptions, type ServerRequestMessage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as signalR from '@microsoft/signalr';
|
|
2
|
+
|
|
3
|
+
declare class HARRRConnectionOptions {
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare class HARRRConnection {
|
|
7
|
+
private _hubConnection;
|
|
8
|
+
private _accessTokenFactory;
|
|
9
|
+
private _serverRequestHandlers;
|
|
10
|
+
private _cancellationManager;
|
|
11
|
+
get baseUrl(): string;
|
|
12
|
+
set baseUrl(value: string);
|
|
13
|
+
get connectionId(): string | null;
|
|
14
|
+
get state(): signalR.HubConnectionState;
|
|
15
|
+
get serverTimeoutInMilliseconds(): number;
|
|
16
|
+
set serverTimeoutInMilliseconds(value: number);
|
|
17
|
+
get keepAliveIntervalInMilliseconds(): number;
|
|
18
|
+
set keepAliveIntervalInMilliseconds(value: number);
|
|
19
|
+
constructor(hubConnection: signalR.HubConnection, _options?: HARRRConnectionOptions);
|
|
20
|
+
private _dispatchServerMethod;
|
|
21
|
+
start(): Promise<void>;
|
|
22
|
+
stop(): Promise<void>;
|
|
23
|
+
onClose(callback: (error?: Error) => void): void;
|
|
24
|
+
onReconnecting(callback: (error?: Error) => void): void;
|
|
25
|
+
onReconnected(callback: (connectionId?: string) => void): void;
|
|
26
|
+
invoke<T>(methodName: string, ...args: unknown[]): Promise<T>;
|
|
27
|
+
send(methodName: string, ...args: unknown[]): Promise<void>;
|
|
28
|
+
stream<T>(methodName: string, ...args: unknown[]): signalR.IStreamResult<T>;
|
|
29
|
+
on(methodName: string, newMethod: (...args: any[]) => void): void;
|
|
30
|
+
onServerMethod(methodName: string, func: (...args: unknown[]) => unknown): this;
|
|
31
|
+
off(methodName: string): void;
|
|
32
|
+
off(methodName: string, method: (...args: any[]) => void): void;
|
|
33
|
+
asSignalRHubConnection(): signalR.HubConnection;
|
|
34
|
+
static create(hubConnection: signalR.HubConnection | ((builder: signalR.HubConnectionBuilder) => void), options?: HARRRConnectionOptions): HARRRConnection;
|
|
35
|
+
private _extractException;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ClientRequestMessage {
|
|
39
|
+
Method: string;
|
|
40
|
+
Arguments: unknown[];
|
|
41
|
+
Authorization?: string;
|
|
42
|
+
GenericArguments?: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface ServerRequestMessage {
|
|
46
|
+
Id: string;
|
|
47
|
+
Method: string;
|
|
48
|
+
Arguments?: unknown[];
|
|
49
|
+
GenericArguments?: string[];
|
|
50
|
+
CancellationGuid?: string;
|
|
51
|
+
StreamId?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface CancellationTokenReference {
|
|
55
|
+
Id: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { type CancellationTokenReference, type ClientRequestMessage, HARRRConnection, HARRRConnectionOptions, type ServerRequestMessage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// src/harrr-connection.ts
|
|
2
|
+
import * as signalR from "@microsoft/signalr";
|
|
3
|
+
|
|
4
|
+
// src/models/cancellation-token-reference.ts
|
|
5
|
+
function isCancellationTokenReference(v) {
|
|
6
|
+
return typeof v === "object" && v !== null && typeof v["Id"] === "string";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/cancellation-manager.ts
|
|
10
|
+
var CancellationManager = class {
|
|
11
|
+
_controllers = /* @__PURE__ */ new Map();
|
|
12
|
+
create(id) {
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
this._controllers.set(id, controller);
|
|
15
|
+
return controller.signal;
|
|
16
|
+
}
|
|
17
|
+
cancel(id) {
|
|
18
|
+
const controller = this._controllers.get(id);
|
|
19
|
+
if (controller) {
|
|
20
|
+
controller.abort();
|
|
21
|
+
this._controllers.delete(id);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
remove(id) {
|
|
25
|
+
this._controllers.delete(id);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/harrr-connection.ts
|
|
30
|
+
var HARRRConnection = class _HARRRConnection {
|
|
31
|
+
_hubConnection;
|
|
32
|
+
_accessTokenFactory = () => "";
|
|
33
|
+
_serverRequestHandlers = /* @__PURE__ */ new Map();
|
|
34
|
+
_cancellationManager = new CancellationManager();
|
|
35
|
+
get baseUrl() {
|
|
36
|
+
return this._hubConnection.baseUrl;
|
|
37
|
+
}
|
|
38
|
+
set baseUrl(value) {
|
|
39
|
+
this._hubConnection.baseUrl = value;
|
|
40
|
+
}
|
|
41
|
+
get connectionId() {
|
|
42
|
+
return this._hubConnection.connectionId;
|
|
43
|
+
}
|
|
44
|
+
get state() {
|
|
45
|
+
return this._hubConnection.state;
|
|
46
|
+
}
|
|
47
|
+
get serverTimeoutInMilliseconds() {
|
|
48
|
+
return this._hubConnection.serverTimeoutInMilliseconds;
|
|
49
|
+
}
|
|
50
|
+
set serverTimeoutInMilliseconds(value) {
|
|
51
|
+
this._hubConnection.serverTimeoutInMilliseconds = value;
|
|
52
|
+
}
|
|
53
|
+
get keepAliveIntervalInMilliseconds() {
|
|
54
|
+
return this._hubConnection.keepAliveIntervalInMilliseconds;
|
|
55
|
+
}
|
|
56
|
+
set keepAliveIntervalInMilliseconds(value) {
|
|
57
|
+
this._hubConnection.keepAliveIntervalInMilliseconds = value;
|
|
58
|
+
}
|
|
59
|
+
constructor(hubConnection, _options) {
|
|
60
|
+
this._hubConnection = hubConnection;
|
|
61
|
+
const conn = hubConnection["connection"];
|
|
62
|
+
const factory = conn?.["_options"]?.["accessTokenFactory"] ?? conn?.["_accessTokenFactory"];
|
|
63
|
+
if (typeof factory === "function") {
|
|
64
|
+
this._accessTokenFactory = factory;
|
|
65
|
+
}
|
|
66
|
+
this._hubConnection.on("ChallengeAuthentication", async (req) => {
|
|
67
|
+
const token = this._accessTokenFactory();
|
|
68
|
+
await this._hubConnection.send("ReplyServerRequest", req.Id, token, null);
|
|
69
|
+
});
|
|
70
|
+
this._hubConnection.on("InvokeServerRequest", async (req) => {
|
|
71
|
+
let payload = null;
|
|
72
|
+
let error = null;
|
|
73
|
+
try {
|
|
74
|
+
payload = await this._dispatchServerMethod(req);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
error = String(e);
|
|
77
|
+
}
|
|
78
|
+
await this._hubConnection.send("ReplyServerRequest", req.Id, payload, error);
|
|
79
|
+
});
|
|
80
|
+
this._hubConnection.on("InvokeServerMessage", async (req) => {
|
|
81
|
+
try {
|
|
82
|
+
await this._dispatchServerMethod(req);
|
|
83
|
+
} catch {
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
this._hubConnection.on("CancelTokenFromServer", (req) => {
|
|
87
|
+
if (req.CancellationGuid) {
|
|
88
|
+
this._cancellationManager.cancel(req.CancellationGuid);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
async _dispatchServerMethod(req) {
|
|
93
|
+
const handler = this._serverRequestHandlers.get(req.Method);
|
|
94
|
+
if (!handler) return void 0;
|
|
95
|
+
const args = (req.Arguments ?? []).map((arg) => {
|
|
96
|
+
if (isCancellationTokenReference(arg) && req.CancellationGuid) {
|
|
97
|
+
return this._cancellationManager.create(req.CancellationGuid);
|
|
98
|
+
}
|
|
99
|
+
return arg;
|
|
100
|
+
});
|
|
101
|
+
return await handler(...args);
|
|
102
|
+
}
|
|
103
|
+
start() {
|
|
104
|
+
return this._hubConnection.start();
|
|
105
|
+
}
|
|
106
|
+
stop() {
|
|
107
|
+
return this._hubConnection.stop();
|
|
108
|
+
}
|
|
109
|
+
onClose(callback) {
|
|
110
|
+
this._hubConnection.onclose(callback);
|
|
111
|
+
}
|
|
112
|
+
onReconnecting(callback) {
|
|
113
|
+
this._hubConnection.onreconnecting(callback);
|
|
114
|
+
}
|
|
115
|
+
onReconnected(callback) {
|
|
116
|
+
this._hubConnection.onreconnected(callback);
|
|
117
|
+
}
|
|
118
|
+
invoke(methodName, ...args) {
|
|
119
|
+
const msg = {
|
|
120
|
+
Method: methodName,
|
|
121
|
+
Arguments: args,
|
|
122
|
+
Authorization: this._accessTokenFactory()
|
|
123
|
+
};
|
|
124
|
+
return this._hubConnection.invoke("InvokeMessageResult", msg).catch((err) => Promise.reject(this._extractException(err)));
|
|
125
|
+
}
|
|
126
|
+
send(methodName, ...args) {
|
|
127
|
+
const msg = {
|
|
128
|
+
Method: methodName,
|
|
129
|
+
Arguments: args,
|
|
130
|
+
Authorization: this._accessTokenFactory()
|
|
131
|
+
};
|
|
132
|
+
return this._hubConnection.send("InvokeMessage", msg);
|
|
133
|
+
}
|
|
134
|
+
stream(methodName, ...args) {
|
|
135
|
+
const msg = {
|
|
136
|
+
Method: methodName,
|
|
137
|
+
Arguments: args,
|
|
138
|
+
Authorization: this._accessTokenFactory()
|
|
139
|
+
};
|
|
140
|
+
return this._hubConnection.stream("StreamMessage", msg);
|
|
141
|
+
}
|
|
142
|
+
on(methodName, newMethod) {
|
|
143
|
+
this._hubConnection.on(methodName, newMethod);
|
|
144
|
+
}
|
|
145
|
+
onServerMethod(methodName, func) {
|
|
146
|
+
this._serverRequestHandlers.set(methodName, func);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
off(methodName, method) {
|
|
150
|
+
if (!method) {
|
|
151
|
+
this._hubConnection.off(methodName);
|
|
152
|
+
} else {
|
|
153
|
+
this._hubConnection.off(methodName, method);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
asSignalRHubConnection() {
|
|
157
|
+
return this._hubConnection;
|
|
158
|
+
}
|
|
159
|
+
static create(hubConnection, options) {
|
|
160
|
+
if (hubConnection instanceof Function) {
|
|
161
|
+
const builder = new signalR.HubConnectionBuilder();
|
|
162
|
+
hubConnection(builder);
|
|
163
|
+
return new _HARRRConnection(builder.build(), options);
|
|
164
|
+
}
|
|
165
|
+
return new _HARRRConnection(hubConnection, options);
|
|
166
|
+
}
|
|
167
|
+
_extractException(error) {
|
|
168
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
169
|
+
const matches = /.*\[(.*)\]\s*(.*)/m.exec(msg);
|
|
170
|
+
return {
|
|
171
|
+
type: matches?.[1] ?? "Error",
|
|
172
|
+
message: matches?.[2] ?? msg
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/harrr-connection-options.ts
|
|
178
|
+
var HARRRConnectionOptions = class {
|
|
179
|
+
};
|
|
180
|
+
export {
|
|
181
|
+
HARRRConnection,
|
|
182
|
+
HARRRConnectionOptions
|
|
183
|
+
};
|
|
184
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/harrr-connection.ts","../src/models/cancellation-token-reference.ts","../src/cancellation-manager.ts","../src/harrr-connection-options.ts"],"sourcesContent":["import * as signalR from '@microsoft/signalr';\nimport { ClientRequestMessage } from './models/client-request-message.js';\nimport { ServerRequestMessage } from './models/server-request-message.js';\nimport { isCancellationTokenReference } from './models/cancellation-token-reference.js';\nimport { HARRRConnectionOptions } from './harrr-connection-options.js';\nimport { CancellationManager } from './cancellation-manager.js';\n\nexport class HARRRConnection {\n private _hubConnection: signalR.HubConnection;\n private _accessTokenFactory: () => string = () => '';\n private _serverRequestHandlers = new Map<string, (...args: unknown[]) => unknown>();\n private _cancellationManager = new CancellationManager();\n\n public get baseUrl(): string {\n return this._hubConnection.baseUrl;\n }\n\n public set baseUrl(value: string) {\n this._hubConnection.baseUrl = value;\n }\n\n public get connectionId(): string | null {\n return this._hubConnection.connectionId;\n }\n\n public get state(): signalR.HubConnectionState {\n return this._hubConnection.state;\n }\n\n public get serverTimeoutInMilliseconds(): number {\n return this._hubConnection.serverTimeoutInMilliseconds;\n }\n\n public set serverTimeoutInMilliseconds(value: number) {\n this._hubConnection.serverTimeoutInMilliseconds = value;\n }\n\n public get keepAliveIntervalInMilliseconds(): number {\n return this._hubConnection.keepAliveIntervalInMilliseconds;\n }\n\n public set keepAliveIntervalInMilliseconds(value: number) {\n this._hubConnection.keepAliveIntervalInMilliseconds = value;\n }\n\n constructor(hubConnection: signalR.HubConnection, _options?: HARRRConnectionOptions) {\n this._hubConnection = hubConnection;\n\n const conn = (hubConnection as unknown as Record<string, unknown>)['connection'] as Record<string, unknown> | undefined;\n const factory =\n (conn?.['_options'] as Record<string, unknown> | undefined)?.['accessTokenFactory'] ??\n conn?.['_accessTokenFactory'];\n if (typeof factory === 'function') {\n this._accessTokenFactory = factory as () => string;\n }\n\n this._hubConnection.on('ChallengeAuthentication', async (req: ServerRequestMessage) => {\n const token = this._accessTokenFactory();\n await this._hubConnection.send('ReplyServerRequest', req.Id, token, null);\n });\n\n this._hubConnection.on('InvokeServerRequest', async (req: ServerRequestMessage) => {\n let payload: unknown = null;\n let error: string | null = null;\n try {\n payload = await this._dispatchServerMethod(req);\n } catch (e) {\n error = String(e);\n }\n await this._hubConnection.send('ReplyServerRequest', req.Id, payload, error);\n });\n\n this._hubConnection.on('InvokeServerMessage', async (req: ServerRequestMessage) => {\n try {\n await this._dispatchServerMethod(req);\n } catch {\n // ignored — fire-and-forget\n }\n });\n\n this._hubConnection.on('CancelTokenFromServer', (req: ServerRequestMessage) => {\n if (req.CancellationGuid) {\n this._cancellationManager.cancel(req.CancellationGuid);\n }\n });\n }\n\n private async _dispatchServerMethod(req: ServerRequestMessage): Promise<unknown> {\n const handler = this._serverRequestHandlers.get(req.Method);\n if (!handler) return undefined;\n\n const args = (req.Arguments ?? []).map(arg => {\n if (isCancellationTokenReference(arg) && req.CancellationGuid) {\n return this._cancellationManager.create(req.CancellationGuid);\n }\n return arg;\n });\n\n return await handler(...args);\n }\n\n public start(): Promise<void> {\n return this._hubConnection.start();\n }\n\n public stop(): Promise<void> {\n return this._hubConnection.stop();\n }\n\n public onClose(callback: (error?: Error) => void): void {\n this._hubConnection.onclose(callback);\n }\n\n public onReconnecting(callback: (error?: Error) => void): void {\n this._hubConnection.onreconnecting(callback);\n }\n\n public onReconnected(callback: (connectionId?: string) => void): void {\n this._hubConnection.onreconnected(callback);\n }\n\n public invoke<T>(methodName: string, ...args: unknown[]): Promise<T> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection\n .invoke<T>('InvokeMessageResult', msg)\n .catch(err => Promise.reject(this._extractException(err)));\n }\n\n public send(methodName: string, ...args: unknown[]): Promise<void> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection.send('InvokeMessage', msg);\n }\n\n public stream<T>(methodName: string, ...args: unknown[]): signalR.IStreamResult<T> {\n const msg: ClientRequestMessage = {\n Method: methodName,\n Arguments: args,\n Authorization: this._accessTokenFactory(),\n };\n return this._hubConnection.stream<T>('StreamMessage', msg);\n }\n\n public on(methodName: string, newMethod: (...args: any[]) => void): void {\n this._hubConnection.on(methodName, newMethod);\n }\n\n public onServerMethod(methodName: string, func: (...args: unknown[]) => unknown): this {\n this._serverRequestHandlers.set(methodName, func);\n return this;\n }\n\n public off(methodName: string): void;\n public off(methodName: string, method: (...args: any[]) => void): void;\n public off(methodName: string, method?: (...args: any[]) => void): void {\n if (!method) {\n this._hubConnection.off(methodName);\n } else {\n this._hubConnection.off(methodName, method);\n }\n }\n\n public asSignalRHubConnection(): signalR.HubConnection {\n return this._hubConnection;\n }\n\n public static create(\n hubConnection: signalR.HubConnection | ((builder: signalR.HubConnectionBuilder) => void),\n options?: HARRRConnectionOptions,\n ): HARRRConnection {\n if (hubConnection instanceof Function) {\n const builder = new signalR.HubConnectionBuilder();\n hubConnection(builder);\n return new HARRRConnection(builder.build(), options);\n }\n return new HARRRConnection(hubConnection, options);\n }\n\n private _extractException(error: unknown): { type: string; message: string } {\n const msg = error instanceof Error ? error.message : String(error);\n const matches = /.*\\[(.*)\\]\\s*(.*)/m.exec(msg);\n return {\n type: matches?.[1] ?? 'Error',\n message: matches?.[2] ?? msg,\n };\n }\n}\n","export interface CancellationTokenReference {\n Id: string;\n}\n\nexport function isCancellationTokenReference(v: unknown): v is CancellationTokenReference {\n return typeof v === 'object' && v !== null && typeof (v as Record<string, unknown>)['Id'] === 'string';\n}\n","export class CancellationManager {\n private _controllers = new Map<string, AbortController>();\n\n create(id: string): AbortSignal {\n const controller = new AbortController();\n this._controllers.set(id, controller);\n return controller.signal;\n }\n\n cancel(id: string): void {\n const controller = this._controllers.get(id);\n if (controller) {\n controller.abort();\n this._controllers.delete(id);\n }\n }\n\n remove(id: string): void {\n this._controllers.delete(id);\n }\n}\n","export class HARRRConnectionOptions {}\n"],"mappings":";AAAA,YAAY,aAAa;;;ACIlB,SAAS,6BAA6B,GAA6C;AACxF,SAAO,OAAO,MAAM,YAAY,MAAM,QAAQ,OAAQ,EAA8B,IAAI,MAAM;AAChG;;;ACNO,IAAM,sBAAN,MAA0B;AAAA,EACvB,eAAe,oBAAI,IAA6B;AAAA,EAExD,OAAO,IAAyB;AAC9B,UAAM,aAAa,IAAI,gBAAgB;AACvC,SAAK,aAAa,IAAI,IAAI,UAAU;AACpC,WAAO,WAAW;AAAA,EACpB;AAAA,EAEA,OAAO,IAAkB;AACvB,UAAM,aAAa,KAAK,aAAa,IAAI,EAAE;AAC3C,QAAI,YAAY;AACd,iBAAW,MAAM;AACjB,WAAK,aAAa,OAAO,EAAE;AAAA,IAC7B;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,QAAQ,MAAM,UAAU,OAAO,KAAK;AACjE,UAAM,UAAU,qBAAqB,KAAK,GAAG;AAC7C,WAAO;AAAA,MACL,MAAM,UAAU,CAAC,KAAK;AAAA,MACtB,SAAS,UAAU,CAAC,KAAK;AAAA,IAC3B;AAAA,EACF;AACF;;;AGjMO,IAAM,yBAAN,MAA6B;AAAC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cocoar/signalarrr",
|
|
3
|
+
"version": "4.0.0-beta.6",
|
|
4
|
+
"description": "TypeScript client for SignalARRR — type-safe RPC over SignalR",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.cjs"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"dev": "tsup --watch"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/"
|
|
21
|
+
],
|
|
22
|
+
"author": "Bernhard Windisch",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@microsoft/signalr": "^10.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsup": "^8.0.0",
|
|
29
|
+
"typescript": "^5.7.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|