@vitrinka/web 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,14 +17,68 @@
17
17
  * 4xx) is skipped with a warning: the report still files, without it. The
18
18
  * reporter's files go before the replay — they are what they chose to send.
19
19
  * `reportUrl` is the server's — shown as returned, never composed.
20
+ *
21
+ * EVERY LEG HAS A DEADLINE (`deadline`): a request nothing answers — a server
22
+ * stuck in a handler, a proxy or connection holding it — rejects like a
23
+ * dropped connection, so the card shows Try again instead of "Sending…"
24
+ * forever. An upload's allowance grows with its bytes at a slow uplink's
25
+ * pace, so only a stalled one runs out. A create whose answer was lost after
26
+ * the server stored it is created again by Try again: the same risk a dropped
27
+ * connection already carries.
20
28
  */
21
29
  import { api, permanentStatus, uploadTo, VitrinkaApiError } from '../recorder/api';
22
30
  import { EVENTS_PER_POST } from './engine';
31
+ /** 30 s for a JSON leg; an upload adds its bytes at 32 KiB/s (≈ 256 kbit/s), so a 10 MiB file gets about six minutes. */
32
+ export const LEG_LIMITS = { ms: 30_000, bytesPerMs: 32 };
33
+ /** A leg ran past its deadline: retryable, never a refusal for good. */
34
+ export class LegTimeout extends Error {
35
+ constructor(what, ms) {
36
+ super(`vitrinka: ${what} got no answer in ${Math.round(ms / 1000)} s — Try again resumes it`);
37
+ this.name = 'LegTimeout';
38
+ }
39
+ }
40
+ /**
41
+ * Run `work` under a deadline: past `ms` its signal aborts (the fetch and its
42
+ * answer's body stop) and the returned promise rejects with LegTimeout —
43
+ * whether or not `work` honours the signal, so nothing waits on it forever.
44
+ */
45
+ export async function deadline(what, ms, work) {
46
+ const ctl = new AbortController();
47
+ let timer;
48
+ const expired = new Promise((_, reject) => {
49
+ timer = setTimeout(() => {
50
+ const e = new LegTimeout(what, ms);
51
+ ctl.abort(e);
52
+ reject(e);
53
+ }, ms);
54
+ });
55
+ try {
56
+ return await Promise.race([work(ctl.signal), expired]);
57
+ }
58
+ finally {
59
+ clearTimeout(timer);
60
+ }
61
+ }
62
+ /** What a report carries when its capture could not be prepared. */
63
+ export const EMPTY_CAPTURE = { chunks: [], truncated: false, console: [], network: [] };
64
+ /**
65
+ * The capture a report sends, under its deadline like every leg (reading an
66
+ * earlier load's stored windows can hang): past it, or failed, the report
67
+ * goes without one. A build that finishes later is dropped — it never
68
+ * reaches the job this answer started.
69
+ */
70
+ export function prepareCapture(build, ms = LEG_LIMITS.ms) {
71
+ return deadline('preparing the capture', ms, build).catch((e) => {
72
+ console.warn('vitrinka: the capture could not be prepared — the report goes without it', e);
73
+ return EMPTY_CAPTURE;
74
+ });
75
+ }
23
76
  export class ReportJob {
24
77
  capture;
25
78
  tabHost;
26
79
  target;
27
80
  files;
81
+ limits;
28
82
  id = '';
29
83
  custody = '';
30
84
  /** The editable fields as the server last received them. */
@@ -35,11 +89,23 @@ export class ReportJob {
35
89
  /** The files (by index) the server has — landed or refused for good. */
36
90
  filesDone = new Set();
37
91
  /** `target`: the widget's own base and key — never whatever another widget configured. `files`: the reporter's attachments, fixed for the job. */
38
- constructor(capture, tabHost, target, files = []) {
92
+ constructor(capture, tabHost, target, files = [], limits = LEG_LIMITS) {
39
93
  this.capture = capture;
40
94
  this.tabHost = tabHost;
41
95
  this.target = target;
42
96
  this.files = files;
97
+ this.limits = limits;
98
+ }
99
+ /** One JSON call under its leg deadline. */
100
+ call(method, path, body) {
101
+ return deadline(`${method} ${path}`, this.limits.ms, (signal) => api(method, path, body, { target: this.target, signal }));
102
+ }
103
+ /** One upload under a deadline that grows with its bytes. */
104
+ upload(path, contentType, body) {
105
+ // The bytes fetch sends: a JSON chunk's UTF-8, never its UTF-16 length (non-ASCII text runs up to 3× longer).
106
+ const bytes = typeof body === 'string' ? new TextEncoder().encode(body).byteLength : body.size;
107
+ const ms = this.limits.ms + Math.ceil(bytes / this.limits.bytesPerMs);
108
+ return deadline(`POST ${path}`, ms, (signal) => uploadTo(path, contentType, body, this.target, signal));
43
109
  }
44
110
  /** A file has not landed yet: the attachment leg is what a retry resumes. */
45
111
  get filesPending() {
@@ -47,17 +113,16 @@ export class ReportJob {
47
113
  }
48
114
  /** Run the legs from wherever the last run stopped; rejects with the failing leg's error. */
49
115
  async run(env) {
50
- const to = { target: this.target };
51
116
  const edits = { title: env.title, description: env.description, markers: env.markers, notify: env.notify ?? false };
52
117
  const editsKey = JSON.stringify(edits);
53
118
  if (!this.id) {
54
- const res = await api('POST', '/api/v1/reports', env, to).catch((e) => {
119
+ const res = await this.call('POST', '/api/v1/reports', env).catch((e) => {
55
120
  // A vitrinka older than the report type refuses the unknown field: the hint is optional, the report is not.
56
121
  if (env.type === undefined || !(e instanceof VitrinkaApiError) || e.status !== 400 || !/\btype\b/.test(e.message))
57
122
  throw e;
58
123
  console.warn('vitrinka: this server takes no report type yet — sending the report without it');
59
124
  const { type: _dropped, ...rest } = env;
60
- return api('POST', '/api/v1/reports', rest, to);
125
+ return this.call('POST', '/api/v1/reports', rest);
61
126
  });
62
127
  if (typeof res.id !== 'string' || !res.id)
63
128
  throw new Error('vitrinka: the server filed the report without an id');
@@ -66,7 +131,7 @@ export class ReportJob {
66
131
  this.sent = editsKey;
67
132
  }
68
133
  else if (this.sent !== editsKey) {
69
- await api('PATCH', `/api/v1/reports/${encodeURIComponent(this.id)}`, edits, to);
134
+ await this.call('PATCH', `/api/v1/reports/${encodeURIComponent(this.id)}`, edits);
70
135
  this.sent = editsKey;
71
136
  }
72
137
  await this.sendFiles();
@@ -81,7 +146,7 @@ export class ReportJob {
81
146
  this.skipCapture = true;
82
147
  }
83
148
  }
84
- const reply = await api('POST', `/api/v1/reports/${encodeURIComponent(this.id)}/submit`, {}, to);
149
+ const reply = await this.call('POST', `/api/v1/reports/${encodeURIComponent(this.id)}/submit`, {});
85
150
  return {
86
151
  status: reply.status === 'filed' ? 'filed' : 'quarantined',
87
152
  ...(typeof reply.reportUrl === 'string' && reply.reportUrl ? { reportUrl: reply.reportUrl } : {}),
@@ -93,7 +158,7 @@ export class ReportJob {
93
158
  if (this.filesDone.has(i))
94
159
  continue;
95
160
  try {
96
- await uploadTo(`/api/v1/reports/${id}/attachment?name=${encodeURIComponent(f.name)}`, f.type || 'application/octet-stream', f, this.target);
161
+ await this.upload(`/api/v1/reports/${id}/attachment?name=${encodeURIComponent(f.name)}`, f.type || 'application/octet-stream', f);
97
162
  }
98
163
  catch (e) {
99
164
  if (!(e instanceof VitrinkaApiError) || !permanentStatus(e.status))
@@ -109,7 +174,7 @@ export class ReportJob {
109
174
  for (const c of chunks) {
110
175
  if (this.blobKeys.has(c.seq))
111
176
  continue;
112
- const up = await uploadTo(`/api/v1/reports/${id}/chunk?seq=${c.seq}`, 'application/json', c.body, this.target);
177
+ const up = await this.upload(`/api/v1/reports/${id}/chunk?seq=${c.seq}`, 'application/json', c.body);
113
178
  if (!up.blobKey)
114
179
  throw new Error(`vitrinka: chunk ${c.seq} came back without a blob key`);
115
180
  this.blobKeys.set(c.seq, up.blobKey);
@@ -124,7 +189,7 @@ export class ReportJob {
124
189
  }));
125
190
  while (this.rowsPosted < rows.length) {
126
191
  const batch = rows.slice(this.rowsPosted, this.rowsPosted + EVENTS_PER_POST);
127
- await api('POST', `/api/v1/reports/${id}/events`, { events: batch }, { target: this.target });
192
+ await this.call('POST', `/api/v1/reports/${id}/events`, { events: batch });
128
193
  this.rowsPosted += batch.length;
129
194
  }
130
195
  }