@bettercorp/service-base
Advanced tools
Comparing version 8.3.32-rc.0 to 8.3.35-ea.0
@@ -37,5 +37,5 @@ const fs = require("fs"); | ||
let indexFile = path.join(libDir, "./index." + client); | ||
let indexIsMain = true; | ||
let indexIsMain = false; | ||
if (!fs.existsSync(indexFile + indexExts[0])) { | ||
indexIsMain = false; | ||
indexIsMain = true; | ||
indexFile = path.join(libDir, "./index"); | ||
@@ -94,8 +94,2 @@ } | ||
pkgg.files = ["lib/**/*"]; | ||
if (indexFile !== false) { | ||
pkgg.files.push("index.js"); | ||
} | ||
if (sharedDir !== false) { | ||
pkgg.files.push("shared/**/*"); | ||
} | ||
pkgg.bin = {}; | ||
@@ -102,0 +96,0 @@ pkgg.bsb_project = undefined; |
@@ -8,9 +8,13 @@ /// <reference types="node" /> | ||
constructor(pluginName: string, cwd: string, pluginCwd: string, log: IPluginLogger); | ||
onBroadcast(callerPluginName: string, pluginName: string, event: string, listener: { | ||
(args: Array<any>): Promise<void>; | ||
}): Promise<void>; | ||
emitBroadcast(callerPluginName: string, pluginName: string, event: string, args: Array<any>): Promise<void>; | ||
onEvent(callerPluginName: string, pluginName: string, event: string, listener: { | ||
(args: Array<any>): Promise<void>; | ||
}): Promise<void>; | ||
emitEvent(callerPluginName: string, pluginName: string, event: string, args: Array<any>): Promise<void>; | ||
onReturnableEvent(callerPluginName: string, pluginName: string, event: string, listener: { | ||
(args: Array<any>): Promise<any>; | ||
}): Promise<void>; | ||
emitEvent(callerPluginName: string, pluginName: string, event: string, args: Array<any>): Promise<void>; | ||
emitEventAndReturn(callerPluginName: string, pluginName: string, event: string, timeoutSeconds: number, args: Array<any>): Promise<any>; | ||
@@ -17,0 +21,0 @@ receiveStream(callerPluginName: string, listener: (error: Error | null, stream: Readable) => Promise<void>, timeoutSeconds?: number): Promise<string>; |
@@ -10,11 +10,17 @@ "use strict"; | ||
} | ||
async onEvent(callerPluginName, pluginName, event, listener) { | ||
async onBroadcast(callerPluginName, pluginName, event, listener) { | ||
throw static_1.ErrorMessages.EventsNotImplementedProperly; | ||
} | ||
async onReturnableEvent(callerPluginName, pluginName, event, listener) { | ||
async emitBroadcast(callerPluginName, pluginName, event, args) { | ||
throw static_1.ErrorMessages.EventsNotImplementedProperly; | ||
} | ||
async onEvent(callerPluginName, pluginName, event, listener) { | ||
throw static_1.ErrorMessages.EventsNotImplementedProperly; | ||
} | ||
async emitEvent(callerPluginName, pluginName, event, args) { | ||
throw static_1.ErrorMessages.EventsNotImplementedProperly; | ||
} | ||
async onReturnableEvent(callerPluginName, pluginName, event, listener) { | ||
throw static_1.ErrorMessages.EventsNotImplementedProperly; | ||
} | ||
async emitEventAndReturn(callerPluginName, pluginName, event, timeoutSeconds, args) { | ||
@@ -21,0 +27,0 @@ throw static_1.ErrorMessages.EventsNotImplementedProperly; |
@@ -16,3 +16,5 @@ /// <reference types="node" /> | ||
] : [event: Method, ...a: Arguments] : [event: Method, noMatchingEvent: never] : Interface[Method] extends (...a: infer Arguments) => infer Return ? Return extends Promise<unknown> ? Return : Promise<Return> : Promise<never>; | ||
export interface IServiceEvents<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents> { | ||
export interface IServiceEvents<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, onBroadcast, emitBroadcast> { | ||
onBroadcast<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onBroadcast>, TA, false>): Promise<void>; | ||
emitBroadcast<TA extends string>(...args: DynamicallyReferencedMethodEmitIEvents<DynamicallyReferencedMethodType<emitBroadcast>, TA>): Promise<void>; | ||
onEvent<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onEvents>, TA, false>): Promise<void>; | ||
@@ -19,0 +21,0 @@ onEventSpecific<TA extends string>(serverId: string, ...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onEvents>, TA, false>): Promise<void>; |
import { IServiceEvents } from "./events"; | ||
export interface IService<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents> extends IServiceEvents<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents> { | ||
export interface IService<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, onBroadcast, emitBroadcast> extends IServiceEvents<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, onBroadcast, emitBroadcast> { | ||
initIndex?: number; | ||
@@ -4,0 +4,0 @@ init?(): Promise<void>; |
@@ -9,3 +9,5 @@ /// <reference types="node" /> | ||
import { PluginConfig } from "./sec.config"; | ||
import broadcast from './events/broadcast'; | ||
export declare class Events extends EventsBase<PluginConfig> { | ||
protected broadcast: broadcast; | ||
protected emit: emit; | ||
@@ -16,2 +18,6 @@ protected ear: emitAndReturn; | ||
dispose(): void; | ||
onBroadcast(callerPluginName: string, pluginName: string, event: string, listener: { | ||
(args: Array<any>): Promise<void>; | ||
}): Promise<void>; | ||
emitBroadcast(callerPluginName: string, pluginName: string, event: string, args: Array<any>): Promise<void>; | ||
onEvent(callerPluginName: string, pluginName: string, event: string, listener: { | ||
@@ -18,0 +24,0 @@ (args: Array<any>): Promise<void>; |
@@ -11,5 +11,7 @@ "use strict"; | ||
const events_1 = require("../../events/events"); | ||
const broadcast_1 = __importDefault(require("./events/broadcast")); | ||
class Events extends events_1.EventsBase { | ||
constructor(pluginName, cwd, pluginCwd, log) { | ||
super(pluginName, cwd, pluginCwd, log); | ||
this.broadcast = new broadcast_1.default(log); | ||
this.emit = new emit_1.default(log); | ||
@@ -20,2 +22,3 @@ this.ear = new emitAndReturn_1.default(log); | ||
dispose() { | ||
this.broadcast.dispose(); | ||
this.emit.dispose(); | ||
@@ -25,2 +28,8 @@ this.ear.dispose(); | ||
} | ||
async onBroadcast(callerPluginName, pluginName, event, listener) { | ||
await this.broadcast.onBroadcast(callerPluginName, pluginName, event, listener); | ||
} | ||
async emitBroadcast(callerPluginName, pluginName, event, args) { | ||
await this.broadcast.emitBroadcast(callerPluginName, pluginName, event, args); | ||
} | ||
async onEvent(callerPluginName, pluginName, event, listener) { | ||
@@ -27,0 +36,0 @@ await this.emit.onEvent(callerPluginName, pluginName, event, listener); |
export interface ServiceEvents { | ||
} | ||
export interface ServiceBroadcasts { | ||
} | ||
export interface ServiceReturnableEvents { | ||
@@ -4,0 +6,0 @@ } |
@@ -10,4 +10,4 @@ /// <reference types="node" /> | ||
import { DynamicallyReferencedMethodOnIEvents, DynamicallyReferencedMethodEmitIEvents, DynamicallyReferencedMethodEmitEARIEvents } from "../interfaces/events"; | ||
import { ServiceCallable, ServiceEvents, ServiceReturnableEvents } from "./base"; | ||
export declare class ServicesBase<onEvents = ServiceEvents, emitEvents = ServiceEvents, onReturnableEvents = ServiceReturnableEvents, emitReturnableEvents = ServiceReturnableEvents, callableMethods = ServiceCallable, pluginConfigType extends IPluginConfig = any> extends DefaultBase<pluginConfigType> implements IService<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents> { | ||
import { ServiceCallable, ServiceEvents, ServiceBroadcasts, ServiceReturnableEvents } from "./base"; | ||
export declare class ServicesBase<onEvents = ServiceEvents, emitEvents = ServiceEvents, onReturnableEvents = ServiceReturnableEvents, emitReturnableEvents = ServiceReturnableEvents, callableMethods = ServiceCallable, pluginConfigType extends IPluginConfig = any, onBroadcast = ServiceBroadcasts, emitBroadcast = ServiceBroadcasts> extends DefaultBase<pluginConfigType> implements IService<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, onBroadcast, emitBroadcast> { | ||
readonly initBeforePlugins?: Array<string>; | ||
@@ -18,3 +18,3 @@ readonly initAfterPlugins?: Array<string>; | ||
run(): Promise<void>; | ||
registerPluginClient<pluginClientOnEvents, pluginClientEmitEvents, pluginClientOnReturnableEvents, pluginClientEmitReturnableEvents, pluginCallableMethods, pluginClientConfigType extends IPluginConfig>(pluginName: string): Promise<RegisteredPlugin<pluginClientOnEvents, pluginClientEmitEvents, pluginClientOnReturnableEvents, pluginClientEmitReturnableEvents, pluginCallableMethods, pluginClientConfigType>>; | ||
registerPluginClient<pluginClientOnEvents, pluginClientEmitEvents, pluginClientOnReturnableEvents, pluginClientEmitReturnableEvents, pluginCallableMethods, pluginClientConfigType extends IPluginConfig, pluginClientOnBroadcast, pluginClientEmitBroadcast>(pluginName: string): Promise<RegisteredPlugin<pluginClientOnEvents, pluginClientEmitEvents, pluginClientOnReturnableEvents, pluginClientEmitReturnableEvents, pluginCallableMethods, pluginClientConfigType, pluginClientOnBroadcast, pluginClientEmitBroadcast>>; | ||
protected _clients: Array<ServicesClient>; | ||
@@ -24,2 +24,4 @@ constructor(pluginName: string, cwd: string, pluginCwd: string, log: IPluginLogger); | ||
sendStream(streamId: string, stream: Readable): Promise<void>; | ||
onBroadcast<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onBroadcast>, TA, false>): Promise<void>; | ||
emitBroadcast<TA extends string>(...args: DynamicallyReferencedMethodEmitIEvents<DynamicallyReferencedMethodType<emitBroadcast>, TA>): Promise<void>; | ||
onEvent<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onEvents>, TA, false>): Promise<void>; | ||
@@ -26,0 +28,0 @@ onEventSpecific<TA extends string>(serverId: string, ...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<onEvents>, TA, false>): Promise<void>; |
@@ -21,2 +21,8 @@ "use strict"; | ||
} | ||
onBroadcast(...args) { | ||
throw static_1.ErrorMessages.BSBNotInit; | ||
} | ||
emitBroadcast(...args) { | ||
throw static_1.ErrorMessages.BSBNotInit; | ||
} | ||
onEvent(...args) { | ||
@@ -23,0 +29,0 @@ throw static_1.ErrorMessages.BSBNotInit; |
@@ -8,6 +8,8 @@ /// <reference types="node" /> | ||
import { DynamicallyReferencedMethod, DynamicallyReferencedMethodType } from "@bettercorp/tools/lib/Interfaces"; | ||
import { ServiceEvents, ServiceReturnableEvents, ServiceCallable } from "./base"; | ||
export declare class RegisteredPlugin<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, callableMethods, PluginConfigType extends IPluginConfig> extends DefaultBase<PluginConfigType> implements IServiceEvents<emitEvents, onEvents, emitReturnableEvents, onReturnableEvents> { | ||
import { ServiceEvents, ServiceReturnableEvents, ServiceCallable, ServiceBroadcasts } from "./base"; | ||
export declare class RegisteredPlugin<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, callableMethods, PluginConfigType extends IPluginConfig, onBroadcast, emitBroadcast> extends DefaultBase<PluginConfigType> implements IServiceEvents<emitEvents, onEvents, emitReturnableEvents, onReturnableEvents, emitBroadcast, onBroadcast> { | ||
receiveStream(listener: (error: Error | null, stream: Readable) => Promise<void>, timeoutSeconds?: number): Promise<string>; | ||
sendStream(streamId: string, stream: Readable): Promise<void>; | ||
onBroadcast<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<emitBroadcast>, TA, false>): Promise<void>; | ||
emitBroadcast<TA extends string>(...args: DynamicallyReferencedMethodEmitIEvents<DynamicallyReferencedMethodType<onBroadcast>, TA>): Promise<void>; | ||
onEvent<TA extends string>(...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<emitEvents>, TA, false>): Promise<void>; | ||
@@ -25,3 +27,3 @@ onEventSpecific<TA extends string>(serverId: string, ...args: DynamicallyReferencedMethodOnIEvents<DynamicallyReferencedMethodType<emitEvents>, TA, false>): Promise<void>; | ||
} | ||
export declare class ServicesClient<onEvents = ServiceEvents, emitEvents = ServiceEvents, onReturnableEvents = ServiceReturnableEvents, emitReturnableEvents = ServiceReturnableEvents, callableMethods = ServiceCallable> { | ||
export declare class ServicesClient<onEvents = ServiceEvents, emitEvents = ServiceEvents, onReturnableEvents = ServiceReturnableEvents, emitReturnableEvents = ServiceReturnableEvents, callableMethods = ServiceCallable, onBroadcast = ServiceBroadcasts, emitBroadcast = ServiceBroadcasts> { | ||
readonly _pluginName: string; | ||
@@ -33,5 +35,5 @@ readonly initBeforePlugins?: Array<string>; | ||
private _referencedPlugin; | ||
protected _plugin: RegisteredPlugin<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, callableMethods, any>; | ||
protected _plugin: RegisteredPlugin<onEvents, emitEvents, onReturnableEvents, emitReturnableEvents, callableMethods, any, onBroadcast, emitBroadcast>; | ||
protected _register(): Promise<void>; | ||
constructor(self: ServicesBase<any, any, any>); | ||
} |
@@ -13,2 +13,8 @@ "use strict"; | ||
} | ||
onBroadcast(...args) { | ||
throw static_1.ErrorMessages.BSBNotInit; | ||
} | ||
emitBroadcast(...args) { | ||
throw static_1.ErrorMessages.BSBNotInit; | ||
} | ||
onEvent(...args) { | ||
@@ -15,0 +21,0 @@ throw static_1.ErrorMessages.BSBNotInit; |
@@ -10,4 +10,4 @@ import { ServicesBase } from "../service/service"; | ||
static setupPlugin<PluginConfigType extends IPluginConfig = any>(appId: string, runningDebug: boolean, runningLive: boolean, plugin: DefaultBase<PluginConfigType>, config: ConfigBase<PluginConfigType>): void; | ||
static setupServicePluginSpecific<PluginConfigType extends IPluginConfig = any>(plugin: ServicesBase<PluginConfigType> | RegisteredPlugin<any, any, any, any, any, any>, events: IServiceEvents<any, any, any, any>): void; | ||
static setupServicePlugin<PluginConfigType extends IPluginConfig = any>(plugin: ServicesBase<PluginConfigType>, events: IServiceEvents<any, any, any, any>, config: ConfigBase<PluginConfigType>, cwd: string, pluginCwd: string, generateEventsForService: (pluginName: string, mappedPluginName: string) => IServiceEvents<any, any, any, any>, generateLoggerForPlugin: { | ||
static setupServicePluginSpecific<PluginConfigType extends IPluginConfig = any>(plugin: ServicesBase<PluginConfigType> | RegisteredPlugin<any, any, any, any, any, any, any, any>, events: IServiceEvents<any, any, any, any, any, any>): void; | ||
static setupServicePlugin<PluginConfigType extends IPluginConfig = any>(plugin: ServicesBase<PluginConfigType>, events: IServiceEvents<any, any, any, any, any, any>, config: ConfigBase<PluginConfigType>, cwd: string, pluginCwd: string, generateEventsForService: (pluginName: string, mappedPluginName: string) => IServiceEvents<any, any, any, any, any, any>, generateLoggerForPlugin: { | ||
(pluginName: string): IPluginLogger; | ||
@@ -14,0 +14,0 @@ }, log: IPluginLogger, callPluginMethod: { |
@@ -20,10 +20,8 @@ "use strict"; | ||
static setupServicePluginSpecific(plugin, events) { | ||
plugin.emitEvent = | ||
events.emitEvent; | ||
plugin.emitEventSpecific = | ||
events.emitEventSpecific; | ||
plugin.onEvent = | ||
events.onEvent; | ||
plugin.onEventSpecific = | ||
events.onEventSpecific; | ||
plugin.emitBroadcast = events.emitBroadcast; | ||
plugin.onBroadcast = events.onBroadcast; | ||
plugin.emitEvent = events.emitEvent; | ||
plugin.emitEventSpecific = events.emitEventSpecific; | ||
plugin.onEvent = events.onEvent; | ||
plugin.onEventSpecific = events.onEventSpecific; | ||
plugin.emitEventAndReturnTimed = events.emitEventAndReturnTimed; | ||
@@ -35,6 +33,4 @@ plugin.emitEventAndReturnTimedSpecific = events.emitEventAndReturnTimedSpecific; | ||
plugin.onReturnableEventSpecific = events.onReturnableEventSpecific; | ||
plugin.receiveStream = | ||
events.receiveStream; | ||
plugin.sendStream = | ||
events.sendStream; | ||
plugin.receiveStream = events.receiveStream; | ||
plugin.sendStream = events.sendStream; | ||
} | ||
@@ -41,0 +37,0 @@ static setupServicePlugin(plugin, events, config, cwd, pluginCwd, generateEventsForService, generateLoggerForPlugin, log, callPluginMethod) { |
@@ -11,3 +11,3 @@ import { IPluginLogger } from "../interfaces/logger"; | ||
setupEvents(appId: string, runningDebug: boolean, runningLive: boolean, cwd: string, config: ConfigBase, plugin: IReadyPlugin, pluginLog: IPluginLogger): Promise<void>; | ||
generateEventsForService(pluginName: string, mappedPluginName: string): IServiceEvents<any, any, any, any>; | ||
generateEventsForService(pluginName: string, mappedPluginName: string): IServiceEvents<any, any, any, any, any, any>; | ||
} |
@@ -72,2 +72,21 @@ "use strict"; | ||
return { | ||
onBroadcast: async (...args) => { | ||
await self._activeEvents.onBroadcast(pluginName, mappedPluginName, args[0], async (iargs) => { | ||
const start = process.hrtime(); | ||
try { | ||
await args[1](...iargs); | ||
let diff = process.hrtime(start); | ||
await self.log.reportStat(`on-broadcast-${mappedPluginName}-${args[0]}`, (diff[0] * serviceBase_1.NS_PER_SEC + diff[1]) * serviceBase_1.MS_PER_NS); | ||
} | ||
catch (exc) { | ||
await self.log.reportStat(`on-broadcast-${mappedPluginName}-${args[0]}`, -1); | ||
await self.log.error(exc); | ||
throw exc; | ||
} | ||
}); | ||
}, | ||
emitBroadcast: async (...args) => { | ||
let event = args.splice(0, 1)[0]; | ||
await self._activeEvents.emitBroadcast(pluginName, mappedPluginName, event, args); | ||
}, | ||
onEvent: async (...args) => { | ||
@@ -74,0 +93,0 @@ await self._activeEvents.onEvent(pluginName, mappedPluginName, args[0], async (iargs) => { |
@@ -14,3 +14,3 @@ import { IPluginLogger } from "../interfaces/logger"; | ||
}, generateEventsForService: { | ||
(pluginName: string, mappedPluginName: string): IServiceEvents<any, any, any, any>; | ||
(pluginName: string, mappedPluginName: string): IServiceEvents<any, any, any, any, any, any>; | ||
}, generateLoggerForPlugin: { | ||
@@ -17,0 +17,0 @@ (pluginName: string): IPluginLogger; |
@@ -23,2 +23,29 @@ "use strict"; | ||
const emitData = true; | ||
it("only one plugin should receive the event", async () => { | ||
const thisCaller = randomName(); | ||
const thisPlugin = randomName(); | ||
const thisEvent = randomName(); | ||
let emitTimeout = setTimeout(() => { | ||
assert_1.default.fail("Event not received"); | ||
}, maxTimeoutToExpectAResponse); | ||
await emitter.onEvent(thisCaller, thisPlugin, thisEvent, async (data) => { | ||
if (emitTimeout === null) { | ||
assert_1.default.fail("Event received twice"); | ||
return; | ||
} | ||
clearTimeout(emitTimeout); | ||
emitTimeout = null; | ||
assert_1.default.ok(data[0]); | ||
}); | ||
await emitter.onEvent(thisCaller, thisPlugin, thisEvent, async (data) => { | ||
if (emitTimeout === null) { | ||
assert_1.default.fail("Event received twice"); | ||
return; | ||
} | ||
clearTimeout(emitTimeout); | ||
emitTimeout = null; | ||
assert_1.default.ok(data[0]); | ||
}); | ||
await emitter.emitEvent(thisCaller, thisPlugin, thisEvent, [emitData]); | ||
}); | ||
it("should be able to emit to events with plugin name defined", async () => { | ||
@@ -25,0 +52,0 @@ const thisCaller = randomName(); |
@@ -8,2 +8,3 @@ "use strict"; | ||
const plugin_1 = require("../../../plugins/events-default/plugin"); | ||
const broadcast_1 = require("./events/broadcast"); | ||
const emit_1 = require("./events/emit"); | ||
@@ -44,2 +45,9 @@ const emitAndReturn_1 = require("./events/emitAndReturn"); | ||
describe("plugins/events-default", () => { | ||
(0, broadcast_1.broadcast)(async () => { | ||
const refP = new plugin_1.Events("test-plugin", process.cwd(), process.cwd(), fakeLogger); | ||
refP.getPluginConfig = getPluginConfig; | ||
if (refP.init !== undefined) | ||
await refP.init(); | ||
return refP; | ||
}, 10); | ||
(0, emit_1.emit)(async () => { | ||
@@ -46,0 +54,0 @@ const refP = new plugin_1.Events("test-plugin", process.cwd(), process.cwd(), fakeLogger); |
@@ -32,3 +32,3 @@ { | ||
"main": "lib/index.js", | ||
"version": "8.3.32-rc.0", | ||
"version": "8.3.35-ea.0", | ||
"devDependencies": { | ||
@@ -35,0 +35,0 @@ "@types/assert": "^1.5.6", |
@@ -26,1 +26,4 @@ [](https://hub.docker.com/r/betterweb/service-base) | ||
View the docs here: [https://bsbcode.dev/](https://bsbcode.dev/) | ||
This project is tested with BrowserStack. |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
391161
147
4986
29