@arcanejs/protocol 0.7.0 → 0.8.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.
package/README.md CHANGED
@@ -1,43 +1,108 @@
1
- # `@arcanejs/diff`
1
+ # `@arcanejs/protocol`
2
2
 
3
- [![NPM Version](https://img.shields.io/npm/v/%40arcanejs%2Fdiff)](https://www.npmjs.com/package/@arcanejs/diff)
3
+ [![NPM Version](https://img.shields.io/npm/v/%40arcanejs%2Fprotocol)](https://www.npmjs.com/package/@arcanejs/protocol)
4
4
 
5
- This package provides an easy way to:
5
+ TypeScript protocol contracts for ArcaneJS.
6
6
 
7
- - Create diffs by comparing objects
8
- - Update objects by applying diffs
7
+ This package defines the shared wire/message types used between:
9
8
 
10
- This library is written in TypeScript,
11
- and produces diffs that are type-safe,
12
- and can only be applied to objects that match the type
13
- of the objects being compared.
9
+ - ArcaneJS server runtime (`@arcanejs/toolkit`)
10
+ - Browser frontend stage (`@arcanejs/toolkit/frontend` + `@arcanejs/toolkit-frontend`)
11
+ - Custom extensions (custom namespaces/components)
14
12
 
15
- This package is part of the
16
- [`arcanejs` project](https://github.com/ArcaneWizards/arcanejs#arcanejs),
17
- and is used to maintain a copy of a JSON tree in downstream clients in real-time
18
- via websockets.
13
+ ## Install
19
14
 
20
- ## Usage
15
+ ```bash
16
+ npm install @arcanejs/protocol
17
+ ```
18
+
19
+ ## What It Defines
20
+
21
+ ## Base protocol (`@arcanejs/protocol`)
22
+
23
+ - Base component envelope (`BaseComponentProto`, `AnyComponentProto`)
24
+ - Server messages:
25
+ - `metadata`
26
+ - `tree-full`
27
+ - `tree-diff`
28
+ - `call-response`
29
+ - `pong`
30
+ - Client messages:
31
+ - `component-message`
32
+ - `component-call`
33
+ - `ping`
34
+ - Type helpers for call/response pair typing:
35
+ - `BaseClientComponentCallPair`
36
+ - `CallForPair`
37
+ - `ReturnForPair`
38
+
39
+ ## Core namespace (`@arcanejs/protocol/core`)
40
+
41
+ Core component protocol types and guards:
42
+
43
+ - Component types: `ButtonComponent`, `GroupComponent`, `LabelComponent`, `RectComponent`, `SliderButtonComponent`, `SwitchComponent`, `TabComponent`, `TabsComponent`, `TextInputComponent`, `TimelineComponent`
44
+ - Message types: `CoreComponentMessage` and specific message variants
45
+ - Call type map: `CoreComponentCalls`
46
+ - Guards:
47
+ - `isCoreComponent(...)`
48
+ - `isCoreComponentMessage(...)`
49
+ - `isCoreComponentCall(...)`
50
+
51
+ ## Supporting modules
52
+
53
+ - `@arcanejs/protocol/styles`: shared style contracts (`GroupComponentStyle`, `LabelComponentStyle`)
54
+ - `@arcanejs/protocol/logging`: logger contract (`Logger`)
55
+
56
+ ## Usage Example
21
57
 
22
58
  ```ts
23
- import { diffJson, Diff } from '@arcanejs/diff/diff';
24
- import { patchJson } from '@arcanejs/diff/patch';
59
+ import type {
60
+ ServerMessage,
61
+ ClientMessage,
62
+ BaseComponentProto,
63
+ CallForPair,
64
+ ReturnForPair,
65
+ } from '@arcanejs/protocol';
66
+
67
+ import type {
68
+ CoreComponentCalls,
69
+ CoreComponentMessage,
70
+ } from '@arcanejs/protocol/core';
71
+
72
+ const onServerMessage = (msg: ServerMessage) => {
73
+ if (msg.type === 'tree-full') {
74
+ const root: BaseComponentProto<string, string> = msg.root;
75
+ console.log(root.namespace, root.component);
76
+ }
77
+ };
25
78
 
26
- type E = {
27
- foo: string;
28
- bar?: number[];
79
+ const message: CoreComponentMessage = {
80
+ type: 'component-message',
81
+ namespace: 'core',
82
+ componentKey: 1,
83
+ component: 'switch',
29
84
  };
30
85
 
31
- const a: E = { foo: 'bar' };
32
- const b: E = { foo: 'baz', bar: [1] };
86
+ const callMessage: CallForPair<'core', CoreComponentCalls, 'press'> = {
87
+ type: 'component-call',
88
+ namespace: 'core',
89
+ componentKey: 5,
90
+ action: 'press',
91
+ };
92
+
93
+ type PressReturn = ReturnForPair<CoreComponentCalls, 'press'>; // true
94
+ const _resultExample: PressReturn = true;
33
95
 
34
- const diffA: Diff<E> = diffJson(a, b);
96
+ const _clientMessage: ClientMessage = message;
97
+ ```
35
98
 
36
- const resultA = patchJson(a, diffA);
99
+ ## Notes
37
100
 
38
- console.log(resultB); // { foo: 'baz', bar: [1] }
101
+ - This package is primarily type contracts and guard helpers.
102
+ - `tree-diff` payloads use `Diff<...>` from `@arcanejs/diff`.
103
+ - `ping`/`pong` can be used for browser/server clock sync. `pong` includes `serverTimeMillis`.
104
+ - Custom namespaces should follow the same pattern as `core`: define component proto types, message/call unions, and guard helpers.
39
105
 
40
- const c = { baz: 'foo' };
106
+ ## Example Reference
41
107
 
42
- const resultB = patchJson(c, diffA); // TypeScript Type Error: Property 'baz' is missing in type '{ foo: string; bar?: number[] | undefined; }' but required in type '{ baz: string; }'
43
- ```
108
+ - Custom protocol implementation example: <https://github.com/ArcaneWizards/arcanejs/blob/main/examples/custom-components/src/custom-proto.ts>
package/dist/index.d.mts CHANGED
@@ -12,6 +12,12 @@ type MetadataMessage = {
12
12
  * The UUID for the current connection
13
13
  */
14
14
  connectionUuid: string;
15
+ clockSync: {
16
+ /**
17
+ * How often the frontend should send ping requests.
18
+ */
19
+ pingIntervalMs: number;
20
+ } | null;
15
21
  };
16
22
  type SendTreeMsg = {
17
23
  type: 'tree-full';
@@ -32,7 +38,15 @@ type CallResponseMsg<Namespace extends string, T> = {
32
38
  success: false;
33
39
  errorMessage: string;
34
40
  });
35
- type ServerMessage = MetadataMessage | SendTreeMsg | UpdateTreeMsg | CallResponseMsg<string, unknown>;
41
+ type PongResponseMessage = {
42
+ type: 'pong';
43
+ pingId: number;
44
+ /**
45
+ * Server clock time in milliseconds.
46
+ */
47
+ serverTimeMillis: number;
48
+ };
49
+ type ServerMessage = MetadataMessage | SendTreeMsg | UpdateTreeMsg | CallResponseMsg<string, unknown> | PongResponseMessage;
36
50
  type BaseClientComponentMessage<Namespace extends string> = {
37
51
  type: 'component-message';
38
52
  namespace: Namespace;
@@ -57,6 +71,10 @@ type ReturnForPair<Pairs, Action extends string & keyof Pairs> = Pairs extends R
57
71
  }> ? R : never;
58
72
  type AnyClientComponentMessage = BaseClientComponentMessage<string>;
59
73
  type AnyClientComponentCall = BaseClientComponentCall<string, string>;
60
- type ClientMessage = AnyClientComponentMessage | AnyClientComponentCall;
74
+ type PingRequestMessage = {
75
+ type: 'ping';
76
+ pingId: number;
77
+ };
78
+ type ClientMessage = AnyClientComponentMessage | AnyClientComponentCall | PingRequestMessage;
61
79
 
62
- export type { AnyClientComponentCall, AnyClientComponentMessage, AnyComponentProto, BaseClientComponentCall, BaseClientComponentCallPair, BaseClientComponentMessage, BaseComponentProto, CallForPair, CallResponseMsg, ClientMessage, MetadataMessage, ReturnForPair, SendTreeMsg, ServerMessage, UpdateTreeMsg };
80
+ export type { AnyClientComponentCall, AnyClientComponentMessage, AnyComponentProto, BaseClientComponentCall, BaseClientComponentCallPair, BaseClientComponentMessage, BaseComponentProto, CallForPair, CallResponseMsg, ClientMessage, MetadataMessage, PingRequestMessage, PongResponseMessage, ReturnForPair, SendTreeMsg, ServerMessage, UpdateTreeMsg };
package/dist/index.d.ts CHANGED
@@ -12,6 +12,12 @@ type MetadataMessage = {
12
12
  * The UUID for the current connection
13
13
  */
14
14
  connectionUuid: string;
15
+ clockSync: {
16
+ /**
17
+ * How often the frontend should send ping requests.
18
+ */
19
+ pingIntervalMs: number;
20
+ } | null;
15
21
  };
16
22
  type SendTreeMsg = {
17
23
  type: 'tree-full';
@@ -32,7 +38,15 @@ type CallResponseMsg<Namespace extends string, T> = {
32
38
  success: false;
33
39
  errorMessage: string;
34
40
  });
35
- type ServerMessage = MetadataMessage | SendTreeMsg | UpdateTreeMsg | CallResponseMsg<string, unknown>;
41
+ type PongResponseMessage = {
42
+ type: 'pong';
43
+ pingId: number;
44
+ /**
45
+ * Server clock time in milliseconds.
46
+ */
47
+ serverTimeMillis: number;
48
+ };
49
+ type ServerMessage = MetadataMessage | SendTreeMsg | UpdateTreeMsg | CallResponseMsg<string, unknown> | PongResponseMessage;
36
50
  type BaseClientComponentMessage<Namespace extends string> = {
37
51
  type: 'component-message';
38
52
  namespace: Namespace;
@@ -57,6 +71,10 @@ type ReturnForPair<Pairs, Action extends string & keyof Pairs> = Pairs extends R
57
71
  }> ? R : never;
58
72
  type AnyClientComponentMessage = BaseClientComponentMessage<string>;
59
73
  type AnyClientComponentCall = BaseClientComponentCall<string, string>;
60
- type ClientMessage = AnyClientComponentMessage | AnyClientComponentCall;
74
+ type PingRequestMessage = {
75
+ type: 'ping';
76
+ pingId: number;
77
+ };
78
+ type ClientMessage = AnyClientComponentMessage | AnyClientComponentCall | PingRequestMessage;
61
79
 
62
- export type { AnyClientComponentCall, AnyClientComponentMessage, AnyComponentProto, BaseClientComponentCall, BaseClientComponentCallPair, BaseClientComponentMessage, BaseComponentProto, CallForPair, CallResponseMsg, ClientMessage, MetadataMessage, ReturnForPair, SendTreeMsg, ServerMessage, UpdateTreeMsg };
80
+ export type { AnyClientComponentCall, AnyClientComponentMessage, AnyComponentProto, BaseClientComponentCall, BaseClientComponentCallPair, BaseClientComponentMessage, BaseComponentProto, CallForPair, CallResponseMsg, ClientMessage, MetadataMessage, PingRequestMessage, PongResponseMessage, ReturnForPair, SendTreeMsg, ServerMessage, UpdateTreeMsg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcanejs/protocol",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
5
  "description": "The JSON protocol types for the @arcanejs Toolkit",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "./package.json": "./package.json"
35
35
  },
36
36
  "dependencies": {
37
- "@arcanejs/diff": "^0.5.1"
37
+ "@arcanejs/diff": "^0.5.2"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/eslint": "^8.56.5",