@libo11y/sourcemaps 0.4.0 → 0.6.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
@@ -9,15 +9,9 @@ fingerprint. It runs on Bun 1.3.13 and Node 24.
9
9
  npm install --save-dev @libo11y/sourcemaps
10
10
  ```
11
11
 
12
- The package first ships in `ts/v0.3.0`. It is not on npm yet. Until its
13
- first npm publish, install it from the tarball attached to the
14
- [`ts/v0.4.0` GitHub Release](https://github.com/jamesainslie/libo11y/releases/tag/ts/v0.4.0),
15
- whose notes list the exact URL:
16
-
17
- ```sh
18
- npm install --save-dev \
19
- https://github.com/jamesainslie/libo11y/releases/download/ts/v0.4.0/libo11y-sourcemaps-0.4.0.tgz
20
- ```
12
+ The package first ships in `ts/v0.3.0` and is published on npm at `0.6.0`
13
+ (also attached as a tarball to the
14
+ [`ts/v0.6.0` GitHub Release](https://github.com/libo11y/libo11y/releases/tag/ts/v0.6.0)).
21
15
 
22
16
  The gateway stores each map under the app's `service.name`, the release's
23
17
  `service.version`, the over-the-air update ID when there is one, and the
package/dist/upload.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readFile, rm, writeFile } from "node:fs/promises";
1
+ import { readFile, realpath, rm, writeFile } from "node:fs/promises";
2
2
  import { gzipSync } from "node:zlib";
3
3
  import { findSourceMaps, mapComment } from "./find.js";
4
4
  /** An upload the gateway refused, or that could not reach it. */
@@ -65,7 +65,7 @@ export async function uploadSourceMapsWith(options, deps) {
65
65
  failure ??=
66
66
  err instanceof UploadError
67
67
  ? err
68
- : new UploadError(`reading ${file.map} failed: ${redact(describe(err), options.token)}`, 0, file.script, [], { cause: err });
68
+ : new UploadError(`reading ${file.map} failed: ${redact(describe(err), options.token)}`, 0, file.script, [], { cause: sanitizeCause(err, options.token) });
69
69
  }
70
70
  }
71
71
  };
@@ -114,6 +114,21 @@ function describe(err) {
114
114
  function redact(message, token) {
115
115
  return token === "" ? message : message.split(token).join("[token]");
116
116
  }
117
+ // sanitizeCause clones an error chain with the token redacted so UploadError.cause never leaks it.
118
+ function sanitizeCause(err, token, seen = new Set()) {
119
+ if (err === undefined || err === null)
120
+ return undefined;
121
+ if (err instanceof Error) {
122
+ if (seen.has(err))
123
+ return undefined;
124
+ seen.add(err);
125
+ const cause = sanitizeCause(err.cause, token, seen);
126
+ const clean = new Error(redact(err.message, token), cause === undefined ? undefined : { cause });
127
+ clean.name = redact(err.name, token);
128
+ return clean;
129
+ }
130
+ return new Error(redact(String(err), token));
131
+ }
117
132
  // retryDelay is the wait before attempt+1: the response's Retry-After in seconds, capped, or the
118
133
  // backoff for the attempt.
119
134
  function retryDelay(attempt, retryAfter) {
@@ -134,6 +149,7 @@ async function uploadOne(base, options, file, doFetch, deps) {
134
149
  const timeoutMillis = deps.timeoutMillis ?? DEFAULT_TIMEOUT_MILLIS;
135
150
  for (let attempt = 1;; attempt++) {
136
151
  let res;
152
+ let body201;
137
153
  try {
138
154
  res = await doFetch(url, {
139
155
  method: "POST",
@@ -145,6 +161,8 @@ async function uploadOne(base, options, file, doFetch, deps) {
145
161
  body,
146
162
  signal: AbortSignal.timeout(timeoutMillis),
147
163
  });
164
+ if (res.status === 201)
165
+ body201 = await res.text();
148
166
  }
149
167
  catch (err) {
150
168
  if (attempt < MAX_ATTEMPTS) {
@@ -155,47 +173,65 @@ async function uploadOne(base, options, file, doFetch, deps) {
155
173
  ? `timed out after ${timeoutMillis} ms`
156
174
  : redact(describe(err), options.token);
157
175
  throw new UploadError(`upload of ${file.url} could not reach the gateway: ${reason}`, 0, file.script, [], {
158
- cause: err,
176
+ cause: sanitizeCause(err, options.token),
159
177
  });
160
178
  }
161
- if (res.ok) {
162
- const key = await keyOf(res);
163
- return { script: file.script, url: file.url, key };
179
+ if (res.status === 201) {
180
+ const text = body201 ?? "";
181
+ const key = keyOf(text);
182
+ if (key !== "")
183
+ return { script: file.script, url: file.url, key };
184
+ const detail = redact(text, options.token).trim().slice(0, 200);
185
+ throw new UploadError(`upload of ${file.url} returned 201 without an object key${detail === "" ? "" : `: ${detail}`}`, res.status, file.script);
164
186
  }
165
- const detail = (await res.text().catch(() => "")).trim().slice(0, 200);
166
187
  if ((res.status === 429 || res.status >= 500) && attempt < MAX_ATTEMPTS) {
188
+ await res.body?.cancel().catch(() => { });
167
189
  await deps.sleep(retryDelay(attempt, res.headers.get("retry-after")));
168
190
  continue;
169
191
  }
170
- throw new UploadError(`upload of ${file.url} failed with ${res.status}${detail === "" ? "" : `: ${redact(detail, options.token)}`}`, res.status, file.script);
192
+ const text = await res.text().catch(() => "");
193
+ const detail = redact(text, options.token).trim().slice(0, 200);
194
+ throw new UploadError(`upload of ${file.url} failed with ${res.status}${detail === "" ? "" : `: ${detail}`}`, res.status, file.script);
171
195
  }
172
196
  }
173
- async function keyOf(res) {
197
+ function keyOf(text) {
174
198
  try {
175
- const parsed = await res.json();
176
- if (typeof parsed === "object" && parsed !== null && "key" in parsed && typeof parsed.key === "string") {
199
+ const parsed = JSON.parse(text);
200
+ if (typeof parsed === "object" &&
201
+ parsed !== null &&
202
+ "key" in parsed &&
203
+ typeof parsed.key === "string" &&
204
+ parsed.key.trim() !== "") {
177
205
  return parsed.key;
178
206
  }
179
207
  }
180
208
  catch {
181
- // A success without a JSON body still stored the map.
209
+ // Non-JSON responses cannot confirm the gateway stored the map.
182
210
  }
183
211
  return "";
184
212
  }
185
213
  // removeUploaded strips the sourceMappingURL comment of every uploaded script, and deletes each map
186
214
  // once every script it describes has uploaded.
187
215
  async function removeUploaded(files, uploaded) {
188
- const pending = new Map();
216
+ // Resolve aliases before deleting anything, since removing the target can break a symlink.
217
+ const canonical = new Map();
218
+ for (const { map } of files) {
219
+ if (!canonical.has(map)) {
220
+ // A path that has already disappeared still groups with entries using the same path.
221
+ canonical.set(map, await realpath(map).catch(() => map));
222
+ }
223
+ }
224
+ const pending = new Set();
189
225
  for (const [i, f] of files.entries()) {
190
226
  if (uploaded[i] === undefined)
191
- pending.set(f.map, (pending.get(f.map) ?? 0) + 1);
227
+ pending.add(canonical.get(f.map) ?? f.map);
192
228
  }
193
229
  const removed = new Set();
194
230
  for (const [i, f] of files.entries()) {
195
231
  if (uploaded[i] === undefined)
196
232
  continue;
197
233
  await stripComment(f.script);
198
- if (!pending.has(f.map) && !removed.has(f.map)) {
234
+ if (!pending.has(canonical.get(f.map) ?? f.map) && !removed.has(f.map)) {
199
235
  removed.add(f.map);
200
236
  await rm(f.map, { force: true });
201
237
  }
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** The `@libo11y/sourcemaps` package version. scripts/release.ts keeps it equal to package.json. */
2
- export declare const VERSION = "0.4.0";
2
+ export declare const VERSION = "0.6.0";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /** The `@libo11y/sourcemaps` package version. scripts/release.ts keeps it equal to package.json. */
2
- export const VERSION = "0.4.0";
2
+ export const VERSION = "0.6.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libo11y/sourcemaps",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Uploads JavaScript source maps to the libo11y RUM gateway so it can symbolicate browser and React Native stack traces. A library and the libo11y-sourcemaps command for Bun and Node.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -16,11 +16,11 @@
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "git+https://github.com/jamesainslie/libo11y.git",
19
+ "url": "git+https://github.com/libo11y/libo11y.git",
20
20
  "directory": "ts/packages/sourcemaps"
21
21
  },
22
22
  "devDependencies": {
23
- "@libo11y/test": "0.4.0"
23
+ "@libo11y/test": "0.6.0"
24
24
  },
25
25
  "files": [
26
26
  "dist",