@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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @splithub/node
2
2
 
3
- Self-hosted ingest and results for SplitHub. One fetch-style handler that
3
+ Self-hosted ingest and results for sh2. One fetch-style handler that
4
4
  works in Next.js route handlers, Remix, Hono, Bun, Deno and plain Node.
5
5
 
6
6
  ```ts
@@ -41,12 +41,12 @@ Reads return raw numbers from your own database:
41
41
  ],
42
42
  "metrics": [{ "metric": "refund", "role": "guardrail", "worse": "higher", "variants": [] }],
43
43
  "days": 6.2,
44
- "analysis": { "available": false, "message": "Verdicts, stars and recommendations come with SplitHub Cloud.", "url": "https://splithub.io/cloud" }
44
+ "analysis": { "available": false, "message": "Verdicts, stars and recommendations come with sh2 Cloud.", "url": "https://sh2.uk/cloud" }
45
45
  }
46
46
  ```
47
47
 
48
48
  The first variant is the control. `rawResults(config, store, id)` and `rawStatusTable(config, store)`
49
- give the same numbers in code. Verdicts, data-quality stars and recommendations come with SplitHub Cloud.
49
+ give the same numbers in code. Verdicts, data-quality stars and recommendations come with sh2 Cloud.
50
50
 
51
51
  ## Apps
52
52
 
package/dist/index.cjs CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  ANALYTICS_TYPES: () => ANALYTICS_TYPES,
25
25
  ATTRIBUTION_SKEW_MS: () => ATTRIBUTION_SKEW_MS,
26
26
  GOAL_DAILY_CAP: () => GOAL_DAILY_CAP,
27
+ MAX_BODY_BYTES: () => MAX_BODY_BYTES,
27
28
  PROPERTY_REPORTS_UNAVAILABLE: () => PROPERTY_REPORTS_UNAVAILABLE,
28
29
  SAME_ACTION_MS: () => SAME_ACTION_MS,
29
30
  appDimensions: () => appDimensions,
@@ -61,8 +62,8 @@ var import_core = require("@splithub/core");
61
62
  var DAY_MS = 864e5;
62
63
  var ANALYSIS_UNAVAILABLE = {
63
64
  available: false,
64
- message: "Verdicts, stars and recommendations come with SplitHub Cloud.",
65
- url: "https://splithub.io/cloud"
65
+ message: "Verdicts, stars and recommendations come with sh2 Cloud.",
66
+ url: "https://sh2.uk/cloud"
66
67
  };
67
68
  function resultsExperiment(config, key) {
68
69
  return (0, import_core.getExperiment)(config, key) ?? (key === import_core.HOLDOUT_KEY ? (0, import_core.holdoutExperiment)(config) : null);
@@ -203,7 +204,7 @@ function storedEventFromRow(r) {
203
204
  }
204
205
  var PROPERTY_REPORTS_UNAVAILABLE = {
205
206
  ...ANALYSIS_UNAVAILABLE,
206
- message: "Reports with pages, sources, sessions and conversions come with SplitHub Cloud."
207
+ message: "Reports with pages, sources, sessions and conversions come with sh2 Cloud."
207
208
  };
208
209
  var DAY = /^\d{4}-\d{2}-\d{2}$/;
209
210
  var dayOf = (ms) => new Date(ms).toISOString().slice(0, 10);
@@ -627,6 +628,31 @@ function withAppDimensions(events, app) {
627
628
  // src/handler.ts
628
629
  var isBotUserAgent = (ua) => (0, import_core4.isCrawler)(ua);
629
630
  var visitorUserAgent = (request) => request.headers.get("x-splithub-ua") ?? request.headers.get("user-agent");
631
+ var MAX_BODY_BYTES = 1024 * 1024;
632
+ async function readCapped(request, max) {
633
+ if (Number(request.headers.get("content-length") ?? 0) > max) return null;
634
+ if (!request.body) return "";
635
+ const reader = request.body.getReader();
636
+ const chunks = [];
637
+ let size = 0;
638
+ for (; ; ) {
639
+ const { done, value } = await reader.read();
640
+ if (done) break;
641
+ size += value.byteLength;
642
+ if (size > max) {
643
+ await reader.cancel().catch(() => void 0);
644
+ return null;
645
+ }
646
+ chunks.push(value);
647
+ }
648
+ const all = new Uint8Array(size);
649
+ let at = 0;
650
+ for (const c of chunks) {
651
+ all.set(c, at);
652
+ at += c.byteLength;
653
+ }
654
+ return new TextDecoder().decode(all);
655
+ }
630
656
  function createSplitHubHandler(options) {
631
657
  const { config, store } = options;
632
658
  const derivable = experimentGoals(config);
@@ -654,9 +680,11 @@ function createSplitHubHandler(options) {
654
680
  const tail = path.split("/").slice(-2);
655
681
  if (request.method === "OPTIONS") return new Response(null, { status: 204, headers: corsHeaders(request) });
656
682
  if (request.method === "POST") {
683
+ const text2 = await readCapped(request, MAX_BODY_BYTES);
684
+ if (text2 === null) return json(request, { error: `body larger than ${MAX_BODY_BYTES} bytes` }, 413);
657
685
  let body;
658
686
  try {
659
- body = await request.json();
687
+ body = JSON.parse(text2);
660
688
  } catch {
661
689
  return json(request, { error: "invalid JSON" }, 400);
662
690
  }
@@ -708,6 +736,8 @@ function createSplitHubHandler(options) {
708
736
  }
709
737
 
710
738
  // src/nodeAdapter.ts
739
+ var BodyTooLarge = class extends Error {
740
+ };
711
741
  function toNodeListener(handler) {
712
742
  return (req, res) => {
713
743
  void (async () => {
@@ -719,9 +749,12 @@ function toNodeListener(handler) {
719
749
  const body = response.body ? Buffer.from(await response.arrayBuffer()) : null;
720
750
  res.end(body);
721
751
  } catch (error) {
722
- res.statusCode = 500;
752
+ const tooLarge = error instanceof BodyTooLarge;
753
+ res.statusCode = tooLarge ? 413 : 500;
723
754
  res.setHeader("content-type", "application/json");
724
- res.end(JSON.stringify({ error: error instanceof Error ? error.message : "internal error" }));
755
+ if (tooLarge) res.setHeader("connection", "close");
756
+ res.end(JSON.stringify({ error: tooLarge ? `body larger than ${MAX_BODY_BYTES} bytes` : error instanceof Error ? error.message : "internal error" }));
757
+ if (tooLarge) req.destroy();
725
758
  }
726
759
  })();
727
760
  };
@@ -739,11 +772,21 @@ async function toRequest(req) {
739
772
  const method = req.method ?? "GET";
740
773
  let body;
741
774
  if (method !== "GET" && method !== "HEAD") {
775
+ if (Number(req.headers["content-length"] ?? 0) > MAX_BODY_BYTES) throw new BodyTooLarge();
742
776
  const chunks = [];
743
- for await (const chunk of req) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
744
- const all = Buffer.concat(chunks);
745
- const copy = new Uint8Array(all.byteLength);
746
- copy.set(all);
777
+ let size = 0;
778
+ for await (const chunk of req) {
779
+ const b = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
780
+ size += b.byteLength;
781
+ if (size > MAX_BODY_BYTES) throw new BodyTooLarge();
782
+ chunks.push(b);
783
+ }
784
+ const copy = new Uint8Array(size);
785
+ let at = 0;
786
+ for (const c of chunks) {
787
+ copy.set(c, at);
788
+ at += c.byteLength;
789
+ }
747
790
  body = copy.buffer;
748
791
  }
749
792
  return new Request(url, { method, headers, body });
@@ -859,6 +902,7 @@ function sortCounts(rows) {
859
902
  ANALYTICS_TYPES,
860
903
  ATTRIBUTION_SKEW_MS,
861
904
  GOAL_DAILY_CAP,
905
+ MAX_BODY_BYTES,
862
906
  PROPERTY_REPORTS_UNAVAILABLE,
863
907
  SAME_ACTION_MS,
864
908
  appDimensions,