@splithub/node 0.5.0 → 0.6.0

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/dist/index.d.ts CHANGED
@@ -56,7 +56,7 @@ interface VariantCountsSql {
56
56
  }): unknown[];
57
57
  }
58
58
  /**
59
- * The attribution query for an events table with the columns of the SplitHub
59
+ * The attribution query for an events table with the columns of the sh2
60
60
  * stores (id, type, experiment, variant, goal, visitor_id, ts, props). Returns
61
61
  * one row per variant: variant, exposures, conversions, revenue_sum,
62
62
  * revenue_sum_sq. Derived tables only, no CTEs or window functions, so the same
@@ -93,7 +93,7 @@ interface StoredEvent {
93
93
  country?: string | null;
94
94
  device?: string | null;
95
95
  browser?: string | null;
96
- /** Extra dimensions (SplitHub Cloud, `d1Store({ meta: 'extended' })`): OS, language, geo, referrer, UTM. */
96
+ /** Extra dimensions (sh2 Cloud, `d1Store({ meta: 'extended' })`): OS, language, geo, referrer, UTM. */
97
97
  os?: string | null;
98
98
  language?: string | null;
99
99
  city?: string | null;
@@ -143,7 +143,7 @@ interface Store {
143
143
  }): Promise<StoredEvent[]>;
144
144
  close?(): Promise<void> | void;
145
145
  }
146
- /** Raw counts of one analytics property; aggregated reports are SplitHub Cloud's. */
146
+ /** Raw counts of one analytics property; aggregated reports are sh2 Cloud's. */
147
147
  interface PropertyCounts {
148
148
  pageviews: number;
149
149
  visitors: number;
@@ -176,6 +176,11 @@ interface HandlerOptions {
176
176
  cors?: string[] | '*';
177
177
  }
178
178
  type FetchHandler = (request: Request) => Promise<Response>;
179
+ /**
180
+ * Largest ingest body accepted. The POST is public, so the cap applies before anything is
181
+ * buffered; a full batch (500 events) is far smaller, and sh2 Cloud takes 256 KiB.
182
+ */
183
+ declare const MAX_BODY_BYTES: number;
179
184
  /**
180
185
  * One handler for ingest and results, mountable at any path:
181
186
  *
@@ -186,7 +191,7 @@ type FetchHandler = (request: Request) => Promise<Response>;
186
191
  * GET {base}/properties/:key → one property's raw counts
187
192
  * GET {base}/properties/:key/events → its latest raw pageviews and events (`?before=&limit=`)
188
193
  *
189
- * Reads are raw counts; verdicts and recommendations are SplitHub Cloud's.
194
+ * Reads are raw counts; verdicts and recommendations are sh2 Cloud's.
190
195
  * GET {base}/health → { ok: true }
191
196
  *
192
197
  * Works in Next.js route handlers, Remix, Hono, Bun, Deno and Node (via `toNodeListener`).
@@ -202,7 +207,7 @@ declare function createSplitHubHandler(options: HandlerOptions): FetchHandler;
202
207
  */
203
208
  declare function toNodeListener(handler: FetchHandler): (req: IncomingMessage, res: ServerResponse) => void;
204
209
 
205
- /** Self-hosted reads give counts; verdicts and recommendations are computed by SplitHub Cloud. */
210
+ /** Self-hosted reads give counts; verdicts and recommendations are computed by sh2 Cloud. */
206
211
  declare const ANALYSIS_UNAVAILABLE: {
207
212
  available: false;
208
213
  message: string;
@@ -276,7 +281,7 @@ interface PropertySql {
276
281
  sql: string;
277
282
  bind(values: Partial<Record<Param, unknown>>): unknown[];
278
283
  }
279
- /** The queries behind `propertyCounts` / `propertyEvents` for the SplitHub table layout, on SQLite, D1, Postgres and MySQL. */
284
+ /** The queries behind `propertyCounts` / `propertyEvents` for the sh2 table layout, on SQLite, D1, Postgres and MySQL. */
280
285
  declare function propertyCountsSql(options: PropertyCountsSqlOptions): {
281
286
  totals: PropertySql;
282
287
  events: PropertySql;
@@ -404,4 +409,4 @@ type DerivedGoal = StoredEvent & {
404
409
  /** Goal rows derived from the batch's `event` and `page` rows (see the rule above). */
405
410
  declare function goalsFromAnalytics(events: readonly StoredEvent[], goals: readonly DerivableGoal[]): DerivedGoal[];
406
411
 
407
- export { ANALYSIS_UNAVAILABLE, ANALYTICS_TYPES, ATTRIBUTION_SKEW_MS, type AppDimensions, type AttributedCounts, type AttributionEvent, type ConformanceCase, type DerivableGoal, type DerivedGoal, type ExperimentSummary, type FetchHandler, GOAL_DAILY_CAP, type HandlerOptions, PROPERTY_REPORTS_UNAVAILABLE, type PropertyCounts, type PropertyCountsSqlOptions, type PropertySql, type RawMetric, type RawPropertyReport, type RawResults, type RawStatusRow, type RawVariant, SAME_ACTION_MS, type SqlDialect, type Store, type StoreVariantCounts, type StoredEvent, type ValidationResult, type VariantCounts, type VariantCountsSql, type VariantCountsSqlOptions, appDimensions, attributeVariants, attributionConformance, countProperty, countsFromRow, createMemoryStore, createSplitHubHandler, experimentGoals, goalsFromAnalytics, propertyCountsFromRows, propertyCountsSql, rawPropertyRange, rawPropertyReport, rawResults, rawStatusTable, resultsExperiment, revenueOf, sanitizeApp, sortCounts, storedEventFromRow, toNodeListener, validateBatch, variantCountsSql, withAppDimensions };
412
+ export { ANALYSIS_UNAVAILABLE, ANALYTICS_TYPES, ATTRIBUTION_SKEW_MS, type AppDimensions, type AttributedCounts, type AttributionEvent, type ConformanceCase, type DerivableGoal, type DerivedGoal, type ExperimentSummary, type FetchHandler, GOAL_DAILY_CAP, type HandlerOptions, MAX_BODY_BYTES, PROPERTY_REPORTS_UNAVAILABLE, type PropertyCounts, type PropertyCountsSqlOptions, type PropertySql, type RawMetric, type RawPropertyReport, type RawResults, type RawStatusRow, type RawVariant, SAME_ACTION_MS, type SqlDialect, type Store, type StoreVariantCounts, type StoredEvent, type ValidationResult, type VariantCounts, type VariantCountsSql, type VariantCountsSqlOptions, appDimensions, attributeVariants, attributionConformance, countProperty, countsFromRow, createMemoryStore, createSplitHubHandler, experimentGoals, goalsFromAnalytics, propertyCountsFromRows, propertyCountsSql, rawPropertyRange, rawPropertyReport, rawResults, rawStatusTable, resultsExperiment, revenueOf, sanitizeApp, sortCounts, storedEventFromRow, toNodeListener, validateBatch, variantCountsSql, withAppDimensions };
package/dist/index.js CHANGED
@@ -6,8 +6,8 @@ import { getExperiment, HOLDOUT_KEY, holdoutExperiment } from "@splithub/core";
6
6
  var DAY_MS = 864e5;
7
7
  var ANALYSIS_UNAVAILABLE = {
8
8
  available: false,
9
- message: "Verdicts, stars and recommendations come with SplitHub Cloud.",
10
- url: "https://splithub.io/cloud"
9
+ message: "Verdicts, stars and recommendations come with sh2 Cloud.",
10
+ url: "https://sh2.uk/cloud"
11
11
  };
12
12
  function resultsExperiment(config, key) {
13
13
  return getExperiment(config, key) ?? (key === HOLDOUT_KEY ? holdoutExperiment(config) : null);
@@ -148,7 +148,7 @@ function storedEventFromRow(r) {
148
148
  }
149
149
  var PROPERTY_REPORTS_UNAVAILABLE = {
150
150
  ...ANALYSIS_UNAVAILABLE,
151
- message: "Reports with pages, sources, sessions and conversions come with SplitHub Cloud."
151
+ message: "Reports with pages, sources, sessions and conversions come with sh2 Cloud."
152
152
  };
153
153
  var DAY = /^\d{4}-\d{2}-\d{2}$/;
154
154
  var dayOf = (ms) => new Date(ms).toISOString().slice(0, 10);
@@ -572,6 +572,31 @@ function withAppDimensions(events, app) {
572
572
  // src/handler.ts
573
573
  var isBotUserAgent = (ua) => isCrawler(ua);
574
574
  var visitorUserAgent = (request) => request.headers.get("x-splithub-ua") ?? request.headers.get("user-agent");
575
+ var MAX_BODY_BYTES = 1024 * 1024;
576
+ async function readCapped(request, max) {
577
+ if (Number(request.headers.get("content-length") ?? 0) > max) return null;
578
+ if (!request.body) return "";
579
+ const reader = request.body.getReader();
580
+ const chunks = [];
581
+ let size = 0;
582
+ for (; ; ) {
583
+ const { done, value } = await reader.read();
584
+ if (done) break;
585
+ size += value.byteLength;
586
+ if (size > max) {
587
+ await reader.cancel().catch(() => void 0);
588
+ return null;
589
+ }
590
+ chunks.push(value);
591
+ }
592
+ const all = new Uint8Array(size);
593
+ let at = 0;
594
+ for (const c of chunks) {
595
+ all.set(c, at);
596
+ at += c.byteLength;
597
+ }
598
+ return new TextDecoder().decode(all);
599
+ }
575
600
  function createSplitHubHandler(options) {
576
601
  const { config, store } = options;
577
602
  const derivable = experimentGoals(config);
@@ -599,9 +624,11 @@ function createSplitHubHandler(options) {
599
624
  const tail = path.split("/").slice(-2);
600
625
  if (request.method === "OPTIONS") return new Response(null, { status: 204, headers: corsHeaders(request) });
601
626
  if (request.method === "POST") {
627
+ const text2 = await readCapped(request, MAX_BODY_BYTES);
628
+ if (text2 === null) return json(request, { error: `body larger than ${MAX_BODY_BYTES} bytes` }, 413);
602
629
  let body;
603
630
  try {
604
- body = await request.json();
631
+ body = JSON.parse(text2);
605
632
  } catch {
606
633
  return json(request, { error: "invalid JSON" }, 400);
607
634
  }
@@ -653,6 +680,8 @@ function createSplitHubHandler(options) {
653
680
  }
654
681
 
655
682
  // src/nodeAdapter.ts
683
+ var BodyTooLarge = class extends Error {
684
+ };
656
685
  function toNodeListener(handler) {
657
686
  return (req, res) => {
658
687
  void (async () => {
@@ -664,9 +693,12 @@ function toNodeListener(handler) {
664
693
  const body = response.body ? Buffer.from(await response.arrayBuffer()) : null;
665
694
  res.end(body);
666
695
  } catch (error) {
667
- res.statusCode = 500;
696
+ const tooLarge = error instanceof BodyTooLarge;
697
+ res.statusCode = tooLarge ? 413 : 500;
668
698
  res.setHeader("content-type", "application/json");
669
- res.end(JSON.stringify({ error: error instanceof Error ? error.message : "internal error" }));
699
+ if (tooLarge) res.setHeader("connection", "close");
700
+ res.end(JSON.stringify({ error: tooLarge ? `body larger than ${MAX_BODY_BYTES} bytes` : error instanceof Error ? error.message : "internal error" }));
701
+ if (tooLarge) req.destroy();
670
702
  }
671
703
  })();
672
704
  };
@@ -684,11 +716,21 @@ async function toRequest(req) {
684
716
  const method = req.method ?? "GET";
685
717
  let body;
686
718
  if (method !== "GET" && method !== "HEAD") {
719
+ if (Number(req.headers["content-length"] ?? 0) > MAX_BODY_BYTES) throw new BodyTooLarge();
687
720
  const chunks = [];
688
- for await (const chunk of req) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
689
- const all = Buffer.concat(chunks);
690
- const copy = new Uint8Array(all.byteLength);
691
- copy.set(all);
721
+ let size = 0;
722
+ for await (const chunk of req) {
723
+ const b = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
724
+ size += b.byteLength;
725
+ if (size > MAX_BODY_BYTES) throw new BodyTooLarge();
726
+ chunks.push(b);
727
+ }
728
+ const copy = new Uint8Array(size);
729
+ let at = 0;
730
+ for (const c of chunks) {
731
+ copy.set(c, at);
732
+ at += c.byteLength;
733
+ }
692
734
  body = copy.buffer;
693
735
  }
694
736
  return new Request(url, { method, headers, body });
@@ -803,6 +845,7 @@ export {
803
845
  ANALYTICS_TYPES,
804
846
  ATTRIBUTION_SKEW_MS,
805
847
  GOAL_DAILY_CAP,
848
+ MAX_BODY_BYTES,
806
849
  PROPERTY_REPORTS_UNAVAILABLE,
807
850
  SAME_ACTION_MS,
808
851
  appDimensions,