@gramlot/gramlot 0.3.18 → 0.3.19
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/dist/components/editors/__init__.py +1 -1
- package/dist/components/editors/assets/collection.js +1 -1
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/collection.js +1 -1
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +1 -1
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +1 -1
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/gramlot.js +1095 -618
- package/dist/gramlot.min.js +37 -37
- package/package.json +2 -2
- package/src/binding/data-binder.js +71 -0
- package/src/binding/providers.js +7 -4
- package/src/binding/router.js +3 -1
- package/src/binding/runtime.js +9 -1
- package/src/builder/binding.json +49 -0
- package/src/frame-messages.js +142 -73
- package/src/gramlot.d.ts +6 -3
- package/src/gramlot.js +10 -3
- package/src/handlers/rpc-handler.js +1 -1
- package/src/handlers/subscriptions-handler.js +20 -14
- package/src/page-store-connection.js +220 -0
- package/src/server-adapter.d.ts +4 -3
- package/src/server-adapter.js +6 -5
- package/src/websocket-transport.d.ts +10 -0
- package/src/websocket-transport.js +13 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {FrameMessages} from '../frame-messages.js';
|
|
2
1
|
import {Handler} from './handler.js';
|
|
3
2
|
import {sourceTarget} from '@genrojs/builders';
|
|
4
3
|
|
|
@@ -9,10 +8,7 @@ export class SubscriptionsHandler extends Handler {
|
|
|
9
8
|
#nodeIds = new WeakMap();
|
|
10
9
|
#sequence = 0;
|
|
11
10
|
#closed = false;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
attach(document) { this.#frames = new FrameMessages(this.gramlot, document); }
|
|
15
|
-
ready() { this.#frames.ready(); }
|
|
11
|
+
ready() { this.gramlot._frames.ready(); }
|
|
16
12
|
|
|
17
13
|
subscribe(topic, callback) {
|
|
18
14
|
this.#requireOpen();
|
|
@@ -32,33 +28,44 @@ export class SubscriptionsHandler extends Handler {
|
|
|
32
28
|
}
|
|
33
29
|
}
|
|
34
30
|
|
|
35
|
-
publish(topic, payload = {}) {
|
|
31
|
+
publish(topic, payload = {}, metadata = {}) {
|
|
36
32
|
this.#requireOpen();
|
|
37
33
|
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
38
34
|
throw new TypeError('publish requires one object of named data');
|
|
39
35
|
}
|
|
40
|
-
if (typeof topic === 'string') return this.#deliver(topic, payload);
|
|
36
|
+
if (typeof topic === 'string') return this.#deliver(topic, payload, metadata);
|
|
41
37
|
if (!topic || typeof topic !== 'object' || Array.isArray(topic)) {
|
|
42
38
|
throw new TypeError('publish requires a topic string or descriptor');
|
|
43
39
|
}
|
|
44
40
|
const keys = Object.keys(topic);
|
|
45
|
-
if (keys.some(key => !['topic', 'nodeId', 'parent', 'iframe'].includes(key))) {
|
|
41
|
+
if (keys.some(key => !['topic', 'nodeId', 'parent', 'iframe', 'pageId', 'server'].includes(key))) {
|
|
46
42
|
throw new Error('This publication route is not implemented yet');
|
|
47
43
|
}
|
|
48
44
|
this.#requireTopic(topic.topic);
|
|
49
45
|
if (topic.parent !== undefined && typeof topic.parent !== 'boolean') throw new TypeError('parent must be boolean');
|
|
46
|
+
if (topic.server !== undefined && typeof topic.server !== 'boolean') throw new TypeError('server must be boolean');
|
|
50
47
|
if (topic.iframe === '*') throw new Error('Recursive iframe broadcast is not implemented yet');
|
|
51
|
-
if (
|
|
48
|
+
if (topic.pageId !== undefined && (typeof topic.pageId !== 'string' || !topic.pageId)) throw new TypeError('pageId must be a nonempty string');
|
|
49
|
+
if ((topic.parent || topic.iframe !== undefined || topic.pageId !== undefined) && Object.hasOwn(topic, 'nodeId')) {
|
|
52
50
|
throw new Error('Interframe node addressing is not implemented yet');
|
|
53
51
|
}
|
|
54
|
-
|
|
52
|
+
let forward = null;
|
|
53
|
+
if (topic.server === true) {
|
|
54
|
+
if (['parent', 'iframe', 'nodeId'].some(key => Object.hasOwn(topic, key))) {
|
|
55
|
+
throw new Error('Server publication cannot combine window or node selectors');
|
|
56
|
+
}
|
|
57
|
+
if (!this.gramlot._pageStore) throw new Error('Server publication requires a WebSocket channel');
|
|
58
|
+
forward = this.gramlot._pageStore.preparePublish(topic, payload);
|
|
59
|
+
} else if (topic.parent || topic.iframe !== undefined || topic.pageId !== undefined) {
|
|
60
|
+
forward = this.gramlot._frames.prepare(topic, payload);
|
|
61
|
+
}
|
|
55
62
|
if (Object.hasOwn(topic, 'nodeId')) {
|
|
56
63
|
const node = this.gramlot.src.binding.nodeIds.get(topic.nodeId);
|
|
57
64
|
if (!node) throw new Error(`nodeId not found: ${topic.nodeId}`);
|
|
58
65
|
return this.publishNode(node, topic.topic, payload);
|
|
59
66
|
}
|
|
60
67
|
this.#deliver(topic.topic, payload);
|
|
61
|
-
forward?.();
|
|
68
|
+
return forward?.();
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
topicFor(node, topic) {
|
|
@@ -104,18 +111,17 @@ export class SubscriptionsHandler extends Handler {
|
|
|
104
111
|
|
|
105
112
|
dispose() {
|
|
106
113
|
this.#closed = true;
|
|
107
|
-
this.#frames.dispose();
|
|
108
114
|
this.#topics.clear();
|
|
109
115
|
}
|
|
110
116
|
|
|
111
|
-
#deliver(topic, payload) {
|
|
117
|
+
#deliver(topic, payload, metadata = {}) {
|
|
112
118
|
this.#requireTopic(topic);
|
|
113
119
|
const callbacks = this.#topics.get(topic);
|
|
114
120
|
if (!callbacks) return;
|
|
115
121
|
// Synchronous, ordered, no retention/equality suppression; exceptions propagate.
|
|
116
122
|
for (const [handle, callback] of [...callbacks]) {
|
|
117
123
|
if (this.#closed) break;
|
|
118
|
-
if (callbacks.has(handle)) callback(payload);
|
|
124
|
+
if (callbacks.has(handle)) callback(payload, {...metadata});
|
|
119
125
|
}
|
|
120
126
|
}
|
|
121
127
|
#requireTopic(topic) {
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {fromTytx, toTytx} from '@genrojs/tytx';
|
|
2
|
+
import {assertTransportData} from './references.js';
|
|
3
|
+
import {randomId} from './random-id.js';
|
|
4
|
+
import {RpcError} from './handlers/rpc-handler.js';
|
|
5
|
+
|
|
6
|
+
/** A logical server channel using the common Gramlot family connection. */
|
|
7
|
+
export class PageStoreConnection {
|
|
8
|
+
constructor(gramlot, url, document) {
|
|
9
|
+
this.gramlot = gramlot;
|
|
10
|
+
this.window = document.defaultView;
|
|
11
|
+
this.frames = gramlot._frames;
|
|
12
|
+
this.binders = new Map();
|
|
13
|
+
this.calls = new Map();
|
|
14
|
+
// Server page routes through registered direct children, not a window registry.
|
|
15
|
+
this.routes = new Map();
|
|
16
|
+
this.error = null;
|
|
17
|
+
this.closed = false;
|
|
18
|
+
this.ready = false;
|
|
19
|
+
this.socket = null;
|
|
20
|
+
const address = new URL(url, document.baseURI);
|
|
21
|
+
address.protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
22
|
+
this.endpoint = address.href;
|
|
23
|
+
this.origin = this.window.location.origin;
|
|
24
|
+
this.parent = this.frames.parent?.window ?? null;
|
|
25
|
+
this.frames.registerChannel('server', {
|
|
26
|
+
changed: peer => this.peerChanged(peer),
|
|
27
|
+
disconnected: peer => {
|
|
28
|
+
if (peer.parent) this.fail(new Error('PageStore parent disconnected'));
|
|
29
|
+
else this.forget(peer.window);
|
|
30
|
+
},
|
|
31
|
+
receive: (peer, message) => {
|
|
32
|
+
try { this.receive(peer, message); }
|
|
33
|
+
catch (error) { this.fail(error); }
|
|
34
|
+
},
|
|
35
|
+
}, {endpoint: this.endpoint, ready: false});
|
|
36
|
+
if (this.parent) return;
|
|
37
|
+
address.searchParams.set('pageId', gramlot.pageId);
|
|
38
|
+
this.socket = new this.window.WebSocket(address.href);
|
|
39
|
+
this.socket.addEventListener('open', () => this.connected());
|
|
40
|
+
this.socket.addEventListener('message', event => {
|
|
41
|
+
try { this.deliver(event.data); }
|
|
42
|
+
catch (error) { this.fail(error); }
|
|
43
|
+
});
|
|
44
|
+
this.socket.addEventListener('error', () => this.fail(new Error('PageStore WebSocket failed')));
|
|
45
|
+
this.socket.addEventListener('close', event => {
|
|
46
|
+
if (!this.closed) this.fail(new Error('PageStore disconnected (' + event.code + ')'));
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
peerChanged(peer) {
|
|
51
|
+
if (this.closed || !peer.parent) return;
|
|
52
|
+
const state = peer.channels.server;
|
|
53
|
+
if (peer.origin !== this.origin || state?.endpoint !== this.endpoint) {
|
|
54
|
+
this.fail(new Error('Parent does not provide the same PageStore endpoint'));
|
|
55
|
+
} else if (state.ready) this.connected();
|
|
56
|
+
else if (this.ready) this.fail(new Error('PageStore parent channel closed'));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
connected() {
|
|
60
|
+
if (this.closed || this.ready) return;
|
|
61
|
+
this.ready = true;
|
|
62
|
+
if (this.parent) this.upstream({op: 'attach', pageId: this.gramlot.pageId});
|
|
63
|
+
for (const binder of this.binders.values()) this.subscribe(binder);
|
|
64
|
+
for (const call of this.calls.values()) this.sendCall(call);
|
|
65
|
+
this.frames.setChannelState('server', {endpoint: this.endpoint, ready: true});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
receive(peer, payload) {
|
|
69
|
+
if (this.closed || peer.origin !== this.origin || peer.channels.server?.endpoint !== this.endpoint) return;
|
|
70
|
+
if (peer.parent) {
|
|
71
|
+
this.deliver(toTytx(payload));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!this.ready) return;
|
|
75
|
+
const pageId = payload?.pageId;
|
|
76
|
+
if (typeof pageId !== 'string' || !pageId || pageId === this.gramlot.pageId) return;
|
|
77
|
+
if (payload.op === 'attach') {
|
|
78
|
+
if (this.routes.has(pageId)) return;
|
|
79
|
+
this.routes.set(pageId, peer.window);
|
|
80
|
+
} else if (this.routes.get(pageId) !== peer.window) return;
|
|
81
|
+
if (payload.op === 'detach') this.routes.delete(pageId);
|
|
82
|
+
this.upstream(payload);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
forget(window) {
|
|
86
|
+
for (const [pageId, route] of this.routes) {
|
|
87
|
+
if (route !== window) continue;
|
|
88
|
+
this.routes.delete(pageId);
|
|
89
|
+
if (this.ready && !this.closed) this.upstream({op: 'detach', pageId});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
upstream(message) {
|
|
94
|
+
if (this.closed || !this.ready) throw new Error('PageStore is not connected');
|
|
95
|
+
if (this.parent) this.frames.send(this.parent, 'server', message);
|
|
96
|
+
else this.socket.send(toTytx(message));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
deliver(wire) {
|
|
100
|
+
if (this.closed) return;
|
|
101
|
+
const message = fromTytx(wire);
|
|
102
|
+
if (message.pageId === this.gramlot.pageId) {
|
|
103
|
+
if (message.op === 'closed') {
|
|
104
|
+
this.fail(new Error('PageStore page closed on the server'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!Object.hasOwn(message, 'op') && Object.hasOwn(message, 'id')) {
|
|
108
|
+
this.calls.get(message.id)?.finish(null, wire);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (message.op === 'publish') {
|
|
112
|
+
try { this.gramlot._subscriptions.publish(message.topic, message.payload, message.metadata); }
|
|
113
|
+
catch (error) { this.window.console.error('PageStore publication subscriber failed', error); }
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const binder = this.binders.get(message.binderId);
|
|
117
|
+
if (binder) binder.apply(message);
|
|
118
|
+
} else {
|
|
119
|
+
const window = this.routes.get(message.pageId);
|
|
120
|
+
if (window) this.frames.send(window, 'server', message);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
register(binder) {
|
|
125
|
+
if (this.closed) throw new Error('PageStore connection is closed');
|
|
126
|
+
this.binders.set(binder.id, binder);
|
|
127
|
+
if (this.ready) this.subscribe(binder);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
subscribe(binder) {
|
|
131
|
+
this.send({op: 'subscribe', binderId: binder.id, path: binder.serverPath});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
unregister(binder) {
|
|
135
|
+
if (!this.binders.delete(binder.id)) return;
|
|
136
|
+
if (!this.closed && this.ready) this.send({op: 'unsubscribe', binderId: binder.id});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
send(message) {
|
|
140
|
+
this.upstream({...message, pageId: this.gramlot.pageId});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Capture before local subscribers run; events are never queued or replayed. */
|
|
144
|
+
preparePublish(descriptor, payload) {
|
|
145
|
+
if (this.closed || !this.ready) throw new Error('PageStore is not connected');
|
|
146
|
+
assertTransportData(payload);
|
|
147
|
+
const message = {op: 'publish', id: randomId(), pageId: this.gramlot.pageId, topic: descriptor.topic, payload};
|
|
148
|
+
if (descriptor.pageId !== undefined) message.targetPageId = descriptor.pageId;
|
|
149
|
+
const wire = toTytx(message);
|
|
150
|
+
return async () => {
|
|
151
|
+
const response = fromTytx(await this.call(wire));
|
|
152
|
+
if (response.error) throw new RpcError(response.error);
|
|
153
|
+
return response.value;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Queue until attached, then pair responses by id independently of binder changes. */
|
|
158
|
+
call(text, signal) {
|
|
159
|
+
if (this.closed) return Promise.reject(this.error ?? new Error('PageStore connection is closed'));
|
|
160
|
+
if (signal?.aborted) return Promise.reject(signal.reason);
|
|
161
|
+
const message = fromTytx(text);
|
|
162
|
+
if (message.pageId !== this.gramlot.pageId || typeof message.id !== 'string'
|
|
163
|
+
|| !message.id || (Object.hasOwn(message, 'op') && message.op !== 'publish') || this.calls.has(message.id)) {
|
|
164
|
+
return Promise.reject(new Error('Invalid or duplicate RPC identity'));
|
|
165
|
+
}
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
const call = {message, sent: false, finish: (error, wire) => {
|
|
168
|
+
if (!this.calls.delete(message.id)) return;
|
|
169
|
+
signal?.removeEventListener('abort', abort);
|
|
170
|
+
if (wire === undefined) reject(error);
|
|
171
|
+
else resolve(wire);
|
|
172
|
+
}};
|
|
173
|
+
const abort = () => {
|
|
174
|
+
call.finish(signal.reason);
|
|
175
|
+
if (call.sent && this.ready && !this.closed) {
|
|
176
|
+
try { this.send({op: 'cancel', id: message.id}); }
|
|
177
|
+
catch (error) { this.fail(error); }
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
this.calls.set(message.id, call);
|
|
181
|
+
signal?.addEventListener('abort', abort, {once: true});
|
|
182
|
+
if (this.ready) this.sendCall(call);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
sendCall(call) {
|
|
187
|
+
try {
|
|
188
|
+
this.upstream(call.message);
|
|
189
|
+
call.sent = true;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
call.finish(error);
|
|
192
|
+
this.fail(error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
fail(error) {
|
|
197
|
+
this.error = error;
|
|
198
|
+
this.close();
|
|
199
|
+
this.window.console.error('PageStore channel failed', error);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
close() {
|
|
203
|
+
if (this.closed) return;
|
|
204
|
+
this.closed = true;
|
|
205
|
+
if (this.parent && this.ready && this.frames.isConnected(this.parent)) {
|
|
206
|
+
for (const pageId of [...this.routes.keys(), this.gramlot.pageId]) {
|
|
207
|
+
this.frames.send(this.parent, 'server', {op: 'detach', pageId});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
this.ready = false;
|
|
211
|
+
for (const call of this.calls.values()) {
|
|
212
|
+
call.finish(this.error ?? new Error('PageStore connection is closed'));
|
|
213
|
+
}
|
|
214
|
+
this.binders.clear();
|
|
215
|
+
this.routes.clear();
|
|
216
|
+
this.frames.unregisterChannel('server');
|
|
217
|
+
this.socket?.close();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
package/src/server-adapter.d.ts
CHANGED
|
@@ -8,10 +8,11 @@ import type {RpcTransport} from './handlers/rpc-handler.js';
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The server-dependent behaviour of one page, consulted by the core and readable as `gramlot.serverAdapter`; page
|
|
11
|
-
* authors do not call it. This class
|
|
11
|
+
* authors do not call it. This class selects HTTP or the configured shared WebSocket; a server chooses a subclass by naming its module in
|
|
12
12
|
* the bootstrap (`resources.serverAdapter`).
|
|
13
13
|
*/
|
|
14
14
|
export class ServerAdapter {
|
|
15
|
-
/**
|
|
16
|
-
createTransport(options: {rpcUrl?: string; closeUrl?: string; document: DomDocument
|
|
15
|
+
/** Use the configured shared channel, otherwise HTTP. */
|
|
16
|
+
createTransport(options: {rpcUrl?: string; closeUrl?: string; document: DomDocument;
|
|
17
|
+
channel?: Pick<RpcTransport, 'call'> | null}): RpcTransport;
|
|
17
18
|
}
|
package/src/server-adapter.js
CHANGED
|
@@ -5,19 +5,20 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
import {HttpTransport} from './http-transport.js';
|
|
8
|
+
import {WebSocketTransport} from './websocket-transport.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* The server-dependent behaviour of one page, consulted by the core and readable as `gramlot.serverAdapter`; it
|
|
11
|
-
* is not a proxy and page authors do not call it. This class
|
|
12
|
+
* is not a proxy and page authors do not call it. This class selects HTTP or the configured shared WebSocket. A server
|
|
12
13
|
* chooses another adapter, a subclass, by naming its module in the bootstrap (`resources.serverAdapter`, GC-230
|
|
13
14
|
* §010); the module imports this class from `@gramlot/gramlot/page`, which the import map points at the runtime
|
|
14
15
|
* already loaded.
|
|
15
16
|
*/
|
|
16
17
|
export class ServerAdapter {
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
createTransport({rpcUrl, closeUrl, document}) {
|
|
18
|
+
/** Use the shared WebSocket channel when configured; otherwise use HTTP. */
|
|
19
|
+
createTransport({rpcUrl, closeUrl, document, channel = null}) {
|
|
20
20
|
const window = document.defaultView;
|
|
21
|
-
|
|
21
|
+
const http = new HttpTransport(rpcUrl, {closeUrl, navigator: window.navigator, XMLHttpRequest: window.XMLHttpRequest});
|
|
22
|
+
return channel === null ? http : new WebSocketTransport(channel, http);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type {RpcTransport} from './handlers/rpc-handler.js';
|
|
2
|
+
|
|
3
|
+
/** Asynchronous RPC on an existing page channel; never opens another socket. */
|
|
4
|
+
export class WebSocketTransport {
|
|
5
|
+
constructor(channel: Pick<RpcTransport, 'call'>, closeTransport: Pick<RpcTransport, 'close' | 'dispose'>);
|
|
6
|
+
readonly supportsCallSync: false;
|
|
7
|
+
call(text: string, signal?: AbortSignal): Promise<string>;
|
|
8
|
+
close(pageId: string, options?: {beacon?: boolean}): void;
|
|
9
|
+
dispose(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* @ts-self-types="./websocket-transport.d.ts" */
|
|
2
|
+
/** RPC over the page's existing logical channel; Gramlot owns its socket lifecycle. */
|
|
3
|
+
export class WebSocketTransport {
|
|
4
|
+
constructor(channel, closeTransport) {
|
|
5
|
+
this.channel = channel;
|
|
6
|
+
this.closeTransport = closeTransport;
|
|
7
|
+
}
|
|
8
|
+
get supportsCallSync() { return false; }
|
|
9
|
+
call(text, signal) { return this.channel.call(text, signal); }
|
|
10
|
+
// Also release a bootstrapped page whose channel never attached.
|
|
11
|
+
close(pageId, options) { this.closeTransport.close(pageId, options); }
|
|
12
|
+
dispose() { this.closeTransport.dispose(); }
|
|
13
|
+
}
|