Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

[CM-1206] Cleanup build and convert to module #207

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
command: npm run build
- run:
name: Run tests
command: npm run test
command: npm run test:ci
- store_test_results:
path: test-results
- store_artifacts:
Expand Down
6 changes: 6 additions & 0 deletions mocha.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"spec": "test/*.spec.ts",
"node-option": [
"loader=ts-node/esm"
]
}
800 changes: 83 additions & 717 deletions package-lock.json

Large diffs are not rendered by default.

40 changes: 13 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@
"license": "Apache-2.0",
"private": false,
"name": "live-connect-common",
"type": "commonjs",
"type": "module",
"engines": {
"node": ">=18"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"main": "./dist/index.cjs",
"module": "./dist/index.cjs.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"require": "./dist/index.cjs",
"import": "./dist/index.cjs.mjs"
},
"./package.json": "./package.json"
},
"files": [
"dist/*"
],
"scripts": {
"build": "rollup -c rollup.config.mjs",
"test": "ts-mocha -p ./tsconfig-test.json 'test/**/*.ts'",
"build": "rollup -c rollup.config.js",
"test": "mocha --config mocha.json",
"test:ci": "mocha --config mocha.json -R mocha-junit-reporter -O mochaFile=test-results/results.xml",
"lint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
"fix": "eslint --fix 'src/**/*.ts' 'test/**/*.ts'",
"release:ci:prepatch": "release-it patch --preRelease=alpha-$(git rev-parse --short HEAD) --ci",
Expand All @@ -44,42 +45,27 @@
},
"devDependencies": {
"@rollup/plugin-strip": "^3.0.2",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@types/chai": "^4.3.5",
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.13",
"@types/chai": "^4.3.12",
"@types/chai-spies": "^1.0.6",
"@types/dirty-chai": "^2.0.2",
"@types/express": "^4.17.17",
"@types/js-cookie": "^3.0.3",
"@types/mocha": "^10.0.1",
"@types/sinon": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"chai": "^4.3.7",
"chai-spies": "^1.0.0",
"core-js": "^3.31.0",
"dirty-chai": "^2.0.1",
"eslint": "^8.43.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-typescript-formatter": "0.0.2",
"mocha": "^10.2.0",
"mocha-junit-reporter": "^2.2.0",
"mocha-junit-reporter": "^2.2.1",
"release-it": "^17.0.0",
"rollup": "^4.0.2",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-dts": "^6.0.1",
"rollup-plugin-mjs-entry": "^0.1.1",
"serialize-javascript": ">=6.0.1",
"sinon": "^17.0.0",
"sinon-chai": "^3.7.0",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"tslib": "^2.6.0",
"ts-node": "^10.9.2",
"typescript": "^5.1.3"
}
}
4 changes: 1 addition & 3 deletions rollup.config.mjs → rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cleaner from 'rollup-plugin-cleaner'
import mjsEntry from 'rollup-plugin-mjs-entry'
import dts from 'rollup-plugin-dts'
import del from "rollup-plugin-delete";
import terser from '@rollup/plugin-terser'

const OUTPUT_DIR = './dist'

Expand All @@ -15,7 +14,7 @@ export default [
{
file: `${OUTPUT_DIR}/index.js`,
format: 'cjs',
sourcemap: true
sourcemap: false
}
],
plugins: [
Expand All @@ -27,7 +26,6 @@ export default [
}
}),
strip(),
terser(),
mjsEntry() // https://nodejs.org/api/packages.html#packages_dual_commonjs_es_module_packages
]
},
Expand Down
8 changes: 4 additions & 4 deletions src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ERRORS_CHANNEL } from './consts'
import { EventBus } from './types'
import { wrapError } from './utils'
import { ERRORS_CHANNEL } from './consts.js'
import { EventBus } from './types.js'
import { wrapError } from './utils.js'

interface EventHandler {
callback: (data: unknown) => void
Expand Down Expand Up @@ -68,7 +68,7 @@ export class ReplayEmitter implements EventBus {
return this
}

off(name: string, callback: (event: unknown) => void): this {
off(name: string, callback?: (event: unknown) => void): this {
const handlers: EventHandler[] = this.data.h[name]
const liveHandlers: EventHandler[] = (handlers && callback && handlers.filter(h => h.unbound !== callback)) || []

Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './utils'
export * from './types'
export * from './bus'
export * from './consts'
export * from './utils.js'
export * from './types.js'
export * from './bus.js'
export * from './consts.js'
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface EventBus extends ErrorBus {
on<F extends ((event: unknown) => void)>(name: string, callback: F, ctx?: ThisParameterType<F>): this
once<F extends ((event: unknown) => void)>(name: string, callback: F, ctx?: ThisParameterType<F>): this
emit(name: string, event: unknown): this
off(name: string, callback: (event: unknown) => void): this
off(name: string, callback?: (event: unknown) => void): this
}

export interface ReadOnlyStorageHandler {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorDetails } from './types'
import { ErrorDetails } from './types.js'

export const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'

Expand Down
41 changes: 27 additions & 14 deletions test/bus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-nocheck
import { expect, spy, use } from 'chai'
import chai from 'chai'
import chaiSpies from 'chai-spies'
import { ERRORS_CHANNEL, ReplayEmitter, wrapError } from '../src'
import { ERRORS_CHANNEL, ReplayEmitter, wrapError } from '../src/index.js'

use(chaiSpies)
chai.use(chaiSpies)

const { expect, spy } = chai

describe('ReplayEmitter', () => {
it('should replay all stored events to a handler and continue handling when it is attached using `on`', () => {
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('ReplayEmitter', () => {
it('should handle misconfiguration and set default queue size', () => {
const emitter = new ReplayEmitter('not int')

expect(emitter.data.size).to.be.eq(5)
expect(getEmitterData(emitter).size).to.be.eq(5)
})

it('should properly turn off handlers with callbacks passed', () => {
Expand All @@ -83,15 +84,15 @@ describe('ReplayEmitter', () => {
emitter.on('test', callback1)
emitter.on('test', callback2)

expect(emitter.data.h.test.length).to.be.eq(2)
expect(getEmitterData(emitter).h.test.length).to.be.eq(2)

emitter.off('test', callback1)

expect(emitter.data.h.test.length).to.be.eq(1)
expect(getEmitterData(emitter).h.test.length).to.be.eq(1)

emitter.off('test', callback2)

expect(emitter.data.h.test).to.be.undefined()
expect(getEmitterData(emitter).h.test).to.be.undefined()
})

it('should turn off all handlers when no callback passed', () => {
Expand All @@ -103,11 +104,11 @@ describe('ReplayEmitter', () => {
emitter.on('test', callback1)
emitter.on('test', callback2)

expect(emitter.data.h.test.length).to.be.eq(2)
expect(getEmitterData(emitter).h.test.length).to.be.eq(2)

emitter.off('test')

expect(emitter.data.h.test).to.be.undefined()
expect(getEmitterData(emitter).h.test).to.be.undefined()
})

it('should wrap an exception with name, message and stack trace', () => {
Expand All @@ -127,9 +128,12 @@ describe('ReplayEmitter', () => {
it('should emit error with message to error namespace', () => {
const emitter = new ReplayEmitter(5)

const ex = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ex: any[] = []

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reporter = (error: any) => ex.push(error)

const reporter = (error) => ex.push(error)
emitter.on(ERRORS_CHANNEL, reporter)
emitter.emitErrorWithMessage('some name', 'message', new Error('the original message'))

Expand All @@ -143,9 +147,12 @@ describe('ReplayEmitter', () => {
it('should emit error using the message in the exception', () => {
const emitter = new ReplayEmitter(5)

const ex = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ex: any[] = []

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reporter = (error: any) => ex.push(error)

const reporter = (error) => ex.push(error)
emitter.on(ERRORS_CHANNEL, reporter)
emitter.emitError('some name', new Error('the original message'))

Expand All @@ -165,3 +172,9 @@ describe('ReplayEmitter', () => {
expect(messages.length).to.eql(0)
})
})

function getEmitterData(emitter: ReplayEmitter) {
// @ts-expect-error
const emmiterData = emitter.data
return emmiterData
}
2 changes: 1 addition & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, use } from 'chai'
import * as utils from '../src/utils'
import * as utils from '../src/utils.js'
import dirtyChai from 'dirty-chai'

use(dirtyChai)
Expand Down
6 changes: 0 additions & 6 deletions tsconfig-test.json

This file was deleted.

11 changes: 5 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"compilerOptions": {
"allowJs": false,
"typeRoots": ["./node_modules/@types"],
"types": ["mocha"],
"esModuleInterop": true,
"resolveJsonModule": true,
"module": "ESNext",
"strict": true,
"declaration": true,
"sourceMap": true,
"moduleResolution": "Node",
"sourceMap": false,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"noEmitOnError": true
},
"include": ["./src/**/*", "./test/**/*"]
"include": ["./src/**/*.ts", "./test/**/*.ts"]
}
Loading