@lakutata/core 3.1.1 → 3.1.4
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/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/dist/cjs/exports/Core.d.ts +1 -0
- package/dist/cjs/exports/Core.js +1 -0
- package/dist/cjs/interfaces/IInterfaceListening.d.ts +159 -0
- package/dist/cjs/interfaces/IInterfaceListening.js +2 -0
- package/dist/esm/exports/Core.js +1 -0
- package/dist/esm/interfaces/IInterfaceListening.js +1 -0
- package/dist/types/exports/Core.d.ts +1 -0
- package/dist/types/interfaces/IInterfaceListening.d.ts +159 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -169,6 +169,7 @@ await container.destroy()
|
|
|
169
169
|
| `Before`, `After` | `lakutata/decorator/asst` | Functions run before and after a method |
|
|
170
170
|
| `PaginationSearchDTO`, `PaginationResultDTO`, `paginator`, `FlexibleDTO`, `toJsonSchema`, `fromJsonSchema` | `lakutata/dtos` | Pagination, open DTOs and JSON Schema conversion |
|
|
171
171
|
| `ConsoleLogger`, `ILogger` | `@lakutata/core/com/logger` | The logger interface and its console implementation |
|
|
172
|
+
| `IInterfaceListening`, `IInterfaceListener`, `IInterfaceListenOptions`, `IListeningAddress`, `NetworkInterfaceSelector` | `lakutata` | The contract of the listening on network interfaces, which the entrypoints use for their `interface` option and `@lakutata/netif` implements (types only) |
|
|
172
173
|
|
|
173
174
|
The typings document each declaration with examples.
|
|
174
175
|
|
package/README.zh-CN.md
CHANGED
|
@@ -158,6 +158,7 @@ await container.destroy()
|
|
|
158
158
|
| `Before`、`After` | `lakutata/decorator/asst` | 在方法之前和之后执行的函数 |
|
|
159
159
|
| `PaginationSearchDTO`、`PaginationResultDTO`、`paginator`、`FlexibleDTO`、`toJsonSchema`、`fromJsonSchema` | `lakutata/dtos` | 分页、接受任意字段的 DTO、JSON Schema 转换 |
|
|
160
160
|
| `ConsoleLogger`、`ILogger` | `@lakutata/core/com/logger` | 日志记录器接口及其控制台实现 |
|
|
161
|
+
| `IInterfaceListening`、`IInterfaceListener`、`IInterfaceListenOptions`、`IListeningAddress`、`NetworkInterfaceSelector` | `lakutata` | 按网口监听的接口:入口的 `interface` 选项通过它监听,`@lakutata/netif` 实现了它(只有类型) |
|
|
161
162
|
|
|
162
163
|
类型声明中的每一项都附有说明和示例。
|
|
163
164
|
|
package/dist/cjs/exports/Core.js
CHANGED
|
@@ -121,6 +121,7 @@ __exportStar(require("../types/ModuleOptions.js"), exports);
|
|
|
121
121
|
*/
|
|
122
122
|
__exportStar(require("../interfaces/IBaseObjectConstructor.js"), exports);
|
|
123
123
|
__exportStar(require("../interfaces/ILogger.js"), exports);
|
|
124
|
+
__exportStar(require("../interfaces/IInterfaceListening.js"), exports);
|
|
124
125
|
__exportStar(require("../interfaces/IConstructor.js"), exports);
|
|
125
126
|
|
|
126
127
|
//The default of the entry: the entry itself (scripts/package-build/cjs-package.mjs)
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { Server } from 'node:net';
|
|
2
|
+
/**
|
|
3
|
+
* A network interface, as an implementation of {@link IInterfaceListening} takes it: its name (`'eth1'`; on Windows the
|
|
4
|
+
* name of the network connection), its hardware address (`{mac: '52:54:00:12:34:56'}`) or its index (`{index: 3}`).
|
|
5
|
+
*/
|
|
6
|
+
export type NetworkInterfaceSelector = string | {
|
|
7
|
+
/**
|
|
8
|
+
* The hardware address of the interface.
|
|
9
|
+
*/
|
|
10
|
+
mac: string;
|
|
11
|
+
} | {
|
|
12
|
+
/**
|
|
13
|
+
* The index of the interface.
|
|
14
|
+
*/
|
|
15
|
+
index: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* An address a listener of a network interface can be reached at.
|
|
19
|
+
*/
|
|
20
|
+
export interface IListeningAddress {
|
|
21
|
+
/**
|
|
22
|
+
* The address (an IPv6 address without its scope).
|
|
23
|
+
*/
|
|
24
|
+
address: string;
|
|
25
|
+
/**
|
|
26
|
+
* `'IPv4'` or `'IPv6'`.
|
|
27
|
+
*/
|
|
28
|
+
family: 'IPv4' | 'IPv6';
|
|
29
|
+
/**
|
|
30
|
+
* The port.
|
|
31
|
+
*/
|
|
32
|
+
port: number;
|
|
33
|
+
/**
|
|
34
|
+
* IPv6 only: the scope of the address (the index of the interface for a link-local one), 0 for a global one.
|
|
35
|
+
*/
|
|
36
|
+
scopeid?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The options of {@link IInterfaceListening.listenOnAll}.
|
|
40
|
+
*/
|
|
41
|
+
export interface IInterfaceListenOptions {
|
|
42
|
+
/**
|
|
43
|
+
* The port; 0 lets the system choose one (for one interface only).
|
|
44
|
+
*/
|
|
45
|
+
port: number;
|
|
46
|
+
/**
|
|
47
|
+
* The families listened on.
|
|
48
|
+
* @default 'both'
|
|
49
|
+
*/
|
|
50
|
+
family?: 'IPv4' | 'IPv6' | 'both';
|
|
51
|
+
/**
|
|
52
|
+
* Refuse to listen where the connections reaching the addresses of the interface through another interface cannot
|
|
53
|
+
* be refused, and stop listening while they cannot. An implementation that never refuses them throws.
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
strict?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The listener of a network interface for a server: it accepts the connections of the interface and gives them to the
|
|
60
|
+
* server, whatever the addresses of the interface, again when they change and when the interface is unplugged and
|
|
61
|
+
* plugged back. The server itself does not listen. The `InterfaceListener` of `@lakutata/netif` implements it.
|
|
62
|
+
*/
|
|
63
|
+
export interface IInterfaceListener {
|
|
64
|
+
/**
|
|
65
|
+
* The interface listened on: its name (`undefined` while it does not exist), and whether it is the loopback one.
|
|
66
|
+
*/
|
|
67
|
+
readonly networkInterface: {
|
|
68
|
+
readonly name: string | undefined;
|
|
69
|
+
readonly loopback: boolean;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* The port, `undefined` until a port 0 is given one by the system.
|
|
73
|
+
*/
|
|
74
|
+
readonly port: number | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The addresses it can be reached at now: none while the interface does not exist or has no address.
|
|
77
|
+
*/
|
|
78
|
+
readonly addresses: IListeningAddress[];
|
|
79
|
+
/**
|
|
80
|
+
* Whether the interface is listened on now.
|
|
81
|
+
*/
|
|
82
|
+
readonly listening: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Whether the system refuses the connections that reach the addresses of the interface through another interface.
|
|
85
|
+
*/
|
|
86
|
+
readonly enforced: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Why `enforced` is `false`, when it is.
|
|
89
|
+
*/
|
|
90
|
+
readonly reason: string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* Whether it was closed.
|
|
93
|
+
*/
|
|
94
|
+
readonly closed: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Stop listening; the connections established are left to the server.
|
|
97
|
+
*/
|
|
98
|
+
close(): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* The interface is listened on, again after a change.
|
|
101
|
+
* @param event `'listening'`.
|
|
102
|
+
* @param listener Called without argument.
|
|
103
|
+
*/
|
|
104
|
+
on(event: 'listening', listener: () => void): this;
|
|
105
|
+
/**
|
|
106
|
+
* A failure of listening again, retried by the listener; with `strict`, the guarantee lost.
|
|
107
|
+
* @param event `'error'`.
|
|
108
|
+
* @param listener Called with the error.
|
|
109
|
+
*/
|
|
110
|
+
on(event: 'error', listener: (error: Error) => void): this;
|
|
111
|
+
/**
|
|
112
|
+
* `enforced` changed.
|
|
113
|
+
* @param event `'enforced'`.
|
|
114
|
+
* @param listener Called with its new value.
|
|
115
|
+
*/
|
|
116
|
+
on(event: 'enforced', listener: (enforced: boolean) => void): this;
|
|
117
|
+
/**
|
|
118
|
+
* The listener was closed.
|
|
119
|
+
* @param event `'close'`.
|
|
120
|
+
* @param listener Called without argument.
|
|
121
|
+
*/
|
|
122
|
+
on(event: 'close', listener: () => void): this;
|
|
123
|
+
/**
|
|
124
|
+
* Remove a listener of an event.
|
|
125
|
+
* @param event The event.
|
|
126
|
+
* @param listener The listener given to `on()`.
|
|
127
|
+
*/
|
|
128
|
+
off(event: 'listening' | 'error' | 'enforced' | 'close', listener: (...args: any[]) => void): this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Listening on network interfaces instead of addresses: what the entrypoints use for their `interface` option. The
|
|
132
|
+
* package `@lakutata/netif` implements it, and is the one used by default; another implementation is given in the
|
|
133
|
+
* `interfaceListening` option of the entrypoints.
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* import type {IInterfaceListener, IInterfaceListening} from 'lakutata'
|
|
137
|
+
* import {listenOnAll} from '@lakutata/netif'
|
|
138
|
+
*
|
|
139
|
+
* //An implementation that writes each listener made before giving it to the entrypoint
|
|
140
|
+
* const logged: IInterfaceListening = {
|
|
141
|
+
* listenOnAll: async (selectors, server, options): Promise<IInterfaceListener[]> => {
|
|
142
|
+
* const listeners: IInterfaceListener[] = await listenOnAll(selectors, server, options)
|
|
143
|
+
* for (const listener of listeners) console.log(listener.networkInterface.name, listener.addresses)
|
|
144
|
+
* return listeners
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export interface IInterfaceListening {
|
|
150
|
+
/**
|
|
151
|
+
* Listen on interfaces for a server, one listener by interface; when one cannot be listened on, the others are
|
|
152
|
+
* closed and the error is thrown. An interface that does not exist yet is listened on once it appears.
|
|
153
|
+
* @param selectors The interfaces; several need a port other than 0.
|
|
154
|
+
* @param server The server the connections are given to.
|
|
155
|
+
* @param options The port, the families, and whether the other interfaces must be refused.
|
|
156
|
+
* @returns The listeners, in the order of the interfaces.
|
|
157
|
+
*/
|
|
158
|
+
listenOnAll(selectors: NetworkInterfaceSelector | NetworkInterfaceSelector[], server: Server, options: IInterfaceListenOptions): Promise<IInterfaceListener[]>;
|
|
159
|
+
}
|
package/dist/esm/exports/Core.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { Server } from 'node:net';
|
|
2
|
+
/**
|
|
3
|
+
* A network interface, as an implementation of {@link IInterfaceListening} takes it: its name (`'eth1'`; on Windows the
|
|
4
|
+
* name of the network connection), its hardware address (`{mac: '52:54:00:12:34:56'}`) or its index (`{index: 3}`).
|
|
5
|
+
*/
|
|
6
|
+
export type NetworkInterfaceSelector = string | {
|
|
7
|
+
/**
|
|
8
|
+
* The hardware address of the interface.
|
|
9
|
+
*/
|
|
10
|
+
mac: string;
|
|
11
|
+
} | {
|
|
12
|
+
/**
|
|
13
|
+
* The index of the interface.
|
|
14
|
+
*/
|
|
15
|
+
index: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* An address a listener of a network interface can be reached at.
|
|
19
|
+
*/
|
|
20
|
+
export interface IListeningAddress {
|
|
21
|
+
/**
|
|
22
|
+
* The address (an IPv6 address without its scope).
|
|
23
|
+
*/
|
|
24
|
+
address: string;
|
|
25
|
+
/**
|
|
26
|
+
* `'IPv4'` or `'IPv6'`.
|
|
27
|
+
*/
|
|
28
|
+
family: 'IPv4' | 'IPv6';
|
|
29
|
+
/**
|
|
30
|
+
* The port.
|
|
31
|
+
*/
|
|
32
|
+
port: number;
|
|
33
|
+
/**
|
|
34
|
+
* IPv6 only: the scope of the address (the index of the interface for a link-local one), 0 for a global one.
|
|
35
|
+
*/
|
|
36
|
+
scopeid?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The options of {@link IInterfaceListening.listenOnAll}.
|
|
40
|
+
*/
|
|
41
|
+
export interface IInterfaceListenOptions {
|
|
42
|
+
/**
|
|
43
|
+
* The port; 0 lets the system choose one (for one interface only).
|
|
44
|
+
*/
|
|
45
|
+
port: number;
|
|
46
|
+
/**
|
|
47
|
+
* The families listened on.
|
|
48
|
+
* @default 'both'
|
|
49
|
+
*/
|
|
50
|
+
family?: 'IPv4' | 'IPv6' | 'both';
|
|
51
|
+
/**
|
|
52
|
+
* Refuse to listen where the connections reaching the addresses of the interface through another interface cannot
|
|
53
|
+
* be refused, and stop listening while they cannot. An implementation that never refuses them throws.
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
strict?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The listener of a network interface for a server: it accepts the connections of the interface and gives them to the
|
|
60
|
+
* server, whatever the addresses of the interface, again when they change and when the interface is unplugged and
|
|
61
|
+
* plugged back. The server itself does not listen. The `InterfaceListener` of `@lakutata/netif` implements it.
|
|
62
|
+
*/
|
|
63
|
+
export interface IInterfaceListener {
|
|
64
|
+
/**
|
|
65
|
+
* The interface listened on: its name (`undefined` while it does not exist), and whether it is the loopback one.
|
|
66
|
+
*/
|
|
67
|
+
readonly networkInterface: {
|
|
68
|
+
readonly name: string | undefined;
|
|
69
|
+
readonly loopback: boolean;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* The port, `undefined` until a port 0 is given one by the system.
|
|
73
|
+
*/
|
|
74
|
+
readonly port: number | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The addresses it can be reached at now: none while the interface does not exist or has no address.
|
|
77
|
+
*/
|
|
78
|
+
readonly addresses: IListeningAddress[];
|
|
79
|
+
/**
|
|
80
|
+
* Whether the interface is listened on now.
|
|
81
|
+
*/
|
|
82
|
+
readonly listening: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Whether the system refuses the connections that reach the addresses of the interface through another interface.
|
|
85
|
+
*/
|
|
86
|
+
readonly enforced: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Why `enforced` is `false`, when it is.
|
|
89
|
+
*/
|
|
90
|
+
readonly reason: string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* Whether it was closed.
|
|
93
|
+
*/
|
|
94
|
+
readonly closed: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Stop listening; the connections established are left to the server.
|
|
97
|
+
*/
|
|
98
|
+
close(): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* The interface is listened on, again after a change.
|
|
101
|
+
* @param event `'listening'`.
|
|
102
|
+
* @param listener Called without argument.
|
|
103
|
+
*/
|
|
104
|
+
on(event: 'listening', listener: () => void): this;
|
|
105
|
+
/**
|
|
106
|
+
* A failure of listening again, retried by the listener; with `strict`, the guarantee lost.
|
|
107
|
+
* @param event `'error'`.
|
|
108
|
+
* @param listener Called with the error.
|
|
109
|
+
*/
|
|
110
|
+
on(event: 'error', listener: (error: Error) => void): this;
|
|
111
|
+
/**
|
|
112
|
+
* `enforced` changed.
|
|
113
|
+
* @param event `'enforced'`.
|
|
114
|
+
* @param listener Called with its new value.
|
|
115
|
+
*/
|
|
116
|
+
on(event: 'enforced', listener: (enforced: boolean) => void): this;
|
|
117
|
+
/**
|
|
118
|
+
* The listener was closed.
|
|
119
|
+
* @param event `'close'`.
|
|
120
|
+
* @param listener Called without argument.
|
|
121
|
+
*/
|
|
122
|
+
on(event: 'close', listener: () => void): this;
|
|
123
|
+
/**
|
|
124
|
+
* Remove a listener of an event.
|
|
125
|
+
* @param event The event.
|
|
126
|
+
* @param listener The listener given to `on()`.
|
|
127
|
+
*/
|
|
128
|
+
off(event: 'listening' | 'error' | 'enforced' | 'close', listener: (...args: any[]) => void): this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Listening on network interfaces instead of addresses: what the entrypoints use for their `interface` option. The
|
|
132
|
+
* package `@lakutata/netif` implements it, and is the one used by default; another implementation is given in the
|
|
133
|
+
* `interfaceListening` option of the entrypoints.
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* import type {IInterfaceListener, IInterfaceListening} from 'lakutata'
|
|
137
|
+
* import {listenOnAll} from '@lakutata/netif'
|
|
138
|
+
*
|
|
139
|
+
* //An implementation that writes each listener made before giving it to the entrypoint
|
|
140
|
+
* const logged: IInterfaceListening = {
|
|
141
|
+
* listenOnAll: async (selectors, server, options): Promise<IInterfaceListener[]> => {
|
|
142
|
+
* const listeners: IInterfaceListener[] = await listenOnAll(selectors, server, options)
|
|
143
|
+
* for (const listener of listeners) console.log(listener.networkInterface.name, listener.addresses)
|
|
144
|
+
* return listeners
|
|
145
|
+
* }
|
|
146
|
+
* }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export interface IInterfaceListening {
|
|
150
|
+
/**
|
|
151
|
+
* Listen on interfaces for a server, one listener by interface; when one cannot be listened on, the others are
|
|
152
|
+
* closed and the error is thrown. An interface that does not exist yet is listened on once it appears.
|
|
153
|
+
* @param selectors The interfaces; several need a port other than 0.
|
|
154
|
+
* @param server The server the connections are given to.
|
|
155
|
+
* @param options The port, the families, and whether the other interfaces must be refused.
|
|
156
|
+
* @returns The listeners, in the order of the interfaces.
|
|
157
|
+
*/
|
|
158
|
+
listenOnAll(selectors: NetworkInterfaceSelector | NetworkInterfaceSelector[], server: Server, options: IInterfaceListenOptions): Promise<IInterfaceListener[]>;
|
|
159
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lakutata/core",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.4",
|
|
4
4
|
"description": "The core of the lakutata framework: the IoC container and dependency injection, the object model with asynchronous construction, and DTO schema validation with the API of joi.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lakutata",
|