@libo11y/sourcemaps 0.6.0 → 0.8.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 +75 -6
- package/dist/cli.d.ts +2 -1
- package/dist/cli.js +64 -14
- package/dist/dsyms.d.ts +64 -0
- package/dist/dsyms.js +185 -0
- package/dist/index.d.ts +21 -3
- package/dist/index.js +21 -3
- package/dist/macho.d.ts +17 -0
- package/dist/macho.js +104 -0
- package/dist/upload.d.ts +24 -0
- package/dist/upload.js +32 -23
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/zip.d.ts +11 -0
- package/dist/zip.js +84 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
# @libo11y/sourcemaps
|
|
2
2
|
|
|
3
|
-
Uploads JavaScript source maps
|
|
4
|
-
gateway uses them to symbolicate the
|
|
5
|
-
|
|
3
|
+
Uploads JavaScript source maps and iOS debug symbols (dSYMs) to the
|
|
4
|
+
libo11y RUM gateway (`o11yrum`). The gateway uses them to symbolicate the
|
|
5
|
+
stack traces of browser and React Native errors and of native iOS crashes
|
|
6
|
+
and hangs before it exports them, and to give each error a stable
|
|
6
7
|
fingerprint. It runs on Bun 1.3.13 and Node 24.
|
|
7
8
|
|
|
8
9
|
```sh
|
|
9
10
|
npm install --save-dev @libo11y/sourcemaps
|
|
10
11
|
```
|
|
11
12
|
|
|
12
|
-
The package first ships in `ts/v0.3.0` and is published on npm at `0.
|
|
13
|
+
The package first ships in `ts/v0.3.0` and is published on npm at `0.8.0`
|
|
13
14
|
(also attached as a tarball to the
|
|
14
|
-
[`ts/v0.
|
|
15
|
+
[`ts/v0.8.0` GitHub Release](https://github.com/libo11y/libo11y/releases/tag/ts/v0.8.0)).
|
|
15
16
|
|
|
16
17
|
The gateway stores each map under the app's `service.name`, the release's
|
|
17
18
|
`service.version`, the over-the-air update ID when there is one, and the
|
|
@@ -87,6 +88,72 @@ refused before anything is sent, and no message ever includes the token.
|
|
|
87
88
|
With `deleteAfterUpload`, a map is removed only once every script it
|
|
88
89
|
describes has uploaded.
|
|
89
90
|
|
|
91
|
+
## dSYMs
|
|
92
|
+
|
|
93
|
+
A native iOS crash or hang that `@libo11y/rum-react-native` records
|
|
94
|
+
carries each frame as a binary UUID and an offset. The gateway resolves
|
|
95
|
+
those frames to function, file and line through the binary's dSYM, so
|
|
96
|
+
upload the dSYMs of every build you release:
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
export O11Y_SOURCEMAPS_TOKEN=... # the app's upload token
|
|
100
|
+
libo11y-sourcemaps dsyms --endpoint https://app.example.com --service my-app ios/build
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Each `PATH` may be a `.dSYM` bundle, a directory that holds bundles at any
|
|
104
|
+
depth (such as an Xcode archive's `dSYMs` folder), a zip archive of
|
|
105
|
+
bundles, or one DWARF file. The command uploads every file under a
|
|
106
|
+
bundle's `Contents/Resources/DWARF`, gzipped, to `POST /_o11y/v1/dsyms`,
|
|
107
|
+
and prints `uploaded <file> -> <UUID> <arch>` for each binary the gateway
|
|
108
|
+
stored. A file that is not a Mach-O with DWARF and a UUID is skipped with
|
|
109
|
+
a note. `--endpoint`, `--service`, `--token-env` and `--concurrency`
|
|
110
|
+
(default 2) work as for `upload`. It exits 0 when every file uploaded, 1
|
|
111
|
+
when an upload failed or nothing was found, and 2 on a usage error.
|
|
112
|
+
|
|
113
|
+
The gateway keys each binary by its UUID, so one upload serves every
|
|
114
|
+
release of that build, over-the-air updates included. A binary and its
|
|
115
|
+
dSYM share the UUID, and every build makes new ones. Upload before the
|
|
116
|
+
build reaches people: the gateway resolves a stack when it receives it.
|
|
117
|
+
|
|
118
|
+
**EAS Build.** fastlane writes every dSYM of the archive to
|
|
119
|
+
`ios/build/<app name>.app.dSYM.zip`. Upload it from the build's
|
|
120
|
+
`eas-build-on-success` hook, with the token in an EAS secret environment
|
|
121
|
+
variable named `O11Y_SOURCEMAPS_TOKEN`:
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"scripts": {
|
|
126
|
+
"eas-build-on-success": "[ \"$EAS_BUILD_PLATFORM\" != ios ] || libo11y-sourcemaps dsyms --endpoint https://app.example.com --service my-app ios/build"
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
A local `eas build --local` runs the same hook. For an archive made in
|
|
132
|
+
Xcode, pass the archive's `dSYMs` folder.
|
|
133
|
+
|
|
134
|
+
**Hermes** is a prebuilt framework, so the archive holds no dSYM for it.
|
|
135
|
+
Download the Hermes dSYM for your React Native version from the React
|
|
136
|
+
Native release artifacts and upload it too, or Hermes frames stay as
|
|
137
|
+
offsets.
|
|
138
|
+
|
|
139
|
+
The library has the same function:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { uploadDsyms } from "@libo11y/sourcemaps";
|
|
143
|
+
|
|
144
|
+
const uploaded = await uploadDsyms({
|
|
145
|
+
endpoint: "https://app.example.com",
|
|
146
|
+
token: process.env.O11Y_SOURCEMAPS_TOKEN ?? "",
|
|
147
|
+
service: "my-app",
|
|
148
|
+
paths: ["ios/build"],
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`findDsyms(paths)` lists the DWARF files without uploading anything, and
|
|
153
|
+
`machOImages(bytes)` reads the architecture, UUID and DWARF presence of
|
|
154
|
+
each slice of a Mach-O file. A failed upload throws a `DsymUploadError`
|
|
155
|
+
with the `status`, the `file` and the files that did upload.
|
|
156
|
+
|
|
90
157
|
## The gateway side
|
|
91
158
|
|
|
92
159
|
An app accepts uploads when the gateway configuration names its token
|
|
@@ -101,4 +168,6 @@ apps:
|
|
|
101
168
|
sourcemaps: { upload_token_file: /run/secrets/shop-web-sourcemaps }
|
|
102
169
|
```
|
|
103
170
|
|
|
104
|
-
|
|
171
|
+
The same token authorises dSYM uploads to `POST /_o11y/v1/dsyms` for an
|
|
172
|
+
app with `dsyms: { enabled: true }`. Without it the gateway answers 403. See the
|
|
173
|
+
[o11yrum README](../../../go/o11yrum/README.md) for the gateway.
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { type DsymUploadOptions, type UploadedDsym } from "./dsyms.ts";
|
|
2
3
|
import { type UploadOptions } from "./upload.ts";
|
|
3
4
|
/** Runs the command and returns its exit status. */
|
|
4
5
|
export declare function run(args: readonly string[], env: Readonly<Record<string, string | undefined>>, out: (line: string) => void, err: (line: string) => void, upload?: (options: UploadOptions) => Promise<readonly {
|
|
5
6
|
url: string;
|
|
6
7
|
key: string;
|
|
7
|
-
}[]>): Promise<number>;
|
|
8
|
+
}[]>, dsyms?: (options: DsymUploadOptions) => Promise<readonly UploadedDsym[]>): Promise<number>;
|
package/dist/cli.js
CHANGED
|
@@ -1,33 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/**
|
|
4
|
-
* The libo11y-sourcemaps command. `libo11y-sourcemaps upload ... DIR`
|
|
5
|
-
* build to the libo11y RUM gateway
|
|
4
|
+
* The libo11y-sourcemaps command, for a build or deploy job. `libo11y-sourcemaps upload ... DIR`
|
|
5
|
+
* uploads the source maps of a build to the libo11y RUM gateway. `libo11y-sourcemaps dsyms ...
|
|
6
|
+
* PATH...` uploads the iOS debug symbols (dSYMs) of a native build: .dSYM bundles, directories of
|
|
7
|
+
* them, zip archives of them, or DWARF files.
|
|
6
8
|
*
|
|
7
9
|
* The upload token is read only from an environment variable, never from a flag, so it never
|
|
8
10
|
* appears in a process list.
|
|
9
11
|
*
|
|
10
|
-
* Exit status: 0 when every
|
|
12
|
+
* Exit status: 0 when every file uploaded, 1 when an upload failed or nothing was found to upload,
|
|
11
13
|
* 2 on a usage error.
|
|
12
14
|
*/
|
|
13
15
|
import { realpathSync } from "node:fs";
|
|
14
16
|
import { fileURLToPath } from "node:url";
|
|
15
17
|
import { parseArgs } from "node:util";
|
|
18
|
+
import { DsymUploadError, uploadDsyms } from "./dsyms.js";
|
|
16
19
|
import { checkToken, UploadError, uploadSourceMaps } from "./upload.js";
|
|
17
20
|
import { VERSION } from "./version.js";
|
|
18
21
|
const USAGE = `usage: libo11y-sourcemaps upload --endpoint URL --service NAME --release VERSION --url-prefix PREFIX
|
|
19
22
|
[--update-id ID] [--token-env VAR] [--concurrency N] [--delete] DIR
|
|
23
|
+
libo11y-sourcemaps dsyms --endpoint URL --service NAME [--token-env VAR] [--concurrency N] PATH...
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
upload: uploads every source map under DIR to the RUM gateway at URL, for the app NAME at release
|
|
26
|
+
VERSION.
|
|
27
|
+
dsyms: uploads the iOS debug symbols under each PATH (a .dSYM bundle, a directory holding bundles,
|
|
28
|
+
a zip archive of bundles such as EAS Build's ios/build/<app>.app.dSYM.zip, or a DWARF file) for the
|
|
29
|
+
app NAME. The gateway keys them by binary UUID, so they serve every release of that build.
|
|
22
30
|
|
|
23
31
|
--endpoint URL the gateway's base URL, under which it serves /_o11y/v1/
|
|
24
32
|
--service NAME the app's service.name
|
|
25
|
-
--release VERSION the release's service.version
|
|
26
|
-
--url-prefix PREFIX the URL path DIR is served at, such as /assets/, or ~/ for the site root
|
|
27
|
-
--update-id ID the over-the-air update the maps belong to (o11y.rum.app.update_id)
|
|
33
|
+
--release VERSION the release's service.version (upload)
|
|
34
|
+
--url-prefix PREFIX the URL path DIR is served at, such as /assets/, or ~/ for the site root (upload)
|
|
35
|
+
--update-id ID the over-the-air update the maps belong to (o11y.rum.app.update_id) (upload)
|
|
28
36
|
--token-env VAR the environment variable holding the upload token (default O11Y_SOURCEMAPS_TOKEN)
|
|
29
|
-
--concurrency N uploads in flight at once (default 4)
|
|
30
|
-
--delete remove each uploaded map and its sourceMappingURL comment from DIR
|
|
37
|
+
--concurrency N uploads in flight at once (default 4 for upload, 2 for dsyms)
|
|
38
|
+
--delete remove each uploaded map and its sourceMappingURL comment from DIR (upload)
|
|
31
39
|
--help print this help
|
|
32
40
|
--version print the version`;
|
|
33
41
|
const DEFAULT_TOKEN_ENV = "O11Y_SOURCEMAPS_TOKEN";
|
|
@@ -35,7 +43,7 @@ function message(err) {
|
|
|
35
43
|
return err instanceof Error ? err.message : String(err);
|
|
36
44
|
}
|
|
37
45
|
/** Runs the command and returns its exit status. */
|
|
38
|
-
export async function run(args, env, out, err, upload = uploadSourceMaps) {
|
|
46
|
+
export async function run(args, env, out, err, upload = uploadSourceMaps, dsyms = uploadDsyms) {
|
|
39
47
|
let parsed;
|
|
40
48
|
try {
|
|
41
49
|
parsed = parse(args);
|
|
@@ -54,19 +62,24 @@ export async function run(args, env, out, err, upload = uploadSourceMaps) {
|
|
|
54
62
|
out(VERSION);
|
|
55
63
|
return 0;
|
|
56
64
|
}
|
|
57
|
-
|
|
65
|
+
const command = positionals[0];
|
|
66
|
+
const isUpload = command === "upload" && positionals.length === 2;
|
|
67
|
+
const isDsyms = command === "dsyms" && positionals.length >= 2;
|
|
68
|
+
if (!isUpload && !isDsyms) {
|
|
58
69
|
err(USAGE);
|
|
59
70
|
return 2;
|
|
60
71
|
}
|
|
61
|
-
const
|
|
62
|
-
|
|
72
|
+
const required = isUpload
|
|
73
|
+
? ["endpoint", "service", "release", "url-prefix"]
|
|
74
|
+
: ["endpoint", "service"];
|
|
75
|
+
for (const flag of required) {
|
|
63
76
|
if (values[flag] === undefined || values[flag] === "") {
|
|
64
77
|
err(`libo11y-sourcemaps: --${flag} is required`);
|
|
65
78
|
err(USAGE);
|
|
66
79
|
return 2;
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
|
-
const concurrency = values.concurrency === undefined ? 4 : Number(values.concurrency);
|
|
82
|
+
const concurrency = values.concurrency === undefined ? (isUpload ? 4 : 2) : Number(values.concurrency);
|
|
70
83
|
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
71
84
|
err(`libo11y-sourcemaps: --concurrency must be a positive integer, got ${values.concurrency}`);
|
|
72
85
|
return 2;
|
|
@@ -91,6 +104,16 @@ export async function run(args, env, out, err, upload = uploadSourceMaps) {
|
|
|
91
104
|
err(`libo11y-sourcemaps: ${tokenEnv}: ${message(e)}`);
|
|
92
105
|
return 2;
|
|
93
106
|
}
|
|
107
|
+
if (isDsyms) {
|
|
108
|
+
return runDsyms({
|
|
109
|
+
endpoint: values.endpoint ?? "",
|
|
110
|
+
token,
|
|
111
|
+
service: values.service ?? "",
|
|
112
|
+
paths: positionals.slice(1),
|
|
113
|
+
concurrency,
|
|
114
|
+
}, out, err, dsyms);
|
|
115
|
+
}
|
|
116
|
+
const directory = positionals[1] ?? "";
|
|
94
117
|
const options = {
|
|
95
118
|
endpoint: values.endpoint ?? "",
|
|
96
119
|
token,
|
|
@@ -122,6 +145,33 @@ export async function run(args, env, out, err, upload = uploadSourceMaps) {
|
|
|
122
145
|
out(`uploaded ${u.url} -> ${u.key}`);
|
|
123
146
|
return 0;
|
|
124
147
|
}
|
|
148
|
+
async function runDsyms(options, out, err, dsyms) {
|
|
149
|
+
const print = (u) => {
|
|
150
|
+
for (const i of u.images)
|
|
151
|
+
out(`uploaded ${u.file} -> ${i.uuid} ${i.arch}`);
|
|
152
|
+
};
|
|
153
|
+
let uploaded;
|
|
154
|
+
try {
|
|
155
|
+
uploaded = await dsyms({
|
|
156
|
+
...options,
|
|
157
|
+
onSkip: (f) => err(`libo11y-sourcemaps: skipped ${f}: no Mach-O image with DWARF and a UUID`),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
if (e instanceof DsymUploadError)
|
|
162
|
+
for (const u of e.uploaded)
|
|
163
|
+
print(u);
|
|
164
|
+
err(`libo11y-sourcemaps: ${message(e)}`);
|
|
165
|
+
return 1;
|
|
166
|
+
}
|
|
167
|
+
if (uploaded.length === 0) {
|
|
168
|
+
err(`libo11y-sourcemaps: no dSYM DWARF files found under ${options.paths.join(", ")}`);
|
|
169
|
+
return 1;
|
|
170
|
+
}
|
|
171
|
+
for (const u of uploaded)
|
|
172
|
+
print(u);
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
125
175
|
function parse(args) {
|
|
126
176
|
return parseArgs({
|
|
127
177
|
args: [...args],
|
package/dist/dsyms.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type UploadDeps } from "./upload.ts";
|
|
2
|
+
/** A DWARF file found under the given paths. */
|
|
3
|
+
export interface DsymFile {
|
|
4
|
+
/** Its path, or `<archive>!<entry>` inside a zip archive. */
|
|
5
|
+
readonly source: string;
|
|
6
|
+
/** Reads the file's bytes. */
|
|
7
|
+
read(): Promise<Buffer>;
|
|
8
|
+
}
|
|
9
|
+
/** Options for uploadDsyms. */
|
|
10
|
+
export interface DsymUploadOptions {
|
|
11
|
+
/** The gateway's base URL, under which it serves /_o11y/v1/. */
|
|
12
|
+
readonly endpoint: string;
|
|
13
|
+
/** The app's upload token, the same one source map uploads use. */
|
|
14
|
+
readonly token: string;
|
|
15
|
+
/** The app's service.name. */
|
|
16
|
+
readonly service: string;
|
|
17
|
+
/** .dSYM bundles, directories, zip archives or DWARF files. */
|
|
18
|
+
readonly paths: readonly string[];
|
|
19
|
+
/** The uploads in flight at once. Default 2: DWARF files are large. */
|
|
20
|
+
readonly concurrency?: number;
|
|
21
|
+
/** Called with each found file that holds no Mach-O image with DWARF and a UUID. */
|
|
22
|
+
readonly onSkip?: (source: string) => void;
|
|
23
|
+
/** The fetch to use. Default globalThis.fetch. */
|
|
24
|
+
readonly fetch?: typeof fetch;
|
|
25
|
+
}
|
|
26
|
+
/** One binary the gateway stored the debug symbols of. */
|
|
27
|
+
export interface StoredImage {
|
|
28
|
+
/** The binary's UUID, as crash reports print it. */
|
|
29
|
+
readonly uuid: string;
|
|
30
|
+
readonly arch: string;
|
|
31
|
+
/** The object key of its symbol index. */
|
|
32
|
+
readonly key: string;
|
|
33
|
+
}
|
|
34
|
+
/** A DWARF file the gateway stored. */
|
|
35
|
+
export interface UploadedDsym {
|
|
36
|
+
readonly file: string;
|
|
37
|
+
readonly images: readonly StoredImage[];
|
|
38
|
+
}
|
|
39
|
+
/** A dSYM upload the gateway refused, or that could not reach it. */
|
|
40
|
+
export declare class DsymUploadError extends Error {
|
|
41
|
+
/** The HTTP status, or 0 when the gateway could not be reached or the file could not be read. */
|
|
42
|
+
readonly status: number;
|
|
43
|
+
/** The file that failed to upload. */
|
|
44
|
+
readonly file: string;
|
|
45
|
+
/** The files that did upload before the failure stopped the run. */
|
|
46
|
+
readonly uploaded: readonly UploadedDsym[];
|
|
47
|
+
constructor(message: string, status: number, file: string, uploaded: readonly UploadedDsym[], options?: ErrorOptions);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The DWARF files under paths, in path order and then by name: every file in the
|
|
51
|
+
* Contents/Resources/DWARF directory of a .dSYM bundle, in directories (symbolic links and
|
|
52
|
+
* node_modules are not followed) and zip archives; a path that is a plain file is taken as one DWARF
|
|
53
|
+
* file. A missing path is an error.
|
|
54
|
+
*/
|
|
55
|
+
export declare function findDsyms(paths: readonly string[]): Promise<DsymFile[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Finds the DWARF files under options.paths and uploads each one that holds a Mach-O image with DWARF
|
|
58
|
+
* and a UUID, gzipped, to the gateway's POST /_o11y/v1/dsyms, retrying as uploadSourceMaps does. Once
|
|
59
|
+
* an upload fails no further upload starts, and the first failure is thrown as a DsymUploadError,
|
|
60
|
+
* listing the files that did upload, after the uploads in flight settle.
|
|
61
|
+
*/
|
|
62
|
+
export declare function uploadDsyms(options: DsymUploadOptions): Promise<UploadedDsym[]>;
|
|
63
|
+
/** uploadDsyms with its seams, for tests. */
|
|
64
|
+
export declare function uploadDsymsWith(options: DsymUploadOptions, deps: UploadDeps): Promise<UploadedDsym[]>;
|
package/dist/dsyms.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds iOS debug symbols (dSYMs) and uploads them to the libo11y RUM gateway, which resolves the
|
|
3
|
+
* frames of native crash and hang stacks through them by binary UUID.
|
|
4
|
+
*
|
|
5
|
+
* A path may be a .dSYM bundle, a directory holding bundles at any depth (such as an Xcode archive's
|
|
6
|
+
* dSYMs folder or ios/build), a zip archive of bundles (such as the <app>.app.dSYM.zip fastlane writes
|
|
7
|
+
* on EAS Build), or one DWARF file. Each file under a bundle's Contents/Resources/DWARF is uploaded
|
|
8
|
+
* once, gzipped, to POST /_o11y/v1/dsyms. The gateway splits a universal file into its slices and
|
|
9
|
+
* keys each by its UUID.
|
|
10
|
+
*/
|
|
11
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
12
|
+
import { join, resolve } from "node:path";
|
|
13
|
+
import { gzipSync } from "node:zlib";
|
|
14
|
+
import { machOImages } from "./macho.js";
|
|
15
|
+
import { checkToken, defaultDeps, describe, endpointURL, postUpload, redact, sanitizeCause, UploadError, } from "./upload.js";
|
|
16
|
+
import { zipEntries } from "./zip.js";
|
|
17
|
+
/** A dSYM upload the gateway refused, or that could not reach it. */
|
|
18
|
+
export class DsymUploadError extends Error {
|
|
19
|
+
/** The HTTP status, or 0 when the gateway could not be reached or the file could not be read. */
|
|
20
|
+
status;
|
|
21
|
+
/** The file that failed to upload. */
|
|
22
|
+
file;
|
|
23
|
+
/** The files that did upload before the failure stopped the run. */
|
|
24
|
+
uploaded;
|
|
25
|
+
constructor(message, status, file, uploaded, options) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.name = "DsymUploadError";
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.file = file;
|
|
30
|
+
this.uploaded = uploaded;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const UPLOAD_PATH = "_o11y/v1/dsyms";
|
|
34
|
+
const DWARF_DIR = /(?:^|\/)[^/]+\.dSYM\/Contents\/Resources\/DWARF\/[^/]+$/;
|
|
35
|
+
async function walk(dir, out) {
|
|
36
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
37
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
38
|
+
for (const e of entries) {
|
|
39
|
+
const p = join(dir, e.name);
|
|
40
|
+
if (e.isSymbolicLink() || e.name === "node_modules")
|
|
41
|
+
continue;
|
|
42
|
+
if (e.isDirectory())
|
|
43
|
+
await walk(p, out);
|
|
44
|
+
else if (e.isFile()) {
|
|
45
|
+
if (e.name.endsWith(".zip"))
|
|
46
|
+
out.push(...(await fromZip(p)));
|
|
47
|
+
else if (DWARF_DIR.test(p.replaceAll("\\", "/")))
|
|
48
|
+
out.push(fromFile(p));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function fromFile(path) {
|
|
53
|
+
return { source: path, read: () => readFile(path) };
|
|
54
|
+
}
|
|
55
|
+
async function fromZip(path) {
|
|
56
|
+
const buf = await readFile(path);
|
|
57
|
+
let entries;
|
|
58
|
+
try {
|
|
59
|
+
entries = zipEntries(buf);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
throw new Error(`${path}: ${err instanceof Error ? err.message : String(err)}`);
|
|
63
|
+
}
|
|
64
|
+
return entries
|
|
65
|
+
.filter((e) => DWARF_DIR.test(e.name))
|
|
66
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
|
|
67
|
+
.map((e) => ({ source: `${path}!${e.name}`, read: async () => e.read() }));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The DWARF files under paths, in path order and then by name: every file in the
|
|
71
|
+
* Contents/Resources/DWARF directory of a .dSYM bundle, in directories (symbolic links and
|
|
72
|
+
* node_modules are not followed) and zip archives; a path that is a plain file is taken as one DWARF
|
|
73
|
+
* file. A missing path is an error.
|
|
74
|
+
*/
|
|
75
|
+
export async function findDsyms(paths) {
|
|
76
|
+
const out = [];
|
|
77
|
+
for (const raw of paths) {
|
|
78
|
+
const p = resolve(raw);
|
|
79
|
+
// A path named on the command line is followed when it is a symbolic link; links inside a
|
|
80
|
+
// directory are not.
|
|
81
|
+
const st = await stat(p).catch(() => undefined);
|
|
82
|
+
if (st === undefined)
|
|
83
|
+
throw new Error(`${raw} is missing`);
|
|
84
|
+
if (st.isDirectory())
|
|
85
|
+
await walk(p, out);
|
|
86
|
+
else if (p.endsWith(".zip"))
|
|
87
|
+
out.push(...(await fromZip(p)));
|
|
88
|
+
else
|
|
89
|
+
out.push(fromFile(p));
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
/** Each image the gateway reports, or undefined when the response is not that shape. */
|
|
94
|
+
function storedImages(text) {
|
|
95
|
+
try {
|
|
96
|
+
const parsed = JSON.parse(text);
|
|
97
|
+
if (typeof parsed !== "object" ||
|
|
98
|
+
parsed === null ||
|
|
99
|
+
!("images" in parsed) ||
|
|
100
|
+
!Array.isArray(parsed.images)) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const images = parsed.images.flatMap((i) => {
|
|
104
|
+
if (typeof i !== "object" || i === null)
|
|
105
|
+
return [];
|
|
106
|
+
const { uuid, arch, key } = i;
|
|
107
|
+
return typeof uuid === "string" && typeof key === "string" && key !== ""
|
|
108
|
+
? [{ uuid, arch: typeof arch === "string" ? arch : "", key }]
|
|
109
|
+
: [];
|
|
110
|
+
});
|
|
111
|
+
return images.length === 0 ? undefined : images;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Finds the DWARF files under options.paths and uploads each one that holds a Mach-O image with DWARF
|
|
119
|
+
* and a UUID, gzipped, to the gateway's POST /_o11y/v1/dsyms, retrying as uploadSourceMaps does. Once
|
|
120
|
+
* an upload fails no further upload starts, and the first failure is thrown as a DsymUploadError,
|
|
121
|
+
* listing the files that did upload, after the uploads in flight settle.
|
|
122
|
+
*/
|
|
123
|
+
export function uploadDsyms(options) {
|
|
124
|
+
return uploadDsymsWith(options, defaultDeps);
|
|
125
|
+
}
|
|
126
|
+
/** uploadDsyms with its seams, for tests. */
|
|
127
|
+
export async function uploadDsymsWith(options, deps) {
|
|
128
|
+
const concurrency = options.concurrency ?? 2;
|
|
129
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
130
|
+
throw new RangeError(`@libo11y/sourcemaps: concurrency must be a positive integer, got ${concurrency}`);
|
|
131
|
+
}
|
|
132
|
+
checkToken(options.token);
|
|
133
|
+
const base = endpointURL(options.endpoint, UPLOAD_PATH);
|
|
134
|
+
base.searchParams.set("service", options.service);
|
|
135
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
136
|
+
const files = await findDsyms(options.paths);
|
|
137
|
+
const uploaded = new Array(files.length);
|
|
138
|
+
let failure;
|
|
139
|
+
let next = 0;
|
|
140
|
+
const one = async (file) => {
|
|
141
|
+
const bytes = await file.read();
|
|
142
|
+
const images = machOImages(bytes).filter((i) => i.dwarf);
|
|
143
|
+
if (images.length === 0) {
|
|
144
|
+
options.onSkip?.(file.source);
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
const text = await postUpload({
|
|
148
|
+
url: base,
|
|
149
|
+
body: gzipSync(bytes),
|
|
150
|
+
contentType: "application/octet-stream",
|
|
151
|
+
token: options.token,
|
|
152
|
+
label: file.source,
|
|
153
|
+
file: file.source,
|
|
154
|
+
}, doFetch, deps);
|
|
155
|
+
const stored = storedImages(text);
|
|
156
|
+
if (stored === undefined) {
|
|
157
|
+
const detail = redact(text, options.token).trim().slice(0, 200);
|
|
158
|
+
throw new UploadError(`upload of ${file.source} returned 201 without the images it stored${detail === "" ? "" : `: ${detail}`}`, 201, file.source);
|
|
159
|
+
}
|
|
160
|
+
return { file: file.source, images: stored };
|
|
161
|
+
};
|
|
162
|
+
const worker = async () => {
|
|
163
|
+
while (failure === undefined && next < files.length) {
|
|
164
|
+
const i = next++;
|
|
165
|
+
const file = files[i];
|
|
166
|
+
if (file === undefined)
|
|
167
|
+
continue;
|
|
168
|
+
try {
|
|
169
|
+
uploaded[i] = await one(file);
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
failure ??=
|
|
173
|
+
err instanceof UploadError
|
|
174
|
+
? new DsymUploadError(err.message, err.status, file.source, [], { cause: err.cause })
|
|
175
|
+
: new DsymUploadError(`reading ${file.source} failed: ${redact(describe(err), options.token)}`, 0, file.source, [], { cause: sanitizeCause(err, options.token) });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, files.length) }, worker));
|
|
180
|
+
const done = uploaded.filter((u) => u !== undefined);
|
|
181
|
+
if (failure !== undefined) {
|
|
182
|
+
throw new DsymUploadError(failure.message, failure.status, failure.file, done, { cause: failure.cause });
|
|
183
|
+
}
|
|
184
|
+
return done;
|
|
185
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Uploads JavaScript source maps to the libo11y RUM gateway, which uses them to
|
|
3
|
-
* stack traces of browser and React Native errors. It runs on Bun
|
|
2
|
+
* Uploads JavaScript source maps and iOS debug symbols to the libo11y RUM gateway, which uses them to
|
|
3
|
+
* symbolicate the stack traces of browser and React Native errors and native crashes. It runs on Bun
|
|
4
|
+
* and Node.
|
|
4
5
|
*
|
|
5
6
|
* ```ts
|
|
6
7
|
* import { uploadSourceMaps } from "@libo11y/sourcemaps";
|
|
@@ -16,10 +17,27 @@
|
|
|
16
17
|
* });
|
|
17
18
|
* ```
|
|
18
19
|
*
|
|
19
|
-
*
|
|
20
|
+
* uploadDsyms uploads the iOS debug symbols (dSYMs) of a native build, which the gateway uses to
|
|
21
|
+
* resolve the frames of native crash and hang stacks by binary UUID:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { uploadDsyms } from "@libo11y/sourcemaps";
|
|
25
|
+
*
|
|
26
|
+
* await uploadDsyms({
|
|
27
|
+
* endpoint: "https://app.example.com",
|
|
28
|
+
* token: process.env.O11Y_SOURCEMAPS_TOKEN ?? "",
|
|
29
|
+
* service: "my-app",
|
|
30
|
+
* paths: ["ios/build"],
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* The `libo11y-sourcemaps` command does the same from a build job: `libo11y-sourcemaps upload` for
|
|
35
|
+
* source maps and `libo11y-sourcemaps dsyms` for dSYMs.
|
|
20
36
|
*
|
|
21
37
|
* @packageDocumentation
|
|
22
38
|
*/
|
|
39
|
+
export { type DsymFile, DsymUploadError, type DsymUploadOptions, findDsyms, type StoredImage, type UploadedDsym, uploadDsyms, } from "./dsyms.ts";
|
|
23
40
|
export { findSourceMaps, type SourceMapFile } from "./find.ts";
|
|
41
|
+
export { type MachOImage, machOImages } from "./macho.ts";
|
|
24
42
|
export { UploadError, type UploadedMap, type UploadOptions, uploadSourceMaps } from "./upload.ts";
|
|
25
43
|
export { VERSION } from "./version.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Uploads JavaScript source maps to the libo11y RUM gateway, which uses them to
|
|
3
|
-
* stack traces of browser and React Native errors. It runs on Bun
|
|
2
|
+
* Uploads JavaScript source maps and iOS debug symbols to the libo11y RUM gateway, which uses them to
|
|
3
|
+
* symbolicate the stack traces of browser and React Native errors and native crashes. It runs on Bun
|
|
4
|
+
* and Node.
|
|
4
5
|
*
|
|
5
6
|
* ```ts
|
|
6
7
|
* import { uploadSourceMaps } from "@libo11y/sourcemaps";
|
|
@@ -16,10 +17,27 @@
|
|
|
16
17
|
* });
|
|
17
18
|
* ```
|
|
18
19
|
*
|
|
19
|
-
*
|
|
20
|
+
* uploadDsyms uploads the iOS debug symbols (dSYMs) of a native build, which the gateway uses to
|
|
21
|
+
* resolve the frames of native crash and hang stacks by binary UUID:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { uploadDsyms } from "@libo11y/sourcemaps";
|
|
25
|
+
*
|
|
26
|
+
* await uploadDsyms({
|
|
27
|
+
* endpoint: "https://app.example.com",
|
|
28
|
+
* token: process.env.O11Y_SOURCEMAPS_TOKEN ?? "",
|
|
29
|
+
* service: "my-app",
|
|
30
|
+
* paths: ["ios/build"],
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* The `libo11y-sourcemaps` command does the same from a build job: `libo11y-sourcemaps upload` for
|
|
35
|
+
* source maps and `libo11y-sourcemaps dsyms` for dSYMs.
|
|
20
36
|
*
|
|
21
37
|
* @packageDocumentation
|
|
22
38
|
*/
|
|
39
|
+
export { DsymUploadError, findDsyms, uploadDsyms, } from "./dsyms.js";
|
|
23
40
|
export { findSourceMaps } from "./find.js";
|
|
41
|
+
export { machOImages } from "./macho.js";
|
|
24
42
|
export { UploadError, uploadSourceMaps } from "./upload.js";
|
|
25
43
|
export { VERSION } from "./version.js";
|
package/dist/macho.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Just enough of the Mach-O format to name the debug symbols a dSYM holds: each slice's
|
|
3
|
+
* architecture, its LC_UUID, and whether it has a __DWARF segment with __debug_info. Thin 32- and
|
|
4
|
+
* 64-bit files and universal (fat) files are read; every read is bounds-checked, so a truncated or
|
|
5
|
+
* foreign file gives no images instead of throwing.
|
|
6
|
+
*/
|
|
7
|
+
/** One architecture slice of a Mach-O file. */
|
|
8
|
+
export interface MachOImage {
|
|
9
|
+
/** The slice's architecture, such as arm64 or x86_64. */
|
|
10
|
+
readonly arch: string;
|
|
11
|
+
/** The binary's UUID in its 8-4-4-4-12 form, upper case, as crash reports print it. */
|
|
12
|
+
readonly uuid: string;
|
|
13
|
+
/** Whether the slice has DWARF debug information (__DWARF,__debug_info). */
|
|
14
|
+
readonly dwarf: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** The images of a thin or universal Mach-O file: one per slice with an LC_UUID. */
|
|
17
|
+
export declare function machOImages(buf: Buffer): MachOImage[];
|
package/dist/macho.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Just enough of the Mach-O format to name the debug symbols a dSYM holds: each slice's
|
|
3
|
+
* architecture, its LC_UUID, and whether it has a __DWARF segment with __debug_info. Thin 32- and
|
|
4
|
+
* 64-bit files and universal (fat) files are read; every read is bounds-checked, so a truncated or
|
|
5
|
+
* foreign file gives no images instead of throwing.
|
|
6
|
+
*/
|
|
7
|
+
const MH_MAGIC = 0xfeedface;
|
|
8
|
+
const MH_MAGIC_64 = 0xfeedfacf;
|
|
9
|
+
const FAT_MAGIC = 0xcafebabe;
|
|
10
|
+
const FAT_MAGIC_64 = 0xcafebabf;
|
|
11
|
+
const LC_SEGMENT = 0x1;
|
|
12
|
+
const LC_SEGMENT_64 = 0x19;
|
|
13
|
+
const LC_UUID = 0x1b;
|
|
14
|
+
/** More load commands than any real binary has, against a corrupt count. */
|
|
15
|
+
const MAX_COMMANDS = 4096;
|
|
16
|
+
/** More slices than any universal file has. */
|
|
17
|
+
const MAX_SLICES = 64;
|
|
18
|
+
const ARCHES = new Map([
|
|
19
|
+
[0x7, "i386"],
|
|
20
|
+
[0x01000007, "x86_64"],
|
|
21
|
+
[0xc, "arm"],
|
|
22
|
+
[0x0100000c, "arm64"],
|
|
23
|
+
[0x0200000c, "arm64_32"],
|
|
24
|
+
]);
|
|
25
|
+
function cstring(buf, at, len) {
|
|
26
|
+
const raw = buf.subarray(at, at + len);
|
|
27
|
+
const nul = raw.indexOf(0);
|
|
28
|
+
return raw.subarray(0, nul < 0 ? raw.length : nul).toString("latin1");
|
|
29
|
+
}
|
|
30
|
+
function formatUUID(b) {
|
|
31
|
+
const h = b.toString("hex").toUpperCase();
|
|
32
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
33
|
+
}
|
|
34
|
+
/** The image of one thin Mach-O, or undefined when it is not one or has no LC_UUID. */
|
|
35
|
+
function thin(buf) {
|
|
36
|
+
if (buf.length < 28)
|
|
37
|
+
return undefined;
|
|
38
|
+
const magic = buf.readUInt32LE(0);
|
|
39
|
+
if (magic !== MH_MAGIC && magic !== MH_MAGIC_64)
|
|
40
|
+
return undefined;
|
|
41
|
+
const is64 = magic === MH_MAGIC_64;
|
|
42
|
+
const cpu = buf.readUInt32LE(4);
|
|
43
|
+
const subtype = buf.readUInt32LE(8) & 0x00ffffff;
|
|
44
|
+
const ncmds = Math.min(buf.readUInt32LE(16), MAX_COMMANDS);
|
|
45
|
+
let arch = ARCHES.get(cpu) ?? `cpu${cpu}`;
|
|
46
|
+
if (arch === "arm64" && subtype === 2)
|
|
47
|
+
arch = "arm64e";
|
|
48
|
+
let at = is64 ? 32 : 28;
|
|
49
|
+
let uuid;
|
|
50
|
+
let dwarf = false;
|
|
51
|
+
for (let i = 0; i < ncmds && at + 8 <= buf.length; i++) {
|
|
52
|
+
const cmd = buf.readUInt32LE(at);
|
|
53
|
+
const size = buf.readUInt32LE(at + 4);
|
|
54
|
+
if (size < 8 || at + size > buf.length)
|
|
55
|
+
break;
|
|
56
|
+
if (cmd === LC_UUID && size >= 24)
|
|
57
|
+
uuid = formatUUID(buf.subarray(at + 8, at + 24));
|
|
58
|
+
if ((cmd === LC_SEGMENT_64 || cmd === LC_SEGMENT) && cstring(buf, at + 8, 16) === "__DWARF") {
|
|
59
|
+
const header = cmd === LC_SEGMENT_64 ? 72 : 56;
|
|
60
|
+
const section = cmd === LC_SEGMENT_64 ? 80 : 68;
|
|
61
|
+
if (size < header) {
|
|
62
|
+
at += size;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const nsects = buf.readUInt32LE(at + (cmd === LC_SEGMENT_64 ? 64 : 48));
|
|
66
|
+
for (let s = 0; s < nsects; s++) {
|
|
67
|
+
const sa = at + header + s * section;
|
|
68
|
+
if (sa + 16 > at + size)
|
|
69
|
+
break;
|
|
70
|
+
if (cstring(buf, sa, 16) === "__debug_info")
|
|
71
|
+
dwarf = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
at += size;
|
|
75
|
+
}
|
|
76
|
+
return uuid === undefined ? undefined : { arch, uuid, dwarf };
|
|
77
|
+
}
|
|
78
|
+
/** The images of a thin or universal Mach-O file: one per slice with an LC_UUID. */
|
|
79
|
+
export function machOImages(buf) {
|
|
80
|
+
if (buf.length < 8)
|
|
81
|
+
return [];
|
|
82
|
+
const magic = buf.readUInt32BE(0);
|
|
83
|
+
if (magic !== FAT_MAGIC && magic !== FAT_MAGIC_64) {
|
|
84
|
+
const image = thin(buf);
|
|
85
|
+
return image === undefined ? [] : [image];
|
|
86
|
+
}
|
|
87
|
+
const is64 = magic === FAT_MAGIC_64;
|
|
88
|
+
const entry = is64 ? 32 : 20;
|
|
89
|
+
const n = Math.min(buf.readUInt32BE(4), MAX_SLICES);
|
|
90
|
+
const out = [];
|
|
91
|
+
for (let i = 0; i < n; i++) {
|
|
92
|
+
const at = 8 + i * entry;
|
|
93
|
+
if (at + entry > buf.length)
|
|
94
|
+
break;
|
|
95
|
+
const offset = is64 ? Number(buf.readBigUInt64BE(at + 8)) : buf.readUInt32BE(at + 8);
|
|
96
|
+
const size = is64 ? Number(buf.readBigUInt64BE(at + 16)) : buf.readUInt32BE(at + 12);
|
|
97
|
+
if (offset + size > buf.length)
|
|
98
|
+
continue;
|
|
99
|
+
const image = thin(buf.subarray(offset, offset + size));
|
|
100
|
+
if (image !== undefined)
|
|
101
|
+
out.push(image);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
package/dist/upload.d.ts
CHANGED
|
@@ -50,6 +50,7 @@ export interface UploadDeps {
|
|
|
50
50
|
/** How long one attempt may take. Default 120000. */
|
|
51
51
|
readonly timeoutMillis?: number;
|
|
52
52
|
}
|
|
53
|
+
export declare const defaultDeps: UploadDeps;
|
|
53
54
|
/**
|
|
54
55
|
* Finds the scripts with external source maps under options.directory and uploads each map to the
|
|
55
56
|
* gateway's POST /_o11y/v1/sourcemaps, gzipped. A 429, a 5xx, a timeout or a network failure is
|
|
@@ -63,3 +64,26 @@ export declare function uploadSourceMaps(options: UploadOptions): Promise<Upload
|
|
|
63
64
|
export declare function uploadSourceMapsWith(options: UploadOptions, deps: UploadDeps): Promise<UploadedMap[]>;
|
|
64
65
|
/** Throws when token is empty or holds a character an HTTP header cannot carry. It never echoes it. */
|
|
65
66
|
export declare function checkToken(token: string): void;
|
|
67
|
+
/** The gateway URL of an upload path under endpoint. */
|
|
68
|
+
export declare function endpointURL(endpoint: string, path?: string): URL;
|
|
69
|
+
export declare function describe(err: unknown): string;
|
|
70
|
+
export declare function redact(message: string, token: string): string;
|
|
71
|
+
export declare function sanitizeCause(err: unknown, token: string, seen?: Set<unknown>): Error | undefined;
|
|
72
|
+
/** One gzipped upload to the gateway. */
|
|
73
|
+
export interface PostUpload {
|
|
74
|
+
readonly url: URL;
|
|
75
|
+
readonly body: Buffer<ArrayBuffer>;
|
|
76
|
+
readonly contentType: string;
|
|
77
|
+
readonly token: string;
|
|
78
|
+
/** What the upload is, for error messages: a script URL or a file path. */
|
|
79
|
+
readonly label: string;
|
|
80
|
+
/** The file UploadError names. */
|
|
81
|
+
readonly file: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* POSTs one gzipped body with the bearer token and returns the text of the 201 response. A 429, a
|
|
85
|
+
* 5xx, a timeout or a network failure is retried, up to three attempts in all, waiting for the
|
|
86
|
+
* response's Retry-After (at most ten seconds) when it has one. Anything else throws an UploadError
|
|
87
|
+
* with the token redacted.
|
|
88
|
+
*/
|
|
89
|
+
export declare function postUpload(p: PostUpload, doFetch: typeof fetch, deps: UploadDeps): Promise<string>;
|
package/dist/upload.js
CHANGED
|
@@ -25,7 +25,7 @@ const DEFAULT_TIMEOUT_MILLIS = 120_000;
|
|
|
25
25
|
// An HTTP header value can carry only visible ASCII without surprises. A token with anything else,
|
|
26
26
|
// such as a line break pasted into a CI secret, would make fetch throw an error quoting it.
|
|
27
27
|
const TOKEN = /^[\x21-\x7e]+$/;
|
|
28
|
-
const defaultDeps = {
|
|
28
|
+
export const defaultDeps = {
|
|
29
29
|
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
30
30
|
};
|
|
31
31
|
/**
|
|
@@ -86,7 +86,8 @@ export function checkToken(token) {
|
|
|
86
86
|
throw new TypeError("@libo11y/sourcemaps: the upload token holds white space or characters an HTTP header cannot carry");
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
|
|
89
|
+
/** The gateway URL of an upload path under endpoint. */
|
|
90
|
+
export function endpointURL(endpoint, path = UPLOAD_PATH) {
|
|
90
91
|
let url;
|
|
91
92
|
try {
|
|
92
93
|
url = new URL(endpoint);
|
|
@@ -96,11 +97,11 @@ function endpointURL(endpoint) {
|
|
|
96
97
|
}
|
|
97
98
|
if (!url.pathname.endsWith("/"))
|
|
98
99
|
url.pathname += "/";
|
|
99
|
-
return new URL(
|
|
100
|
+
return new URL(path, url);
|
|
100
101
|
}
|
|
101
102
|
// describe renders an error with its cause, where fetch keeps the reason a request failed, such as
|
|
102
103
|
// ECONNREFUSED or a certificate error.
|
|
103
|
-
function describe(err) {
|
|
104
|
+
export function describe(err) {
|
|
104
105
|
if (!(err instanceof Error))
|
|
105
106
|
return String(err);
|
|
106
107
|
const cause = err.cause;
|
|
@@ -111,11 +112,11 @@ function describe(err) {
|
|
|
111
112
|
return err.message;
|
|
112
113
|
}
|
|
113
114
|
// redact removes the token from a message, whatever a runtime put in its error.
|
|
114
|
-
function redact(message, token) {
|
|
115
|
+
export function redact(message, token) {
|
|
115
116
|
return token === "" ? message : message.split(token).join("[token]");
|
|
116
117
|
}
|
|
117
118
|
// sanitizeCause clones an error chain with the token redacted so UploadError.cause never leaks it.
|
|
118
|
-
function sanitizeCause(err, token, seen = new Set()) {
|
|
119
|
+
export function sanitizeCause(err, token, seen = new Set()) {
|
|
119
120
|
if (err === undefined || err === null)
|
|
120
121
|
return undefined;
|
|
121
122
|
if (err instanceof Error) {
|
|
@@ -146,19 +147,33 @@ async function uploadOne(base, options, file, doFetch, deps) {
|
|
|
146
147
|
}
|
|
147
148
|
url.searchParams.set("url", file.url);
|
|
148
149
|
const body = gzipSync(await readFile(file.map));
|
|
150
|
+
const text = await postUpload({ url, body, contentType: "application/json", token: options.token, label: file.url, file: file.script }, doFetch, deps);
|
|
151
|
+
const key = keyOf(text);
|
|
152
|
+
if (key !== "")
|
|
153
|
+
return { script: file.script, url: file.url, key };
|
|
154
|
+
const detail = redact(text, options.token).trim().slice(0, 200);
|
|
155
|
+
throw new UploadError(`upload of ${file.url} returned 201 without an object key${detail === "" ? "" : `: ${detail}`}`, 201, file.script);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* POSTs one gzipped body with the bearer token and returns the text of the 201 response. A 429, a
|
|
159
|
+
* 5xx, a timeout or a network failure is retried, up to three attempts in all, waiting for the
|
|
160
|
+
* response's Retry-After (at most ten seconds) when it has one. Anything else throws an UploadError
|
|
161
|
+
* with the token redacted.
|
|
162
|
+
*/
|
|
163
|
+
export async function postUpload(p, doFetch, deps) {
|
|
149
164
|
const timeoutMillis = deps.timeoutMillis ?? DEFAULT_TIMEOUT_MILLIS;
|
|
150
165
|
for (let attempt = 1;; attempt++) {
|
|
151
166
|
let res;
|
|
152
167
|
let body201;
|
|
153
168
|
try {
|
|
154
|
-
res = await doFetch(url, {
|
|
169
|
+
res = await doFetch(p.url, {
|
|
155
170
|
method: "POST",
|
|
156
171
|
headers: {
|
|
157
|
-
authorization: `Bearer ${
|
|
158
|
-
"content-type":
|
|
172
|
+
authorization: `Bearer ${p.token}`,
|
|
173
|
+
"content-type": p.contentType,
|
|
159
174
|
"content-encoding": "gzip",
|
|
160
175
|
},
|
|
161
|
-
body,
|
|
176
|
+
body: p.body,
|
|
162
177
|
signal: AbortSignal.timeout(timeoutMillis),
|
|
163
178
|
});
|
|
164
179
|
if (res.status === 201)
|
|
@@ -171,27 +186,21 @@ async function uploadOne(base, options, file, doFetch, deps) {
|
|
|
171
186
|
}
|
|
172
187
|
const reason = err instanceof Error && err.name === "TimeoutError"
|
|
173
188
|
? `timed out after ${timeoutMillis} ms`
|
|
174
|
-
: redact(describe(err),
|
|
175
|
-
throw new UploadError(`upload of ${
|
|
176
|
-
cause: sanitizeCause(err,
|
|
189
|
+
: redact(describe(err), p.token);
|
|
190
|
+
throw new UploadError(`upload of ${p.label} could not reach the gateway: ${reason}`, 0, p.file, [], {
|
|
191
|
+
cause: sanitizeCause(err, p.token),
|
|
177
192
|
});
|
|
178
193
|
}
|
|
179
|
-
if (res.status === 201)
|
|
180
|
-
|
|
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);
|
|
186
|
-
}
|
|
194
|
+
if (res.status === 201)
|
|
195
|
+
return body201 ?? "";
|
|
187
196
|
if ((res.status === 429 || res.status >= 500) && attempt < MAX_ATTEMPTS) {
|
|
188
197
|
await res.body?.cancel().catch(() => { });
|
|
189
198
|
await deps.sleep(retryDelay(attempt, res.headers.get("retry-after")));
|
|
190
199
|
continue;
|
|
191
200
|
}
|
|
192
201
|
const text = await res.text().catch(() => "");
|
|
193
|
-
const detail = redact(text,
|
|
194
|
-
throw new UploadError(`upload of ${
|
|
202
|
+
const detail = redact(text, p.token).trim().slice(0, 200);
|
|
203
|
+
throw new UploadError(`upload of ${p.label} failed with ${res.status}${detail === "" ? "" : `: ${detail}`}`, res.status, p.file);
|
|
195
204
|
}
|
|
196
205
|
}
|
|
197
206
|
function keyOf(text) {
|
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.
|
|
2
|
+
export declare const VERSION = "0.8.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.
|
|
2
|
+
export const VERSION = "0.8.0";
|
package/dist/zip.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** One file in a zip archive. */
|
|
2
|
+
export interface ZipFile {
|
|
3
|
+
/** The entry's path inside the archive, with forward slashes. */
|
|
4
|
+
readonly name: string;
|
|
5
|
+
/** The uncompressed size the archive records. */
|
|
6
|
+
readonly size: number;
|
|
7
|
+
/** Inflates the entry. Throws when it is encrypted, uses another method or its size is wrong. */
|
|
8
|
+
read(): Buffer;
|
|
9
|
+
}
|
|
10
|
+
/** The files of a zip archive held in memory. Directory entries are left out. */
|
|
11
|
+
export declare function zipEntries(buf: Buffer): ZipFile[];
|
package/dist/zip.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small zip reader for dSYM archives, such as the <app>.app.dSYM.zip fastlane writes on EAS Build:
|
|
3
|
+
* the central directory, stored and deflated entries, and no encryption or ZIP64. Entries are read
|
|
4
|
+
* one at a time, so an archive of many dSYMs is never inflated all at once.
|
|
5
|
+
*/
|
|
6
|
+
import { inflateRawSync } from "node:zlib";
|
|
7
|
+
const EOCD = 0x06054b50;
|
|
8
|
+
const CENTRAL = 0x02014b50;
|
|
9
|
+
const LOCAL = 0x04034b50;
|
|
10
|
+
/** The end of central directory record is 22 bytes plus a comment of at most 65535. */
|
|
11
|
+
const EOCD_SEARCH = 22 + 0xffff;
|
|
12
|
+
function fail(message) {
|
|
13
|
+
throw new Error(`zip: ${message}`);
|
|
14
|
+
}
|
|
15
|
+
/** The files of a zip archive held in memory. Directory entries are left out. */
|
|
16
|
+
export function zipEntries(buf) {
|
|
17
|
+
let end = -1;
|
|
18
|
+
for (let i = buf.length - 22; i >= Math.max(0, buf.length - EOCD_SEARCH); i--) {
|
|
19
|
+
if (buf.readUInt32LE(i) === EOCD) {
|
|
20
|
+
end = i;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (end < 0)
|
|
25
|
+
fail("not a zip file (no end of central directory)");
|
|
26
|
+
const count = buf.readUInt16LE(end + 10);
|
|
27
|
+
const cdSize = buf.readUInt32LE(end + 12);
|
|
28
|
+
const cdOffset = buf.readUInt32LE(end + 16);
|
|
29
|
+
if (count === 0xffff || cdOffset === 0xffffffff)
|
|
30
|
+
fail("ZIP64 archives are not supported");
|
|
31
|
+
if (cdOffset + cdSize > end)
|
|
32
|
+
fail("the central directory lies outside the file");
|
|
33
|
+
const files = [];
|
|
34
|
+
let at = cdOffset;
|
|
35
|
+
for (let i = 0; i < count; i++) {
|
|
36
|
+
if (at + 46 > end || buf.readUInt32LE(at) !== CENTRAL)
|
|
37
|
+
fail("a central directory entry is damaged");
|
|
38
|
+
const flags = buf.readUInt16LE(at + 8);
|
|
39
|
+
const method = buf.readUInt16LE(at + 10);
|
|
40
|
+
const compressed = buf.readUInt32LE(at + 20);
|
|
41
|
+
const size = buf.readUInt32LE(at + 24);
|
|
42
|
+
const nameLen = buf.readUInt16LE(at + 28);
|
|
43
|
+
const extraLen = buf.readUInt16LE(at + 30);
|
|
44
|
+
const commentLen = buf.readUInt16LE(at + 32);
|
|
45
|
+
const local = buf.readUInt32LE(at + 42);
|
|
46
|
+
const name = buf.subarray(at + 46, at + 46 + nameLen).toString((flags & 0x800) !== 0 ? "utf8" : "latin1");
|
|
47
|
+
at += 46 + nameLen + extraLen + commentLen;
|
|
48
|
+
if (name.endsWith("/"))
|
|
49
|
+
continue;
|
|
50
|
+
files.push({
|
|
51
|
+
name,
|
|
52
|
+
size,
|
|
53
|
+
read() {
|
|
54
|
+
if ((flags & 0x1) !== 0)
|
|
55
|
+
fail(`${name} is encrypted`);
|
|
56
|
+
if (compressed === 0xffffffff || size === 0xffffffff)
|
|
57
|
+
fail(`${name}: ZIP64 entries are not supported`);
|
|
58
|
+
if (local + 30 > buf.length || buf.readUInt32LE(local) !== LOCAL)
|
|
59
|
+
fail(`${name}: the local header is damaged`);
|
|
60
|
+
const start = local + 30 + buf.readUInt16LE(local + 26) + buf.readUInt16LE(local + 28);
|
|
61
|
+
if (start + compressed > buf.length)
|
|
62
|
+
fail(`${name} runs past the end of the file`);
|
|
63
|
+
const data = buf.subarray(start, start + compressed);
|
|
64
|
+
let out;
|
|
65
|
+
if (method === 0)
|
|
66
|
+
out = Buffer.from(data);
|
|
67
|
+
else if (method === 8) {
|
|
68
|
+
try {
|
|
69
|
+
out = inflateRawSync(data, { maxOutputLength: Math.max(size, 1) });
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
fail(`${name} does not inflate to its recorded size: ${err instanceof Error ? err.message : String(err)}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else
|
|
76
|
+
fail(`${name} uses compression method ${method}`);
|
|
77
|
+
if (out.length !== size)
|
|
78
|
+
fail(`${name} has size ${out.length}, not the recorded ${size}`);
|
|
79
|
+
return out;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return files;
|
|
84
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libo11y/sourcemaps",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Uploads JavaScript source maps to the libo11y RUM gateway so it can symbolicate browser
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Uploads JavaScript source maps and iOS dSYMs to the libo11y RUM gateway so it can symbolicate browser, React Native and native iOS stack traces. A library and the libo11y-sourcemaps command for Bun and Node.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"directory": "ts/packages/sourcemaps"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@libo11y/test": "0.
|
|
23
|
+
"@libo11y/test": "0.8.0"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"dist",
|