@logux/server 0.16.3 → 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
  */
@@ -319,6 +294,10 @@ interface Authorizer<
319
294
  /**
320
295
  * Return object with keys for meta to resend action to other users.
321
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
+ *
322
301
  * @param ctx Information about node, who create this action.
323
302
  * @param action The action data.
324
303
  * @param meta The action metadata.
@@ -609,7 +588,10 @@ interface ReportersArguments {
609
588
  }
610
589
  unsubscribed: SubscriptionReporter
611
590
  useless: ActionReporter
612
- duplicate: ActionReporter
591
+ duplicate: CleanReporter
592
+ destroyDetached: {
593
+ actions: number
594
+ }
613
595
  wrongChannel: SubscriptionReporter
614
596
  zombie: {
615
597
  nodeId: string
@@ -625,14 +607,10 @@ export interface Reporter {
625
607
 
626
608
  export type Resend =
627
609
  | {
628
- channel?: string
629
610
  channels?: string[]
630
- client?: string
631
611
  clients?: string[]
632
612
  excludeClients?: string[]
633
- node?: string
634
613
  nodes?: string[]
635
- user?: string
636
614
  users?: string[]
637
615
  }
638
616
  | string
@@ -707,6 +685,16 @@ export class BaseServer<
707
685
  * ```js
708
686
  * server.log.each(finder)
709
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
+ * ```
710
698
  */
711
699
  log: ServerLog
712
700
 
@@ -933,6 +921,13 @@ export class BaseServer<
933
921
  * }
934
922
  * })
935
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`.
936
931
  */
937
932
  http(
938
933
  method: string,
@@ -949,6 +944,24 @@ export class BaseServer<
949
944
  ) => boolean | Promise<boolean>
950
945
  ): void
951
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
+
952
965
  /**
953
966
  * Start WebSocket server and listen for clients.
954
967
  *
@@ -1148,7 +1161,8 @@ export class BaseServer<
1148
1161
  *
1149
1162
  * @param action New action to resend and process.
1150
1163
  * @param meta Action’s meta.
1151
- * @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.
1152
1166
  */
1153
1167
  process<TypeAction extends Action = AnyAction>(
1154
1168
  action: TypeAction,
@@ -1156,8 +1170,12 @@ export class BaseServer<
1156
1170
  ): Promise<Readonly<ServerMeta>>
1157
1171
 
1158
1172
  /**
1159
- * Send action, received by other server, to all clients of current server.
1160
- * 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.
1161
1179
  *
1162
1180
  * ```js
1163
1181
  * server.on('add', (action, meta) => {
@@ -1166,14 +1184,14 @@ export class BaseServer<
1166
1184
  * }
1167
1185
  * })
1168
1186
  * onReceivingFromOtherServer((action, meta) => {
1169
- * server.sendAction(action, meta)
1187
+ * server.sendWithoutProcess(action, meta)
1170
1188
  * })
1171
1189
  * ```
1172
1190
  *
1173
1191
  * @param action New action.
1174
1192
  * @param meta Action’s metadata.
1175
1193
  */
1176
- sendAction(action: Action, meta: ServerMeta): Promise<void> | void
1194
+ sendWithoutProcess(action: Action, meta: ServerMeta): Promise<void> | void
1177
1195
 
1178
1196
  /**
1179
1197
  * Change a way how server loads actions history for the client.
@@ -1192,6 +1210,9 @@ export class BaseServer<
1192
1210
  * Set `meta.added` to let the client ask only for newer actions after
1193
1211
  * the reconnect: the biggest `added` will be sent as the sync position.
1194
1212
  *
1213
+ * Like the default history, the returned actions are prefixed
1214
+ * by `logux/prepare`.
1215
+ *
1195
1216
  * @param loader Callback which loads list of actions and meta.
1196
1217
  */
1197
1218
  sendOnConnect(loader: ConnectLoader<Headers>): void
@@ -1205,8 +1226,9 @@ export class BaseServer<
1205
1226
  *
1206
1227
  * @param nodeId Node ID.
1207
1228
  * @param channel Channel name.
1229
+ * @returns Promise until `logux/subscribed` was published.
1208
1230
  */
1209
- subscribe(nodeId: string, channel: string): void
1231
+ subscribe(nodeId: string, channel: string): Promise<unknown>
1210
1232
 
1211
1233
  /**
1212
1234
  * @param actionCreator Action creator function.
@@ -1256,11 +1278,15 @@ export class BaseServer<
1256
1278
  * }
1257
1279
  * ```
1258
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
+ *
1259
1285
  * @param action The original action to undo.
1260
1286
  * @param meta The action’s metadata.
1261
1287
  * @param reason Optional code for reason. Default is `'error'`.
1262
1288
  * @param extra Extra fields to `logux/undo` action.
1263
- * @returns When action was saved to the log.
1289
+ * @returns When the answer was published.
1264
1290
  */
1265
1291
  undo(
1266
1292
  action: Action,
@@ -1270,8 +1296,8 @@ export class BaseServer<
1270
1296
  ): Promise<void>
1271
1297
 
1272
1298
  /**
1273
- * If you receive action with unknown type, this method will mark this action
1274
- * 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.
1275
1301
  *
1276
1302
  * If you didn’t set {@link Server#otherType},
1277
1303
  * Logux will call it automatically.