@logux/server 0.16.2 → 0.17.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.
@@ -66,21 +66,11 @@ interface ConnectLoader<Headers extends object = unknown> {
66
66
  type ServerNodeConstructor = new (...args: unknown[]) => ServerNode
67
67
 
68
68
  export interface ServerMeta extends Meta {
69
- /**
70
- * All nodes subscribed to channel will receive the action.
71
- */
72
- channel?: string
73
-
74
69
  /**
75
70
  * All nodes subscribed to listed channels will receive the action.
76
71
  */
77
72
  channels?: string[]
78
73
 
79
- /**
80
- * All nodes with listed client ID will receive the action.
81
- */
82
- client?: string
83
-
84
74
  /**
85
75
  * All nodes with listed client IDs will receive the action.
86
76
  */
@@ -91,11 +81,6 @@ export interface ServerMeta extends Meta {
91
81
  */
92
82
  excludeClients?: string[]
93
83
 
94
- /**
95
- * Node with listed node ID will receive the action.
96
- */
97
- node?: string
98
-
99
84
  /**
100
85
  * All nodes with listed node IDs will receive the action.
101
86
  */
@@ -106,16 +91,6 @@ export interface ServerMeta extends Meta {
106
91
  */
107
92
  server: string
108
93
 
109
- /**
110
- * Action processing status
111
- */
112
- status?: 'error' | 'processed' | 'waiting'
113
-
114
- /**
115
- * All nodes with listed user ID will receive the action.
116
- */
117
- user?: string
118
-
119
94
  /**
120
95
  * All nodes with listed user IDs will receive the action.
121
96
  */
@@ -238,6 +213,13 @@ export interface BaseServerOptions {
238
213
  */
239
214
  subprotocol?: number
240
215
 
216
+ /**
217
+ * How long an action can stay in the processing queue of the client
218
+ * before the server answers `logux/undo` and moves to the next action.
219
+ * `300000` (5 minutes) by default, `0` disables it.
220
+ */
221
+ queueTimeout?: number
222
+
241
223
  /**
242
224
  * How many actions could be sent in a single message. `100` by default.
243
225
  *
@@ -312,6 +294,10 @@ interface Authorizer<
312
294
  /**
313
295
  * Return object with keys for meta to resend action to other users.
314
296
  *
297
+ * It is called only for the actions from the clients and
298
+ * from {@link BaseServer#process}. Facts, which were added
299
+ * by `Server#log.add()`, are routed by their meta alone.
300
+ *
315
301
  * @param ctx Information about node, who create this action.
316
302
  * @param action The action data.
317
303
  * @param meta The action metadata.
@@ -602,6 +588,10 @@ interface ReportersArguments {
602
588
  }
603
589
  unsubscribed: SubscriptionReporter
604
590
  useless: ActionReporter
591
+ duplicate: CleanReporter
592
+ destroyDetached: {
593
+ actions: number
594
+ }
605
595
  wrongChannel: SubscriptionReporter
606
596
  zombie: {
607
597
  nodeId: string
@@ -617,14 +607,10 @@ export interface Reporter {
617
607
 
618
608
  export type Resend =
619
609
  | {
620
- channel?: string
621
610
  channels?: string[]
622
- client?: string
623
611
  clients?: string[]
624
612
  excludeClients?: string[]
625
- node?: string
626
613
  nodes?: string[]
627
- user?: string
628
614
  users?: string[]
629
615
  }
630
616
  | string
@@ -699,6 +685,16 @@ export class BaseServer<
699
685
  * ```js
700
686
  * server.log.each(finder)
701
687
  * ```
688
+ *
689
+ * Adding an action delivers it by `meta.channels`, `users`, `clients`,
690
+ * `nodes`. Callbacks from {@link BaseServer#type} are not called,
691
+ * use {@link BaseServer#process} for that.
692
+ *
693
+ * ```js
694
+ * server.log.add({ type: 'users/renamed', userId, name }, {
695
+ * channels: [`users/${userId}`]
696
+ * })
697
+ * ```
702
698
  */
703
699
  log: ServerLog
704
700
 
@@ -925,6 +921,13 @@ export class BaseServer<
925
921
  * }
926
922
  * })
927
923
  * ```
924
+ *
925
+ * The URL can contain `*` to match the rest of the path, including `/`.
926
+ *
927
+ * Listeners are checked in the order they were added, so add the specific
928
+ * URLs before the patterns. Adding a listener for the same method and URL
929
+ * replaces the previous one: it allows to override the built-in pages
930
+ * like `/` or `/health`.
928
931
  */
929
932
  http(
930
933
  method: string,
@@ -941,6 +944,24 @@ export class BaseServer<
941
944
  ) => boolean | Promise<boolean>
942
945
  ): void
943
946
 
947
+ /**
948
+ * Replace the default `404 Not found` answer for HTTP requests, which
949
+ * were not processed by `Server#http()` listeners.
950
+ *
951
+ * ```js
952
+ * server.httpNotFound((req, res) => {
953
+ * res.writeHead(404, { 'Content-Type': 'application/json' })
954
+ * res.end('{"error":"Not found"}')
955
+ * })
956
+ * ```
957
+ */
958
+ httpNotFound(
959
+ listener: (
960
+ req: IncomingMessage,
961
+ res: ServerResponse
962
+ ) => Promise<void> | void
963
+ ): void
964
+
944
965
  /**
945
966
  * Start WebSocket server and listen for clients.
946
967
  *
@@ -1140,7 +1161,8 @@ export class BaseServer<
1140
1161
  *
1141
1162
  * @param action New action to resend and process.
1142
1163
  * @param meta Action’s meta.
1143
- * @returns Promise until new action will be resend to clients and processed.
1164
+ * @returns Promise with the action’s meta. It will be rejected
1165
+ * with the error of the failed callback.
1144
1166
  */
1145
1167
  process<TypeAction extends Action = AnyAction>(
1146
1168
  action: TypeAction,
@@ -1148,8 +1170,12 @@ export class BaseServer<
1148
1170
  ): Promise<Readonly<ServerMeta>>
1149
1171
 
1150
1172
  /**
1151
- * Send action, received by other server, to all clients of current server.
1152
- * This method is for multi-server configuration only.
1173
+ * Send an action, which was already processed by another server,
1174
+ * to the clients of this server. The action is not written to the log
1175
+ * and no callback from {@link BaseServer#type} is called.
1176
+ *
1177
+ * This method is for multi-server configuration only. Use
1178
+ * `Server#log.add()` to also store the action in this server’s log.
1153
1179
  *
1154
1180
  * ```js
1155
1181
  * server.on('add', (action, meta) => {
@@ -1158,14 +1184,14 @@ export class BaseServer<
1158
1184
  * }
1159
1185
  * })
1160
1186
  * onReceivingFromOtherServer((action, meta) => {
1161
- * server.sendAction(action, meta)
1187
+ * server.sendWithoutProcess(action, meta)
1162
1188
  * })
1163
1189
  * ```
1164
1190
  *
1165
1191
  * @param action New action.
1166
1192
  * @param meta Action’s metadata.
1167
1193
  */
1168
- sendAction(action: Action, meta: ServerMeta): Promise<void> | void
1194
+ sendWithoutProcess(action: Action, meta: ServerMeta): Promise<void> | void
1169
1195
 
1170
1196
  /**
1171
1197
  * Change a way how server loads actions history for the client.
@@ -1184,6 +1210,9 @@ export class BaseServer<
1184
1210
  * Set `meta.added` to let the client ask only for newer actions after
1185
1211
  * the reconnect: the biggest `added` will be sent as the sync position.
1186
1212
  *
1213
+ * Like the default history, the returned actions are prefixed
1214
+ * by `logux/prepare`.
1215
+ *
1187
1216
  * @param loader Callback which loads list of actions and meta.
1188
1217
  */
1189
1218
  sendOnConnect(loader: ConnectLoader<Headers>): void
@@ -1197,8 +1226,9 @@ export class BaseServer<
1197
1226
  *
1198
1227
  * @param nodeId Node ID.
1199
1228
  * @param channel Channel name.
1229
+ * @returns Promise until `logux/subscribed` was published.
1200
1230
  */
1201
- subscribe(nodeId: string, channel: string): void
1231
+ subscribe(nodeId: string, channel: string): Promise<unknown>
1202
1232
 
1203
1233
  /**
1204
1234
  * @param actionCreator Action creator function.
@@ -1248,11 +1278,15 @@ export class BaseServer<
1248
1278
  * }
1249
1279
  * ```
1250
1280
  *
1281
+ * During the action processing it sets the outcome of the current task:
1282
+ * the callback can not answer `logux/processed` after it. Outside
1283
+ * of the processing it publishes the compensating action.
1284
+ *
1251
1285
  * @param action The original action to undo.
1252
1286
  * @param meta The action’s metadata.
1253
1287
  * @param reason Optional code for reason. Default is `'error'`.
1254
1288
  * @param extra Extra fields to `logux/undo` action.
1255
- * @returns When action was saved to the log.
1289
+ * @returns When the answer was published.
1256
1290
  */
1257
1291
  undo(
1258
1292
  action: Action,
@@ -1262,8 +1296,8 @@ export class BaseServer<
1262
1296
  ): Promise<void>
1263
1297
 
1264
1298
  /**
1265
- * If you receive action with unknown type, this method will mark this action
1266
- * with `error` status and undo it on the clients.
1299
+ * If you receive action with unknown type, this method will undo it
1300
+ * on the clients.
1267
1301
  *
1268
1302
  * If you didn’t set {@link Server#otherType},
1269
1303
  * Logux will call it automatically.