@decocms/blocks 7.71.1 → 7.71.3
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/package.json +1 -1
- package/src/sdk/cacheHeaders.test.ts +24 -0
- package/src/sdk/cacheHeaders.ts +49 -0
- package/src/sdk/otel.test.ts +55 -0
- package/src/sdk/otel.ts +37 -6
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import {
|
|
3
3
|
canonicalizeServerFnPayloadForCacheKey,
|
|
4
|
+
payloadHasTrackingParams,
|
|
4
5
|
detectCacheProfile,
|
|
5
6
|
getCacheProfile,
|
|
6
7
|
registerCachePattern,
|
|
@@ -232,3 +233,26 @@ describe("cache configuration can tighten, not loosen", () => {
|
|
|
232
233
|
expect(detectCacheProfile("/tenis")).toBe("static");
|
|
233
234
|
});
|
|
234
235
|
});
|
|
236
|
+
|
|
237
|
+
describe("payloadHasTrackingParams", () => {
|
|
238
|
+
const p = (v: unknown) => JSON.stringify(v);
|
|
239
|
+
|
|
240
|
+
it("finds a tracking param in an embedded absolute URL", () => {
|
|
241
|
+
expect(payloadHasTrackingParams(p({ data: { pageUrl: "https://x.test/p?gclid=1" } }))).toBe(
|
|
242
|
+
true,
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("finds a tracking param in an embedded route path", () => {
|
|
247
|
+
expect(payloadHasTrackingParams(p(["/produtos?utm_source=ads&page=2"]))).toBe(true);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("ignores real params and plain strings", () => {
|
|
251
|
+
expect(payloadHasTrackingParams(p({ pageUrl: "/produtos?sort=price:asc&page=2" }))).toBe(false);
|
|
252
|
+
expect(payloadHasTrackingParams(p({ label: "gclid=1 in a sentence" }))).toBe(false);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("is fail-safe on malformed payloads", () => {
|
|
256
|
+
expect(payloadHasTrackingParams("not json {")).toBe(false);
|
|
257
|
+
});
|
|
258
|
+
});
|
package/src/sdk/cacheHeaders.ts
CHANGED
|
@@ -508,6 +508,55 @@ export function serverFnPagePath(url: URL): string | null {
|
|
|
508
508
|
}
|
|
509
509
|
}
|
|
510
510
|
|
|
511
|
+
/**
|
|
512
|
+
* `true` when a server-fn payload (GET `payload` param or POST body) embeds a
|
|
513
|
+
* route path or absolute URL carrying a tracking param — e.g. a deferred
|
|
514
|
+
* section's `pageUrl` built from `location.href` on an ad landing. Same walk as
|
|
515
|
+
* {@link canonicalizeServerFnPayloadForCacheKey}. Returns `false` on any parse
|
|
516
|
+
* failure (fail-safe: the caller keeps its normal behavior).
|
|
517
|
+
*/
|
|
518
|
+
export function payloadHasTrackingParams(payload: string): boolean {
|
|
519
|
+
try {
|
|
520
|
+
let found = false;
|
|
521
|
+
const check = (s: string): void => {
|
|
522
|
+
let search: string;
|
|
523
|
+
if (/^https?:\/\//i.test(s)) {
|
|
524
|
+
try {
|
|
525
|
+
search = new URL(s).search;
|
|
526
|
+
} catch {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
} else if (s.startsWith("/") && !s.startsWith("//")) {
|
|
530
|
+
const q = s.indexOf("?");
|
|
531
|
+
if (q < 0) return;
|
|
532
|
+
search = s.slice(q);
|
|
533
|
+
} else {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
for (const k of new URLSearchParams(search).keys()) {
|
|
537
|
+
if (isTrackingParam(k)) {
|
|
538
|
+
found = true;
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
const visit = (v: unknown): void => {
|
|
544
|
+
if (found) return;
|
|
545
|
+
if (typeof v === "string") {
|
|
546
|
+
check(v);
|
|
547
|
+
} else if (Array.isArray(v)) {
|
|
548
|
+
for (const x of v) visit(x);
|
|
549
|
+
} else if (v && typeof v === "object") {
|
|
550
|
+
for (const x of Object.values(v)) visit(x);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
visit(JSON.parse(payload));
|
|
554
|
+
return found;
|
|
555
|
+
} catch {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
511
560
|
/**
|
|
512
561
|
* Search params that never change a CMS page's resolved server-fn response and
|
|
513
562
|
* so must not fragment its edge cache key. `skuId`/`idsku` select a product
|
package/src/sdk/otel.test.ts
CHANGED
|
@@ -506,6 +506,61 @@ describe("instrumentWorker — OTLP/HTTP error-log channel wiring", () => {
|
|
|
506
506
|
expect(payload.resourceLogs[0].scopeLogs[0].logRecords[0].severityText).toBe("info");
|
|
507
507
|
});
|
|
508
508
|
|
|
509
|
+
async function shipConsoleError(...args: unknown[]): Promise<string> {
|
|
510
|
+
const { impl, calls } = makeFetchSpy();
|
|
511
|
+
const handler = {
|
|
512
|
+
fetch: vi.fn(async () => {
|
|
513
|
+
console.error(...args);
|
|
514
|
+
return new Response("ok");
|
|
515
|
+
}),
|
|
516
|
+
};
|
|
517
|
+
const wrapped = instrumentWorker(handler, {
|
|
518
|
+
otlpLogsFetchImpl: impl,
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
const env: TestEnv = {
|
|
522
|
+
DECO_OTEL_LOGS_ENDPOINT: "https://ingest.test/v1/logs",
|
|
523
|
+
} as TestEnv & { DECO_OTEL_LOGS_ENDPOINT: string };
|
|
524
|
+
const ctx = fakeCtx();
|
|
525
|
+
await wrapped.fetch(new Request("https://example.test/"), env, ctx);
|
|
526
|
+
await Promise.all(ctx.waited);
|
|
527
|
+
|
|
528
|
+
expect(calls).toHaveLength(1);
|
|
529
|
+
const payload = JSON.parse(calls[0].body) as {
|
|
530
|
+
resourceLogs: Array<{
|
|
531
|
+
scopeLogs: Array<{ logRecords: Array<{ body: { stringValue: string } }> }>;
|
|
532
|
+
}>;
|
|
533
|
+
};
|
|
534
|
+
return payload.resourceLogs[0].scopeLogs[0].logRecords[0].body.stringValue;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
it("console.error(err) ships message and stack, not \"{}\"", async () => {
|
|
538
|
+
const body = await shipConsoleError("[VTEX] PLP error:", new TypeError("boom"));
|
|
539
|
+
expect(body).toContain("[VTEX] PLP error: TypeError: boom");
|
|
540
|
+
expect(body).toMatch(/TypeError: boom\n\s+at /); // stack frames shipped
|
|
541
|
+
expect(body).not.toContain("{}");
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it("keeps enumerable fields of an Error subclass", async () => {
|
|
545
|
+
const err = Object.assign(new Error("circuit open"), { host: "x.vtex.com", status: 503 });
|
|
546
|
+
const body = await shipConsoleError(err);
|
|
547
|
+
expect(body).toContain("Error: circuit open");
|
|
548
|
+
expect(body).toContain('{"host":"x.vtex.com","status":503}');
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it("expands errors nested in objects and arrays", async () => {
|
|
552
|
+
const body = await shipConsoleError({ error: new Error("nested") }, [new RangeError("in array")]);
|
|
553
|
+
expect(body).toContain('"message":"nested"');
|
|
554
|
+
expect(body).toContain('"name":"RangeError","message":"in array"');
|
|
555
|
+
expect(body).not.toContain('{"error":{}}');
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it("formats error-like values that are not instanceof Error", async () => {
|
|
559
|
+
const domLike = { name: "AbortError", message: "aborted", stack: "AbortError: aborted\n at x" };
|
|
560
|
+
const body = await shipConsoleError(domLike);
|
|
561
|
+
expect(body).toBe("AbortError: aborted\n at x");
|
|
562
|
+
});
|
|
563
|
+
|
|
509
564
|
it("otlpLogsEnabled=false disables the channel even when env is set", async () => {
|
|
510
565
|
const { impl, calls } = makeFetchSpy();
|
|
511
566
|
const handler = {
|
package/src/sdk/otel.ts
CHANGED
|
@@ -651,6 +651,42 @@ function configureTracerStack(otlpAdapter: OtlpHttpTracer | null): void {
|
|
|
651
651
|
// Boot — wires the loggers/meters once (per worker isolate)
|
|
652
652
|
// ---------------------------------------------------------------------------
|
|
653
653
|
|
|
654
|
+
/**
|
|
655
|
+
* Error's `name`/`message`/`stack` are non-enumerable, so a plain
|
|
656
|
+
* `JSON.stringify(err)` ships `"{}"`. Duck-typed so DOMException and
|
|
657
|
+
* cross-realm errors are covered too.
|
|
658
|
+
*/
|
|
659
|
+
function isErrorLike(v: unknown): v is Error {
|
|
660
|
+
return v instanceof Error || (
|
|
661
|
+
typeof v === "object" && v !== null &&
|
|
662
|
+
typeof (v as Error).message === "string" &&
|
|
663
|
+
typeof (v as Error).name === "string" &&
|
|
664
|
+
"stack" in v
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/** Expands nested errors (`{ error: err }`, `[err]`) instead of `{}`. */
|
|
669
|
+
const errorReplacer = (_key: string, v: unknown) =>
|
|
670
|
+
isErrorLike(v) ? { ...(v as object), name: v.name, message: v.message, stack: v.stack } : v;
|
|
671
|
+
|
|
672
|
+
/** Formats one `console.*` argument as a log-body fragment. */
|
|
673
|
+
function formatConsoleArg(a: unknown): string {
|
|
674
|
+
if (typeof a === "string") return a;
|
|
675
|
+
try {
|
|
676
|
+
if (isErrorLike(a)) {
|
|
677
|
+
const head = a.stack ?? `${a.name}: ${a.message}`;
|
|
678
|
+
// Keep enumerable own fields (e.g. `status`, `host`) the old
|
|
679
|
+
// JSON.stringify path used to ship.
|
|
680
|
+
const { name: _n, message: _m, stack: _s, ...rest } = a as object as Record<string, unknown>;
|
|
681
|
+
const extra = JSON.stringify(rest, errorReplacer);
|
|
682
|
+
return extra === "{}" ? head : `${head} ${extra}`;
|
|
683
|
+
}
|
|
684
|
+
return JSON.stringify(a, errorReplacer);
|
|
685
|
+
} catch {
|
|
686
|
+
return String(a);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
654
690
|
/**
|
|
655
691
|
* Replaces `console.*` with thin shims that route every call through the
|
|
656
692
|
* framework `logger`, which forwards to the configured adapter (direct-POST
|
|
@@ -684,12 +720,7 @@ function patchConsole(state: BootState): void {
|
|
|
684
720
|
if (busy) return;
|
|
685
721
|
busy = true;
|
|
686
722
|
try {
|
|
687
|
-
const msg = args
|
|
688
|
-
.map((a) => {
|
|
689
|
-
if (typeof a === "string") return a;
|
|
690
|
-
try { return JSON.stringify(a); } catch { return String(a); }
|
|
691
|
-
})
|
|
692
|
-
.join(" ");
|
|
723
|
+
const msg = args.map(formatConsoleArg).join(" ");
|
|
693
724
|
logger[level](msg);
|
|
694
725
|
} finally {
|
|
695
726
|
busy = false;
|