report-studio 1.0.0 → 1.1.0

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 (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +129 -4
  3. package/dist/designer-qy59eEux.js +3416 -0
  4. package/dist/designer.js +2 -3209
  5. package/dist/index.js +2 -2
  6. package/dist/react-designer.js +41 -0
  7. package/dist/react.js +60 -0
  8. package/dist/{viewer-D-c0lKzQ.js → viewer-xa17tyMJ.js} +682 -537
  9. package/package.json +80 -66
  10. package/src/cli/serve.js +135 -135
  11. package/src/designer/designer.js +2054 -1900
  12. package/src/designer/dialog.js +708 -708
  13. package/src/designer/fields.js +51 -2
  14. package/src/designer/icons.js +5 -0
  15. package/src/designer/index.html +28 -28
  16. package/src/designer/main.js +39 -39
  17. package/src/designer/menu.js +2 -2
  18. package/src/designer/panel.js +21 -1
  19. package/src/designer/sample-data.js +180 -9
  20. package/src/designer/structure.js +87 -6
  21. package/src/designer/styles.css +1654 -1616
  22. package/src/designer/tokens.js +9 -0
  23. package/src/engine/csv.js +350 -315
  24. package/src/engine/group.js +128 -65
  25. package/src/engine/index.js +35 -34
  26. package/src/engine/measure.js +13 -12
  27. package/src/engine/paginate.js +8 -4
  28. package/src/engine/resolve.js +37 -0
  29. package/src/engine/tables.js +87 -0
  30. package/src/engine/validate.js +97 -22
  31. package/src/index.html +79 -79
  32. package/src/index.js +51 -50
  33. package/src/preview/chrome.css +505 -505
  34. package/src/preview/chrome.js +147 -147
  35. package/src/preview/index.html +28 -28
  36. package/src/preview/viewer.js +763 -745
  37. package/src/react/designer.js +87 -0
  38. package/src/react/index.js +117 -0
  39. package/src/render/items.js +94 -1
  40. package/src/render/report.css +30 -0
  41. package/src/server/routes.js +331 -331
  42. package/src/shared/brand.css +29 -29
  43. package/src/shared/dropdown.css +136 -136
  44. package/src/shared/modal.css +125 -125
  45. package/src/shared/store.js +145 -145
  46. package/src/shared/theme.css +72 -72
  47. package/src/shared/window.js +138 -138
@@ -1,331 +1,331 @@
1
- /**
2
- * routes.js reads and writes report files, and is the only code in the project
3
- * that touches a disk.
4
- *
5
- * A browser cannot list a directory or write to one, and cannot be constrained
6
- * to a folder even where it can - showDirectoryPicker takes no such argument
7
- * and the user may navigate anywhere from it. A local dev server can do both,
8
- * because it is Node and it knows where the project is. So the split is not a
9
- * preference: it is the only place each half of the job can be done.
10
- *
11
- * Written as a Connect-style `(req, res, next)` handler and nothing else, so
12
- * the same function serves both mounts - Vite's middleware stack and the CLI's
13
- * bare http server take the same signature. The scoping, the guards and the
14
- * error shapes are therefore written once.
15
- *
16
- * GET <prefix> list the reports
17
- * GET <prefix>/:id read one
18
- * PUT <prefix>/:id write one
19
- * DELETE <prefix>/:id remove one
20
- * GET <prefix>/:id/data read its data payload
21
- * PUT <prefix>/:id/data write its data payload
22
- *
23
- * The data payload lives beside the layout as `<id>.data.json`, the pairing the
24
- * fixtures already use. A layout is a set of questions - which fields, which
25
- * dataset, what to group by - and the data file is where the host application's
26
- * answers go. Two files rather than one means real data can be swapped in
27
- * without the designer ever rewriting it.
28
- *
29
- * There is no id or timestamp in the layout file. The *filename* is the id and
30
- * the file's mtime is when it changed - one source of truth each, and no field
31
- * for the engine to ignore and a later reader to believe.
32
- */
33
-
34
- import { readFile, writeFile, readdir, mkdir, stat, unlink } from 'node:fs/promises';
35
- import { resolve, join, sep } from 'node:path';
36
- import { DEFAULT_PREFIX } from '../shared/prefix.js';
37
-
38
- export { DEFAULT_PREFIX };
39
-
40
- /**
41
- * An id is a slug and nothing else - no dots, no separators, no percent
42
- * escapes. `..` is then impossible to express rather than merely rejected,
43
- * which is a stronger thing to rely on than a blocklist someone has to keep up
44
- * with. The extension is added here, never sent by the client.
45
- */
46
- const ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
47
-
48
- /** a layout is a page of JSON, not a payload; this is already generous */
49
- const MAX_BODY = 4 * 1024 * 1024;
50
-
51
-
52
- export function isValidId(id) {
53
- return typeof id === 'string' && ID.test(id);
54
- }
55
-
56
-
57
- /**
58
- * The path a report id maps to, or null if it does not map to one.
59
- *
60
- * Two guards, both cheap, and both kept: the pattern makes traversal
61
- * inexpressible, and the resolve check confirms the result really did stay
62
- * inside the directory. The second catches anything the first has not thought
63
- * of - a decoding quirk, a future change to the pattern - and this is the one
64
- * place in the project where being wrong writes to somebody's disk.
65
- */
66
- export function reportPath(dir, id, kind = 'layout') {
67
- if (!isValidId(id)) return null;
68
-
69
- const name = kind === 'data' ? `${id}.data.json` : `${id}.json`;
70
- const root = resolve(dir);
71
- const file = resolve(join(root, name));
72
-
73
- if (file !== join(root, name)) return null;
74
- if (!file.startsWith(root + sep)) return null;
75
-
76
- return file;
77
- }
78
-
79
-
80
-
81
- /**
82
- * Builds the request handler.
83
- *
84
- * @param {object} options
85
- * @param {string} options.dir the one directory reports are read from and written to
86
- * @param {string} [options.prefix] the URL the routes live under
87
- * @returns {(req, res, next) => void}
88
- */
89
- export function createReportRoutes({ dir, prefix = DEFAULT_PREFIX }) {
90
- const root = resolve(dir);
91
-
92
- return function handle(req, res, next) {
93
- const url = (req.url || '').split('?')[0];
94
-
95
- if (url !== prefix && !url.startsWith(`${prefix}/`)) {
96
- return next?.();
97
- }
98
-
99
- const rest = url.slice(prefix.length).replace(/^\//, '');
100
-
101
- const [id, kind] = rest.split('/').map(decodeSafely);
102
-
103
- route(req, res, root, id, kind)
104
- .catch(error => send(res, 500, { error: String(error?.message ?? error) }));
105
- };
106
- }
107
-
108
-
109
- /**
110
- * A malformed escape throws out of decodeURIComponent, which would otherwise
111
- * surface as a 500 for what is really a bad request.
112
- */
113
- function decodeSafely(text) {
114
- try {
115
- return decodeURIComponent(text);
116
- } catch {
117
- return text;
118
- }
119
- }
120
-
121
-
122
- async function route(req, res, root, id, kind) {
123
- const method = (req.method || 'GET').toUpperCase();
124
-
125
- if (!id) {
126
- if (method !== 'GET') return send(res, 405, { error: `cannot ${method} the list` });
127
- return send(res, 200, { reports: await list(root) });
128
- }
129
-
130
- /** the only sub-path there is; anything else is a URL nobody meant to write */
131
- if (kind !== undefined && kind !== 'data') {
132
- return send(res, 404, { error: `there is no "${kind}" under a report` });
133
- }
134
-
135
- const isData = kind === 'data';
136
- const file = reportPath(root, id, isData ? 'data' : 'layout');
137
-
138
- if (!file) {
139
- return send(res, 400, {
140
- error: `"${id}" is not a usable report name - letters, digits, ` +
141
- `dashes and underscores only`
142
- });
143
- }
144
-
145
- switch (method) {
146
- case 'GET': return read(res, file, id, isData);
147
- case 'PUT': return write(req, res, root, file, id, isData);
148
-
149
- case 'DELETE':
150
- if (isData) return send(res, 405, { error: 'cannot DELETE a data file' });
151
- return remove(res, file, id);
152
-
153
- default: return send(res, 405, { error: `cannot ${method} a report` });
154
- }
155
- }
156
-
157
-
158
- /**
159
- * Every report in the directory, newest first.
160
- *
161
- * The layout is opened to read its `name`, because a list of filenames is not
162
- * how anyone thinks of their reports. One that will not parse is still listed -
163
- * it is the one you most need to be able to open and fix.
164
- */
165
- async function list(root) {
166
- let names;
167
-
168
- try {
169
- names = await readdir(root);
170
- } catch (error) {
171
- /** a directory nobody has saved into yet holds no reports, and is fine */
172
- if (error.code === 'ENOENT') return [];
173
- throw error;
174
- }
175
-
176
- const reports = await Promise.all(names
177
- .filter(file => file.endsWith('.json') && !file.endsWith('.data.json'))
178
- .map(async file => {
179
- const id = file.slice(0, -'.json'.length);
180
- if (!isValidId(id)) return null;
181
-
182
- const path = join(root, file);
183
- const [info, layout] = await Promise.all([
184
- stat(path).catch(() => null),
185
- readFile(path, 'utf8').then(JSON.parse).catch(() => null)
186
- ]);
187
-
188
- return {
189
- id,
190
- name: typeof layout?.name === 'string' && layout.name ? layout.name : id,
191
- updatedAt: info ? info.mtime.toISOString() : null,
192
- readable: layout != null
193
- };
194
- }));
195
-
196
- return reports
197
- .filter(Boolean)
198
- .sort((a, b) => String(b.updatedAt).localeCompare(String(a.updatedAt)));
199
- }
200
-
201
-
202
- async function read(res, file, id, isData = false) {
203
- const what = isData ? `${id}.data.json` : `${id}.json`;
204
- let text;
205
-
206
- try {
207
- text = await readFile(file, 'utf8');
208
- } catch (error) {
209
- if (error.code === 'ENOENT') {
210
- return send(res, 404, {
211
- error: isData
212
- ? `"${id}" has no data file yet`
213
- : `there is no report called "${id}"`
214
- });
215
- }
216
- throw error;
217
- }
218
-
219
- try {
220
- return send(res, 200, JSON.parse(text));
221
- } catch {
222
- return send(res, 422, {
223
- error: `"${what}" is not valid JSON, so it cannot be opened`
224
- });
225
- }
226
- }
227
-
228
-
229
- /**
230
- * Writes the report.
231
- *
232
- * The layout is checked as JSON and as an object, and no further: a report
233
- * half-way through being designed is legitimately invalid - groupBy set before
234
- * the group band is added is one keystroke of it - and refusing to save
235
- * somebody's work in progress is not a safety feature. The designer shows what
236
- * is wrong; the disk keeps what was asked for.
237
- */
238
- async function write(req, res, root, file, id, isData = false) {
239
- let body;
240
-
241
- try {
242
- body = await readBody(req);
243
- } catch (error) {
244
- return send(res, 413, { error: error.message });
245
- }
246
-
247
- let layout;
248
-
249
- try {
250
- layout = JSON.parse(body);
251
- } catch {
252
- return send(res, 400, { error: 'the request body is not valid JSON' });
253
- }
254
-
255
- if (layout == null || typeof layout !== 'object' || Array.isArray(layout)) {
256
- return send(res, 400, {
257
- error: isData
258
- ? 'a data payload must be a JSON object keyed by dataset name'
259
- : 'a report must be a JSON object'
260
- });
261
- }
262
-
263
- /** created on demand: the user asked for this directory by naming it */
264
- await mkdir(root, { recursive: true });
265
-
266
- /** two spaces and a trailing newline, so the file diffs like source */
267
- await writeFile(file, `${JSON.stringify(layout, null, 2)}\n`, 'utf8');
268
-
269
- const info = await stat(file);
270
-
271
- return send(res, 200, {
272
- id,
273
- kind: isData ? 'data' : 'layout',
274
- name: typeof layout.name === 'string' ? layout.name : id,
275
- updatedAt: info.mtime.toISOString()
276
- });
277
- }
278
-
279
-
280
- async function remove(res, file, id) {
281
- try {
282
- await unlink(file);
283
- } catch (error) {
284
- if (error.code === 'ENOENT') {
285
- return send(res, 404, { error: `there is no report called "${id}"` });
286
- }
287
- throw error;
288
- }
289
-
290
- /**
291
- * The data file goes with it. Left behind, the next report saved under the
292
- * same name would silently inherit the last one's data.
293
- */
294
- await unlink(file.replace(/\.json$/, '.data.json')).catch(() => { });
295
-
296
- return send(res, 200, { id, deleted: true });
297
- }
298
-
299
-
300
- function readBody(req) {
301
- return new Promise((resolve, reject) => {
302
- const chunks = [];
303
- let size = 0;
304
-
305
- req.on('data', chunk => {
306
- size += chunk.length;
307
-
308
- if (size > MAX_BODY) {
309
- reject(new Error(`a report may not be larger than ${MAX_BODY} bytes`));
310
- req.destroy();
311
- return;
312
- }
313
-
314
- chunks.push(chunk);
315
- });
316
-
317
- req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
318
- req.on('error', reject);
319
- });
320
- }
321
-
322
-
323
- function send(res, status, body) {
324
- const text = JSON.stringify(body);
325
-
326
- res.statusCode = status;
327
- res.setHeader('Content-Type', 'application/json; charset=utf-8');
328
- /** a report read a second ago is not the one on disk now */
329
- res.setHeader('Cache-Control', 'no-store');
330
- res.end(text);
331
- }
1
+ /**
2
+ * routes.js reads and writes report files, and is the only code in the project
3
+ * that touches a disk.
4
+ *
5
+ * A browser cannot list a directory or write to one, and cannot be constrained
6
+ * to a folder even where it can - showDirectoryPicker takes no such argument
7
+ * and the user may navigate anywhere from it. A local dev server can do both,
8
+ * because it is Node and it knows where the project is. So the split is not a
9
+ * preference: it is the only place each half of the job can be done.
10
+ *
11
+ * Written as a Connect-style `(req, res, next)` handler and nothing else, so
12
+ * the same function serves both mounts - Vite's middleware stack and the CLI's
13
+ * bare http server take the same signature. The scoping, the guards and the
14
+ * error shapes are therefore written once.
15
+ *
16
+ * GET <prefix> list the reports
17
+ * GET <prefix>/:id read one
18
+ * PUT <prefix>/:id write one
19
+ * DELETE <prefix>/:id remove one
20
+ * GET <prefix>/:id/data read its data payload
21
+ * PUT <prefix>/:id/data write its data payload
22
+ *
23
+ * The data payload lives beside the layout as `<id>.data.json`, the pairing the
24
+ * fixtures already use. A layout is a set of questions - which fields, which
25
+ * dataset, what to group by - and the data file is where the host application's
26
+ * answers go. Two files rather than one means real data can be swapped in
27
+ * without the designer ever rewriting it.
28
+ *
29
+ * There is no id or timestamp in the layout file. The *filename* is the id and
30
+ * the file's mtime is when it changed - one source of truth each, and no field
31
+ * for the engine to ignore and a later reader to believe.
32
+ */
33
+
34
+ import { readFile, writeFile, readdir, mkdir, stat, unlink } from 'node:fs/promises';
35
+ import { resolve, join, sep } from 'node:path';
36
+ import { DEFAULT_PREFIX } from '../shared/prefix.js';
37
+
38
+ export { DEFAULT_PREFIX };
39
+
40
+ /**
41
+ * An id is a slug and nothing else - no dots, no separators, no percent
42
+ * escapes. `..` is then impossible to express rather than merely rejected,
43
+ * which is a stronger thing to rely on than a blocklist someone has to keep up
44
+ * with. The extension is added here, never sent by the client.
45
+ */
46
+ const ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
47
+
48
+ /** a layout is a page of JSON, not a payload; this is already generous */
49
+ const MAX_BODY = 4 * 1024 * 1024;
50
+
51
+
52
+ export function isValidId(id) {
53
+ return typeof id === 'string' && ID.test(id);
54
+ }
55
+
56
+
57
+ /**
58
+ * The path a report id maps to, or null if it does not map to one.
59
+ *
60
+ * Two guards, both cheap, and both kept: the pattern makes traversal
61
+ * inexpressible, and the resolve check confirms the result really did stay
62
+ * inside the directory. The second catches anything the first has not thought
63
+ * of - a decoding quirk, a future change to the pattern - and this is the one
64
+ * place in the project where being wrong writes to somebody's disk.
65
+ */
66
+ export function reportPath(dir, id, kind = 'layout') {
67
+ if (!isValidId(id)) return null;
68
+
69
+ const name = kind === 'data' ? `${id}.data.json` : `${id}.json`;
70
+ const root = resolve(dir);
71
+ const file = resolve(join(root, name));
72
+
73
+ if (file !== join(root, name)) return null;
74
+ if (!file.startsWith(root + sep)) return null;
75
+
76
+ return file;
77
+ }
78
+
79
+
80
+
81
+ /**
82
+ * Builds the request handler.
83
+ *
84
+ * @param {object} options
85
+ * @param {string} options.dir the one directory reports are read from and written to
86
+ * @param {string} [options.prefix] the URL the routes live under
87
+ * @returns {(req, res, next) => void}
88
+ */
89
+ export function createReportRoutes({ dir, prefix = DEFAULT_PREFIX }) {
90
+ const root = resolve(dir);
91
+
92
+ return function handle(req, res, next) {
93
+ const url = (req.url || '').split('?')[0];
94
+
95
+ if (url !== prefix && !url.startsWith(`${prefix}/`)) {
96
+ return next?.();
97
+ }
98
+
99
+ const rest = url.slice(prefix.length).replace(/^\//, '');
100
+
101
+ const [id, kind] = rest.split('/').map(decodeSafely);
102
+
103
+ route(req, res, root, id, kind)
104
+ .catch(error => send(res, 500, { error: String(error?.message ?? error) }));
105
+ };
106
+ }
107
+
108
+
109
+ /**
110
+ * A malformed escape throws out of decodeURIComponent, which would otherwise
111
+ * surface as a 500 for what is really a bad request.
112
+ */
113
+ function decodeSafely(text) {
114
+ try {
115
+ return decodeURIComponent(text);
116
+ } catch {
117
+ return text;
118
+ }
119
+ }
120
+
121
+
122
+ async function route(req, res, root, id, kind) {
123
+ const method = (req.method || 'GET').toUpperCase();
124
+
125
+ if (!id) {
126
+ if (method !== 'GET') return send(res, 405, { error: `cannot ${method} the list` });
127
+ return send(res, 200, { reports: await list(root) });
128
+ }
129
+
130
+ /** the only sub-path there is; anything else is a URL nobody meant to write */
131
+ if (kind !== undefined && kind !== 'data') {
132
+ return send(res, 404, { error: `there is no "${kind}" under a report` });
133
+ }
134
+
135
+ const isData = kind === 'data';
136
+ const file = reportPath(root, id, isData ? 'data' : 'layout');
137
+
138
+ if (!file) {
139
+ return send(res, 400, {
140
+ error: `"${id}" is not a usable report name - letters, digits, ` +
141
+ `dashes and underscores only`
142
+ });
143
+ }
144
+
145
+ switch (method) {
146
+ case 'GET': return read(res, file, id, isData);
147
+ case 'PUT': return write(req, res, root, file, id, isData);
148
+
149
+ case 'DELETE':
150
+ if (isData) return send(res, 405, { error: 'cannot DELETE a data file' });
151
+ return remove(res, file, id);
152
+
153
+ default: return send(res, 405, { error: `cannot ${method} a report` });
154
+ }
155
+ }
156
+
157
+
158
+ /**
159
+ * Every report in the directory, newest first.
160
+ *
161
+ * The layout is opened to read its `name`, because a list of filenames is not
162
+ * how anyone thinks of their reports. One that will not parse is still listed -
163
+ * it is the one you most need to be able to open and fix.
164
+ */
165
+ async function list(root) {
166
+ let names;
167
+
168
+ try {
169
+ names = await readdir(root);
170
+ } catch (error) {
171
+ /** a directory nobody has saved into yet holds no reports, and is fine */
172
+ if (error.code === 'ENOENT') return [];
173
+ throw error;
174
+ }
175
+
176
+ const reports = await Promise.all(names
177
+ .filter(file => file.endsWith('.json') && !file.endsWith('.data.json'))
178
+ .map(async file => {
179
+ const id = file.slice(0, -'.json'.length);
180
+ if (!isValidId(id)) return null;
181
+
182
+ const path = join(root, file);
183
+ const [info, layout] = await Promise.all([
184
+ stat(path).catch(() => null),
185
+ readFile(path, 'utf8').then(JSON.parse).catch(() => null)
186
+ ]);
187
+
188
+ return {
189
+ id,
190
+ name: typeof layout?.name === 'string' && layout.name ? layout.name : id,
191
+ updatedAt: info ? info.mtime.toISOString() : null,
192
+ readable: layout != null
193
+ };
194
+ }));
195
+
196
+ return reports
197
+ .filter(Boolean)
198
+ .sort((a, b) => String(b.updatedAt).localeCompare(String(a.updatedAt)));
199
+ }
200
+
201
+
202
+ async function read(res, file, id, isData = false) {
203
+ const what = isData ? `${id}.data.json` : `${id}.json`;
204
+ let text;
205
+
206
+ try {
207
+ text = await readFile(file, 'utf8');
208
+ } catch (error) {
209
+ if (error.code === 'ENOENT') {
210
+ return send(res, 404, {
211
+ error: isData
212
+ ? `"${id}" has no data file yet`
213
+ : `there is no report called "${id}"`
214
+ });
215
+ }
216
+ throw error;
217
+ }
218
+
219
+ try {
220
+ return send(res, 200, JSON.parse(text));
221
+ } catch {
222
+ return send(res, 422, {
223
+ error: `"${what}" is not valid JSON, so it cannot be opened`
224
+ });
225
+ }
226
+ }
227
+
228
+
229
+ /**
230
+ * Writes the report.
231
+ *
232
+ * The layout is checked as JSON and as an object, and no further: a report
233
+ * half-way through being designed is legitimately invalid - groupBy set before
234
+ * the group band is added is one keystroke of it - and refusing to save
235
+ * somebody's work in progress is not a safety feature. The designer shows what
236
+ * is wrong; the disk keeps what was asked for.
237
+ */
238
+ async function write(req, res, root, file, id, isData = false) {
239
+ let body;
240
+
241
+ try {
242
+ body = await readBody(req);
243
+ } catch (error) {
244
+ return send(res, 413, { error: error.message });
245
+ }
246
+
247
+ let layout;
248
+
249
+ try {
250
+ layout = JSON.parse(body);
251
+ } catch {
252
+ return send(res, 400, { error: 'the request body is not valid JSON' });
253
+ }
254
+
255
+ if (layout == null || typeof layout !== 'object' || Array.isArray(layout)) {
256
+ return send(res, 400, {
257
+ error: isData
258
+ ? 'a data payload must be a JSON object keyed by dataset name'
259
+ : 'a report must be a JSON object'
260
+ });
261
+ }
262
+
263
+ /** created on demand: the user asked for this directory by naming it */
264
+ await mkdir(root, { recursive: true });
265
+
266
+ /** two spaces and a trailing newline, so the file diffs like source */
267
+ await writeFile(file, `${JSON.stringify(layout, null, 2)}\n`, 'utf8');
268
+
269
+ const info = await stat(file);
270
+
271
+ return send(res, 200, {
272
+ id,
273
+ kind: isData ? 'data' : 'layout',
274
+ name: typeof layout.name === 'string' ? layout.name : id,
275
+ updatedAt: info.mtime.toISOString()
276
+ });
277
+ }
278
+
279
+
280
+ async function remove(res, file, id) {
281
+ try {
282
+ await unlink(file);
283
+ } catch (error) {
284
+ if (error.code === 'ENOENT') {
285
+ return send(res, 404, { error: `there is no report called "${id}"` });
286
+ }
287
+ throw error;
288
+ }
289
+
290
+ /**
291
+ * The data file goes with it. Left behind, the next report saved under the
292
+ * same name would silently inherit the last one's data.
293
+ */
294
+ await unlink(file.replace(/\.json$/, '.data.json')).catch(() => { });
295
+
296
+ return send(res, 200, { id, deleted: true });
297
+ }
298
+
299
+
300
+ function readBody(req) {
301
+ return new Promise((resolve, reject) => {
302
+ const chunks = [];
303
+ let size = 0;
304
+
305
+ req.on('data', chunk => {
306
+ size += chunk.length;
307
+
308
+ if (size > MAX_BODY) {
309
+ reject(new Error(`a report may not be larger than ${MAX_BODY} bytes`));
310
+ req.destroy();
311
+ return;
312
+ }
313
+
314
+ chunks.push(chunk);
315
+ });
316
+
317
+ req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
318
+ req.on('error', reject);
319
+ });
320
+ }
321
+
322
+
323
+ function send(res, status, body) {
324
+ const text = JSON.stringify(body);
325
+
326
+ res.statusCode = status;
327
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
328
+ /** a report read a second ago is not the one on disk now */
329
+ res.setHeader('Cache-Control', 'no-store');
330
+ res.end(text);
331
+ }