trebol 0.1.0 → 0.1.1

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.
Files changed (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
@@ -0,0 +1,541 @@
1
+ // Network natives: HTTP with JSON built in, WebSocket and server-sent events.
2
+ //
3
+ // All of them are asynchronous by default: they return `await` and the task
4
+ // continues with the result, so from Trebol `data: fetch url` just reads.
5
+ // Failures (network, timeout, HTTP status >= 400) are access errors that
6
+ // `try` can catch. `cancel` on the task aborts the request in flight.
7
+ import { K, NONE, I64, native, value, string, stringOf, text, binary, makeError, throwError, form, spell, ctxFind, intern, parseJson, toJson, queryText, } from "../core/index.js";
8
+ import { schedulerOf, spawn, spawnHandler, makeHandle, handleOf } from "./scheduler.js";
9
+ import { cancellers } from "./natives.js";
10
+ // --- files -----------------------------------------------------------------------
11
+ //
12
+ // A file the user chose (an upload field) is a handle of kind 'file' over a
13
+ // Blob: Trebol never holds its bytes until it asks with read-file.
14
+ /** A handle for a file the user chose. */
15
+ export function fileHandle(file) {
16
+ return makeHandle('file', file);
17
+ }
18
+ native('read-file', '["The bytes of a file the user chose." file [handle!]]', (_t, f, v) => {
19
+ if (f.phase === 1)
20
+ return value(v);
21
+ f.phase = 1;
22
+ const file = handleOf(f.args[0], 'file');
23
+ if (file === null)
24
+ return throwError('script', 'bad-handle', 'read-file needs a file from an upload field');
25
+ const p = file.arrayBuffer().then(buf => binary([...new Uint8Array(buf)]), (e) => { throw makeError('access', 'read', `cannot read ${file instanceof File ? file.name : 'the file'}: ${e.message}`); });
26
+ return { k: 'await', p };
27
+ });
28
+ // The other way, `download` gives the user a file made by the program: the
29
+ // browser saves it with its usual dialog or into Downloads.
30
+ const TYPES = {
31
+ csv: 'text/csv', txt: 'text/plain', json: 'application/json', xml: 'application/xml', html: 'text/html',
32
+ trb: 'text/plain', md: 'text/markdown', svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg',
33
+ pdf: 'application/pdf', zip: 'application/zip',
34
+ };
35
+ /** Where `download` hands a file; false when this host has no browser. Tests point it elsewhere. */
36
+ export const saver = {
37
+ save: (name, blob) => {
38
+ const doc = globalThis.document;
39
+ if (doc === undefined)
40
+ return false;
41
+ const url = URL.createObjectURL(blob);
42
+ const a = doc.createElement('a');
43
+ a.href = url;
44
+ a.download = name;
45
+ a.style.display = 'none';
46
+ doc.body.append(a);
47
+ a.click();
48
+ a.remove();
49
+ // the browser reads the blob after the click returns
50
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
51
+ return true;
52
+ },
53
+ };
54
+ /**
55
+ * The file name a server gives in content-disposition: filename*=UTF-8''... (which
56
+ * carries accents) wins over filename=...; '' when there is none.
57
+ */
58
+ export function dispositionName(header) {
59
+ const star = /filename\*\s*=\s*([^']*)'[^']*'([^;]+)/i.exec(header);
60
+ if (star !== null) {
61
+ try {
62
+ const raw = star[2].trim();
63
+ if (/^utf-8$/i.test(star[1].trim()))
64
+ return decodeURIComponent(raw);
65
+ // ISO-8859-1: each escaped byte is its own character
66
+ return raw.replace(/%([0-9a-f]{2})/gi, (_m, h) => String.fromCharCode(parseInt(h, 16)));
67
+ }
68
+ catch { /* a bad escape: try the plain name */ }
69
+ }
70
+ const plain = /filename\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^;]+))/i.exec(header);
71
+ if (plain === null)
72
+ return '';
73
+ return plain[1] !== undefined ? plain[1].replace(/\\(.)/g, '$1') : plain[2].trim();
74
+ }
75
+ /** The field of an answer of fetch/with [full: true]; undefined when it has none. */
76
+ function answerField(o, name) {
77
+ const i = ctxFind(o.ctx, intern(name));
78
+ return i < 0 ? undefined : o.ctx.vals[i];
79
+ }
80
+ /** A header of an answer, by its name in lowercase as fetch gives them. */
81
+ function answerHeader(headers, name) {
82
+ if (headers === undefined || headers.t !== K.Map)
83
+ return '';
84
+ const d = headers.s.data;
85
+ for (let i = 0; i + 1 < d.length; i += 2) {
86
+ if (d[i].t === K.String && text(d[i]).toLowerCase() === name)
87
+ return form(d[i + 1]);
88
+ }
89
+ return '';
90
+ }
91
+ native('download', '["Gives the user a file to save: text (as UTF-8), bytes, or the answer of fetch/with [full: true], under the name the server gives." name [file! string!] data [string! binary! object!]]', (_t, f) => {
92
+ let name = text(f.args[0]).split('/').pop() ?? '';
93
+ let data = f.args[1];
94
+ let given = '';
95
+ let renamed = false;
96
+ if (data.t === K.Object) {
97
+ const status = answerField(data, 'status');
98
+ const headers = answerField(data, 'headers');
99
+ const body = answerField(data, 'body');
100
+ if (status === undefined || status.t !== K.Integer || body === undefined) {
101
+ return throwError('script', 'bad-answer', 'download needs an answer of fetch/with [full: true]');
102
+ }
103
+ const code = I64.toNumber(status.v);
104
+ if (code >= 400)
105
+ return throwError('access', 'http', `HTTP ${code}: no file to save`);
106
+ const server = dispositionName(answerHeader(headers, 'content-disposition')).split(/[/\\]/).pop() ?? '';
107
+ if (server !== '') {
108
+ name = server;
109
+ renamed = true;
110
+ }
111
+ given = answerHeader(headers, 'content-type');
112
+ data = body.t === K.Binary || body.t === K.String ? body : string(toJson(body));
113
+ }
114
+ if (name === '')
115
+ return throwError('script', 'bad-file', 'download needs a file name');
116
+ const ext = name.includes('.') ? name.slice(name.lastIndexOf('.') + 1).toLowerCase() : '';
117
+ const type = TYPES[ext] ?? 'application/octet-stream';
118
+ // a byte order mark lets spreadsheets read a CSV's accents as UTF-8
119
+ const body = data.t === K.Binary
120
+ ? new Uint8Array(data.s.data.slice(data.i))
121
+ : (ext === 'csv' ? '\ufeff' : '') + text(data);
122
+ const blob = new Blob([body], { type: given !== '' ? given : data.t === K.Binary ? type : `${type};charset=utf-8` });
123
+ if (!saver.save(name, blob))
124
+ return throwError('access', 'no-download', 'this host cannot give the user files');
125
+ // the name it was saved under, when the server gave one
126
+ return value(renamed ? stringOf(f.args[0].t === K.File ? K.File : K.String, name) : f.args[0]);
127
+ });
128
+ // --- HTTP ------------------------------------------------------------------------
129
+ function field(opts, name) {
130
+ if (opts === null)
131
+ return null;
132
+ const i = ctxFind(opts.ctx, intern(name));
133
+ const v = i < 0 ? undefined : opts.ctx.vals[i];
134
+ return v === undefined || v.t === K.None ? null : v;
135
+ }
136
+ /** A yes-or-no option: on unless it is missing, none or false. */
137
+ const isOn = (v) => v !== null && !(v.t === K.Logic && !v.v);
138
+ /** Pairs from a block [name value ...], a map or an object, as strings. */
139
+ function pairs(c) {
140
+ const out = [];
141
+ const name = (k) => (k.t >= K.Word && k.t <= K.Issue ? spell(k.sym) : form(k));
142
+ if (c.t === K.Block || c.t === K.Map) {
143
+ const d = c.s.data.slice(c.t === K.Block ? c.i : 0);
144
+ for (let i = 0; i + 1 < d.length; i += 2)
145
+ out.push([name(d[i]), form(d[i + 1])]);
146
+ }
147
+ else if (c.t === K.Object) {
148
+ c.ctx.keys.forEach((k, i) => out.push([spell(k), form(c.ctx.vals[i])]));
149
+ }
150
+ return out;
151
+ }
152
+ /** Turns a decoded body into a value: JSON when the server says so, text, or binary. */
153
+ function decodeBody(type, bytes) {
154
+ if (/json/i.test(type)) {
155
+ const r = parseJson(new TextDecoder().decode(bytes));
156
+ return typeof r === 'string' ? string(new TextDecoder().decode(bytes)) : r;
157
+ }
158
+ if (/^text\/|xml|javascript|x-www-form-urlencoded/i.test(type) || type === '') {
159
+ return string(new TextDecoder().decode(bytes));
160
+ }
161
+ return { t: K.Binary, s: { data: [...bytes] }, i: 0 };
162
+ }
163
+ function toSeconds(c) {
164
+ if (c.t === K.Time)
165
+ return c.v / 1e9;
166
+ if (c.t === K.Integer)
167
+ return I64.toNumber(c.v);
168
+ return c.v;
169
+ }
170
+ /**
171
+ * Sends a request with fetch. With progress, the body of the answer is read
172
+ * as it comes, so the bytes received can be told.
173
+ */
174
+ async function sendFetch(url, init, progress) {
175
+ const res = await fetch(url, init);
176
+ const headers = [];
177
+ res.headers.forEach((val, k) => headers.push([k, val]));
178
+ let bytes;
179
+ if (progress === null || res.body === null) {
180
+ bytes = new Uint8Array(await res.arrayBuffer());
181
+ }
182
+ else {
183
+ const size = Number(res.headers.get('content-length') ?? NaN);
184
+ const total = Number.isFinite(size) && res.headers.get('content-encoding') === null ? size : null;
185
+ const parts = [];
186
+ let done = 0;
187
+ progress(0, total);
188
+ for (const reader = res.body.getReader();;) {
189
+ const { value: part, done: end } = await reader.read();
190
+ if (end)
191
+ break;
192
+ parts.push(part);
193
+ done += part.length;
194
+ progress(done, total);
195
+ }
196
+ // at the end the total is known: what came
197
+ if (total === null)
198
+ progress(done, done);
199
+ bytes = new Uint8Array(done);
200
+ let at = 0;
201
+ for (const part of parts) {
202
+ bytes.set(part, at);
203
+ at += part.length;
204
+ }
205
+ }
206
+ return { status: res.status, statusText: res.statusText, ok: res.ok, headers, bytes };
207
+ }
208
+ /**
209
+ * Sends a request with XMLHttpRequest, the one way a browser tells how much
210
+ * of the body went up: first the bytes sent, then the bytes received.
211
+ */
212
+ function sendXhr(url, init, progress) {
213
+ return new Promise((resolve, reject) => {
214
+ const xhr = new XMLHttpRequest();
215
+ xhr.open(init.method ?? 'GET', url);
216
+ init.headers.forEach((val, k) => xhr.setRequestHeader(k, val));
217
+ xhr.withCredentials = init.credentials === 'include';
218
+ xhr.responseType = 'arraybuffer';
219
+ const tell = (e) => progress(e.loaded, e.lengthComputable ? e.total : null);
220
+ if (init.body !== undefined)
221
+ xhr.upload.onprogress = tell;
222
+ xhr.onprogress = tell;
223
+ xhr.onload = () => {
224
+ const headers = xhr.getAllResponseHeaders().trim().split(/[\r\n]+/).filter(l => l.includes(':'))
225
+ .map(l => [l.slice(0, l.indexOf(':')).trim().toLowerCase(), l.slice(l.indexOf(':') + 1).trim()]);
226
+ const ok = xhr.status >= 200 && xhr.status < 300;
227
+ const got = xhr.response.byteLength;
228
+ progress(got, got);
229
+ resolve({ status: xhr.status, statusText: xhr.statusText, ok, headers, bytes: new Uint8Array(xhr.response) });
230
+ };
231
+ xhr.onerror = () => reject(new Error('network error'));
232
+ xhr.onabort = () => reject(new Error('aborted'));
233
+ init.signal?.addEventListener('abort', () => xhr.abort());
234
+ xhr.send((init.body ?? null));
235
+ });
236
+ }
237
+ /**
238
+ * What runs as a request goes: a block, with done and total set, or a face,
239
+ * through the host's show-progress. Calls come at most every tenth of a
240
+ * second, and always at the end; `end` says the request is over.
241
+ */
242
+ function progressOf(t, target) {
243
+ const sch = schedulerOf(t);
244
+ let last = 0;
245
+ const run = (done, total) => {
246
+ if (target.t === K.Block) {
247
+ spawnHandler(sch, target, { done, total });
248
+ return;
249
+ }
250
+ // a host mezzanine: in user, where the host's Trebol code is loaded
251
+ const i = ctxFind(t.rt.user, intern('show-progress'));
252
+ const fn = i < 0 ? undefined : t.rt.user.vals[i];
253
+ if (fn !== undefined)
254
+ spawn(sch, { data: [fn, target, done, total] }, 0);
255
+ };
256
+ const count = (n) => ({ t: K.Integer, v: n });
257
+ return {
258
+ tell: (done, total) => {
259
+ const now = Date.now();
260
+ if (now - last < 100 && done !== total)
261
+ return;
262
+ last = now;
263
+ run(count(done), total === null ? NONE : count(total));
264
+ },
265
+ end: () => { if (target.t !== K.Block)
266
+ run(NONE, NONE); },
267
+ };
268
+ }
269
+ native('http-request', '["Sends an HTTP request; used by fetch." url [url! string!] options [object! none!]]', (t, f, v) => {
270
+ if (f.phase === 1)
271
+ return value(v);
272
+ f.phase = 1;
273
+ const opts = f.args[1].t === K.Object ? f.args[1] : null;
274
+ let url = text(f.args[0]);
275
+ const q = field(opts, 'query');
276
+ if (q !== null) {
277
+ const qs = queryText(q);
278
+ if (qs === null)
279
+ return throwError('script', 'bad-query', 'a query takes words or strings as names');
280
+ const hash = url.includes('#') ? url.slice(url.indexOf('#')) : '';
281
+ const base = url.slice(0, url.length - hash.length);
282
+ if (qs !== '')
283
+ url = base + (base.includes('?') ? '&' : '?') + qs + hash;
284
+ }
285
+ const method = field(opts, 'method');
286
+ const headers = new Headers();
287
+ const h = field(opts, 'headers');
288
+ if (h !== null)
289
+ for (const [k, val] of pairs(h))
290
+ headers.set(k, val);
291
+ let body;
292
+ const b = field(opts, 'body');
293
+ const formFields = field(opts, 'form');
294
+ const multipart = field(opts, 'multipart');
295
+ if (formFields !== null) {
296
+ body = new URLSearchParams(pairs(formFields));
297
+ }
298
+ else if (multipart !== null) {
299
+ const fd = new FormData();
300
+ const d = multipart.t === K.Block ? multipart.s.data.slice(multipart.i) : [];
301
+ for (let i = 0; i + 1 < d.length; i += 2) {
302
+ const k = d[i];
303
+ const val = d[i + 1];
304
+ const key = k.t >= K.Word && k.t <= K.Issue ? spell(k.sym) : form(k);
305
+ const file = handleOf(val, 'file');
306
+ if (val.t === K.Binary)
307
+ fd.append(key, new Blob([new Uint8Array(val.s.data.slice(val.i))]), key);
308
+ else if (file !== null)
309
+ fd.append(key, file, file instanceof File ? file.name : key);
310
+ else
311
+ fd.append(key, form(val));
312
+ }
313
+ body = fd;
314
+ }
315
+ else if (b !== null) {
316
+ if (b.t === K.String) {
317
+ body = text(b);
318
+ }
319
+ else if (b.t === K.Binary) {
320
+ body = new Uint8Array(b.s.data.slice(b.i));
321
+ }
322
+ else {
323
+ body = toJson(b);
324
+ if (!headers.has('content-type'))
325
+ headers.set('content-type', 'application/json');
326
+ }
327
+ }
328
+ if (!headers.has('accept'))
329
+ headers.set('accept', 'application/json, text/*;q=0.9, */*;q=0.8');
330
+ const timeout = field(opts, 'timeout');
331
+ const full = isOn(field(opts, 'full'));
332
+ // without cookies the browser's own rule: sent to the page's own server only
333
+ const cookies = field(opts, 'cookies');
334
+ const credentials = cookies === null ? 'same-origin' : isOn(cookies) ? 'include' : 'omit';
335
+ const ctl = new AbortController();
336
+ let timedOut = false;
337
+ const timer = timeout !== null ? setTimeout(() => { timedOut = true; ctl.abort(); }, toSeconds(timeout) * 1000) : null;
338
+ const me = schedulerOf(t).byTask.get(t);
339
+ if (me)
340
+ me.abort = () => ctl.abort();
341
+ const verb = method === null ? (body === undefined ? 'GET' : 'POST') : form(method).toUpperCase();
342
+ const watch = field(opts, 'progress');
343
+ const shown = watch === null ? null : progressOf(t, watch);
344
+ const p = (async () => {
345
+ try {
346
+ const init = { method: verb, headers, body, credentials, signal: ctl.signal };
347
+ const res = shown !== null && typeof XMLHttpRequest !== 'undefined'
348
+ ? await sendXhr(url, init, shown.tell)
349
+ : await sendFetch(url, init, shown === null ? null : shown.tell);
350
+ const type = res.headers.find(([k]) => k === 'content-type')?.[1] ?? '';
351
+ const data = decodeBody(type, res.bytes);
352
+ if (full) {
353
+ const hs = [];
354
+ for (const [k, val] of res.headers)
355
+ hs.push(string(k), string(val));
356
+ return {
357
+ t: K.Object,
358
+ ctx: {
359
+ keys: ['status', 'headers', 'body'].map(n => intern(n)),
360
+ vals: [{ t: K.Integer, v: res.status }, { t: K.Map, s: { data: hs } }, data],
361
+ parent: null,
362
+ },
363
+ };
364
+ }
365
+ if (!res.ok)
366
+ throw makeError('access', 'http', `HTTP ${res.status} ${res.statusText} for ${url}`);
367
+ return data;
368
+ }
369
+ catch (e) {
370
+ if (e.t === K.Error)
371
+ throw e;
372
+ if (timedOut)
373
+ throw makeError('access', 'timeout', `request to ${url} timed out`);
374
+ if (ctl.signal.aborted)
375
+ throw makeError('access', 'cancelled', `request to ${url} was cancelled`);
376
+ throw makeError('access', 'network', `cannot reach ${url}: ${e.message}`);
377
+ }
378
+ finally {
379
+ if (timer !== null)
380
+ clearTimeout(timer);
381
+ shown?.end();
382
+ }
383
+ })();
384
+ return { k: 'await', p };
385
+ });
386
+ /** A message as a value: JSON objects and arrays load, anything else stays text. */
387
+ function messageValue(data) {
388
+ const s = data.trimStart();
389
+ if (s.startsWith('{') || s.startsWith('[')) {
390
+ const r = parseJson(data);
391
+ if (typeof r !== 'string')
392
+ return r;
393
+ }
394
+ return string(data);
395
+ }
396
+ function emit(st, kind, words) {
397
+ const bodies = st.handlers[kind];
398
+ if (bodies === undefined) {
399
+ st.pending.push([kind, words]);
400
+ return;
401
+ }
402
+ for (const body of bodies)
403
+ spawnHandler(st.sch, body, words);
404
+ }
405
+ native('open-socket', '["Opens a WebSocket; gives a handle once it is open." url [url! string!]]', (t, f, v) => {
406
+ if (f.phase === 1)
407
+ return value(v);
408
+ f.phase = 1;
409
+ const url = text(f.args[0]);
410
+ const sch = schedulerOf(t);
411
+ const p = new Promise((resolve, reject) => {
412
+ let ws;
413
+ try {
414
+ ws = new WebSocket(url);
415
+ }
416
+ catch (e) {
417
+ reject(makeError('access', 'network', `cannot open ${url}: ${e.message}`));
418
+ return;
419
+ }
420
+ ws.binaryType = 'arraybuffer';
421
+ const st = {
422
+ sch,
423
+ handlers: {},
424
+ pending: [],
425
+ close: () => ws.close(),
426
+ send: data => ws.send(data),
427
+ };
428
+ let opened = false;
429
+ ws.onopen = () => {
430
+ opened = true;
431
+ resolve(makeHandle('stream', st));
432
+ };
433
+ ws.onmessage = ev => {
434
+ const val = typeof ev.data === 'string'
435
+ ? messageValue(ev.data)
436
+ : { t: K.Binary, s: { data: [...new Uint8Array(ev.data)] }, i: 0 };
437
+ emit(st, 'message', { value: val });
438
+ };
439
+ ws.onerror = () => {
440
+ if (!opened)
441
+ reject(makeError('access', 'network', `cannot open ${url}`));
442
+ else
443
+ emit(st, 'error', { value: makeError('access', 'network', `socket error on ${url}`) });
444
+ };
445
+ ws.onclose = ev => {
446
+ if (opened)
447
+ emit(st, 'close', { value: { t: K.Integer, v: ev.code } });
448
+ };
449
+ });
450
+ return { k: 'await', p };
451
+ });
452
+ /** Splits a server-sent events stream into events. */
453
+ function parseSse(buffer, onEvent) {
454
+ let rest = buffer;
455
+ for (;;) {
456
+ const m = /\r?\n\r?\n/.exec(rest);
457
+ if (m === null)
458
+ return rest;
459
+ const block = rest.slice(0, m.index);
460
+ rest = rest.slice(m.index + m[0].length);
461
+ let event = 'message';
462
+ const data = [];
463
+ for (const line of block.split(/\r?\n/)) {
464
+ if (line.startsWith(':'))
465
+ continue;
466
+ const colon = line.indexOf(':');
467
+ const name = colon < 0 ? line : line.slice(0, colon);
468
+ const val = colon < 0 ? '' : line.slice(colon + 1).replace(/^ /, '');
469
+ if (name === 'event')
470
+ event = val;
471
+ else if (name === 'data')
472
+ data.push(val);
473
+ }
474
+ if (data.length > 0)
475
+ onEvent(event, data.join('\n'));
476
+ }
477
+ }
478
+ native('open-events', '["Opens a stream of server-sent events; gives a handle." url [url! string!]]', (t, f) => {
479
+ const url = text(f.args[0]);
480
+ const sch = schedulerOf(t);
481
+ const ctl = new AbortController();
482
+ const st = { sch, handlers: {}, pending: [], close: () => ctl.abort() };
483
+ const handle = makeHandle('stream', st);
484
+ void (async () => {
485
+ try {
486
+ const res = await fetch(url, { headers: { accept: 'text/event-stream' }, signal: ctl.signal });
487
+ if (!res.ok || res.body === null)
488
+ throw new Error(`HTTP ${res.status}`);
489
+ const reader = res.body.getReader();
490
+ const dec = new TextDecoder();
491
+ let buf = '';
492
+ for (;;) {
493
+ const { done, value: chunk } = await reader.read();
494
+ if (done)
495
+ break;
496
+ buf = parseSse(buf + dec.decode(chunk, { stream: true }), (event, data) => {
497
+ emit(st, 'message', { value: messageValue(data), event: { t: K.Word, sym: intern(event), bind: null } });
498
+ });
499
+ }
500
+ emit(st, 'close', { value: NONE });
501
+ }
502
+ catch (e) {
503
+ if (!ctl.signal.aborted)
504
+ emit(st, 'error', { value: makeError('access', 'network', `event stream ${url}: ${e.message}`) });
505
+ }
506
+ })();
507
+ return value(handle);
508
+ });
509
+ function onEvent(kind) {
510
+ return (_t, f) => {
511
+ const st = handleOf(f.args[0], 'stream');
512
+ if (st === null)
513
+ return throwError('script', 'bad-handle', `on-${kind} needs a socket or event stream`);
514
+ (st.handlers[kind] ??= []).push(f.args[1]);
515
+ const waiting = st.pending.filter(([k]) => k === kind);
516
+ st.pending = st.pending.filter(([k]) => k !== kind);
517
+ for (const [, words] of waiting)
518
+ emit(st, kind, words);
519
+ return value(f.args[0]);
520
+ };
521
+ }
522
+ native('on-message', '["Runs a block for each message that comes on a stream." stream [handle!] body [block!]]', onEvent('message'));
523
+ native('on-close', '["Runs a block when a stream closes." stream [handle!] body [block!]]', onEvent('close'));
524
+ native('on-error', '["Runs a block when a stream fails." stream [handle!] body [block!]]', onEvent('error'));
525
+ native('send', '["Sends a value on a socket." stream [handle!] data [any-value!]]', (_t, f) => {
526
+ const st = handleOf(f.args[0], 'stream');
527
+ if (st === null || st.send === undefined) {
528
+ return throwError('script', 'bad-handle', 'send needs an open socket');
529
+ }
530
+ const d = f.args[1];
531
+ st.send(d.t === K.String ? text(d) : d.t === K.Binary ? new Uint8Array(d.s.data.slice(d.i)) : toJson(d));
532
+ return value(f.args[0]);
533
+ });
534
+ cancellers['stream'] = obj => obj.close();
535
+ const closeStream = (_t, f) => {
536
+ handleOf(f.args[0], 'stream')?.close();
537
+ return value(f.args[0]);
538
+ };
539
+ native('close', '["Closes a stream, or hides a dialog." stream [handle!]]', closeStream);
540
+ // the UI dialect redefines close for dialogs and falls back to this one
541
+ native('close-stream', '["Closes a socket or a stream of events." stream [handle!]]', closeStream);
@@ -0,0 +1,9 @@
1
+ /** Where the page natives reach the browser. Tests and the console point them elsewhere. */
2
+ export declare const page: {
3
+ /** Opens an address in a new tab; false when this host cannot. */
4
+ open: (url: string) => boolean;
5
+ /** Puts text on the clipboard; null when this host has no clipboard. */
6
+ writeClipboard: (s: string) => Promise<boolean> | null;
7
+ /** The text on the clipboard, or null when there is none or the user refused; null at once when this host has no clipboard. */
8
+ readClipboard: () => Promise<string | null> | null;
9
+ };
@@ -0,0 +1,55 @@
1
+ // What a program asks of the browser around the page: open another address in
2
+ // a new tab, and put text on the clipboard or take it from there, as R3's
3
+ // browse and Red's write-clipboard / read-clipboard.
4
+ //
5
+ // They go through `page`, so the console can open the system's browser and
6
+ // the tests can watch what was asked; a host without them gives an access error.
7
+ import { NONE, UNSET, native, value, string, text, logic, throwError } from "../core/index.js";
8
+ const nav = () => globalThis.navigator;
9
+ /** Where the page natives reach the browser. Tests and the console point them elsewhere. */
10
+ export const page = {
11
+ /** Opens an address in a new tab; false when this host cannot. */
12
+ open: (url) => {
13
+ const win = globalThis.window;
14
+ if (win === undefined)
15
+ return false;
16
+ // with noopener window.open gives null, so the new tab is cut off by hand
17
+ const tab = win.open(url, '_blank');
18
+ if (tab !== null)
19
+ tab.opener = null;
20
+ return true;
21
+ },
22
+ /** Puts text on the clipboard; null when this host has no clipboard. */
23
+ writeClipboard: (s) => {
24
+ const c = nav()?.clipboard;
25
+ return c === undefined ? null : c.writeText(s).then(() => true, () => false);
26
+ },
27
+ /** The text on the clipboard, or null when there is none or the user refused; null at once when this host has no clipboard. */
28
+ readClipboard: () => {
29
+ const c = nav()?.clipboard;
30
+ return c === undefined ? null : c.readText().then(s => s, () => null);
31
+ },
32
+ };
33
+ native('browse', '["Opens a web address in a new tab." url [url! file!]]', (_t, f) => {
34
+ if (!page.open(text(f.args[0])))
35
+ return throwError('access', 'no-browse', 'this host cannot open web addresses');
36
+ return value(UNSET);
37
+ });
38
+ native('write-clipboard', '["Puts text on the clipboard; true when it is there." data [string!]]', (_t, f, v) => {
39
+ if (f.phase === 1)
40
+ return value(v);
41
+ f.phase = 1;
42
+ const p = page.writeClipboard(text(f.args[0]));
43
+ if (p === null)
44
+ return throwError('access', 'no-clipboard', 'this host has no clipboard');
45
+ return { k: 'await', p: p.then(logic) };
46
+ });
47
+ native('read-clipboard', '["The text on the clipboard, or none; the browser may ask the user first."]', (_t, f, v) => {
48
+ if (f.phase === 1)
49
+ return value(v);
50
+ f.phase = 1;
51
+ const p = page.readClipboard();
52
+ if (p === null)
53
+ return throwError('access', 'no-clipboard', 'this host has no clipboard');
54
+ return { k: 'await', p: p.then((s) => s === null || s === '' ? NONE : string(s)) };
55
+ });
@@ -0,0 +1,15 @@
1
+ import { type Cell, type Sym } from '../core/index.ts';
2
+ /** A field of a map! or object! row; map keys may be words or strings. */
3
+ export declare function rowField(row: Cell, sym: Sym): Cell;
4
+ /**
5
+ * A row of data that heads or closes a group: [head 1 "Title"], whose title may
6
+ * be a text per language, or [foot 1 #(amount: ...)], whose row is shown as the
7
+ * totals are. The level is 1 or 2; 2 is indented.
8
+ */
9
+ export declare function groupRow(c: Cell): {
10
+ kind: 'head' | 'foot';
11
+ level: 1 | 2;
12
+ value: Cell;
13
+ } | null;
14
+ /** Sorts the rows between the heads and feet of groups, each run on its own; those stay where they are. */
15
+ export declare function sortGroups(rows: Cell[], col: Sym, desc: boolean, language: string, shows: (v: Cell) => string): Cell[];