Skip to content

Commit

Permalink
feat: add SSEConnection shared lib (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathannorris authored May 22, 2024
1 parent 55bef64 commit bd3ac04
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/shared/sse-connection/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions lib/shared/sse-connection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# sse-connection

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build sse-connection` to build the library.

## Running unit tests

Run `nx test sse-connection` to execute the unit tests via [Jest](https://jestjs.io).
14 changes: 14 additions & 0 deletions lib/shared/sse-connection/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */
export default {
displayName: 'shared-sse-connection',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{ tsconfig: '<rootDir>/tsconfig.spec.json' },
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../lib/coverage/shared/sse-connection'
}
40 changes: 40 additions & 0 deletions lib/shared/sse-connection/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "sse-connection",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "lib/shared/sse-connection/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/lib/shared/sse-connection",
"main": "lib/shared/sse-connection/src/index.ts",
"tsConfig": "lib/shared/sse-connection/tsconfig.lib.json",
"assets": ["lib/shared/sse-connection/*.md"]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["lib/shared/sse-connection/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "lib/shared/sse-connection/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions lib/shared/sse-connection/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/SSEConnection'
6 changes: 6 additions & 0 deletions lib/shared/sse-connection/src/lib/SSEConnection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Add empty test as a placeholder, passWithNoTests option wasn't working
describe('SSEConnection', () => {
it('empty test', () => {
expect(null).toBeNull()
})
})
49 changes: 49 additions & 0 deletions lib/shared/sse-connection/src/lib/SSEConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { DVCLogger } from '@devcycle/types'

export class SSEConnection {
private connection?: EventSource

constructor(
private url: string,
private onMessage: (message: unknown) => void,
private logger: DVCLogger,
) {
this.openConnection()
}

private openConnection() {
if (typeof EventSource === 'undefined') {
this.logger.warn(
'StreamingConnection not opened. EventSource is not available.',
)
return
}
this.connection = new EventSource(this.url, { withCredentials: true })
this.connection.onmessage = (event) => {
this.onMessage(event.data)
}
this.connection.onerror = () => {
this.logger.warn(
'StreamingConnection warning. Connection failed to establish.',
)
}
this.connection.onopen = () => {
this.logger.debug('StreamingConnection opened')
}
}

isConnected(): boolean {
return this.connection?.readyState === this.connection?.OPEN
}

reopen(): void {
if (!this.isConnected()) {
this.close()
this.openConnection()
}
}

close(): void {
this.connection?.close()
}
}
13 changes: 13 additions & 0 deletions lib/shared/sse-connection/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions lib/shared/sse-connection/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": []
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions lib/shared/sse-connection/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"target": "es2019",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
9 changes: 6 additions & 3 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"@devcycle/config-manager": [
"lib/shared/config-manager/src/index.ts"
],
"@devcycle/web-debugger": ["lib/web-debugger/index.ts"],
"@devcycle/web-debugger/react": ["lib/web-debugger/react.tsx"],
"@devcycle/js-client-sdk": ["sdk/js/src/index.ts"],
"@devcycle/js-cloud-server-sdk": [
"sdk/js-cloud-server/src/index.ts"
Expand All @@ -54,7 +52,12 @@
"@devcycle/server-request": [
"lib/shared/server-request/src/index.ts"
],
"@devcycle/types": ["lib/shared/types/src/index.ts"]
"@devcycle/sse-connection": [
"lib/shared/sse-connection/src/index.ts"
],
"@devcycle/types": ["lib/shared/types/src/index.ts"],
"@devcycle/web-debugger": ["lib/web-debugger/index.ts"],
"@devcycle/web-debugger/react": ["lib/web-debugger/react.tsx"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit bd3ac04

Please sign in to comment.