@luutuankiet/clipboard2markdown 0.2.2 โ†’ 0.3.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/README.md CHANGED
@@ -24,7 +24,7 @@ Workflow platforms don't let you copy as Markdown โ€” you get rich text. But you
24
24
 
25
25
  - **๐Ÿ”„ Turndown.js Engine** โ€” Robust GFM support (tables, task lists, strikethrough)
26
26
  - **๐Ÿงน Platform Sanitizers** โ€” Pre-process quirky HTML before conversion
27
- - **๐Ÿ”’ Client-Side** โ€” Nothing you paste leaves your browser. The one exception to *any* network traffic is the diagrams mode (below), which downloads the Excalidraw scenes your pasted links point to, read-only
27
+ - **๐Ÿ”’ Client-Side** โ€” Nothing you paste leaves your machine. The only network traffic is read-only downloads of the Excalidraw scenes your links point to: on the page in the diagrams mode, and in the `c2m` clipboard command when it converts rich text to Markdown (turn that off with `--no-diagrams`)
28
28
  - **๐Ÿงญ Diagram round trip** โ€” Turn excalidraw.com share links into Mermaid that agents can read, and let agents edit a canvas by link (see below)
29
29
  - **๐Ÿงช Automated Testing** โ€” Fixture-based regression suite prevents breakage
30
30
  - **โšก Vite Build System** โ€” Hot reload dev server, optimized production builds
@@ -81,7 +81,25 @@ npm install -g @luutuankiet/clipboard2markdown
81
81
  c2m --help
82
82
  ```
83
83
 
84
- With no subcommand, `c2m` reads the clipboard, converts it (HTML to Markdown, or Markdown to HTML), and writes the result back. Clipboard conversion needs macOS. `c2m excalidraw` and `c2m diagrams` run anywhere Node 20+ runs. `c2m-diagrams` still works as an alias of `c2m diagrams`.
84
+ With no subcommand, `c2m` reads the clipboard, converts it (HTML to Markdown, or Markdown to HTML), and writes the result back. Clipboard conversion needs macOS.
85
+
86
+ It handles Excalidraw share links in both directions:
87
+
88
+ - **Rich text โ†’ Markdown:** every excalidraw.com share link gets a Mermaid reading copy under it, as `c2m diagrams expand` produces. This downloads each scene read-only (one `GET` per link to `json.excalidraw.com`; the key in the link's `#` fragment is never sent). All links download at once, temporary failures are retried, and the step gives up after 6 seconds. A diagram that still cannot be fetched keeps its link with no block.
89
+ - **Markdown โ†’ HTML:** generated reading copies are removed first, so only the link reaches Slack or a chat box. Mermaid you wrote yourself is kept. No network.
90
+ - `--no-diagrams` skips both steps.
91
+
92
+ stdout stays empty on a clean run and gets one line when something needs attention, for example `c2m: 2 of 3 diagrams skipped (timeout, 404)`, or the empty-clipboard and conversion-failed messages. A keyboard macro can notify only when stdout is not empty. Exit codes: `0` converted (also when diagrams were skipped), `1` empty clipboard, `2` conversion failed.
93
+
94
+ For a macro, a global install run by absolute path skips npx's registry lookup (about 1.7s per run):
95
+
96
+ ```bash
97
+ npm i -g @luutuankiet/clipboard2markdown@latest
98
+ # prints the line to put in the macro; launchers often don't load your PATH
99
+ echo "$(command -v node) $(npm root -g)/@luutuankiet/clipboard2markdown/bin/cli.js 2>/dev/null"
100
+ ```
101
+
102
+ A global install belongs to one Node version: after switching or upgrading Node (nvm), reinstall and update the macro's path. `c2m excalidraw` and `c2m diagrams` run anywhere Node 20+ runs. `c2m-diagrams` still works as an alias of `c2m diagrams`.
85
103
 
86
104
  ## Development
87
105
 
package/bin/cli.js CHANGED
@@ -9,11 +9,19 @@ const HELP = `clipboard2markdown (short name: c2m) - clipboard HTML <-> Markdown
9
9
  and Excalidraw diagrams for agents
10
10
 
11
11
  Usage:
12
- c2m HTML on the clipboard -> Markdown, plain text -> HTML
13
- c2m --to-md force HTML -> Markdown
14
- c2m --to-html force Markdown -> HTML
15
- c2m --help show this help
16
- c2m --version print the version
12
+ c2m HTML on the clipboard -> Markdown, plain text -> HTML
13
+ c2m --to-md force HTML -> Markdown
14
+ c2m --to-html force Markdown -> HTML
15
+ c2m --no-diagrams skip the Excalidraw step below (no network at all)
16
+ c2m --help show this help
17
+ c2m --version print the version
18
+
19
+ Excalidraw share links: HTML -> Markdown adds a Mermaid reading copy under
20
+ every excalidraw.com link, downloading each scene read-only (one GET per link
21
+ to json.excalidraw.com; the key in the link's # fragment is never sent).
22
+ Markdown -> HTML removes generated reading copies first, so only the link is
23
+ pasted; Mermaid you wrote yourself is kept. A diagram that cannot be fetched
24
+ in a few seconds is skipped and its link left as written.
17
25
 
18
26
  c2m excalidraw <file|-> JSON diagram -> excalidraw.com share link and
19
27
  Mermaid preview (see c2m excalidraw --help)
@@ -26,6 +34,8 @@ Every command also runs without installing:
26
34
  Clipboard conversion needs macOS (it uses osascript); excalidraw and
27
35
  diagrams run anywhere Node 20+ runs.
28
36
  Exit codes: 0 converted, 1 empty clipboard, 2 conversion failed.
37
+ stdout is empty on a clean clipboard run; it gets one line when something
38
+ needs attention (skipped diagrams, empty clipboard, failed conversion).
29
39
  `;
30
40
 
31
41
  // Converters need jsdom's DOM globals; loaded lazily so commands that
@@ -115,20 +125,9 @@ pb.setStringForType(str, "public.utf8-plain-text");
115
125
  }
116
126
  }
117
127
 
118
- // --- core logic (exported for testing) ---
119
-
120
- /**
121
- * Determine conversion direction.
122
- * @param {'html'|'text'} clipboardType - what the pasteboard held
123
- * @param {boolean} forceToMd - --to-md flag was passed
124
- * @param {boolean} forceToHtml - --to-html flag was passed
125
- * @returns {'to-md'|'to-html'}
126
- */
127
- export function detectDirection(clipboardType, forceToMd, forceToHtml) {
128
- if (forceToMd) return 'to-md';
129
- if (forceToHtml) return 'to-html';
130
- return clipboardType === 'html' ? 'to-md' : 'to-html';
131
- }
128
+ // --- core logic lives in src/clipboard-convert.js; re-exported for tests ---
129
+
130
+ export { detectDirection } from '../src/clipboard-convert.js';
132
131
 
133
132
  // --- entry point (only runs when executed directly) ---
134
133
 
@@ -173,29 +172,36 @@ if (isEntryPoint()) {
173
172
  process.exit(2);
174
173
  }
175
174
 
176
- const forceToMd = args.includes('--to-md');
177
- const forceToHtml = args.includes('--to-html');
175
+ // stdout carries one line only when something needs attention, so a macro
176
+ // can notify on "stdout not empty"; stderr keeps the diagnostics
177
+ const notify = (line) => {
178
+ console.error(line);
179
+ process.stdout.write(line + '\n');
180
+ };
178
181
 
179
182
  try {
180
- const { convert, convertMdToHtml } = await loadConverters();
183
+ const { convertClipboard } = await import('../src/clipboard-convert.js');
184
+ const converters = await loadConverters();
181
185
  const clipboard = readClipboard();
182
186
  if (!clipboard.content || !clipboard.content.trim()) {
183
- console.error('c2m: empty clipboard');
187
+ notify('c2m: empty clipboard');
184
188
  process.exit(1);
185
189
  }
186
- const direction = detectDirection(clipboard.type, forceToMd, forceToHtml);
187
- if (direction === 'to-md') {
188
- const md = convert(clipboard.content);
189
- console.error(`c2m: ${clipboard.type} โ†’ md (${md.length} chars)`);
190
- writeTextToClipboard(md);
191
- } else {
192
- const html = convertMdToHtml(clipboard.content);
193
- console.error(`c2m: ${clipboard.type} โ†’ html (${html.length} chars)`);
194
- writeHtmlToClipboard(html);
195
- }
190
+ const result = await convertClipboard({
191
+ content: clipboard.content,
192
+ type: clipboard.type,
193
+ forceToMd: args.includes('--to-md'),
194
+ forceToHtml: args.includes('--to-html'),
195
+ diagrams: !args.includes('--no-diagrams'),
196
+ converters,
197
+ });
198
+ console.error(`c2m: ${clipboard.type} โ†’ ${result.kind} (${result.output.length} chars)`);
199
+ if (result.kind === 'md') writeTextToClipboard(result.output);
200
+ else writeHtmlToClipboard(result.output);
201
+ if (result.notice) notify(result.notice);
196
202
  process.exit(0);
197
203
  } catch (e) {
198
- console.error(`c2m: ${e.message}`);
204
+ notify(`c2m: ${e.message}`);
199
205
  process.exit(2);
200
206
  }
201
207
  }
package/bin/diagrams.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  expandDiagramLinks, stripDiagramBlocks, loadScene, sceneToMermaid, editLink,
14
14
  parseShareLink, normalizeScene,
15
15
  } from '../src/diagrams/index.js';
16
+ import { retryingFetch } from '../src/diagrams/retry.js';
16
17
 
17
18
  const USAGE = `usage: c2m diagrams <command> [options]
18
19
 
@@ -30,7 +31,8 @@ ops: [{"op":"rename","node","label"}, {"op":"add","id","label","near","side"?},
30
31
  `;
31
32
 
32
33
  // Test hook: a directory standing in for json.excalidraw.com. GET reads
33
- // <dir>/<id>, POST writes a new file. Never set this outside tests.
34
+ // <dir>/<id>, POST writes a new file. A file <dir>/<id>.fail holding a status
35
+ // answers one GET with that status and is consumed. Never set this outside tests.
34
36
  function fileBackendFetch(dir) {
35
37
  return async function (url, init) {
36
38
  var method = ((init && init.method) || 'GET').toUpperCase();
@@ -48,6 +50,11 @@ function fileBackendFetch(dir) {
48
50
  return respond(200, Buffer.from(JSON.stringify({ id: id })));
49
51
  }
50
52
  var file = path.join(dir, url.split('/').pop());
53
+ if (fs.existsSync(file + '.fail')) {
54
+ var status = Number(fs.readFileSync(file + '.fail', 'utf8'));
55
+ fs.unlinkSync(file + '.fail');
56
+ return respond(status, Buffer.from('{}'));
57
+ }
51
58
  if (!fs.existsSync(file)) return respond(404, Buffer.from('{}'));
52
59
  return respond(200, fs.readFileSync(file));
53
60
  };
@@ -93,8 +100,10 @@ export async function main(argv, io) {
93
100
  out = stripDiagramBlocks(md);
94
101
  } else {
95
102
  var report = {};
103
+ var retrying = retryingFetch(fetchImpl);
96
104
  out = await expandDiagramLinks(md, {
97
- loadScene: function (url) { return loadScene(url, { fetch: fetchImpl }); },
105
+ loadScene: function (url) { return loadScene(url, { fetch: retrying }); },
106
+ parallel: true,
98
107
  onProgress: function (p) { if (p.total) stderr.write('expanding ' + p.done + ' of ' + p.total + ' diagrams\n'); },
99
108
  report: report,
100
109
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luutuankiet/clipboard2markdown",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Convert rich clipboard HTML from Jira, Confluence, Slack and Google apps into clean Markdown, and back.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,71 @@
1
+ // What the clipboard command does to the clipboard's content, without the
2
+ // clipboard: bin/cli.js keeps only pasteboard I/O, output streams and exit codes.
3
+ //
4
+ // Rich text -> Markdown, then a Mermaid reading copy under every excalidraw.com
5
+ // share link. Markdown -> HTML, with every generated reading copy removed first
6
+ // so only the link reaches the HTML. `diagrams: false` skips both steps and
7
+ // makes no request.
8
+
9
+ /**
10
+ * Determine conversion direction.
11
+ * @param {'html'|'text'} clipboardType - what the pasteboard held
12
+ * @param {boolean} forceToMd - --to-md flag was passed
13
+ * @param {boolean} forceToHtml - --to-html flag was passed
14
+ * @returns {'to-md'|'to-html'}
15
+ */
16
+ export function detectDirection(clipboardType, forceToMd, forceToHtml) {
17
+ if (forceToMd) return 'to-md';
18
+ if (forceToHtml) return 'to-html';
19
+ return clipboardType === 'html' ? 'to-md' : 'to-html';
20
+ }
21
+
22
+ /**
23
+ * @param {object} input
24
+ * @param {string} input.content - clipboard content
25
+ * @param {'html'|'text'} input.type - what the pasteboard held
26
+ * @param {boolean} [input.forceToMd]
27
+ * @param {boolean} [input.forceToHtml]
28
+ * @param {boolean} [input.diagrams=true] - expand / strip Excalidraw reading copies
29
+ * @param {Function} [input.fetch] - injectable for tests; defaults to globalThis.fetch
30
+ * @param {{convert: Function, convertMdToHtml: Function}} input.converters
31
+ * @returns {Promise<{output: string, kind: 'md'|'html', notice: string}>}
32
+ * notice is one line when something needs attention, else ''
33
+ */
34
+ export async function convertClipboard(input) {
35
+ var direction = detectDirection(input.type, input.forceToMd, input.forceToHtml);
36
+ var diagrams = input.diagrams !== false;
37
+ var converters = input.converters;
38
+
39
+ if (direction === 'to-html') {
40
+ var md = input.content;
41
+ if (diagrams) {
42
+ // Lazy, so --no-diagrams never loads the diagrams code
43
+ var { stripDiagramBlocks } = await import('./diagrams/markdown.js');
44
+ md = stripDiagramBlocks(md);
45
+ }
46
+ return { output: converters.convertMdToHtml(md), kind: 'html', notice: '' };
47
+ }
48
+
49
+ var markdown = converters.convert(input.content);
50
+ if (!diagrams) return { output: markdown, kind: 'md', notice: '' };
51
+
52
+ var [{ expandDiagramLinks }, { loadScene }, { retryingFetch, RETRY_POLICY }] = await Promise.all([
53
+ import('./diagrams/markdown.js'), import('./diagrams/share-link.js'), import('./diagrams/retry.js'),
54
+ ]);
55
+ var fetchImpl = retryingFetch(input.fetch || globalThis.fetch);
56
+ var report = {};
57
+ var output = await expandDiagramLinks(markdown, {
58
+ loadScene: function (url) { return loadScene(url, { fetch: fetchImpl }); },
59
+ parallel: true,
60
+ deadlineMs: RETRY_POLICY.deadlineMs,
61
+ onFailure: 'omit',
62
+ report: report,
63
+ });
64
+ return { output: output, kind: 'md', notice: skippedNotice(report) };
65
+ }
66
+
67
+ export function skippedNotice(report) {
68
+ if (!report.failed) return '';
69
+ var reasons = [...new Set(report.skipped.map(function (s) { return s.reason; }))];
70
+ return 'c2m: ' + report.failed + ' of ' + report.total + ' diagrams skipped (' + reasons.join(', ') + ')';
71
+ }
@@ -16,15 +16,24 @@
16
16
  // returns the original.
17
17
 
18
18
  import { parseShareLink, SHARE_LINK_PATTERN } from './share-link.js';
19
- import { sceneToMermaid, headerLine } from './scene-to-mermaid.js';
19
+ import { sceneToMermaid, headerLine, GENERATED_MARKER } from './scene-to-mermaid.js';
20
+ import { failureReason } from './retry.js';
20
21
 
21
22
  const BLOCK_PATTERN = /(?:\r?\n){0,2}[ \t]*<!-- excalidraw-mermaid:begin ([A-Za-z0-9_-]+) -->[\s\S]*?<!-- excalidraw-mermaid:end \1 -->/g;
23
+ // A generated block whose begin/end comments were lost (rich text drops HTML
24
+ // comments): a fence whose first line is our header. Any other fence is the
25
+ // author's and is never touched.
26
+ const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
27
+ const HEADER_FENCE_PATTERN = new RegExp(
28
+ '(?:\\r?\\n){0,2}^[ \\t]*(`{3,}|~{3,})[ \\t]*(?:mermaid)?[ \\t]*\\r?\\n' +
29
+ '[ \\t]*%% generated from [^\\n;]*; ' + escapeRe(GENERATED_MARKER) + '[^\\n]*\\n' +
30
+ '(?:[^\\n]*\\n)*?[ \\t]*\\1[ \\t]*\\r?$', 'gm');
22
31
  const LIST_MARKER = /^(\s*)([-*+]|\d+[.)])(\s+)/;
23
32
  const FENCE = /^\s*(```|~~~)/;
24
33
  const HEADING = /^\s{0,3}#{1,6}\s/;
25
34
 
26
35
  export function stripDiagramBlocks(markdown) {
27
- return String(markdown).replace(BLOCK_PATTERN, '');
36
+ return String(markdown).replace(BLOCK_PATTERN, '').replace(HEADER_FENCE_PATTERN, '');
28
37
  }
29
38
 
30
39
  // Line table with offsets so insertion works on the original string untouched.
@@ -114,39 +123,93 @@ export function findDiagramLinks(markdown) {
114
123
  }
115
124
 
116
125
  // Replaces any existing generated blocks, then inserts fresh ones. One failing
117
- // link produces a visible error block and never stops the others.
126
+ // link never stops the others.
127
+ //
128
+ // options.loadScene url -> scene (required)
129
+ // options.parallel download every link at once instead of one by one
130
+ // options.deadlineMs give up on links not loaded by then (reason "timeout")
131
+ // options.onFailure "block" (default): a visible "could not load" block;
132
+ // "omit": leave the link as written with no block
133
+ // options.report filled with { total, done, failed, outline, skipped },
134
+ // skipped = [{ url, reason }] for every failed link
118
135
  export async function expandDiagramLinks(markdown, options) {
119
- var loadScene = options && options.loadScene;
136
+ options = options || {};
137
+ var loadScene = options.loadScene;
120
138
  if (typeof loadScene !== 'function') throw new Error('expandDiagramLinks needs options.loadScene');
121
- var onProgress = (options && options.onProgress) || function () {};
139
+ var onProgress = options.onProgress || function () {};
140
+ var omitFailures = options.onFailure === 'omit';
122
141
  var text = stripDiagramBlocks(markdown);
123
142
  var links = findDiagramLinks(text);
124
143
  var stats = { total: links.length, done: 0, failed: 0, outline: 0 };
144
+ var skipped = [];
125
145
  onProgress(Object.assign({}, stats));
126
146
 
127
- var blocks = [];
128
- for (var i = 0; i < links.length; i++) {
129
- var item = links[i];
130
- var mermaid;
131
- try {
132
- var scene = await loadScene(item.link.url);
133
- var result = sceneToMermaid(scene, { sourceUrl: item.link.url });
134
- if (result.kind === 'outline') stats.outline++;
135
- mermaid = result.text;
136
- } catch (err) {
147
+ function render(item, scene) {
148
+ var result = sceneToMermaid(scene, { sourceUrl: item.link.url });
149
+ if (result.kind === 'outline') stats.outline++;
150
+ return result.text;
151
+ }
152
+ function settle(item, outcome) {
153
+ var mermaid = null;
154
+ if (outcome.ok) {
155
+ try {
156
+ mermaid = render(item, outcome.scene);
157
+ } catch (err) {
158
+ outcome = { ok: false, err: err };
159
+ }
160
+ }
161
+ if (!outcome.ok) {
162
+ var err = outcome.err;
137
163
  stats.failed++;
138
- mermaid = headerLine(item.link.url) + '\n%% could not load: ' + String(err && err.message ? err.message : err).replace(/\s+/g, ' ');
164
+ skipped.push({ url: item.link.url, reason: failureReason(err) });
165
+ if (!omitFailures) {
166
+ mermaid = headerLine(item.link.url) + '\n%% could not load: ' + String(err && err.message ? err.message : err).replace(/\s+/g, ' ');
167
+ }
139
168
  }
140
- blocks.push({ offset: item.offset, text: '\n\n' + renderBlock(item.link.id, mermaid, item.indent) });
141
169
  stats.done++;
142
170
  onProgress(Object.assign({}, stats));
171
+ return mermaid;
143
172
  }
173
+ function load(item) {
174
+ return Promise.resolve()
175
+ .then(function () { return loadScene(item.link.url); })
176
+ .then(function (scene) { return { ok: true, scene: scene }; }, function (err) { return { ok: false, err: err }; });
177
+ }
178
+
179
+ var timer;
180
+ var deadline = options.deadlineMs == null ? null : new Promise(function (resolve) {
181
+ timer = setTimeout(function () {
182
+ var err = new Error('no answer within ' + options.deadlineMs + 'ms');
183
+ err.reason = 'timeout';
184
+ resolve({ ok: false, err: err });
185
+ }, options.deadlineMs);
186
+ });
187
+ var race = function (p) { return deadline ? Promise.race([p, deadline]) : p; };
188
+
189
+ var mermaids = [];
190
+ var run = function (item, idx) {
191
+ return race(load(item)).then(function (outcome) { mermaids[idx] = settle(item, outcome); });
192
+ };
193
+ try {
194
+ if (options.parallel) {
195
+ await Promise.all(links.map(run));
196
+ } else {
197
+ for (var i = 0; i < links.length; i++) await run(links[i], i);
198
+ }
199
+ } finally {
200
+ clearTimeout(timer);
201
+ }
202
+
203
+ var blocks = [];
204
+ links.forEach(function (item, idx) {
205
+ if (mermaids[idx] != null) blocks.push({ offset: item.offset, text: '\n\n' + renderBlock(item.link.id, mermaids[idx], item.indent) });
206
+ });
144
207
 
145
208
  // Insert back to front so earlier offsets stay valid; same-offset blocks keep order.
146
209
  var out = text;
147
210
  for (var k = blocks.length - 1; k >= 0; k--) {
148
211
  out = out.slice(0, blocks[k].offset) + blocks[k].text + out.slice(blocks[k].offset);
149
212
  }
150
- if (options && options.report) Object.assign(options.report, stats);
213
+ if (options.report) Object.assign(options.report, stats, { skipped: skipped });
151
214
  return out;
152
215
  }
@@ -0,0 +1,65 @@
1
+ // Retry policy for scene downloads, used by the CLI paths only. The page calls
2
+ // loadScene with a plain fetch and never loads this file.
3
+ //
4
+ // Each attempt gets its own timeout. A timeout, a network error, HTTP 429 and
5
+ // HTTP 5xx are retried; anything else (404, other 4xx) is handed back at once,
6
+ // and decrypt / decompress failures happen after fetch so are never retried.
7
+
8
+ export const RETRY_POLICY = { attemptTimeoutMs: 3000, delaysMs: [250, 750], deadlineMs: 6000 };
9
+
10
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
11
+
12
+ function failure(message, reason) {
13
+ var err = new Error(message);
14
+ err.reason = reason;
15
+ return err;
16
+ }
17
+
18
+ function attempt(fetchImpl, url, init, timeoutMs) {
19
+ var controller = typeof AbortController === 'function' ? new AbortController() : null;
20
+ var timer;
21
+ // Raced rather than trusted to the signal: a fetch that ignores abort must
22
+ // still lose to the clock.
23
+ var timeout = new Promise(function (_, reject) {
24
+ timer = setTimeout(function () {
25
+ if (controller) controller.abort();
26
+ reject(failure('timed out after ' + timeoutMs + 'ms', 'timeout'));
27
+ }, timeoutMs);
28
+ });
29
+ var request = Promise.resolve()
30
+ .then(function () { return fetchImpl(url, Object.assign({}, init, controller ? { signal: controller.signal } : {})); })
31
+ .catch(function (err) { throw err && err.reason ? err : failure('network error: ' + (err && err.message ? err.message : err), 'network'); });
32
+ return Promise.race([request, timeout]).finally(function () { clearTimeout(timer); });
33
+ }
34
+
35
+ const retryable = (status) => status === 429 || status >= 500;
36
+
37
+ // A fetch with the same signature that retries temporary failures.
38
+ export function retryingFetch(fetchImpl, policy) {
39
+ var p = Object.assign({}, RETRY_POLICY, policy);
40
+ return async function (url, init) {
41
+ var last;
42
+ for (var i = 0; i <= p.delaysMs.length; i++) {
43
+ if (i > 0) await wait(p.delaysMs[i - 1]);
44
+ try {
45
+ var res = await attempt(fetchImpl, url, init, p.attemptTimeoutMs);
46
+ if (!retryable(res.status)) return res;
47
+ last = res;
48
+ } catch (err) {
49
+ last = err;
50
+ }
51
+ }
52
+ if (last instanceof Error) throw last;
53
+ return last;
54
+ };
55
+ }
56
+
57
+ // Short reason for a failed download, for the one-line notice.
58
+ export function failureReason(err) {
59
+ if (err && err.reason) return err.reason;
60
+ var status = err && err.status;
61
+ if (status === 404) return '404';
62
+ if (status >= 500) return '5xx';
63
+ if (status) return String(status);
64
+ return 'error';
65
+ }
@@ -142,14 +142,18 @@ export async function loadScene(url, options) {
142
142
  if (!link) throw new Error('not an excalidraw.com share link');
143
143
  var res = await pickFetch(options)(link.backendBase + link.id, { method: 'GET' });
144
144
  if (!res.ok) {
145
- throw new Error(res.status === 404 ? 'scene not found (HTTP 404)' : 'HTTP ' + res.status);
145
+ var httpErr = new Error(res.status === 404 ? 'scene not found (HTTP 404)' : 'HTTP ' + res.status);
146
+ httpErr.status = res.status;
147
+ throw httpErr;
146
148
  }
147
149
  var bytes = new Uint8Array(await res.arrayBuffer());
148
150
  var data;
149
151
  try {
150
152
  data = await decodePayload(bytes, link.key);
151
153
  } catch (e) {
152
- throw new Error('could not decrypt scene (wrong key or corrupt payload)');
154
+ var decryptErr = new Error('could not decrypt scene (wrong key or corrupt payload)');
155
+ decryptErr.reason = 'decrypt';
156
+ throw decryptErr;
153
157
  }
154
158
  return normalizeScene(data);
155
159
  }