@kevisual/router 0.0.26-alpha.4 → 0.0.26-alpha.5

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.26-alpha.4",
4
+ "version": "0.0.26-alpha.5",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -0,0 +1,33 @@
1
+ export class ServerTimer {
2
+ updatedAt: number;
3
+ timer: any;
4
+ timeout: number;
5
+ onTimeout: any;
6
+ interval = 10 * 1000;
7
+ constructor(opts?: { timeout?: number }) {
8
+ this.timeout = opts?.timeout || 15 * 60 * 1000;
9
+ this.run();
10
+ }
11
+ startTimer() {
12
+ const that = this;
13
+ if (this.timer) {
14
+ clearInterval(this.timer);
15
+ }
16
+ this.timer = setInterval(() => {
17
+ const updatedAt = Date.now();
18
+ const timeout = that.timeout;
19
+ const onTimeout = that.onTimeout;
20
+ const isExpired = updatedAt - that.updatedAt > timeout;
21
+ if (isExpired) {
22
+ onTimeout?.();
23
+ clearInterval(that.timer);
24
+ that.timer = null;
25
+ }
26
+ }, that.interval);
27
+ }
28
+
29
+ run(): number {
30
+ this.updatedAt = Date.now();
31
+ return this.updatedAt;
32
+ }
33
+ }
@@ -3,12 +3,15 @@ import { QueryRouterServer } from '../route.ts';
3
3
  import { getRuntime } from './runtime.ts';
4
4
  import { runFirstCheck } from './listen/run-check.ts';
5
5
  import { cleanup } from './listen/cleanup.ts';
6
+ import { ServerTimer } from './listen/server-time.ts';
6
7
 
7
8
  type ListenSocketOptions = {
8
9
  path?: string;
9
10
  app?: QueryRouterServer;
10
11
  pidPath?: string;
12
+ timeout?: number;
11
13
  };
14
+
12
15
  const server = async (req, app: QueryRouterServer) => {
13
16
  const runtime = getRuntime();
14
17
  let data;
@@ -17,21 +20,36 @@ const server = async (req, app: QueryRouterServer) => {
17
20
  } else {
18
21
  data = await parseBody(req);
19
22
  }
20
-
23
+ // @ts-ignore
24
+ const serverTimer = app.serverTimer;
25
+ if (serverTimer) {
26
+ serverTimer?.run?.();
27
+ }
21
28
  const result = await app.queryRoute(data as any);
22
29
  const response = new Response(JSON.stringify(result));
23
30
  response.headers.set('Content-Type', 'application/json');
24
31
  return response;
25
32
  };
26
-
33
+ export const closeListenSocket = () => {
34
+ console.log('Closing listen socket');
35
+ process.emit('SIGINT');
36
+ };
37
+ export const serverTimer = new ServerTimer();
27
38
  export const listenSocket = async (options?: ListenSocketOptions) => {
28
39
  const path = options?.path || './app.sock';
29
40
  const pidPath = options?.pidPath || './app.pid';
41
+ const timeout = options?.timeout || 10 * 1000; // 10 seconds
30
42
  const runtime = getRuntime();
43
+
44
+ serverTimer.timeout = timeout;
45
+ serverTimer.startTimer();
46
+ serverTimer.onTimeout = closeListenSocket;
47
+
31
48
  let app = options?.app || globalThis.context?.app;
32
49
  if (!app) {
33
50
  app = new QueryRouterServer();
34
51
  }
52
+ app.serverTimer = serverTimer;
35
53
  await runFirstCheck(path, pidPath);
36
54
  let close = async () => {};
37
55
  cleanup({ path, close });
@@ -2,6 +2,7 @@ import { getRuntime } from './runtime.ts';
2
2
  import { glob } from './utils/glob.ts';
3
3
  type GlobOptions = {
4
4
  cwd?: string;
5
+ load?: (args?: any) => Promise<any>;
5
6
  };
6
7
 
7
8
  export const getMatchFiles = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}): Promise<string[]> => {
@@ -31,7 +32,7 @@ export const getMatchFiles = async (match: string = './*.ts', { cwd = process.cw
31
32
  return [];
32
33
  };
33
34
 
34
- export const loadTS = async (match: string = './*.ts', { cwd = process.cwd() }: GlobOptions = {}): Promise<any[]> => {
35
+ export const loadTS = async (match: string = './*.ts', { cwd = process.cwd(), load }: GlobOptions = {}): Promise<any[]> => {
35
36
  const files = await getMatchFiles(match, { cwd });
36
- return Promise.all(files.map((file) => import(file)));
37
+ return Promise.all(files.map((file) => (load ? load(file) : import(file))));
37
38
  };