Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

it-ws

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

it-ws - npm Package Compare versions

Comparing version 5.0.6 to 6.0.0

dist/index.min.js

4

dist/src/duplex.d.ts
/// <reference types="ws" />
import type WebSocket from './web-socket.js';
import type { SinkOptions } from './sink.js';
import type { Duplex } from 'it-stream-types';
export interface DuplexWebSocket extends Duplex<Uint8Array, Uint8Array, Promise<void>> {
import type { Duplex, Source } from 'it-stream-types';
export interface DuplexWebSocket extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
connected: () => Promise<void>;

@@ -7,0 +7,0 @@ localAddress?: string;

@@ -23,3 +23,3 @@ import source from './source.js';

source: connectedSource,
connected: async () => await connectedSource.connected(),
connected: async () => { await connectedSource.connected(); },
close: async () => {

@@ -26,0 +26,0 @@ if (socket.readyState === socket.CONNECTING || socket.readyState === socket.OPEN) {

import type { WebSocket } from 'ws';
declare const _default: (socket: WebSocket) => Promise<void> | undefined;
declare const _default: (socket: WebSocket) => Promise<void>;
export default _default;
//# sourceMappingURL=ready.d.ts.map

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

export default (socket) => {
export default async (socket) => {
// if the socket is closing or closed, return end

@@ -10,3 +10,3 @@ if (socket.readyState >= 2) {

}
return new Promise((resolve, reject) => {
await new Promise((resolve, reject) => {
function cleanup() {

@@ -13,0 +13,0 @@ socket.removeEventListener('open', handleOpen);

@@ -12,3 +12,3 @@ import duplex from './duplex.js';

this.wsServer = new WSServer({
server: server,
server,
perMessageDeflate: false,

@@ -21,4 +21,4 @@ verifyClient: opts.verifyClient

return await new Promise((resolve, reject) => {
this.wsServer.once('error', (e) => reject(e));
this.wsServer.once('listening', () => resolve(this));
this.wsServer.once('error', (e) => { reject(e); });
this.wsServer.once('listening', () => { resolve(this); });
this.server.listen(typeof addrInfo === 'number' ? addrInfo : addrInfo.port);

@@ -28,6 +28,7 @@ });

async close() {
return await new Promise((resolve, reject) => {
await new Promise((resolve, reject) => {
this.server.close((err) => {
if (err != null) {
return reject(err);
reject(err);
return;
}

@@ -34,0 +35,0 @@ resolve();

import type { WebSocket } from 'ws';
import type { Sink } from 'it-stream-types';
import type { Sink, Source } from 'it-stream-types';
export interface SinkOptions {
closeOnEnd?: boolean;
}
declare const _default: (socket: WebSocket, options: SinkOptions) => Sink<Uint8Array, Promise<void>>;
declare const _default: (socket: WebSocket, options: SinkOptions) => Sink<Source<Uint8Array>, Promise<void>>;
export default _default;
//# sourceMappingURL=sink.d.ts.map

@@ -18,3 +18,3 @@ import ready from './ready.js';

if (options.closeOnEnd != null && socket.readyState <= 1) {
return await new Promise((resolve, reject) => {
await new Promise((resolve, reject) => {
socket.addEventListener('close', event => {

@@ -29,3 +29,3 @@ if (event.wasClean || event.code === 1006) {

});
setTimeout(() => socket.close());
setTimeout(() => { socket.close(); });
});

@@ -32,0 +32,0 @@ }

import type { WebSocket } from 'ws';
export interface ConnectedSource extends AsyncIterable<Uint8Array> {
export interface ConnectedSource extends AsyncGenerator<Uint8Array> {
connected: () => Promise<void>;

@@ -4,0 +4,0 @@ }

@@ -11,21 +11,25 @@ import { EventIterator } from 'event-iterator';

socket.binaryType = 'arraybuffer';
const connected = async () => await new Promise((resolve, reject) => {
if (isConnected) {
return resolve();
}
if (connError != null) {
return reject(connError);
}
const cleanUp = (cont) => {
socket.removeEventListener('open', onOpen);
socket.removeEventListener('error', onError);
cont();
};
const onOpen = () => cleanUp(resolve);
const onError = (event) => {
cleanUp(() => reject(event.error ?? new Error(`connect ECONNREFUSED ${socket.url}`)));
};
socket.addEventListener('open', onOpen);
socket.addEventListener('error', onError);
});
const connected = async () => {
await new Promise((resolve, reject) => {
if (isConnected) {
resolve();
return;
}
if (connError != null) {
reject(connError);
return;
}
const cleanUp = (cont) => {
socket.removeEventListener('open', onOpen);
socket.removeEventListener('error', onError);
cont();
};
const onOpen = () => { cleanUp(resolve); };
const onError = (event) => {
cleanUp(() => { reject(event.error ?? new Error(`connect ECONNREFUSED ${socket.url}`)); });
};
socket.addEventListener('open', onOpen);
socket.addEventListener('error', onError);
});
};
const source = (async function* () {

@@ -49,3 +53,3 @@ const messages = new EventIterator(({ push, stop, fail }) => {

};
const onError = (event) => fail(event.error ?? new Error('Socket error'));
const onError = (event) => { fail(event.error ?? new Error('Socket error')); };
socket.addEventListener('message', onMessage);

@@ -52,0 +56,0 @@ socket.addEventListener('error', onError);

{
"name": "it-ws",
"version": "5.0.6",
"version": "6.0.0",
"description": "Simple async iterables for websocket client connections",

@@ -46,3 +46,3 @@ "author": "Alan Shaw",

"src",
"dist/src",
"dist",
"!dist/test",

@@ -180,3 +180,4 @@ "!**/*.tsbuildinfo"

"test:electron-main": "aegir test -t electron-main",
"release": "aegir release"
"release": "aegir release",
"docs": "aegir docs"
},

@@ -186,3 +187,3 @@ "dependencies": {

"iso-url": "^1.1.2",
"it-stream-types": "^1.0.2",
"it-stream-types": "^2.0.1",
"uint8arrays": "^4.0.2",

@@ -193,11 +194,11 @@ "ws": "^8.4.0"

"@types/ws": "^8.2.2",
"aegir": "^37.0.15",
"aegir": "^38.1.8",
"delay": "^5.0.0",
"it-all": "^2.0.0",
"it-drain": "^2.0.0",
"it-foreach": "^1.0.0",
"it-all": "^3.0.1",
"it-drain": "^3.0.1",
"it-foreach": "^2.0.2",
"it-goodbye": "^4.0.0",
"it-map": "^2.0.0",
"it-map": "^3.0.2",
"it-ndjson": "^1.0.0",
"it-pipe": "^2.0.3",
"it-pipe": "^3.0.1",
"p-defer": "^4.0.0",

@@ -204,0 +205,0 @@ "wherearewe": "^2.0.1",

# it-ws <!-- omit in toc -->
[![codecov](https://img.shields.io/codecov/c/github/alanshaw/it-ws.svg?style=flat-square)](https://codecov.io/gh/alanshaw/it-ws)
[![CI](https://img.shields.io/github/workflow/status/alanshaw/it-ws/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/alanshaw/it-ws/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/it-ws/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/alanshaw/it-ws/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)

@@ -11,2 +11,3 @@ > Simple async iterables for websocket client connections

- [Install](#install)
- [Browser `<script>` tag](#browser-script-tag)
- [Usage](#usage)

@@ -25,3 +26,3 @@ - [Example - client](#example---client)

- [License](#license)
- [Contribute](#contribute)
- [Contribution](#contribution)

@@ -34,2 +35,10 @@ ## Install

### Browser `<script>` tag
Loading this module through a script tag will make it's exports available as `ItWs` in the global namespace.
```html
<script src="https://unpkg.com/it-ws/dist/index.min.js"></script>
```
## Usage

@@ -265,4 +274,4 @@

## Contribute
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

@@ -5,5 +5,5 @@ import source from './source.js'

import type { SinkOptions } from './sink.js'
import type { Duplex } from 'it-stream-types'
import type { Duplex, Source } from 'it-stream-types'
export interface DuplexWebSocket extends Duplex<Uint8Array, Uint8Array, Promise<void>> {
export interface DuplexWebSocket extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
connected: () => Promise<void>

@@ -47,3 +47,3 @@ localAddress?: string

source: connectedSource,
connected: async () => await connectedSource.connected(),
connected: async () => { await connectedSource.connected() },
close: async () => {

@@ -50,0 +50,0 @@ if (socket.readyState === socket.CONNECTING || socket.readyState === socket.OPEN) {

import type { ErrorEvent, WebSocket } from 'ws'
export default (socket: WebSocket) => {
export default async (socket: WebSocket): Promise<void> => {
// if the socket is closing or closed, return end

@@ -14,4 +14,4 @@ if (socket.readyState >= 2) {

return new Promise<void>((resolve, reject) => {
function cleanup () {
await new Promise<void>((resolve, reject) => {
function cleanup (): void {
socket.removeEventListener('open', handleOpen)

@@ -21,3 +21,3 @@ socket.removeEventListener('error', handleErr)

function handleOpen () {
function handleOpen (): void {
cleanup()

@@ -27,3 +27,3 @@ resolve()

function handleErr (event: ErrorEvent) {
function handleErr (event: ErrorEvent): void {
cleanup()

@@ -30,0 +30,0 @@ reject(event.error ?? new Error(`connect ECONNREFUSED ${socket.url}`))

@@ -32,3 +32,3 @@ import duplex, { DuplexWebSocket } from './duplex.js'

this.wsServer = new WSServer({
server: server,
server,
perMessageDeflate: false,

@@ -40,6 +40,6 @@ verifyClient: opts.verifyClient

async listen (addrInfo: { port: number } | number) {
async listen (addrInfo: { port: number } | number): Promise<WebSocketServer> {
return await new Promise<WebSocketServer>((resolve, reject) => {
this.wsServer.once('error', (e) => reject(e))
this.wsServer.once('listening', () => resolve(this))
this.wsServer.once('error', (e) => { reject(e) })
this.wsServer.once('listening', () => { resolve(this) })
this.server.listen(typeof addrInfo === 'number' ? addrInfo : addrInfo.port)

@@ -49,7 +49,7 @@ })

async close () {
return await new Promise<void>((resolve, reject) => {
async close (): Promise<void> {
await new Promise<void>((resolve, reject) => {
this.server.close((err) => {
if (err != null) {
return reject(err)
reject(err); return
}

@@ -62,7 +62,7 @@

address () {
address (): string | AddressInfo | null {
return this.server.address()
}
onWsServerConnection (socket: WebSocket, req: http.IncomingMessage) {
onWsServerConnection (socket: WebSocket, req: http.IncomingMessage): void {
const addr = this.wsServer.address()

@@ -103,3 +103,3 @@

function proxy (server: http.Server, event: string) {
function proxy (server: http.Server, event: string): http.Server {
return server.on(event, (...args: any[]) => {

@@ -106,0 +106,0 @@ wss.emit(event, ...args)

import ready from './ready.js'
import type { WebSocket } from 'ws'
import type { Sink } from 'it-stream-types'
import type { Sink, Source } from 'it-stream-types'

@@ -9,7 +9,7 @@ export interface SinkOptions {

export default (socket: WebSocket, options: SinkOptions) => {
export default (socket: WebSocket, options: SinkOptions): Sink<Source<Uint8Array>, Promise<void>> => {
options = options ?? {}
options.closeOnEnd = options.closeOnEnd !== false
const sink: Sink<Uint8Array, Promise<void>> = async source => {
const sink: Sink<Source<Uint8Array>, Promise<void>> = async source => {
for await (const data of source) {

@@ -27,3 +27,3 @@ try {

if (options.closeOnEnd != null && socket.readyState <= 1) {
return await new Promise((resolve, reject) => {
await new Promise<void>((resolve, reject) => {
socket.addEventListener('close', event => {

@@ -38,3 +38,3 @@ if (event.wasClean || event.code === 1006) {

setTimeout(() => socket.close())
setTimeout(() => { socket.close() })
})

@@ -41,0 +41,0 @@ }

@@ -13,3 +13,3 @@

export interface ConnectedSource extends AsyncIterable<Uint8Array> {
export interface ConnectedSource extends AsyncGenerator<Uint8Array> {
connected: () => Promise<void>

@@ -21,24 +21,26 @@ }

const connected = async () => await new Promise<void>((resolve, reject) => {
if (isConnected) {
return resolve()
}
if (connError != null) {
return reject(connError)
}
const connected = async (): Promise<void> => {
await new Promise<void>((resolve, reject) => {
if (isConnected) {
resolve(); return
}
if (connError != null) {
reject(connError); return
}
const cleanUp = (cont: () => void) => {
socket.removeEventListener('open', onOpen)
socket.removeEventListener('error', onError)
cont()
}
const cleanUp = (cont: () => void): void => {
socket.removeEventListener('open', onOpen)
socket.removeEventListener('error', onError)
cont()
}
const onOpen = () => cleanUp(resolve)
const onError = (event: ErrorEvent) => {
cleanUp(() => reject(event.error ?? new Error(`connect ECONNREFUSED ${socket.url}`)))
}
const onOpen = (): void => { cleanUp(resolve) }
const onError = (event: ErrorEvent): void => {
cleanUp(() => { reject(event.error ?? new Error(`connect ECONNREFUSED ${socket.url}`)) })
}
socket.addEventListener('open', onOpen)
socket.addEventListener('error', onError)
})
socket.addEventListener('open', onOpen)
socket.addEventListener('error', onError)
})
}

@@ -48,3 +50,3 @@ const source = (async function * () {

({ push, stop, fail }) => {
const onMessage = (event: MessageEvent) => {
const onMessage = (event: MessageEvent): void => {
let data: Uint8Array | null = null

@@ -70,3 +72,3 @@

}
const onError = (event: ErrorEvent) => fail(event.error ?? new Error('Socket error'))
const onError = (event: ErrorEvent): void => { fail(event.error ?? new Error('Socket error')) }

@@ -73,0 +75,0 @@ socket.addEventListener('message', onMessage)

@@ -6,2 +6,2 @@ import { relative } from 'iso-url'

export default (url: string, location: string | Partial<Location>) => relative(url, location, map, def)
export default (url: string, location: string | Partial<Location>): string => relative(url, location, map, def)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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