@vite-hub/source 0.0.1 → 0.0.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.
package/package.json CHANGED
@@ -1,59 +1,91 @@
1
1
  {
2
2
  "name": "@vite-hub/source",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Typed source retrieval primitives for ViteHub.",
5
- "author": "onmax <maximogarciamtnez@gmail.com>",
6
- "license": "Apache-2.0",
5
+ "keywords": [
6
+ "content",
7
+ "files",
8
+ "github",
9
+ "source",
10
+ "vitehub"
11
+ ],
7
12
  "homepage": "https://github.com/vite-hub/vitehub#readme",
13
+ "bugs": "https://github.com/vite-hub/vitehub/issues",
14
+ "license": "Apache-2.0",
15
+ "author": "onmax <maximogarciamtnez@gmail.com>",
8
16
  "repository": {
9
17
  "type": "git",
10
18
  "url": "git+https://github.com/vite-hub/vitehub.git",
11
19
  "directory": "packages/source"
12
20
  },
13
- "bugs": "https://github.com/vite-hub/vitehub/issues",
14
- "keywords": [
15
- "vitehub",
16
- "source",
17
- "content",
18
- "github",
19
- "files"
21
+ "files": [
22
+ "dist",
23
+ "package.json"
20
24
  ],
21
- "sideEffects": false,
22
25
  "type": "module",
26
+ "sideEffects": false,
23
27
  "types": "./dist/index.d.ts",
24
28
  "exports": {
25
- ".": "./dist/index.js",
26
- "./sources": "./dist/sources/index.js",
27
- "./sources/custom": "./dist/sources/custom.js",
28
- "./sources/file": "./dist/sources/file.js",
29
- "./sources/github": "./dist/sources/github.js",
30
- "./sources/glob": "./dist/sources/glob.js",
31
- "./sources/markdown": "./dist/sources/markdown.js",
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ },
33
+ "./sources": {
34
+ "types": "./dist/sources/index.d.ts",
35
+ "import": "./dist/sources/index.js"
36
+ },
37
+ "./sources/custom": {
38
+ "types": "./dist/sources/custom.d.ts",
39
+ "import": "./dist/sources/custom.js"
40
+ },
41
+ "./sources/file": {
42
+ "types": "./dist/sources/file.d.ts",
43
+ "import": "./dist/sources/file.js"
44
+ },
45
+ "./sources/github": {
46
+ "types": "./dist/sources/github.d.ts",
47
+ "import": "./dist/sources/github.js"
48
+ },
49
+ "./sources/glob": {
50
+ "types": "./dist/sources/glob.d.ts",
51
+ "import": "./dist/sources/glob.js"
52
+ },
53
+ "./sources/markdown": {
54
+ "types": "./dist/sources/markdown.d.ts",
55
+ "import": "./dist/sources/markdown.js"
56
+ },
57
+ "./sources/mcp-resources": {
58
+ "types": "./dist/sources/mcp-resources.d.ts",
59
+ "import": "./dist/sources/mcp-resources.js"
60
+ },
32
61
  "./package.json": "./package.json"
33
62
  },
34
- "files": [
35
- "dist",
36
- "package.json"
37
- ],
38
63
  "dependencies": {
64
+ "@modelcontextprotocol/sdk": "^1.29.0",
65
+ "effect": "4.0.0-beta.99",
39
66
  "mrmime": "^2.0.1",
40
- "ocache": "^0.1.4",
67
+ "ocache": "^0.1.5",
41
68
  "pathe": "^2.0.3",
42
- "picomatch": "^4.0.4",
43
- "tinyglobby": "^0.2.16"
69
+ "picomatch": "^4.0.5",
70
+ "tinyglobby": "^0.2.17",
71
+ "@vite-hub/runtime": "0.0.2"
44
72
  },
45
73
  "devDependencies": {
46
- "@types/node": "^25.6.0",
74
+ "@types/node": "^24.13.3",
47
75
  "@types/picomatch": "^4.0.3",
48
- "publint": "^0.3.15",
49
- "tsdown": "^0.21.8",
50
- "typescript": "^6.0.2",
51
- "vitest": "^4.1.4"
76
+ "publint": "^0.3.21",
77
+ "typescript": "^6.0.3",
78
+ "vite-plus": "^0.1.24",
79
+ "vitest": "^4.1.4",
80
+ "@vite-hub/internal": "0.0.1"
81
+ },
82
+ "engines": {
83
+ "node": ">=24.0.0"
52
84
  },
53
85
  "scripts": {
54
- "build": "tsdown --tsconfig tsconfig.build.json",
55
- "dev": "tsdown --watch",
86
+ "build": "vp pack",
87
+ "dev": "vp pack --watch",
56
88
  "typecheck": "tsc --noEmit -p ./tsconfig.json",
57
- "test": "pnpm --filter @vite-hub/source build && vitest run"
89
+ "test": "vp run -t @vite-hub/source#build && vp test"
58
90
  }
59
91
  }
@@ -1,266 +0,0 @@
1
- import { a as SourceError, i as normalizeSourcePath, n as matchesAny } from "./path-C0L1l0Fw.js";
2
- import { defineCachedFunction } from "ocache";
3
- import { Buffer } from "node:buffer";
4
- import { gunzipSync } from "node:zlib";
5
- //#region src/sources/github/archive.ts
6
- function parseGitHubArchive(bytes) {
7
- const tar = gunzipSync(bytes);
8
- const files = [];
9
- let offset = 0;
10
- let paxPath;
11
- while (offset + 512 <= tar.length) {
12
- const header = tar.subarray(offset, offset + 512);
13
- if (header.every((byte) => byte === 0)) break;
14
- const name = readTarString(header, 0, 100);
15
- const prefix = readTarString(header, 345, 155);
16
- const path = paxPath || [prefix, name].filter(Boolean).join("/");
17
- const size = Number.parseInt(readTarString(header, 124, 12).trim() || "0", 8) || 0;
18
- const type = String.fromCharCode(header[156] || 0);
19
- const contentStart = offset + 512;
20
- const contentEnd = contentStart + size;
21
- const content = tar.subarray(contentStart, contentEnd);
22
- if (type === "x") paxPath = readPaxPath(content);
23
- else {
24
- if (type === "0" || type === "\0") {
25
- const entryPath = stripArchiveRoot(path);
26
- if (entryPath) files.push({
27
- content: new Uint8Array(content),
28
- path: entryPath
29
- });
30
- }
31
- paxPath = void 0;
32
- }
33
- offset = contentStart + Math.ceil(size / 512) * 512;
34
- }
35
- return files;
36
- }
37
- function readTarString(buffer, offset, length) {
38
- const slice = buffer.subarray(offset, offset + length);
39
- const end = slice.indexOf(0);
40
- return Buffer.from(end === -1 ? slice : slice.subarray(0, end)).toString("utf8");
41
- }
42
- function readPaxPath(content) {
43
- let index = 0;
44
- while (index < content.length) {
45
- let space = index;
46
- while (space < content.length && content[space] !== 32) space++;
47
- if (space >= content.length) return;
48
- const length = Number.parseInt(Buffer.from(content.subarray(index, space)).toString("ascii"), 10);
49
- if (!Number.isFinite(length) || length <= 0 || index + length > content.length) return;
50
- const record = content.subarray(space + 1, index + length - 1);
51
- const equals = record.indexOf(61);
52
- if (equals !== -1 && Buffer.from(record.subarray(0, equals)).toString("utf8") === "path") return Buffer.from(record.subarray(equals + 1)).toString("utf8");
53
- index += length;
54
- }
55
- }
56
- function stripArchiveRoot(path) {
57
- const slash = path.indexOf("/");
58
- if (slash === -1) return;
59
- return normalizeSourcePath(path.slice(slash + 1));
60
- }
61
- //#endregion
62
- //#region src/sources/github/cache.ts
63
- function normalizeGitHubCache(options) {
64
- if (options.cache === false) return;
65
- if (options.cache && typeof options.cache === "object") return { maxAge: options.cache.maxAge };
66
- }
67
- function createGitHubCacheKey(input) {
68
- return [
69
- input.kind,
70
- input.repo,
71
- input.ref,
72
- input.root,
73
- normalizePatternCacheKey(input.include),
74
- normalizePatternCacheKey(input.exclude),
75
- input.token,
76
- input.key || ""
77
- ].join(":");
78
- }
79
- function normalizePatternCacheKey(value) {
80
- if (!value) return "";
81
- return Array.isArray(value) ? value.join(",") : value;
82
- }
83
- //#endregion
84
- //#region src/sources/github/client.ts
85
- async function requestGitHubJson(input) {
86
- const response = await fetch(input.url, { headers: {
87
- accept: "application/vnd.github+json",
88
- ...input.token ? { authorization: `Bearer ${input.token}` } : {},
89
- "user-agent": "vitehub-source",
90
- "x-github-api-version": "2022-11-28"
91
- } });
92
- if (!response.ok) {
93
- if (response.status === 404) throw new SourceError(`[vitehub] source.github(${JSON.stringify(input.repo)}) could not access the repository or ref ${JSON.stringify(input.ref)}. Check that the repo exists, the ref exists, and auth can access it.`);
94
- throw new SourceError(`[vitehub] source.github(${JSON.stringify(input.repo)}) request failed with ${response.status} for ${input.url}.`);
95
- }
96
- return await response.json();
97
- }
98
- async function fetchGitHubArchive(input) {
99
- const response = await fetch(`https://codeload.github.com/${input.repo}/tar.gz/${encodeURIComponent(input.ref)}`, { headers: {
100
- ...input.token ? { authorization: `Bearer ${input.token}` } : {},
101
- "user-agent": "vitehub-source"
102
- } });
103
- if (!response.ok) {
104
- if (response.status === 404) throw new SourceError(`[vitehub] source.github(${JSON.stringify(input.repo)}) could not access the repository or ref ${JSON.stringify(input.ref)}. Check that the repo exists and the ref exists.`);
105
- throw new SourceError(`[vitehub] source.github(${JSON.stringify(input.repo)}) archive request failed with ${response.status}.`);
106
- }
107
- return new Uint8Array(await response.arrayBuffer());
108
- }
109
- //#endregion
110
- //#region src/sources/github/index.ts
111
- function normalizeGitHubRoot(path = "") {
112
- return normalizeSourcePath(path).split("/").filter((part) => part && part !== ".").join("/");
113
- }
114
- function github(options) {
115
- const configuredRef = options.ref;
116
- const root = normalizeGitHubRoot(options.root || "");
117
- let auth;
118
- const providerCache = normalizeGitHubCache(options);
119
- function resolveAuth() {
120
- const token = typeof options.auth === "function" ? options.auth() : options.auth;
121
- return typeof token === "string" && token.length > 0 ? token : void 0;
122
- }
123
- function refreshAuth() {
124
- const nextAuth = resolveAuth();
125
- if (nextAuth !== auth) auth = nextAuth;
126
- return auth;
127
- }
128
- function keyForRepoPath(path) {
129
- const normalized = normalizeSourcePath(path);
130
- if (!root) return normalized;
131
- if (!normalized.startsWith(`${root}/`)) return void 0;
132
- return normalized.slice(root.length + 1);
133
- }
134
- function shouldInclude(key) {
135
- if (options.include && !matchesAny(key, options.include)) return false;
136
- if (options.exclude && matchesAny(key, options.exclude)) return false;
137
- return true;
138
- }
139
- async function resolveRef(token = auth) {
140
- if (configuredRef) return configuredRef;
141
- try {
142
- const defaultBranch = (await requestGitHubJson({
143
- ref: "default",
144
- repo: options.repo,
145
- token,
146
- url: `https://api.github.com/repos/${options.repo}`
147
- })).default_branch || "main";
148
- return (await requestGitHubJson({
149
- ref: defaultBranch,
150
- repo: options.repo,
151
- token,
152
- url: `https://api.github.com/repos/${options.repo}/commits/${encodeURIComponent(defaultBranch)}`
153
- })).sha || defaultBranch;
154
- } catch (error) {
155
- if (shouldResolveMainFallback(error)) return "main";
156
- throw error;
157
- }
158
- }
159
- function shouldResolveMainFallback(error) {
160
- return error instanceof SourceError && error.message.includes(" request failed with 403 ");
161
- }
162
- const cachedResolveRef = providerCache ? defineCachedFunction(async (token) => await resolveRef(token), {
163
- ...providerCache,
164
- getKey: (token) => cacheKey("ref", token || ""),
165
- name: "github-source-ref"
166
- }) : async (token) => await resolveRef(token);
167
- async function getRef(token = refreshAuth()) {
168
- return await cachedResolveRef(token);
169
- }
170
- async function loadArchiveFiles(token = auth) {
171
- const ref = await getRef(token);
172
- return parseGitHubArchive(await fetchGitHubArchive({
173
- ref,
174
- repo: options.repo,
175
- token
176
- })).map((entry) => {
177
- const key = keyForRepoPath(entry.path);
178
- if (!key || !shouldInclude(key)) return void 0;
179
- return {
180
- content: entry.content,
181
- key,
182
- path: key,
183
- sha: ref
184
- };
185
- }).filter((file) => Boolean(file));
186
- }
187
- async function loadFiles(token = auth) {
188
- return await loadArchiveFiles(token);
189
- }
190
- const cachedLoadFiles = providerCache ? defineCachedFunction(async (token) => await loadFiles(token), {
191
- ...providerCache,
192
- getKey: (token) => cacheKey("archive", token || ""),
193
- name: "github-source-archive"
194
- }) : async (token) => await loadFiles(token);
195
- function getFiles(token = refreshAuth()) {
196
- return cachedLoadFiles(token);
197
- }
198
- function fetchContent(key, file) {
199
- if (file.content) return Promise.resolve(file.content);
200
- throw new SourceError(`[vitehub] source.github(${JSON.stringify(options.repo)}) did not include archive content for ${JSON.stringify(key)}.`);
201
- }
202
- function cacheKey(kind, token, key = "") {
203
- return createGitHubCacheKey({
204
- exclude: options.exclude,
205
- include: options.include,
206
- key,
207
- kind,
208
- ref: configuredRef || "default",
209
- repo: options.repo,
210
- root,
211
- token
212
- });
213
- }
214
- return {
215
- cache: options.cache,
216
- fingerprint: {
217
- exclude: options.exclude,
218
- include: options.include,
219
- ref: configuredRef || "default",
220
- repo: options.repo,
221
- root
222
- },
223
- name: "github",
224
- async getKeys() {
225
- return (await getFiles()).map((file) => file.key);
226
- },
227
- async getItems() {
228
- const token = refreshAuth();
229
- const ref = await getRef(token);
230
- return await Promise.all((await getFiles(token)).map(async (file) => ({
231
- key: file.key,
232
- path: file.path,
233
- content: await fetchContent(file.key, file),
234
- metadata: {
235
- ref,
236
- sha: file.sha
237
- }
238
- })));
239
- },
240
- async getMeta(key) {
241
- const token = refreshAuth();
242
- const file = (await getFiles(token)).find((file) => file.key === key);
243
- if (!file) return;
244
- return {
245
- ref: await getRef(token),
246
- sha: file.sha
247
- };
248
- },
249
- async getItem(key) {
250
- const token = refreshAuth();
251
- const file = (await getFiles(token)).find((file) => file.key === key);
252
- if (!file) throw new SourceError(`[vitehub] source.github(${JSON.stringify(options.repo)}) could not find ${JSON.stringify(key)}.`);
253
- return {
254
- key,
255
- path: file.path,
256
- content: await fetchContent(key, file),
257
- metadata: {
258
- ref: await getRef(token),
259
- sha: file.sha
260
- }
261
- };
262
- }
263
- };
264
- }
265
- //#endregion
266
- export { github as t };
File without changes