@cored3v/web-core 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -5
- package/dist/cjs/app/createApp.js +4 -1
- package/dist/cjs/app/security.d.ts +9 -0
- package/dist/cjs/app/security.js +7 -2
- package/dist/cjs/auth/jwt.d.ts +3 -0
- package/dist/cjs/auth/jwt.js +8 -1
- package/dist/cjs/frameworks/h3.d.ts +21 -0
- package/dist/cjs/frameworks/h3.js +38 -0
- package/dist/cjs/frameworks/next.d.ts +21 -0
- package/dist/cjs/frameworks/next.js +34 -0
- package/dist/cjs/frameworks/types.d.ts +24 -0
- package/dist/cjs/frameworks/types.js +2 -0
- package/dist/cjs/index.cjs +5 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/index.d.mts +2 -0
- package/dist/cjs/index.mjs +2 -0
- package/dist/cjs/license/crypto.js +7 -7
- package/dist/cjs/types.d.ts +2 -2
- package/dist/esm/app/createApp.js +4 -1
- package/dist/esm/app/security.d.ts +9 -0
- package/dist/esm/app/security.js +6 -2
- package/dist/esm/auth/jwt.d.ts +3 -0
- package/dist/esm/auth/jwt.js +7 -1
- package/dist/esm/frameworks/h3.d.ts +21 -0
- package/dist/esm/frameworks/h3.js +35 -0
- package/dist/esm/frameworks/next.d.ts +21 -0
- package/dist/esm/frameworks/next.js +31 -0
- package/dist/esm/frameworks/types.d.ts +24 -0
- package/dist/esm/frameworks/types.js +1 -0
- package/dist/esm/index.cjs +5 -1
- package/dist/esm/index.d.cts +2 -0
- package/dist/esm/index.d.mts +2 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/license/crypto.js +7 -7
- package/dist/esm/types.d.ts +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,112 @@
|
|
|
1
1
|
# @cored3v/web-core
|
|
2
2
|
|
|
3
|
-
A
|
|
4
|
-
bootstrapping, configuration, authentication, database lifecycle management,
|
|
5
|
-
and **license-based execution enforcement**.
|
|
3
|
+
A generic, production-ready application core for Node.js, designed to provide reusable **bootstrapping, configuration, authentication, and license-based execution enforcement** across multiple frameworks.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
It supports **Express.js**, **Next.js**, **Nuxt (H3)**, and generic Node.js environments.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **License Enforcement**: Protect your source code by enforcing valid license keys.
|
|
10
|
+
- **JWT Authentication**: Optional, built-in JWT verification helper and middleware.
|
|
11
|
+
- **Database Management**: Optional, automatic Postgres connection pooling and lifecycle management.
|
|
12
|
+
- **Security Defaults**: Pre-configured Helmet and CORS for Express.
|
|
13
|
+
- **Multi-Framework**: First-class adapters for Express, Next.js, and H3.
|
|
14
|
+
|
|
15
|
+
> **Note**: Only **License Enforcement** and **Routes** are mandatory. Auth and Database modules are opt-in and handled via configuration.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @cored3v/web-core
|
|
21
|
+
# or
|
|
22
|
+
yarn add @cored3v/web-core
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Next.js (App Router & Middleware)
|
|
28
|
+
|
|
29
|
+
Use `createNextApp` to integrate core services.
|
|
30
|
+
|
|
31
|
+
**`src/lib/core.ts`**
|
|
32
|
+
```typescript
|
|
33
|
+
import { createNextApp } from "@cored3v/web-core";
|
|
34
|
+
|
|
35
|
+
// Initialize once (singleton pattern recommended)
|
|
36
|
+
export const core = await createNextApp({
|
|
37
|
+
appId: "my-next-app"
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**`src/app/api/hello/route.ts`**
|
|
42
|
+
```typescript
|
|
43
|
+
import { core } from "@/lib/core";
|
|
44
|
+
import { NextResponse } from "next/server";
|
|
45
|
+
|
|
46
|
+
export async function GET(request: Request) {
|
|
47
|
+
// Verify Session
|
|
48
|
+
const user = await core.verifySession(request);
|
|
49
|
+
if (!user) {
|
|
50
|
+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return NextResponse.json({ message: "Hello World", user });
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Express.js
|
|
58
|
+
|
|
59
|
+
Use `createApp` to bootstrap a full Express server.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { createApp } from "@cored3v/web-core";
|
|
63
|
+
|
|
64
|
+
await createApp({
|
|
65
|
+
appId: "my-express-app",
|
|
66
|
+
http: { port: 3000 },
|
|
67
|
+
routes: ({ app, router, auth, db }) => {
|
|
68
|
+
|
|
69
|
+
// Protected Route
|
|
70
|
+
router.get("/secure", auth.middleware(), (req, res) => {
|
|
71
|
+
res.json({ message: "Secure Data", user: req.user });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
}).then(({ start }) => start());
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Nuxt / H3 / Nitro
|
|
79
|
+
|
|
80
|
+
Use `createH3App` for H3-based servers.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { createH3App } from "@cored3v/web-core";
|
|
84
|
+
|
|
85
|
+
const core = await createH3App({ appId: "my-nuxt-app" });
|
|
86
|
+
|
|
87
|
+
export default eventHandler(async (event) => {
|
|
88
|
+
const user = await core.verifySession(event);
|
|
89
|
+
if (!user) {
|
|
90
|
+
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
|
91
|
+
}
|
|
92
|
+
return { message: "Success", user };
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
The library uses `dotenv` to load configuration. Ensure you have a `.env` file or environment variables set.
|
|
99
|
+
|
|
100
|
+
| Variable | Description | Required | Default |
|
|
101
|
+
|----------|-------------|----------|---------|
|
|
102
|
+
| `LICENSE_PATH` | Path to the `license.json` file | No | `./license.json` |
|
|
103
|
+
| `JWT_SECRET` | Secret key for JWT verification | **Yes** (if auth used) | - |
|
|
104
|
+
| `DATABASE_URL` | Postgres Connection String | No | - |
|
|
105
|
+
| `PORT` | Http Port (Express only) | No | `3000` |
|
|
106
|
+
|
|
107
|
+
**Note:**
|
|
108
|
+
`license.json` is mandatory for the app to work. However, if the file exists in root directory, then LICENSE_PATH env variable is not required.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
Private / Proprietary.
|
|
@@ -65,7 +65,10 @@ async function createApp(opts) {
|
|
|
65
65
|
// 5) DB (non-optional if declared)
|
|
66
66
|
const db = await (0, index_js_2.initDb)(opts.db);
|
|
67
67
|
// 6) AUTH
|
|
68
|
-
|
|
68
|
+
let auth;
|
|
69
|
+
if (opts.auth) {
|
|
70
|
+
auth = (0, jwt_js_1.initJwtAuth)(config);
|
|
71
|
+
}
|
|
69
72
|
// 7) ROUTES
|
|
70
73
|
if (opts.routes) {
|
|
71
74
|
opts.routes({ app, router, db, auth, config });
|
|
@@ -1,2 +1,11 @@
|
|
|
1
1
|
import { Express } from "express";
|
|
2
|
+
import cors from "cors";
|
|
3
|
+
export declare const securityConfig: {
|
|
4
|
+
helmet: (req: import("http").IncomingMessage, res: import("http").ServerResponse, next: (err?: unknown) => void) => void;
|
|
5
|
+
cors: (req: cors.CorsRequest, res: {
|
|
6
|
+
statusCode?: number | undefined;
|
|
7
|
+
setHeader(key: string, value: string): any;
|
|
8
|
+
end(): any;
|
|
9
|
+
}, next: (err?: any) => any) => void;
|
|
10
|
+
};
|
|
2
11
|
export declare function applySecurity(app: Express): void;
|
package/dist/cjs/app/security.js
CHANGED
|
@@ -3,10 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.securityConfig = void 0;
|
|
6
7
|
exports.applySecurity = applySecurity;
|
|
7
8
|
const helmet_1 = __importDefault(require("helmet"));
|
|
8
9
|
const cors_1 = __importDefault(require("cors"));
|
|
10
|
+
exports.securityConfig = {
|
|
11
|
+
helmet: (0, helmet_1.default)(),
|
|
12
|
+
cors: (0, cors_1.default)()
|
|
13
|
+
};
|
|
9
14
|
function applySecurity(app) {
|
|
10
|
-
app.use(
|
|
11
|
-
app.use(
|
|
15
|
+
app.use(exports.securityConfig.helmet);
|
|
16
|
+
app.use(exports.securityConfig.cors);
|
|
12
17
|
}
|
package/dist/cjs/auth/jwt.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import jwt from "jsonwebtoken";
|
|
1
2
|
import { Config } from "../types.js";
|
|
3
|
+
export declare function verifyToken(token: string, secret: string): string | jwt.JwtPayload;
|
|
2
4
|
export declare function initJwtAuth(config: Config): {
|
|
5
|
+
verify(token: string): string | jwt.JwtPayload;
|
|
3
6
|
middleware(): (req: any, _res: any, next: any) => any;
|
|
4
7
|
};
|
package/dist/cjs/auth/jwt.js
CHANGED
|
@@ -3,13 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.verifyToken = verifyToken;
|
|
6
7
|
exports.initJwtAuth = initJwtAuth;
|
|
7
8
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
9
|
+
function verifyToken(token, secret) {
|
|
10
|
+
return jsonwebtoken_1.default.verify(token, secret);
|
|
11
|
+
}
|
|
8
12
|
function initJwtAuth(config) {
|
|
9
13
|
const secret = config.get("JWT_SECRET");
|
|
10
14
|
if (!secret)
|
|
11
15
|
throw new Error("JWT_SECRET missing");
|
|
12
16
|
return {
|
|
17
|
+
verify(token) {
|
|
18
|
+
return verifyToken(token, secret);
|
|
19
|
+
},
|
|
13
20
|
middleware() {
|
|
14
21
|
return (req, _res, next) => {
|
|
15
22
|
const h = req.headers.authorization;
|
|
@@ -17,7 +24,7 @@ function initJwtAuth(config) {
|
|
|
17
24
|
return next();
|
|
18
25
|
try {
|
|
19
26
|
const token = h.replace("Bearer ", "");
|
|
20
|
-
req.user =
|
|
27
|
+
req.user = verifyToken(token, secret);
|
|
21
28
|
}
|
|
22
29
|
catch { }
|
|
23
30
|
next();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CreateFrameworkAppOptions } from "./types.js";
|
|
2
|
+
export declare function createH3App(opts: CreateFrameworkAppOptions): Promise<{
|
|
3
|
+
db: {
|
|
4
|
+
query: {
|
|
5
|
+
<T extends import("pg").Submittable>(queryStream: T): T;
|
|
6
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryArrayResult<R>>;
|
|
7
|
+
<R extends import("pg").QueryResultRow = any, I = any>(queryConfig: import("pg").QueryConfig<I>): Promise<import("pg").QueryResult<R>>;
|
|
8
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryResult<R>>;
|
|
9
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, callback: (err: Error, result: import("pg").QueryArrayResult<R>) => void): void;
|
|
10
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
11
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryText: string, values: import("pg").QueryConfigValues<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
12
|
+
};
|
|
13
|
+
close: () => Promise<void>;
|
|
14
|
+
} | null;
|
|
15
|
+
auth: {
|
|
16
|
+
verify(token: string): string | import("jsonwebtoken").JwtPayload;
|
|
17
|
+
middleware(): (req: any, _res: any, next: any) => any;
|
|
18
|
+
};
|
|
19
|
+
config: import("../types.js").Config;
|
|
20
|
+
verifySession: (event: any) => Promise<string | import("jsonwebtoken").JwtPayload | null>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createH3App = createH3App;
|
|
4
|
+
const index_js_1 = require("../config/index.js");
|
|
5
|
+
const verify_js_1 = require("../license/verify.js");
|
|
6
|
+
const jwt_js_1 = require("../auth/jwt.js");
|
|
7
|
+
const index_js_2 = require("../db/index.js");
|
|
8
|
+
// H3 is often used in Nuxt or Nitro. We don't import h3 directly to avoid hard dependency,
|
|
9
|
+
// but we assume the user passes the event or app instance if needed.
|
|
10
|
+
// For now, we return a setup object similar to Next.js
|
|
11
|
+
async function createH3App(opts) {
|
|
12
|
+
const config = (0, index_js_1.loadConfig)();
|
|
13
|
+
// License Check
|
|
14
|
+
await (0, verify_js_1.verifyLicense)({
|
|
15
|
+
appId: opts.appId,
|
|
16
|
+
licensePath: config.get("LICENSE_PATH", "./license.json"),
|
|
17
|
+
});
|
|
18
|
+
const db = await (0, index_js_2.initDb)({ type: "postgres" });
|
|
19
|
+
const auth = (0, jwt_js_1.initJwtAuth)(config);
|
|
20
|
+
return {
|
|
21
|
+
db,
|
|
22
|
+
auth,
|
|
23
|
+
config,
|
|
24
|
+
verifySession: async (event) => {
|
|
25
|
+
// H3 event structure abstraction
|
|
26
|
+
const h = event.node?.req?.headers?.authorization || event.headers?.get("authorization");
|
|
27
|
+
if (!h)
|
|
28
|
+
return null;
|
|
29
|
+
try {
|
|
30
|
+
const token = h.replace("Bearer ", "");
|
|
31
|
+
return auth.verify(token);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CreateFrameworkAppOptions } from "./types.js";
|
|
2
|
+
export declare function createNextApp(opts: CreateFrameworkAppOptions): Promise<{
|
|
3
|
+
db: {
|
|
4
|
+
query: {
|
|
5
|
+
<T extends import("pg").Submittable>(queryStream: T): T;
|
|
6
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryArrayResult<R>>;
|
|
7
|
+
<R extends import("pg").QueryResultRow = any, I = any>(queryConfig: import("pg").QueryConfig<I>): Promise<import("pg").QueryResult<R>>;
|
|
8
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryResult<R>>;
|
|
9
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, callback: (err: Error, result: import("pg").QueryArrayResult<R>) => void): void;
|
|
10
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
11
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryText: string, values: import("pg").QueryConfigValues<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
12
|
+
};
|
|
13
|
+
close: () => Promise<void>;
|
|
14
|
+
} | null;
|
|
15
|
+
auth: {
|
|
16
|
+
verify(token: string): string | import("jsonwebtoken").JwtPayload;
|
|
17
|
+
middleware(): (req: any, _res: any, next: any) => any;
|
|
18
|
+
};
|
|
19
|
+
config: import("../types.js").Config;
|
|
20
|
+
verifySession: (req: Request) => Promise<string | import("jsonwebtoken").JwtPayload | null>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createNextApp = createNextApp;
|
|
4
|
+
const index_js_1 = require("../config/index.js");
|
|
5
|
+
const verify_js_1 = require("../license/verify.js");
|
|
6
|
+
const jwt_js_1 = require("../auth/jwt.js");
|
|
7
|
+
const index_js_2 = require("../db/index.js");
|
|
8
|
+
async function createNextApp(opts) {
|
|
9
|
+
const config = (0, index_js_1.loadConfig)();
|
|
10
|
+
// License Check
|
|
11
|
+
await (0, verify_js_1.verifyLicense)({
|
|
12
|
+
appId: opts.appId,
|
|
13
|
+
licensePath: config.get("LICENSE_PATH", "./license.json"),
|
|
14
|
+
});
|
|
15
|
+
const db = await (0, index_js_2.initDb)({ type: "postgres" }); // Defaulting to existing DB logic
|
|
16
|
+
const auth = (0, jwt_js_1.initJwtAuth)(config);
|
|
17
|
+
return {
|
|
18
|
+
db,
|
|
19
|
+
auth,
|
|
20
|
+
config,
|
|
21
|
+
verifySession: async (req) => {
|
|
22
|
+
const h = req.headers.get("authorization");
|
|
23
|
+
if (!h)
|
|
24
|
+
return null;
|
|
25
|
+
try {
|
|
26
|
+
const token = h.replace("Bearer ", "");
|
|
27
|
+
return auth.verify(token);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Config } from "../types.js";
|
|
2
|
+
export interface WebCoreRequest {
|
|
3
|
+
headers: Record<string, string | string[] | undefined>;
|
|
4
|
+
url?: string;
|
|
5
|
+
method?: string;
|
|
6
|
+
body?: any;
|
|
7
|
+
}
|
|
8
|
+
export interface WebCoreResponse {
|
|
9
|
+
status(code: number): WebCoreResponse;
|
|
10
|
+
json(body: any): WebCoreResponse;
|
|
11
|
+
send(body: any): WebCoreResponse;
|
|
12
|
+
setHeader(key: string, value: string): WebCoreResponse;
|
|
13
|
+
}
|
|
14
|
+
export interface WebCoreContext {
|
|
15
|
+
config: Config;
|
|
16
|
+
auth: {
|
|
17
|
+
verify(token: string): any;
|
|
18
|
+
};
|
|
19
|
+
db?: any;
|
|
20
|
+
}
|
|
21
|
+
export interface CreateFrameworkAppOptions {
|
|
22
|
+
appId: string;
|
|
23
|
+
config?: Record<string, any>;
|
|
24
|
+
}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -14,7 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createApp = void 0;
|
|
17
|
+
exports.createH3App = exports.createNextApp = exports.createApp = void 0;
|
|
18
18
|
var createApp_1 = require("./app/createApp");
|
|
19
19
|
Object.defineProperty(exports, "createApp", { enumerable: true, get: function () { return createApp_1.createApp; } });
|
|
20
|
+
var next_1 = require("./frameworks/next");
|
|
21
|
+
Object.defineProperty(exports, "createNextApp", { enumerable: true, get: function () { return next_1.createNextApp; } });
|
|
22
|
+
var h3_1 = require("./frameworks/h3");
|
|
23
|
+
Object.defineProperty(exports, "createH3App", { enumerable: true, get: function () { return h3_1.createH3App; } });
|
|
20
24
|
__exportStar(require("./types"), exports);
|
package/dist/cjs/index.d.cts
CHANGED
package/dist/cjs/index.d.mts
CHANGED
package/dist/cjs/index.mjs
CHANGED
|
@@ -7,13 +7,13 @@ exports.verifySignature = verifySignature;
|
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
const PUBLIC_KEY = `
|
|
9
9
|
-----BEGIN PUBLIC KEY-----
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArCxkEU67ODBGA9LQbMrm
|
|
11
|
+
mquTktu9xme2nJKPoPce1cYqjTOt/NJ2a+LDuhM7t8PHVye18+RJq+mFOkOSgilo
|
|
12
|
+
qpsoM/45NqN4doBkfajZqXtCSjiYg2F4O/zppElzTlsFxc7vtKs+KoTE91s0w0e6
|
|
13
|
+
xTrgdpqwZnUQ9q+16tuCIKPdxJ4mwqZbnUXdE3WKfedbnI2DNE0CfpJFrLkpSsnK
|
|
14
|
+
s/gcPBCi4obvE2RJWNqWoMULW6iLXSNqwDmxE+o87hwCTGfpZ/bxyirRbBCSq/hY
|
|
15
|
+
SdVQJlDhE2ta6CoITROp0c3+xffpCvlYnADp544nuz5IlTZk7LN9X5mF27VcTPc6
|
|
16
|
+
bwIDAQAB
|
|
17
17
|
-----END PUBLIC KEY-----
|
|
18
18
|
`;
|
|
19
19
|
function verifySignature(payload, signature) {
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -26,7 +26,10 @@ export async function createApp(opts) {
|
|
|
26
26
|
// 5) DB (non-optional if declared)
|
|
27
27
|
const db = await initDb(opts.db);
|
|
28
28
|
// 6) AUTH
|
|
29
|
-
|
|
29
|
+
let auth;
|
|
30
|
+
if (opts.auth) {
|
|
31
|
+
auth = initJwtAuth(config);
|
|
32
|
+
}
|
|
30
33
|
// 7) ROUTES
|
|
31
34
|
if (opts.routes) {
|
|
32
35
|
opts.routes({ app, router, db, auth, config });
|
|
@@ -1,2 +1,11 @@
|
|
|
1
1
|
import { Express } from "express";
|
|
2
|
+
import cors from "cors";
|
|
3
|
+
export declare const securityConfig: {
|
|
4
|
+
helmet: (req: import("http").IncomingMessage, res: import("http").ServerResponse, next: (err?: unknown) => void) => void;
|
|
5
|
+
cors: (req: cors.CorsRequest, res: {
|
|
6
|
+
statusCode?: number | undefined;
|
|
7
|
+
setHeader(key: string, value: string): any;
|
|
8
|
+
end(): any;
|
|
9
|
+
}, next: (err?: any) => any) => void;
|
|
10
|
+
};
|
|
2
11
|
export declare function applySecurity(app: Express): void;
|
package/dist/esm/app/security.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import helmet from "helmet";
|
|
2
2
|
import cors from "cors";
|
|
3
|
+
export const securityConfig = {
|
|
4
|
+
helmet: helmet(),
|
|
5
|
+
cors: cors()
|
|
6
|
+
};
|
|
3
7
|
export function applySecurity(app) {
|
|
4
|
-
app.use(helmet
|
|
5
|
-
app.use(cors
|
|
8
|
+
app.use(securityConfig.helmet);
|
|
9
|
+
app.use(securityConfig.cors);
|
|
6
10
|
}
|
package/dist/esm/auth/jwt.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import jwt from "jsonwebtoken";
|
|
1
2
|
import { Config } from "../types.js";
|
|
3
|
+
export declare function verifyToken(token: string, secret: string): string | jwt.JwtPayload;
|
|
2
4
|
export declare function initJwtAuth(config: Config): {
|
|
5
|
+
verify(token: string): string | jwt.JwtPayload;
|
|
3
6
|
middleware(): (req: any, _res: any, next: any) => any;
|
|
4
7
|
};
|
package/dist/esm/auth/jwt.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import jwt from "jsonwebtoken";
|
|
2
|
+
export function verifyToken(token, secret) {
|
|
3
|
+
return jwt.verify(token, secret);
|
|
4
|
+
}
|
|
2
5
|
export function initJwtAuth(config) {
|
|
3
6
|
const secret = config.get("JWT_SECRET");
|
|
4
7
|
if (!secret)
|
|
5
8
|
throw new Error("JWT_SECRET missing");
|
|
6
9
|
return {
|
|
10
|
+
verify(token) {
|
|
11
|
+
return verifyToken(token, secret);
|
|
12
|
+
},
|
|
7
13
|
middleware() {
|
|
8
14
|
return (req, _res, next) => {
|
|
9
15
|
const h = req.headers.authorization;
|
|
@@ -11,7 +17,7 @@ export function initJwtAuth(config) {
|
|
|
11
17
|
return next();
|
|
12
18
|
try {
|
|
13
19
|
const token = h.replace("Bearer ", "");
|
|
14
|
-
req.user =
|
|
20
|
+
req.user = verifyToken(token, secret);
|
|
15
21
|
}
|
|
16
22
|
catch { }
|
|
17
23
|
next();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CreateFrameworkAppOptions } from "./types.js";
|
|
2
|
+
export declare function createH3App(opts: CreateFrameworkAppOptions): Promise<{
|
|
3
|
+
db: {
|
|
4
|
+
query: {
|
|
5
|
+
<T extends import("pg").Submittable>(queryStream: T): T;
|
|
6
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryArrayResult<R>>;
|
|
7
|
+
<R extends import("pg").QueryResultRow = any, I = any>(queryConfig: import("pg").QueryConfig<I>): Promise<import("pg").QueryResult<R>>;
|
|
8
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryResult<R>>;
|
|
9
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, callback: (err: Error, result: import("pg").QueryArrayResult<R>) => void): void;
|
|
10
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
11
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryText: string, values: import("pg").QueryConfigValues<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
12
|
+
};
|
|
13
|
+
close: () => Promise<void>;
|
|
14
|
+
} | null;
|
|
15
|
+
auth: {
|
|
16
|
+
verify(token: string): string | import("jsonwebtoken").JwtPayload;
|
|
17
|
+
middleware(): (req: any, _res: any, next: any) => any;
|
|
18
|
+
};
|
|
19
|
+
config: import("../types.js").Config;
|
|
20
|
+
verifySession: (event: any) => Promise<string | import("jsonwebtoken").JwtPayload | null>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { loadConfig } from "../config/index.js";
|
|
2
|
+
import { verifyLicense } from "../license/verify.js";
|
|
3
|
+
import { initJwtAuth } from "../auth/jwt.js";
|
|
4
|
+
import { initDb } from "../db/index.js";
|
|
5
|
+
// H3 is often used in Nuxt or Nitro. We don't import h3 directly to avoid hard dependency,
|
|
6
|
+
// but we assume the user passes the event or app instance if needed.
|
|
7
|
+
// For now, we return a setup object similar to Next.js
|
|
8
|
+
export async function createH3App(opts) {
|
|
9
|
+
const config = loadConfig();
|
|
10
|
+
// License Check
|
|
11
|
+
await verifyLicense({
|
|
12
|
+
appId: opts.appId,
|
|
13
|
+
licensePath: config.get("LICENSE_PATH", "./license.json"),
|
|
14
|
+
});
|
|
15
|
+
const db = await initDb({ type: "postgres" });
|
|
16
|
+
const auth = initJwtAuth(config);
|
|
17
|
+
return {
|
|
18
|
+
db,
|
|
19
|
+
auth,
|
|
20
|
+
config,
|
|
21
|
+
verifySession: async (event) => {
|
|
22
|
+
// H3 event structure abstraction
|
|
23
|
+
const h = event.node?.req?.headers?.authorization || event.headers?.get("authorization");
|
|
24
|
+
if (!h)
|
|
25
|
+
return null;
|
|
26
|
+
try {
|
|
27
|
+
const token = h.replace("Bearer ", "");
|
|
28
|
+
return auth.verify(token);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CreateFrameworkAppOptions } from "./types.js";
|
|
2
|
+
export declare function createNextApp(opts: CreateFrameworkAppOptions): Promise<{
|
|
3
|
+
db: {
|
|
4
|
+
query: {
|
|
5
|
+
<T extends import("pg").Submittable>(queryStream: T): T;
|
|
6
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryArrayResult<R>>;
|
|
7
|
+
<R extends import("pg").QueryResultRow = any, I = any>(queryConfig: import("pg").QueryConfig<I>): Promise<import("pg").QueryResult<R>>;
|
|
8
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, values?: import("pg").QueryConfigValues<I>): Promise<import("pg").QueryResult<R>>;
|
|
9
|
+
<R extends any[] = any[], I = any[]>(queryConfig: import("pg").QueryArrayConfig<I>, callback: (err: Error, result: import("pg").QueryArrayResult<R>) => void): void;
|
|
10
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryTextOrConfig: string | import("pg").QueryConfig<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
11
|
+
<R extends import("pg").QueryResultRow = any, I = any[]>(queryText: string, values: import("pg").QueryConfigValues<I>, callback: (err: Error, result: import("pg").QueryResult<R>) => void): void;
|
|
12
|
+
};
|
|
13
|
+
close: () => Promise<void>;
|
|
14
|
+
} | null;
|
|
15
|
+
auth: {
|
|
16
|
+
verify(token: string): string | import("jsonwebtoken").JwtPayload;
|
|
17
|
+
middleware(): (req: any, _res: any, next: any) => any;
|
|
18
|
+
};
|
|
19
|
+
config: import("../types.js").Config;
|
|
20
|
+
verifySession: (req: Request) => Promise<string | import("jsonwebtoken").JwtPayload | null>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { loadConfig } from "../config/index.js";
|
|
2
|
+
import { verifyLicense } from "../license/verify.js";
|
|
3
|
+
import { initJwtAuth } from "../auth/jwt.js";
|
|
4
|
+
import { initDb } from "../db/index.js";
|
|
5
|
+
export async function createNextApp(opts) {
|
|
6
|
+
const config = loadConfig();
|
|
7
|
+
// License Check
|
|
8
|
+
await verifyLicense({
|
|
9
|
+
appId: opts.appId,
|
|
10
|
+
licensePath: config.get("LICENSE_PATH", "./license.json"),
|
|
11
|
+
});
|
|
12
|
+
const db = await initDb({ type: "postgres" }); // Defaulting to existing DB logic
|
|
13
|
+
const auth = initJwtAuth(config);
|
|
14
|
+
return {
|
|
15
|
+
db,
|
|
16
|
+
auth,
|
|
17
|
+
config,
|
|
18
|
+
verifySession: async (req) => {
|
|
19
|
+
const h = req.headers.get("authorization");
|
|
20
|
+
if (!h)
|
|
21
|
+
return null;
|
|
22
|
+
try {
|
|
23
|
+
const token = h.replace("Bearer ", "");
|
|
24
|
+
return auth.verify(token);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Config } from "../types.js";
|
|
2
|
+
export interface WebCoreRequest {
|
|
3
|
+
headers: Record<string, string | string[] | undefined>;
|
|
4
|
+
url?: string;
|
|
5
|
+
method?: string;
|
|
6
|
+
body?: any;
|
|
7
|
+
}
|
|
8
|
+
export interface WebCoreResponse {
|
|
9
|
+
status(code: number): WebCoreResponse;
|
|
10
|
+
json(body: any): WebCoreResponse;
|
|
11
|
+
send(body: any): WebCoreResponse;
|
|
12
|
+
setHeader(key: string, value: string): WebCoreResponse;
|
|
13
|
+
}
|
|
14
|
+
export interface WebCoreContext {
|
|
15
|
+
config: Config;
|
|
16
|
+
auth: {
|
|
17
|
+
verify(token: string): any;
|
|
18
|
+
};
|
|
19
|
+
db?: any;
|
|
20
|
+
}
|
|
21
|
+
export interface CreateFrameworkAppOptions {
|
|
22
|
+
appId: string;
|
|
23
|
+
config?: Record<string, any>;
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/esm/index.cjs
CHANGED
|
@@ -14,7 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createApp = void 0;
|
|
17
|
+
exports.createH3App = exports.createNextApp = exports.createApp = void 0;
|
|
18
18
|
var createApp_1 = require("./app/createApp");
|
|
19
19
|
Object.defineProperty(exports, "createApp", { enumerable: true, get: function () { return createApp_1.createApp; } });
|
|
20
|
+
var next_1 = require("./frameworks/next");
|
|
21
|
+
Object.defineProperty(exports, "createNextApp", { enumerable: true, get: function () { return next_1.createNextApp; } });
|
|
22
|
+
var h3_1 = require("./frameworks/h3");
|
|
23
|
+
Object.defineProperty(exports, "createH3App", { enumerable: true, get: function () { return h3_1.createH3App; } });
|
|
20
24
|
__exportStar(require("./types"), exports);
|
package/dist/esm/index.d.cts
CHANGED
package/dist/esm/index.d.mts
CHANGED
package/dist/esm/index.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import crypto from "crypto";
|
|
2
2
|
const PUBLIC_KEY = `
|
|
3
3
|
-----BEGIN PUBLIC KEY-----
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArCxkEU67ODBGA9LQbMrm
|
|
5
|
+
mquTktu9xme2nJKPoPce1cYqjTOt/NJ2a+LDuhM7t8PHVye18+RJq+mFOkOSgilo
|
|
6
|
+
qpsoM/45NqN4doBkfajZqXtCSjiYg2F4O/zppElzTlsFxc7vtKs+KoTE91s0w0e6
|
|
7
|
+
xTrgdpqwZnUQ9q+16tuCIKPdxJ4mwqZbnUXdE3WKfedbnI2DNE0CfpJFrLkpSsnK
|
|
8
|
+
s/gcPBCi4obvE2RJWNqWoMULW6iLXSNqwDmxE+o87hwCTGfpZ/bxyirRbBCSq/hY
|
|
9
|
+
SdVQJlDhE2ta6CoITROp0c3+xffpCvlYnADp544nuz5IlTZk7LN9X5mF27VcTPc6
|
|
10
|
+
bwIDAQAB
|
|
11
11
|
-----END PUBLIC KEY-----
|
|
12
12
|
`;
|
|
13
13
|
export function verifySignature(payload, signature) {
|
package/dist/esm/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cored3v/web-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Reusable licensed Express core for web applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.cjs",
|
|
@@ -39,4 +39,4 @@
|
|
|
39
39
|
"typescript": "^5.4.0",
|
|
40
40
|
"undici-types": "^7.18.2"
|
|
41
41
|
}
|
|
42
|
-
}
|
|
42
|
+
}
|