This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
102 lines (90 loc) · 2.71 KB
/
index.ts
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
/**
* @license
* Copyright (c) Aiden.ai
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as express from 'express';
import { NodeHttpTransport } from '@improbable-eng/grpc-web-node-http-transport';
import { bankingServiceDefinition } from './service';
import { getBankingHandler } from './handler';
import {
AuthServerContextConnector,
AuthClientContextConnector,
} from './auth_context';
import * as http from 'http';
import { ModuleRpcProtocolServer } from '../../protocol/server';
import { ModuleRpcProtocolClient } from '../../protocol/client';
main().catch(
/* istanbul ignore next */
err => {
console.error(err);
process.exit(1);
},
);
async function main() {
// We launch an Express server, register the RPC routes using the
// `http_json` protocol, initialize an `grpc_web` client connected to this server
// and perform some remote procedure calls.
//
// The context comes from a mock "authentication" connector.
const server = setupServer();
try {
await clientInteraction(`http://127.0.0.1:${server.address().port}/api`);
} finally {
server.close();
}
}
function setupServer() {
const app = express();
app.use(
'/api',
ModuleRpcProtocolServer.registerRpcRoutes(
bankingServiceDefinition,
getBankingHandler(),
{
serverContextConnector: new AuthServerContextConnector('u1'),
},
),
);
const server = http.createServer(app).listen();
const port = server.address().port;
app.set('port', port);
return server;
}
async function clientInteraction(remoteAddress: string) {
const client = ModuleRpcProtocolClient.getRpcClient(
bankingServiceDefinition,
{
remoteAddress,
clientContextConnector: new AuthClientContextConnector('u1'),
getGrpcWebTransport: NodeHttpTransport(),
},
);
const balanceResponseBefore = await client.getBalance({});
console.log('Balance is', balanceResponseBefore.value);
console.log('Transfering...');
await client.transfer({
toUserId: 'u2',
amount: balanceResponseBefore.value / 2,
});
const balanceResponseAfter = await client.getBalance({});
console.log('Now balance is', balanceResponseAfter.value);
// If the user is not expected, an unauthenticated error is thrown
{
const unauthenticatedClient = ModuleRpcProtocolClient.getRpcClient(
bankingServiceDefinition,
{
remoteAddress,
clientContextConnector: new AuthClientContextConnector('u2'),
getGrpcWebTransport: NodeHttpTransport(),
},
);
try {
await unauthenticatedClient.getBalance({});
} catch (err) {
console.log('Expected error', err);
}
}
}