speexjs 0.2.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 +555 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +1017 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client/index.d.ts +73 -0
- package/dist/client/index.js +927 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/signals/index.d.ts +62 -0
- package/dist/client/signals/index.js +248 -0
- package/dist/client/signals/index.js.map +1 -0
- package/dist/client/vdom/index.d.ts +50 -0
- package/dist/client/vdom/index.js +540 -0
- package/dist/client/vdom/index.js.map +1 -0
- package/dist/client/vdom/jsx-runtime.d.ts +9 -0
- package/dist/client/vdom/jsx-runtime.js +203 -0
- package/dist/client/vdom/jsx-runtime.js.map +1 -0
- package/dist/index-CMkhSDh7.d.ts +97 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +6402 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-DGrnv8QB.d.ts +8 -0
- package/dist/response-Ca8KWK5_.d.ts +173 -0
- package/dist/rpc/index.d.ts +70 -0
- package/dist/rpc/index.js +136 -0
- package/dist/rpc/index.js.map +1 -0
- package/dist/schema/index.d.ts +231 -0
- package/dist/schema/index.js +1160 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/server/auth/index.d.ts +61 -0
- package/dist/server/auth/index.js +462 -0
- package/dist/server/auth/index.js.map +1 -0
- package/dist/server/cache/index.d.ts +45 -0
- package/dist/server/cache/index.js +238 -0
- package/dist/server/cache/index.js.map +1 -0
- package/dist/server/container/index.d.ts +20 -0
- package/dist/server/container/index.js +62 -0
- package/dist/server/container/index.js.map +1 -0
- package/dist/server/controller/index.d.ts +37 -0
- package/dist/server/controller/index.js +139 -0
- package/dist/server/controller/index.js.map +1 -0
- package/dist/server/database/index.d.ts +461 -0
- package/dist/server/database/index.js +1977 -0
- package/dist/server/database/index.js.map +1 -0
- package/dist/server/events/index.d.ts +29 -0
- package/dist/server/events/index.js +159 -0
- package/dist/server/events/index.js.map +1 -0
- package/dist/server/gate/index.d.ts +36 -0
- package/dist/server/gate/index.js +169 -0
- package/dist/server/gate/index.js.map +1 -0
- package/dist/server/http/index.d.ts +45 -0
- package/dist/server/http/index.js +871 -0
- package/dist/server/http/index.js.map +1 -0
- package/dist/server/index.d.ts +79 -0
- package/dist/server/index.js +4185 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/middleware/index.d.ts +5 -0
- package/dist/server/middleware/index.js +416 -0
- package/dist/server/middleware/index.js.map +1 -0
- package/dist/server/router/index.d.ts +5 -0
- package/dist/server/router/index.js +231 -0
- package/dist/server/router/index.js.map +1 -0
- package/dist/server/storage/index.d.ts +66 -0
- package/dist/server/storage/index.js +244 -0
- package/dist/server/storage/index.js.map +1 -0
- package/dist/session-guard-CZeN87L9.d.ts +48 -0
- package/dist/types-CXH8hPei.d.ts +38 -0
- package/package.json +138 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// src/server/router/index.ts
|
|
2
|
+
var Router = class {
|
|
3
|
+
routes = [];
|
|
4
|
+
groupMiddleware = [];
|
|
5
|
+
groupPrefix = "";
|
|
6
|
+
namedRoutes = /* @__PURE__ */ new Map();
|
|
7
|
+
get(path, handler) {
|
|
8
|
+
return this.match(["GET"], path, handler);
|
|
9
|
+
}
|
|
10
|
+
post(path, handler) {
|
|
11
|
+
return this.match(["POST"], path, handler);
|
|
12
|
+
}
|
|
13
|
+
put(path, handler) {
|
|
14
|
+
return this.match(["PUT"], path, handler);
|
|
15
|
+
}
|
|
16
|
+
patch(path, handler) {
|
|
17
|
+
return this.match(["PATCH"], path, handler);
|
|
18
|
+
}
|
|
19
|
+
delete(path, handler) {
|
|
20
|
+
return this.match(["DELETE"], path, handler);
|
|
21
|
+
}
|
|
22
|
+
options(path, handler) {
|
|
23
|
+
return this.match(["OPTIONS"], path, handler);
|
|
24
|
+
}
|
|
25
|
+
any(path, handler) {
|
|
26
|
+
return this.match(
|
|
27
|
+
["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
|
28
|
+
path,
|
|
29
|
+
handler
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
match(methods, path, handler) {
|
|
33
|
+
const fullPath = this.groupPrefix + normalizePath(path);
|
|
34
|
+
const { regexp, keys } = pathToRegexp(fullPath);
|
|
35
|
+
this.routes.push({
|
|
36
|
+
methods: methods.map((m) => m.toUpperCase()),
|
|
37
|
+
path: fullPath,
|
|
38
|
+
handler,
|
|
39
|
+
middleware: [...this.groupMiddleware],
|
|
40
|
+
regexp,
|
|
41
|
+
paramKeys: keys
|
|
42
|
+
});
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
group(prefix, callback) {
|
|
46
|
+
const previousPrefix = this.groupPrefix;
|
|
47
|
+
const previousMiddleware = [...this.groupMiddleware];
|
|
48
|
+
this.groupPrefix = previousPrefix + normalizePath(prefix);
|
|
49
|
+
callback(this);
|
|
50
|
+
this.groupPrefix = previousPrefix;
|
|
51
|
+
this.groupMiddleware = previousMiddleware;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
resource(name, controller) {
|
|
55
|
+
const actions = {
|
|
56
|
+
index: "index",
|
|
57
|
+
create: "create",
|
|
58
|
+
store: "store",
|
|
59
|
+
show: "show",
|
|
60
|
+
edit: "edit",
|
|
61
|
+
update: "update",
|
|
62
|
+
destroy: "destroy"
|
|
63
|
+
};
|
|
64
|
+
return this.registerResourceRoutes(name, controller, actions);
|
|
65
|
+
}
|
|
66
|
+
apiResource(name, controller) {
|
|
67
|
+
const actions = {
|
|
68
|
+
index: "index",
|
|
69
|
+
store: "store",
|
|
70
|
+
show: "show",
|
|
71
|
+
update: "update",
|
|
72
|
+
destroy: "destroy"
|
|
73
|
+
};
|
|
74
|
+
return this.registerResourceRoutes(name, controller, actions);
|
|
75
|
+
}
|
|
76
|
+
registerResourceRoutes(name, controller, actions) {
|
|
77
|
+
const basePath = normalizePath(name);
|
|
78
|
+
const paramName = singularize(name);
|
|
79
|
+
if (actions.index !== void 0) {
|
|
80
|
+
this.get(
|
|
81
|
+
basePath,
|
|
82
|
+
this.createControllerHandler(controller, actions.index)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (actions.create !== void 0) {
|
|
86
|
+
this.get(
|
|
87
|
+
`${basePath}/create`,
|
|
88
|
+
this.createControllerHandler(controller, actions.create)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (actions.store !== void 0) {
|
|
92
|
+
this.post(
|
|
93
|
+
basePath,
|
|
94
|
+
this.createControllerHandler(controller, actions.store)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (actions.show !== void 0) {
|
|
98
|
+
this.get(
|
|
99
|
+
`${basePath}/:${paramName}`,
|
|
100
|
+
this.createControllerHandler(controller, actions.show)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (actions.edit !== void 0) {
|
|
104
|
+
this.get(
|
|
105
|
+
`${basePath}/:${paramName}/edit`,
|
|
106
|
+
this.createControllerHandler(controller, actions.edit)
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (actions.update !== void 0) {
|
|
110
|
+
this.put(
|
|
111
|
+
`${basePath}/:${paramName}`,
|
|
112
|
+
this.createControllerHandler(controller, actions.update)
|
|
113
|
+
);
|
|
114
|
+
this.patch(
|
|
115
|
+
`${basePath}/:${paramName}`,
|
|
116
|
+
this.createControllerHandler(controller, actions.update)
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (actions.destroy !== void 0) {
|
|
120
|
+
this.delete(
|
|
121
|
+
`${basePath}/:${paramName}`,
|
|
122
|
+
this.createControllerHandler(controller, actions.destroy)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
createControllerHandler(controller, action) {
|
|
128
|
+
return async (ctx) => {
|
|
129
|
+
const instance = createControllerInstance(controller, ctx);
|
|
130
|
+
const handler = instance[action];
|
|
131
|
+
if (typeof handler === "function") {
|
|
132
|
+
await handler.call(instance, ctx);
|
|
133
|
+
} else {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Action ${action} not found on controller ${controller.name}`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
middleware(middleware) {
|
|
141
|
+
const mw = Array.isArray(middleware) ? middleware : [middleware];
|
|
142
|
+
this.groupMiddleware.push(...mw);
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
name(name) {
|
|
146
|
+
if (this.routes.length > 0) {
|
|
147
|
+
const lastRoute = this.routes[this.routes.length - 1];
|
|
148
|
+
if (lastRoute !== void 0) {
|
|
149
|
+
lastRoute.name = name;
|
|
150
|
+
this.namedRoutes.set(name, lastRoute);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
route(name, params) {
|
|
156
|
+
const entry = this.namedRoutes.get(name);
|
|
157
|
+
if (entry === void 0) {
|
|
158
|
+
throw new Error(`Route not found: ${name}`);
|
|
159
|
+
}
|
|
160
|
+
let url = entry.path;
|
|
161
|
+
if (params !== void 0) {
|
|
162
|
+
for (const [key, value] of Object.entries(params)) {
|
|
163
|
+
url = url.replace(`:${key}`, encodeURIComponent(value));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return url;
|
|
167
|
+
}
|
|
168
|
+
resolve(method, path) {
|
|
169
|
+
const normalizedPath = normalizePath(path);
|
|
170
|
+
const upperMethod = method.toUpperCase();
|
|
171
|
+
for (const route of this.routes) {
|
|
172
|
+
if (!route.methods.includes(upperMethod)) continue;
|
|
173
|
+
const match = normalizedPath.match(route.regexp);
|
|
174
|
+
if (match === null) continue;
|
|
175
|
+
const params = {};
|
|
176
|
+
for (let i = 0; i < route.paramKeys.length; i++) {
|
|
177
|
+
const key = route.paramKeys[i];
|
|
178
|
+
const value = match[i + 1];
|
|
179
|
+
if (key !== void 0 && value !== void 0) {
|
|
180
|
+
params[key] = decodeURIComponent(value);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
handler: route.handler,
|
|
185
|
+
params,
|
|
186
|
+
middleware: route.middleware
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
getRoutes() {
|
|
192
|
+
return [...this.routes];
|
|
193
|
+
}
|
|
194
|
+
getNamedRoutes() {
|
|
195
|
+
return new Map(this.namedRoutes);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function normalizePath(path) {
|
|
199
|
+
let normalized = path.replace(/\\/g, "/");
|
|
200
|
+
if (!normalized.startsWith("/")) {
|
|
201
|
+
normalized = "/" + normalized;
|
|
202
|
+
}
|
|
203
|
+
if (normalized.length > 1 && normalized.endsWith("/")) {
|
|
204
|
+
normalized = normalized.slice(0, -1);
|
|
205
|
+
}
|
|
206
|
+
return normalized;
|
|
207
|
+
}
|
|
208
|
+
function pathToRegexp(pattern) {
|
|
209
|
+
const keys = [];
|
|
210
|
+
const regexpStr = pattern.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_match, key) => {
|
|
211
|
+
keys.push(key);
|
|
212
|
+
return "([^/]+)";
|
|
213
|
+
}).replace(/\*/g, ".*?");
|
|
214
|
+
return { regexp: new RegExp(`^${regexpStr}$`), keys };
|
|
215
|
+
}
|
|
216
|
+
function singularize(word) {
|
|
217
|
+
const lastChar = word[word.length - 1];
|
|
218
|
+
if (lastChar === "s") return word.slice(0, -1);
|
|
219
|
+
if (lastChar === "S") return word.slice(0, -1);
|
|
220
|
+
return word;
|
|
221
|
+
}
|
|
222
|
+
function createControllerInstance(controller, ctx) {
|
|
223
|
+
const instance = new controller();
|
|
224
|
+
instance.__ctx = ctx;
|
|
225
|
+
instance.__container = ctx.container;
|
|
226
|
+
return instance;
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
Router
|
|
230
|
+
};
|
|
231
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/router/index.ts"],"sourcesContent":["import type { Container } from \"../container\";\nimport type { SuperRequest } from \"../http/request\";\nimport type { SuperResponse } from \"../http/response\";\nimport type { Middleware } from \"../middleware\";\n\nexport type RouteHandler = (ctx: RouteContext) => void | Promise<void>;\n\nexport interface RouteContext {\n\trequest: SuperRequest;\n\tresponse: SuperResponse;\n\tparams: Record<string, string>;\n\tquery: Record<string, string | string[]>;\n\tcontainer: Container;\n}\n\nexport interface ResolvedRoute {\n\thandler: RouteHandler;\n\tparams: Record<string, string>;\n\tmiddleware: Middleware[];\n}\n\ninterface RouteEntry {\n\tmethods: string[];\n\tpath: string;\n\thandler: RouteHandler;\n\tmiddleware: Middleware[];\n\tname?: string;\n\tregexp: RegExp;\n\tparamKeys: string[];\n}\n\nexport interface ControllerClass {\n\tnew (...args: unknown[]): object;\n}\n\ninterface ResourceActions {\n\tindex?: string;\n\tcreate?: string;\n\tstore?: string;\n\tshow?: string;\n\tedit?: string;\n\tupdate?: string;\n\tdestroy?: string;\n}\n\nexport class Router {\n\tprivate routes: RouteEntry[] = [];\n\tprivate groupMiddleware: Middleware[] = [];\n\tprivate groupPrefix = \"\";\n\tprivate namedRoutes = new Map<string, RouteEntry>();\n\n\tget(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"GET\"], path, handler);\n\t}\n\n\tpost(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"POST\"], path, handler);\n\t}\n\n\tput(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"PUT\"], path, handler);\n\t}\n\n\tpatch(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"PATCH\"], path, handler);\n\t}\n\n\tdelete(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"DELETE\"], path, handler);\n\t}\n\n\toptions(path: string, handler: RouteHandler): this {\n\t\treturn this.match([\"OPTIONS\"], path, handler);\n\t}\n\n\tany(path: string, handler: RouteHandler): this {\n\t\treturn this.match(\n\t\t\t[\"GET\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"],\n\t\t\tpath,\n\t\t\thandler,\n\t\t);\n\t}\n\n\tmatch(methods: string[], path: string, handler: RouteHandler): this {\n\t\tconst fullPath = this.groupPrefix + normalizePath(path);\n\t\tconst { regexp, keys } = pathToRegexp(fullPath);\n\n\t\tthis.routes.push({\n\t\t\tmethods: methods.map((m) => m.toUpperCase()),\n\t\t\tpath: fullPath,\n\t\t\thandler,\n\t\t\tmiddleware: [...this.groupMiddleware],\n\t\t\tregexp,\n\t\t\tparamKeys: keys,\n\t\t});\n\n\t\treturn this;\n\t}\n\n\tgroup(prefix: string, callback: (router: Router) => void): this {\n\t\tconst previousPrefix = this.groupPrefix;\n\t\tconst previousMiddleware = [...this.groupMiddleware];\n\n\t\tthis.groupPrefix = previousPrefix + normalizePath(prefix);\n\n\t\tcallback(this);\n\n\t\tthis.groupPrefix = previousPrefix;\n\t\tthis.groupMiddleware = previousMiddleware;\n\n\t\treturn this;\n\t}\n\n\tresource(name: string, controller: ControllerClass): this {\n\t\tconst actions: ResourceActions = {\n\t\t\tindex: \"index\",\n\t\t\tcreate: \"create\",\n\t\t\tstore: \"store\",\n\t\t\tshow: \"show\",\n\t\t\tedit: \"edit\",\n\t\t\tupdate: \"update\",\n\t\t\tdestroy: \"destroy\",\n\t\t};\n\n\t\treturn this.registerResourceRoutes(name, controller, actions);\n\t}\n\n\tapiResource(name: string, controller: ControllerClass): this {\n\t\tconst actions: ResourceActions = {\n\t\t\tindex: \"index\",\n\t\t\tstore: \"store\",\n\t\t\tshow: \"show\",\n\t\t\tupdate: \"update\",\n\t\t\tdestroy: \"destroy\",\n\t\t};\n\n\t\treturn this.registerResourceRoutes(name, controller, actions);\n\t}\n\n\tprivate registerResourceRoutes(\n\t\tname: string,\n\t\tcontroller: ControllerClass,\n\t\tactions: ResourceActions,\n\t): this {\n\t\tconst basePath = normalizePath(name);\n\t\tconst paramName = singularize(name);\n\n\t\tif (actions.index !== undefined) {\n\t\t\tthis.get(\n\t\t\t\tbasePath,\n\t\t\t\tthis.createControllerHandler(controller, actions.index),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.create !== undefined) {\n\t\t\tthis.get(\n\t\t\t\t`${basePath}/create`,\n\t\t\t\tthis.createControllerHandler(controller, actions.create),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.store !== undefined) {\n\t\t\tthis.post(\n\t\t\t\tbasePath,\n\t\t\t\tthis.createControllerHandler(controller, actions.store),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.show !== undefined) {\n\t\t\tthis.get(\n\t\t\t\t`${basePath}/:${paramName}`,\n\t\t\t\tthis.createControllerHandler(controller, actions.show),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.edit !== undefined) {\n\t\t\tthis.get(\n\t\t\t\t`${basePath}/:${paramName}/edit`,\n\t\t\t\tthis.createControllerHandler(controller, actions.edit),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.update !== undefined) {\n\t\t\tthis.put(\n\t\t\t\t`${basePath}/:${paramName}`,\n\t\t\t\tthis.createControllerHandler(controller, actions.update),\n\t\t\t);\n\t\t\tthis.patch(\n\t\t\t\t`${basePath}/:${paramName}`,\n\t\t\t\tthis.createControllerHandler(controller, actions.update),\n\t\t\t);\n\t\t}\n\n\t\tif (actions.destroy !== undefined) {\n\t\t\tthis.delete(\n\t\t\t\t`${basePath}/:${paramName}`,\n\t\t\t\tthis.createControllerHandler(controller, actions.destroy),\n\t\t\t);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\tprivate createControllerHandler(\n\t\tcontroller: ControllerClass,\n\t\taction: string,\n\t): RouteHandler {\n\t\treturn async (ctx: RouteContext) => {\n\t\t\tconst instance = createControllerInstance(controller, ctx);\n\t\t\tconst handler = (instance as Record<string, unknown>)[action];\n\t\t\tif (typeof handler === \"function\") {\n\t\t\t\tawait handler.call(instance, ctx);\n\t\t\t} else {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Action ${action} not found on controller ${controller.name}`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\t}\n\n\tmiddleware(middleware: Middleware | Middleware[]): this {\n\t\tconst mw = Array.isArray(middleware) ? middleware : [middleware];\n\t\tthis.groupMiddleware.push(...mw);\n\t\treturn this;\n\t}\n\n\tname(name: string): this {\n\t\tif (this.routes.length > 0) {\n\t\t\tconst lastRoute = this.routes[this.routes.length - 1];\n\t\t\tif (lastRoute !== undefined) {\n\t\t\t\tlastRoute.name = name;\n\t\t\t\tthis.namedRoutes.set(name, lastRoute);\n\t\t\t}\n\t\t}\n\t\treturn this;\n\t}\n\n\troute(name: string, params?: Record<string, string>): string {\n\t\tconst entry = this.namedRoutes.get(name);\n\t\tif (entry === undefined) {\n\t\t\tthrow new Error(`Route not found: ${name}`);\n\t\t}\n\n\t\tlet url = entry.path;\n\t\tif (params !== undefined) {\n\t\t\tfor (const [key, value] of Object.entries(params)) {\n\t\t\t\turl = url.replace(`:${key}`, encodeURIComponent(value));\n\t\t\t}\n\t\t}\n\n\t\treturn url;\n\t}\n\n\tresolve(method: string, path: string): ResolvedRoute | null {\n\t\tconst normalizedPath = normalizePath(path);\n\t\tconst upperMethod = method.toUpperCase();\n\n\t\tfor (const route of this.routes) {\n\t\t\tif (!route.methods.includes(upperMethod)) continue;\n\n\t\t\tconst match = normalizedPath.match(route.regexp);\n\t\t\tif (match === null) continue;\n\n\t\t\tconst params: Record<string, string> = {};\n\t\t\tfor (let i = 0; i < route.paramKeys.length; i++) {\n\t\t\t\tconst key = route.paramKeys[i];\n\t\t\t\tconst value = match[i + 1];\n\t\t\t\tif (key !== undefined && value !== undefined) {\n\t\t\t\t\tparams[key] = decodeURIComponent(value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\thandler: route.handler,\n\t\t\t\tparams,\n\t\t\t\tmiddleware: route.middleware,\n\t\t\t};\n\t\t}\n\n\t\treturn null;\n\t}\n\n\tgetRoutes(): RouteEntry[] {\n\t\treturn [...this.routes];\n\t}\n\n\tgetNamedRoutes(): Map<string, RouteEntry> {\n\t\treturn new Map(this.namedRoutes);\n\t}\n}\n\nfunction normalizePath(path: string): string {\n\tlet normalized = path.replace(/\\\\/g, \"/\");\n\n\tif (!normalized.startsWith(\"/\")) {\n\t\tnormalized = \"/\" + normalized;\n\t}\n\n\tif (normalized.length > 1 && normalized.endsWith(\"/\")) {\n\t\tnormalized = normalized.slice(0, -1);\n\t}\n\n\treturn normalized;\n}\n\nfunction pathToRegexp(pattern: string): { regexp: RegExp; keys: string[] } {\n\tconst keys: string[] = [];\n\n\tconst regexpStr = pattern\n\t\t.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_match: string, key: string) => {\n\t\t\tkeys.push(key);\n\t\t\treturn \"([^/]+)\";\n\t\t})\n\t\t.replace(/\\*/g, \".*?\");\n\n\treturn { regexp: new RegExp(`^${regexpStr}$`), keys };\n}\n\nfunction singularize(word: string): string {\n\tconst lastChar = word[word.length - 1];\n\tif (lastChar === \"s\") return word.slice(0, -1);\n\tif (lastChar === \"S\") return word.slice(0, -1);\n\treturn word;\n}\n\nfunction createControllerInstance(\n\tcontroller: ControllerClass,\n\tctx: RouteContext,\n): object {\n\tconst instance = new controller();\n\t(instance as Record<string, unknown>).__ctx = ctx;\n\t(instance as Record<string, unknown>).__container = ctx.container;\n\treturn instance;\n}\n"],"mappings":";AA6CO,IAAM,SAAN,MAAa;AAAA,EACX,SAAuB,CAAC;AAAA,EACxB,kBAAgC,CAAC;AAAA,EACjC,cAAc;AAAA,EACd,cAAc,oBAAI,IAAwB;AAAA,EAElD,IAAI,MAAc,SAA6B;AAC9C,WAAO,KAAK,MAAM,CAAC,KAAK,GAAG,MAAM,OAAO;AAAA,EACzC;AAAA,EAEA,KAAK,MAAc,SAA6B;AAC/C,WAAO,KAAK,MAAM,CAAC,MAAM,GAAG,MAAM,OAAO;AAAA,EAC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AM,OAAiB,CAAC;AAExB,QAAM,YAAY,QAChB,QAAQ,8BAA8B,CAAC,QAAgB,QAAgB;AACvE,SAAK,KAAK,GAAG;AACb,WAAO;AAAA,EACR,CAAC,EACA,QAAQ,OAAO,KAAK;AAEtB,SAAO,EAAE,QAAQ,IAAI,OAAO,IAAI,SAAS,GAAG,GAAG,KAAK;AACrD;AAEA,SAAS,YAAY,MAAsB;AAC1C,QAAM,WAAW,KAAK,KAAK,SAAS,CAAC;AACrC,MAAI,aAAa,IAAK,QAAO,KAAK,MAAM,GAAG,EAAE;AAC7C,MAAI,aAAa,IAAK,QAAO,KAAK,MAAM,GAAG,EAAE;AAC7C,SAAO;AACR;AAEA,SAAS,yBACR,YACA,KACS;AACT,QAAM,WAAW,IAAI,WAAW;AAChC,EAAC,SAAqC,QAAQ;AAC9C,EAAC,SAAqC,cAAc,IAAI;AACxD,SAAO;AACR;","names":[]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
|
|
3
|
+
interface StorageConfig {
|
|
4
|
+
defaultDisk?: string;
|
|
5
|
+
disks: {
|
|
6
|
+
[name: string]: {
|
|
7
|
+
driver: "local";
|
|
8
|
+
root: string;
|
|
9
|
+
url?: string;
|
|
10
|
+
permissions?: number;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface FileOptions {
|
|
15
|
+
name?: string;
|
|
16
|
+
disk?: string;
|
|
17
|
+
overwrite?: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare class LocalDisk {
|
|
20
|
+
private root;
|
|
21
|
+
private baseUrl?;
|
|
22
|
+
constructor(root: string, baseUrl?: string);
|
|
23
|
+
put(filePath: string, content: string | Buffer): Promise<string>;
|
|
24
|
+
get(filePath: string): Promise<Buffer>;
|
|
25
|
+
exists(filePath: string): Promise<boolean>;
|
|
26
|
+
delete(filePath: string): Promise<boolean>;
|
|
27
|
+
copy(from: string, to: string): Promise<boolean>;
|
|
28
|
+
move(from: string, to: string): Promise<boolean>;
|
|
29
|
+
url(filePath: string): string;
|
|
30
|
+
size(filePath: string): Promise<number>;
|
|
31
|
+
lastModified(filePath: string): Promise<Date>;
|
|
32
|
+
files(directory?: string): Promise<string[]>;
|
|
33
|
+
directories(directory?: string): Promise<string[]>;
|
|
34
|
+
makeDirectory(dirPath: string): Promise<void>;
|
|
35
|
+
deleteDirectory(dirPath: string): Promise<void>;
|
|
36
|
+
append(filePath: string, content: string): Promise<void>;
|
|
37
|
+
prepend(filePath: string, content: string): Promise<void>;
|
|
38
|
+
readStream(filePath: string): fs.ReadStream;
|
|
39
|
+
writeStream(filePath: string): fs.WriteStream;
|
|
40
|
+
getRoot(): string;
|
|
41
|
+
getUrl(): string | undefined;
|
|
42
|
+
private resolvePath;
|
|
43
|
+
}
|
|
44
|
+
declare class Storage {
|
|
45
|
+
private config;
|
|
46
|
+
private diskInstances;
|
|
47
|
+
constructor(config: StorageConfig);
|
|
48
|
+
disk(name?: string): LocalDisk;
|
|
49
|
+
put(filePath: string, content: string | Buffer): Promise<string>;
|
|
50
|
+
get(filePath: string): Promise<Buffer>;
|
|
51
|
+
exists(filePath: string): Promise<boolean>;
|
|
52
|
+
delete(filePath: string): Promise<boolean>;
|
|
53
|
+
copy(from: string, to: string): Promise<boolean>;
|
|
54
|
+
move(from: string, to: string): Promise<boolean>;
|
|
55
|
+
url(filePath: string): Promise<string>;
|
|
56
|
+
size(filePath: string): Promise<number>;
|
|
57
|
+
lastModified(filePath: string): Promise<Date>;
|
|
58
|
+
files(directory?: string): Promise<string[]>;
|
|
59
|
+
directories(directory?: string): Promise<string[]>;
|
|
60
|
+
makeDirectory(dirPath: string): Promise<void>;
|
|
61
|
+
deleteDirectory(dirPath: string): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
declare function createStorage(config: StorageConfig): Storage;
|
|
64
|
+
declare function storage(): Storage;
|
|
65
|
+
|
|
66
|
+
export { type FileOptions, LocalDisk, Storage, type StorageConfig, createStorage, storage };
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// src/server/storage/index.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
var LocalDisk = class {
|
|
5
|
+
root;
|
|
6
|
+
baseUrl;
|
|
7
|
+
constructor(root, baseUrl) {
|
|
8
|
+
this.root = path.resolve(root);
|
|
9
|
+
this.baseUrl = baseUrl;
|
|
10
|
+
}
|
|
11
|
+
async put(filePath, content) {
|
|
12
|
+
const fullPath = this.resolvePath(filePath);
|
|
13
|
+
const dir = path.dirname(fullPath);
|
|
14
|
+
if (!fs.existsSync(dir)) {
|
|
15
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
16
|
+
}
|
|
17
|
+
fs.writeFileSync(fullPath, content);
|
|
18
|
+
return filePath;
|
|
19
|
+
}
|
|
20
|
+
async get(filePath) {
|
|
21
|
+
const fullPath = this.resolvePath(filePath);
|
|
22
|
+
if (!fs.existsSync(fullPath)) {
|
|
23
|
+
throw new Error(`File not found: ${filePath}`);
|
|
24
|
+
}
|
|
25
|
+
return fs.readFileSync(fullPath);
|
|
26
|
+
}
|
|
27
|
+
async exists(filePath) {
|
|
28
|
+
const fullPath = this.resolvePath(filePath);
|
|
29
|
+
return fs.existsSync(fullPath);
|
|
30
|
+
}
|
|
31
|
+
async delete(filePath) {
|
|
32
|
+
const fullPath = this.resolvePath(filePath);
|
|
33
|
+
if (!fs.existsSync(fullPath)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
fs.unlinkSync(fullPath);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
async copy(from, to) {
|
|
40
|
+
const fromPath = this.resolvePath(from);
|
|
41
|
+
const toPath = this.resolvePath(to);
|
|
42
|
+
if (!fs.existsSync(fromPath)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const dir = path.dirname(toPath);
|
|
46
|
+
if (!fs.existsSync(dir)) {
|
|
47
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
48
|
+
}
|
|
49
|
+
fs.copyFileSync(fromPath, toPath);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
async move(from, to) {
|
|
53
|
+
const fromPath = this.resolvePath(from);
|
|
54
|
+
const toPath = this.resolvePath(to);
|
|
55
|
+
if (!fs.existsSync(fromPath)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const dir = path.dirname(toPath);
|
|
59
|
+
if (!fs.existsSync(dir)) {
|
|
60
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
fs.renameSync(fromPath, toPath);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
url(filePath) {
|
|
66
|
+
if (this.baseUrl === void 0) {
|
|
67
|
+
throw new Error("Base URL not configured for this disk");
|
|
68
|
+
}
|
|
69
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
70
|
+
return `${this.baseUrl.replace(/\/$/, "")}/${normalized.replace(/^\//, "")}`;
|
|
71
|
+
}
|
|
72
|
+
async size(filePath) {
|
|
73
|
+
const fullPath = this.resolvePath(filePath);
|
|
74
|
+
if (!fs.existsSync(fullPath)) {
|
|
75
|
+
throw new Error(`File not found: ${filePath}`);
|
|
76
|
+
}
|
|
77
|
+
return fs.statSync(fullPath).size;
|
|
78
|
+
}
|
|
79
|
+
async lastModified(filePath) {
|
|
80
|
+
const fullPath = this.resolvePath(filePath);
|
|
81
|
+
if (!fs.existsSync(fullPath)) {
|
|
82
|
+
throw new Error(`File not found: ${filePath}`);
|
|
83
|
+
}
|
|
84
|
+
return fs.statSync(fullPath).mtime;
|
|
85
|
+
}
|
|
86
|
+
async files(directory = "") {
|
|
87
|
+
const fullPath = this.resolvePath(directory);
|
|
88
|
+
if (!fs.existsSync(fullPath)) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
return fs.readdirSync(fullPath).filter((name) => {
|
|
92
|
+
const stat = fs.statSync(path.join(fullPath, name));
|
|
93
|
+
return stat.isFile();
|
|
94
|
+
}).map((name) => path.join(directory, name).replace(/\\/g, "/"));
|
|
95
|
+
}
|
|
96
|
+
async directories(directory = "") {
|
|
97
|
+
const fullPath = this.resolvePath(directory);
|
|
98
|
+
if (!fs.existsSync(fullPath)) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
return fs.readdirSync(fullPath).filter((name) => {
|
|
102
|
+
const stat = fs.statSync(path.join(fullPath, name));
|
|
103
|
+
return stat.isDirectory();
|
|
104
|
+
}).map((name) => path.join(directory, name).replace(/\\/g, "/"));
|
|
105
|
+
}
|
|
106
|
+
async makeDirectory(dirPath) {
|
|
107
|
+
const fullPath = this.resolvePath(dirPath);
|
|
108
|
+
fs.mkdirSync(fullPath, { recursive: true });
|
|
109
|
+
}
|
|
110
|
+
async deleteDirectory(dirPath) {
|
|
111
|
+
const fullPath = this.resolvePath(dirPath);
|
|
112
|
+
if (fs.existsSync(fullPath)) {
|
|
113
|
+
fs.rmSync(fullPath, { recursive: true, force: true });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async append(filePath, content) {
|
|
117
|
+
const fullPath = this.resolvePath(filePath);
|
|
118
|
+
const dir = path.dirname(fullPath);
|
|
119
|
+
if (!fs.existsSync(dir)) {
|
|
120
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
121
|
+
}
|
|
122
|
+
fs.appendFileSync(fullPath, content, "utf-8");
|
|
123
|
+
}
|
|
124
|
+
async prepend(filePath, content) {
|
|
125
|
+
const fullPath = this.resolvePath(filePath);
|
|
126
|
+
const dir = path.dirname(fullPath);
|
|
127
|
+
if (!fs.existsSync(dir)) {
|
|
128
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
129
|
+
}
|
|
130
|
+
const existing = fs.existsSync(fullPath) ? fs.readFileSync(fullPath, "utf-8") : "";
|
|
131
|
+
fs.writeFileSync(fullPath, content + existing, "utf-8");
|
|
132
|
+
}
|
|
133
|
+
readStream(filePath) {
|
|
134
|
+
const fullPath = this.resolvePath(filePath);
|
|
135
|
+
if (!fs.existsSync(fullPath)) {
|
|
136
|
+
throw new Error(`File not found: ${filePath}`);
|
|
137
|
+
}
|
|
138
|
+
return fs.createReadStream(fullPath);
|
|
139
|
+
}
|
|
140
|
+
writeStream(filePath) {
|
|
141
|
+
const fullPath = this.resolvePath(filePath);
|
|
142
|
+
const dir = path.dirname(fullPath);
|
|
143
|
+
if (!fs.existsSync(dir)) {
|
|
144
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
145
|
+
}
|
|
146
|
+
return fs.createWriteStream(fullPath);
|
|
147
|
+
}
|
|
148
|
+
getRoot() {
|
|
149
|
+
return this.root;
|
|
150
|
+
}
|
|
151
|
+
getUrl() {
|
|
152
|
+
return this.baseUrl;
|
|
153
|
+
}
|
|
154
|
+
resolvePath(filePath) {
|
|
155
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
156
|
+
const resolved = path.resolve(this.root, normalized);
|
|
157
|
+
const resolvedRoot = path.resolve(this.root);
|
|
158
|
+
if (!resolved.startsWith(resolvedRoot)) {
|
|
159
|
+
throw new Error(`Path traversal detected: ${filePath}`);
|
|
160
|
+
}
|
|
161
|
+
return resolved;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var Storage = class {
|
|
165
|
+
config;
|
|
166
|
+
diskInstances;
|
|
167
|
+
constructor(config) {
|
|
168
|
+
this.config = config;
|
|
169
|
+
this.diskInstances = /* @__PURE__ */ new Map();
|
|
170
|
+
}
|
|
171
|
+
disk(name) {
|
|
172
|
+
const diskName = name ?? this.config.defaultDisk ?? "local";
|
|
173
|
+
const existing = this.diskInstances.get(diskName);
|
|
174
|
+
if (existing !== void 0) {
|
|
175
|
+
return existing;
|
|
176
|
+
}
|
|
177
|
+
const diskConfig = this.config.disks[diskName];
|
|
178
|
+
if (diskConfig === void 0) {
|
|
179
|
+
throw new Error(`Disk not configured: ${diskName}`);
|
|
180
|
+
}
|
|
181
|
+
const instance = new LocalDisk(diskConfig.root, diskConfig.url);
|
|
182
|
+
this.diskInstances.set(diskName, instance);
|
|
183
|
+
return instance;
|
|
184
|
+
}
|
|
185
|
+
async put(filePath, content) {
|
|
186
|
+
return this.disk().put(filePath, content);
|
|
187
|
+
}
|
|
188
|
+
async get(filePath) {
|
|
189
|
+
return this.disk().get(filePath);
|
|
190
|
+
}
|
|
191
|
+
async exists(filePath) {
|
|
192
|
+
return this.disk().exists(filePath);
|
|
193
|
+
}
|
|
194
|
+
async delete(filePath) {
|
|
195
|
+
return this.disk().delete(filePath);
|
|
196
|
+
}
|
|
197
|
+
async copy(from, to) {
|
|
198
|
+
return this.disk().copy(from, to);
|
|
199
|
+
}
|
|
200
|
+
async move(from, to) {
|
|
201
|
+
return this.disk().move(from, to);
|
|
202
|
+
}
|
|
203
|
+
async url(filePath) {
|
|
204
|
+
return this.disk().url(filePath);
|
|
205
|
+
}
|
|
206
|
+
async size(filePath) {
|
|
207
|
+
return this.disk().size(filePath);
|
|
208
|
+
}
|
|
209
|
+
async lastModified(filePath) {
|
|
210
|
+
return this.disk().lastModified(filePath);
|
|
211
|
+
}
|
|
212
|
+
async files(directory) {
|
|
213
|
+
return this.disk().files(directory);
|
|
214
|
+
}
|
|
215
|
+
async directories(directory) {
|
|
216
|
+
return this.disk().directories(directory);
|
|
217
|
+
}
|
|
218
|
+
async makeDirectory(dirPath) {
|
|
219
|
+
return this.disk().makeDirectory(dirPath);
|
|
220
|
+
}
|
|
221
|
+
async deleteDirectory(dirPath) {
|
|
222
|
+
return this.disk().deleteDirectory(dirPath);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
var defaultInstance = null;
|
|
226
|
+
function createStorage(config) {
|
|
227
|
+
defaultInstance = new Storage(config);
|
|
228
|
+
return defaultInstance;
|
|
229
|
+
}
|
|
230
|
+
function storage() {
|
|
231
|
+
if (defaultInstance === null) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
"Storage not initialized. Call createStorage(config) first."
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return defaultInstance;
|
|
237
|
+
}
|
|
238
|
+
export {
|
|
239
|
+
LocalDisk,
|
|
240
|
+
Storage,
|
|
241
|
+
createStorage,
|
|
242
|
+
storage
|
|
243
|
+
};
|
|
244
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/storage/index.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\nexport interface StorageConfig {\n\tdefaultDisk?: string;\n\tdisks: {\n\t\t[name: string]: {\n\t\t\tdriver: \"local\";\n\t\t\troot: string;\n\t\t\turl?: string;\n\t\t\tpermissions?: number;\n\t\t};\n\t};\n}\n\nexport interface FileOptions {\n\tname?: string;\n\tdisk?: string;\n\toverwrite?: boolean;\n}\n\nexport class LocalDisk {\n\tprivate root: string;\n\tprivate baseUrl?: string;\n\n\tconstructor(root: string, baseUrl?: string) {\n\t\tthis.root = path.resolve(root);\n\t\tthis.baseUrl = baseUrl;\n\t}\n\n\tasync put(filePath: string, content: string | Buffer): Promise<string> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tconst dir = path.dirname(fullPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\tfs.writeFileSync(fullPath, content);\n\t\treturn filePath;\n\t}\n\n\tasync get(filePath: string): Promise<Buffer> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\tthrow new Error(`File not found: ${filePath}`);\n\t\t}\n\t\treturn fs.readFileSync(fullPath);\n\t}\n\n\tasync exists(filePath: string): Promise<boolean> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\treturn fs.existsSync(fullPath);\n\t}\n\n\tasync delete(filePath: string): Promise<boolean> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\treturn false;\n\t\t}\n\t\tfs.unlinkSync(fullPath);\n\t\treturn true;\n\t}\n\n\tasync copy(from: string, to: string): Promise<boolean> {\n\t\tconst fromPath = this.resolvePath(from);\n\t\tconst toPath = this.resolvePath(to);\n\t\tif (!fs.existsSync(fromPath)) {\n\t\t\treturn false;\n\t\t}\n\t\tconst dir = path.dirname(toPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\tfs.copyFileSync(fromPath, toPath);\n\t\treturn true;\n\t}\n\n\tasync move(from: string, to: string): Promise<boolean> {\n\t\tconst fromPath = this.resolvePath(from);\n\t\tconst toPath = this.resolvePath(to);\n\t\tif (!fs.existsSync(fromPath)) {\n\t\t\treturn false;\n\t\t}\n\t\tconst dir = path.dirname(toPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\tfs.renameSync(fromPath, toPath);\n\t\treturn true;\n\t}\n\n\turl(filePath: string): string {\n\t\tif (this.baseUrl === undefined) {\n\t\t\tthrow new Error(\"Base URL not configured for this disk\");\n\t\t}\n\t\tconst normalized = filePath.replace(/\\\\/g, \"/\");\n\t\treturn `${this.baseUrl.replace(/\\/$/, \"\")}/${normalized.replace(/^\\//, \"\")}`;\n\t}\n\n\tasync size(filePath: string): Promise<number> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\tthrow new Error(`File not found: ${filePath}`);\n\t\t}\n\t\treturn fs.statSync(fullPath).size;\n\t}\n\n\tasync lastModified(filePath: string): Promise<Date> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\tthrow new Error(`File not found: ${filePath}`);\n\t\t}\n\t\treturn fs.statSync(fullPath).mtime;\n\t}\n\n\tasync files(directory: string = \"\"): Promise<string[]> {\n\t\tconst fullPath = this.resolvePath(directory);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn fs\n\t\t\t.readdirSync(fullPath)\n\t\t\t.filter((name) => {\n\t\t\t\tconst stat = fs.statSync(path.join(fullPath, name));\n\t\t\t\treturn stat.isFile();\n\t\t\t})\n\t\t\t.map((name) => path.join(directory, name).replace(/\\\\/g, \"/\"));\n\t}\n\n\tasync directories(directory: string = \"\"): Promise<string[]> {\n\t\tconst fullPath = this.resolvePath(directory);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn fs\n\t\t\t.readdirSync(fullPath)\n\t\t\t.filter((name) => {\n\t\t\t\tconst stat = fs.statSync(path.join(fullPath, name));\n\t\t\t\treturn stat.isDirectory();\n\t\t\t})\n\t\t\t.map((name) => path.join(directory, name).replace(/\\\\/g, \"/\"));\n\t}\n\n\tasync makeDirectory(dirPath: string): Promise<void> {\n\t\tconst fullPath = this.resolvePath(dirPath);\n\t\tfs.mkdirSync(fullPath, { recursive: true });\n\t}\n\n\tasync deleteDirectory(dirPath: string): Promise<void> {\n\t\tconst fullPath = this.resolvePath(dirPath);\n\t\tif (fs.existsSync(fullPath)) {\n\t\t\tfs.rmSync(fullPath, { recursive: true, force: true });\n\t\t}\n\t}\n\n\tasync append(filePath: string, content: string): Promise<void> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tconst dir = path.dirname(fullPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\tfs.appendFileSync(fullPath, content, \"utf-8\");\n\t}\n\n\tasync prepend(filePath: string, content: string): Promise<void> {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tconst dir = path.dirname(fullPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\tconst existing = fs.existsSync(fullPath)\n\t\t\t? fs.readFileSync(fullPath, \"utf-8\")\n\t\t\t: \"\";\n\t\tfs.writeFileSync(fullPath, content + existing, \"utf-8\");\n\t}\n\n\treadStream(filePath: string): fs.ReadStream {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tif (!fs.existsSync(fullPath)) {\n\t\t\tthrow new Error(`File not found: ${filePath}`);\n\t\t}\n\t\treturn fs.createReadStream(fullPath);\n\t}\n\n\twriteStream(filePath: string): fs.WriteStream {\n\t\tconst fullPath = this.resolvePath(filePath);\n\t\tconst dir = path.dirname(fullPath);\n\t\tif (!fs.existsSync(dir)) {\n\t\t\tfs.mkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn fs.createWriteStream(fullPath);\n\t}\n\n\tgetRoot(): string {\n\t\treturn this.root;\n\t}\n\n\tgetUrl(): string | undefined {\n\t\treturn this.baseUrl;\n\t}\n\n\tprivate resolvePath(filePath: string): string {\n\t\tconst normalized = filePath.replace(/\\\\/g, \"/\");\n\t\tconst resolved = path.resolve(this.root, normalized);\n\t\tconst resolvedRoot = path.resolve(this.root);\n\n\t\tif (!resolved.startsWith(resolvedRoot)) {\n\t\t\tthrow new Error(`Path traversal detected: ${filePath}`);\n\t\t}\n\n\t\treturn resolved;\n\t}\n}\n\nexport class Storage {\n\tprivate config: StorageConfig;\n\tprivate diskInstances: Map<string, LocalDisk>;\n\n\tconstructor(config: StorageConfig) {\n\t\tthis.config = config;\n\t\tthis.diskInstances = new Map();\n\t}\n\n\tdisk(name?: string): LocalDisk {\n\t\tconst diskName = name ?? this.config.defaultDisk ?? \"local\";\n\t\tconst existing = this.diskInstances.get(diskName);\n\t\tif (existing !== undefined) {\n\t\t\treturn existing;\n\t\t}\n\n\t\tconst diskConfig = this.config.disks[diskName];\n\t\tif (diskConfig === undefined) {\n\t\t\tthrow new Error(`Disk not configured: ${diskName}`);\n\t\t}\n\n\t\tconst instance = new LocalDisk(diskConfig.root, diskConfig.url);\n\t\tthis.diskInstances.set(diskName, instance);\n\t\treturn instance;\n\t}\n\n\tasync put(filePath: string, content: string | Buffer): Promise<string> {\n\t\treturn this.disk().put(filePath, content);\n\t}\n\n\tasync get(filePath: string): Promise<Buffer> {\n\t\treturn this.disk().get(filePath);\n\t}\n\n\tasync exists(filePath: string): Promise<boolean> {\n\t\treturn this.disk().exists(filePath);\n\t}\n\n\tasync delete(filePath: string): Promise<boolean> {\n\t\treturn this.disk().delete(filePath);\n\t}\n\n\tasync copy(from: string, to: string): Promise<boolean> {\n\t\treturn this.disk().copy(from, to);\n\t}\n\n\tasync move(from: string, to: string): Promise<boolean> {\n\t\treturn this.disk().move(from, to);\n\t}\n\n\tasync url(filePath: string): Promise<string> {\n\t\treturn this.disk().url(filePath);\n\t}\n\n\tasync size(filePath: string): Promise<number> {\n\t\treturn this.disk().size(filePath);\n\t}\n\n\tasync lastModified(filePath: string): Promise<Date> {\n\t\treturn this.disk().lastModified(filePath);\n\t}\n\n\tasync files(directory?: string): Promise<string[]> {\n\t\treturn this.disk().files(directory);\n\t}\n\n\tasync directories(directory?: string): Promise<string[]> {\n\t\treturn this.disk().directories(directory);\n\t}\n\n\tasync makeDirectory(dirPath: string): Promise<void> {\n\t\treturn this.disk().makeDirectory(dirPath);\n\t}\n\n\tasync deleteDirectory(dirPath: string): Promise<void> {\n\t\treturn this.disk().deleteDirectory(dirPath);\n\t}\n}\n\nlet defaultInstance: Storage | null = null;\n\nexport function createStorage(config: StorageConfig): Storage {\n\tdefaultInstance = new Storage(config);\n\treturn defaultInstance;\n}\n\nexport function storage(): Storage {\n\tif (defaultInstance === null) {\n\t\tthrow new Error(\n\t\t\t\"Storage not initialized. Call createStorage(config) first.\",\n\t\t);\n\t}\n\treturn defaultInstance;\n}\n"],"mappings":";AAAA,YAAY,QAAQ;AACpB,YAAY,UAAU;AAoBf,IAAM,YAAN,MAAgB;AAAA,EACd;AAAA,EACA;AAAA,EAER,YAAY,MAAc,SAAkB;AAC3C,SAAK,OAAY,aAAQ,IAAI;AAC7B,SAAK,UAAU;AAAA,EAChB;AAAA,EAEA,MAAM,IAAI,UAAkB,SAA2C;AACtE,UAAM,WAAW,KAAK,YAA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,KAAK,KAAK,EAAE,gBAAgB,OAAO;AAAA,EAC3C;AACD;AAEA,IAAI,kBAAkC;AAE/B,SAAS,cAAc,QAAgC;AAC7D,oBAAkB,IAAI,QAAQ,MAAM;AACpC,SAAO;AACR;AAEO,SAAS,UAAmB;AAClC,MAAI,oBAAoB,MAAM;AAC7B,UAAM,IAAI;AAAA,MACT;AAAA,IACD;AAAA,EACD;AACA,SAAO;AACR;","names":[]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { S as SuperRequest, a as SuperResponse } from './response-Ca8KWK5_.js';
|
|
2
|
+
|
|
3
|
+
interface AuthUser {
|
|
4
|
+
id: string | number;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
interface UserProvider {
|
|
8
|
+
findById(id: string | number): Promise<AuthUser | null>;
|
|
9
|
+
findByCredential(field: string, value: string): Promise<AuthUser | null>;
|
|
10
|
+
}
|
|
11
|
+
interface SessionGuardConfig {
|
|
12
|
+
cookieName?: string;
|
|
13
|
+
lifetime?: number;
|
|
14
|
+
table?: string;
|
|
15
|
+
identifier?: string;
|
|
16
|
+
password?: string;
|
|
17
|
+
provider?: UserProvider;
|
|
18
|
+
encryptionKey?: string;
|
|
19
|
+
}
|
|
20
|
+
declare class SessionGuard {
|
|
21
|
+
private config;
|
|
22
|
+
private req;
|
|
23
|
+
private res;
|
|
24
|
+
private cachedPayload;
|
|
25
|
+
constructor(config?: SessionGuardConfig);
|
|
26
|
+
setContext(req: SuperRequest, res: SuperResponse): this;
|
|
27
|
+
attempt(credentials: {
|
|
28
|
+
email: string;
|
|
29
|
+
password: string;
|
|
30
|
+
}, remember?: boolean): Promise<boolean>;
|
|
31
|
+
login(userId: string | number, remember?: boolean): Promise<void>;
|
|
32
|
+
loginUser(user: AuthUser): Promise<void>;
|
|
33
|
+
logout(): Promise<void>;
|
|
34
|
+
user(): Promise<AuthUser | null>;
|
|
35
|
+
check(): Promise<boolean>;
|
|
36
|
+
guest(): Promise<boolean>;
|
|
37
|
+
id(): Promise<string | number | null>;
|
|
38
|
+
set(key: string, value: unknown): Promise<void>;
|
|
39
|
+
get(key: string): Promise<unknown>;
|
|
40
|
+
private createSession;
|
|
41
|
+
private readSession;
|
|
42
|
+
private writeSessionCookie;
|
|
43
|
+
private calculateExpiry;
|
|
44
|
+
private encryptSession;
|
|
45
|
+
private decryptSession;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { type AuthUser as A, SessionGuard as S, type UserProvider as U, type SessionGuardConfig as a };
|