Skip to content

Commit

Permalink
feat: add Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
thetayloredman committed Feb 20, 2023
1 parent d95b79d commit 21b2dd9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/shell/algorithms/Raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import type ShellAlgorithm from "./ShellAlgorithm";

/** The initial {@link ShellAlgorithm} used before any encryption is enabled. */
export default class RawShellAlgorithm implements ShellAlgorithm {
public encode(data: Buffer): Buffer {
return data;
}

public decode(packet: Buffer): Buffer {
return packet;
}
}
25 changes: 25 additions & 0 deletions src/shell/algorithms/ShellAlgorithm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/** Represents an algorithm for encoding/decoding data */
export default interface ShellAlgorithm {
/** Encodes data into a packet */
encode(data: Buffer): Buffer;
/** Decodes a packet into data */
decode(packet: Buffer): Buffer;
}
65 changes: 65 additions & 0 deletions src/shell/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import SafeEventEmitter from "../lib/SafeEventEmitter";
import type Stream from "../stream";
import { StreamEvents } from "../stream";
import type ShellAlgorithm from "./algorithms/ShellAlgorithm";

export enum ShellEvents {
/** Fired when a new packet is received */
Packet = "packet",
/** Fired after a Read Reset */
Reset = "reset",
/** Fired when the underlying {@link Stream} closes */
Close = "close"
}
export type ShellEventArguments = {
[ShellEvents.Packet]: [number, Buffer];
[ShellEvents.Reset]: [];
[ShellEvents.Close]: [];
};

/**
* Represents and handles the {@link https://github.com/ME1312/SubData-2/wiki/Protocol-Format#layer-2-the-shell SubData 2 Shell}.
* Given an {@link ShellAlgorithm}, call it with data, and hook onto an {@link Stream}.
*/
export default class Shell extends SafeEventEmitter<ShellEventArguments> {
private _algorithm: ShellAlgorithm;
private _stream: Stream;

public constructor(algorithm: ShellAlgorithm, stream: Stream) {
super();
this._algorithm = algorithm;
this._stream = stream;
this._stream.on(StreamEvents.Close, () => {
this.emit(ShellEvents.Close);
});
this._stream.on(StreamEvents.Packet, (size, packet) => {
this.emit(ShellEvents.Packet, size, this._algorithm.decode(packet));
});
this._stream.on(StreamEvents.Close, () => {
this.emit(ShellEvents.Close);
});
}

/** Send a packet. */
public send(data: Buffer): void {
this._stream.writePacket(this._algorithm.encode(data));
}
}

0 comments on commit 21b2dd9

Please sign in to comment.