report-studio 0.0.1 → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +238 -4
- package/dist/designer.js +3209 -0
- package/dist/index.js +2 -4
- package/dist/viewer-D-c0lKzQ.js +1576 -0
- package/package.json +66 -33
- package/src/asset/images/icon.png +0 -0
- package/src/asset/images/logo.png +0 -0
- package/src/cli/index.js +282 -0
- package/src/cli/serve.js +135 -0
- package/src/designer/blank.js +53 -0
- package/src/designer/canvas.js +518 -0
- package/src/designer/designer.js +1900 -0
- package/src/designer/dialog.js +708 -0
- package/src/designer/fields.js +584 -0
- package/src/designer/geometry.js +154 -0
- package/src/designer/guides.js +370 -0
- package/src/designer/history.js +113 -0
- package/src/designer/icons.js +118 -0
- package/src/designer/index.html +28 -0
- package/src/designer/main.js +39 -0
- package/src/designer/menu.js +126 -0
- package/src/designer/panel.js +478 -0
- package/src/designer/preview.js +95 -0
- package/src/designer/rulers.js +231 -0
- package/src/designer/sample-data.js +240 -0
- package/src/designer/select.js +699 -0
- package/src/designer/structure.js +604 -0
- package/src/designer/styles.css +1616 -0
- package/src/designer/toast.js +62 -0
- package/src/designer/tokens.js +284 -0
- package/src/engine/csv.js +315 -0
- package/src/engine/group.js +304 -0
- package/src/engine/index.js +34 -0
- package/src/engine/measure.js +390 -0
- package/src/engine/paginate.js +861 -0
- package/src/engine/regions.js +175 -0
- package/src/engine/resolve.js +171 -0
- package/src/engine/search.js +129 -0
- package/src/engine/text-metrics.js +206 -0
- package/src/engine/validate.js +328 -0
- package/src/index.html +79 -0
- package/src/index.js +50 -0
- package/src/preview/chrome.css +505 -0
- package/src/preview/chrome.js +147 -0
- package/src/preview/index.html +28 -0
- package/src/preview/main.js +66 -0
- package/src/preview/styles.css +16 -0
- package/src/preview/viewer.js +745 -0
- package/src/render/items.js +425 -0
- package/src/render/render.js +98 -0
- package/src/render/report.css +202 -0
- package/src/server/routes.js +331 -0
- package/src/shared/brand.css +29 -0
- package/src/shared/dropdown.css +136 -0
- package/src/shared/dropdown.js +166 -0
- package/src/shared/modal.css +125 -0
- package/src/shared/prefix.js +13 -0
- package/src/shared/store.js +145 -0
- package/src/shared/theme.css +72 -0
- package/src/shared/window.js +138 -0
- package/src/vite/index.js +53 -0
- package/dist/react.js +0 -4
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* report.css is how a report looks, wherever it is drawn.
|
|
3
|
+
*
|
|
4
|
+
* It is the stylesheet half of the rule in items.js: the preview and the
|
|
5
|
+
* designer canvas share their markup, so they must share their appearance too,
|
|
6
|
+
* or the design still drifts from the print - just through CSS instead of
|
|
7
|
+
* through HTML.
|
|
8
|
+
*
|
|
9
|
+
* Everything here is scoped under .page. The bare `p`/`table`/`th` selectors
|
|
10
|
+
* this replaced were safe while the whole window was the report; the designer
|
|
11
|
+
* has a properties panel full of its own paragraphs and tables, and would have
|
|
12
|
+
* had them absolutely positioned.
|
|
13
|
+
*
|
|
14
|
+
* What is NOT here: paging, zoom, and anything else about how a page is
|
|
15
|
+
* presented. Those differ between the two screens and belong to each.
|
|
16
|
+
*
|
|
17
|
+
* Every colour carries its own fallback, so an application that links this and
|
|
18
|
+
* nothing else gets a report that looks right. Without them a consumer would
|
|
19
|
+
* see `border: 1px solid var(--border)` resolve to nothing and conclude the
|
|
20
|
+
* table has no borders - and the two rules that are not decoration at all,
|
|
21
|
+
* `table-layout: fixed` and `p { margin: 0 }`, would be doing their work behind
|
|
22
|
+
* a sheet that appeared broken. Define the variables and this takes them.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
.page {
|
|
26
|
+
background: var(--surface, #fdfdfe);
|
|
27
|
+
position: relative;
|
|
28
|
+
overflow: hidden;
|
|
29
|
+
border: 1px solid var(--border, #cdd4db);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.band {
|
|
33
|
+
position: relative;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.page p {
|
|
37
|
+
position: absolute;
|
|
38
|
+
margin: 0;
|
|
39
|
+
white-space: pre-wrap;
|
|
40
|
+
line-height: 1.3;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.page table {
|
|
44
|
+
position: absolute;
|
|
45
|
+
border-collapse: collapse;
|
|
46
|
+
table-layout: fixed;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* No border, no padding, no height of the cell's own: the .cell box inside
|
|
51
|
+
* carries all three, so a row is exactly as tall as the engine budgeted it.
|
|
52
|
+
*
|
|
53
|
+
* Anything here is added to that height instead. `height` on a <tr> is only a
|
|
54
|
+
* minimum, and under border-collapse a 1px rule costs a further pixel a row -
|
|
55
|
+
* both invisible until the thirtieth row has pushed the detail band over the
|
|
56
|
+
* page footer. The engine paginates against headerHeight + rows x rowHeight,
|
|
57
|
+
* so that is what a row has to measure.
|
|
58
|
+
*/
|
|
59
|
+
.page th,
|
|
60
|
+
.page td {
|
|
61
|
+
border: none;
|
|
62
|
+
padding: 0;
|
|
63
|
+
overflow: hidden;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The font the engine wrapped this cell against. items.js sets these from the
|
|
67
|
+
* table's own style; the fallbacks are what measure.js assumes when it says
|
|
68
|
+
* nothing, so what is measured is always what is drawn.
|
|
69
|
+
*/
|
|
70
|
+
font-family: var(--rs-cell-family, Helvetica, Arial, sans-serif);
|
|
71
|
+
font-size: var(--rs-cell-size, 12px);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The cell's own box: the declared row height, with the padding and the rules
|
|
76
|
+
* drawn inside it. Each cell owns its top and left rule, so the grid lines
|
|
77
|
+
* between cells are drawn once, by the cell below and the cell to the right.
|
|
78
|
+
*/
|
|
79
|
+
.page .cell {
|
|
80
|
+
box-sizing: border-box;
|
|
81
|
+
padding: 0 8px;
|
|
82
|
+
overflow: hidden;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A column, so the text is vertically centred whether it came out one line or
|
|
86
|
+
* three - and so the anonymous box holding it fills the width, which is what
|
|
87
|
+
* lets `text-align` place each line rather than the block as a whole. A
|
|
88
|
+
* right-aligned column of wrapped text needs every line on the right, not the
|
|
89
|
+
* paragraph pushed over.
|
|
90
|
+
*/
|
|
91
|
+
display: flex;
|
|
92
|
+
flex-direction: column;
|
|
93
|
+
justify-content: center;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Cells wrap. They used to be cut off with an ellipsis, which kept a row
|
|
97
|
+
* exactly its declared height by throwing away somebody's data - an address
|
|
98
|
+
* that runs long is not less true for it. measure.js now works out how tall
|
|
99
|
+
* each row wraps to and pagination slices by that, so the row grows instead.
|
|
100
|
+
*
|
|
101
|
+
* A table can ask for the old behaviour back with `wrap: false`, which is the
|
|
102
|
+
* right answer for a column of figures that must stay on one line.
|
|
103
|
+
*/
|
|
104
|
+
white-space: var(--rs-cell-wrap, normal);
|
|
105
|
+
overflow-wrap: anywhere;
|
|
106
|
+
text-overflow: ellipsis;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The ratio text-metrics.js counts a wrapped line at. Left to the browser's
|
|
110
|
+
* "normal" it is nearer 1.15, and a row measured at three lines of 1.3 would
|
|
111
|
+
* print three lines of 1.15 with a gap under them - close enough to look like
|
|
112
|
+
* nothing until the gaps add up to a row that no longer fits the page.
|
|
113
|
+
*/
|
|
114
|
+
line-height: 1.3;
|
|
115
|
+
|
|
116
|
+
color: var(--rs-cell-color, inherit);
|
|
117
|
+
border-top: var(--rs-rule-width, 1px) var(--rs-rule-style, solid) var(--rs-rule-color, var(--border, #cdd4db));
|
|
118
|
+
border-left: var(--rs-rule-width, 1px) var(--rs-rule-style, solid) var(--rs-rule-color, var(--border, #cdd4db));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/*
|
|
122
|
+
* The grid the table asked for. items.js sets --rs-rule-style, --rs-rule-color
|
|
123
|
+
* and --rs-rule-width on the table when it has an opinion, and custom
|
|
124
|
+
* properties inherit, so one declaration up there reaches every cell. Each
|
|
125
|
+
* carries its own fallback, so a table that says nothing still takes the
|
|
126
|
+
* sheet's colour and a hairline.
|
|
127
|
+
*
|
|
128
|
+
* .cell is box-sizing: border-box with a fixed height, so the rule is drawn
|
|
129
|
+
* inside the row rather than added to it - which is what keeps a table exactly
|
|
130
|
+
* headerHeight + rows x rowHeight tall. items.js clamps the width to half the
|
|
131
|
+
* shallowest row so that stays true.
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
/* the outer right and bottom edges, drawn inside the cells that end the grid */
|
|
135
|
+
.page tr > :last-child .cell {
|
|
136
|
+
border-right: var(--rs-rule-width, 1px) var(--rs-rule-style, solid) var(--rs-rule-color, var(--border, #cdd4db));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.page tbody tr:last-child .cell {
|
|
140
|
+
border-bottom: var(--rs-rule-width, 1px) var(--rs-rule-style, solid) var(--rs-rule-color, var(--border, #cdd4db));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The header row. items.js sets --rs-head-bg and --rs-head-color on the table
|
|
146
|
+
* when it has an opinion; a table that says nothing keeps the quiet fill this
|
|
147
|
+
* sheet has always given it, so a report linked against report.css alone still
|
|
148
|
+
* looks like a report.
|
|
149
|
+
*/
|
|
150
|
+
.page th {
|
|
151
|
+
font-weight: 600;
|
|
152
|
+
background: var(--rs-head-bg, var(--surface-muted, #f1f3f5));
|
|
153
|
+
color: var(--rs-head-color, inherit);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A box is its own outline: w and h are the outside of it however thick the
|
|
158
|
+
* border, which is what the designer draws and what the engine measured.
|
|
159
|
+
*/
|
|
160
|
+
.page .box {
|
|
161
|
+
position: absolute;
|
|
162
|
+
box-sizing: border-box;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A line is a box with a rule down the middle of it, not a rule that is the box:
|
|
167
|
+
* see items.js. The box is what the designer can grab and what the engine
|
|
168
|
+
* paginates against; the mark inside it is what prints.
|
|
169
|
+
*/
|
|
170
|
+
.page .line {
|
|
171
|
+
position: absolute;
|
|
172
|
+
display: flex;
|
|
173
|
+
align-items: center;
|
|
174
|
+
justify-content: center;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.page .line-mark {
|
|
178
|
+
display: block;
|
|
179
|
+
width: 100%;
|
|
180
|
+
/* the one side the item asks for is set inline, and only that side is drawn */
|
|
181
|
+
border: 0 none;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.page .line.is-vertical .line-mark {
|
|
185
|
+
width: auto;
|
|
186
|
+
height: 100%;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* group bands live inside the table, so the cell must not add chrome of its own */
|
|
190
|
+
.page .group-band-cell {
|
|
191
|
+
padding: 0;
|
|
192
|
+
border: none;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/* warm, so a group break reads differently from a column header */
|
|
196
|
+
.page .group-header .group-band-cell {
|
|
197
|
+
background: var(--accent-soft, #fdf1e9);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.page .group-band-cell .band p {
|
|
201
|
+
padding: 0 8px;
|
|
202
|
+
}
|
|
@@ -0,0 +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
|
+
}
|