@unhandled/nextjs 0.2.16 → 0.2.17
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/build/client_abort.d.ts +2 -0
- package/build/client_abort.js +40 -0
- package/build/register.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { subscribe } from "node:diagnostics_channel";
|
|
3
|
+
import { ServerResponse } from "node:http";
|
|
4
|
+
const CLIENT_CLOSED_REQUEST = 499;
|
|
5
|
+
const HANDLE_REQUEST = "BaseServer.handleRequest";
|
|
6
|
+
const currentResponse = new AsyncLocalStorage();
|
|
7
|
+
const responses = new WeakMap();
|
|
8
|
+
const enterResponse = (message) => {
|
|
9
|
+
const response = typeof message === "object" && message !== null ? Reflect.get(message, "response") : undefined;
|
|
10
|
+
if (response instanceof ServerResponse) {
|
|
11
|
+
currentResponse.enterWith(response);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
// Next.js opens its own SERVER span instead of the HTTP instrumentation's, so it never learns the client hung up.
|
|
15
|
+
const clientAbortProcessor = {
|
|
16
|
+
onStart(span) {
|
|
17
|
+
const response = span.attributes["next.span_type"] === HANDLE_REQUEST ? currentResponse.getStore() : undefined;
|
|
18
|
+
if (response) {
|
|
19
|
+
responses.set(span, response);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
onEnding(span) {
|
|
23
|
+
const response = responses.get(span);
|
|
24
|
+
if (response?.destroyed && !response.writableFinished) {
|
|
25
|
+
span.setAttributes({ "http.status_code": CLIENT_CLOSED_REQUEST, "error.type": "aborted" });
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: SpanProcessor requires onEnd; attributes are frozen by then
|
|
29
|
+
onEnd() { },
|
|
30
|
+
forceFlush: () => Promise.resolve(),
|
|
31
|
+
shutdown: () => Promise.resolve(),
|
|
32
|
+
};
|
|
33
|
+
export const clientAbort = {
|
|
34
|
+
name: "nextjs-client-abort",
|
|
35
|
+
enabled: true,
|
|
36
|
+
setup: () => {
|
|
37
|
+
subscribe("http.server.request.start", enterResponse);
|
|
38
|
+
return { spanProcessors: [clientAbortProcessor] };
|
|
39
|
+
},
|
|
40
|
+
};
|
package/build/register.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { init } from "@unhandled/node";
|
|
2
2
|
import { frameworkLayer } from "@unhandled/node/framework";
|
|
3
|
+
import { clientAbort } from "./client_abort.js";
|
|
3
4
|
export const registerUnhandled = (options = {}) => {
|
|
4
5
|
if (process.env.NEXT_RUNTIME !== "nodejs") {
|
|
5
6
|
return null;
|
|
6
7
|
}
|
|
7
8
|
return init({
|
|
8
9
|
...options,
|
|
10
|
+
integrations: [...(options.integrations ?? []), clientAbort],
|
|
9
11
|
frameworkLayer: frameworkLayer(new URL("../package.json", import.meta.url), "nextjs"),
|
|
10
12
|
});
|
|
11
13
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unhandled/nextjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.17",
|
|
4
4
|
"description": "Unhandled for Next.js: instrumentation.ts registration, request error reporting, route handler and server action wrappers on @unhandled/node",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|