@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
package/src/adapters/host.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/* @ts-self-types="./host.d.ts" */
|
|
2
|
-
/**
|
|
3
|
-
* The neutral host: registers pages and builds their Sources, with no HTTP engine.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
8
|
-
import {Page, sourceMethod} from './page.js';
|
|
9
|
-
import {loadOrder} from './resources.js';
|
|
10
|
-
|
|
11
|
-
export class PageExpired extends Error {}
|
|
12
|
-
export class PageNotFound extends Error {}
|
|
13
|
-
/** An unknown remote Source method, as the Python `SourceNotFound`. */
|
|
14
|
-
export class SourceNotFound extends Error {}
|
|
15
|
-
export class HostCapacity extends Error {}
|
|
16
|
-
|
|
17
|
-
const scriptJson = value => JSON.stringify(value).replaceAll('<', '\\u003c');
|
|
18
|
-
|
|
19
|
-
/** 16 random bytes in URL-safe base64 without padding, as Python secrets.token_urlsafe(16). */
|
|
20
|
-
function createNonce() {
|
|
21
|
-
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
22
|
-
return btoa(String.fromCharCode(...bytes)).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Add the mount prefix once to a root-relative URL (/…, not //…); relative and
|
|
26
|
-
* absolute URLs stay as written. */
|
|
27
|
-
const prefixed = (prefix, url) => url.startsWith('/') && !url.startsWith('//') ? prefix + url : url;
|
|
28
|
-
|
|
29
|
-
/** Neutral host: no Node/Bun imports or server startup side effects. The core never
|
|
30
|
-
* searches files: override resolvePage and resolveResources in concrete integrations;
|
|
31
|
-
* FileHost implements them on one pages folder. Adapters supply request ownership.
|
|
32
|
-
*/
|
|
33
|
-
export class Host {
|
|
34
|
-
constructor({runtimeUrl = '/assets/gramlot.js', mainUrl = '/gramlot/main', sourceUrl = '/gramlot/source',
|
|
35
|
-
closeUrl = '/gramlot/close',
|
|
36
|
-
rootId = 'gramlot-root', pageTtl = 1800, maxPages = 1000} = {}) {
|
|
37
|
-
if (typeof pageTtl !== 'number' || pageTtl <= 0 ||
|
|
38
|
-
!Number.isFinite(performance.now() + pageTtl * 1000) ||
|
|
39
|
-
!Number.isSafeInteger(maxPages) || maxPages < 1) {
|
|
40
|
-
throw new TypeError('Page TTL must be finite and positive; capacity must be a positive safe integer');
|
|
41
|
-
}
|
|
42
|
-
Object.assign(this, {runtimeUrl, mainUrl, sourceUrl, closeUrl, rootId, pageTtl, maxPages});
|
|
43
|
-
this.pages = new Map();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The Page class of path, or PageNotFound. */
|
|
47
|
-
async resolvePage(path) { throw new PageNotFound(`Page not found: ${path}`); }
|
|
48
|
-
|
|
49
|
-
/** {css: [url], js: [{url, group}]} in load order and without mount prefix, or PageNotFound. */
|
|
50
|
-
async resolveResources(path, PageClass) { throw new PageNotFound(`Page resources not found: ${path}`); }
|
|
51
|
-
|
|
52
|
-
prune() {
|
|
53
|
-
const now = performance.now();
|
|
54
|
-
for (const [id, record] of this.pages) if (record.expires <= now) this.pages.delete(id);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Register a page independently of its HTTP or Worker transport. */
|
|
58
|
-
async registerPage(path, {owner = null} = {}) {
|
|
59
|
-
const PageClass = await this.resolvePage(path);
|
|
60
|
-
if (typeof PageClass !== 'function' || !(PageClass.prototype instanceof Page)) {
|
|
61
|
-
throw new TypeError('Page modules must export a subclass of Page');
|
|
62
|
-
}
|
|
63
|
-
const resources = loadOrder(await this.resolveResources(path, PageClass));
|
|
64
|
-
this.prune();
|
|
65
|
-
if (this.pages.size >= this.maxPages) throw new HostCapacity('Page registry capacity reached');
|
|
66
|
-
const pageId = crypto.randomUUID().replaceAll('-', '');
|
|
67
|
-
this.pages.set(pageId, {PageClass, owner, expires: performance.now() + this.pageTtl * 1000});
|
|
68
|
-
return {pageId, title: PageClass.title, resources};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Register the page and return its bootstrap. prefix is the mount prefix chosen
|
|
72
|
-
* by the adapter, added once to the root-relative bootstrap URLs. */
|
|
73
|
-
async openPage(path, {owner = null, prefix = ''} = {}) {
|
|
74
|
-
if (typeof prefix !== 'string') throw new TypeError('Mount prefix must be a string');
|
|
75
|
-
const {pageId, title, resources} = await this.registerPage(path, {owner});
|
|
76
|
-
try {
|
|
77
|
-
const nonce = createNonce();
|
|
78
|
-
// The CSS links and the JS modules reach the page through PageBootstrap (§4.12, D9).
|
|
79
|
-
const bootstrap = scriptJson({
|
|
80
|
-
config: {pageId, mainUrl: prefixed(prefix, this.mainUrl), sourceUrl: prefixed(prefix, this.sourceUrl),
|
|
81
|
-
closeUrl: prefixed(prefix, this.closeUrl), rootId: this.rootId},
|
|
82
|
-
resources: {css: resources.css.map(url => prefixed(prefix, url)),
|
|
83
|
-
js: resources.js.map(({url, group}) => ({url: prefixed(prefix, url), group}))},
|
|
84
|
-
});
|
|
85
|
-
const document = new GramlotBuilder();
|
|
86
|
-
const root = document.root.html();
|
|
87
|
-
const head = root.head();
|
|
88
|
-
head.meta({charset: 'utf-8'});
|
|
89
|
-
head.title(title);
|
|
90
|
-
// A page module served for its Logic imports the core from the runtime already loaded.
|
|
91
|
-
head.script(scriptJson({imports: {'@gramlot/gramlot/page': prefixed(prefix, this.runtimeUrl)}}),
|
|
92
|
-
{type: 'importmap', nonce});
|
|
93
|
-
const body = root.body();
|
|
94
|
-
body.div({id: this.rootId});
|
|
95
|
-
body.script(`import {PageBootstrap} from ${scriptJson(prefixed(prefix, this.runtimeUrl))};` +
|
|
96
|
-
`await new PageBootstrap(${bootstrap}).run();`,
|
|
97
|
-
{type: 'module', nonce});
|
|
98
|
-
const html = document.render({doctype: true});
|
|
99
|
-
return {pageId, html, nonce};
|
|
100
|
-
} catch (error) {
|
|
101
|
-
this.closePage(pageId, {owner});
|
|
102
|
-
throw error;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async main(pageId, {owner = null} = {}) {
|
|
107
|
-
return this.buildSource(pageId, null, {}, owner);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** A remote Source; `params` null or omitted is `{}`, as in Python (an adapter passes null when a request has none). */
|
|
111
|
-
async source(pageId, method, params = null, {owner = null} = {}) {
|
|
112
|
-
if (typeof method !== 'string' || method === 'main') throw new SourceNotFound('Unknown Source method');
|
|
113
|
-
if (params !== null && (typeof params !== 'object' || Array.isArray(params))) {
|
|
114
|
-
throw new TypeError('Source params must be an object');
|
|
115
|
-
}
|
|
116
|
-
return this.buildSource(pageId, method, params ?? {}, owner);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async buildSource(pageId, method, params, owner) {
|
|
120
|
-
this.prune();
|
|
121
|
-
const record = this.pages.get(pageId);
|
|
122
|
-
if (!record || record.owner !== owner) throw new PageExpired('Unknown, expired or unowned page');
|
|
123
|
-
const page = new record.PageClass();
|
|
124
|
-
page.pageId = pageId;
|
|
125
|
-
const callable = method === null ? page.main : sourceMethod(page, method);
|
|
126
|
-
if (!callable) throw new SourceNotFound(`Unknown Source method: ${method}`);
|
|
127
|
-
const builder = new record.PageClass.sourceBuilder();
|
|
128
|
-
const result = await callable.call(page, builder.root, params);
|
|
129
|
-
if (result !== undefined && result !== null) throw new TypeError('Source methods populate root and return no value');
|
|
130
|
-
return builder.toTytx();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
closePage(pageId, {owner = null} = {}) {
|
|
134
|
-
if (this.pages.get(pageId)?.owner === owner) this.pages.delete(pageId);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
}
|
package/src/adapters/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The server entry point of Gramlot: the host-side `Page` and `Host` classes, the file-based
|
|
3
|
-
* reference host and the resource name helpers. It is not part of the browser entry point.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
export {Page, source} from './page.js';
|
|
8
|
-
export {Host, PageExpired, PageNotFound, SourceNotFound, HostCapacity} from './host.js';
|
|
9
|
-
export type {HostOptions, OpenedPage, RegisteredPage} from './host.js';
|
|
10
|
-
export {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
11
|
-
export {FileHost} from './file-host.js';
|
|
12
|
-
export {InvalidResourceName, parseRequires} from './resources.js';
|
|
13
|
-
export type {Resources} from './resources.js';
|
package/src/adapters/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/* @ts-self-types="./index.d.ts" */
|
|
2
|
-
/**
|
|
3
|
-
* The server entry point of Gramlot: the host-side `Page` and `Host` classes, the file-based
|
|
4
|
-
* reference host 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, source} from './page.js';
|
|
10
|
-
export {Host, PageExpired, PageNotFound, SourceNotFound, HostCapacity} from './host.js';
|
|
11
|
-
export {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
12
|
-
export {FileHost} from './file-host.js';
|
|
13
|
-
export {InvalidResourceName, parseRequires} from './resources.js';
|
package/src/adapters/page.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Host-side page base class and the `source` registration.
|
|
3
|
-
*
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
import type {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Explicit registration of a remote Source method, equivalent to the Python `@source`.
|
|
10
|
-
* A Source method populates its `root` argument and returns nothing.
|
|
11
|
-
*
|
|
12
|
-
* Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
|
|
13
|
-
* they arrive together with the `remote` grammar attribute and `@endpoint`.
|
|
14
|
-
*/
|
|
15
|
-
export function source<T extends Function>(method: T): T;
|
|
16
|
-
|
|
17
|
-
/** The registered Source method `name` of `page`, or null. */
|
|
18
|
-
export function sourceMethod(page: Page, name: string): Function | null;
|
|
19
|
-
|
|
20
|
-
/** Host-side page base; unrelated to browser view components. */
|
|
21
|
-
export class Page {
|
|
22
|
-
/** The title of the page document. */
|
|
23
|
-
static title: string;
|
|
24
|
-
/** CSS URLs of the page. */
|
|
25
|
-
static css: string[];
|
|
26
|
-
/** Comma-separated names of the CSS resources of the page. */
|
|
27
|
-
static css_requires: string;
|
|
28
|
-
/** Comma-separated names of the JS resources of the page. */
|
|
29
|
-
static js_requires: string;
|
|
30
|
-
/** The builder class that authors the Source of the page. */
|
|
31
|
-
static sourceBuilder: typeof GramlotBuilder;
|
|
32
|
-
/** The id of the page, set by the host. */
|
|
33
|
-
pageId?: string;
|
|
34
|
-
/** Populate the `main` Source in `root`; subclasses must implement it. */
|
|
35
|
-
main(root: unknown): void | Promise<void>;
|
|
36
|
-
}
|
package/src/adapters/page.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/* @ts-self-types="./page.d.ts" */
|
|
2
|
-
/**
|
|
3
|
-
* Host-side page base class and the `source` registration.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
8
|
-
|
|
9
|
-
const SOURCE_METHOD = Symbol('gramlot.source');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Explicit method registration, equivalent to Python @source without syntax transforms.
|
|
13
|
-
*
|
|
14
|
-
* Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
|
|
15
|
-
* they arrive together with the `remote` grammar attribute and `@endpoint`.
|
|
16
|
-
*/
|
|
17
|
-
export function source(method) {
|
|
18
|
-
if (typeof method !== 'function') throw new TypeError('source requires a method');
|
|
19
|
-
method[SOURCE_METHOD] = true;
|
|
20
|
-
return method;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function sourceMethod(page, name) {
|
|
24
|
-
if (typeof name !== 'string' || !/^[A-Za-z][\w]*$/.test(name)) return null;
|
|
25
|
-
for (let proto = Object.getPrototypeOf(page); proto; proto = Object.getPrototypeOf(proto)) {
|
|
26
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, name);
|
|
27
|
-
if (descriptor) return descriptor.value?.[SOURCE_METHOD] ? descriptor.value : null;
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Host-side page base; unrelated to browser view components. */
|
|
33
|
-
export class Page {
|
|
34
|
-
static title = 'Gramlot';
|
|
35
|
-
static css = [];
|
|
36
|
-
static css_requires = '';
|
|
37
|
-
static js_requires = '';
|
|
38
|
-
static sourceBuilder = GramlotBuilder;
|
|
39
|
-
|
|
40
|
-
main(root) {
|
|
41
|
-
throw new Error('Page.main(root) must be implemented');
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/transport.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Main and remote Source transport; 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-host transport: loads `main` and remote Sources and closes the server page. */
|
|
12
|
-
export class MainTransport {
|
|
13
|
-
/** The URL of the `main` endpoint. */
|
|
14
|
-
url: string;
|
|
15
|
-
/** The function that sends the requests. */
|
|
16
|
-
fetcher: Fetcher;
|
|
17
|
-
/** The URL of the remote Source endpoint. */
|
|
18
|
-
sourceUrl: string;
|
|
19
|
-
/** The URL of the page close endpoint. */
|
|
20
|
-
closeUrl: string;
|
|
21
|
-
/** The navigator whose `sendBeacon` sends the close request on page hide. */
|
|
22
|
-
navigator: BeaconNavigator;
|
|
23
|
-
/** Create a transport with the endpoint URLs of the page. */
|
|
24
|
-
constructor(url: string, fetcher?: Fetcher, sourceUrl?: string, closeUrl?: string,
|
|
25
|
-
navigator?: BeaconNavigator);
|
|
26
|
-
/** Request the `main` Source of `pageId`; resolves with the TYTX text. */
|
|
27
|
-
main(pageId: string, signal?: AbortSignal): Promise<string>;
|
|
28
|
-
/** Request the remote Source `method` of `pageId` with `params`; resolves with the TYTX text. */
|
|
29
|
-
source(pageId: string, method: string, params: Record<string, unknown>, signal?: AbortSignal): Promise<string>;
|
|
30
|
-
/** POST a JSON payload to `url`; resolves with the response text, rejects on a non-2xx status. */
|
|
31
|
-
request(operation: string, url: string, payload: unknown, signal?: AbortSignal): Promise<string>;
|
|
32
|
-
/** Ask the host to close the page, with a beacon or a best-effort fetch; delivery failure is ignored. */
|
|
33
|
-
close(pageId: string, options?: {beacon?: boolean}): void;
|
|
34
|
-
}
|