diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df968a..02159be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,17 +1,2 @@ -- Fixed an issue importing the library. - -## Breaking Changes - -### New importing -From now on, there is no default export. - -ESM - -```ts -import { connectTo } from "zilaws-client"; -``` - -CommonJS -```ts -const { connectTo } = require("zilaws-client"); -``` \ No newline at end of file +- Fixed an issue that made ZilaWS uncompatible with browsers. +- Fixed an issue that because of VSCode wanted to import the types by default. \ No newline at end of file diff --git a/config/webpack.config.js b/config/webpack.config.js index 94ea7cc..8a7c700 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -35,6 +35,10 @@ module.exports = { new TerserPlugin() ] }, + externalsType: 'self', + externals: { + ws: 'WebSocket' + }, resolve: { extensions: ['.ts', '.js', '.tsx', '.jsx'], } diff --git a/package-lock.json b/package-lock.json index b51cd1e..6c4ffe8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zilaws-client", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "zilaws-client", - "version": "1.1.0", + "version": "1.1.1", "license": "MIT", "dependencies": { "uuid": "^9.0.0", diff --git a/package.json b/package.json index b7b3aed..45a0582 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "$schema": "https://json.schemastore.org/package", "name": "zilaws-client", - "version": "1.1.0", + "version": "1.1.1", "description": "ZilaWS is a blazingly fast and very lightweight library that provides an extremely easy-to-use way to transmit data via websockets between clientside and serverside using eventhandlers and async waiters.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "umd:main": "dist/umd/index.js", - "types": "dist/types/index.d.js", + "types": "dist/types/index.d.ts", "browser": "dist/umd/index.js", "scripts": { "build": "npm run format:fix && npm run test:cov-build && npm run format:fix && npm run build:cjs && npm run build:esm && npm run build:umd && npm run build:types", diff --git a/src/index.ts b/src/index.ts index 8864fb3..75b4f41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,7 @@ export class ZilaConnection { private localEventCallbacks: { [K in keyof ICallableLocalEvents]?: Array; } = {}; - private connection: WebSocket | undefined; + private connection: WebSocket | globalThis.WebSocket | undefined; private errorCallback: errorCallbackType | undefined; private _status: WSStatus = WSStatus.OPENING; @@ -90,12 +90,17 @@ export class ZilaConnection { allowSelfSignedCert = false ): Promise { return new Promise(async (resolve, reject) => { - let ws: WebSocket | undefined; + let ws: WebSocket | globalThis.WebSocket | undefined; try { - ws = new WebSocket(wsUrl, { - rejectUnauthorized: !allowSelfSignedCert, - }); + /* istanbul ignore next */ + if (typeof window !== "undefined" && typeof window.document !== "undefined") { + ws = new window.WebSocket(wsUrl); + } else { + ws = new WebSocket(wsUrl, { + rejectUnauthorized: !allowSelfSignedCert, + }); + } } catch (error) { const errorMessage = (error as Error).stack?.split("\n")[0]; if (error && errorCallback) errorCallback(errorMessage); @@ -114,12 +119,12 @@ export class ZilaConnection { }); } - private constructor(ws: WebSocket, errorCallback?: errorCallbackType) { + private constructor(ws: WebSocket | globalThis.WebSocket, errorCallback?: errorCallbackType) { this.connection = ws; this.errorCallback = errorCallback; this._status = WSStatus.OPENING; - this.connection.onerror = async (event) => { + this.connection.onerror = async () => { this.status = WSStatus.ERROR; this.errorCallback?.call(undefined); @@ -131,15 +136,15 @@ export class ZilaConnection { this.status = WSStatus.OPEN; }; - this.connection.onclose = (ev) => { - if (ev.code == CloseCodes.BANNED) { + this.connection.onclose = ({ code, reason }: { code: number; reason: string }) => { + if (code == CloseCodes.BANNED) { console.error("ZilaWS: The client is banned from the WebSocket server."); - } else if (ev.code == CloseCodes.KICKED) { + } else if (code == CloseCodes.KICKED) { console.error("ZilaWS: The client got disconnected from the server."); } this.status = WSStatus.CLOSED; - if (this.errorCallback) this.errorCallback(ev.reason); + if (this.errorCallback) this.errorCallback(reason); }; this.connection.onmessage = (ev: any) => { diff --git a/test/index.test.ts b/test/index.test.ts index 1c57c56..622fc09 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import { connectTo, WSStatus, ZilaConnection } from "../src/index"; -import {ZilaServer, ZilaClient } from "zilaws-server"; +import { ZilaServer, ZilaClient } from "zilaws-server"; var client: ZilaConnection; var server: ZilaServer;