@aetheros/kernel 0.0.0-stage → 0.1.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 +21 -0
- package/package.json +24 -4
- package/src/index.js +30 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hariprasath Thanarajah
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aetheros/kernel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/Abigshana/AetherOS.git",
|
|
8
|
+
"directory": "packages/kernel"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"check": "node --check src/index.js",
|
|
16
|
+
"test": "node --test test/*.test.mjs"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"dist",
|
|
21
|
+
"bin",
|
|
22
|
+
"templates",
|
|
23
|
+
"kits",
|
|
24
|
+
"README.md"
|
|
25
|
+
]
|
|
26
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {randomUUID,createHash,timingSafeEqual} from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
const assertId=(value,label)=>{if(typeof value!=='string'||!value.trim())throw new Error(label+' is required');return value;};
|
|
4
|
+
const copy=value=>structuredClone(value);
|
|
5
|
+
const hash=value=>createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
6
|
+
|
|
7
|
+
/** In-memory enterprise kernel reference implementation. No persistent security claims. */
|
|
8
|
+
export class EnterpriseKernel {
|
|
9
|
+
#tenants=new Map();#users=new Map();#members=new Map();#connectors=new Map();#documents=new Map();#workflows=new Map();#approvals=new Map();#events=[];#previous='GENESIS';#budget=new Map();
|
|
10
|
+
constructor({clock=()=>new Date().toISOString(),ids=randomUUID}={}){this.clock=clock;this.ids=ids;}
|
|
11
|
+
#audit(tenantId,actor,action,metadata={}){const event={id:this.ids(),at:this.clock(),tenantId,actor,action,metadata:copy(metadata),previousHash:this.#previous};event.hash=hash(event);this.#previous=event.hash;this.#events.push(event);return copy(event);}
|
|
12
|
+
createTenant({id,name,ownerId}){assertId(id,'tenant id');assertId(name,'tenant name');assertId(ownerId,'owner id');if(this.#tenants.has(id))throw new Error('Tenant already exists');const tenant={id,name};this.#tenants.set(id,tenant);this.#users.set(ownerId,{id:ownerId});this.#members.set(id,new Map([[ownerId,'owner']]));this.#audit(id,ownerId,'tenant.created');return copy(tenant);}
|
|
13
|
+
#role(tenantId,actor){assertId(tenantId,'tenant');assertId(actor,'actor');if(!this.#tenants.has(tenantId))throw new Error('Unknown tenant');const role=this.#members.get(tenantId).get(actor);if(!role)throw new Error('Access denied');return role;}
|
|
14
|
+
authorize(tenantId,actor,permission){const role=this.#role(tenantId,actor);const matrix={owner:['*'],admin:['members','read','write','connectors','workflow','approve','audit','budget'],member:['read','write','workflow'],viewer:['read']};if(!matrix[role]?.includes('*')&&!matrix[role]?.includes(permission))throw new Error('Permission denied');return true;}
|
|
15
|
+
addMember(tenantId,actor,userId,role){this.authorize(tenantId,actor,'members');assertId(userId,'user');if(!['admin','member','viewer'].includes(role))throw new Error('Invalid role');this.#users.set(userId,{id:userId});this.#members.get(tenantId).set(userId,role);this.#audit(tenantId,actor,'member.assigned',{userId,role});}
|
|
16
|
+
setBudget(tenantId,actor,limit){this.authorize(tenantId,actor,'budget');if(!Number.isSafeInteger(limit)||limit<0)throw new Error('Invalid budget');this.#budget.set(tenantId,{limit,used:0});this.#audit(tenantId,actor,'budget.configured',{limit});}
|
|
17
|
+
charge(tenantId,actor,units){this.authorize(tenantId,actor,'workflow');if(!Number.isSafeInteger(units)||units<0)throw new Error('Invalid usage');const b=this.#budget.get(tenantId);if(!b)throw new Error('Budget must be configured');if(b.used+units>b.limit)throw new Error('Budget exceeded');b.used+=units;this.#audit(tenantId,actor,'usage.charged',{units});return copy(b);}
|
|
18
|
+
registerConnector(tenantId,actor,{id,scopes=[]}){this.authorize(tenantId,actor,'connectors');assertId(id,'connector id');if(!Array.isArray(scopes)||scopes.some(x=>!['read','write'].includes(x)))throw new Error('Invalid connector scopes');const key=tenantId+':'+id;if(this.#connectors.has(key))throw new Error('Connector already exists');const item={id,scopes:[...new Set(scopes)],enabled:false};this.#connectors.set(key,item);this.#audit(tenantId,actor,'connector.registered',{id,scopes:item.scopes});return copy(item);}
|
|
19
|
+
enableConnector(tenantId,actor,id){this.authorize(tenantId,actor,'connectors');const item=this.#connectors.get(tenantId+':'+id);if(!item)throw new Error('Unknown connector');item.enabled=true;this.#audit(tenantId,actor,'connector.enabled',{id});}
|
|
20
|
+
assertConnectorScope(tenantId,actor,id,scope){this.authorize(tenantId,actor,scope);const item=this.#connectors.get(tenantId+':'+id);if(!item?.enabled||!item.scopes.includes(scope))throw new Error('Connector scope denied');return true;}
|
|
21
|
+
addDocument(tenantId,actor,{id,text,readers=[]}){this.authorize(tenantId,actor,'write');assertId(id,'document id');if(typeof text!=='string'||!Array.isArray(readers)||readers.some(x=>typeof x!=='string'))throw new Error('Invalid document');const key=tenantId+':'+id;if(this.#documents.has(key))throw new Error('Document already exists');this.#documents.set(key,{id,text,readers:[...new Set([...readers,actor])]});this.#audit(tenantId,actor,'document.created',{id});}
|
|
22
|
+
searchDocuments(tenantId,actor,query){this.authorize(tenantId,actor,'read');assertId(query,'query');return [...this.#documents.entries()].filter(([key,doc])=>key.startsWith(tenantId+':')&&doc.readers.includes(actor)&&doc.text.toLowerCase().includes(query.toLowerCase())).map(([,doc])=>copy({id:doc.id,text:doc.text}));}
|
|
23
|
+
requestApproval(tenantId,actor,{action,reason}){this.authorize(tenantId,actor,'workflow');assertId(action,'action');assertId(reason,'reason');const item={id:this.ids(),tenantId,requester:actor,action,reason,status:'pending'};this.#approvals.set(item.id,item);this.#audit(tenantId,actor,'approval.requested',{id:item.id,action});return copy(item);}
|
|
24
|
+
decideApproval(tenantId,actor,id,approved){this.authorize(tenantId,actor,'approve');const item=this.#approvals.get(id);if(!item||item.tenantId!==tenantId)throw new Error('Unknown approval');if(item.status!=='pending')throw new Error('Approval already decided');if(item.requester===actor)throw new Error('Separation of duties required');if(typeof approved!=='boolean')throw new Error('Invalid decision');item.status=approved?'approved':'rejected';item.decidedBy=actor;this.#audit(tenantId,actor,'approval.decided',{id,status:item.status});return copy(item);}
|
|
25
|
+
createWorkflow(tenantId,actor,{id,steps,approvalId}){this.authorize(tenantId,actor,'workflow');assertId(id,'workflow id');if(!Array.isArray(steps)||!steps.length||steps.some(x=>typeof x!=='string'||!x.trim()))throw new Error('Invalid workflow steps');const key=tenantId+':'+id;if(this.#workflows.has(key))throw new Error('Workflow already exists');const item={id,tenantId,steps:[...steps],approvalId:approvalId??null,status:'pending',completed:0};this.#workflows.set(key,item);this.#audit(tenantId,actor,'workflow.created',{id});return copy(item);}
|
|
26
|
+
async runWorkflow(tenantId,actor,id,executor){this.authorize(tenantId,actor,'workflow');const item=this.#workflows.get(tenantId+':'+id);if(!item)throw new Error('Unknown workflow');if(item.status!=='pending')throw new Error('Workflow cannot be rerun');if(typeof executor!=='function')throw new Error('Executor required');if(item.approvalId){const approval=this.#approvals.get(item.approvalId);if(!approval||approval.tenantId!==tenantId||approval.status!=='approved')throw new Error('Workflow approval required');}
|
|
27
|
+
item.status='running';this.#audit(tenantId,actor,'workflow.started',{id});try{for(const step of item.steps){await executor(step);item.completed++;this.#audit(tenantId,actor,'workflow.step.completed',{id,stepIndex:item.completed-1});}item.status='completed';}catch{item.status='failed';this.#audit(tenantId,actor,'workflow.failed',{id,completed:item.completed});throw new Error('Workflow execution failed');}this.#audit(tenantId,actor,'workflow.completed',{id});return copy(item);}
|
|
28
|
+
getAudit(tenantId,actor){this.authorize(tenantId,actor,'audit');return this.#events.filter(e=>e.tenantId===tenantId).map(copy);}
|
|
29
|
+
verifyAudit(){let previous='GENESIS';for(const event of this.#events){const {hash:stored,...payload}=event;if(event.previousHash!==previous||hash(payload)!==stored)return false;previous=stored;}return true;}
|
|
30
|
+
}
|
package/README.md
DELETED