@vite-hub/source 0.0.4 → 0.3.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/README.md +267 -96
- package/dist/client.d.ts +27 -10
- package/dist/client.js +99 -15
- package/dist/collection-B8sgxw5-.d.ts +290 -0
- package/dist/collection-C0Rvrx3p.js +297 -0
- package/dist/collection-route-BplWPxHR.js +10 -0
- package/dist/drizzle.d.ts +48 -0
- package/dist/drizzle.js +87 -0
- package/dist/error-diagnostics-ClQKdi7c.js +194 -0
- package/dist/github-D5S0dEZe.js +1312 -0
- package/dist/{glob.js → glob-DmdMVqjx.js} +154 -57
- package/dist/index.d.ts +284 -62
- package/dist/index.js +583 -29
- package/dist/invariant-DolT9EEx.js +22 -0
- package/dist/mcp.d.ts +3 -4
- package/dist/mcp.js +184 -5917
- package/dist/object-CtSHeGQO.js +17 -0
- package/dist/{path-CUhvUREm.js → path-DFlSryRA.js} +90 -40
- package/dist/root-DJNmqCEW.js +11 -0
- package/dist/server.d.ts +24 -11
- package/dist/server.js +97 -26
- package/dist/types-Ln-FxVFn.d.ts +98 -0
- package/dist/vite.d.ts +202 -14
- package/dist/vite.js +1683 -122
- package/package.json +55 -58
- package/dist/auth-Ddw55OUG.js +0 -1261
- package/dist/client-CDfTmtQu.js +0 -7903
- package/dist/collection-B8pXGM2d.js +0 -141
- package/dist/collection-CF2ON__k.d.ts +0 -92
- package/dist/collection-route-Dw7QLFyx.js +0 -6
- package/dist/content/client.d.ts +0 -3
- package/dist/content/client.js +0 -2
- package/dist/content.d.ts +0 -44
- package/dist/content.js +0 -157
- package/dist/file-BSWQg9Qv.js +0 -503
- package/dist/file-EQ3AjVM6.d.ts +0 -20
- package/dist/file.d.ts +0 -2
- package/dist/file.js +0 -2
- package/dist/github.d.ts +0 -17
- package/dist/github.js +0 -1033
- package/dist/glob.d.ts +0 -15
- package/dist/markdown.d.ts +0 -7
- package/dist/markdown.js +0 -10
- package/dist/path-BzmU4Eni.js +0 -36
- package/dist/registry-C8skfCSV.js +0 -120
- package/dist/sse-CE9Q2JqM.js +0 -407
- package/dist/streamableHttp-BszaXLmg.js +0 -491
- package/dist/types-AIBmt_m6.js +0 -6275
- package/dist/types-Bu3WvNX9.d.ts +0 -71
- package/tsconfig.vite.json +0 -6
- package/types/source/fallback.d.ts +0 -1
- package/types/source/index.d.ts +0 -1
package/dist/github.js
DELETED
|
@@ -1,1033 +0,0 @@
|
|
|
1
|
-
import { t as matchesAny } from "./path-CUhvUREm.js";
|
|
2
|
-
import { a as sourceError, i as isSourceError, r as normalizeSourcePath } from "./path-BzmU4Eni.js";
|
|
3
|
-
import { Buffer } from "node:buffer";
|
|
4
|
-
import { createHash } from "node:crypto";
|
|
5
|
-
import { gunzipSync } from "node:zlib";
|
|
6
|
-
import { devNull } from "node:os";
|
|
7
|
-
//#region ../../node_modules/.pnpm/ohash@2.0.12/node_modules/ohash/dist/_chunks/is-equal.mjs
|
|
8
|
-
function serialize(input) {
|
|
9
|
-
if (typeof input === "string") return `'${input}'`;
|
|
10
|
-
return new Serializer().serialize(input);
|
|
11
|
-
}
|
|
12
|
-
const asciiOrder = " _-,;:!?.'\"()[]{}@*/\\&#%`^+<=>|~$0123456789abcdefghijklmnopqrstuvwxyz";
|
|
13
|
-
const asciiWeights = /*@__PURE__*/ (function() {
|
|
14
|
-
const weights = /* @__PURE__ */ new Uint8Array(128);
|
|
15
|
-
for (let i = 0; i < 69; i++) weights[asciiOrder.charCodeAt(i)] = i + 1;
|
|
16
|
-
for (let code = 65; code <= 90; code++) weights[code] = weights[code + 32];
|
|
17
|
-
return weights;
|
|
18
|
-
})();
|
|
19
|
-
function compareStrings(a, b) {
|
|
20
|
-
if (a === b) return 0;
|
|
21
|
-
const length = Math.min(a.length, b.length);
|
|
22
|
-
let tieBreaker = 0;
|
|
23
|
-
for (let i = 0; i < length; i++) {
|
|
24
|
-
const codeA = a.charCodeAt(i);
|
|
25
|
-
const codeB = b.charCodeAt(i);
|
|
26
|
-
if (codeA === codeB) continue;
|
|
27
|
-
const weightA = codeA < 128 && asciiWeights[codeA] ? asciiWeights[codeA] : codeA + 128;
|
|
28
|
-
const weightB = codeB < 128 && asciiWeights[codeB] ? asciiWeights[codeB] : codeB + 128;
|
|
29
|
-
if (weightA !== weightB) return weightA < weightB ? -1 : 1;
|
|
30
|
-
if (tieBreaker === 0) tieBreaker = codeA > codeB ? -1 : 1;
|
|
31
|
-
}
|
|
32
|
-
if (a.length !== b.length) return a.length < b.length ? -1 : 1;
|
|
33
|
-
return tieBreaker;
|
|
34
|
-
}
|
|
35
|
-
const Serializer = /*@__PURE__*/ (function() {
|
|
36
|
-
class Serializer {
|
|
37
|
-
#context = /* @__PURE__ */ new Map();
|
|
38
|
-
compare(a, b) {
|
|
39
|
-
const typeA = typeof a;
|
|
40
|
-
const typeB = typeof b;
|
|
41
|
-
if (typeA === "string" && typeB === "string") return compareStrings(a, b);
|
|
42
|
-
if (typeA === "number" && typeB === "number") return a - b;
|
|
43
|
-
return compareStrings(this.serialize(a, true), this.serialize(b, true));
|
|
44
|
-
}
|
|
45
|
-
serialize(value, noQuotes) {
|
|
46
|
-
if (value === null) return "null";
|
|
47
|
-
switch (typeof value) {
|
|
48
|
-
case "string": return noQuotes ? value : `'${value}'`;
|
|
49
|
-
case "bigint": return `${value}n`;
|
|
50
|
-
case "object": return this.$object(value);
|
|
51
|
-
case "function": return this.$function(value);
|
|
52
|
-
}
|
|
53
|
-
return String(value);
|
|
54
|
-
}
|
|
55
|
-
serializeObject(object) {
|
|
56
|
-
const objString = Object.prototype.toString.call(object);
|
|
57
|
-
if (objString !== "[object Object]") return this.serializeBuiltInType(objString.length < 10 ? `unknown:${objString}` : objString.slice(8, -1), object);
|
|
58
|
-
const constructor = object.constructor;
|
|
59
|
-
const objName = constructor === Object || constructor === void 0 ? "" : constructor.name;
|
|
60
|
-
if (objName !== "" && globalThis[objName] === constructor) return this.serializeBuiltInType(objName, object);
|
|
61
|
-
if ("toJSON" in object && typeof object.toJSON === "function") {
|
|
62
|
-
const json = object.toJSON();
|
|
63
|
-
return objName + (json !== null && typeof json === "object" ? this.$object(json) : `(${this.serialize(json)})`);
|
|
64
|
-
}
|
|
65
|
-
const keys = Object.keys(object).sort(compareStrings);
|
|
66
|
-
let content = `${objName}{`;
|
|
67
|
-
for (let i = 0; i < keys.length; i++) {
|
|
68
|
-
const key = keys[i];
|
|
69
|
-
content += `${key}:${this.serialize(object[key])}`;
|
|
70
|
-
if (i < keys.length - 1) content += ",";
|
|
71
|
-
}
|
|
72
|
-
return content + "}";
|
|
73
|
-
}
|
|
74
|
-
serializeBuiltInType(type, object) {
|
|
75
|
-
const handler = this["$" + type];
|
|
76
|
-
if (handler) return handler.call(this, object);
|
|
77
|
-
if (typeof object.entries === "function") return this.serializeObjectEntries(type, object.entries());
|
|
78
|
-
throw new Error(`Cannot serialize ${type}`);
|
|
79
|
-
}
|
|
80
|
-
serializeObjectEntries(type, entries) {
|
|
81
|
-
const sortedEntries = Array.from(entries).sort((a, b) => this.compare(a[0], b[0]));
|
|
82
|
-
let content = `${type}{`;
|
|
83
|
-
for (let i = 0; i < sortedEntries.length; i++) {
|
|
84
|
-
const [key, value] = sortedEntries[i];
|
|
85
|
-
content += `${this.serialize(key, true)}:${this.serialize(value)}`;
|
|
86
|
-
if (i < sortedEntries.length - 1) content += ",";
|
|
87
|
-
}
|
|
88
|
-
return content + "}";
|
|
89
|
-
}
|
|
90
|
-
$object(object) {
|
|
91
|
-
let content = this.#context.get(object);
|
|
92
|
-
if (content === void 0) {
|
|
93
|
-
this.#context.set(object, `#${this.#context.size}`);
|
|
94
|
-
content = this.serializeObject(object);
|
|
95
|
-
this.#context.set(object, content);
|
|
96
|
-
}
|
|
97
|
-
return content;
|
|
98
|
-
}
|
|
99
|
-
$function(fn) {
|
|
100
|
-
const fnStr = Function.prototype.toString.call(fn);
|
|
101
|
-
if (fnStr.slice(-15) === "[native code] }") return `${fn.name || ""}()[native]`;
|
|
102
|
-
return `${fn.name}(${fn.length})${fnStr.replace(/\s*\n\s*/g, "")}`;
|
|
103
|
-
}
|
|
104
|
-
$Array(arr) {
|
|
105
|
-
let content = "[";
|
|
106
|
-
for (let i = 0; i < arr.length; i++) {
|
|
107
|
-
content += this.serialize(arr[i]);
|
|
108
|
-
if (i < arr.length - 1) content += ",";
|
|
109
|
-
}
|
|
110
|
-
return content + "]";
|
|
111
|
-
}
|
|
112
|
-
$Date(date) {
|
|
113
|
-
try {
|
|
114
|
-
return `Date(${date.toISOString()})`;
|
|
115
|
-
} catch {
|
|
116
|
-
return `Date(null)`;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
$ArrayBuffer(arr) {
|
|
120
|
-
return `ArrayBuffer[${new Uint8Array(arr).join(",")}]`;
|
|
121
|
-
}
|
|
122
|
-
$Set(set) {
|
|
123
|
-
return `Set${this.$Array(Array.from(set).sort((a, b) => this.compare(a, b)))}`;
|
|
124
|
-
}
|
|
125
|
-
$Map(map) {
|
|
126
|
-
return this.serializeObjectEntries("Map", map.entries());
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
for (const type of [
|
|
130
|
-
"Error",
|
|
131
|
-
"RegExp",
|
|
132
|
-
"URL"
|
|
133
|
-
]) Serializer.prototype["$" + type] = function(val) {
|
|
134
|
-
return `${type}(${val})`;
|
|
135
|
-
};
|
|
136
|
-
for (const type of [
|
|
137
|
-
"Int8Array",
|
|
138
|
-
"Uint8Array",
|
|
139
|
-
"Uint8ClampedArray",
|
|
140
|
-
"Int16Array",
|
|
141
|
-
"Uint16Array",
|
|
142
|
-
"Int32Array",
|
|
143
|
-
"Uint32Array",
|
|
144
|
-
"Float32Array",
|
|
145
|
-
"Float64Array"
|
|
146
|
-
]) Serializer.prototype["$" + type] = function(arr) {
|
|
147
|
-
return `${type}[${arr.join(",")}]`;
|
|
148
|
-
};
|
|
149
|
-
for (const type of ["BigInt64Array", "BigUint64Array"]) Serializer.prototype["$" + type] = function(arr) {
|
|
150
|
-
return `${type}[${arr.join("n,")}${arr.length > 0 ? "n" : ""}]`;
|
|
151
|
-
};
|
|
152
|
-
return Serializer;
|
|
153
|
-
})();
|
|
154
|
-
//#endregion
|
|
155
|
-
//#region ../../node_modules/.pnpm/ohash@2.0.12/node_modules/ohash/dist/crypto/node/index.mjs
|
|
156
|
-
const fastHash = /*@__PURE__*/ (() => globalThis.process?.getBuiltinModule?.("crypto")?.hash)();
|
|
157
|
-
const algorithm = "sha256";
|
|
158
|
-
const encoding = "base64url";
|
|
159
|
-
function digest(data) {
|
|
160
|
-
if (fastHash) return fastHash(algorithm, data, encoding);
|
|
161
|
-
const h = createHash(algorithm).update(data);
|
|
162
|
-
return globalThis.process?.versions?.webcontainer ? h.digest().toString(encoding) : h.digest(encoding);
|
|
163
|
-
}
|
|
164
|
-
//#endregion
|
|
165
|
-
//#region ../../node_modules/.pnpm/ohash@2.0.12/node_modules/ohash/dist/index.mjs
|
|
166
|
-
function hash(input) {
|
|
167
|
-
return digest(serialize(input));
|
|
168
|
-
}
|
|
169
|
-
//#endregion
|
|
170
|
-
//#region ../../node_modules/.pnpm/ocache@0.1.5/node_modules/ocache/dist/index.mjs
|
|
171
|
-
function createMemoryStorage() {
|
|
172
|
-
const map = /* @__PURE__ */ new Map();
|
|
173
|
-
const timers = /* @__PURE__ */ new Map();
|
|
174
|
-
return {
|
|
175
|
-
get(key) {
|
|
176
|
-
const entry = map.get(key);
|
|
177
|
-
if (!entry) return null;
|
|
178
|
-
if (entry.expires && Date.now() > entry.expires) {
|
|
179
|
-
map.delete(key);
|
|
180
|
-
_clearTimer(timers, key);
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
return entry.value;
|
|
184
|
-
},
|
|
185
|
-
set(key, value, opts) {
|
|
186
|
-
_clearTimer(timers, key);
|
|
187
|
-
if (value === null || value === void 0) {
|
|
188
|
-
map.delete(key);
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const ttlMs = opts?.ttl ? opts.ttl * 1e3 : void 0;
|
|
192
|
-
map.set(key, {
|
|
193
|
-
value,
|
|
194
|
-
expires: ttlMs ? Date.now() + ttlMs : void 0
|
|
195
|
-
});
|
|
196
|
-
if (ttlMs) {
|
|
197
|
-
const timer = setTimeout(() => {
|
|
198
|
-
map.delete(key);
|
|
199
|
-
timers.delete(key);
|
|
200
|
-
}, ttlMs);
|
|
201
|
-
if (timer && typeof timer === "object" && "unref" in timer) timer.unref();
|
|
202
|
-
timers.set(key, timer);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
function _clearTimer(timers, key) {
|
|
208
|
-
const existing = timers.get(key);
|
|
209
|
-
if (existing !== void 0) {
|
|
210
|
-
clearTimeout(existing);
|
|
211
|
-
timers.delete(key);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
let _storage;
|
|
215
|
-
function useStorage() {
|
|
216
|
-
if (!_storage) _storage = createMemoryStorage();
|
|
217
|
-
return _storage;
|
|
218
|
-
}
|
|
219
|
-
function defaultCacheOptions$1() {
|
|
220
|
-
return {
|
|
221
|
-
name: "_",
|
|
222
|
-
base: "/cache",
|
|
223
|
-
swr: true,
|
|
224
|
-
maxAge: 1
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function defineCachedFunction(fn, opts = {}) {
|
|
228
|
-
opts = {
|
|
229
|
-
...defaultCacheOptions$1(),
|
|
230
|
-
...opts
|
|
231
|
-
};
|
|
232
|
-
const pending = {};
|
|
233
|
-
const group = opts.group || "functions";
|
|
234
|
-
const name = opts.name || fn.name || "_";
|
|
235
|
-
const integrity = opts.integrity || hash([fn, _integrityOpts$1(opts)]);
|
|
236
|
-
const validate = opts.validate || ((entry) => entry.value !== void 0);
|
|
237
|
-
const _onError = (context, error) => {
|
|
238
|
-
if (opts.onError) opts.onError(error);
|
|
239
|
-
else console.error(context, error);
|
|
240
|
-
};
|
|
241
|
-
async function get(key, resolver, shouldInvalidateCache, event) {
|
|
242
|
-
const bases = _normalizeBases(opts.base);
|
|
243
|
-
let entry = {};
|
|
244
|
-
let hitIndex = -1;
|
|
245
|
-
try {
|
|
246
|
-
for (let i = 0; i < bases.length; i++) {
|
|
247
|
-
const result = await useStorage().get(_buildCacheKey(key, {
|
|
248
|
-
group,
|
|
249
|
-
name
|
|
250
|
-
}, bases[i]));
|
|
251
|
-
if (result) {
|
|
252
|
-
entry = result;
|
|
253
|
-
hitIndex = i;
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
} catch (error) {
|
|
258
|
-
_onError("[cache] Cache read error.", error);
|
|
259
|
-
}
|
|
260
|
-
if (typeof entry !== "object") {
|
|
261
|
-
entry = {};
|
|
262
|
-
_onError("[cache]", /* @__PURE__ */ new Error("Malformed data read from cache."));
|
|
263
|
-
}
|
|
264
|
-
const ttl = (opts.maxAge ?? 0) * 1e3;
|
|
265
|
-
if (ttl > 0) entry.expires = Date.now() + ttl;
|
|
266
|
-
const staleTtl = opts.swr && opts.staleMaxAge != null && opts.staleMaxAge >= 0 ? opts.staleMaxAge * 1e3 : void 0;
|
|
267
|
-
const isFullyExpired = staleTtl !== void 0 && ttl > 0 && Date.now() - (entry.mtime || 0) > ttl + staleTtl;
|
|
268
|
-
const expired = shouldInvalidateCache || entry.stale === true || entry.integrity !== integrity || opts.maxAge === 0 || ttl > 0 && Date.now() - (entry.mtime || 0) > ttl || validate(entry) === false;
|
|
269
|
-
if (isFullyExpired) {
|
|
270
|
-
entry.value = void 0;
|
|
271
|
-
entry.integrity = void 0;
|
|
272
|
-
entry.mtime = void 0;
|
|
273
|
-
entry.expires = void 0;
|
|
274
|
-
}
|
|
275
|
-
const _resolve = async () => {
|
|
276
|
-
const isPending = pending[key];
|
|
277
|
-
if (!isPending) {
|
|
278
|
-
if (entry.value !== void 0 && (opts.staleMaxAge || 0) >= 0 && opts.swr === false) {
|
|
279
|
-
entry.value = void 0;
|
|
280
|
-
entry.integrity = void 0;
|
|
281
|
-
entry.mtime = void 0;
|
|
282
|
-
entry.expires = void 0;
|
|
283
|
-
}
|
|
284
|
-
pending[key] = Promise.resolve(resolver());
|
|
285
|
-
}
|
|
286
|
-
try {
|
|
287
|
-
entry.value = await pending[key];
|
|
288
|
-
} catch (error) {
|
|
289
|
-
if (!isPending) {
|
|
290
|
-
delete pending[key];
|
|
291
|
-
const evictPromise = _evictFromStorage(key, bases, group, name).catch((error) => {
|
|
292
|
-
_onError("[cache] Cache eviction error.", error);
|
|
293
|
-
});
|
|
294
|
-
event?.req.waitUntil?.(evictPromise);
|
|
295
|
-
}
|
|
296
|
-
throw error;
|
|
297
|
-
}
|
|
298
|
-
if (!isPending) {
|
|
299
|
-
entry.mtime = Date.now();
|
|
300
|
-
entry.integrity = integrity;
|
|
301
|
-
entry.stale = void 0;
|
|
302
|
-
delete pending[key];
|
|
303
|
-
if (validate(entry) !== false) {
|
|
304
|
-
let setOpts;
|
|
305
|
-
if (opts.maxAge != null && opts.maxAge > 0) if (opts.swr) {
|
|
306
|
-
if (opts.staleMaxAge != null && opts.staleMaxAge >= 0) setOpts = { ttl: opts.maxAge + opts.staleMaxAge };
|
|
307
|
-
} else setOpts = { ttl: opts.maxAge };
|
|
308
|
-
const writeBases = hitIndex < 0 ? bases : bases.slice(0, hitIndex + 1);
|
|
309
|
-
const promise = (async () => {
|
|
310
|
-
try {
|
|
311
|
-
await Promise.all(writeBases.map((b) => useStorage().set(_buildCacheKey(key, {
|
|
312
|
-
group,
|
|
313
|
-
name
|
|
314
|
-
}, b), entry, setOpts)));
|
|
315
|
-
} catch (error) {
|
|
316
|
-
_onError("[cache] Cache write error.", error);
|
|
317
|
-
}
|
|
318
|
-
})();
|
|
319
|
-
event?.req.waitUntil?.(promise);
|
|
320
|
-
} else {
|
|
321
|
-
const evictPromise = _evictFromStorage(key, bases, group, name).catch((error) => {
|
|
322
|
-
_onError("[cache] Cache eviction error.", error);
|
|
323
|
-
});
|
|
324
|
-
event?.req.waitUntil?.(evictPromise);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
const _resolvePromise = expired ? _resolve() : Promise.resolve();
|
|
329
|
-
if (entry.value === void 0) await _resolvePromise;
|
|
330
|
-
else if (expired) event?.req.waitUntil?.(_resolvePromise);
|
|
331
|
-
if (opts.swr && validate(entry) !== false) {
|
|
332
|
-
_resolvePromise.catch((error) => {
|
|
333
|
-
_onError("[cache] SWR handler error.", error);
|
|
334
|
-
});
|
|
335
|
-
return entry;
|
|
336
|
-
}
|
|
337
|
-
return _resolvePromise.then(() => entry);
|
|
338
|
-
}
|
|
339
|
-
const cachedFn = async (...args) => {
|
|
340
|
-
if (await opts.shouldBypassCache?.(...args)) return fn(...args);
|
|
341
|
-
const entry = await get(await (opts.getKey || getKey)(...args), () => fn(...args), await opts.shouldInvalidateCache?.(...args), isHTTPEvent(args[0]) ? args[0] : void 0);
|
|
342
|
-
let value = entry.value;
|
|
343
|
-
if (opts.transform) value = await opts.transform(entry, ...args) || value;
|
|
344
|
-
return value;
|
|
345
|
-
};
|
|
346
|
-
cachedFn.resolveKeys = (...args) => resolveCacheKeys({
|
|
347
|
-
options: opts,
|
|
348
|
-
args
|
|
349
|
-
});
|
|
350
|
-
cachedFn.invalidate = (...args) => invalidateCache({
|
|
351
|
-
options: opts,
|
|
352
|
-
args
|
|
353
|
-
});
|
|
354
|
-
cachedFn.expire = (...args) => expireCache({
|
|
355
|
-
options: opts,
|
|
356
|
-
args
|
|
357
|
-
});
|
|
358
|
-
return cachedFn;
|
|
359
|
-
}
|
|
360
|
-
async function resolveCacheKeys(input = {}) {
|
|
361
|
-
const opts = input.options ?? {};
|
|
362
|
-
const args = input.args ?? [];
|
|
363
|
-
const key = await (opts.getKey || getKey)(...args);
|
|
364
|
-
return _normalizeBases(opts.base).map((base) => _buildCacheKey(key, opts, base));
|
|
365
|
-
}
|
|
366
|
-
async function invalidateCache(input = {}) {
|
|
367
|
-
const keys = await resolveCacheKeys(input);
|
|
368
|
-
const storage = useStorage();
|
|
369
|
-
await Promise.all(keys.map((key) => storage.set(key, null)));
|
|
370
|
-
}
|
|
371
|
-
async function expireCache(input = {}) {
|
|
372
|
-
const opts = input.options ?? {};
|
|
373
|
-
const keys = await resolveCacheKeys(input);
|
|
374
|
-
const storage = useStorage();
|
|
375
|
-
await Promise.all(keys.map(async (key) => {
|
|
376
|
-
const entry = await storage.get(key);
|
|
377
|
-
if (!entry || typeof entry !== "object" || entry.value === void 0) return;
|
|
378
|
-
await storage.set(key, {
|
|
379
|
-
...entry,
|
|
380
|
-
stale: true
|
|
381
|
-
}, _remainingTtl(entry, opts));
|
|
382
|
-
}));
|
|
383
|
-
}
|
|
384
|
-
function isHTTPEvent(input) {
|
|
385
|
-
return input?.req instanceof Request;
|
|
386
|
-
}
|
|
387
|
-
function getKey(...args) {
|
|
388
|
-
return args.length > 0 ? hash(args) : "";
|
|
389
|
-
}
|
|
390
|
-
function _buildCacheKey(key, opts, base) {
|
|
391
|
-
return [
|
|
392
|
-
base,
|
|
393
|
-
opts.group || "functions",
|
|
394
|
-
opts.name || "_",
|
|
395
|
-
key + ".json"
|
|
396
|
-
].filter(Boolean).join(":").replace(/:\/$/, ":index");
|
|
397
|
-
}
|
|
398
|
-
function _normalizeBases(base) {
|
|
399
|
-
if (Array.isArray(base)) return base;
|
|
400
|
-
return [base ?? "/cache"];
|
|
401
|
-
}
|
|
402
|
-
async function _evictFromStorage(key, bases, group, name) {
|
|
403
|
-
await Promise.all(bases.map((b) => useStorage().set(_buildCacheKey(key, {
|
|
404
|
-
group,
|
|
405
|
-
name
|
|
406
|
-
}, b), null)));
|
|
407
|
-
}
|
|
408
|
-
function _remainingTtl(entry, opts) {
|
|
409
|
-
if (!entry.mtime || opts.maxAge == null || opts.maxAge <= 0) return;
|
|
410
|
-
const ttlWindow = opts.swr === false ? opts.maxAge : opts.staleMaxAge != null && opts.staleMaxAge >= 0 ? opts.maxAge + opts.staleMaxAge : void 0;
|
|
411
|
-
if (ttlWindow === void 0) return;
|
|
412
|
-
return { ttl: Math.max(Math.ceil((entry.mtime + ttlWindow * 1e3 - Date.now()) / 1e3), 1) };
|
|
413
|
-
}
|
|
414
|
-
function _integrityOpts$1(opts) {
|
|
415
|
-
const { base: _, group: _g, name: _n, ...rest } = opts;
|
|
416
|
-
return rest;
|
|
417
|
-
}
|
|
418
|
-
//#endregion
|
|
419
|
-
//#region src/sources/github/archive.ts
|
|
420
|
-
function parseGitHubArchive(bytes) {
|
|
421
|
-
const tar = gunzipSync(bytes);
|
|
422
|
-
const files = [];
|
|
423
|
-
let offset = 0;
|
|
424
|
-
let paxPath;
|
|
425
|
-
while (offset + 512 <= tar.length) {
|
|
426
|
-
const header = tar.subarray(offset, offset + 512);
|
|
427
|
-
if (header.every((byte) => byte === 0)) break;
|
|
428
|
-
const name = readTarString(header, 0, 100);
|
|
429
|
-
const prefix = readTarString(header, 345, 155);
|
|
430
|
-
const path = paxPath || [prefix, name].filter(Boolean).join("/");
|
|
431
|
-
const size = Number.parseInt(readTarString(header, 124, 12).trim() || "0", 8) || 0;
|
|
432
|
-
const type = String.fromCharCode(header[156] || 0);
|
|
433
|
-
const contentStart = offset + 512;
|
|
434
|
-
const contentEnd = contentStart + size;
|
|
435
|
-
const content = tar.subarray(contentStart, contentEnd);
|
|
436
|
-
if (type === "x") paxPath = readPaxPath(content);
|
|
437
|
-
else {
|
|
438
|
-
if (type === "0" || type === "\0") {
|
|
439
|
-
const entryPath = stripArchiveRoot(path);
|
|
440
|
-
if (entryPath) files.push({
|
|
441
|
-
content: new Uint8Array(content),
|
|
442
|
-
path: entryPath
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
paxPath = void 0;
|
|
446
|
-
}
|
|
447
|
-
offset = contentStart + Math.ceil(size / 512) * 512;
|
|
448
|
-
}
|
|
449
|
-
return files;
|
|
450
|
-
}
|
|
451
|
-
function readTarString(buffer, offset, length) {
|
|
452
|
-
const slice = buffer.subarray(offset, offset + length);
|
|
453
|
-
const end = slice.indexOf(0);
|
|
454
|
-
return Buffer.from(end === -1 ? slice : slice.subarray(0, end)).toString("utf8");
|
|
455
|
-
}
|
|
456
|
-
function readPaxPath(content) {
|
|
457
|
-
let index = 0;
|
|
458
|
-
while (index < content.length) {
|
|
459
|
-
let space = index;
|
|
460
|
-
while (space < content.length && content[space] !== 32) space++;
|
|
461
|
-
if (space >= content.length) return;
|
|
462
|
-
const length = Number.parseInt(Buffer.from(content.subarray(index, space)).toString("ascii"), 10);
|
|
463
|
-
if (!Number.isFinite(length) || length <= 0 || index + length > content.length) return;
|
|
464
|
-
const record = content.subarray(space + 1, index + length - 1);
|
|
465
|
-
const equals = record.indexOf(61);
|
|
466
|
-
if (equals !== -1 && Buffer.from(record.subarray(0, equals)).toString("utf8") === "path") return Buffer.from(record.subarray(equals + 1)).toString("utf8");
|
|
467
|
-
index += length;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
function stripArchiveRoot(path) {
|
|
471
|
-
const slash = path.indexOf("/");
|
|
472
|
-
if (slash === -1) return;
|
|
473
|
-
return normalizeSourcePath(path.slice(slash + 1));
|
|
474
|
-
}
|
|
475
|
-
//#endregion
|
|
476
|
-
//#region src/sources/github/cache.ts
|
|
477
|
-
function normalizeGitHubCache(options) {
|
|
478
|
-
if (options.cache === false) return;
|
|
479
|
-
if (options.cache && typeof options.cache === "object") return { maxAge: options.cache.maxAge };
|
|
480
|
-
}
|
|
481
|
-
function createGitHubCacheKey(input) {
|
|
482
|
-
return [
|
|
483
|
-
input.kind,
|
|
484
|
-
input.repo,
|
|
485
|
-
input.ref,
|
|
486
|
-
input.root,
|
|
487
|
-
normalizePatternCacheKey(input.include),
|
|
488
|
-
normalizePatternCacheKey(input.ignore),
|
|
489
|
-
input.authScope,
|
|
490
|
-
input.key || ""
|
|
491
|
-
].join(":");
|
|
492
|
-
}
|
|
493
|
-
function githubAuthenticationScope(token) {
|
|
494
|
-
return token ? createHash("sha256").update(token).digest("hex") : "anonymous";
|
|
495
|
-
}
|
|
496
|
-
function normalizePatternCacheKey(value) {
|
|
497
|
-
if (!value) return "";
|
|
498
|
-
return Array.isArray(value) ? value.join(",") : String(value);
|
|
499
|
-
}
|
|
500
|
-
//#endregion
|
|
501
|
-
//#region src/sources/github/client.ts
|
|
502
|
-
async function requestGitHubJson(input) {
|
|
503
|
-
const response = await fetch(input.url, {
|
|
504
|
-
headers: {
|
|
505
|
-
accept: "application/vnd.github+json",
|
|
506
|
-
...input.token ? { authorization: `Bearer ${input.token}` } : {},
|
|
507
|
-
"user-agent": "vitehub-source",
|
|
508
|
-
"x-github-api-version": "2022-11-28"
|
|
509
|
-
},
|
|
510
|
-
signal: input.signal
|
|
511
|
-
});
|
|
512
|
-
if (!response.ok) {
|
|
513
|
-
if (response.status === 404) throw sourceError(`[vitehub] 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.`);
|
|
514
|
-
throw sourceError(`[vitehub] github(${JSON.stringify(input.repo)}) request failed with ${response.status} for ${input.url}.`);
|
|
515
|
-
}
|
|
516
|
-
return await response.json();
|
|
517
|
-
}
|
|
518
|
-
async function fetchGitHubArchive(input) {
|
|
519
|
-
const response = await fetch(`https://codeload.github.com/${input.repo}/tar.gz/${encodeURIComponent(input.ref)}`, {
|
|
520
|
-
headers: {
|
|
521
|
-
...input.token ? { authorization: `Bearer ${input.token}` } : {},
|
|
522
|
-
"user-agent": "vitehub-source"
|
|
523
|
-
},
|
|
524
|
-
signal: input.signal
|
|
525
|
-
});
|
|
526
|
-
if (!response.ok) {
|
|
527
|
-
if (response.status === 404) throw sourceError(`[vitehub] 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.`);
|
|
528
|
-
throw sourceError(`[vitehub] github(${JSON.stringify(input.repo)}) archive request failed with ${response.status}.`);
|
|
529
|
-
}
|
|
530
|
-
return new Uint8Array(await response.arrayBuffer());
|
|
531
|
-
}
|
|
532
|
-
//#endregion
|
|
533
|
-
//#region src/sources/github/git.ts
|
|
534
|
-
function createGitEnv(home, token, baseEnv = process.env) {
|
|
535
|
-
const env = { ...baseEnv };
|
|
536
|
-
for (const key of [
|
|
537
|
-
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
|
|
538
|
-
"GIT_COMMON_DIR",
|
|
539
|
-
"GIT_CONFIG",
|
|
540
|
-
"GIT_CONFIG_COUNT",
|
|
541
|
-
"GIT_CONFIG_PARAMETERS",
|
|
542
|
-
"GIT_DIR",
|
|
543
|
-
"GIT_GRAFT_FILE",
|
|
544
|
-
"GIT_IMPLICIT_WORK_TREE",
|
|
545
|
-
"GIT_INDEX_FILE",
|
|
546
|
-
"GIT_NO_REPLACE_OBJECTS",
|
|
547
|
-
"GIT_OBJECT_DIRECTORY",
|
|
548
|
-
"GIT_PREFIX",
|
|
549
|
-
"GIT_REPLACE_REF_BASE",
|
|
550
|
-
"GIT_SHALLOW_FILE",
|
|
551
|
-
"GIT_WORK_TREE"
|
|
552
|
-
]) delete env[key];
|
|
553
|
-
for (const key of Object.keys(env)) if (/^GIT_CONFIG_(?:KEY|VALUE)_\d+$/.test(key)) delete env[key];
|
|
554
|
-
Object.assign(env, {
|
|
555
|
-
GCM_INTERACTIVE: "Never",
|
|
556
|
-
GIT_ASKPASS: "",
|
|
557
|
-
GIT_CONFIG_COUNT: token ? "1" : "0",
|
|
558
|
-
GIT_CONFIG_GLOBAL: devNull,
|
|
559
|
-
GIT_CONFIG_NOSYSTEM: "1",
|
|
560
|
-
GIT_TERMINAL_PROMPT: "0",
|
|
561
|
-
HOME: home,
|
|
562
|
-
SSH_ASKPASS: "",
|
|
563
|
-
USERPROFILE: home,
|
|
564
|
-
XDG_CONFIG_HOME: home
|
|
565
|
-
});
|
|
566
|
-
if (!token) return env;
|
|
567
|
-
const credential = Buffer.from(`x-access-token:${token}`).toString("base64");
|
|
568
|
-
env.GIT_CONFIG_KEY_0 = "http.https://github.com/.extraheader";
|
|
569
|
-
env.GIT_CONFIG_VALUE_0 = `AUTHORIZATION: basic ${credential}`;
|
|
570
|
-
return env;
|
|
571
|
-
}
|
|
572
|
-
async function runGit(args, options = {}) {
|
|
573
|
-
const { spawn } = await import("node:child_process");
|
|
574
|
-
return await new Promise((resolve, reject) => {
|
|
575
|
-
const child = spawn("git", args, {
|
|
576
|
-
env: options.env,
|
|
577
|
-
signal: options.signal,
|
|
578
|
-
stdio: [
|
|
579
|
-
"pipe",
|
|
580
|
-
"pipe",
|
|
581
|
-
"pipe"
|
|
582
|
-
]
|
|
583
|
-
});
|
|
584
|
-
const stdout = [];
|
|
585
|
-
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
586
|
-
child.stderr.resume();
|
|
587
|
-
child.stdin.on("error", () => {});
|
|
588
|
-
child.stdin.end();
|
|
589
|
-
child.on("error", reject);
|
|
590
|
-
child.on("close", (code) => {
|
|
591
|
-
if (code === 0) {
|
|
592
|
-
resolve(Buffer.concat(stdout));
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
const command = args[0] === "-C" ? args[2] : args[0];
|
|
596
|
-
reject(/* @__PURE__ */ new Error(`git ${command || "command"} exited with ${code ?? "an unknown status"}.`));
|
|
597
|
-
});
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
function isGitSparsePattern(pattern) {
|
|
601
|
-
if (pattern.includes("\0") || pattern.includes("\r") || pattern.includes("\n")) return false;
|
|
602
|
-
if (pattern.split("/").some((segment) => segment === "." || segment === "..")) return false;
|
|
603
|
-
if (!pattern.includes("*")) return !/[?[\]{}()!+@]/.test(pattern);
|
|
604
|
-
if (!pattern.endsWith("/**")) return false;
|
|
605
|
-
return !/[*?[\]{}()!+@]/.test(pattern.slice(0, -3));
|
|
606
|
-
}
|
|
607
|
-
function getGitSparsePatterns(root, include) {
|
|
608
|
-
const patterns = (include ? Array.isArray(include) ? include : [include] : []).map((pattern) => [root, normalizeSourcePath(pattern)].filter(Boolean).join("/")).filter(Boolean);
|
|
609
|
-
if (patterns.some((pattern) => !isGitSparsePattern(pattern))) return;
|
|
610
|
-
if (patterns.length) return [...new Set(patterns)];
|
|
611
|
-
return root && isGitSparsePattern(root) ? [`${root}/**`] : void 0;
|
|
612
|
-
}
|
|
613
|
-
function isGitLfsPointer(content) {
|
|
614
|
-
if (content.byteLength > 64 * 1024) return false;
|
|
615
|
-
const lines = Buffer.from(content).toString("utf8").split(/\r?\n/);
|
|
616
|
-
return lines[0] === "version https://git-lfs.github.com/spec/v1" && lines.some((line) => /^oid sha256:[0-9a-f]{64}$/.test(line)) && lines.some((line) => /^size \d+$/.test(line));
|
|
617
|
-
}
|
|
618
|
-
async function readGitArchiveFiles(dir, input, executeGit, options) {
|
|
619
|
-
input.signal?.throwIfAborted();
|
|
620
|
-
const archive = await executeGit([
|
|
621
|
-
"-C",
|
|
622
|
-
dir,
|
|
623
|
-
"archive",
|
|
624
|
-
"--format=tar.gz",
|
|
625
|
-
"--prefix=archive/",
|
|
626
|
-
input.sha,
|
|
627
|
-
"--",
|
|
628
|
-
...input.sparsePatterns.map((pattern) => {
|
|
629
|
-
return `:(literal)${pattern.endsWith("/**") ? pattern.slice(0, -3) : pattern}`;
|
|
630
|
-
})
|
|
631
|
-
], options);
|
|
632
|
-
return parseGitHubArchive(Buffer.from(archive)).map((entry) => {
|
|
633
|
-
const key = input.keyForRepoPath(entry.path);
|
|
634
|
-
if (!key || !input.shouldInclude(key)) return;
|
|
635
|
-
if (isGitLfsPointer(entry.content)) throw new Error("git archive contained a Git LFS pointer; use the GitHub archive instead.");
|
|
636
|
-
return {
|
|
637
|
-
content: entry.content,
|
|
638
|
-
key,
|
|
639
|
-
path: key,
|
|
640
|
-
ref: input.ref,
|
|
641
|
-
sha: input.sha
|
|
642
|
-
};
|
|
643
|
-
}).filter((file) => Boolean(file));
|
|
644
|
-
}
|
|
645
|
-
async function loadGitArchiveFiles(input, executeGit = runGit) {
|
|
646
|
-
const [{ mkdtemp, rm }, { tmpdir }, { join }] = await Promise.all([
|
|
647
|
-
import("node:fs/promises"),
|
|
648
|
-
import("node:os"),
|
|
649
|
-
import("node:path")
|
|
650
|
-
]);
|
|
651
|
-
const dir = await mkdtemp(join(tmpdir(), "vitehub-github-"));
|
|
652
|
-
try {
|
|
653
|
-
const options = {
|
|
654
|
-
env: createGitEnv(dir, input.token, input.env),
|
|
655
|
-
signal: input.signal
|
|
656
|
-
};
|
|
657
|
-
await executeGit([
|
|
658
|
-
"init",
|
|
659
|
-
"--quiet",
|
|
660
|
-
dir
|
|
661
|
-
], options);
|
|
662
|
-
await executeGit([
|
|
663
|
-
"-C",
|
|
664
|
-
dir,
|
|
665
|
-
"remote",
|
|
666
|
-
"add",
|
|
667
|
-
"origin",
|
|
668
|
-
input.repositoryUrl || `https://github.com/${input.repo}.git`
|
|
669
|
-
], options);
|
|
670
|
-
await executeGit([
|
|
671
|
-
"-C",
|
|
672
|
-
dir,
|
|
673
|
-
"config",
|
|
674
|
-
"remote.origin.promisor",
|
|
675
|
-
"true"
|
|
676
|
-
], options);
|
|
677
|
-
await executeGit([
|
|
678
|
-
"-C",
|
|
679
|
-
dir,
|
|
680
|
-
"config",
|
|
681
|
-
"remote.origin.partialclonefilter",
|
|
682
|
-
"blob:none"
|
|
683
|
-
], options);
|
|
684
|
-
await executeGit([
|
|
685
|
-
"-C",
|
|
686
|
-
dir,
|
|
687
|
-
"fetch",
|
|
688
|
-
"--depth=1",
|
|
689
|
-
"--filter=blob:none",
|
|
690
|
-
"--no-tags",
|
|
691
|
-
"--",
|
|
692
|
-
"origin",
|
|
693
|
-
`+${input.ref}`
|
|
694
|
-
], options);
|
|
695
|
-
const sha = Buffer.from(await executeGit([
|
|
696
|
-
"-C",
|
|
697
|
-
dir,
|
|
698
|
-
"rev-parse",
|
|
699
|
-
"FETCH_HEAD"
|
|
700
|
-
], options)).toString("utf8").trim();
|
|
701
|
-
return await readGitArchiveFiles(dir, {
|
|
702
|
-
...input,
|
|
703
|
-
sha
|
|
704
|
-
}, executeGit, options);
|
|
705
|
-
} finally {
|
|
706
|
-
await rm(dir, {
|
|
707
|
-
force: true,
|
|
708
|
-
recursive: true
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
//#endregion
|
|
713
|
-
//#region src/sources/github/index.ts
|
|
714
|
-
function normalizeGitHubRoot(path = "") {
|
|
715
|
-
return normalizeSourcePath(path).split("/").filter((part) => part && part !== ".").join("/");
|
|
716
|
-
}
|
|
717
|
-
function dedupeProviderPromise(promises, key, load) {
|
|
718
|
-
const promise = promises.get(key);
|
|
719
|
-
if (promise) return promise;
|
|
720
|
-
const nextPromise = load().finally(() => {
|
|
721
|
-
promises.delete(key);
|
|
722
|
-
});
|
|
723
|
-
promises.set(key, nextPromise);
|
|
724
|
-
return nextPromise;
|
|
725
|
-
}
|
|
726
|
-
async function waitForCachedGitHubResult(load, signal) {
|
|
727
|
-
signal?.throwIfAborted();
|
|
728
|
-
const result = load();
|
|
729
|
-
if (!signal) return await result;
|
|
730
|
-
return await new Promise((resolve, reject) => {
|
|
731
|
-
const abort = () => reject(signal.reason);
|
|
732
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
733
|
-
result.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
734
|
-
});
|
|
735
|
-
}
|
|
736
|
-
function github(options) {
|
|
737
|
-
const configuredRef = options.ref;
|
|
738
|
-
const root = normalizeGitHubRoot(options.root || "");
|
|
739
|
-
const sparsePatterns = getGitSparsePatterns(root, options.include);
|
|
740
|
-
let auth;
|
|
741
|
-
const authByContext = /* @__PURE__ */ new WeakMap();
|
|
742
|
-
const providerCache = normalizeGitHubCache(options);
|
|
743
|
-
function resolveAuth() {
|
|
744
|
-
const token = typeof options.auth === "function" ? options.auth() : options.auth;
|
|
745
|
-
return typeof token === "string" && token.length > 0 ? token : void 0;
|
|
746
|
-
}
|
|
747
|
-
function refreshAuth() {
|
|
748
|
-
const nextAuth = resolveAuth();
|
|
749
|
-
if (nextAuth !== auth) auth = nextAuth;
|
|
750
|
-
return auth;
|
|
751
|
-
}
|
|
752
|
-
function contextAuth(ctx) {
|
|
753
|
-
return authByContext.has(ctx) ? authByContext.get(ctx) : refreshAuth();
|
|
754
|
-
}
|
|
755
|
-
function keyForRepoPath(path) {
|
|
756
|
-
const normalized = normalizeSourcePath(path);
|
|
757
|
-
if (!root) return normalized;
|
|
758
|
-
if (!normalized.startsWith(`${root}/`)) return void 0;
|
|
759
|
-
return normalized.slice(root.length + 1);
|
|
760
|
-
}
|
|
761
|
-
function repoPathForKey(key) {
|
|
762
|
-
const normalized = normalizeSourcePath(key);
|
|
763
|
-
return root ? `${root}/${normalized}` : normalized;
|
|
764
|
-
}
|
|
765
|
-
function contentsUrl(repoPath, ref) {
|
|
766
|
-
return `https://api.github.com/repos/${options.repo}/contents/${encodeURIComponent(repoPath).replace(/%2F/g, "/")}?ref=${encodeURIComponent(ref)}`;
|
|
767
|
-
}
|
|
768
|
-
function shouldInclude(key) {
|
|
769
|
-
if (options.include && !matchesAny(key, options.include)) return false;
|
|
770
|
-
if (options.ignore && matchesAny(key, options.ignore)) return false;
|
|
771
|
-
return true;
|
|
772
|
-
}
|
|
773
|
-
async function resolveRef(token = auth, signal) {
|
|
774
|
-
if (configuredRef) return configuredRef;
|
|
775
|
-
return (await resolveSourceRevision(token, signal)).id;
|
|
776
|
-
}
|
|
777
|
-
async function resolveSourceRevision(token = auth, signal) {
|
|
778
|
-
if (!configuredRef) try {
|
|
779
|
-
const ref = (await requestGitHubJson({
|
|
780
|
-
ref: "default",
|
|
781
|
-
repo: options.repo,
|
|
782
|
-
signal,
|
|
783
|
-
token,
|
|
784
|
-
url: `https://api.github.com/repos/${options.repo}`
|
|
785
|
-
})).default_branch || "main";
|
|
786
|
-
const commit = await requestGitHubJson({
|
|
787
|
-
ref,
|
|
788
|
-
repo: options.repo,
|
|
789
|
-
signal,
|
|
790
|
-
token,
|
|
791
|
-
url: `https://api.github.com/repos/${options.repo}/commits/${encodeURIComponent(ref)}`
|
|
792
|
-
});
|
|
793
|
-
return {
|
|
794
|
-
id: commit.sha || ref,
|
|
795
|
-
immutable: Boolean(commit.sha),
|
|
796
|
-
ref
|
|
797
|
-
};
|
|
798
|
-
} catch (error) {
|
|
799
|
-
if (shouldResolveMainFallback(error)) return {
|
|
800
|
-
id: "main",
|
|
801
|
-
immutable: false,
|
|
802
|
-
ref: "main"
|
|
803
|
-
};
|
|
804
|
-
throw error;
|
|
805
|
-
}
|
|
806
|
-
try {
|
|
807
|
-
const commit = await requestGitHubJson({
|
|
808
|
-
ref: configuredRef,
|
|
809
|
-
repo: options.repo,
|
|
810
|
-
signal,
|
|
811
|
-
token,
|
|
812
|
-
url: `https://api.github.com/repos/${options.repo}/commits/${encodeURIComponent(configuredRef)}`
|
|
813
|
-
});
|
|
814
|
-
return {
|
|
815
|
-
id: commit.sha || configuredRef,
|
|
816
|
-
immutable: Boolean(commit.sha),
|
|
817
|
-
ref: configuredRef
|
|
818
|
-
};
|
|
819
|
-
} catch (error) {
|
|
820
|
-
if (shouldResolveMainFallback(error)) return {
|
|
821
|
-
id: configuredRef,
|
|
822
|
-
immutable: false,
|
|
823
|
-
ref: configuredRef
|
|
824
|
-
};
|
|
825
|
-
throw error;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
function shouldResolveMainFallback(error) {
|
|
829
|
-
return isSourceError(error) && error instanceof Error && error.message.includes(" request failed with 403 ");
|
|
830
|
-
}
|
|
831
|
-
const resolvedRefs = /* @__PURE__ */ new Map();
|
|
832
|
-
const cachedResolveRef = providerCache ? defineCachedFunction(async (token) => await resolveRef(token), {
|
|
833
|
-
...providerCache,
|
|
834
|
-
getKey: (token) => cacheKey("ref", token || ""),
|
|
835
|
-
name: "github-source-ref"
|
|
836
|
-
}) : (token) => dedupeProviderPromise(resolvedRefs, cacheKey("ref", token || ""), () => resolveRef(token));
|
|
837
|
-
async function getRef(token = refreshAuth(), signal) {
|
|
838
|
-
return await waitForCachedGitHubResult(() => cachedResolveRef(token), signal);
|
|
839
|
-
}
|
|
840
|
-
async function validateConfiguredRef(token = auth, signal) {
|
|
841
|
-
if (!configuredRef) return;
|
|
842
|
-
await requestGitHubJson({
|
|
843
|
-
ref: configuredRef,
|
|
844
|
-
repo: options.repo,
|
|
845
|
-
signal,
|
|
846
|
-
token,
|
|
847
|
-
url: `https://api.github.com/repos/${options.repo}/commits/${encodeURIComponent(configuredRef)}`
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
async function loadArchiveFiles(token = auth, signal, resolvedRef) {
|
|
851
|
-
const ref = resolvedRef ?? await getRef(token, signal);
|
|
852
|
-
return parseGitHubArchive(await fetchGitHubArchive({
|
|
853
|
-
ref,
|
|
854
|
-
repo: options.repo,
|
|
855
|
-
signal,
|
|
856
|
-
token
|
|
857
|
-
})).map((entry) => {
|
|
858
|
-
const key = keyForRepoPath(entry.path);
|
|
859
|
-
if (!key || !shouldInclude(key)) return void 0;
|
|
860
|
-
return {
|
|
861
|
-
content: entry.content,
|
|
862
|
-
key,
|
|
863
|
-
path: key,
|
|
864
|
-
ref,
|
|
865
|
-
sha: ref
|
|
866
|
-
};
|
|
867
|
-
}).filter((file) => Boolean(file));
|
|
868
|
-
}
|
|
869
|
-
async function loadFiles(token = auth, signal, resolvedRef) {
|
|
870
|
-
const ref = resolvedRef ?? await getRef(token, signal);
|
|
871
|
-
if (sparsePatterns) try {
|
|
872
|
-
return await loadGitArchiveFiles({
|
|
873
|
-
keyForRepoPath,
|
|
874
|
-
ref,
|
|
875
|
-
repo: options.repo,
|
|
876
|
-
shouldInclude,
|
|
877
|
-
signal,
|
|
878
|
-
sparsePatterns,
|
|
879
|
-
token
|
|
880
|
-
});
|
|
881
|
-
} catch (error) {
|
|
882
|
-
if (signal?.aborted) throw error;
|
|
883
|
-
}
|
|
884
|
-
return await loadArchiveFiles(token, signal, ref);
|
|
885
|
-
}
|
|
886
|
-
const loadedFiles = /* @__PURE__ */ new Map();
|
|
887
|
-
const cachedLoadFiles = providerCache ? defineCachedFunction(async (resolvedRef, token) => await loadFiles(token, void 0, resolvedRef), {
|
|
888
|
-
...providerCache,
|
|
889
|
-
getKey: (resolvedRef, token) => cacheKey("archive", token, "", resolvedRef),
|
|
890
|
-
name: "github-source-archive"
|
|
891
|
-
}) : (resolvedRef, token) => dedupeProviderPromise(loadedFiles, cacheKey("archive", token, "", resolvedRef), () => loadFiles(token, void 0, resolvedRef));
|
|
892
|
-
function getFiles(token = refreshAuth(), signal, resolvedRef) {
|
|
893
|
-
return waitForCachedGitHubResult(() => cachedLoadFiles(resolvedRef, token), signal);
|
|
894
|
-
}
|
|
895
|
-
async function loadFileMetadata(key, token = auth, signal, resolvedRef) {
|
|
896
|
-
const normalizedKey = normalizeSourcePath(key);
|
|
897
|
-
if (!normalizedKey || !shouldInclude(normalizedKey)) return;
|
|
898
|
-
const ref = resolvedRef ?? await getRef(token, signal);
|
|
899
|
-
const repoPath = repoPathForKey(normalizedKey);
|
|
900
|
-
let file;
|
|
901
|
-
try {
|
|
902
|
-
file = await requestGitHubJson({
|
|
903
|
-
ref,
|
|
904
|
-
repo: options.repo,
|
|
905
|
-
signal,
|
|
906
|
-
token,
|
|
907
|
-
url: contentsUrl(repoPath, ref)
|
|
908
|
-
});
|
|
909
|
-
} catch (error) {
|
|
910
|
-
if (isGitHubAccessError(error)) {
|
|
911
|
-
await validateConfiguredRef(token, signal);
|
|
912
|
-
return;
|
|
913
|
-
}
|
|
914
|
-
if (shouldResolveMainFallback(error)) return (signal ? await loadArchiveFiles(token, signal, ref) : await getFiles(token, void 0, ref)).find((file) => file.key === normalizedKey);
|
|
915
|
-
throw error;
|
|
916
|
-
}
|
|
917
|
-
if (!file || Array.isArray(file)) return;
|
|
918
|
-
if (file.type === "dir") return;
|
|
919
|
-
return {
|
|
920
|
-
key: normalizedKey,
|
|
921
|
-
path: normalizedKey,
|
|
922
|
-
ref,
|
|
923
|
-
sha: file.sha || ref
|
|
924
|
-
};
|
|
925
|
-
}
|
|
926
|
-
const loadedFileMetadata = /* @__PURE__ */ new Map();
|
|
927
|
-
const cachedLoadFileMetadata = providerCache ? defineCachedFunction(async (key, resolvedRef, token) => await loadFileMetadata(key, token, void 0, resolvedRef), {
|
|
928
|
-
...providerCache,
|
|
929
|
-
getKey: (key, resolvedRef, token) => cacheKey("metadata", token, key, resolvedRef),
|
|
930
|
-
name: "github-source-metadata"
|
|
931
|
-
}) : (key, resolvedRef, token) => dedupeProviderPromise(loadedFileMetadata, cacheKey("metadata", token, key, resolvedRef), () => loadFileMetadata(key, token, void 0, resolvedRef));
|
|
932
|
-
function isGitHubAccessError(error) {
|
|
933
|
-
return isSourceError(error) && error instanceof Error && error.message.includes(" could not access the repository or ref ");
|
|
934
|
-
}
|
|
935
|
-
async function loadFile(key, token = auth, signal, resolvedRef) {
|
|
936
|
-
const normalizedKey = normalizeSourcePath(key);
|
|
937
|
-
if (!normalizedKey || !shouldInclude(normalizedKey)) throw sourceError(`[vitehub] github(${JSON.stringify(options.repo)}) could not find ${JSON.stringify(key)}.`);
|
|
938
|
-
const ref = resolvedRef ?? await getRef(token, signal);
|
|
939
|
-
const repoPath = repoPathForKey(normalizedKey);
|
|
940
|
-
const file = await requestGitHubJson({
|
|
941
|
-
ref,
|
|
942
|
-
repo: options.repo,
|
|
943
|
-
signal,
|
|
944
|
-
token,
|
|
945
|
-
url: contentsUrl(repoPath, ref)
|
|
946
|
-
});
|
|
947
|
-
if (Array.isArray(file) || file.type === "dir") throw sourceError(`[vitehub] github(${JSON.stringify(options.repo)}) expected ${JSON.stringify(key)} to be a file, but it is a directory.`);
|
|
948
|
-
if (file.encoding !== "base64" || typeof file.content !== "string") throw sourceError(`[vitehub] github(${JSON.stringify(options.repo)}) did not include file content for ${JSON.stringify(key)}.`);
|
|
949
|
-
return {
|
|
950
|
-
content: new Uint8Array(Buffer.from(file.content.replace(/\s/g, ""), "base64")),
|
|
951
|
-
key: normalizedKey,
|
|
952
|
-
path: normalizedKey,
|
|
953
|
-
ref,
|
|
954
|
-
sha: file.sha || ref
|
|
955
|
-
};
|
|
956
|
-
}
|
|
957
|
-
async function getFile(key, ctx, token = refreshAuth()) {
|
|
958
|
-
if (ctx?.abortSignal) return await loadFile(key, token, ctx.abortSignal, ctx.revision?.id);
|
|
959
|
-
const file = (await getFiles(token, void 0, ctx?.revision?.id)).find((file) => file.key === key);
|
|
960
|
-
if (!file) throw sourceError(`[vitehub] github(${JSON.stringify(options.repo)}) could not find ${JSON.stringify(key)}.`);
|
|
961
|
-
return file;
|
|
962
|
-
}
|
|
963
|
-
function fetchContent(key, file) {
|
|
964
|
-
if (file.content) return Promise.resolve(file.content);
|
|
965
|
-
throw sourceError(`[vitehub] github(${JSON.stringify(options.repo)}) did not include archive content for ${JSON.stringify(key)}.`);
|
|
966
|
-
}
|
|
967
|
-
function cacheKey(kind, token, key = "", resolvedRef) {
|
|
968
|
-
return createGitHubCacheKey({
|
|
969
|
-
authScope: githubAuthenticationScope(token),
|
|
970
|
-
ignore: options.ignore,
|
|
971
|
-
include: options.include,
|
|
972
|
-
key,
|
|
973
|
-
kind,
|
|
974
|
-
ref: resolvedRef || configuredRef || "default",
|
|
975
|
-
repo: options.repo,
|
|
976
|
-
root
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
return {
|
|
980
|
-
cache: options.cache,
|
|
981
|
-
fingerprint: {
|
|
982
|
-
ignore: options.ignore,
|
|
983
|
-
include: options.include,
|
|
984
|
-
ref: configuredRef || "default",
|
|
985
|
-
repo: options.repo,
|
|
986
|
-
root
|
|
987
|
-
},
|
|
988
|
-
name: "github",
|
|
989
|
-
async resolveRevision(ctx) {
|
|
990
|
-
const token = refreshAuth();
|
|
991
|
-
authByContext.set(ctx, token);
|
|
992
|
-
return await resolveSourceRevision(token, ctx.abortSignal);
|
|
993
|
-
},
|
|
994
|
-
async getKeys(ctx) {
|
|
995
|
-
return (await getFiles(contextAuth(ctx), ctx.abortSignal, ctx.revision?.id)).map((file) => file.key);
|
|
996
|
-
},
|
|
997
|
-
async getItems(ctx) {
|
|
998
|
-
const token = contextAuth(ctx);
|
|
999
|
-
return await Promise.all((await getFiles(token, ctx.abortSignal, ctx.revision?.id)).map(async (file) => ({
|
|
1000
|
-
key: file.key,
|
|
1001
|
-
path: file.path,
|
|
1002
|
-
content: await fetchContent(file.key, file),
|
|
1003
|
-
metadata: {
|
|
1004
|
-
ref: file.ref,
|
|
1005
|
-
sha: file.sha
|
|
1006
|
-
}
|
|
1007
|
-
})));
|
|
1008
|
-
},
|
|
1009
|
-
async getMeta(key, ctx) {
|
|
1010
|
-
const token = contextAuth(ctx);
|
|
1011
|
-
const file = await waitForCachedGitHubResult(() => cachedLoadFileMetadata(key, ctx.revision?.id, token), ctx?.abortSignal);
|
|
1012
|
-
if (!file) return;
|
|
1013
|
-
return {
|
|
1014
|
-
ref: file.ref,
|
|
1015
|
-
sha: file.sha
|
|
1016
|
-
};
|
|
1017
|
-
},
|
|
1018
|
-
async getItem(key, ctx) {
|
|
1019
|
-
const file = await getFile(key, ctx, contextAuth(ctx));
|
|
1020
|
-
return {
|
|
1021
|
-
key: file.key,
|
|
1022
|
-
path: file.path,
|
|
1023
|
-
content: await fetchContent(key, file),
|
|
1024
|
-
metadata: {
|
|
1025
|
-
ref: file.ref,
|
|
1026
|
-
sha: file.sha
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
}
|
|
1030
|
-
};
|
|
1031
|
-
}
|
|
1032
|
-
//#endregion
|
|
1033
|
-
export { github };
|