@iann29/rastro 0.1.0-alpha.0 → 0.1.0-alpha.2

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.
Files changed (79) hide show
  1. package/README.md +98 -18
  2. package/dist/component/_generated/api.d.ts +7 -1
  3. package/dist/component/_generated/api.d.ts.map +1 -1
  4. package/dist/component/_generated/api.js.map +1 -1
  5. package/dist/component/constants.d.ts +11 -6
  6. package/dist/component/constants.d.ts.map +1 -1
  7. package/dist/component/constants.js +11 -6
  8. package/dist/component/constants.js.map +1 -1
  9. package/dist/component/convex.config.d.ts +2 -2
  10. package/dist/component/convex.config.d.ts.map +1 -1
  11. package/dist/component/convex.config.js +4 -1
  12. package/dist/component/convex.config.js.map +1 -1
  13. package/dist/component/eventStore.d.ts +102 -0
  14. package/dist/component/eventStore.d.ts.map +1 -0
  15. package/dist/component/eventStore.js +463 -0
  16. package/dist/component/eventStore.js.map +1 -0
  17. package/dist/component/goals.d.ts.map +1 -1
  18. package/dist/component/goals.js +17 -4
  19. package/dist/component/goals.js.map +1 -1
  20. package/dist/component/http.d.ts.map +1 -1
  21. package/dist/component/http.js +39 -7
  22. package/dist/component/http.js.map +1 -1
  23. package/dist/component/ingest.d.ts.map +1 -1
  24. package/dist/component/ingest.js +520 -207
  25. package/dist/component/ingest.js.map +1 -1
  26. package/dist/component/live.d.ts +8 -0
  27. package/dist/component/live.d.ts.map +1 -1
  28. package/dist/component/live.js +72 -8
  29. package/dist/component/live.js.map +1 -1
  30. package/dist/component/migrations.d.ts +17 -0
  31. package/dist/component/migrations.d.ts.map +1 -0
  32. package/dist/component/migrations.js +44 -0
  33. package/dist/component/migrations.js.map +1 -0
  34. package/dist/component/reports.d.ts +16 -16
  35. package/dist/component/reports.d.ts.map +1 -1
  36. package/dist/component/reports.js +228 -65
  37. package/dist/component/reports.js.map +1 -1
  38. package/dist/component/retention.d.ts.map +1 -1
  39. package/dist/component/retention.js +15 -10
  40. package/dist/component/retention.js.map +1 -1
  41. package/dist/component/sanitize.d.ts.map +1 -1
  42. package/dist/component/sanitize.js +11 -3
  43. package/dist/component/sanitize.js.map +1 -1
  44. package/dist/component/schema.d.ts +132 -9
  45. package/dist/component/schema.js +32 -7
  46. package/dist/component/schema.js.map +1 -1
  47. package/dist/component/sites.d.ts.map +1 -1
  48. package/dist/component/sites.js +27 -18
  49. package/dist/component/sites.js.map +1 -1
  50. package/dist/component/validators.d.ts +96 -0
  51. package/dist/component/validators.d.ts.map +1 -1
  52. package/dist/component/validators.js +12 -0
  53. package/dist/component/validators.js.map +1 -1
  54. package/dist/tracker/generated.d.ts +4 -4
  55. package/dist/tracker/generated.d.ts.map +1 -1
  56. package/dist/tracker/generated.js +4 -4
  57. package/dist/tracker/generated.js.map +1 -1
  58. package/dist/tracker/tracker.js +33 -22
  59. package/dist/tracker/tracker.js.map +1 -1
  60. package/dist/tracker.min.js +1 -1
  61. package/docs/benchmarks/2026-08-20-realistic.md +171 -0
  62. package/package.json +15 -5
  63. package/scripts/benchmark-ingest.mjs +499 -0
  64. package/src/component/_generated/api.ts +7 -1
  65. package/src/component/constants.ts +11 -6
  66. package/src/component/convex.config.ts +5 -1
  67. package/src/component/eventStore.ts +684 -0
  68. package/src/component/goals.ts +25 -4
  69. package/src/component/http.ts +49 -6
  70. package/src/component/ingest.ts +671 -215
  71. package/src/component/live.ts +83 -8
  72. package/src/component/migrations.ts +52 -0
  73. package/src/component/reports.ts +295 -86
  74. package/src/component/retention.ts +15 -12
  75. package/src/component/sanitize.ts +10 -3
  76. package/src/component/schema.ts +35 -7
  77. package/src/component/sites.ts +31 -18
  78. package/src/component/validators.ts +16 -0
  79. package/src/test.ts +2 -0
@@ -62,10 +62,25 @@ export const upsert = mutation({
62
62
  if (args.goalId && existingByKey && existingByKey._id !== args.goalId) {
63
63
  fail("CONFLICT", "another goal already uses this key");
64
64
  }
65
+ const goals = await ctx.db
66
+ .query("goals")
67
+ .withIndex("by_siteId", (range) => range.eq("siteId", args.siteId))
68
+ .take(MAX_GOALS_PER_SITE + 1);
65
69
  const now = Date.now();
66
70
  if (goalId) {
67
71
  const goal = await ctx.db.get("goals", goalId);
68
72
  if (!goal || goal.siteId !== args.siteId) fail("NOT_FOUND", "goal not found");
73
+ if (
74
+ (args.active ?? goal.active) &&
75
+ goals.some((candidate) =>
76
+ candidate._id !== goalId &&
77
+ candidate.active &&
78
+ candidate.matchType === args.matchType &&
79
+ candidate.matchValue === matchValue
80
+ )
81
+ ) {
82
+ fail("CONFLICT", "another active goal already uses this matcher");
83
+ }
69
84
  await ctx.db.patch("goals", goalId, {
70
85
  key,
71
86
  name,
@@ -78,10 +93,16 @@ export const upsert = mutation({
78
93
  return goalId;
79
94
  }
80
95
 
81
- const goals = await ctx.db
82
- .query("goals")
83
- .withIndex("by_siteId", (range) => range.eq("siteId", args.siteId))
84
- .take(MAX_GOALS_PER_SITE + 1);
96
+ if (
97
+ (args.active ?? true) &&
98
+ goals.some((candidate) =>
99
+ candidate.active &&
100
+ candidate.matchType === args.matchType &&
101
+ candidate.matchValue === matchValue
102
+ )
103
+ ) {
104
+ fail("CONFLICT", "another active goal already uses this matcher");
105
+ }
85
106
  if (goals.length >= MAX_GOALS_PER_SITE) {
86
107
  fail("LIMIT_EXCEEDED", "goal limit reached", { limit: MAX_GOALS_PER_SITE });
87
108
  }
@@ -24,16 +24,29 @@ http.route({
24
24
  handler: httpAction(async (ctx, request) => {
25
25
  const origin = request.headers.get("origin");
26
26
  if (!origin) return json({ error: { code: "ORIGIN_REQUIRED" } }, 400);
27
- const bytes = await request.arrayBuffer();
28
- if (bytes.byteLength > MAX_BATCH_BYTES) {
29
- return json({ error: { code: "BATCH_TOO_LARGE", limitBytes: MAX_BATCH_BYTES } }, 413);
27
+ const headers = corsHeaders(origin);
28
+ const contentLength = Number(request.headers.get("content-length"));
29
+ if (Number.isFinite(contentLength) && contentLength > MAX_BATCH_BYTES) {
30
+ return json(
31
+ { error: { code: "BATCH_TOO_LARGE", limitBytes: MAX_BATCH_BYTES } },
32
+ 413,
33
+ headers,
34
+ );
30
35
  }
31
36
 
32
37
  let payload: { siteId: string; events: TrackerEvent[] };
33
38
  try {
39
+ const bytes = await readLimitedBody(request, MAX_BATCH_BYTES);
34
40
  const parsed: unknown = JSON.parse(new TextDecoder().decode(bytes));
35
41
  payload = parsePayload(parsed);
36
42
  } catch (error) {
43
+ if (error instanceof PayloadTooLargeError) {
44
+ return json(
45
+ { error: { code: "BATCH_TOO_LARGE", limitBytes: MAX_BATCH_BYTES } },
46
+ 413,
47
+ headers,
48
+ );
49
+ }
37
50
  return json(
38
51
  {
39
52
  error: {
@@ -42,6 +55,7 @@ http.route({
42
55
  },
43
56
  },
44
57
  400,
58
+ headers,
45
59
  );
46
60
  }
47
61
 
@@ -61,7 +75,7 @@ http.route({
61
75
  events: payload.events,
62
76
  context,
63
77
  });
64
- return json(result, 200, corsHeaders(origin));
78
+ return json(result, 200, headers);
65
79
  } catch (error) {
66
80
  if (error instanceof ConvexError) {
67
81
  const data = error.data as { code?: string; message?: string };
@@ -71,9 +85,9 @@ http.route({
71
85
  : data.code === "RATE_LIMITED"
72
86
  ? 429
73
87
  : 400;
74
- return json({ error: data }, status);
88
+ return json({ error: data }, status, headers);
75
89
  }
76
- return json({ error: { code: "INVALID_REQUEST" } }, 400);
90
+ return json({ error: { code: "INVALID_REQUEST" } }, 400, headers);
77
91
  }
78
92
  }),
79
93
  });
@@ -136,6 +150,35 @@ function parsePayload(value: unknown): { siteId: string; events: TrackerEvent[]
136
150
  return { siteId: value.siteId, events: value.events.map(parseEvent) };
137
151
  }
138
152
 
153
+ class PayloadTooLargeError extends Error {}
154
+
155
+ async function readLimitedBody(
156
+ request: Request,
157
+ maximumBytes: number,
158
+ ): Promise<Uint8Array> {
159
+ if (!request.body) return new Uint8Array();
160
+ const reader = request.body.getReader();
161
+ const chunks: Uint8Array[] = [];
162
+ let length = 0;
163
+ while (true) {
164
+ const { done, value } = await reader.read();
165
+ if (done) break;
166
+ length += value.byteLength;
167
+ if (length > maximumBytes) {
168
+ await reader.cancel();
169
+ throw new PayloadTooLargeError();
170
+ }
171
+ chunks.push(value);
172
+ }
173
+ const result = new Uint8Array(length);
174
+ let offset = 0;
175
+ for (const chunk of chunks) {
176
+ result.set(chunk, offset);
177
+ offset += chunk.byteLength;
178
+ }
179
+ return result;
180
+ }
181
+
139
182
  function parseEvent(value: unknown): TrackerEvent {
140
183
  if (!isPlainRecord(value)) throw new Error("each event must be an object");
141
184
  const type = value.type;