-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConnection.ts
More file actions
138 lines (121 loc) · 4.32 KB
/
Connection.ts
File metadata and controls
138 lines (121 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import type { ConnectionDirection } from '../../models/message.model';
import type { ConnectionHeaders, FetchOptions, FetchResponse } from '../../models/connection.model';
import { ConnectionStatus } from '../../models/connection.model';
import * as grpc from '@grpc/grpc-js';
/**
* @description This class represents a connection to a target client and manages sending messages to it.
*/
export class Connection {
private _token: string;
private _targetAddress: string;
private _status: ConnectionStatus;
private _peerClient: any;
public direction: ConnectionDirection;
/**
* Creates a new Connection instance with the given token, target address, and connection direction.
*
* @param token - The authentication token for the connection.
* @param targetAddress - The unique address of the target client.
* @param direction - The direction of the connection (e.g., sending or receiving).
*/
constructor(token: string, targetAddress: string, direction: ConnectionDirection, peerCtor: any) {
this._token = token;
this._targetAddress = targetAddress;
this._peerClient = new peerCtor(targetAddress, grpc.credentials.createInsecure());
this.direction = direction;
this._status = ConnectionStatus.CONNECTED;
}
/**
* Replace newly generated token
* @param token New token to be replaced
*/
public set token(token: string) {
this._token = token;
}
/**
* Revokes the token of the connection, effectively invalidating it.
* The connection status is set to UNAUTHORIZED.
*/
public handleRevokeToken(): void {
this._token = '';
this._status = ConnectionStatus.UNAUTHORIZED;
}
/**
* Returns token for this Connection object
* @returns Connection token
*/
public get token(): string {
return this._token;
}
/**
* Returns status for specific
* @returns Connection status
*/
public get status(): string {
return this._status;
}
/**
* Sets the status of this connection.
* @param status The new status of the connection.
*/
public set status(status: ConnectionStatus) {
this._status = status;
}
/**
* Returns target address for this Connection object
* @returns Target address
*/
public get targetAddress(): string {
return this._targetAddress;
}
/**
* "HTTP-like" fetch via gRPC protocol
* @returns Promise with peer's response
*/
public fetch(options: FetchOptions = {}): Promise<FetchResponse> {
if (!this._peerClient) {
return Promise.reject(new Error(`Peer client not attached for ${this.targetAddress}`));
}
// Build a request object
const method = (options.method ?? 'POST').toUpperCase();
const path = options.path ?? '/';
const headers: ConnectionHeaders = { ...(options.headers ?? {}) };
let bodyBuf: Buffer = Buffer.alloc(0);
const b = options.body;
if (b != null) {
if (Buffer.isBuffer(b)) bodyBuf = b;
else if (b instanceof Uint8Array) bodyBuf = Buffer.from(b);
else if (typeof b === 'string') bodyBuf = Buffer.from(b, 'utf8');
else return Promise.reject(new Error('Body must be a string/Buffer/Uint8Array'));
}
const req = { method, path, headers, body: bodyBuf };
// Return promise with response
return new Promise<FetchResponse>((resolve, reject) => {
this._peerClient.Fetch(req, (err: any, resp: any) => {
if (err) return reject(err);
const resBody = resp?.body ? Buffer.from(resp.body) : Buffer.alloc(0);
const fetchResponse: FetchResponse = {
status: Number(resp?.status ?? 200),
headers: resp?.headers ?? {},
body: resBody,
text: async () => resBody.toString('utf8'),
json: async () => JSON.parse(resBody.toString('utf8')),
};
resolve(fetchResponse);
});
});
}
}