@synnaxlabs/client 0.2.0 → 0.2.1

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 (120) hide show
  1. package/.DS_Store +0 -0
  2. package/.editorconfig +0 -15
  3. package/.eslintrc.json +0 -33
  4. package/.gitignore +0 -9
  5. package/.nyc_output/20720f2d-6abe-420f-a3c5-304d52d60827.json +0 -1
  6. package/.nyc_output/4725921c-6f1b-4ae9-9819-e455f702d31c.json +0 -1
  7. package/.nyc_output/47478588-5ffd-4332-873c-facaa4a2fc38.json +0 -1
  8. package/.nyc_output/48180641-e0b2-49ab-a6eb-e7910e9eac2f.json +0 -1
  9. package/.nyc_output/cb0abf31-740f-47db-b94a-8e3f8f117cb8.json +0 -1
  10. package/.nyc_output/fc77fce2-dad0-49a8-8d4b-0a9014ecf8c5.json +0 -1
  11. package/.nyc_output/processinfo/20720f2d-6abe-420f-a3c5-304d52d60827.json +0 -1
  12. package/.nyc_output/processinfo/4725921c-6f1b-4ae9-9819-e455f702d31c.json +0 -1
  13. package/.nyc_output/processinfo/47478588-5ffd-4332-873c-facaa4a2fc38.json +0 -1
  14. package/.nyc_output/processinfo/48180641-e0b2-49ab-a6eb-e7910e9eac2f.json +0 -1
  15. package/.nyc_output/processinfo/cb0abf31-740f-47db-b94a-8e3f8f117cb8.json +0 -1
  16. package/.nyc_output/processinfo/fc77fce2-dad0-49a8-8d4b-0a9014ecf8c5.json +0 -1
  17. package/.nyc_output/processinfo/index.json +0 -1
  18. package/.prettierignore +0 -2
  19. package/build/tsconfig.module.tsbuildinfo +0 -1
  20. package/build/tsconfig.tsbuildinfo +0 -1
  21. package/coverage/base.css +0 -224
  22. package/coverage/block-navigation.js +0 -87
  23. package/coverage/favicon.png +0 -0
  24. package/coverage/index.html +0 -191
  25. package/coverage/lcov-report/base.css +0 -224
  26. package/coverage/lcov-report/block-navigation.js +0 -87
  27. package/coverage/lcov-report/favicon.png +0 -0
  28. package/coverage/lcov-report/index.html +0 -191
  29. package/coverage/lcov-report/prettify.css +0 -1
  30. package/coverage/lcov-report/prettify.js +0 -2
  31. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  32. package/coverage/lcov-report/sorter.js +0 -196
  33. package/coverage/lcov-report/src/index.html +0 -116
  34. package/coverage/lcov-report/src/lib/auth.ts.html +0 -340
  35. package/coverage/lcov-report/src/lib/channel/client.ts.html +0 -604
  36. package/coverage/lcov-report/src/lib/channel/creator.ts.html +0 -304
  37. package/coverage/lcov-report/src/lib/channel/index.html +0 -176
  38. package/coverage/lcov-report/src/lib/channel/payload.ts.html +0 -139
  39. package/coverage/lcov-report/src/lib/channel/registry.ts.html +0 -202
  40. package/coverage/lcov-report/src/lib/channel/retriever.ts.html +0 -244
  41. package/coverage/lcov-report/src/lib/client.ts.html +0 -244
  42. package/coverage/lcov-report/src/lib/errors.ts.html +0 -484
  43. package/coverage/lcov-report/src/lib/index.html +0 -176
  44. package/coverage/lcov-report/src/lib/segment/client.ts.html +0 -463
  45. package/coverage/lcov-report/src/lib/segment/index.html +0 -206
  46. package/coverage/lcov-report/src/lib/segment/iterator.ts.html +0 -928
  47. package/coverage/lcov-report/src/lib/segment/payload.ts.html +0 -139
  48. package/coverage/lcov-report/src/lib/segment/splitter.ts.html +0 -181
  49. package/coverage/lcov-report/src/lib/segment/typed.ts.html +0 -307
  50. package/coverage/lcov-report/src/lib/segment/validator.ts.html +0 -331
  51. package/coverage/lcov-report/src/lib/segment/writer.ts.html +0 -727
  52. package/coverage/lcov-report/src/lib/telem.ts.html +0 -2056
  53. package/coverage/lcov-report/src/lib/transport.ts.html +0 -196
  54. package/coverage/lcov-report/src/lib/user/index.html +0 -116
  55. package/coverage/lcov-report/src/lib/user/payload.ts.html +0 -109
  56. package/coverage/lcov-report/src/lib/util/index.html +0 -116
  57. package/coverage/lcov-report/src/lib/util/telem.ts.html +0 -124
  58. package/coverage/lcov-report/src/setupspecs.ts.html +0 -133
  59. package/coverage/lcov.info +0 -1230
  60. package/coverage/prettify.css +0 -1
  61. package/coverage/prettify.js +0 -2
  62. package/coverage/sort-arrow-sprite.png +0 -0
  63. package/coverage/sorter.js +0 -196
  64. package/coverage/src/index.html +0 -116
  65. package/coverage/src/lib/auth.ts.html +0 -340
  66. package/coverage/src/lib/channel/client.ts.html +0 -604
  67. package/coverage/src/lib/channel/creator.ts.html +0 -304
  68. package/coverage/src/lib/channel/index.html +0 -176
  69. package/coverage/src/lib/channel/payload.ts.html +0 -139
  70. package/coverage/src/lib/channel/registry.ts.html +0 -202
  71. package/coverage/src/lib/channel/retriever.ts.html +0 -244
  72. package/coverage/src/lib/client.ts.html +0 -244
  73. package/coverage/src/lib/errors.ts.html +0 -484
  74. package/coverage/src/lib/index.html +0 -176
  75. package/coverage/src/lib/segment/client.ts.html +0 -463
  76. package/coverage/src/lib/segment/index.html +0 -206
  77. package/coverage/src/lib/segment/iterator.ts.html +0 -928
  78. package/coverage/src/lib/segment/payload.ts.html +0 -139
  79. package/coverage/src/lib/segment/splitter.ts.html +0 -181
  80. package/coverage/src/lib/segment/typed.ts.html +0 -307
  81. package/coverage/src/lib/segment/validator.ts.html +0 -331
  82. package/coverage/src/lib/segment/writer.ts.html +0 -727
  83. package/coverage/src/lib/telem.ts.html +0 -2056
  84. package/coverage/src/lib/transport.ts.html +0 -196
  85. package/coverage/src/lib/user/index.html +0 -116
  86. package/coverage/src/lib/user/payload.ts.html +0 -109
  87. package/coverage/src/lib/util/index.html +0 -116
  88. package/coverage/src/lib/util/telem.ts.html +0 -124
  89. package/coverage/src/setupspecs.ts.html +0 -133
  90. package/src/index.ts +0 -13
  91. package/src/lib/.DS_Store +0 -0
  92. package/src/lib/auth.spec.ts +0 -36
  93. package/src/lib/auth.ts +0 -85
  94. package/src/lib/channel/channel.spec.ts +0 -49
  95. package/src/lib/channel/client.ts +0 -173
  96. package/src/lib/channel/creator.ts +0 -73
  97. package/src/lib/channel/payload.ts +0 -18
  98. package/src/lib/channel/registry.ts +0 -39
  99. package/src/lib/channel/retriever.ts +0 -53
  100. package/src/lib/client.ts +0 -53
  101. package/src/lib/errors.ts +0 -133
  102. package/src/lib/segment/client.ts +0 -126
  103. package/src/lib/segment/iterator.spec.ts +0 -78
  104. package/src/lib/segment/iterator.ts +0 -281
  105. package/src/lib/segment/payload.ts +0 -18
  106. package/src/lib/segment/splitter.ts +0 -32
  107. package/src/lib/segment/typed.ts +0 -74
  108. package/src/lib/segment/validator.ts +0 -82
  109. package/src/lib/segment/writer.spec.ts +0 -85
  110. package/src/lib/segment/writer.ts +0 -214
  111. package/src/lib/telem.spec.ts +0 -200
  112. package/src/lib/telem.ts +0 -657
  113. package/src/lib/transport.ts +0 -37
  114. package/src/lib/user/payload.ts +0 -8
  115. package/src/lib/util/telem.ts +0 -13
  116. package/src/setupspecs.ts +0 -16
  117. package/tsconfig.json +0 -47
  118. package/tsconfig.module.json +0 -9
  119. package/yarn-error.log +0 -5756
  120. package/yarn.lock +0 -5936
@@ -1,133 +0,0 @@
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/src/index.ts DELETED
@@ -1,13 +0,0 @@
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';
package/src/lib/.DS_Store DELETED
Binary file
@@ -1,36 +0,0 @@
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
- });
package/src/lib/auth.ts DELETED
@@ -1,85 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
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
- });
@@ -1,173 +0,0 @@
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
- }
@@ -1,73 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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>;
@@ -1,39 +0,0 @@
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
- }
@@ -1,53 +0,0 @@
1
- import { UnaryClient } from '@synnaxlabs/freighter';
2
- import { z } from 'zod';
3
-
4
- import Transport from '../transport';
5
-
6
- import { ChannelPayload, ChannelPayloadSchema } from './payload';
7
-
8
- const RequestSchema = z.object({
9
- keys: z.string().array().optional(),
10
- nodeId: z.number().optional(),
11
- names: z.string().array().optional(),
12
- });
13
-
14
- type Request = z.infer<typeof RequestSchema>;
15
-
16
- const ResponseSchema = z.object({
17
- channels: ChannelPayloadSchema.array(),
18
- });
19
-
20
- export default class Retriever {
21
- private static ENDPOINT = '/channel/retrieve';
22
- private client: UnaryClient;
23
-
24
- constructor(transport: Transport) {
25
- this.client = transport.getClient();
26
- }
27
-
28
- private async execute(request: Request): Promise<ChannelPayload[]> {
29
- const [res, err] = await this.client.send(
30
- Retriever.ENDPOINT,
31
- request,
32
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
- // @ts-ignore
34
- ResponseSchema
35
- );
36
- if (err) {
37
- throw err;
38
- }
39
- return res?.channels as ChannelPayload[];
40
- }
41
-
42
- async retrieveByKeys(...keys: string[]): Promise<ChannelPayload[]> {
43
- return await this.execute({ keys });
44
- }
45
-
46
- async retrieveByNames(...names: string[]): Promise<ChannelPayload[]> {
47
- return await this.execute({ names });
48
- }
49
-
50
- async retrieveByNodeID(nodeId: number): Promise<ChannelPayload[]> {
51
- return await this.execute({ nodeId: nodeId });
52
- }
53
- }