@gscdump/analysis 0.6.3 → 0.7.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.
@@ -0,0 +1,23 @@
1
+ type MigrationPhase = 'd1' | 'dual' | 'r2';
2
+ type MigrationReadFrom = 'd1' | 'r2';
3
+ interface RoutingInputs {
4
+ userPhase: MigrationPhase | string | null | undefined;
5
+ userReadFrom: MigrationReadFrom | string | null | undefined;
6
+ sitePhase?: MigrationPhase | string | null;
7
+ siteReadFrom?: MigrationReadFrom | string | null;
8
+ }
9
+ interface RoutingEnv {
10
+ ANALYTICS_FORCE_D1?: string;
11
+ R2_READS_ENABLED?: string;
12
+ }
13
+ declare function resolvePhase(inputs: Pick<RoutingInputs, 'userPhase' | 'sitePhase'>): MigrationPhase;
14
+ declare function resolveReadFrom(inputs: Pick<RoutingInputs, 'userReadFrom' | 'siteReadFrom'>): MigrationReadFrom;
15
+ declare function forceD1(env: Pick<RoutingEnv, 'ANALYTICS_FORCE_D1'>): boolean;
16
+ declare function r2ReadsEnabled(env: Pick<RoutingEnv, 'R2_READS_ENABLED'>): boolean;
17
+ declare function shouldDualWrite(inputs: RoutingInputs, env: Pick<RoutingEnv, 'ANALYTICS_FORCE_D1'>): boolean;
18
+ type AnalyticsTable = 'pages' | 'keywords' | 'countries' | 'devices' | 'page_keywords';
19
+ declare function isAnalyticsTable(table: string): table is AnalyticsTable;
20
+ declare function shouldWriteToD1(inputs: RoutingInputs, env: Pick<RoutingEnv, 'ANALYTICS_FORCE_D1'>, table: string): boolean;
21
+ declare function shouldReadFromR2(inputs: RoutingInputs, env: Pick<RoutingEnv, 'ANALYTICS_FORCE_D1' | 'R2_READS_ENABLED'>): boolean;
22
+ declare function isValidReadFromForPhase(phase: MigrationPhase, readFrom: MigrationReadFrom): boolean;
23
+ export { AnalyticsTable, MigrationPhase, MigrationReadFrom, RoutingEnv, RoutingInputs, forceD1, isAnalyticsTable, isValidReadFromForPhase, r2ReadsEnabled, resolvePhase, resolveReadFrom, shouldDualWrite, shouldReadFromR2, shouldWriteToD1 };
@@ -0,0 +1,53 @@
1
+ function isPhase(v) {
2
+ return v === "d1" || v === "dual" || v === "r2";
3
+ }
4
+ function isReadFrom(v) {
5
+ return v === "d1" || v === "r2";
6
+ }
7
+ function resolvePhase(inputs) {
8
+ if (isPhase(inputs.sitePhase)) return inputs.sitePhase;
9
+ if (isPhase(inputs.userPhase)) return inputs.userPhase;
10
+ return "d1";
11
+ }
12
+ function resolveReadFrom(inputs) {
13
+ if (isReadFrom(inputs.siteReadFrom)) return inputs.siteReadFrom;
14
+ if (isReadFrom(inputs.userReadFrom)) return inputs.userReadFrom;
15
+ return "d1";
16
+ }
17
+ function forceD1(env) {
18
+ return env.ANALYTICS_FORCE_D1 === "1";
19
+ }
20
+ function r2ReadsEnabled(env) {
21
+ return env.R2_READS_ENABLED === "1";
22
+ }
23
+ function shouldDualWrite(inputs, env) {
24
+ if (forceD1(env)) return false;
25
+ const phase = resolvePhase(inputs);
26
+ return phase === "dual" || phase === "r2";
27
+ }
28
+ const ANALYTICS_TABLES = new Set([
29
+ "pages",
30
+ "keywords",
31
+ "countries",
32
+ "devices",
33
+ "page_keywords"
34
+ ]);
35
+ function isAnalyticsTable(table) {
36
+ return ANALYTICS_TABLES.has(table);
37
+ }
38
+ function shouldWriteToD1(inputs, env, table) {
39
+ if (forceD1(env)) return true;
40
+ if (!isAnalyticsTable(table)) return true;
41
+ return resolvePhase(inputs) !== "r2";
42
+ }
43
+ function shouldReadFromR2(inputs, env) {
44
+ if (forceD1(env)) return false;
45
+ if (!r2ReadsEnabled(env)) return false;
46
+ if (resolvePhase(inputs) === "d1") return false;
47
+ return resolveReadFrom(inputs) === "r2";
48
+ }
49
+ function isValidReadFromForPhase(phase, readFrom) {
50
+ if (readFrom === "r2" && phase === "d1") return false;
51
+ return true;
52
+ }
53
+ export { forceD1, isAnalyticsTable, isValidReadFromForPhase, r2ReadsEnabled, resolvePhase, resolveReadFrom, shouldDualWrite, shouldReadFromR2, shouldWriteToD1 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gscdump/analysis",
3
3
  "type": "module",
4
- "version": "0.6.3",
4
+ "version": "0.7.1",
5
5
  "description": "GSC analyzers — striking-distance, opportunity, movers, decay, brand, clustering, concentration, seasonality. Pure row-based + DuckDB-native.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -48,6 +48,10 @@
48
48
  "./period": {
49
49
  "types": "./dist/period/index.d.mts",
50
50
  "import": "./dist/period/index.mjs"
51
+ },
52
+ "./routing": {
53
+ "types": "./dist/routing/index.d.mts",
54
+ "import": "./dist/routing/index.mjs"
51
55
  }
52
56
  },
53
57
  "main": "./dist/index.mjs",
@@ -68,11 +72,11 @@
68
72
  },
69
73
  "dependencies": {
70
74
  "drizzle-orm": "^0.45.2",
71
- "@gscdump/engine-duckdb-node": "0.6.3",
72
- "@gscdump/engine-sqlite": "0.6.3",
73
- "@gscdump/engine-wasm": "0.6.3",
74
- "gscdump": "0.6.3",
75
- "@gscdump/engine": "0.6.3"
75
+ "@gscdump/engine": "0.7.1",
76
+ "@gscdump/engine-sqlite": "0.7.1",
77
+ "@gscdump/engine-wasm": "0.7.1",
78
+ "gscdump": "0.7.1",
79
+ "@gscdump/engine-duckdb-node": "0.7.1"
76
80
  },
77
81
  "devDependencies": {
78
82
  "vitest": "^4.1.5"