caelum0x 41653aebf1 feat(product-analytics): destination transforms (where, keep/drop, rename, set)
The destination catalogue had no transforms (recount A23): every destination
got every event with every property.

A destination may now carry a declarative transform, applied by the exports
runner before any send for every kind (webhook, S3, Slack, Discord, HubSpot,
ClickHouse, BigQuery): 'where' keeps the events matching every condition (on
a property, the event name or distinct_id; eq, neq, in, not_in, exists,
not_exists, contains, gt, lt), then 'keep' or 'drop', 'rename' and 'set'
reshape the properties. Events are never mutated in place; the cursor moves
past every row read even when nothing is sent. No code runs, so a transform
cannot reach the network or the database. POST and PUT /exports take it (PUT
with null removes it); the console form has the field.

Also: the surveys and vendor tests resolve the upstream SDK from the
wrapper's own install, else the service's pin of the same version (checked),
so they run on a fresh checkout instead of failing or skipping.

Tests: qy/transforms (unit, refusals, end to end through a webhook,
replace/remove). All product analytics suites (a1, a2, a3, qy): 206 passed,
4 skipped (no cached Chromium on this machine), 0 failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:24:34 +03:00

39 lines
1.7 KiB
TypeScript

// The SDK packages (packages/analytics-js, packages/analytics-node) are not in
// any pnpm workspace that installs their dependencies, so on a fresh checkout
// their `posthog-*` imports have nothing to resolve to. This service pins the
// same versions as devDependencies; when a package's own install is missing,
// its upstream SDK imports resolve from here instead. A package install, when
// present, still wins.
import { existsSync } from "node:fs";
import { registerHooks } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const SERVICE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
const SERVICE_PARENT = pathToFileURL(join(SERVICE_ROOT, "package.json")).href;
const UPSTREAM_SDK = /^posthog-(js|node|react-native)(\/|$)/;
registerHooks({
resolve(specifier, context, next) {
if (!UPSTREAM_SDK.test(specifier)) return next(specifier, context);
try {
return next(specifier, context);
} catch (e) {
if ((e as { code?: string }).code !== "ERR_MODULE_NOT_FOUND") throw e;
return next(specifier, { ...context, parentURL: SERVICE_PARENT });
}
},
});
/** The browser SDK's ESM build: the package's own install, else this service's. */
export function sdkEsmPath(pkgDir: string): string {
const own = join(pkgDir, "node_modules/posthog-js/dist/module.mjs");
return existsSync(own) ? own : join(SERVICE_ROOT, "node_modules/posthog-js/dist/module.mjs");
}
/** An upstream SDK package's directory: the package's own install, else this service's. */
export function sdkPackageDir(pkgDir: string, name: string): string {
const own = join(pkgDir, "node_modules", name);
return existsSync(own) ? own : join(SERVICE_ROOT, "node_modules", name);
}