@guckdev/vite 0.1.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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
- package/src/index.ts +122 -0
- package/tsconfig.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @guckdev/vite
|
|
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.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add -D @guckdev/vite
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { defineConfig } from "vite";
|
|
16
|
+
import { guckVitePlugin } from "@guckdev/vite";
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [guckVitePlugin()],
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then point the browser SDK at `/guck/emit`.
|
|
24
|
+
|
|
25
|
+
## Options
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
guckVitePlugin({
|
|
29
|
+
ingestUrl: "http://127.0.0.1:7331/guck/emit", // shared MCP ingest
|
|
30
|
+
configPath: "/absolute/path/or/project/root", // defaults to process.cwd()
|
|
31
|
+
path: "/guck/emit", // local Vite endpoint
|
|
32
|
+
enabled: true, // auto-disabled for non-dev
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Notes:
|
|
37
|
+
- 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
|
+
- CORS preflight is handled for browser requests.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
export type GuckVitePluginOptions = {
|
|
3
|
+
ingestUrl?: string;
|
|
4
|
+
configPath?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const guckVitePlugin: (options?: GuckVitePluginOptions) => Plugin;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
const DEFAULT_INGEST_URL = "http://127.0.0.1:7331/guck/emit";
|
|
3
|
+
const DEFAULT_PATH = "/guck/emit";
|
|
4
|
+
const applyCors = (res) => {
|
|
5
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
6
|
+
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
|
7
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
8
|
+
};
|
|
9
|
+
const readBody = async (req) => {
|
|
10
|
+
return await new Promise((resolve, reject) => {
|
|
11
|
+
const chunks = [];
|
|
12
|
+
req.on("data", (chunk) => {
|
|
13
|
+
chunks.push(Buffer.from(chunk));
|
|
14
|
+
});
|
|
15
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
16
|
+
req.on("aborted", () => reject(new Error("aborted")));
|
|
17
|
+
req.on("error", reject);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
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);
|
|
53
|
+
}
|
|
54
|
+
const payload = Buffer.from(await response.arrayBuffer());
|
|
55
|
+
res.end(payload);
|
|
56
|
+
}
|
|
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" }));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
export const guckVitePlugin = (options = {}) => {
|
|
65
|
+
const ingestUrl = options.ingestUrl ?? DEFAULT_INGEST_URL;
|
|
66
|
+
const configPath = options.configPath ?? process.cwd();
|
|
67
|
+
const path = options.path ?? DEFAULT_PATH;
|
|
68
|
+
const enabled = options.enabled ?? true;
|
|
69
|
+
return {
|
|
70
|
+
name: "guck-vite",
|
|
71
|
+
apply: "serve",
|
|
72
|
+
configureServer(server) {
|
|
73
|
+
if (!enabled) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
server.middlewares.use(async (req, res, next) => {
|
|
77
|
+
const url = new URL(req.url ?? "", "http://localhost");
|
|
78
|
+
if (url.pathname !== path) {
|
|
79
|
+
next();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (req.method === "OPTIONS") {
|
|
83
|
+
applyCors(res);
|
|
84
|
+
res.statusCode = 204;
|
|
85
|
+
res.end();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (req.method !== "POST") {
|
|
89
|
+
applyCors(res);
|
|
90
|
+
res.statusCode = 404;
|
|
91
|
+
res.end();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
await handleProxy(req, res, ingestUrl, configPath);
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,C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OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC;IAExC,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,OAAO;QACd,eAAe,CAAC,MAAqB;YACnC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO;YACT,CAAC;YACD,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;gBAC9C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE,kBAAkB,CAAC,CAAC;gBACvD,IAAI,GAAG,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;oBAC1B,IAAI,EAAE,CAAC;oBACP,OAAO;gBACT,CAAC;gBAED,IAAI,GAAG,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;oBAC7B,SAAS,CAAC,GAAG,CAAC,CAAC;oBACf,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;oBACrB,GAAG,CAAC,GAAG,EAAE,CAAC;oBACV,OAAO;gBACT,CAAC;gBAED,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;oBAC1B,SAAS,CAAC,GAAG,CAAC,CAAC;oBACf,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;oBACrB,GAAG,CAAC,GAAG,EAAE,CAAC;oBACV,OAAO;gBACT,CAAC;gBAED,MAAM,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;YACrD,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@guckdev/vite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite plugin for Guck browser ingest",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/tillkolter/guck-mcp"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src",
|
|
20
|
+
"tsconfig.json"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"vite": "^5.0.0 || ^6.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "25.2.2",
|
|
33
|
+
"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
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { Plugin, ViteDevServer } from "vite";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
4
|
+
|
|
5
|
+
export type GuckVitePluginOptions = {
|
|
6
|
+
ingestUrl?: string;
|
|
7
|
+
configPath?: string;
|
|
8
|
+
path?: string;
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const DEFAULT_INGEST_URL = "http://127.0.0.1:7331/guck/emit";
|
|
13
|
+
const DEFAULT_PATH = "/guck/emit";
|
|
14
|
+
|
|
15
|
+
const applyCors = (res: ServerResponse): void => {
|
|
16
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
17
|
+
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
|
18
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
19
|
+
};
|
|
20
|
+
|
|
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
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
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 (
|
|
42
|
+
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
|
+
}
|
|
57
|
+
|
|
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,
|
|
66
|
+
});
|
|
67
|
+
|
|
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);
|
|
73
|
+
}
|
|
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" }));
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const guckVitePlugin = (options: GuckVitePluginOptions = {}): Plugin => {
|
|
85
|
+
const ingestUrl = options.ingestUrl ?? DEFAULT_INGEST_URL;
|
|
86
|
+
const configPath = options.configPath ?? process.cwd();
|
|
87
|
+
const path = options.path ?? DEFAULT_PATH;
|
|
88
|
+
const enabled = options.enabled ?? true;
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
name: "guck-vite",
|
|
92
|
+
apply: "serve",
|
|
93
|
+
configureServer(server: ViteDevServer) {
|
|
94
|
+
if (!enabled) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
server.middlewares.use(async (req, res, next) => {
|
|
98
|
+
const url = new URL(req.url ?? "", "http://localhost");
|
|
99
|
+
if (url.pathname !== path) {
|
|
100
|
+
next();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (req.method === "OPTIONS") {
|
|
105
|
+
applyCors(res);
|
|
106
|
+
res.statusCode = 204;
|
|
107
|
+
res.end();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (req.method !== "POST") {
|
|
112
|
+
applyCors(res);
|
|
113
|
+
res.statusCode = 404;
|
|
114
|
+
res.end();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
await handleProxy(req, res, ingestUrl, configPath);
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"noUncheckedIndexedAccess": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"lib": ["ES2022"],
|
|
17
|
+
"types": ["node"]
|
|
18
|
+
},
|
|
19
|
+
"include": ["src/**/*.ts"]
|
|
20
|
+
}
|