@lakutata/entrypoint 3.0.0-beta.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 +23 -0
- package/README.md +183 -0
- package/THIRD_PARTY_NOTICES.md +206 -0
- package/dist/cjs/Entrypoint.d.ts +868 -0
- package/dist/cjs/Entrypoint.js +998 -0
- package/dist/cjs/adapter/commander/CommanderCLIEntrypoint.d.ts +192 -0
- package/dist/cjs/adapter/commander/CommanderCLIEntrypoint.js +439 -0
- package/dist/cjs/adapter/commander/Prompt.d.ts +22 -0
- package/dist/cjs/adapter/commander/Prompt.js +164 -0
- package/dist/cjs/adapter/commander/Width.d.ts +12 -0
- package/dist/cjs/adapter/commander/Width.js +67 -0
- package/dist/cjs/adapter/electron/ElectronIPCServiceEntrypoint.d.ts +203 -0
- package/dist/cjs/adapter/electron/ElectronIPCServiceEntrypoint.js +249 -0
- package/dist/cjs/adapter/express/ExpressHTTPEntrypoint.d.ts +172 -0
- package/dist/cjs/adapter/express/ExpressHTTPEntrypoint.js +301 -0
- package/dist/cjs/adapter/express/Responders.d.ts +89 -0
- package/dist/cjs/adapter/express/Responders.js +71 -0
- package/dist/cjs/adapter/express/StaticUI.d.ts +83 -0
- package/dist/cjs/adapter/express/StaticUI.js +104 -0
- package/dist/cjs/adapter/fastify/FastifyHTTPEntrypoint.d.ts +156 -0
- package/dist/cjs/adapter/fastify/FastifyHTTPEntrypoint.js +339 -0
- package/dist/cjs/adapter/fastify/Responders.d.ts +88 -0
- package/dist/cjs/adapter/fastify/Responders.js +77 -0
- package/dist/cjs/adapter/http/HTTPServiceEntrypoint.d.ts +117 -0
- package/dist/cjs/adapter/http/HTTPServiceEntrypoint.js +257 -0
- package/dist/cjs/adapter/jsonrpc/JSONRPCProtocol.d.ts +194 -0
- package/dist/cjs/adapter/jsonrpc/JSONRPCProtocol.js +187 -0
- package/dist/cjs/adapter/jsonrpc/JSONRPCServiceEntrypoint.d.ts +154 -0
- package/dist/cjs/adapter/jsonrpc/JSONRPCServiceEntrypoint.js +397 -0
- package/dist/cjs/adapter/schedule/ScheduleEntrypoint.d.ts +86 -0
- package/dist/cjs/adapter/schedule/ScheduleEntrypoint.js +275 -0
- package/dist/cjs/adapter/socket.io/SocketIOServiceEntrypoint.d.ts +138 -0
- package/dist/cjs/adapter/socket.io/SocketIOServiceEntrypoint.js +216 -0
- package/dist/cjs/channel/BuiltinChannels.d.ts +18 -0
- package/dist/cjs/channel/BuiltinChannels.js +128 -0
- package/dist/cjs/channel/Channel.d.ts +196 -0
- package/dist/cjs/channel/Channel.js +106 -0
- package/dist/cjs/channel/EntrypointAdapter.d.ts +195 -0
- package/dist/cjs/channel/EntrypointAdapter.js +43 -0
- package/dist/cjs/context/BaseContext.d.ts +75 -0
- package/dist/cjs/context/BaseContext.js +90 -0
- package/dist/cjs/context/CLIContext.d.ts +38 -0
- package/dist/cjs/context/CLIContext.js +53 -0
- package/dist/cjs/context/HTTPContext.d.ts +52 -0
- package/dist/cjs/context/HTTPContext.js +78 -0
- package/dist/cjs/context/ScheduleContext.d.ts +47 -0
- package/dist/cjs/context/ScheduleContext.js +65 -0
- package/dist/cjs/context/ServiceContext.d.ts +47 -0
- package/dist/cjs/context/ServiceContext.js +62 -0
- package/dist/cjs/decorators/CLIAction.d.ts +120 -0
- package/dist/cjs/decorators/CLIAction.js +13 -0
- package/dist/cjs/decorators/HTTPAction.d.ts +178 -0
- package/dist/cjs/decorators/HTTPAction.js +14 -0
- package/dist/cjs/decorators/Prefix.d.ts +44 -0
- package/dist/cjs/decorators/Prefix.js +48 -0
- package/dist/cjs/decorators/Schedule.d.ts +120 -0
- package/dist/cjs/decorators/Schedule.js +64 -0
- package/dist/cjs/decorators/ServiceAction.d.ts +116 -0
- package/dist/cjs/decorators/ServiceAction.js +12 -0
- package/dist/cjs/decorators/UseMiddlewares.d.ts +52 -0
- package/dist/cjs/decorators/UseMiddlewares.js +58 -0
- package/dist/cjs/decorators/http/DELETE.d.ts +87 -0
- package/dist/cjs/decorators/http/DELETE.js +7 -0
- package/dist/cjs/decorators/http/GET.d.ts +89 -0
- package/dist/cjs/decorators/http/GET.js +7 -0
- package/dist/cjs/decorators/http/HEAD.d.ts +84 -0
- package/dist/cjs/decorators/http/HEAD.js +7 -0
- package/dist/cjs/decorators/http/OPTIONS.d.ts +80 -0
- package/dist/cjs/decorators/http/OPTIONS.js +7 -0
- package/dist/cjs/decorators/http/PATCH.d.ts +90 -0
- package/dist/cjs/decorators/http/PATCH.js +7 -0
- package/dist/cjs/decorators/http/POST.d.ts +91 -0
- package/dist/cjs/decorators/http/POST.js +7 -0
- package/dist/cjs/decorators/http/PUT.d.ts +92 -0
- package/dist/cjs/decorators/http/PUT.js +7 -0
- package/dist/cjs/exceptions/AccessDeniedException.d.ts +10 -0
- package/dist/cjs/exceptions/AccessDeniedException.js +17 -0
- package/dist/cjs/exceptions/ControllerActionNotFoundException.d.ts +9 -0
- package/dist/cjs/exceptions/ControllerActionNotFoundException.js +16 -0
- package/dist/cjs/exceptions/DuplicateActionNameException.d.ts +8 -0
- package/dist/cjs/exceptions/DuplicateActionNameException.js +15 -0
- package/dist/cjs/exceptions/EntrypointStoppedException.d.ts +9 -0
- package/dist/cjs/exceptions/EntrypointStoppedException.js +16 -0
- package/dist/cjs/exceptions/InvalidActionGroupException.d.ts +4 -0
- package/dist/cjs/exceptions/InvalidActionGroupException.js +11 -0
- package/dist/cjs/exceptions/InvalidChannelException.d.ts +8 -0
- package/dist/cjs/exceptions/InvalidChannelException.js +15 -0
- package/dist/cjs/exceptions/InvalidEntrypointAdapterException.d.ts +9 -0
- package/dist/cjs/exceptions/InvalidEntrypointAdapterException.js +16 -0
- package/dist/cjs/exceptions/InvalidMiddlewareException.d.ts +8 -0
- package/dist/cjs/exceptions/InvalidMiddlewareException.js +15 -0
- package/dist/cjs/exceptions/InvalidPrefixException.d.ts +8 -0
- package/dist/cjs/exceptions/InvalidPrefixException.js +15 -0
- package/dist/cjs/exceptions/MiddlewareNextCalledException.d.ts +8 -0
- package/dist/cjs/exceptions/MiddlewareNextCalledException.js +15 -0
- package/dist/cjs/exceptions/RouteNotFoundException.d.ts +9 -0
- package/dist/cjs/exceptions/RouteNotFoundException.js +16 -0
- package/dist/cjs/exceptions/ServiceInvokeException.d.ts +11 -0
- package/dist/cjs/exceptions/ServiceInvokeException.js +14 -0
- package/dist/cjs/exports/CTRLDecorators.d.ts +13 -0
- package/dist/cjs/exports/CTRLDecorators.js +38 -0
- package/dist/cjs/exports/Channel.d.ts +14 -0
- package/dist/cjs/exports/Channel.js +33 -0
- package/dist/cjs/exports/CommanderAdapter.d.ts +1 -0
- package/dist/cjs/exports/CommanderAdapter.js +8 -0
- package/dist/cjs/exports/ElectronAdapter.d.ts +1 -0
- package/dist/cjs/exports/ElectronAdapter.js +9 -0
- package/dist/cjs/exports/Entrypoint.d.ts +24 -0
- package/dist/cjs/exports/Entrypoint.js +55 -0
- package/dist/cjs/exports/ExpressAdapter.d.ts +4 -0
- package/dist/cjs/exports/ExpressAdapter.js +15 -0
- package/dist/cjs/exports/FastifyAdapter.d.ts +4 -0
- package/dist/cjs/exports/FastifyAdapter.js +14 -0
- package/dist/cjs/exports/JSONRPCAdapter.d.ts +2 -0
- package/dist/cjs/exports/JSONRPCAdapter.js +11 -0
- package/dist/cjs/exports/SocketIOAdapter.d.ts +1 -0
- package/dist/cjs/exports/SocketIOAdapter.js +9 -0
- package/dist/cjs/lib/AccessControl.d.ts +118 -0
- package/dist/cjs/lib/AccessControl.js +208 -0
- package/dist/cjs/lib/AccessControlRule.d.ts +91 -0
- package/dist/cjs/lib/AccessControlRule.js +76 -0
- package/dist/cjs/lib/ActionOptions.d.ts +67 -0
- package/dist/cjs/lib/ActionOptions.js +67 -0
- package/dist/cjs/lib/ActionResult.d.ts +28 -0
- package/dist/cjs/lib/ActionResult.js +28 -0
- package/dist/cjs/lib/ApplicationOf.d.ts +6 -0
- package/dist/cjs/lib/ApplicationOf.js +14 -0
- package/dist/cjs/lib/Controller.d.ts +59 -0
- package/dist/cjs/lib/Controller.js +64 -0
- package/dist/cjs/lib/ControllerEntrypoint.d.ts +168 -0
- package/dist/cjs/lib/ControllerEntrypoint.js +239 -0
- package/dist/cjs/lib/GetActionDTOAndOptions.d.ts +29 -0
- package/dist/cjs/lib/GetActionDTOAndOptions.js +49 -0
- package/dist/cjs/lib/HTTPErrors.d.ts +55 -0
- package/dist/cjs/lib/HTTPErrors.js +52 -0
- package/dist/cjs/lib/Middleware.d.ts +146 -0
- package/dist/cjs/lib/Middleware.js +144 -0
- package/dist/cjs/lib/Prefix.d.ts +98 -0
- package/dist/cjs/lib/Prefix.js +151 -0
- package/dist/cjs/lib/Result.d.ts +173 -0
- package/dist/cjs/lib/Result.js +153 -0
- package/dist/cjs/lib/ServiceResponseCodec.d.ts +82 -0
- package/dist/cjs/lib/ServiceResponseCodec.js +117 -0
- package/dist/cjs/lib/StringifyPattern.d.ts +7 -0
- package/dist/cjs/lib/StringifyPattern.js +58 -0
- package/dist/cjs/lib/UnsetValue.d.ts +12 -0
- package/dist/cjs/lib/UnsetValue.js +216 -0
- package/dist/cjs/lib/pattern/GlobPattern.d.ts +34 -0
- package/dist/cjs/lib/pattern/GlobPattern.js +68 -0
- package/dist/cjs/lib/pattern/PatternManager.d.ts +103 -0
- package/dist/cjs/lib/pattern/PatternManager.js +233 -0
- package/dist/cjs/lib/schedule/Cron.d.ts +56 -0
- package/dist/cjs/lib/schedule/Cron.js +282 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/CLIContextParams.d.ts +14 -0
- package/dist/cjs/types/CLIContextParams.js +2 -0
- package/dist/cjs/types/ErrorDescription.d.ts +14 -0
- package/dist/cjs/types/ErrorDescription.js +2 -0
- package/dist/cjs/types/HTTPContextParams.d.ts +27 -0
- package/dist/cjs/types/HTTPContextParams.js +2 -0
- package/dist/cjs/types/ScheduleContextParams.d.ts +21 -0
- package/dist/cjs/types/ScheduleContextParams.js +2 -0
- package/dist/cjs/types/ServiceContextParams.d.ts +16 -0
- package/dist/cjs/types/ServiceContextParams.js +2 -0
- package/dist/esm/Entrypoint.js +985 -0
- package/dist/esm/adapter/commander/CommanderCLIEntrypoint.js +433 -0
- package/dist/esm/adapter/commander/Prompt.js +127 -0
- package/dist/esm/adapter/commander/Width.js +63 -0
- package/dist/esm/adapter/electron/ElectronIPCServiceEntrypoint.js +212 -0
- package/dist/esm/adapter/express/ExpressHTTPEntrypoint.js +295 -0
- package/dist/esm/adapter/express/Responders.js +66 -0
- package/dist/esm/adapter/express/StaticUI.js +96 -0
- package/dist/esm/adapter/fastify/FastifyHTTPEntrypoint.js +333 -0
- package/dist/esm/adapter/fastify/Responders.js +73 -0
- package/dist/esm/adapter/http/HTTPServiceEntrypoint.js +253 -0
- package/dist/esm/adapter/jsonrpc/JSONRPCProtocol.js +182 -0
- package/dist/esm/adapter/jsonrpc/JSONRPCServiceEntrypoint.js +361 -0
- package/dist/esm/adapter/schedule/ScheduleEntrypoint.js +272 -0
- package/dist/esm/adapter/socket.io/SocketIOServiceEntrypoint.js +212 -0
- package/dist/esm/channel/BuiltinChannels.js +125 -0
- package/dist/esm/channel/Channel.js +100 -0
- package/dist/esm/channel/EntrypointAdapter.js +39 -0
- package/dist/esm/context/BaseContext.js +87 -0
- package/dist/esm/context/CLIContext.js +49 -0
- package/dist/esm/context/HTTPContext.js +74 -0
- package/dist/esm/context/ScheduleContext.js +61 -0
- package/dist/esm/context/ServiceContext.js +58 -0
- package/dist/esm/decorators/CLIAction.js +10 -0
- package/dist/esm/decorators/HTTPAction.js +11 -0
- package/dist/esm/decorators/Prefix.js +45 -0
- package/dist/esm/decorators/Schedule.js +61 -0
- package/dist/esm/decorators/ServiceAction.js +9 -0
- package/dist/esm/decorators/UseMiddlewares.js +55 -0
- package/dist/esm/decorators/http/DELETE.js +4 -0
- package/dist/esm/decorators/http/GET.js +4 -0
- package/dist/esm/decorators/http/HEAD.js +4 -0
- package/dist/esm/decorators/http/OPTIONS.js +4 -0
- package/dist/esm/decorators/http/PATCH.js +4 -0
- package/dist/esm/decorators/http/POST.js +4 -0
- package/dist/esm/decorators/http/PUT.js +4 -0
- package/dist/esm/exceptions/AccessDeniedException.js +13 -0
- package/dist/esm/exceptions/ControllerActionNotFoundException.js +12 -0
- package/dist/esm/exceptions/DuplicateActionNameException.js +11 -0
- package/dist/esm/exceptions/EntrypointStoppedException.js +12 -0
- package/dist/esm/exceptions/InvalidActionGroupException.js +7 -0
- package/dist/esm/exceptions/InvalidChannelException.js +11 -0
- package/dist/esm/exceptions/InvalidEntrypointAdapterException.js +12 -0
- package/dist/esm/exceptions/InvalidMiddlewareException.js +11 -0
- package/dist/esm/exceptions/InvalidPrefixException.js +11 -0
- package/dist/esm/exceptions/MiddlewareNextCalledException.js +11 -0
- package/dist/esm/exceptions/RouteNotFoundException.js +12 -0
- package/dist/esm/exceptions/ServiceInvokeException.js +10 -0
- package/dist/esm/exports/CTRLDecorators.js +19 -0
- package/dist/esm/exports/Channel.js +11 -0
- package/dist/esm/exports/CommanderAdapter.js +1 -0
- package/dist/esm/exports/ElectronAdapter.js +1 -0
- package/dist/esm/exports/Entrypoint.js +22 -0
- package/dist/esm/exports/ExpressAdapter.js +3 -0
- package/dist/esm/exports/FastifyAdapter.js +3 -0
- package/dist/esm/exports/JSONRPCAdapter.js +2 -0
- package/dist/esm/exports/SocketIOAdapter.js +1 -0
- package/dist/esm/lib/AccessControl.js +204 -0
- package/dist/esm/lib/AccessControlRule.js +72 -0
- package/dist/esm/lib/ActionOptions.js +63 -0
- package/dist/esm/lib/ActionResult.js +24 -0
- package/dist/esm/lib/ApplicationOf.js +11 -0
- package/dist/esm/lib/Controller.js +61 -0
- package/dist/esm/lib/ControllerEntrypoint.js +229 -0
- package/dist/esm/lib/GetActionDTOAndOptions.js +46 -0
- package/dist/esm/lib/HTTPErrors.js +48 -0
- package/dist/esm/lib/Middleware.js +134 -0
- package/dist/esm/lib/Prefix.js +143 -0
- package/dist/esm/lib/Result.js +147 -0
- package/dist/esm/lib/ServiceResponseCodec.js +113 -0
- package/dist/esm/lib/StringifyPattern.js +55 -0
- package/dist/esm/lib/UnsetValue.js +213 -0
- package/dist/esm/lib/pattern/GlobPattern.js +64 -0
- package/dist/esm/lib/pattern/PatternManager.js +229 -0
- package/dist/esm/lib/schedule/Cron.js +277 -0
- package/dist/esm/types/CLIContextParams.js +1 -0
- package/dist/esm/types/ErrorDescription.js +1 -0
- package/dist/esm/types/HTTPContextParams.js +1 -0
- package/dist/esm/types/ScheduleContextParams.js +1 -0
- package/dist/esm/types/ServiceContextParams.js +1 -0
- package/dist/types/Entrypoint.d.ts +868 -0
- package/dist/types/adapter/commander/CommanderCLIEntrypoint.d.ts +192 -0
- package/dist/types/adapter/commander/Prompt.d.ts +22 -0
- package/dist/types/adapter/commander/Width.d.ts +12 -0
- package/dist/types/adapter/electron/ElectronIPCServiceEntrypoint.d.ts +203 -0
- package/dist/types/adapter/express/ExpressHTTPEntrypoint.d.ts +172 -0
- package/dist/types/adapter/express/Responders.d.ts +89 -0
- package/dist/types/adapter/express/StaticUI.d.ts +83 -0
- package/dist/types/adapter/fastify/FastifyHTTPEntrypoint.d.ts +156 -0
- package/dist/types/adapter/fastify/Responders.d.ts +88 -0
- package/dist/types/adapter/http/HTTPServiceEntrypoint.d.ts +117 -0
- package/dist/types/adapter/jsonrpc/JSONRPCProtocol.d.ts +194 -0
- package/dist/types/adapter/jsonrpc/JSONRPCServiceEntrypoint.d.ts +154 -0
- package/dist/types/adapter/schedule/ScheduleEntrypoint.d.ts +86 -0
- package/dist/types/adapter/socket.io/SocketIOServiceEntrypoint.d.ts +138 -0
- package/dist/types/channel/BuiltinChannels.d.ts +18 -0
- package/dist/types/channel/Channel.d.ts +196 -0
- package/dist/types/channel/EntrypointAdapter.d.ts +195 -0
- package/dist/types/context/BaseContext.d.ts +75 -0
- package/dist/types/context/CLIContext.d.ts +38 -0
- package/dist/types/context/HTTPContext.d.ts +52 -0
- package/dist/types/context/ScheduleContext.d.ts +47 -0
- package/dist/types/context/ServiceContext.d.ts +47 -0
- package/dist/types/decorators/CLIAction.d.ts +120 -0
- package/dist/types/decorators/HTTPAction.d.ts +178 -0
- package/dist/types/decorators/Prefix.d.ts +44 -0
- package/dist/types/decorators/Schedule.d.ts +120 -0
- package/dist/types/decorators/ServiceAction.d.ts +116 -0
- package/dist/types/decorators/UseMiddlewares.d.ts +52 -0
- package/dist/types/decorators/http/DELETE.d.ts +87 -0
- package/dist/types/decorators/http/GET.d.ts +89 -0
- package/dist/types/decorators/http/HEAD.d.ts +84 -0
- package/dist/types/decorators/http/OPTIONS.d.ts +80 -0
- package/dist/types/decorators/http/PATCH.d.ts +90 -0
- package/dist/types/decorators/http/POST.d.ts +91 -0
- package/dist/types/decorators/http/PUT.d.ts +92 -0
- package/dist/types/exceptions/AccessDeniedException.d.ts +10 -0
- package/dist/types/exceptions/ControllerActionNotFoundException.d.ts +9 -0
- package/dist/types/exceptions/DuplicateActionNameException.d.ts +8 -0
- package/dist/types/exceptions/EntrypointStoppedException.d.ts +9 -0
- package/dist/types/exceptions/InvalidActionGroupException.d.ts +4 -0
- package/dist/types/exceptions/InvalidChannelException.d.ts +8 -0
- package/dist/types/exceptions/InvalidEntrypointAdapterException.d.ts +9 -0
- package/dist/types/exceptions/InvalidMiddlewareException.d.ts +8 -0
- package/dist/types/exceptions/InvalidPrefixException.d.ts +8 -0
- package/dist/types/exceptions/MiddlewareNextCalledException.d.ts +8 -0
- package/dist/types/exceptions/RouteNotFoundException.d.ts +9 -0
- package/dist/types/exceptions/ServiceInvokeException.d.ts +11 -0
- package/dist/types/exports/CTRLDecorators.d.ts +13 -0
- package/dist/types/exports/Channel.d.ts +14 -0
- package/dist/types/exports/CommanderAdapter.d.ts +1 -0
- package/dist/types/exports/ElectronAdapter.d.ts +1 -0
- package/dist/types/exports/Entrypoint.d.ts +24 -0
- package/dist/types/exports/ExpressAdapter.d.ts +4 -0
- package/dist/types/exports/FastifyAdapter.d.ts +4 -0
- package/dist/types/exports/JSONRPCAdapter.d.ts +2 -0
- package/dist/types/exports/SocketIOAdapter.d.ts +1 -0
- package/dist/types/lib/AccessControl.d.ts +118 -0
- package/dist/types/lib/AccessControlRule.d.ts +91 -0
- package/dist/types/lib/ActionOptions.d.ts +67 -0
- package/dist/types/lib/ActionResult.d.ts +28 -0
- package/dist/types/lib/ApplicationOf.d.ts +6 -0
- package/dist/types/lib/Controller.d.ts +59 -0
- package/dist/types/lib/ControllerEntrypoint.d.ts +168 -0
- package/dist/types/lib/GetActionDTOAndOptions.d.ts +29 -0
- package/dist/types/lib/HTTPErrors.d.ts +55 -0
- package/dist/types/lib/Middleware.d.ts +146 -0
- package/dist/types/lib/Prefix.d.ts +98 -0
- package/dist/types/lib/Result.d.ts +173 -0
- package/dist/types/lib/ServiceResponseCodec.d.ts +82 -0
- package/dist/types/lib/StringifyPattern.d.ts +7 -0
- package/dist/types/lib/UnsetValue.d.ts +12 -0
- package/dist/types/lib/pattern/GlobPattern.d.ts +34 -0
- package/dist/types/lib/pattern/PatternManager.d.ts +103 -0
- package/dist/types/lib/schedule/Cron.d.ts +56 -0
- package/dist/types/types/CLIContextParams.d.ts +14 -0
- package/dist/types/types/ErrorDescription.d.ts +14 -0
- package/dist/types/types/HTTPContextParams.d.ts +27 -0
- package/dist/types/types/ScheduleContextParams.d.ts +21 -0
- package/dist/types/types/ServiceContextParams.d.ts +16 -0
- package/package.json +152 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { ScheduleContext } from '../../context/ScheduleContext.js';
|
|
2
|
+
import { ScheduleChannel } from '../../channel/BuiltinChannels.js';
|
|
3
|
+
import { Cron } from '../../lib/schedule/Cron.js';
|
|
4
|
+
/**
|
|
5
|
+
* The longest time a lock of a run is kept after it (the other instances whose clocks are late skip it)
|
|
6
|
+
*/
|
|
7
|
+
const MAX_HOLD = 30000;
|
|
8
|
+
/**
|
|
9
|
+
* The longest delay of a timer
|
|
10
|
+
*/
|
|
11
|
+
const MAX_DELAY = 2 ** 31 - 1;
|
|
12
|
+
/**
|
|
13
|
+
* Create the schedule adapter of an entrypoint: it runs the controller actions declared with `@Schedule`, which never
|
|
14
|
+
* run without it. It needs no optional dependency.
|
|
15
|
+
*
|
|
16
|
+
* The runs begin once the application has launched (all its objects, its bootstrap and the other adapters are ready).
|
|
17
|
+
* Each run is dispatched like a request: a new controller in its own scope, the middlewares and the access control, a
|
|
18
|
+
* {@link ScheduleContext} giving the schedule, the time the run was due and its number. An interval keeps its pace:
|
|
19
|
+
* the runs missed while the process was suspended are skipped, not caught up. An action whose previous run is still in
|
|
20
|
+
* progress skips its run unless it allows `overlap`.
|
|
21
|
+
*
|
|
22
|
+
* An `exclusive` action runs in one instance of the application at a time: each run first tries to acquire a lock
|
|
23
|
+
* named `schedule:<key>` (the `key` of its `exclusive` option, the action name by default) from the mutex component
|
|
24
|
+
* (the `Mutex` of `lakutata/com/mutex`, named `mutex` by default), and is skipped when another
|
|
25
|
+
* instance holds it; the lock is kept after the run for half of its period (30 seconds at most) so that the instances
|
|
26
|
+
* whose clocks are late skip it too. An exclusive action whose mutex component cannot be resolved never runs (the
|
|
27
|
+
* error is logged).
|
|
28
|
+
*
|
|
29
|
+
* When the entrypoint stops, no new run starts, the kept locks are released and the entrypoint waits for the runs in
|
|
30
|
+
* progress (aborting them after its `shutdownTimeout`).
|
|
31
|
+
* @param options The adapter options.
|
|
32
|
+
* @returns The adapter, to give to the `schedule` option of `buildEntrypoints`.
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import {Application} from 'lakutata'
|
|
36
|
+
* import {buildEntrypoints, Controller, ScheduleContext, scheduleEntrypoint} from 'lakutata/com/entrypoint'
|
|
37
|
+
* import {Schedule} from 'lakutata/decorator/ctrl'
|
|
38
|
+
*
|
|
39
|
+
* class MaintenanceController extends Controller {
|
|
40
|
+
* //Every 5 minutes, and once the application launched
|
|
41
|
+
* @Schedule({every: '5m', immediate: true})
|
|
42
|
+
* public async purge(): Promise<void> {
|
|
43
|
+
* const context: ScheduleContext = this.context as ScheduleContext
|
|
44
|
+
* console.log(`Run ${context.run}, due at ${context.scheduledAt.toISOString()}`)
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* //Every day at 03:00 in Shanghai
|
|
48
|
+
* @Schedule({cron: '0 3 * * *', timezone: 'Asia/Shanghai'})
|
|
49
|
+
* public async report(): Promise<void> {
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* Application.run({
|
|
54
|
+
* id: 'worker.app',
|
|
55
|
+
* name: 'Worker',
|
|
56
|
+
* components: {
|
|
57
|
+
* entrypoint: buildEntrypoints({
|
|
58
|
+
* controllers: [MaintenanceController],
|
|
59
|
+
* schedule: scheduleEntrypoint({
|
|
60
|
+
* onError: (error, action): void => console.error(`${action.name} failed`, error),
|
|
61
|
+
* onSkip: (action, scheduledAt, reason): void => console.warn(`${action.name} skipped (${reason})`)
|
|
62
|
+
* })
|
|
63
|
+
* })
|
|
64
|
+
* }
|
|
65
|
+
* })
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function scheduleEntrypoint(options = {}) {
|
|
69
|
+
const timers = new Set();
|
|
70
|
+
let stopped = false;
|
|
71
|
+
//The locks of the runs ended, kept a while: released at once when the adapter stops
|
|
72
|
+
const keptLocks = new Map();
|
|
73
|
+
const releaseKept = (lock) => {
|
|
74
|
+
clearTimeout(keptLocks.get(lock));
|
|
75
|
+
keptLocks.delete(lock);
|
|
76
|
+
lock.release().catch(() => undefined);
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Run a function at a time
|
|
80
|
+
* @param at
|
|
81
|
+
* @param run
|
|
82
|
+
*/
|
|
83
|
+
const at = (time, run) => {
|
|
84
|
+
if (stopped)
|
|
85
|
+
return;
|
|
86
|
+
const delay = time - Date.now();
|
|
87
|
+
const timer = setTimeout(() => {
|
|
88
|
+
timers.delete(timer);
|
|
89
|
+
//The delays longer than a timer allows are waited in several times
|
|
90
|
+
delay > MAX_DELAY ? at(time, run) : run();
|
|
91
|
+
}, Math.max(0, Math.min(delay, MAX_DELAY)));
|
|
92
|
+
timers.add(timer);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Write a failed run to the log of the application
|
|
96
|
+
* @param runtime
|
|
97
|
+
* @param error
|
|
98
|
+
* @param action
|
|
99
|
+
* @param context
|
|
100
|
+
*/
|
|
101
|
+
const report = async (runtime, error, action, context) => {
|
|
102
|
+
if (options.onError)
|
|
103
|
+
return await options.onError(error, action, context);
|
|
104
|
+
await log(runtime, error, `The scheduled action ${action.name} (${context.schedule}) failed`);
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Write an error to the log of the application
|
|
108
|
+
* @param runtime
|
|
109
|
+
* @param error
|
|
110
|
+
* @param message
|
|
111
|
+
*/
|
|
112
|
+
const log = async (runtime, error, message) => {
|
|
113
|
+
try {
|
|
114
|
+
const logger = await runtime.getObject('log');
|
|
115
|
+
logger.error(error, message);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
console.error(`${message}:`, error);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
channel: 'schedule',
|
|
123
|
+
definition: ScheduleChannel,
|
|
124
|
+
name: options.name,
|
|
125
|
+
middlewares: options.middlewares,
|
|
126
|
+
start: (runtime) => {
|
|
127
|
+
const states = new Map();
|
|
128
|
+
//The components of the locks of the exclusive actions
|
|
129
|
+
const locks = new Map();
|
|
130
|
+
/**
|
|
131
|
+
* The time between a run of an action and its next one
|
|
132
|
+
* @param action
|
|
133
|
+
* @param scheduledAt
|
|
134
|
+
*/
|
|
135
|
+
const period = (action, scheduledAt) => {
|
|
136
|
+
if (action.address.every !== undefined)
|
|
137
|
+
return action.address.every;
|
|
138
|
+
const next = new Cron(action.address.cron, action.address.timezone).next(scheduledAt);
|
|
139
|
+
return next ? next.getTime() - scheduledAt : MAX_HOLD * 2;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Tell a run skipped: an error of the handler is written to the log, the schedule goes on
|
|
143
|
+
* @param action
|
|
144
|
+
* @param scheduledAt
|
|
145
|
+
* @param reason
|
|
146
|
+
*/
|
|
147
|
+
const skip = (action, scheduledAt, reason) => {
|
|
148
|
+
try {
|
|
149
|
+
options.onSkip?.(action, new Date(scheduledAt), reason);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
void log(runtime, error, `The onSkip handler of the scheduled action ${action.name} failed`);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Run an action
|
|
157
|
+
* @param action
|
|
158
|
+
* @param scheduledAt
|
|
159
|
+
*/
|
|
160
|
+
const fire = (action, scheduledAt) => {
|
|
161
|
+
if (stopped)
|
|
162
|
+
return;
|
|
163
|
+
const state = states.get(action);
|
|
164
|
+
if (state.running && !action.address.overlap)
|
|
165
|
+
return skip(action, scheduledAt, 'overlap');
|
|
166
|
+
state.running++;
|
|
167
|
+
const exclusive = locks.get(action);
|
|
168
|
+
void (async () => {
|
|
169
|
+
let lock;
|
|
170
|
+
try {
|
|
171
|
+
if (exclusive) {
|
|
172
|
+
lock = await exclusive.tryAcquire(`schedule:${action.address.exclusiveKey ?? action.name}`);
|
|
173
|
+
//Run by another instance
|
|
174
|
+
if (!lock)
|
|
175
|
+
return skip(action, scheduledAt, 'exclusive');
|
|
176
|
+
}
|
|
177
|
+
state.runs++;
|
|
178
|
+
const context = new ScheduleContext({ schedule: action.address.schedule, scheduledAt: new Date(scheduledAt), run: state.runs, data: {} });
|
|
179
|
+
await runtime.dispatchTo(action, context).catch((error) => report(runtime, error, action, context));
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
await log(runtime, error, `The lock of the scheduled action ${action.name} failed`);
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
state.running--;
|
|
186
|
+
if (lock) {
|
|
187
|
+
//Kept until the half of its period (30 s at most): the instances whose clocks are late skip
|
|
188
|
+
//the run too
|
|
189
|
+
const held = lock;
|
|
190
|
+
const remaining = scheduledAt + Math.min(period(action, scheduledAt) / 2, MAX_HOLD) - Date.now();
|
|
191
|
+
if (remaining <= 0 || stopped) {
|
|
192
|
+
held.release().catch(() => undefined);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
const timer = setTimeout(() => releaseKept(held), remaining);
|
|
196
|
+
keptLocks.set(held, timer);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
})().catch(() => undefined);
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Plan the runs of an action
|
|
204
|
+
* @param action
|
|
205
|
+
*/
|
|
206
|
+
const plan = (action) => {
|
|
207
|
+
if (action.address.every !== undefined) {
|
|
208
|
+
const interval = action.address.every;
|
|
209
|
+
//The exclusive ones aligned on the multiples of their interval: the same times in every instance
|
|
210
|
+
let next = action.address.exclusive ? Math.ceil((Date.now() + 1) / interval) * interval : Date.now() + interval;
|
|
211
|
+
const tick = () => {
|
|
212
|
+
fire(action, next);
|
|
213
|
+
//The runs keep their pace: the times missed (the process was suspended) are skipped
|
|
214
|
+
next += interval;
|
|
215
|
+
const now = Date.now();
|
|
216
|
+
if (next <= now)
|
|
217
|
+
next += Math.ceil((now - next + 1) / interval) * interval;
|
|
218
|
+
at(next, tick);
|
|
219
|
+
};
|
|
220
|
+
at(next, tick);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
const cron = new Cron(action.address.cron, action.address.timezone);
|
|
224
|
+
const schedule = (after) => {
|
|
225
|
+
const time = cron.next(after);
|
|
226
|
+
if (!time)
|
|
227
|
+
return;
|
|
228
|
+
at(time.getTime(), () => {
|
|
229
|
+
fire(action, time.getTime());
|
|
230
|
+
schedule(Math.max(Date.now(), time.getTime()));
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
schedule(Date.now());
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
//The runs begin once the application launched: its objects and the other adapters are ready
|
|
237
|
+
//Only resolved, fire() reports the errors of the runs
|
|
238
|
+
void runtime.ready.then(async () => {
|
|
239
|
+
if (stopped)
|
|
240
|
+
return;
|
|
241
|
+
for (const action of runtime.actions) {
|
|
242
|
+
if (action.address.exclusive) {
|
|
243
|
+
//Its locks: without them, it does not run (rather than in every instance)
|
|
244
|
+
try {
|
|
245
|
+
const component = await runtime.getObject(action.address.exclusive);
|
|
246
|
+
if (typeof component?.tryAcquire !== 'function')
|
|
247
|
+
throw new Error(`The object ${action.address.exclusive} is not a component of the locks (the Mutex of @lakutata/mutex)`);
|
|
248
|
+
locks.set(action, component);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
await log(runtime, error, `The exclusive scheduled action ${action.name} does not run: its component of the locks ${action.address.exclusive} is not there`);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (stopped)
|
|
256
|
+
return;
|
|
257
|
+
states.set(action, { running: 0, runs: 0 });
|
|
258
|
+
if (action.address.immediate)
|
|
259
|
+
fire(action, Date.now());
|
|
260
|
+
plan(action);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
stop: () => {
|
|
265
|
+
//No new run, the runs in progress are waited for by the entrypoint
|
|
266
|
+
stopped = true;
|
|
267
|
+
timers.forEach((timer) => clearTimeout(timer));
|
|
268
|
+
timers.clear();
|
|
269
|
+
[...keptLocks.keys()].forEach(releaseKept);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Server as SocketIOServer } from 'socket.io';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { ServiceContext } from '../../context/ServiceContext.js';
|
|
4
|
+
import { ServiceChannel } from '../../channel/BuiltinChannels.js';
|
|
5
|
+
import { ServiceResponseCodec } from '../../lib/ServiceResponseCodec.js';
|
|
6
|
+
import { EntrypointStoppedException } from '../../exceptions/EntrypointStoppedException.js';
|
|
7
|
+
/**
|
|
8
|
+
* Get the socket.io socket that sent a service action's request, when {@link socketIOServiceEntrypoint} serves it (to
|
|
9
|
+
* read its handshake, join it to rooms, emit to it…).
|
|
10
|
+
* @param context The service action's context (`this.context` in a controller).
|
|
11
|
+
* @returns The socket, or `undefined` when another adapter serves the request.
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import {Controller, ServiceContext} from 'lakutata/com/entrypoint'
|
|
15
|
+
* import {ServiceAction} from 'lakutata/decorator/ctrl'
|
|
16
|
+
* import {socketOfContext} from 'lakutata/adapter/socket.io'
|
|
17
|
+
*
|
|
18
|
+
* class RoomController extends Controller {
|
|
19
|
+
* //socket.emit('message', {cmd: 'room.join', room: 'lobby'}, (response) => …)
|
|
20
|
+
* @ServiceAction({cmd: 'room.join'})
|
|
21
|
+
* public async join(input: {room: string}): Promise<void> {
|
|
22
|
+
* await socketOfContext(this.context as ServiceContext)?.join(input.room)
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function socketOfContext(context) {
|
|
28
|
+
return context.socket;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The id of the client of a socket: the one it gives when it connects (its auth.id, its id header, its id query), its
|
|
32
|
+
* own id otherwise
|
|
33
|
+
* @param socket
|
|
34
|
+
*/
|
|
35
|
+
function clientIdOf(socket) {
|
|
36
|
+
const given = socket.handshake.auth?.id ?? socket.handshake.headers.id ?? socket.handshake.query.id;
|
|
37
|
+
const id = Array.isArray(given) ? given[0] : given;
|
|
38
|
+
return typeof id === 'string' && id && id.length <= 200 ? id : socket.id;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create a service adapter of an entrypoint served by socket.io. It needs the optional peer dependency `socket.io`
|
|
42
|
+
* (and `socket.io-client` on the clients).
|
|
43
|
+
*
|
|
44
|
+
* A request is an emit of the request event (`'message'` by default) whose data holds the action's pattern and input,
|
|
45
|
+
* with an acknowledgement callback receiving the response `{success, error, payload}` (see `ServiceResponseCodec`):
|
|
46
|
+
* `payload` is the result, or `error` the whole error object when the action failed (no matching action, validation…).
|
|
47
|
+
* An emit without an acknowledgement still runs the action.
|
|
48
|
+
*
|
|
49
|
+
* Each socket belongs to a client, identified by the id it gives when it connects (`auth.id`, else the `id` header,
|
|
50
|
+
* else the `id` query parameter; at most 200 characters), else by its socket id; its requests carry it as
|
|
51
|
+
* `ServiceContext.clientId`. The events pushed by `Entrypoint.notify` are emitted to every socket of the client, those
|
|
52
|
+
* of `Entrypoint.broadcast` to every socket of the namespace, with the event's arguments as the emit's arguments.
|
|
53
|
+
*
|
|
54
|
+
* When a socket disconnects, its requests in progress are aborted (their abort signal fires). When the entrypoint
|
|
55
|
+
* stops, the new requests are answered with an `EntrypointStoppedException` error; once the requests in progress end,
|
|
56
|
+
* the socket.io server is closed (the sockets disconnected, with the attached HTTP server left open).
|
|
57
|
+
* @param options The adapter options; by default it listens on port 3000 of all the interfaces with a server of its own.
|
|
58
|
+
* @returns The adapter, to give to the `service` option of `buildEntrypoints`.
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import {Application} from 'lakutata'
|
|
62
|
+
* import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
|
|
63
|
+
* import {ServiceAction} from 'lakutata/decorator/ctrl'
|
|
64
|
+
* import {socketIOServiceEntrypoint} from 'lakutata/adapter/socket.io'
|
|
65
|
+
* import {io} from 'socket.io-client'
|
|
66
|
+
*
|
|
67
|
+
* class EchoController extends Controller {
|
|
68
|
+
* @ServiceAction({cmd: 'echo'})
|
|
69
|
+
* public async echo(input: {text: string}): Promise<string> {
|
|
70
|
+
* return input.text
|
|
71
|
+
* }
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* Application.run({
|
|
75
|
+
* id: 'echo.service',
|
|
76
|
+
* name: 'Echo',
|
|
77
|
+
* components: {
|
|
78
|
+
* entrypoint: buildEntrypoints({
|
|
79
|
+
* controllers: [EchoController],
|
|
80
|
+
* service: socketIOServiceEntrypoint({port: 3000, options: {cors: {origin: '*'}}})
|
|
81
|
+
* })
|
|
82
|
+
* }
|
|
83
|
+
* })
|
|
84
|
+
*
|
|
85
|
+
* //A client, identified as client-1 (the events pushed by entrypoint.notify('client-1', 'progress', 50) reach it)
|
|
86
|
+
* const socket = io('http://localhost:3000', {auth: {id: 'client-1'}})
|
|
87
|
+
* socket.on('progress', (percent: number): void => console.log(`${percent}%`))
|
|
88
|
+
* socket.emit('message', {cmd: 'echo', text: 'hi'}, (response: {success: boolean, error: unknown, payload: unknown}): void => {
|
|
89
|
+
* //{success: true, error: null, payload: 'hi'}
|
|
90
|
+
* console.log(response)
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export function socketIOServiceEntrypoint(options = {}) {
|
|
95
|
+
const event = options.event ?? 'message';
|
|
96
|
+
let io;
|
|
97
|
+
let namespace;
|
|
98
|
+
let ownServer;
|
|
99
|
+
let sharedServer = false;
|
|
100
|
+
let stopping = false;
|
|
101
|
+
let onConnection;
|
|
102
|
+
//The sockets of each client (a client may connect several times)
|
|
103
|
+
const clients = new Map();
|
|
104
|
+
//The removal of the listeners the adapter added to each socket (the sockets of a given io outlive the adapter)
|
|
105
|
+
const detachers = new Map();
|
|
106
|
+
return {
|
|
107
|
+
channel: 'service',
|
|
108
|
+
definition: ServiceChannel,
|
|
109
|
+
name: options.name,
|
|
110
|
+
middlewares: options.middlewares,
|
|
111
|
+
start: async (runtime) => {
|
|
112
|
+
const attachTo = typeof options.server === 'function' ? options.server() : options.server;
|
|
113
|
+
if (options.io) {
|
|
114
|
+
io = options.io;
|
|
115
|
+
}
|
|
116
|
+
else if (attachTo) {
|
|
117
|
+
io = new SocketIOServer(attachTo, options.options);
|
|
118
|
+
sharedServer = true;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
ownServer = createServer();
|
|
122
|
+
io = new SocketIOServer(ownServer, options.options);
|
|
123
|
+
}
|
|
124
|
+
namespace = io.of(options.namespace ?? '/');
|
|
125
|
+
onConnection = (socket) => {
|
|
126
|
+
const clientId = clientIdOf(socket);
|
|
127
|
+
if (!clients.has(clientId))
|
|
128
|
+
clients.set(clientId, new Set());
|
|
129
|
+
clients.get(clientId).add(socket);
|
|
130
|
+
//The requests of a socket in progress, aborted when it disconnects
|
|
131
|
+
const requests = new Set();
|
|
132
|
+
const onDisconnect = () => {
|
|
133
|
+
requests.forEach((request) => request.abort());
|
|
134
|
+
clients.get(clientId)?.delete(socket);
|
|
135
|
+
if (!clients.get(clientId)?.size)
|
|
136
|
+
clients.delete(clientId);
|
|
137
|
+
detachers.delete(socket);
|
|
138
|
+
};
|
|
139
|
+
const onRequest = async (data, acknowledge) => {
|
|
140
|
+
const reply = (response) => {
|
|
141
|
+
if (typeof acknowledge === 'function')
|
|
142
|
+
acknowledge(response);
|
|
143
|
+
};
|
|
144
|
+
if (stopping)
|
|
145
|
+
return reply(ServiceResponseCodec.encodeError(new EntrypointStoppedException('The entrypoint is stopping')));
|
|
146
|
+
const request = new AbortController();
|
|
147
|
+
requests.add(request);
|
|
148
|
+
try {
|
|
149
|
+
const context = new ServiceContext({ data: data && typeof data === 'object' ? data : {}, socket: socket, clientId: clientId });
|
|
150
|
+
reply(ServiceResponseCodec.encode(await runtime.dispatch(context, { signal: request.signal }), false));
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
reply(ServiceResponseCodec.encodeError(error));
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
requests.delete(request);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
socket.once('disconnect', onDisconnect);
|
|
160
|
+
const onEvent = (data, acknowledge) => void onRequest(data, acknowledge);
|
|
161
|
+
socket.on(event, onEvent);
|
|
162
|
+
detachers.set(socket, () => {
|
|
163
|
+
socket.off('disconnect', onDisconnect);
|
|
164
|
+
socket.off(event, onEvent);
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
namespace.on('connection', onConnection);
|
|
168
|
+
if (ownServer) {
|
|
169
|
+
//The adapter starts once its server listens (an occupied port fails the start)
|
|
170
|
+
await new Promise((resolve, reject) => {
|
|
171
|
+
ownServer.once('error', reject).listen(options.port ?? 3000, options.host, () => {
|
|
172
|
+
ownServer.off('error', reject);
|
|
173
|
+
resolve();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
await options.listening?.(io, ownServer ?? attachTo);
|
|
178
|
+
},
|
|
179
|
+
stop: () => {
|
|
180
|
+
//The new requests are refused, the requests in progress end
|
|
181
|
+
stopping = true;
|
|
182
|
+
},
|
|
183
|
+
notify: (clientId, name, args) => {
|
|
184
|
+
const sockets = clients.get(clientId);
|
|
185
|
+
sockets?.forEach((socket) => socket.emit(name, ...args));
|
|
186
|
+
return !!sockets?.size;
|
|
187
|
+
},
|
|
188
|
+
broadcast: (name, args) => {
|
|
189
|
+
if (!namespace)
|
|
190
|
+
return 0;
|
|
191
|
+
namespace.emit(name, ...args);
|
|
192
|
+
return clients.size;
|
|
193
|
+
},
|
|
194
|
+
close: async () => {
|
|
195
|
+
clients.clear();
|
|
196
|
+
if (namespace && onConnection)
|
|
197
|
+
namespace.off('connection', onConnection);
|
|
198
|
+
//The sockets connected keep no listener of the adapter (they stay connected to a given io)
|
|
199
|
+
detachers.forEach((detach) => detach());
|
|
200
|
+
detachers.clear();
|
|
201
|
+
if (!io || options.io)
|
|
202
|
+
return;
|
|
203
|
+
if (sharedServer) {
|
|
204
|
+
//The server is closed by its owner: the clients are disconnected and the transports closed
|
|
205
|
+
io.disconnectSockets(true);
|
|
206
|
+
io.engine.close();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
await new Promise((resolve) => void io.close(() => resolve()));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { getObjectPropertyPaths, uniqueArray } from '@lakutata/utils';
|
|
2
|
+
import { defineChannel } from './Channel.js';
|
|
3
|
+
import { PatternManager } from '../lib/pattern/PatternManager.js';
|
|
4
|
+
import { stringifyPattern } from '../lib/StringifyPattern.js';
|
|
5
|
+
import { prefixCommand, prefixPattern, prefixRoute } from '../lib/Prefix.js';
|
|
6
|
+
import { ContextType } from '../context/BaseContext.js';
|
|
7
|
+
import { ControllerActionNotFoundException } from '../exceptions/ControllerActionNotFoundException.js';
|
|
8
|
+
import { unsetValue } from '../lib/UnsetValue.js';
|
|
9
|
+
/**
|
|
10
|
+
* A lookup by exact key, falling back to the glob patterns
|
|
11
|
+
* @param entries
|
|
12
|
+
* @param key
|
|
13
|
+
* @param subject
|
|
14
|
+
*/
|
|
15
|
+
function exactMatcher(entries, key, subject) {
|
|
16
|
+
const exact = new Map();
|
|
17
|
+
const patterns = new PatternManager();
|
|
18
|
+
for (const entry of entries) {
|
|
19
|
+
exact.set(key(entry.details.pattern), entry);
|
|
20
|
+
patterns.add(entry.details.pattern, entry);
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
match: (context) => {
|
|
24
|
+
const pattern = subject(context);
|
|
25
|
+
const entry = exact.get(key(pattern)) ?? patterns.find(pattern);
|
|
26
|
+
return entry ? { entry: entry } : undefined;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The key of an HTTP action
|
|
32
|
+
* @param pattern
|
|
33
|
+
*/
|
|
34
|
+
function httpKey(pattern) {
|
|
35
|
+
return `${pattern.method}\u0000${pattern.route}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The HTTP channel, addressed by route and method
|
|
39
|
+
*/
|
|
40
|
+
export const HTTPChannel = defineChannel({
|
|
41
|
+
name: 'http',
|
|
42
|
+
kind: 'request',
|
|
43
|
+
contextType: ContextType.HTTP,
|
|
44
|
+
identify: (pattern) => pattern.route,
|
|
45
|
+
describe: (pattern) => ({ route: pattern.route }),
|
|
46
|
+
prefix: (prefix, pattern) => prefix.route === undefined ? undefined : { ...pattern, route: prefixRoute(prefix.route, pattern.route) },
|
|
47
|
+
createMatcher: (entries) => exactMatcher(entries, httpKey, (context) => ({
|
|
48
|
+
route: context.route,
|
|
49
|
+
method: context.method
|
|
50
|
+
})),
|
|
51
|
+
notFound: (context) => new ControllerActionNotFoundException('Route \'{route}\' not found', context)
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* The CLI channel, addressed by command
|
|
55
|
+
*/
|
|
56
|
+
export const CLIChannel = defineChannel({
|
|
57
|
+
name: 'cli',
|
|
58
|
+
kind: 'request',
|
|
59
|
+
contextType: ContextType.CLI,
|
|
60
|
+
identify: (pattern) => pattern.command,
|
|
61
|
+
describe: (pattern) => ({ command: pattern.command }),
|
|
62
|
+
prefix: (prefix, pattern) => prefix.command === undefined ? undefined : { command: prefixCommand(prefix.command, pattern.command) },
|
|
63
|
+
createMatcher: (entries) => exactMatcher(entries, (pattern) => pattern.command, (context) => ({ command: context.command })),
|
|
64
|
+
notFound: () => new ControllerActionNotFoundException('Command not found')
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* The service channel, addressed by pattern
|
|
68
|
+
*/
|
|
69
|
+
export const ServiceChannel = defineChannel({
|
|
70
|
+
name: 'service',
|
|
71
|
+
kind: 'request',
|
|
72
|
+
contextType: ContextType.SERVICE,
|
|
73
|
+
identify: (pattern) => stringifyPattern(pattern),
|
|
74
|
+
describe: (pattern) => ({ pattern: pattern }),
|
|
75
|
+
prefix: (prefix, pattern) => prefix.pattern === undefined ? undefined : prefixPattern(prefix.pattern, pattern),
|
|
76
|
+
createMatcher: (entries) => {
|
|
77
|
+
const patterns = new PatternManager();
|
|
78
|
+
for (const entry of entries)
|
|
79
|
+
patterns.add(entry.details.pattern, entry);
|
|
80
|
+
return {
|
|
81
|
+
match: (context) => {
|
|
82
|
+
const entry = patterns.find(context.input);
|
|
83
|
+
return entry ? { entry: entry } : undefined;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
//The keys of the pattern are not a part of the action input
|
|
88
|
+
prepare: (entry, context) => {
|
|
89
|
+
const patternPaths = getObjectPropertyPaths(entry.details.pattern);
|
|
90
|
+
patternPaths.forEach((path) => unsetValue(context.data, path));
|
|
91
|
+
const checkPatternPaths = uniqueArray(patternPaths
|
|
92
|
+
.map((path) => path.substring(0, path.lastIndexOf('.')))
|
|
93
|
+
.filter((path) => !!path));
|
|
94
|
+
checkPatternPaths.forEach((path) => {
|
|
95
|
+
let target = context.data;
|
|
96
|
+
path.split('.').forEach((key) => target = target[key] ? target[key] : undefined);
|
|
97
|
+
if (target && !Object.keys(target).length)
|
|
98
|
+
unsetValue(context.data, path);
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
notFound: () => new ControllerActionNotFoundException('Controller action not found')
|
|
102
|
+
});
|
|
103
|
+
/**
|
|
104
|
+
* The schedule channel: no external caller, each action is run by the timers of the schedule adapter.
|
|
105
|
+
* The address of an action is its schedule, with the key telling apart the actions of a same schedule
|
|
106
|
+
*/
|
|
107
|
+
export const ScheduleChannel = defineChannel({
|
|
108
|
+
name: 'schedule',
|
|
109
|
+
kind: 'trigger',
|
|
110
|
+
contextType: ContextType.SCHEDULE,
|
|
111
|
+
identify: (pattern) => `${pattern.schedule}#${pattern.key}`,
|
|
112
|
+
describe: (pattern) => {
|
|
113
|
+
const { key, ...schedule } = pattern;
|
|
114
|
+
void key;
|
|
115
|
+
return schedule;
|
|
116
|
+
},
|
|
117
|
+
createMatcher: (entries) => ({
|
|
118
|
+
//The runs are dispatched to their action: a context is matched by its schedule
|
|
119
|
+
match: (context) => {
|
|
120
|
+
const entry = entries.find((candidate) => candidate.details.pattern.schedule === context.schedule);
|
|
121
|
+
return entry ? { entry: entry } : undefined;
|
|
122
|
+
}
|
|
123
|
+
}),
|
|
124
|
+
notFound: (context) => new ControllerActionNotFoundException('No action is scheduled {schedule}', { schedule: context.schedule })
|
|
125
|
+
});
|