Skip to content

Commit

Permalink
Cleanup all on Packet events
Browse files Browse the repository at this point in the history
Simplified and organized files/functions
utils updated
  • Loading branch information
tomekmarchi committed Nov 20, 2023
1 parent 9f6e3c4 commit b382b43
Show file tree
Hide file tree
Showing 21 changed files with 801 additions and 883 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
"error",
"tab",
{
"SwitchCase": 1,
"ObjectExpression": 1,
"ImportDeclaration": 1
}
Expand Down
1,080 changes: 517 additions & 563 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"@rollup/plugin-terser": "^0.4.0",
"@universalweb/acid": "^3.0.39",
"@universalweb/acid": "^3.0.63",
"@universalweb/docredux": "^3.2.2",
"auto-changelog": "^2.4.0",
"axios": "^1.5.1",
Expand Down
23 changes: 1 addition & 22 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { watch } from '#watch';
// Client specific imports to extend class
import { emit } from '../requestMethods/emit.js';
import { uwRequest } from '#udsp/requestMethods/request';
import { processFrame } from './processFrame.js';
import { processFrame } from '../processFrame.js';
import { onPacket } from './onPacket.js';
import { onListening } from './listening.js';
import { keychainGet } from '#keychain';
Expand Down Expand Up @@ -455,26 +455,6 @@ export class Client extends UDSP {
await this.calculatePacketOverhead();
this.handshakeCompleted();
}
proccessProtocol(rpc, frame, header) {
switch (rpc) {
case 0:
this.intro(frame, header);
break;
default:
console.trace('Unknown Protocol Packet', frame, header);
break;
}
}
proccessProtocolPacketFrame(frame, header) {
const rpc = frame[1];
console.log('Processing Protocol Packet Frame', frame);
this.proccessProtocol(rpc, frame, header);
}
proccessProtocolPacketHeader(frame, header) {
const rpc = header[1];
console.log('Processing Protocol Packet Header', header);
this.proccessProtocol(rpc, frame, header);
}
request = uwRequest;
fetch = fetchRequest;
post = post;
Expand Down Expand Up @@ -502,5 +482,4 @@ export async function client(configuration) {
}
// Add the request export here for simple auto connect and then just grab contents to return called UDSP
// client is for a longer stateful connection request to a remote server
// udsp is for a single request to a remote server then closes after response
export { getCertificate };
24 changes: 5 additions & 19 deletions udsp/client/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
import { decode, encode } from '#utilities/serialize';
import { decrypt, createSessionKey } from '#crypto';
import { decodePacket, decodePacketHeaders } from '#udsp/encoding/decodePacket';
import { processFrame } from './processFrame.js';
import { processFrame } from '../processFrame.js';
import {
hasValue, isArray, isFalse, isNumber
} from '@universalweb/acid';
import { proccessProtocolPacketHeader } from '#udsp/proccessProtocolPacket';
export async function onPacket(packet) {
msgReceived('Packet Received');
const config = {
Expand All @@ -31,24 +32,9 @@ export async function onPacket(packet) {
} = config.packetDecoded;
console.log(config);
if (isFalse(config.isShortHeaderMode)) {
if (header && isArray(header)) {
const headerRPC = header[1];
if (isNumber(headerRPC)) {
await this.proccessProtocolPacketHeader(message, header);
}
}
}
if (message && isArray(message)) {
const streamId = message[0];
const messageRPC = message[1];
if (hasValue(streamId)) {
if (streamId === false) {
return this.proccessProtocolPacketFrame(message, header);
}
if (isNumber(messageRPC)) {
return processFrame(message, header, this);
}
}
await proccessProtocolPacketHeader(this, message, header);
}
processFrame(message, header, this, this.requestQueue);
return;
}

35 changes: 0 additions & 35 deletions udsp/client/processFrame.js

This file was deleted.

27 changes: 27 additions & 0 deletions udsp/proccessProtocolPacket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isNotNumber, isArray } from '@universalweb/acid';
import { destroy } from './request/destory.js';
export async function proccessProtocol(source, rpc, frame, header) {
console.log(`Processing Protocol Packet RPC ${rpc}`);
if (isNotNumber(rpc)) {
source.destroy('Invalid RPC Not a Number');
return;
}
switch (rpc) {
case 0:
source.intro(frame, header);
break;
default:
console.trace('Unknown Protocol Packet', frame, header);
break;
}
}
export async function proccessProtocolPacketFrame(source, frame, header) {
console.log('Processing Protocol Packet Frame');
proccessProtocol(source, frame[1], frame, header);
}
export async function proccessProtocolPacketHeader(source, frame, header) {
console.log('Processing Protocol Packet Header');
if (header && isArray(header)) {
proccessProtocol(source, header[1], frame, header);
}
}
34 changes: 34 additions & 0 deletions udsp/processFrame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
success, failed, imported, msgSent, info
} from '#logs';
import {
stringify,
hasValue,
isArray,
isNumber
} from '@universalweb/acid';
import { proccessProtocolPacketFrame } from '#udsp/proccessProtocolPacket';
export async function processFrame(frame, header, source, queue) {
if (!frame) {
return console.trace(`Invalid Frame Received`);
}
if (isArray(frame) && frame.length) {
const streamId = frame[0];
info(`Packet Received Stream ID: ${streamId}`);
if (hasValue(streamId)) {
if (streamId === false) {
proccessProtocolPacketFrame(source, frame, header);
return;
}
const requestObject = queue.get(streamId);
if (requestObject) {
requestObject.onFrame(frame, header);
return;
} else {
console.log('None found returning false', frame);
return false;
}
}
}
}

6 changes: 1 addition & 5 deletions udsp/request/destory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ export async function destroy(err) {
if (this.headCompiled) {
this.headCompiled.fill(0);
}
if (this.isAsk) {
this.source().requestQueue.delete(this.id);
} else {
this.source().replyQueue.delete(this.id);
}
this.source().requestQueue.delete(this.id);
}
Loading

0 comments on commit b382b43

Please sign in to comment.