@cyberskill/shared 3.12.0 → 3.14.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/dist/config/commitlint/index.d.ts +5 -1
- package/dist/{src/config → config}/config.type.d.ts +1 -1
- package/dist/{src/config → config}/config.util.d.ts +1 -1
- package/dist/{src/config → config}/env/env.util.d.ts +5 -0
- package/dist/config/env/env.util.js +20 -16
- package/dist/config/env/env.util.js.map +1 -1
- package/dist/config/env/index.d.ts +3 -1
- package/dist/config/env/index.js +2 -2
- package/dist/config/eslint/index.d.ts +1198 -1
- package/dist/config/graphql-codegen/index.d.ts +2 -1
- package/dist/config/index.d.ts +2 -1
- package/dist/config/lint-staged/index.d.ts +5 -1
- package/dist/config/storybook/index.d.ts +2 -1
- package/dist/config/vitest/index.d.ts +3 -1
- package/dist/config/vitest/vitest.e2e.d.ts +20 -1
- package/dist/config/vitest/vitest.e2e.js +4 -4
- package/dist/config/vitest/vitest.e2e.js.map +1 -1
- package/dist/config/vitest/vitest.unit.d.ts +22 -1
- package/dist/config/vitest/vitest.unit.js +5 -5
- package/dist/config/vitest/vitest.unit.js.map +1 -1
- package/dist/config/vitest/vitest.unit.setup.js +10 -0
- package/dist/config/vitest/vitest.unit.setup.js.map +1 -0
- package/dist/constant/index.d.ts +5 -1
- package/dist/node/apollo-server/apollo-server.type.d.ts +20 -0
- package/dist/{src/node → node}/apollo-server/apollo-server.util.d.ts +1 -0
- package/dist/node/apollo-server/apollo-server.util.js +40 -16
- package/dist/node/apollo-server/apollo-server.util.js.map +1 -1
- package/dist/node/apollo-server/index.d.ts +2 -1
- package/dist/node/cli/index.d.ts +2 -1
- package/dist/node/cli/index.js +26 -28
- package/dist/node/cli/index.js.map +1 -1
- package/dist/{src/node → node}/command/command.util.d.ts +5 -0
- package/dist/node/command/command.util.js +49 -48
- package/dist/node/command/command.util.js.map +1 -1
- package/dist/node/command/index.d.ts +2 -1
- package/dist/node/command/index.js +2 -2
- package/dist/{src/node → node}/express/express.type.d.ts +11 -0
- package/dist/{src/node → node}/express/express.util.d.ts +34 -6
- package/dist/node/express/express.util.js +81 -56
- package/dist/node/express/express.util.js.map +1 -1
- package/dist/node/express/index.d.ts +2 -1
- package/dist/node/express/index.js +2 -2
- package/dist/node/fs/index.d.ts +2 -1
- package/dist/node/log/index.d.ts +2 -1
- package/dist/node/log/log.type.d.ts +46 -0
- package/dist/node/log/log.type.js.map +1 -1
- package/dist/{src/node → node}/log/log.util.d.ts +1 -1
- package/dist/node/log/log.util.js +25 -11
- package/dist/node/log/log.util.js.map +1 -1
- package/dist/node/mongo/index.d.ts +7 -1
- package/dist/node/mongo/index.js +7 -8
- package/dist/{src/node → node}/mongo/mongo.constant.d.ts +5 -0
- package/dist/node/mongo/mongo.constant.js +2 -2
- package/dist/node/mongo/mongo.constant.js.map +1 -1
- package/dist/{src/node → node}/mongo/mongo.controller.helpers.d.ts +1 -1
- package/dist/{src/node → node}/mongo/mongo.controller.mongoose.d.ts +4 -1
- package/dist/node/mongo/mongo.controller.mongoose.js +41 -55
- package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
- package/dist/{src/node → node}/mongo/mongo.controller.native.d.ts +30 -3
- package/dist/node/mongo/mongo.controller.native.js +31 -14
- package/dist/node/mongo/mongo.controller.native.js.map +1 -1
- package/dist/{src/node → node}/mongo/mongo.controller.type.d.ts +1 -1
- package/dist/{src/node → node}/mongo/mongo.type.d.ts +3 -1
- package/dist/{src/node → node}/mongo/mongo.util.d.ts +1 -0
- package/dist/node/mongo/mongo.util.js +38 -17
- package/dist/node/mongo/mongo.util.js.map +1 -1
- package/dist/node/package/index.d.ts +2 -1
- package/dist/{src/node → node}/package/package.util.d.ts +1 -1
- package/dist/node/package/package.util.js +47 -47
- package/dist/node/path/index.d.ts +2 -1
- package/dist/node/path/index.js +2 -2
- package/dist/{src/node → node}/path/path.constant.d.ts +4 -0
- package/dist/node/path/path.constant.js +75 -72
- package/dist/node/path/path.constant.js.map +1 -1
- package/dist/node/storage/index.d.ts +3 -1
- package/dist/{src/node → node}/storage/storage.util.d.ts +50 -1
- package/dist/node/storage/storage.util.js +79 -54
- package/dist/node/storage/storage.util.js.map +1 -1
- package/dist/node/upload/index.d.ts +3 -1
- package/dist/{src/node → node}/upload/upload.type.d.ts +2 -0
- package/dist/node/upload/upload.type.js.map +1 -1
- package/dist/{src/node → node}/upload/upload.util.d.ts +2 -1
- package/dist/node/upload/upload.util.js +62 -52
- package/dist/node/upload/upload.util.js.map +1 -1
- package/dist/node/ws/index.d.ts +2 -1
- package/dist/{src/node → node}/ws/ws.util.d.ts +7 -0
- package/dist/node/ws/ws.util.js +20 -19
- package/dist/node/ws/ws.util.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.component.js.map +1 -1
- package/dist/{src/react → react}/apollo-client/apollo-client.type.d.ts +3 -1
- package/dist/react/apollo-client/apollo-client.util.js +6 -6
- package/dist/react/apollo-client/apollo-client.util.js.map +1 -1
- package/dist/react/apollo-client/index.d.ts +9 -1
- package/dist/react/apollo-client/links/index.d.ts +1 -1
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.d.ts +16 -1
- package/dist/react/apollo-client-nextjs/index.d.ts +2 -1
- package/dist/react/apollo-error/apollo-error.component.js +1 -1
- package/dist/react/apollo-error/apollo-error.component.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.util.js.map +1 -1
- package/dist/react/apollo-error/index.d.ts +6 -1
- package/dist/react/i18next/i18next.server.d.ts +17 -0
- package/dist/react/i18next/i18next.server.js +9 -0
- package/dist/react/i18next/i18next.server.js.map +1 -0
- package/dist/react/i18next/index.d.ts +2 -1
- package/dist/react/loading/index.d.ts +5 -1
- package/dist/{src/react → react}/loading/loading.provider.d.ts +1 -1
- package/dist/react/log/index.d.ts +2 -1
- package/dist/react/log/log.type.d.ts +1 -0
- package/dist/{src/react → react}/log/log.util.d.ts +1 -1
- package/dist/react/next-intl/index.d.ts +6 -1
- package/dist/{src/react → react}/next-intl/next-intl.hoc.d.ts +5 -9
- package/dist/react/next-intl/next-intl.hoc.js +14 -10
- package/dist/react/next-intl/next-intl.hoc.js.map +1 -1
- package/dist/react/next-intl/next-intl.server.d.ts +10 -0
- package/dist/react/next-intl/next-intl.server.js +7 -0
- package/dist/react/next-intl/next-intl.server.js.map +1 -0
- package/dist/{src/react → react}/next-intl/next-intl.type.d.ts +1 -1
- package/dist/react/storage/index.d.ts +2 -1
- package/dist/{src/react → react}/storage/storage.hook.d.ts +1 -1
- package/dist/{src/react → react}/storage/storage.util.d.ts +34 -1
- package/dist/react/storage/storage.util.js +30 -5
- package/dist/react/storage/storage.util.js.map +1 -1
- package/dist/react/toast/index.d.ts +1 -1
- package/dist/react/userback/index.d.ts +2 -1
- package/dist/react/userback/userback.component.js.map +1 -1
- package/dist/{src/typescript → typescript}/common.type.d.ts +4 -0
- package/dist/typescript/common.type.js +2 -2
- package/dist/typescript/common.type.js.map +1 -1
- package/dist/typescript/index.d.ts +5 -1
- package/dist/typescript/index.js +2 -2
- package/dist/util/common/index.d.ts +5 -1
- package/dist/util/index.d.ts +9 -1
- package/dist/util/log/index.d.ts +2 -1
- package/dist/{src/util → util}/log/log.util.d.ts +1 -1
- package/dist/util/object/index.d.ts +4 -1
- package/dist/util/object/object.util.js +29 -18
- package/dist/util/object/object.util.js.map +1 -1
- package/dist/util/serializer/index.d.ts +2 -1
- package/dist/{src/util → util}/serializer/serializer.util.d.ts +8 -0
- package/dist/util/serializer/serializer.util.js +51 -64
- package/dist/util/serializer/serializer.util.js.map +1 -1
- package/dist/util/storage/storage-envelope.d.ts +25 -0
- package/dist/util/storage/storage-envelope.js +18 -0
- package/dist/util/storage/storage-envelope.js.map +1 -0
- package/dist/util/string/index.d.ts +5 -1
- package/dist/util/validate/index.d.ts +4 -1
- package/package.json +33 -12
- package/dist/node/mongo/mongo.type.js +0 -8
- package/dist/node/mongo/mongo.type.js.map +0 -1
- package/dist/node_modules/.pnpm/vitest@4.1.2_@types_node@25.5.0_jsdom@29.0.1_@noble_hashes@1.8.0__vite@8.0.3_@types_nod_0827261ede788764a5d99ac6bdf44bde/node_modules/vitest/dist/config.js +0 -8
- package/dist/node_modules/.pnpm/vitest@4.1.2_@types_node@25.5.0_jsdom@29.0.1_@noble_hashes@1.8.0__vite@8.0.3_@types_nod_0827261ede788764a5d99ac6bdf44bde/node_modules/vitest/dist/config.js.map +0 -1
- package/dist/src/config/commitlint/index.d.ts +0 -5
- package/dist/src/config/env/index.d.ts +0 -3
- package/dist/src/config/eslint/index.d.ts +0 -1198
- package/dist/src/config/graphql-codegen/index.d.ts +0 -2
- package/dist/src/config/index.d.ts +0 -2
- package/dist/src/config/lint-staged/index.d.ts +0 -5
- package/dist/src/config/storybook/index.d.ts +0 -2
- package/dist/src/config/vitest/index.d.ts +0 -3
- package/dist/src/config/vitest/vitest.e2e.d.ts +0 -20
- package/dist/src/config/vitest/vitest.unit.d.ts +0 -22
- package/dist/src/constant/index.d.ts +0 -5
- package/dist/src/node/apollo-server/apollo-server.type.d.ts +0 -8
- package/dist/src/node/apollo-server/index.d.ts +0 -2
- package/dist/src/node/cli/index.d.ts +0 -2
- package/dist/src/node/command/index.d.ts +0 -2
- package/dist/src/node/express/index.d.ts +0 -2
- package/dist/src/node/fs/index.d.ts +0 -2
- package/dist/src/node/log/index.d.ts +0 -2
- package/dist/src/node/log/log.type.d.ts +0 -29
- package/dist/src/node/mongo/index.d.ts +0 -6
- package/dist/src/node/package/index.d.ts +0 -2
- package/dist/src/node/path/index.d.ts +0 -2
- package/dist/src/node/storage/index.d.ts +0 -3
- package/dist/src/node/upload/index.d.ts +0 -3
- package/dist/src/node/ws/index.d.ts +0 -2
- package/dist/src/react/apollo-client/index.d.ts +0 -9
- package/dist/src/react/apollo-client/links/index.d.ts +0 -1
- package/dist/src/react/apollo-client-nextjs/apollo-client-nextjs.rsc.d.ts +0 -16
- package/dist/src/react/apollo-client-nextjs/index.d.ts +0 -2
- package/dist/src/react/apollo-error/index.d.ts +0 -6
- package/dist/src/react/i18next/index.d.ts +0 -2
- package/dist/src/react/loading/index.d.ts +0 -5
- package/dist/src/react/log/index.d.ts +0 -2
- package/dist/src/react/log/log.type.d.ts +0 -1
- package/dist/src/react/next-intl/index.d.ts +0 -6
- package/dist/src/react/storage/index.d.ts +0 -2
- package/dist/src/react/toast/index.d.ts +0 -1
- package/dist/src/react/userback/index.d.ts +0 -2
- package/dist/src/typescript/index.d.ts +0 -5
- package/dist/src/util/common/index.d.ts +0 -5
- package/dist/src/util/index.d.ts +0 -9
- package/dist/src/util/log/index.d.ts +0 -2
- package/dist/src/util/object/index.d.ts +0 -4
- package/dist/src/util/serializer/index.d.ts +0 -2
- package/dist/src/util/string/index.d.ts +0 -5
- package/dist/src/util/validate/index.d.ts +0 -4
- /package/dist/{src/config → config}/env/env.constant.d.ts +0 -0
- /package/dist/{src/config → config}/env/env.type.d.ts +0 -0
- /package/dist/{src/config → config}/graphql-codegen/graphql-codegen.type.d.ts +0 -0
- /package/dist/{src/config → config}/graphql-codegen/graphql-codegen.util.d.ts +0 -0
- /package/dist/{src/config → config}/storybook/storybook.main.d.ts +0 -0
- /package/dist/{src/config → config}/storybook/storybook.preview.d.ts +0 -0
- /package/dist/{src/config → config}/vitest/vitest.unit.setup.d.ts +0 -0
- /package/dist/{src/constant → constant}/common.d.ts +0 -0
- /package/dist/{src/constant → constant}/response-status.d.ts +0 -0
- /package/dist/{src/node → node}/command/command.type.d.ts +0 -0
- /package/dist/{src/node → node}/fs/fs.type.d.ts +0 -0
- /package/dist/{src/node → node}/fs/fs.util.d.ts +0 -0
- /package/dist/{src/node → node}/mongo/mongo.controller.d.ts +0 -0
- /package/dist/{src/node → node}/mongo/mongo.dynamic-populate.d.ts +0 -0
- /package/dist/{src/node → node}/mongo/mongo.internal-types.d.ts +0 -0
- /package/dist/{src/node → node}/mongo/mongo.populate.d.ts +0 -0
- /package/dist/{src/node → node}/package/package.type.d.ts +0 -0
- /package/dist/{src/node → node}/path/path.util.d.ts +0 -0
- /package/dist/{src/node → node}/storage/storage.constant.d.ts +0 -0
- /package/dist/{src/node → node}/storage/storage.type.d.ts +0 -0
- /package/dist/{src/node → node}/upload/upload.constant.d.ts +0 -0
- /package/dist/{src/node → node}/ws/ws.type.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/apollo-client.component.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/apollo-client.constant.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/apollo-client.context.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/apollo-client.hook.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/apollo-client.util.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client/links/upload.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client-nextjs/apollo-client-nextjs.component.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-client-nextjs/apollo-client-nextjs.util.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.component.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.context.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.hook.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.provider.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.type.d.ts +0 -0
- /package/dist/{src/react → react}/apollo-error/apollo-error.util.d.ts +0 -0
- /package/dist/{src/react → react}/i18next/i18next.hook.d.ts +0 -0
- /package/dist/{src/react → react}/i18next/i18next.util.d.ts +0 -0
- /package/dist/{src/react → react}/loading/loading.component.d.ts +0 -0
- /package/dist/{src/react → react}/loading/loading.context.d.ts +0 -0
- /package/dist/{src/react → react}/loading/loading.hook.d.ts +0 -0
- /package/dist/{src/react → react}/loading/loading.type.d.ts +0 -0
- /package/dist/{src/react → react}/next-intl/next-intl.constant.d.ts +0 -0
- /package/dist/{src/react → react}/next-intl/next-intl.context.d.ts +0 -0
- /package/dist/{src/react → react}/next-intl/next-intl.hook.d.ts +0 -0
- /package/dist/{src/react → react}/next-intl/next-intl.provider.d.ts +0 -0
- /package/dist/{src/react → react}/userback/userback.component.d.ts +0 -0
- /package/dist/{src/react → react}/userback/userback.type.d.ts +0 -0
- /package/dist/{src/typescript → typescript}/react.type.d.ts +0 -0
- /package/dist/{src/util → util}/common/common.type.d.ts +0 -0
- /package/dist/{src/util → util}/common/common.util.d.ts +0 -0
- /package/dist/{src/util → util}/log/log.type.d.ts +0 -0
- /package/dist/{src/util → util}/object/object.util.d.ts +0 -0
- /package/dist/{src/util → util}/serializer/serializer.type.d.ts +0 -0
- /package/dist/{src/util → util}/string/string.type.d.ts +0 -0
- /package/dist/{src/util → util}/string/string.util.d.ts +0 -0
- /package/dist/{src/util → util}/validate/validate.util.d.ts +0 -0
|
@@ -1,58 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { createTtlEnvelope as e, isExpiredEnvelope as t, isTtlEnvelope as n } from "../../util/storage/storage-envelope.js";
|
|
2
|
+
import { getEnv as r } from "../../config/env/env.util.js";
|
|
3
|
+
import { catchError as i, log as a } from "../log/log.util.js";
|
|
4
|
+
import { STORAGE_KEY_EXTENSION as o } from "./storage.constant.js";
|
|
5
|
+
import s from "node:path";
|
|
6
|
+
import c from "node:fs/promises";
|
|
6
7
|
//#region src/node/storage/storage.util.ts
|
|
7
|
-
var
|
|
8
|
-
function
|
|
9
|
-
if (e.length >
|
|
10
|
-
return `${encodeURIComponent(e)}${
|
|
8
|
+
var l = 200, u = { baseDir: "" };
|
|
9
|
+
function d(e) {
|
|
10
|
+
if (e.length > l) throw RangeError(`Storage key exceeds maximum length of ${l} characters`);
|
|
11
|
+
return `${encodeURIComponent(e)}${o}`;
|
|
11
12
|
}
|
|
12
|
-
function
|
|
13
|
-
return decodeURIComponent(e.slice(0, -
|
|
13
|
+
function f(e) {
|
|
14
|
+
return decodeURIComponent(e.slice(0, -o.length));
|
|
14
15
|
}
|
|
15
|
-
function
|
|
16
|
-
return
|
|
16
|
+
function p(e, t) {
|
|
17
|
+
return s.join(t, d(e));
|
|
17
18
|
}
|
|
18
|
-
var
|
|
19
|
-
async init(
|
|
19
|
+
var m = {
|
|
20
|
+
async init(e) {
|
|
20
21
|
try {
|
|
21
|
-
typeof
|
|
22
|
+
typeof e == "string" && e.length > 0 ? u.baseDir = e : u.baseDir = r().CYBERSKILL_STORAGE_DIRECTORY, await c.mkdir(u.baseDir, { recursive: !0 });
|
|
22
23
|
} catch (e) {
|
|
23
|
-
throw
|
|
24
|
+
throw a.error("[Storage:init]", e), e;
|
|
24
25
|
}
|
|
25
26
|
},
|
|
26
27
|
async clear() {
|
|
27
|
-
let { baseDir: e } =
|
|
28
|
+
let { baseDir: e } = u;
|
|
28
29
|
if (!e) return;
|
|
29
30
|
let t = `${e}.trash.${Date.now()}`, n = `${e}.fresh.${Date.now()}`;
|
|
30
31
|
try {
|
|
31
|
-
await
|
|
32
|
+
await c.mkdir(n, { recursive: !0 });
|
|
32
33
|
try {
|
|
33
|
-
await
|
|
34
|
+
await c.rename(e, t);
|
|
34
35
|
} catch {}
|
|
35
|
-
await
|
|
36
|
+
await c.rename(n, e), c.rm(t, {
|
|
36
37
|
recursive: !0,
|
|
37
38
|
force: !0
|
|
38
39
|
}).catch(() => {});
|
|
39
40
|
} catch {
|
|
40
|
-
await
|
|
41
|
+
await c.rm(e, {
|
|
41
42
|
recursive: !0,
|
|
42
43
|
force: !0
|
|
43
|
-
}), await
|
|
44
|
+
}), await c.mkdir(e, { recursive: !0 }), c.rm(n, {
|
|
44
45
|
recursive: !0,
|
|
45
46
|
force: !0
|
|
46
|
-
}).catch(() => {}),
|
|
47
|
+
}).catch(() => {}), c.rm(t, {
|
|
47
48
|
recursive: !0,
|
|
48
49
|
force: !0
|
|
49
50
|
}).catch(() => {});
|
|
50
51
|
}
|
|
51
52
|
},
|
|
52
53
|
async getItem(e) {
|
|
53
|
-
let { baseDir: t } =
|
|
54
|
+
let { baseDir: t } = u, n = p(e, t);
|
|
54
55
|
try {
|
|
55
|
-
let e = await
|
|
56
|
+
let e = await c.readFile(n, "utf8");
|
|
56
57
|
return JSON.parse(e);
|
|
57
58
|
} catch (e) {
|
|
58
59
|
if (e.code === "ENOENT") return null;
|
|
@@ -60,70 +61,94 @@ var d = {
|
|
|
60
61
|
}
|
|
61
62
|
},
|
|
62
63
|
async keys() {
|
|
63
|
-
let { baseDir: e } =
|
|
64
|
+
let { baseDir: e } = u;
|
|
64
65
|
try {
|
|
65
|
-
return (await
|
|
66
|
+
return (await c.readdir(e)).filter((e) => e.endsWith(o)).map(f);
|
|
66
67
|
} catch (e) {
|
|
67
68
|
if (e.code === "ENOENT") return [];
|
|
68
69
|
throw e;
|
|
69
70
|
}
|
|
70
71
|
},
|
|
71
72
|
async removeItem(e) {
|
|
72
|
-
let { baseDir: t } =
|
|
73
|
-
await
|
|
73
|
+
let { baseDir: t } = u, n = p(e, t);
|
|
74
|
+
await c.rm(n, { force: !0 });
|
|
74
75
|
},
|
|
75
76
|
async setItem(e, t) {
|
|
76
|
-
let { baseDir: n } =
|
|
77
|
-
return await
|
|
77
|
+
let { baseDir: n } = u, r = p(e, n);
|
|
78
|
+
return await c.mkdir(n, { recursive: !0 }), await c.writeFile(r, JSON.stringify(t), "utf8"), t;
|
|
78
79
|
}
|
|
79
|
-
},
|
|
80
|
-
async function
|
|
81
|
-
return
|
|
82
|
-
throw
|
|
83
|
-
}), await
|
|
80
|
+
}, h = null, g = m;
|
|
81
|
+
async function _() {
|
|
82
|
+
return h ? (await h, g) : (h = g.init().catch((e) => {
|
|
83
|
+
throw h = null, e;
|
|
84
|
+
}), await h, g);
|
|
84
85
|
}
|
|
85
|
-
var
|
|
86
|
+
var v = {
|
|
87
|
+
async initDriver(e) {
|
|
88
|
+
g = e, h = null, await _();
|
|
89
|
+
},
|
|
86
90
|
async get(e) {
|
|
87
91
|
try {
|
|
88
|
-
|
|
92
|
+
let r = await _(), i = await r.getItem(e);
|
|
93
|
+
return i === null ? null : n(i) ? t(i) ? (r.removeItem(e).catch(() => {}), null) : i.value : i;
|
|
89
94
|
} catch (e) {
|
|
90
|
-
return
|
|
95
|
+
return i(e, { returnValue: null });
|
|
91
96
|
}
|
|
92
97
|
},
|
|
93
|
-
async set(
|
|
98
|
+
async set(t, n, r) {
|
|
94
99
|
try {
|
|
95
|
-
|
|
100
|
+
let i = await _(), a = n;
|
|
101
|
+
r?.ttlMs && (a = e(n, r.ttlMs)), await i.setItem(t, a);
|
|
96
102
|
} catch (e) {
|
|
97
|
-
throw
|
|
103
|
+
throw i(e), e;
|
|
98
104
|
}
|
|
99
105
|
},
|
|
100
106
|
async remove(e) {
|
|
101
107
|
try {
|
|
102
|
-
await (await
|
|
108
|
+
await (await _()).removeItem(e);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
i(e);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
async has(e) {
|
|
114
|
+
try {
|
|
115
|
+
let r = await _(), i = await r.getItem(e);
|
|
116
|
+
return i === null ? !1 : n(i) && t(i) ? (r.removeItem(e).catch(() => {}), !1) : !0;
|
|
103
117
|
} catch (e) {
|
|
104
|
-
|
|
118
|
+
return i(e, { returnValue: !1 });
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
async clear() {
|
|
122
|
+
try {
|
|
123
|
+
await (await _()).clear();
|
|
124
|
+
} catch (e) {
|
|
125
|
+
i(e);
|
|
105
126
|
}
|
|
106
127
|
},
|
|
107
128
|
async keys() {
|
|
108
129
|
try {
|
|
109
|
-
let e = await (await
|
|
110
|
-
return Array.isArray(e) ? e : (
|
|
130
|
+
let e = await (await _()).keys();
|
|
131
|
+
return Array.isArray(e) ? e : (a.warn("[Storage:keys] Invalid keys response:", e), []);
|
|
111
132
|
} catch (e) {
|
|
112
|
-
return
|
|
133
|
+
return i(e, { returnValue: [] });
|
|
113
134
|
}
|
|
114
135
|
},
|
|
115
|
-
async getLogLink(
|
|
136
|
+
async getLogLink(e) {
|
|
116
137
|
try {
|
|
117
|
-
return `${
|
|
138
|
+
return `${s.join(r().CYBERSKILL_STORAGE_DIRECTORY, d(e))} (key: ${e})`;
|
|
118
139
|
} catch (e) {
|
|
119
|
-
return
|
|
140
|
+
return i(e, { returnValue: null });
|
|
120
141
|
}
|
|
142
|
+
},
|
|
143
|
+
async getOrSet(e, t, n) {
|
|
144
|
+
let r = await this.get(e);
|
|
145
|
+
return r === null && (r = await t(), await this.set(e, r, n)), r;
|
|
121
146
|
}
|
|
122
147
|
};
|
|
123
|
-
function
|
|
124
|
-
|
|
148
|
+
function y() {
|
|
149
|
+
h = null, g = m, u.baseDir = "";
|
|
125
150
|
}
|
|
126
151
|
//#endregion
|
|
127
|
-
export {
|
|
152
|
+
export { y as resetStorageForTesting, v as storage };
|
|
128
153
|
|
|
129
154
|
//# sourceMappingURL=storage.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport { catchError, log } from '../log/index.js';\nimport { STORAGE_KEY_EXTENSION } from './storage.constant.js';\n\nconst MAX_KEY_LENGTH = 200;\n\ninterface NodeFsDriverState {\n baseDir: string;\n}\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n * Validates key length before encoding to prevent OS filename limits.\n *\n * @throws {RangeError} When key exceeds maximum length.\n */\nfunction encodeKey(key: string): string {\n if (key.length > MAX_KEY_LENGTH) {\n throw new RangeError(`Storage key exceeds maximum length of ${MAX_KEY_LENGTH} characters`);\n }\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Filesystem-backed storage driver that stores JSON-encoded values on disk.\n * Directly implements all storage operations without any external dependencies.\n * storage operations without the unnecessary browser-oriented dependency.\n */\nconst fsDriver = {\n /** Ensures the storage directory exists. */\n async init(baseDir?: string) {\n try {\n if (typeof baseDir === 'string' && baseDir.length > 0) {\n nodeFsDriverState.baseDir = baseDir;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries atomically by swapping to a fresh directory. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n // Atomic swap: create a fresh temp dir, rename old→trash, rename fresh→baseDir, remove trash\n const trashDir = `${baseDir}.trash.${Date.now()}`;\n const freshDir = `${baseDir}.fresh.${Date.now()}`;\n\n try {\n await fs.mkdir(freshDir, { recursive: true });\n // Try atomic rename swap\n try {\n await fs.rename(baseDir, trashDir);\n }\n catch {\n // baseDir might not exist yet; no-op\n }\n await fs.rename(freshDir, baseDir);\n // Clean up trash in the background (non-blocking)\n fs.rm(trashDir, { recursive: true, force: true }).catch(() => {});\n }\n catch {\n // Fallback: non-atomic clear (e.g., cross-device rename)\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n // Clean up any leftover temp dirs\n fs.rm(freshDir, { recursive: true, force: true }).catch(() => {});\n fs.rm(trashDir, { recursive: true, force: true }).catch(() => {});\n }\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\n\n/**\n * Initializes the filesystem storage driver (singleton, idempotent).\n * Ensures the storage directory exists before any read/write operations.\n */\nasync function ensureDriverReady(): Promise<typeof fsDriver> {\n if (initPromise) {\n await initPromise;\n return fsDriver;\n }\n\n initPromise = fsDriver.init().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return fsDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * Uses a filesystem-backed driver that stores JSON-encoded values on disk,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n try {\n const driver = await ensureDriverReady();\n const result = await driver.getItem<T>(key);\n\n return result ?? null;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T): Promise<void> {\n try {\n const driver = await ensureDriverReady();\n\n await driver.setItem(key, value);\n }\n catch (error) {\n catchError(error);\n throw error;\n }\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureDriverReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureDriverReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;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,MAAM,CAAC;;;CAYvD,MAAM,IAAiB,GAAa,GAAyB;AACzD,MAAI;AAGA,UAFe,MAAM,GAAmB,EAE3B,QAAQ,GAAK,EAAM;WAE7B,GAAO;AAEV,SADA,EAAW,EAAM,EACX;;;CAUd,MAAM,OAAO,GAA4B;AACrC,MAAI;AAGA,UAFe,MAAM,GAAmB,EAE3B,WAAW,EAAI;WAEzB,GAAO;AACV,KAAW,EAAM;;;CAUzB,MAAM,OAA0B;AAC5B,MAAI;GAEA,IAAM,IAAO,OADE,MAAM,GAAmB,EACd,MAAM;AAOhC,UALK,MAAM,QAAQ,EAAK,GAKjB,KAJH,EAAI,KAAK,yCAAyC,EAAK,EAChD,EAAE;WAKV,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,EAAE,EAAE,CAAC;;;CAWrD,MAAM,WAAW,GAAqC;AAClD,MAAI;AAGA,UAAO,GAFa,EAAK,KAAK,GAAQ,CAAC,8BAA8B,EAAU,EAAI,CAAC,CAE9D,SAAS,EAAI;WAEhC,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAI1D;AAOD,SAAgB,IAA+B;AAE3C,CADA,IAAc,MACd,EAAkB,UAAU"}
|
|
1
|
+
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport { createTtlEnvelope, isExpiredEnvelope, isTtlEnvelope } from '../../util/storage/storage-envelope.js';\nimport { catchError, log } from '../log/index.js';\nimport { STORAGE_KEY_EXTENSION } from './storage.constant.js';\n\nconst MAX_KEY_LENGTH = 200;\n\ninterface NodeFsDriverState {\n baseDir: string;\n}\n\nexport interface I_StorageDriver {\n init: (options?: unknown) => Promise<void>;\n clear: () => Promise<void>;\n getItem: <T>(key: string) => Promise<T | null>;\n keys: () => Promise<string[]>;\n removeItem: (key: string) => Promise<void>;\n setItem: <T>(key: string, value: T) => Promise<T>;\n}\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n * Validates key length before encoding to prevent OS filename limits.\n *\n * @throws {RangeError} When key exceeds maximum length.\n */\nfunction encodeKey(key: string): string {\n if (key.length > MAX_KEY_LENGTH) {\n throw new RangeError(`Storage key exceeds maximum length of ${MAX_KEY_LENGTH} characters`);\n }\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Filesystem-backed storage driver that stores JSON-encoded values on disk.\n * Directly implements all storage operations without any external dependencies.\n */\nconst fsDriver: I_StorageDriver = {\n /** Ensures the storage directory exists. */\n async init(baseDir?: unknown) {\n try {\n if (typeof baseDir === 'string' && baseDir.length > 0) {\n nodeFsDriverState.baseDir = baseDir;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries atomically by swapping to a fresh directory. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n // Atomic swap: create a fresh temp dir, rename old→trash, rename fresh→baseDir, remove trash\n const trashDir = `${baseDir}.trash.${Date.now()}`;\n const freshDir = `${baseDir}.fresh.${Date.now()}`;\n\n try {\n await fs.mkdir(freshDir, { recursive: true });\n // Try atomic rename swap\n try {\n await fs.rename(baseDir, trashDir);\n }\n catch {\n // baseDir might not exist yet; no-op\n }\n await fs.rename(freshDir, baseDir);\n // Clean up trash in the background (non-blocking)\n fs.rm(trashDir, { recursive: true, force: true }).catch(() => { });\n }\n catch {\n // Fallback: non-atomic clear (e.g., cross-device rename)\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n // Clean up any leftover temp dirs\n fs.rm(freshDir, { recursive: true, force: true }).catch(() => { });\n fs.rm(trashDir, { recursive: true, force: true }).catch(() => { });\n }\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\nlet activeDriver: I_StorageDriver = fsDriver;\n\n/**\n * Initializes the storage driver (singleton, idempotent).\n */\nasync function ensureDriverReady(): Promise<I_StorageDriver> {\n if (initPromise) {\n await initPromise;\n return activeDriver;\n }\n\n initPromise = activeDriver.init().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return activeDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * Uses a filesystem-backed driver that stores JSON-encoded values on disk,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Initializes the utility with a custom storage driver instead of the default filesystem driver.\n * This allows swapping to Redis, Memory, or cloud-based drivers.\n * Must optionally be called before the first read/write operation.\n *\n * @param driver - The custom storage driver object that adheres to I_StorageDriver.\n */\n async initDriver(driver: I_StorageDriver): Promise<void> {\n activeDriver = driver;\n initPromise = null;\n await ensureDriverReady();\n },\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n try {\n const driver = await ensureDriverReady();\n const result = await driver.getItem<unknown>(key);\n\n if (result === null) {\n return null;\n }\n\n if (isTtlEnvelope<T>(result)) {\n if (isExpiredEnvelope(result)) {\n driver.removeItem(key).catch(() => { });\n\n return null;\n }\n\n return result.value;\n }\n\n return result as T;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @param options - Optional settings, such as `ttlMs` for setting an expiration on the key.\n * @param options.ttlMs - The time-to-live in milliseconds.\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T, options?: { ttlMs?: number }): Promise<void> {\n try {\n const driver = await ensureDriverReady();\n\n let payloadToStore: unknown = value;\n\n if (options?.ttlMs) {\n payloadToStore = createTtlEnvelope(value, options.ttlMs);\n }\n\n await driver.setItem(key, payloadToStore);\n }\n catch (error) {\n catchError(error);\n throw error;\n }\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureDriverReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Checks if a key exists in persistent storage.\n * This method efficiently checks for key existence and respects TTL parsing.\n * Returns false if the key exists but has expired.\n *\n * @param key - The unique identifier to check.\n * @returns A promise that resolves to true if the key exists and has not expired.\n * @since 3.13.0\n */\n async has(key: string): Promise<boolean> {\n try {\n const driver = await ensureDriverReady();\n const result = await driver.getItem<unknown>(key);\n\n if (result === null) {\n return false;\n }\n\n if (isTtlEnvelope<unknown>(result)) {\n if (isExpiredEnvelope(result)) {\n driver.removeItem(key).catch(() => { });\n\n return false;\n }\n }\n\n return true;\n }\n catch (error) {\n return catchError(error, { returnValue: false });\n }\n },\n /**\n * Clears all entries from storage atomically.\n * @returns A promise that resolves when the clearing operation is complete.\n */\n async clear(): Promise<void> {\n try {\n const driver = await ensureDriverReady();\n await driver.clear();\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureDriverReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Retrieves a value from persistent storage, or creates and stores it if it doesn't exist.\n * This method combines check, creation, and storage into a single convenient operation.\n *\n * @param key - The unique identifier for the value.\n * @param factory - A function (sync or async) that generates the value if it's missing or expired.\n * @param options - Optional storage options.\n * @param options.ttlMs - The time-to-live in milliseconds.\n * @returns A promise that resolves to the retrieved or newly created value.\n */\n async getOrSet<T = unknown>(key: string, factory: () => T | Promise<T>, options?: { ttlMs?: number }): Promise<T> {\n let value = await this.get<T>(key);\n\n if (value === null) {\n value = await factory();\n await this.set(key, value, options);\n }\n\n return value;\n },\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n * @since 3.13.0\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n activeDriver = fsDriver;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;;;;AASA,IAAM,IAAiB,KAejB,IAAuC,EACzC,SAAS,IACZ;AAQD,SAAS,EAAU,GAAqB;AACpC,KAAI,EAAI,SAAS,EACb,OAAU,WAAW,yCAAyC,EAAe,aAAa;AAE9F,QAAO,GAAG,mBAAmB,EAAI,GAAG;;AAMxC,SAAS,EAAU,GAA0B;AACzC,QAAO,mBAAmB,EAAS,MAAM,GAAG,CAAC,EAAsB,OAAO,CAAC;;AAM/E,SAAS,EAAY,GAAa,GAAyB;AACvD,QAAO,EAAK,KAAK,GAAS,EAAU,EAAI,CAAC;;AAO7C,IAAM,IAA4B;CAE9B,MAAM,KAAK,GAAmB;AAC1B,MAAI;AAQA,GAPI,OAAO,KAAY,YAAY,EAAQ,SAAS,IAChD,EAAkB,UAAU,IAG5B,EAAkB,UAAU,GAAQ,CAAC,8BAGzC,MAAM,EAAG,MAAM,EAAkB,SAAS,EAAE,WAAW,IAAM,CAAC;WAE3D,GAAO;AAEV,SADA,EAAI,MAAM,kBAAkB,EAAM,EAC5B;;;CAId,MA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;GACA,IAAM,IAAS,MAAM,GAAmB,EAClC,IAAS,MAAM,EAAO,QAAiB,EAAI;AAcjD,UAZI,MAAW,OACJ,KAGP,EAAuB,EAAO,IAC1B,EAAkB,EAAO,IACzB,EAAO,WAAW,EAAI,CAAC,YAAY,GAAI,EAEhC,MAIR;WAEJ,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,IAAO,CAAC;;;CAOxD,MAAM,QAAuB;AACzB,MAAI;AAEA,UADe,MAAM,GAAmB,EAC3B,OAAO;WAEjB,GAAO;AACV,KAAW,EAAM;;;CAUzB,MAAM,OAA0B;AAC5B,MAAI;GAEA,IAAM,IAAO,OADE,MAAM,GAAmB,EACd,MAAM;AAOhC,UALK,MAAM,QAAQ,EAAK,GAKjB,KAJH,EAAI,KAAK,yCAAyC,EAAK,EAChD,EAAE;WAKV,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,EAAE,EAAE,CAAC;;;CAWrD,MAAM,WAAW,GAAqC;AAClD,MAAI;AAGA,UAAO,GAFa,EAAK,KAAK,GAAQ,CAAC,8BAA8B,EAAU,EAAI,CAAC,CAE9D,SAAS,EAAI;WAEhC,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAavD,MAAM,SAAsB,GAAa,GAA+B,GAA0C;EAC9G,IAAI,IAAQ,MAAM,KAAK,IAAO,EAAI;AAOlC,SALI,MAAU,SACV,IAAQ,MAAM,GAAS,EACvB,MAAM,KAAK,IAAI,GAAK,GAAO,EAAQ,GAGhC;;CAEd;AAQD,SAAgB,IAA+B;AAG3C,CAFA,IAAc,MACd,IAAe,GACf,EAAkB,UAAU"}
|
|
@@ -8,6 +8,7 @@ export declare enum E_UploadType {
|
|
|
8
8
|
export interface I_UploadValidationConfig {
|
|
9
9
|
filename: string;
|
|
10
10
|
fileSize?: number;
|
|
11
|
+
mimetype?: string;
|
|
11
12
|
}
|
|
12
13
|
export interface I_UploadTypeConfig {
|
|
13
14
|
allowedExtensions: string[];
|
|
@@ -23,6 +24,7 @@ export interface I_UploadConfig {
|
|
|
23
24
|
export interface I_UploadFileData {
|
|
24
25
|
createReadStream: () => NodeJS.ReadableStream;
|
|
25
26
|
filename: string;
|
|
27
|
+
mimetype?: string;
|
|
26
28
|
}
|
|
27
29
|
export interface I_UploadFile {
|
|
28
30
|
file: I_UploadFileData;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.type.js","names":[],"sources":["../../../src/node/upload/upload.type.ts"],"sourcesContent":["export enum E_UploadType {\n IMAGE = 'IMAGE',\n VIDEO = 'VIDEO',\n AUDIO = 'AUDIO',\n DOCUMENT = 'DOCUMENT',\n OTHER = 'OTHER',\n}\n\nexport interface I_UploadValidationConfig {\n filename: string;\n fileSize?: number;\n}\n\nexport interface I_UploadTypeConfig {\n allowedExtensions: string[];\n sizeLimit: number;\n}\n\nexport interface I_UploadConfig {\n [E_UploadType.IMAGE]: I_UploadTypeConfig;\n [E_UploadType.VIDEO]: I_UploadTypeConfig;\n [E_UploadType.AUDIO]: I_UploadTypeConfig;\n [E_UploadType.DOCUMENT]: I_UploadTypeConfig;\n [E_UploadType.OTHER]: I_UploadTypeConfig;\n}\n\nexport interface I_UploadFileData {\n createReadStream: () => NodeJS.ReadableStream;\n filename: string;\n}\n\nexport interface I_UploadFile {\n file: I_UploadFileData;\n}\n\nexport interface I_UploadOptions {\n file: Promise<I_UploadFile>;\n path: string;\n type: E_UploadType;\n config?: I_UploadConfig;\n /** Base directory for path containment validation. When set, the resolved `path` must reside within this directory. */\n baseDir?: string;\n}\n"],"mappings":";AAAA,IAAY,IAAL,yBAAA,GAAA;QACH,EAAA,QAAA,SACA,EAAA,QAAA,SACA,EAAA,QAAA,SACA,EAAA,WAAA,YACA,EAAA,QAAA;KACH"}
|
|
1
|
+
{"version":3,"file":"upload.type.js","names":[],"sources":["../../../src/node/upload/upload.type.ts"],"sourcesContent":["export enum E_UploadType {\n IMAGE = 'IMAGE',\n VIDEO = 'VIDEO',\n AUDIO = 'AUDIO',\n DOCUMENT = 'DOCUMENT',\n OTHER = 'OTHER',\n}\n\nexport interface I_UploadValidationConfig {\n filename: string;\n fileSize?: number;\n mimetype?: string;\n}\n\nexport interface I_UploadTypeConfig {\n allowedExtensions: string[];\n sizeLimit: number;\n}\n\nexport interface I_UploadConfig {\n [E_UploadType.IMAGE]: I_UploadTypeConfig;\n [E_UploadType.VIDEO]: I_UploadTypeConfig;\n [E_UploadType.AUDIO]: I_UploadTypeConfig;\n [E_UploadType.DOCUMENT]: I_UploadTypeConfig;\n [E_UploadType.OTHER]: I_UploadTypeConfig;\n}\n\nexport interface I_UploadFileData {\n createReadStream: () => NodeJS.ReadableStream;\n filename: string;\n mimetype?: string;\n}\n\nexport interface I_UploadFile {\n file: I_UploadFileData;\n}\n\nexport interface I_UploadOptions {\n file: Promise<I_UploadFile>;\n path: string;\n type: E_UploadType;\n config?: I_UploadConfig;\n /** Base directory for path containment validation. When set, the resolved `path` must reside within this directory. */\n baseDir?: string;\n}\n"],"mappings":";AAAA,IAAY,IAAL,yBAAA,GAAA;QACH,EAAA,QAAA,SACA,EAAA,QAAA,SACA,EAAA,QAAA,SACA,EAAA,WAAA,YACA,EAAA,QAAA;KACH"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReadableStream } from 'node:stream/web';
|
|
2
|
-
import { I_Return } from '
|
|
2
|
+
import { I_Return } from '../../typescript/index.js';
|
|
3
3
|
import { I_UploadConfig, I_UploadFile, I_UploadFileData, I_UploadOptions, I_UploadValidationConfig, E_UploadType } from './upload.type.js';
|
|
4
4
|
/**
|
|
5
5
|
* Calculates the size of a file from a readable stream.
|
|
@@ -73,6 +73,7 @@ export declare function validateUpload(config: I_UploadValidationConfig, uploadC
|
|
|
73
73
|
*
|
|
74
74
|
* @param overrides - Optional configuration overrides to merge with the default configuration.
|
|
75
75
|
* @returns A complete upload configuration object with defaults and any provided overrides.
|
|
76
|
+
* @since 3.13.0
|
|
76
77
|
*/
|
|
77
78
|
export declare function createUploadConfig(overrides?: Partial<I_UploadConfig>): I_UploadConfig;
|
|
78
79
|
/**
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { RESPONSE_STATUS as e } from "../../constant/response-status.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
2
|
+
import { log as t } from "../log/log.util.js";
|
|
3
|
+
import { E_UploadType as n } from "./upload.type.js";
|
|
4
|
+
import { BYTES_PER_MB as r, DEFAULT_UPLOAD_CONFIG as i } from "./upload.constant.js";
|
|
5
|
+
import { createWriteStream as a, mkdirSync as o, pathExistsSync as s } from "../fs/fs.util.js";
|
|
6
|
+
import { dirname as c } from "../path/path.util.js";
|
|
7
7
|
import l from "node:path";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { Buffer as u } from "node:buffer";
|
|
9
|
+
import { Transform as d } from "node:stream";
|
|
10
|
+
import { ReadableStream as f } from "node:stream/web";
|
|
10
11
|
//#region src/node/upload/upload.util.ts
|
|
11
|
-
async function
|
|
12
|
+
async function p(e) {
|
|
12
13
|
return new Promise((t, n) => {
|
|
13
14
|
let r = 0;
|
|
14
15
|
e.on("data", (e) => {
|
|
@@ -16,10 +17,11 @@ async function f(e) {
|
|
|
16
17
|
}), e.on("end", () => t(r)), e.on("error", n);
|
|
17
18
|
});
|
|
18
19
|
}
|
|
19
|
-
async function
|
|
20
|
-
let i = await (await n).file, a = await
|
|
20
|
+
async function m(t, n, r) {
|
|
21
|
+
let i = await (await n).file, a = await p(i.createReadStream()), o = r ?? v(), s = _({
|
|
21
22
|
filename: i.filename,
|
|
22
|
-
fileSize: a
|
|
23
|
+
fileSize: a,
|
|
24
|
+
mimetype: i.mimetype
|
|
23
25
|
}, o, t);
|
|
24
26
|
return s.isValid ? {
|
|
25
27
|
success: !0,
|
|
@@ -31,59 +33,63 @@ async function p(t, n, r) {
|
|
|
31
33
|
code: e.BAD_REQUEST.CODE
|
|
32
34
|
};
|
|
33
35
|
}
|
|
34
|
-
async function
|
|
35
|
-
let i = (
|
|
36
|
+
async function h(e, t, n) {
|
|
37
|
+
let i = (n ?? v())[e], a = await m(e, t, n);
|
|
36
38
|
if (!a.success) return a;
|
|
37
|
-
let { createReadStream: o } = a.result, s = i.sizeLimit,
|
|
38
|
-
s -= e.length, s < 0 ?
|
|
39
|
-
} }),
|
|
39
|
+
let { createReadStream: o } = a.result, s = i.sizeLimit, c = new d({ transform(e, t, n) {
|
|
40
|
+
s -= e.length, s < 0 ? n(/* @__PURE__ */ Error(`File size exceeds limit of ${i.sizeLimit / r}MB`)) : n(null, e);
|
|
41
|
+
} }), l = o().pipe(c);
|
|
40
42
|
return {
|
|
41
43
|
success: !0,
|
|
42
|
-
result: new
|
|
43
|
-
|
|
44
|
-
e.enqueue(typeof t == "string" ?
|
|
45
|
-
}),
|
|
44
|
+
result: new f({ start(e) {
|
|
45
|
+
l.on("data", (t) => {
|
|
46
|
+
e.enqueue(typeof t == "string" ? u.from(t) : t);
|
|
47
|
+
}), l.on("end", () => e.close()), l.on("error", (t) => e.error(t));
|
|
46
48
|
} })
|
|
47
49
|
};
|
|
48
50
|
}
|
|
49
|
-
function
|
|
51
|
+
function g(e, t) {
|
|
50
52
|
let n = e.lastIndexOf(".");
|
|
51
53
|
if (n === -1) return !1;
|
|
52
54
|
let r = e.substring(n + 1).toLowerCase();
|
|
53
55
|
return t.includes(r);
|
|
54
56
|
}
|
|
55
|
-
function
|
|
56
|
-
let { filename:
|
|
57
|
-
if (!
|
|
57
|
+
function _(e, r, i) {
|
|
58
|
+
let { filename: a, fileSize: o, mimetype: s } = e, { allowedExtensions: c, sizeLimit: l } = r[i];
|
|
59
|
+
if (!g(a, c)) return {
|
|
58
60
|
isValid: !1,
|
|
59
|
-
error: `File extension not allowed for ${
|
|
61
|
+
error: `File extension not allowed for ${i.toLowerCase()} files. Allowed extensions: ${c.join(", ")}`
|
|
60
62
|
};
|
|
61
|
-
if (
|
|
62
|
-
let e = Math.round(
|
|
63
|
+
if (o !== void 0 && o > l) {
|
|
64
|
+
let e = Math.round(l / (1024 * 1024));
|
|
63
65
|
return {
|
|
64
66
|
isValid: !1,
|
|
65
|
-
error: `File size exceeds limit for ${
|
|
67
|
+
error: `File size exceeds limit for ${i.toLowerCase()} files. Maximum size: ${e}MB`
|
|
66
68
|
};
|
|
67
69
|
}
|
|
70
|
+
if (s) {
|
|
71
|
+
let e = "";
|
|
72
|
+
i === n.IMAGE ? e = "image/" : i === n.VIDEO ? e = "video/" : i === n.AUDIO && (e = "audio/"), e && !s.startsWith(e) && t.warn(`Advisory Mimetype Warning: File '${a}' (type: ${i}) has unexpected mimetype '${s}'. Expected prefix: '${e}'`);
|
|
73
|
+
}
|
|
68
74
|
return { isValid: !0 };
|
|
69
75
|
}
|
|
70
|
-
function
|
|
76
|
+
function v(e) {
|
|
71
77
|
return {
|
|
72
|
-
...
|
|
78
|
+
...i,
|
|
73
79
|
...e
|
|
74
80
|
};
|
|
75
81
|
}
|
|
76
|
-
async function
|
|
77
|
-
let { path: r, file:
|
|
82
|
+
async function y(t) {
|
|
83
|
+
let { path: r, file: i, config: u, type: d, baseDir: f } = t;
|
|
78
84
|
if (!r || typeof r != "string") return {
|
|
79
85
|
success: !1,
|
|
80
86
|
message: "Invalid path provided",
|
|
81
87
|
code: e.BAD_REQUEST.CODE
|
|
82
88
|
};
|
|
83
|
-
let
|
|
89
|
+
let p = l.resolve(r);
|
|
84
90
|
if (f) {
|
|
85
91
|
let t = l.resolve(f) + l.sep;
|
|
86
|
-
if (!
|
|
92
|
+
if (!p.startsWith(t) && p !== l.resolve(f)) return {
|
|
87
93
|
success: !1,
|
|
88
94
|
message: "Path traversal detected: path resolves outside the allowed base directory",
|
|
89
95
|
code: e.BAD_REQUEST.CODE
|
|
@@ -93,39 +99,43 @@ async function v(n) {
|
|
|
93
99
|
message: "Path traversal detected: \"..\" segments are not allowed",
|
|
94
100
|
code: e.BAD_REQUEST.CODE
|
|
95
101
|
};
|
|
96
|
-
if (!
|
|
102
|
+
if (!i || typeof i != "object") return {
|
|
97
103
|
success: !1,
|
|
98
104
|
message: "Invalid file provided",
|
|
99
105
|
code: e.BAD_REQUEST.CODE
|
|
100
106
|
};
|
|
101
107
|
if (u) {
|
|
102
|
-
let
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
let t = [
|
|
109
|
+
n.IMAGE,
|
|
110
|
+
n.VIDEO,
|
|
111
|
+
n.DOCUMENT,
|
|
112
|
+
n.OTHER
|
|
107
113
|
];
|
|
108
|
-
for (let
|
|
109
|
-
if (!u[
|
|
114
|
+
for (let n of t) {
|
|
115
|
+
if (!u[n] || !Array.isArray(u[n].allowedExtensions) || u[n].allowedExtensions.length === 0) return {
|
|
110
116
|
success: !1,
|
|
111
|
-
message: `Invalid config for ${
|
|
117
|
+
message: `Invalid config for ${n.toLowerCase()} files`,
|
|
112
118
|
code: e.BAD_REQUEST.CODE
|
|
113
119
|
};
|
|
114
|
-
if (typeof u[
|
|
120
|
+
if (typeof u[n].sizeLimit != "number" || u[n].sizeLimit <= 0) return {
|
|
115
121
|
success: !1,
|
|
116
|
-
message: `Invalid size limit for ${
|
|
122
|
+
message: `Invalid size limit for ${n.toLowerCase()} files`,
|
|
117
123
|
code: e.BAD_REQUEST.CODE
|
|
118
124
|
};
|
|
119
125
|
}
|
|
120
126
|
}
|
|
121
127
|
try {
|
|
122
|
-
let t = await
|
|
128
|
+
let t = await m(d, await i, u);
|
|
123
129
|
if (!t.success) return t;
|
|
124
|
-
let { createReadStream: n } = t.result, l =
|
|
125
|
-
|
|
126
|
-
let f = n(),
|
|
127
|
-
return f.pipe(
|
|
128
|
-
|
|
130
|
+
let { createReadStream: n } = t.result, l = c(r);
|
|
131
|
+
s(l) || o(l, { recursive: !0 });
|
|
132
|
+
let f = n(), p = a(r);
|
|
133
|
+
return f.pipe(p), await new Promise((e, t) => {
|
|
134
|
+
p.on("finish", () => e()), p.on("error", (e) => {
|
|
135
|
+
"destroy" in f && typeof f.destroy == "function" && f.destroy(), t(e);
|
|
136
|
+
}), f.on("error", (e) => {
|
|
137
|
+
p.destroy(), t(e);
|
|
138
|
+
});
|
|
129
139
|
}), {
|
|
130
140
|
success: !0,
|
|
131
141
|
result: r,
|
|
@@ -141,6 +151,6 @@ async function v(n) {
|
|
|
141
151
|
}
|
|
142
152
|
}
|
|
143
153
|
//#endregion
|
|
144
|
-
export {
|
|
154
|
+
export { v as createUploadConfig, m as getAndValidateFile, p as getFileSizeFromStream, h as getFileWebStream, y as upload, g as validateFileExtension, _ as validateUpload };
|
|
145
155
|
|
|
146
156
|
//# sourceMappingURL=upload.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.util.js","names":[],"sources":["../../../src/node/upload/upload.util.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\nimport nodePath from 'node:path';\nimport { Transform } from 'node:stream';\nimport { ReadableStream } from 'node:stream/web';\n\nimport type { I_Return } from '#typescript/index.js';\n\nimport { RESPONSE_STATUS } from '#constant/index.js';\n\nimport type { I_UploadConfig, I_UploadFile, I_UploadFileData, I_UploadOptions, I_UploadTypeConfig, I_UploadValidationConfig } from './upload.type.js';\n\nimport { createWriteStream, mkdirSync, pathExistsSync } from '../fs/index.js';\nimport { dirname } from '../path/index.js';\nimport { BYTES_PER_MB, DEFAULT_UPLOAD_CONFIG } from './upload.constant.js';\nimport { E_UploadType } from './upload.type.js';\n\n/**\n * Calculates the size of a file from a readable stream.\n * This function reads through the entire stream to determine the total byte size\n * by accumulating the length of each data chunk.\n *\n * @param stream - The readable stream to calculate the size for.\n * @returns A promise that resolves to the total size of the stream in bytes.\n */\nexport async function getFileSizeFromStream(stream: NodeJS.ReadableStream): Promise<number> {\n return new Promise((resolve, reject) => {\n let size = 0;\n stream.on('data', (chunk) => {\n size += chunk.length;\n });\n stream.on('end', () => resolve(size));\n stream.on('error', reject);\n });\n}\n\n/**\n * Extracts and validates file data from an upload file.\n * This function processes upload files by:\n * - Extracting file metadata and creating a readable stream\n * - Calculating the file size from the stream\n * - Validating file size and extension against upload configuration\n * - Returning a standardized response with success status and error codes\n * - Providing validated file data for further processing\n *\n * @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).\n * @param file - The upload file object containing file metadata and stream creation method.\n * @param config - Optional upload configuration. If not provided, uses default configuration.\n * @returns A promise that resolves to a standardized response containing validated file data or error information.\n */\nexport async function getAndValidateFile(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<I_UploadFileData>> {\n const fileData = await (await file).file;\n const stream = fileData.createReadStream();\n // Stream is consumed here for validation; callers use createReadStream() again for the actual write.\n // This is intentional — createReadStream() is a factory that yields a new stream per call.\n const fileSize = await getFileSizeFromStream(stream);\n const uploadConfig = config ?? createUploadConfig();\n\n const validationResult = validateUpload(\n { filename: fileData.filename, fileSize },\n uploadConfig,\n type,\n );\n\n if (!validationResult.isValid) {\n return {\n success: false,\n message: validationResult.error || 'File validation failed',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n return {\n success: true,\n result: fileData,\n message: 'File validated successfully',\n };\n}\n\n/**\n * Creates a validated web-readable stream from an upload file with size validation.\n * This function processes file uploads for web environments by:\n * - Validating file data using getAndValidateFile function\n * - Creating a size validation transform stream to monitor upload progress\n * - Returning a web-compatible ReadableStream with real-time validation\n * - Providing standardized error responses for validation failures\n * - Wrapping the stream in a standardized response format\n *\n * @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).\n * @param file - The upload file object containing file metadata and stream creation method.\n * @param config - Optional upload configuration. If not provided, uses default configuration.\n * @returns A promise that resolves to a standardized response containing either a web ReadableStream or error information.\n */\nexport async function getFileWebStream(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<ReadableStream<Uint8Array>>> {\n const uploadConfig = config ?? createUploadConfig();\n const typeConfig = uploadConfig[type];\n\n const fileData = await getAndValidateFile(type, file, config);\n\n if (!fileData.success) {\n return fileData;\n }\n\n const { createReadStream } = fileData.result;\n\n let remainingBytes = typeConfig.sizeLimit;\n\n const sizeValidationStream = new Transform({\n transform(chunk: Buffer, _enc: BufferEncoding, cb) {\n remainingBytes -= chunk.length;\n\n if (remainingBytes < 0) {\n cb(new Error(`File size exceeds limit of ${typeConfig.sizeLimit / BYTES_PER_MB}MB`));\n }\n else {\n cb(null, chunk);\n }\n },\n });\n const originalStream = createReadStream();\n const validatedStream = originalStream.pipe(sizeValidationStream);\n\n return {\n success: true,\n result: new ReadableStream<Uint8Array>({\n start(controller) {\n validatedStream.on('data', (chunk: Buffer | string) => {\n controller.enqueue(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);\n });\n validatedStream.on('end', () => controller.close());\n validatedStream.on('error', (err: unknown) => controller.error(err));\n },\n }),\n };\n}\n\n/**\n * Validates if a file has an allowed extension.\n * This function extracts the file extension from the filename and checks if it's\n * included in the list of allowed extensions (case-insensitive comparison).\n *\n * @param filename - The filename to check for valid extension.\n * @param allowedExtensions - An array of allowed file extensions (without dots).\n * @returns True if the file extension is allowed, false otherwise.\n */\nexport function validateFileExtension(filename: string, allowedExtensions: string[]): boolean {\n const lastDotIndex = filename.lastIndexOf('.');\n\n if (lastDotIndex === -1) {\n return false;\n }\n\n const extension = filename.substring(lastDotIndex + 1).toLowerCase();\n\n return allowedExtensions.includes(extension);\n}\n\n/**\n * Validates an upload against the specified configuration.\n * This function performs comprehensive validation including:\n * - File extension validation against allowed extensions\n * - File size validation against size limits\n * - Returns detailed error messages for validation failures\n *\n * @param config - The validation configuration including filename and optional file size.\n * @param uploadConfig - The upload configuration containing allowed extensions and size limits.\n * @param uploadType - The type of upload being validated.\n * @returns An object indicating validation success and optional error message.\n */\nexport function validateUpload(\n config: I_UploadValidationConfig,\n uploadConfig: I_UploadConfig,\n uploadType: E_UploadType,\n): { isValid: boolean; error?: string } {\n const { filename, fileSize } = config;\n const typeConfig: I_UploadTypeConfig = uploadConfig[uploadType];\n\n const { allowedExtensions, sizeLimit } = typeConfig;\n\n if (!validateFileExtension(filename, allowedExtensions)) {\n return {\n isValid: false,\n error: `File extension not allowed for ${uploadType.toLowerCase()} files. Allowed extensions: ${allowedExtensions.join(', ')}`,\n };\n }\n\n if (fileSize !== undefined && fileSize > sizeLimit) {\n const maxSizeMB = Math.round(sizeLimit / (1024 * 1024));\n\n return {\n isValid: false,\n error: `File size exceeds limit for ${uploadType.toLowerCase()} files. Maximum size: ${maxSizeMB}MB`,\n };\n }\n\n return { isValid: true };\n}\n\n/**\n * Creates a default upload configuration with predefined settings for different file types.\n * This function provides sensible defaults for image, video, document, and other file types,\n * including allowed extensions and size limits. The configuration can be customized with overrides.\n *\n * @param overrides - Optional configuration overrides to merge with the default configuration.\n * @returns A complete upload configuration object with defaults and any provided overrides.\n */\nexport function createUploadConfig(overrides?: Partial<I_UploadConfig>): I_UploadConfig {\n return { ...DEFAULT_UPLOAD_CONFIG, ...overrides };\n}\n\n/**\n * Uploads a file with comprehensive validation and error handling.\n * This function processes file uploads with the following features:\n * - Input validation for path and file parameters\n * - Configuration validation for all upload types\n * - File validation using getAndValidateFile function\n * - Automatic directory creation\n * - Stream-based file writing\n * - Comprehensive error handling with standardized response codes\n *\n * @param options - Upload configuration including file, path, type, and optional validation config.\n * @returns A promise that resolves to a standardized response with success status, message, file path, and response codes.\n */\nexport async function upload(options: I_UploadOptions): Promise<I_Return<string>> {\n const { path, file, config, type, baseDir } = options;\n\n if (!path || typeof path !== 'string') {\n return {\n success: false,\n message: 'Invalid path provided',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n // Security: Validate path is within allowed base directory to prevent path traversal.\n // When baseDir is provided, the resolved path must start with it.\n // When baseDir is not provided, reject any path containing \"..\" segments.\n const resolvedPath = nodePath.resolve(path);\n\n if (baseDir) {\n const resolvedBase = nodePath.resolve(baseDir) + nodePath.sep;\n if (!resolvedPath.startsWith(resolvedBase) && resolvedPath !== nodePath.resolve(baseDir)) {\n return {\n success: false,\n message: 'Path traversal detected: path resolves outside the allowed base directory',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n }\n else if (path.includes('..')) {\n return {\n success: false,\n message: 'Path traversal detected: \"..\" segments are not allowed',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n if (!file || typeof file !== 'object') {\n return {\n success: false,\n message: 'Invalid file provided',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n if (config) {\n const requiredTypes = [E_UploadType.IMAGE, E_UploadType.VIDEO, E_UploadType.DOCUMENT, E_UploadType.OTHER];\n\n for (const requiredType of requiredTypes) {\n if (!config[requiredType] || !Array.isArray(config[requiredType].allowedExtensions) || config[requiredType].allowedExtensions.length === 0) {\n return {\n success: false,\n message: `Invalid config for ${requiredType.toLowerCase()} files`,\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n if (typeof config[requiredType].sizeLimit !== 'number' || config[requiredType].sizeLimit <= 0) {\n return {\n success: false,\n message: `Invalid size limit for ${requiredType.toLowerCase()} files`,\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n }\n }\n\n try {\n const fileData = await getAndValidateFile(type, await file, config);\n\n if (!fileData.success) {\n return fileData;\n }\n\n const { createReadStream } = fileData.result;\n\n const dir = dirname(path);\n\n if (!pathExistsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n\n const writeStream = createReadStream();\n const out = createWriteStream(path);\n writeStream.pipe(out);\n\n await new Promise<void>((resolve, reject) => {\n out.on('finish', () => resolve());\n out.on('error', reject);\n writeStream.on('error', reject);\n });\n\n return {\n success: true,\n result: path,\n message: 'File uploaded successfully',\n code: RESPONSE_STATUS.OK.CODE,\n };\n }\n catch (error) {\n return {\n success: false,\n message: error instanceof Error ? error.message : 'File upload failed',\n code: RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE,\n };\n }\n}\n"],"mappings":";;;;;;;;;;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SAAS,aAAiB,QAAQ,EAAM,UAAU;GAClD,MAAM,EAAgB,sBAAsB;GAC/C"}
|
|
1
|
+
{"version":3,"file":"upload.util.js","names":[],"sources":["../../../src/node/upload/upload.util.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\nimport nodePath from 'node:path';\nimport { Transform } from 'node:stream';\nimport { ReadableStream } from 'node:stream/web';\n\nimport type { I_Return } from '#typescript/index.js';\n\nimport { RESPONSE_STATUS } from '#constant/index.js';\n\nimport type { I_UploadConfig, I_UploadFile, I_UploadFileData, I_UploadOptions, I_UploadTypeConfig, I_UploadValidationConfig } from './upload.type.js';\n\nimport { createWriteStream, mkdirSync, pathExistsSync } from '../fs/index.js';\nimport { log } from '../log/index.js';\nimport { dirname } from '../path/index.js';\nimport { BYTES_PER_MB, DEFAULT_UPLOAD_CONFIG } from './upload.constant.js';\nimport { E_UploadType } from './upload.type.js';\n\n/**\n * Calculates the size of a file from a readable stream.\n * This function reads through the entire stream to determine the total byte size\n * by accumulating the length of each data chunk.\n *\n * @param stream - The readable stream to calculate the size for.\n * @returns A promise that resolves to the total size of the stream in bytes.\n */\nexport async function getFileSizeFromStream(stream: NodeJS.ReadableStream): Promise<number> {\n return new Promise((resolve, reject) => {\n let size = 0;\n stream.on('data', (chunk) => {\n size += chunk.length;\n });\n stream.on('end', () => resolve(size));\n stream.on('error', reject);\n });\n}\n\n/**\n * Extracts and validates file data from an upload file.\n * This function processes upload files by:\n * - Extracting file metadata and creating a readable stream\n * - Calculating the file size from the stream\n * - Validating file size and extension against upload configuration\n * - Returning a standardized response with success status and error codes\n * - Providing validated file data for further processing\n *\n * @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).\n * @param file - The upload file object containing file metadata and stream creation method.\n * @param config - Optional upload configuration. If not provided, uses default configuration.\n * @returns A promise that resolves to a standardized response containing validated file data or error information.\n */\nexport async function getAndValidateFile(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<I_UploadFileData>> {\n const fileData = await (await file).file;\n const stream = fileData.createReadStream();\n // Stream is consumed here for validation; callers use createReadStream() again for the actual write.\n // This is intentional — createReadStream() is a factory that yields a new stream per call.\n const fileSize = await getFileSizeFromStream(stream);\n const uploadConfig = config ?? createUploadConfig();\n\n const validationResult = validateUpload(\n { filename: fileData.filename, fileSize, mimetype: fileData.mimetype },\n uploadConfig,\n type,\n );\n\n if (!validationResult.isValid) {\n return {\n success: false,\n message: validationResult.error || 'File validation failed',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n return {\n success: true,\n result: fileData,\n message: 'File validated successfully',\n };\n}\n\n/**\n * Creates a validated web-readable stream from an upload file with size validation.\n * This function processes file uploads for web environments by:\n * - Validating file data using getAndValidateFile function\n * - Creating a size validation transform stream to monitor upload progress\n * - Returning a web-compatible ReadableStream with real-time validation\n * - Providing standardized error responses for validation failures\n * - Wrapping the stream in a standardized response format\n *\n * @param type - The type of upload being processed (IMAGE, VIDEO, DOCUMENT, OTHER).\n * @param file - The upload file object containing file metadata and stream creation method.\n * @param config - Optional upload configuration. If not provided, uses default configuration.\n * @returns A promise that resolves to a standardized response containing either a web ReadableStream or error information.\n */\nexport async function getFileWebStream(type: E_UploadType, file: I_UploadFile, config?: I_UploadConfig): Promise<I_Return<ReadableStream<Uint8Array>>> {\n const uploadConfig = config ?? createUploadConfig();\n const typeConfig = uploadConfig[type];\n\n const fileData = await getAndValidateFile(type, file, config);\n\n if (!fileData.success) {\n return fileData;\n }\n\n const { createReadStream } = fileData.result;\n\n let remainingBytes = typeConfig.sizeLimit;\n\n const sizeValidationStream = new Transform({\n transform(chunk: Buffer, _enc: BufferEncoding, cb) {\n remainingBytes -= chunk.length;\n\n if (remainingBytes < 0) {\n cb(new Error(`File size exceeds limit of ${typeConfig.sizeLimit / BYTES_PER_MB}MB`));\n }\n else {\n cb(null, chunk);\n }\n },\n });\n const originalStream = createReadStream();\n const validatedStream = originalStream.pipe(sizeValidationStream);\n\n return {\n success: true,\n result: new ReadableStream<Uint8Array>({\n start(controller) {\n validatedStream.on('data', (chunk: Buffer | string) => {\n controller.enqueue(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);\n });\n validatedStream.on('end', () => controller.close());\n validatedStream.on('error', (err: unknown) => controller.error(err));\n },\n }),\n };\n}\n\n/**\n * Validates if a file has an allowed extension.\n * This function extracts the file extension from the filename and checks if it's\n * included in the list of allowed extensions (case-insensitive comparison).\n *\n * @param filename - The filename to check for valid extension.\n * @param allowedExtensions - An array of allowed file extensions (without dots).\n * @returns True if the file extension is allowed, false otherwise.\n */\nexport function validateFileExtension(filename: string, allowedExtensions: string[]): boolean {\n const lastDotIndex = filename.lastIndexOf('.');\n\n if (lastDotIndex === -1) {\n return false;\n }\n\n const extension = filename.substring(lastDotIndex + 1).toLowerCase();\n\n return allowedExtensions.includes(extension);\n}\n\n/**\n * Validates an upload against the specified configuration.\n * This function performs comprehensive validation including:\n * - File extension validation against allowed extensions\n * - File size validation against size limits\n * - Returns detailed error messages for validation failures\n *\n * @param config - The validation configuration including filename and optional file size.\n * @param uploadConfig - The upload configuration containing allowed extensions and size limits.\n * @param uploadType - The type of upload being validated.\n * @returns An object indicating validation success and optional error message.\n */\nexport function validateUpload(\n config: I_UploadValidationConfig,\n uploadConfig: I_UploadConfig,\n uploadType: E_UploadType,\n): { isValid: boolean; error?: string } {\n const { filename, fileSize, mimetype } = config;\n const typeConfig: I_UploadTypeConfig = uploadConfig[uploadType];\n\n const { allowedExtensions, sizeLimit } = typeConfig;\n\n if (!validateFileExtension(filename, allowedExtensions)) {\n return {\n isValid: false,\n error: `File extension not allowed for ${uploadType.toLowerCase()} files. Allowed extensions: ${allowedExtensions.join(', ')}`,\n };\n }\n\n if (fileSize !== undefined && fileSize > sizeLimit) {\n const maxSizeMB = Math.round(sizeLimit / (1024 * 1024));\n\n return {\n isValid: false,\n error: `File size exceeds limit for ${uploadType.toLowerCase()} files. Maximum size: ${maxSizeMB}MB`,\n };\n }\n\n if (mimetype) {\n let expectedPrefix = '';\n\n if (uploadType === E_UploadType.IMAGE) {\n expectedPrefix = 'image/';\n }\n else if (uploadType === E_UploadType.VIDEO) {\n expectedPrefix = 'video/';\n }\n else if (uploadType === E_UploadType.AUDIO) {\n expectedPrefix = 'audio/';\n }\n\n if (expectedPrefix && !mimetype.startsWith(expectedPrefix)) {\n // Advisory MIME validation - log warning but DO NOT reject\n log.warn(`Advisory Mimetype Warning: File '${filename}' (type: ${uploadType}) has unexpected mimetype '${mimetype}'. Expected prefix: '${expectedPrefix}'`);\n }\n }\n\n return { isValid: true };\n}\n\n/**\n * Creates a default upload configuration with predefined settings for different file types.\n * This function provides sensible defaults for image, video, document, and other file types,\n * including allowed extensions and size limits. The configuration can be customized with overrides.\n *\n * @param overrides - Optional configuration overrides to merge with the default configuration.\n * @returns A complete upload configuration object with defaults and any provided overrides.\n * @since 3.13.0\n */\nexport function createUploadConfig(overrides?: Partial<I_UploadConfig>): I_UploadConfig {\n return { ...DEFAULT_UPLOAD_CONFIG, ...overrides };\n}\n\n/**\n * Uploads a file with comprehensive validation and error handling.\n * This function processes file uploads with the following features:\n * - Input validation for path and file parameters\n * - Configuration validation for all upload types\n * - File validation using getAndValidateFile function\n * - Automatic directory creation\n * - Stream-based file writing\n * - Comprehensive error handling with standardized response codes\n *\n * @param options - Upload configuration including file, path, type, and optional validation config.\n * @returns A promise that resolves to a standardized response with success status, message, file path, and response codes.\n */\nexport async function upload(options: I_UploadOptions): Promise<I_Return<string>> {\n const { path, file, config, type, baseDir } = options;\n\n if (!path || typeof path !== 'string') {\n return {\n success: false,\n message: 'Invalid path provided',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n // Security: Validate path is within allowed base directory to prevent path traversal.\n // When baseDir is provided, the resolved path must start with it.\n // When baseDir is not provided, reject any path containing \"..\" segments.\n const resolvedPath = nodePath.resolve(path);\n\n if (baseDir) {\n const resolvedBase = nodePath.resolve(baseDir) + nodePath.sep;\n if (!resolvedPath.startsWith(resolvedBase) && resolvedPath !== nodePath.resolve(baseDir)) {\n return {\n success: false,\n message: 'Path traversal detected: path resolves outside the allowed base directory',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n }\n else if (path.includes('..')) {\n return {\n success: false,\n message: 'Path traversal detected: \"..\" segments are not allowed',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n if (!file || typeof file !== 'object') {\n return {\n success: false,\n message: 'Invalid file provided',\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n\n if (config) {\n const requiredTypes = [E_UploadType.IMAGE, E_UploadType.VIDEO, E_UploadType.DOCUMENT, E_UploadType.OTHER];\n\n for (const requiredType of requiredTypes) {\n if (!config[requiredType] || !Array.isArray(config[requiredType].allowedExtensions) || config[requiredType].allowedExtensions.length === 0) {\n return {\n success: false,\n message: `Invalid config for ${requiredType.toLowerCase()} files`,\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n if (typeof config[requiredType].sizeLimit !== 'number' || config[requiredType].sizeLimit <= 0) {\n return {\n success: false,\n message: `Invalid size limit for ${requiredType.toLowerCase()} files`,\n code: RESPONSE_STATUS.BAD_REQUEST.CODE,\n };\n }\n }\n }\n\n try {\n const fileData = await getAndValidateFile(type, await file, config);\n\n if (!fileData.success) {\n return fileData;\n }\n\n const { createReadStream } = fileData.result;\n\n const dir = dirname(path);\n\n if (!pathExistsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n\n const readStream = createReadStream();\n const out = createWriteStream(path);\n readStream.pipe(out);\n\n await new Promise<void>((resolve, reject) => {\n out.on('finish', () => resolve());\n out.on('error', (err) => {\n // Destroy the read stream to release resources if write fails\n if ('destroy' in readStream && typeof readStream.destroy === 'function') {\n readStream.destroy();\n }\n reject(err);\n });\n readStream.on('error', (err) => {\n out.destroy();\n reject(err);\n });\n });\n\n return {\n success: true,\n result: path,\n message: 'File uploaded successfully',\n code: RESPONSE_STATUS.OK.CODE,\n };\n }\n catch (error) {\n return {\n success: false,\n message: error instanceof Error ? error.message : 'File upload failed',\n code: RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;AAyBA,eAAsB,EAAsB,GAAgD;AACxF,QAAO,IAAI,SAAS,GAAS,MAAW;EACpC,IAAI,IAAO;AAKX,EAJA,EAAO,GAAG,SAAS,MAAU;AACzB,QAAQ,EAAM;IAChB,EACF,EAAO,GAAG,aAAa,EAAQ,EAAK,CAAC,EACrC,EAAO,GAAG,SAAS,EAAO;GAC5B;;AAiBN,eAAsB,EAAmB,GAAoB,GAAoB,GAA8D;CAC3I,IAAM,IAAW,OAAO,MAAM,GAAM,MAI9B,IAAW,MAAM,EAHR,EAAS,kBAAkB,CAGU,EAC9C,IAAe,KAAU,GAAoB,EAE7C,IAAmB,EACrB;EAAE,UAAU,EAAS;EAAU;EAAU,UAAU,EAAS;EAAU,EACtE,GACA,EACH;AAUD,QARK,EAAiB,UAQf;EACH,SAAS;EACT,QAAQ;EACR,SAAS;EACZ,GAXU;EACH,SAAS;EACT,SAAS,EAAiB,SAAS;EACnC,MAAM,EAAgB,YAAY;EACrC;;AAwBT,eAAsB,EAAiB,GAAoB,GAAoB,GAAwE;CAEnJ,IAAM,KADe,KAAU,GAAoB,EACnB,IAE1B,IAAW,MAAM,EAAmB,GAAM,GAAM,EAAO;AAE7D,KAAI,CAAC,EAAS,QACV,QAAO;CAGX,IAAM,EAAE,wBAAqB,EAAS,QAElC,IAAiB,EAAW,WAE1B,IAAuB,IAAI,EAAU,EACvC,UAAU,GAAe,GAAsB,GAAI;AAG/C,EAFA,KAAkB,EAAM,QAEpB,IAAiB,IACjB,EAAG,gBAAI,MAAM,8BAA8B,EAAW,YAAY,EAAa,IAAI,CAAC,GAGpF,EAAG,MAAM,EAAM;IAG1B,CAAC,EAEI,IADiB,GAAkB,CACF,KAAK,EAAqB;AAEjE,QAAO;EACH,SAAS;EACT,QAAQ,IAAI,EAA2B,EACnC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EAAK;AAEzB,EAAK,EAAe,EAAI,IACpB,EAAU,GAAK,EAAE,WAAW,IAAM,CAAC;EAGvC,IAAM,IAAa,GAAkB,EAC/B,IAAM,EAAkB,EAAK;AAkBnC,SAjBA,EAAW,KAAK,EAAI,EAEpB,MAAM,IAAI,SAAe,GAAS,MAAW;AASzC,GARA,EAAI,GAAG,gBAAgB,GAAS,CAAC,EACjC,EAAI,GAAG,UAAU,MAAQ;AAKrB,IAHI,aAAa,KAAc,OAAO,EAAW,WAAY,cACzD,EAAW,SAAS,EAExB,EAAO,EAAI;KACb,EACF,EAAW,GAAG,UAAU,MAAQ;AAE5B,IADA,EAAI,SAAS,EACb,EAAO,EAAI;KACb;IACJ,EAEK;GACH,SAAS;GACT,QAAQ;GACR,SAAS;GACT,MAAM,EAAgB,GAAG;GAC5B;UAEE,GAAO;AACV,SAAO;GACH,SAAS;GACT,SAAS,aAAiB,QAAQ,EAAM,UAAU;GAClD,MAAM,EAAgB,sBAAsB;GAC/C"}
|
package/dist/node/ws/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './ws.type.js';
|
|
2
|
+
export * from './ws.util.js';
|
|
@@ -5,6 +5,13 @@ import { I_GraphqlWSOptions, I_WSOptions } from './ws.type.js';
|
|
|
5
5
|
* This function creates a WebSocket server instance that can be attached to an HTTP server
|
|
6
6
|
* and configured with a specific path for WebSocket connections.
|
|
7
7
|
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* **Authentication Warning:** When `sessionParser` is not provided, the WebSocket server
|
|
10
|
+
* has **no session-based authentication**. Any client that can reach the endpoint can
|
|
11
|
+
* establish a connection. Only omit `sessionParser` for truly public WebSocket endpoints
|
|
12
|
+
* (e.g., health-check or anonymous broadcast channels). For authenticated subscriptions,
|
|
13
|
+
* always provide a `sessionParser` to validate sessions on upgrade.
|
|
14
|
+
*
|
|
8
15
|
* @param options - Configuration options including the HTTP server instance and WebSocket path.
|
|
9
16
|
* @returns A configured WebSocket server instance ready to handle connections.
|
|
10
17
|
*/
|