Skip to content

Commit

Permalink
Adding Parms
Browse files Browse the repository at this point in the history
Adding Path
Added On Events
Added Progress
Updated Fetch & Request
  • Loading branch information
Universal Web committed Aug 15, 2023
1 parent 8268569 commit 1b365d2
Show file tree
Hide file tree
Showing 15 changed files with 366 additions and 139 deletions.
292 changes: 164 additions & 128 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions scripts/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const domainProfile = await createProfile({
// packetCompression: true,
// headerCompression: true,
// footerCompression: true,
// pathCompression: true,
// parameter Compression: true,
autoLogin: true,
// The cryptographic algo used, intended, and or generated with the provided public key
keypairType: 'ed25519',
Expand Down
16 changes: 15 additions & 1 deletion udsp/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class UDSP {
connectionIdSize,
maxPayloadSize,
maxDataSize,
maxHeadSize
maxHeadSize,
maxPathSize,
maxParamsSize
} = this;
if (maxPayloadSize) {
if (!maxDataSize) {
Expand All @@ -18,6 +20,12 @@ export class UDSP {
if (!maxHeadSize) {
this.maxHeadSize = maxPayloadSize;
}
if (!maxParamsSize) {
this.maxParamsSize = maxPayloadSize;
}
if (!maxPathSize) {
this.maxPathSize = maxPayloadSize;
}
} else {
const packetInitialOverhead = 2;
this.encryptPacketOverhead = this.cryptography.encryptOverhead;
Expand All @@ -31,6 +39,12 @@ export class UDSP {
if (!maxHeadSize) {
this.maxHeadSize = this.maxPayloadSize - this.emptyPayloadOverHeadSize;
}
if (!maxParamsSize) {
this.maxParamsSize = this.maxPayloadSize - this.emptyPayloadOverParamsSize;
}
if (!maxPathSize) {
this.maxPathSize = this.maxPayloadSize - this.emptyPayloadOverPathSize;
}
console.log(`packetInitialOverhead: ${packetInitialOverhead} bytes`);
}
console.log(`encryptPacketOverhead: ${this.encryptPacketOverhead} bytes`);
Expand Down
3 changes: 3 additions & 0 deletions udsp/encodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export async function encodePacket(message, source, destination, headers, footer
}
if (message) {
console.log(message);
if (message?.frame?.length === 1) {
message.frame = message.frame[0];
}
}
if (headers) {
console.log(headers);
Expand Down
6 changes: 5 additions & 1 deletion udsp/request/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Base } from './base.js';
import { request } from '#udsp/requestTypes/request';
export class Ask extends Base {
constructor(method = 'get', path, data, head, options = {}, source) {
constructor(method = 'get', path, params, data, head, options = {}, source) {
super(options, source);
const {
requestQueue,
Expand All @@ -26,6 +26,10 @@ export class Ask extends Base {
this.path = path;
this.pathSize = path.length;
}
if (params) {
this.request.params = params;
this.params = params;
}
if (data) {
this.request.data = data;
}
Expand Down
74 changes: 73 additions & 1 deletion udsp/request/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class Base {
maxPacketSize,
maxDataSize,
maxHeadSize,
maxPathSize,
maxParamsSize,
packetMaxPayloadSafeEstimate
} = source;
if (events) {
Expand All @@ -40,6 +42,12 @@ export class Base {
if (maxHeadSize) {
this.maxHeadSize = maxHeadSize;
}
if (maxPathSize) {
this.maxPathSize = maxPathSize;
}
if (maxParamsSize) {
this.maxParamsSize = maxParamsSize;
}
if (packetMaxPayloadSafeEstimate) {
this.packetMaxPayloadSafeEstimate = packetMaxPayloadSafeEstimate;
}
Expand Down Expand Up @@ -78,6 +86,34 @@ export class Base {
this.readyState = 2;
this.headAssembled = true;
}
setParams() {
clear(this.incomingParamsPackets);
if (this.incomingParams?.length) {
const paramsCompiled = Buffer.concat(this.incomingParams);
clearBuffer(this.incomingParams);
this.params = decode(paramsCompiled);
paramsCompiled.fill(0);
console.log('HEAD SET', this.params);
} else {
this.params = {};
}
this.readyState = 2;
this.paramsAssembled = true;
}
setPath() {
clear(this.incomingPathPackets);
if (this.incomingPath?.length) {
const pathCompiled = Buffer.concat(this.incomingPath);
clearBuffer(this.incomingPath);
this.path = decode(pathCompiled);
pathCompiled.fill(0);
console.log('HEAD SET', this.path);
} else {
this.path = {};
}
this.readyState = 2;
this.pathAssembled = true;
}
async assembleHead() {
if (this.headAssembled) {
return console.log('Head already assembled');
Expand Down Expand Up @@ -259,14 +295,44 @@ export class Base {
outgoingHeadPackets[packetId] = message;
// message.offset = safeEndIndex;
if (safeEndIndex === headSize) {
message.last = true;
break;
}
packetId++;
currentBytePosition += maxHeadSize;
}
console.log('outgoingHeadSize', source.outgoingHeadSize);
}
async pathPacketization() {
const {
maxPathSize,
isAsk,
outgoingPathPackets
} = this;
const source = (this.isAsk) ? this.request : this.response;
console.log('pathPacketization', source.path);
this.outgoingPath = encode(source.path);
this.outgoingPathSize = this.outgoingPath.length;
console.log('outgoingPathSize', source.outgoingPathSize);
let currentBytePosition = 0;
let packetId = 0;
const pathSize = this.outgoingPathSize;
console.log('maxPathSize', maxPathSize);
while (currentBytePosition < pathSize) {
const message = this.getPacketTemplate();
message.frame.push(packetId);
const endIndex = currentBytePosition + maxPathSize;
const safeEndIndex = endIndex > pathSize ? pathSize : endIndex;
message.path = this.outgoingPath.subarray(currentBytePosition, safeEndIndex);
outgoingPathPackets[packetId] = message;
// message.offset = safeEndIndex;
if (safeEndIndex === pathSize) {
break;
}
packetId++;
currentBytePosition += maxPathSize;
}
console.log('outgoingPathSize', source.outgoingPathSize);
}
async dataPacketization() {
const {
isAsk,
Expand Down Expand Up @@ -404,8 +470,14 @@ export class Base {
};
outgoingDataPackets = [];
outgoingHeadPackets = [];
outgoingPathPackets = [];
outgoingParamsPackets = [];
incomingHeadPackets = [];
incomingPathPackets = [];
incomingParamsPackets = [];
incomingHead = [];
incomingPath = [];
incomingParams = [];
incomingDataPackets = [];
incomingData = [];
incomingAks = [];
Expand Down
3 changes: 2 additions & 1 deletion udsp/request/onData.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { progress } from '@universalweb/acid';
export async function onData(message) {
console.log('On Data event');
if (this.totalIncomingDataSize) {
if (this.currentIncomingDataSize > 0) {
this.incomingProgress = (this.currentIncomingDataSize / this.totalIncomingDataSize) * 100;
this.incomingProgress = progress(this.totalIncomingDataSize, this.currentIncomingDataSize);
}
console.log('Incoming Progress', this.incomingProgress);
}
Expand Down
13 changes: 13 additions & 0 deletions udsp/request/onHead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { progress } from '@universalweb/acid';
export async function onHead(message) {
console.log('On Head event');
if (this.totalIncomingHeadSize) {
if (this.currentIncomingHeadSize > 0) {
this.incomingProgress = progress(this.totalIncomingHeadSize, this.currentIncomingHeadSize);
}
console.log('Incoming Progress', this.incomingProgress);
}
if (this.events.head) {
this.events.head(message.head, message.pid);
}
}
42 changes: 40 additions & 2 deletions udsp/request/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export async function onPacket(packet) {
console.log('On Packet event', message);
// console.log(packet);
const {
// main data payload
data,
// URL or Path Endpoint for API or dynamic path related usage
path,
// Path parameters
params,
// header payload
head,
frame,
// main data payload
data,
// Packet total
hpt: totalIncomingUniqueHeadPackets,
dpt: totalIncomingUniqueDataPackets,
Expand Down Expand Up @@ -59,6 +63,40 @@ export async function onPacket(packet) {
this.assembleHead();
}
}
if (path && !this.incomingPathPackets[packetId]) {
this.totalReceivedUniquePackets++;
this.incomingPathPackets[packetId] = message;
this.incomingPath[packetId] = message.path;
this.totalReceivedUniquePathPackets++;
this.currentIncomingPathSize += path.length;
if (this.missingPathPackets.has(packetId)) {
this.missingPathPackets.delete(packetId);
}
if (this.onPath) {
await this.onPath(message);
}
console.log(this, this.currentIncomingPathSize);
if (this.totalIncomingPathSize === this.currentIncomingPathSize) {
this.assemblePath();
}
}
if (params && !this.incomingParamsPackets[packetId]) {
this.totalReceivedUniquePackets++;
this.incomingParamsPackets[packetId] = message;
this.incomingParams[packetId] = message.params;
this.totalReceivedUniqueParamsPackets++;
this.currentIncomingParamsSize += params.length;
if (this.missingParamsPackets.has(packetId)) {
this.missingParamsPackets.delete(packetId);
}
if (this.onParams) {
await this.onParams(message);
}
console.log(this, this.currentIncomingParamsSize);
if (this.totalIncomingParamsSize === this.currentIncomingParamsSize) {
this.assembleParams();
}
}
if (data && !this.incomingDataPackets[packetId]) {
this.totalReceivedUniquePackets++;
this.incomingDataPackets[packetId] = message;
Expand Down
13 changes: 13 additions & 0 deletions udsp/request/onParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { progress } from '@universalweb/acid';
export async function onParams(message) {
console.log('On Params event');
if (this.totalIncomingParamsSize) {
if (this.currentIncomingParamsSize > 0) {
this.incomingProgress = progress(this.totalIncomingParamsSize, this.currentIncomingParamsSize);
}
console.log('Incoming Progress', this.incomingProgress);
}
if (this.events.params) {
this.events.params(message.params, message.pid);
}
}
13 changes: 13 additions & 0 deletions udsp/request/onPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { progress } from '@universalweb/acid';
export async function onPath(message) {
console.log('On Path event');
if (this.totalIncomingPathSize) {
if (this.currentIncomingPathSize > 0) {
this.incomingProgress = progress(this.totalIncomingPathSize, this.currentIncomingPathSize);
}
console.log('Incoming Progress', this.incomingProgress);
}
if (this.events.path) {
this.events.path(message.path, message.pid);
}
}
7 changes: 5 additions & 2 deletions udsp/request/reply.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
isEmpty, isBuffer, promise, eachArray, assign, construct, stringify, hasValue, get, objectSize
isEmpty, isBuffer, promise, eachArray, assign, construct, stringify, hasValue, get, objectSize, isArray
} from '@universalweb/acid';
import { decode, encode } from 'msgpackr';
import {
Expand All @@ -16,7 +16,10 @@ export class Reply extends Base {
console.log('Setting up new reply');
const thisReply = this;
const { message } = request;
const id = message?.frame[0];
if (!message.frame) {
return this.destroy('No frame in message');
}
const id = (isArray(message?.frame)) ? message.frame[0] : message.frame;
this.id = id;
const { replyQueue, } = source;
this.events = source.events;
Expand Down
5 changes: 4 additions & 1 deletion udsp/requestTypes/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
success, failed, imported, msgSent, info
} from '#logs';
import { promise, construct, isString } from '@universalweb/acid';
// If path arg has params in it then paramArg becomes dataArg
// params support both Complex Data Binary Supported Params and simple traditional URL percent encoded params
export async function fetchRequest(path, config = {}) {
info(`FETCH => ${path}`);
const request = await this.request(config.method, path, config.data || config.body, config.head || config.headers, config.options);
const request = await this.request(config.method, path, config.params || config.param,
config.data || config.body, config.head || config.headers, config.options);
return request.send();
}
8 changes: 6 additions & 2 deletions udsp/requestTypes/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
import {
promise, construct, isString, isPlainObject
} from '@universalweb/acid';
export async function request(methodArg, pathArg, dataArg, headersArg, optionsArg) {
// If path arg has params in it then paramArg becomes dataArg
// params support both Complex Data Binary Supported Params and simple traditional URL percent encoded params
export async function request(methodArg, pathArg, paramArg, dataArg, headersArg, optionsArg) {
let method = methodArg;
let path = pathArg;
let params = paramArg;
let data = dataArg;
let options = optionsArg;
let head = headersArg;
Expand All @@ -16,9 +19,10 @@ export async function request(methodArg, pathArg, dataArg, headersArg, optionsAr
path = methodArg.path;
method = methodArg.method;
head = methodArg.head || methodArg.headers;
params = methodArg.param || methodArg.params;
}
info(`Request Function: ${method || 'get'} ${path}`);
const ask = this.ask(method, path, data, head, options);
const ask = this.ask(method, path, params, data, head, options);
console.log(data, ask);
return ask;
}
8 changes: 8 additions & 0 deletions udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class Client {
maxPacketSize,
maxDataSize,
maxHeadSize,
maxPathSize,
maxParamsSize,
packetMaxPayloadSafeEstimate
} = server;
if (maxPacketSize) {
Expand All @@ -55,6 +57,12 @@ export class Client {
if (maxHeadSize) {
this.maxHeadSize = maxHeadSize;
}
if (maxPathSize) {
this.maxPathSize = maxPathSize;
}
if (maxParamsSize) {
this.maxHeadSize = maxParamsSize;
}
if (packetMaxPayloadSafeEstimate) {
this.packetMaxPayloadSafeEstimate = packetMaxPayloadSafeEstimate;
}
Expand Down

0 comments on commit 1b365d2

Please sign in to comment.