@guckdev/vite 0.1.0 → 0.10.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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @guckdev/vite
2
2
 
3
- Vite dev server plugin that proxies `/guck/emit` to a shared Guck MCP ingest
4
- endpoint and injects the project config server-side.
3
+ Vite dev server plugin that accepts `/guck/emit` and writes events directly to
4
+ Guck’s local log store.
5
5
 
6
6
  ## Install
7
7
 
@@ -26,7 +26,6 @@ Then point the browser SDK at `/guck/emit`.
26
26
 
27
27
  ```ts
28
28
  guckVitePlugin({
29
- ingestUrl: "http://127.0.0.1:7331/guck/emit", // shared MCP ingest
30
29
  configPath: "/absolute/path/or/project/root", // defaults to process.cwd()
31
30
  path: "/guck/emit", // local Vite endpoint
32
31
  enabled: true, // auto-disabled for non-dev
@@ -35,5 +34,5 @@ guckVitePlugin({
35
34
 
36
35
  Notes:
37
36
  - This plugin only runs in Vite dev server mode (`apply: "serve"`).
38
- - The project config path is injected as a header on the server side.
39
37
  - CORS preflight is handled for browser requests.
38
+ - The log directory is controlled by the server env (`GUCK_DIR`) or the default `~/.guck/logs`.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { Plugin } from "vite";
2
2
  export type GuckVitePluginOptions = {
3
- ingestUrl?: string;
4
3
  configPath?: string;
5
4
  path?: string;
6
5
  enabled?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAC;AAIlD,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AA0EF,eAAO,MAAM,cAAc,aAAa,qBAAqB,KAAQ,MAsCpE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAC;AAYlD,MAAM,MAAM,qBAAqB,GAAG;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAiFF,eAAO,MAAM,cAAc,aAAa,qBAAqB,KAAQ,MA+EpE,CAAC"}
package/dist/index.js CHANGED
@@ -1,70 +1,76 @@
1
1
  import { Buffer } from "node:buffer";
2
- const DEFAULT_INGEST_URL = "http://127.0.0.1:7331/guck/emit";
2
+ import { appendEvent, buildEvent, loadConfig, redactEvent, resolveStoreDir, } from "@guckdev/core";
3
3
  const DEFAULT_PATH = "/guck/emit";
4
+ const DEFAULT_MAX_BODY_BYTES = 512000;
4
5
  const applyCors = (res) => {
5
6
  res.setHeader("Access-Control-Allow-Origin", "*");
6
7
  res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
7
8
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
8
9
  };
9
- const readBody = async (req) => {
10
- return await new Promise((resolve, reject) => {
10
+ const writeJson = (res, status, payload) => {
11
+ applyCors(res);
12
+ res.statusCode = status;
13
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
14
+ res.end(JSON.stringify(payload));
15
+ };
16
+ const readBody = async (req, maxBodyBytes) => {
17
+ return await new Promise((resolve) => {
11
18
  const chunks = [];
19
+ let size = 0;
20
+ let tooLarge = false;
21
+ let settled = false;
22
+ const finish = (result) => {
23
+ if (settled) {
24
+ return;
25
+ }
26
+ settled = true;
27
+ resolve(result);
28
+ };
12
29
  req.on("data", (chunk) => {
13
- chunks.push(Buffer.from(chunk));
30
+ if (settled) {
31
+ return;
32
+ }
33
+ size += chunk.length;
34
+ if (size > maxBodyBytes) {
35
+ tooLarge = true;
36
+ }
37
+ if (!tooLarge) {
38
+ chunks.push(Buffer.from(chunk));
39
+ }
40
+ });
41
+ req.on("aborted", () => finish({ ok: false, error: "aborted" }));
42
+ req.on("error", () => finish({ ok: false, error: "aborted" }));
43
+ req.on("end", () => {
44
+ if (settled) {
45
+ return;
46
+ }
47
+ if (tooLarge) {
48
+ finish({ ok: false, error: "too_large" });
49
+ return;
50
+ }
51
+ finish({ ok: true, data: Buffer.concat(chunks).toString("utf8") });
14
52
  });
15
- req.on("end", () => resolve(Buffer.concat(chunks)));
16
- req.on("aborted", () => reject(new Error("aborted")));
17
- req.on("error", reject);
18
53
  });
19
54
  };
20
- const readContentType = (req) => {
21
- const header = req.headers["content-type"];
22
- if (Array.isArray(header)) {
23
- return header[0] ?? "application/json";
24
- }
25
- return header ?? "application/json";
26
- };
27
- const handleProxy = async (req, res, ingestUrl, configPath) => {
28
- let body;
29
- try {
30
- body = await readBody(req);
31
- }
32
- catch {
33
- applyCors(res);
34
- res.statusCode = 400;
35
- res.setHeader("Content-Type", "application/json; charset=utf-8");
36
- res.end(JSON.stringify({ ok: false, error: "Request aborted" }));
37
- return;
38
- }
39
- try {
40
- const response = await fetch(ingestUrl, {
41
- method: "POST",
42
- headers: {
43
- "content-type": readContentType(req),
44
- "x-guck-config-path": configPath,
45
- },
46
- body,
47
- });
48
- applyCors(res);
49
- res.statusCode = response.status;
50
- const contentType = response.headers.get("content-type");
51
- if (contentType) {
52
- res.setHeader("Content-Type", contentType);
55
+ const coerceItems = (payload) => {
56
+ if (Array.isArray(payload)) {
57
+ const items = [];
58
+ for (const item of payload) {
59
+ if (!item || typeof item !== "object") {
60
+ return null;
61
+ }
62
+ items.push(item);
53
63
  }
54
- const payload = Buffer.from(await response.arrayBuffer());
55
- res.end(payload);
64
+ return items;
56
65
  }
57
- catch {
58
- applyCors(res);
59
- res.statusCode = 502;
60
- res.setHeader("Content-Type", "application/json; charset=utf-8");
61
- res.end(JSON.stringify({ ok: false, error: "Upstream unreachable" }));
66
+ if (payload && typeof payload === "object") {
67
+ return [payload];
62
68
  }
69
+ return null;
63
70
  };
64
71
  export const guckVitePlugin = (options = {}) => {
65
- const ingestUrl = options.ingestUrl ?? DEFAULT_INGEST_URL;
66
72
  const configPath = options.configPath ?? process.cwd();
67
- const path = options.path ?? DEFAULT_PATH;
73
+ const routePath = options.path ?? DEFAULT_PATH;
68
74
  const enabled = options.enabled ?? true;
69
75
  return {
70
76
  name: "guck-vite",
@@ -75,7 +81,7 @@ export const guckVitePlugin = (options = {}) => {
75
81
  }
76
82
  server.middlewares.use(async (req, res, next) => {
77
83
  const url = new URL(req.url ?? "", "http://localhost");
78
- if (url.pathname !== path) {
84
+ if (url.pathname !== routePath) {
79
85
  next();
80
86
  return;
81
87
  }
@@ -91,7 +97,46 @@ export const guckVitePlugin = (options = {}) => {
91
97
  res.end();
92
98
  return;
93
99
  }
94
- await handleProxy(req, res, ingestUrl, configPath);
100
+ const { config, rootDir } = loadConfig({ configPath });
101
+ if (!config.enabled) {
102
+ writeJson(res, 403, { ok: false, error: "Guck disabled" });
103
+ return;
104
+ }
105
+ const body = await readBody(req, DEFAULT_MAX_BODY_BYTES);
106
+ if (!body.ok) {
107
+ if (body.error === "too_large") {
108
+ writeJson(res, 413, { ok: false, error: "Payload too large" });
109
+ return;
110
+ }
111
+ writeJson(res, 400, { ok: false, error: "Request aborted" });
112
+ return;
113
+ }
114
+ let parsed;
115
+ try {
116
+ parsed = JSON.parse(body.data);
117
+ }
118
+ catch {
119
+ writeJson(res, 400, { ok: false, error: "Invalid JSON" });
120
+ return;
121
+ }
122
+ const items = coerceItems(parsed);
123
+ if (!items) {
124
+ writeJson(res, 400, { ok: false, error: "Invalid event payload" });
125
+ return;
126
+ }
127
+ const storeDir = resolveStoreDir(config, rootDir);
128
+ try {
129
+ for (const item of items) {
130
+ const event = buildEvent(item, { service: config.default_service });
131
+ const redacted = redactEvent(config, event);
132
+ await appendEvent(storeDir, redacted);
133
+ }
134
+ }
135
+ catch {
136
+ writeJson(res, 500, { ok: false, error: "Failed to write event" });
137
+ return;
138
+ }
139
+ writeJson(res, 200, { ok: true, count: items.length });
95
140
  });
96
141
  },
97
142
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAUrC,MAAM,kBAAkB,GAAG,iCAAiC,CAAC;AAC7D,MAAM,YAAY,GAAG,YAAY,CAAC;AAElC,MAAM,SAAS,GAAG,CAAC,GAAmB,EAAQ,EAAE;IAC9C,GAAG,CAAC,SAAS,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;IAClD,GAAG,CAAC,SAAS,CAAC,8BAA8B,EAAE,eAAe,CAAC,CAAC;IAC/D,GAAG,CAAC,SAAS,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAC;AAChE,CAAC,CAAC;AAEF,MAAM,QAAQ,GAAG,KAAK,EAAE,GAAoB,EAAmB,EAAE;IAC/D,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACvB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;QACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACpD,GAAG,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACtD,GAAG,CAAC,EAAE,CAAC,O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AAE,CAAC;wBACzB,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;wBACpE,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;wBAC5C,MAAM,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;oBACxC,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;oBACnE,OAAO;gBACT,CAAC;gBAED,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;YACzD,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@guckdev/vite",
3
- "version": "0.1.0",
4
- "description": "Vite plugin for Guck browser ingest",
3
+ "version": "0.10.1",
4
+ "description": "Vite plugin for Guck browser logging",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tillkolter/guck-mcp"
@@ -16,25 +16,31 @@
16
16
  },
17
17
  "files": [
18
18
  "dist",
19
+ "scripts",
19
20
  "src",
20
21
  "tsconfig.json"
21
22
  ],
22
23
  "publishConfig": {
23
24
  "access": "public"
24
25
  },
26
+ "scripts": {
27
+ "build": "node scripts/ensure-deps.mjs && tsc -p tsconfig.json",
28
+ "build:watch": "tsc -p tsconfig.json --watch",
29
+ "test": "pnpm run build && node --test test/*.test.js",
30
+ "prepare": "pnpm run build"
31
+ },
25
32
  "engines": {
26
33
  "node": ">=18"
27
34
  },
28
35
  "peerDependencies": {
29
36
  "vite": "^5.0.0 || ^6.0.0"
30
37
  },
38
+ "dependencies": {
39
+ "@guckdev/core": "0.10.1"
40
+ },
31
41
  "devDependencies": {
32
42
  "@types/node": "25.2.2",
43
+ "typescript": "5.6.3",
33
44
  "vite": "^5.4.0"
34
- },
35
- "scripts": {
36
- "build": "pnpm --package=typescript@5.6.3 dlx tsc -p tsconfig.json",
37
- "build:watch": "pnpm --package=typescript@5.6.3 dlx tsc -p tsconfig.json --watch",
38
- "test": "pnpm run build && node --test test/*.test.js"
39
45
  }
40
- }
46
+ }
@@ -0,0 +1,23 @@
1
+ import { existsSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { spawnSync } from "node:child_process";
4
+
5
+ const dep = { name: "@guckdev/core", entry: "dist/index.js" };
6
+
7
+ const npmExecPath = process.env.npm_execpath;
8
+ const run = (cwd, args) => {
9
+ const cmd = npmExecPath ? process.execPath : "pnpm";
10
+ const cmdArgs = npmExecPath ? [npmExecPath, ...args] : args;
11
+ const result = spawnSync(cmd, cmdArgs, { cwd, stdio: "inherit" });
12
+ if (result.status !== 0) {
13
+ process.exit(result.status ?? 1);
14
+ }
15
+ };
16
+
17
+ const depDir = path.join(process.cwd(), "node_modules", dep.name);
18
+ if (existsSync(depDir)) {
19
+ const entryPath = path.join(depDir, dep.entry);
20
+ if (!existsSync(entryPath)) {
21
+ run(depDir, ["run", "build"]);
22
+ }
23
+ }
package/src/index.ts CHANGED
@@ -1,16 +1,23 @@
1
1
  import type { Plugin, ViteDevServer } from "vite";
2
- import { Buffer } from "node:buffer";
3
2
  import type { IncomingMessage, ServerResponse } from "node:http";
3
+ import { Buffer } from "node:buffer";
4
+ import {
5
+ appendEvent,
6
+ buildEvent,
7
+ GuckEvent,
8
+ loadConfig,
9
+ redactEvent,
10
+ resolveStoreDir,
11
+ } from "@guckdev/core";
4
12
 
5
13
  export type GuckVitePluginOptions = {
6
- ingestUrl?: string;
7
14
  configPath?: string;
8
15
  path?: string;
9
16
  enabled?: boolean;
10
17
  };
11
18
 
12
- const DEFAULT_INGEST_URL = "http://127.0.0.1:7331/guck/emit";
13
19
  const DEFAULT_PATH = "/guck/emit";
20
+ const DEFAULT_MAX_BODY_BYTES = 512000;
14
21
 
15
22
  const applyCors = (res: ServerResponse): void => {
16
23
  res.setHeader("Access-Control-Allow-Origin", "*");
@@ -18,73 +25,79 @@ const applyCors = (res: ServerResponse): void => {
18
25
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
19
26
  };
20
27
 
21
- const readBody = async (req: IncomingMessage): Promise<Buffer> => {
22
- return await new Promise((resolve, reject) => {
23
- const chunks: Buffer[] = [];
24
- req.on("data", (chunk) => {
25
- chunks.push(Buffer.from(chunk));
26
- });
27
- req.on("end", () => resolve(Buffer.concat(chunks)));
28
- req.on("aborted", () => reject(new Error("aborted")));
29
- req.on("error", reject);
30
- });
28
+ const writeJson = (res: ServerResponse, status: number, payload: unknown): void => {
29
+ applyCors(res);
30
+ res.statusCode = status;
31
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
32
+ res.end(JSON.stringify(payload));
31
33
  };
32
34
 
33
- const readContentType = (req: IncomingMessage): string => {
34
- const header = req.headers["content-type"];
35
- if (Array.isArray(header)) {
36
- return header[0] ?? "application/json";
37
- }
38
- return header ?? "application/json";
39
- };
40
-
41
- const handleProxy = async (
35
+ const readBody = async (
42
36
  req: IncomingMessage,
43
- res: ServerResponse,
44
- ingestUrl: string,
45
- configPath: string,
46
- ): Promise<void> => {
47
- let body: Buffer;
48
- try {
49
- body = await readBody(req);
50
- } catch {
51
- applyCors(res);
52
- res.statusCode = 400;
53
- res.setHeader("Content-Type", "application/json; charset=utf-8");
54
- res.end(JSON.stringify({ ok: false, error: "Request aborted" }));
55
- return;
56
- }
37
+ maxBodyBytes: number,
38
+ ): Promise<{ ok: true; data: string } | { ok: false; error: "too_large" | "aborted" }> => {
39
+ return await new Promise((resolve) => {
40
+ const chunks: Buffer[] = [];
41
+ let size = 0;
42
+ let tooLarge = false;
43
+ let settled = false;
57
44
 
58
- try {
59
- const response = await fetch(ingestUrl, {
60
- method: "POST",
61
- headers: {
62
- "content-type": readContentType(req),
63
- "x-guck-config-path": configPath,
64
- },
65
- body,
45
+ const finish = (result: { ok: true; data: string } | { ok: false; error: "too_large" | "aborted" }) => {
46
+ if (settled) {
47
+ return;
48
+ }
49
+ settled = true;
50
+ resolve(result);
51
+ };
52
+
53
+ req.on("data", (chunk) => {
54
+ if (settled) {
55
+ return;
56
+ }
57
+ size += chunk.length;
58
+ if (size > maxBodyBytes) {
59
+ tooLarge = true;
60
+ }
61
+ if (!tooLarge) {
62
+ chunks.push(Buffer.from(chunk));
63
+ }
64
+ });
65
+
66
+ req.on("aborted", () => finish({ ok: false, error: "aborted" }));
67
+ req.on("error", () => finish({ ok: false, error: "aborted" }));
68
+ req.on("end", () => {
69
+ if (settled) {
70
+ return;
71
+ }
72
+ if (tooLarge) {
73
+ finish({ ok: false, error: "too_large" });
74
+ return;
75
+ }
76
+ finish({ ok: true, data: Buffer.concat(chunks).toString("utf8") });
66
77
  });
78
+ });
79
+ };
67
80
 
68
- applyCors(res);
69
- res.statusCode = response.status;
70
- const contentType = response.headers.get("content-type");
71
- if (contentType) {
72
- res.setHeader("Content-Type", contentType);
81
+ const coerceItems = (payload: unknown): Array<Partial<GuckEvent>> | null => {
82
+ if (Array.isArray(payload)) {
83
+ const items: Array<Partial<GuckEvent>> = [];
84
+ for (const item of payload) {
85
+ if (!item || typeof item !== "object") {
86
+ return null;
87
+ }
88
+ items.push(item as Partial<GuckEvent>);
73
89
  }
74
- const payload = Buffer.from(await response.arrayBuffer());
75
- res.end(payload);
76
- } catch {
77
- applyCors(res);
78
- res.statusCode = 502;
79
- res.setHeader("Content-Type", "application/json; charset=utf-8");
80
- res.end(JSON.stringify({ ok: false, error: "Upstream unreachable" }));
90
+ return items;
81
91
  }
92
+ if (payload && typeof payload === "object") {
93
+ return [payload as Partial<GuckEvent>];
94
+ }
95
+ return null;
82
96
  };
83
97
 
84
98
  export const guckVitePlugin = (options: GuckVitePluginOptions = {}): Plugin => {
85
- const ingestUrl = options.ingestUrl ?? DEFAULT_INGEST_URL;
86
99
  const configPath = options.configPath ?? process.cwd();
87
- const path = options.path ?? DEFAULT_PATH;
100
+ const routePath = options.path ?? DEFAULT_PATH;
88
101
  const enabled = options.enabled ?? true;
89
102
 
90
103
  return {
@@ -96,7 +109,7 @@ export const guckVitePlugin = (options: GuckVitePluginOptions = {}): Plugin => {
96
109
  }
97
110
  server.middlewares.use(async (req, res, next) => {
98
111
  const url = new URL(req.url ?? "", "http://localhost");
99
- if (url.pathname !== path) {
112
+ if (url.pathname !== routePath) {
100
113
  next();
101
114
  return;
102
115
  }
@@ -115,7 +128,49 @@ export const guckVitePlugin = (options: GuckVitePluginOptions = {}): Plugin => {
115
128
  return;
116
129
  }
117
130
 
118
- await handleProxy(req, res, ingestUrl, configPath);
131
+ const { config, rootDir } = loadConfig({ configPath });
132
+ if (!config.enabled) {
133
+ writeJson(res, 403, { ok: false, error: "Guck disabled" });
134
+ return;
135
+ }
136
+
137
+ const body = await readBody(req, DEFAULT_MAX_BODY_BYTES);
138
+ if (!body.ok) {
139
+ if (body.error === "too_large") {
140
+ writeJson(res, 413, { ok: false, error: "Payload too large" });
141
+ return;
142
+ }
143
+ writeJson(res, 400, { ok: false, error: "Request aborted" });
144
+ return;
145
+ }
146
+
147
+ let parsed: unknown;
148
+ try {
149
+ parsed = JSON.parse(body.data);
150
+ } catch {
151
+ writeJson(res, 400, { ok: false, error: "Invalid JSON" });
152
+ return;
153
+ }
154
+
155
+ const items = coerceItems(parsed);
156
+ if (!items) {
157
+ writeJson(res, 400, { ok: false, error: "Invalid event payload" });
158
+ return;
159
+ }
160
+
161
+ const storeDir = resolveStoreDir(config, rootDir);
162
+ try {
163
+ for (const item of items) {
164
+ const event = buildEvent(item, { service: config.default_service });
165
+ const redacted = redactEvent(config, event);
166
+ await appendEvent(storeDir, redacted);
167
+ }
168
+ } catch {
169
+ writeJson(res, 500, { ok: false, error: "Failed to write event" });
170
+ return;
171
+ }
172
+
173
+ writeJson(res, 200, { ok: true, count: items.length });
119
174
  });
120
175
  },
121
176
  };
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.