@logbrew/next 0.1.0 → 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/README.md +103 -21
- package/client.cjs +340 -0
- package/client.d.cts +95 -0
- package/client.d.ts +95 -0
- package/client.js +339 -0
- package/examples/client-route-spans.mjs +38 -0
- package/examples/index.mjs +4 -0
- package/examples/package.json +1 -0
- package/index.cjs +209 -59
- package/index.d.cts +45 -2
- package/index.d.ts +45 -2
- package/index.js +209 -59
- package/package.json +32 -2
- package/release-artifacts.cjs +199 -0
- package/release-artifacts.d.cts +35 -0
- package/release-artifacts.d.ts +35 -0
- package/release-artifacts.js +6 -0
package/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
1
2
|
import {
|
|
2
3
|
LogBrewClient,
|
|
3
4
|
RecordingTransport,
|
|
4
5
|
SdkError,
|
|
5
|
-
parseTraceparent
|
|
6
|
-
spanAttributesFromTraceparent
|
|
6
|
+
parseTraceparent
|
|
7
7
|
} from "@logbrew/sdk";
|
|
8
8
|
|
|
9
9
|
const DEFAULT_SDK_NAME = "logbrew-next";
|
|
10
10
|
const DEFAULT_SDK_VERSION = "0.1.0";
|
|
11
|
+
const activeTraceContext = new AsyncLocalStorage();
|
|
11
12
|
|
|
12
13
|
export function createLogBrewNextClient({
|
|
13
14
|
apiKey,
|
|
@@ -34,40 +35,47 @@ export function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
34
35
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
35
36
|
const client = resolveClient(options, request, context);
|
|
36
37
|
const transport = resolveTransport(options, request, context, client);
|
|
37
|
-
const
|
|
38
|
+
const trace = createRouteTraceContext(request, undefined, options);
|
|
39
|
+
const helpers = createRouteHelpers(client, transport, trace);
|
|
38
40
|
const startedAt = nowMs(options);
|
|
39
41
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
return activeTraceContext.run(trace, async () => {
|
|
43
|
+
try {
|
|
44
|
+
const response = await handler(request, context, helpers);
|
|
45
|
+
ensureResponse(response);
|
|
46
|
+
await captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace });
|
|
47
|
+
return response;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
await recordRouteError(options, error, { request, context, client, transport, trace });
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
49
53
|
};
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
export function getActiveLogBrewTrace() {
|
|
57
|
+
return activeTraceContext.getStore();
|
|
58
|
+
}
|
|
59
|
+
|
|
52
60
|
export function createRouteRequestEvent(request, response, {
|
|
53
61
|
now = () => new Date().toISOString(),
|
|
54
62
|
durationMs = 0,
|
|
55
63
|
idFactory = defaultRouteRequestId,
|
|
56
|
-
spanIdFactory = defaultSpanIdFactory
|
|
64
|
+
spanIdFactory = defaultSpanIdFactory,
|
|
65
|
+
trace
|
|
57
66
|
} = {}) {
|
|
58
67
|
const url = safeUrl(request);
|
|
59
68
|
const method = request?.method ?? "GET";
|
|
60
69
|
const statusCode = Number(response?.status ?? 0);
|
|
61
70
|
const id = idFactory(request, response);
|
|
62
|
-
const
|
|
63
|
-
const spanEvent =
|
|
64
|
-
? createTraceparentRouteSpan(
|
|
71
|
+
const routeTrace = trace ?? getActiveLogBrewTrace() ?? createRouteTraceContext(request, response, { spanIdFactory });
|
|
72
|
+
const spanEvent = routeTrace
|
|
73
|
+
? createTraceparentRouteSpan(routeTrace, {
|
|
65
74
|
durationMs,
|
|
66
75
|
id,
|
|
67
76
|
method,
|
|
68
77
|
now,
|
|
69
78
|
pathname: url.pathname,
|
|
70
|
-
spanIdFactory: () => spanIdFactory(request, response),
|
|
71
79
|
statusCode
|
|
72
80
|
})
|
|
73
81
|
: undefined;
|
|
@@ -96,11 +104,13 @@ export function createRouteRequestEvent(request, response, {
|
|
|
96
104
|
export function createRouteErrorEvent(error, request, {
|
|
97
105
|
includeSearchParams = false,
|
|
98
106
|
now = () => new Date().toISOString(),
|
|
99
|
-
idFactory = defaultRouteErrorId
|
|
107
|
+
idFactory = defaultRouteErrorId,
|
|
108
|
+
trace
|
|
100
109
|
} = {}) {
|
|
101
110
|
const url = safeUrl(request);
|
|
102
111
|
const method = request?.method ?? "GET";
|
|
103
112
|
const message = error instanceof Error ? error.message : String(error);
|
|
113
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
104
114
|
return {
|
|
105
115
|
id: idFactory(request),
|
|
106
116
|
timestamp: now(),
|
|
@@ -111,7 +121,38 @@ export function createRouteErrorEvent(error, request, {
|
|
|
111
121
|
metadata: {
|
|
112
122
|
method,
|
|
113
123
|
pathname: url.pathname,
|
|
114
|
-
...(includeSearchParams ? { search: url.search || null } : {})
|
|
124
|
+
...(includeSearchParams ? { search: url.search || null } : {}),
|
|
125
|
+
...traceMetadata(routeTrace)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function createRequestMetricEvent(request, response, {
|
|
132
|
+
now = () => new Date().toISOString(),
|
|
133
|
+
durationMs = 0,
|
|
134
|
+
idFactory = defaultRouteMetricId,
|
|
135
|
+
metricName = "http.server.duration",
|
|
136
|
+
routeTemplate
|
|
137
|
+
} = {}) {
|
|
138
|
+
const method = request?.method ?? "GET";
|
|
139
|
+
const resolvedRouteTemplate = routeTemplateOnly(routeTemplate ?? safeUrl(request).pathname);
|
|
140
|
+
const statusCode = Number(response?.status ?? 0);
|
|
141
|
+
return {
|
|
142
|
+
id: idFactory(request, response, resolvedRouteTemplate),
|
|
143
|
+
timestamp: now(),
|
|
144
|
+
attributes: {
|
|
145
|
+
name: metricName,
|
|
146
|
+
kind: "histogram",
|
|
147
|
+
value: Math.max(0, Number(durationMs)),
|
|
148
|
+
unit: "ms",
|
|
149
|
+
temporality: "delta",
|
|
150
|
+
metadata: {
|
|
151
|
+
framework: "nextjs",
|
|
152
|
+
method,
|
|
153
|
+
routeTemplate: resolvedRouteTemplate,
|
|
154
|
+
statusCode,
|
|
155
|
+
statusCodeClass: statusCodeClass(statusCode)
|
|
115
156
|
}
|
|
116
157
|
}
|
|
117
158
|
};
|
|
@@ -134,10 +175,11 @@ function resolveTransport(options, request, context, client) {
|
|
|
134
175
|
return options.transport ?? RecordingTransport.alwaysAccept();
|
|
135
176
|
}
|
|
136
177
|
|
|
137
|
-
function createRouteHelpers(client, transport) {
|
|
178
|
+
function createRouteHelpers(client, transport, trace) {
|
|
138
179
|
return {
|
|
139
180
|
client,
|
|
140
181
|
logbrew: client,
|
|
182
|
+
trace,
|
|
141
183
|
previewJson: () => client.previewJson(),
|
|
142
184
|
flush: () => client.flush(transport),
|
|
143
185
|
shutdown: () => client.shutdown(transport)
|
|
@@ -162,43 +204,63 @@ async function notifyFailure(options, error, context) {
|
|
|
162
204
|
}
|
|
163
205
|
}
|
|
164
206
|
|
|
165
|
-
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt }) {
|
|
207
|
+
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace }) {
|
|
166
208
|
try {
|
|
209
|
+
const durationMs = (options.captureRequests !== false || options.captureRequestMetrics === true)
|
|
210
|
+
? routeDurationMs(options, startedAt)
|
|
211
|
+
: 0;
|
|
167
212
|
if (options.captureRequests !== false) {
|
|
168
|
-
recordRouteRequest(options, { request, response, context, client,
|
|
213
|
+
recordRouteRequest(options, { request, response, context, client, durationMs, trace });
|
|
214
|
+
}
|
|
215
|
+
if (options.captureRequestMetrics === true) {
|
|
216
|
+
recordRouteMetric(options, { request, response, context, client, durationMs, trace });
|
|
169
217
|
}
|
|
170
218
|
const flushResponse = await client.shutdown(transport);
|
|
171
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
219
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace });
|
|
172
220
|
} catch (error) {
|
|
173
|
-
await notifyFailure(options, error, { request, context, client });
|
|
221
|
+
await notifyFailure(options, error, { request, context, client, trace });
|
|
174
222
|
}
|
|
175
223
|
}
|
|
176
224
|
|
|
177
|
-
function recordRouteRequest(options, { request, response, context, client,
|
|
178
|
-
const durationMs = Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
225
|
+
function recordRouteRequest(options, { request, response, context, client, durationMs, trace }) {
|
|
179
226
|
const event = typeof options.requestEvent === "function"
|
|
180
|
-
? options.requestEvent(request, response, { client, context, durationMs, request, response })
|
|
227
|
+
? options.requestEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
181
228
|
: createRouteRequestEvent(request, response, {
|
|
182
229
|
...options,
|
|
183
230
|
durationMs,
|
|
184
|
-
idFactory: options.requestIdFactory
|
|
231
|
+
idFactory: options.requestIdFactory,
|
|
232
|
+
trace
|
|
185
233
|
});
|
|
186
234
|
captureRouteRequestEvent(client, event);
|
|
187
235
|
}
|
|
188
236
|
|
|
189
|
-
|
|
237
|
+
function recordRouteMetric(options, { request, response, context, client, durationMs, trace }) {
|
|
238
|
+
const routeTemplate = resolveRouteTemplate(options, request, context);
|
|
239
|
+
const event = typeof options.requestMetricEvent === "function"
|
|
240
|
+
? options.requestMetricEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
241
|
+
: createRequestMetricEvent(request, response, {
|
|
242
|
+
...options,
|
|
243
|
+
durationMs,
|
|
244
|
+
idFactory: options.metricIdFactory,
|
|
245
|
+
routeTemplate
|
|
246
|
+
});
|
|
247
|
+
captureRouteMetricEvent(client, event);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function recordRouteError(options, error, { request, context, client, transport, trace }) {
|
|
190
251
|
if (options.captureErrors === false) {
|
|
191
252
|
return;
|
|
192
253
|
}
|
|
254
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
193
255
|
const event = typeof options.errorEvent === "function"
|
|
194
|
-
? options.errorEvent(error, { request, context, client })
|
|
195
|
-
: createRouteErrorEvent(error, request, options);
|
|
256
|
+
? options.errorEvent(error, { request, context, client, trace: routeTrace })
|
|
257
|
+
: createRouteErrorEvent(error, request, { ...options, trace: routeTrace });
|
|
196
258
|
try {
|
|
197
259
|
client.issue(event.id, event.timestamp, event.attributes);
|
|
198
260
|
const flushResponse = await client.shutdown(transport);
|
|
199
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
261
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace: routeTrace });
|
|
200
262
|
} catch (captureError) {
|
|
201
|
-
await notifyFailure(options, captureError, { request, context, client });
|
|
263
|
+
await notifyFailure(options, captureError, { request, context, client, trace: routeTrace });
|
|
202
264
|
}
|
|
203
265
|
}
|
|
204
266
|
|
|
@@ -210,6 +272,10 @@ function captureRouteRequestEvent(client, event) {
|
|
|
210
272
|
client.log(event.id, event.timestamp, event.attributes);
|
|
211
273
|
}
|
|
212
274
|
|
|
275
|
+
function captureRouteMetricEvent(client, event) {
|
|
276
|
+
client.metric(event.id, event.timestamp, event.attributes);
|
|
277
|
+
}
|
|
278
|
+
|
|
213
279
|
function defaultRouteRequestId(request, response) {
|
|
214
280
|
const url = safeUrl(request);
|
|
215
281
|
const slug = `${request?.method ?? "GET"}-${url.pathname}-${response?.status ?? 0}`
|
|
@@ -232,6 +298,15 @@ function defaultRouteErrorId(request) {
|
|
|
232
298
|
return `evt_next_error_${slug || "route"}`;
|
|
233
299
|
}
|
|
234
300
|
|
|
301
|
+
function defaultRouteMetricId(request, response, routeTemplate = safeUrl(request).pathname) {
|
|
302
|
+
const sanitizedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
303
|
+
const slug = `${request?.method ?? "GET"}-${sanitizedRouteTemplate}-${response?.status ?? 0}`
|
|
304
|
+
.toLowerCase()
|
|
305
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
306
|
+
.replace(/^_+|_+$/g, "");
|
|
307
|
+
return `evt_next_metric_${slug || "route"}`;
|
|
308
|
+
}
|
|
309
|
+
|
|
235
310
|
function safeUrl(request) {
|
|
236
311
|
try {
|
|
237
312
|
return new URL(request?.url ?? "http://localhost/");
|
|
@@ -248,44 +323,117 @@ function getTraceparentHeader(request) {
|
|
|
248
323
|
return typeof value === "string" ? value : undefined;
|
|
249
324
|
}
|
|
250
325
|
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
326
|
+
function resolveRouteTemplate(options, request, context) {
|
|
327
|
+
const value = typeof options.routeTemplate === "function"
|
|
328
|
+
? options.routeTemplate(request, context)
|
|
329
|
+
: options.routeTemplate;
|
|
330
|
+
return routeTemplateOnly(value ?? safeUrl(request).pathname);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function routeTemplateOnly(value) {
|
|
334
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
335
|
+
return "/";
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
return new URL(value, "http://localhost").pathname || "/";
|
|
339
|
+
} catch {
|
|
340
|
+
return value.split(/[?#]/)[0] || "/";
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function statusCodeClass(statusCode) {
|
|
345
|
+
if (!Number.isFinite(statusCode) || statusCode <= 0) {
|
|
346
|
+
return "unknown";
|
|
347
|
+
}
|
|
348
|
+
return `${Math.floor(statusCode / 100)}xx`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function createRouteTraceContext(request, response, {
|
|
352
|
+
spanIdFactory = defaultSpanIdFactory
|
|
353
|
+
} = {}) {
|
|
354
|
+
const traceparent = getTraceparentHeader(request);
|
|
260
355
|
if (!traceparent) {
|
|
261
356
|
return undefined;
|
|
262
357
|
}
|
|
263
358
|
|
|
264
359
|
try {
|
|
265
|
-
parseTraceparent(traceparent);
|
|
266
|
-
const spanId = spanIdFactory();
|
|
360
|
+
const context = parseTraceparent(traceparent);
|
|
361
|
+
const spanId = normalizeSpanId(spanIdFactory(request, response));
|
|
362
|
+
if (!spanId) {
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
267
365
|
return {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
durationMs,
|
|
273
|
-
metadata: {
|
|
274
|
-
framework: "nextjs",
|
|
275
|
-
method,
|
|
276
|
-
pathname,
|
|
277
|
-
statusCode
|
|
278
|
-
},
|
|
279
|
-
name: `${method} ${pathname}`,
|
|
280
|
-
spanId,
|
|
281
|
-
status: statusCode >= 500 ? "error" : "ok"
|
|
282
|
-
})
|
|
366
|
+
traceId: context.traceId,
|
|
367
|
+
spanId,
|
|
368
|
+
parentSpanId: context.parentSpanId,
|
|
369
|
+
sampled: context.sampled
|
|
283
370
|
};
|
|
284
371
|
} catch {
|
|
285
372
|
return undefined;
|
|
286
373
|
}
|
|
287
374
|
}
|
|
288
375
|
|
|
376
|
+
function normalizeSpanId(value) {
|
|
377
|
+
if (typeof value !== "string") {
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
const spanId = value.toLowerCase();
|
|
381
|
+
if (!/^[0-9a-f]{16}$/u.test(spanId) || spanId === "0000000000000000") {
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
return spanId;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function traceMetadata(trace) {
|
|
388
|
+
if (!trace) {
|
|
389
|
+
return {};
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
parentSpanId: trace.parentSpanId,
|
|
393
|
+
sampled: trace.sampled,
|
|
394
|
+
spanId: trace.spanId,
|
|
395
|
+
traceId: trace.traceId
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function createTraceparentRouteSpan(traceContext, {
|
|
400
|
+
durationMs,
|
|
401
|
+
id,
|
|
402
|
+
method,
|
|
403
|
+
now,
|
|
404
|
+
pathname,
|
|
405
|
+
statusCode
|
|
406
|
+
}) {
|
|
407
|
+
if (!traceContext) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
id,
|
|
413
|
+
timestamp: now(),
|
|
414
|
+
type: "span",
|
|
415
|
+
attributes: {
|
|
416
|
+
name: `${method} ${pathname}`,
|
|
417
|
+
traceId: traceContext.traceId,
|
|
418
|
+
spanId: traceContext.spanId,
|
|
419
|
+
parentSpanId: traceContext.parentSpanId,
|
|
420
|
+
status: statusCode >= 500 ? "error" : "ok",
|
|
421
|
+
durationMs,
|
|
422
|
+
metadata: {
|
|
423
|
+
framework: "nextjs",
|
|
424
|
+
method,
|
|
425
|
+
pathname,
|
|
426
|
+
sampled: traceContext.sampled,
|
|
427
|
+
statusCode
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function routeDurationMs(options, startedAt) {
|
|
434
|
+
return Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
435
|
+
}
|
|
436
|
+
|
|
289
437
|
function nowMs(options) {
|
|
290
438
|
if (typeof options.nowMs === "function") {
|
|
291
439
|
return options.nowMs();
|
|
@@ -316,7 +464,9 @@ function readEnvServerApiKey() {
|
|
|
316
464
|
|
|
317
465
|
export default {
|
|
318
466
|
createLogBrewNextClient,
|
|
467
|
+
createRequestMetricEvent,
|
|
319
468
|
createRouteErrorEvent,
|
|
320
469
|
createRouteRequestEvent,
|
|
470
|
+
getActiveLogBrewTrace,
|
|
321
471
|
withLogBrewRouteHandler
|
|
322
472
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logbrew/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Next.js App Router helpers for the public LogBrew JavaScript SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|
|
@@ -16,6 +16,28 @@
|
|
|
16
16
|
"default": "./index.cjs"
|
|
17
17
|
},
|
|
18
18
|
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"./client": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./client.d.ts",
|
|
23
|
+
"default": "./client.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./client.d.cts",
|
|
27
|
+
"default": "./client.cjs"
|
|
28
|
+
},
|
|
29
|
+
"default": "./client.js"
|
|
30
|
+
},
|
|
31
|
+
"./release-artifacts": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./release-artifacts.d.ts",
|
|
34
|
+
"default": "./release-artifacts.js"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./release-artifacts.d.cts",
|
|
38
|
+
"default": "./release-artifacts.cjs"
|
|
39
|
+
},
|
|
40
|
+
"default": "./release-artifacts.js"
|
|
19
41
|
}
|
|
20
42
|
},
|
|
21
43
|
"files": [
|
|
@@ -23,6 +45,14 @@
|
|
|
23
45
|
"index.js",
|
|
24
46
|
"index.d.ts",
|
|
25
47
|
"index.d.cts",
|
|
48
|
+
"client.cjs",
|
|
49
|
+
"client.js",
|
|
50
|
+
"client.d.ts",
|
|
51
|
+
"client.d.cts",
|
|
52
|
+
"release-artifacts.cjs",
|
|
53
|
+
"release-artifacts.js",
|
|
54
|
+
"release-artifacts.d.ts",
|
|
55
|
+
"release-artifacts.d.cts",
|
|
26
56
|
"README.md",
|
|
27
57
|
"examples"
|
|
28
58
|
],
|
|
@@ -51,6 +81,6 @@
|
|
|
51
81
|
"react-dom": ">=18"
|
|
52
82
|
},
|
|
53
83
|
"scripts": {
|
|
54
|
-
"test": "node --check index.js && node --check index.cjs && node --check examples/index.mjs && node --check examples/readme-example.mjs && node --check examples/real-user-smoke.mjs"
|
|
84
|
+
"test": "node --check index.js && node --check index.cjs && node --check client.js && node --check client.cjs && node --check release-artifacts.js && node --check release-artifacts.cjs && node --check examples/index.mjs && node --check examples/client-route-spans.mjs && node --check examples/readme-example.mjs && node --check examples/real-user-smoke.mjs && node --test"
|
|
55
85
|
}
|
|
56
86
|
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
const PACKAGE_DIR = path.dirname(require.resolve("./package.json"));
|
|
7
|
+
const DEFAULT_MANIFEST_NAME = "logbrew-release-artifacts.json";
|
|
8
|
+
const DEFAULT_MINIFIED_PATH_PREFIX = "app:///_next/static/chunks";
|
|
9
|
+
|
|
10
|
+
function requiredString(options, name) {
|
|
11
|
+
const value = options?.[name];
|
|
12
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
13
|
+
throw new Error(`LogBrew Next release-artifact helper requires ${name}`);
|
|
14
|
+
}
|
|
15
|
+
return value.trim();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function optionalString(options, name) {
|
|
19
|
+
const value = options?.[name];
|
|
20
|
+
if (value === undefined || value === null) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
24
|
+
throw new Error(`LogBrew Next release-artifact helper option ${name} must be a non-empty string`);
|
|
25
|
+
}
|
|
26
|
+
return value.trim();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function normalizeStringArray(value, name) {
|
|
30
|
+
if (value === undefined || value === null) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string" || item.trim() === "")) {
|
|
34
|
+
throw new Error(`LogBrew Next release-artifact helper option ${name} must be an array of non-empty strings`);
|
|
35
|
+
}
|
|
36
|
+
return value.map((item) => item.trim());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function resolveLogBrewSdkFile(fileName) {
|
|
40
|
+
try {
|
|
41
|
+
const sdkEntry = require.resolve("@logbrew/sdk", { paths: [PACKAGE_DIR, process.cwd()] });
|
|
42
|
+
return path.join(path.dirname(sdkEntry), fileName);
|
|
43
|
+
} catch {
|
|
44
|
+
const sourceTreeFile = path.resolve(PACKAGE_DIR, `../logbrew-js/${fileName}`);
|
|
45
|
+
if (fs.existsSync(sourceTreeFile)) {
|
|
46
|
+
return sourceTreeFile;
|
|
47
|
+
}
|
|
48
|
+
throw new Error("LogBrew Next release-artifact helper requires @logbrew/sdk to be installed");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function resolvePathFromRoot(root, value) {
|
|
53
|
+
return path.isAbsolute(value) ? path.resolve(value) : path.resolve(root, value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function resolveBuildDir(root, distDir, explicitBuildDir) {
|
|
57
|
+
if (explicitBuildDir) {
|
|
58
|
+
return resolvePathFromRoot(root, explicitBuildDir);
|
|
59
|
+
}
|
|
60
|
+
return path.join(resolvePathFromRoot(root, distDir || ".next"), "static", "chunks");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function resolveManifestPath(root, distDir, explicitManifestPath) {
|
|
64
|
+
if (explicitManifestPath) {
|
|
65
|
+
return resolvePathFromRoot(root, explicitManifestPath);
|
|
66
|
+
}
|
|
67
|
+
return path.join(resolvePathFromRoot(root, distDir || ".next"), DEFAULT_MANIFEST_NAME);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function materializeConfig(nextConfig, args) {
|
|
71
|
+
if (typeof nextConfig === "function") {
|
|
72
|
+
const result = nextConfig(...args);
|
|
73
|
+
if (result && typeof result.then === "function") {
|
|
74
|
+
return result.then((resolvedConfig) => resolvedConfig || {});
|
|
75
|
+
}
|
|
76
|
+
return result || {};
|
|
77
|
+
}
|
|
78
|
+
return nextConfig || {};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function patchNextConfig(config, options) {
|
|
82
|
+
const {
|
|
83
|
+
createReleaseArtifactUploadArgs,
|
|
84
|
+
formatReleaseArtifactUploadSummary,
|
|
85
|
+
normalizeReleaseArtifactProjectId,
|
|
86
|
+
normalizeReleaseArtifactUploadOptions,
|
|
87
|
+
runReleaseArtifactCli,
|
|
88
|
+
} = require(resolveLogBrewSdkFile("release-artifacts-build.cjs"));
|
|
89
|
+
const releaseArtifactsCli = resolveLogBrewSdkFile("release-artifacts.js");
|
|
90
|
+
const release = requiredString(options, "release");
|
|
91
|
+
const environment = requiredString(options, "environment");
|
|
92
|
+
const service = requiredString(options, "service");
|
|
93
|
+
const projectId = normalizeReleaseArtifactProjectId(options?.projectId, "Next");
|
|
94
|
+
const minifiedPathPrefix = optionalString(options, "minifiedPathPrefix") || DEFAULT_MINIFIED_PATH_PREFIX;
|
|
95
|
+
const repositoryUrl = optionalString(options, "repositoryUrl");
|
|
96
|
+
const commitSha = optionalString(options, "commitSha");
|
|
97
|
+
const explicitBuildDir = optionalString(options, "buildDir");
|
|
98
|
+
const explicitManifestPath = optionalString(options, "manifestPath");
|
|
99
|
+
const stripSourcesContent = options?.stripSourcesContent !== false;
|
|
100
|
+
const enableSourceMaps = options?.enableSourceMaps !== false;
|
|
101
|
+
const userSourcePrefixes = normalizeStringArray(options?.stripSourcePrefix, "stripSourcePrefix");
|
|
102
|
+
const upload = normalizeReleaseArtifactUploadOptions(options?.upload, { integration: "Next", projectId });
|
|
103
|
+
const root = path.resolve(optionalString(options, "root") || process.cwd());
|
|
104
|
+
const distDir = config.distDir || ".next";
|
|
105
|
+
const compiler = { ...(config.compiler || {}) };
|
|
106
|
+
const existingHook = compiler.runAfterProductionCompile;
|
|
107
|
+
|
|
108
|
+
const patchedConfig = {
|
|
109
|
+
...config,
|
|
110
|
+
compiler,
|
|
111
|
+
};
|
|
112
|
+
if (enableSourceMaps && patchedConfig.productionBrowserSourceMaps === undefined) {
|
|
113
|
+
patchedConfig.productionBrowserSourceMaps = true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (existingHook !== undefined && typeof existingHook !== "function") {
|
|
117
|
+
if (upload) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
"LogBrew Next release-artifact runAfterProductionCompile must be a function when upload is enabled",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return patchedConfig;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
compiler.runAfterProductionCompile = async function logBrewRunAfterProductionCompile(context = {}) {
|
|
126
|
+
if (typeof existingHook === "function") {
|
|
127
|
+
await existingHook.apply(this, arguments);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const hookDistDir = context.distDir || distDir;
|
|
131
|
+
const buildDir = resolveBuildDir(root, hookDistDir, explicitBuildDir);
|
|
132
|
+
const manifestPath = resolveManifestPath(root, hookDistDir, explicitManifestPath);
|
|
133
|
+
const sourcePrefixes = userSourcePrefixes.length > 0 ? userSourcePrefixes : [root];
|
|
134
|
+
const prepareArgs = ["--build-dir", buildDir, "--write"];
|
|
135
|
+
if (stripSourcesContent) {
|
|
136
|
+
prepareArgs.push("--strip-sources-content");
|
|
137
|
+
}
|
|
138
|
+
for (const prefix of sourcePrefixes) {
|
|
139
|
+
prepareArgs.push("--strip-source-prefix", prefix);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
runReleaseArtifactCli(releaseArtifactsCli, "prepare-js", prepareArgs);
|
|
143
|
+
|
|
144
|
+
const manifestArgs = [
|
|
145
|
+
"--build-dir",
|
|
146
|
+
buildDir,
|
|
147
|
+
"--release",
|
|
148
|
+
release,
|
|
149
|
+
"--environment",
|
|
150
|
+
environment,
|
|
151
|
+
"--service",
|
|
152
|
+
service,
|
|
153
|
+
"--minified-path-prefix",
|
|
154
|
+
minifiedPathPrefix,
|
|
155
|
+
];
|
|
156
|
+
if (repositoryUrl) {
|
|
157
|
+
manifestArgs.push("--repository-url", repositoryUrl);
|
|
158
|
+
}
|
|
159
|
+
if (commitSha) {
|
|
160
|
+
manifestArgs.push("--commit-sha", commitSha);
|
|
161
|
+
}
|
|
162
|
+
if (projectId) {
|
|
163
|
+
manifestArgs.push("--project-id", projectId);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const { stdout } = runReleaseArtifactCli(releaseArtifactsCli, "manifest-js", manifestArgs);
|
|
167
|
+
fs.mkdirSync(path.dirname(manifestPath), { recursive: true });
|
|
168
|
+
fs.writeFileSync(manifestPath, stdout, "utf8");
|
|
169
|
+
if (upload) {
|
|
170
|
+
const { report } = runReleaseArtifactCli(
|
|
171
|
+
releaseArtifactsCli,
|
|
172
|
+
"upload-js",
|
|
173
|
+
createReleaseArtifactUploadArgs(buildDir, manifestPath, upload),
|
|
174
|
+
);
|
|
175
|
+
console.info(formatReleaseArtifactUploadSummary(report));
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
return patchedConfig;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function withLogBrewNextReleaseArtifacts(nextConfig = {}, options = {}) {
|
|
183
|
+
if (typeof nextConfig === "function") {
|
|
184
|
+
return function logBrewNextConfigWithReleaseArtifacts() {
|
|
185
|
+
const args = Array.from(arguments);
|
|
186
|
+
const materialized = materializeConfig(nextConfig, args);
|
|
187
|
+
if (materialized && typeof materialized.then === "function") {
|
|
188
|
+
return materialized.then((config) => patchNextConfig(config, options));
|
|
189
|
+
}
|
|
190
|
+
return patchNextConfig(materialized, options);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return patchNextConfig(nextConfig, options);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = {
|
|
197
|
+
withLogBrewNextReleaseArtifacts,
|
|
198
|
+
default: withLogBrewNextReleaseArtifacts,
|
|
199
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type LogBrewNextReleaseArtifactUploadOptions = {
|
|
2
|
+
endpoint: string;
|
|
3
|
+
allowHostedUpload?: boolean;
|
|
4
|
+
tokenEnv?: string;
|
|
5
|
+
dryRun?: boolean;
|
|
6
|
+
maxRetries?: number;
|
|
7
|
+
retryDelay?: number;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type LogBrewNextReleaseArtifactsOptions = {
|
|
12
|
+
release: string;
|
|
13
|
+
environment: string;
|
|
14
|
+
service: string;
|
|
15
|
+
projectId?: string;
|
|
16
|
+
minifiedPathPrefix?: string;
|
|
17
|
+
root?: string;
|
|
18
|
+
buildDir?: string;
|
|
19
|
+
manifestPath?: string;
|
|
20
|
+
repositoryUrl?: string;
|
|
21
|
+
commitSha?: string;
|
|
22
|
+
stripSourcesContent?: boolean;
|
|
23
|
+
stripSourcePrefix?: string[];
|
|
24
|
+
enableSourceMaps?: boolean;
|
|
25
|
+
upload?: LogBrewNextReleaseArtifactUploadOptions;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export declare function withLogBrewNextReleaseArtifacts<TConfig>(
|
|
29
|
+
nextConfig: TConfig,
|
|
30
|
+
options: LogBrewNextReleaseArtifactsOptions
|
|
31
|
+
): TConfig;
|
|
32
|
+
|
|
33
|
+
declare const defaultExport: typeof withLogBrewNextReleaseArtifacts;
|
|
34
|
+
|
|
35
|
+
export default defaultExport;
|