plugins: backend event: newSocket
Massimo Melina committed
Oct 12, 2024 at 14:24 UTC
b4270ad7ea0f61d8412dd7f85924b0e168d699b4
4 files changed
+14
-6
dev-plugins.md
+9
-5
@@ -445,8 +445,8 @@ api.events.on('deleting', async () => your-code-here)
445
446
### Stop, the way you prevent default behavior
447
448
-Some events allow you to stop their default behavior, by returning `api.events.stop`.
449
-This is reported in the list below with the word "stoppable".
448
+Some events allow you to stop their default behavior, by returning `api.events.preventDefault`.
449
+This is reported in the list below with the word "preventable".
450
451
```js
452
api.events.on('deleting', ({ node }) => node.source.endsWith('.jpg'))
@@ -462,7 +462,7 @@ This section is still partially documented, and you may need to have a look at t
462
- parameters: { node, ctx }
463
- called just before trying to delete a file or folder (which still may not exist and fail)
464
- async supported
465
- - stoppable
465
+ - preventable
466
- `login`
467
- `logout`
468
- `attemptingLogin`
@@ -488,11 +488,14 @@ This section is still partially documented, and you may need to have a look at t
488
- `pluginStarted`
489
- `uploadStart`
490
- parameters: { ctx, writeStream }
491
- - stoppable
491
+ - preventable
492
- return: callback to call when upload is finished
493
- `uploadFinished`
494
- `publicIpsChanged`
495
- parameters: { IPs, IP4, IP6, IPX }
496
+- `newSocket`
497
+ - parameters: { socket,ip }
498
+ - preventable
499
500
# Notifications (backend-to-frontend events)
501
@@ -658,7 +661,7 @@ If you want to override a text regardless of the language, use the special langu
661
662
## API version history
663
661
-- 9.4 (v0.54.0)
664
+- 9.5 (v0.54.0)
665
- frontend event: showPlay
666
- api.addBlock
667
- api.misc
@@ -671,6 +674,7 @@ If you want to override a text regardless of the language, use the special langu
674
- frontend event: appendMenuBar
675
- config.helperText: basic md formatting
676
- HFS.onEvent.setOrder
677
+ - backend event: newSocket
678
- 8.891 (v0.53.0)
679
- api.openDb
680
- frontend event: menuZip
src/connections.ts
+3
@@ -40,6 +40,9 @@ export function normalizeIp(ip: string) {
40
const all: Connection[] = []
41
42
export function newConnection(socket: Socket) {
43
+ const ip = normalizeIp(socket.remoteAddress || '')
44
+ if (events.emit('newSocket', { socket, ip })?.isDefaultPrevented())
45
+ return socket.destroy()
46
new Connection(socket)
47
}
48
src/const.ts
+1
-1
@@ -7,7 +7,7 @@ import _ from 'lodash'
7
import { basename, dirname, join } from 'path'
8
export * from './cross-const'
9
10
-export const API_VERSION = 9.4
10
+export const API_VERSION = 9.5
11
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12
export const HFS_REPO = 'rejetto/hfs'
13
src/events.ts
+1
@@ -7,6 +7,7 @@ const LISTENERS_SUFFIX = '\0listeners'
7
export class BetterEventEmitter {
8
protected listeners = new Map<string, Listeners>()
9
preventDefault = Symbol()
10
+ stop = this.preventDefault // legacy pre-0.54 (introduced in 0.53)
11
on(event: string | string[], listener: Listener, { warnAfter=10 }={}) {
12
if (typeof event === 'string')
13
event = [event]