@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.
@@ -14,6 +14,7 @@ import { Autowired, Component, Value } from '@celljs/core';
14
14
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
15
15
  import { TA_FILES_PATH } from '@celljs/ta-protocol';
16
16
  import { Middleware } from '@celljs/web/lib/node';
17
+ import { PngThumbnail } from './png-thumbnail.js';
17
18
  import { ACCESS_MIDDLEWARE_PRIORITY, TaAccessUtil } from './ta-access.js';
18
19
  import { WorkspaceFiles } from './workspace-protocol.js';
19
20
  import { WorkspaceUtil } from './workspace-util.js';
@@ -65,6 +66,13 @@ const TYPES = {
65
66
  */
66
67
  const SANDBOX = 'sandbox allow-scripts allow-forms allow-popups allow-modals allow-downloads';
67
68
  const ID_BYTES = 16;
69
+ /** The smallest and largest sides of the box a smaller picture is asked for. */
70
+ const THUMB_MIN = 16;
71
+ const THUMB_MAX = 4096;
72
+ /** Smaller pictures kept, made once for every page that shows them, up to this many bytes. */
73
+ const THUMB_CACHE_BYTES = 32 * 1024 * 1024;
74
+ /** Pictures larger than this are sent as they are, never read whole to make a smaller one. */
75
+ const THUMB_SOURCE_BYTES = 48 * 1024 * 1024;
68
76
  /** Read whole from another machine: a part of a larger file is read with `tail` and `head`. */
69
77
  const REMOTE_WHOLE_BYTES = 8 * 1024 * 1024;
70
78
  export var WorkspaceFilesUtil;
@@ -110,6 +118,25 @@ export var WorkspaceFilesUtil;
110
118
  return start <= end && start < size ? { start, end } : false;
111
119
  }
112
120
  WorkspaceFilesUtil.range = range;
121
+ /**
122
+ * The box of `?w=<px>&h=<px>&fit=cover`, which asks for a smaller picture, as the page shows one it has not
123
+ * enlarged (contain without `fit`); undefined for a URL without both sides, or sides out of bounds.
124
+ */
125
+ function thumbnail(url) {
126
+ const query = (url !== null && url !== void 0 ? url : '').split('#')[0].split('?')[1];
127
+ if (!query) {
128
+ return undefined;
129
+ }
130
+ const params = new URLSearchParams(query);
131
+ const side = (name) => {
132
+ const value = Number(params.get(name));
133
+ return Number.isInteger(value) && value >= THUMB_MIN && value <= THUMB_MAX ? value : undefined;
134
+ };
135
+ const width = side('w');
136
+ const height = side('h');
137
+ return width && height ? { width, height, fit: params.get('fit') === 'cover' ? 'cover' : 'contain' } : undefined;
138
+ }
139
+ WorkspaceFilesUtil.thumbnail = thumbnail;
113
140
  function etag(stat) {
114
141
  return `"${stat.size.toString(36)}-${Math.floor(stat.mtimeMs).toString(36)}"`;
115
142
  }
@@ -133,6 +160,11 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
133
160
  */
134
161
  this.directories = new Map();
135
162
  this.ids = new Map();
163
+ /** Smaller pictures made, by file, version and box, the last used last; `null` for one sent as it is. */
164
+ this.thumbnails = new Map();
165
+ this.thumbnailBytes = 0;
166
+ /** Made one at a time: each takes the event loop in turns, and many at once would hold many large pictures. */
167
+ this.making = Promise.resolve();
136
168
  }
137
169
  url(cwd, path) {
138
170
  const absolute = resolve(cwd, path || '.');
@@ -193,6 +225,10 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
193
225
  response.end();
194
226
  return;
195
227
  }
228
+ const box = type === 'image/png' && !request.headers.range && stat.size <= THUMB_SOURCE_BYTES ? WorkspaceFilesUtil.thumbnail(request.url) : undefined;
229
+ if (box && (await this.sendThumbnail(request, response, env, absolute, etag, box))) {
230
+ return;
231
+ }
196
232
  const range = WorkspaceFilesUtil.range(request.headers.range, stat.size);
197
233
  if (range === false) {
198
234
  response.setHeader('Content-Range', `bytes */${stat.size}`);
@@ -216,6 +252,56 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
216
252
  response.end(await this.read(env, absolute, stat.size, start, end));
217
253
  }
218
254
  }
255
+ /**
256
+ * A smaller picture of a PNG for `box` (PngThumbnail), with an ETag of its own; false when the picture itself is
257
+ * to be sent (not much larger than the box, or a PNG it does not read).
258
+ */
259
+ async sendThumbnail(request, response, env, absolute, etag, box) {
260
+ var _a;
261
+ const own = `${etag.slice(0, -1)}-${box.width}x${box.height}${box.fit === 'cover' ? 'c' : ''}"`;
262
+ // Made from this version of the file before: what the page has is still right.
263
+ if (request.headers['if-none-match'] === own) {
264
+ response.setHeader('ETag', own);
265
+ response.removeHeader('Accept-Ranges');
266
+ response.statusCode = 304;
267
+ response.end();
268
+ return true;
269
+ }
270
+ const key = `${absolute}\0${own}`;
271
+ let made = this.thumbnails.get(key);
272
+ if (made === undefined) {
273
+ const making = this.making.then(async () => PngThumbnail.make(await env.readFile(absolute), box).catch(() => undefined));
274
+ this.making = making.catch(() => undefined);
275
+ made = (_a = (await making.catch(() => undefined))) !== null && _a !== void 0 ? _a : null;
276
+ this.keepThumbnail(key, made);
277
+ }
278
+ else {
279
+ this.thumbnails.delete(key);
280
+ this.thumbnails.set(key, made);
281
+ }
282
+ if (!made) {
283
+ return false;
284
+ }
285
+ response.setHeader('ETag', own);
286
+ response.setHeader('Content-Type', made.type);
287
+ response.removeHeader('Accept-Ranges');
288
+ response.statusCode = 200;
289
+ response.setHeader('Content-Length', made.bytes.length);
290
+ response.end(request.method === 'HEAD' ? undefined : made.bytes);
291
+ return true;
292
+ }
293
+ keepThumbnail(key, made) {
294
+ var _a, _b;
295
+ this.thumbnails.set(key, made);
296
+ this.thumbnailBytes += (_a = made === null || made === void 0 ? void 0 : made.bytes.length) !== null && _a !== void 0 ? _a : 0;
297
+ for (const [old, bytes] of this.thumbnails) {
298
+ if (this.thumbnailBytes <= THUMB_CACHE_BYTES && this.thumbnails.size <= 4096) {
299
+ break;
300
+ }
301
+ this.thumbnails.delete(old);
302
+ this.thumbnailBytes -= (_b = bytes === null || bytes === void 0 ? void 0 : bytes.bytes.length) !== null && _b !== void 0 ? _b : 0;
303
+ }
304
+ }
219
305
  /** The file at `path` of `root`, or the `index.html` of the directory there. */
220
306
  async find(env, root, path) {
221
307
  let absolute;
@@ -129,8 +129,12 @@ let ExtensionDataStoreImpl = class ExtensionDataStoreImpl {
129
129
  .then(async () => {
130
130
  const text = ExtensionDataUtil.update(await this.read(file), key, value);
131
131
  await (0, promises_1.mkdir)((0, node_path_1.dirname)(file), { recursive: true });
132
- await (0, promises_1.writeFile)(`${file}.tmp`, text, { mode: 0o600 });
133
- await (0, promises_1.rename)(`${file}.tmp`, file);
132
+ // A file of its own: another Ta of this agent directory (one starting as one stops) writing
133
+ // the same name at once would have one rename a file the other is halfway through, and the
134
+ // next read of it, unparsed, would leave every other key out.
135
+ const temporary = `${file}.${(0, node_crypto_1.randomUUID)()}.tmp`;
136
+ await (0, promises_1.writeFile)(temporary, text, { mode: 0o600 });
137
+ await (0, promises_1.rename)(temporary, file);
134
138
  for (const listener of this.listeners) {
135
139
  try {
136
140
  listener(name, key, directory);
@@ -18,6 +18,7 @@ export * from './relay-bridge';
18
18
  export * from './remote';
19
19
  export * from './remote-ui-context';
20
20
  export * from './rooms-bridge';
21
+ export * from './serve-updates';
21
22
  export * from './session-catalog';
22
23
  export * from './session-files';
23
24
  export * from './session-handles';
package/lib/node/index.js CHANGED
@@ -34,6 +34,7 @@ __exportStar(require("./relay-bridge"), exports);
34
34
  __exportStar(require("./remote"), exports);
35
35
  __exportStar(require("./remote-ui-context"), exports);
36
36
  __exportStar(require("./rooms-bridge"), exports);
37
+ __exportStar(require("./serve-updates"), exports);
37
38
  __exportStar(require("./session-catalog"), exports);
38
39
  __exportStar(require("./session-files"), exports);
39
40
  __exportStar(require("./session-handles"), exports);
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A baseline JPEG of opaque RGBA, without a library: YCbCr without subsampling (the letters of a screenshot keep
3
+ * their colour), the tables of the standard scaled to `quality` (1–100, as libjpeg reads it).
4
+ */
5
+ export declare namespace JpegEncoder {
6
+ function encode(rgba: Uint8Array, width: number, height: number, quality?: number): Uint8Array;
7
+ }
8
+ //# sourceMappingURL=jpeg-encoder.d.ts.map
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JpegEncoder = void 0;
4
+ /** The order of the 64 coefficients of a block as a JPEG writes them, as indices of its rows one after another. */
5
+ const ZIGZAG = [
6
+ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29,
7
+ 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
8
+ ];
9
+ /** The quantization tables of the JPEG standard (Annex K), by rows. */
10
+ const LUMA_QUANT = [
11
+ 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109,
12
+ 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99,
13
+ ];
14
+ const CHROMA_QUANT = [
15
+ 17,
16
+ 18,
17
+ 24,
18
+ 47,
19
+ 99,
20
+ 99,
21
+ 99,
22
+ 99,
23
+ 18,
24
+ 21,
25
+ 26,
26
+ 66,
27
+ 99,
28
+ 99,
29
+ 99,
30
+ 99,
31
+ 24,
32
+ 26,
33
+ 56,
34
+ 99,
35
+ 99,
36
+ 99,
37
+ 99,
38
+ 99,
39
+ 47,
40
+ 66,
41
+ 99,
42
+ 99,
43
+ 99,
44
+ 99,
45
+ 99,
46
+ 99,
47
+ ...new Array(32).fill(99),
48
+ ];
49
+ /** The Huffman tables of the standard: how many codes of each length 1 to 16, then their values. */
50
+ const DC_LUMA = { bits: [0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] };
51
+ const DC_CHROMA = { bits: [0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] };
52
+ const AC_LUMA = {
53
+ bits: [0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d],
54
+ values: [
55
+ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
56
+ 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
57
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,
58
+ 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
59
+ 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
60
+ 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
61
+ 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
62
+ ],
63
+ };
64
+ const AC_CHROMA = {
65
+ bits: [0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77],
66
+ values: [
67
+ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1,
68
+ 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28,
69
+ 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63,
70
+ 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92,
71
+ 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
72
+ 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
73
+ 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
74
+ ],
75
+ };
76
+ /** For each value of a table, its code and the length of the code. */
77
+ function codes(table) {
78
+ const code = new Uint16Array(256);
79
+ const length = new Uint8Array(256);
80
+ let next = 0;
81
+ let at = 0;
82
+ for (let size = 1; size <= 16; size++) {
83
+ for (let n = 0; n < table.bits[size - 1]; n++) {
84
+ code[table.values[at]] = next;
85
+ length[table.values[at]] = size;
86
+ next++;
87
+ at++;
88
+ }
89
+ next <<= 1;
90
+ }
91
+ return { code, length };
92
+ }
93
+ /** cos((2x + 1) u π / 16), with the factor of u = 0, for the forward DCT. */
94
+ const COS = (() => {
95
+ const table = new Float64Array(64);
96
+ for (let u = 0; u < 8; u++) {
97
+ for (let x = 0; x < 8; x++) {
98
+ table[u * 8 + x] = Math.cos(((2 * x + 1) * u * Math.PI) / 16) * (u === 0 ? Math.SQRT1_2 : 1);
99
+ }
100
+ }
101
+ return table;
102
+ })();
103
+ class BitWriter {
104
+ constructor() {
105
+ this.bytes = new Uint8Array(64 * 1024);
106
+ this.length = 0;
107
+ this.value = 0;
108
+ this.count = 0;
109
+ }
110
+ byte(byte) {
111
+ if (this.length === this.bytes.length) {
112
+ const larger = new Uint8Array(this.bytes.length * 2);
113
+ larger.set(this.bytes);
114
+ this.bytes = larger;
115
+ }
116
+ this.bytes[this.length++] = byte;
117
+ }
118
+ raw(bytes) {
119
+ for (const byte of bytes) {
120
+ this.byte(byte);
121
+ }
122
+ }
123
+ bits(value, count) {
124
+ for (let i = count - 1; i >= 0; i--) {
125
+ this.value = (this.value << 1) | ((value >> i) & 1);
126
+ if (++this.count === 8) {
127
+ this.byte(this.value);
128
+ // A 0xFF of the data is followed by a 0, so that it is not taken for a marker.
129
+ if (this.value === 0xff) {
130
+ this.byte(0);
131
+ }
132
+ this.value = 0;
133
+ this.count = 0;
134
+ }
135
+ }
136
+ }
137
+ /** The last bits, padded with ones. */
138
+ flush() {
139
+ if (this.count > 0) {
140
+ this.bits((1 << (8 - this.count)) - 1, 8 - this.count);
141
+ }
142
+ }
143
+ done() {
144
+ return this.bytes.slice(0, this.length);
145
+ }
146
+ }
147
+ /**
148
+ * A baseline JPEG of opaque RGBA, without a library: YCbCr without subsampling (the letters of a screenshot keep
149
+ * their colour), the tables of the standard scaled to `quality` (1–100, as libjpeg reads it).
150
+ */
151
+ var JpegEncoder;
152
+ (function (JpegEncoder) {
153
+ function encode(rgba, width, height, quality = 80) {
154
+ const scale = quality < 50 ? 5000 / quality : 200 - quality * 2;
155
+ const quant = (base) => base.map(value => Math.min(255, Math.max(1, Math.floor((value * scale + 50) / 100))));
156
+ const luma = quant(LUMA_QUANT);
157
+ const chroma = quant(CHROMA_QUANT);
158
+ const out = new BitWriter();
159
+ out.raw([0xff, 0xd8]);
160
+ // JFIF, version 1.1, no density.
161
+ out.raw([0xff, 0xe0, 0, 16, 0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
162
+ for (const [id, table] of [luma, chroma].entries()) {
163
+ out.raw([0xff, 0xdb, 0, 67, id, ...ZIGZAG.map(index => table[index])]);
164
+ }
165
+ out.raw([0xff, 0xc0, 0, 17, 8, height >> 8, height & 255, width >> 8, width & 255, 3, 1, 0x11, 0, 2, 0x11, 1, 3, 0x11, 1]);
166
+ for (const [klass, table] of [
167
+ [0x00, DC_LUMA],
168
+ [0x10, AC_LUMA],
169
+ [0x01, DC_CHROMA],
170
+ [0x11, AC_CHROMA],
171
+ ]) {
172
+ out.raw([0xff, 0xc4, (19 + table.values.length) >> 8, (19 + table.values.length) & 255, klass, ...table.bits, ...table.values]);
173
+ }
174
+ out.raw([0xff, 0xda, 0, 12, 3, 1, 0x00, 2, 0x11, 3, 0x11, 0, 63, 0]);
175
+ const tables = [
176
+ { quant: luma, dc: codes(DC_LUMA), ac: codes(AC_LUMA) },
177
+ { quant: chroma, dc: codes(DC_CHROMA), ac: codes(AC_CHROMA) },
178
+ { quant: chroma, dc: codes(DC_CHROMA), ac: codes(AC_CHROMA) },
179
+ ];
180
+ const previous = [0, 0, 0];
181
+ const planes = [new Float64Array(64), new Float64Array(64), new Float64Array(64)];
182
+ const coefficients = new Int32Array(64);
183
+ const rows = new Float64Array(64);
184
+ for (let top = 0; top < height; top += 8) {
185
+ for (let left = 0; left < width; left += 8) {
186
+ for (let y = 0; y < 8; y++) {
187
+ const row = Math.min(height - 1, top + y) * width;
188
+ for (let x = 0; x < 8; x++) {
189
+ const at = (row + Math.min(width - 1, left + x)) * 4;
190
+ const r = rgba[at];
191
+ const g = rgba[at + 1];
192
+ const b = rgba[at + 2];
193
+ planes[0][y * 8 + x] = 0.299 * r + 0.587 * g + 0.114 * b - 128;
194
+ planes[1][y * 8 + x] = -0.168736 * r - 0.331264 * g + 0.5 * b;
195
+ planes[2][y * 8 + x] = 0.5 * r - 0.418688 * g - 0.081312 * b;
196
+ }
197
+ }
198
+ for (let component = 0; component < 3; component++) {
199
+ const table = tables[component];
200
+ dct(planes[component], rows, coefficients, table.quant);
201
+ previous[component] = block(out, coefficients, previous[component], table.dc, table.ac);
202
+ }
203
+ }
204
+ }
205
+ out.flush();
206
+ out.raw([0xff, 0xd9]);
207
+ return out.done();
208
+ }
209
+ JpegEncoder.encode = encode;
210
+ /** The coefficients of a block of 8 × 8, quantized, in the order of the zigzag. */
211
+ function dct(block, rows, out, quant) {
212
+ for (let y = 0; y < 8; y++) {
213
+ for (let u = 0; u < 8; u++) {
214
+ let sum = 0;
215
+ for (let x = 0; x < 8; x++) {
216
+ sum += block[y * 8 + x] * COS[u * 8 + x];
217
+ }
218
+ rows[y * 8 + u] = sum;
219
+ }
220
+ }
221
+ for (let k = 0; k < 64; k++) {
222
+ const index = ZIGZAG[k];
223
+ const v = index >> 3;
224
+ const u = index & 7;
225
+ let sum = 0;
226
+ for (let y = 0; y < 8; y++) {
227
+ sum += rows[y * 8 + u] * COS[v * 8 + y];
228
+ }
229
+ out[k] = Math.round(sum / 4 / quant[index]);
230
+ }
231
+ }
232
+ /** One block written with its tables; the DC of it, which the next of its component is written from. */
233
+ function block(out, coefficients, previous, dc, ac) {
234
+ const diff = coefficients[0] - previous;
235
+ const size = category(diff);
236
+ out.bits(dc.code[size], dc.length[size]);
237
+ out.bits(magnitude(diff, size), size);
238
+ let run = 0;
239
+ for (let k = 1; k < 64; k++) {
240
+ const value = coefficients[k];
241
+ if (value === 0) {
242
+ run++;
243
+ continue;
244
+ }
245
+ while (run > 15) {
246
+ out.bits(ac.code[0xf0], ac.length[0xf0]);
247
+ run -= 16;
248
+ }
249
+ const bits = category(value);
250
+ const symbol = (run << 4) | bits;
251
+ out.bits(ac.code[symbol], ac.length[symbol]);
252
+ out.bits(magnitude(value, bits), bits);
253
+ run = 0;
254
+ }
255
+ if (run > 0) {
256
+ out.bits(ac.code[0], ac.length[0]);
257
+ }
258
+ return coefficients[0];
259
+ }
260
+ function category(value) {
261
+ let size = 0;
262
+ for (let magnitude = Math.abs(value); magnitude > 0; magnitude >>= 1) {
263
+ size++;
264
+ }
265
+ return size;
266
+ }
267
+ function magnitude(value, size) {
268
+ return value < 0 ? value + (1 << size) - 1 : value;
269
+ }
270
+ })(JpegEncoder || (exports.JpegEncoder = JpegEncoder = {}));
271
+ //# sourceMappingURL=jpeg-encoder.js.map
@@ -0,0 +1,41 @@
1
+ /** How a picture fits the box it is made for: all of it in the box, or the box filled (the page cuts what is over). */
2
+ export type ThumbnailFit = 'contain' | 'cover';
3
+ /** The box a smaller picture is asked for, in pixels of the device. */
4
+ export interface ThumbnailBox {
5
+ width: number;
6
+ height: number;
7
+ fit: ThumbnailFit;
8
+ }
9
+ /**
10
+ * A smaller picture of a PNG, made without a library: the image read (any color type, 8 or 16 bits, a palette of 1 to 8,
11
+ * not interlaced), averaged down to the box (alpha weighted, so edges keep their colour) and written again, RGB when
12
+ * it is opaque. It yields to the event loop as it reads. Undefined for what it does not read, and for a picture already
13
+ * near the size of the box: then the picture itself is sent.
14
+ */
15
+ export declare namespace PngThumbnail {
16
+ function isPng(bytes: Uint8Array): boolean;
17
+ /** The size a picture of `width` × `height` takes for `box`, never larger than it is. */
18
+ function size(width: number, height: number, box: ThumbnailBox): {
19
+ width: number;
20
+ height: number;
21
+ };
22
+ /**
23
+ * The smaller picture: a PNG, or a JPEG when the picture is opaque and of a photo (its PNG large for its pixels)
24
+ * and the JPEG is smaller.
25
+ */
26
+ function make(bytes: Uint8Array, box: ThumbnailBox): Promise<{
27
+ bytes: Uint8Array;
28
+ type: 'image/png' | 'image/jpeg';
29
+ } | undefined>;
30
+ /** The pixels of a PNG as RGBA, as `make` reads them; undefined for one it does not read. */
31
+ function read(bytes: Uint8Array): Promise<{
32
+ width: number;
33
+ height: number;
34
+ rgba: Uint8Array;
35
+ } | undefined>;
36
+ /** RGBA written as a PNG: RGB when every pixel is opaque, each row with the filter that leaves the smallest sums. */
37
+ function encode(rgba: Uint8Array, width: number, height: number): Uint8Array;
38
+ /** A PNG of these chunks, each with its length and CRC. */
39
+ function chunked(parts: readonly [string, Uint8Array][]): Uint8Array;
40
+ }
41
+ //# sourceMappingURL=png-thumbnail.d.ts.map