@gramlot/gramlot 0.2.11 → 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 +394 -181
- package/dist/gramlot.min.js +42 -0
- package/package.json +5 -5
- package/src/binding/inline.js +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.d.ts +2 -2
- package/src/bootstrap.js +4 -4
- 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 +12 -10
- package/src/gramlot.js +7 -6
- package/src/handlers/rpc-handler.d.ts +44 -7
- package/src/handlers/rpc-handler.js +32 -5
- package/src/handlers/source-handler.d.ts +1 -1
- package/src/handlers/source-handler.js +5 -3
- package/src/http-transport.d.ts +27 -0
- package/src/{transport.js → http-transport.js} +11 -18
- package/src/index.d.ts +5 -3
- package/src/index.js +3 -2
- package/src/renderer/gramlot-renderer.js +1 -1
- package/src/server/assets.d.ts +12 -0
- package/src/server/assets.js +30 -0
- package/src/server/conformance.d.ts +9 -0
- package/src/server/conformance.js +187 -0
- package/src/{adapters/file-host.d.ts → server/gramlot-file-server.d.ts} +15 -7
- package/src/{adapters/file-host.js → server/gramlot-file-server.js} +26 -13
- package/src/{adapters/host.d.ts → server/gramlot-server.d.ts} +50 -27
- package/src/server/gramlot-server.js +208 -0
- package/src/server/index.d.ts +17 -0
- package/src/server/index.js +16 -0
- package/src/server/page.d.ts +46 -0
- package/src/server/page.js +76 -0
- package/src/{adapters → server}/resources.d.ts +3 -1
- package/src/{adapters → server}/resources.js +2 -2
- package/src/view/button.js +7 -7
- package/src/adapters/host.js +0 -137
- package/src/adapters/index.d.ts +0 -13
- package/src/adapters/index.js +0 -13
- package/src/adapters/page.d.ts +0 -36
- package/src/adapters/page.js +0 -43
- package/src/transport.d.ts +0 -34
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The neutral
|
|
2
|
+
* The neutral server: registers pages and builds their Sources, with no HTTP engine.
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
@@ -9,22 +9,47 @@ import type {Resources} from './resources.js';
|
|
|
9
9
|
export type {Resources};
|
|
10
10
|
|
|
11
11
|
/** The page is unknown, expired or owned by someone else. */
|
|
12
|
-
export class PageExpired extends Error {
|
|
12
|
+
export class PageExpired extends Error {
|
|
13
|
+
name: 'PageExpired';
|
|
14
|
+
}
|
|
13
15
|
/** No page exists for the requested path. */
|
|
14
|
-
export class PageNotFound extends Error {
|
|
16
|
+
export class PageNotFound extends Error {
|
|
17
|
+
name: 'PageNotFound';
|
|
18
|
+
}
|
|
15
19
|
/** An unknown remote Source method, as the Python `SourceNotFound`. */
|
|
16
|
-
export class SourceNotFound extends Error {
|
|
20
|
+
export class SourceNotFound extends Error {
|
|
21
|
+
name: 'SourceNotFound';
|
|
22
|
+
}
|
|
23
|
+
/** No endpoint of that name, as the Python `EndpointNotFound`. */
|
|
24
|
+
export class EndpointNotFound extends Error {
|
|
25
|
+
name: 'EndpointNotFound';
|
|
26
|
+
}
|
|
27
|
+
/** The `auth` evaluator answered `not_authenticated`. */
|
|
28
|
+
export class NotAuthenticated extends Error {
|
|
29
|
+
name: 'NotAuthenticated';
|
|
30
|
+
}
|
|
31
|
+
/** The `auth` evaluator answered `not_authorized`. */
|
|
32
|
+
export class NotAuthorized extends Error {
|
|
33
|
+
name: 'NotAuthorized';
|
|
34
|
+
}
|
|
35
|
+
/** A request envelope that is not `{id, pageId, contentType, name, params}`, as the Python `InvalidRequest`. */
|
|
36
|
+
export class InvalidRequest extends Error {
|
|
37
|
+
name: 'InvalidRequest';
|
|
38
|
+
}
|
|
17
39
|
/** The page registry is full. */
|
|
18
|
-
export class
|
|
40
|
+
export class ServerCapacity extends Error {
|
|
41
|
+
name: 'ServerCapacity';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The outcome of a refused `auth` rule, or null when the call may proceed. */
|
|
45
|
+
export type AuthOutcome = 'not_authenticated' | 'not_authorized' | null;
|
|
19
46
|
|
|
20
|
-
/** The options of a `
|
|
21
|
-
export interface
|
|
47
|
+
/** The options of a `GramlotServer`. */
|
|
48
|
+
export interface GramlotServerOptions {
|
|
22
49
|
/** URL of the runtime bundle. */
|
|
23
50
|
runtimeUrl?: string;
|
|
24
|
-
/** URL of the
|
|
25
|
-
|
|
26
|
-
/** URL of the remote Source endpoint. */
|
|
27
|
-
sourceUrl?: string;
|
|
51
|
+
/** URL of the request envelopes. */
|
|
52
|
+
rpcUrl?: string;
|
|
28
53
|
/** URL of the page close endpoint. */
|
|
29
54
|
closeUrl?: string;
|
|
30
55
|
/** Id of the root element of the page. */
|
|
@@ -56,16 +81,14 @@ export interface OpenedPage {
|
|
|
56
81
|
}
|
|
57
82
|
|
|
58
83
|
/**
|
|
59
|
-
* Neutral
|
|
84
|
+
* Neutral server: no Node or Bun imports and no server startup side effects. The core never
|
|
60
85
|
* searches files: override `resolvePage` and `resolveResources` in concrete integrations.
|
|
61
86
|
*/
|
|
62
|
-
export class
|
|
87
|
+
export class GramlotServer {
|
|
63
88
|
/** URL of the runtime bundle. */
|
|
64
89
|
runtimeUrl: string;
|
|
65
|
-
/** URL of the
|
|
66
|
-
|
|
67
|
-
/** URL of the remote Source endpoint. */
|
|
68
|
-
sourceUrl: string;
|
|
90
|
+
/** URL of the request envelopes. */
|
|
91
|
+
rpcUrl: string;
|
|
69
92
|
/** URL of the page close endpoint. */
|
|
70
93
|
closeUrl: string;
|
|
71
94
|
/** Id of the root element of the page. */
|
|
@@ -76,26 +99,26 @@ export class Host {
|
|
|
76
99
|
maxPages: number;
|
|
77
100
|
/** The registered pages by id. */
|
|
78
101
|
pages: Map<string, {PageClass: typeof Page; owner: unknown; expires: number}>;
|
|
79
|
-
/** Create a
|
|
80
|
-
constructor(options?:
|
|
102
|
+
/** Create a server. */
|
|
103
|
+
constructor(options?: GramlotServerOptions);
|
|
81
104
|
/** The Page class of `path`, or `PageNotFound`. */
|
|
82
105
|
resolvePage(path: string): Promise<typeof Page>;
|
|
83
106
|
/** The resources of the page at `path` in load order and without mount prefix, or `PageNotFound`. */
|
|
84
107
|
resolveResources(path: string, PageClass: typeof Page): Promise<Resources>;
|
|
108
|
+
/** The capabilities announced in the bootstrap; none in the base server. */
|
|
109
|
+
get capabilities(): string[];
|
|
110
|
+
/** null when a target with `rule` may run for `owner`; the base server refuses any rule. */
|
|
111
|
+
evaluateAuth(rule: string | null, options?: {owner?: unknown}): AuthOutcome;
|
|
85
112
|
/** Drop the expired pages. */
|
|
86
113
|
prune(): void;
|
|
87
114
|
/** Register a page independently of its HTTP or Worker transport. */
|
|
88
115
|
registerPage(path: string, options?: {owner?: unknown}): Promise<RegisteredPage>;
|
|
89
116
|
/** Register the page and return its bootstrap; `prefix` is the mount prefix added to root-relative URLs. */
|
|
90
117
|
openPage(path: string, options?: {owner?: unknown; prefix?: string}): Promise<OpenedPage>;
|
|
91
|
-
/**
|
|
92
|
-
|
|
93
|
-
/** A remote Source of a registered page, as TYTX text; `params` null or omitted is `{}`. */
|
|
94
|
-
source(pageId: string, method: string, params?: Record<string, unknown> | null,
|
|
95
|
-
options?: {owner?: unknown}): Promise<string>;
|
|
96
|
-
/** Build the Source of a page by calling its `main` or a registered Source method. */
|
|
97
|
-
buildSource(pageId: string, method: string | null, params: Record<string, unknown>,
|
|
98
|
-
owner: unknown): Promise<string>;
|
|
118
|
+
/** Answer the request envelope `text` (TYTX) with the response envelope (TYTX); throws only `InvalidRequest`. */
|
|
119
|
+
call(text: string, options?: {owner?: unknown}): Promise<string>;
|
|
99
120
|
/** Forget a registered page when `owner` owns it. */
|
|
100
121
|
closePage(pageId: string, options?: {owner?: unknown}): void;
|
|
122
|
+
/** Forget every registered page; adapters call it on shutdown. */
|
|
123
|
+
closeAll(): void;
|
|
101
124
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/* @ts-self-types="./gramlot-server.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The neutral server: registers pages and builds their Sources, with no HTTP engine.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import {fromTytx, toTytx} from '@genrojs/tytx';
|
|
8
|
+
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
9
|
+
import {gramlotDev} from './assets.js';
|
|
10
|
+
import {ENDPOINT_METHOD, Page, SOURCE_METHOD, endpointMethod, sourceMethod} from './page.js';
|
|
11
|
+
import {loadOrder} from './resources.js';
|
|
12
|
+
|
|
13
|
+
export class PageExpired extends Error { name = 'PageExpired'; }
|
|
14
|
+
export class PageNotFound extends Error { name = 'PageNotFound'; }
|
|
15
|
+
/** An unknown remote Source method, as the Python `SourceNotFound`. */
|
|
16
|
+
export class SourceNotFound extends Error { name = 'SourceNotFound'; }
|
|
17
|
+
export class EndpointNotFound extends Error { name = 'EndpointNotFound'; }
|
|
18
|
+
export class NotAuthenticated extends Error { name = 'NotAuthenticated'; }
|
|
19
|
+
export class NotAuthorized extends Error { name = 'NotAuthorized'; }
|
|
20
|
+
/** A request envelope that is not `{id, pageId, contentType, name, params}`, as the Python `InvalidRequest`. */
|
|
21
|
+
export class InvalidRequest extends Error { name = 'InvalidRequest'; }
|
|
22
|
+
export class ServerCapacity extends Error { name = 'ServerCapacity'; }
|
|
23
|
+
|
|
24
|
+
/** The single place the outcome codes are spelled; any other error is application_error. */
|
|
25
|
+
const OUTCOME_CODES = new Map([[PageExpired, 'page_expired'], [SourceNotFound, 'not_found'],
|
|
26
|
+
[EndpointNotFound, 'not_found'], [NotAuthenticated, 'not_authenticated'], [NotAuthorized, 'not_authorized']]);
|
|
27
|
+
|
|
28
|
+
const isMapping = value => value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype;
|
|
29
|
+
|
|
30
|
+
/** Decode and check the request envelope {id, pageId, contentType, name, params}. */
|
|
31
|
+
function parseRequest(text) {
|
|
32
|
+
let request;
|
|
33
|
+
try {
|
|
34
|
+
request = fromTytx(text);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new InvalidRequest('Request is not TYTX text', {cause: error});
|
|
37
|
+
}
|
|
38
|
+
if (!isMapping(request)) throw new InvalidRequest('Request must be a mapping');
|
|
39
|
+
for (const field of ['id', 'pageId', 'contentType', 'name']) {
|
|
40
|
+
if (typeof request[field] !== 'string') throw new InvalidRequest(`Request ${field} must be a string`);
|
|
41
|
+
}
|
|
42
|
+
if (!['source', 'data'].includes(request.contentType)) {
|
|
43
|
+
throw new InvalidRequest('Request contentType must be source or data');
|
|
44
|
+
}
|
|
45
|
+
if (!isMapping(request.params)) throw new InvalidRequest('Request params must be a mapping');
|
|
46
|
+
return request;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const scriptJson = value => JSON.stringify(value).replaceAll('<', '\\u003c');
|
|
50
|
+
|
|
51
|
+
/** 16 random bytes in URL-safe base64 without padding, as Python secrets.token_urlsafe(16). */
|
|
52
|
+
function createNonce() {
|
|
53
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
54
|
+
return btoa(String.fromCharCode(...bytes)).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Add the mount prefix once to a root-relative URL (/…, not //…); relative and
|
|
58
|
+
* absolute URLs stay as written. */
|
|
59
|
+
const prefixed = (prefix, url) => url.startsWith('/') && !url.startsWith('//') ? prefix + url : url;
|
|
60
|
+
|
|
61
|
+
/** Neutral server: no Node/Bun imports or server startup side effects. The core never
|
|
62
|
+
* searches files: override resolvePage and resolveResources in concrete integrations;
|
|
63
|
+
* GramlotFileServer implements them on one pages folder. Adapters supply request ownership.
|
|
64
|
+
*/
|
|
65
|
+
export class GramlotServer {
|
|
66
|
+
constructor({runtimeUrl = '/assets/gramlot.js', rpcUrl = '/gramlot/rpc', closeUrl = '/gramlot/close',
|
|
67
|
+
rootId = 'gramlot-root', pageTtl = 1800, maxPages = 1000} = {}) {
|
|
68
|
+
if (typeof pageTtl !== 'number' || pageTtl <= 0 ||
|
|
69
|
+
!Number.isFinite(performance.now() + pageTtl * 1000) ||
|
|
70
|
+
!Number.isSafeInteger(maxPages) || maxPages < 1) {
|
|
71
|
+
throw new TypeError('Page TTL must be finite and positive; capacity must be a positive safe integer');
|
|
72
|
+
}
|
|
73
|
+
Object.assign(this, {runtimeUrl, rpcUrl, closeUrl, rootId, pageTtl, maxPages});
|
|
74
|
+
this.pages = new Map();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The Page class of path, or PageNotFound. */
|
|
78
|
+
async resolvePage(path) { throw new PageNotFound(`Page not found: ${path}`); }
|
|
79
|
+
|
|
80
|
+
/** {css: [url], js: [{url, group}]} in load order and without mount prefix, or PageNotFound. */
|
|
81
|
+
async resolveResources(path, PageClass) { throw new PageNotFound(`Page resources not found: ${path}`); }
|
|
82
|
+
|
|
83
|
+
/** The capabilities announced in the bootstrap; none in the base server. */
|
|
84
|
+
get capabilities() {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** null when a target with `rule` may run for `owner`, else 'not_authenticated' or
|
|
89
|
+
* 'not_authorized'. The base server knows no identity: any rule is closed. */
|
|
90
|
+
evaluateAuth(rule, {owner = null} = {}) {
|
|
91
|
+
return rule === null ? null : 'not_authenticated';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
prune() {
|
|
95
|
+
const now = performance.now();
|
|
96
|
+
for (const [id, record] of this.pages) if (record.expires <= now) this.pages.delete(id);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Register a page independently of its HTTP or Worker transport. */
|
|
100
|
+
async registerPage(path, {owner = null} = {}) {
|
|
101
|
+
const PageClass = await this.resolvePage(path);
|
|
102
|
+
if (typeof PageClass !== 'function' || !(PageClass.prototype instanceof Page)) {
|
|
103
|
+
throw new TypeError('Page modules must export a subclass of Page');
|
|
104
|
+
}
|
|
105
|
+
const resources = loadOrder(await this.resolveResources(path, PageClass));
|
|
106
|
+
this.prune();
|
|
107
|
+
if (this.pages.size >= this.maxPages) throw new ServerCapacity('Page registry capacity reached');
|
|
108
|
+
const pageId = crypto.randomUUID().replaceAll('-', '');
|
|
109
|
+
this.pages.set(pageId, {PageClass, owner, expires: performance.now() + this.pageTtl * 1000});
|
|
110
|
+
return {pageId, title: PageClass.title, resources};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Register the page and return its bootstrap. prefix is the mount prefix chosen
|
|
114
|
+
* by the adapter, added once to the root-relative bootstrap URLs. */
|
|
115
|
+
async openPage(path, {owner = null, prefix = ''} = {}) {
|
|
116
|
+
if (typeof prefix !== 'string') throw new TypeError('Mount prefix must be a string');
|
|
117
|
+
const {pageId, title, resources} = await this.registerPage(path, {owner});
|
|
118
|
+
try {
|
|
119
|
+
const nonce = createNonce();
|
|
120
|
+
// The CSS links and the JS modules reach the page through PageBootstrap (§4.12, D9).
|
|
121
|
+
const bootstrap = scriptJson({
|
|
122
|
+
config: {pageId, rpcUrl: prefixed(prefix, this.rpcUrl), closeUrl: prefixed(prefix, this.closeUrl),
|
|
123
|
+
rootId: this.rootId, capabilities: this.capabilities},
|
|
124
|
+
resources: {css: resources.css.map(url => prefixed(prefix, url)),
|
|
125
|
+
js: resources.js.map(({url, group}) => ({url: prefixed(prefix, url), group}))},
|
|
126
|
+
});
|
|
127
|
+
const document = new GramlotBuilder();
|
|
128
|
+
const root = document.root.html();
|
|
129
|
+
const head = root.head();
|
|
130
|
+
head.meta({charset: 'utf-8'});
|
|
131
|
+
head.title(title);
|
|
132
|
+
// A page module served for its Logic imports the core from the runtime already loaded.
|
|
133
|
+
head.script(scriptJson({imports: {'@gramlot/gramlot/page': prefixed(prefix, this.runtimeUrl)}}),
|
|
134
|
+
{type: 'importmap', nonce});
|
|
135
|
+
const body = root.body();
|
|
136
|
+
body.div({id: this.rootId});
|
|
137
|
+
body.script(`import {PageBootstrap} from ${scriptJson(prefixed(prefix, this.runtimeUrl))};` +
|
|
138
|
+
`await new PageBootstrap(${bootstrap}).run();`,
|
|
139
|
+
{type: 'module', nonce});
|
|
140
|
+
const html = document.render({doctype: true});
|
|
141
|
+
return {pageId, html, nonce};
|
|
142
|
+
} catch (error) {
|
|
143
|
+
this.closePage(pageId, {owner});
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Answer the request envelope `text` (TYTX) with the response envelope (TYTX).
|
|
149
|
+
* Throws only InvalidRequest; every other failure is an outcome in the response,
|
|
150
|
+
* a value that TYTX cannot serialise included. */
|
|
151
|
+
async call(text, {owner = null} = {}) {
|
|
152
|
+
const request = parseRequest(text);
|
|
153
|
+
const response = {id: request.id, contentType: request.contentType};
|
|
154
|
+
try {
|
|
155
|
+
response.value = (await this.#run(request, owner)) ?? null;
|
|
156
|
+
return toTytx(response);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
delete response.value;
|
|
159
|
+
response.error = {code: OUTCOME_CODES.get(error?.constructor) ?? 'application_error',
|
|
160
|
+
name: error?.name ?? 'Error', message: error?.message ?? String(error)};
|
|
161
|
+
if (gramlotDev() === 'DEBUG') response.error.details = error?.stack;
|
|
162
|
+
}
|
|
163
|
+
return toTytx(response);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Run the fragment or endpoint of a checked request: the value of the response. */
|
|
167
|
+
async #run({pageId, contentType, name, params}, owner) {
|
|
168
|
+
this.prune();
|
|
169
|
+
const record = this.pages.get(pageId);
|
|
170
|
+
if (!record || record.owner !== owner) throw new PageExpired('Unknown, expired or unowned page');
|
|
171
|
+
const page = new record.PageClass();
|
|
172
|
+
page.pageId = pageId;
|
|
173
|
+
const source = contentType === 'source';
|
|
174
|
+
let callable = page.main;
|
|
175
|
+
let rule = null;
|
|
176
|
+
if (!source || name !== 'main') {
|
|
177
|
+
callable = (source ? sourceMethod : endpointMethod)(page, name);
|
|
178
|
+
if (!callable) {
|
|
179
|
+
throw source ? new SourceNotFound(`Unknown Source method: ${name}`)
|
|
180
|
+
: new EndpointNotFound(`Unknown endpoint: ${name}`);
|
|
181
|
+
}
|
|
182
|
+
rule = callable[source ? SOURCE_METHOD : ENDPOINT_METHOD].auth;
|
|
183
|
+
}
|
|
184
|
+
const refused = this.evaluateAuth(rule, {owner});
|
|
185
|
+
if (![null, 'not_authenticated', 'not_authorized'].includes(refused)) {
|
|
186
|
+
throw new TypeError("evaluateAuth must return null, 'not_authenticated' or 'not_authorized'");
|
|
187
|
+
}
|
|
188
|
+
if (refused !== null) {
|
|
189
|
+
const Refusal = [NotAuthenticated, NotAuthorized].find(ErrorClass => OUTCOME_CODES.get(ErrorClass) === refused);
|
|
190
|
+
throw new Refusal(`Access refused: ${name}`);
|
|
191
|
+
}
|
|
192
|
+
if (!source) return callable.call(page, params);
|
|
193
|
+
const builder = new record.PageClass.sourceBuilder(name);
|
|
194
|
+
const result = await callable.call(page, builder.root, params);
|
|
195
|
+
if (result !== undefined && result !== null) throw new TypeError('Source methods populate root and return no value');
|
|
196
|
+
return builder.toTytx();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
closePage(pageId, {owner = null} = {}) {
|
|
200
|
+
if (this.pages.get(pageId)?.owner === owner) this.pages.delete(pageId);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Forget every registered page; adapters call it on shutdown. */
|
|
204
|
+
closeAll() {
|
|
205
|
+
this.pages = new Map();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server entry point of Gramlot: the server-side `Page` and `GramlotServer` classes, the file-based
|
|
3
|
+
* reference server and the resource name helpers. It is not part of the browser entry point.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export {Page} from './page.js';
|
|
8
|
+
export {GramlotServer, EndpointNotFound, InvalidRequest, NotAuthenticated, NotAuthorized, PageExpired, PageNotFound,
|
|
9
|
+
SourceNotFound, ServerCapacity} from './gramlot-server.js';
|
|
10
|
+
export type {GramlotServerOptions, OpenedPage, RegisteredPage} from './gramlot-server.js';
|
|
11
|
+
export {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
12
|
+
export {GramlotFileServer} from './gramlot-file-server.js';
|
|
13
|
+
export type {GramlotFileServerOptions} from './gramlot-file-server.js';
|
|
14
|
+
export {gramlotDev, runtimeAsset} from './assets.js';
|
|
15
|
+
export {checkProtocol} from './conformance.js';
|
|
16
|
+
export {InvalidResourceName, parseRequires} from './resources.js';
|
|
17
|
+
export type {Resources} from './resources.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* @ts-self-types="./index.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* The server entry point of Gramlot: the server-side `Page` and `GramlotServer` classes, the file-based
|
|
4
|
+
* reference server and the resource name helpers. It is not part of the browser entry point.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
// Server entry point: deliberately not re-exported by the browser entry point.
|
|
9
|
+
export {Page} from './page.js';
|
|
10
|
+
export {GramlotServer, EndpointNotFound, InvalidRequest, NotAuthenticated, NotAuthorized, PageExpired, PageNotFound,
|
|
11
|
+
SourceNotFound, ServerCapacity} from './gramlot-server.js';
|
|
12
|
+
export {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
13
|
+
export {GramlotFileServer} from './gramlot-file-server.js';
|
|
14
|
+
export {gramlotDev, runtimeAsset} from './assets.js';
|
|
15
|
+
export {checkProtocol} from './conformance.js';
|
|
16
|
+
export {InvalidResourceName, parseRequires} from './resources.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side page base class and the `registerSource` and `registerEndpoint` markers.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
7
|
+
|
|
8
|
+
/** The marker of a Source method: `{auth}` on the method. */
|
|
9
|
+
export const SOURCE_METHOD: unique symbol;
|
|
10
|
+
/** The marker of an endpoint: `{auth}` on the method. */
|
|
11
|
+
export const ENDPOINT_METHOD: unique symbol;
|
|
12
|
+
|
|
13
|
+
/** The registered Source method `name` of `page`, or null. */
|
|
14
|
+
export function sourceMethod(page: Page, name: string): Function | null;
|
|
15
|
+
/** The registered endpoint `name` of `page`, or null. */
|
|
16
|
+
export function endpointMethod(page: Page, name: string): Function | null;
|
|
17
|
+
|
|
18
|
+
/** Server-side page base; unrelated to browser view components. */
|
|
19
|
+
export class Page {
|
|
20
|
+
/** The title of the page document. */
|
|
21
|
+
static title: string;
|
|
22
|
+
/** CSS URLs of the page. */
|
|
23
|
+
static css: string[];
|
|
24
|
+
/** Comma-separated names of the CSS resources of the page. */
|
|
25
|
+
static css_requires: string;
|
|
26
|
+
/** Comma-separated names of the JS resources of the page. */
|
|
27
|
+
static js_requires: string;
|
|
28
|
+
/** The builder class that authors the Source of the page. */
|
|
29
|
+
static sourceBuilder: typeof GramlotBuilder;
|
|
30
|
+
/** The id of the page, set by the server. */
|
|
31
|
+
pageId?: string;
|
|
32
|
+
/** Populate the `main` Source in `root`; subclasses must implement it. */
|
|
33
|
+
main(root: unknown): void | Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Mark the own method `methodName` of this page class as a remote Source method,
|
|
36
|
+
* equivalent to the Python `@source`; call it after the class. A Source method populates
|
|
37
|
+
* its `root` argument and returns nothing.
|
|
38
|
+
*
|
|
39
|
+
* Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
|
|
40
|
+
* they arrive together with the `remote` grammar attribute.
|
|
41
|
+
*/
|
|
42
|
+
static registerSource(methodName: string, options?: {auth?: string | null}): void;
|
|
43
|
+
/** Mark the own method `methodName` of this page class as an endpoint called with
|
|
44
|
+
* `contentType: 'data'`, equivalent to the Python `@endpoint`; call it after the class. */
|
|
45
|
+
static registerEndpoint(methodName: string, options?: {auth?: string | null}): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* @ts-self-types="./page.d.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* Server-side page base class and the `registerSource` and `registerEndpoint` markers.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
8
|
+
|
|
9
|
+
/** The marker of a Source method and of an endpoint: `{auth}` on the method, as the Python
|
|
10
|
+
* `__gramlot_source__` and `__gramlot_endpoint__` attributes. */
|
|
11
|
+
export const SOURCE_METHOD = Symbol('gramlot.source');
|
|
12
|
+
export const ENDPOINT_METHOD = Symbol('gramlot.endpoint');
|
|
13
|
+
const MARKERS = new Map([[SOURCE_METHOD, 'registerSource'], [ENDPOINT_METHOD, 'registerEndpoint']]);
|
|
14
|
+
|
|
15
|
+
function declaredMethod(page, name, marker) {
|
|
16
|
+
if (typeof name !== 'string' || !/^[A-Za-z][\w]*$/.test(name)) return null;
|
|
17
|
+
for (let proto = Object.getPrototypeOf(page); proto; proto = Object.getPrototypeOf(proto)) {
|
|
18
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, name);
|
|
19
|
+
if (descriptor) return descriptor.value?.[marker] ? descriptor.value : null;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function sourceMethod(page, name) {
|
|
25
|
+
return declaredMethod(page, name, SOURCE_METHOD);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function endpointMethod(page, name) {
|
|
29
|
+
return declaredMethod(page, name, ENDPOINT_METHOD);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function declare(PageClass, methodName, marker, auth) {
|
|
33
|
+
const register = MARKERS.get(marker);
|
|
34
|
+
if (methodName === 'main') throw new TypeError(`${register} cannot declare main`);
|
|
35
|
+
const method = Object.getOwnPropertyDescriptor(PageClass.prototype, methodName)?.value;
|
|
36
|
+
if (typeof method !== 'function') {
|
|
37
|
+
throw new TypeError(`${register} requires an own method of ${PageClass.name}: ${String(methodName)}`);
|
|
38
|
+
}
|
|
39
|
+
if (method[marker]) {
|
|
40
|
+
throw new TypeError(`${marker === SOURCE_METHOD ? 'Source method' : 'Endpoint'} already registered: ${methodName}`);
|
|
41
|
+
}
|
|
42
|
+
if (method[SOURCE_METHOD] || method[ENDPOINT_METHOD]) {
|
|
43
|
+
throw new TypeError(`${methodName} is declared both as Source method and endpoint`);
|
|
44
|
+
}
|
|
45
|
+
method[marker] = {auth};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Server-side page base; unrelated to browser view components. */
|
|
49
|
+
export class Page {
|
|
50
|
+
static title = 'Gramlot';
|
|
51
|
+
static css = [];
|
|
52
|
+
static css_requires = '';
|
|
53
|
+
static js_requires = '';
|
|
54
|
+
static sourceBuilder = GramlotBuilder;
|
|
55
|
+
|
|
56
|
+
main(root) {
|
|
57
|
+
throw new Error('Page.main(root) must be implemented');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Mark the own method `methodName` of this page class as a remote Source method,
|
|
62
|
+
* equivalent to Python `@source`; call it after the class.
|
|
63
|
+
*
|
|
64
|
+
* Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
|
|
65
|
+
* they arrive together with the `remote` grammar attribute.
|
|
66
|
+
*/
|
|
67
|
+
static registerSource(methodName, {auth = null} = {}) {
|
|
68
|
+
declare(this, methodName, SOURCE_METHOD, auth);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Mark the own method `methodName` of this page class as an endpoint called with
|
|
72
|
+
* `contentType: 'data'`, equivalent to Python `@endpoint`; call it after the class. */
|
|
73
|
+
static registerEndpoint(methodName, {auth = null} = {}) {
|
|
74
|
+
declare(this, methodName, ENDPOINT_METHOD, auth);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -11,7 +11,9 @@ export const SEGMENT: RegExp;
|
|
|
11
11
|
export const SPACES: string;
|
|
12
12
|
|
|
13
13
|
/** A page declares its resources in an invalid way. */
|
|
14
|
-
export class InvalidResourceName extends Error {
|
|
14
|
+
export class InvalidResourceName extends Error {
|
|
15
|
+
name: 'InvalidResourceName';
|
|
16
|
+
}
|
|
15
17
|
|
|
16
18
|
/** Parse `css_requires` or `js_requires`: comma-separated resource names, without duplicates. */
|
|
17
19
|
export function parseRequires(text: string): string[];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* @ts-self-types="./resources.d.ts" */
|
|
2
2
|
/** Resource names and bootstrap load order; no file lookup and no HTTP engine.
|
|
3
3
|
* The Python counterpart is src/gramlot/server/resources.py; both apply the same
|
|
4
|
-
* rules. css_requires/js_requires names are interpreted by a
|
|
4
|
+
* rules. css_requires/js_requires names are interpreted by a GramlotServer with a resource
|
|
5
5
|
* system, not by the core.
|
|
6
6
|
*
|
|
7
7
|
* @module
|
|
@@ -15,7 +15,7 @@ export const SPACES = '\u0009\u000a\u000b\u000c\u000d \u0085 ' +
|
|
|
15
15
|
'
';
|
|
16
16
|
|
|
17
17
|
/** A page declares its resources in an invalid way. */
|
|
18
|
-
export class InvalidResourceName extends Error {}
|
|
18
|
+
export class InvalidResourceName extends Error { name = 'InvalidResourceName'; }
|
|
19
19
|
|
|
20
20
|
function checkName(name) {
|
|
21
21
|
if (name.includes(':')) throw new InvalidResourceName(`Resource name "${name}": 'name:media' is not supported`);
|
package/src/view/button.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The click of a `<button>` (source plan §2 Button, §4.11, S12). One mechanism per button (P10): the
|
|
3
|
-
* `dataController` nested in it, `action`, or the `fire`/`fire_*` family. The nested
|
|
4
|
-
* normal ControllerProvider (B7); the click is one more trigger. Controller, `action` and counter use
|
|
3
|
+
* `dataController` or `dataRpc` nested in it, `action`, or the `fire`/`fire_*` family. The nested
|
|
4
|
+
* provider is a normal ControllerProvider or RpcProvider (B7); the click is one more trigger. Controller, `action` and counter use
|
|
5
5
|
* the button's own node, also when the click hits a descendant (R11).
|
|
6
6
|
*
|
|
7
7
|
* R3, provisional: only a button with a mechanism gets `type="button"` when the author wrote no
|
|
@@ -31,22 +31,22 @@ export class ButtonBinding {
|
|
|
31
31
|
get node() { return this.binding.node; }
|
|
32
32
|
get element() { return this.#record.element; }
|
|
33
33
|
|
|
34
|
-
/** The `dataController` children of the button, in Source order. */
|
|
34
|
+
/** The `dataController` and `dataRpc` children of the button, in Source order. */
|
|
35
35
|
get controllers() {
|
|
36
36
|
const value = this.node.value;
|
|
37
37
|
if (!value?.getNodes) return [];
|
|
38
|
-
return value.getNodes().filter(child => child.nodeTag === 'dataController');
|
|
38
|
+
return value.getNodes().filter(child => child.nodeTag === 'dataController' || child.nodeTag === 'dataRpc');
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController`
|
|
43
|
-
* children, or two mechanisms together, are an error naming the button (P10).
|
|
42
|
+
* 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController` or
|
|
43
|
+
* `dataRpc` children, or two mechanisms together, are an error naming the button (P10).
|
|
44
44
|
*/
|
|
45
45
|
get mechanism() {
|
|
46
46
|
const node = this.node;
|
|
47
47
|
const controllers = this.controllers;
|
|
48
48
|
if (controllers.length > 1) {
|
|
49
|
-
throw new Error(`${node.nodeTag} '${node.label}': several dataController children; a button has one click mechanism`);
|
|
49
|
+
throw new Error(`${node.nodeTag} '${node.label}': several dataController or dataRpc children; a button has one click mechanism`);
|
|
50
50
|
}
|
|
51
51
|
const attrs = node.getAttr() ?? {};
|
|
52
52
|
const found = [];
|