@pikku/core 0.6.9
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/CHANGELOG.md +339 -0
- package/README.md +5 -0
- package/dist/channel/channel-handler.d.ts +3 -0
- package/dist/channel/channel-handler.js +97 -0
- package/dist/channel/channel-runner.d.ts +26 -0
- package/dist/channel/channel-runner.js +86 -0
- package/dist/channel/channel-store.d.ts +14 -0
- package/dist/channel/channel-store.js +2 -0
- package/dist/channel/channel.types.d.ts +106 -0
- package/dist/channel/channel.types.js +1 -0
- package/dist/channel/eventhub-forwarder.d.ts +17 -0
- package/dist/channel/eventhub-forwarder.js +1 -0
- package/dist/channel/eventhub-service.d.ts +23 -0
- package/dist/channel/eventhub-service.js +1 -0
- package/dist/channel/eventhub-store.d.ts +5 -0
- package/dist/channel/eventhub-store.js +2 -0
- package/dist/channel/index.d.ts +9 -0
- package/dist/channel/index.js +9 -0
- package/dist/channel/local/index.d.ts +3 -0
- package/dist/channel/local/index.js +3 -0
- package/dist/channel/local/local-channel-handler.d.ts +17 -0
- package/dist/channel/local/local-channel-handler.js +41 -0
- package/dist/channel/local/local-channel-runner.d.ts +3 -0
- package/dist/channel/local/local-channel-runner.js +46 -0
- package/dist/channel/local/local-eventhub-service.d.ts +44 -0
- package/dist/channel/local/local-eventhub-service.js +81 -0
- package/dist/channel/log-channels.d.ts +6 -0
- package/dist/channel/log-channels.js +17 -0
- package/dist/channel/pikku-abstract-channel-handler.d.ts +15 -0
- package/dist/channel/pikku-abstract-channel-handler.js +33 -0
- package/dist/channel/serverless/index.d.ts +1 -0
- package/dist/channel/serverless/index.js +1 -0
- package/dist/channel/serverless/serverless-channel-runner.d.ts +10 -0
- package/dist/channel/serverless/serverless-channel-runner.js +98 -0
- package/dist/errors/error-handler.d.ts +39 -0
- package/dist/errors/error-handler.js +54 -0
- package/dist/errors/errors.d.ts +190 -0
- package/dist/errors/errors.js +315 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/http/http-route-runner.d.ts +30 -0
- package/dist/http/http-route-runner.js +208 -0
- package/dist/http/http-routes.types.d.ts +127 -0
- package/dist/http/http-routes.types.js +1 -0
- package/dist/http/http-session-service.d.ts +15 -0
- package/dist/http/http-session-service.js +1 -0
- package/dist/http/index.d.ts +6 -0
- package/dist/http/index.js +5 -0
- package/dist/http/log-http-routes.d.ts +6 -0
- package/dist/http/log-http-routes.js +17 -0
- package/dist/http/pikku-http-abstract-request.d.ts +57 -0
- package/dist/http/pikku-http-abstract-request.js +69 -0
- package/dist/http/pikku-http-abstract-response.d.ts +58 -0
- package/dist/http/pikku-http-abstract-response.js +54 -0
- package/dist/http/pikku-http-session-service.d.ts +45 -0
- package/dist/http/pikku-http-session-service.js +92 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/permissions.d.ts +10 -0
- package/dist/permissions.js +32 -0
- package/dist/pikku-request.d.ts +14 -0
- package/dist/pikku-request.js +21 -0
- package/dist/pikku-response.d.ts +6 -0
- package/dist/pikku-response.js +6 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +3 -0
- package/dist/scheduler/log-schedulers.d.ts +6 -0
- package/dist/scheduler/log-schedulers.js +17 -0
- package/dist/scheduler/scheduler-runner.d.ts +21 -0
- package/dist/scheduler/scheduler-runner.js +73 -0
- package/dist/scheduler/scheduler.types.d.ts +21 -0
- package/dist/scheduler/scheduler.types.js +1 -0
- package/dist/schema.d.ts +26 -0
- package/dist/schema.js +100 -0
- package/dist/services/content-service.d.ts +55 -0
- package/dist/services/content-service.js +1 -0
- package/dist/services/index.d.ts +12 -0
- package/dist/services/index.js +14 -0
- package/dist/services/jwt-service.d.ts +29 -0
- package/dist/services/jwt-service.js +1 -0
- package/dist/services/local-content.d.ts +33 -0
- package/dist/services/local-content.js +6 -0
- package/dist/services/local-secrets.d.ts +26 -0
- package/dist/services/local-secrets.js +39 -0
- package/dist/services/local-variables.d.ts +7 -0
- package/dist/services/local-variables.js +12 -0
- package/dist/services/logger-console.d.ts +52 -0
- package/dist/services/logger-console.js +82 -0
- package/dist/services/logger.d.ts +48 -0
- package/dist/services/logger.js +9 -0
- package/dist/services/schema-service.d.ts +24 -0
- package/dist/services/schema-service.js +1 -0
- package/dist/services/secret-service.d.ts +17 -0
- package/dist/services/secret-service.js +1 -0
- package/dist/services/user-session-service.d.ts +6 -0
- package/dist/services/user-session-service.js +1 -0
- package/dist/services/variables-service.d.ts +4 -0
- package/dist/services/variables-service.js +1 -0
- package/dist/types/core.types.d.ts +109 -0
- package/dist/types/core.types.js +1 -0
- package/dist/types/functions.types.d.ts +27 -0
- package/dist/types/functions.types.js +1 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +15 -0
- package/package.json +48 -0
- package/run-tests.sh +53 -0
- package/src/channel/channel-handler.ts +179 -0
- package/src/channel/channel-runner.ts +156 -0
- package/src/channel/channel-store.ts +32 -0
- package/src/channel/channel.types.ts +202 -0
- package/src/channel/eventhub-forwarder.ts +25 -0
- package/src/channel/eventhub-service.ts +30 -0
- package/src/channel/eventhub-store.ts +8 -0
- package/src/channel/index.ts +9 -0
- package/src/channel/local/index.ts +3 -0
- package/src/channel/local/local-channel-handler.ts +56 -0
- package/src/channel/local/local-channel-runner.test.ts +128 -0
- package/src/channel/local/local-channel-runner.ts +94 -0
- package/src/channel/local/local-eventhub-service.test.ts +95 -0
- package/src/channel/local/local-eventhub-service.ts +95 -0
- package/src/channel/log-channels.ts +20 -0
- package/src/channel/pikku-abstract-channel-handler.test.ts +77 -0
- package/src/channel/pikku-abstract-channel-handler.ts +44 -0
- package/src/channel/serverless/index.ts +1 -0
- package/src/channel/serverless/serverless-channel-runner.ts +190 -0
- package/src/errors/error-handler.ts +74 -0
- package/src/errors/error.test.ts +54 -0
- package/src/errors/errors.ts +340 -0
- package/src/errors/index.ts +2 -0
- package/src/http/http-route-runner.test.ts +193 -0
- package/src/http/http-route-runner.ts +365 -0
- package/src/http/http-routes.types.ts +189 -0
- package/src/http/http-session-service.ts +19 -0
- package/src/http/index.ts +16 -0
- package/src/http/log-http-routes.ts +20 -0
- package/src/http/pikku-http-abstract-request.ts +95 -0
- package/src/http/pikku-http-abstract-response.ts +79 -0
- package/src/http/pikku-http-session-service.test.ts +106 -0
- package/src/http/pikku-http-session-service.ts +135 -0
- package/src/index.ts +15 -0
- package/src/permissions.test.ts +58 -0
- package/src/permissions.ts +45 -0
- package/src/pikku-request.ts +19 -0
- package/src/pikku-response.ts +5 -0
- package/src/scheduler/index.ts +11 -0
- package/src/scheduler/log-schedulers.ts +20 -0
- package/src/scheduler/scheduler-runner.ts +132 -0
- package/src/scheduler/scheduler.types.ts +27 -0
- package/src/schema.test.ts +57 -0
- package/src/schema.ts +124 -0
- package/src/services/content-service.ts +69 -0
- package/src/services/index.ts +16 -0
- package/src/services/jwt-service.ts +36 -0
- package/src/services/local-content.ts +39 -0
- package/src/services/local-secrets.ts +43 -0
- package/src/services/local-variables.ts +17 -0
- package/src/services/logger-console.ts +97 -0
- package/src/services/logger.ts +57 -0
- package/src/services/schema-service.ts +26 -0
- package/src/services/secret-service.ts +17 -0
- package/src/services/user-session-service.ts +7 -0
- package/src/services/variables-service.ts +6 -0
- package/src/types/core.types.ts +162 -0
- package/src/types/functions.types.ts +44 -0
- package/src/utils.ts +21 -0
- package/tsconfig.json +13 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CoreUserSession } from '../types/core.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for handling JSON Web Tokens (JWT).
|
|
4
|
+
* @template UserSession - The type of the user session.
|
|
5
|
+
*/
|
|
6
|
+
export interface JWTService<UserSession = CoreUserSession> {
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a payload into a JWT.
|
|
9
|
+
* @param expiresIn - The expiration time of the token.
|
|
10
|
+
* @param payload - The payload to encode.
|
|
11
|
+
* @returns A promise that resolves to the encoded JWT.
|
|
12
|
+
*/
|
|
13
|
+
encode: <T extends any>(expiresIn: string, payload: T) => Promise<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Decodes a JWT into its payload.
|
|
16
|
+
* @param hash - The JWT to decode.
|
|
17
|
+
* @param invalidHashError - An optional error to throw if the hash is invalid.
|
|
18
|
+
* @param debug - An optional flag for debugging.
|
|
19
|
+
* @returns A promise that resolves to the decoded payload.
|
|
20
|
+
*/
|
|
21
|
+
decode: <T>(hash: string, invalidHashError?: Error, debug?: boolean) => Promise<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Decodes a user session from a JWT.
|
|
24
|
+
* @param jwtToken - The JWT representing the user session.
|
|
25
|
+
* @param debug - An optional flag for debugging.
|
|
26
|
+
* @returns A promise that resolves to the decoded user session.
|
|
27
|
+
*/
|
|
28
|
+
decodeSession: (jwtToken: string, debug?: any) => Promise<UserSession>;
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ContentService } from './content-service.js';
|
|
2
|
+
import { Logger } from './logger.js';
|
|
3
|
+
export interface LocalContentConfig {
|
|
4
|
+
contentDirectory: string;
|
|
5
|
+
assetsUrl: string;
|
|
6
|
+
uploadUrl: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Service for handling local content operations.
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class LocalContent implements ContentService {
|
|
12
|
+
constructor(_config: LocalContentConfig, _logger: Logger);
|
|
13
|
+
abstract getUploadURL: (fileKey: string, contentType: string) => Promise<{
|
|
14
|
+
uploadUrl: string;
|
|
15
|
+
assetKey: string;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the local content service.
|
|
19
|
+
*/
|
|
20
|
+
abstract init(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Signs a URL to provide secure access.
|
|
23
|
+
* @param url - The URL to sign.
|
|
24
|
+
* @returns A promise that resolves to the signed URL.
|
|
25
|
+
*/
|
|
26
|
+
abstract signURL(url: string): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Signs a content key to provide secure access to an asset.
|
|
29
|
+
* @param assetKey - The key of the asset to sign.
|
|
30
|
+
* @returns A promise that resolves to the signed content key URL.
|
|
31
|
+
*/
|
|
32
|
+
abstract signContentKey(assetKey: string): Promise<string>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SecretService } from './secret-service.js';
|
|
2
|
+
import { VariablesService } from './variables-service.js';
|
|
3
|
+
/**
|
|
4
|
+
* Service for retrieving secrets from environment variables.
|
|
5
|
+
*/
|
|
6
|
+
export declare class LocalSecretService implements SecretService {
|
|
7
|
+
private variablesService;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of LocalSecretService.
|
|
10
|
+
*/
|
|
11
|
+
constructor(variablesService?: VariablesService);
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves a secret by key.
|
|
14
|
+
* @param key - The key of the secret to retrieve.
|
|
15
|
+
* @returns A promise that resolves to the secret value.
|
|
16
|
+
* @throws {Error} If the secret is not found.
|
|
17
|
+
*/
|
|
18
|
+
getSecretJSON<R>(key: string): Promise<R>;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves a secret by key.
|
|
21
|
+
* @param key - The key of the secret to retrieve.
|
|
22
|
+
* @returns A promise that resolves to the secret value.
|
|
23
|
+
* @throws {Error} If the secret is not found.
|
|
24
|
+
*/
|
|
25
|
+
getSecret(key: string): Promise<string>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { LocalVariablesService } from './local-variables.js';
|
|
2
|
+
/**
|
|
3
|
+
* Service for retrieving secrets from environment variables.
|
|
4
|
+
*/
|
|
5
|
+
export class LocalSecretService {
|
|
6
|
+
variablesService;
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of LocalSecretService.
|
|
9
|
+
*/
|
|
10
|
+
constructor(variablesService = new LocalVariablesService()) {
|
|
11
|
+
this.variablesService = variablesService;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Retrieves a secret by key.
|
|
15
|
+
* @param key - The key of the secret to retrieve.
|
|
16
|
+
* @returns A promise that resolves to the secret value.
|
|
17
|
+
* @throws {Error} If the secret is not found.
|
|
18
|
+
*/
|
|
19
|
+
async getSecretJSON(key) {
|
|
20
|
+
const value = await this.variablesService.get(key);
|
|
21
|
+
if (value) {
|
|
22
|
+
return JSON.parse(value);
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`Secret Not Found: ${key}`);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves a secret by key.
|
|
28
|
+
* @param key - The key of the secret to retrieve.
|
|
29
|
+
* @returns A promise that resolves to the secret value.
|
|
30
|
+
* @throws {Error} If the secret is not found.
|
|
31
|
+
*/
|
|
32
|
+
async getSecret(key) {
|
|
33
|
+
const value = await this.variablesService.get(key);
|
|
34
|
+
if (value) {
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
throw new Error(`Secret Not Found: ${key}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { VariablesService } from './variables-service.js';
|
|
2
|
+
export declare class LocalVariablesService implements VariablesService {
|
|
3
|
+
private variables;
|
|
4
|
+
constructor(variables?: Record<string, string | undefined>);
|
|
5
|
+
getAll(): Promise<Record<string, string | undefined>> | Record<string, string | undefined>;
|
|
6
|
+
get(name: string): Promise<string | undefined> | string | undefined;
|
|
7
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Logger, LogLevel } from './logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* A logger implementation that logs messages to the console.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ConsoleLogger implements Logger {
|
|
6
|
+
/**
|
|
7
|
+
* The current logging level.
|
|
8
|
+
*/
|
|
9
|
+
private level;
|
|
10
|
+
/**
|
|
11
|
+
* Sets the logging level.
|
|
12
|
+
* @param level - The logging level to set.
|
|
13
|
+
*/
|
|
14
|
+
setLevel(level: LogLevel): void;
|
|
15
|
+
/**
|
|
16
|
+
* Logs a trace message.
|
|
17
|
+
* @param message - The message to log.
|
|
18
|
+
* @param meta - Additional metadata to log.
|
|
19
|
+
*/
|
|
20
|
+
trace?(message: string, ...meta: any[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Logs a debug message.
|
|
23
|
+
* @param message - The message to log.
|
|
24
|
+
* @param meta - Additional metadata to log.
|
|
25
|
+
*/
|
|
26
|
+
debug(message: string, ...meta: any[]): void;
|
|
27
|
+
/**
|
|
28
|
+
* Logs an informational message.
|
|
29
|
+
* @param messageOrObj - The message or object to log.
|
|
30
|
+
* @param meta - Additional metadata to log.
|
|
31
|
+
*/
|
|
32
|
+
info(messageOrObj: string | Record<string, any>, ...meta: any[]): void;
|
|
33
|
+
/**
|
|
34
|
+
* Logs a warning message.
|
|
35
|
+
* @param messageOrObj - The message or object to log.
|
|
36
|
+
* @param meta - Additional metadata to log.
|
|
37
|
+
*/
|
|
38
|
+
warn(messageOrObj: string | Record<string, any>, ...meta: any[]): void;
|
|
39
|
+
/**
|
|
40
|
+
* Logs an error message.
|
|
41
|
+
* @param messageOrObj - The message, object, or error to log.
|
|
42
|
+
* @param meta - Additional metadata to log.
|
|
43
|
+
*/
|
|
44
|
+
error(messageOrObj: string | Record<string, any> | Error, ...meta: any[]): void;
|
|
45
|
+
/**
|
|
46
|
+
* Logs a message at a specified level.
|
|
47
|
+
* @param level - The logging level.
|
|
48
|
+
* @param message - The message to log.
|
|
49
|
+
* @param meta - Additional metadata to log.
|
|
50
|
+
*/
|
|
51
|
+
log(level: string, message: string, ...meta: any[]): void;
|
|
52
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LogLevel } from './logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* A logger implementation that logs messages to the console.
|
|
4
|
+
*/
|
|
5
|
+
export class ConsoleLogger {
|
|
6
|
+
/**
|
|
7
|
+
* The current logging level.
|
|
8
|
+
*/
|
|
9
|
+
level = LogLevel.info;
|
|
10
|
+
/**
|
|
11
|
+
* Sets the logging level.
|
|
12
|
+
* @param level - The logging level to set.
|
|
13
|
+
*/
|
|
14
|
+
setLevel(level) {
|
|
15
|
+
this.level = level;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Logs a trace message.
|
|
19
|
+
* @param message - The message to log.
|
|
20
|
+
* @param meta - Additional metadata to log.
|
|
21
|
+
*/
|
|
22
|
+
trace(message, ...meta) {
|
|
23
|
+
if (this.level <= LogLevel.trace) {
|
|
24
|
+
console.trace('TRACE:', message, ...meta);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Logs a debug message.
|
|
29
|
+
* @param message - The message to log.
|
|
30
|
+
* @param meta - Additional metadata to log.
|
|
31
|
+
*/
|
|
32
|
+
debug(message, ...meta) {
|
|
33
|
+
if (this.level <= LogLevel.debug) {
|
|
34
|
+
console.debug('DEBUG:', message, ...meta);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Logs an informational message.
|
|
39
|
+
* @param messageOrObj - The message or object to log.
|
|
40
|
+
* @param meta - Additional metadata to log.
|
|
41
|
+
*/
|
|
42
|
+
info(messageOrObj, ...meta) {
|
|
43
|
+
if (this.level <= LogLevel.info) {
|
|
44
|
+
console.info('INFO:', messageOrObj, ...meta);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Logs a warning message.
|
|
49
|
+
* @param messageOrObj - The message or object to log.
|
|
50
|
+
* @param meta - Additional metadata to log.
|
|
51
|
+
*/
|
|
52
|
+
warn(messageOrObj, ...meta) {
|
|
53
|
+
if (this.level <= LogLevel.warn) {
|
|
54
|
+
console.warn('WARN:', messageOrObj, ...meta);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Logs an error message.
|
|
59
|
+
* @param messageOrObj - The message, object, or error to log.
|
|
60
|
+
* @param meta - Additional metadata to log.
|
|
61
|
+
*/
|
|
62
|
+
error(messageOrObj, ...meta) {
|
|
63
|
+
if (this.level <= LogLevel.error) {
|
|
64
|
+
console.error('ERROR:', messageOrObj instanceof Error ? messageOrObj.message : messageOrObj, ...meta);
|
|
65
|
+
if (messageOrObj instanceof Error) {
|
|
66
|
+
console.error('STACK:', messageOrObj.stack);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Logs a message at a specified level.
|
|
72
|
+
* @param level - The logging level.
|
|
73
|
+
* @param message - The message to log.
|
|
74
|
+
* @param meta - Additional metadata to log.
|
|
75
|
+
*/
|
|
76
|
+
log(level, message, ...meta) {
|
|
77
|
+
const logLevel = LogLevel[level];
|
|
78
|
+
if (this.level <= logLevel) {
|
|
79
|
+
console.log(`${level.toUpperCase()}:`, message, ...meta);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export declare enum LogLevel {
|
|
2
|
+
'trace' = 0,// Add this level
|
|
3
|
+
'debug' = 1,
|
|
4
|
+
'info' = 2,
|
|
5
|
+
'warn' = 3,
|
|
6
|
+
'error' = 4,
|
|
7
|
+
'critical' = 5
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Interface for logging messages at various levels.
|
|
11
|
+
*/
|
|
12
|
+
export interface Logger {
|
|
13
|
+
/**
|
|
14
|
+
* Logs an informational message.
|
|
15
|
+
* @param messageOrObj - The message or object to log.
|
|
16
|
+
* @param meta - Additional metadata to log.
|
|
17
|
+
*/
|
|
18
|
+
info(messageOrObj: string | Record<string, any>, ...meta: any[]): void;
|
|
19
|
+
/**
|
|
20
|
+
* Logs a warning message.
|
|
21
|
+
* @param messageOrObj - The message or object to log.
|
|
22
|
+
* @param meta - Additional metadata to log.
|
|
23
|
+
*/
|
|
24
|
+
warn(messageOrObj: string | Record<string, any>, ...meta: any[]): void;
|
|
25
|
+
/**
|
|
26
|
+
* Logs an error message.
|
|
27
|
+
* @param messageOrObj - The message, object, or error to log.
|
|
28
|
+
* @param meta - Additional metadata to log.
|
|
29
|
+
*/
|
|
30
|
+
error(messageOrObj: string | Record<string, any> | Error, ...meta: any[]): void;
|
|
31
|
+
/**
|
|
32
|
+
* Logs a debug message.
|
|
33
|
+
* @param message - The message to log.
|
|
34
|
+
* @param meta - Additional metadata to log.
|
|
35
|
+
*/
|
|
36
|
+
debug(message: string, ...meta: any[]): void;
|
|
37
|
+
/**
|
|
38
|
+
* Logs a trace message.
|
|
39
|
+
* @param message - The message to log.
|
|
40
|
+
* @param meta - Additional metadata to log.
|
|
41
|
+
*/
|
|
42
|
+
trace?(message: string, ...meta: any[]): void;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the logging level.
|
|
45
|
+
* @param level - The logging level to set.
|
|
46
|
+
*/
|
|
47
|
+
setLevel(level: LogLevel): void;
|
|
48
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export var LogLevel;
|
|
2
|
+
(function (LogLevel) {
|
|
3
|
+
LogLevel[LogLevel["trace"] = 0] = "trace";
|
|
4
|
+
LogLevel[LogLevel["debug"] = 1] = "debug";
|
|
5
|
+
LogLevel[LogLevel["info"] = 2] = "info";
|
|
6
|
+
LogLevel[LogLevel["warn"] = 3] = "warn";
|
|
7
|
+
LogLevel[LogLevel["error"] = 4] = "error";
|
|
8
|
+
LogLevel[LogLevel["critical"] = 5] = "critical";
|
|
9
|
+
})(LogLevel || (LogLevel = {}));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for a schema service that handles schema compilation, validation, and management.
|
|
3
|
+
*/
|
|
4
|
+
export interface SchemaService {
|
|
5
|
+
/**
|
|
6
|
+
* Compiles a schema with the provided name and value, making it available for use.
|
|
7
|
+
* @param {string} name - The unique name for the schema.
|
|
8
|
+
* @param {any} value - The schema definition or configuration to be compiled.
|
|
9
|
+
* @returns {Promise<void> | void} A promise if asynchronous, or void if synchronous.
|
|
10
|
+
*/
|
|
11
|
+
compileSchema: (name: string, value: any) => Promise<void> | void;
|
|
12
|
+
/**
|
|
13
|
+
* Validates data against a specified schema.
|
|
14
|
+
* @param {string} schema - The name of the schema to validate against.
|
|
15
|
+
* @param {any} data - The data to validate against the schema.
|
|
16
|
+
* @returns {Promise<void> | void} A promise if asynchronous, or void if synchronous.
|
|
17
|
+
*/
|
|
18
|
+
validateSchema: (schema: string, data: any) => Promise<void> | void;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves a set of all registered schema names.
|
|
21
|
+
* @returns {Set<string>} A set containing the names of all available schemas.
|
|
22
|
+
*/
|
|
23
|
+
getSchemaNames: () => Set<string>;
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for retrieving secrets.
|
|
3
|
+
*/
|
|
4
|
+
export interface SecretService {
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves a secret by key.
|
|
7
|
+
* @param key - The key of the secret to retrieve.
|
|
8
|
+
* @returns A promise that resolves to the secret value.
|
|
9
|
+
*/
|
|
10
|
+
getSecretJSON<Return = {}>(key: string): Promise<Return>;
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves a secret by key.
|
|
13
|
+
* @param key - The key of the secret to retrieve.
|
|
14
|
+
* @returns A promise that resolves to the secret value.
|
|
15
|
+
*/
|
|
16
|
+
getSecret(key: string): Promise<string>;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CoreUserSession } from '../types/core.types.js';
|
|
2
|
+
export interface UserSessionService<UserSession extends CoreUserSession> {
|
|
3
|
+
setSession: (session: UserSession) => Promise<void> | void;
|
|
4
|
+
deleteSession: () => Promise<void> | void;
|
|
5
|
+
getSession: () => Promise<UserSession | void> | UserSession | void;
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Logger, LogLevel } from '../services/logger.js';
|
|
2
|
+
import type { HTTPSessionService } from '../http/http-session-service.js';
|
|
3
|
+
import { VariablesService } from '../services/variables-service.js';
|
|
4
|
+
import { EventHubService } from '../channel/eventhub-service.js';
|
|
5
|
+
import { SchemaService } from '../services/schema-service.js';
|
|
6
|
+
import { enforceChannelAccess } from '../channel/channel.types.js';
|
|
7
|
+
import { enforceHTTPAccess, PikkuHTTP } from '../http/http-routes.types.js';
|
|
8
|
+
import { UserSessionService } from '../services/user-session-service.js';
|
|
9
|
+
import { JWTService } from '../services/jwt-service.js';
|
|
10
|
+
import { SecretService } from '../services/secret-service.js';
|
|
11
|
+
export type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
|
12
|
+
/**
|
|
13
|
+
* Represents a JSON primitive type which can be a string, number, boolean, null, or undefined.
|
|
14
|
+
*/
|
|
15
|
+
export type JSONPrimitive = string | number | boolean | null | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Represents a JSON value which can be a primitive, an array, or an object.
|
|
18
|
+
*/
|
|
19
|
+
export type JSONValue = JSONPrimitive | JSONValue[] | {
|
|
20
|
+
[key: string]: JSONValue;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Utility type for making certain keys required and leaving the rest as optional.
|
|
24
|
+
*/
|
|
25
|
+
export type PickRequired<T, K extends keyof T> = Required<Pick<T, K>> & Partial<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Utility type for making certain keys optional while keeping the rest required.
|
|
28
|
+
*/
|
|
29
|
+
export type PickOptional<T, K extends keyof T> = Partial<T> & Pick<T, K>;
|
|
30
|
+
/**
|
|
31
|
+
* Utility type that ensures at least one key in the given type `T` is required.
|
|
32
|
+
*/
|
|
33
|
+
export type RequireAtLeastOne<T> = {
|
|
34
|
+
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
|
|
35
|
+
}[keyof T];
|
|
36
|
+
/**
|
|
37
|
+
* Interface for the core configuration settings of Pikku.
|
|
38
|
+
*/
|
|
39
|
+
export type CoreConfig<Config extends Record<string, unknown> = {}> = {
|
|
40
|
+
/** The log level for the application. */
|
|
41
|
+
logLevel?: LogLevel;
|
|
42
|
+
/** The maximum compute time allowed, in milliseconds (optional). */
|
|
43
|
+
maximumComputeTime?: number;
|
|
44
|
+
/** Secrets used by the application (optional). */
|
|
45
|
+
secrets?: {};
|
|
46
|
+
} & Config;
|
|
47
|
+
/**
|
|
48
|
+
* Represents a core user session, which can be extended for more specific session information.
|
|
49
|
+
*/
|
|
50
|
+
export type CoreUserSession<UserSession = unknown> = UserSession;
|
|
51
|
+
/**
|
|
52
|
+
* Interface for core singleton services provided by Pikku.
|
|
53
|
+
*/
|
|
54
|
+
export type CoreSingletonServices<Config extends CoreConfig = CoreConfig, UserSession extends CoreUserSession = CoreUserSession, UserServices extends Record<string, unknown> = {}> = {
|
|
55
|
+
/** The http permission service used for authorization (optional). */
|
|
56
|
+
enforceHTTPAccess?: enforceHTTPAccess;
|
|
57
|
+
/** The channel permission service used by the application (optional). */
|
|
58
|
+
enforceChannelAccess?: enforceChannelAccess;
|
|
59
|
+
/** JWT Service */
|
|
60
|
+
jwt?: JWTService<UserSession>;
|
|
61
|
+
/** The session service used by the application (optional). */
|
|
62
|
+
httpSessionService?: HTTPSessionService;
|
|
63
|
+
/** The schema library used to validate data */
|
|
64
|
+
schemaService?: SchemaService;
|
|
65
|
+
/** The core configuration for the application. */
|
|
66
|
+
config: Config;
|
|
67
|
+
/** The logger used by the application. */
|
|
68
|
+
logger: Logger;
|
|
69
|
+
/** The variable service to be used */
|
|
70
|
+
variablesService: VariablesService;
|
|
71
|
+
/** The subscription service that is passed to streams */
|
|
72
|
+
eventHub?: EventHubService<unknown>;
|
|
73
|
+
/** SecretServce */
|
|
74
|
+
secretService?: SecretService;
|
|
75
|
+
} & UserServices;
|
|
76
|
+
/**
|
|
77
|
+
* Represents different forms of interaction within Pikku and the outside world.
|
|
78
|
+
*/
|
|
79
|
+
export interface PikkuInteractions {
|
|
80
|
+
http?: PikkuHTTP;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Represents the core services used by Pikku, including singleton services and the request/response interaction.
|
|
84
|
+
*/
|
|
85
|
+
export type CoreServices<SingletonServices = CoreSingletonServices, UserSession extends CoreUserSession = CoreUserSession, CoreServices extends Record<string, unknown> = {}> = SingletonServices & PikkuInteractions & {
|
|
86
|
+
userSession?: UserSessionService<UserSession>;
|
|
87
|
+
} & CoreServices;
|
|
88
|
+
export type SessionServices<SingletonServices extends CoreSingletonServices = CoreSingletonServices, Services = CoreServices<SingletonServices>> = Omit<Services, keyof SingletonServices | keyof PikkuInteractions | 'session'>;
|
|
89
|
+
/**
|
|
90
|
+
* Defines a function type for creating singleton services from the given configuration.
|
|
91
|
+
*/
|
|
92
|
+
export type CreateSingletonServices<Config extends CoreConfig, SingletonServices extends CoreSingletonServices> = (config: Config, existingServices?: Partial<SingletonServices>) => Promise<SingletonServices>;
|
|
93
|
+
/**
|
|
94
|
+
* Defines a function type for creating session-specific services.
|
|
95
|
+
*/
|
|
96
|
+
export type CreateSessionServices<SingletonServices extends CoreSingletonServices = CoreSingletonServices, Services extends CoreServices<SingletonServices> = CoreServices<SingletonServices>, UserSession extends CoreUserSession = CoreUserSession> = (services: SingletonServices, interaction: PikkuInteractions, session: UserSession | undefined) => Promise<SessionServices<Services, SingletonServices>>;
|
|
97
|
+
/**
|
|
98
|
+
* Defines a function type for creating config.
|
|
99
|
+
*/
|
|
100
|
+
export type CreateConfig<Config extends CoreConfig> = (variablesService?: VariablesService) => Promise<Config>;
|
|
101
|
+
/**
|
|
102
|
+
* Represents the documentation for a route, including summary, description, tags, and errors.
|
|
103
|
+
*/
|
|
104
|
+
export type APIDocs = {
|
|
105
|
+
summary?: string;
|
|
106
|
+
description?: string;
|
|
107
|
+
tags?: string[];
|
|
108
|
+
errors?: string[];
|
|
109
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CoreServices, CoreUserSession } from './core.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a core API function that performs an operation using core services and a user session.
|
|
4
|
+
*
|
|
5
|
+
* @template In - The input type.
|
|
6
|
+
* @template Out - The output type.
|
|
7
|
+
* @template Services - The services type, defaults to `CoreServices`.
|
|
8
|
+
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
9
|
+
*/
|
|
10
|
+
export type CoreAPIFunction<In, Out, Services = CoreServices, Session = CoreUserSession> = (services: Services, data: In, session: Session) => Promise<Out>;
|
|
11
|
+
/**
|
|
12
|
+
* Represents a core API function that can be used without a session.
|
|
13
|
+
*
|
|
14
|
+
* @template In - The input type.
|
|
15
|
+
* @template Out - The output type.
|
|
16
|
+
* @template Services - The services type, defaults to `CoreServices`.
|
|
17
|
+
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
18
|
+
*/
|
|
19
|
+
export type CoreAPIFunctionSessionless<In, Out, Services = CoreServices, Session = CoreUserSession> = (services: Services, data: In, session?: Session) => Promise<Out>;
|
|
20
|
+
/**
|
|
21
|
+
* Represents a function that checks permissions for a given API operation.
|
|
22
|
+
*
|
|
23
|
+
* @template In - The input type.
|
|
24
|
+
* @template Services - The services type, defaults to `CoreServices`.
|
|
25
|
+
* @template Session - The session type, defaults to `CoreUserSession`.
|
|
26
|
+
*/
|
|
27
|
+
export type CoreAPIPermission<In = any, Services = CoreServices, Session = CoreUserSession> = (services: Services, data: In, session?: Session) => Promise<boolean>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// TODO: SessionServices probably needs it's own type
|
|
2
|
+
// but is an issue for the future and will be tackled
|
|
3
|
+
// with dependency injection
|
|
4
|
+
export const closeSessionServices = async (logger, sessionServices) => {
|
|
5
|
+
await Promise.all(Object.values(sessionServices).map(async (service) => {
|
|
6
|
+
if (service?.close) {
|
|
7
|
+
try {
|
|
8
|
+
await service.close();
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
logger.error(e);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pikku/core",
|
|
3
|
+
"version": "0.6.9",
|
|
4
|
+
"author": "yasser.fadl@gmail.com",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"tsc": "tsc",
|
|
11
|
+
"build:esm": "tsc -b",
|
|
12
|
+
"build": "yarn build:esm",
|
|
13
|
+
"ncu": "ncu",
|
|
14
|
+
"release": "npm run build && npm test",
|
|
15
|
+
"test": "bash run-tests.sh",
|
|
16
|
+
"test:watch": "bash run-tests.sh --watch",
|
|
17
|
+
"test:coverage": "bash run-tests.sh --coverage"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/index.js",
|
|
21
|
+
"./channel": "./dist/channel/index.js",
|
|
22
|
+
"./channel/local": "./dist/channel/local/index.js",
|
|
23
|
+
"./channel/serverless": "./dist/channel/serverless/index.js",
|
|
24
|
+
"./http": "./dist/http/index.js",
|
|
25
|
+
"./http/pikku-http-session-service": "./dist/http/pikku-http-session-service.js",
|
|
26
|
+
"./http/pikku-http-abstract-response": "./dist/http/pikku-http-abstract-response.js",
|
|
27
|
+
"./http/pikku-http-abstract-request": "./dist/http/pikku-http-abstract-request.js",
|
|
28
|
+
"./errors": "./dist/errors/index.js",
|
|
29
|
+
"./scheduler": "./dist/scheduler/index.js",
|
|
30
|
+
"./services": "./dist/services/index.js",
|
|
31
|
+
"./schema": "./dist/schema.js",
|
|
32
|
+
"./types": "./dist/types/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@types/cookie": "^0.6.0",
|
|
36
|
+
"@types/uuid": "^10.0.0",
|
|
37
|
+
"cookie": "^1.0.1",
|
|
38
|
+
"path-to-regexp": "^8.2.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.7.8",
|
|
42
|
+
"tsx": "^4.19.2",
|
|
43
|
+
"typescript": "^5.6"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
}
|
|
48
|
+
}
|