From efcd43ef7a5effecc71e19149053a33484f2a91c Mon Sep 17 00:00:00 2001 From: Justus Perlwitz Date: Thu, 28 Sep 2023 10:22:17 +0900 Subject: [PATCH 1/4] Format code using prettier - Fix tiny issues here and there, like whitespace inconsistencies, arrow function syntax, and so on. - Add a .editorconfig, which prettier reads - Add two shortcuts two package.json that check for correct format or correct the formatting, both using prettier --- .editorconfig | 14 ++++++++++++++ package-lock.json | 22 ++++++++++++++++++++++ package.json | 5 ++++- src/index.ts | 34 ++++++++++++++++++++-------------- src/lib/constants.ts | 6 +++--- 5 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8a7f074 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[src/*.ts] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true +max_line_length = 80 diff --git a/package-lock.json b/package-lock.json index 95bde19..acab9aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "jsdom": "^22.1.0", "mock-socket": "^9.2.1", "npm-upgrade": "^3.1.0", + "prettier": "^3.0.3", "ts-jest": "^29.1.0", "typescript": "^5.1.3" } @@ -6430,6 +6431,21 @@ "node": ">=4" } }, + "node_modules/prettier": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz", + "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", @@ -12974,6 +12990,12 @@ "integrity": "sha512-ravE6m9Atw9Z/jjttRUZ+clIXogdghyZAuWJ3qEzjT+jI/dL1ifAqhZeC5VHzQp1MSt1+jxKkFNemj/iO7tVUA==", "dev": true }, + "prettier": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz", + "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==", + "dev": true + }, "pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", diff --git a/package.json b/package.json index 0dbfdb3..2b509b4 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "jsdom": "^22.1.0", "mock-socket": "^9.2.1", "npm-upgrade": "^3.1.0", + "prettier": "^3.0.3", "ts-jest": "^29.1.0", "typescript": "^5.1.3" }, @@ -24,7 +25,9 @@ "build": "npx tsc --project tsconfig.json", "watch": "npx tsc --project tsconfig.json --watch", "cover": "npx jest --coverage --coverageReporters=text-lcov | coveralls", - "test": "npx jest --coverage" + "test": "npx jest --coverage", + "prettier": "prettier src --write", + "check-prettier": "prettier src --check" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index fa75e2c..f161c89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,14 @@ // File Dependencies -import { WS_EVENT_NAMES, DATA_STORAGE_TYPES, DEFAULT_EVENT_LISTENERS_OBJECT } from "./lib/constants"; +import { + WS_EVENT_NAMES, + DATA_STORAGE_TYPES, + DEFAULT_EVENT_LISTENERS_OBJECT, +} from "./lib/constants"; import { serialize, deserialize } from "./lib/dataTransformer"; -import { PartialEventListenersInterface, EventListenersInterface } from "./lib/validators"; +import { + PartialEventListenersInterface, + EventListenersInterface, +} from "./lib/validators"; interface StorageParams { storageType: string; @@ -93,7 +100,7 @@ export default class Sarus { retryProcessTimePeriod, // TODO - write a test case to check this retryConnectionDelay, storageType = "memory", - storageKey = "sarus" + storageKey = "sarus", } = props; this.eventListeners = this.auditEventListeners(eventListeners); @@ -246,15 +253,14 @@ export default class Sarus { error: [], close: [], }; - + const mergedEventListeners: EventListenersInterface = { ...defaultEventListeners, ...eventListeners, } as EventListenersInterface; // Type assertion added here - + return mergedEventListeners; } - /** * Connects the WebSocket client, and attaches event listeners @@ -310,7 +316,7 @@ export default class Sarus { const eventFunctions = this.eventListeners[eventName]; if (eventFunctions && eventFunctions.indexOf(eventFunc) !== -1) { throw new Error( - `${eventFunc.name} has already been added to this event Listener` + `${eventFunc.name} has already been added to this event Listener`, ); } if (eventFunctions && eventFunctions instanceof Array) { @@ -347,7 +353,7 @@ export default class Sarus { | { doNotThrowError: boolean; } - | undefined + | undefined, ) { if (!existingFunc) { if (!(opts && opts.doNotThrowError)) { @@ -366,7 +372,7 @@ export default class Sarus { off( eventName: string, eventFuncOrName: Function | string, - opts?: { doNotThrowError: boolean } | undefined + opts?: { doNotThrowError: boolean } | undefined, ) { const existingFunc = this.findFunction(eventName, eventFuncOrName); if (existingFunc) { @@ -421,7 +427,7 @@ export default class Sarus { */ attachEventListeners() { const self: any = this; - WS_EVENT_NAMES.forEach(eventName => { + WS_EVENT_NAMES.forEach((eventName) => { self.ws[`on${eventName}`] = (e: Function) => { self.eventListeners[eventName].forEach((f: Function) => f(e)); if (eventName === "close" && self.reconnectAutomatically) { @@ -433,16 +439,16 @@ export default class Sarus { } /** - * Removes the event listeners from a closed WebSocket instance, so that + * Removes the event listeners from a closed WebSocket instance, so that * they are cleaned up */ removeEventListeners() { const self: any = this; - WS_EVENT_NAMES.forEach(eventName => { + WS_EVENT_NAMES.forEach((eventName) => { if (self.ws.listeners && self.ws.listeners[eventName]) { - self.ws.listeners[eventName].forEach((iel:Function) => { + self.ws.listeners[eventName].forEach((iel: Function) => { self.ws.removeEventListener(eventName, iel); - }) + }); } }); } diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 2a1c234..e0fcb07 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -10,7 +10,7 @@ export const WS_EVENT_NAMES: Array = [ "open", "close", "message", - "error" + "error", ]; /** @@ -34,5 +34,5 @@ export const DEFAULT_EVENT_LISTENERS_OBJECT: EventListenersInterface = { open: [], message: [], error: [], - close: [] -}; \ No newline at end of file + close: [], +}; From 34cb84bd9d6e9b561eca3281ea572a4c0fa7cb1c Mon Sep 17 00:00:00 2001 From: Justus Perlwitz Date: Thu, 28 Sep 2023 10:24:55 +0900 Subject: [PATCH 2/4] Add prettier github workflow --- .github/workflows/prettier.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/prettier.yml diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml new file mode 100644 index 0000000..3bd4a7e --- /dev/null +++ b/.github/workflows/prettier.yml @@ -0,0 +1,28 @@ +name: Lint + +on: + push: + branches: + - master + pull_request: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4.1.0 + - name: Setup Node.js + uses: actions/setup-node@v3.8.1 + - name: Clean NPM install + run: npm clean-install + - name: Check with prettier + run: npm run check-prettier From 7c55d8dc6472f3487bac226bad9086936669a0f8 Mon Sep 17 00:00:00 2001 From: Justus Perlwitz Date: Thu, 28 Sep 2023 10:27:27 +0900 Subject: [PATCH 3/4] Change node build targets to latest LTS and current As detailed on https://github.com/nodejs/release#release-schedule, the release schedule has changed since this node.js.yml workflow has last been touched. 18.x will expire in October this year 2023. 14.x has been EOL'd in 2021-10-19. --- .github/workflows/node.js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 2d8bf2c..bb853a9 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: - node-version: [14.x] + node-version: [18.x, 20.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: From 9c8152af1cb6d7861bd63b5a2fc9ee45718b94d5 Mon Sep 17 00:00:00 2001 From: Justus Perlwitz Date: Thu, 28 Sep 2023 18:21:20 +0900 Subject: [PATCH 4/4] Update LICENSE authors and populate contributors --- LICENSE | 2 +- package.json | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 05f4a04..6a6e704 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Anephenix OÜ +Copyright (c) 2019-2023 The Sarus Authors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/package.json b/package.json index 2b509b4..e2e13f9 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,22 @@ "version": "0.4.6", "description": "A WebSocket JavaScript library", "main": "dist/index.js", + "contributors": [ + { + "name": "Paul Jensen", + "email": "paulbjensen@gmail.com", + "url": "https://paulbjensen.co.uk" + }, + { + "name": "Chris Lajoie", + "email": "chris@ettaviation.com" + }, + { + "name": "Justus Perlwitz", + "email": "justus@jwpconsulting.net", + "url": "https://www.jwpconsulting.net" + } + ], "devDependencies": { "@babel/parser": "^7.22.5", "@babel/types": "^7.22.5",