Skip to content

Commit

Permalink
fix websocket status checks in base class
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Jan 19, 2024
1 parent 4df7023 commit ff9848e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
3 changes: 3 additions & 0 deletions apps/nocapd/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
docker-compose.yaml
yarn.lock
39 changes: 39 additions & 0 deletions apps/trawler/.nginx/nocap.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
user nginx;
worker_processes auto;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Logging Settings
access_log off;
error_log /var/log/nginx/error.log;

# Gzip Settings
gzip on;
gzip_disable "msie6";

# No server block is defined here
# This means Nginx will not listen on any port for incoming requests

# Additional configurations can be included if necessary
# include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
}

# Daemon off directive is useful when running Nginx in a foreground mode
# Especially useful in containerized environments like Docker
daemon off;
3 changes: 3 additions & 0 deletions packages/nocap/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.*
./**/*.test.js
wtf.js
30 changes: 28 additions & 2 deletions packages/nocap/adapters/default/WebsocketAdapterDefault/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "websocket-polyfill";
import WebSocket from 'ws';
import { WebsocketTor } from 'ws-tor'
// import { WebsocketTor } from 'ws-tor'

class WebsocketAdapterDefault {

Expand All @@ -20,8 +20,13 @@ class WebsocketAdapterDefault {
// if(this.$.results.network === 'tor')
// $ws = new WebsocketTor(this.$.url, { socksHost: this.$.config?.tor?.host, socksPort: this.$.config?.tor?.port })
// else

$ws = new WebSocket(this.$.url)

/*
If a WebSocket adapter doesn't use WS, don't set a ws obj
But you do need to create isConnecting, isConnected, isClosing, isClosing methods
*/
this.$.set('ws', $ws)
this.bind_events()
return deferred
Expand Down Expand Up @@ -95,8 +100,29 @@ class WebsocketAdapterDefault {
}
}

unsubscribe() {
this.$.ws.send(['CLOSE', subid])
}

/*
since this adapter uses `ws` library, these methods are handled by base class
and do not need to be implemented in adapter.
*/
// isConnected() {
// this.ws?.readyState && this.ws.readyState === WebSocket.OPEN ? true : false
// }
// isConnecting() {
// this.ws?.readyState && this.ws.readyState === WebSocket.CONNECTING ? true : false
// }
// isClosing() {
// this.ws?.readyState && this.ws.readyState === WebSocket.CLOSING ? true : false
// }
// isClosed() {
// this.ws?.readyState && this.ws.readyState === WebSocket.CLOSED ? true : false
// }

close(){
if(!this.$.isConnected())
if(!this.isConnected())
return
this.$.ws.close()
}
Expand Down
2 changes: 2 additions & 0 deletions packages/nocap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"test-ui": "vitest --ui"
},
"dependencies": {
"@nostrwatch/logger": "^0.0.1",
"@nostrwatch/utils": "^0.0.1",
"fetch-h2": "3.0.2",
"get-ssl-certificate": "2.3.3",
"jest": "29.7.0",
Expand Down
23 changes: 11 additions & 12 deletions packages/nocap/src/classes/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ export default class {
reason = `${key}: Precheck found that connect check was already fulfilled, returning cached result`
// this.promises.get(key).resolve(precheck.result)
checkDeferred.resolve(precheck.result)
}
else if(precheck.status == "error") {
} (precheck.status == "error") {
reason = `${key} precheck failed: ${precheck.message}`
// this.promises.get(key).resolve({ [key]: false, [`${key}Duration`]: -1, ...precheck })
checkDeferred.resolve({ [key]: false, [`${key}Duration`]: -1, ...precheck })
Expand Down Expand Up @@ -440,12 +439,12 @@ export default class {
return
if(this.adapters.websocket?.unsubscribe)
return this.adapters.websocket.unsubscribe()
// this.maybeExecuteAdapterMethod(
// 'websocket',
// 'close',
// () => this.ws.send(['CLOSE', subid]),
// subid
// )
this.maybeExecuteAdapterMethod(
'websocket',
'unsubscribe',
() => this.ws.send(['CLOSE', subid]),
subid
)
}

/**
Expand All @@ -460,8 +459,8 @@ export default class {
return
this.maybeExecuteAdapterMethod(
'websocket',
'close',
() => this.ws.close()
'close',
() => this.ws.close()
)
}

Expand Down Expand Up @@ -866,7 +865,7 @@ export default class {
return this.maybeExecuteAdapterMethod(
'websocket',
'isClosing',
() => this.ws?.readyState && this.ws.readyState === WebSocket.CONNECTING ? true : false
() => this.ws?.readyState && this.ws.readyState === WebSocket.CLOSING ? true : false
)
}

Expand All @@ -881,7 +880,7 @@ export default class {
return this.maybeExecuteAdapterMethod(
'websocket',
'isClosed',
() => this.ws?.readyState && this.ws.readyState === WebSocket.CONNECTING ? true : false
() => this.ws?.readyState && this.ws.readyState === WebSocket.CLOSED ? true : false
)
}

Expand Down

0 comments on commit ff9848e

Please sign in to comment.