-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 665aab6
Showing
7 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Protocol for 2-way WebSocket communication |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |