Skip to content

Commit

Permalink
ES6 target
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Sep 6, 2023
1 parent 1999c7a commit ca2468a
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"eventsource": "^2.0.2",
"jest": "^28.1.0",
"protobufjs-cli": "^1.1.1",
"rollup": "^3.28.1",
"rollup": "^3.29.0",
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-typescript2": "^0.35.0",
"ts-jest": "^28.0.2",
Expand Down
4 changes: 0 additions & 4 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ export default [
{
file: 'build/index.js',
format: 'cjs',
sourcemap: true
},
{
file: 'build/index.mjs',
format: 'es',
sourcemap: true
}
]
},
Expand Down Expand Up @@ -67,12 +65,10 @@ export default [
{
file: 'build/cjs/protobuf.js',
format: 'cjs',
sourcemap: true
},
{
file: 'build/esm/protobuf.js',
format: 'es',
sourcemap: true
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions src/browser.protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import {
State, SubscriptionState,
} from './types';

// @ts-ignore – required for browser build.
Centrifuge.SubscriptionState = SubscriptionState;
// @ts-ignore – need for browser build.
Centrifuge.State = State
// @ts-ignore – need for browser build.
Centrifuge.UnauthorizedError = UnauthorizedError;

export default class CentrifugeProtobuf extends Centrifuge {
Expand Down
3 changes: 3 additions & 0 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
State, SubscriptionState,
} from './types';

// @ts-ignore – need for browser build.
Centrifuge.SubscriptionState = SubscriptionState;
// @ts-ignore – need for browser build.
Centrifuge.State = State
// @ts-ignore – need for browser build.
Centrifuge.UnauthorizedError = UnauthorizedError;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
5 changes: 4 additions & 1 deletion src/centrifuge.protobuf.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Centrifuge } from './protobuf'
import { DisconnectedContext, UnsubscribedContext, TransportName, PublicationContext, State, SubscriptionState } from './types';
import {
DisconnectedContext, UnsubscribedContext, TransportName,
PublicationContext, State, SubscriptionState
} from './types';
import { disconnectedCodes, unsubscribedCodes } from './codes';
import WebSocket from 'ws';
import { fetch } from 'undici';
Expand Down
43 changes: 23 additions & 20 deletions src/centrifuge.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Centrifuge } from './centrifuge'
import { DisconnectedContext, Error as CentrifugeError, PublicationContext, TransportName, UnsubscribedContext } from './types';
import {
DisconnectedContext, Error as CentrifugeError,
PublicationContext, TransportName, UnsubscribedContext, State, SubscriptionState
} from './types';
import { disconnectedCodes, unsubscribedCodes, connectingCodes } from './codes';

import WebSocket from 'ws';
Expand Down Expand Up @@ -44,11 +47,11 @@ test.each(transportCases)("%s: connects and disconnects", async (transport, endp

c.connect();
await c.ready(5000);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(c.state).toBe(State.Connected);

c.disconnect();
const ctx = await p;
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
expect(ctx.code).toBe(disconnectedCodes.disconnectCalled);
});

Expand Down Expand Up @@ -78,16 +81,16 @@ test.each(transportCases)("%s: subscribe and unsubscribe", async (transport, end

sub.subscribe()
await sub.ready(5000);
expect(sub.state).toBe(Centrifuge.SubscriptionState.Subscribed);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(sub.state).toBe(SubscriptionState.Subscribed);
expect(c.state).toBe(State.Connected);

sub.unsubscribe();
c.disconnect();

const ctx = await p;

expect(sub.state).toBe(Centrifuge.SubscriptionState.Unsubscribed);
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(sub.state).toBe(SubscriptionState.Unsubscribed);
expect(c.state).toBe(State.Disconnected);
expect(ctx.code).toBe(unsubscribedCodes.unsubscribeCalled)
});

Expand Down Expand Up @@ -185,13 +188,13 @@ test.each(transportCases)("%s: handles offline/online events", async (transport,

c.connect();
await c.ready(5000);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(c.state).toBe(State.Connected);

const offlineEvent = new Event('offline', { bubbles: true });
networkEventTarget.dispatchEvent(offlineEvent);

const ctx = await p;
expect(c.state).toBe(Centrifuge.State.Connecting);
expect(c.state).toBe(State.Connecting);
expect(ctx.code).toBe(connectingCodes.transportClosed);

const onlineEvent = new Event('online', { bubbles: true });
Expand All @@ -206,11 +209,11 @@ test.each(transportCases)("%s: handles offline/online events", async (transport,
})

await c.ready(5000);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(c.state).toBe(State.Connected);

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
});

test.each(transportCases.slice(0, 1))("%s: not connecting on online in disconnected state", async (transport, endpoint) => {
Expand All @@ -230,7 +233,7 @@ test.each(transportCases.slice(0, 1))("%s: not connecting on online in disconnec

c.connect();
await c.ready(5000);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(c.state).toBe(State.Connected);

let disconnectCalled: any;
const disconnectedPromise = new Promise<DisconnectedContext>((resolve, _) => {
Expand All @@ -242,11 +245,11 @@ test.each(transportCases.slice(0, 1))("%s: not connecting on online in disconnec

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);

const onlineEvent = new Event('online', { bubbles: true });
networkEventTarget.dispatchEvent(onlineEvent);
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
});

test.each(transportCases)("%s: subscribe and presence", async (transport, endpoint) => {
Expand All @@ -267,8 +270,8 @@ test.each(transportCases)("%s: subscribe and presence", async (transport, endpoi

sub.subscribe()
await sub.ready(5000);
expect(sub.state).toBe(Centrifuge.SubscriptionState.Subscribed);
expect(c.state).toBe(Centrifuge.State.Connected);
expect(sub.state).toBe(SubscriptionState.Subscribed);
expect(c.state).toBe(State.Connected);

const presence = await sub.presence();
expect(Object.keys(presence.clients).length).toBeGreaterThan(0);
Expand All @@ -287,7 +290,7 @@ test.each(transportCases)("%s: subscribe and presence", async (transport, endpoi

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
});

test.each(transportCases)("%s: connect disconnect loop", async (transport, endpoint) => {
Expand All @@ -314,7 +317,7 @@ test.each(transportCases)("%s: connect disconnect loop", async (transport, endpo
c.connect();
c.disconnect();
}
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
await disconnectedPromise;
});

Expand Down Expand Up @@ -346,7 +349,7 @@ test.each(transportCases)("%s: subscribe and unsubscribe loop", async (transport
sub.subscribe();
sub.unsubscribe();
}
expect(sub.state).toBe(Centrifuge.SubscriptionState.Unsubscribed);
expect(sub.state).toBe(SubscriptionState.Unsubscribed);
await unsubscribedPromise;

let disconnectCalled: any;
Expand All @@ -359,5 +362,5 @@ test.each(transportCases)("%s: subscribe and unsubscribe loop", async (transport

c.disconnect();
await disconnectedPromise;
expect(c.state).toBe(Centrifuge.State.Disconnected);
expect(c.state).toBe(State.Disconnected);
});
4 changes: 0 additions & 4 deletions src/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli
private _config: Options;
protected _codec: any;

static SubscriptionState: typeof SubscriptionState;
static State: typeof State;
static UnauthorizedError: typeof UnauthorizedError;

/** Constructs Centrifuge client. Call connect() method to start connecting. */
constructor(endpoint: string | Array<TransportEndpoint>, options?: Partial<Options>) {
super();
Expand Down
1 change: 1 addition & 0 deletions src/protobuf.codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Command = proto.lookupType('protocol.Command');
const Reply = proto.lookupType('protocol.Reply');
const EmulationRequest = proto.lookupType('protocol.EmulationRequest');

/** @internal */
export class ProtobufCodec {
name() {
return 'protobuf';
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"target": "ES6",
"module": "ES6",
"moduleResolution": "node",
"declaration": true,
"strict": true,
Expand All @@ -18,8 +18,9 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"sourceMap": true,
"sourceMap": false,
"stripInternal": true,
"inlineSourceMap": false,
"types": [
"node",
"jest"
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3228,10 +3228,10 @@ rollup-plugin-typescript2@^0.35.0:
semver "^7.3.7"
tslib "^2.4.0"

rollup@^3.28.1:
version "3.28.1"
resolved "https://registry.npmjs.org/rollup/-/rollup-3.28.1.tgz"
integrity sha512-R9OMQmIHJm9znrU3m3cpE8uhN0fGdXiawME7aZIpQqvpS/85+Vt1Hq1/yVIcYfOmaQiHjvXkQAoJukvLpau6Yw==
rollup@^3.29.0:
version "3.29.0"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.0.tgz#1b40e64818afc979c7e5bef93de675829288986b"
integrity sha512-nszM8DINnx1vSS+TpbWKMkxem0CDWk3cSit/WWCBVs9/JZ1I/XLwOsiUglYuYReaeWWSsW9kge5zE5NZtf/a4w==
optionalDependencies:
fsevents "~2.3.2"

Expand Down

0 comments on commit ca2468a

Please sign in to comment.