@steadphast/connector-nextjs 0.5.3 → 0.5.4

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.
Files changed (2) hide show
  1. package/README.md +201 -18
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -55,6 +55,51 @@ The package has no runtime dependencies; install no other Steadphast package. On
55
55
  earlier, `npm install` stops with `ERESOLVE` and `pnpm add` warns about an unmet peer: upgrade
56
56
  Next.js rather than forcing the install.
57
57
 
58
+ ## Let a coding agent do the setup
59
+
60
+ If a coding agent (Claude Code, Codex, Cursor, or similar) has access to your application's
61
+ repository, paste the prompt for your deployment mode into it. The prompt sends the agent to this
62
+ README, has it install the package, write the entry file, propose the routes to exclude from
63
+ capture, and run the build, and it stops before anything that needs your account: it never creates,
64
+ asks for, or writes an API key, and it does not deploy. Review the routes it excluded before you
65
+ deploy. In the self-hosted prompt, replace `https://www.example.com` and `www.example.com` with
66
+ your public origin and host first.
67
+
68
+ ### Prompt: Next.js on Vercel
69
+
70
+ ```text
71
+ Integrate the Steadphast Next.js connector into this application. The site is deployed on Vercel.
72
+
73
+ The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
74
+
75
+ Steps:
76
+ 1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts) and Next.js 16 (entry file proxy.ts). If the app is on an older version, stop and tell me; do not upgrade Next.js.
77
+ 2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
78
+ 3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "vercel", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
79
+ 4. Run the production build and fix any errors. Do not start a dev server.
80
+ 5. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js on Vercel", and set STEADPHAST_API_KEY in the Vercel project's environment myself.
81
+
82
+ Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps (set the variable, redeploy), and how to verify: request the production domain with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
83
+ ```
84
+
85
+ ### Prompt: Next.js self-hosted
86
+
87
+ ```text
88
+ Integrate the Steadphast Next.js connector into this application in self-hosted mode. The site runs with next start on our own server behind nginx, and visitors reach it at https://www.example.com.
89
+
90
+ The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
91
+
92
+ Steps:
93
+ 1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts, which must also export runtime: "nodejs" in its config) and Next.js 16 (entry file proxy.ts, which takes no runtime option). If the app is on an older version, stop and tell me; do not upgrade Next.js.
94
+ 2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
95
+ 3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "self-hosted", publicOrigin: "https://www.example.com", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
96
+ 4. Run the production build and fix any errors. Do not start a dev server. On Next.js 15 with runtime: "nodejs", the build output shows no Middleware line and .next/server/middleware-manifest.json stays empty; that is expected. The middleware is registered in .next/server/functions-config-manifest.json instead. Keep runtime: "nodejs".
97
+ 5. Copy the reference nginx configuration from the README's Next.js self-hosted section into deploy/nginx/nginx.conf and change only the values its table lists: the public host and port (www.example.com on 443, in every occurrence, keeping the [.] spelling inside the map regular expression), the certificate paths, and the proxy_pass listener (127.0.0.1:39515). Keep every other directive exactly as written; it is what sets X-Steadphast-Client-Ip from $remote_addr and X-Steadphast-Request-Id from $request_id on every proxied request and answers 421 to any other host. Do not run nginx, systemctl, certbot, or sudo, and do not connect to any server; I will apply this on the host.
98
+ 6. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js self-hosted", and set STEADPHAST_API_KEY in the environment of the next start process myself.
99
+
100
+ Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps on the host (install the nginx config, the key, restart), and how to verify: request the site with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
101
+ ```
102
+
58
103
  ## Quick start: Next.js on Vercel
59
104
 
60
105
  ### Next.js 15: `middleware.ts`
@@ -100,11 +145,19 @@ export const config = {
100
145
  ### Set the API key
101
146
 
102
147
  Add `STEADPHAST_API_KEY` to every Vercel environment where capture should be enabled. It is a
103
- server-only secret: never give it a `NEXT_PUBLIC_*` name. When no key is set, the connector logs a
104
- warning once and ships nothing; the visitor response is unaffected.
105
-
106
- Deploy, make a request to a matched path, and open **Explore** in the Steadphast dashboard. The
107
- event normally appears within about 60 seconds.
148
+ server-only secret: never give it a `NEXT_PUBLIC_*` name. When no key is set, the connector ships
149
+ nothing and the visitor response is unaffected; it logs one warning per running instance, on the
150
+ first matched request that is not a skipped static asset, that begins
151
+ `Steadphast connector skipped event`.
152
+ Look for it in the deployment's runtime logs. Adding the variable takes effect on the next
153
+ deployment, so redeploy after you set it.
154
+
155
+ Deploy, make a request to a matched path on your production domain (deployment-specific
156
+ `*.vercel.app` URLs are protected by Vercel Deployment Protection by default), and open **Explore**
157
+ in the Steadphast dashboard. The event normally appears within about 60 seconds. Explore opens on AI
158
+ traffic only, so a request from your own browser is hidden until you switch the traffic filter to
159
+ all traffic; a request sent with an AI crawler's user agent, such as `curl -A GPTBot`, appears in the
160
+ default view.
108
161
 
109
162
  ### Keep your own Middleware or Proxy logic
110
163
 
@@ -136,9 +189,9 @@ Self-hosted mode is for a Next.js application started with `next start` on a ser
136
189
  machine, or container you operate, with nginx terminating client TLS directly in front of it. It
137
190
  is the same package configured with `kind: "self-hosted"`. Use it in either entry file; for
138
191
  Next.js 15 `middleware.ts` also add `runtime: "nodejs"` to the exported `config` (Next.js 16
139
- `proxy.ts` is already Node-only and does not accept the `runtime` option). The full setup guide,
140
- including the reference nginx configuration, is in the dashboard at
141
- <https://app.steadphast.ai/docs/integrations/nextjs-self-hosted>.
192
+ `proxy.ts` is already Node-only and does not accept the `runtime` option). The full setup guide is in the
193
+ dashboard at <https://app.steadphast.ai/docs/integrations/nextjs-self-hosted>; the reference nginx
194
+ configuration it uses is reproduced [below](#reference-nginx-configuration).
142
195
 
143
196
  ```ts
144
197
  import { withDiscover } from "@steadphast/connector-nextjs";
@@ -174,10 +227,9 @@ is reachable without nginx, or a proxy that passes client headers through, lets
174
227
  the IP recorded for its requests.
175
228
 
176
229
  The supported topology is exactly one nginx directly in front of the application, and the
177
- supported configuration is the reference nginx configuration in the
178
- [dashboard guide](https://app.steadphast.ai/docs/integrations/nextjs-self-hosted). Start from that
179
- file and change only the values it lists (public host and port, certificate paths, and the loopback
180
- `proxy_pass` address). It:
230
+ supported configuration is the [reference nginx configuration](#reference-nginx-configuration)
231
+ below. Start from that file and change only the values it lists (public host and port, certificate
232
+ paths, and the loopback `proxy_pass` address). It:
181
233
 
182
234
  - terminates client TLS directly on TLS 1.2 or 1.3 and refuses a handshake without the configured
183
235
  server name;
@@ -195,6 +247,131 @@ front of nginx, a public direct Next.js port, and pass-through internal headers
195
247
  the connector cannot detect them, and a CDN in front makes the recorded client IP that CDN's
196
248
  address. No TLS fingerprint is available in this mode.
197
249
 
250
+ ### Reference nginx configuration
251
+
252
+ This is the complete `nginx.conf` Steadphast tests with, reproduced exactly. It replaces the
253
+ distribution's `nginx.conf` and everything it includes from `conf.d` or `sites-enabled` rather than
254
+ being added beside them. Install it as `nginx.conf` in a directory of its own (for example
255
+ `/etc/steadphast/nginx/`) so its relative paths resolve there, check it with
256
+ `nginx -t -p /etc/steadphast/nginx/ -c nginx.conf`, and start nginx as root with
257
+ `nginx -e stderr -p /etc/steadphast/nginx/ -c nginx.conf -g "daemon off;"`. It needs nginx 1.25.1
258
+ or later.
259
+
260
+ ```nginx
261
+ # Steadphast Next.js connector: reference nginx configuration for self-hosted mode.
262
+ #
263
+ # nginx is the only process that terminates client TLS. It accepts exactly one public host,
264
+ # sets the two internal connector headers from facts it established itself, and proxies to a
265
+ # Next.js `next start` listener bound to loopback. The public scheme, host, and port served
266
+ # here must equal the connector's `publicOrigin` exactly. The placeholder host and port used
267
+ # below are replaced with your own; see the values table in the setup guide.
268
+ #
269
+ # This is a complete nginx.conf. Install it as `nginx.conf` inside an explicit prefix so the
270
+ # relative TLS and temp paths resolve there, then run
271
+ # `nginx -e stderr -p <prefix>/ -c nginx.conf -g "daemon off;"`.
272
+ #
273
+ # Trust boundary (do not weaken):
274
+ # * x-steadphast-client-ip <- $remote_addr, the direct TCP peer. Nothing may sit in front
275
+ # of nginx: no CDN and no load balancer.
276
+ # * x-steadphast-request-id <- $request_id, the request identifier nginx generates itself.
277
+ # * Host / X-Forwarded-Host / X-Forwarded-Proto <- the routing and transport facts nginx checked.
278
+ # * X-Forwarded-For <- the client's value plus $remote_addr, kept only as untrusted
279
+ # evidence.
280
+ # * Anything else the client sends (X-Real-IP, x-vercel-*, a public x-request-id, ...) is
281
+ # passed through as ordinary request data; the connector never uses it as the client IP or
282
+ # request ID in self-hosted mode.
283
+
284
+ worker_processes 1;
285
+ pid nginx.pid;
286
+ error_log stderr warn;
287
+
288
+ events {
289
+ worker_connections 1024;
290
+ }
291
+
292
+ http {
293
+ access_log off;
294
+ server_tokens off;
295
+ client_body_temp_path client_body_temp;
296
+ proxy_temp_path proxy_temp;
297
+ fastcgi_temp_path fastcgi_temp;
298
+ uwsgi_temp_path uwsgi_temp;
299
+ scgi_temp_path scgi_temp;
300
+
301
+ # `$request` retains the raw HTTP/1 request-target. Accept origin-form only with exactly one
302
+ # leading slash; an absolute-form target must spell this listener's complete HTTPS authority.
303
+ map $request $steadphast_request_target_ok {
304
+ default 0;
305
+ "~^[^ ]+ /(?:[^/ ][^ ]*)? HTTP/[0-9.]+$" 1;
306
+ "~^[^ ]+ https://connector[.]steadphast[.]test:39516(?:/[^ ]*|[?][^ ]*)? HTTP/[0-9.]+$" 1;
307
+ }
308
+
309
+ # Handshake policy belongs to the shared/default context: nginx selects a virtual server only
310
+ # after the ClientHello, so the default and named handshakes must inherit the same minimum.
311
+ ssl_protocols TLSv1.2 TLSv1.3;
312
+
313
+ # Catch-all: a handshake without the configured SNI is refused outright.
314
+ server {
315
+ listen 39516 ssl default_server;
316
+ server_name _;
317
+ ssl_reject_handshake on;
318
+ return 421;
319
+ }
320
+
321
+ server {
322
+ listen 39516 ssl;
323
+ http2 on;
324
+ server_name connector.steadphast.test;
325
+
326
+ ssl_certificate steadphast/tls.crt;
327
+ ssl_certificate_key steadphast/tls.key;
328
+ ssl_prefer_server_ciphers off;
329
+
330
+ # Require the raw request-target and complete wire authority, then independently check
331
+ # nginx's normalized host. `$host` drops an absolute-form target's port.
332
+ if ($steadphast_request_target_ok = 0) {
333
+ return 421;
334
+ }
335
+ if ($http_host != "connector.steadphast.test:39516") {
336
+ return 421;
337
+ }
338
+ if ($host != "connector.steadphast.test") {
339
+ return 421;
340
+ }
341
+
342
+ location / {
343
+ # The private Next.js listener: `next start -H 127.0.0.1 -p 39515`.
344
+ proxy_pass http://127.0.0.1:39515;
345
+ proxy_http_version 1.1;
346
+ proxy_set_header Connection "";
347
+
348
+ # Validated external facts replace whatever the client sent.
349
+ proxy_set_header Host "connector.steadphast.test:39516";
350
+ proxy_set_header X-Forwarded-Host "connector.steadphast.test:39516";
351
+ proxy_set_header X-Forwarded-Proto "https";
352
+
353
+ # Internal connector headers: always overwritten, never preserved. `proxy_set_header`
354
+ # replaces every client-supplied occurrence, so duplicates cannot leak through.
355
+ proxy_set_header X-Steadphast-Client-Ip $remote_addr;
356
+ proxy_set_header X-Steadphast-Request-Id $request_id;
357
+
358
+ # Untrusted evidence only: the client chain with our direct peer appended.
359
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
360
+ }
361
+ }
362
+ }
363
+ ```
364
+
365
+ The reference is written for a placeholder public host and port and a Next.js listener on loopback. Adapt it by changing only these values and keep every directive. In the `map` block the host is a regular expression, so each dot is written `[.]` to match a literal dot; keep that spelling when you substitute your own host, for example `www[.]example[.]com`:
366
+
367
+ | Reference value | Replace with |
368
+ |---|---|
369
+ | `connector.steadphast.test:39516` (the absolute-form request-target `map`, the `$http_host` check, and both the `proxy_set_header Host` and `X-Forwarded-Host` lines) | Your public authority, identical in every occurrence and identical to `publicOrigin`. On the default port `443` clients send a bare `Host`, so use `www.example.com` with no port in all of these and set `publicOrigin` to `https://www.example.com`; on any other port keep the `host:port` form in all of them and in `publicOrigin` |
370
+ | `connector.steadphast.test` (`server_name` and the `$host` check) | Your public host |
371
+ | `listen 39516 ssl` in both `server` blocks | Your public port |
372
+ | `ssl_certificate` and `ssl_certificate_key` | Your certificate and key paths |
373
+ | `proxy_pass http://127.0.0.1:39515` | The loopback address and port of your `next start` process |
374
+
198
375
  ## Configuration reference
199
376
 
200
377
  ```ts
@@ -322,10 +499,13 @@ handler.
322
499
 
323
500
  ## Troubleshooting
324
501
 
325
- - **No events appear:** confirm the API key was issued for the mode you set in `kind` (**Next.js on
326
- Vercel** or **Next.js self-hosted**), that `STEADPHAST_API_KEY` is set in the deployed
327
- environment, that the entry file default-exports the wrapper, that `recordIdPrefix` is within its
328
- 128-byte limit, and that the matcher or static-asset filter did not skip the path.
502
+ - **No events appear:** switch the Explore traffic filter to all traffic first, because the default
503
+ view shows AI traffic only. Then confirm the API key was issued for the mode you set in `kind`
504
+ (**Next.js on Vercel** or **Next.js self-hosted**), that `STEADPHAST_API_KEY` is set in the
505
+ deployed environment and the process or deployment was restarted after you set it, that the entry
506
+ file default-exports the wrapper, that `recordIdPrefix` is within its 128-byte limit, and that the
507
+ matcher or static-asset filter did not skip the path. A `Steadphast connector skipped event` line
508
+ in the runtime logs means no key reached the process.
329
509
  - **The deployment fails at startup:** `apiUrl` is not HTTPS or its path is neither `/` nor
330
510
  `/v1/connectors/nextjs/events`, `publicOrigin` is not a bare HTTPS origin, or `apiKeyEnvVar`
331
511
  names a `NEXT_PUBLIC_*` variable. The thrown `DiscoverConfigError` names the setting.
@@ -333,8 +513,9 @@ handler.
333
513
  are captured, so a plain `http://localhost` server never ships. Front the server with an HTTPS
334
514
  proxy or send `X-Forwarded-Proto: https`, which Next.js honors when building the request URL.
335
515
  - **Self-hosted events have no client IP:** nginx is not overwriting `X-Steadphast-Client-Ip`, or
336
- the request bypassed nginx. Compare your configuration with the reference nginx configuration in
337
- the dashboard guide and confirm the Next.js port is not reachable from outside.
516
+ the request bypassed nginx. Compare your configuration with the
517
+ [reference nginx configuration](#reference-nginx-configuration) and confirm the Next.js port is
518
+ not reachable from outside.
338
519
  - **Self-hosted requests answer `421`:** nginx received a `Host` or absolute-form request target
339
520
  that does not match its configured public authority. Make the `server_name`, the `Host` checks,
340
521
  the `proxy_set_header Host` lines, and `publicOrigin` agree with each other.
@@ -347,6 +528,8 @@ handler.
347
528
 
348
529
  ## Upgrading
349
530
 
531
+ - **0.5.4** Documentation only: this README now carries the reference nginx configuration and the
532
+ coding-agent setup prompts; no runtime change.
350
533
  - **0.5.3** Documentation and metadata only; no runtime change.
351
534
  - **0.5.2** changes documentation and package metadata only; the shipped code is identical to
352
535
  0.5.1, and no upgrade action is needed.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@steadphast/connector-nextjs",
3
3
  "description": "Next.js Middleware and Proxy connector for Steadphast: captures request metadata before it reaches your app and sends it to Steadphast for AI and bot traffic classification, without touching the response.",
4
- "version": "0.5.3",
4
+ "version": "0.5.4",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Steadphast",
7
7
  "homepage": "https://steadphast.ai",