Socket
Socket
Sign inDemoInstall

dblapi.js

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dblapi.js - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

0

index.js
module.exports = require('./src/index');

4

package.json
{
"name": "dblapi.js",
"version": "2.0.1",
"version": "2.1.0",
"description": "An official module for interacting with the discordbots.org API",

@@ -20,5 +20,5 @@ "main": "index.js",

"dependencies": {
"snekfetch": "^3.6.4"
"snekfetch": "^4.0.2"
},
"typings": "./typings/index.d.ts"
}

@@ -18,2 +18,11 @@ # dblapi.js

const dbl = new DBL('Your discordbots.org token', client);
// Optional events
dbl.on('posted', () => {
console.log('Server count posted!');
})
dbl.on('error', e => {
console.log(`Oops! ${e}`);
})
```

@@ -20,0 +29,0 @@

@@ -0,1 +1,2 @@

const EventEmitter = require('events');
const snekfetch = require('snekfetch');

@@ -15,3 +16,3 @@ const API = 'https://discordbots.org/api/';

class DBLAPI {
class DBLAPI extends EventEmitter {
/**

@@ -25,5 +26,7 @@ * Creates a new DBLAPI Instance.

* @param {string} [options.webhookPath='/dblwebhook'] The path for the webhook request.
* @param {http.Server} [options.webhookServer] An existing http server to attach the webhook to.
* @param {any} [client] Your Client instance, if present and supported it will auto update your stats every `options.statsInterval` ms.
*/
constructor(token, options, client) {
super();
this.token = token;

@@ -40,7 +43,22 @@ if (isASupportedLibrary(options)) {

/**
* Event that fires when the stats have been posted successfully by the autoposter
* @event posted
*/
/**
* Event to notify that the autoposter post request failed
* @event error
* @param {error} error The error
*/
this.client = client;
this.client.on('ready', () => {
this.postStats().catch(e => console.error(`[dblapi.js autopost] Failed to post stats: ${e.text}`)); // eslint-disable-line no-console
this.postStats()
.then(() => this.emit('posted'))
.catch(e => this.emit('error', e));
setInterval(() => {
this.postStats().catch(e => console.error(`[dblapi.js autopost] Failed to post stats: ${e.text}`)); // eslint-disable-line no-console
this.postStats()
.then(() => this.emit('posted'))
.catch(e => this.emit('error', e));
}, this.options.statsInterval);

@@ -52,5 +70,5 @@ });

if (this.options.webhookPort) {
if (this.options.webhookPort || this.options.webhookServer) {
const DBLWebhook = require('./webhook');
this.webhook = new DBLWebhook(this.options.webhookPort, this.options.webhookPath, this.options.webhookAuth);
this.webhook = new DBLWebhook(this.options.webhookPort, this.options.webhookPath, this.options.webhookAuth, this.options.webhookServer);
}

@@ -57,0 +75,0 @@ }

@@ -11,6 +11,8 @@ const EventEmitter = require('events');

* @param {string} [auth] The string for Authorization you set on the site for verification.
* @param {http.Server} [server] An existing http server to connect with.
*/
constructor(port, path, auth) {
constructor(port, path, auth, server) {
if (!port && !server) throw new Error('Neither port nor server provided');
super();
this.port = port;
this.port = port || 0;
this.path = path || '/dblwebhook';

@@ -20,21 +22,38 @@ this.auth = auth;

this._server = null;
this.attached = false;
this._startWebhook();
if (server && !(server instanceof http.Server)) throw Error('Server provided is not a http server');
if (server) {
this._attachWebhook(server);
} else {
this._startWebhook();
}
}
_emitListening() {
/**
* Event to notify that the webhook is listening
* @event ready
* @param {string} hostname The hostname of the webhook server
* @param {number} port The port the webhook server is running on
* @param {string} path The path for the webhook
*/
// Get the user's public IP via an API for hostname later?
this.emit('ready', { hostname: '0.0.0.0', port: this.port, path: this.path });
}
_startWebhook() {
this._server = http.createServer(this._handleRequest.bind(this));
this._server.listen(this.port, () => {
/**
* Event to notify that the webhook is listening
* @event ready
* @param {string} hostname The hostname of the webhook server
* @param {number} port The port the webhook server is running on
* @param {string} path The path for the webhook
*/
// Get the user's public IP via an API for hostname later?
this.emit('ready', { hostname: '0.0.0.0', port: this.port, path: this.path });
});
this._server.listen(this.port, this._emitListening.bind(this));
}
_attachWebhook(server) {
this._server = server;
this._listeners = server.listeners('request');
server.removeAllListeners('request');
server.on('request', this._handleRequest.bind(this));
server.on('listening', this._emitListening.bind(this));
this.attached = true;
}
_handleRequest(req, res) {

@@ -72,2 +91,8 @@ if (req.url === this.path && req.method === 'POST') {

} else {
if (this.attached) {
for (const listener of this._listeners) {
listener.call(this._server, req, res);
}
return undefined;
}
return this._returnResponse(res, 404);

@@ -74,0 +99,0 @@ }

export = DBLAPI;
import { EventEmitter } from 'events';
declare class DBLAPI {
declare class DBLAPI extends EventEmitter {
constructor(token: string, options: DBLAPI.DBLOptions, client?: object);

@@ -18,8 +19,10 @@ constructor(token: string, client?: object);

private _request(method: string, endpoint: string, data?: object, auth?: boolean): Promise<object>
public on(event: 'posted', listener: () => void): this;
public on(event: 'error', listener: (error: Error) => void): this;
}
import { EventEmitter } from 'events';
import { Server, ServerResponse, IncomingMessage } from 'http';
declare class DBLWebhook extends EventEmitter {
constructor(port: number, path?: string, auth?: string)
constructor(port?: number, path?: string, auth?: string, server?: Server)

@@ -30,3 +33,6 @@ public port: number;

private _server: Server;
private attached: boolean;
private _emitListening(): void;
private _startWebhook(): void;
private _attachWebhook(server: Server): void;
private _handleRequest(req: IncomingMessage, res: ServerResponse): void;

@@ -45,2 +51,3 @@ private _returnResponse(res: ServerResponse, statusCode: number, data?: string): void;

webhookPath?: string;
webhookServer?: Server;
}

@@ -47,0 +54,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc