Comparing version 0.0.1 to 0.0.2
97
index.ts
@@ -1,3 +0,96 @@ | ||
export function test() { | ||
console.log('Official w3c bidi implementation. Work in progress') | ||
import WebSocket from 'ws' | ||
import {BidiRequest} from './types/BidiRequest' | ||
import {BidiResponse} from './types/BidiResponse' | ||
const RESPONSE_TIMEOUT = 10 * 30 | ||
export class BIDI { | ||
private readonly _ws: WebSocket | ||
private _isConnected = false | ||
private _id = 0 | ||
constructor(url: string) { | ||
this._ws = new WebSocket(url) | ||
this._ws.on('open', () => { | ||
this._isConnected = true | ||
}) | ||
} | ||
async waitForConnection() { | ||
return new Promise<void>((resolve, reject) => { | ||
if (this._isConnected) { | ||
resolve() | ||
} else { | ||
this._ws.once('open', () => { | ||
resolve() | ||
}) | ||
this._ws.once('error', (error: Error) => { | ||
reject(`Bidi connection failed ${error.message}`) | ||
}) | ||
} | ||
}) | ||
} | ||
get socket(): WebSocket { | ||
return this._ws | ||
} | ||
get isConnected(): boolean { | ||
return this._isConnected | ||
} | ||
async send(params: BidiRequest) { | ||
if (!this.isConnected) { | ||
await this.waitForConnection() | ||
} | ||
const id = ++this._id | ||
this._ws.send(JSON.stringify({id, ...params})) | ||
return new Promise<BidiResponse>((resolve, reject) => { | ||
const timeoutId = setTimeout(() => { | ||
reject(new Error(`Request with id ${id} timed out`)) | ||
handler.off('message', listener) | ||
}, RESPONSE_TIMEOUT) | ||
const listener = (data: string) => { | ||
try { | ||
const payload = JSON.parse(data) | ||
if (payload.id === id) { | ||
clearTimeout(timeoutId) | ||
handler.off('message', listener) | ||
resolve(payload) | ||
} | ||
} catch (err: unknown) { | ||
if (err instanceof Error) { | ||
console.error(`Failed parse message: ${err.message}`) | ||
} | ||
} | ||
} | ||
const handler = this._ws.on('message', listener) | ||
}) | ||
} | ||
async close(): Promise<void> { | ||
const closeWebSocket = (callback: () => void) => { | ||
// don't close if it's already closed | ||
if (this._ws.readyState === 3) { | ||
callback() | ||
} else { | ||
// don't notify on user-initiated shutdown ('disconnect' event) | ||
this._ws.removeAllListeners('close') | ||
this._ws.once('close', () => { | ||
this._ws.removeAllListeners() | ||
callback() | ||
}) | ||
this._ws.close() | ||
} | ||
} | ||
return new Promise((fulfill) => { | ||
closeWebSocket(fulfill) | ||
}) | ||
} | ||
} |
{ | ||
"name": "wd-bidi", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
"main": "index.js", | ||
"main": "./build/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"lint": "eslint . --ext .ts", | ||
"build": "tsc" | ||
}, | ||
@@ -14,3 +15,6 @@ "repository": { | ||
"keywords": [], | ||
"author": "", | ||
"author": { | ||
"name": "Sri Harsha", | ||
"email": "sri_harsha509@hotmail.com" | ||
}, | ||
"license": "ISC", | ||
@@ -24,2 +28,5 @@ "bugs": { | ||
"@types/ws": "^8.5.3", | ||
"@typescript-eslint/eslint-plugin": "^5.47.0", | ||
"@typescript-eslint/parser": "^5.47.0", | ||
"eslint": "^8.30.0", | ||
"typescript": "^4.9.4", | ||
@@ -26,0 +33,0 @@ "ws": "^8.11.0" |
@@ -10,3 +10,3 @@ { | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
"rootDir": "src", /* Specify the root folder within your source files. */ | ||
"rootDir": ".", /* Specify the root folder within your source files. */ | ||
"resolveJsonModule": true, /* Enable importing .json files. */ | ||
@@ -28,4 +28,5 @@ | ||
/* Completeness */ | ||
"skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
"skipLibCheck": true, /* Skip type checking all .d.ts files. */ | ||
"declaration": true | ||
} | ||
} |
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9057
9
188
0
1
7