@vite-hub/auth 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +79 -34
- package/dist/agent.d.ts +1 -0
- package/dist/agent.js +1 -1
- package/dist/host-declarations.d.ts +13 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +6 -1
- package/dist/nuxt.d.ts +38 -0
- package/dist/nuxt.js +61 -0
- package/dist/runtime/empty-definition.d.ts +1 -1
- package/dist/runtime/nuxt.d.ts +4 -0
- package/dist/runtime/nuxt.js +8 -0
- package/dist/{server-Diw3mAbX.js → server-BZn8iQrB.js} +51 -3
- package/dist/server.d.ts +5 -2
- package/dist/server.js +2 -2
- package/dist/{types-B0fg9XFd.d.ts → types-jaRlW3D1.d.ts} +12 -1
- package/dist/vite-Cmey8ODS.js +1018 -0
- package/dist/vite.d.ts +18 -3
- package/dist/vite.js +2 -995
- package/dist/vue.d.ts +38 -0
- package/dist/vue.js +34 -0
- package/package.json +20 -5
package/dist/vite.js
CHANGED
|
@@ -1,995 +1,2 @@
|
|
|
1
|
-
import { n as
|
|
2
|
-
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
import { Readable } from "node:stream";
|
|
5
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
7
|
-
//#region ../internal/src/build/vite.ts
|
|
8
|
-
const generatedViteHubFilesPattern = "**/.vitehub/**";
|
|
9
|
-
[
|
|
10
|
-
".ts",
|
|
11
|
-
".mts",
|
|
12
|
-
".cts",
|
|
13
|
-
".js",
|
|
14
|
-
".mjs",
|
|
15
|
-
".cjs"
|
|
16
|
-
].flatMap((extension) => [["server", `email${extension}`], [`server.email${extension}`]]);
|
|
17
|
-
function createNoExternalMerger(packageName) {
|
|
18
|
-
return (current) => {
|
|
19
|
-
if (current === true) return true;
|
|
20
|
-
if (!current) return [packageName];
|
|
21
|
-
const values = Array.isArray(current) ? current : [current];
|
|
22
|
-
return values.includes(packageName) ? values : [...values, packageName];
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function isServerEnvironment(name, config) {
|
|
26
|
-
return name === "ssr" || config.consumer === "server";
|
|
27
|
-
}
|
|
28
|
-
function mergeGeneratedViteHubWatchIgnored(ignored) {
|
|
29
|
-
if (!ignored) return [generatedViteHubFilesPattern];
|
|
30
|
-
if (Array.isArray(ignored)) return ignored.includes(generatedViteHubFilesPattern) ? ignored : [...ignored, generatedViteHubFilesPattern];
|
|
31
|
-
return [ignored, generatedViteHubFilesPattern];
|
|
32
|
-
}
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region ../../node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs
|
|
35
|
-
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
36
|
-
function normalizeWindowsPath(input = "") {
|
|
37
|
-
if (!input) return input;
|
|
38
|
-
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
39
|
-
}
|
|
40
|
-
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
41
|
-
function cwd() {
|
|
42
|
-
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
43
|
-
return "/";
|
|
44
|
-
}
|
|
45
|
-
const resolve$1 = function(...arguments_) {
|
|
46
|
-
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
47
|
-
let resolvedPath = "";
|
|
48
|
-
let resolvedAbsolute = false;
|
|
49
|
-
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
50
|
-
const path = index >= 0 ? arguments_[index] : cwd();
|
|
51
|
-
if (!path || path.length === 0) continue;
|
|
52
|
-
resolvedPath = `${path}/${resolvedPath}`;
|
|
53
|
-
resolvedAbsolute = isAbsolute(path);
|
|
54
|
-
}
|
|
55
|
-
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
56
|
-
if (resolvedAbsolute && !isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
57
|
-
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
58
|
-
};
|
|
59
|
-
function normalizeString(path, allowAboveRoot) {
|
|
60
|
-
let res = "";
|
|
61
|
-
let lastSegmentLength = 0;
|
|
62
|
-
let lastSlash = -1;
|
|
63
|
-
let dots = 0;
|
|
64
|
-
let char = null;
|
|
65
|
-
for (let index = 0; index <= path.length; ++index) {
|
|
66
|
-
if (index < path.length) char = path[index];
|
|
67
|
-
else if (char === "/") break;
|
|
68
|
-
else char = "/";
|
|
69
|
-
if (char === "/") {
|
|
70
|
-
if (lastSlash === index - 1 || dots === 1);
|
|
71
|
-
else if (dots === 2) {
|
|
72
|
-
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
73
|
-
if (res.length > 2) {
|
|
74
|
-
const lastSlashIndex = res.lastIndexOf("/");
|
|
75
|
-
if (lastSlashIndex === -1) {
|
|
76
|
-
res = "";
|
|
77
|
-
lastSegmentLength = 0;
|
|
78
|
-
} else {
|
|
79
|
-
res = res.slice(0, lastSlashIndex);
|
|
80
|
-
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
81
|
-
}
|
|
82
|
-
lastSlash = index;
|
|
83
|
-
dots = 0;
|
|
84
|
-
continue;
|
|
85
|
-
} else if (res.length > 0) {
|
|
86
|
-
res = "";
|
|
87
|
-
lastSegmentLength = 0;
|
|
88
|
-
lastSlash = index;
|
|
89
|
-
dots = 0;
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (allowAboveRoot) {
|
|
94
|
-
res += res.length > 0 ? "/.." : "..";
|
|
95
|
-
lastSegmentLength = 2;
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
99
|
-
else res = path.slice(lastSlash + 1, index);
|
|
100
|
-
lastSegmentLength = index - lastSlash - 1;
|
|
101
|
-
}
|
|
102
|
-
lastSlash = index;
|
|
103
|
-
dots = 0;
|
|
104
|
-
} else if (char === "." && dots !== -1) ++dots;
|
|
105
|
-
else dots = -1;
|
|
106
|
-
}
|
|
107
|
-
return res;
|
|
108
|
-
}
|
|
109
|
-
const isAbsolute = function(p) {
|
|
110
|
-
return _IS_ABSOLUTE_RE.test(p);
|
|
111
|
-
};
|
|
112
|
-
//#endregion
|
|
113
|
-
//#region ../internal/src/definition-catalog.ts
|
|
114
|
-
async function writeFileIfChanged(file, contents) {
|
|
115
|
-
if (await readFile(file, "utf8").catch(() => void 0) === contents) return;
|
|
116
|
-
await mkdir(resolve$1(file, ".."), { recursive: true });
|
|
117
|
-
await writeFile(file, contents, "utf8");
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region ../internal/src/source-scanner.ts
|
|
121
|
-
function isQuote(char) {
|
|
122
|
-
return char === "\"" || char === "'" || char === "`";
|
|
123
|
-
}
|
|
124
|
-
function skipQuoted(source, index, controlFlowRegexes = /* @__PURE__ */ new Map()) {
|
|
125
|
-
const quote = source[index];
|
|
126
|
-
if (quote === "`") return skipTemplateLiteral(source, index, controlFlowRegexes);
|
|
127
|
-
index += 1;
|
|
128
|
-
while (index < source.length) {
|
|
129
|
-
if (source[index] === "\\") {
|
|
130
|
-
index += 2;
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
if (source[index] === quote) return index + 1;
|
|
134
|
-
index += 1;
|
|
135
|
-
}
|
|
136
|
-
return index;
|
|
137
|
-
}
|
|
138
|
-
function skipTemplateLiteral(source, index, controlFlowRegexes) {
|
|
139
|
-
index += 1;
|
|
140
|
-
let expressionDepth = 0;
|
|
141
|
-
let previousSignificant = "";
|
|
142
|
-
while (index < source.length) {
|
|
143
|
-
const char = source[index];
|
|
144
|
-
const next = source[index + 1];
|
|
145
|
-
if (char === "\\") {
|
|
146
|
-
index += 2;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (expressionDepth === 0) {
|
|
150
|
-
if (char === "`") return index + 1;
|
|
151
|
-
if (char === "$" && next === "{") {
|
|
152
|
-
expressionDepth = 1;
|
|
153
|
-
previousSignificant = "{";
|
|
154
|
-
index += 2;
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
index += 1;
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
if (char === "\"" || char === "'") {
|
|
161
|
-
index = skipQuoted(source, index, controlFlowRegexes);
|
|
162
|
-
previousSignificant = "literal";
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (char === "`") {
|
|
166
|
-
index = skipTemplateLiteral(source, index, controlFlowRegexes);
|
|
167
|
-
previousSignificant = "literal";
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (char === "/" && next === "/") {
|
|
171
|
-
index = skipLineComment(source, index);
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (char === "/" && next === "*") {
|
|
175
|
-
index = skipBlockComment(source, index);
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
if (char === "/" && (isRegexLiteralStart(previousSignificant) || isControlFlowRegexStart(source, index, controlFlowRegexes))) {
|
|
179
|
-
index = skipRegexLiteral(source, index);
|
|
180
|
-
previousSignificant = "/";
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
if (char === "{") expressionDepth += 1;
|
|
184
|
-
if (char === "}") expressionDepth -= 1;
|
|
185
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
186
|
-
index += 1;
|
|
187
|
-
}
|
|
188
|
-
return index;
|
|
189
|
-
}
|
|
190
|
-
function skipLineComment(source, index) {
|
|
191
|
-
const end = source.indexOf("\n", index + 2);
|
|
192
|
-
return end === -1 ? source.length : end + 1;
|
|
193
|
-
}
|
|
194
|
-
function skipBlockComment(source, index) {
|
|
195
|
-
const end = source.indexOf("*/", index + 2);
|
|
196
|
-
return end === -1 ? source.length : end + 2;
|
|
197
|
-
}
|
|
198
|
-
function isIdentifierChar(char) {
|
|
199
|
-
return !!char && /[\w$]/.test(char);
|
|
200
|
-
}
|
|
201
|
-
function isRegexLiteralStart(previousSignificant) {
|
|
202
|
-
const token = previousSignificant.trimEnd();
|
|
203
|
-
if (/^\.[\w$]+$/.test(token)) return false;
|
|
204
|
-
return !token || /[({[=,:!&|?;>+\-*%^~]/.test(token) || /\b(?:await|case|delete|do|else|in|instanceof|return|throw|typeof|void|yield)$/.test(token);
|
|
205
|
-
}
|
|
206
|
-
function findLineCommentStart(source, start, end, controlFlowRegexes) {
|
|
207
|
-
for (let index = start; index <= end;) {
|
|
208
|
-
const char = source[index];
|
|
209
|
-
const next = source[index + 1];
|
|
210
|
-
if (isQuote(char)) {
|
|
211
|
-
index = skipQuoted(source, index, controlFlowRegexes);
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (char === "/" && next === "*") {
|
|
215
|
-
index = skipBlockComment(source, index);
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
if (char === "/" && next === "/") return index;
|
|
219
|
-
index += 1;
|
|
220
|
-
}
|
|
221
|
-
return -1;
|
|
222
|
-
}
|
|
223
|
-
function previousCodeIndex(source, index, controlFlowRegexes) {
|
|
224
|
-
let current = index;
|
|
225
|
-
while (current >= 0) {
|
|
226
|
-
while (/\s/.test(source[current] ?? "")) current--;
|
|
227
|
-
if (source[current] === "/" && source[current - 1] === "*") {
|
|
228
|
-
const start = source.lastIndexOf("/*", current - 2);
|
|
229
|
-
if (start === -1) return current;
|
|
230
|
-
current = start - 1;
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
const lineStart = source.lastIndexOf("\n", current) + 1;
|
|
234
|
-
const lineComment = findLineCommentStart(source, lineStart, current, controlFlowRegexes);
|
|
235
|
-
if (lineComment !== -1 && lineComment <= current) {
|
|
236
|
-
current = lineStart - 1;
|
|
237
|
-
continue;
|
|
238
|
-
}
|
|
239
|
-
return current;
|
|
240
|
-
}
|
|
241
|
-
return current;
|
|
242
|
-
}
|
|
243
|
-
function isControlFlowRegexStart(source, index, controlFlowRegexes = /* @__PURE__ */ new Map()) {
|
|
244
|
-
const cached = controlFlowRegexes.get(index);
|
|
245
|
-
if (cached !== void 0) return cached;
|
|
246
|
-
if (controlFlowRegexes.has(index)) return true;
|
|
247
|
-
controlFlowRegexes.set(index, void 0);
|
|
248
|
-
try {
|
|
249
|
-
const closeParen = previousCodeIndex(source, index - 1, controlFlowRegexes);
|
|
250
|
-
if (source[closeParen] !== ")") {
|
|
251
|
-
controlFlowRegexes.set(index, false);
|
|
252
|
-
return false;
|
|
253
|
-
}
|
|
254
|
-
for (let current = closeParen; current >= 0; current--) {
|
|
255
|
-
if (source[current] !== "(") continue;
|
|
256
|
-
if (findMatching(source, current, "(", ")", controlFlowRegexes) !== closeParen) continue;
|
|
257
|
-
const head = source.slice(0, current).replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, " ");
|
|
258
|
-
const result = /(?:^|[^\w$])(?:catch|for|if|while|with)\s*$/.test(head);
|
|
259
|
-
controlFlowRegexes.set(index, result);
|
|
260
|
-
return result;
|
|
261
|
-
}
|
|
262
|
-
controlFlowRegexes.set(index, false);
|
|
263
|
-
return false;
|
|
264
|
-
} catch (error) {
|
|
265
|
-
controlFlowRegexes.delete(index);
|
|
266
|
-
throw error;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
function skipRegexLiteral(source, index) {
|
|
270
|
-
index += 1;
|
|
271
|
-
while (index < source.length) {
|
|
272
|
-
const char = source[index];
|
|
273
|
-
if (char === "\\") {
|
|
274
|
-
index += 2;
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
if (char === "[") {
|
|
278
|
-
index += 1;
|
|
279
|
-
while (index < source.length) {
|
|
280
|
-
if (source[index] === "\\") {
|
|
281
|
-
index += 2;
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
if (source[index] === "]") break;
|
|
285
|
-
index += 1;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (char === "/") {
|
|
289
|
-
index += 1;
|
|
290
|
-
while (/[a-z]/i.test(source[index] ?? "")) index += 1;
|
|
291
|
-
return index;
|
|
292
|
-
}
|
|
293
|
-
index += 1;
|
|
294
|
-
}
|
|
295
|
-
return index;
|
|
296
|
-
}
|
|
297
|
-
function trackSignificant(previousSignificant, char) {
|
|
298
|
-
if (/[a-z$]/i.test(char ?? "")) return /[\w$]$/.test(previousSignificant) || previousSignificant === "." ? previousSignificant + char : char ?? "";
|
|
299
|
-
if (/\s/.test(char ?? "")) return /[\w$]$/.test(previousSignificant) ? `${previousSignificant} ` : previousSignificant;
|
|
300
|
-
if (!/\s/.test(char ?? "")) return char ?? "";
|
|
301
|
-
return previousSignificant;
|
|
302
|
-
}
|
|
303
|
-
function isFunctionDeclarationName(source, index) {
|
|
304
|
-
return /(?:^|[^\w$])(?:async\s+)?function\s*\*?\s*$/.test(source.slice(0, index));
|
|
305
|
-
}
|
|
306
|
-
function previousNonWhitespace(source, index) {
|
|
307
|
-
let current = index - 1;
|
|
308
|
-
while (/\s/.test(source[current] ?? "")) current -= 1;
|
|
309
|
-
return source[current];
|
|
310
|
-
}
|
|
311
|
-
function nextNonWhitespace(source, index) {
|
|
312
|
-
return source[skipWhitespaceAndComments(source, index)];
|
|
313
|
-
}
|
|
314
|
-
function skipWhitespaceAndComments(source, index) {
|
|
315
|
-
while (index < source.length) {
|
|
316
|
-
if (/\s/.test(source[index] ?? "")) {
|
|
317
|
-
index += 1;
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
if (source[index] === "/" && source[index + 1] === "/") {
|
|
321
|
-
index = skipLineComment(source, index);
|
|
322
|
-
continue;
|
|
323
|
-
}
|
|
324
|
-
if (source[index] === "/" && source[index + 1] === "*") {
|
|
325
|
-
index = skipBlockComment(source, index);
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
return index;
|
|
329
|
-
}
|
|
330
|
-
return index;
|
|
331
|
-
}
|
|
332
|
-
function isMethodDeclarationName(source, index, closeParen) {
|
|
333
|
-
const previous = previousNonWhitespace(source, index);
|
|
334
|
-
return source[skipWhitespaceAndComments(source, closeParen + 1)] === "{" && previous !== "(" && previous !== "=" && previous !== "," && previous !== ":";
|
|
335
|
-
}
|
|
336
|
-
function isMemberAccessName(source, index) {
|
|
337
|
-
return previousNonWhitespace(source, index) === ".";
|
|
338
|
-
}
|
|
339
|
-
function findMatching(source, index, open, close, controlFlowRegexes = /* @__PURE__ */ new Map()) {
|
|
340
|
-
let depth = 0;
|
|
341
|
-
let previousSignificant = "";
|
|
342
|
-
for (let current = index; current < source.length; current++) {
|
|
343
|
-
const char = source[current];
|
|
344
|
-
const next = source[current + 1];
|
|
345
|
-
if (isQuote(char)) {
|
|
346
|
-
current = skipQuoted(source, current, controlFlowRegexes) - 1;
|
|
347
|
-
previousSignificant = "literal";
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
if (char === "/" && next === "/") {
|
|
351
|
-
current = skipLineComment(source, current) - 1;
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
if (char === "/" && next === "*") {
|
|
355
|
-
current = skipBlockComment(source, current) - 1;
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
if (char === "/" && (isRegexLiteralStart(previousSignificant) || isControlFlowRegexStart(source, current, controlFlowRegexes))) {
|
|
359
|
-
current = skipRegexLiteral(source, current) - 1;
|
|
360
|
-
previousSignificant = "/";
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
if (char === open) {
|
|
364
|
-
depth += 1;
|
|
365
|
-
previousSignificant = char;
|
|
366
|
-
continue;
|
|
367
|
-
}
|
|
368
|
-
if (char === close && !(open === "<" && close === ">" && source[current - 1] === "=")) {
|
|
369
|
-
depth -= 1;
|
|
370
|
-
if (depth === 0) return current;
|
|
371
|
-
previousSignificant = char;
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
function splitTopLevel(source, separator = ",") {
|
|
378
|
-
const parts = [];
|
|
379
|
-
let depth = 0;
|
|
380
|
-
let previousSignificant = "";
|
|
381
|
-
let start = 0;
|
|
382
|
-
for (let index = 0; index < source.length; index++) {
|
|
383
|
-
const char = source[index];
|
|
384
|
-
const next = source[index + 1];
|
|
385
|
-
if (isQuote(char)) {
|
|
386
|
-
index = skipQuoted(source, index) - 1;
|
|
387
|
-
previousSignificant = "literal";
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
if (char === "/" && next === "/") {
|
|
391
|
-
index = skipLineComment(source, index) - 1;
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
if (char === "/" && next === "*") {
|
|
395
|
-
index = skipBlockComment(source, index) - 1;
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
if (char === "/" && (isRegexLiteralStart(previousSignificant) || isControlFlowRegexStart(source, index))) {
|
|
399
|
-
index = skipRegexLiteral(source, index) - 1;
|
|
400
|
-
previousSignificant = "/";
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
if (char === "<") {
|
|
404
|
-
const genericEnd = findMatching(source, index, "<", ">");
|
|
405
|
-
if (genericEnd !== void 0 && nextNonWhitespace(source, genericEnd + 1) === "(") {
|
|
406
|
-
index = genericEnd;
|
|
407
|
-
previousSignificant = ">";
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (char === "(" || char === "{" || char === "[") {
|
|
412
|
-
depth += 1;
|
|
413
|
-
previousSignificant = char;
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
if (char === ")" || char === "}" || char === "]") {
|
|
417
|
-
depth -= 1;
|
|
418
|
-
previousSignificant = char;
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
if (char === separator && depth === 0) {
|
|
422
|
-
parts.push(source.slice(start, index).trim());
|
|
423
|
-
start = index + 1;
|
|
424
|
-
continue;
|
|
425
|
-
}
|
|
426
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
427
|
-
}
|
|
428
|
-
parts.push(source.slice(start).trim());
|
|
429
|
-
return parts;
|
|
430
|
-
}
|
|
431
|
-
function findIdentifierCalls(source, name) {
|
|
432
|
-
const calls = [];
|
|
433
|
-
let previousSignificant = "";
|
|
434
|
-
for (let index = 0; index < source.length; index++) {
|
|
435
|
-
const char = source[index];
|
|
436
|
-
const next = source[index + 1];
|
|
437
|
-
if (isQuote(char)) {
|
|
438
|
-
index = skipQuoted(source, index) - 1;
|
|
439
|
-
previousSignificant = "literal";
|
|
440
|
-
continue;
|
|
441
|
-
}
|
|
442
|
-
if (char === "/" && next === "/") {
|
|
443
|
-
index = skipLineComment(source, index) - 1;
|
|
444
|
-
continue;
|
|
445
|
-
}
|
|
446
|
-
if (char === "/" && next === "*") {
|
|
447
|
-
index = skipBlockComment(source, index) - 1;
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
if (char === "/" && (isRegexLiteralStart(previousSignificant) || isControlFlowRegexStart(source, index))) {
|
|
451
|
-
index = skipRegexLiteral(source, index) - 1;
|
|
452
|
-
previousSignificant = "/";
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
if (!source.startsWith(name, index) || isIdentifierChar(source[index - 1]) || isIdentifierChar(source[index + name.length]) || isFunctionDeclarationName(source, index) || isMemberAccessName(source, index)) {
|
|
456
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
457
|
-
continue;
|
|
458
|
-
}
|
|
459
|
-
let openParen = skipWhitespaceAndComments(source, index + name.length);
|
|
460
|
-
if (source[openParen] === "<") {
|
|
461
|
-
const genericEnd = findMatching(source, openParen, "<", ">");
|
|
462
|
-
if (genericEnd === void 0) {
|
|
463
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
464
|
-
continue;
|
|
465
|
-
}
|
|
466
|
-
openParen = skipWhitespaceAndComments(source, genericEnd + 1);
|
|
467
|
-
}
|
|
468
|
-
if (source[openParen] !== "(") {
|
|
469
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
const closeParen = findMatching(source, openParen, "(", ")");
|
|
473
|
-
if (closeParen === void 0) {
|
|
474
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
|
-
if (isMethodDeclarationName(source, index, closeParen)) {
|
|
478
|
-
previousSignificant = trackSignificant(previousSignificant, char);
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
calls.push({
|
|
482
|
-
arguments: splitTopLevel(source.slice(openParen + 1, closeParen)),
|
|
483
|
-
closeParen,
|
|
484
|
-
name,
|
|
485
|
-
openParen,
|
|
486
|
-
start: index
|
|
487
|
-
});
|
|
488
|
-
previousSignificant = ")";
|
|
489
|
-
index = closeParen;
|
|
490
|
-
}
|
|
491
|
-
return calls;
|
|
492
|
-
}
|
|
493
|
-
//#endregion
|
|
494
|
-
//#region src/discovery.ts
|
|
495
|
-
const authDefinitionExtensions = [
|
|
496
|
-
".ts",
|
|
497
|
-
".mts",
|
|
498
|
-
".cts",
|
|
499
|
-
".js",
|
|
500
|
-
".mjs",
|
|
501
|
-
".cjs"
|
|
502
|
-
];
|
|
503
|
-
function authDefinitionCandidates(rootDir, serverDirs = [resolve(rootDir, "server")]) {
|
|
504
|
-
return authDefinitionExtensions.flatMap((extension) => [...serverDirs.map((serverDir) => ({
|
|
505
|
-
handler: resolve(serverDir, `auth${extension}`),
|
|
506
|
-
name: "default",
|
|
507
|
-
source: "server-auth"
|
|
508
|
-
})), {
|
|
509
|
-
handler: resolve(rootDir, `server.auth${extension}`),
|
|
510
|
-
name: "default",
|
|
511
|
-
source: "server-auth-suffix"
|
|
512
|
-
}]);
|
|
513
|
-
}
|
|
514
|
-
function discoverAuthDefinitions(rootDir, options = {}) {
|
|
515
|
-
const definitions = authDefinitionCandidates(rootDir, options.serverDirs).filter((definition) => existsSync(definition.handler));
|
|
516
|
-
if (definitions.length > 1) throw new Error(["[vitehub] Only one Auth Definition is allowed. Found:", ...definitions.map((definition) => ` - ${definition.handler}`)].join("\n"));
|
|
517
|
-
return definitions;
|
|
518
|
-
}
|
|
519
|
-
function discoverAuthDefinition(rootDir, options = {}) {
|
|
520
|
-
return discoverAuthDefinitions(rootDir, options)[0];
|
|
521
|
-
}
|
|
522
|
-
//#endregion
|
|
523
|
-
//#region src/config.ts
|
|
524
|
-
function objectLiteralBody(value) {
|
|
525
|
-
const trimmed = value?.trim();
|
|
526
|
-
if (!trimmed?.startsWith("{")) return;
|
|
527
|
-
const closeIndex = findMatching(trimmed, 0, "{", "}");
|
|
528
|
-
return typeof closeIndex === "undefined" ? void 0 : trimmed.slice(1, closeIndex);
|
|
529
|
-
}
|
|
530
|
-
function readReturnedObjectBody(block) {
|
|
531
|
-
const body = objectLiteralBody(block);
|
|
532
|
-
if (typeof body === "undefined") return;
|
|
533
|
-
const match = /\breturn\s*/.exec(body);
|
|
534
|
-
if (!match) return;
|
|
535
|
-
return readExpressionObjectBody(body.slice(match.index + match[0].length));
|
|
536
|
-
}
|
|
537
|
-
function readExpressionObjectBody(value) {
|
|
538
|
-
const trimmed = value?.trim();
|
|
539
|
-
if (!trimmed) return;
|
|
540
|
-
if (trimmed.startsWith("{")) return objectLiteralBody(trimmed);
|
|
541
|
-
if (trimmed.startsWith("(")) {
|
|
542
|
-
const closeIndex = findMatching(trimmed, 0, "(", ")");
|
|
543
|
-
if (typeof closeIndex === "undefined") return;
|
|
544
|
-
return objectLiteralBody(trimmed.slice(1, closeIndex));
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
function readDefinitionArgumentObjectBody(argument) {
|
|
548
|
-
const trimmed = argument?.trim();
|
|
549
|
-
if (!trimmed) return;
|
|
550
|
-
const arrowIndex = trimmed.indexOf("=>");
|
|
551
|
-
if (arrowIndex !== -1) {
|
|
552
|
-
const body = trimmed.slice(arrowIndex + 2).trim();
|
|
553
|
-
const callbackBody = body.startsWith("{") ? readReturnedObjectBody(body) : readExpressionObjectBody(body);
|
|
554
|
-
return typeof callbackBody === "undefined" ? void 0 : {
|
|
555
|
-
body: callbackBody,
|
|
556
|
-
callback: true
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
const objectBody = readExpressionObjectBody(trimmed);
|
|
560
|
-
if (typeof objectBody !== "undefined") return {
|
|
561
|
-
body: objectBody,
|
|
562
|
-
callback: false
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
function readDefinitionObjectBody(file) {
|
|
566
|
-
const source = readFileSync(file, "utf8");
|
|
567
|
-
return readDefinitionArgumentObjectBody(findIdentifierCalls(source, "defineAuth").find((item) => /(?:^|[;\n])\s*export\s+default\s*$/.test(source.slice(Math.max(0, item.start - 100), item.start)))?.arguments[0]);
|
|
568
|
-
}
|
|
569
|
-
function arrayLiteralBody(value) {
|
|
570
|
-
const trimmed = value?.trim();
|
|
571
|
-
if (!trimmed?.startsWith("[")) return;
|
|
572
|
-
const closeIndex = findMatching(trimmed, 0, "[", "]");
|
|
573
|
-
return typeof closeIndex === "undefined" ? void 0 : trimmed.slice(1, closeIndex);
|
|
574
|
-
}
|
|
575
|
-
function readEntryKey(entry) {
|
|
576
|
-
const match = /^\s*(?:([A-Za-z_$][\w$]*)|["'`]([^"'`]+)["'`])\s*(?::|$)/.exec(entry);
|
|
577
|
-
return match?.[1] || match?.[2];
|
|
578
|
-
}
|
|
579
|
-
function readEntryValue(entry) {
|
|
580
|
-
const match = /^\s*(?:[A-Za-z_$][\w$]*|["'`][^"'`]+["'`])\s*:/.exec(entry);
|
|
581
|
-
return match ? entry.slice(match[0].length).trim() : void 0;
|
|
582
|
-
}
|
|
583
|
-
function readObjectEntries(body) {
|
|
584
|
-
if (!body) return [];
|
|
585
|
-
return splitTopLevel(body).filter((entry) => entry.length > 0).map((entry) => ({
|
|
586
|
-
key: readEntryKey(entry) ?? "",
|
|
587
|
-
value: readEntryValue(entry)
|
|
588
|
-
}));
|
|
589
|
-
}
|
|
590
|
-
function readObjectPropertyValue(body, property) {
|
|
591
|
-
return readObjectEntries(body).find((entry) => entry.key === property)?.value;
|
|
592
|
-
}
|
|
593
|
-
function hasObjectProperty(body, property) {
|
|
594
|
-
return readObjectEntries(body).some((entry) => entry.key === property);
|
|
595
|
-
}
|
|
596
|
-
function readStaticStringLiteral(expression) {
|
|
597
|
-
const trimmed = expression?.trim();
|
|
598
|
-
if (!trimmed) return;
|
|
599
|
-
return /^["']([^"']*)["']$/.exec(trimmed)?.[1] ?? /^`([^`$]*)`$/.exec(trimmed)?.[1];
|
|
600
|
-
}
|
|
601
|
-
function readStaticBooleanLiteral(expression) {
|
|
602
|
-
const trimmed = expression?.trim();
|
|
603
|
-
if (trimmed === "true") return true;
|
|
604
|
-
if (trimmed === "false") return false;
|
|
605
|
-
}
|
|
606
|
-
function readStaticStringProperty(body, property, label) {
|
|
607
|
-
if (!hasObjectProperty(body, property)) return;
|
|
608
|
-
const value = readStaticStringLiteral(readObjectPropertyValue(body, property));
|
|
609
|
-
if (typeof value === "undefined") throw new TypeError(`\`defineAuth()\` ${label} must be an inline string literal.`);
|
|
610
|
-
if (value.trim().length === 0 || value !== value.trim()) throw new TypeError(`\`defineAuth()\` ${label} must be a non-empty trimmed string.`);
|
|
611
|
-
return value;
|
|
612
|
-
}
|
|
613
|
-
function readStaticRouteProperty(body, property, label) {
|
|
614
|
-
const value = readStaticStringProperty(body, property, label);
|
|
615
|
-
if (typeof value !== "undefined" && !value.startsWith("/")) throw new TypeError(`\`defineAuth()\` ${label} must start with \`/\`.`);
|
|
616
|
-
return value;
|
|
617
|
-
}
|
|
618
|
-
function readStaticBooleanProperty(body, property, label) {
|
|
619
|
-
if (!hasObjectProperty(body, property)) return;
|
|
620
|
-
const value = readStaticBooleanLiteral(readObjectPropertyValue(body, property));
|
|
621
|
-
if (typeof value === "undefined") throw new TypeError(`\`defineAuth()\` ${label} must be an inline boolean literal.`);
|
|
622
|
-
return value;
|
|
623
|
-
}
|
|
624
|
-
function assertOnlyObjectKeys(body, allowed, label) {
|
|
625
|
-
const unknownEntry = readObjectEntries(body).find((entry) => !entry.key || !allowed.has(entry.key));
|
|
626
|
-
if (!unknownEntry) return;
|
|
627
|
-
if (!unknownEntry.key) throw new TypeError(`\`defineAuth()\` ${label} must use static object keys.`);
|
|
628
|
-
throw new TypeError(`\`defineAuth()\` ${label} does not support the "${unknownEntry.key}" option.`);
|
|
629
|
-
}
|
|
630
|
-
function assertStaticObjectKeys(body, label) {
|
|
631
|
-
if (readObjectEntries(body).some((entry) => !entry.key)) throw new TypeError(`\`defineAuth()\` ${label} must use static object keys.`);
|
|
632
|
-
}
|
|
633
|
-
function readAuthRouteConfig(body) {
|
|
634
|
-
const basePath = normalizeAuthBasePath(readStaticStringProperty(body, "basePath", "basePath"));
|
|
635
|
-
if (!hasObjectProperty(body, "route")) return basePath;
|
|
636
|
-
if (readStaticBooleanLiteral(readObjectPropertyValue(body, "route")) === false) return false;
|
|
637
|
-
throw new TypeError("`defineAuth()` route can only be `false` when provided.");
|
|
638
|
-
}
|
|
639
|
-
function readAuthAccessRoute(entry, index) {
|
|
640
|
-
const route = readStaticStringLiteral(entry);
|
|
641
|
-
if (typeof route !== "undefined") {
|
|
642
|
-
if (!route.startsWith("/")) throw new TypeError(`\`defineAuth()\` access.routes[${index}] must start with \`/\`.`);
|
|
643
|
-
return { route };
|
|
644
|
-
}
|
|
645
|
-
const routeObject = objectLiteralBody(entry);
|
|
646
|
-
if (typeof routeObject === "undefined") throw new TypeError(`\`defineAuth()\` access.routes[${index}] must be an inline route string or route object.`);
|
|
647
|
-
assertOnlyObjectKeys(routeObject, new Set(["method", "route"]), `access.routes[${index}]`);
|
|
648
|
-
const resolvedRoute = readStaticRouteProperty(routeObject, "route", `access.routes[${index}].route`);
|
|
649
|
-
if (!resolvedRoute) throw new TypeError(`\`defineAuth()\` access.routes[${index}].route is required.`);
|
|
650
|
-
const method = readStaticStringProperty(routeObject, "method", `access.routes[${index}].method`);
|
|
651
|
-
return method ? {
|
|
652
|
-
method,
|
|
653
|
-
route: resolvedRoute
|
|
654
|
-
} : { route: resolvedRoute };
|
|
655
|
-
}
|
|
656
|
-
function readAuthAccessRoutesConfig(body) {
|
|
657
|
-
const access = objectLiteralBody(readObjectPropertyValue(body, "access"));
|
|
658
|
-
if (!access || !hasObjectProperty(access, "routes")) return [];
|
|
659
|
-
const routes = arrayLiteralBody(readObjectPropertyValue(access, "routes"));
|
|
660
|
-
if (typeof routes === "undefined") throw new TypeError("`defineAuth()` access.routes must be an inline array.");
|
|
661
|
-
return splitTopLevel(routes).filter((entry) => entry.length > 0).map((entry, index) => readAuthAccessRoute(entry, index));
|
|
662
|
-
}
|
|
663
|
-
function readAuthDatabaseConfig(body, allowRuntimeValue = false) {
|
|
664
|
-
if (!hasObjectProperty(body, "database")) return { mode: "default" };
|
|
665
|
-
const expression = readObjectPropertyValue(body, "database")?.trim();
|
|
666
|
-
if (expression === "true") return { mode: "default" };
|
|
667
|
-
if (expression === "false") throw new TypeError("`defineAuth()` database cannot be `false`.");
|
|
668
|
-
const database = objectLiteralBody(expression);
|
|
669
|
-
if (typeof database === "undefined") {
|
|
670
|
-
if (allowRuntimeValue) return { mode: "default" };
|
|
671
|
-
throw new TypeError("`defineAuth()` database must be `true` or an inline object with `name`.");
|
|
672
|
-
}
|
|
673
|
-
assertOnlyObjectKeys(database, new Set(["dedicated", "name"]), "database");
|
|
674
|
-
return {
|
|
675
|
-
dedicated: readStaticBooleanProperty(database, "dedicated", "database.dedicated") ?? false,
|
|
676
|
-
mode: "named",
|
|
677
|
-
name: readStaticStringProperty(database, "name", "database.name") ?? (() => {
|
|
678
|
-
throw new TypeError("`defineAuth()` database.name is required when database is an object.");
|
|
679
|
-
})()
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
function readAuthSecondaryStorageConfig(body, allowRuntimeValue = false) {
|
|
683
|
-
if (!hasObjectProperty(body, "secondaryStorage")) return false;
|
|
684
|
-
const expression = readObjectPropertyValue(body, "secondaryStorage")?.trim();
|
|
685
|
-
if (expression === "true") return { mode: "default" };
|
|
686
|
-
if (expression === "false") throw new TypeError("`defineAuth()` secondaryStorage cannot be `false`.");
|
|
687
|
-
const secondaryStorage = objectLiteralBody(expression);
|
|
688
|
-
if (typeof secondaryStorage === "undefined") {
|
|
689
|
-
if (allowRuntimeValue) return false;
|
|
690
|
-
throw new TypeError("`defineAuth()` secondaryStorage must be `true` or an inline object with `store`.");
|
|
691
|
-
}
|
|
692
|
-
assertOnlyObjectKeys(secondaryStorage, new Set(["store"]), "secondaryStorage");
|
|
693
|
-
return {
|
|
694
|
-
mode: "named",
|
|
695
|
-
store: readStaticStringProperty(secondaryStorage, "store", "secondaryStorage.store") ?? (() => {
|
|
696
|
-
throw new TypeError("`defineAuth()` secondaryStorage.store is required when secondaryStorage is an object.");
|
|
697
|
-
})()
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
function resolveAuthViteConfig(options, rootDir = process.cwd(), discovery = {}) {
|
|
701
|
-
if (options === false) return;
|
|
702
|
-
const definition = discoverAuthDefinition(rootDir, discovery);
|
|
703
|
-
if (!definition) return;
|
|
704
|
-
const definitionObject = readDefinitionObjectBody(definition.handler);
|
|
705
|
-
if (typeof definitionObject === "undefined") throw new TypeError("`defineAuth()` options must be an inline object literal.");
|
|
706
|
-
const body = definitionObject.body;
|
|
707
|
-
assertStaticObjectKeys(body, "options");
|
|
708
|
-
const basePath = normalizeAuthBasePath(readStaticStringProperty(body, "basePath", "basePath"));
|
|
709
|
-
return {
|
|
710
|
-
access: { routes: readAuthAccessRoutesConfig(body) },
|
|
711
|
-
basePath,
|
|
712
|
-
database: readAuthDatabaseConfig(body, definitionObject.callback),
|
|
713
|
-
definition,
|
|
714
|
-
rootDir,
|
|
715
|
-
route: readAuthRouteConfig(body),
|
|
716
|
-
secondaryStorage: readAuthSecondaryStorageConfig(body, definitionObject.callback)
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
//#endregion
|
|
720
|
-
//#region src/vite.ts
|
|
721
|
-
const AUTH_DEFINITION_ID = "#vitehub/auth/definition";
|
|
722
|
-
const AUTH_SERVER_ID = "#vitehub/auth/server";
|
|
723
|
-
const AUTH_VITE_PLUGIN_NAME = "@vite-hub/auth/vite";
|
|
724
|
-
const RESOLVED_AUTH_DEFINITION_ID = `\0${AUTH_DEFINITION_ID}`;
|
|
725
|
-
const RESOLVED_AUTH_SERVER_ID = `\0${AUTH_SERVER_ID}`;
|
|
726
|
-
const authPackageName = "@vite-hub/auth";
|
|
727
|
-
const envServerModuleId = "#vitehub/env/server";
|
|
728
|
-
const envVitePluginName = "@vite-hub/env/vite";
|
|
729
|
-
const generatedAuthAccessMiddlewareHandler = ".vitehub/auth/access-middleware.ts";
|
|
730
|
-
const generatedAuthRouteHandler = ".vitehub/auth/route.ts";
|
|
731
|
-
const mergeNoExternal = createNoExternalMerger(authPackageName);
|
|
732
|
-
function renderAuthDefinitionModule(config) {
|
|
733
|
-
if (!config) return "export const definition = undefined\nexport default definition\n";
|
|
734
|
-
return [
|
|
735
|
-
`import definition from ${JSON.stringify(config.definition.handler)}`,
|
|
736
|
-
"export { definition }",
|
|
737
|
-
"export default definition",
|
|
738
|
-
""
|
|
739
|
-
].join("\n");
|
|
740
|
-
}
|
|
741
|
-
function renderAuthServerModule(options = { serverEnv: false }) {
|
|
742
|
-
const serverImport = `${options.importBase ?? authPackageName}/server`;
|
|
743
|
-
return [
|
|
744
|
-
...options.serverEnv ? [
|
|
745
|
-
`import { setAuthRuntimeEnvResolver } from ${JSON.stringify(serverImport)}`,
|
|
746
|
-
`import { useServerEnv } from ${JSON.stringify(envServerModuleId)}`,
|
|
747
|
-
"setAuthRuntimeEnvResolver(useServerEnv)",
|
|
748
|
-
""
|
|
749
|
-
] : [],
|
|
750
|
-
`export * from ${JSON.stringify(serverImport)}`,
|
|
751
|
-
`export { handleAuth as default } from ${JSON.stringify(serverImport)}`,
|
|
752
|
-
""
|
|
753
|
-
].join("\n");
|
|
754
|
-
}
|
|
755
|
-
function renderAuthRouteHandler() {
|
|
756
|
-
return [`export { default } from ${JSON.stringify(AUTH_SERVER_ID)}`, ""].join("\n");
|
|
757
|
-
}
|
|
758
|
-
function renderAuthAccessMiddlewareHandler(config) {
|
|
759
|
-
const routes = JSON.stringify(config?.access.routes ?? []);
|
|
760
|
-
return [
|
|
761
|
-
`import { requireAuth } from ${JSON.stringify(AUTH_SERVER_ID)}`,
|
|
762
|
-
"",
|
|
763
|
-
`const routes = ${routes}`,
|
|
764
|
-
"",
|
|
765
|
-
"function routeMatches(pattern, pathname) {",
|
|
766
|
-
" if (pattern.endsWith('/**')) {",
|
|
767
|
-
" const base = pattern.slice(0, -3)",
|
|
768
|
-
" return pathname === base || pathname.startsWith(`${base}/`)",
|
|
769
|
-
" }",
|
|
770
|
-
" return pathname === pattern",
|
|
771
|
-
"}",
|
|
772
|
-
"",
|
|
773
|
-
"function matchesAccessRoute(event) {",
|
|
774
|
-
" const method = event.req.method",
|
|
775
|
-
" const pathname = event.url.pathname",
|
|
776
|
-
" return routes.some(route => (!route.method || route.method.toUpperCase() === method) && routeMatches(route.route, pathname))",
|
|
777
|
-
"}",
|
|
778
|
-
"",
|
|
779
|
-
"export default function viteHubAuthAccessMiddleware(event) {",
|
|
780
|
-
" if (!matchesAccessRoute(event)) return",
|
|
781
|
-
" return requireAuth(event)",
|
|
782
|
-
"}",
|
|
783
|
-
""
|
|
784
|
-
].join("\n");
|
|
785
|
-
}
|
|
786
|
-
function authAmbientTypesPath(root) {
|
|
787
|
-
return resolve(root, ".vitehub", "types", "auth.d.ts");
|
|
788
|
-
}
|
|
789
|
-
function renderAuthAmbientTypes(options = { serverEnv: false }) {
|
|
790
|
-
const serverImport = `${options.importBase ?? authPackageName}/server`;
|
|
791
|
-
return [
|
|
792
|
-
...options.serverEnv ? [
|
|
793
|
-
`import type { ServerEnv } from ${JSON.stringify(envServerModuleId)}`,
|
|
794
|
-
"",
|
|
795
|
-
"declare global {",
|
|
796
|
-
" namespace ViteHub {",
|
|
797
|
-
" interface AuthRuntimeEnv extends ServerEnv {}",
|
|
798
|
-
" }",
|
|
799
|
-
"}",
|
|
800
|
-
""
|
|
801
|
-
] : [],
|
|
802
|
-
`declare module ${JSON.stringify(AUTH_SERVER_ID)} {`,
|
|
803
|
-
` export * from ${JSON.stringify(serverImport)}`,
|
|
804
|
-
` export { handleAuth as default } from ${JSON.stringify(serverImport)}`,
|
|
805
|
-
"}",
|
|
806
|
-
""
|
|
807
|
-
].join("\n");
|
|
808
|
-
}
|
|
809
|
-
async function refreshAuthGeneratedFiles(root, config, options = {}) {
|
|
810
|
-
await Promise.all([
|
|
811
|
-
writeFileIfChanged(resolve(root, generatedAuthAccessMiddlewareHandler), renderAuthAccessMiddlewareHandler(config)),
|
|
812
|
-
writeFileIfChanged(authAmbientTypesPath(root), renderAuthAmbientTypes({
|
|
813
|
-
importBase: options.importBase,
|
|
814
|
-
serverEnv: Boolean(options.serverEnv)
|
|
815
|
-
})),
|
|
816
|
-
writeFileIfChanged(resolve(root, generatedAuthRouteHandler), renderAuthRouteHandler())
|
|
817
|
-
]);
|
|
818
|
-
}
|
|
819
|
-
function hasServerEnvIntegration(config) {
|
|
820
|
-
return Array.isArray(config?.plugins) && config.plugins.some((plugin) => plugin.name === envVitePluginName);
|
|
821
|
-
}
|
|
822
|
-
function cloneNitroConfig(value) {
|
|
823
|
-
return value && typeof value === "object" && !Array.isArray(value) ? { ...value } : {};
|
|
824
|
-
}
|
|
825
|
-
function authRoutePattern(route) {
|
|
826
|
-
return `${route}/**`;
|
|
827
|
-
}
|
|
828
|
-
function mergeNitroAuthHandler(value, config) {
|
|
829
|
-
const nitro = cloneNitroConfig(value);
|
|
830
|
-
if (!config) return nitro;
|
|
831
|
-
const existingHandlers = Array.isArray(nitro.handlers) ? nitro.handlers : [];
|
|
832
|
-
const authHandlers = [...config.route === false ? [] : [{
|
|
833
|
-
handler: generatedAuthRouteHandler,
|
|
834
|
-
route: authRoutePattern(config.route)
|
|
835
|
-
}], ...config.access.routes.length > 0 ? [{
|
|
836
|
-
handler: generatedAuthAccessMiddlewareHandler,
|
|
837
|
-
middleware: true,
|
|
838
|
-
route: "/**"
|
|
839
|
-
}] : []];
|
|
840
|
-
if (!authHandlers.length) return nitro;
|
|
841
|
-
return {
|
|
842
|
-
...nitro,
|
|
843
|
-
handlers: [...existingHandlers, ...authHandlers]
|
|
844
|
-
};
|
|
845
|
-
}
|
|
846
|
-
function readForwardedProtocol(request) {
|
|
847
|
-
const value = request.headers["x-forwarded-proto"];
|
|
848
|
-
return Array.isArray(value) ? value[0] ?? "http" : value ?? "http";
|
|
849
|
-
}
|
|
850
|
-
function createWebRequest(request) {
|
|
851
|
-
const host = request.headers.host ?? "localhost";
|
|
852
|
-
const url = new URL(request.url ?? "/", `${readForwardedProtocol(request)}://${host}`);
|
|
853
|
-
const headers = new Headers();
|
|
854
|
-
for (const [key, value] of Object.entries(request.headers)) {
|
|
855
|
-
if (typeof value === "undefined") continue;
|
|
856
|
-
if (Array.isArray(value)) {
|
|
857
|
-
for (const item of value) headers.append(key, item);
|
|
858
|
-
continue;
|
|
859
|
-
}
|
|
860
|
-
headers.set(key, value);
|
|
861
|
-
}
|
|
862
|
-
const init = {
|
|
863
|
-
headers,
|
|
864
|
-
method: request.method
|
|
865
|
-
};
|
|
866
|
-
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
867
|
-
init.body = Readable.toWeb(request);
|
|
868
|
-
init.duplex = "half";
|
|
869
|
-
}
|
|
870
|
-
return new Request(url, init);
|
|
871
|
-
}
|
|
872
|
-
async function sendWebResponse(response, target) {
|
|
873
|
-
target.statusCode = response.status;
|
|
874
|
-
target.statusMessage = response.statusText;
|
|
875
|
-
const setCookieHeaders = response.headers.getSetCookie();
|
|
876
|
-
response.headers.forEach((value, key) => {
|
|
877
|
-
if (key.toLowerCase() === "set-cookie" && setCookieHeaders.length > 0) return;
|
|
878
|
-
target.appendHeader(key, value);
|
|
879
|
-
});
|
|
880
|
-
for (const value of setCookieHeaders) target.appendHeader("set-cookie", value);
|
|
881
|
-
if (!response.body) {
|
|
882
|
-
target.end();
|
|
883
|
-
return;
|
|
884
|
-
}
|
|
885
|
-
const reader = response.body.getReader();
|
|
886
|
-
try {
|
|
887
|
-
for (;;) {
|
|
888
|
-
const chunk = await reader.read();
|
|
889
|
-
if (chunk.done) break;
|
|
890
|
-
target.write(Buffer.from(chunk.value));
|
|
891
|
-
}
|
|
892
|
-
} finally {
|
|
893
|
-
target.end();
|
|
894
|
-
reader.releaseLock();
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
function loadAuthDefinitionModule(module) {
|
|
898
|
-
const value = module;
|
|
899
|
-
return value.default ?? value.definition;
|
|
900
|
-
}
|
|
901
|
-
function hubAuth(options) {
|
|
902
|
-
const importBase = options?.importBase ?? authPackageName;
|
|
903
|
-
let resolved;
|
|
904
|
-
let runtimeConfig;
|
|
905
|
-
let serverDirs;
|
|
906
|
-
let serverEnv = false;
|
|
907
|
-
function resolvedOptions() {
|
|
908
|
-
return resolved?.auth ?? options;
|
|
909
|
-
}
|
|
910
|
-
function refreshRuntimeConfig() {
|
|
911
|
-
if (!resolved) return;
|
|
912
|
-
runtimeConfig = resolveAuthViteConfig(resolvedOptions(), resolved.root, { serverDirs });
|
|
913
|
-
resetAuth();
|
|
914
|
-
return runtimeConfig;
|
|
915
|
-
}
|
|
916
|
-
return {
|
|
917
|
-
name: AUTH_VITE_PLUGIN_NAME,
|
|
918
|
-
enforce: "pre",
|
|
919
|
-
api: {
|
|
920
|
-
getConfig: () => runtimeConfig,
|
|
921
|
-
refresh: refreshRuntimeConfig
|
|
922
|
-
},
|
|
923
|
-
config(config) {
|
|
924
|
-
const configRoot = config.root || process.cwd();
|
|
925
|
-
serverDirs = config["__vitehubServerDirs"] ?? serverDirs;
|
|
926
|
-
const authConfig = resolveAuthViteConfig(config.auth ?? options, configRoot, { serverDirs });
|
|
927
|
-
const nitro = mergeNitroAuthHandler(config.nitro, authConfig);
|
|
928
|
-
const hasNitroHandlers = Boolean(authConfig && (authConfig.route !== false || authConfig.access.routes.length > 0));
|
|
929
|
-
return {
|
|
930
|
-
ssr: { noExternal: mergeNoExternal(config.ssr?.noExternal) },
|
|
931
|
-
...hasNitroHandlers ? { nitro } : {},
|
|
932
|
-
server: { watch: { ignored: mergeGeneratedViteHubWatchIgnored(config.server?.watch?.ignored) } }
|
|
933
|
-
};
|
|
934
|
-
},
|
|
935
|
-
async configResolved(config) {
|
|
936
|
-
resolved = config;
|
|
937
|
-
serverEnv = hasServerEnvIntegration(config);
|
|
938
|
-
const runtimeConfig = refreshRuntimeConfig();
|
|
939
|
-
await refreshAuthGeneratedFiles(config.root, runtimeConfig, {
|
|
940
|
-
importBase,
|
|
941
|
-
serverEnv
|
|
942
|
-
});
|
|
943
|
-
},
|
|
944
|
-
configEnvironment(name, config) {
|
|
945
|
-
if (!isServerEnvironment(name, config)) return;
|
|
946
|
-
return { resolve: { noExternal: mergeNoExternal(config.resolve?.noExternal) } };
|
|
947
|
-
},
|
|
948
|
-
configureServer(server) {
|
|
949
|
-
server.middlewares.use(async (request, response, next) => {
|
|
950
|
-
try {
|
|
951
|
-
const config = runtimeConfig ?? refreshRuntimeConfig();
|
|
952
|
-
if (!config || config.route === false || !request.url) {
|
|
953
|
-
next();
|
|
954
|
-
return;
|
|
955
|
-
}
|
|
956
|
-
if (!isAuthRequestPath(new URL(request.url, "http://vitehub.local").pathname, config.basePath)) {
|
|
957
|
-
next();
|
|
958
|
-
return;
|
|
959
|
-
}
|
|
960
|
-
const definition = loadAuthDefinitionModule(await server.ssrLoadModule(AUTH_DEFINITION_ID));
|
|
961
|
-
if (!definition) {
|
|
962
|
-
next();
|
|
963
|
-
return;
|
|
964
|
-
}
|
|
965
|
-
const webRequest = createWebRequest(request);
|
|
966
|
-
await sendWebResponse(typeof definition.options === "function" || typeof definition.options.runtime === "function" ? await handleAuthRequest(definition, webRequest) : await getAuthForDefinition(definition).handler(webRequest), response);
|
|
967
|
-
} catch (error) {
|
|
968
|
-
next(error);
|
|
969
|
-
}
|
|
970
|
-
});
|
|
971
|
-
},
|
|
972
|
-
handleHotUpdate(context) {
|
|
973
|
-
const changed = context.file.replace(/\\/g, "/");
|
|
974
|
-
if (changed !== runtimeConfig?.definition.handler.replace(/\\/g, "/") && !/\/?server\.auth\.(?:c|m)?[jt]s$/i.test(changed) && !/\/server\/auth\.(?:c|m)?[jt]s$/i.test(changed)) return;
|
|
975
|
-
refreshRuntimeConfig();
|
|
976
|
-
const definitionModule = context.server.moduleGraph.getModuleById(RESOLVED_AUTH_DEFINITION_ID);
|
|
977
|
-
if (definitionModule) context.server.moduleGraph.invalidateModule(definitionModule);
|
|
978
|
-
const serverModule = context.server.moduleGraph.getModuleById(RESOLVED_AUTH_SERVER_ID);
|
|
979
|
-
if (serverModule) context.server.moduleGraph.invalidateModule(serverModule);
|
|
980
|
-
},
|
|
981
|
-
resolveId(id) {
|
|
982
|
-
if (id === "#vitehub/auth/definition") return RESOLVED_AUTH_DEFINITION_ID;
|
|
983
|
-
if (id === "#vitehub/auth/server") return RESOLVED_AUTH_SERVER_ID;
|
|
984
|
-
},
|
|
985
|
-
load(id) {
|
|
986
|
-
if (id === RESOLVED_AUTH_DEFINITION_ID) return renderAuthDefinitionModule(runtimeConfig);
|
|
987
|
-
if (id === RESOLVED_AUTH_SERVER_ID) return renderAuthServerModule({
|
|
988
|
-
importBase,
|
|
989
|
-
serverEnv
|
|
990
|
-
});
|
|
991
|
-
}
|
|
992
|
-
};
|
|
993
|
-
}
|
|
994
|
-
//#endregion
|
|
995
|
-
export { AUTH_DEFINITION_ID, AUTH_SERVER_ID, AUTH_VITE_PLUGIN_NAME, hubAuth };
|
|
1
|
+
import { a as hubAuth, i as createAuthNitroConfig, n as AUTH_SERVER_ID, o as resolveAuthViteConfig, r as AUTH_VITE_PLUGIN_NAME, t as AUTH_DEFINITION_ID } from "./vite-Cmey8ODS.js";
|
|
2
|
+
export { AUTH_DEFINITION_ID, AUTH_SERVER_ID, AUTH_VITE_PLUGIN_NAME, createAuthNitroConfig, hubAuth, resolveAuthViteConfig };
|