Skip to content

Commit

Permalink
Initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
matteopt committed Sep 18, 2022
0 parents commit 665aab6
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish package to GitHub Packages
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v3
with:
node-version: '18.9.0'
registry-url: 'https://npm.pkg.github.com'
- run: npm ci
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Protocol for 2-way WebSocket communication
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@doggofrens/filesharing-ws-proto",
"version": "0.1.0",
"description": "Protocol for 2-way WebSocket communication",
"main": "src/main.ts",
"scripts": {
"build": "tsc",
"prepare" : "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/DoggoFrens/filesharing-ws-proto.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/DoggoFrens/filesharing-ws-proto/issues"
},
"homepage": "https://github.com/DoggoFrens/filesharing-ws-proto#readme",
"devDependencies": {
"typescript": "^4.8.3"
}
}
144 changes: 144 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
namespace Protocol {


/**
* Enum of all known message types.
*
* @enum {number}
*/
// NOTE: Use values between 0 and 255 to avoid overflow, as the value is converted to a single byte.
enum MessageType {
Ack = 0,
Chunk = 1
}


/**
* An abstract class that represents a WebSocket message.
*/
abstract class Message {

/**
* The type of the message.
*
* @type {MessageType}
* @readonly
*/
readonly type: MessageType;

/**
* @param type The type of the message.
* @see MessageType
*/
constructor(type: MessageType) {
this.type = type;
}

/**
* Returns the whole message as a Uint8Array.
*/
abstract toUint8Array(): Uint8Array;

}

/**
* A message that is sent to acknowledge a received message.
*
* @extends Message
*/
class AckMessage extends Message {

constructor() {
super(MessageType.Ack);
}

/**
* @override
*/
toUint8Array(): Uint8Array {
return new Uint8Array([this.type]);
}

}


/**
* A message that contains a chunk of data.
*
* @extends Message
*/
class ChunkMessage extends Message {

/**
* The number of the chunk, where 0 is the first chunk.
*
* @type {number}
* @readonly
*/
readonly chunkNumber: number;

/**
* The chunk data.
*
* @type {Uint8Array}
* @readonly
*/
readonly chunkBytes: Uint8Array;

/**
* @param chunkNumber The number of the chunk, where 0 is the first chunk.
* @param chunkBytes The chunk data.
*/
constructor(chunkNumber: number, chunkBytes: Uint8Array) {
super(MessageType.Chunk);
this.chunkNumber = chunkNumber;
this.chunkBytes = chunkBytes;
}

/**
* Parses a ChunkMessage from a Uint8Array.
*
* @param byteArray The byte array to parse.
* @param expectedChunkSize The expected size of the chunk, in bytes.
* @returns The parsed message.
*/
static fromUint8Array(byteArray: Uint8Array, expectedChunkSize?: number): ChunkMessage | null {
if (expectedChunkSize && byteArray.byteLength != 1 + expectedChunkSize) {
return null;
}

const chunkNumber = byteArray[0];
const chunkBytes = byteArray.slice(1);

return new ChunkMessage(chunkNumber, chunkBytes);
}

/**
* @override
*/
toUint8Array(): Uint8Array {
const byteArray = new Uint8Array(1 + this.chunkBytes.byteLength);
byteArray[0] = this.type;
byteArray.set(this.chunkBytes, 1);

return byteArray;
}

}

function parseMessage(byteArray: Uint8Array, expectedChunkSize?: number): Message | null {
const type = byteArray[0];

switch (type) {
case MessageType.Ack:
return new AckMessage();
case MessageType.Chunk:
return ChunkMessage.fromUint8Array(byteArray, expectedChunkSize);
default:
return null;
}
}

}

export default Protocol;
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules"]
}

0 comments on commit 665aab6

Please sign in to comment.