skyguard-js 1.2.1 → 1.2.3
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 +11 -680
- package/dist/app.d.ts +14 -9
- package/dist/app.js +37 -30
- package/dist/http/context.d.ts +115 -0
- package/dist/http/context.js +147 -0
- package/dist/http/httpAdapter.d.ts +4 -4
- package/dist/http/index.d.ts +3 -1
- package/dist/http/index.js +5 -1
- package/dist/http/logger.d.ts +9 -2
- package/dist/http/logger.js +49 -1
- package/dist/http/nodeNativeHttp.d.ts +4 -4
- package/dist/http/nodeNativeHttp.js +11 -4
- package/dist/http/request.d.ts +4 -0
- package/dist/http/request.js +8 -0
- package/dist/http/response.d.ts +21 -2
- package/dist/http/response.js +30 -2
- package/dist/http/webHttp.d.ts +19 -0
- package/dist/http/webHttp.js +95 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/middlewares/cors.d.ts +10 -4
- package/dist/middlewares/cors.js +35 -18
- package/dist/middlewares/csrf.js +33 -33
- package/dist/middlewares/index.d.ts +1 -1
- package/dist/middlewares/rateLimiter.d.ts +58 -6
- package/dist/middlewares/rateLimiter.js +149 -40
- package/dist/middlewares/session.js +4 -4
- package/dist/parsers/contentParserManager.d.ts +4 -2
- package/dist/parsers/contentParserManager.js +22 -3
- package/dist/routing/routeResolveFunc.d.ts +10 -0
- package/dist/routing/routeResolveFunc.js +21 -0
- package/dist/routing/router.d.ts +16 -10
- package/dist/routing/router.js +32 -25
- package/dist/routing/routerGroup.d.ts +10 -5
- package/dist/routing/routerGroup.js +11 -10
- package/dist/server/bunRuntimeServer.d.ts +7 -0
- package/dist/server/bunRuntimeServer.js +28 -0
- package/dist/server/createRuntimeServer.d.ts +4 -0
- package/dist/server/createRuntimeServer.js +23 -0
- package/dist/server/denoRuntimeServer.d.ts +7 -0
- package/dist/server/denoRuntimeServer.js +27 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +19 -0
- package/dist/server/modulePath.d.ts +6 -0
- package/dist/server/modulePath.js +18 -0
- package/dist/server/nodeRuntimeServer.d.ts +7 -0
- package/dist/server/nodeRuntimeServer.js +21 -0
- package/dist/server/runtimeDetector.d.ts +4 -0
- package/dist/server/runtimeDetector.js +15 -0
- package/dist/server/types.d.ts +11 -0
- package/dist/server/types.js +2 -0
- package/dist/sessions/index.d.ts +2 -2
- package/dist/storage/storage.d.ts +3 -3
- package/dist/storage/storage.js +7 -7
- package/dist/storage/types.d.ts +5 -5
- package/dist/storage/uploader.d.ts +9 -9
- package/dist/storage/uploader.js +62 -62
- package/dist/types/index.d.ts +11 -10
- package/dist/validators/index.d.ts +2 -2
- package/dist/validators/rules/index.d.ts +5 -5
- package/dist/validators/validationSchema.js +8 -8
- package/package.json +2 -2
- package/dist/helpers/http.d.ts +0 -95
- package/dist/helpers/http.js +0 -112
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RouterGroup = void 0;
|
|
4
4
|
const buildFullPath_1 = require("./buildFullPath");
|
|
5
|
+
const routeResolveFunc_1 = require("./routeResolveFunc");
|
|
5
6
|
/**
|
|
6
7
|
* Route group helper.
|
|
7
8
|
*
|
|
@@ -64,24 +65,24 @@ class RouterGroup {
|
|
|
64
65
|
if (totalMiddlewares.length > 0)
|
|
65
66
|
layer.setMiddlewares(totalMiddlewares);
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
get(path, handlerOrMiddleware, handler) {
|
|
69
|
+
const { action, middlewares } = (0, routeResolveFunc_1.normalizeRouteArgs)(handlerOrMiddleware, handler);
|
|
69
70
|
this.addRoute("get", path, action, middlewares);
|
|
70
71
|
}
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
post(path, handlerOrMiddleware, handler) {
|
|
73
|
+
const { action, middlewares } = (0, routeResolveFunc_1.normalizeRouteArgs)(handlerOrMiddleware, handler);
|
|
73
74
|
this.addRoute("post", path, action, middlewares);
|
|
74
75
|
}
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
put(path, handlerOrMiddleware, handler) {
|
|
77
|
+
const { action, middlewares } = (0, routeResolveFunc_1.normalizeRouteArgs)(handlerOrMiddleware, handler);
|
|
77
78
|
this.addRoute("put", path, action, middlewares);
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
patch(path, handlerOrMiddleware, handler) {
|
|
81
|
+
const { action, middlewares } = (0, routeResolveFunc_1.normalizeRouteArgs)(handlerOrMiddleware, handler);
|
|
81
82
|
this.addRoute("patch", path, action, middlewares);
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
delete(path, handlerOrMiddleware, handler) {
|
|
85
|
+
const { action, middlewares } = (0, routeResolveFunc_1.normalizeRouteArgs)(handlerOrMiddleware, handler);
|
|
85
86
|
this.addRoute("delete", path, action, middlewares);
|
|
86
87
|
}
|
|
87
88
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LoggerOptions } from "../http/logger";
|
|
2
|
+
import type { AdapterHandler, RuntimeListenOptions, RuntimeServer } from "./types";
|
|
3
|
+
export declare class BunRuntimeServer implements RuntimeServer {
|
|
4
|
+
private readonly loggerOptions;
|
|
5
|
+
constructor(loggerOptions?: LoggerOptions);
|
|
6
|
+
listen(handler: AdapterHandler, options: RuntimeListenOptions): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BunRuntimeServer = void 0;
|
|
4
|
+
const webHttp_1 = require("../http/webHttp");
|
|
5
|
+
class BunRuntimeServer {
|
|
6
|
+
loggerOptions;
|
|
7
|
+
constructor(loggerOptions = {}) {
|
|
8
|
+
this.loggerOptions = loggerOptions;
|
|
9
|
+
}
|
|
10
|
+
listen(handler, options) {
|
|
11
|
+
const bun = globalThis.Bun;
|
|
12
|
+
if (!bun) {
|
|
13
|
+
throw new Error("Bun runtime not detected");
|
|
14
|
+
}
|
|
15
|
+
bun.serve({
|
|
16
|
+
hostname: options.hostname,
|
|
17
|
+
port: options.port,
|
|
18
|
+
fetch: async (request) => {
|
|
19
|
+
const adapter = new webHttp_1.WebHttpAdapter(request, this.loggerOptions);
|
|
20
|
+
await handler(adapter);
|
|
21
|
+
return adapter.toWebResponse();
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
if (options.callback)
|
|
25
|
+
options.callback();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.BunRuntimeServer = BunRuntimeServer;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { LoggerOptions } from "../http/logger";
|
|
2
|
+
import type { RuntimeKind, RuntimeServer } from "./types";
|
|
3
|
+
export declare const createRuntimeServer: (loggerOptions?: LoggerOptions, runtime?: RuntimeKind) => RuntimeServer;
|
|
4
|
+
export declare const createRuntimeServerByKind: (runtime: RuntimeKind, loggerOptions?: LoggerOptions) => RuntimeServer;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRuntimeServerByKind = exports.createRuntimeServer = void 0;
|
|
4
|
+
const bunRuntimeServer_1 = require("./bunRuntimeServer");
|
|
5
|
+
const denoRuntimeServer_1 = require("./denoRuntimeServer");
|
|
6
|
+
const nodeRuntimeServer_1 = require("./nodeRuntimeServer");
|
|
7
|
+
const runtimeDetector_1 = require("./runtimeDetector");
|
|
8
|
+
const createRuntimeServer = (loggerOptions = {}, runtime = (0, runtimeDetector_1.detectRuntime)()) => {
|
|
9
|
+
switch (runtime) {
|
|
10
|
+
case "bun":
|
|
11
|
+
return new bunRuntimeServer_1.BunRuntimeServer(loggerOptions);
|
|
12
|
+
case "deno":
|
|
13
|
+
return new denoRuntimeServer_1.DenoRuntimeServer(loggerOptions);
|
|
14
|
+
case "node":
|
|
15
|
+
default:
|
|
16
|
+
return new nodeRuntimeServer_1.NodeRuntimeServer(loggerOptions);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
exports.createRuntimeServer = createRuntimeServer;
|
|
20
|
+
const createRuntimeServerByKind = (runtime, loggerOptions = {}) => {
|
|
21
|
+
return (0, exports.createRuntimeServer)(loggerOptions, runtime);
|
|
22
|
+
};
|
|
23
|
+
exports.createRuntimeServerByKind = createRuntimeServerByKind;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LoggerOptions } from "../http/logger";
|
|
2
|
+
import type { AdapterHandler, RuntimeListenOptions, RuntimeServer } from "./types";
|
|
3
|
+
export declare class DenoRuntimeServer implements RuntimeServer {
|
|
4
|
+
private readonly loggerOptions;
|
|
5
|
+
constructor(loggerOptions?: LoggerOptions);
|
|
6
|
+
listen(handler: AdapterHandler, options: RuntimeListenOptions): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DenoRuntimeServer = void 0;
|
|
4
|
+
const webHttp_1 = require("../http/webHttp");
|
|
5
|
+
class DenoRuntimeServer {
|
|
6
|
+
loggerOptions;
|
|
7
|
+
constructor(loggerOptions = {}) {
|
|
8
|
+
this.loggerOptions = loggerOptions;
|
|
9
|
+
}
|
|
10
|
+
listen(handler, options) {
|
|
11
|
+
const deno = globalThis.Deno;
|
|
12
|
+
if (!deno) {
|
|
13
|
+
throw new Error("Deno runtime not detected");
|
|
14
|
+
}
|
|
15
|
+
deno.serve({
|
|
16
|
+
hostname: options.hostname,
|
|
17
|
+
port: options.port,
|
|
18
|
+
}, async (request) => {
|
|
19
|
+
const adapter = new webHttp_1.WebHttpAdapter(request, this.loggerOptions);
|
|
20
|
+
await handler(adapter);
|
|
21
|
+
return adapter.toWebResponse();
|
|
22
|
+
});
|
|
23
|
+
if (options.callback)
|
|
24
|
+
options.callback();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.DenoRuntimeServer = DenoRuntimeServer;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createRuntimeServer, createRuntimeServerByKind, } from "./createRuntimeServer";
|
|
2
|
+
export { detectRuntime, isBunRuntime, isDenoRuntime } from "./runtimeDetector";
|
|
3
|
+
export { NodeRuntimeServer } from "./nodeRuntimeServer";
|
|
4
|
+
export { BunRuntimeServer } from "./bunRuntimeServer";
|
|
5
|
+
export { DenoRuntimeServer } from "./denoRuntimeServer";
|
|
6
|
+
export type { AdapterHandler, RuntimeKind, RuntimeListenOptions, RuntimeServer, } from "./types";
|
|
7
|
+
export { getModulePathInfo, resolveFromModuleUrl, type ModulePathInfo, } from "./modulePath";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveFromModuleUrl = exports.getModulePathInfo = exports.DenoRuntimeServer = exports.BunRuntimeServer = exports.NodeRuntimeServer = exports.isDenoRuntime = exports.isBunRuntime = exports.detectRuntime = exports.createRuntimeServerByKind = exports.createRuntimeServer = void 0;
|
|
4
|
+
var createRuntimeServer_1 = require("./createRuntimeServer");
|
|
5
|
+
Object.defineProperty(exports, "createRuntimeServer", { enumerable: true, get: function () { return createRuntimeServer_1.createRuntimeServer; } });
|
|
6
|
+
Object.defineProperty(exports, "createRuntimeServerByKind", { enumerable: true, get: function () { return createRuntimeServer_1.createRuntimeServerByKind; } });
|
|
7
|
+
var runtimeDetector_1 = require("./runtimeDetector");
|
|
8
|
+
Object.defineProperty(exports, "detectRuntime", { enumerable: true, get: function () { return runtimeDetector_1.detectRuntime; } });
|
|
9
|
+
Object.defineProperty(exports, "isBunRuntime", { enumerable: true, get: function () { return runtimeDetector_1.isBunRuntime; } });
|
|
10
|
+
Object.defineProperty(exports, "isDenoRuntime", { enumerable: true, get: function () { return runtimeDetector_1.isDenoRuntime; } });
|
|
11
|
+
var nodeRuntimeServer_1 = require("./nodeRuntimeServer");
|
|
12
|
+
Object.defineProperty(exports, "NodeRuntimeServer", { enumerable: true, get: function () { return nodeRuntimeServer_1.NodeRuntimeServer; } });
|
|
13
|
+
var bunRuntimeServer_1 = require("./bunRuntimeServer");
|
|
14
|
+
Object.defineProperty(exports, "BunRuntimeServer", { enumerable: true, get: function () { return bunRuntimeServer_1.BunRuntimeServer; } });
|
|
15
|
+
var denoRuntimeServer_1 = require("./denoRuntimeServer");
|
|
16
|
+
Object.defineProperty(exports, "DenoRuntimeServer", { enumerable: true, get: function () { return denoRuntimeServer_1.DenoRuntimeServer; } });
|
|
17
|
+
var modulePath_1 = require("./modulePath");
|
|
18
|
+
Object.defineProperty(exports, "getModulePathInfo", { enumerable: true, get: function () { return modulePath_1.getModulePathInfo; } });
|
|
19
|
+
Object.defineProperty(exports, "resolveFromModuleUrl", { enumerable: true, get: function () { return modulePath_1.resolveFromModuleUrl; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveFromModuleUrl = exports.getModulePathInfo = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const node_url_1 = require("node:url");
|
|
6
|
+
const getModulePathInfo = (metaUrl) => {
|
|
7
|
+
const filename = (0, node_url_1.fileURLToPath)(metaUrl);
|
|
8
|
+
return {
|
|
9
|
+
filename,
|
|
10
|
+
dirname: (0, node_path_1.dirname)(filename),
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
exports.getModulePathInfo = getModulePathInfo;
|
|
14
|
+
const resolveFromModuleUrl = (metaUrl, ...segments) => {
|
|
15
|
+
const { dirname: moduleDirname } = (0, exports.getModulePathInfo)(metaUrl);
|
|
16
|
+
return (0, node_path_1.join)(moduleDirname, ...segments);
|
|
17
|
+
};
|
|
18
|
+
exports.resolveFromModuleUrl = resolveFromModuleUrl;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LoggerOptions } from "../http/logger";
|
|
2
|
+
import type { RuntimeListenOptions, RuntimeServer, AdapterHandler } from "./types";
|
|
3
|
+
export declare class NodeRuntimeServer implements RuntimeServer {
|
|
4
|
+
private readonly loggerOptions;
|
|
5
|
+
constructor(loggerOptions?: LoggerOptions);
|
|
6
|
+
listen(handler: AdapterHandler, options: RuntimeListenOptions): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NodeRuntimeServer = void 0;
|
|
4
|
+
const node_http_1 = require("node:http");
|
|
5
|
+
const nodeNativeHttp_1 = require("../http/nodeNativeHttp");
|
|
6
|
+
class NodeRuntimeServer {
|
|
7
|
+
loggerOptions;
|
|
8
|
+
constructor(loggerOptions = {}) {
|
|
9
|
+
this.loggerOptions = loggerOptions;
|
|
10
|
+
}
|
|
11
|
+
listen(handler, options) {
|
|
12
|
+
(0, node_http_1.createServer)((req, res) => {
|
|
13
|
+
const adapter = new nodeNativeHttp_1.NodeHttpAdapter(req, res, this.loggerOptions);
|
|
14
|
+
void handler(adapter);
|
|
15
|
+
}).listen(options.port, options.hostname, () => {
|
|
16
|
+
if (options.callback)
|
|
17
|
+
options.callback();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.NodeRuntimeServer = NodeRuntimeServer;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.detectRuntime = exports.isDenoRuntime = exports.isBunRuntime = void 0;
|
|
4
|
+
const isBunRuntime = () => typeof globalThis.Bun !== "undefined";
|
|
5
|
+
exports.isBunRuntime = isBunRuntime;
|
|
6
|
+
const isDenoRuntime = () => typeof globalThis.Deno !== "undefined";
|
|
7
|
+
exports.isDenoRuntime = isDenoRuntime;
|
|
8
|
+
const detectRuntime = () => {
|
|
9
|
+
if ((0, exports.isBunRuntime)())
|
|
10
|
+
return "bun";
|
|
11
|
+
if ((0, exports.isDenoRuntime)())
|
|
12
|
+
return "deno";
|
|
13
|
+
return "node";
|
|
14
|
+
};
|
|
15
|
+
exports.detectRuntime = detectRuntime;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HttpAdapter } from "../http/httpAdapter";
|
|
2
|
+
export type RuntimeKind = "node" | "bun" | "deno";
|
|
3
|
+
export interface RuntimeListenOptions {
|
|
4
|
+
port: number;
|
|
5
|
+
hostname?: string;
|
|
6
|
+
callback?: VoidFunction;
|
|
7
|
+
}
|
|
8
|
+
export type AdapterHandler = (adapter: HttpAdapter) => Promise<void>;
|
|
9
|
+
export interface RuntimeServer {
|
|
10
|
+
listen(handler: AdapterHandler, options: RuntimeListenOptions): void;
|
|
11
|
+
}
|
package/dist/sessions/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { MemorySessionStorage } from "./memorySessionStorage";
|
|
2
2
|
export { FileSessionStorage } from "./fileSessionStorage";
|
|
3
|
-
export { DatabaseSessionStorage, SessionDatabaseAdapter, } from "./databaseSessionStorage";
|
|
3
|
+
export { DatabaseSessionStorage, type SessionDatabaseAdapter, } from "./databaseSessionStorage";
|
|
4
4
|
export { Session } from "./session";
|
|
5
|
-
export { SessionStorage } from "./sessionStorage";
|
|
5
|
+
export type { SessionStorage } from "./sessionStorage";
|
|
6
6
|
export { parseCookies, serializeCookie } from "./cookies";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Storage, type StorageOptions } from "./types";
|
|
2
|
-
import { Request } from "../http/request";
|
|
3
2
|
import type { UploadedFile } from "../parsers/parserInterface";
|
|
3
|
+
import { Context } from "../http/context";
|
|
4
4
|
/**
|
|
5
5
|
* Disk-based storage engine responsible for persisting uploaded files
|
|
6
6
|
* to the local filesystem.
|
|
@@ -51,7 +51,7 @@ export declare class DiskStorage implements Storage {
|
|
|
51
51
|
*
|
|
52
52
|
* @throws UploadException if the file cannot be written to disk.
|
|
53
53
|
*/
|
|
54
|
-
handleFile(
|
|
54
|
+
handleFile(context: Context, file: Partial<UploadedFile>, fileData: Buffer): Promise<UploadedFile>;
|
|
55
55
|
/**
|
|
56
56
|
* Removes a previously stored file from disk.
|
|
57
57
|
*
|
|
@@ -99,6 +99,6 @@ export declare class MemoryStorage implements Storage {
|
|
|
99
99
|
private ttlMs;
|
|
100
100
|
private computeChecksum;
|
|
101
101
|
constructor(options?: StorageOptions);
|
|
102
|
-
handleFile(
|
|
102
|
+
handleFile(context: Context, file: Partial<UploadedFile>, fileData: Buffer): UploadedFile;
|
|
103
103
|
removeFile(file: UploadedFile): void;
|
|
104
104
|
}
|
package/dist/storage/storage.js
CHANGED
|
@@ -60,10 +60,10 @@ class DiskStorage {
|
|
|
60
60
|
*
|
|
61
61
|
* @throws UploadException if the file cannot be written to disk.
|
|
62
62
|
*/
|
|
63
|
-
async handleFile(
|
|
63
|
+
async handleFile(context, file, fileData) {
|
|
64
64
|
try {
|
|
65
|
-
const destination = await this.resolveDestination(
|
|
66
|
-
const filename = await this.filenameGenerator(
|
|
65
|
+
const destination = await this.resolveDestination(context, file);
|
|
66
|
+
const filename = await this.filenameGenerator(context, file);
|
|
67
67
|
const filePath = (0, node_path_1.join)(destination, filename);
|
|
68
68
|
await (0, promises_1.mkdir)(destination, { recursive: true });
|
|
69
69
|
await (0, promises_1.writeFile)(filePath, fileData);
|
|
@@ -102,9 +102,9 @@ class DiskStorage {
|
|
|
102
102
|
* If a resolver function was provided, it will be executed.
|
|
103
103
|
* Otherwise the static directory is returned.
|
|
104
104
|
*/
|
|
105
|
-
async resolveDestination(
|
|
105
|
+
async resolveDestination(context, file) {
|
|
106
106
|
if (typeof this.destination === "function") {
|
|
107
|
-
return await this.destination(
|
|
107
|
+
return await this.destination(context, file);
|
|
108
108
|
}
|
|
109
109
|
return this.destination;
|
|
110
110
|
}
|
|
@@ -119,7 +119,7 @@ class DiskStorage {
|
|
|
119
119
|
* Example:
|
|
120
120
|
* 1700000000000-a3f9b1c2d4e5f678.png
|
|
121
121
|
*/
|
|
122
|
-
generateUniqueFilename(
|
|
122
|
+
generateUniqueFilename(context, file) {
|
|
123
123
|
const timestamp = Date.now();
|
|
124
124
|
const randomString = (0, node_crypto_1.randomBytes)(8).toString("hex");
|
|
125
125
|
const ext = (0, node_path_1.extname)(file.originalname || "");
|
|
@@ -151,7 +151,7 @@ class MemoryStorage {
|
|
|
151
151
|
this.ttlMs = options.memory.ttlMs ?? 0;
|
|
152
152
|
this.computeChecksum = options.memory.computeChecksum ?? false;
|
|
153
153
|
}
|
|
154
|
-
handleFile(
|
|
154
|
+
handleFile(context, file, fileData) {
|
|
155
155
|
// Enforce per-file size if configured
|
|
156
156
|
if (this.maxFileSize !== undefined && fileData.length > this.maxFileSize) {
|
|
157
157
|
throw new uploadException_1.UploadException(`File too large. Max size: ${this.maxFileSize} bytes`, types_1.UploadErrorCode.LIMIT_FILE_SIZE, file.fieldName);
|
package/dist/storage/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { UploadedFile } from "../parsers/parserInterface";
|
|
2
|
-
import {
|
|
2
|
+
import { Context } from "../http/context";
|
|
3
3
|
/**
|
|
4
4
|
* Available storage engine types.
|
|
5
5
|
*
|
|
@@ -35,12 +35,12 @@ interface DiskStorageOptions {
|
|
|
35
35
|
* Destination directory or resolver function.
|
|
36
36
|
* If a function is provided, it receives the request and file metadata.
|
|
37
37
|
*/
|
|
38
|
-
destination?: string | ((
|
|
38
|
+
destination?: string | ((ctx: Context, file: Partial<UploadedFile>) => string | Promise<string>);
|
|
39
39
|
/**
|
|
40
40
|
* Custom filename generator.
|
|
41
41
|
* Receives the request and partial file metadata.
|
|
42
42
|
*/
|
|
43
|
-
filename?: (
|
|
43
|
+
filename?: (ctx: Context, file: Partial<UploadedFile>) => string | Promise<string>;
|
|
44
44
|
}
|
|
45
45
|
interface MemoryStoreOptions {
|
|
46
46
|
/** Maximum total bytes allowed to be stored in memory (default: 50MB) */
|
|
@@ -76,7 +76,7 @@ export interface Storage {
|
|
|
76
76
|
* @param fileData Raw file buffer.
|
|
77
77
|
* @returns Final uploaded file metadata.
|
|
78
78
|
*/
|
|
79
|
-
handleFile(
|
|
79
|
+
handleFile(context: Context, file: Partial<UploadedFile>, fileData: Buffer): Promise<UploadedFile> | UploadedFile;
|
|
80
80
|
/**
|
|
81
81
|
* Removes a stored file.
|
|
82
82
|
*
|
|
@@ -99,7 +99,7 @@ export type FileFilterCallback = (error: Error | null, acceptFile: boolean) => v
|
|
|
99
99
|
*
|
|
100
100
|
* The callback must be invoked to accept or reject the file.
|
|
101
101
|
*/
|
|
102
|
-
export type FileFilter = (
|
|
102
|
+
export type FileFilter = (context: Context, file: Partial<UploadedFile>, callback: FileFilterCallback) => void | Promise<void>;
|
|
103
103
|
/**
|
|
104
104
|
* Global uploader configuration.
|
|
105
105
|
*/
|
|
@@ -43,7 +43,7 @@ declare class Uploader {
|
|
|
43
43
|
* - If the file field does not exist, it does not error; it only attaches text
|
|
44
44
|
* fields and continues.
|
|
45
45
|
* - If present, the file is validated, filtered (optional), stored via the
|
|
46
|
-
* configured {@link Storage}, and attached as `
|
|
46
|
+
* configured {@link Storage}, and attached as `ctx.req.files`.
|
|
47
47
|
*
|
|
48
48
|
* @param fieldName Multipart field name expected to contain a file.
|
|
49
49
|
* @returns A framework {@link Middleware} to be used in the route pipeline.
|
|
@@ -59,11 +59,11 @@ declare class Uploader {
|
|
|
59
59
|
*
|
|
60
60
|
* Behavior:
|
|
61
61
|
* - If no multipart payload is present, it passes through.
|
|
62
|
-
* - If no files exist for the given field, it sets `
|
|
62
|
+
* - If no files exist for the given field, it sets `ctx.req.files = []`,
|
|
63
63
|
* attaches text fields, and continues.
|
|
64
64
|
* - If the number of files exceeds `maxCount`, it throws.
|
|
65
65
|
* - Each file is validated, filtered (optional), stored, and collected into
|
|
66
|
-
* `
|
|
66
|
+
* `ctx.req.files` as an array of {@link UploadedFile}.
|
|
67
67
|
*
|
|
68
68
|
* @param fieldName Multipart field name expected to contain files.
|
|
69
69
|
* @param maxCount Maximum number of files allowed for this field.
|
|
@@ -82,7 +82,7 @@ declare class Uploader {
|
|
|
82
82
|
* Behavior:
|
|
83
83
|
* - Rejects any file whose field name is not declared in `fields`.
|
|
84
84
|
* - Enforces `maxCount` per declared field (defaults to 1).
|
|
85
|
-
* - Aggregates results as `
|
|
85
|
+
* - Aggregates results as `ctx.req.files` in a map:
|
|
86
86
|
* `{ [fieldName]: UploadedFile[] }`.
|
|
87
87
|
*
|
|
88
88
|
* @param fields List of allowed fields and their per-field maximum counts.
|
|
@@ -101,7 +101,7 @@ declare class Uploader {
|
|
|
101
101
|
*
|
|
102
102
|
* Behavior:
|
|
103
103
|
* - Enforces the global total file limit: `limits.files`.
|
|
104
|
-
* - Stores all files and attaches them as `
|
|
104
|
+
* - Stores all files and attaches them as `ctx.req.files` (array).
|
|
105
105
|
*
|
|
106
106
|
* @returns A framework {@link Middleware}.
|
|
107
107
|
*
|
|
@@ -208,10 +208,10 @@ declare class Uploader {
|
|
|
208
208
|
},
|
|
209
209
|
});
|
|
210
210
|
|
|
211
|
-
//
|
|
212
|
-
app.post("/file", (
|
|
213
|
-
return json({ file:
|
|
214
|
-
}
|
|
211
|
+
// Assign middleware to a route
|
|
212
|
+
app.post("/file", [uploader.single("file")], context => {
|
|
213
|
+
return context.json({ file: context.req.files });
|
|
214
|
+
})
|
|
215
215
|
*/
|
|
216
216
|
export declare const createUploader: (config?: UploaderConfig) => Uploader;
|
|
217
217
|
export {};
|