Skip to content

Commit

Permalink
code enums
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Sep 2, 2023
1 parent ec51b85 commit cfc100d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 92 deletions.
11 changes: 11 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default [
del({
targets: [
'build/protobuf.*',
'build/transport_*.d.ts',
'build/json.d.ts',
'build/utils.d.ts',
],
hook: 'writeBundle',
runOnce: true,
Expand Down Expand Up @@ -48,6 +51,14 @@ export default [
targets: [
'build/cjs/index.d.ts',
'build/esm/index.d.ts',
'build/cjs/transport_*.d.ts',
'build/esm/transport_*.d.ts',
'build/cjs/protobuf.codec.d.ts',
'build/esm/protobuf.codec.d.ts',
'build/cjs/json.d.ts',
'build/cjs/utils.d.ts',
'build/esm/json.d.ts',
'build/esm/utils.d.ts',
],
hook: 'writeBundle',
runOnce: true,
Expand Down
2 changes: 1 addition & 1 deletion src/browser.protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* The code executes when loaded in a browser.
*/
import { Centrifuge } from './centrifuge';
import { ProtobufCodec } from './protobuf';
import { ProtobufCodec } from './protobuf.codec';

export default class CentrifugeProtobuf extends Centrifuge {
protected _formatOverride() {
Expand Down
72 changes: 36 additions & 36 deletions src/codes.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
export const errorCodes = {
timeout: 1,
transportClosed: 2,
clientDisconnected: 3,
clientClosed: 4,
clientConnectToken: 5,
clientRefreshToken: 6,
subscriptionUnsubscribed: 7,
subscriptionSubscribeToken: 8,
subscriptionRefreshToken: 9,
transportWriteError: 10,
connectionClosed: 11,
badConfiguration: 12,
};
export enum errorCodes {
timeout = 1,
transportClosed = 2,
clientDisconnected = 3,
clientClosed = 4,
clientConnectToken = 5,
clientRefreshToken = 6,
subscriptionUnsubscribed = 7,
subscriptionSubscribeToken = 8,
subscriptionRefreshToken = 9,
transportWriteError = 10,
connectionClosed = 11,
badConfiguration = 12,
}

export const connectingCodes = {
connectCalled: 0,
transportClosed: 1,
noPing: 2,
subscribeTimeout: 3,
unsubscribeError: 4
};
export enum connectingCodes {
connectCalled = 0,
transportClosed = 1,
noPing = 2,
subscribeTimeout = 3,
unsubscribeError = 4
}

export const disconnectedCodes = {
disconnectCalled: 0,
unauthorized: 1,
badProtocol: 2,
messageSizeLimit: 3
};
export enum disconnectedCodes {
disconnectCalled = 0,
unauthorized = 1,
badProtocol = 2,
messageSizeLimit = 3
}

export const subscribingCodes = {
subscribeCalled: 0,
transportClosed: 1
};
export enum subscribingCodes {
subscribeCalled = 0,
transportClosed = 1
}

export const unsubscribedCodes = {
unsubscribeCalled: 0,
unauthorized: 1,
clientClosed: 2
};
export enum unsubscribedCodes {
unsubscribeCalled = 0,
unauthorized = 1,
clientClosed = 2
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Centrifuge, UnauthorizedError } from './centrifuge';
import { Subscription } from './subscription';
export * from "./types";
export * from "./codes";

export {
Centrifuge,
Expand Down
54 changes: 54 additions & 0 deletions src/protobuf.codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as protobuf from 'protobufjs/light'
import * as protoJSON from './client.proto.json';
const proto = protobuf.Root.fromJSON(protoJSON);

const Command = proto.lookupType('protocol.Command');
const Reply = proto.lookupType('protocol.Reply');
const EmulationRequest = proto.lookupType('protocol.EmulationRequest');

export class ProtobufCodec {
name() {
return 'protobuf';
}

encodeEmulationRequest(req: any) {
const writer = protobuf.Writer.create();
EmulationRequest.encode(req, writer);
return writer.finish();
}

encodeCommands(commands: any[]) {
const writer = protobuf.Writer.create();
for (const i in commands) {
if (commands.hasOwnProperty(i)) {
const command = Object.assign({}, commands[i]);
Command.encodeDelimited(command, writer);
}
}
return writer.finish();
}

decodeReplies(data: any) {
const replies: any[] = [];
const reader = protobuf.Reader.create(new Uint8Array(data));
while (reader.pos < reader.len) {
const reply = Reply.decodeDelimited(reader);
replies.push(reply);
}
return replies;
}

decodeReply(data: any) {
const reader = protobuf.Reader.create(new Uint8Array(data));
while (reader.pos < reader.len) {
Reply.decodeDelimited(reader);
return {
ok: true,
pos: reader.pos
};
}
return {
ok: false
};
}
}
57 changes: 2 additions & 55 deletions src/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,8 @@
import { Centrifuge, UnauthorizedError } from './centrifuge';
import { Subscription } from './subscription';
import { ProtobufCodec } from './protobuf.codec';
export * from "./types";

import * as protobuf from 'protobufjs/light'
import * as protoJSON from './client.proto.json';
const proto = protobuf.Root.fromJSON(protoJSON);

const Command = proto.lookupType('protocol.Command');
const Reply = proto.lookupType('protocol.Reply');
const EmulationRequest = proto.lookupType('protocol.EmulationRequest');

export class ProtobufCodec {
name() {
return 'protobuf';
}

encodeEmulationRequest(req: any) {
const writer = protobuf.Writer.create();
EmulationRequest.encode(req, writer);
return writer.finish();
}

encodeCommands(commands: any[]) {
const writer = protobuf.Writer.create();
for (const i in commands) {
if (commands.hasOwnProperty(i)) {
const command = Object.assign({}, commands[i]);
Command.encodeDelimited(command, writer);
}
}
return writer.finish();
}

decodeReplies(data: any) {
const replies: any[] = [];
const reader = protobuf.Reader.create(new Uint8Array(data));
while (reader.pos < reader.len) {
const reply = Reply.decodeDelimited(reader);
replies.push(reply);
}
return replies;
}

decodeReply(data: any) {
const reader = protobuf.Reader.create(new Uint8Array(data));
while (reader.pos < reader.len) {
Reply.decodeDelimited(reader);
return {
ok: true,
pos: reader.pos
};
}
return {
ok: false
};
}
}
export * from "./codes";

class CentrifugeProtobuf extends Centrifuge {
protected _formatOverride() {
Expand Down

0 comments on commit cfc100d

Please sign in to comment.