@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 CHANGED
@@ -6519,30 +6519,27 @@ var require_lib = __commonJS({
6519
6519
  }
6520
6520
  });
6521
6521
 
6522
- // src/transport.js
6523
- var MainTransport = class {
6524
- constructor(url, fetcher = globalThis.fetch?.bind(globalThis), sourceUrl = "/gramlot/source", closeUrl = "/gramlot/close", navigator = globalThis.navigator) {
6525
- this.url = url;
6522
+ // src/http-transport.js
6523
+ var HttpTransport = class {
6524
+ constructor(rpcUrl, {
6525
+ fetcher = globalThis.fetch?.bind(globalThis),
6526
+ closeUrl = "/gramlot/close",
6527
+ navigator = globalThis.navigator
6528
+ } = {}) {
6529
+ this.rpcUrl = rpcUrl;
6526
6530
  this.fetcher = fetcher;
6527
- this.sourceUrl = sourceUrl;
6528
6531
  this.closeUrl = closeUrl;
6529
6532
  this.navigator = navigator;
6530
6533
  }
6531
- async main(pageId, signal) {
6532
- return this.request("main", this.url, { pageId }, signal);
6533
- }
6534
- async source(pageId, method, params, signal) {
6535
- return this.request("source", this.sourceUrl, { pageId, method, params }, signal);
6536
- }
6537
- async request(operation, url, payload, signal) {
6538
- const response = await this.fetcher(url, {
6534
+ async call(text, signal) {
6535
+ const response = await this.fetcher(this.rpcUrl, {
6539
6536
  method: "POST",
6540
6537
  credentials: "same-origin",
6541
6538
  signal,
6542
6539
  headers: { "Content-Type": "application/json" },
6543
- body: JSON.stringify(payload)
6540
+ body: text
6544
6541
  });
6545
- if (!response.ok) throw new Error(`${operation} failed: HTTP ${response.status}`);
6542
+ if (response.status !== 200) throw new Error(`rpc failed: HTTP ${response.status}`);
6546
6543
  return response.text();
6547
6544
  }
6548
6545
  close(pageId, { beacon = false } = {}) {
@@ -21785,6 +21782,7 @@ var Handler = class {
21785
21782
  var CODE_ATTRIBUTES = {
21786
21783
  dataFormula: ["formula", "_if", "_else"],
21787
21784
  dataController: ["script", "_if", "_else"],
21785
+ dataRpc: ["_if", "_else", "_onCalling", "_onResult", "_onError"],
21788
21786
  button: ["action"]
21789
21787
  };
21790
21788
  function isExpression(value) {
@@ -21797,7 +21795,8 @@ var GramlotBuilderBagNode = class extends SourceBagNode {
21797
21795
  #activatedCode = null;
21798
21796
  /**
21799
21797
  * The inline code attributes of this node as `[name, text]`, under the names InlineCompiler uses:
21800
- * `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of both, `action` of a
21798
+ * `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the three providers and the
21799
+ * `_onCalling`/`_onResult`/`_onError` of a dataRpc, `action` of a
21801
21800
  * button, every `connect_on<event>`, every `==` attribute (canonical name) and a `==` node value (`''`).
21802
21801
  */
21803
21802
  codeAttributes() {
@@ -22558,16 +22557,103 @@ var binding_default = {
22558
22557
  _meta: {
22559
22558
  data_element: true
22560
22559
  }
22560
+ },
22561
+ dataRpc: {
22562
+ doc: "Server endpoint called through gramlot.rpc; the value is written at result_path when given.",
22563
+ sub_tags: "",
22564
+ parent_tags: null,
22565
+ inherits_from: null,
22566
+ ns: null,
22567
+ attributes: {
22568
+ parameters: [
22569
+ {
22570
+ name: "method",
22571
+ kind: "positional_or_keyword",
22572
+ role: "attribute",
22573
+ annotation: {
22574
+ kind: "type",
22575
+ module: "builtins",
22576
+ name: "str"
22577
+ },
22578
+ has_default: false
22579
+ },
22580
+ {
22581
+ name: "result_path",
22582
+ kind: "positional_or_keyword",
22583
+ role: "attribute",
22584
+ annotation: {
22585
+ kind: "union",
22586
+ items: [
22587
+ {
22588
+ kind: "type",
22589
+ module: "builtins",
22590
+ name: "str"
22591
+ },
22592
+ {
22593
+ kind: "type",
22594
+ module: "builtins",
22595
+ name: "NoneType"
22596
+ }
22597
+ ]
22598
+ },
22599
+ has_default: true,
22600
+ default: null
22601
+ },
22602
+ {
22603
+ name: "timeout",
22604
+ kind: "positional_or_keyword",
22605
+ role: "attribute",
22606
+ annotation: {
22607
+ kind: "union",
22608
+ items: [
22609
+ {
22610
+ kind: "type",
22611
+ module: "builtins",
22612
+ name: "int"
22613
+ },
22614
+ {
22615
+ kind: "type",
22616
+ module: "builtins",
22617
+ name: "float"
22618
+ },
22619
+ {
22620
+ kind: "type",
22621
+ module: "builtins",
22622
+ name: "NoneType"
22623
+ }
22624
+ ]
22625
+ },
22626
+ has_default: true,
22627
+ default: null
22628
+ },
22629
+ {
22630
+ name: "params",
22631
+ kind: "var_keyword",
22632
+ role: "attribute",
22633
+ annotation: {
22634
+ kind: "any"
22635
+ },
22636
+ has_default: false
22637
+ }
22638
+ ],
22639
+ accepts_var_keyword: true,
22640
+ accepts_var_positional: false
22641
+ },
22642
+ node_label: null,
22643
+ collection_key: null,
22644
+ _meta: {
22645
+ data_element: true
22646
+ }
22561
22647
  }
22562
22648
  }
22563
22649
  };
22564
22650
 
22565
22651
  // src/builder/gramlot-builder.js
22566
22652
  var DATA_FORBIDDEN = "data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element";
22567
- var EXCLUDED_ELEMENTS = /* @__PURE__ */ new Set(["datarpc", "dataremote"]);
22653
+ var EXCLUDED_ELEMENTS = /* @__PURE__ */ new Set(["dataremote"]);
22568
22654
  var EXCLUDED_ATTRIBUTES = /* @__PURE__ */ new Set(["serverpath", "dbenv", "shared_id", "remote", "_ask", "ask"]);
22569
22655
  var EXCLUDED_ATTRIBUTE_PREFIXES = ["subscribe_", "selfsubscribe_", "formsubscribe_"];
22570
- var DATA_PATH_ATTRIBUTES = { dataSetter: "destination_path", dataFormula: "result_path" };
22656
+ var DATA_PATH_ATTRIBUTES = { dataSetter: "destination_path", dataFormula: "result_path", dataRpc: "result_path" };
22571
22657
  var DATA_INLINE_ATTRIBUTES = { dataFormula: "formula", dataController: "script" };
22572
22658
  function dataElementError(tag2, attrs) {
22573
22659
  const pathName = DATA_PATH_ATTRIBUTES[tag2];
@@ -23306,21 +23392,21 @@ var ButtonBinding = class _ButtonBinding {
23306
23392
  get element() {
23307
23393
  return this.#record.element;
23308
23394
  }
23309
- /** The `dataController` children of the button, in Source order. */
23395
+ /** The `dataController` and `dataRpc` children of the button, in Source order. */
23310
23396
  get controllers() {
23311
23397
  const value = this.node.value;
23312
23398
  if (!value?.getNodes) return [];
23313
- return value.getNodes().filter((child) => child.nodeTag === "dataController");
23399
+ return value.getNodes().filter((child) => child.nodeTag === "dataController" || child.nodeTag === "dataRpc");
23314
23400
  }
23315
23401
  /**
23316
- * 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController`
23317
- * children, or two mechanisms together, are an error naming the button (P10).
23402
+ * 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController` or
23403
+ * `dataRpc` children, or two mechanisms together, are an error naming the button (P10).
23318
23404
  */
23319
23405
  get mechanism() {
23320
23406
  const node = this.node;
23321
23407
  const controllers = this.controllers;
23322
23408
  if (controllers.length > 1) {
23323
- throw new Error(`${node.nodeTag} '${node.label}': several dataController children; a button has one click mechanism`);
23409
+ throw new Error(`${node.nodeTag} '${node.label}': several dataController or dataRpc children; a button has one click mechanism`);
23324
23410
  }
23325
23411
  const attrs = node.getAttr() ?? {};
23326
23412
  const found = [];
@@ -23819,9 +23905,15 @@ function requireInstallable(branch) {
23819
23905
  }
23820
23906
  }
23821
23907
  var PROVIDER_ATTRIBUTES = ["_init", "_onStart", "_onBuilt", "_delay", "_timing", "_userChanges"];
23822
- var PROVIDER_TAGS = /* @__PURE__ */ new Set(["dataFormula", "dataController"]);
23908
+ var PROVIDER_TAGS = /* @__PURE__ */ new Set(["dataFormula", "dataController", "dataRpc"]);
23909
+ var RPC_ATTRIBUTES = ["_onCalling", "_onResult", "_onError"];
23823
23910
  var LIFECYCLE_ATTRIBUTES = ["_init", "_onBuilt", "_onStart"];
23824
23911
  function requireProviderAttributes(node) {
23912
+ const isSet = (name2) => node.getAttr(name2) !== null && node.getAttr(name2) !== void 0;
23913
+ if (node.nodeTag !== "dataRpc") {
23914
+ const callback = RPC_ATTRIBUTES.find(isSet);
23915
+ if (callback) throw new Error(`${node.nodeTag ?? "fragment"} '${node.label}': '${callback}' is allowed only on dataRpc`);
23916
+ }
23825
23917
  if (PROVIDER_TAGS.has(node.nodeTag)) {
23826
23918
  const expression = Object.entries(node.getAttr()).find(([, value]) => isExpression2(value));
23827
23919
  if (expression) {
@@ -23834,8 +23926,8 @@ function requireProviderAttributes(node) {
23834
23926
  }
23835
23927
  return;
23836
23928
  }
23837
- const name = PROVIDER_ATTRIBUTES.find((each) => node.getAttr(each) !== null && node.getAttr(each) !== void 0);
23838
- if (name) throw new Error(`${node.nodeTag ?? "fragment"} '${node.label}': '${name}' is allowed only on dataFormula and dataController`);
23929
+ const name = PROVIDER_ATTRIBUTES.find(isSet);
23930
+ if (name) throw new Error(`${node.nodeTag ?? "fragment"} '${node.label}': '${name}' is allowed only on dataFormula, dataController and dataRpc`);
23839
23931
  }
23840
23932
  function branchNodes(branch) {
23841
23933
  if (branch instanceof SourceBag) return branch.getNodes().flatMap((node) => branchNodes(node));
@@ -23858,7 +23950,12 @@ var CONTROL_ATTRIBUTES = /* @__PURE__ */ new Set([
23858
23950
  "_onBuilt",
23859
23951
  "_delay",
23860
23952
  "_timing",
23861
- "_userChanges"
23953
+ "_userChanges",
23954
+ "method",
23955
+ "timeout",
23956
+ "_onCalling",
23957
+ "_onResult",
23958
+ "_onError"
23862
23959
  ]);
23863
23960
  var Provider = class {
23864
23961
  #binding;
@@ -23866,6 +23963,8 @@ var Provider = class {
23866
23963
  #timingRegistration = null;
23867
23964
  #stopTiming = null;
23868
23965
  #cancelDelay = null;
23966
+ // The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
23967
+ #rpcCall = null;
23869
23968
  constructor(binding) {
23870
23969
  this.#binding = binding;
23871
23970
  binding.track(() => {
@@ -23879,9 +23978,9 @@ var Provider = class {
23879
23978
  get node() {
23880
23979
  return this.binding.node;
23881
23980
  }
23882
- /** 'formula' | 'controller'. */
23981
+ /** 'formula' | 'controller' | 'rpc'. */
23883
23982
  get kind() {
23884
- throw new Error("Provider.kind is defined by FormulaProvider and ControllerProvider");
23983
+ throw new Error("Provider.kind is defined by FormulaProvider, ControllerProvider and RpcProvider");
23885
23984
  }
23886
23985
  /**
23887
23986
  * Step 4 of §5.1, and every rebinding: the `^` pointers are registered again from the current
@@ -23930,19 +24029,26 @@ var Provider = class {
23930
24029
  * `_else` are inline (Q11.1): a false `_if` stops, or `_else` replaces the body (D5); an empty `_if`
23931
24030
  * is absent and an empty `_else` or body is none, as legacy. A body is
23932
24031
  * the inline `formula`/`script`, compiled by the page's InlineCompiler, or the named `func`. An
23933
- * error propagates to the caller (P12). A closed node runs nothing.
24032
+ * error propagates to the caller (P12). A closed node runs nothing. The body of a `dataRpc` is the
24033
+ * call: `invoke` returns its promise, and a false `_if` runs `_else` as inline code without calling.
23934
24034
  */
23935
24035
  invoke(trigger) {
23936
24036
  if (this.binding.closed) return;
23937
24037
  const node = this.node;
23938
24038
  const formula = this.kind === "formula";
24039
+ const rpc = this.kind === "rpc";
23939
24040
  const kwargs = this.readArguments(trigger);
23940
24041
  let [attr, body] = [formula ? "formula" : "script", node.getAttr(formula ? "formula" : "script")];
23941
24042
  const condition = node.getAttr("_if");
23942
24043
  if (condition && !this.#inline("_if", condition, `return (${condition})`, kwargs, false)) {
23943
24044
  [attr, body] = ["_else", node.getAttr("_else")];
23944
24045
  if (!body) return;
24046
+ if (rpc) {
24047
+ this.#inline(attr, body, body, kwargs, true);
24048
+ return;
24049
+ }
23945
24050
  }
24051
+ if (rpc) return this.#call(kwargs);
23946
24052
  const func = node.getAttr("func");
23947
24053
  let result;
23948
24054
  if (body) {
@@ -24031,6 +24137,56 @@ var Provider = class {
24031
24137
  ];
24032
24138
  return this.binding.runtime.inlineCompiler.compile(this.node, attr, code, names, source)({ ...kwargs, _kwargs: kwargs });
24033
24139
  }
24140
+ /**
24141
+ * The call of a `dataRpc`: `_onCalling`, then `gramlot.rpc.call('data', method, …)` with the author
24142
+ * arguments, then the value written at `result_path` when present and `_onResult`. The previous
24143
+ * call of the node is aborted and its answer dropped (latest wins); the close of the node aborts
24144
+ * the call too. `timeout` > 0 ms aborts it with a `TimeoutError`. A failure goes to `_onError`, or
24145
+ * rejects the returned promise when the node declares none.
24146
+ */
24147
+ async #call(kwargs) {
24148
+ const node = this.node;
24149
+ const [onCalling, onResult, onError] = ["_onCalling", "_onResult", "_onError"].map((name) => node.getAttr(name));
24150
+ if (onCalling) this.#inline("_onCalling", onCalling, onCalling, kwargs, true);
24151
+ this.#rpcCall?.abort();
24152
+ const controller = new AbortController();
24153
+ this.#rpcCall = controller;
24154
+ const release = this.binding.track(() => controller.abort());
24155
+ const timeout = this.#resolved("timeout");
24156
+ const timer = timeout > 0 ? setTimeout(() => {
24157
+ const error = new Error(`${node.nodeTag} '${node.label}': no answer within ${timeout} ms`);
24158
+ error.name = "TimeoutError";
24159
+ controller.abort(error);
24160
+ }, timeout) : null;
24161
+ const aborted = new Promise((resolve, reject) => {
24162
+ controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true });
24163
+ });
24164
+ let outcome;
24165
+ try {
24166
+ const call = this.binding.runtime.gramlot.rpc.call(
24167
+ "data",
24168
+ this.#resolved("method"),
24169
+ authorArguments(kwargs),
24170
+ { signal: controller.signal }
24171
+ );
24172
+ outcome = { result: await Promise.race([call, aborted]) };
24173
+ } catch (error) {
24174
+ outcome = { error };
24175
+ }
24176
+ clearTimeout(timer);
24177
+ release();
24178
+ const current = this.#rpcCall === controller && !this.binding.closed;
24179
+ if (this.#rpcCall === controller) this.#rpcCall = null;
24180
+ if (!current) return;
24181
+ if ("error" in outcome) {
24182
+ if (!onError) throw outcome.error;
24183
+ this.#inline("_onError", onError, onError, { ...kwargs, error: outcome.error }, true);
24184
+ return;
24185
+ }
24186
+ const { result } = outcome;
24187
+ if (node.getAttr("result_path") != null) node.setRelativeData(node.pathAttribute("result_path"), result);
24188
+ if (onResult) this.#inline("_onResult", onResult, onResult, { ...kwargs, result }, true);
24189
+ }
24034
24190
  #closeRegistrations() {
24035
24191
  for (const registration of this.#registrations.splice(0)) registration.close();
24036
24192
  this.#timingRegistration = null;
@@ -24046,6 +24202,11 @@ var ControllerProvider = class extends Provider {
24046
24202
  return "controller";
24047
24203
  }
24048
24204
  };
24205
+ var RpcProvider = class extends Provider {
24206
+ get kind() {
24207
+ return "rpc";
24208
+ }
24209
+ };
24049
24210
  var TRIGGER_FIELDS = ["_node", "_triggerpars", "_reason"];
24050
24211
  var CLICK_FIELDS = ["_evt", "button_counter", "button_shift", "button_ctrl", "button_alt", "button_meta"];
24051
24212
  function authorArguments(kwargs) {
@@ -24257,7 +24418,7 @@ ${text}`, { cause: error });
24257
24418
  }
24258
24419
 
24259
24420
  // src/binding/runtime.js
24260
- var PROVIDERS = { dataFormula: FormulaProvider, dataController: ControllerProvider };
24421
+ var PROVIDERS = { dataFormula: FormulaProvider, dataController: ControllerProvider, dataRpc: RpcProvider };
24261
24422
  var CONTEXT_ATTRIBUTES = ["datapath", "_anchor", "node_id", "form", "formId"];
24262
24423
  var BindingRuntime = class {
24263
24424
  #gramlot;
@@ -24606,7 +24767,7 @@ var NodeBinding = class {
24606
24767
  get registrations() {
24607
24768
  return [...this.#registrations];
24608
24769
  }
24609
- /** The Provider of a `dataFormula` or `dataController`, as a list of zero or one. */
24770
+ /** The Provider of a `dataFormula`, `dataController` or `dataRpc`, as a list of zero or one. */
24610
24771
  get providers() {
24611
24772
  return this.#provider ? [this.#provider] : [];
24612
24773
  }
@@ -25572,6 +25733,7 @@ var SourceHandler = class extends Handler {
25572
25733
  if (!(wire instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
25573
25734
  return wire.bindBuilder(builder).activateCode();
25574
25735
  }
25736
+ if (typeof wire !== "string") throw new TypeError("Source value did not decode to SourceBag");
25575
25737
  const source = sourceBagFromTytx(wire, builder);
25576
25738
  if (!(source instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
25577
25739
  return source.activateCode();
@@ -25586,7 +25748,7 @@ var SourceHandler = class extends Handler {
25586
25748
  */
25587
25749
  async remoteSource(target, method, params = {}) {
25588
25750
  const rpc = this.gramlot.rpc;
25589
- if (!rpc.transport?.source) throw new Error("Remote Source is unavailable without a server transport");
25751
+ if (!rpc.transport?.call) throw new Error("Remote Source is unavailable without a server transport");
25590
25752
  const binding = this.binding.bindingFor(target);
25591
25753
  if (this.gramlot.state !== "started" || !binding) throw new Error("Remote Source target is not mounted");
25592
25754
  rpc.remoteRequests.get(target)?.controller.abort();
@@ -25596,7 +25758,7 @@ var SourceHandler = class extends Handler {
25596
25758
  let applying = false;
25597
25759
  const current = () => this.gramlot.state !== "disposed" && !request.controller.signal.aborted && rpc.remoteRequests.get(target) === request && !binding.closed;
25598
25760
  try {
25599
- const wire = await rpc.transport.source(this.gramlot.pageId, method, params, request.controller.signal);
25761
+ const wire = await rpc.call("source", method, params, { signal: request.controller.signal });
25600
25762
  if (!current()) return false;
25601
25763
  const prepared = this.prepareSource(wire, target.builder);
25602
25764
  if (!current()) return false;
@@ -25616,12 +25778,35 @@ var SourceHandler = class extends Handler {
25616
25778
  };
25617
25779
 
25618
25780
  // src/handlers/rpc-handler.js
25781
+ var requestId = () => Array.from(
25782
+ globalThis.crypto.getRandomValues(new Uint8Array(16)),
25783
+ (byte) => byte.toString(16).padStart(2, "0")
25784
+ ).join("");
25785
+ var RpcError = class extends Error {
25786
+ name = "RpcError";
25787
+ constructor({ code, name, message, details }) {
25788
+ super(message);
25789
+ this.code = code;
25790
+ this.remoteName = name;
25791
+ if (details !== void 0) this.details = details;
25792
+ }
25793
+ };
25619
25794
  var RpcHandler = class extends Handler {
25620
- constructor(gramlot, { mainUrl, sourceUrl, closeUrl, document, transport }) {
25795
+ constructor(gramlot, { rpcUrl, closeUrl, document, transport }) {
25621
25796
  super(gramlot);
25622
- this.transport = transport === false ? null : transport ?? new MainTransport(mainUrl, void 0, sourceUrl, closeUrl, document.defaultView.navigator);
25797
+ this.transport = transport === false ? null : transport ?? new HttpTransport(rpcUrl, { closeUrl, navigator: document.defaultView.navigator });
25623
25798
  this.remoteRequests = /* @__PURE__ */ new Map();
25624
25799
  }
25800
+ /** Send one envelope of `contentType` to `name` with `params`; resolves with the value or rejects with an RpcError. */
25801
+ async call(contentType, name, params = {}, { signal } = {}) {
25802
+ if (!this.transport?.call) throw new Error(`rpc '${name}' is unavailable without a server transport`);
25803
+ const id = requestId();
25804
+ const text = toTytx({ id, pageId: this.gramlot.pageId, contentType, name, params });
25805
+ const response = fromTytx(await this.transport.call(text, signal));
25806
+ if (response.id !== id) throw new Error("rpc response id mismatch");
25807
+ if (response.error) throw new RpcError(response.error);
25808
+ return response.value;
25809
+ }
25625
25810
  };
25626
25811
 
25627
25812
  // src/handlers/dom-handler.js
@@ -25765,24 +25950,25 @@ var UtilitiesHandler = class extends Handler {
25765
25950
  var Gramlot = class {
25766
25951
  constructor({
25767
25952
  pageId,
25768
- mainUrl = "/gramlot/main",
25769
- sourceUrl = "/gramlot/source",
25953
+ rpcUrl = "/gramlot/rpc",
25770
25954
  closeUrl = "/gramlot/close",
25771
25955
  rootId = "gramlot-root",
25956
+ capabilities = [],
25772
25957
  element = null,
25773
25958
  document = globalThis.document,
25774
25959
  transport = null,
25775
25960
  collections = []
25776
25961
  } = {}) {
25777
25962
  this.pageId = pageId;
25963
+ this.capabilities = Object.freeze([...capabilities]);
25778
25964
  this.src = new SourceHandler(this, { collections, destination: element ?? document.getElementById(rootId) });
25779
25965
  this.data = this.src.builder.data;
25780
- this.rpc = new RpcHandler(this, { mainUrl, sourceUrl, closeUrl, document, transport });
25966
+ this.rpc = new RpcHandler(this, { rpcUrl, closeUrl, document, transport });
25781
25967
  this.dom = new DomHandler(this);
25782
25968
  this.utl = new UtilitiesHandler(this, document);
25783
25969
  this.state = "ready";
25784
25970
  this.abort = new AbortController();
25785
- if (this.rpc.transport instanceof MainTransport) {
25971
+ if (this.rpc.transport instanceof HttpTransport) {
25786
25972
  this.pagehide = (event) => {
25787
25973
  if (!event.persisted) this.dispose({ beacon: true });
25788
25974
  };
@@ -25803,7 +25989,7 @@ var Gramlot = class {
25803
25989
  }
25804
25990
  async loadMain() {
25805
25991
  try {
25806
- const wire = await this.rpc.transport.main(this.pageId, this.abort.signal);
25992
+ const wire = await this.rpc.call("source", "main", {}, { signal: this.abort.signal });
25807
25993
  if (this.state === "disposed") throw new Error("Gramlot was disposed during main");
25808
25994
  return this.src.mountMainSource(wire);
25809
25995
  } catch (error) {
@@ -25825,7 +26011,7 @@ var Gramlot = class {
25825
26011
  this.src.renderer.dispose();
25826
26012
  }
25827
26013
  } finally {
25828
- if (this.rpc.transport instanceof MainTransport) this.rpc.transport.close(this.pageId, { beacon });
26014
+ if (this.rpc.transport instanceof HttpTransport) this.rpc.transport.close(this.pageId, { beacon });
25829
26015
  this.rpc.transport?.dispose?.();
25830
26016
  }
25831
26017
  }
@@ -25886,13 +26072,30 @@ var PageBootstrap = class {
25886
26072
  }
25887
26073
  /** The close request of a page that never got its Gramlot instance: a beacon, as `Gramlot` sends on pagehide. */
25888
26074
  closePage() {
25889
- const { mainUrl, sourceUrl, closeUrl, pageId } = this.config;
25890
- new MainTransport(mainUrl, void 0, sourceUrl, closeUrl, this.document.defaultView.navigator).close(pageId, { beacon: true });
26075
+ const { rpcUrl, closeUrl, pageId } = this.config;
26076
+ new HttpTransport(rpcUrl, { closeUrl, navigator: this.document.defaultView.navigator }).close(pageId, { beacon: true });
25891
26077
  }
25892
26078
  };
25893
26079
 
25894
26080
  // src/server/page.js
25895
26081
  var SOURCE_METHOD = /* @__PURE__ */ Symbol("gramlot.source");
26082
+ var ENDPOINT_METHOD = /* @__PURE__ */ Symbol("gramlot.endpoint");
26083
+ var MARKERS = /* @__PURE__ */ new Map([[SOURCE_METHOD, "registerSource"], [ENDPOINT_METHOD, "registerEndpoint"]]);
26084
+ function declare(PageClass, methodName, marker, auth) {
26085
+ const register = MARKERS.get(marker);
26086
+ if (methodName === "main") throw new TypeError(`${register} cannot declare main`);
26087
+ const method = Object.getOwnPropertyDescriptor(PageClass.prototype, methodName)?.value;
26088
+ if (typeof method !== "function") {
26089
+ throw new TypeError(`${register} requires an own method of ${PageClass.name}: ${String(methodName)}`);
26090
+ }
26091
+ if (method[marker]) {
26092
+ throw new TypeError(`${marker === SOURCE_METHOD ? "Source method" : "Endpoint"} already registered: ${methodName}`);
26093
+ }
26094
+ if (method[SOURCE_METHOD] || method[ENDPOINT_METHOD]) {
26095
+ throw new TypeError(`${methodName} is declared both as Source method and endpoint`);
26096
+ }
26097
+ method[marker] = { auth };
26098
+ }
25896
26099
  var Page = class {
25897
26100
  static title = "Gramlot";
25898
26101
  static css = [];
@@ -25907,15 +26110,15 @@ var Page = class {
25907
26110
  * equivalent to Python `@source`; call it after the class.
25908
26111
  *
25909
26112
  * Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
25910
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
26113
+ * they arrive together with the `remote` grammar attribute.
25911
26114
  */
25912
- static registerSource(methodName) {
25913
- const method = Object.getOwnPropertyDescriptor(this.prototype, methodName)?.value;
25914
- if (typeof method !== "function") {
25915
- throw new TypeError(`registerSource requires an own method of ${this.name}: ${String(methodName)}`);
25916
- }
25917
- if (method[SOURCE_METHOD]) throw new TypeError(`Source method already registered: ${methodName}`);
25918
- method[SOURCE_METHOD] = true;
26115
+ static registerSource(methodName, { auth = null } = {}) {
26116
+ declare(this, methodName, SOURCE_METHOD, auth);
26117
+ }
26118
+ /** Mark the own method `methodName` of this page class as an endpoint called with
26119
+ * `contentType: 'data'`, equivalent to Python `@endpoint`; call it after the class. */
26120
+ static registerEndpoint(methodName, { auth = null } = {}) {
26121
+ declare(this, methodName, ENDPOINT_METHOD, auth);
25919
26122
  }
25920
26123
  };
25921
26124
  export {
@@ -25928,11 +26131,12 @@ export {
25928
26131
  GramlotRenderer,
25929
26132
  GramlotSvgRenderer,
25930
26133
  Handler,
26134
+ HttpTransport,
25931
26135
  InOut,
25932
- MainTransport,
25933
26136
  Page,
25934
26137
  PageBootstrap,
25935
26138
  References,
26139
+ RpcError,
25936
26140
  RpcHandler,
25937
26141
  SourceHandler,
25938
26142
  UtilitiesHandler