switch to env

This commit is contained in:
bee 2022-03-01 19:14:15 -08:00
parent 966223bc0f
commit 15d712dfa9
No known key found for this signature in database
GPG key ID: 70EECBF29DA75D8B
6 changed files with 65 additions and 52 deletions

8
.env_example Normal file
View file

@ -0,0 +1,8 @@
# SPPK
url="https://devapi.apparyllis.com"
socket="wss://devapi.apparyllis.com/v1/socket"
pk_url="https://api.pluralkit.me/v2"
token="AAAAAAAAAAAAAAAAAAAA"
userId="AAAAAAAAAAAAAAAAAAA"
pk_token= "AAAAAAAAAAAAAAAA"
pk_system="AAAAAAAAAAAAAAAA"

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
/node_modules
/.vscode
config.json
package-lock.json
package-lock.json
.env

View file

@ -1,12 +1,12 @@
# SPPK
### SimplyPlural -> PluralKit Connectivity.
#### config.json
#### .env
```
"token": "token_here",
"userId": "userid_here",
"pk_token": "pluralkit_token_here",
"pk_system": "pluralkit_system_id"
token = "token_here",
userId = "userid_here",
pk_token = "pluralkit_token_here",
pk_system = "pluralkit_system_id"
```
`token`: Your SimplyPlural account token. As of now, the only permission necessary is the Read permission.

View file

@ -1,77 +1,77 @@
const WebSocket = require('ws');
const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19);
const WebSocket = require('ws')
const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19)
function WebSocketClient(url) {
let client;
let timeout;
let connecting = false;
let backoff = 250;
let client
let timeout
let connecting = false
let backoff = 250
const init = () => {
console.error(`::SimplyWS:: [${timestamp()}] connecting`);
connecting = false;
console.error(`::SimplyWS:: [${timestamp()}] connecting`)
connecting = false
if (client !== undefined) {
client.removeAllListeners();
client.removeAllListeners()
}
client = new WebSocket(url);
client = new WebSocket(url)
const heartbeat = () => {
if (timeout !== undefined) {
clearTimeout(timeout);
timeout = undefined;
clearTimeout(timeout)
timeout = undefined
}
timeout = setTimeout(() => client.terminate(), 350000);
};
timeout = setTimeout(() => client.terminate(), 350000)
}
client.on('ping', () => {
console.log(`::SimplyWS:: [${timestamp()}] pinged`);
heartbeat();
});
console.log(`::SimplyWS:: [${timestamp()}] pinged`)
heartbeat()
})
client.on('open', (e) => {
if (typeof this.onOpen === 'function') {
this.onOpen();
this.onOpen()
} else {
console.log(`::SimplyWS:: [${timestamp()}] opened`);
console.log(e);
console.log(`::SimplyWS:: [${timestamp()}] opened`)
console.log(e)
}
heartbeat();
});
heartbeat()
})
client.on('message', (e) => {
if (typeof this.onMessage === 'function') {
this.onMessage(e);
this.onMessage(e)
} else {
console.log(`::SimplyWS:: [${timestamp()}] messaged`);
console.log(`::SimplyWS:: [${timestamp()}] messaged`)
}
heartbeat();
});
heartbeat()
})
client.on('close', (e) => {
if (e.code !== 1000) {
if (connecting === false) { // abnormal closure
backoff = backoff === 8000 ? 250 : backoff * 2;
setTimeout(() => init(), backoff);
connecting = true;
backoff = backoff === 8000 ? 250 : backoff * 2
setTimeout(() => init(), backoff)
connecting = true
}
} else if (typeof this.onClose === 'function') {
this.onClose();
this.onClose()
} else {
console.error(`::SimplyWS:: [${timestamp()}] closed`);
console.error(e);
console.error(`::SimplyWS:: [${timestamp()}] closed`)
console.error(e)
}
});
})
client.on('error', (e) => {
if (e.code === 'ECONREFUSED') {
if (connecting === false) { // abnormal closure
backoff = backoff === 8000 ? 250 : backoff * 2;
setTimeout(() => init(), backoff);
connecting = true;
backoff = backoff === 8000 ? 250 : backoff * 2
setTimeout(() => init(), backoff)
connecting = true
}
} else if (typeof this.onError === 'function') {
this.onError(e);
this.onError(e)
} else {
console.error(`::SimplyWS:: [${timestamp()}] errored`);
console.error(e);
console.error(`::SimplyWS:: [${timestamp()}] errored`)
console.error(e)
}
});
this.send = client.send.bind(client);
};
init();
})
this.send = client.send.bind(client)
}
init()
}
module.exports = WebSocketClient;
module.exports = WebSocketClient

View file

@ -1,9 +1,12 @@
const dotenv = require('dotenv')
dotenv.config()
const config = process.env
const axios = require('axios')
const config = require('./config.json')
const SAPI = require('./SimplyAPI')
const SimplyAPI = new SAPI(config)
const pkUrl = 'https://api.pluralkit.me/v2'
const pkUrl = config.pk_url
const pkHeader = {
'Content-Type': 'application/json',
'Authorization': config.pk_token

View file

@ -12,6 +12,7 @@
"dependencies": {
"ajv": "^8.10.0",
"axios": "^0.26.0",
"dotenv": "^16.0.0",
"lodash.isarray": "^4.0.0",
"lodash.isequal": "^4.5.0",
"lodash.isobject": "^3.0.2",