snake-orm 0.1.0b1__py3-none-any.whl
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.
- snake_orm-0.1.0b1.dist-info/METADATA +366 -0
- snake_orm-0.1.0b1.dist-info/RECORD +198 -0
- snake_orm-0.1.0b1.dist-info/WHEEL +4 -0
- snake_orm-0.1.0b1.dist-info/entry_points.txt +2 -0
- snake_orm-0.1.0b1.dist-info/licenses/LICENSE +21 -0
- snakeorm/__init__.py +214 -0
- snakeorm/advisor.py +109 -0
- snakeorm/cli/__init__.py +3 -0
- snakeorm/cli/app.py +992 -0
- snakeorm/cli/discovery.py +179 -0
- snakeorm/cli/hooks.py +86 -0
- snakeorm/compiler/__init__.py +3 -0
- snakeorm/compiler/compiler.py +330 -0
- snakeorm/connection.py +228 -0
- snakeorm/contrib/__init__.py +17 -0
- snakeorm/contrib/asgi.py +192 -0
- snakeorm/contrib/config.py +264 -0
- snakeorm/contrib/deliver.py +256 -0
- snakeorm/contrib/django.py +245 -0
- snakeorm/contrib/sidecar.py +44 -0
- snakeorm/contrib/wsgi.py +176 -0
- snakeorm/core/__init__.py +1 -0
- snakeorm/core/config.py +173 -0
- snakeorm/core/converters.py +172 -0
- snakeorm/core/exceptions.py +175 -0
- snakeorm/core/placement.py +15 -0
- snakeorm/core/sentinels.py +37 -0
- snakeorm/core/signals.py +114 -0
- snakeorm/debug/__init__.py +42 -0
- snakeorm/debug/assets/css/panel.css +616 -0
- snakeorm/debug/assets/css/snake_orm_app.css +293 -0
- snakeorm/debug/assets/img/icon.svg +5 -0
- snakeorm/debug/assets/js/history.js +425 -0
- snakeorm/debug/assets/js/language.js +283 -0
- snakeorm/debug/assets/js/panel.js +241 -0
- snakeorm/debug/assets/js/snake_orm_app.js +82 -0
- snakeorm/debug/assets/pages/cli.html +28 -0
- snakeorm/debug/assets/pages/config.html +34 -0
- snakeorm/debug/assets/pages/database_first.html +29 -0
- snakeorm/debug/assets/pages/help.html +21 -0
- snakeorm/debug/assets/pages/history.html +27 -0
- snakeorm/debug/capture.py +234 -0
- snakeorm/debug/channel.py +131 -0
- snakeorm/debug/collector.py +144 -0
- snakeorm/debug/config.py +149 -0
- snakeorm/debug/html.py +578 -0
- snakeorm/debug/matrix.py +60 -0
- snakeorm/debug/origin.py +42 -0
- snakeorm/debug/otel/__init__.py +68 -0
- snakeorm/debug/otel/context.py +43 -0
- snakeorm/debug/otel/exporter.py +277 -0
- snakeorm/debug/otel/payload.py +97 -0
- snakeorm/debug/otel/report.py +29 -0
- snakeorm/debug/otel/spans.py +393 -0
- snakeorm/debug/otel/summary.py +59 -0
- snakeorm/debug/record.py +54 -0
- snakeorm/debug/report.py +311 -0
- snakeorm/debug/testing.py +23 -0
- snakeorm/decorators/__init__.py +13 -0
- snakeorm/decorators/abstract.py +26 -0
- snakeorm/decorators/db_first.py +104 -0
- snakeorm/decorators/function.py +28 -0
- snakeorm/decorators/model.py +268 -0
- snakeorm/decorators/polymorphic.py +140 -0
- snakeorm/decorators/result.py +167 -0
- snakeorm/decorators/row.py +90 -0
- snakeorm/decorators/trigger.py +47 -0
- snakeorm/decorators/view.py +149 -0
- snakeorm/dialects/__init__.py +6 -0
- snakeorm/dialects/base.py +373 -0
- snakeorm/dialects/capabilities.py +571 -0
- snakeorm/dialects/literals.py +46 -0
- snakeorm/dialects/matrix.py +384 -0
- snakeorm/dialects/mysql.py +519 -0
- snakeorm/dialects/postgres.py +504 -0
- snakeorm/dialects/sqlite.py +417 -0
- snakeorm/drivers/__init__.py +18 -0
- snakeorm/drivers/asyncbase.py +73 -0
- snakeorm/drivers/asyncdecorators.py +244 -0
- snakeorm/drivers/asyncpool.py +228 -0
- snakeorm/drivers/asyncpsycopg.py +124 -0
- snakeorm/drivers/asyncpymysql.py +50 -0
- snakeorm/drivers/asyncsqlite.py +30 -0
- snakeorm/drivers/base.py +76 -0
- snakeorm/drivers/failures.py +181 -0
- snakeorm/drivers/logging.py +167 -0
- snakeorm/drivers/pool.py +352 -0
- snakeorm/drivers/psycopg.py +188 -0
- snakeorm/drivers/pymysql.py +176 -0
- snakeorm/drivers/savepoints.py +46 -0
- snakeorm/drivers/sqlite.py +170 -0
- snakeorm/drivers/threaded.py +143 -0
- snakeorm/drivers/timeout.py +87 -0
- snakeorm/dto/__init__.py +55 -0
- snakeorm/dto/read.py +231 -0
- snakeorm/dto/region.py +326 -0
- snakeorm/dto/resolve.py +354 -0
- snakeorm/dto/spec.py +201 -0
- snakeorm/expressions/__init__.py +88 -0
- snakeorm/expressions/conditional.py +178 -0
- snakeorm/expressions/expression.py +810 -0
- snakeorm/expressions/functions.py +71 -0
- snakeorm/expressions/keys.py +299 -0
- snakeorm/expressions/paths.py +43 -0
- snakeorm/expressions/scalar.py +293 -0
- snakeorm/expressions/window.py +248 -0
- snakeorm/fields/__init__.py +68 -0
- snakeorm/fields/check.py +79 -0
- snakeorm/fields/column.py +316 -0
- snakeorm/fields/enum.py +64 -0
- snakeorm/fields/index.py +46 -0
- snakeorm/fields/relationship.py +738 -0
- snakeorm/fields/typed.py +736 -0
- snakeorm/helpers/__init__.py +1 -0
- snakeorm/helpers/annotations.py +21 -0
- snakeorm/helpers/inheritance.py +41 -0
- snakeorm/helpers/pyliteral.py +52 -0
- snakeorm/helpers/pytype.py +50 -0
- snakeorm/introspection/__init__.py +13 -0
- snakeorm/introspection/base.py +30 -0
- snakeorm/introspection/drift.py +78 -0
- snakeorm/introspection/models.py +633 -0
- snakeorm/introspection/mysql.py +367 -0
- snakeorm/introspection/postgres.py +397 -0
- snakeorm/introspection/sqlite.py +217 -0
- snakeorm/introspection/unsupported.py +94 -0
- snakeorm/linker/__init__.py +3 -0
- snakeorm/linker/linker.py +521 -0
- snakeorm/metadata/__init__.py +41 -0
- snakeorm/metadata/check.py +37 -0
- snakeorm/metadata/column.py +201 -0
- snakeorm/metadata/enum_storage.py +33 -0
- snakeorm/metadata/fk_action.py +19 -0
- snakeorm/metadata/foreign_key.py +26 -0
- snakeorm/metadata/index.py +43 -0
- snakeorm/metadata/index_method.py +19 -0
- snakeorm/metadata/int_size.py +20 -0
- snakeorm/metadata/json_storage.py +21 -0
- snakeorm/metadata/polymorphic.py +30 -0
- snakeorm/metadata/primary_key.py +23 -0
- snakeorm/metadata/relationship.py +60 -0
- snakeorm/metadata/relationship_kind.py +43 -0
- snakeorm/metadata/routine.py +26 -0
- snakeorm/metadata/server_default.py +20 -0
- snakeorm/metadata/table.py +97 -0
- snakeorm/metadata/table_kind.py +23 -0
- snakeorm/metadata/trigger.py +62 -0
- snakeorm/metadata/type_params.py +348 -0
- snakeorm/migration/__init__.py +88 -0
- snakeorm/migration/asyncrunner.py +168 -0
- snakeorm/migration/autodetect.py +96 -0
- snakeorm/migration/ddl.py +1172 -0
- snakeorm/migration/diff.py +701 -0
- snakeorm/migration/loader.py +77 -0
- snakeorm/migration/operations.py +952 -0
- snakeorm/migration/planner.py +78 -0
- snakeorm/migration/realize.py +420 -0
- snakeorm/migration/renames.py +153 -0
- snakeorm/migration/render.py +1095 -0
- snakeorm/migration/runner.py +314 -0
- snakeorm/migration/squash.py +60 -0
- snakeorm/migration/state.py +107 -0
- snakeorm/model.py +162 -0
- snakeorm/py.typed +0 -0
- snakeorm/query/__init__.py +9 -0
- snakeorm/query/compound.py +288 -0
- snakeorm/query/join_kind.py +19 -0
- snakeorm/query/joined.py +143 -0
- snakeorm/query/query.py +1001 -0
- snakeorm/query/recursive.py +244 -0
- snakeorm/registry/__init__.py +5 -0
- snakeorm/registry/by_module.py +163 -0
- snakeorm/registry/registry.py +166 -0
- snakeorm/session/__init__.py +8 -0
- snakeorm/session/asyncsession.py +638 -0
- snakeorm/session/coercion.py +306 -0
- snakeorm/session/factory.py +29 -0
- snakeorm/session/guards.py +269 -0
- snakeorm/session/isolation.py +23 -0
- snakeorm/session/mapper.py +213 -0
- snakeorm/session/planning.py +815 -0
- snakeorm/session/retry.py +96 -0
- snakeorm/session/session.py +871 -0
- snakeorm/session/shared.py +355 -0
- snakeorm/sql/__init__.py +18 -0
- snakeorm/sql/adapt.py +104 -0
- snakeorm/sql/aggregate.py +232 -0
- snakeorm/sql/condition.py +359 -0
- snakeorm/sql/delete.py +46 -0
- snakeorm/sql/insert.py +149 -0
- snakeorm/sql/joins.py +158 -0
- snakeorm/sql/pk_subquery.py +54 -0
- snakeorm/sql/refs.py +19 -0
- snakeorm/sql/resolver.py +28 -0
- snakeorm/sql/select.py +175 -0
- snakeorm/sql/update.py +82 -0
- snakeorm/sql/value.py +552 -0
- snakeorm/times.py +276 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/// <reference path="./language.js" />
|
|
2
|
+
/// <reference path="./panel.js" />
|
|
3
|
+
/**
|
|
4
|
+
* HISTORY module (`SnakeOrmHistory`) — the calls the page makes AFTER the render.
|
|
5
|
+
*
|
|
6
|
+
* The report above the panel is the one for the request that painted the page. Anything the page
|
|
7
|
+
* fetches later never reloads it, so those queries are invisible to every other view. This module
|
|
8
|
+
* stacks them, oldest first, in the tab the server left empty.
|
|
9
|
+
*
|
|
10
|
+
* It watches the TWO primitives a browser has — `fetch` and `XMLHttpRequest` — and no library's
|
|
11
|
+
* events. HTMX goes through XHR and everything else through `fetch`, so the primitives cover
|
|
12
|
+
* callers this file has never heard of; a list of libraries only ever finds the ones on the list.
|
|
13
|
+
*
|
|
14
|
+
* IT RUNS INSIDE SOMEBODY ELSE'S PAGE, and that is the rule every line below answers to. A throw
|
|
15
|
+
* here breaks THEIR application, not this panel, so: the wrappers hand back exactly what the
|
|
16
|
+
* original returned, they add no step to the app's own chain, everything of ours runs inside
|
|
17
|
+
* `safe`, and nothing is patched at all when the tab cannot show what it collects.
|
|
18
|
+
*/
|
|
19
|
+
const SnakeOrmHistory = (() => {
|
|
20
|
+
const safe = SnakeOrmPanel.safe;
|
|
21
|
+
|
|
22
|
+
/** Class names (they match the CSS and `pages/history.html`). @enum {string} */
|
|
23
|
+
const CLASS = {
|
|
24
|
+
view: 'snk-view-history', list: 'snk-history-list', metrics: 'snk-hmetrics',
|
|
25
|
+
entry: 'snk-h', head: 'snk-h-head', seq: 'snk-h-seq', path: 'snk-h-path',
|
|
26
|
+
status: 'snk-h-status', count: 'snk-h-n', body: 'snk-h-body', note: 'snk-h-note',
|
|
27
|
+
query: 'snk-h-q', sql: 'snk-h-sql', warn: 'snk-h-warn',
|
|
28
|
+
tag: 'snk-tag', read: 'snk-select', write: 'snk-write', ms: 'snk-ms', n: 'snk-qn',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* i18n keys this module paints. They are all declared HERE because the panel's translation test
|
|
33
|
+
* reads its list from this table: a key painted from somewhere else is a key nobody checks, and
|
|
34
|
+
* the server-side scan cannot see anything the client writes. @enum {string}
|
|
35
|
+
*/
|
|
36
|
+
const KEY = {
|
|
37
|
+
queries: 'queries', empty: 'empty',
|
|
38
|
+
warnings: 'hist_warnings', loading: 'hist_loading',
|
|
39
|
+
gone: 'hist_gone', failed: 'hist_failed', none: 'hist_none',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** `data-*` names (camelCase in `dataset`) and the value that switches the tab off. @enum {string} */
|
|
43
|
+
const DATA = { envelope: 'envelope', key: 't' };
|
|
44
|
+
const OFF = 'off';
|
|
45
|
+
|
|
46
|
+
/** The sidecar's own prefix: its reports are asked for here, and never become entries themselves. */
|
|
47
|
+
const SIDECAR = '/__snake__/';
|
|
48
|
+
|
|
49
|
+
/** Per-XHR markers. Symbols so nothing of ours can collide with a property the app set. */
|
|
50
|
+
const OPENED = Symbol('snakeorm-history-open');
|
|
51
|
+
const WATCHED = Symbol('snakeorm-history-watched');
|
|
52
|
+
|
|
53
|
+
// State (filled in by `mount`).
|
|
54
|
+
/** @type {(key: string) => string} */ let translate = (key) => key;
|
|
55
|
+
/** @type {(count: number, hot: boolean) => void} */ let onCount = () => {};
|
|
56
|
+
/** @type {HTMLElement | null} */ let list = null;
|
|
57
|
+
/** @type {HTMLElement | null} */ let metrics = null;
|
|
58
|
+
/** @type {typeof fetch | null} */ let plainFetch = null;
|
|
59
|
+
/**
|
|
60
|
+
* Every call folded so far: the cards are arithmetic over THIS list and nothing else.
|
|
61
|
+
* @type {{count: number | null, ms: number | null, mapping: number | null}[]}
|
|
62
|
+
*/
|
|
63
|
+
const calls = [];
|
|
64
|
+
let queries = 0;
|
|
65
|
+
let hot = false;
|
|
66
|
+
let seq = 0;
|
|
67
|
+
|
|
68
|
+
// ===== Reading what the ORM said about a call ================================================
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A `Server-Timing` header parsed into `{db: {dur, desc}, app: {...}, total: {...}}`.
|
|
72
|
+
* Unknown metrics come through untouched: other middleware writes to this header too.
|
|
73
|
+
*/
|
|
74
|
+
const timings = (header) => {
|
|
75
|
+
const found = {};
|
|
76
|
+
for (const part of String(header).split(',')) {
|
|
77
|
+
const [name, ...rest] = part.split(';');
|
|
78
|
+
if (!name.trim()) continue;
|
|
79
|
+
const metric = {};
|
|
80
|
+
for (const pair of rest) {
|
|
81
|
+
const at = pair.indexOf('=');
|
|
82
|
+
if (at > 0) metric[pair.slice(0, at).trim()] = pair.slice(at + 1).trim().replace(/^"|"$/g, '');
|
|
83
|
+
}
|
|
84
|
+
found[name.trim()] = metric;
|
|
85
|
+
}
|
|
86
|
+
return found;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** How many queries a `db;desc="3 queries"` announces, or null when the header does not say. */
|
|
90
|
+
const announced = (metric) => {
|
|
91
|
+
const match = /^(\d+)\s/.exec((metric && metric.desc) || '');
|
|
92
|
+
return match ? Number(match[1]) : null;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** A number, or null when there is nothing to read. Absent is NOT zero and is not painted as one. */
|
|
96
|
+
const number = (value) => (value === undefined || value === null || value === '' ? null : Number(value));
|
|
97
|
+
|
|
98
|
+
/** The ORM's report inside an envelope body: the sibling key, on the object or on its `{data}` wrapper. */
|
|
99
|
+
const reportIn = (payload) => (payload && typeof payload === 'object' ? payload.snakeorm || null : null);
|
|
100
|
+
|
|
101
|
+
/** True when a report carries something the panel would light the badge for. */
|
|
102
|
+
const burns = (report) => Boolean(report && report.warnings && report.warnings.length);
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Folds one finished call into the entry it deserves, or null when the ORM said nothing about it.
|
|
106
|
+
* An entry exists because a REPORT arrived — by header or by body. Without that rule the tab
|
|
107
|
+
* would fill with every image, font and beacon the page happens to request.
|
|
108
|
+
*/
|
|
109
|
+
const observed = (call) => {
|
|
110
|
+
const header = call.header('Server-Timing');
|
|
111
|
+
const report = call.report;
|
|
112
|
+
if (!header && !report) return null;
|
|
113
|
+
const timed = header ? timings(header) : {};
|
|
114
|
+
const db = timed.db || null;
|
|
115
|
+
const map = timed.map || null;
|
|
116
|
+
return {
|
|
117
|
+
method: call.method,
|
|
118
|
+
path: call.path,
|
|
119
|
+
status: call.status,
|
|
120
|
+
count: report ? number(report.count) : announced(db),
|
|
121
|
+
ms: report ? number(report.db_ms) : number(db && db.dur),
|
|
122
|
+
mapping: report ? number(report.mapping_ms) : number(map && map.dur),
|
|
123
|
+
token: call.header('X-Debug-Token'),
|
|
124
|
+
report,
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// ===== The tab's own aggregate cards ==========================================================
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* NOT EVERY ENTRY BRINGS THE SAME DATA, and that is the whole design of this block. A JSON call
|
|
132
|
+
* carries the full report in its body; an HTMX one carries only what fits in `Server-Timing`, and
|
|
133
|
+
* with the `sidecar` channel off there is not even a token to ask for the detail. So a call can
|
|
134
|
+
* arrive with no query count, or with no milliseconds.
|
|
135
|
+
*
|
|
136
|
+
* A call that did not report a number is NOT a zero. Summing the zeros gives a total that LOOKS
|
|
137
|
+
* exact and is false, which is the one thing this panel exists not to do. Every aggregate carries
|
|
138
|
+
* instead how much of the list it is computed over ("3/5"), and the card shows it.
|
|
139
|
+
*/
|
|
140
|
+
const sum = (values) => values.reduce((total, value) => total + value, 0);
|
|
141
|
+
const top = (values) => values.reduce((best, value) => (value > best ? value : best));
|
|
142
|
+
|
|
143
|
+
/** Folds one field over the calls, DROPPING the ones that did not report it. */
|
|
144
|
+
const fold = (field, reduce) => {
|
|
145
|
+
const known = calls.map((call) => call[field]).filter((value) => value !== null);
|
|
146
|
+
return {
|
|
147
|
+
known: known.length,
|
|
148
|
+
total: calls.length,
|
|
149
|
+
value: known.length ? reduce(known) : null,
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/** Writes a value keeping the `<small>ms</small>` the server painted: only the number changes. */
|
|
154
|
+
const setValue = (node, text) => {
|
|
155
|
+
if (node && node.firstChild) node.firstChild.nodeValue = text;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const asInt = (value) => String(value);
|
|
159
|
+
const asMs = (value) => value.toFixed(2);
|
|
160
|
+
|
|
161
|
+
/** Paints one card: the number, and the coverage when it is not the whole list. */
|
|
162
|
+
const paintCard = (name, folded, format) => {
|
|
163
|
+
if (!metrics) return;
|
|
164
|
+
const shown = folded.value === null ? '—' : format(folded.value);
|
|
165
|
+
setValue(metrics.querySelector(`[data-hm="${name}"]`), shown);
|
|
166
|
+
const part = metrics.querySelector(`[data-hm-part="${name}"]`);
|
|
167
|
+
if (part) {
|
|
168
|
+
part.textContent = folded.known < folded.total ? `${folded.known}/${folded.total}` : '';
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** Recomputes every card from the list already in the client: no request, no new data. */
|
|
173
|
+
const repaint = () => {
|
|
174
|
+
const total = calls.length;
|
|
175
|
+
paintCard('calls', { known: total, total, value: total }, asInt);
|
|
176
|
+
paintCard('queries', fold('count', sum), asInt);
|
|
177
|
+
paintCard('db', fold('ms', sum), asMs);
|
|
178
|
+
paintCard('map', fold('mapping', sum), asMs);
|
|
179
|
+
paintCard('slowest', fold('ms', top), asMs);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// ===== Painting ===============================================================================
|
|
183
|
+
|
|
184
|
+
const el = (tag, className, text) => {
|
|
185
|
+
const node = document.createElement(tag);
|
|
186
|
+
if (className) node.className = className;
|
|
187
|
+
if (text !== undefined && text !== null) node.textContent = String(text);
|
|
188
|
+
return node;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** A translated span that KEEPS its key, so switching language later reaches it like any other. */
|
|
192
|
+
const label = (key) => {
|
|
193
|
+
const node = el('span', null, translate(key));
|
|
194
|
+
node.dataset[DATA.key] = key;
|
|
195
|
+
return node;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/** A one-line note in the body of an entry (loading, gone, nothing to show). */
|
|
199
|
+
const note = (key) => {
|
|
200
|
+
const node = el('p', CLASS.note, translate(key));
|
|
201
|
+
node.dataset[DATA.key] = key;
|
|
202
|
+
return node;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/** Fills a body with a report: its warnings first, then one line per query. */
|
|
206
|
+
const paintReport = (body, report) => {
|
|
207
|
+
body.replaceChildren();
|
|
208
|
+
const warnings = report.warnings || [];
|
|
209
|
+
if (warnings.length) {
|
|
210
|
+
const title = el('p', CLASS.warn);
|
|
211
|
+
title.append(label(KEY.warnings));
|
|
212
|
+
body.append(title);
|
|
213
|
+
for (const warning of warnings) body.append(el('p', CLASS.warn, warning));
|
|
214
|
+
}
|
|
215
|
+
for (const query of report.queries || []) {
|
|
216
|
+
const row = el('div', CLASS.query);
|
|
217
|
+
row.append(
|
|
218
|
+
el('span', CLASS.n, `#${query.n}`),
|
|
219
|
+
el('code', CLASS.sql, query.sql),
|
|
220
|
+
el('span', CLASS.ms, `${Number(query.ms).toFixed(2)} ms`),
|
|
221
|
+
);
|
|
222
|
+
body.append(row);
|
|
223
|
+
}
|
|
224
|
+
if (!body.childElementCount) body.append(note(KEY.empty));
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Asks the sidecar for the report of `token`, ONCE, the first time the entry is opened.
|
|
229
|
+
* `404` is not a failure to hide: the ring buffer evicted it, and the entry says so.
|
|
230
|
+
*/
|
|
231
|
+
const wireDetail = (details, body, token) => {
|
|
232
|
+
let asked = false;
|
|
233
|
+
details.addEventListener('toggle', () => {
|
|
234
|
+
if (!details.open || asked) return;
|
|
235
|
+
asked = true;
|
|
236
|
+
body.replaceChildren(note(KEY.loading));
|
|
237
|
+
// The ORIGINAL `fetch`: our own request is not one of the page's calls.
|
|
238
|
+
plainFetch(`${SIDECAR}${token}`, {
|
|
239
|
+
headers: { Accept: 'application/json' },
|
|
240
|
+
credentials: 'same-origin',
|
|
241
|
+
})
|
|
242
|
+
.then((response) => (response.ok ? response.json() : Promise.reject(response.status)))
|
|
243
|
+
.then((report) => {
|
|
244
|
+
paintReport(body, report);
|
|
245
|
+
if (burns(report)) bump(0, true);
|
|
246
|
+
})
|
|
247
|
+
.catch((status) => body.replaceChildren(note(status === 404 ? KEY.gone : KEY.failed)));
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/** One entry: a `<details>` (native, no state to keep) with the line, and the detail underneath. */
|
|
252
|
+
const entry = (call) => {
|
|
253
|
+
const item = document.createElement('li');
|
|
254
|
+
const details = el('details', CLASS.entry);
|
|
255
|
+
const head = el('summary', CLASS.head);
|
|
256
|
+
head.append(
|
|
257
|
+
el('span', CLASS.seq, `+${seq}`),
|
|
258
|
+
el('span', `${CLASS.tag} ${call.method === 'GET' ? CLASS.read : CLASS.write}`, call.method),
|
|
259
|
+
el('span', CLASS.path, call.path),
|
|
260
|
+
);
|
|
261
|
+
if (call.status) head.append(el('span', CLASS.status, call.status));
|
|
262
|
+
if (call.count !== null) {
|
|
263
|
+
const count = el('span', CLASS.count, `${call.count} `);
|
|
264
|
+
count.append(label(KEY.queries));
|
|
265
|
+
head.append(count);
|
|
266
|
+
}
|
|
267
|
+
if (call.ms !== null) head.append(el('span', CLASS.ms, `${call.ms.toFixed(2)} ms`));
|
|
268
|
+
|
|
269
|
+
const body = el('div', CLASS.body);
|
|
270
|
+
if (call.report) paintReport(body, call.report);
|
|
271
|
+
else if (call.token && plainFetch) wireDetail(details, body, call.token);
|
|
272
|
+
else body.append(note(KEY.none));
|
|
273
|
+
|
|
274
|
+
details.append(head, body);
|
|
275
|
+
item.append(details);
|
|
276
|
+
return item;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
/** Extends the badge: it counts the cost since the page loaded, so it only ever grows. */
|
|
280
|
+
const bump = (count, burning) => {
|
|
281
|
+
queries += count;
|
|
282
|
+
hot = hot || burning;
|
|
283
|
+
safe(() => onCount(queries, hot));
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/** Puts one call at the TOP: the newest is what you opened the panel to see, so it needs no scroll. */
|
|
287
|
+
const record = (call) => {
|
|
288
|
+
const painted = observed(call);
|
|
289
|
+
if (!painted) return;
|
|
290
|
+
seq += 1;
|
|
291
|
+
list.prepend(entry(painted));
|
|
292
|
+
calls.push(painted);
|
|
293
|
+
safe(repaint);
|
|
294
|
+
bump(painted.count || 0, burns(painted.report));
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// ===== Watching the two primitives ============================================================
|
|
298
|
+
|
|
299
|
+
/** Calls this module has nothing to say about: another origin, or the sidecar answering us. */
|
|
300
|
+
const ignored = (url) => url.origin !== location.origin || url.pathname.startsWith(SIDECAR);
|
|
301
|
+
|
|
302
|
+
/** The verb of a `fetch(input, init)` call: the init wins, then a `Request`, then the default. */
|
|
303
|
+
const verbOf = (args) => {
|
|
304
|
+
const [input, init] = args;
|
|
305
|
+
return String((init && init.method) || (input && input.method) || 'GET').toUpperCase();
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Wraps `fetch`. The caller gets the ORIGINAL promise back: no extra tick, no new object, no
|
|
310
|
+
* chance of us changing the order anything settles in. We only branch off it to look.
|
|
311
|
+
*
|
|
312
|
+
* The empty rejection handler is what stops OUR branch from becoming an unhandled rejection of
|
|
313
|
+
* its own on a failed request.
|
|
314
|
+
*/
|
|
315
|
+
const watchFetch = () => {
|
|
316
|
+
const original = window.fetch;
|
|
317
|
+
if (typeof original !== 'function') return null;
|
|
318
|
+
window.fetch = function (...args) {
|
|
319
|
+
const promise = original.apply(this, args);
|
|
320
|
+
safe(() => promise.then((response) => safe(() => fromFetch(response, args)), () => {}));
|
|
321
|
+
return promise;
|
|
322
|
+
};
|
|
323
|
+
return original;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/** Reads a finished `fetch`, taking the JSON body off a CLONE so the app still gets its own. */
|
|
327
|
+
const fromFetch = (response, args) => {
|
|
328
|
+
const url = new URL(response.url || String(args[0]), location.href);
|
|
329
|
+
if (ignored(url)) return;
|
|
330
|
+
const header = (name) => response.headers.get(name);
|
|
331
|
+
const call = {
|
|
332
|
+
method: verbOf(args),
|
|
333
|
+
path: url.pathname + url.search,
|
|
334
|
+
status: response.status,
|
|
335
|
+
header,
|
|
336
|
+
report: null,
|
|
337
|
+
};
|
|
338
|
+
const type = header('Content-Type') || '';
|
|
339
|
+
if (!type.includes('application/json') || response.bodyUsed) {
|
|
340
|
+
record(call);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
// `clone()` tees the stream instead of copying it, and this reader drains its half right away.
|
|
344
|
+
const copy = response.clone();
|
|
345
|
+
copy.json().then(
|
|
346
|
+
(payload) => safe(() => record({ ...call, report: reportIn(payload) })),
|
|
347
|
+
() => safe(() => record(call)),
|
|
348
|
+
);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Wraps `XMLHttpRequest`. `open` only notes the verb and the url (a `Response` carries neither),
|
|
353
|
+
* and `send` adds ONE `load` listener — added, never assigned, so nothing of the app's is
|
|
354
|
+
* displaced and its own handlers keep running first.
|
|
355
|
+
*/
|
|
356
|
+
const watchXhr = () => {
|
|
357
|
+
const proto = XMLHttpRequest.prototype;
|
|
358
|
+
const open = proto.open;
|
|
359
|
+
const send = proto.send;
|
|
360
|
+
proto.open = function (...args) {
|
|
361
|
+
safe(() => {
|
|
362
|
+
this[OPENED] = { method: String(args[0] || 'GET').toUpperCase(), url: String(args[1] || '') };
|
|
363
|
+
});
|
|
364
|
+
return open.apply(this, args);
|
|
365
|
+
};
|
|
366
|
+
proto.send = function (...args) {
|
|
367
|
+
safe(() => {
|
|
368
|
+
if (this[WATCHED]) return;
|
|
369
|
+
this[WATCHED] = true;
|
|
370
|
+
this.addEventListener('load', () => safe(() => fromXhr(this)));
|
|
371
|
+
});
|
|
372
|
+
return send.apply(this, args);
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/** Reads a finished XHR. Free, unlike `fetch`: the body is already a string the app keeps. */
|
|
377
|
+
const fromXhr = (xhr) => {
|
|
378
|
+
const opened = xhr[OPENED] || {};
|
|
379
|
+
const url = new URL(xhr.responseURL || opened.url || '', location.href);
|
|
380
|
+
if (ignored(url)) return;
|
|
381
|
+
const header = (name) => xhr.getResponseHeader(name);
|
|
382
|
+
record({
|
|
383
|
+
method: opened.method || 'GET',
|
|
384
|
+
path: url.pathname + url.search,
|
|
385
|
+
status: xhr.status,
|
|
386
|
+
header,
|
|
387
|
+
report: reportIn(safe(() => jsonOf(xhr, header('Content-Type') || ''))),
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The parsed JSON body of an XHR, asked for the way its `responseType` allows: reading
|
|
393
|
+
* `responseText` on a typed response throws, which is a throw inside somebody else's page.
|
|
394
|
+
*/
|
|
395
|
+
const jsonOf = (xhr, type) => {
|
|
396
|
+
if (!type.includes('application/json')) return null;
|
|
397
|
+
if (xhr.responseType === 'json') return xhr.response;
|
|
398
|
+
if (xhr.responseType === '' || xhr.responseType === 'text') return JSON.parse(xhr.responseText);
|
|
399
|
+
return null;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
// ===== Mounting ===============================================================================
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Wires the tab and starts watching. It patches NOTHING and returns false when there is no list
|
|
406
|
+
* to paint into or the `envelope` channel is off: the tab already explains itself in that case,
|
|
407
|
+
* and patching a page's globals to collect what nobody can see is all cost and no feature.
|
|
408
|
+
* @param {ShadowRoot | HTMLElement} root
|
|
409
|
+
* @param {{translate: (key: string) => string, queries: number, onCount: (count: number, hot: boolean) => void}} wiring
|
|
410
|
+
*/
|
|
411
|
+
const mount = (root, wiring) => {
|
|
412
|
+
const view = root.querySelector(`.${CLASS.view}`);
|
|
413
|
+
list = root.querySelector(`.${CLASS.list}`);
|
|
414
|
+
metrics = root.querySelector(`.${CLASS.metrics}`);
|
|
415
|
+
if (!view || !list || view.dataset[DATA.envelope] === OFF) return false;
|
|
416
|
+
translate = wiring.translate;
|
|
417
|
+
queries = wiring.queries || 0;
|
|
418
|
+
onCount = wiring.onCount;
|
|
419
|
+
plainFetch = watchFetch();
|
|
420
|
+
watchXhr();
|
|
421
|
+
return true;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
return { CLASS, KEY, mount };
|
|
425
|
+
})();
|