@omujs/omu 0.9.48 → 0.9.49

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 (202) hide show
  1. package/dist/dts/api/asset/extension.d.ts +25 -0
  2. package/dist/dts/api/asset/index.d.ts +2 -0
  3. package/dist/dts/api/dashboard/extension.d.ts +183 -0
  4. package/{src/api/dashboard/handler.ts → dist/dts/api/dashboard/handler.d.ts} +0 -2
  5. package/dist/dts/api/dashboard/index.d.ts +3 -0
  6. package/dist/dts/api/dashboard/packets.d.ts +73 -0
  7. package/dist/dts/api/endpoint/endpoint.d.ts +21 -0
  8. package/dist/dts/api/endpoint/extension.d.ts +17 -0
  9. package/{src/api/endpoint/index.ts → dist/dts/api/endpoint/index.d.ts} +0 -1
  10. package/dist/dts/api/endpoint/packets.d.ts +51 -0
  11. package/dist/dts/api/extension-registry.d.ts +8 -0
  12. package/dist/dts/api/extension.d.ts +11 -0
  13. package/dist/dts/api/http/extension.d.ts +41 -0
  14. package/dist/dts/api/http/http.d.ts +36 -0
  15. package/{src/api/http/index.ts → dist/dts/api/http/index.d.ts} +0 -1
  16. package/dist/dts/api/http/websocket.d.ts +125 -0
  17. package/dist/dts/api/i18n/extension.d.ts +19 -0
  18. package/{src/api/i18n/index.ts → dist/dts/api/i18n/index.d.ts} +0 -1
  19. package/{src/api/index.ts → dist/dts/api/index.d.ts} +0 -1
  20. package/dist/dts/api/logger/extension.d.ts +25 -0
  21. package/dist/dts/api/logger/index.d.ts +1 -0
  22. package/dist/dts/api/logger/packets.d.ts +18 -0
  23. package/dist/dts/api/permission/extension.d.ts +19 -0
  24. package/{src/api/permission/index.ts → dist/dts/api/permission/index.d.ts} +0 -1
  25. package/dist/dts/api/permission/permission.d.ts +25 -0
  26. package/dist/dts/api/plugin/extension.d.ts +43 -0
  27. package/{src/api/plugin/index.ts → dist/dts/api/plugin/index.d.ts} +0 -1
  28. package/{src/api/plugin/package-info.ts → dist/dts/api/plugin/package-info.d.ts} +0 -7
  29. package/dist/dts/api/registry/extension.d.ts +23 -0
  30. package/{src/api/registry/index.ts → dist/dts/api/registry/index.d.ts} +0 -1
  31. package/dist/dts/api/registry/packets.d.ts +20 -0
  32. package/{src/api/registry/registry.ts → dist/dts/api/registry/registry.d.ts} +11 -53
  33. package/dist/dts/api/server/extension.d.ts +61 -0
  34. package/dist/dts/api/server/index.d.ts +1 -0
  35. package/dist/dts/api/session/extension.d.ts +52 -0
  36. package/dist/dts/api/session/index.d.ts +1 -0
  37. package/dist/dts/api/signal/extension.d.ts +19 -0
  38. package/dist/dts/api/signal/packets.d.ts +21 -0
  39. package/dist/dts/api/signal/signal.d.ts +33 -0
  40. package/dist/dts/api/table/extension.d.ts +26 -0
  41. package/{src/api/table/index.ts → dist/dts/api/table/index.d.ts} +0 -1
  42. package/dist/dts/api/table/packets.d.ts +65 -0
  43. package/dist/dts/api/table/table.d.ts +75 -0
  44. package/dist/dts/app.d.ts +49 -0
  45. package/dist/dts/const.d.ts +2 -0
  46. package/dist/dts/errors.d.ts +36 -0
  47. package/dist/dts/event/index.d.ts +6 -0
  48. package/dist/dts/helper.d.ts +10 -0
  49. package/dist/dts/identifier.d.ts +51 -0
  50. package/dist/dts/index.d.ts +36 -0
  51. package/{src/localization/index.ts → dist/dts/localization/index.d.ts} +0 -1
  52. package/dist/dts/localization/locale.d.ts +735 -0
  53. package/{src/localization/localization.ts → dist/dts/localization/localization.d.ts} +0 -2
  54. package/dist/dts/lock.d.ts +6 -0
  55. package/dist/dts/network/connection.d.ts +17 -0
  56. package/dist/dts/network/encryption.d.ts +38 -0
  57. package/dist/dts/network/frame-transport.d.ts +22 -0
  58. package/{src/network/index.ts → dist/dts/network/index.d.ts} +0 -1
  59. package/dist/dts/network/network.d.ts +59 -0
  60. package/{src/network/packet/index.ts → dist/dts/network/packet/index.d.ts} +0 -1
  61. package/dist/dts/network/packet/packet-types.d.ts +84 -0
  62. package/dist/dts/network/packet/packet.d.ts +25 -0
  63. package/dist/dts/network/websocket-transport.d.ts +24 -0
  64. package/dist/dts/omu.d.ts +61 -0
  65. package/dist/dts/result.d.ts +26 -0
  66. package/dist/dts/serialize/bytebuffer.d.ts +73 -0
  67. package/dist/dts/serialize/serializer.d.ts +26 -0
  68. package/dist/dts/token.d.ts +14 -0
  69. package/dist/dts/version.d.ts +2 -0
  70. package/dist/esm/address.js +4 -0
  71. package/dist/esm/api/asset/extension.js +911 -0
  72. package/dist/esm/api/asset/index.js +909 -0
  73. package/dist/esm/api/dashboard/extension.js +1362 -0
  74. package/dist/esm/api/dashboard/handler.js +4 -0
  75. package/dist/esm/api/dashboard/index.js +1361 -0
  76. package/dist/esm/api/dashboard/packets.js +419 -0
  77. package/dist/esm/api/endpoint/endpoint.js +383 -0
  78. package/dist/esm/api/endpoint/extension.js +1096 -0
  79. package/dist/esm/api/endpoint/index.js +383 -0
  80. package/dist/esm/api/endpoint/packets.js +799 -0
  81. package/dist/esm/api/extension-registry.js +23 -0
  82. package/dist/esm/api/extension.js +278 -0
  83. package/dist/esm/api/http/extension.js +1420 -0
  84. package/dist/esm/api/http/http.js +448 -0
  85. package/dist/esm/api/http/index.js +1419 -0
  86. package/dist/esm/api/http/websocket.js +220 -0
  87. package/dist/esm/api/i18n/extension.js +1339 -0
  88. package/dist/esm/api/i18n/index.js +1337 -0
  89. package/dist/esm/api/index.js +295 -0
  90. package/dist/esm/api/logger/extension.js +856 -0
  91. package/dist/esm/api/logger/index.js +852 -0
  92. package/dist/esm/api/logger/packets.js +673 -0
  93. package/dist/esm/api/permission/extension.js +535 -0
  94. package/dist/esm/api/permission/index.js +294 -0
  95. package/dist/esm/api/permission/permission.js +294 -0
  96. package/dist/esm/api/plugin/extension.js +531 -0
  97. package/dist/esm/api/plugin/index.js +529 -0
  98. package/dist/esm/api/plugin/package-info.js +4 -0
  99. package/dist/esm/api/registry/extension.js +1286 -0
  100. package/dist/esm/api/registry/index.js +1285 -0
  101. package/dist/esm/api/registry/packets.js +694 -0
  102. package/dist/esm/api/registry/registry.js +383 -0
  103. package/dist/esm/api/server/extension.js +2140 -0
  104. package/dist/esm/api/server/index.js +2118 -0
  105. package/dist/esm/api/session/extension.js +2235 -0
  106. package/dist/esm/api/session/index.js +2235 -0
  107. package/dist/esm/api/signal/extension.js +1031 -0
  108. package/dist/esm/api/signal/index.js +379 -0
  109. package/dist/esm/api/signal/packets.js +690 -0
  110. package/dist/esm/api/signal/signal.js +379 -0
  111. package/dist/esm/api/table/extension.js +1553 -0
  112. package/dist/esm/api/table/index.js +1552 -0
  113. package/dist/esm/api/table/packets.js +904 -0
  114. package/dist/esm/api/table/table.js +383 -0
  115. package/dist/esm/app.js +264 -0
  116. package/dist/esm/const.js +11 -0
  117. package/dist/esm/errors.js +548 -0
  118. package/dist/esm/event/index.js +22 -0
  119. package/dist/esm/helper.js +15 -0
  120. package/dist/esm/identifier.js +266 -0
  121. package/dist/esm/index.js +5186 -0
  122. package/dist/esm/localization/index.js +742 -0
  123. package/dist/esm/localization/locale.js +742 -0
  124. package/dist/esm/localization/localization.js +4 -0
  125. package/dist/esm/lock.js +46 -0
  126. package/dist/esm/network/connection.js +293 -0
  127. package/dist/esm/network/encryption.js +986 -0
  128. package/dist/esm/network/frame-transport.js +531 -0
  129. package/dist/esm/network/index.js +1627 -0
  130. package/dist/esm/network/network.js +1368 -0
  131. package/dist/esm/network/packet/index.js +485 -0
  132. package/dist/esm/network/packet/packet-types.js +486 -0
  133. package/dist/esm/network/packet/packet.js +126 -0
  134. package/dist/esm/network/websocket-transport.js +493 -0
  135. package/dist/esm/omu.js +4945 -0
  136. package/dist/esm/result.js +68 -0
  137. package/dist/esm/serialize/bytebuffer.js +384 -0
  138. package/dist/esm/serialize/index.js +471 -0
  139. package/dist/esm/serialize/serializer.js +97 -0
  140. package/dist/esm/token.js +42 -0
  141. package/dist/esm/version.js +11 -0
  142. package/dist/tsconfig.tsbuildinfo +1 -0
  143. package/package.json +4 -1
  144. package/CHANGELOG.md +0 -1845
  145. package/src/api/asset/extension.ts +0 -193
  146. package/src/api/asset/index.ts +0 -5
  147. package/src/api/dashboard/extension.ts +0 -651
  148. package/src/api/dashboard/index.ts +0 -38
  149. package/src/api/dashboard/packets.ts +0 -126
  150. package/src/api/endpoint/endpoint.ts +0 -56
  151. package/src/api/endpoint/extension.ts +0 -133
  152. package/src/api/endpoint/packets.ts +0 -176
  153. package/src/api/extension-registry.ts +0 -17
  154. package/src/api/extension.ts +0 -19
  155. package/src/api/http/extension.ts +0 -401
  156. package/src/api/http/http.ts +0 -79
  157. package/src/api/http/websocket.ts +0 -283
  158. package/src/api/i18n/extension.ts +0 -65
  159. package/src/api/logger/extension.ts +0 -75
  160. package/src/api/logger/index.ts +0 -4
  161. package/src/api/logger/packets.ts +0 -38
  162. package/src/api/permission/extension.ts +0 -107
  163. package/src/api/permission/permission.ts +0 -53
  164. package/src/api/plugin/extension.ts +0 -100
  165. package/src/api/registry/extension.ts +0 -326
  166. package/src/api/registry/packets.ts +0 -64
  167. package/src/api/server/extension.ts +0 -155
  168. package/src/api/server/index.ts +0 -11
  169. package/src/api/session/extension.ts +0 -205
  170. package/src/api/session/index.ts +0 -7
  171. package/src/api/signal/extension.ts +0 -125
  172. package/src/api/signal/packets.ts +0 -61
  173. package/src/api/signal/signal.ts +0 -67
  174. package/src/api/table/extension.ts +0 -554
  175. package/src/api/table/packets.ts +0 -252
  176. package/src/api/table/table.ts +0 -113
  177. package/src/app.ts +0 -95
  178. package/src/const.ts +0 -2
  179. package/src/errors.ts +0 -70
  180. package/src/event/index.ts +0 -18
  181. package/src/helper.ts +0 -16
  182. package/src/identifier.ts +0 -256
  183. package/src/index.ts +0 -87
  184. package/src/localization/locale.ts +0 -737
  185. package/src/lock.ts +0 -43
  186. package/src/network/connection.ts +0 -45
  187. package/src/network/encryption.ts +0 -221
  188. package/src/network/frame-transport.ts +0 -186
  189. package/src/network/network.ts +0 -322
  190. package/src/network/packet/packet-types.ts +0 -164
  191. package/src/network/packet/packet.ts +0 -55
  192. package/src/network/websocket-transport.ts +0 -123
  193. package/src/omu.ts +0 -156
  194. package/src/result.ts +0 -78
  195. package/src/serialize/bytebuffer.ts +0 -432
  196. package/src/serialize/serializer.ts +0 -149
  197. package/src/token.ts +0 -39
  198. package/src/version.ts +0 -2
  199. package/tsconfig.json +0 -18
  200. /package/{src/address.ts → dist/dts/address.d.ts} +0 -0
  201. /package/{src/api/signal/index.ts → dist/dts/api/signal/index.d.ts} +0 -0
  202. /package/{src/serialize/index.ts → dist/dts/serialize/index.d.ts} +0 -0
@@ -1,554 +0,0 @@
1
- import type { Unlisten } from '../../event';
2
- import { EventEmitter } from '../../event';
3
- import { Identifier, IdentifierMap } from '../../identifier';
4
- import { PacketType } from '../../network/packet/packet.js';
5
- import { Omu } from '../../omu';
6
- import type { JsonType, Serializable } from '../../serialize';
7
- import { Serializer } from '../../serialize';
8
- import { EndpointType } from '../endpoint/endpoint.js';
9
- import type { Extension } from '../extension.js';
10
- import { ExtensionType } from '../extension.js';
11
-
12
- import {
13
- SetConfigPacket,
14
- SetPermissionPacket,
15
- TableFetchPacket,
16
- TableFetchRangePacket,
17
- TableItemsPacket,
18
- TableKeysPacket,
19
- TablePacket,
20
- TableProxyPacket,
21
- } from './packets.js';
22
- import type { Table, TableConfig, TableEvents, TablePermissions } from './table.js';
23
- import { TableType } from './table.js';
24
-
25
- export const TABLE_EXTENSION_TYPE: ExtensionType<TableExtension> = new ExtensionType(
26
- 'table',
27
- (client) => new TableExtension(client),
28
- );
29
- export const TABLE_PERMISSION_ID: Identifier = TABLE_EXTENSION_TYPE.join('permission');
30
-
31
- const TABLE_SET_PERMISSION_PACKET = PacketType.createSerialized<SetPermissionPacket>(TABLE_EXTENSION_TYPE, {
32
- name: 'set_permission',
33
- serializer: SetPermissionPacket,
34
- });
35
- const TABLE_SET_CONFIG_PACKET = PacketType.createSerialized<SetConfigPacket>(TABLE_EXTENSION_TYPE, {
36
- name: 'set_config',
37
- serializer: SetConfigPacket,
38
- });
39
- const TABLE_LISTEN_PACKET = PacketType.createJson<Identifier>(TABLE_EXTENSION_TYPE, {
40
- name: 'listen',
41
- serializer: Identifier,
42
- });
43
- const TABLE_PROXY_LISTEN_PACKET = PacketType.createJson<Identifier>(TABLE_EXTENSION_TYPE, {
44
- name: 'proxy_listen',
45
- serializer: Identifier,
46
- });
47
- const TABLE_PROXY_PACKET = PacketType.createSerialized<TableProxyPacket>(TABLE_EXTENSION_TYPE, {
48
- name: 'proxy',
49
- serializer: TableProxyPacket,
50
- });
51
- const TABLE_ITEM_ADD_PACKET = PacketType.createSerialized<TableItemsPacket>(TABLE_EXTENSION_TYPE, {
52
- name: 'item_add',
53
- serializer: TableItemsPacket,
54
- });
55
- const TABLE_ITEM_UPDATE_PACKET = PacketType.createSerialized<TableItemsPacket>(TABLE_EXTENSION_TYPE, {
56
- name: 'item_update',
57
- serializer: TableItemsPacket,
58
- });
59
- const TABLE_ITEM_REMOVE_PACKET = PacketType.createSerialized<TableItemsPacket>(TABLE_EXTENSION_TYPE, {
60
- name: 'item_remove',
61
- serializer: TableItemsPacket,
62
- });
63
- const TABLE_ITEM_GET_ENDPOINT = EndpointType.createSerialized<TableKeysPacket, TableItemsPacket>(TABLE_EXTENSION_TYPE, {
64
- name: 'item_get',
65
- requestSerializer: TableKeysPacket,
66
- responseSerializer: TableItemsPacket,
67
- permissionId: TABLE_PERMISSION_ID,
68
- });
69
- const TABLE_ITEM_HAS_ENDPOINT = EndpointType.createSerialized<TableKeysPacket, Record<string, boolean>>(TABLE_EXTENSION_TYPE, {
70
- name: 'item_has',
71
- requestSerializer: TableKeysPacket,
72
- responseSerializer: Serializer.json(),
73
- permissionId: TABLE_PERMISSION_ID,
74
- });
75
- const TABLE_ITEM_HAS_ALL_ENDPOINT = EndpointType.createSerialized<TableKeysPacket, boolean>(TABLE_EXTENSION_TYPE, {
76
- name: 'item_has_all',
77
- requestSerializer: TableKeysPacket,
78
- responseSerializer: Serializer.json(),
79
- permissionId: TABLE_PERMISSION_ID,
80
- });
81
- const TABLE_ITEM_HAS_ANY_ENDPOINT = EndpointType.createSerialized<TableKeysPacket, boolean>(TABLE_EXTENSION_TYPE, {
82
- name: 'item_has_any',
83
- requestSerializer: TableKeysPacket,
84
- responseSerializer: Serializer.json(),
85
- permissionId: TABLE_PERMISSION_ID,
86
- });
87
- const TABLE_FETCH_ENDPOINT = EndpointType.createSerialized<TableFetchPacket, TableItemsPacket>(TABLE_EXTENSION_TYPE, {
88
- name: 'fetch',
89
- requestSerializer: TableFetchPacket,
90
- responseSerializer: TableItemsPacket,
91
- permissionId: TABLE_PERMISSION_ID,
92
- });
93
- const TABLE_FETCH_RANGE_ENDPOINT = EndpointType.createSerialized<TableFetchRangePacket, TableItemsPacket>(TABLE_EXTENSION_TYPE, {
94
- name: 'fetch_range',
95
- requestSerializer: TableFetchRangePacket,
96
- responseSerializer: TableItemsPacket,
97
- permissionId: TABLE_PERMISSION_ID,
98
- });
99
- const TABLE_FETCH_ALL_ENDPOINT = EndpointType.createSerialized<TablePacket, TableItemsPacket>(TABLE_EXTENSION_TYPE, {
100
- name: 'fetch_all',
101
- requestSerializer: TablePacket,
102
- responseSerializer: TableItemsPacket,
103
- permissionId: TABLE_PERMISSION_ID,
104
- });
105
- const TABLE_SIZE_ENDPOINT = EndpointType.createSerialized<TablePacket, number>(TABLE_EXTENSION_TYPE, {
106
- name: 'size',
107
- requestSerializer: TablePacket,
108
- responseSerializer: Serializer.json(),
109
- permissionId: TABLE_PERMISSION_ID,
110
- });
111
- const TABLE_ITEM_CLEAR_PACKET = PacketType.createSerialized<TablePacket>(TABLE_EXTENSION_TYPE, {
112
- name: 'clear',
113
- serializer: TablePacket,
114
- });
115
-
116
- export class TableExtension implements Extension {
117
- public readonly type: ExtensionType<TableExtension> = TABLE_EXTENSION_TYPE;
118
- private readonly tableMap = new IdentifierMap<Table<unknown>>();
119
-
120
- constructor(private readonly omu: Omu) {
121
- omu.network.registerPacket(
122
- TABLE_SET_PERMISSION_PACKET,
123
- TABLE_SET_CONFIG_PACKET,
124
- TABLE_LISTEN_PACKET,
125
- TABLE_PROXY_LISTEN_PACKET,
126
- TABLE_PROXY_PACKET,
127
- TABLE_ITEM_ADD_PACKET,
128
- TABLE_ITEM_UPDATE_PACKET,
129
- TABLE_ITEM_REMOVE_PACKET,
130
- TABLE_ITEM_CLEAR_PACKET,
131
- );
132
- }
133
-
134
- private createTable<T>(tableType: TableType<T>): Table<T> {
135
- this.omu.permissions.require(TABLE_PERMISSION_ID);
136
- if (this.has(tableType.id)) {
137
- throw new Error('Table already exists');
138
- }
139
- const table = new TableImpl<T>(this.omu, tableType);
140
- this.tableMap.set(tableType.id, table as Table<unknown>);
141
- return table;
142
- }
143
-
144
- public json<T, D extends JsonType = JsonType>(name: string, options: { key: (item: T) => string; serializer?: Serializable<T, D> }): Table<T> {
145
- const tableType = new TableType<T>(
146
- this.omu.app.id.base.join(name),
147
- Serializer.pipe<T, JsonType, Uint8Array>(options.serializer ?? Serializer.noop(), Serializer.json()),
148
- options.key,
149
- );
150
- return this.createTable(tableType);
151
- }
152
-
153
- public serialized<T>(name: string, options: { key: (item: T) => string; serializer: Serializable<T, Uint8Array> }): Table<T> {
154
- const tableType = new TableType<T>(
155
- this.omu.app.id.base.join(name),
156
- options.serializer,
157
- options.key,
158
- );
159
- return this.createTable(tableType);
160
- }
161
-
162
- public get<T>(type: TableType<T>): Table<T> {
163
- if (this.has(type.id)) {
164
- return this.tableMap.get(type.id) as Table<T>;
165
- }
166
- return this.createTable(type);
167
- }
168
-
169
- public has(identifier: Identifier): boolean {
170
- return this.tableMap.has(identifier);
171
- }
172
- }
173
-
174
- class TableImpl<T> implements Table<T> {
175
- public cache: Map<string, T>;
176
- public readonly event: TableEvents<T>;
177
- private readonly promiseMap: Map<string, Promise<T | undefined>>;
178
- private readonly proxies: Array<(item: T) => T | undefined>;
179
- private id: Identifier;
180
- private serializer: Serializable<T, Uint8Array>;
181
- private keyFunction: (item: T) => string;
182
- private listening: boolean;
183
- private cacheSize?: number;
184
- private config?: TableConfig;
185
- private permissions?: TablePermissions;
186
-
187
- constructor(
188
- private readonly omu: Omu,
189
- tableType: TableType<T>,
190
- ) {
191
- this.id = tableType.id;
192
- this.serializer = tableType.serializer;
193
- this.keyFunction = tableType.keyFunction;
194
- this.cache = new Map();
195
- this.event = {
196
- add: new EventEmitter<[Map<string, T>]>(),
197
- update: new EventEmitter<[Map<string, T>]>(),
198
- remove: new EventEmitter<[Map<string, T>]>(),
199
- clear: new EventEmitter<[]>(),
200
- cacheUpdate: new EventEmitter<[Map<string, T>]>(),
201
- };
202
- this.promiseMap = new Map();
203
- this.proxies = [];
204
- this.listening = false;
205
-
206
- omu.network.addPacketHandler(TABLE_PROXY_PACKET, (packet) => {
207
- if (!packet.id.isEqual(this.id)) {
208
- return;
209
- }
210
- let items = this.deserializeItems(packet.items);
211
- this.proxies.forEach((proxy) => {
212
- items = new Map(
213
- [...items.entries()]
214
- .map(([key, item]) => {
215
- const proxyItem = proxy(item);
216
- if (proxyItem !== undefined) {
217
- return [key, proxyItem as T];
218
- }
219
- return undefined;
220
- })
221
- .filter((item): item is [string, T] => {
222
- return typeof item !== 'undefined';
223
- }),
224
- );
225
- });
226
- omu.send(TABLE_PROXY_PACKET, {
227
- id: this.id,
228
- key: packet.key,
229
- items: Object.fromEntries(
230
- [...items.entries()].map(([key, item]) => {
231
- return [key, this.serializer.serialize(item)];
232
- }),
233
- ),
234
- });
235
- });
236
- omu.network.addPacketHandler(TABLE_ITEM_ADD_PACKET, (packet) => {
237
- if (!packet.id.isEqual(this.id)) {
238
- return;
239
- }
240
- const items = this.deserializeItems(packet.items);
241
- this.updateCache(items);
242
- this.event.add.emit(items);
243
- });
244
- omu.network.addPacketHandler(TABLE_ITEM_UPDATE_PACKET, (packet) => {
245
- if (!packet.id.isEqual(this.id)) {
246
- return;
247
- }
248
- const items = this.deserializeItems(packet.items);
249
- this.updateCache(items);
250
- this.event.update.emit(items);
251
- });
252
- omu.network.addPacketHandler(TABLE_ITEM_REMOVE_PACKET, (packet) => {
253
- if (!packet.id.isEqual(this.id)) {
254
- return;
255
- }
256
- const items = this.deserializeItems(packet.items);
257
- items.forEach((_, key) => {
258
- this.cache.delete(key);
259
- });
260
- this.event.remove.emit(items);
261
- this.event.cacheUpdate.emit(this.cache);
262
- });
263
- omu.network.addPacketHandler(TABLE_ITEM_CLEAR_PACKET, (packet) => {
264
- if (!packet.id.isEqual(this.id)) {
265
- return;
266
- }
267
- this.cache = new Map();
268
- this.event.clear.emit();
269
- this.event.cacheUpdate.emit(this.cache);
270
- });
271
- omu.event.ready.listen(() => this.onReady());
272
- }
273
-
274
- private updateCache(items: Map<string, T>): void {
275
- if (!this.cacheSize) {
276
- this.cache = items;
277
- } else {
278
- const cache = new Map([...this.cache, ...items].slice(-this.cacheSize));
279
- this.cache = cache;
280
- }
281
- this.event.cacheUpdate.emit(this.cache);
282
- }
283
-
284
- public listen(listener?: (items: Map<string, T>) => void): Unlisten {
285
- if (!this.listening) {
286
- this.omu.onReady(() => {
287
- this.omu.send(TABLE_LISTEN_PACKET, this.id);
288
- });
289
- this.listening = true;
290
- }
291
- if (listener) {
292
- return this.event.cacheUpdate.listen(listener);
293
- }
294
- return () => {};
295
- }
296
-
297
- public proxy(callback: (item: T) => T | undefined): Unlisten {
298
- if (this.proxies.length === 0) {
299
- let cancel = (): void => {};
300
- cancel = this.omu.onReady(() => {
301
- if (this.proxies.length === 0) {
302
- cancel();
303
- return;
304
- }
305
- this.omu.send(TABLE_PROXY_LISTEN_PACKET, this.id);
306
- });
307
- }
308
- this.proxies.push(callback);
309
- return () => {
310
- this.proxies.splice(this.proxies.indexOf(callback), 1);
311
- };
312
- }
313
-
314
- private onReady(): void {
315
- if (this.config) {
316
- this.omu.send(TABLE_SET_CONFIG_PACKET, {
317
- id: this.id,
318
- config: this.config,
319
- });
320
- }
321
- }
322
-
323
- public async get(key: string): Promise<T | undefined> {
324
- if (this.cache.has(key)) {
325
- return this.cache.get(key);
326
- }
327
- if (this.promiseMap.has(key)) {
328
- return await this.promiseMap.get(key);
329
- }
330
- const promise = this.omu.endpoints
331
- .call(TABLE_ITEM_GET_ENDPOINT, {
332
- id: this.id,
333
- keys: [key],
334
- })
335
- .then((res) => {
336
- const items = this.deserializeItems(res.items);
337
- this.updateCache(items);
338
- return this.cache.get(key);
339
- });
340
- this.promiseMap.set(key, promise);
341
- return await promise.then((item) => {
342
- this.promiseMap.delete(key);
343
- return item;
344
- });
345
- }
346
-
347
- public async getMany(...keys: string[]): Promise<Map<string, T>> {
348
- const filteredKeys = keys
349
- .filter((key) => !this.cache.has(key))
350
- .filter((key) => !this.promiseMap.has(key));
351
- if (filteredKeys.length === 0) {
352
- return new Map([...this.cache].filter(([key]) => keys.includes(key)));
353
- }
354
- const promise = this.omu.endpoints
355
- .call(TABLE_ITEM_GET_ENDPOINT, {
356
- id: this.id,
357
- keys: filteredKeys,
358
- })
359
- .then((res) => {
360
- const items = this.deserializeItems(res.items);
361
- this.updateCache(items);
362
- return items;
363
- });
364
- for (const key of filteredKeys) {
365
- this.promiseMap.set(
366
- key,
367
- promise.then((items) => items.get(key)),
368
- );
369
- }
370
- const items = await promise;
371
- for (const key of filteredKeys) {
372
- this.promiseMap.delete(key);
373
- }
374
- return new Map([...this.cache, ...items].filter(([key]) => keys.includes(key)));
375
- }
376
-
377
- public async add(...items: T[]): Promise<void> {
378
- const data = this.serializeItems(items);
379
- this.omu.send(TABLE_ITEM_ADD_PACKET, {
380
- id: this.id,
381
- items: data,
382
- });
383
- }
384
-
385
- public async update(...items: T[]): Promise<void> {
386
- const data = this.serializeItems(items);
387
- this.omu.send(TABLE_ITEM_UPDATE_PACKET, {
388
- id: this.id,
389
- items: data,
390
- });
391
- }
392
-
393
- public async remove(...items: T[]): Promise<void> {
394
- const data = this.serializeItems(items);
395
- const keys = Object.keys(data);
396
- this.cache = new Map([...this.cache].filter(([key]) => !keys.includes(key)));
397
- this.omu.send(TABLE_ITEM_REMOVE_PACKET, {
398
- id: this.id,
399
- items: data,
400
- });
401
- }
402
-
403
- public async clear(): Promise<void> {
404
- this.omu.send(TABLE_ITEM_CLEAR_PACKET, {
405
- id: this.id,
406
- });
407
- }
408
-
409
- public async has(key: string): Promise<boolean> {
410
- if (this.cache.has(key)) {
411
- return true;
412
- }
413
- const res = await this.omu.endpoints.call(TABLE_ITEM_HAS_ENDPOINT, {
414
- id: this.id,
415
- keys: [key],
416
- });
417
- return res[key] ?? false;
418
- }
419
-
420
- public async hasMany<T extends Record<string, unknown>>(...keys: (keyof T)[]): Promise<{ [key in keyof T]: boolean }> {
421
- const res = await this.omu.endpoints.call(TABLE_ITEM_HAS_ENDPOINT, {
422
- id: this.id,
423
- keys: keys,
424
- });
425
- return res as { [key in keyof T]: boolean };
426
- }
427
-
428
- public async hasAll(...keys: string[]): Promise<boolean> {
429
- const res = await this.omu.endpoints.call(TABLE_ITEM_HAS_ALL_ENDPOINT, {
430
- id: this.id,
431
- keys,
432
- });
433
- return res;
434
- }
435
-
436
- public async hasAny(...keys: string[]): Promise<boolean> {
437
- const res = await this.omu.endpoints.call(TABLE_ITEM_HAS_ANY_ENDPOINT, {
438
- id: this.id,
439
- keys,
440
- });
441
- return res;
442
- }
443
-
444
- public async fetchItems({
445
- limit,
446
- backward,
447
- cursor,
448
- }: {
449
- limit: number;
450
- backward?: boolean;
451
- cursor?: string;
452
- }): Promise<Map<string, T>> {
453
- const res = await this.omu.endpoints.call(
454
- TABLE_FETCH_ENDPOINT,
455
- new TableFetchPacket(this.id, limit, backward, cursor),
456
- );
457
- const items = this.deserializeItems(res.items);
458
- this.updateCache(items);
459
- return items;
460
- }
461
-
462
- public async fetchRange({
463
- start,
464
- end,
465
- }: {
466
- start: string;
467
- end: string;
468
- }): Promise<Map<string, T>> {
469
- const res = await this.omu.endpoints.call(TABLE_FETCH_RANGE_ENDPOINT, {
470
- id: this.id,
471
- start,
472
- end,
473
- });
474
- const items = this.deserializeItems(res.items);
475
- this.updateCache(items);
476
- return items;
477
- }
478
-
479
- public async fetchAll(): Promise<Map<string, T>> {
480
- const res = await this.omu.endpoints.call(TABLE_FETCH_ALL_ENDPOINT, {
481
- id: this.id,
482
- });
483
- const items = this.deserializeItems(res.items);
484
- this.updateCache(items);
485
- return items;
486
- }
487
-
488
- public async *iterate({
489
- backward,
490
- cursor,
491
- }: {
492
- backward?: boolean;
493
- cursor?: string;
494
- }): AsyncIterable<T> {
495
- while (true) {
496
- const items = await this.fetchItems({
497
- limit: this.cacheSize ?? 100,
498
- backward,
499
- cursor,
500
- });
501
- if (items.size === 0) {
502
- break;
503
- }
504
- yield* items.values();
505
- if (cursor !== undefined) {
506
- items.delete(cursor);
507
- }
508
- const cursorItem = backward ? items.values().next().value : [...items.values()].pop();
509
- if (!cursorItem) {
510
- break;
511
- }
512
- cursor = this.keyFunction(cursorItem);
513
- }
514
- }
515
-
516
- public async size(): Promise<number> {
517
- return await this.omu.endpoints.call(TABLE_SIZE_ENDPOINT, {
518
- id: this.id,
519
- });
520
- }
521
-
522
- private deserializeItems(items: Record<string, Uint8Array>): Map<string, T> {
523
- return new Map(
524
- Object.entries(items).map(([key, data]) => {
525
- const item = this.serializer.deserialize(data);
526
- this.cache.set(key, item);
527
- return [key, item];
528
- }),
529
- );
530
- }
531
-
532
- private serializeItems(items: T[]): Record<string, Uint8Array> {
533
- return Object.fromEntries(
534
- items.map((item) => {
535
- return [this.keyFunction(item), this.serializer.serialize(item)];
536
- }),
537
- );
538
- }
539
-
540
- public setCacheSize(size: number): void {
541
- this.cacheSize = size;
542
- }
543
-
544
- public setConfig(config: TableConfig): void {
545
- if (this.omu.running) {
546
- throw new Error('Cannot set config after client has started');
547
- }
548
- this.config = config;
549
- }
550
-
551
- public setPermissions(permissions: TablePermissions): void {
552
- this.permissions = permissions;
553
- }
554
- }