@synnaxlabs/client 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/.DS_Store +0 -0
  2. package/.editorconfig +15 -0
  3. package/.eslintrc.json +33 -0
  4. package/.gitignore +9 -0
  5. package/.nyc_output/20720f2d-6abe-420f-a3c5-304d52d60827.json +1 -0
  6. package/.nyc_output/4725921c-6f1b-4ae9-9819-e455f702d31c.json +1 -0
  7. package/.nyc_output/47478588-5ffd-4332-873c-facaa4a2fc38.json +1 -0
  8. package/.nyc_output/48180641-e0b2-49ab-a6eb-e7910e9eac2f.json +1 -0
  9. package/.nyc_output/cb0abf31-740f-47db-b94a-8e3f8f117cb8.json +1 -0
  10. package/.nyc_output/fc77fce2-dad0-49a8-8d4b-0a9014ecf8c5.json +1 -0
  11. package/.nyc_output/processinfo/20720f2d-6abe-420f-a3c5-304d52d60827.json +1 -0
  12. package/.nyc_output/processinfo/4725921c-6f1b-4ae9-9819-e455f702d31c.json +1 -0
  13. package/.nyc_output/processinfo/47478588-5ffd-4332-873c-facaa4a2fc38.json +1 -0
  14. package/.nyc_output/processinfo/48180641-e0b2-49ab-a6eb-e7910e9eac2f.json +1 -0
  15. package/.nyc_output/processinfo/cb0abf31-740f-47db-b94a-8e3f8f117cb8.json +1 -0
  16. package/.nyc_output/processinfo/fc77fce2-dad0-49a8-8d4b-0a9014ecf8c5.json +1 -0
  17. package/.nyc_output/processinfo/index.json +1 -0
  18. package/.prettierignore +2 -0
  19. package/CHANGELOG.md +5 -0
  20. package/build/main/index.d.ts +1 -1
  21. package/build/main/index.js +2 -3
  22. package/build/main/lib/auth.d.ts +54 -0
  23. package/build/main/lib/auth.js +62 -0
  24. package/build/main/lib/auth.spec.d.ts +1 -0
  25. package/build/main/lib/auth.spec.js +39 -0
  26. package/build/main/lib/channel/channel.spec.js +17 -3
  27. package/build/main/lib/channel/client.d.ts +2 -2
  28. package/build/main/lib/channel/client.js +6 -3
  29. package/build/main/lib/channel/payload.d.ts +2 -2
  30. package/build/main/lib/client.d.ts +13 -6
  31. package/build/main/lib/client.js +16 -4
  32. package/build/main/lib/segment/iterator.spec.js +14 -3
  33. package/build/main/lib/segment/typed.js +4 -4
  34. package/build/main/lib/segment/writer.spec.js +17 -3
  35. package/build/main/lib/telem.d.ts +2 -2
  36. package/build/main/lib/telem.js +4 -4
  37. package/build/main/lib/telem.spec.js +4 -2
  38. package/build/main/lib/transport.d.ts +2 -1
  39. package/build/main/lib/transport.js +5 -1
  40. package/build/main/lib/user/payload.d.ts +12 -0
  41. package/build/main/lib/user/payload.js +9 -0
  42. package/build/main/setupspecs.d.ts +4 -0
  43. package/build/main/setupspecs.js +17 -0
  44. package/build/module/index.d.ts +1 -1
  45. package/build/module/index.js +2 -2
  46. package/build/module/lib/auth.d.ts +54 -0
  47. package/build/module/lib/auth.js +63 -0
  48. package/build/module/lib/auth.spec.d.ts +1 -0
  49. package/build/module/lib/auth.spec.js +34 -0
  50. package/build/module/lib/channel/channel.spec.js +17 -3
  51. package/build/module/lib/channel/client.d.ts +2 -2
  52. package/build/module/lib/channel/client.js +6 -3
  53. package/build/module/lib/channel/payload.d.ts +2 -2
  54. package/build/module/lib/client.d.ts +13 -6
  55. package/build/module/lib/client.js +17 -4
  56. package/build/module/lib/segment/iterator.spec.js +14 -3
  57. package/build/module/lib/segment/typed.js +5 -5
  58. package/build/module/lib/segment/writer.spec.js +18 -4
  59. package/build/module/lib/telem.d.ts +2 -2
  60. package/build/module/lib/telem.js +4 -4
  61. package/build/module/lib/telem.spec.js +5 -3
  62. package/build/module/lib/transport.d.ts +2 -1
  63. package/build/module/lib/transport.js +5 -1
  64. package/build/module/lib/user/payload.d.ts +12 -0
  65. package/build/module/lib/user/payload.js +6 -0
  66. package/build/module/setupspecs.d.ts +4 -0
  67. package/build/module/setupspecs.js +16 -0
  68. package/build/tsconfig.module.tsbuildinfo +1 -0
  69. package/build/tsconfig.tsbuildinfo +1 -0
  70. package/coverage/base.css +224 -0
  71. package/coverage/block-navigation.js +87 -0
  72. package/coverage/favicon.png +0 -0
  73. package/coverage/index.html +191 -0
  74. package/coverage/lcov-report/base.css +224 -0
  75. package/coverage/lcov-report/block-navigation.js +87 -0
  76. package/coverage/lcov-report/favicon.png +0 -0
  77. package/coverage/lcov-report/index.html +191 -0
  78. package/coverage/lcov-report/prettify.css +1 -0
  79. package/coverage/lcov-report/prettify.js +2 -0
  80. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  81. package/coverage/lcov-report/sorter.js +196 -0
  82. package/coverage/lcov-report/src/index.html +116 -0
  83. package/coverage/lcov-report/src/lib/auth.ts.html +340 -0
  84. package/coverage/lcov-report/src/lib/channel/client.ts.html +604 -0
  85. package/coverage/lcov-report/src/lib/channel/creator.ts.html +304 -0
  86. package/coverage/lcov-report/src/lib/channel/index.html +176 -0
  87. package/coverage/lcov-report/src/lib/channel/payload.ts.html +139 -0
  88. package/coverage/lcov-report/src/lib/channel/registry.ts.html +202 -0
  89. package/coverage/lcov-report/src/lib/channel/retriever.ts.html +244 -0
  90. package/coverage/lcov-report/src/lib/client.ts.html +244 -0
  91. package/coverage/lcov-report/src/lib/errors.ts.html +484 -0
  92. package/coverage/lcov-report/src/lib/index.html +176 -0
  93. package/coverage/lcov-report/src/lib/segment/client.ts.html +463 -0
  94. package/coverage/lcov-report/src/lib/segment/index.html +206 -0
  95. package/coverage/lcov-report/src/lib/segment/iterator.ts.html +928 -0
  96. package/coverage/lcov-report/src/lib/segment/payload.ts.html +139 -0
  97. package/coverage/lcov-report/src/lib/segment/splitter.ts.html +181 -0
  98. package/coverage/lcov-report/src/lib/segment/typed.ts.html +307 -0
  99. package/coverage/lcov-report/src/lib/segment/validator.ts.html +331 -0
  100. package/coverage/lcov-report/src/lib/segment/writer.ts.html +727 -0
  101. package/coverage/lcov-report/src/lib/telem.ts.html +2056 -0
  102. package/coverage/lcov-report/src/lib/transport.ts.html +196 -0
  103. package/coverage/lcov-report/src/lib/user/index.html +116 -0
  104. package/coverage/lcov-report/src/lib/user/payload.ts.html +109 -0
  105. package/coverage/lcov-report/src/lib/util/index.html +116 -0
  106. package/coverage/lcov-report/src/lib/util/telem.ts.html +124 -0
  107. package/coverage/lcov-report/src/setupspecs.ts.html +133 -0
  108. package/coverage/lcov.info +1230 -0
  109. package/coverage/prettify.css +1 -0
  110. package/coverage/prettify.js +2 -0
  111. package/coverage/sort-arrow-sprite.png +0 -0
  112. package/coverage/sorter.js +196 -0
  113. package/coverage/src/index.html +116 -0
  114. package/coverage/src/lib/auth.ts.html +340 -0
  115. package/coverage/src/lib/channel/client.ts.html +604 -0
  116. package/coverage/src/lib/channel/creator.ts.html +304 -0
  117. package/coverage/src/lib/channel/index.html +176 -0
  118. package/coverage/src/lib/channel/payload.ts.html +139 -0
  119. package/coverage/src/lib/channel/registry.ts.html +202 -0
  120. package/coverage/src/lib/channel/retriever.ts.html +244 -0
  121. package/coverage/src/lib/client.ts.html +244 -0
  122. package/coverage/src/lib/errors.ts.html +484 -0
  123. package/coverage/src/lib/index.html +176 -0
  124. package/coverage/src/lib/segment/client.ts.html +463 -0
  125. package/coverage/src/lib/segment/index.html +206 -0
  126. package/coverage/src/lib/segment/iterator.ts.html +928 -0
  127. package/coverage/src/lib/segment/payload.ts.html +139 -0
  128. package/coverage/src/lib/segment/splitter.ts.html +181 -0
  129. package/coverage/src/lib/segment/typed.ts.html +307 -0
  130. package/coverage/src/lib/segment/validator.ts.html +331 -0
  131. package/coverage/src/lib/segment/writer.ts.html +727 -0
  132. package/coverage/src/lib/telem.ts.html +2056 -0
  133. package/coverage/src/lib/transport.ts.html +196 -0
  134. package/coverage/src/lib/user/index.html +116 -0
  135. package/coverage/src/lib/user/payload.ts.html +109 -0
  136. package/coverage/src/lib/util/index.html +116 -0
  137. package/coverage/src/lib/util/telem.ts.html +124 -0
  138. package/coverage/src/setupspecs.ts.html +133 -0
  139. package/package.json +2 -2
  140. package/src/index.ts +13 -0
  141. package/src/lib/.DS_Store +0 -0
  142. package/src/lib/auth.spec.ts +36 -0
  143. package/src/lib/auth.ts +85 -0
  144. package/src/lib/channel/channel.spec.ts +49 -0
  145. package/src/lib/channel/client.ts +173 -0
  146. package/src/lib/channel/creator.ts +73 -0
  147. package/src/lib/channel/payload.ts +18 -0
  148. package/src/lib/channel/registry.ts +39 -0
  149. package/src/lib/channel/retriever.ts +53 -0
  150. package/src/lib/client.ts +53 -0
  151. package/src/lib/errors.ts +133 -0
  152. package/src/lib/segment/client.ts +126 -0
  153. package/src/lib/segment/iterator.spec.ts +78 -0
  154. package/src/lib/segment/iterator.ts +281 -0
  155. package/src/lib/segment/payload.ts +18 -0
  156. package/src/lib/segment/splitter.ts +32 -0
  157. package/src/lib/segment/typed.ts +74 -0
  158. package/src/lib/segment/validator.ts +82 -0
  159. package/src/lib/segment/writer.spec.ts +85 -0
  160. package/src/lib/segment/writer.ts +214 -0
  161. package/src/lib/telem.spec.ts +200 -0
  162. package/src/lib/telem.ts +657 -0
  163. package/src/lib/transport.ts +37 -0
  164. package/src/lib/user/payload.ts +8 -0
  165. package/src/lib/util/telem.ts +13 -0
  166. package/src/setupspecs.ts +16 -0
  167. package/tsconfig.json +47 -0
  168. package/tsconfig.module.json +9 -0
  169. package/yarn-error.log +5756 -0
  170. package/yarn.lock +5936 -0
@@ -0,0 +1,133 @@
1
+
2
+ <!doctype html>
3
+ <html lang="en">
4
+
5
+ <head>
6
+ <title>Code coverage report for src/setupspecs.ts</title>
7
+ <meta charset="utf-8" />
8
+ <link rel="stylesheet" href="../prettify.css" />
9
+ <link rel="stylesheet" href="../base.css" />
10
+ <link rel="shortcut icon" type="image/x-icon" href="../favicon.png" />
11
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
12
+ <style type='text/css'>
13
+ .coverage-summary .sorter {
14
+ background-image: url(../sort-arrow-sprite.png);
15
+ }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <div class='wrapper'>
21
+ <div class='pad1'>
22
+ <h1><a href="../index.html">All files</a> / <a href="index.html">src</a> setupspecs.ts</h1>
23
+ <div class='clearfix'>
24
+
25
+ <div class='fl pad1y space-right2'>
26
+ <span class="strong">88.88% </span>
27
+ <span class="quiet">Statements</span>
28
+ <span class='fraction'>8/9</span>
29
+ </div>
30
+
31
+
32
+ <div class='fl pad1y space-right2'>
33
+ <span class="strong">50% </span>
34
+ <span class="quiet">Branches</span>
35
+ <span class='fraction'>1/2</span>
36
+ </div>
37
+
38
+
39
+ <div class='fl pad1y space-right2'>
40
+ <span class="strong">100% </span>
41
+ <span class="quiet">Functions</span>
42
+ <span class='fraction'>1/1</span>
43
+ </div>
44
+
45
+
46
+ <div class='fl pad1y space-right2'>
47
+ <span class="strong">100% </span>
48
+ <span class="quiet">Lines</span>
49
+ <span class='fraction'>7/7</span>
50
+ </div>
51
+
52
+
53
+ </div>
54
+ <p class="quiet">
55
+ Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
56
+ </p>
57
+ <template id="filterTemplate">
58
+ <div class="quiet">
59
+ Filter:
60
+ <input oninput="onInput()" type="search" id="fileSearch">
61
+ </div>
62
+ </template>
63
+ </div>
64
+ <div class='status-line high'></div>
65
+ <pre><table class="coverage">
66
+ <tr><td class="line-count quiet"><a name='L1'></a><a href='#L1'>1</a>
67
+ <a name='L2'></a><a href='#L2'>2</a>
68
+ <a name='L3'></a><a href='#L3'>3</a>
69
+ <a name='L4'></a><a href='#L4'>4</a>
70
+ <a name='L5'></a><a href='#L5'>5</a>
71
+ <a name='L6'></a><a href='#L6'>6</a>
72
+ <a name='L7'></a><a href='#L7'>7</a>
73
+ <a name='L8'></a><a href='#L8'>8</a>
74
+ <a name='L9'></a><a href='#L9'>9</a>
75
+ <a name='L10'></a><a href='#L10'>10</a>
76
+ <a name='L11'></a><a href='#L11'>11</a>
77
+ <a name='L12'></a><a href='#L12'>12</a>
78
+ <a name='L13'></a><a href='#L13'>13</a>
79
+ <a name='L14'></a><a href='#L14'>14</a>
80
+ <a name='L15'></a><a href='#L15'>15</a>
81
+ <a name='L16'></a><a href='#L16'>16</a>
82
+ <a name='L17'></a><a href='#L17'>17</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">4x</span>
83
+ <span class="cline-any cline-neutral">&nbsp;</span>
84
+ <span class="cline-any cline-yes">4x</span>
85
+ <span class="cline-any cline-yes">4x</span>
86
+ <span class="cline-any cline-neutral">&nbsp;</span>
87
+ <span class="cline-any cline-yes">4x</span>
88
+ <span class="cline-any cline-yes">3x</span>
89
+ <span class="cline-any cline-yes">3x</span>
90
+ <span class="cline-any cline-yes">3x</span>
91
+ <span class="cline-any cline-neutral">&nbsp;</span>
92
+ <span class="cline-any cline-neutral">&nbsp;</span>
93
+ <span class="cline-any cline-neutral">&nbsp;</span>
94
+ <span class="cline-any cline-neutral">&nbsp;</span>
95
+ <span class="cline-any cline-neutral">&nbsp;</span>
96
+ <span class="cline-any cline-neutral">&nbsp;</span>
97
+ <span class="cline-any cline-neutral">&nbsp;</span>
98
+ <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">import Synnax, { SynnaxProps } from './lib/client';
99
+ &nbsp;
100
+ export const HOST = 'localhost';
101
+ export const PORT = 8080;
102
+ &nbsp;
103
+ export const newClient = (...props: SynnaxProps[]): Synnax =&gt; {
104
+ let _props = {};
105
+ <span class="missing-if-branch" title="if path not taken" >I</span>if (props.length &gt; 0) <span class="cstat-no" title="statement not covered" >_props = props[0];</span>
106
+ return new Synnax({
107
+ host: HOST,
108
+ port: PORT,
109
+ username: 'synnax',
110
+ password: 'seldon',
111
+ ..._props,
112
+ });
113
+ };
114
+ &nbsp;</pre></td></tr></table></pre>
115
+
116
+ <div class='push'></div><!-- for sticky footer -->
117
+ </div><!-- /wrapper -->
118
+ <div class='footer quiet pad2 space-top1 center small'>
119
+ Code coverage generated by
120
+ <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
121
+ at 2022-10-04T02:07:47.903Z
122
+ </div>
123
+ <script src="../prettify.js"></script>
124
+ <script>
125
+ window.onload = function () {
126
+ prettyPrint();
127
+ };
128
+ </script>
129
+ <script src="../sorter.js"></script>
130
+ <script src="../block-navigation.js"></script>
131
+ </body>
132
+ </html>
133
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synnaxlabs/client",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "synnax Client Library",
5
5
  "main": "build/main/index.js",
6
6
  "typings": "build/main/index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "node": ">=10"
51
51
  },
52
52
  "dependencies": {
53
- "@synnaxlabs/freighter": "^0.1.0",
53
+ "@synnaxlabs/freighter": "^0.2.0",
54
54
  "zod": "^3.19.1"
55
55
  },
56
56
  "devDependencies": {
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { default as Synnax } from './lib/client';
2
+ export * from './lib/telem';
3
+ export {
4
+ AuthError,
5
+ ContiguityError,
6
+ GeneralError,
7
+ ParseError,
8
+ QueryError,
9
+ RouteError,
10
+ UnexpectedError,
11
+ ValidationError,
12
+ } from './lib/errors';
13
+ export { Channel } from './lib/channel/client';
Binary file
@@ -0,0 +1,36 @@
1
+ import { URL } from '@synnaxlabs/freighter';
2
+ import test from 'ava';
3
+
4
+ import { HOST, PORT } from '../setupspecs';
5
+
6
+ import AuthenticationClient from './auth';
7
+ import { AuthError } from './errors';
8
+ import Transport from './transport';
9
+
10
+ test('[auth] - valid credentials', async (t) => {
11
+ const transport = new Transport(new URL({ host: HOST, port: PORT }));
12
+ const client = new AuthenticationClient(transport.httpFactory, {
13
+ username: 'synnax',
14
+ password: 'seldon',
15
+ });
16
+ await client.authenticating;
17
+ t.assert(client.authenticated);
18
+ });
19
+
20
+ test('[auth] - invalid credentials', async (t) => {
21
+ const transport = new Transport(new URL({ host: HOST, port: PORT }));
22
+ const client = new AuthenticationClient(transport.httpFactory, {
23
+ username: 'synnax',
24
+ password: 'wrong',
25
+ });
26
+ try {
27
+ await client.authenticating;
28
+ t.assert(false);
29
+ } catch (e) {
30
+ t.assert(!client.authenticated);
31
+ t.assert(e instanceof AuthError);
32
+ if (e instanceof AuthError) {
33
+ t.is(e.message, '[synnax] - invalid credentials');
34
+ }
35
+ }
36
+ });
@@ -0,0 +1,85 @@
1
+ import {
2
+ HTTPClientFactory,
3
+ Middleware,
4
+ UnaryClient,
5
+ } from '@synnaxlabs/freighter';
6
+ import { z } from 'zod';
7
+
8
+ import { AuthError } from './errors';
9
+ import { UserPayload, UserPayloadSchema } from './user/payload';
10
+
11
+ export const tokenMiddleware = (token: () => Promise<string>): Middleware => {
12
+ return async (md, next) => {
13
+ md.params['Authorization'] = `Bearer ${await token()}`;
14
+ return await next(md);
15
+ };
16
+ };
17
+
18
+ export const InsecureCredentialsSchema = z.object({
19
+ username: z.string(),
20
+ password: z.string(),
21
+ });
22
+
23
+ export type InsecureCredentials = z.infer<typeof InsecureCredentialsSchema>;
24
+
25
+ export const TokenResponseSchema = z.object({
26
+ token: z.string(),
27
+ user: UserPayloadSchema,
28
+ });
29
+
30
+ export type TokenResponse = z.infer<typeof TokenResponseSchema>;
31
+
32
+ export default class AuthenticationClient {
33
+ private static ENDPOINT = '/auth/login';
34
+ private token: string | undefined;
35
+ private client: UnaryClient;
36
+ private credentials: InsecureCredentials;
37
+ authenticating: Promise<void> | undefined;
38
+ authenticated: boolean;
39
+ user: UserPayload | undefined;
40
+
41
+ constructor(factory: HTTPClientFactory, creds: InsecureCredentials) {
42
+ this.client = factory.postClient();
43
+ this.credentials = creds;
44
+ this.authenticated = false;
45
+ this.authenticate();
46
+ }
47
+
48
+ authenticate() {
49
+ this.authenticating = new Promise((resolve, reject) => {
50
+ this.client
51
+ .send<InsecureCredentials, TokenResponse>(
52
+ AuthenticationClient.ENDPOINT,
53
+ this.credentials,
54
+ TokenResponseSchema
55
+ )
56
+ .then(([res, err]) => {
57
+ if (err) {
58
+ reject(err);
59
+ return;
60
+ }
61
+ this.token = res?.token;
62
+ this.user = res?.user;
63
+ this.authenticated = true;
64
+ resolve();
65
+ });
66
+ });
67
+ }
68
+
69
+ private async maybeWaitAuthenticated() {
70
+ if (this.authenticating) await this.authenticating;
71
+ this.authenticating = undefined;
72
+ }
73
+
74
+ middleware(): Middleware {
75
+ return tokenMiddleware(async () => {
76
+ await this.maybeWaitAuthenticated();
77
+ if (!this.token) {
78
+ throw new AuthError(
79
+ '[auth] - attempting to authenticate without a token'
80
+ );
81
+ }
82
+ return this.token;
83
+ });
84
+ }
85
+ }
@@ -0,0 +1,49 @@
1
+ import test from 'ava';
2
+
3
+ import { newClient } from '../../setupspecs';
4
+ import { QueryError } from '../errors';
5
+ import { DataType, Rate } from '../telem';
6
+
7
+ const client = newClient();
8
+
9
+ test('Channel - create', async (t) => {
10
+ const channel = await client.channel.create({
11
+ name: 'test',
12
+ nodeId: 1,
13
+ rate: Rate.Hz(1),
14
+ dataType: DataType.Float32,
15
+ });
16
+ t.is(channel.name, 'test');
17
+ t.is(channel.nodeId, 1);
18
+ t.deepEqual(channel.rate, Rate.Hz(1));
19
+ t.deepEqual(channel.dataType, DataType.Float32);
20
+ });
21
+
22
+ test('Channel - retrieve by key', async (t) => {
23
+ const channel = await client.channel.create({
24
+ name: 'test',
25
+ nodeId: 1,
26
+ rate: Rate.Hz(1),
27
+ dataType: DataType.Float32,
28
+ });
29
+ const retrieved = (await client.channel.retrieveByKeys(channel.key))[0];
30
+ t.is(retrieved.name, 'test');
31
+ t.is(retrieved.nodeId, 1);
32
+ t.deepEqual(retrieved.rate, Rate.Hz(1));
33
+ t.deepEqual(retrieved.dataType, DataType.Float32);
34
+ });
35
+
36
+ test('Channel - retrieve by key - not found', async (t) => {
37
+ const err = await t.throwsAsync(async () => {
38
+ await client.channel.retrieveByKeys('1-1000');
39
+ });
40
+ t.true(err instanceof QueryError);
41
+ });
42
+
43
+ test('Channel - retrieve by node id', async (t) => {
44
+ const retrieved = await client.channel.retrieveByNodeId(1);
45
+ t.true(retrieved.length > 0);
46
+ retrieved.forEach((ch) => {
47
+ t.is(ch.nodeId, 1);
48
+ });
49
+ });
@@ -0,0 +1,173 @@
1
+ import SegmentClient from '../segment/client';
2
+ import {
3
+ DataType,
4
+ Density,
5
+ Rate,
6
+ TypedArray,
7
+ UnparsedTimeStamp,
8
+ } from '../telem';
9
+
10
+ import ChannelCreator from './creator';
11
+ import { CreateChannelProps } from './creator';
12
+ import { ChannelPayload } from './payload';
13
+ import ChannelRetriever from './retriever';
14
+
15
+ /**
16
+ * Represents a Channel in a Synnax database. It should not be instantiated
17
+ * directly, but rather created or retrieved from a {@link ChannelClient}.
18
+ */
19
+ export class Channel {
20
+ private readonly segmentClient: SegmentClient;
21
+ private payload: ChannelPayload;
22
+
23
+ constructor(payload: ChannelPayload, segmentClient: SegmentClient) {
24
+ this.payload = payload;
25
+ this.segmentClient = segmentClient;
26
+ }
27
+
28
+ get key(): string {
29
+ if (!this.payload.key) {
30
+ throw new Error('Channel key is not set');
31
+ }
32
+ return this.payload.key;
33
+ }
34
+
35
+ get name(): string {
36
+ if (!this.payload.name) {
37
+ throw new Error('Channel name is not set');
38
+ }
39
+ return this.payload.name;
40
+ }
41
+
42
+ get nodeId(): number {
43
+ if (this.payload.nodeId === undefined) {
44
+ throw new Error('Channel nodeId is not set');
45
+ }
46
+ return this.payload.nodeId;
47
+ }
48
+
49
+ get rate(): Rate {
50
+ return this.payload.rate;
51
+ }
52
+
53
+ get dataType(): DataType {
54
+ return this.payload.dataType;
55
+ }
56
+
57
+ get density(): Density {
58
+ if (!this.payload.density) {
59
+ throw new Error('Channel density is not set');
60
+ }
61
+ return this.payload.density;
62
+ }
63
+
64
+ /**
65
+ * Reads telemetry from the channel between the two timestamps.
66
+ *
67
+ * @param start - The starting timestamp of the range to read from.
68
+ * @param end - The ending timestamp of the range to read from.
69
+ * @returns a typed array containing the retrieved
70
+ */
71
+ async read(
72
+ start: UnparsedTimeStamp,
73
+ end: UnparsedTimeStamp
74
+ ): Promise<TypedArray> {
75
+ return await this.segmentClient.read(this.key, start, end);
76
+ }
77
+
78
+ /**
79
+ * Writes telemetry to the channel starting at the given timestamp.
80
+ *
81
+ * @param start - The starting timestamp of the first sample in data.
82
+ * @param data - THe telemetry to write to the channel.
83
+ */
84
+ async write(start: UnparsedTimeStamp, data: TypedArray) {
85
+ return await this.segmentClient.write(this.key, start, data);
86
+ }
87
+ }
88
+
89
+ /** The core client class for executing channel operations against a Synnax
90
+ * database .
91
+ * */
92
+ export default class ChannelClient {
93
+ private readonly segmentClient: SegmentClient;
94
+ private readonly retriever: ChannelRetriever;
95
+ private readonly creator: ChannelCreator;
96
+
97
+ constructor(
98
+ segmentClient: SegmentClient,
99
+ retriever: ChannelRetriever,
100
+ creator: ChannelCreator
101
+ ) {
102
+ this.segmentClient = segmentClient;
103
+ this.retriever = retriever;
104
+ this.creator = creator;
105
+ }
106
+
107
+ /**
108
+ * Creates a new channel with the given properties.
109
+ *
110
+ * @param props.rate - The rate of the channel.
111
+ * @param props.dataType - The data type of the channel.
112
+ * @param props.name - The name of the channel. Optional.
113
+ * @param props.nodeId - The ID of the node that holds the lease on the channel.
114
+ * If you don't know what this is, don't worry about it.
115
+ * @returns the created channel.
116
+ */
117
+ async create(props: CreateChannelProps): Promise<Channel> {
118
+ return (await this.createMany({ ...props, count: 1 }))[0];
119
+ }
120
+
121
+ /**
122
+ * creates N channels using the given parameters as a template.
123
+ *
124
+ * @param props.rate - The rate of the channel.
125
+ * @param props.dataType - The data type of the channel.
126
+ * @param props.name - The name of the channel. Optional.
127
+ * @param props.nodeId - The ID of the node that holds the lease on the channel.
128
+ * If you don't know what this is, don't worry about it.
129
+ * @param props.count - The number of channels to create.
130
+ * @returns the created channels.
131
+ */
132
+ async createMany(
133
+ props: CreateChannelProps & { count: number }
134
+ ): Promise<Channel[]> {
135
+ return this.sugar(...(await this.creator.createMany(props)));
136
+ }
137
+
138
+ /**
139
+ * Retrieves channels with the given keys.
140
+ *
141
+ * @param keys - The keys of the channels to retrieve.
142
+ * @throws QueryError if any of the channels can't be found.
143
+ * @returns the retrieved channels.
144
+ */
145
+ async retrieveByKeys(...keys: string[]): Promise<Channel[]> {
146
+ return this.sugar(...(await this.retriever.retrieveByKeys(...keys)));
147
+ }
148
+
149
+ /**
150
+ * Retrieves channels with the given names.
151
+ *
152
+ * @param names - The list of names to retrieve channels for.
153
+ * @returns A list of retrieved channels matching the given names. If a channel
154
+ * with a given name can't be found, it will be omitted from the list.
155
+ */
156
+ async retrieveByNames(...names: string[]): Promise<Channel[]> {
157
+ return this.sugar(...(await this.retriever.retrieveByNames(...names)));
158
+ }
159
+
160
+ /**
161
+ * Retrieves channels whose lease node is the given ID.
162
+ *
163
+ * @param nodeId - The ID of the node to retrieve channels for.
164
+ * @returns A list of retrieved channels matching the given node ID.
165
+ */
166
+ async retrieveByNodeId(nodeId: number): Promise<Channel[]> {
167
+ return this.sugar(...(await this.retriever.retrieveByNodeID(nodeId)));
168
+ }
169
+
170
+ private sugar(...payloads: ChannelPayload[]): Channel[] {
171
+ return payloads.map((p) => new Channel(p, this.segmentClient));
172
+ }
173
+ }
@@ -0,0 +1,73 @@
1
+ import { UnaryClient } from '@synnaxlabs/freighter';
2
+ import { z } from 'zod';
3
+
4
+ import { DataType, Rate, UnparsedDataType, UnparsedRate } from '../telem';
5
+ import Transport from '../transport';
6
+
7
+ import { ChannelPayload, ChannelPayloadSchema } from './payload';
8
+
9
+ const RequestSchema = z.object({
10
+ channel: ChannelPayloadSchema,
11
+ count: z.number(),
12
+ });
13
+
14
+ type Request = z.infer<typeof RequestSchema>;
15
+
16
+ const ResponseSchema = z.object({
17
+ channels: ChannelPayloadSchema.array(),
18
+ });
19
+
20
+ type Response = z.infer<typeof ResponseSchema>;
21
+
22
+ export type CreateChannelProps = {
23
+ rate: UnparsedRate;
24
+ dataType: UnparsedDataType;
25
+ name?: string;
26
+ nodeId?: number;
27
+ };
28
+
29
+ export default class Creator {
30
+ private static ENDPOINT = '/channel/create';
31
+ private client: UnaryClient;
32
+
33
+ constructor(transport: Transport) {
34
+ this.client = transport.postClient();
35
+ }
36
+
37
+ async create(props: CreateChannelProps): Promise<ChannelPayload> {
38
+ const [channel] = await this.createMany({ ...props, count: 1 });
39
+ return channel;
40
+ }
41
+
42
+ async createMany({
43
+ rate,
44
+ dataType,
45
+ name = '',
46
+ nodeId = 0,
47
+ count = 1,
48
+ }: CreateChannelProps & { count: number }): Promise<ChannelPayload[]> {
49
+ return (
50
+ await this.execute({
51
+ channel: {
52
+ name,
53
+ nodeId,
54
+ rate: new Rate(rate),
55
+ dataType: new DataType(dataType),
56
+ },
57
+ count,
58
+ })
59
+ ).channels;
60
+ }
61
+
62
+ private async execute(request: Request): Promise<Response> {
63
+ const [res, err] = await this.client.send(
64
+ Creator.ENDPOINT,
65
+ request,
66
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
+ // @ts-ignore
68
+ ResponseSchema
69
+ );
70
+ if (err) throw err;
71
+ return res as Response;
72
+ }
73
+ }
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+
3
+ import { DataType, Density, Rate } from '../telem';
4
+
5
+ export const ChannelPayloadSchema = z.object({
6
+ rate: z.number().transform((n) => new Rate(n)),
7
+ dataType: z.string().transform((s) => new DataType(s)),
8
+ key: z.string().default('').optional(),
9
+ name: z.string().default('').optional(),
10
+ nodeId: z.number().default(0).optional(),
11
+ density: z
12
+ .number()
13
+ .default(0)
14
+ .transform((n) => new Density(n))
15
+ .optional(),
16
+ });
17
+
18
+ export type ChannelPayload = z.infer<typeof ChannelPayloadSchema>;
@@ -0,0 +1,39 @@
1
+ import { ChannelPayload } from './payload';
2
+ import Retriever from './retriever';
3
+
4
+ export default class Registry {
5
+ private retriever: Retriever;
6
+ private channels: Map<string, ChannelPayload>;
7
+
8
+ constructor(retriever: Retriever) {
9
+ this.retriever = retriever;
10
+ this.channels = new Map();
11
+ }
12
+
13
+ async get(key: string): Promise<ChannelPayload> {
14
+ let channel = this.channels.get(key);
15
+ if (channel === undefined) {
16
+ channel = (await this.retriever.retrieveByKeys(key))[0];
17
+ this.channels.set(key, channel);
18
+ }
19
+ return channel;
20
+ }
21
+
22
+ async getN(...keys: string[]): Promise<ChannelPayload[]> {
23
+ const results: ChannelPayload[] = [];
24
+ const retrieveKeys: string[] = [];
25
+ keys.forEach((key) => {
26
+ const channel = this.channels.get(key);
27
+ if (channel === undefined) retrieveKeys.push(key);
28
+ else results.push(channel);
29
+ });
30
+ if (retrieveKeys.length > 0) {
31
+ const channels = await this.retriever.retrieveByKeys(...retrieveKeys);
32
+ channels.forEach((channel) => {
33
+ this.channels.set(channel.key as string, channel);
34
+ results.push(channel);
35
+ });
36
+ }
37
+ return results;
38
+ }
39
+ }