@celljs/ta-host 4.15.0 → 4.15.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.
@@ -17,6 +17,7 @@ const core_1 = require("@celljs/core");
17
17
  const ta_exec_1 = require("@celljs/ta-exec");
18
18
  const ta_protocol_1 = require("@celljs/ta-protocol");
19
19
  const node_1 = require("@celljs/web/lib/node");
20
+ const png_thumbnail_1 = require("./png-thumbnail");
20
21
  const ta_access_1 = require("./ta-access");
21
22
  const workspace_protocol_1 = require("./workspace-protocol");
22
23
  const workspace_util_1 = require("./workspace-util");
@@ -68,6 +69,13 @@ const TYPES = {
68
69
  */
69
70
  const SANDBOX = 'sandbox allow-scripts allow-forms allow-popups allow-modals allow-downloads';
70
71
  const ID_BYTES = 16;
72
+ /** The smallest and largest sides of the box a smaller picture is asked for. */
73
+ const THUMB_MIN = 16;
74
+ const THUMB_MAX = 4096;
75
+ /** Smaller pictures kept, made once for every page that shows them, up to this many bytes. */
76
+ const THUMB_CACHE_BYTES = 32 * 1024 * 1024;
77
+ /** Pictures larger than this are sent as they are, never read whole to make a smaller one. */
78
+ const THUMB_SOURCE_BYTES = 48 * 1024 * 1024;
71
79
  /** Read whole from another machine: a part of a larger file is read with `tail` and `head`. */
72
80
  const REMOTE_WHOLE_BYTES = 8 * 1024 * 1024;
73
81
  var WorkspaceFilesUtil;
@@ -113,6 +121,25 @@ var WorkspaceFilesUtil;
113
121
  return start <= end && start < size ? { start, end } : false;
114
122
  }
115
123
  WorkspaceFilesUtil.range = range;
124
+ /**
125
+ * The box of `?w=<px>&h=<px>&fit=cover`, which asks for a smaller picture, as the page shows one it has not
126
+ * enlarged (contain without `fit`); undefined for a URL without both sides, or sides out of bounds.
127
+ */
128
+ function thumbnail(url) {
129
+ const query = (url !== null && url !== void 0 ? url : '').split('#')[0].split('?')[1];
130
+ if (!query) {
131
+ return undefined;
132
+ }
133
+ const params = new URLSearchParams(query);
134
+ const side = (name) => {
135
+ const value = Number(params.get(name));
136
+ return Number.isInteger(value) && value >= THUMB_MIN && value <= THUMB_MAX ? value : undefined;
137
+ };
138
+ const width = side('w');
139
+ const height = side('h');
140
+ return width && height ? { width, height, fit: params.get('fit') === 'cover' ? 'cover' : 'contain' } : undefined;
141
+ }
142
+ WorkspaceFilesUtil.thumbnail = thumbnail;
116
143
  function etag(stat) {
117
144
  return `"${stat.size.toString(36)}-${Math.floor(stat.mtimeMs).toString(36)}"`;
118
145
  }
@@ -136,6 +163,11 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
136
163
  */
137
164
  this.directories = new Map();
138
165
  this.ids = new Map();
166
+ /** Smaller pictures made, by file, version and box, the last used last; `null` for one sent as it is. */
167
+ this.thumbnails = new Map();
168
+ this.thumbnailBytes = 0;
169
+ /** Made one at a time: each takes the event loop in turns, and many at once would hold many large pictures. */
170
+ this.making = Promise.resolve();
139
171
  }
140
172
  url(cwd, path) {
141
173
  const absolute = (0, node_path_1.resolve)(cwd, path || '.');
@@ -196,6 +228,10 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
196
228
  response.end();
197
229
  return;
198
230
  }
231
+ const box = type === 'image/png' && !request.headers.range && stat.size <= THUMB_SOURCE_BYTES ? WorkspaceFilesUtil.thumbnail(request.url) : undefined;
232
+ if (box && (await this.sendThumbnail(request, response, env, absolute, etag, box))) {
233
+ return;
234
+ }
199
235
  const range = WorkspaceFilesUtil.range(request.headers.range, stat.size);
200
236
  if (range === false) {
201
237
  response.setHeader('Content-Range', `bytes */${stat.size}`);
@@ -219,6 +255,56 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
219
255
  response.end(await this.read(env, absolute, stat.size, start, end));
220
256
  }
221
257
  }
258
+ /**
259
+ * A smaller picture of a PNG for `box` (PngThumbnail), with an ETag of its own; false when the picture itself is
260
+ * to be sent (not much larger than the box, or a PNG it does not read).
261
+ */
262
+ async sendThumbnail(request, response, env, absolute, etag, box) {
263
+ var _a;
264
+ const own = `${etag.slice(0, -1)}-${box.width}x${box.height}${box.fit === 'cover' ? 'c' : ''}"`;
265
+ // Made from this version of the file before: what the page has is still right.
266
+ if (request.headers['if-none-match'] === own) {
267
+ response.setHeader('ETag', own);
268
+ response.removeHeader('Accept-Ranges');
269
+ response.statusCode = 304;
270
+ response.end();
271
+ return true;
272
+ }
273
+ const key = `${absolute}\0${own}`;
274
+ let made = this.thumbnails.get(key);
275
+ if (made === undefined) {
276
+ const making = this.making.then(async () => png_thumbnail_1.PngThumbnail.make(await env.readFile(absolute), box).catch(() => undefined));
277
+ this.making = making.catch(() => undefined);
278
+ made = (_a = (await making.catch(() => undefined))) !== null && _a !== void 0 ? _a : null;
279
+ this.keepThumbnail(key, made);
280
+ }
281
+ else {
282
+ this.thumbnails.delete(key);
283
+ this.thumbnails.set(key, made);
284
+ }
285
+ if (!made) {
286
+ return false;
287
+ }
288
+ response.setHeader('ETag', own);
289
+ response.setHeader('Content-Type', made.type);
290
+ response.removeHeader('Accept-Ranges');
291
+ response.statusCode = 200;
292
+ response.setHeader('Content-Length', made.bytes.length);
293
+ response.end(request.method === 'HEAD' ? undefined : made.bytes);
294
+ return true;
295
+ }
296
+ keepThumbnail(key, made) {
297
+ var _a, _b;
298
+ this.thumbnails.set(key, made);
299
+ this.thumbnailBytes += (_a = made === null || made === void 0 ? void 0 : made.bytes.length) !== null && _a !== void 0 ? _a : 0;
300
+ for (const [old, bytes] of this.thumbnails) {
301
+ if (this.thumbnailBytes <= THUMB_CACHE_BYTES && this.thumbnails.size <= 4096) {
302
+ break;
303
+ }
304
+ this.thumbnails.delete(old);
305
+ this.thumbnailBytes -= (_b = bytes === null || bytes === void 0 ? void 0 : bytes.bytes.length) !== null && _b !== void 0 ? _b : 0;
306
+ }
307
+ }
222
308
  /** The file at `path` of `root`, or the `index.html` of the directory there. */
223
309
  async find(env, root, path) {
224
310
  let absolute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-host",
3
- "version": "4.15.0",
3
+ "version": "4.15.1",
4
4
  "description": "ta serve: pi sessions for many clients over WebSocket",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -34,15 +34,15 @@
34
34
  "watch": "tsc -b -w --preserveWatchOutput"
35
35
  },
36
36
  "dependencies": {
37
- "@celljs/ai-agent": "4.15.0",
38
- "@celljs/ai-coding-agent": "4.15.0",
39
- "@celljs/core": "4.15.0",
40
- "@celljs/rpc": "4.15.0",
41
- "@celljs/ta-exec": "4.15.0",
42
- "@celljs/ta-harness": "4.15.0",
43
- "@celljs/ta-protocol": "4.15.0",
44
- "@celljs/ta-relay": "4.15.0",
45
- "@celljs/web": "4.15.0",
37
+ "@celljs/ai-agent": "4.15.1",
38
+ "@celljs/ai-coding-agent": "4.15.1",
39
+ "@celljs/core": "4.15.1",
40
+ "@celljs/rpc": "4.15.1",
41
+ "@celljs/ta-exec": "4.15.1",
42
+ "@celljs/ta-harness": "4.15.1",
43
+ "@celljs/ta-protocol": "4.15.1",
44
+ "@celljs/ta-relay": "4.15.1",
45
+ "@celljs/web": "4.15.1",
46
46
  "@earendil-works/pi-coding-agent": "^1.5.2"
47
47
  },
48
48
  "devDependencies": {