Comparing version 2.0.7 to 2.0.8
/// <reference types="koa-bodyparser" /> | ||
/// <reference types="node" /> | ||
import type Router from 'koa-router'; | ||
@@ -17,2 +18,6 @@ import Koa from 'koa'; | ||
middlewares?: (app: Koa) => void; | ||
/** | ||
* Handle signal received for graceful shutdown. | ||
*/ | ||
onSignalReceived?: (signal: NodeJS.Signals, app: Koa) => Promise<void>; | ||
} | ||
@@ -19,0 +24,0 @@ export interface KobpModule { |
@@ -10,3 +10,2 @@ "use strict"; | ||
const allOpts = options; | ||
console.log('registering', allOpts.length); | ||
return { | ||
@@ -28,3 +27,2 @@ onInit: async () => { | ||
middlewares: (app) => { | ||
console.log('middlewares!!!', app); | ||
for (const opt of allOpts) { | ||
@@ -35,2 +33,9 @@ if (opt.middlewares) { | ||
} | ||
}, | ||
onSignalReceived: async (signal, app) => { | ||
for (const opt of allOpts) { | ||
if (opt.onSignalReceived) { | ||
await opt.onSignalReceived(signal, app); | ||
} | ||
} | ||
} | ||
@@ -81,2 +86,14 @@ }; | ||
opts.onAppCreated && opts.onAppCreated(koa); | ||
if (opts.onSignalReceived) { | ||
// register onSignalReceived handler. | ||
process.on('SIGTERM', (signal) => { | ||
opts.onSignalReceived(signal, koa) | ||
.then(() => { | ||
process.exit(0); | ||
}) | ||
.catch((e) => { | ||
process.exit(1); | ||
}); | ||
}); | ||
} | ||
} | ||
@@ -83,0 +100,0 @@ } |
@@ -22,3 +22,3 @@ "use strict"; | ||
onInit: async () => { | ||
console.log('Bootstrap Module initialized.'); | ||
console.log(`Kobp initialized.`); | ||
}, | ||
@@ -33,2 +33,6 @@ middlewares: (app) => { | ||
}, | ||
onSignalReceived: async (signal, app) => { | ||
// gracefully shutting this down. | ||
console.log('Kobp Shutting Down'); | ||
}, | ||
}; | ||
@@ -35,0 +39,0 @@ } |
@@ -15,6 +15,6 @@ import { KobpServiceContext } from '../context'; | ||
export declare class KobpRequestRoom { | ||
protected readonly data: Map<string, any>; | ||
private static counter; | ||
readonly id: number; | ||
constructor(data: Map<string, any>); | ||
private data; | ||
constructor(context: KobpServiceContext); | ||
get<T>(key: string): T; | ||
@@ -28,4 +28,2 @@ set<T>(key: string, data: T): void; | ||
export declare class RequestRoomProvider { | ||
private storage; | ||
roomRegistries: Record<string, new (context: any) => any>; | ||
static readonly shared: RequestRoomProvider; | ||
@@ -37,4 +35,3 @@ private constructor(); | ||
currentInstance<O>(key: string | RequestContextCreator<O>): O | undefined; | ||
createContext(_ctx: KobpServiceContext): KobpRequestRoom; | ||
} | ||
export {}; |
@@ -7,2 +7,7 @@ "use strict"; | ||
const isDebug = env_1.env.b('KOBP_DEBUG', false); | ||
const _storage = new async_hooks_1.AsyncLocalStorage(); | ||
const _roomRegistries = {}; | ||
if (isDebug) { | ||
console.log('RCTX initialized, >>>>>>>>>>>>>>>>>>> this should appear only ONCE. >>>>>>>>>>>>>>>>>>>'); | ||
} | ||
/** | ||
@@ -24,3 +29,3 @@ * Marking any class to be available when resource is being created. | ||
} | ||
RequestRoomProvider.shared.roomRegistries[keyName] = constructor; | ||
_roomRegistries[keyName] = constructor; | ||
}; | ||
@@ -30,13 +35,24 @@ } | ||
class KobpRequestRoom { | ||
constructor(data) { | ||
this.data = data; | ||
constructor(context) { | ||
this.id = KobpRequestRoom.counter++; | ||
this.data = new Map(); | ||
for (const regKey in _roomRegistries) { | ||
const cnstr = _roomRegistries[regKey]; | ||
this.set(regKey, new cnstr(context)); | ||
} | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${data.size}`); | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${this.data.size}`); | ||
} | ||
} | ||
get(key) { | ||
return this.data[key]; | ||
const v = this.data[key]; | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} get(${key}) ${v}.`); | ||
} | ||
return v; | ||
} | ||
set(key, data) { | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} has been set with ${key}.`); | ||
} | ||
this.data[key] = data; | ||
@@ -46,3 +62,3 @@ } | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${this.data.size} has been freed.`); | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${[...this.data.keys()].join(', ')} has been freed.`); | ||
} | ||
@@ -59,4 +75,2 @@ this.data.clear(); | ||
constructor() { | ||
this.storage = new async_hooks_1.AsyncLocalStorage(); | ||
this.roomRegistries = {}; | ||
if (isDebug) { | ||
@@ -69,15 +83,13 @@ console.log(`RCTX RoomProvider. This should be called only once. If you see this message multiple times. Then something is wrong..`); | ||
createAsync(_ctx, next) { | ||
const rctx = this.createContext(_ctx); | ||
const rctx = new KobpRequestRoom(_ctx); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createAsync. Middleware registered. If you see this message multiple times. Then something is wrong..`); | ||
console.log(`RCTX RoomProvider.createAsync. RoomContext created.`); | ||
} | ||
return this.storage.run(rctx, (...args) => { | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider RoomContext injected.`); | ||
} | ||
return next(...args); | ||
}).finally(rctx.free.bind(rctx)); | ||
return _storage.run(rctx, next).finally(rctx.free.bind(rctx)); | ||
} | ||
current() { | ||
return this.storage.getStore(); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.current().`); | ||
} | ||
return _storage.getStore(); | ||
} | ||
@@ -97,16 +109,2 @@ static instanceOf(keyOrCnstr) { | ||
} | ||
createContext(_ctx) { | ||
const _room = new KobpRequestRoom(new Map()); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context is being initialized.`); | ||
} | ||
for (const regKey in this.roomRegistries) { | ||
const cnstr = this.roomRegistries[regKey]; | ||
_room.set(regKey, new cnstr(_ctx)); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context populating: ${regKey}, new instance of ${cnstr}`); | ||
} | ||
} | ||
return _room; | ||
} | ||
} | ||
@@ -113,0 +111,0 @@ exports.RequestRoomProvider = RequestRoomProvider; |
/// <reference types="koa-bodyparser" /> | ||
/// <reference types="node" /> | ||
import type Router from 'koa-router'; | ||
@@ -17,2 +18,6 @@ import Koa from 'koa'; | ||
middlewares?: (app: Koa) => void; | ||
/** | ||
* Handle signal received for graceful shutdown. | ||
*/ | ||
onSignalReceived?: (signal: NodeJS.Signals, app: Koa) => Promise<void>; | ||
} | ||
@@ -19,0 +24,0 @@ export interface KobpModule { |
import Koa from 'koa'; | ||
const _compileCustomization = (options) => { | ||
const allOpts = options; | ||
console.log('registering', allOpts.length); | ||
return { | ||
@@ -21,3 +20,2 @@ onInit: async () => { | ||
middlewares: (app) => { | ||
console.log('middlewares!!!', app); | ||
for (const opt of allOpts) { | ||
@@ -28,2 +26,9 @@ if (opt.middlewares) { | ||
} | ||
}, | ||
onSignalReceived: async (signal, app) => { | ||
for (const opt of allOpts) { | ||
if (opt.onSignalReceived) { | ||
await opt.onSignalReceived(signal, app); | ||
} | ||
} | ||
} | ||
@@ -74,4 +79,16 @@ }; | ||
opts.onAppCreated && opts.onAppCreated(koa); | ||
if (opts.onSignalReceived) { | ||
// register onSignalReceived handler. | ||
process.on('SIGTERM', (signal) => { | ||
opts.onSignalReceived(signal, koa) | ||
.then(() => { | ||
process.exit(0); | ||
}) | ||
.catch((e) => { | ||
process.exit(1); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=bootstrap.js.map |
@@ -17,3 +17,3 @@ import bodyParser from 'koa-bodyparser'; | ||
onInit: async () => { | ||
console.log('Bootstrap Module initialized.'); | ||
console.log(`Kobp initialized.`); | ||
}, | ||
@@ -28,2 +28,6 @@ middlewares: (app) => { | ||
}, | ||
onSignalReceived: async (signal, app) => { | ||
// gracefully shutting this down. | ||
console.log('Kobp Shutting Down'); | ||
}, | ||
}; | ||
@@ -30,0 +34,0 @@ } |
@@ -15,6 +15,6 @@ import { KobpServiceContext } from '../context'; | ||
export declare class KobpRequestRoom { | ||
protected readonly data: Map<string, any>; | ||
private static counter; | ||
readonly id: number; | ||
constructor(data: Map<string, any>); | ||
private data; | ||
constructor(context: KobpServiceContext); | ||
get<T>(key: string): T; | ||
@@ -28,4 +28,2 @@ set<T>(key: string, data: T): void; | ||
export declare class RequestRoomProvider { | ||
private storage; | ||
roomRegistries: Record<string, new (context: any) => any>; | ||
static readonly shared: RequestRoomProvider; | ||
@@ -37,4 +35,3 @@ private constructor(); | ||
currentInstance<O>(key: string | RequestContextCreator<O>): O | undefined; | ||
createContext(_ctx: KobpServiceContext): KobpRequestRoom; | ||
} | ||
export {}; |
import { AsyncLocalStorage } from 'async_hooks'; | ||
import { env } from './env'; | ||
const isDebug = env.b('KOBP_DEBUG', false); | ||
const _storage = new AsyncLocalStorage(); | ||
const _roomRegistries = {}; | ||
if (isDebug) { | ||
console.log('RCTX initialized, >>>>>>>>>>>>>>>>>>> this should appear only ONCE. >>>>>>>>>>>>>>>>>>>'); | ||
} | ||
/** | ||
@@ -20,17 +25,28 @@ * Marking any class to be available when resource is being created. | ||
} | ||
RequestRoomProvider.shared.roomRegistries[keyName] = constructor; | ||
_roomRegistries[keyName] = constructor; | ||
}; | ||
} | ||
export class KobpRequestRoom { | ||
constructor(data) { | ||
this.data = data; | ||
constructor(context) { | ||
this.id = KobpRequestRoom.counter++; | ||
this.data = new Map(); | ||
for (const regKey in _roomRegistries) { | ||
const cnstr = _roomRegistries[regKey]; | ||
this.set(regKey, new cnstr(context)); | ||
} | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${data.size}`); | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${this.data.size}`); | ||
} | ||
} | ||
get(key) { | ||
return this.data[key]; | ||
const v = this.data[key]; | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} get(${key}) ${v}.`); | ||
} | ||
return v; | ||
} | ||
set(key, data) { | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} has been set with ${key}.`); | ||
} | ||
this.data[key] = data; | ||
@@ -40,3 +56,3 @@ } | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${this.data.size} has been freed.`); | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${[...this.data.keys()].join(', ')} has been freed.`); | ||
} | ||
@@ -52,4 +68,2 @@ this.data.clear(); | ||
constructor() { | ||
this.storage = new AsyncLocalStorage(); | ||
this.roomRegistries = {}; | ||
if (isDebug) { | ||
@@ -62,15 +76,13 @@ console.log(`RCTX RoomProvider. This should be called only once. If you see this message multiple times. Then something is wrong..`); | ||
createAsync(_ctx, next) { | ||
const rctx = this.createContext(_ctx); | ||
const rctx = new KobpRequestRoom(_ctx); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createAsync. Middleware registered. If you see this message multiple times. Then something is wrong..`); | ||
console.log(`RCTX RoomProvider.createAsync. RoomContext created.`); | ||
} | ||
return this.storage.run(rctx, (...args) => { | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider RoomContext injected.`); | ||
} | ||
return next(...args); | ||
}).finally(rctx.free.bind(rctx)); | ||
return _storage.run(rctx, next).finally(rctx.free.bind(rctx)); | ||
} | ||
current() { | ||
return this.storage.getStore(); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.current().`); | ||
} | ||
return _storage.getStore(); | ||
} | ||
@@ -90,18 +102,4 @@ static instanceOf(keyOrCnstr) { | ||
} | ||
createContext(_ctx) { | ||
const _room = new KobpRequestRoom(new Map()); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context is being initialized.`); | ||
} | ||
for (const regKey in this.roomRegistries) { | ||
const cnstr = this.roomRegistries[regKey]; | ||
_room.set(regKey, new cnstr(_ctx)); | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context populating: ${regKey}, new instance of ${cnstr}`); | ||
} | ||
} | ||
return _room; | ||
} | ||
} | ||
RequestRoomProvider.shared = new RequestRoomProvider(); | ||
//# sourceMappingURL=RequestContext.js.map |
{ | ||
"name": "kobp", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "Koa Boilerplate with MikroORM", | ||
@@ -5,0 +5,0 @@ "main": "lib/cjs/src/index.js", |
@@ -17,2 +17,6 @@ import type Router from 'koa-router' | ||
middlewares?: (app: Koa) => void | ||
/** | ||
* Handle signal received for graceful shutdown. | ||
*/ | ||
onSignalReceived?: (signal: NodeJS.Signals, app: Koa) => Promise<void> | ||
} | ||
@@ -22,3 +26,2 @@ | ||
const allOpts = options | ||
console.log('registering', allOpts.length) | ||
return { | ||
@@ -40,3 +43,2 @@ onInit: async () => { | ||
middlewares: (app: Koa) => { | ||
console.log('middlewares!!!', app) | ||
for(const opt of allOpts) { | ||
@@ -47,2 +49,9 @@ if (opt.middlewares) { | ||
} | ||
}, | ||
onSignalReceived: async (signal: NodeJS.Signals, app: Koa): Promise<void> => { | ||
for(const opt of allOpts) { | ||
if (opt.onSignalReceived) { | ||
await opt.onSignalReceived(signal, app) | ||
} | ||
} | ||
} | ||
@@ -111,3 +120,16 @@ } | ||
opts.onAppCreated && opts.onAppCreated(koa) | ||
if (opts.onSignalReceived) { | ||
// register onSignalReceived handler. | ||
process.on('SIGTERM', (signal) => { | ||
opts.onSignalReceived(signal, koa) | ||
.then(() => { | ||
process.exit(0) | ||
}) | ||
.catch((e) => { | ||
process.exit(1) | ||
}) | ||
}) | ||
} | ||
} | ||
} |
@@ -28,3 +28,3 @@ import type { KobpCustomization } from '../bootstrap' | ||
onInit: async () => { | ||
console.log('Bootstrap Module initialized.') | ||
console.log(`Kobp initialized.`) | ||
}, | ||
@@ -39,4 +39,8 @@ middlewares: (app) => { | ||
}, | ||
onSignalReceived: async (signal, app) => { | ||
// gracefully shutting this down. | ||
console.log('Kobp Shutting Down') | ||
}, | ||
} | ||
} | ||
} |
@@ -9,2 +9,10 @@ import { AsyncLocalStorage } from 'async_hooks' | ||
const _storage = new AsyncLocalStorage<KobpRequestRoom>() | ||
const _roomRegistries: Record<string, new (context: any) => any> = {} | ||
if (isDebug) { | ||
console.log('RCTX initialized, >>>>>>>>>>>>>>>>>>> this should appear only ONCE. >>>>>>>>>>>>>>>>>>>') | ||
} | ||
/** | ||
@@ -26,3 +34,3 @@ * Marking any class to be available when resource is being created. | ||
} | ||
RequestRoomProvider.shared.roomRegistries[keyName] = constructor | ||
_roomRegistries[keyName] = constructor | ||
} | ||
@@ -37,5 +45,11 @@ } | ||
constructor(protected readonly data: Map<string, any>) { | ||
private data: Map<string, any> = new Map() | ||
public constructor(context: KobpServiceContext) { | ||
for(const regKey in _roomRegistries) { | ||
const cnstr = _roomRegistries[regKey] | ||
this.set(regKey, new cnstr(context)) | ||
} | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${data.size}`) | ||
console.log(`RCTX KobpRequestRoom #${this.id} created with data of size: ${this.data.size}`) | ||
} | ||
@@ -45,6 +59,13 @@ } | ||
get<T>(key: string): T { | ||
return this.data[key] | ||
const v = this.data[key] | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} get(${key}) ${v}.`) | ||
} | ||
return v | ||
} | ||
set<T>(key: string, data: T) { | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} has been set with ${key}.`) | ||
} | ||
this.data[key] = data | ||
@@ -55,3 +76,3 @@ } | ||
if (isDebug) { | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${this.data.size} has been freed.`) | ||
console.log(`RCTX KobpRequestRoom #${this.id} of size ${[...this.data.keys()].join(', ')} has been freed.`) | ||
} | ||
@@ -67,6 +88,2 @@ this.data.clear() | ||
private storage = new AsyncLocalStorage<KobpRequestRoom>() | ||
public roomRegistries: Record<string, new (context: any) => any> = {} | ||
public static readonly shared = new RequestRoomProvider() | ||
@@ -83,16 +100,14 @@ | ||
public createAsync(_ctx: KobpServiceContext, next: (...args: any[]) => Promise<KobpRequestRoom>): Promise<KobpRequestRoom> { | ||
const rctx = this.createContext(_ctx) | ||
const rctx = new KobpRequestRoom(_ctx) | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createAsync. Middleware registered. If you see this message multiple times. Then something is wrong..`) | ||
console.log(`RCTX RoomProvider.createAsync. RoomContext created.`) | ||
} | ||
return this.storage.run(rctx, (...args) => { | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider RoomContext injected.`) | ||
} | ||
return next(...args) | ||
}).finally(rctx.free.bind(rctx)) | ||
return _storage.run(rctx, next).finally(rctx.free.bind(rctx)) | ||
} | ||
public current(): KobpRequestRoom | undefined { | ||
return this.storage.getStore() | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.current().`) | ||
} | ||
return _storage.getStore() | ||
} | ||
@@ -114,17 +129,2 @@ | ||
} | ||
public createContext(_ctx: KobpServiceContext): KobpRequestRoom { | ||
const _room = new KobpRequestRoom(new Map()) | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context is being initialized.`) | ||
} | ||
for(const regKey in this.roomRegistries) { | ||
const cnstr = this.roomRegistries[regKey] | ||
_room.set(regKey, new cnstr(_ctx)) | ||
if (isDebug) { | ||
console.log(`RCTX RoomProvider.createContext. KobpRequestRoom #${_room.id} Context populating: ${regKey}, new instance of ${cnstr}`) | ||
} | ||
} | ||
return _room | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
290876
3234