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 /node_modules
/.vscode /.vscode
config.json config.json
package-lock.json package-lock.json
.env

View file

@ -1,12 +1,12 @@
# SPPK # SPPK
### SimplyPlural -> PluralKit Connectivity. ### SimplyPlural -> PluralKit Connectivity.
#### config.json #### .env
``` ```
"token": "token_here", token = "token_here",
"userId": "userid_here", userId = "userid_here",
"pk_token": "pluralkit_token_here", pk_token = "pluralkit_token_here",
"pk_system": "pluralkit_system_id" pk_system = "pluralkit_system_id"
``` ```
`token`: Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. `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 WebSocket = require('ws')
const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19); const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19)
function WebSocketClient(url) { function WebSocketClient(url) {
let client; let client
let timeout; let timeout
let connecting = false; let connecting = false
let backoff = 250; let backoff = 250
const init = () => { const init = () => {
console.error(`::SimplyWS:: [${timestamp()}] connecting`); console.error(`::SimplyWS:: [${timestamp()}] connecting`)
connecting = false; connecting = false
if (client !== undefined) { if (client !== undefined) {
client.removeAllListeners(); client.removeAllListeners()
} }
client = new WebSocket(url); client = new WebSocket(url)
const heartbeat = () => { const heartbeat = () => {
if (timeout !== undefined) { if (timeout !== undefined) {
clearTimeout(timeout); clearTimeout(timeout)
timeout = undefined; timeout = undefined
} }
timeout = setTimeout(() => client.terminate(), 350000); timeout = setTimeout(() => client.terminate(), 350000)
}; }
client.on('ping', () => { client.on('ping', () => {
console.log(`::SimplyWS:: [${timestamp()}] pinged`); console.log(`::SimplyWS:: [${timestamp()}] pinged`)
heartbeat(); heartbeat()
}); })
client.on('open', (e) => { client.on('open', (e) => {
if (typeof this.onOpen === 'function') { if (typeof this.onOpen === 'function') {
this.onOpen(); this.onOpen()
} else { } else {
console.log(`::SimplyWS:: [${timestamp()}] opened`); console.log(`::SimplyWS:: [${timestamp()}] opened`)
console.log(e); console.log(e)
} }
heartbeat(); heartbeat()
}); })
client.on('message', (e) => { client.on('message', (e) => {
if (typeof this.onMessage === 'function') { if (typeof this.onMessage === 'function') {
this.onMessage(e); this.onMessage(e)
} else { } else {
console.log(`::SimplyWS:: [${timestamp()}] messaged`); console.log(`::SimplyWS:: [${timestamp()}] messaged`)
} }
heartbeat(); heartbeat()
}); })
client.on('close', (e) => { client.on('close', (e) => {
if (e.code !== 1000) { if (e.code !== 1000) {
if (connecting === false) { // abnormal closure if (connecting === false) { // abnormal closure
backoff = backoff === 8000 ? 250 : backoff * 2; backoff = backoff === 8000 ? 250 : backoff * 2
setTimeout(() => init(), backoff); setTimeout(() => init(), backoff)
connecting = true; connecting = true
} }
} else if (typeof this.onClose === 'function') { } else if (typeof this.onClose === 'function') {
this.onClose(); this.onClose()
} else { } else {
console.error(`::SimplyWS:: [${timestamp()}] closed`); console.error(`::SimplyWS:: [${timestamp()}] closed`)
console.error(e); console.error(e)
} }
}); })
client.on('error', (e) => { client.on('error', (e) => {
if (e.code === 'ECONREFUSED') { if (e.code === 'ECONREFUSED') {
if (connecting === false) { // abnormal closure if (connecting === false) { // abnormal closure
backoff = backoff === 8000 ? 250 : backoff * 2; backoff = backoff === 8000 ? 250 : backoff * 2
setTimeout(() => init(), backoff); setTimeout(() => init(), backoff)
connecting = true; connecting = true
} }
} else if (typeof this.onError === 'function') { } else if (typeof this.onError === 'function') {
this.onError(e); this.onError(e)
} else { } else {
console.error(`::SimplyWS:: [${timestamp()}] errored`); console.error(`::SimplyWS:: [${timestamp()}] errored`)
console.error(e); console.error(e)
} }
}); })
this.send = client.send.bind(client); this.send = client.send.bind(client)
}; }
init(); 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 axios = require('axios')
const config = require('./config.json')
const SAPI = require('./SimplyAPI') const SAPI = require('./SimplyAPI')
const SimplyAPI = new SAPI(config) const SimplyAPI = new SAPI(config)
const pkUrl = 'https://api.pluralkit.me/v2' const pkUrl = config.pk_url
const pkHeader = { const pkHeader = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': config.pk_token 'Authorization': config.pk_token

View file

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