@gramlot/gramlot 0.2.12 → 0.2.14
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/gramlot.js +257 -53
- package/dist/gramlot.min.js +17 -17
- package/package.json +1 -1
- package/src/binding/installation.js +14 -7
- package/src/binding/providers.js +69 -6
- package/src/binding/runtime.js +3 -3
- package/src/bootstrap.js +3 -3
- package/src/builder/binding.json +87 -0
- package/src/builder/gramlot-builder.js +2 -2
- package/src/builder/source.js +3 -1
- package/src/gramlot.d.ts +10 -8
- package/src/gramlot.js +7 -6
- package/src/handlers/rpc-handler.d.ts +42 -5
- package/src/handlers/rpc-handler.js +30 -3
- package/src/handlers/source-handler.js +4 -2
- package/src/http-transport.d.ts +27 -0
- package/src/{transport.js → http-transport.js} +11 -18
- package/src/index.d.ts +4 -2
- package/src/index.js +2 -1
- package/src/renderer/gramlot-renderer.js +1 -1
- package/src/server/assets.js +3 -3
- package/src/server/conformance.d.ts +1 -1
- package/src/server/conformance.js +100 -51
- package/src/server/gramlot-server.d.ts +29 -17
- package/src/server/gramlot-server.js +86 -20
- package/src/server/index.d.ts +2 -1
- package/src/server/index.js +2 -1
- package/src/server/page.d.ts +13 -3
- package/src/server/page.js +41 -12
- package/src/view/button.js +7 -7
- package/src/transport.d.ts +0 -34
package/package.json
CHANGED
|
@@ -169,18 +169,25 @@ export function requireInstallable(branch) {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
// Provider attributes (§4.8): only a `dataFormula` or a `
|
|
172
|
+
// Provider attributes (§4.8): only a `dataFormula`, a `dataController` or a `dataRpc` carries them;
|
|
173
|
+
// the callbacks of the call only a `dataRpc`.
|
|
173
174
|
const PROVIDER_ATTRIBUTES = ['_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges'];
|
|
174
|
-
const PROVIDER_TAGS = new Set(['dataFormula', 'dataController']);
|
|
175
|
+
const PROVIDER_TAGS = new Set(['dataFormula', 'dataController', 'dataRpc']);
|
|
176
|
+
const RPC_ATTRIBUTES = ['_onCalling', '_onResult', '_onError'];
|
|
175
177
|
// The lifecycle attributes: true, false, null or a number (a delay in ms, P22); a pointer is not read.
|
|
176
178
|
const LIFECYCLE_ATTRIBUTES = ['_init', '_onBuilt', '_onStart'];
|
|
177
179
|
|
|
178
180
|
/**
|
|
179
|
-
* A provider attribute on any other node is an error before any write
|
|
180
|
-
*
|
|
181
|
-
* lifecycle attribute whose value is not `true`, `false`, null or a number.
|
|
181
|
+
* A provider attribute on any other node is an error before any write, and so is an rpc callback
|
|
182
|
+
* outside a `dataRpc`; so is a `==` expression in an attribute of a provider node (S09: `==` belongs
|
|
183
|
+
* to the visual nodes), and a lifecycle attribute whose value is not `true`, `false`, null or a number.
|
|
182
184
|
*/
|
|
183
185
|
function requireProviderAttributes(node) {
|
|
186
|
+
const isSet = name => node.getAttr(name) !== null && node.getAttr(name) !== undefined;
|
|
187
|
+
if (node.nodeTag !== 'dataRpc') {
|
|
188
|
+
const callback = RPC_ATTRIBUTES.find(isSet);
|
|
189
|
+
if (callback) throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': '${callback}' is allowed only on dataRpc`);
|
|
190
|
+
}
|
|
184
191
|
if (PROVIDER_TAGS.has(node.nodeTag)) {
|
|
185
192
|
const expression = Object.entries(node.getAttr()).find(([, value]) => isExpression(value));
|
|
186
193
|
if (expression) {
|
|
@@ -194,8 +201,8 @@ function requireProviderAttributes(node) {
|
|
|
194
201
|
}
|
|
195
202
|
return;
|
|
196
203
|
}
|
|
197
|
-
const name = PROVIDER_ATTRIBUTES.find(
|
|
198
|
-
if (name) throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': '${name}' is allowed only on dataFormula and
|
|
204
|
+
const name = PROVIDER_ATTRIBUTES.find(isSet);
|
|
205
|
+
if (name) throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': '${name}' is allowed only on dataFormula, dataController and dataRpc`);
|
|
199
206
|
}
|
|
200
207
|
|
|
201
208
|
/** The nodes of `branch` (a SourceBagNode or a SourceBag), depth first, in document order. */
|
package/src/binding/providers.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {Bag} from '@genrojs/bag';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Providers of one Gramlot page (source plan §4.8): the semantic runtime of a `dataFormula
|
|
5
|
-
* `dataController`, owned by its NodeBinding.
|
|
4
|
+
* Providers of one Gramlot page (source plan §4.8): the semantic runtime of a `dataFormula`, a
|
|
5
|
+
* `dataController` or a `dataRpc`, owned by its NodeBinding.
|
|
6
6
|
*
|
|
7
7
|
* One owner per trigger (B7): the router delivers the Data changes of the `^` pointers to
|
|
8
8
|
* `receive`; `_timing` is started by `register`; `_init`, `_onBuilt` and `_onStart` are invoked by
|
|
9
|
-
* BindingRuntime; the click of a button invokes the controller nested in it, through ButtonBinding. Every timer is tracked on the NodeBinding, so the close of the node cancels it.
|
|
9
|
+
* BindingRuntime; the click of a button invokes the controller or the rpc nested in it, through ButtonBinding. Every timer is tracked on the NodeBinding, so the close of the node cancels it.
|
|
10
10
|
* A body is named logic (P20), the group method given by `LogicRegistry.resolve`, or inline code
|
|
11
11
|
* compiled by the page's InlineCompiler (S09); there is no fallback from a name to inline.
|
|
12
12
|
*/
|
|
@@ -16,6 +16,7 @@ import {Bag} from '@genrojs/bag';
|
|
|
16
16
|
const CONTROL_ATTRIBUTES = new Set([
|
|
17
17
|
'destination_path', 'result_path', 'func', 'formula', 'script', 'if', 'else',
|
|
18
18
|
'_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
19
|
+
'method', 'timeout', '_onCalling', '_onResult', '_onError',
|
|
19
20
|
]);
|
|
20
21
|
|
|
21
22
|
export class Provider {
|
|
@@ -24,6 +25,8 @@ export class Provider {
|
|
|
24
25
|
#timingRegistration = null;
|
|
25
26
|
#stopTiming = null;
|
|
26
27
|
#cancelDelay = null;
|
|
28
|
+
// The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
|
|
29
|
+
#rpcCall = null;
|
|
27
30
|
|
|
28
31
|
constructor(binding) {
|
|
29
32
|
this.#binding = binding;
|
|
@@ -35,8 +38,8 @@ export class Provider {
|
|
|
35
38
|
|
|
36
39
|
get binding() { return this.#binding; }
|
|
37
40
|
get node() { return this.binding.node; }
|
|
38
|
-
/** 'formula' | 'controller'. */
|
|
39
|
-
get kind() { throw new Error('Provider.kind is defined by FormulaProvider and
|
|
41
|
+
/** 'formula' | 'controller' | 'rpc'. */
|
|
42
|
+
get kind() { throw new Error('Provider.kind is defined by FormulaProvider, ControllerProvider and RpcProvider'); }
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
45
|
* Step 4 of §5.1, and every rebinding: the `^` pointers are registered again from the current
|
|
@@ -87,12 +90,14 @@ export class Provider {
|
|
|
87
90
|
* `_else` are inline (Q11.1): a false `_if` stops, or `_else` replaces the body (D5); an empty `_if`
|
|
88
91
|
* is absent and an empty `_else` or body is none, as legacy. A body is
|
|
89
92
|
* the inline `formula`/`script`, compiled by the page's InlineCompiler, or the named `func`. An
|
|
90
|
-
* error propagates to the caller (P12). A closed node runs nothing.
|
|
93
|
+
* error propagates to the caller (P12). A closed node runs nothing. The body of a `dataRpc` is the
|
|
94
|
+
* call: `invoke` returns its promise, and a false `_if` runs `_else` as inline code without calling.
|
|
91
95
|
*/
|
|
92
96
|
invoke(trigger) {
|
|
93
97
|
if (this.binding.closed) return;
|
|
94
98
|
const node = this.node;
|
|
95
99
|
const formula = this.kind === 'formula';
|
|
100
|
+
const rpc = this.kind === 'rpc';
|
|
96
101
|
const kwargs = this.readArguments(trigger);
|
|
97
102
|
let [attr, body] = [formula ? 'formula' : 'script', node.getAttr(formula ? 'formula' : 'script')];
|
|
98
103
|
// Truthiness of `_if`, `_else` and the body, as legacy (`gnrdomsource.js:371-385, 473-482`).
|
|
@@ -100,7 +105,12 @@ export class Provider {
|
|
|
100
105
|
if (condition && !this.#inline('_if', condition, `return (${condition})`, kwargs, false)) {
|
|
101
106
|
[attr, body] = ['_else', node.getAttr('_else')];
|
|
102
107
|
if (!body) return;
|
|
108
|
+
if (rpc) {
|
|
109
|
+
this.#inline(attr, body, body, kwargs, true);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
103
112
|
}
|
|
113
|
+
if (rpc) return this.#call(kwargs);
|
|
104
114
|
const func = node.getAttr('func');
|
|
105
115
|
let result;
|
|
106
116
|
if (body) {
|
|
@@ -193,6 +203,54 @@ export class Provider {
|
|
|
193
203
|
return this.binding.runtime.inlineCompiler.compile(this.node, attr, code, names, source)({...kwargs, _kwargs: kwargs});
|
|
194
204
|
}
|
|
195
205
|
|
|
206
|
+
/**
|
|
207
|
+
* The call of a `dataRpc`: `_onCalling`, then `gramlot.rpc.call('data', method, …)` with the author
|
|
208
|
+
* arguments, then the value written at `result_path` when present and `_onResult`. The previous
|
|
209
|
+
* call of the node is aborted and its answer dropped (latest wins); the close of the node aborts
|
|
210
|
+
* the call too. `timeout` > 0 ms aborts it with a `TimeoutError`. A failure goes to `_onError`, or
|
|
211
|
+
* rejects the returned promise when the node declares none.
|
|
212
|
+
*/
|
|
213
|
+
async #call(kwargs) {
|
|
214
|
+
const node = this.node;
|
|
215
|
+
const [onCalling, onResult, onError] = ['_onCalling', '_onResult', '_onError'].map(name => node.getAttr(name));
|
|
216
|
+
if (onCalling) this.#inline('_onCalling', onCalling, onCalling, kwargs, true);
|
|
217
|
+
this.#rpcCall?.abort();
|
|
218
|
+
const controller = new AbortController();
|
|
219
|
+
this.#rpcCall = controller;
|
|
220
|
+
const release = this.binding.track(() => controller.abort());
|
|
221
|
+
const timeout = this.#resolved('timeout');
|
|
222
|
+
const timer = timeout > 0 ? setTimeout(() => {
|
|
223
|
+
const error = new Error(`${node.nodeTag} '${node.label}': no answer within ${timeout} ms`);
|
|
224
|
+
error.name = 'TimeoutError';
|
|
225
|
+
controller.abort(error);
|
|
226
|
+
}, timeout) : null;
|
|
227
|
+
const aborted = new Promise((resolve, reject) => {
|
|
228
|
+
controller.signal.addEventListener('abort', () => reject(controller.signal.reason), {once: true});
|
|
229
|
+
});
|
|
230
|
+
let outcome;
|
|
231
|
+
try {
|
|
232
|
+
const call = this.binding.runtime.gramlot.rpc.call('data', this.#resolved('method'), authorArguments(kwargs),
|
|
233
|
+
{signal: controller.signal});
|
|
234
|
+
outcome = {result: await Promise.race([call, aborted])};
|
|
235
|
+
} catch (error) {
|
|
236
|
+
outcome = {error};
|
|
237
|
+
}
|
|
238
|
+
clearTimeout(timer);
|
|
239
|
+
release();
|
|
240
|
+
// Superseded by a newer call, or the node closed: the answer is dropped.
|
|
241
|
+
const current = this.#rpcCall === controller && !this.binding.closed;
|
|
242
|
+
if (this.#rpcCall === controller) this.#rpcCall = null;
|
|
243
|
+
if (!current) return;
|
|
244
|
+
if ('error' in outcome) {
|
|
245
|
+
if (!onError) throw outcome.error;
|
|
246
|
+
this.#inline('_onError', onError, onError, {...kwargs, error: outcome.error}, true);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const {result} = outcome;
|
|
250
|
+
if (node.getAttr('result_path') != null) node.setRelativeData(node.pathAttribute('result_path'), result);
|
|
251
|
+
if (onResult) this.#inline('_onResult', onResult, onResult, {...kwargs, result}, true);
|
|
252
|
+
}
|
|
253
|
+
|
|
196
254
|
#closeRegistrations() {
|
|
197
255
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
198
256
|
this.#timingRegistration = null;
|
|
@@ -209,6 +267,11 @@ export class ControllerProvider extends Provider {
|
|
|
209
267
|
get kind() { return 'controller'; }
|
|
210
268
|
}
|
|
211
269
|
|
|
270
|
+
/** The provider of a `dataRpc`: the body is the call of a server endpoint through `gramlot.rpc`. */
|
|
271
|
+
export class RpcProvider extends Provider {
|
|
272
|
+
get kind() { return 'rpc'; }
|
|
273
|
+
}
|
|
274
|
+
|
|
212
275
|
// The trigger fields of the kwargs, in the legacy order of the inline parameters.
|
|
213
276
|
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason'];
|
|
214
277
|
// The fields a click adds (P9): locals of the inline bodies, never author arguments.
|
package/src/binding/runtime.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {SourceBag} from '@genrojs/builders';
|
|
2
2
|
import {DataRouter} from './router.js';
|
|
3
3
|
import {DataInstaller} from './installation.js';
|
|
4
|
-
import {ControllerProvider, FormulaProvider} from './providers.js';
|
|
4
|
+
import {ControllerProvider, FormulaProvider, RpcProvider} from './providers.js';
|
|
5
5
|
import {InlineCompiler} from './inline.js';
|
|
6
6
|
|
|
7
7
|
// The Provider class of each provider data-element (§4.8).
|
|
8
|
-
const PROVIDERS = {dataFormula: FormulaProvider, dataController: ControllerProvider};
|
|
8
|
+
const PROVIDERS = {dataFormula: FormulaProvider, dataController: ControllerProvider, dataRpc: RpcProvider};
|
|
9
9
|
|
|
10
10
|
// Attributes whose change moves the Data context of a whole branch (§4.4, upd_attrs row).
|
|
11
11
|
const CONTEXT_ATTRIBUTES = ['datapath', '_anchor', 'node_id', 'form', 'formId'];
|
|
@@ -345,7 +345,7 @@ export class NodeBinding {
|
|
|
345
345
|
get closed() { return this.#closed; }
|
|
346
346
|
/** The active DataRegistrations of the node. */
|
|
347
347
|
get registrations() { return [...this.#registrations]; }
|
|
348
|
-
/** The Provider of a `dataFormula` or `
|
|
348
|
+
/** The Provider of a `dataFormula`, `dataController` or `dataRpc`, as a list of zero or one. */
|
|
349
349
|
get providers() { return this.#provider ? [this.#provider] : []; }
|
|
350
350
|
|
|
351
351
|
/** The clicks counted by the node's ButtonBinding since the node entered the Source (P9). */
|
package/src/bootstrap.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import {Gramlot} from './gramlot.js';
|
|
8
8
|
import {LogicRegistry} from './binding/logic.js';
|
|
9
|
-
import {
|
|
9
|
+
import {HttpTransport} from './http-transport.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* The browser start of one page (source plan §4.12). The server writes
|
|
@@ -71,8 +71,8 @@ export class PageBootstrap {
|
|
|
71
71
|
|
|
72
72
|
/** The close request of a page that never got its Gramlot instance: a beacon, as `Gramlot` sends on pagehide. */
|
|
73
73
|
closePage() {
|
|
74
|
-
const {
|
|
75
|
-
new
|
|
74
|
+
const {rpcUrl, closeUrl, pageId} = this.config;
|
|
75
|
+
new HttpTransport(rpcUrl, {closeUrl, navigator: this.document.defaultView.navigator})
|
|
76
76
|
.close(pageId, {beacon: true});
|
|
77
77
|
}
|
|
78
78
|
}
|
package/src/builder/binding.json
CHANGED
|
@@ -211,6 +211,93 @@
|
|
|
211
211
|
"_meta": {
|
|
212
212
|
"data_element": true
|
|
213
213
|
}
|
|
214
|
+
},
|
|
215
|
+
"dataRpc": {
|
|
216
|
+
"doc": "Server endpoint called through gramlot.rpc; the value is written at result_path when given.",
|
|
217
|
+
"sub_tags": "",
|
|
218
|
+
"parent_tags": null,
|
|
219
|
+
"inherits_from": null,
|
|
220
|
+
"ns": null,
|
|
221
|
+
"attributes": {
|
|
222
|
+
"parameters": [
|
|
223
|
+
{
|
|
224
|
+
"name": "method",
|
|
225
|
+
"kind": "positional_or_keyword",
|
|
226
|
+
"role": "attribute",
|
|
227
|
+
"annotation": {
|
|
228
|
+
"kind": "type",
|
|
229
|
+
"module": "builtins",
|
|
230
|
+
"name": "str"
|
|
231
|
+
},
|
|
232
|
+
"has_default": false
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"name": "result_path",
|
|
236
|
+
"kind": "positional_or_keyword",
|
|
237
|
+
"role": "attribute",
|
|
238
|
+
"annotation": {
|
|
239
|
+
"kind": "union",
|
|
240
|
+
"items": [
|
|
241
|
+
{
|
|
242
|
+
"kind": "type",
|
|
243
|
+
"module": "builtins",
|
|
244
|
+
"name": "str"
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
"kind": "type",
|
|
248
|
+
"module": "builtins",
|
|
249
|
+
"name": "NoneType"
|
|
250
|
+
}
|
|
251
|
+
]
|
|
252
|
+
},
|
|
253
|
+
"has_default": true,
|
|
254
|
+
"default": null
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
"name": "timeout",
|
|
258
|
+
"kind": "positional_or_keyword",
|
|
259
|
+
"role": "attribute",
|
|
260
|
+
"annotation": {
|
|
261
|
+
"kind": "union",
|
|
262
|
+
"items": [
|
|
263
|
+
{
|
|
264
|
+
"kind": "type",
|
|
265
|
+
"module": "builtins",
|
|
266
|
+
"name": "int"
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
"kind": "type",
|
|
270
|
+
"module": "builtins",
|
|
271
|
+
"name": "float"
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
"kind": "type",
|
|
275
|
+
"module": "builtins",
|
|
276
|
+
"name": "NoneType"
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
},
|
|
280
|
+
"has_default": true,
|
|
281
|
+
"default": null
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
"name": "params",
|
|
285
|
+
"kind": "var_keyword",
|
|
286
|
+
"role": "attribute",
|
|
287
|
+
"annotation": {
|
|
288
|
+
"kind": "any"
|
|
289
|
+
},
|
|
290
|
+
"has_default": false
|
|
291
|
+
}
|
|
292
|
+
],
|
|
293
|
+
"accepts_var_keyword": true,
|
|
294
|
+
"accepts_var_positional": false
|
|
295
|
+
},
|
|
296
|
+
"node_label": null,
|
|
297
|
+
"collection_key": null,
|
|
298
|
+
"_meta": {
|
|
299
|
+
"data_element": true
|
|
300
|
+
}
|
|
214
301
|
}
|
|
215
302
|
}
|
|
216
303
|
}
|
|
@@ -17,13 +17,13 @@ import BINDING from './binding.json' with {type: 'json'};
|
|
|
17
17
|
// P19: `data` is ambiguous between the legacy data-element and the HTML5 element.
|
|
18
18
|
const DATA_FORBIDDEN = 'data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element';
|
|
19
19
|
// Declarations outside Gramlot 0.2.0 that fail explicitly (source plan, section 2).
|
|
20
|
-
const EXCLUDED_ELEMENTS = new Set(['
|
|
20
|
+
const EXCLUDED_ELEMENTS = new Set(['dataremote']);
|
|
21
21
|
const EXCLUDED_ATTRIBUTES = new Set(['serverpath', 'dbenv', 'shared_id', 'remote', '_ask', 'ask']);
|
|
22
22
|
const EXCLUDED_ATTRIBUTE_PREFIXES = ['subscribe_', 'selfsubscribe_', 'formsubscribe_'];
|
|
23
23
|
|
|
24
24
|
// P18 and B8: the Data path of each data-element, which does not accept `?attr`,
|
|
25
25
|
// and its inline attribute, which excludes `func` on the same node, as `_if` does (Q11.1).
|
|
26
|
-
const DATA_PATH_ATTRIBUTES = {dataSetter: 'destination_path', dataFormula: 'result_path'};
|
|
26
|
+
const DATA_PATH_ATTRIBUTES = {dataSetter: 'destination_path', dataFormula: 'result_path', dataRpc: 'result_path'};
|
|
27
27
|
const DATA_INLINE_ATTRIBUTES = {dataFormula: 'formula', dataController: 'script'};
|
|
28
28
|
|
|
29
29
|
/** The P18 violation of a data-element's attributes, or null. */
|
package/src/builder/source.js
CHANGED
|
@@ -5,6 +5,7 @@ import {getSubtypeDict, setSubtypeDict} from '@genrojs/tytx';
|
|
|
5
5
|
const CODE_ATTRIBUTES = {
|
|
6
6
|
dataFormula: ['formula', '_if', '_else'],
|
|
7
7
|
dataController: ['script', '_if', '_else'],
|
|
8
|
+
dataRpc: ['_if', '_else', '_onCalling', '_onResult', '_onError'],
|
|
8
9
|
button: ['action'],
|
|
9
10
|
};
|
|
10
11
|
|
|
@@ -28,7 +29,8 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
28
29
|
|
|
29
30
|
/**
|
|
30
31
|
* The inline code attributes of this node as `[name, text]`, under the names InlineCompiler uses:
|
|
31
|
-
* `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of
|
|
32
|
+
* `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the three providers and the
|
|
33
|
+
* `_onCalling`/`_onResult`/`_onError` of a dataRpc, `action` of a
|
|
32
34
|
* button, every `connect_on<event>`, every `==` attribute (canonical name) and a `==` node value (`''`).
|
|
33
35
|
*/
|
|
34
36
|
codeAttributes() {
|
package/src/gramlot.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type {DomElement, DomDocument, DomEvent, DomWindow} from './dom.d.ts';
|
|
7
7
|
import type {Bag} from '@genrojs/bag';
|
|
8
|
-
import type {
|
|
8
|
+
import type {RpcTransport} from './handlers/rpc-handler.js';
|
|
9
9
|
import type {SourceHandler} from './handlers/source-handler.js';
|
|
10
10
|
import type {RpcHandler} from './handlers/rpc-handler.js';
|
|
11
11
|
import type {DomHandler} from './handlers/dom-handler.js';
|
|
@@ -15,20 +15,20 @@ import type {UtilitiesHandler} from './handlers/utilities-handler.js';
|
|
|
15
15
|
export interface GramlotOptions {
|
|
16
16
|
/** The id of the server page, as registered by the server. */
|
|
17
17
|
pageId?: string;
|
|
18
|
-
/** The URL of the
|
|
19
|
-
|
|
20
|
-
/** The URL of the remote Source endpoint. */
|
|
21
|
-
sourceUrl?: string;
|
|
18
|
+
/** The URL of the rpc endpoint that receives the envelopes. */
|
|
19
|
+
rpcUrl?: string;
|
|
22
20
|
/** The URL of the page close endpoint. */
|
|
23
21
|
closeUrl?: string;
|
|
24
22
|
/** The id of the root element that receives the page. */
|
|
25
23
|
rootId?: string;
|
|
24
|
+
/** The capabilities announced by the server in the bootstrap. */
|
|
25
|
+
capabilities?: string[];
|
|
26
26
|
/** The root element; when null it is looked up by `rootId`. */
|
|
27
27
|
element?: DomElement | null;
|
|
28
28
|
/** The document of the page. */
|
|
29
29
|
document?: DomDocument;
|
|
30
30
|
/** A transport, or `false` for an instance with no server transport. */
|
|
31
|
-
transport?:
|
|
31
|
+
transport?: RpcTransport | false | null;
|
|
32
32
|
/** Grammar collections loaded into the builder. */
|
|
33
33
|
collections?: unknown[];
|
|
34
34
|
}
|
|
@@ -40,6 +40,8 @@ export type GramlotState = 'ready' | 'loading' | 'started' | 'failed' | 'dispose
|
|
|
40
40
|
export class Gramlot {
|
|
41
41
|
/** The id of the server page. */
|
|
42
42
|
pageId: string | undefined;
|
|
43
|
+
/** The capabilities announced by the server in the bootstrap, frozen. */
|
|
44
|
+
capabilities: readonly string[];
|
|
43
45
|
/** The root group of the named logic of the page. */
|
|
44
46
|
logic: unknown;
|
|
45
47
|
/** The Source and every change of it. */
|
|
@@ -58,9 +60,9 @@ export class Gramlot {
|
|
|
58
60
|
abort: AbortController;
|
|
59
61
|
/** The pending `start` promise, while loading. */
|
|
60
62
|
loading?: Promise<Gramlot> | null;
|
|
61
|
-
/** The window whose `pagehide` disposes the page; set only with the default `
|
|
63
|
+
/** The window whose `pagehide` disposes the page; set only with the default `HttpTransport`. */
|
|
62
64
|
window?: DomWindow;
|
|
63
|
-
/** The `pagehide` listener that disposes the page with a beacon; set only with the default `
|
|
65
|
+
/** The `pagehide` listener that disposes the page with a beacon; set only with the default `HttpTransport`. */
|
|
64
66
|
pagehide?: (event: DomEvent) => void;
|
|
65
67
|
/** Create the page instance; the construction order is the one of the source plan. */
|
|
66
68
|
constructor(options?: GramlotOptions);
|
package/src/gramlot.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import {HttpTransport} from './http-transport.js';
|
|
8
8
|
import {SourceHandler} from './handlers/source-handler.js';
|
|
9
9
|
import {RpcHandler} from './handlers/rpc-handler.js';
|
|
10
10
|
import {DomHandler} from './handlers/dom-handler.js';
|
|
@@ -15,17 +15,18 @@ import {UtilitiesHandler} from './handlers/utilities-handler.js';
|
|
|
15
15
|
* `SourceHandler` creates the builder, the binding runtime and the renderer in the order of source plan §4.3.
|
|
16
16
|
*/
|
|
17
17
|
export class Gramlot {
|
|
18
|
-
constructor({pageId,
|
|
18
|
+
constructor({pageId, rpcUrl = '/gramlot/rpc', closeUrl = '/gramlot/close', rootId = 'gramlot-root', capabilities = [],
|
|
19
19
|
element = null, document = globalThis.document, transport = null, collections = []} = {}) {
|
|
20
20
|
this.pageId = pageId;
|
|
21
|
+
this.capabilities = Object.freeze([...capabilities]);
|
|
21
22
|
this.src = new SourceHandler(this, {collections, destination: element ?? document.getElementById(rootId)});
|
|
22
23
|
this.data = this.src.builder.data;
|
|
23
|
-
this.rpc = new RpcHandler(this, {
|
|
24
|
+
this.rpc = new RpcHandler(this, {rpcUrl, closeUrl, document, transport});
|
|
24
25
|
this.dom = new DomHandler(this);
|
|
25
26
|
this.utl = new UtilitiesHandler(this, document);
|
|
26
27
|
this.state = 'ready';
|
|
27
28
|
this.abort = new AbortController();
|
|
28
|
-
if (this.rpc.transport instanceof
|
|
29
|
+
if (this.rpc.transport instanceof HttpTransport) {
|
|
29
30
|
this.pagehide = event => { if (!event.persisted) this.dispose({beacon: true}); };
|
|
30
31
|
document.defaultView.addEventListener('pagehide', this.pagehide);
|
|
31
32
|
this.window = document.defaultView;
|
|
@@ -42,7 +43,7 @@ export class Gramlot {
|
|
|
42
43
|
}
|
|
43
44
|
async loadMain() {
|
|
44
45
|
try {
|
|
45
|
-
const wire = await this.rpc.
|
|
46
|
+
const wire = await this.rpc.call('source', 'main', {}, {signal: this.abort.signal});
|
|
46
47
|
if (this.state === 'disposed') throw new Error('Gramlot was disposed during main');
|
|
47
48
|
return this.src.mountMainSource(wire);
|
|
48
49
|
} catch (error) {
|
|
@@ -61,7 +62,7 @@ export class Gramlot {
|
|
|
61
62
|
try { this.src.binding.dispose(); }
|
|
62
63
|
finally { this.src.renderer.dispose(); }
|
|
63
64
|
} finally {
|
|
64
|
-
if (this.rpc.transport instanceof
|
|
65
|
+
if (this.rpc.transport instanceof HttpTransport) this.rpc.transport.close(this.pageId, {beacon});
|
|
65
66
|
this.rpc.transport?.dispose?.();
|
|
66
67
|
}
|
|
67
68
|
}
|
|
@@ -6,16 +6,53 @@
|
|
|
6
6
|
import type {SourceBagNode} from '@genrojs/builders';
|
|
7
7
|
import type {DomDocument} from '../dom.d.ts';
|
|
8
8
|
import type {Gramlot} from '../gramlot.js';
|
|
9
|
-
import type {
|
|
9
|
+
import type {HttpTransport} from '../http-transport.js';
|
|
10
10
|
import {Handler} from './handler.js';
|
|
11
11
|
|
|
12
|
+
/** The transport contract of `RpcHandler`: one envelope text in, one envelope text out. */
|
|
13
|
+
export interface RpcTransport {
|
|
14
|
+
/** Send the request envelope text; resolves with the response envelope text. */
|
|
15
|
+
call(text: string, signal?: AbortSignal): Promise<string>;
|
|
16
|
+
/** Ask the server to close the page. */
|
|
17
|
+
close?(pageId: string, options?: {beacon?: boolean}): void;
|
|
18
|
+
/** Release the transport on dispose. */
|
|
19
|
+
dispose?(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The error outcome of a response envelope. */
|
|
23
|
+
export interface RpcErrorOutcome {
|
|
24
|
+
/** The outcome code (`page_expired`, `not_found`, `not_authenticated`, `not_authorized`, `application_error`). */
|
|
25
|
+
code: string;
|
|
26
|
+
/** The class name of the server exception. */
|
|
27
|
+
name: string;
|
|
28
|
+
/** The message of the server exception. */
|
|
29
|
+
message: string;
|
|
30
|
+
/** The server traceback, with `GRAMLOT_DEV=DEBUG`. */
|
|
31
|
+
details?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** An error outcome answered by the server inside the response envelope. */
|
|
35
|
+
export class RpcError extends Error {
|
|
36
|
+
/** The outcome code. */
|
|
37
|
+
code: string;
|
|
38
|
+
/** The class name of the server exception. */
|
|
39
|
+
remoteName: string;
|
|
40
|
+
/** The server traceback, when the server sent it. */
|
|
41
|
+
details?: string;
|
|
42
|
+
/** Create the error from the `error` field of a response envelope. */
|
|
43
|
+
constructor(outcome: RpcErrorOutcome);
|
|
44
|
+
}
|
|
45
|
+
|
|
12
46
|
/** The exchange with the server. */
|
|
13
47
|
export class RpcHandler extends Handler {
|
|
14
48
|
/** The server transport, or null. */
|
|
15
|
-
transport:
|
|
49
|
+
transport: RpcTransport | null;
|
|
16
50
|
/** The remote Source requests in progress, by target node. */
|
|
17
51
|
remoteRequests: Map<SourceBagNode, {controller: AbortController}>;
|
|
18
|
-
/** Create the transport of `gramlot`: the given one, none for `false`, else
|
|
19
|
-
constructor(gramlot: Gramlot, options: {
|
|
20
|
-
transport?:
|
|
52
|
+
/** Create the transport of `gramlot`: the given one, none for `false`, else an `HttpTransport` on the URLs. */
|
|
53
|
+
constructor(gramlot: Gramlot, options: {rpcUrl: string; closeUrl: string; document: DomDocument;
|
|
54
|
+
transport?: RpcTransport | HttpTransport | false | null});
|
|
55
|
+
/** Send one envelope of `contentType` to `name` with `params`; resolves with the value or rejects with an `RpcError`. */
|
|
56
|
+
call(contentType: 'source' | 'data', name: string, params?: Record<string, unknown>,
|
|
57
|
+
options?: {signal?: AbortSignal}): Promise<unknown>;
|
|
21
58
|
}
|
|
@@ -4,15 +4,42 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
|
+
import {fromTytx, toTytx} from '@genrojs/tytx';
|
|
7
8
|
import {Handler} from './handler.js';
|
|
8
|
-
import {
|
|
9
|
+
import {HttpTransport} from '../http-transport.js';
|
|
10
|
+
|
|
11
|
+
/** A request id: 16 random bytes as 32 hex characters; getRandomValues, unlike randomUUID,
|
|
12
|
+
* exists outside secure contexts too. */
|
|
13
|
+
const requestId = () => Array.from(globalThis.crypto.getRandomValues(new Uint8Array(16)),
|
|
14
|
+
byte => byte.toString(16).padStart(2, '0')).join('');
|
|
15
|
+
|
|
16
|
+
/** An error outcome answered by the server inside the response envelope. */
|
|
17
|
+
export class RpcError extends Error {
|
|
18
|
+
name = 'RpcError';
|
|
19
|
+
constructor({code, name, message, details}) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.remoteName = name;
|
|
23
|
+
if (details !== undefined) this.details = details;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
9
26
|
|
|
10
27
|
/** The exchange with the server. */
|
|
11
28
|
export class RpcHandler extends Handler {
|
|
12
|
-
constructor(gramlot, {
|
|
29
|
+
constructor(gramlot, {rpcUrl, closeUrl, document, transport}) {
|
|
13
30
|
super(gramlot);
|
|
14
31
|
this.transport = transport === false ? null : transport ??
|
|
15
|
-
new
|
|
32
|
+
new HttpTransport(rpcUrl, {closeUrl, navigator: document.defaultView.navigator});
|
|
16
33
|
this.remoteRequests = new Map();
|
|
17
34
|
}
|
|
35
|
+
/** Send one envelope of `contentType` to `name` with `params`; resolves with the value or rejects with an RpcError. */
|
|
36
|
+
async call(contentType, name, params = {}, {signal} = {}) {
|
|
37
|
+
if (!this.transport?.call) throw new Error(`rpc '${name}' is unavailable without a server transport`);
|
|
38
|
+
const id = requestId();
|
|
39
|
+
const text = toTytx({id, pageId: this.gramlot.pageId, contentType, name, params});
|
|
40
|
+
const response = fromTytx(await this.transport.call(text, signal));
|
|
41
|
+
if (response.id !== id) throw new Error('rpc response id mismatch');
|
|
42
|
+
if (response.error) throw new RpcError(response.error);
|
|
43
|
+
return response.value;
|
|
44
|
+
}
|
|
18
45
|
}
|
|
@@ -67,6 +67,8 @@ export class SourceHandler extends Handler {
|
|
|
67
67
|
if (!(wire instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
|
|
68
68
|
return wire.bindBuilder(builder).activateCode();
|
|
69
69
|
}
|
|
70
|
+
// A source envelope decodes its value: anything but a SourceBag or a TYTX text is not a Source.
|
|
71
|
+
if (typeof wire !== 'string') throw new TypeError('Source value did not decode to SourceBag');
|
|
70
72
|
const source = sourceBagFromTytx(wire, builder);
|
|
71
73
|
if (!(source instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
|
|
72
74
|
return source.activateCode();
|
|
@@ -82,7 +84,7 @@ export class SourceHandler extends Handler {
|
|
|
82
84
|
*/
|
|
83
85
|
async remoteSource(target, method, params = {}) {
|
|
84
86
|
const rpc = this.gramlot.rpc;
|
|
85
|
-
if (!rpc.transport?.
|
|
87
|
+
if (!rpc.transport?.call) throw new Error('Remote Source is unavailable without a server transport');
|
|
86
88
|
const binding = this.binding.bindingFor(target);
|
|
87
89
|
if (this.gramlot.state !== 'started' || !binding) throw new Error('Remote Source target is not mounted');
|
|
88
90
|
rpc.remoteRequests.get(target)?.controller.abort();
|
|
@@ -93,7 +95,7 @@ export class SourceHandler extends Handler {
|
|
|
93
95
|
const current = () => this.gramlot.state !== 'disposed' && !request.controller.signal.aborted &&
|
|
94
96
|
rpc.remoteRequests.get(target) === request && !binding.closed;
|
|
95
97
|
try {
|
|
96
|
-
const wire = await rpc.
|
|
98
|
+
const wire = await rpc.call('source', method, params, {signal: request.controller.signal});
|
|
97
99
|
if (!current()) return false;
|
|
98
100
|
const prepared = this.prepareSource(wire, target.builder);
|
|
99
101
|
if (!current()) return false;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP realisation of the Gramlot envelope; application pages do not issue fetch calls.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {BeaconNavigator} from './dom.d.ts';
|
|
7
|
+
|
|
8
|
+
/** The fetch function used by the transport. */
|
|
9
|
+
export type Fetcher = (input: string, init?: RequestInit) => Promise<Response>;
|
|
10
|
+
|
|
11
|
+
/** The page-to-server transport over HTTP: sends the envelope to the rpc URL and closes the server page. */
|
|
12
|
+
export class HttpTransport {
|
|
13
|
+
/** The URL of the rpc endpoint. */
|
|
14
|
+
rpcUrl: string;
|
|
15
|
+
/** The function that sends the requests. */
|
|
16
|
+
fetcher: Fetcher;
|
|
17
|
+
/** The URL of the page close endpoint. */
|
|
18
|
+
closeUrl: string;
|
|
19
|
+
/** The navigator whose `sendBeacon` sends the close request on page hide. */
|
|
20
|
+
navigator: BeaconNavigator;
|
|
21
|
+
/** Create a transport on the rpc URL of the page. */
|
|
22
|
+
constructor(rpcUrl: string, options?: {fetcher?: Fetcher; closeUrl?: string; navigator?: BeaconNavigator});
|
|
23
|
+
/** POST the request envelope text; resolves with the response envelope text, rejects on a non-200 status. */
|
|
24
|
+
call(text: string, signal?: AbortSignal): Promise<string>;
|
|
25
|
+
/** Ask the server to close the page, with a beacon or a best-effort fetch; delivery failure is ignored. */
|
|
26
|
+
close(pageId: string, options?: {beacon?: boolean}): void;
|
|
27
|
+
}
|