Socket
Socket
Sign inDemoInstall

wspromisify

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wspromisify - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

54

package.json
{
"name": "wspromisify",
"version": "0.0.1",
"description": "Makes it easier to handle different data via WebSockets.",
"main": "WS.js",
"scripts": {
"test": "npm run test"
"_from": "wspromisify",
"_id": "wspromisify@0.0.11",
"_inBundle": false,
"_integrity": "sha512-586/177fc75jGEmnl4anuDbHYqENvJwYrY94PSOOmJZfEEEKICutj/ujlKdWO52N3HeXqKKJlXjzCWD6WRrz4w==",
"_location": "/wspromisify",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "wspromisify",
"name": "wspromisify",
"escapedName": "wspromisify",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/houd1ni/WebsocketPromisify.git"
"_requiredBy": [
"#DEV:/",
"#USER"
],
"_resolved": "https://registry.npmjs.org/wspromisify/-/wspromisify-0.0.11.tgz",
"_shasum": "6cf45bd945835d95ed2b0dcf6428a31d26002c74",
"_spec": "wspromisify",
"_where": "C:\\Users\\m.akiliev\\Documents\\tests\\NodeWebsocketLoadTest",
"author": {
"name": "Michael Akiliev"
},
"bugs": {
"url": "https://github.com/houd1ni/WebsocketPromisify/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Makes it easier to handle different data via WebSockets.",
"homepage": "https://github.com/houd1ni/WebsocketPromisify#readme",
"keywords": [

@@ -24,8 +47,13 @@ "WebSockets",

],
"author": "Michael Akiliev",
"license": "ISC",
"bugs": {
"url": "https://github.com/houd1ni/WebsocketPromisify/issues"
"main": "WS.js",
"name": "wspromisify",
"repository": {
"type": "git",
"url": "git+https://github.com/houd1ni/WebsocketPromisify.git"
},
"homepage": "https://github.com/houd1ni/WebsocketPromisify#readme"
"scripts": {
"test": "npm run test"
},
"version": "0.0.2"
}
SHA1 = require('./SHA1.js')
const SHA1 = require('./SHA1.js')

@@ -7,14 +7,16 @@ /* .send(your_data) wraps request to server with {id: `hash`, data: `actually your data`},

resolved if server returns the same signature: {id: `same_hash`, data: `response data`}
works only with {json: true} in config to constructor.
*/
is_func = (handler) => typeof handler == 'function'
add_event = (o, e, handler) => o.addEventListener(e, handler)
sett = (a, b) => setTimeout(b, (a || 0))
const add_event = (o, e, handler) => o.addEventListener(e, handler)
const sett = (a, b) => setTimeout(b, a)
const default_config = {
json: true,
data_type: 'json', // ToDo some other stuff maybe.
url: 'localhost',
timeout: 1400,
id_name: 'id'
adapter: (host => new WebSocket(host)),
server: {
id_key: 'id',
data_key: 'data'
}
}

@@ -25,29 +27,54 @@

on(event_name, handler, predicate) {
return add_event(this.ws, event_name, event => {
if(!predicate || predicate(event)) {
handler(event)
}
})
}
close() {
delete this.queue // Free up memory.
delete this.messages
this.open = null
return this.ws.close()
}
async send(user_message, opts = {}) {
let message = {}
const id_name = this.config.id_name
const message = {}
const id_key = this.config.server.id_key
const data_key = this.config.server.data_key
const is_json = typeof opts.json == 'boolean' ? opts.data_type=='json' : this.config.json
message.data = user_message
message.json = typeof opts.json == 'boolean' ? opts.json : this.config.json
message[id_name] = SHA1(user_message + '' + ((Math.random()*1e5)|0)).slice(0, 20)
message[data_key] = user_message // is_json ? JSON.stringify(user_message
message[id_key] = SHA1(user_message + '' + ((Math.random()*1e5)|0)).slice(0, 20)
if(typeof opts.top == 'object') {
if(opts.top[data_key]) {
throw new Error('Attempting to set data key/token via send() options!')
}
Object.assign(message, opts.top)
}
if(this.open) {
if(this.open === true) {
this.ws.send(JSON.stringify(message))
} else {
} else if(this.open === false) {
this.messages.push({send: () => this.ws.send(JSON.stringify(message))})
} else if(this.open === null) {
throw new Error('Attempting to send via closed WebSocket connection!')
}
return new Promise((ff, rj) => {
this.queue[message[id_name]] = {
ff
this.queue[message[id_key]] = {
ff,
data_type: this.config.data_type,
timeout: sett(this.config.timeout, () => {
if(this.queue[message[id_key]]) {
rj({
'Websocket timeout expired: ': this.config.timeout,
'for the message': message
})
delete this.queue[message[id_key]]
}
})
}
sett(this.config.timeout, () => {
if(this.queue[message[id_name]]) {
rj({
'Websocket timeout expired: ': this.config.timeout,
'for the message': message
})
delete this.queue[message[id_name]]
}
})
})

@@ -57,17 +84,15 @@ }

constructor(config = {}) {
this.config = {}
Object.assign(this.config, default_config)
Object.assign(this.config, config)
config = this.config
const id_name = this.config.id_name
this.queue = {} // data queuse
constructor(user_config = {}) {
const config = {}
Object.assign(config, default_config)
Object.assign(config, user_config)
const id_key = config.server.id_key
const data_key = config.server.data_key
const ws = config.adapter(`ws://${config.url}`)
this.ws = ws
this.queue = {} // data queuse
this.messages = [] // send() queue
this.ws = new WebSocket(`ws://${config.url}`)
const ws = this.ws
this.config = config
add_event(ws, 'open', (e) => {
if(is_func(config.onopen)) {
config.onopen()
}
this.open = true

@@ -77,24 +102,14 @@ this.messages.forEach((message) => message.send())

add_event(ws, 'close', (e) => {
if(is_func(config.onclose)) {
config.onclose()
}
this.ws = null
this.open = false
})
if(is_func(config.onerror)) {
add_event(ws, 'error', config.onerror)
}
add_event(ws, 'message', (e) => {
if(is_func(config.onmessage)) {
config.onmessage({
data: e.data
})
}
try {
const data = config.json ? JSON.parse(e.data) : e.data
if(data[id_name]) {
const q = this.queue[data[id_name]]
const data = JSON.parse(e.data)
if(data[id_key]) {
const q = this.queue[data[id_key]]
if(q) {
q.ff(data.data)
delete this.queue[data[id_name]]
q.ff(data[data_key])
clearTimeout(q.timeout)
delete this.queue[data[id_key]]
}

@@ -101,0 +116,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