@vite-hub/source 0.0.0 → 0.0.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 +38 -1
- package/dist/chunk-pbuEa-1d.js +13 -0
- package/dist/custom-CMmjCoru.d.ts +6 -0
- package/dist/file-D8j_HGST.d.ts +20 -0
- package/dist/github-Dp-ghUUj.js +266 -0
- package/dist/glob-B_tSBNmd.d.ts +15 -0
- package/dist/index-ChHHP4I8.d.ts +17 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +117 -0
- package/dist/markdown-IpYjHOyM.d.ts +7 -0
- package/dist/path-C0L1l0Fw.js +47 -0
- package/dist/sources/custom.d.ts +2 -0
- package/dist/sources/custom.js +6 -0
- package/dist/sources/file.d.ts +2 -0
- package/dist/sources/file.js +53 -0
- package/dist/sources/github.d.ts +2 -0
- package/dist/sources/github.js +2 -0
- package/dist/sources/glob.d.ts +2 -0
- package/dist/sources/glob.js +103 -0
- package/dist/sources/index.d.ts +12 -0
- package/dist/sources/index.js +16 -0
- package/dist/sources/markdown.d.ts +2 -0
- package/dist/sources/markdown.js +10 -0
- package/dist/types-DgWwkKHY.d.ts +72 -0
- package/package.json +55 -5
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
1
|
# @vite-hub/source
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
|
|
5
|
+
<img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
|
|
6
|
+
<img alt="Sources" src="https://img.shields.io/badge/Source-typed%20retrieval-0f766e?style=flat-square">
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
`@vite-hub/source` loads files, globs, markdown, GitHub content, or custom data before something else places it in a workspace.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add @vite-hub/source
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Minimal API
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// server/utils/sources.ts
|
|
21
|
+
import { defineSources, file, github, glob, registerSources, useSource } from "@vite-hub/source"
|
|
22
|
+
|
|
23
|
+
registerSources(defineSources({
|
|
24
|
+
docs: glob({ include: "docs/**/*.md" }),
|
|
25
|
+
readme: file("README.md"),
|
|
26
|
+
upstream: github({ repo: "vite-hub/vitehub" }),
|
|
27
|
+
}))
|
|
28
|
+
|
|
29
|
+
const docs = useSource("docs", { rootDir: process.cwd() })
|
|
30
|
+
const keys = await docs.keys()
|
|
31
|
+
const first = await docs.read(keys[0]!)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Used by
|
|
35
|
+
|
|
36
|
+
[`@vite-hub/workspace`](../workspace/README.md) materializes Source output into workspace files. Use this package directly when you only need typed source loading and not a workspace file tree.
|
|
37
|
+
|
|
38
|
+
Built on [tinyglobby](https://github.com/SuperchupuDev/tinyglobby), [picomatch](https://github.com/micromatch/picomatch), and [mrmime](https://github.com/lukeed/mrmime).
|
|
39
|
+
|
|
40
|
+
Learn more at [vitehub.dev](https://vitehub.dev).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll as t };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { a as SourceContent, r as Source } from "./types-DgWwkKHY.js";
|
|
2
|
+
|
|
3
|
+
//#region src/sources/file.d.ts
|
|
4
|
+
interface FileSourcePathOptions<TKey extends string = string> {
|
|
5
|
+
path: string;
|
|
6
|
+
workspacePath?: TKey;
|
|
7
|
+
content?: never;
|
|
8
|
+
mediaType?: string;
|
|
9
|
+
}
|
|
10
|
+
interface FileSourceInlineOptions<TKey extends string = string> {
|
|
11
|
+
workspacePath: TKey;
|
|
12
|
+
content: SourceContent;
|
|
13
|
+
mediaType?: string;
|
|
14
|
+
path?: never;
|
|
15
|
+
}
|
|
16
|
+
type FileSourceOptions<TKey extends string = string> = FileSourcePathOptions<TKey> | FileSourceInlineOptions<TKey>;
|
|
17
|
+
type FileSourceInput<TKey extends string = string> = FileSourceOptions<TKey> | TKey;
|
|
18
|
+
declare function file<const TKey extends string = string>(input: FileSourceInput<TKey>): Source<TKey>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { file as i, FileSourceOptions as n, FileSourcePathOptions as r, FileSourceInlineOptions as t };
|
|
@@ -0,0 +1,266 @@
|
|
|
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 };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { r as Source } from "./types-DgWwkKHY.js";
|
|
2
|
+
|
|
3
|
+
//#region src/sources/glob.d.ts
|
|
4
|
+
interface GlobSourceOptions {
|
|
5
|
+
cwd?: string;
|
|
6
|
+
dot?: boolean;
|
|
7
|
+
followSymlinks?: boolean;
|
|
8
|
+
include: string | string[];
|
|
9
|
+
ignore?: string | string[];
|
|
10
|
+
keyCache?: boolean;
|
|
11
|
+
prefix?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function glob<const TKey extends string = string>(options: GlobSourceOptions): Source<TKey>;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { glob as n, GlobSourceOptions as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { i as SourceCacheOptions, r as Source } from "./types-DgWwkKHY.js";
|
|
2
|
+
|
|
3
|
+
//#region src/sources/github/types.d.ts
|
|
4
|
+
interface GitHubSourceOptions {
|
|
5
|
+
repo: string;
|
|
6
|
+
ref?: string;
|
|
7
|
+
root?: string;
|
|
8
|
+
auth?: string | (() => string | undefined);
|
|
9
|
+
include?: string | string[];
|
|
10
|
+
exclude?: string | string[];
|
|
11
|
+
cache?: false | SourceCacheOptions;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/sources/github/index.d.ts
|
|
15
|
+
declare function github<const TKey extends string = string>(options: GitHubSourceOptions): Source<TKey>;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { GitHubSourceOptions as n, github as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { a as SourceContent, c as SourceKey, d as SourceName, f as SourceReader, i as SourceCacheOptions, l as SourceListEntry, m as SourceSearchQuery, n as ReadSourceResult, o as SourceContext, p as SourceSearchHit, r as Source, s as SourceItem, t as ReadSourceOptions, u as SourceMap } from "./types-DgWwkKHY.js";
|
|
2
|
+
import { t as custom } from "./custom-CMmjCoru.js";
|
|
3
|
+
import { i as file, n as FileSourceOptions } from "./file-D8j_HGST.js";
|
|
4
|
+
import { n as GitHubSourceOptions, t as github } from "./index-ChHHP4I8.js";
|
|
5
|
+
import { n as glob, t as GlobSourceOptions } from "./glob-B_tSBNmd.js";
|
|
6
|
+
import { t as markdown } from "./markdown-IpYjHOyM.js";
|
|
7
|
+
import { t as index_d_exports } from "./sources/index.js";
|
|
8
|
+
|
|
9
|
+
//#region src/core/define.d.ts
|
|
10
|
+
declare function defineSource<const TSource extends Source>(source: TSource): TSource;
|
|
11
|
+
declare function defineSources<const TSources extends Record<string, Source>>(sources: TSources): TSources;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/core/registry.d.ts
|
|
14
|
+
declare function registerSource<const TName extends string, const TSource extends Source>(name: TName, source: TSource): TSource;
|
|
15
|
+
declare function registerSources<const TSources extends Record<string, Source>>(sources: TSources): TSources;
|
|
16
|
+
declare function getRegisteredSource<TName extends SourceName>(name: TName): Source<SourceKey<TName>>;
|
|
17
|
+
declare function clearSources(): void;
|
|
18
|
+
declare function useSource<TName extends SourceName>(name: TName, context?: Partial<SourceContext>): SourceReader<TName>;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/core/errors.d.ts
|
|
21
|
+
declare class SourceError extends Error {
|
|
22
|
+
constructor(message: string, options?: ErrorOptions);
|
|
23
|
+
}
|
|
24
|
+
declare class SourceNotFoundError extends SourceError {
|
|
25
|
+
constructor(name: string);
|
|
26
|
+
}
|
|
27
|
+
declare class SourcePathError extends SourceError {
|
|
28
|
+
constructor(path: string);
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { type FileSourceOptions, type GitHubSourceOptions, type GlobSourceOptions, ReadSourceOptions, ReadSourceResult, Source, SourceCacheOptions, SourceContent, SourceContext, SourceError, SourceItem, SourceKey, SourceListEntry, SourceMap, SourceName, SourceNotFoundError, SourcePathError, SourceReader, SourceSearchHit, SourceSearchQuery, clearSources, custom, defineSource, defineSources, file, getRegisteredSource, github, glob, markdown, registerSource, registerSources, index_d_exports as source, useSource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { a as SourceError, i as normalizeSourcePath, o as SourceNotFoundError, r as normalizeSafeSourcePath, s as SourcePathError, t as decodeSourceContent } from "./path-C0L1l0Fw.js";
|
|
2
|
+
import { custom } from "./sources/custom.js";
|
|
3
|
+
import { file } from "./sources/file.js";
|
|
4
|
+
import { t as github } from "./github-Dp-ghUUj.js";
|
|
5
|
+
import { glob } from "./sources/glob.js";
|
|
6
|
+
import { markdown } from "./sources/markdown.js";
|
|
7
|
+
import { t as sources_exports } from "./sources/index.js";
|
|
8
|
+
//#region src/core/define.ts
|
|
9
|
+
function defineSource(source) {
|
|
10
|
+
return source;
|
|
11
|
+
}
|
|
12
|
+
function defineSources(sources) {
|
|
13
|
+
return sources;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/core/registry.ts
|
|
17
|
+
const sourceRegistry = /* @__PURE__ */ new Map();
|
|
18
|
+
function registerSource(name, source) {
|
|
19
|
+
sourceRegistry.set(name, source);
|
|
20
|
+
return source;
|
|
21
|
+
}
|
|
22
|
+
function registerSources(sources) {
|
|
23
|
+
for (const [name, source] of Object.entries(sources)) registerSource(name, source);
|
|
24
|
+
return sources;
|
|
25
|
+
}
|
|
26
|
+
function getRegisteredSource(name) {
|
|
27
|
+
const source = sourceRegistry.get(name);
|
|
28
|
+
if (!source) throw new SourceNotFoundError(name);
|
|
29
|
+
return source;
|
|
30
|
+
}
|
|
31
|
+
function clearSources() {
|
|
32
|
+
sourceRegistry.clear();
|
|
33
|
+
}
|
|
34
|
+
function createSourceContext(name, context = {}) {
|
|
35
|
+
return {
|
|
36
|
+
rootDir: context.rootDir || process.cwd(),
|
|
37
|
+
source: context.source || name,
|
|
38
|
+
sourceRootDir: context.sourceRootDir,
|
|
39
|
+
workspace: context.workspace
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createMissingContentError(key) {
|
|
43
|
+
return new SourceError(`[vitehub] Source item ${JSON.stringify(key)} does not provide readable content.`);
|
|
44
|
+
}
|
|
45
|
+
function isNestedUnder(path, prefix) {
|
|
46
|
+
return !prefix || path === prefix || path.startsWith(`${prefix}/`);
|
|
47
|
+
}
|
|
48
|
+
function createDirectorySet(paths) {
|
|
49
|
+
const directories = /* @__PURE__ */ new Set();
|
|
50
|
+
for (const path of paths) {
|
|
51
|
+
const segments = normalizeSourcePath(path).split("/").filter(Boolean);
|
|
52
|
+
for (let index = 1; index < segments.length; index++) directories.add(segments.slice(0, index).join("/"));
|
|
53
|
+
}
|
|
54
|
+
return directories;
|
|
55
|
+
}
|
|
56
|
+
function useSource(name, context) {
|
|
57
|
+
const source = getRegisteredSource(name);
|
|
58
|
+
const ctx = createSourceContext(name, context);
|
|
59
|
+
let preparePromise;
|
|
60
|
+
async function ensurePrepared() {
|
|
61
|
+
if (!source.prepare) return;
|
|
62
|
+
preparePromise ||= source.prepare(ctx);
|
|
63
|
+
await preparePromise;
|
|
64
|
+
}
|
|
65
|
+
async function keys() {
|
|
66
|
+
await ensurePrepared();
|
|
67
|
+
return await source.getKeys(ctx);
|
|
68
|
+
}
|
|
69
|
+
async function get(key) {
|
|
70
|
+
await ensurePrepared();
|
|
71
|
+
return await source.getItem(key, ctx);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
keys,
|
|
75
|
+
get,
|
|
76
|
+
async read(key, options) {
|
|
77
|
+
const item = await get(key);
|
|
78
|
+
if (typeof item.content === "undefined") throw createMissingContentError(key);
|
|
79
|
+
return decodeSourceContent(item.content, options);
|
|
80
|
+
},
|
|
81
|
+
async meta(key) {
|
|
82
|
+
await ensurePrepared();
|
|
83
|
+
return await source.getMeta?.(key, ctx);
|
|
84
|
+
},
|
|
85
|
+
async exists(key) {
|
|
86
|
+
return (await keys()).includes(key);
|
|
87
|
+
},
|
|
88
|
+
async list(prefix = "") {
|
|
89
|
+
await ensurePrepared();
|
|
90
|
+
const normalizedPrefix = prefix ? normalizeSafeSourcePath(prefix, { allowEmpty: true }) : "";
|
|
91
|
+
const sourceKeys = await keys();
|
|
92
|
+
const directories = createDirectorySet(sourceKeys);
|
|
93
|
+
const result = /* @__PURE__ */ new Map();
|
|
94
|
+
for (const directory of directories) {
|
|
95
|
+
if (directory === normalizedPrefix) continue;
|
|
96
|
+
if (!isNestedUnder(directory, normalizedPrefix)) continue;
|
|
97
|
+
if (normalizeSourcePath(directory.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
|
|
98
|
+
result.set(directory, {
|
|
99
|
+
key: directory,
|
|
100
|
+
type: "directory"
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
for (const key of sourceKeys) {
|
|
104
|
+
if (key === normalizedPrefix) continue;
|
|
105
|
+
if (!isNestedUnder(key, normalizedPrefix)) continue;
|
|
106
|
+
if (normalizeSourcePath(key.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
|
|
107
|
+
result.set(key, {
|
|
108
|
+
key,
|
|
109
|
+
type: "file"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return [...result.values()].sort((left, right) => left.key.localeCompare(right.key));
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
export { SourceError, SourceNotFoundError, SourcePathError, clearSources, custom, defineSource, defineSources, file, getRegisteredSource, github, glob, markdown, registerSource, registerSources, sources_exports as source, useSource };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { r as Source } from "./types-DgWwkKHY.js";
|
|
2
|
+
import { n as FileSourceOptions } from "./file-D8j_HGST.js";
|
|
3
|
+
|
|
4
|
+
//#region src/sources/markdown.d.ts
|
|
5
|
+
declare function markdown<const TKey extends string = string>(options: FileSourceOptions<TKey>): Source<TKey>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { markdown as t };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import picomatch from "picomatch";
|
|
2
|
+
import { isAbsolute } from "pathe";
|
|
3
|
+
//#region src/core/errors.ts
|
|
4
|
+
var SourceError = class extends Error {
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = "SourceError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var SourceNotFoundError = class extends SourceError {
|
|
11
|
+
constructor(name) {
|
|
12
|
+
super(`[vitehub] Source "${name}" is not registered.`);
|
|
13
|
+
this.name = "SourceNotFoundError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var SourcePathError = class extends SourceError {
|
|
17
|
+
constructor(path) {
|
|
18
|
+
super(`[vitehub] Source path escapes the source root: ${JSON.stringify(path)}.`);
|
|
19
|
+
this.name = "SourcePathError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/core/path.ts
|
|
24
|
+
function normalizeSourcePath(path = "") {
|
|
25
|
+
return path.replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "");
|
|
26
|
+
}
|
|
27
|
+
function normalizeSafeSourcePath(path = "", options = {}) {
|
|
28
|
+
const raw = path.replace(/\\/g, "/");
|
|
29
|
+
const normalized = normalizeSourcePath(path);
|
|
30
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
31
|
+
if (!options.allowEmpty && !normalized) throw new SourcePathError(path);
|
|
32
|
+
if (isAbsolute(raw) || parts.some((part) => part === "." || part === "..")) throw new SourcePathError(path);
|
|
33
|
+
if (!options.allowReserved && (parts[0] === ".git" || parts[0] === ".vitehub")) throw new SourcePathError(path);
|
|
34
|
+
return normalized;
|
|
35
|
+
}
|
|
36
|
+
function matchesAny(path, patterns) {
|
|
37
|
+
if (!patterns) return true;
|
|
38
|
+
const list = Array.isArray(patterns) ? patterns : [patterns];
|
|
39
|
+
const normalizedPath = normalizeSourcePath(path);
|
|
40
|
+
return picomatch.isMatch(normalizedPath, list.map((pattern) => normalizeSourcePath(pattern)), { dot: true });
|
|
41
|
+
}
|
|
42
|
+
function decodeSourceContent(content, options) {
|
|
43
|
+
if (options?.encoding === "binary") return typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
44
|
+
return typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { SourceError as a, normalizeSourcePath as i, matchesAny as n, SourceNotFoundError as o, normalizeSafeSourcePath as r, SourcePathError as s, decodeSourceContent as t };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { a as SourceError, i as normalizeSourcePath, r as normalizeSafeSourcePath } from "../path-C0L1l0Fw.js";
|
|
2
|
+
import { lookup } from "mrmime";
|
|
3
|
+
//#region src/sources/file.ts
|
|
4
|
+
function basename(path) {
|
|
5
|
+
return path.replace(/\\/g, "/").split("/").filter(Boolean).pop() || path;
|
|
6
|
+
}
|
|
7
|
+
function normalizeFileSourceOptions(input) {
|
|
8
|
+
return typeof input === "string" ? { path: input } : input;
|
|
9
|
+
}
|
|
10
|
+
function sourceKey(options) {
|
|
11
|
+
if (options.workspacePath) return normalizeSourcePath(options.workspacePath);
|
|
12
|
+
if ("path" in options && options.path) return normalizeSourcePath(basename(normalizeSafeSourcePath(options.path)));
|
|
13
|
+
throw new TypeError("[vitehub] source.file requires a path or workspacePath.");
|
|
14
|
+
}
|
|
15
|
+
function resolveSourceRoot(ctx) {
|
|
16
|
+
return ctx.sourceRootDir || ctx.rootDir;
|
|
17
|
+
}
|
|
18
|
+
async function readSourceFile(options, ctx) {
|
|
19
|
+
if (!("path" in options) || !options.path) throw new TypeError("[vitehub] source.file requires path when content is not provided.");
|
|
20
|
+
const { readFile } = await import("node:fs/promises");
|
|
21
|
+
const { resolve } = await import("node:path");
|
|
22
|
+
return new Uint8Array(await readFile(resolve(resolveSourceRoot(ctx), normalizeSafeSourcePath(options.path))));
|
|
23
|
+
}
|
|
24
|
+
function file(input) {
|
|
25
|
+
const options = normalizeFileSourceOptions(input);
|
|
26
|
+
const key = sourceKey(options);
|
|
27
|
+
const mediaType = options.mediaType || lookup(key);
|
|
28
|
+
return {
|
|
29
|
+
name: "file",
|
|
30
|
+
async getKeys() {
|
|
31
|
+
return [key];
|
|
32
|
+
},
|
|
33
|
+
async getMeta(requestedKey, ctx) {
|
|
34
|
+
if (requestedKey !== key) return;
|
|
35
|
+
if (!("path" in options) || !options.path) return;
|
|
36
|
+
const { stat } = await import("node:fs/promises");
|
|
37
|
+
const { resolve } = await import("node:path");
|
|
38
|
+
const info = await stat(resolve(resolveSourceRoot(ctx), normalizeSafeSourcePath(options.path)));
|
|
39
|
+
return { digest: `${info.size}:${info.mtimeMs}` };
|
|
40
|
+
},
|
|
41
|
+
async getItem(requestedKey, ctx) {
|
|
42
|
+
if (requestedKey !== key) throw new SourceError(`[vitehub] source.file could not find ${JSON.stringify(requestedKey)}.`);
|
|
43
|
+
return {
|
|
44
|
+
key,
|
|
45
|
+
path: key,
|
|
46
|
+
content: typeof options.content === "undefined" ? await readSourceFile(options, ctx) : options.content,
|
|
47
|
+
mediaType
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { file };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { a as SourceError, i as normalizeSourcePath, n as matchesAny, r as normalizeSafeSourcePath } from "../path-C0L1l0Fw.js";
|
|
2
|
+
import { glob as glob$1 } from "tinyglobby";
|
|
3
|
+
//#region src/sources/glob.ts
|
|
4
|
+
function glob(options) {
|
|
5
|
+
let snapshot;
|
|
6
|
+
let latest;
|
|
7
|
+
async function getContextKey(ctx) {
|
|
8
|
+
return resolveCwd(resolveSourceRoot(ctx), options.cwd);
|
|
9
|
+
}
|
|
10
|
+
async function loadKeys(ctx) {
|
|
11
|
+
const cwd = await resolveCwd(resolveSourceRoot(ctx), options.cwd);
|
|
12
|
+
const ignore = normalizePatterns(options.ignore);
|
|
13
|
+
return (await glob$1(options.include, {
|
|
14
|
+
cwd,
|
|
15
|
+
dot: options.dot ?? false,
|
|
16
|
+
followSymbolicLinks: options.followSymlinks ?? false,
|
|
17
|
+
ignore: [
|
|
18
|
+
"**/.git/**",
|
|
19
|
+
"**/node_modules/**",
|
|
20
|
+
...ignore
|
|
21
|
+
],
|
|
22
|
+
onlyFiles: true
|
|
23
|
+
})).map((file) => normalizeSourcePath(file)).filter((file) => matchesAny(file, options.include) && !matchesAny(file, ignore)).sort((left, right) => left.localeCompare(right));
|
|
24
|
+
}
|
|
25
|
+
async function refreshKeys(ctx) {
|
|
26
|
+
const key = await getContextKey(ctx);
|
|
27
|
+
const keys = await loadKeys(ctx);
|
|
28
|
+
latest = {
|
|
29
|
+
key,
|
|
30
|
+
keys
|
|
31
|
+
};
|
|
32
|
+
return keys;
|
|
33
|
+
}
|
|
34
|
+
async function getCachedKeys(ctx) {
|
|
35
|
+
if (options.keyCache === false) return await refreshKeys(ctx);
|
|
36
|
+
const key = await getContextKey(ctx);
|
|
37
|
+
if (!snapshot || snapshot.key !== key) snapshot = {
|
|
38
|
+
key,
|
|
39
|
+
keys: refreshKeys(ctx)
|
|
40
|
+
};
|
|
41
|
+
return await snapshot.keys;
|
|
42
|
+
}
|
|
43
|
+
async function getKnownKeys(ctx) {
|
|
44
|
+
const key = await getContextKey(ctx);
|
|
45
|
+
if (latest?.key === key) return latest.keys;
|
|
46
|
+
return await getCachedKeys(ctx);
|
|
47
|
+
}
|
|
48
|
+
async function assertKey(key, ctx) {
|
|
49
|
+
if ((await getKnownKeys(ctx)).includes(key)) return;
|
|
50
|
+
if (options.keyCache !== false) throw new SourceError(`[vitehub] source.glob could not find ${JSON.stringify(key)}.`);
|
|
51
|
+
if ((await refreshKeys(ctx)).includes(key)) return;
|
|
52
|
+
throw new SourceError(`[vitehub] source.glob could not find ${JSON.stringify(key)}.`);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
name: "glob",
|
|
56
|
+
async prepare(ctx) {
|
|
57
|
+
snapshot = void 0;
|
|
58
|
+
await getCachedKeys(ctx);
|
|
59
|
+
},
|
|
60
|
+
getKeys(ctx) {
|
|
61
|
+
return getCachedKeys(ctx);
|
|
62
|
+
},
|
|
63
|
+
async getMeta(key, ctx) {
|
|
64
|
+
await assertKey(key, ctx);
|
|
65
|
+
const { stat } = await import("node:fs/promises");
|
|
66
|
+
const { resolve } = await import("node:path");
|
|
67
|
+
const info = await stat(resolve(await resolveCwd(resolveSourceRoot(ctx), options.cwd), key));
|
|
68
|
+
return { digest: `${info.size}:${info.mtimeMs}` };
|
|
69
|
+
},
|
|
70
|
+
async getItem(key, ctx) {
|
|
71
|
+
await assertKey(key, ctx);
|
|
72
|
+
const { readFile, stat } = await import("node:fs/promises");
|
|
73
|
+
const { resolve } = await import("node:path");
|
|
74
|
+
const cwd = await resolveCwd(resolveSourceRoot(ctx), options.cwd);
|
|
75
|
+
const prefix = normalizeSafeSourcePath(options.prefix || "", { allowEmpty: true });
|
|
76
|
+
const bytes = await readFile(resolve(cwd, key));
|
|
77
|
+
const info = await stat(resolve(cwd, key));
|
|
78
|
+
return {
|
|
79
|
+
key,
|
|
80
|
+
path: normalizeSourcePath(prefix ? `${prefix}/${key}` : key),
|
|
81
|
+
content: new Uint8Array(bytes),
|
|
82
|
+
metadata: { mtime: info.mtimeMs }
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function normalizePatterns(patterns) {
|
|
88
|
+
return Array.isArray(patterns) ? patterns : patterns ? [patterns] : [];
|
|
89
|
+
}
|
|
90
|
+
function resolveSourceRoot(ctx) {
|
|
91
|
+
return ctx.sourceRootDir || ctx.rootDir;
|
|
92
|
+
}
|
|
93
|
+
async function resolveCwd(rootDir, cwd = ".") {
|
|
94
|
+
const { realpath } = await import("node:fs/promises");
|
|
95
|
+
const { isAbsolute, relative, resolve } = await import("node:path");
|
|
96
|
+
const resolvedRoot = await realpath(resolve(rootDir));
|
|
97
|
+
const resolvedCwd = await realpath(isAbsolute(cwd) ? resolve(cwd) : resolve(resolvedRoot, cwd));
|
|
98
|
+
const rel = relative(resolvedRoot, resolvedCwd);
|
|
99
|
+
if (rel && (rel.startsWith("..") || isAbsolute(rel))) throw new SourceError(`[vitehub] source.glob cwd escapes the source root: ${JSON.stringify(cwd)}.`);
|
|
100
|
+
return resolvedCwd;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
export { glob };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { t as custom } from "../custom-CMmjCoru.js";
|
|
2
|
+
import { i as file, n as FileSourceOptions } from "../file-D8j_HGST.js";
|
|
3
|
+
import { n as GitHubSourceOptions, t as github } from "../index-ChHHP4I8.js";
|
|
4
|
+
import { n as glob, t as GlobSourceOptions } from "../glob-B_tSBNmd.js";
|
|
5
|
+
import { t as markdown } from "../markdown-IpYjHOyM.js";
|
|
6
|
+
|
|
7
|
+
//#region src/sources/index.d.ts
|
|
8
|
+
declare namespace index_d_exports {
|
|
9
|
+
export { FileSourceOptions, GitHubSourceOptions, GlobSourceOptions, custom, file, github, glob, markdown };
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type FileSourceOptions, type GitHubSourceOptions, type GlobSourceOptions, custom, file, github, glob, markdown, index_d_exports as t };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { t as __exportAll } from "../chunk-pbuEa-1d.js";
|
|
2
|
+
import { custom } from "./custom.js";
|
|
3
|
+
import { file } from "./file.js";
|
|
4
|
+
import { t as github } from "../github-Dp-ghUUj.js";
|
|
5
|
+
import { glob } from "./glob.js";
|
|
6
|
+
import { markdown } from "./markdown.js";
|
|
7
|
+
//#region src/sources/index.ts
|
|
8
|
+
var sources_exports = /* @__PURE__ */ __exportAll({
|
|
9
|
+
custom: () => custom,
|
|
10
|
+
file: () => file,
|
|
11
|
+
github: () => github,
|
|
12
|
+
glob: () => glob,
|
|
13
|
+
markdown: () => markdown
|
|
14
|
+
});
|
|
15
|
+
//#endregion
|
|
16
|
+
export { custom, file, github, glob, markdown, sources_exports as t };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/core/types.d.ts
|
|
2
|
+
type SourceContent = string | Uint8Array;
|
|
3
|
+
interface ReadSourceOptions {
|
|
4
|
+
encoding?: "utf8" | "binary";
|
|
5
|
+
}
|
|
6
|
+
type ReadSourceResult<TOptions extends ReadSourceOptions | undefined = undefined> = TOptions extends {
|
|
7
|
+
encoding: "binary";
|
|
8
|
+
} ? Uint8Array : string;
|
|
9
|
+
interface SourceContext {
|
|
10
|
+
rootDir: string;
|
|
11
|
+
sourceRootDir?: string;
|
|
12
|
+
source?: string;
|
|
13
|
+
workspace?: string;
|
|
14
|
+
}
|
|
15
|
+
interface SourceCacheOptions {
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
}
|
|
18
|
+
interface SourceSearchQuery {
|
|
19
|
+
pattern: string;
|
|
20
|
+
cwd?: string;
|
|
21
|
+
paths?: string[];
|
|
22
|
+
regex?: boolean;
|
|
23
|
+
caseSensitive?: boolean;
|
|
24
|
+
limit?: number;
|
|
25
|
+
}
|
|
26
|
+
interface SourceSearchHit {
|
|
27
|
+
path: string;
|
|
28
|
+
line: number;
|
|
29
|
+
column: number;
|
|
30
|
+
text: string;
|
|
31
|
+
}
|
|
32
|
+
interface SourceItem<TKey extends string = string> {
|
|
33
|
+
key: TKey;
|
|
34
|
+
path?: string;
|
|
35
|
+
content?: SourceContent;
|
|
36
|
+
data?: unknown;
|
|
37
|
+
mediaType?: string;
|
|
38
|
+
metadata?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
interface Source<TKey extends string = string> {
|
|
41
|
+
name: string;
|
|
42
|
+
cache?: false | SourceCacheOptions;
|
|
43
|
+
fingerprint?: unknown;
|
|
44
|
+
prepare?(ctx: SourceContext): Promise<void>;
|
|
45
|
+
getKeys(ctx: SourceContext): Promise<TKey[]>;
|
|
46
|
+
getItem(key: TKey, ctx: SourceContext): Promise<SourceItem<TKey>>;
|
|
47
|
+
getItems?(ctx: SourceContext): Promise<SourceItem<TKey>[]>;
|
|
48
|
+
getMeta?(key: TKey, ctx: SourceContext): Promise<Record<string, unknown> | undefined>;
|
|
49
|
+
search?(query: SourceSearchQuery, ctx: SourceContext): Promise<SourceSearchHit[]>;
|
|
50
|
+
watch?: unknown[];
|
|
51
|
+
}
|
|
52
|
+
interface SourceListEntry<TKey extends string = string> {
|
|
53
|
+
key: TKey;
|
|
54
|
+
type: "file" | "directory";
|
|
55
|
+
}
|
|
56
|
+
declare global {
|
|
57
|
+
interface ViteHubSourceMap {}
|
|
58
|
+
}
|
|
59
|
+
interface SourceMap extends ViteHubSourceMap {}
|
|
60
|
+
type SourceKeyOf<TSource> = TSource extends Source<infer TKey> ? TKey : string;
|
|
61
|
+
type SourceName = [keyof ViteHubSourceMap] extends [never] ? string : Extract<keyof ViteHubSourceMap, string>;
|
|
62
|
+
type SourceKey<TName extends SourceName = SourceName> = TName extends keyof ViteHubSourceMap ? Extract<SourceKeyOf<ViteHubSourceMap[TName]>, string> : string;
|
|
63
|
+
interface SourceReader<TName extends SourceName = SourceName> {
|
|
64
|
+
keys(): Promise<SourceKey<TName>[]>;
|
|
65
|
+
get(key: SourceKey<TName>): Promise<SourceItem<SourceKey<TName>>>;
|
|
66
|
+
read<TOptions extends ReadSourceOptions | undefined = undefined>(key: SourceKey<TName>, options?: TOptions): Promise<ReadSourceResult<TOptions>>;
|
|
67
|
+
meta(key: SourceKey<TName>): Promise<Record<string, unknown> | undefined>;
|
|
68
|
+
exists(key: SourceKey<TName>): Promise<boolean>;
|
|
69
|
+
list(prefix?: SourceKey<TName> | (string & {}) | ""): Promise<SourceListEntry<SourceKey<TName>>[]>;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
export { SourceContent as a, SourceKey as c, SourceName as d, SourceReader as f, SourceCacheOptions as i, SourceListEntry as l, SourceSearchQuery as m, ReadSourceResult as n, SourceContext as o, SourceSearchHit as p, Source as r, SourceItem as s, ReadSourceOptions as t, SourceMap as u };
|
package/package.json
CHANGED
|
@@ -1,9 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/source",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Typed source retrieval primitives for ViteHub.",
|
|
5
|
+
"author": "onmax <maximogarciamtnez@gmail.com>",
|
|
5
6
|
"license": "Apache-2.0",
|
|
6
|
-
"
|
|
7
|
-
|
|
7
|
+
"homepage": "https://github.com/vite-hub/vitehub#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/vite-hub/vitehub.git",
|
|
11
|
+
"directory": "packages/source"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/vite-hub/vitehub/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"vitehub",
|
|
16
|
+
"source",
|
|
17
|
+
"content",
|
|
18
|
+
"github",
|
|
19
|
+
"files"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"type": "module",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"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",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"package.json"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"mrmime": "^2.0.1",
|
|
40
|
+
"ocache": "^0.1.4",
|
|
41
|
+
"pathe": "^2.0.3",
|
|
42
|
+
"picomatch": "^4.0.4",
|
|
43
|
+
"tinyglobby": "^0.2.16"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^25.6.0",
|
|
47
|
+
"@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"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsdown --tsconfig tsconfig.build.json",
|
|
55
|
+
"dev": "tsdown --watch",
|
|
56
|
+
"typecheck": "tsc --noEmit -p ./tsconfig.json",
|
|
57
|
+
"test": "pnpm --filter @vite-hub/source build && vitest run"
|
|
8
58
|
}
|
|
9
|
-
}
|
|
59
|
+
}
|