@moqtap/codec 0.1.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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/dist/chunk-23YG7F46.js +764 -0
  4. package/dist/chunk-2NARXGVA.cjs +194 -0
  5. package/dist/chunk-3BSZ55L3.cjs +307 -0
  6. package/dist/chunk-5WFXFLL4.cjs +1185 -0
  7. package/dist/chunk-DC4L6ZIT.js +307 -0
  8. package/dist/chunk-GDRGWFEK.cjs +498 -0
  9. package/dist/chunk-IQPDRQVC.js +1185 -0
  10. package/dist/chunk-QYG6KGOV.cjs +101 -0
  11. package/dist/chunk-UOBWHJA5.js +101 -0
  12. package/dist/chunk-WNTXF3DE.cjs +764 -0
  13. package/dist/chunk-YBSEOSSP.js +194 -0
  14. package/dist/chunk-YPXLV5YK.js +498 -0
  15. package/dist/codec-CTvFtQQI.d.cts +86 -0
  16. package/dist/codec-qPzfmLNu.d.ts +86 -0
  17. package/dist/draft14-session.cjs +6 -0
  18. package/dist/draft14-session.d.cts +8 -0
  19. package/dist/draft14-session.d.ts +8 -0
  20. package/dist/draft14-session.js +6 -0
  21. package/dist/draft14.cjs +121 -0
  22. package/dist/draft14.d.cts +96 -0
  23. package/dist/draft14.d.ts +96 -0
  24. package/dist/draft14.js +121 -0
  25. package/dist/draft7-session.cjs +7 -0
  26. package/dist/draft7-session.d.cts +7 -0
  27. package/dist/draft7-session.d.ts +7 -0
  28. package/dist/draft7-session.js +7 -0
  29. package/dist/draft7.cjs +60 -0
  30. package/dist/draft7.d.cts +72 -0
  31. package/dist/draft7.d.ts +72 -0
  32. package/dist/draft7.js +60 -0
  33. package/dist/index.cjs +40 -0
  34. package/dist/index.d.cts +40 -0
  35. package/dist/index.d.ts +40 -0
  36. package/dist/index.js +40 -0
  37. package/dist/session-types-B9NIf7_F.d.ts +101 -0
  38. package/dist/session-types-CCo-oA-d.d.cts +101 -0
  39. package/dist/session.cjs +27 -0
  40. package/dist/session.d.cts +24 -0
  41. package/dist/session.d.ts +24 -0
  42. package/dist/session.js +27 -0
  43. package/dist/types-CIk5W10V.d.cts +249 -0
  44. package/dist/types-CIk5W10V.d.ts +249 -0
  45. package/dist/types-ClXELFGN.d.cts +241 -0
  46. package/dist/types-ClXELFGN.d.ts +241 -0
  47. package/package.json +84 -0
  48. package/src/core/buffer-reader.ts +107 -0
  49. package/src/core/buffer-writer.ts +91 -0
  50. package/src/core/errors.ts +1 -0
  51. package/src/core/session-types.ts +103 -0
  52. package/src/core/types.ts +363 -0
  53. package/src/drafts/draft07/announce-fsm.ts +2 -0
  54. package/src/drafts/draft07/codec.ts +874 -0
  55. package/src/drafts/draft07/index.ts +70 -0
  56. package/src/drafts/draft07/messages.ts +44 -0
  57. package/src/drafts/draft07/parameters.ts +12 -0
  58. package/src/drafts/draft07/rules.ts +75 -0
  59. package/src/drafts/draft07/session-fsm.ts +353 -0
  60. package/src/drafts/draft07/session.ts +21 -0
  61. package/src/drafts/draft07/subscription-fsm.ts +3 -0
  62. package/src/drafts/draft07/varint.ts +23 -0
  63. package/src/drafts/draft14/codec.ts +1330 -0
  64. package/src/drafts/draft14/index.ts +132 -0
  65. package/src/drafts/draft14/messages.ts +76 -0
  66. package/src/drafts/draft14/rules.ts +70 -0
  67. package/src/drafts/draft14/session-fsm.ts +480 -0
  68. package/src/drafts/draft14/session.ts +26 -0
  69. package/src/drafts/draft14/types.ts +365 -0
  70. package/src/index.ts +85 -0
  71. package/src/session.ts +58 -0
@@ -0,0 +1,132 @@
1
+ export { createDraft14Codec } from './codec.js';
2
+ export type { Draft14Codec } from './codec.js';
3
+
4
+ export {
5
+ encodeMessage,
6
+ decodeMessage,
7
+ encodeSubgroupStream,
8
+ encodeDatagram,
9
+ encodeFetchStream,
10
+ decodeSubgroupStream,
11
+ decodeDatagram,
12
+ decodeFetchStream,
13
+ decodeDataStream,
14
+ createStreamDecoder,
15
+ createSubgroupStreamDecoder,
16
+ createFetchStreamDecoder,
17
+ createDataStreamDecoder,
18
+ } from './codec.js';
19
+
20
+ export type {
21
+ Draft14Message,
22
+ Draft14MessageType,
23
+ Draft14BaseMessage,
24
+ Draft14Params,
25
+ UnknownParam,
26
+ Draft14ClientSetup,
27
+ Draft14ServerSetup,
28
+ Draft14Subscribe,
29
+ Draft14SubscribeOk,
30
+ Draft14SubscribeUpdate,
31
+ Draft14SubscribeError,
32
+ Draft14Unsubscribe,
33
+ Draft14Publish,
34
+ Draft14PublishOk,
35
+ Draft14PublishError,
36
+ Draft14PublishDone,
37
+ Draft14PublishNamespace,
38
+ Draft14PublishNamespaceOk,
39
+ Draft14PublishNamespaceError,
40
+ Draft14PublishNamespaceDone,
41
+ Draft14PublishNamespaceCancel,
42
+ Draft14SubscribeNamespace,
43
+ Draft14SubscribeNamespaceOk,
44
+ Draft14SubscribeNamespaceError,
45
+ Draft14UnsubscribeNamespace,
46
+ Draft14Fetch,
47
+ Draft14FetchOk,
48
+ Draft14FetchError,
49
+ Draft14FetchCancel,
50
+ Draft14TrackStatus,
51
+ Draft14TrackStatusOk,
52
+ Draft14TrackStatusError,
53
+ Draft14GoAway,
54
+ Draft14MaxRequestId,
55
+ Draft14RequestsBlocked,
56
+ Draft14DataStream,
57
+ SubgroupStream,
58
+ DatagramObject,
59
+ FetchStream,
60
+ ObjectPayload,
61
+ SubgroupStreamHeader,
62
+ FetchStreamHeader,
63
+ DataStreamHeader,
64
+ DataStreamEvent,
65
+ } from './types.js';
66
+
67
+ export {
68
+ MSG_CLIENT_SETUP,
69
+ MSG_SERVER_SETUP,
70
+ MSG_SUBSCRIBE,
71
+ MSG_SUBSCRIBE_OK,
72
+ MSG_SUBSCRIBE_UPDATE,
73
+ MSG_SUBSCRIBE_ERROR,
74
+ MSG_UNSUBSCRIBE,
75
+ MSG_PUBLISH,
76
+ MSG_PUBLISH_OK,
77
+ MSG_PUBLISH_ERROR,
78
+ MSG_PUBLISH_DONE,
79
+ MSG_PUBLISH_NAMESPACE,
80
+ MSG_PUBLISH_NAMESPACE_OK,
81
+ MSG_PUBLISH_NAMESPACE_ERROR,
82
+ MSG_PUBLISH_NAMESPACE_DONE,
83
+ MSG_PUBLISH_NAMESPACE_CANCEL,
84
+ MSG_SUBSCRIBE_NAMESPACE,
85
+ MSG_SUBSCRIBE_NAMESPACE_OK,
86
+ MSG_SUBSCRIBE_NAMESPACE_ERROR,
87
+ MSG_UNSUBSCRIBE_NAMESPACE,
88
+ MSG_FETCH,
89
+ MSG_FETCH_OK,
90
+ MSG_FETCH_ERROR,
91
+ MSG_FETCH_CANCEL,
92
+ MSG_TRACK_STATUS,
93
+ MSG_TRACK_STATUS_OK,
94
+ MSG_TRACK_STATUS_ERROR,
95
+ MSG_GOAWAY,
96
+ MSG_MAX_REQUEST_ID,
97
+ MSG_REQUESTS_BLOCKED,
98
+ MESSAGE_TYPE_MAP,
99
+ MESSAGE_ID_MAP,
100
+ PARAM_ROLE,
101
+ PARAM_PATH,
102
+ PARAM_MAX_REQUEST_ID,
103
+ } from './messages.js';
104
+
105
+ // Session state machine
106
+ export { createDraft14SessionState } from './session.js';
107
+ export { Draft14SessionFSM } from './session-fsm.js';
108
+ export {
109
+ CONTROL_MESSAGES,
110
+ CLIENT_ONLY_MESSAGES,
111
+ SERVER_ONLY_MESSAGES,
112
+ BIDIRECTIONAL_MESSAGES,
113
+ getLegalOutgoing,
114
+ getLegalIncoming,
115
+ } from './rules.js';
116
+
117
+ export type {
118
+ SessionState,
119
+ SessionStateOptions,
120
+ SessionPhase,
121
+ SubscriptionState,
122
+ SubscriptionPhase,
123
+ PublishState,
124
+ PublishPhase,
125
+ FetchState,
126
+ FetchPhase,
127
+ TransitionResult,
128
+ ValidationResult,
129
+ ProtocolViolation,
130
+ ProtocolViolationCode,
131
+ SideEffect,
132
+ } from './session.js';
@@ -0,0 +1,76 @@
1
+ // Draft-14 message type wire IDs
2
+
3
+ export const MSG_SUBSCRIBE_UPDATE = 0x02n;
4
+ export const MSG_SUBSCRIBE = 0x03n;
5
+ export const MSG_SUBSCRIBE_OK = 0x04n;
6
+ export const MSG_SUBSCRIBE_ERROR = 0x05n;
7
+ export const MSG_PUBLISH_NAMESPACE = 0x06n;
8
+ export const MSG_PUBLISH_NAMESPACE_OK = 0x07n;
9
+ export const MSG_PUBLISH_NAMESPACE_ERROR = 0x08n;
10
+ export const MSG_PUBLISH_NAMESPACE_DONE = 0x09n;
11
+ export const MSG_UNSUBSCRIBE = 0x0An;
12
+ export const MSG_PUBLISH_DONE = 0x0Bn;
13
+ export const MSG_PUBLISH_NAMESPACE_CANCEL = 0x0Cn;
14
+ export const MSG_TRACK_STATUS = 0x0Dn;
15
+ export const MSG_TRACK_STATUS_OK = 0x0En;
16
+ export const MSG_TRACK_STATUS_ERROR = 0x0Fn;
17
+ export const MSG_GOAWAY = 0x10n;
18
+ export const MSG_SUBSCRIBE_NAMESPACE = 0x11n;
19
+ export const MSG_SUBSCRIBE_NAMESPACE_OK = 0x12n;
20
+ export const MSG_SUBSCRIBE_NAMESPACE_ERROR = 0x13n;
21
+ export const MSG_UNSUBSCRIBE_NAMESPACE = 0x14n;
22
+ export const MSG_MAX_REQUEST_ID = 0x15n;
23
+ export const MSG_FETCH = 0x16n;
24
+ export const MSG_FETCH_CANCEL = 0x17n;
25
+ export const MSG_FETCH_OK = 0x18n;
26
+ export const MSG_FETCH_ERROR = 0x19n;
27
+ export const MSG_REQUESTS_BLOCKED = 0x1An;
28
+ export const MSG_PUBLISH = 0x1Dn;
29
+ export const MSG_PUBLISH_OK = 0x1En;
30
+ export const MSG_PUBLISH_ERROR = 0x1Fn;
31
+ export const MSG_CLIENT_SETUP = 0x20n;
32
+ export const MSG_SERVER_SETUP = 0x21n;
33
+
34
+ // Parameter type IDs
35
+ export const PARAM_ROLE = 0x00n;
36
+ export const PARAM_PATH = 0x01n;
37
+ export const PARAM_MAX_REQUEST_ID = 0x02n;
38
+
39
+ // Map from wire ID to message type name
40
+ export const MESSAGE_TYPE_MAP: ReadonlyMap<bigint, string> = new Map([
41
+ [MSG_CLIENT_SETUP, 'client_setup'],
42
+ [MSG_SERVER_SETUP, 'server_setup'],
43
+ [MSG_SUBSCRIBE, 'subscribe'],
44
+ [MSG_SUBSCRIBE_OK, 'subscribe_ok'],
45
+ [MSG_SUBSCRIBE_UPDATE, 'subscribe_update'],
46
+ [MSG_SUBSCRIBE_ERROR, 'subscribe_error'],
47
+ [MSG_UNSUBSCRIBE, 'unsubscribe'],
48
+ [MSG_PUBLISH, 'publish'],
49
+ [MSG_PUBLISH_OK, 'publish_ok'],
50
+ [MSG_PUBLISH_ERROR, 'publish_error'],
51
+ [MSG_PUBLISH_DONE, 'publish_done'],
52
+ [MSG_PUBLISH_NAMESPACE, 'publish_namespace'],
53
+ [MSG_PUBLISH_NAMESPACE_OK, 'publish_namespace_ok'],
54
+ [MSG_PUBLISH_NAMESPACE_ERROR, 'publish_namespace_error'],
55
+ [MSG_PUBLISH_NAMESPACE_DONE, 'publish_namespace_done'],
56
+ [MSG_PUBLISH_NAMESPACE_CANCEL, 'publish_namespace_cancel'],
57
+ [MSG_SUBSCRIBE_NAMESPACE, 'subscribe_namespace'],
58
+ [MSG_SUBSCRIBE_NAMESPACE_OK, 'subscribe_namespace_ok'],
59
+ [MSG_SUBSCRIBE_NAMESPACE_ERROR, 'subscribe_namespace_error'],
60
+ [MSG_UNSUBSCRIBE_NAMESPACE, 'unsubscribe_namespace'],
61
+ [MSG_FETCH, 'fetch'],
62
+ [MSG_FETCH_OK, 'fetch_ok'],
63
+ [MSG_FETCH_ERROR, 'fetch_error'],
64
+ [MSG_FETCH_CANCEL, 'fetch_cancel'],
65
+ [MSG_TRACK_STATUS, 'track_status'],
66
+ [MSG_TRACK_STATUS_OK, 'track_status_ok'],
67
+ [MSG_TRACK_STATUS_ERROR, 'track_status_error'],
68
+ [MSG_GOAWAY, 'goaway'],
69
+ [MSG_MAX_REQUEST_ID, 'max_request_id'],
70
+ [MSG_REQUESTS_BLOCKED, 'requests_blocked'],
71
+ ]);
72
+
73
+ // Reverse map from message type name to wire ID
74
+ export const MESSAGE_ID_MAP: ReadonlyMap<string, bigint> = new Map(
75
+ [...MESSAGE_TYPE_MAP.entries()].map(([id, name]) => [name, id])
76
+ );
@@ -0,0 +1,70 @@
1
+ import type { Draft14MessageType } from './types.js';
2
+
3
+ // All draft-14 control messages
4
+ export const CONTROL_MESSAGES: ReadonlySet<Draft14MessageType> = new Set([
5
+ 'client_setup', 'server_setup',
6
+ 'subscribe', 'subscribe_ok', 'subscribe_update', 'subscribe_error', 'unsubscribe',
7
+ 'publish', 'publish_ok', 'publish_error', 'publish_done',
8
+ 'publish_namespace', 'publish_namespace_ok', 'publish_namespace_error', 'publish_namespace_done', 'publish_namespace_cancel',
9
+ 'subscribe_namespace', 'subscribe_namespace_ok', 'subscribe_namespace_error', 'unsubscribe_namespace',
10
+ 'fetch', 'fetch_ok', 'fetch_error', 'fetch_cancel',
11
+ 'track_status', 'track_status_ok', 'track_status_error',
12
+ 'goaway',
13
+ 'max_request_id', 'requests_blocked',
14
+ ]);
15
+
16
+ // Draft-14 is more symmetric: only setup messages are role-restricted.
17
+ // All other messages can be sent by both client and server.
18
+ export const CLIENT_ONLY_MESSAGES: ReadonlySet<Draft14MessageType> = new Set([
19
+ 'client_setup',
20
+ ]);
21
+
22
+ export const SERVER_ONLY_MESSAGES: ReadonlySet<Draft14MessageType> = new Set([
23
+ 'server_setup',
24
+ ]);
25
+
26
+ // Messages that are bidirectional (both client and server can send)
27
+ export const BIDIRECTIONAL_MESSAGES: ReadonlySet<Draft14MessageType> = new Set([
28
+ 'subscribe', 'subscribe_ok', 'subscribe_update', 'subscribe_error', 'unsubscribe',
29
+ 'publish', 'publish_ok', 'publish_error', 'publish_done',
30
+ 'publish_namespace', 'publish_namespace_ok', 'publish_namespace_error', 'publish_namespace_done', 'publish_namespace_cancel',
31
+ 'subscribe_namespace', 'subscribe_namespace_ok', 'subscribe_namespace_error', 'unsubscribe_namespace',
32
+ 'fetch', 'fetch_ok', 'fetch_error', 'fetch_cancel',
33
+ 'track_status', 'track_status_ok', 'track_status_error',
34
+ 'goaway',
35
+ 'max_request_id', 'requests_blocked',
36
+ ]);
37
+
38
+ // Messages legal in each session phase -- for outbound validation
39
+ export function getLegalOutgoing(phase: string, role: 'client' | 'server'): Set<Draft14MessageType> {
40
+ const legal = new Set<Draft14MessageType>();
41
+
42
+ switch (phase) {
43
+ case 'idle':
44
+ if (role === 'client') legal.add('client_setup');
45
+ break;
46
+ case 'setup':
47
+ if (role === 'server') legal.add('server_setup');
48
+ break;
49
+ case 'ready': {
50
+ // Both roles can send goaway
51
+ legal.add('goaway');
52
+ // All bidirectional messages are legal for both roles
53
+ for (const msg of BIDIRECTIONAL_MESSAGES) {
54
+ legal.add(msg);
55
+ }
56
+ break;
57
+ }
58
+ case 'draining':
59
+ // Limited set during draining - can still finish active operations
60
+ break;
61
+ }
62
+
63
+ return legal;
64
+ }
65
+
66
+ export function getLegalIncoming(phase: string, role: 'client' | 'server'): Set<Draft14MessageType> {
67
+ // Incoming from remote = the other role's outgoing
68
+ const remoteRole = role === 'client' ? 'server' : 'client';
69
+ return getLegalOutgoing(phase, remoteRole);
70
+ }