actor-debugger 0.1.1
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/LICENSE +201 -0
- package/README.md +110 -0
- package/bin/cli.mjs +134 -0
- package/lib/debug_server.mjs +117 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# actor-debugger
|
|
2
|
+
|
|
3
|
+
Drop-in remote debugging for **any Apify Node/TS Actor** with a one-line Dockerfile change. It
|
|
4
|
+
launches your Actor under the Node inspector **and serves a full Chrome DevTools UI over the run's
|
|
5
|
+
container URL** — so you open one link in your own browser and debug. No wstunnel, no local setup,
|
|
6
|
+
no rebuild of your source, and **no browser in the Actor**.
|
|
7
|
+
|
|
8
|
+
```dockerfile
|
|
9
|
+
# auto-detect the Actor's entrypoint:
|
|
10
|
+
CMD ["npx", "actor-debugger"]
|
|
11
|
+
|
|
12
|
+
# or point at a specific entry:
|
|
13
|
+
CMD ["npx", "actor-debugger", "dist/main.js"]
|
|
14
|
+
|
|
15
|
+
# pause on the first line until a debugger attaches (for short-lived Actors):
|
|
16
|
+
CMD ["npx", "actor-debugger", "--brk"]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## How it works
|
|
20
|
+
|
|
21
|
+
1. It resolves your Actor's entrypoint (see detection order below) and launches it as
|
|
22
|
+
`node --inspect=127.0.0.1:9229 <entry>` — so the inspector is on **your** code, in its own
|
|
23
|
+
process, exactly as it normally runs.
|
|
24
|
+
2. It runs one HTTP server on `ACTOR_WEB_SERVER_PORT` that:
|
|
25
|
+
- **serves the Chrome DevTools frontend** (static files) so your *local* browser opens it — no
|
|
26
|
+
Chrome, Xvfb, or VNC in the Actor;
|
|
27
|
+
- **proxies the CDP WebSocket** to the inspector, rewriting `Host` to a loopback IP and dropping
|
|
28
|
+
`Origin`. Node's inspector rejects DNS-name hosts (anti-DNS-rebinding), so this rewrite is
|
|
29
|
+
what lets a browser reach it over `<run>.runs.apify.net`.
|
|
30
|
+
3. The run log prints one URL. Open it in your browser → real DevTools attached to your Actor.
|
|
31
|
+
|
|
32
|
+
The DevTools frontend comes from **[`chii`](https://github.com/liriliri/chii)** (MIT), which ships
|
|
33
|
+
a *prebuilt* Chrome DevTools frontend. The `chrome-devtools-frontend` npm package is unbuilt
|
|
34
|
+
TypeScript source (needs Chromium's GN/ninja toolchain to compile), so it can't be served as-is —
|
|
35
|
+
chii's build is the same frontend, ready to serve.
|
|
36
|
+
|
|
37
|
+
## Activation
|
|
38
|
+
|
|
39
|
+
Running the Actor through `npx actor-debugger` **is** the switch — debugging is on whenever the
|
|
40
|
+
`CMD` line above is in place. To turn it off, revert the `CMD` to the Actor's normal entrypoint
|
|
41
|
+
(e.g. `CMD ["npm", "start"]`) and rebuild. Pass `--brk` to pause on the first line until a
|
|
42
|
+
debugger attaches — useful for Actors that would otherwise finish before you connect.
|
|
43
|
+
|
|
44
|
+
## Connect
|
|
45
|
+
|
|
46
|
+
Open the URL the run log prints, in your own browser:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
https://<run>.runs.apify.net/devtools/js_app.html?wss=<run>.runs.apify.net/<uuid>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
That page **is** Chrome DevTools; it connects to your Actor over the container URL. Set breakpoints
|
|
53
|
+
in your sources (via source maps), step, inspect — no `devtools://` URL, no local install.
|
|
54
|
+
Prefer the raw channel? `npx wscat -c "wss://<run>.runs.apify.net/<uuid>"`, or point
|
|
55
|
+
`Playwright/Puppeteer connectOverCDP` at that wss URL.
|
|
56
|
+
|
|
57
|
+
## Entrypoint detection order
|
|
58
|
+
|
|
59
|
+
1. An explicit path argument, if given.
|
|
60
|
+
2. The file in `package.json` `scripts.start` (e.g. `node dist/main.js` → `dist/main.js`).
|
|
61
|
+
3. `package.json` `main`.
|
|
62
|
+
4. Conventional paths: `dist/main.js`, `dist/index.js`, `build/main.js`, `src/main.js`, `main.js`, `index.js`.
|
|
63
|
+
|
|
64
|
+
## Security
|
|
65
|
+
|
|
66
|
+
The debug endpoint is **unauthenticated** — anyone who reaches the container URL and the run's
|
|
67
|
+
`uuid` can execute code in your run (and read its env, including `APIFY_TOKEN`). Keep the
|
|
68
|
+
`actor-debugger` `CMD` only in builds you are actively debugging, prefer a restricted run/token,
|
|
69
|
+
never ship it in a published Actor, and gate the endpoint (owner-only) before any non-prototype
|
|
70
|
+
use.
|
|
71
|
+
|
|
72
|
+
## Verified
|
|
73
|
+
|
|
74
|
+
Tested end-to-end against a **browserless** generic sample TS Actor (`example-actor/`, plain
|
|
75
|
+
`apify/actor-node`, no Chrome):
|
|
76
|
+
|
|
77
|
+
- The one-line `CMD` detects the entrypoint and runs the Actor under `--inspect`; explicit path
|
|
78
|
+
and auto-detect modes both pass.
|
|
79
|
+
- `/devtools/js_app.html` and its assets are served (200); `/json` is proxied; a CDP client
|
|
80
|
+
round-trips `Runtime.evaluate` through the proxy with browser-like `Host`/`Origin`.
|
|
81
|
+
- **Headless Chromium loaded the served DevTools frontend and the Node inspector reported
|
|
82
|
+
"Debugger attached"** — i.e. the served Chrome DevTools UI genuinely connects to the Actor with no
|
|
83
|
+
browser in the image.
|
|
84
|
+
|
|
85
|
+
One thing to confirm on real infrastructure: the frontend's `wss://` connection through Apify's
|
|
86
|
+
ingress when the ingress negotiates HTTP/2 (WebSocket-over-h2). See the sibling
|
|
87
|
+
`typescript-debug-browser` handoff for the `curl --http1.1` probe that settles it.
|
|
88
|
+
|
|
89
|
+
## Releasing
|
|
90
|
+
|
|
91
|
+
Publishing to npm is automated by [`.github/workflows/publish.yml`](.github/workflows/publish.yml),
|
|
92
|
+
which fires on `v*` tags. To cut a release:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm version patch # or minor / major - bumps package.json, commits, tags vX.Y.Z
|
|
96
|
+
git push --follow-tags
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The workflow syntax-checks the sources, smoke-tests both modes (disabled pass-through and the
|
|
100
|
+
debug server with `/json/list` + DevTools frontend), verifies the tag matches `package.json`
|
|
101
|
+
`version`, and then runs `npm publish`.
|
|
102
|
+
|
|
103
|
+
One-time setup: create a granular npm access token with publish rights for this package and save
|
|
104
|
+
it as the repository secret `NPM_TOKEN`. The publish job runs in the `npm` GitHub environment, so
|
|
105
|
+
you can optionally add required reviewers there to gate releases.
|
|
106
|
+
|
|
107
|
+
## Notes
|
|
108
|
+
|
|
109
|
+
- Depends on `chii` for the prebuilt DevTools frontend (~16 MB of static assets, no runtime browser).
|
|
110
|
+
- Pure Node otherwise — works on any `apify/actor-node*` base image, no Xvfb / Chrome / apt.
|
package/bin/cli.mjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* actor-debugger - launch an Apify Node/TS Actor under the Node inspector, reachable over the
|
|
4
|
+
* run's container URL, with a one-line Dockerfile change:
|
|
5
|
+
*
|
|
6
|
+
* CMD ["npx", "actor-debugger"] # auto-detect the Actor's entrypoint
|
|
7
|
+
* CMD ["npx", "actor-debugger", "dist/x.js"] # explicit entrypoint
|
|
8
|
+
* CMD ["npx", "actor-debugger", "--brk"] # pause on the first line until attached
|
|
9
|
+
*
|
|
10
|
+
* Running through this command is what enables debugging - revert the CMD to the normal
|
|
11
|
+
* entrypoint to turn it off.
|
|
12
|
+
*/
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import http from 'node:http';
|
|
16
|
+
import { createRequire } from 'node:module';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
|
|
19
|
+
import { startDebugServer } from '../lib/debug_server.mjs';
|
|
20
|
+
|
|
21
|
+
const INSPECTOR_PORT = 9229;
|
|
22
|
+
const TAG = '[actor-debugger]';
|
|
23
|
+
|
|
24
|
+
/** Locate chii's prebuilt Chrome DevTools frontend (chrome-devtools-frontend npm ships unbuilt source). */
|
|
25
|
+
function findFrontendDir() {
|
|
26
|
+
const req = createRequire(import.meta.url);
|
|
27
|
+
for (const spec of ['chii/public/front_end/js_app.html', 'chii/package.json']) {
|
|
28
|
+
try {
|
|
29
|
+
const resolved = req.resolve(spec);
|
|
30
|
+
const dir = spec.endsWith('package.json')
|
|
31
|
+
? path.join(path.dirname(resolved), 'public', 'front_end')
|
|
32
|
+
: path.dirname(resolved);
|
|
33
|
+
if (fs.existsSync(path.join(dir, 'js_app.html'))) return dir;
|
|
34
|
+
} catch {
|
|
35
|
+
// try next
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function resolveEntry(argEntry) {
|
|
42
|
+
if (argEntry) return path.resolve(argEntry);
|
|
43
|
+
const candidates = [];
|
|
44
|
+
try {
|
|
45
|
+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
|
|
46
|
+
const startMatch = pkg.scripts?.start && /node\s+(?:--\S+\s+)*(\S+)/.exec(pkg.scripts.start);
|
|
47
|
+
if (startMatch) candidates.push(startMatch[1]);
|
|
48
|
+
if (pkg.main) candidates.push(pkg.main);
|
|
49
|
+
} catch {
|
|
50
|
+
// no/invalid package.json - fall through to conventional paths
|
|
51
|
+
}
|
|
52
|
+
candidates.push('dist/main.js', 'dist/index.js', 'build/main.js', 'src/main.js', 'main.js', 'index.js');
|
|
53
|
+
for (const candidate of candidates) {
|
|
54
|
+
if (candidate && fs.existsSync(candidate)) return path.resolve(candidate);
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function fetchInspectorUuid(retries = 40) {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
const attempt = (left) => {
|
|
62
|
+
const req = http.get(
|
|
63
|
+
{ host: '127.0.0.1', port: INSPECTOR_PORT, path: '/json/list', timeout: 500 },
|
|
64
|
+
(res) => {
|
|
65
|
+
let body = '';
|
|
66
|
+
res.on('data', (c) => (body += c));
|
|
67
|
+
res.on('end', () => {
|
|
68
|
+
try {
|
|
69
|
+
const id = JSON.parse(body)[0]?.id;
|
|
70
|
+
if (id) return resolve(id);
|
|
71
|
+
} catch {
|
|
72
|
+
// not ready yet
|
|
73
|
+
}
|
|
74
|
+
left > 0 ? setTimeout(() => attempt(left - 1), 250) : resolve(null);
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
req.on('error', () => (left > 0 ? setTimeout(() => attempt(left - 1), 250) : resolve(null)));
|
|
79
|
+
req.on('timeout', () => req.destroy());
|
|
80
|
+
};
|
|
81
|
+
attempt(retries);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function announce(webServerUrl, hasFrontend) {
|
|
86
|
+
const uuid = await fetchInspectorUuid();
|
|
87
|
+
if (!uuid) {
|
|
88
|
+
console.error(`${TAG} inspector did not come up on 127.0.0.1:${INSPECTOR_PORT}.`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const base = webServerUrl.replace(/\/$/, '');
|
|
92
|
+
const host = base.replace(/^https?:\/\//, '');
|
|
93
|
+
console.error('='.repeat(72));
|
|
94
|
+
console.error(`${TAG} debugging is ON - reachable over the container URL.`);
|
|
95
|
+
if (hasFrontend) {
|
|
96
|
+
console.error(`${TAG} OPEN THIS in your local browser for a full DevTools UI (no local setup):`);
|
|
97
|
+
console.error(`${TAG} ${base}/devtools/js_app.html?wss=${host}/${uuid}`);
|
|
98
|
+
}
|
|
99
|
+
console.error(`${TAG} or verify the raw CDP channel: npx wscat -c "wss://${host}/${uuid}"`);
|
|
100
|
+
console.error(`${TAG} then send {"id":1,"method":"Runtime.evaluate","params":{"expression":"2+2"}}`);
|
|
101
|
+
console.error('='.repeat(72));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const args = process.argv.slice(2);
|
|
105
|
+
const brk = args.includes('--brk');
|
|
106
|
+
const entry = resolveEntry(args.find((a) => !a.startsWith('--')));
|
|
107
|
+
if (!entry) {
|
|
108
|
+
console.error(`${TAG} could not find an Actor entrypoint. Pass one explicitly:`);
|
|
109
|
+
console.error(`${TAG} CMD ["npx", "actor-debugger", "dist/main.js"]`);
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const inspectFlag = brk ? '--inspect-brk' : '--inspect';
|
|
114
|
+
const nodeArgs = ['--enable-source-maps', `${inspectFlag}=127.0.0.1:${INSPECTOR_PORT}`];
|
|
115
|
+
|
|
116
|
+
const child = spawn(process.execPath, [...nodeArgs, entry], { stdio: 'inherit', env: process.env });
|
|
117
|
+
|
|
118
|
+
let server;
|
|
119
|
+
const { ACTOR_WEB_SERVER_PORT: port, ACTOR_WEB_SERVER_URL: url } = process.env;
|
|
120
|
+
if (port && url) {
|
|
121
|
+
const frontendDir = findFrontendDir();
|
|
122
|
+
if (!frontendDir) console.error(`${TAG} DevTools frontend (chii) not found - serving CDP only, no UI.`);
|
|
123
|
+
server = startDebugServer({ listenPort: Number(port), inspectorPort: INSPECTOR_PORT, frontendDir });
|
|
124
|
+
announce(url, Boolean(frontendDir));
|
|
125
|
+
} else {
|
|
126
|
+
console.error(`${TAG} ACTOR_WEB_SERVER_URL/PORT not set - inspector on 127.0.0.1:${INSPECTOR_PORT} only (no container-URL bridge).`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
child.on('exit', (code, signal) => {
|
|
130
|
+
server?.close();
|
|
131
|
+
process.exit(code ?? (signal ? 1 : 0));
|
|
132
|
+
});
|
|
133
|
+
process.on('SIGTERM', () => child.kill('SIGTERM'));
|
|
134
|
+
process.on('SIGINT', () => child.kill('SIGINT'));
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import net from 'node:net';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
const MIME = {
|
|
7
|
+
'.html': 'text/html; charset=utf-8',
|
|
8
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
9
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
10
|
+
'.css': 'text/css; charset=utf-8',
|
|
11
|
+
'.json': 'application/json; charset=utf-8',
|
|
12
|
+
'.wasm': 'application/wasm',
|
|
13
|
+
'.svg': 'image/svg+xml',
|
|
14
|
+
'.png': 'image/png',
|
|
15
|
+
'.gif': 'image/gif',
|
|
16
|
+
'.map': 'application/json; charset=utf-8',
|
|
17
|
+
'.woff2': 'font/woff2',
|
|
18
|
+
'.ttf': 'font/ttf',
|
|
19
|
+
'.avif': 'image/avif',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const UI_PREFIX = '/devtools/';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A single HTTP server on the container's web-server port that:
|
|
26
|
+
* - serves the Chrome DevTools frontend (static files) so your LOCAL browser opens it - no Chrome
|
|
27
|
+
* in the Actor;
|
|
28
|
+
* - proxies the `/json*` discovery endpoints to the inspector;
|
|
29
|
+
* - proxies the CDP WebSocket upgrade to the inspector, rewriting `Host` to a loopback IP and
|
|
30
|
+
* dropping `Origin` so Node's inspector guard accepts the browser's connection.
|
|
31
|
+
*
|
|
32
|
+
* @param {{ listenPort: number, inspectorPort: number, frontendDir: string|null }} options
|
|
33
|
+
* @returns {import('node:http').Server}
|
|
34
|
+
*/
|
|
35
|
+
export function startDebugServer({ listenPort, inspectorPort, frontendDir }) {
|
|
36
|
+
const rewriteHost = `127.0.0.1:${inspectorPort}`;
|
|
37
|
+
|
|
38
|
+
const serveStatic = (res, relPath) => {
|
|
39
|
+
if (!frontendDir) {
|
|
40
|
+
res.writeHead(404).end('DevTools frontend not bundled');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const safeRel = path.normalize(relPath).replace(/^(\.\.(\/|\\|$))+/, '');
|
|
44
|
+
const filePath = path.join(frontendDir, safeRel);
|
|
45
|
+
if (!filePath.startsWith(frontendDir)) {
|
|
46
|
+
res.writeHead(403).end('forbidden');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
fs.stat(filePath, (err, stat) => {
|
|
50
|
+
if (err || !stat.isFile()) {
|
|
51
|
+
res.writeHead(404).end('not found');
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
res.writeHead(200, { 'content-type': MIME[path.extname(filePath).toLowerCase()] ?? 'application/octet-stream' });
|
|
55
|
+
fs.createReadStream(filePath).pipe(res);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const proxyJson = (req, res) => {
|
|
60
|
+
const headers = { ...req.headers, host: rewriteHost };
|
|
61
|
+
delete headers.origin;
|
|
62
|
+
const upstream = http.request(
|
|
63
|
+
{ host: '127.0.0.1', port: inspectorPort, path: req.url, method: req.method, headers },
|
|
64
|
+
(up) => {
|
|
65
|
+
res.writeHead(up.statusCode ?? 502, up.headers);
|
|
66
|
+
up.pipe(res);
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
upstream.on('error', () => res.writeHead(502).end('inspector unavailable'));
|
|
70
|
+
req.pipe(upstream);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const server = http.createServer((req, res) => {
|
|
74
|
+
const urlPath = (req.url ?? '/').split('?')[0];
|
|
75
|
+
if (urlPath === '/' || urlPath === '') {
|
|
76
|
+
res.writeHead(302, { location: `${UI_PREFIX}js_app.html` }).end();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (urlPath.startsWith('/json')) {
|
|
80
|
+
proxyJson(req, res);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// Serve any other GET as a static frontend asset (js_app.html loads assets by relative and
|
|
84
|
+
// root paths), so both /devtools/js_app.html and /core/... resolve into the frontend dir.
|
|
85
|
+
const rel = urlPath.startsWith(UI_PREFIX) ? urlPath.slice(UI_PREFIX.length) : urlPath.replace(/^\//, '');
|
|
86
|
+
serveStatic(res, rel);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// CDP WebSocket: rewrite Host -> loopback IP, drop Origin, then splice the sockets.
|
|
90
|
+
server.on('upgrade', (req, clientSocket, head) => {
|
|
91
|
+
const upstream = net.connect(inspectorPort, '127.0.0.1', () => {
|
|
92
|
+
const lines = [`${req.method} ${req.url} HTTP/1.1`];
|
|
93
|
+
const raw = req.rawHeaders;
|
|
94
|
+
for (let i = 0; i < raw.length; i += 2) {
|
|
95
|
+
const key = raw[i];
|
|
96
|
+
if (/^host$/i.test(key)) lines.push(`Host: ${rewriteHost}`);
|
|
97
|
+
else if (/^origin$/i.test(key)) continue;
|
|
98
|
+
else lines.push(`${key}: ${raw[i + 1]}`);
|
|
99
|
+
}
|
|
100
|
+
upstream.write(`${lines.join('\r\n')}\r\n\r\n`);
|
|
101
|
+
if (head && head.length) upstream.write(head);
|
|
102
|
+
upstream.pipe(clientSocket);
|
|
103
|
+
clientSocket.pipe(upstream);
|
|
104
|
+
});
|
|
105
|
+
const close = () => {
|
|
106
|
+
clientSocket.destroy();
|
|
107
|
+
upstream.destroy();
|
|
108
|
+
};
|
|
109
|
+
upstream.on('error', close);
|
|
110
|
+
clientSocket.on('error', close);
|
|
111
|
+
clientSocket.on('close', close);
|
|
112
|
+
upstream.on('close', close);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
server.listen(listenPort, '0.0.0.0');
|
|
116
|
+
return server;
|
|
117
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "actor-debugger",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Drop-in remote debugger for Apify Node/TS Actors: one Dockerfile CMD line launches your Actor under the Node inspector, reachable over the run's container URL. No local setup, no rebuild of your own code.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"actor-debugger": "bin/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"lib",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Pijukatel/actor-debugger.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/Pijukatel/actor-debugger/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/Pijukatel/actor-debugger#readme",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"apify",
|
|
27
|
+
"actor",
|
|
28
|
+
"debugger",
|
|
29
|
+
"inspector",
|
|
30
|
+
"devtools",
|
|
31
|
+
"cdp",
|
|
32
|
+
"remote-debugging"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"chii": "^1.14.0"
|
|
40
|
+
}
|
|
41
|
+
}
|