@logbrew/next 0.1.0 → 0.1.2
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 +190 -27
- 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/examples/readme-example.mjs +1 -1
- package/examples/real-user-smoke.mjs +1 -1
- package/index.cjs +218 -64
- package/index.d.cts +53 -3
- package/index.d.ts +53 -3
- package/index.js +218 -64
- package/package.json +34 -3
- 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
|
-
RecordingTransport,
|
|
4
4
|
SdkError,
|
|
5
|
-
parseTraceparent
|
|
6
|
-
spanAttributesFromTraceparent
|
|
5
|
+
parseTraceparent
|
|
7
6
|
} from "@logbrew/sdk";
|
|
7
|
+
import { createNodeFetchTransport } from "@logbrew/node";
|
|
8
8
|
|
|
9
9
|
const DEFAULT_SDK_NAME = "logbrew-next";
|
|
10
|
-
const DEFAULT_SDK_VERSION = "0.1.
|
|
10
|
+
const DEFAULT_SDK_VERSION = "0.1.2";
|
|
11
|
+
const activeTraceContext = new AsyncLocalStorage();
|
|
11
12
|
|
|
12
13
|
export function createLogBrewNextClient({
|
|
13
14
|
apiKey,
|
|
@@ -31,43 +32,54 @@ export function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
31
32
|
throw new SdkError("configuration_error", "withLogBrewRouteHandler requires a handler function");
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
const defaultTransport = options.transport === undefined
|
|
36
|
+
? createNodeFetchTransport(options)
|
|
37
|
+
: undefined;
|
|
38
|
+
|
|
34
39
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
35
40
|
const client = resolveClient(options, request, context);
|
|
36
|
-
const transport = resolveTransport(options, request, context, client);
|
|
37
|
-
const
|
|
41
|
+
const transport = resolveTransport(options, request, context, client, defaultTransport);
|
|
42
|
+
const trace = createRouteTraceContext(request, undefined, options);
|
|
43
|
+
const helpers = createRouteHelpers(client, transport, trace);
|
|
38
44
|
const startedAt = nowMs(options);
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
return activeTraceContext.run(trace, async () => {
|
|
47
|
+
try {
|
|
48
|
+
const response = await handler(request, context, helpers);
|
|
49
|
+
ensureResponse(response);
|
|
50
|
+
await captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace });
|
|
51
|
+
return response;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
await recordRouteError(options, error, { request, context, client, transport, trace });
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
49
57
|
};
|
|
50
58
|
}
|
|
51
59
|
|
|
60
|
+
export function getActiveLogBrewTrace() {
|
|
61
|
+
return activeTraceContext.getStore();
|
|
62
|
+
}
|
|
63
|
+
|
|
52
64
|
export function createRouteRequestEvent(request, response, {
|
|
53
65
|
now = () => new Date().toISOString(),
|
|
54
66
|
durationMs = 0,
|
|
55
67
|
idFactory = defaultRouteRequestId,
|
|
56
|
-
spanIdFactory = defaultSpanIdFactory
|
|
68
|
+
spanIdFactory = defaultSpanIdFactory,
|
|
69
|
+
trace
|
|
57
70
|
} = {}) {
|
|
58
71
|
const url = safeUrl(request);
|
|
59
72
|
const method = request?.method ?? "GET";
|
|
60
73
|
const statusCode = Number(response?.status ?? 0);
|
|
61
74
|
const id = idFactory(request, response);
|
|
62
|
-
const
|
|
63
|
-
const spanEvent =
|
|
64
|
-
? createTraceparentRouteSpan(
|
|
75
|
+
const routeTrace = trace ?? getActiveLogBrewTrace() ?? createRouteTraceContext(request, response, { spanIdFactory });
|
|
76
|
+
const spanEvent = routeTrace
|
|
77
|
+
? createTraceparentRouteSpan(routeTrace, {
|
|
65
78
|
durationMs,
|
|
66
79
|
id,
|
|
67
80
|
method,
|
|
68
81
|
now,
|
|
69
82
|
pathname: url.pathname,
|
|
70
|
-
spanIdFactory: () => spanIdFactory(request, response),
|
|
71
83
|
statusCode
|
|
72
84
|
})
|
|
73
85
|
: undefined;
|
|
@@ -96,11 +108,13 @@ export function createRouteRequestEvent(request, response, {
|
|
|
96
108
|
export function createRouteErrorEvent(error, request, {
|
|
97
109
|
includeSearchParams = false,
|
|
98
110
|
now = () => new Date().toISOString(),
|
|
99
|
-
idFactory = defaultRouteErrorId
|
|
111
|
+
idFactory = defaultRouteErrorId,
|
|
112
|
+
trace
|
|
100
113
|
} = {}) {
|
|
101
114
|
const url = safeUrl(request);
|
|
102
115
|
const method = request?.method ?? "GET";
|
|
103
116
|
const message = error instanceof Error ? error.message : String(error);
|
|
117
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
104
118
|
return {
|
|
105
119
|
id: idFactory(request),
|
|
106
120
|
timestamp: now(),
|
|
@@ -111,7 +125,38 @@ export function createRouteErrorEvent(error, request, {
|
|
|
111
125
|
metadata: {
|
|
112
126
|
method,
|
|
113
127
|
pathname: url.pathname,
|
|
114
|
-
...(includeSearchParams ? { search: url.search || null } : {})
|
|
128
|
+
...(includeSearchParams ? { search: url.search || null } : {}),
|
|
129
|
+
...traceMetadata(routeTrace)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function createRequestMetricEvent(request, response, {
|
|
136
|
+
now = () => new Date().toISOString(),
|
|
137
|
+
durationMs = 0,
|
|
138
|
+
idFactory = defaultRouteMetricId,
|
|
139
|
+
metricName = "http.server.duration",
|
|
140
|
+
routeTemplate
|
|
141
|
+
} = {}) {
|
|
142
|
+
const method = request?.method ?? "GET";
|
|
143
|
+
const resolvedRouteTemplate = routeTemplateOnly(routeTemplate ?? safeUrl(request).pathname);
|
|
144
|
+
const statusCode = Number(response?.status ?? 0);
|
|
145
|
+
return {
|
|
146
|
+
id: idFactory(request, response, resolvedRouteTemplate),
|
|
147
|
+
timestamp: now(),
|
|
148
|
+
attributes: {
|
|
149
|
+
name: metricName,
|
|
150
|
+
kind: "histogram",
|
|
151
|
+
value: Math.max(0, Number(durationMs)),
|
|
152
|
+
unit: "ms",
|
|
153
|
+
temporality: "delta",
|
|
154
|
+
metadata: {
|
|
155
|
+
framework: "nextjs",
|
|
156
|
+
method,
|
|
157
|
+
routeTemplate: resolvedRouteTemplate,
|
|
158
|
+
statusCode,
|
|
159
|
+
statusCodeClass: statusCodeClass(statusCode)
|
|
115
160
|
}
|
|
116
161
|
}
|
|
117
162
|
};
|
|
@@ -127,17 +172,18 @@ function resolveClient(options, request, context) {
|
|
|
127
172
|
return createLogBrewNextClient(options);
|
|
128
173
|
}
|
|
129
174
|
|
|
130
|
-
function resolveTransport(options, request, context, client) {
|
|
175
|
+
function resolveTransport(options, request, context, client, defaultTransport) {
|
|
131
176
|
if (typeof options.transport === "function") {
|
|
132
177
|
return options.transport({ request, context, client });
|
|
133
178
|
}
|
|
134
|
-
return options.transport ??
|
|
179
|
+
return options.transport ?? defaultTransport;
|
|
135
180
|
}
|
|
136
181
|
|
|
137
|
-
function createRouteHelpers(client, transport) {
|
|
182
|
+
function createRouteHelpers(client, transport, trace) {
|
|
138
183
|
return {
|
|
139
184
|
client,
|
|
140
185
|
logbrew: client,
|
|
186
|
+
trace,
|
|
141
187
|
previewJson: () => client.previewJson(),
|
|
142
188
|
flush: () => client.flush(transport),
|
|
143
189
|
shutdown: () => client.shutdown(transport)
|
|
@@ -162,43 +208,63 @@ async function notifyFailure(options, error, context) {
|
|
|
162
208
|
}
|
|
163
209
|
}
|
|
164
210
|
|
|
165
|
-
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt }) {
|
|
211
|
+
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace }) {
|
|
166
212
|
try {
|
|
213
|
+
const durationMs = (options.captureRequests !== false || options.captureRequestMetrics === true)
|
|
214
|
+
? routeDurationMs(options, startedAt)
|
|
215
|
+
: 0;
|
|
167
216
|
if (options.captureRequests !== false) {
|
|
168
|
-
recordRouteRequest(options, { request, response, context, client,
|
|
217
|
+
recordRouteRequest(options, { request, response, context, client, durationMs, trace });
|
|
218
|
+
}
|
|
219
|
+
if (options.captureRequestMetrics === true) {
|
|
220
|
+
recordRouteMetric(options, { request, response, context, client, durationMs, trace });
|
|
169
221
|
}
|
|
170
222
|
const flushResponse = await client.shutdown(transport);
|
|
171
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
223
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace });
|
|
172
224
|
} catch (error) {
|
|
173
|
-
await notifyFailure(options, error, { request, context, client });
|
|
225
|
+
await notifyFailure(options, error, { request, context, client, trace });
|
|
174
226
|
}
|
|
175
227
|
}
|
|
176
228
|
|
|
177
|
-
function recordRouteRequest(options, { request, response, context, client,
|
|
178
|
-
const durationMs = Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
229
|
+
function recordRouteRequest(options, { request, response, context, client, durationMs, trace }) {
|
|
179
230
|
const event = typeof options.requestEvent === "function"
|
|
180
|
-
? options.requestEvent(request, response, { client, context, durationMs, request, response })
|
|
231
|
+
? options.requestEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
181
232
|
: createRouteRequestEvent(request, response, {
|
|
182
233
|
...options,
|
|
183
234
|
durationMs,
|
|
184
|
-
idFactory: options.requestIdFactory
|
|
235
|
+
idFactory: options.requestIdFactory,
|
|
236
|
+
trace
|
|
185
237
|
});
|
|
186
238
|
captureRouteRequestEvent(client, event);
|
|
187
239
|
}
|
|
188
240
|
|
|
189
|
-
|
|
241
|
+
function recordRouteMetric(options, { request, response, context, client, durationMs, trace }) {
|
|
242
|
+
const routeTemplate = resolveRouteTemplate(options, request, context);
|
|
243
|
+
const event = typeof options.requestMetricEvent === "function"
|
|
244
|
+
? options.requestMetricEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
245
|
+
: createRequestMetricEvent(request, response, {
|
|
246
|
+
...options,
|
|
247
|
+
durationMs,
|
|
248
|
+
idFactory: options.metricIdFactory,
|
|
249
|
+
routeTemplate
|
|
250
|
+
});
|
|
251
|
+
captureRouteMetricEvent(client, event);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function recordRouteError(options, error, { request, context, client, transport, trace }) {
|
|
190
255
|
if (options.captureErrors === false) {
|
|
191
256
|
return;
|
|
192
257
|
}
|
|
258
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
193
259
|
const event = typeof options.errorEvent === "function"
|
|
194
|
-
? options.errorEvent(error, { request, context, client })
|
|
195
|
-
: createRouteErrorEvent(error, request, options);
|
|
260
|
+
? options.errorEvent(error, { request, context, client, trace: routeTrace })
|
|
261
|
+
: createRouteErrorEvent(error, request, { ...options, trace: routeTrace });
|
|
196
262
|
try {
|
|
197
263
|
client.issue(event.id, event.timestamp, event.attributes);
|
|
198
264
|
const flushResponse = await client.shutdown(transport);
|
|
199
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
265
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace: routeTrace });
|
|
200
266
|
} catch (captureError) {
|
|
201
|
-
await notifyFailure(options, captureError, { request, context, client });
|
|
267
|
+
await notifyFailure(options, captureError, { request, context, client, trace: routeTrace });
|
|
202
268
|
}
|
|
203
269
|
}
|
|
204
270
|
|
|
@@ -210,6 +276,10 @@ function captureRouteRequestEvent(client, event) {
|
|
|
210
276
|
client.log(event.id, event.timestamp, event.attributes);
|
|
211
277
|
}
|
|
212
278
|
|
|
279
|
+
function captureRouteMetricEvent(client, event) {
|
|
280
|
+
client.metric(event.id, event.timestamp, event.attributes);
|
|
281
|
+
}
|
|
282
|
+
|
|
213
283
|
function defaultRouteRequestId(request, response) {
|
|
214
284
|
const url = safeUrl(request);
|
|
215
285
|
const slug = `${request?.method ?? "GET"}-${url.pathname}-${response?.status ?? 0}`
|
|
@@ -232,6 +302,15 @@ function defaultRouteErrorId(request) {
|
|
|
232
302
|
return `evt_next_error_${slug || "route"}`;
|
|
233
303
|
}
|
|
234
304
|
|
|
305
|
+
function defaultRouteMetricId(request, response, routeTemplate = safeUrl(request).pathname) {
|
|
306
|
+
const sanitizedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
307
|
+
const slug = `${request?.method ?? "GET"}-${sanitizedRouteTemplate}-${response?.status ?? 0}`
|
|
308
|
+
.toLowerCase()
|
|
309
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
310
|
+
.replace(/^_+|_+$/g, "");
|
|
311
|
+
return `evt_next_metric_${slug || "route"}`;
|
|
312
|
+
}
|
|
313
|
+
|
|
235
314
|
function safeUrl(request) {
|
|
236
315
|
try {
|
|
237
316
|
return new URL(request?.url ?? "http://localhost/");
|
|
@@ -248,44 +327,117 @@ function getTraceparentHeader(request) {
|
|
|
248
327
|
return typeof value === "string" ? value : undefined;
|
|
249
328
|
}
|
|
250
329
|
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
330
|
+
function resolveRouteTemplate(options, request, context) {
|
|
331
|
+
const value = typeof options.routeTemplate === "function"
|
|
332
|
+
? options.routeTemplate(request, context)
|
|
333
|
+
: options.routeTemplate;
|
|
334
|
+
return routeTemplateOnly(value ?? safeUrl(request).pathname);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function routeTemplateOnly(value) {
|
|
338
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
339
|
+
return "/";
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
return new URL(value, "http://localhost").pathname || "/";
|
|
343
|
+
} catch {
|
|
344
|
+
return value.split(/[?#]/)[0] || "/";
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function statusCodeClass(statusCode) {
|
|
349
|
+
if (!Number.isFinite(statusCode) || statusCode <= 0) {
|
|
350
|
+
return "unknown";
|
|
351
|
+
}
|
|
352
|
+
return `${Math.floor(statusCode / 100)}xx`;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function createRouteTraceContext(request, response, {
|
|
356
|
+
spanIdFactory = defaultSpanIdFactory
|
|
357
|
+
} = {}) {
|
|
358
|
+
const traceparent = getTraceparentHeader(request);
|
|
260
359
|
if (!traceparent) {
|
|
261
360
|
return undefined;
|
|
262
361
|
}
|
|
263
362
|
|
|
264
363
|
try {
|
|
265
|
-
parseTraceparent(traceparent);
|
|
266
|
-
const spanId = spanIdFactory();
|
|
364
|
+
const context = parseTraceparent(traceparent);
|
|
365
|
+
const spanId = normalizeSpanId(spanIdFactory(request, response));
|
|
366
|
+
if (!spanId) {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
267
369
|
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
|
-
})
|
|
370
|
+
traceId: context.traceId,
|
|
371
|
+
spanId,
|
|
372
|
+
parentSpanId: context.parentSpanId,
|
|
373
|
+
sampled: context.sampled
|
|
283
374
|
};
|
|
284
375
|
} catch {
|
|
285
376
|
return undefined;
|
|
286
377
|
}
|
|
287
378
|
}
|
|
288
379
|
|
|
380
|
+
function normalizeSpanId(value) {
|
|
381
|
+
if (typeof value !== "string") {
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
const spanId = value.toLowerCase();
|
|
385
|
+
if (!/^[0-9a-f]{16}$/u.test(spanId) || spanId === "0000000000000000") {
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
return spanId;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function traceMetadata(trace) {
|
|
392
|
+
if (!trace) {
|
|
393
|
+
return {};
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
parentSpanId: trace.parentSpanId,
|
|
397
|
+
sampled: trace.sampled,
|
|
398
|
+
spanId: trace.spanId,
|
|
399
|
+
traceId: trace.traceId
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function createTraceparentRouteSpan(traceContext, {
|
|
404
|
+
durationMs,
|
|
405
|
+
id,
|
|
406
|
+
method,
|
|
407
|
+
now,
|
|
408
|
+
pathname,
|
|
409
|
+
statusCode
|
|
410
|
+
}) {
|
|
411
|
+
if (!traceContext) {
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
id,
|
|
417
|
+
timestamp: now(),
|
|
418
|
+
type: "span",
|
|
419
|
+
attributes: {
|
|
420
|
+
name: `${method} ${pathname}`,
|
|
421
|
+
traceId: traceContext.traceId,
|
|
422
|
+
spanId: traceContext.spanId,
|
|
423
|
+
parentSpanId: traceContext.parentSpanId,
|
|
424
|
+
status: statusCode >= 500 ? "error" : "ok",
|
|
425
|
+
durationMs,
|
|
426
|
+
metadata: {
|
|
427
|
+
framework: "nextjs",
|
|
428
|
+
method,
|
|
429
|
+
pathname,
|
|
430
|
+
sampled: traceContext.sampled,
|
|
431
|
+
statusCode
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function routeDurationMs(options, startedAt) {
|
|
438
|
+
return Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
439
|
+
}
|
|
440
|
+
|
|
289
441
|
function nowMs(options) {
|
|
290
442
|
if (typeof options.nowMs === "function") {
|
|
291
443
|
return options.nowMs();
|
|
@@ -316,7 +468,9 @@ function readEnvServerApiKey() {
|
|
|
316
468
|
|
|
317
469
|
export default {
|
|
318
470
|
createLogBrewNextClient,
|
|
471
|
+
createRequestMetricEvent,
|
|
319
472
|
createRouteErrorEvent,
|
|
320
473
|
createRouteRequestEvent,
|
|
474
|
+
getActiveLogBrewTrace,
|
|
321
475
|
withLogBrewRouteHandler
|
|
322
476
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logbrew/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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
|
],
|
|
@@ -45,12 +75,13 @@
|
|
|
45
75
|
"url": "git+https://github.com/LogBrewCo/sdk.git"
|
|
46
76
|
},
|
|
47
77
|
"peerDependencies": {
|
|
48
|
-
"@logbrew/
|
|
78
|
+
"@logbrew/node": "^0.1.2",
|
|
79
|
+
"@logbrew/sdk": "^0.1.3",
|
|
49
80
|
"next": ">=15",
|
|
50
81
|
"react": ">=18",
|
|
51
82
|
"react-dom": ">=18"
|
|
52
83
|
},
|
|
53
84
|
"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"
|
|
85
|
+
"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
86
|
}
|
|
56
87
|
}
|
|
@@ -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
|
+
};
|