@kaapi/server 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/lib/index.d.ts +27 -0
- package/lib/index.js +93 -0
- package/lib/index.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 demingongo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @kaapi/server
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Hapi from '@hapi/hapi';
|
|
2
|
+
export type PartialServerRoute<Refs extends Hapi.ReqRef = Hapi.ReqRefDefaults> = Partial<Hapi.ServerRoute<Refs>>;
|
|
3
|
+
export interface KaapiServerRoute<Refs extends Hapi.ReqRef = Hapi.ReqRefDefaults> extends PartialServerRoute<Refs> {
|
|
4
|
+
/**
|
|
5
|
+
* if true, it will set options.auth.startegy = 'kaapi'
|
|
6
|
+
*/
|
|
7
|
+
auth?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type KaapiAuthOptions = {
|
|
10
|
+
tokenType?: string;
|
|
11
|
+
validate?: (request: Hapi.Request<Hapi.ReqRefDefaults>, token: string, h: Hapi.ResponseToolkit<Hapi.ReqRefDefaults>) => Promise<{
|
|
12
|
+
isValid?: boolean;
|
|
13
|
+
artifacts?: unknown;
|
|
14
|
+
credentials?: Hapi.AuthCredentials;
|
|
15
|
+
message?: string;
|
|
16
|
+
scheme?: string;
|
|
17
|
+
} | Hapi.Auth>;
|
|
18
|
+
};
|
|
19
|
+
export interface KaapiServerOptions extends Hapi.ServerOptions {
|
|
20
|
+
auth?: KaapiAuthOptions;
|
|
21
|
+
}
|
|
22
|
+
export declare class KaapiServer<A = Hapi.ServerApplicationState> {
|
|
23
|
+
#private;
|
|
24
|
+
get server(): Hapi.Server<A>;
|
|
25
|
+
constructor(opts?: KaapiServerOptions | undefined);
|
|
26
|
+
route<Refs extends Hapi.ReqRef = Hapi.ReqRefDefaults>(serverRoute: KaapiServerRoute<Refs>, handler: Hapi.HandlerDecorations | Hapi.Lifecycle.Method<Refs, Hapi.Lifecycle.ReturnValue<Refs>>): this;
|
|
27
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _KaapiServer_server;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.KaapiServer = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const hapi_1 = tslib_1.__importDefault(require("@hapi/hapi"));
|
|
7
|
+
const hoek_1 = tslib_1.__importDefault(require("@hapi/hoek"));
|
|
8
|
+
const boom_1 = tslib_1.__importDefault(require("@hapi/boom"));
|
|
9
|
+
class KaapiServer {
|
|
10
|
+
get server() {
|
|
11
|
+
return tslib_1.__classPrivateFieldGet(this, _KaapiServer_server, "f");
|
|
12
|
+
}
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
_KaapiServer_server.set(this, void 0);
|
|
15
|
+
const _a = opts || {}, { auth: authOpts } = _a, serverOpts = tslib_1.__rest(_a, ["auth"]);
|
|
16
|
+
tslib_1.__classPrivateFieldSet(this, _KaapiServer_server, hapi_1.default.server(serverOpts), "f");
|
|
17
|
+
// register the auth scheme
|
|
18
|
+
tslib_1.__classPrivateFieldGet(this, _KaapiServer_server, "f").auth.scheme('kaapi-auth', (_server, options) => {
|
|
19
|
+
return {
|
|
20
|
+
authenticate(request, h) {
|
|
21
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
const settings = hoek_1.default.applyToDefaults({
|
|
24
|
+
tokenType: 'Bearer'
|
|
25
|
+
}, options || {});
|
|
26
|
+
const authorization = request.raw.req.headers.authorization;
|
|
27
|
+
const authSplit = authorization ? authorization.split(/\s+/) : ['', ''];
|
|
28
|
+
const tokenType = authSplit[0];
|
|
29
|
+
let token = authSplit[1];
|
|
30
|
+
if (tokenType.toLowerCase() !== ((_a = settings.tokenType) === null || _a === void 0 ? void 0 : _a.toLowerCase())) {
|
|
31
|
+
token = '';
|
|
32
|
+
}
|
|
33
|
+
if (settings.validate) {
|
|
34
|
+
try {
|
|
35
|
+
const result = yield ((_b = settings.validate) === null || _b === void 0 ? void 0 : _b.call(settings, request, token, h));
|
|
36
|
+
if (result && 'isAuth' in result) {
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
if (result) {
|
|
40
|
+
const { isValid, credentials, artifacts, message, scheme } = result;
|
|
41
|
+
if (isValid && credentials) {
|
|
42
|
+
return h.authenticated({ credentials, artifacts });
|
|
43
|
+
}
|
|
44
|
+
if (message) {
|
|
45
|
+
return h.unauthenticated(boom_1.default.unauthorized(message, scheme || settings.tokenType || ''), {
|
|
46
|
+
credentials: credentials || {},
|
|
47
|
+
artifacts
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
return boom_1.default.internal(err instanceof Error ? err : `${err}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return h.unauthenticated(boom_1.default.unauthorized(), { credentials: {} });
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
// register the auth startegy
|
|
62
|
+
tslib_1.__classPrivateFieldGet(this, _KaapiServer_server, "f").auth.strategy('kaapi', 'kaapi-auth', authOpts);
|
|
63
|
+
}
|
|
64
|
+
route(serverRoute, handler) {
|
|
65
|
+
// Set defaults
|
|
66
|
+
if (!serverRoute.method)
|
|
67
|
+
serverRoute.method = '*';
|
|
68
|
+
if (!serverRoute.path)
|
|
69
|
+
serverRoute.path = '/{any*}';
|
|
70
|
+
const { auth } = serverRoute, route = tslib_1.__rest(serverRoute, ["auth"]);
|
|
71
|
+
if (auth &&
|
|
72
|
+
(!route.options ||
|
|
73
|
+
typeof route.options != 'function' && (!route.options.auth ||
|
|
74
|
+
typeof route.options.auth === 'object' &&
|
|
75
|
+
!route.options.auth.strategy))) {
|
|
76
|
+
if (!route.options) {
|
|
77
|
+
route.options = {};
|
|
78
|
+
}
|
|
79
|
+
if (!route.options.auth) {
|
|
80
|
+
route.options.auth = {};
|
|
81
|
+
}
|
|
82
|
+
if (typeof route.options.auth === 'object') {
|
|
83
|
+
route.options.auth.strategy = 'kaapi';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
route.handler = handler;
|
|
87
|
+
tslib_1.__classPrivateFieldGet(this, _KaapiServer_server, "f").route(route);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.KaapiServer = KaapiServer;
|
|
92
|
+
_KaapiServer_server = new WeakMap();
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAA8B;AAC9B,8DAA6B;AAC7B,8DAA6B;AAqB7B,MAAa,WAAW;IAIpB,IAAI,MAAM;QACN,OAAO,+BAAA,IAAI,2BAAQ,CAAA;IACvB,CAAC;IAED,YAAY,IAAqC;QANjD,sCAAwB;QAOpB,MAAM,KAAoC,IAAI,IAAI,EAAE,EAA9C,EAAE,IAAI,EAAE,QAAQ,OAA8B,EAAzB,UAAU,sBAA/B,QAAiC,CAAa,CAAA;QAEpD,+BAAA,IAAI,uBAAW,cAAI,CAAC,MAAM,CAAC,UAAU,CAAC,MAAA,CAAA;QAEtC,2BAA2B;QAC3B,+BAAA,IAAI,2BAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE;YAExD,OAAO;gBACG,YAAY,CAAC,OAAO,EAAE,CAAC;;;wBAEzB,MAAM,QAAQ,GAAqB,cAAI,CAAC,eAAe,CAAC;4BACpD,SAAS,EAAE,QAAQ;yBACtB,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;wBAElB,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC;wBAE5D,MAAM,SAAS,GAAG,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;wBAExE,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;wBAC9B,IAAI,KAAK,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AAG,OAAO,CAAA;QAEvB,+BAAA,IAAI,2BAAQ,CAAC,KAAK,CAAC,KAA+B,CAAC,CAAC;QAEpD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAvGD,kCAuGC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kaapi/server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "kaapi server",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"author": "demingongo",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/demingongo/kaapi.git",
|
|
11
|
+
"directory": "packages/server"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
16
|
+
"@eslint/js": "^9.30.0",
|
|
17
|
+
"@stylistic/eslint-plugin": "^5.0.0",
|
|
18
|
+
"@types/node": "^24.0.7",
|
|
19
|
+
"@typescript-eslint/eslint-plugin": "^8.35.0",
|
|
20
|
+
"@typescript-eslint/parser": "^8.35.0",
|
|
21
|
+
"eslint": "^9.30.0",
|
|
22
|
+
"globals": "^16.2.0",
|
|
23
|
+
"ts-node": "^10.9.2",
|
|
24
|
+
"typescript": "^5.8.3",
|
|
25
|
+
"typescript-eslint": "^8.35.0"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@hapi/boom": "^10.0.1",
|
|
29
|
+
"@hapi/hapi": "^21.4.0",
|
|
30
|
+
"@hapi/hoek": "^11.0.7",
|
|
31
|
+
"tslib": "^2.8.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
37
|
+
}
|
|
38
|
+
}
|