@scarletgeek/web-kernel 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 playwithcode-fun
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.
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
3
+ };
File without changes
@@ -0,0 +1,9 @@
1
+ import { IEventMap, IEventHandler, IUnsubscribeEvent } from "./types/IKernel";
2
+ declare class KernelEvents extends EventTarget {
3
+ on<K extends keyof IEventMap>(type: K, handler: IEventHandler<IEventMap[K]>, options?: AddEventListenerOptions): IUnsubscribeEvent;
4
+ once<K extends keyof IEventMap>(type: K, handler: IEventHandler<IEventMap[K]>): void;
5
+ off<K extends keyof IEventMap>(type: K, handler: IEventHandler<IEventMap[K]>): void;
6
+ emit<K extends keyof IEventMap>(type: K, detail?: IEventMap[K]): boolean;
7
+ destroy(): void;
8
+ }
9
+ export default KernelEvents;
@@ -0,0 +1,14 @@
1
+ import { ProgramState } from "./types/IKernel";
2
+ declare class ProgramHandler {
3
+ private programs;
4
+ private programState;
5
+ constructor();
6
+ isProgramExists(key: string): boolean;
7
+ addProgram(key: string, program: any): void;
8
+ startProgram(key: string, args?: any): void;
9
+ endProgram(key: string): void;
10
+ getProgramsByState(programState: ProgramState): any;
11
+ getAllProgramStatus(): any;
12
+ getProgramStatus(key: string): any;
13
+ }
14
+ export default ProgramHandler;
@@ -0,0 +1,7 @@
1
+ import { IKernelRequest } from "./types/IKernel";
2
+ declare class RequestHandler {
3
+ requests: IKernelRequest;
4
+ addRequest(key: string, RequestClass: any): void;
5
+ startRequest(key: string): void;
6
+ }
7
+ export default RequestHandler;
@@ -0,0 +1,4 @@
1
+ import Kernel from "./kernel";
2
+ import Request from "./request/Request";
3
+ import { LoggerLevel } from "./utils";
4
+ export { Kernel, LoggerLevel, Request };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var t,e,r,s={};function o(t,e,s=void 0){t==r.ERROR?console.error(r.ERROR,e,s):t==r.WARNING?console.warn(r.WARNING,e,s):console.log(r.LOG,e,s)}s.d=(t,e)=>{for(var r in e)s.o(e,r)&&!s.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),function(t){t.CREATED="CREATED",t.BOOTING="BOOTING",t.BOOTED="BOOTED"}(t||(t={})),function(t){t.IDLE="IDLE",t.RUNNING="RUNNING",t.STOPPED="STOPPED",t.ERROR="ERROR"}(e||(e={})),function(t){t.LOG="LOG :: ",t.ERROR="ERROR :: ",t.WARNING="WARNING :: "}(r||(r={}));const n=class{constructor(){this.programs=new Map,this.programState=new Map}isProgramExists(t){return this.programs.has(t)&&this.programState.has(t)}addProgram(t,s){this.programs.has(t)?o(r.WARNING,`Program ${t} already exists.`):(this.programs.set(t,new s),this.programState.set(t,e.IDLE))}startProgram(t,s=null){const n=this.programs.get(t);if(!n)return void o(r.WARNING,`Program ${t} not found.`);if(this.programState.get(t)!==e.RUNNING)if("function"==typeof n.onStart)try{n.onStart(s),this.programState.set(t,e.RUNNING)}catch(s){this.programState.set(t,e.ERROR),o(r.ERROR,`Error occured in program ${t} - `,s)}else o(r.ERROR,`onStart method is not defined in the program ${t}.`);else o(r.WARNING,`Program ${t} is already running.`)}endProgram(t){const s=this.programs.get(t);if(s)if(this.programState.get(t)===e.RUNNING)if("function"==typeof s.onDestroy)try{s.onDestroy(),this.programState.set(t,e.STOPPED)}catch(s){this.programState.set(t,e.ERROR),o(r.ERROR,`Error occured in program ${t} - `,s)}else o(r.ERROR,`onDestroy method is not defined in the program ${t}.`);else o(r.WARNING,`Program ${t} is not running.`);else o(r.WARNING,`Program ${t} not found.`)}getProgramsByState(t){const e={};for(const[r,s]of this.programState)s===t&&(e[r]=s);return e}getAllProgramStatus(){const t={};for(const[e,r]of this.programState)t[e]=r;return t}getProgramStatus(t){return this.programs.get(t)}};class a extends EventTarget{on(t,e,r){return this.addEventListener(t,e,r),()=>this.off(t,e)}once(t,e){this.addEventListener(t,e,{once:!0})}off(t,e){this.removeEventListener(t,e)}emit(t,e={}){return this.dispatchEvent(new CustomEvent(t,{detail:e}))}destroy(){}}const i=a;var h;function c(t){return fetch(t.url,{method:t.method||h.GET,headers:t.headers,body:t.body??null,signal:t.signal})}!function(t){t.GET="GET",t.POST="POST",t.PUT="PUT",t.PATCH="PATCH",t.DELETE="DELETE"}(h||(h={}));const d=class{async transform(t){const e=t.headers.get("content-type")||"";return e.includes("application/json")?await t.json():e.includes("text/")?await t.text():await t.blob()}};class g{constructor(t,e){if(new.target===g)throw new Error("Request is abstract");this.adapter=t??c,this.transformer=e??new d}get method(){return h.GET}get headers(){}get payload(){return null}get signal(){}async send(){try{this.onProcessing();const t=await this.adapter({url:this.url,method:this.method,headers:this.headers,body:this.payload,signal:this.signal});if(!t.ok)throw t;const e=await this.transformer.transform(t);return this.onSuccess(e),e}catch(t){throw this.onError(t),t}}onProcessing(){}onSuccess(t){}onError(t){}}const l=g;const u=class{constructor(){this.requests={}}addRequest(t,e){const r=new e(c);if(!(r instanceof l))throw new Error(`Invalid request "${t}". It must extend kernel Request class.`);this.requests[t]=r}startRequest(t){this.requests[t]||o(r.WARNING,"Request not found."),"function"!=typeof this.requests[t].send&&o(r.ERROR,"Send method not available for the request."),this.requests[t].send()}};const R=class{constructor(){this.programHandler=new n,this.eventHandler=new i,this.requestHandler=new u,this.state=t.CREATED,this.bootHandlers=[]}onBoot(e){this.state!=t.BOOTED?this.bootHandlers.push(e):e(this)}boot(){if(this.state===t.CREATED){this.state=t.BOOTING;for(const t of this.bootHandlers)t(this);this.bootHandlers.length=0,this.state=t.BOOTED}}registerPrograms(t){Object.keys(t).length&&Object.keys(t).forEach(e=>{this.programHandler.addProgram(e,t[e])})}start(t,e=null){this.programHandler.startProgram(t,e)}destroy(t){this.programHandler.endProgram(t)}emit(t,e={}){this.eventHandler.emit(t,e)}on(t,e,r){return this.eventHandler.on(t,e,r)}once(t,e){this.eventHandler.once(t,e)}registerRequests(t){Object.keys(t).length&&Object.keys(t).forEach(e=>{this.requestHandler.addRequest(e,t[e])})}send(t){this.requestHandler.startRequest(t)}status(){const t={};t.programs=this.programHandler.getAllProgramStatus(),console.log(t)}};export{R as Kernel,r as LoggerLevel,l as Request};
@@ -0,0 +1,24 @@
1
+ import ProgramHandler from "./ProgramHandler";
2
+ import { IKernelProgram, IEventMap, IEventHandler, IUnsubscribeEvent, IKernelRequest, KernelState } from "./types/IKernel";
3
+ import KernelEvents from "./KernelEvents";
4
+ import RequestHandler from "./RequestHandler";
5
+ declare class Kernel {
6
+ programHandler: ProgramHandler;
7
+ eventHandler: KernelEvents;
8
+ requestHandler: RequestHandler;
9
+ state: KernelState;
10
+ bootHandlers: any[];
11
+ constructor();
12
+ onBoot(callback: any): void;
13
+ boot(): void;
14
+ registerPrograms(programs: IKernelProgram): void;
15
+ start(programName: string, args?: any): void;
16
+ destroy(programName: string): void;
17
+ emit<K extends keyof IEventMap>(type: K, detail?: IEventMap[K]): void;
18
+ on<K extends keyof IEventMap>(type: K, handler: IEventHandler<IEventMap[K]>, options?: AddEventListenerOptions): IUnsubscribeEvent;
19
+ once<K extends keyof IEventMap>(type: K, handler: IEventHandler<IEventMap[K]>): void;
20
+ registerRequests(requests: IKernelRequest): void;
21
+ send(key: string): void;
22
+ status(): void;
23
+ }
24
+ export default Kernel;
@@ -0,0 +1,3 @@
1
+ {
2
+ "/index.js": "/index.js"
3
+ }
@@ -0,0 +1,5 @@
1
+ import { ResponseTransformer } from "./interfaces";
2
+ declare class DataTransformer<T = Response> implements ResponseTransformer<T> {
3
+ transform(response: Response): Promise<T>;
4
+ }
5
+ export default DataTransformer;
@@ -0,0 +1,16 @@
1
+ import { RequestAdapter, RequestMethods, ResponseTransformer } from "./interfaces";
2
+ declare abstract class Request<T = Response> {
3
+ protected adapter: RequestAdapter;
4
+ protected transformer: ResponseTransformer<T>;
5
+ constructor(adapter?: RequestAdapter, transformer?: ResponseTransformer<T>);
6
+ protected abstract get url(): string;
7
+ protected get method(): RequestMethods;
8
+ protected get headers(): HeadersInit | undefined;
9
+ protected get payload(): BodyInit | null;
10
+ protected get signal(): AbortSignal | undefined;
11
+ send(): Promise<T>;
12
+ protected onProcessing(): void;
13
+ protected onSuccess(_response: T): void;
14
+ protected onError(_error: any): void;
15
+ }
16
+ export default Request;
@@ -0,0 +1,2 @@
1
+ import { FetchRequestConfig } from "./interfaces";
2
+ export declare function fetchAdapter(config: FetchRequestConfig): Promise<Response>;
@@ -0,0 +1,18 @@
1
+ export interface FetchRequestConfig {
2
+ url: string;
3
+ method?: string;
4
+ headers?: HeadersInit;
5
+ body?: BodyInit | null;
6
+ signal?: AbortSignal;
7
+ }
8
+ export type RequestAdapter = (config: FetchRequestConfig) => Promise<Response>;
9
+ export interface ResponseTransformer<T = any> {
10
+ transform(response: Response): Promise<T>;
11
+ }
12
+ export declare enum RequestMethods {
13
+ GET = "GET",
14
+ POST = "POST",
15
+ PUT = "PUT",
16
+ PATCH = "PATCH",
17
+ DELETE = "DELETE"
18
+ }
@@ -0,0 +1,27 @@
1
+ export interface IKernelProgram {
2
+ [key: string]: any;
3
+ }
4
+ export type IEventMap = Record<string, any>;
5
+ export type IEventHandler<T = unknown> = (event: Event) => void;
6
+ export type IUnsubscribeEvent = () => void;
7
+ export interface IKernelRequest {
8
+ [key: string]: any;
9
+ }
10
+ export declare enum KernelState {
11
+ CREATED = "CREATED",
12
+ BOOTING = "BOOTING",
13
+ BOOTED = "BOOTED"
14
+ }
15
+ export declare enum ProgramState {
16
+ IDLE = "IDLE",
17
+ RUNNING = "RUNNING",
18
+ STOPPED = "STOPPED",
19
+ ERROR = "ERROR"
20
+ }
21
+ export interface IKernelProgramInstance {
22
+ onStart: () => void;
23
+ onDestroy: () => void;
24
+ }
25
+ export interface IKernelProgramConstructor {
26
+ new (): IKernelProgramInstance;
27
+ }
@@ -0,0 +1,6 @@
1
+ export declare enum LoggerLevel {
2
+ LOG = "LOG :: ",
3
+ ERROR = "ERROR :: ",
4
+ WARNING = "WARNING :: "
5
+ }
6
+ export declare function logger(level: LoggerLevel, message: any, data?: any): void;