From 3b8a3f31a9f218a50226735c370dad5ff3bef270 Mon Sep 17 00:00:00 2001 From: Universal Web Date: Fri, 23 Jun 2023 23:01:27 -0400 Subject: [PATCH] 50% of API standardization Major Rework --- .cspell/custom-dictionary-workspace.txt | 1 + packetActions.md | 27 ++++- profiles/default-Ephemeral.cert | Bin 485 -> 485 bytes profiles/default-EphemeralPublic.cert | Bin 264 -> 264 bytes profiles/default-Master.cert | Bin 264 -> 264 bytes profiles/default-MasterPublic.cert | Bin 126 -> 126 bytes profiles/default-Profile.cert | Bin 769 -> 769 bytes scripts/certificates.js | 5 + scripts/simulateClient.js | 20 ++-- services/universal.web-Ephemeral.cert | Bin 953 -> 1002 bytes services/universal.web-EphemeralPublic.cert | Bin 732 -> 781 bytes services/universal.web-Master.cert | Bin 255 -> 255 bytes services/universal.web-MasterPublic.cert | Bin 117 -> 117 bytes services/universal.web-Profile.cert | Bin 1228 -> 1277 bytes udsp/ask.js | 92 +++++++----------- udsp/client/connect.js | 16 +-- udsp/client/emit.js | 4 +- udsp/client/index.js | 17 +++- udsp/client/onPacket.js | 6 +- udsp/client/processMessage.js | 2 +- udsp/cryptography.js | 2 +- udsp/decodePacket.js | 50 +++++----- udsp/encodePacket.js | 24 ++--- udsp/fetch.js | 18 ++++ udsp/reply.js | 16 +-- udsp/request.js | 37 +++---- udsp/request/assembleData.js | 9 +- udsp/request/base.js | 40 +++++--- udsp/request/bufferPacketization.js | 44 +++++++++ udsp/request/bufferToArrayChunks.js | 9 -- udsp/request/bufferToChunks.js | 9 ++ udsp/request/bufferToOutgoingPackets.js | 48 --------- udsp/request/fetch.js | 9 -- udsp/request/flush.js | 2 +- udsp/request/on.js | 21 ++-- udsp/request/onData.js | 12 ++- udsp/request/onPacket.js | 25 +++-- udsp/request/send.js | 17 ---- udsp/request/{sendOutgoing.js => sendAll.js} | 2 +- udsp/request/{sendCompleted.js => sendEnd.js} | 6 +- udsp/request/sendPacket.js | 20 ++-- udsp/server/actions/connect.js | 4 +- udsp/server/actions/file.js | 6 +- udsp/server/actions/reKey.js | 6 +- udsp/server/actions/state.js | 6 +- udsp/server/chunking.js | 6 +- udsp/server/clients/initialize.js | 2 +- udsp/server/emit.js | 4 +- udsp/server/onPacket.js | 6 +- 49 files changed, 333 insertions(+), 317 deletions(-) create mode 100644 udsp/fetch.js create mode 100644 udsp/request/bufferPacketization.js delete mode 100644 udsp/request/bufferToArrayChunks.js create mode 100644 udsp/request/bufferToChunks.js delete mode 100644 udsp/request/bufferToOutgoingPackets.js delete mode 100644 udsp/request/fetch.js delete mode 100644 udsp/request/send.js rename udsp/request/{sendOutgoing.js => sendAll.js} (86%) rename udsp/request/{sendCompleted.js => sendEnd.js} (52%) diff --git a/.cspell/custom-dictionary-workspace.txt b/.cspell/custom-dictionary-workspace.txt index 91c21287..ab0ae92f 100644 --- a/.cspell/custom-dictionary-workspace.txt +++ b/.cspell/custom-dictionary-workspace.txt @@ -7,6 +7,7 @@ keccak keccakprg kmac Nacks +Packetization ripemd Ristretto schnorr diff --git a/packetActions.md b/packetActions.md index d3eddde8..22853725 100644 --- a/packetActions.md +++ b/packetActions.md @@ -1,6 +1,27 @@ # Packet Actions numbers and their meanings -Packet Actions have two formats one which is numerical and one which is character based. Preferred is numerical as its smaller & faster. +Packet Actions have two formats one which is numerical for compression and one which is string based. Preferred is numerical its smaller & faster. -- 0 (connect) -- 1 (file) +- open +- connect +- close +- subscribe +- leave +- publish +- delete +- save +- update +- api (abstract request for application specific API like a websites functions) +- head +- post +- put +- stream +- download +- upload +- patch +- watch +- viat +- alive +- login +- logout +- rekey diff --git a/profiles/default-Ephemeral.cert b/profiles/default-Ephemeral.cert index 3f09539e745e30becf18591d317192a5feb5b8e6..20b7a6f4a1ff4afa3b69be42d70c288cfcd5ce19 100644 GIT binary patch literal 485 zcmcb|z_L0ywWuUBEi*Z>B=smG$2|u2rNt$QMJ0QK6H9ayj7$`~O7j$q42+Bw42-M{ zO{|Pe72JJ8mY1a#6=&w>F)k^~Oe{IPI5jV2Z*XZISVM4X0j7qP1*J(jnaSR%l}8j9 zJH?JD{5$ep_}lvnYm1*)-00(Z$rJoG<^GAx_c;oEx=KneE9TE$_C!V4 zLWAQ7WA}!yccm9KvG}q>O$ynrLsy?)4-R7I~m$|I}t~Y;W&c%fNJ5=Jl+$yW4PuRK4B=smG$2|u2rNt$QMJ0PfGD>w6jEoe#O7j$q42+Bw42`W! z%&d&e6x@A7mY1a#6=&w>F)k^~Oe{IPI5jV2uUk&ryAsHt$nrPsoT6lRFj_p3YIa{{yR&%Cao|X?stpTzBLWy1BTJvBdc9it2BH zigUA5Q=HG7FckeR)yD)iF#zF){=`2fj`KI?SZPH4>?)qEkZ{!gS%=+%o9zwpzc%(T z%w1JblvxH05|D!(!tQfgOy1O&vtf{}rdv4Vk-m7$51k*R{aZ^-hp)S}|d{5-}b zWtoX3hZm>jrR)tZ%>!!)PA$OHu(F^uDJL`8JGJtN0%NDx5ruz8z6*bQe_?I$6N?*t zJTG~I-^QF^`zRW+mG|2E+{EIN)S}?b^t{B9(xTKO4vioGH>6%kog+7W?uX<1Hg%j8 zv_3TRPI=Gy4%N;(7a!d}-u6C6p-)#y$z{d-*~^}&2wP}y9AWI<@b#|rq9zt!cBqK~ o2scFdSYQ10;~%%bU4HZup`X!`_l569-})2(;!^r&@$==p0KJuS(*OVf literal 264 zcmcb|z`nG&B(bPuZ%9U|j)IYqf>&vtf{}rdv4Wwom5G^^k(q+KZ^-hp)S}|d{5-}b zWtoX3hZm>jrR;Sp$^>f&PA$OHu(F^uDJL`8JGJtN!bfXA?};;Cov{|#pZjI^3zp6L zKaT`EevG^Fe%rOhPT^D5=Oz}Hq!tBdrspM=loq8PaggI!=lVIS@6zUd%Ipal5n^)3 zqQcWTO80+Yby8WDr9W-&DV6JvTtYV&7c!O@-(6AtEl_c8c4~_AnG=Sh-=+GPpe6<& n+|Zx+$HZ~|<{T@HsGnWMvlSAK+CS^CTX3_zA^z9K9)`I90f=sK diff --git a/profiles/default-Master.cert b/profiles/default-Master.cert index e3a9b46f4608494e0a82e1f3061c6c5f1d42f531..e59e1b5d7d1beb1fde87439f3492b24d7ef3aeef 100644 GIT binary patch literal 264 zcmcb|z`Qy+wWuUBEi*Z>B=tz$JqDJg#U+VFC3}MtOLP>BOccCI^AwB>jEofwjI0bz ztc*+*+6u$*&AG%2UZ%KT7ao^WkG3DPG+)qYUL4y2p{WB=tz$JqDJg#U+VFC3{0MN_7;Bj1;^|^AwB>jEofwjjc?~ ztc=VQ+6u$+3Qx6304}MT7ao^WkG3DPG+)qYUL4y{=`2fj`KI? zSZPH4>?)qEkZ{!gS%=+%o9zwpzc%(T%w1JblvxIJ2S~BQ=6?(?b5f_v|M-9Svs?b3 bzujBkvUbYLt4U}-; diff --git a/profiles/default-MasterPublic.cert b/profiles/default-MasterPublic.cert index 39a622ccb2957653c734a23d4eb7ae474a66daa0..ecbbd11ac16df5414849bab330aef73d81316f21 100644 GIT binary patch literal 126 zcmcb|z_PTsB(bPuZ*XFXj)IYif>&vtf{}rdv4Vk-m7$51k*R{aZ^-hp)S}|d{5;0R zsd*`TgG=+kN`q4iFqN(>C{4=AO!iK#JfaZcV}0@0kAK|$cKOjugnmX#-WR?bed|yB Ni%aRB#m|@X0su^yE~o$i literal 126 zcmcb|z_PTsB(bPuZ%9U|j)IYqf>&vtf{}rdv4Wwom5G^^k(q+KZ^-hp)S}|d{5;0R zsd*`T-HI~7N`q4iFqN(>C{4=AO!iK#JfhH__{YR?{^lGjji{ep#j_O>j@m!#uv>7m My&?YB#vX>b06-iq^#A|> diff --git a/profiles/default-Profile.cert b/profiles/default-Profile.cert index 771a85f14a7ae90b09f8fd5a2259f0e9b4ef7311..a48e30dd2a353d368c0b21c47b8f8537cf70e6f4 100644 GIT binary patch literal 769 zcmcb|z_c>8AR{$5wJ0&?9s|qjwI4Nff4Q7|%5 z@G8wyFfuSQRxmKKGBmL=GF5Q*4Ow26T2!2wpU1eQEHkm>@Z!|Gl)b^Fd0-8}sRfuC zRu+^dx;jB{Nwhw%a2|n^fOxW zzVO}XTYutTTuT2ee!iS{RY6f^88Apd4t98CvLy1<#YNv29s3v>5ct|OLyTWsiz{V? z%1_4P!-mtU2ziJy&lj{bbF52V`m6fTnsu9R&R^!T{=44%l{ps^_U}-M^Kz@Knm%FY zGLHwJOX9C=ZK^)MNz^&zx#l|Ki{}#_y}QTplyey*IPWnqBggfTI$-3J6xF0fGD!if tcw^_G44?F=3G0_?FjP)|$)~?QP)(3sFTqZ1^0ZBR4-zsAIe-bN1pwmNTk8M- literal 769 zcmcb|z_c>8AR{$5wJ0&?9s|qjwI4aq3gQ7|%6 z@G8wyFfuSQRxmWSGBL9@Z!|Gl)Y|6nP3gUsRfuC zRu+^dHZI_PAbc?^r!7TrE=Yo zOX%j}LdFu~yDO@{1uD+XPEBz>bHY&cyHp<&)WiUU8~PLfm^jYgoMWXC^|PyZwnD;D z`)3_?3vRYI#Q)mZ!!UPMK~ZKIFi1cSb_l!AX)$?Idq%$1{Y!s-?>7Dxn0tTohSP7t zu6CXLG)FvxkcTMqyi@&Cw#d&Sx$FHtUD}!CW?}Q9^-bihi5GWst5mnVnDgdNL;Je= zjQOEgvl_l>+cZ?HDz(;)Ie+quz4wit%MG@S%OJsdkAWFEu8-6KBcG(GCM}Xl3gFHE r7+&V2PM81j|L|wG{6BxYx4vcVl$TeN(0;^g&-Y^^A;XXZn2=fk+c8__ diff --git a/scripts/certificates.js b/scripts/certificates.js index 93938da1..321fe131 100644 --- a/scripts/certificates.js +++ b/scripts/certificates.js @@ -57,6 +57,11 @@ const domainProfile = await createProfile({ // When publicKey is set to true it will use the public key in the certificate as the main Viat wallet for the domain. If a string is provided then it would be the main wallet for the domain. publicKey: true }, + // This allows a browser or connection to use the realtime mode for the UDSP connection + realtime: true, + // blocked methods mean methods the server doesn't permit + // blockedMethods: ['open'], + allowedMethods: ['get', 'connect', 'open', 'close'], compression: true, headerCompression: true, autoLogin: true, diff --git a/scripts/simulateClient.js b/scripts/simulateClient.js index dd9ce005..7f4565e7 100644 --- a/scripts/simulateClient.js +++ b/scripts/simulateClient.js @@ -16,17 +16,25 @@ const uwClient = await client({ console.timeEnd('Connected'); // console.log('INTRO =>', uwClient); console.time('FileRequest'); -const fileRequest = await uwClient.request('file', { - path: 'index.html' -}, { - onData(data) { - console.log('onData', data); +const fileRequest = await uwClient.request({ + method: 'get', + data: { + path: 'index.html' } }); +// Get Method +fileRequest.on({ + data(...args) { + console.log('onData for simulate client', ...args); + } +}); +const response = await fileRequest.fetch(); +console.log(response.data); console.timeEnd('FileRequest'); +const fileFetch = await uwClient.fetch('index.html'); +console.log(fileFetch.toString()); console.timeEnd('Full'); // console.log('Request state', fileRequest); -console.log(fileRequest.response.body.toString()); /** */ // await uwClient.request('event', { // head: { diff --git a/services/universal.web-Ephemeral.cert b/services/universal.web-Ephemeral.cert index 8b9f9d48b37ed7e0bb06d22646198bc94ffab8f4..84e73c383783130b359b6835a85264bac412866d 100644 GIT binary patch delta 528 zcmdnV{)*l79s|qjwI4Nff4Q7|%5@G8wyFfuSQ zRxmKKGBmL=GTmsphtY6FQEFmNNoH>9;dO~QIr-(ODZZ&C8Tl#2Qx>PEmMl-s&&x|q zE?JUakeatNIVZn3b@E3>gZkjoJg}L;sRgKJD!BWGtSl%^%E?UjPOUtmuzvN!vyu*W zYPVN~^nIRfrnzYI?l*zIte*=8mayWivk%FH&~CT5kRSpU3rS;tFz(jcm+oKt8I+=OxO#FDuj9 zy?TMVz&dj;_J7JpylyVG``s9Ryi)xC{>Wv;g-Ln`OSfK<-WkAuQD-}Mt!46K{blzO Rj?CInxM0a~rP%4A3;>IDH9^J`F({r7NvZenpsYEf`zdR}5lX;JDC2NQ!AcNAH8 z4CD^w{IdR>bo-)}XV&f<`}L2lR!U8}AZ@YzamdE{u1^up>k}?-^Sf^HdmY0GJ(-Fb zd-XdRwf@|B#tStv0O1IM6`mYyaM=cU@rfFR~TS&)zF@p*|d@4rfv zWwtzZZfC)Yce6!q8~s1y?JM$0)@x6ymfe?U^Oi6O>B+Cnl|EQhqdVbqO6H~5@K#@& S8{sb{A26K={Pwf&8W#XR@y?vBP&A_DxYNmp_Z^+7m(xjZsWbf33G*=CxHHt&8D_{;ja zV1SvR5R2dK^|^_~C8vI!}OHzx1Gt=`DOG=AUk2shZytt#t!ebzJDCd{;=cL;gtvs`K=h&}* zY_(Eq(gkUY?T3ZFKA)8eH?{8GU+in1zi)cIm diff --git a/services/universal.web-Master.cert b/services/universal.web-Master.cert index b4d5cfb11e1c9ed54d78a7a8e901397c9ae32cf9..4aa8cf5c2ba1cff5ff4fd400ae0d2e9cbaa96096 100644 GIT binary patch literal 255 zcmcb|z`Qy+wWuUBEi*Z>B=tz?JqG5b#U+VFC3}MtOLP>BOccCI^AwB>jEofwjI0bz ztc*+*+n&oIOV>L33xY1*a6yIE~NB=tz?JqG5b#U+VFC3{0MN_7;Bj1;^|^AwB>jEofwjjc?~ ztc=VQ+&vtf{}rdv4Vk-m7$51k*R{aZ^+`*yp+AcrFmc# z!Knq9DpnShCgo%%d#6?&QJBqtVwqW>QCaK4v<{{uYu1vO4=vYx@2_ReF1csP#Z(Lc De^V!8 literal 117 zcmcb|z`V4$B(bPuZ%9U|j)IYqf>&vtf{}rdv4Wwom5G^^k(q+KZ^+`*yp+9eMVVj~ z!Knq9DpnShCgo%%d#6?&Q4m<+$-yQk8)Cnu^YE$vR+l!0{?hfj`9e0S{@>rIez)BK DfVnBe diff --git a/services/universal.web-Profile.cert b/services/universal.web-Profile.cert index 65ba84232738d7e9647d9ea0a7939e2e17df5063..457ab7d0083bf08eb56777ffab24b1dc4721c6cd 100644 GIT binary patch delta 748 zcmX@Z`ImEotSU3_JqEF*#U+VFC3}MtOLP>BOccCI^AwB>jEofwjI0bztc*-II_+UJ zTv3#om{XFOn|gR%Vopwed1{JpYDq?ZO7WD%>8T~llk@ZPQj<%TNEtqj<;iT=8C+RNyT0iZ|EY)d@U+w+AoLVkfd%aIRe9y)6F7JL+-b~_$8X16a z#BBZ(%gh3e%32qubucAavzEksXu0Nle=Tcv$vsOhrs7ovMVV!ZC8;16JBat1Ol6$4 zF|#b2`Js4`vh&q)>;L~eu1^zJkaKKgV_pODQ9V8{QRaPFnbz*r3)BVHnR~JSQ$FH# zbFtm;#_;2n;{W$YE-Nlf(mPnX^^)|?0RD?Q+qr8klOO9ZyO(fe)`r3bOO7kWP7h_6 ze2iJC9%r0`;%9McUdmpgW00i4i%2}f5ErPQdi=+vX_p%BX0`o{N-CF delta 698 zcmey%d4_X>tSZx;dki8=i%SxVO7@0il zS)a+E-mNGTtRgtI09A#8yKl(Kg3_d%%w+G>$|DNrH<)W)l2!HCnf24fm7im-UFu8R)MWoXT%Vg*T#{N8oSB}NSW;S)dc?uR;Kdz979In+Lpi^!KPTP3Xyuu; zJI8+gW2==?lP*YGY=0cGvA*k5g!B4@%iH{}oBUqKa6(U}V#Z$mPDZUicb@S=jSN6I zLSTg_2b-L1i2at%!>9gRUD_D>OV{h>3)!Ule}AL;-FB}kD9S8LEJ+2q*nxL@{`Zq| zv46Ii{pY?p`zde0uLBayjzK&x)n*0+F$cMw!J-=737=CkFU5wp`r6zGe<}HZ=|teSpMBT3 zCVyvEs>d0Fp!ivwnwPSd=olm^@Fua=_Jz;wNqs!gZ=U^`IlJ9 { + thisAsk.accept = accept; + }); + return awaitingResult; } isAsk = true; type = 'ask'; diff --git a/udsp/client/connect.js b/udsp/client/connect.js index eedd13f8..d09e43c3 100644 --- a/udsp/client/connect.js +++ b/udsp/client/connect.js @@ -5,25 +5,25 @@ export async function connect(message = {}) { const thisClient = this; // opn stands for open meaning connect to a server message.intro = 'Hello Server!'; - const result = await thisClient.request('opn', message); - console.log('Connect response', result.response.body); + const result = await thisClient.fetch('opn', message); + console.log('Connect response', result.response.data); const { - body, + data, state, time, // server connection ID sid } = result; if (state === 1 && sid) { - connected(body); + connected(data); thisClient.state = 1; thisClient.destination.id = sid; thisClient.lastPacketTime = Date.now(); thisClient.lastPacketGivenTime = time; - const bodyDecoded = decode(body); - console.log(bodyDecoded); - if (bodyDecoded.reKey) { - // thisClient.reKey(bodyDecoded.reKey); + const dataDecoded = decode(data); + console.log(dataDecoded); + if (dataDecoded.reKey) { + // thisClient.reKey(dataDecoded.reKey); } } console.log('-------CLIENT CONNECTED-------\n'); diff --git a/udsp/client/emit.js b/udsp/client/emit.js index 2a38163f..cf063365 100644 --- a/udsp/client/emit.js +++ b/udsp/client/emit.js @@ -2,7 +2,7 @@ import { success, failed, imported, msgSent, info } from '#logs'; imported('Emit'); -export async function emit(evnt, body) { +export async function emit(evnt, data) { const thisContext = this; const { packetIdGenerator } = thisContext; info(`Emitted`); @@ -10,7 +10,7 @@ export async function emit(evnt, body) { const message = { evnt, eid, - body + data }; return thisContext.send(message); } diff --git a/udsp/client/index.js b/udsp/client/index.js index 8a921387..ba8483cc 100644 --- a/udsp/client/index.js +++ b/udsp/client/index.js @@ -38,10 +38,12 @@ import { emit } from './emit.js'; import { request } from '#udsp/request'; import { cryptography } from '#udsp/cryptography'; import { processMessage } from './processMessage.js'; -import { onMessage } from './onPacket.js'; +import { onPacket } from './onPacket.js'; import { connect as clientConnect } from './connect.js'; import { onListening } from './listening.js'; import { keychainGet } from '#keychain'; +import { Ask } from '../ask.js'; +import { fetchRequest } from '../fetch.js'; // UNIVERSAL WEB Client Class export class Client { constructor(configuration) { @@ -141,7 +143,7 @@ export class Client { return thisClient.onListening(); }); this.socket.on('message', (packet, rinfo) => { - return thisClient.onMessage(packet, rinfo); + return thisClient.onPacket(packet, rinfo); }); } async initialize(configuration) { @@ -173,20 +175,24 @@ export class Client { this.socket.close(); Client.connections.delete(this.id); } + ask(message) { + const ask = construct(Ask, [message, this]); + return ask; + } connect = clientConnect; send = send; request = request; + fetch = fetchRequest; processMessage = processMessage; emit = emit; onListening = onListening; - onMessage = onMessage; + onPacket = onPacket; destination = {}; autoConnect = true; type = 'client'; isClient = true; description = `The Universal Web's UDSP client module to initiate connections to a UDSP Server.`; descriptor = 'UWClient'; - encoding = 'binary'; maxPacketSize = 1328; connectionIdSize = 8; static connections = new Map(); @@ -195,9 +201,10 @@ export class Client { queue = new Map(); packetIdGenerator = construct(UniqID); } -export async function client(configuration, ignoreConnections) { +export async function client(configuration) { console.log('Create Client'); const uwClient = await construct(Client, [configuration]); return uwClient; } +// Add the request export here for simple auto connect and then just grab contents to return export { getCertificate }; diff --git a/udsp/client/onPacket.js b/udsp/client/onPacket.js index 49e6ba7f..13714031 100644 --- a/udsp/client/onPacket.js +++ b/udsp/client/onPacket.js @@ -6,7 +6,7 @@ import { decrypt, createSessionKey } from '#crypto'; import { decodePacket, decodePacketHeaders } from '#udsp/decodePacket'; imported('Server onMessage'); import { processMessage } from './processMessage.js'; -export async function onMessage(packet) { +export async function onPacket(packet) { const { destination: source } = this; msgReceived('Packet Received'); const config = { @@ -15,12 +15,12 @@ export async function onMessage(packet) { packet, }; const wasHeadersDecoded = await decodePacketHeaders(config); - if (!wasHeadersDecoded || !config.packetDecoded.headers) { + if (!wasHeadersDecoded || !config.packetDecoded.header) { return failed('Invalid Packet Headers'); } const wasDecoded = await decodePacket(config); if (!wasDecoded) { - return failed('When decoding the packet but headers passed'); + return failed('When decoding the packet but header passed'); } processMessage(config.packetDecoded, this); } diff --git a/udsp/client/processMessage.js b/udsp/client/processMessage.js index 4a182fea..d8245191 100644 --- a/udsp/client/processMessage.js +++ b/udsp/client/processMessage.js @@ -8,7 +8,7 @@ import { imported('Client ProcessMessage'); export async function processMessage(packet, client) { const { - headers, + header, message, footer } = packet; diff --git a/udsp/cryptography.js b/udsp/cryptography.js index 08c13d6d..6aa401f1 100644 --- a/udsp/cryptography.js +++ b/udsp/cryptography.js @@ -206,7 +206,7 @@ class Cryptography { return this.hashMinMethod(...args); } generateConnectionID() { - const target = randomConnectionId(this.conifg.connectionIdSize || 8); + const target = randomConnectionId(this.config.connectionIdSize || 8); return target; } signKeypair(...args) { diff --git a/udsp/decodePacket.js b/udsp/decodePacket.js index 61b0c3a9..1a51a86d 100644 --- a/udsp/decodePacket.js +++ b/udsp/decodePacket.js @@ -36,27 +36,27 @@ export async function decodePacketHeaders(config) { const packet = decode(packetEncoded); config.packet = packet; info(`Packet Decoded Array Size ${packet.length}`); - const headersEncoded = packet[0]; - info(`headersEncoded Size ${headersEncoded.length}`); - if (!headersEncoded) { - return failed(`No headers -> Invalid Packet`); + const headerEncoded = packet[0]; + info(`headerEncoded Size ${headerEncoded.length}`); + if (!headerEncoded) { + return failed(`No header -> Invalid Packet`); } // Add single header support which holds only the binary data of the packet.id - const headers = decode(headersEncoded); - if (!headers) { - return failed(`No headers -> Invalid Packet`); + const header = decode(headerEncoded); + if (!header) { + return failed(`No header -> Invalid Packet`); } let headerIdEncoded; - const isHeadersBuffer = isBuffer(headers); + const isHeadersBuffer = isBuffer(header); if (isHeadersBuffer) { - headerIdEncoded = headers; + headerIdEncoded = header; info('Headers are in single header format'); } else { - headerIdEncoded = headers.id; - info(`headers.id: ${toBase64(headers.id)}`); + headerIdEncoded = header.id; + info(`header.id: ${toBase64(header.id)}`); } if (!headerIdEncoded) { - return failed(`No connection id in headers -> Invalid Packet`); + return failed(`No connection id in header -> Invalid Packet`); } let headerId; if (encryptConnectionId) { @@ -75,43 +75,43 @@ export async function decodePacketHeaders(config) { info(`clientId: ${toBase64(headerId)}`); if (isHeadersBuffer) { config.packetDecoded = { - headers: headerId + header: headerId }; return true; } else { - headers.id = headerId; + header.id = headerId; } - } else if (!headers?.id && !headers) { + } else if (!header?.id && !header) { return failed(`No ID -> Invalid Packet`); } - if (headers.key) { + if (header.key) { success(`Public Key is given -> Processing as create client`); console.log(toBase64(encryptKeypair.publicKey)); const { encryptClientKey, encryptServerKey } = cryptography.config; - let publicKey = headers.key; + let publicKey = header.key; if (isClient) { if (encryptClientKey === 'sealedbox') { - publicKey = cryptography.decryptServerKey(headers.key, destination.encryptKeypair); + publicKey = cryptography.decryptServerKey(header.key, destination.encryptKeypair); } } if (isServerEnd) { if (encryptServerKey === 'sealedbox') { - publicKey = cryptography.decryptClientKey(headers.key, destination.encryptKeypair); + publicKey = cryptography.decryptClientKey(header.key, destination.encryptKeypair); } } if (!publicKey) { return failed('Client Key Decrypt Failed'); } - headers.key = publicKey; + header.key = publicKey; } else { success(`No Public Key is given -> Processing as a message`); } - // console.log(headers); + // console.log(header); config.packetDecoded = { - headers + header }; return true; } @@ -121,7 +121,7 @@ export async function decodePacket(config) { destination, packet, packetDecoded, - packetDecoded: { headers, } + packetDecoded: { header, } } = config; const { cryptography, } = destination; const footer = packet[2] && decode(packet[2]); @@ -145,8 +145,8 @@ export async function decodePacket(config) { if (message.head) { console.log('head PAYLOAD', message.head); } - if (message.body) { - success('body PAYLOAD', message.body.length); + if (message.data) { + success('data PAYLOAD', message.data.length); } packetDecoded.message = message; return true; diff --git a/udsp/encodePacket.js b/udsp/encodePacket.js index d1080943..941bc460 100644 --- a/udsp/encodePacket.js +++ b/udsp/encodePacket.js @@ -17,7 +17,7 @@ export async function encodePacket(config) { const { source, packet: { - headers = {}, + header = {}, message, footer, options @@ -55,51 +55,51 @@ export async function encodePacket(config) { return console.error(`Connection ID Encrypt failed method given ${encryptConnectionId}`); } } - headers.id = id; + header.id = id; message.t = Date.now(); if (isClient) { if (state === 0) { console.log('DESTINATION ENCRYPT PUBLIC KEY', toBase64(destination.encryptKeypair.publicKey)); - if (!headers.key) { - headers.key = source.encryptKeypair.publicKey; + if (!header.key) { + header.key = source.encryptKeypair.publicKey; } } } - if (headers.key) { + if (header.key) { const { encryptClientKey, encryptServerKey } = cryptography.config; if (isClient) { if (encryptClientKey === 'sealedbox') { - headers.key = cryptography.encryptClientKey(headers.key, destination.encryptKeypair); + header.key = cryptography.encryptClientKey(header.key, destination.encryptKeypair); } } if (isServerEnd) { if (encryptServerKey === 'sealedbox') { - headers.key = cryptography.encryptServerKey(headers.key, destination.encryptKeypair); + header.key = cryptography.encryptServerKey(header.key, destination.encryptKeypair); } } } if (options) { console.log('Packet Options', options); } - const headersEncoded = (objectSize(headers) === 1 && headers.id) ? encode(id) : encode(headers); + const headerEncoded = (objectSize(header) === 1 && header.id) ? encode(id) : encode(header); const messageEncoded = encode(message); - const ad = (footer) ? Buffer.concat([headersEncoded, footer]) : headersEncoded; + const ad = (footer) ? Buffer.concat([headerEncoded, footer]) : headerEncoded; const encryptedMessage = cryptography.encrypt(messageEncoded, source.sessionKeys, ad); if (!encryptedMessage) { return failed('Encryption failed'); } - const packetStructure = [headersEncoded, encryptedMessage]; + const packetStructure = [headerEncoded, encryptedMessage]; if (footer) { packetStructure[2] = encode(footer); } const packet = encode(packetStructure); - info(`clientId: ${toBase64(headers.id)}`); + info(`clientId: ${toBase64(header.id)}`); info(`Transmit Key ${toBase64(source.sessionKeys.transmitKey)}`); const packetSize = packet.length; - console.log('Size Unencrypted', encode([headersEncoded, messageEncoded]).length); + console.log('Size Unencrypted', encode([headerEncoded, messageEncoded]).length); info(`encoded Packet Size ${packetSize}`); if (packetSize >= 1328) { console.log(packet); diff --git a/udsp/fetch.js b/udsp/fetch.js new file mode 100644 index 00000000..e85a552c --- /dev/null +++ b/udsp/fetch.js @@ -0,0 +1,18 @@ +import { + success, failed, imported, msgSent, info +} from '#logs'; +import { promise, construct, isString } from '@universalweb/acid'; +imported('Request'); +export async function fetchRequest(source, options) { + const method = options?.method || (isString(options) && options) || 'get'; + const payload = (isString(source)) ? { + path: source + } : source; + info(`Request then Fetch Function ${method}`); + const requestObject = { + method, + data: payload + }; + const request = await this.request(requestObject, options); + return request.send(); +} diff --git a/udsp/reply.js b/udsp/reply.js index aa8f2fc4..bab16351 100644 --- a/udsp/reply.js +++ b/udsp/reply.js @@ -7,7 +7,6 @@ import { } from '#logs'; import { processEvent } from '#udsp/processEvent'; const incomingDataEncodingTypesChunked = /stream|file|image|binary|string/; -import { flushOutgoing, flushIncoming, flush } from './request/flush.js'; import { Base } from './request/base.js'; /** * @todo Add promise to send use the method that Ask uses assign the accept, return it, and when completed execute. @@ -23,8 +22,6 @@ export class Reply extends Base { queue, packetIdGenerator } = source; - const timeStamp = Date.now(); - thisReply.t = timeStamp; thisReply.source = function() { return source; }; @@ -47,9 +44,6 @@ export class Reply extends Base { thisReply.received(message); return thisReply; } - processRequest = function() { - processEvent(this); - }; isReply = true; async assemble() { const thisReply = this; @@ -59,17 +53,17 @@ export class Reply extends Base { } const packet = thisReply.incomingPackets[0]; eachArray(thisReply.incomingPackets, (item) => { - if (item.body) { - Buffer.concat([packet.body, item.body]); + if (item.data) { + Buffer.concat([packet.data, item.data]); } }); if (incomingDataEncoding === 'struct' || !incomingDataEncoding) { msgReceived(thisReply.request); - if (thisReply.request.body) { - thisReply.request.body = decode(thisReply.request.body); + if (thisReply.request.data) { + thisReply.request.data = decode(thisReply.request.data); } } - await thisReply.processRequest(); + await processEvent(this); } } export function reply(packet, client) { diff --git a/udsp/request.js b/udsp/request.js index 0a770607..56027975 100644 --- a/udsp/request.js +++ b/udsp/request.js @@ -1,32 +1,17 @@ import { success, failed, imported, msgSent, info } from '#logs'; -import { promise, construct, omit } from '@universalweb/acid'; -import { Ask } from './ask.js'; +import { promise, construct, isString } from '@universalweb/acid'; imported('Request'); -export async function request(act, body, options = {}) { - const source = this; - const { - headers, - footer, - head - } = options; - info(`Request Function: ${act}`); - const message = { - act - }; - if (body) { - message.body = body; +export async function request(source, options) { + if (options) { + if (isString(options)) { + source.method = options; + } + source.method = options.method; } - if (head) { - message.head = head; - } - const ask = construct(Ask, [{ - message, - headers, - footer, - options: omit(options, ['footer', 'headers']), - source - }]); - return ask.fetch(); + info(`Request Function: ${source.method}`); + const ask = this.ask(source, options); + console.log(ask); + return ask; } diff --git a/udsp/request/assembleData.js b/udsp/request/assembleData.js index a254556a..e08db796 100644 --- a/udsp/request/assembleData.js +++ b/udsp/request/assembleData.js @@ -1,15 +1,16 @@ import { hasLength } from '@universalweb/acid'; import { decode } from 'msgpackr'; -import { destroy } from './destory'; -export async function assembleData(data, source, incomingDataEncoding) { +import { destroy } from './destory.js'; +export async function assembleData(data, incomingDataEncoding) { if (hasLength(data)) { - source.body = Buffer.concat(data); + let compiledData = Buffer.concat(data); if (incomingDataEncoding === 'struct' || !incomingDataEncoding) { try { - source.body = decode(source.body); + compiledData = decode(data); } catch (err) { return this.destroy('Failed to decode incoming data'); } } + return compiledData; } } diff --git a/udsp/request/base.js b/udsp/request/base.js index 8f650081..6c393e84 100644 --- a/udsp/request/base.js +++ b/udsp/request/base.js @@ -1,24 +1,43 @@ import { sendPacket } from './sendPacket.js'; import { destroy } from './destory.js'; -import { bufferToOutgoingPackets } from './bufferToOutgoingPackets.js'; -import { sendCompleted } from './sendCompleted.js'; +import { bufferPacketization } from './bufferPacketization.js'; +import { sendEnd } from './sendEnd.js'; import { on } from './on.js'; -import { initiate } from './fetch.js'; -import { flushOutgoing, flushIncoming, flush } from './request/flush.js'; +import { flushOutgoing, flushIncoming, flush } from './flush.js'; import { sendPacketsById } from './sendPacketsById.js'; -import { sendOutgoing } from './sendOutgoing.js'; +import { sendAll } from './sendAll.js'; import { onPacket } from './onPacket.js'; export class Base { + constructor(config, source) { + const { events } = config; + const timeStamp = Date.now(); + this.created = timeStamp; + this.source = function() { + return source; + }; + const { + queue, + packetIdGenerator, + maxPacketSize + } = source; + if (events) { + this.on(events); + } + if (maxPacketSize) { + this.maxPacketSize = maxPacketSize; + } + } destroy = destroy; - sendCompleted = sendCompleted; + sendEnd = sendEnd; sendPacketsById = sendPacketsById; - sendOutgoing = sendOutgoing; + sendAll = sendAll; onPacket = onPacket; sendPacket = sendPacket; - bufferToOutgoingPackets = bufferToOutgoingPackets; + bufferPacketization = bufferPacketization; on = on; - fetch = initiate; currentPayloadSize = 0; + totalReceivedUniquePackets = 0; + totalIncomingUniquePackets = 0; progress = 0; request = {}; response = {}; @@ -27,7 +46,7 @@ export class Base { // This is as the data came in over the wire out of order stream = []; events = {}; - headers = {}; + header = {}; options = {}; outgoingPackets = []; incomingPackets = []; @@ -35,7 +54,6 @@ export class Base { incomingNacks = []; outgoingAcks = []; outgoingNacks = []; - totalReceivedUniquePackets = 0; totalOutgoingPackets = 0; totalOutgoingPayloadSize = 0; // Must be checked for uniqueness diff --git a/udsp/request/bufferPacketization.js b/udsp/request/bufferPacketization.js new file mode 100644 index 00000000..15d38523 --- /dev/null +++ b/udsp/request/bufferPacketization.js @@ -0,0 +1,44 @@ +import { assign } from '@universalweb/acid'; +export async function bufferPacketization(data, sid, packets = [], maxPacketSize, dataEncoding) { + const totalPayloadSize = data?.length; + let currentBytePosition = 0; + let packetId = 0; + if (totalPayloadSize > maxPacketSize) { + console.log('Body size', data.length); + while (currentBytePosition < totalPayloadSize) { + const endIndex = currentBytePosition + maxPacketSize; + const safeEndIndex = endIndex > totalPayloadSize ? totalPayloadSize : endIndex; + const chunk = data.subarray(currentBytePosition, safeEndIndex); + console.log('chunksize', chunk.length, currentBytePosition, endIndex); + const packet = assign({ + pid: packetId, + sid + }); + if (packetId === 0) { + if (dataEncoding) { + packet.de = dataEncoding; + } + packet.tps = totalPayloadSize; + } + packet.data = chunk; + packets[packetId] = packets; + if (endIndex >= totalPayloadSize) { + packet.end = true; + break; + } + currentBytePosition = currentBytePosition + maxPacketSize; + packetId++; + } + } else { + const packet = { + pid: 0, + end: true + }; + if (dataEncoding) { + packet.de = dataEncoding; + } + packets[0] = packet; + } + console.log('bufferToPackets', packets); + return packets; +} diff --git a/udsp/request/bufferToArrayChunks.js b/udsp/request/bufferToArrayChunks.js deleted file mode 100644 index 76c156de..00000000 --- a/udsp/request/bufferToArrayChunks.js +++ /dev/null @@ -1,9 +0,0 @@ -export async function bufferToArrayChunks(body, packetMaxPayloadSafeEstimate) { - const chunks = []; - const packetLength = body.length; - for (let index = 0; index < packetLength;index += packetMaxPayloadSafeEstimate) { - const chunk = body.subarray(index, index + packetMaxPayloadSafeEstimate); - chunks.push(chunk); - } - return chunks; -} diff --git a/udsp/request/bufferToChunks.js b/udsp/request/bufferToChunks.js new file mode 100644 index 00000000..62008b8c --- /dev/null +++ b/udsp/request/bufferToChunks.js @@ -0,0 +1,9 @@ +export async function bufferToChunks(data, packetMaxPayloadSafeEstimate) { + const chunks = []; + const packetLength = data.length; + for (let index = 0; index < packetLength;index += packetMaxPayloadSafeEstimate) { + const chunk = data.subarray(index, index + packetMaxPayloadSafeEstimate); + chunks.push(chunk); + } + return chunks; +} diff --git a/udsp/request/bufferToOutgoingPackets.js b/udsp/request/bufferToOutgoingPackets.js deleted file mode 100644 index d8824f12..00000000 --- a/udsp/request/bufferToOutgoingPackets.js +++ /dev/null @@ -1,48 +0,0 @@ -import { assign } from '@universalweb/acid'; -export async function bufferToOutgoingPackets(thisSource, response, incomingDataEncoding) { - const { - responsePacketTemplate, - packetMaxPayload, - packetMaxPayloadSafeEstimate - } = thisSource; - const { body } = response; - console.log('Body size', body.length); - const totalPayloadSize = body?.length; - thisSource.totalReplyDataSize = totalPayloadSize; - let currentBytePosition = 0; - let id = 0; - if (totalPayloadSize > packetMaxPayloadSafeEstimate) { - while (currentBytePosition < totalPayloadSize) { - const endIndex = currentBytePosition + packetMaxPayloadSafeEstimate; - const safeEndIndex = endIndex > totalPayloadSize ? totalPayloadSize : endIndex; - const chunk = body.subarray(currentBytePosition, safeEndIndex); - console.log('chunksize', chunk.length, currentBytePosition, endIndex); - const outgoingPacket = assign({ - pid: id - }, responsePacketTemplate); - if (id === 0) { - if (incomingDataEncoding) { - outgoingPacket.de = incomingDataEncoding; - } - outgoingPacket.tps = totalPayloadSize; - } - outgoingPacket.body = chunk; - thisSource.outgoingPackets[id] = outgoingPacket; - if (endIndex >= totalPayloadSize) { - outgoingPacket.cmplt = true; - break; - } - currentBytePosition = currentBytePosition + packetMaxPayloadSafeEstimate; - id++; - } - } else { - if (incomingDataEncoding) { - response.de = incomingDataEncoding; - } - response.pid = 0; - response.pt = 1; - response.cmplt = true; - thisSource.outgoingPackets[0] = response; - } - console.log('buildReplyPackets', thisSource.outgoingPackets); -} diff --git a/udsp/request/fetch.js b/udsp/request/fetch.js deleted file mode 100644 index 3b414aaf..00000000 --- a/udsp/request/fetch.js +++ /dev/null @@ -1,9 +0,0 @@ -import { promise } from '@universalweb/acid'; -export async function initiate() { - const thisAsk = this; - await this.buildRequest(); - const awaitingResult = promise((accept) => { - thisAsk.accept = accept; - }); - return awaitingResult; -} diff --git a/udsp/request/flush.js b/udsp/request/flush.js index be034b91..321a443e 100644 --- a/udsp/request/flush.js +++ b/udsp/request/flush.js @@ -15,7 +15,7 @@ export function flushIncoming() { this.totalOutgoingPayloadSize = null; this.totalReceivedPackets = null; } -// Flush all body +// Flush all data export function flush() { this.flushOutgoing(); this.flushIncoming(); diff --git a/udsp/request/on.js b/udsp/request/on.js index cf2c4fad..3c1f38ab 100644 --- a/udsp/request/on.js +++ b/udsp/request/on.js @@ -1,9 +1,16 @@ -import { each } from '@universalweb/acid'; -export async function on(events) { - const thisAsk = this; - each(events, (item, propertyName) => { - thisAsk.events[propertyName] = (data) => { - return item.call(thisAsk, data); +import { each, isString } from '@universalweb/acid'; +export async function on(events, eventMethod) { + const thisObject = this; + if (isString(events)) { + this.events[events] = (data) => { + return eventMethod.call(thisObject, data); }; - }); + } else { + each(events, (item, propertyName) => { + thisObject.events[propertyName] = (data) => { + return item.call(thisObject, data); + }; + }); + } + return this; } diff --git a/udsp/request/onData.js b/udsp/request/onData.js index 6eb05952..ee861abd 100644 --- a/udsp/request/onData.js +++ b/udsp/request/onData.js @@ -2,15 +2,17 @@ export async function onData(message) { console.log('On Data event'); const { pid, - body + data } = message; - this.data[pid] = body; - this.currentPayloadSize += body.length; + this.data[pid] = data; + this.currentPayloadSize += data.length; if (this.totalIncomingPayloadSize) { - this.progress = this.totalIncomingPayloadSize / this.currentPayloadSize; + if (this.currentPayloadSize > 0) { + this.progress = (this.currentPayloadSize / this.totalIncomingPayloadSize) * 100; + } console.log('Progress', this.progress); } if (this.events.data) { - this.events.data(body, pid); + this.events.data(data, pid); } } diff --git a/udsp/request/onPacket.js b/udsp/request/onPacket.js index 6d35ba2b..ac159b62 100644 --- a/udsp/request/onPacket.js +++ b/udsp/request/onPacket.js @@ -1,11 +1,11 @@ import { hasValue } from '@universalweb/acid'; -import { destroy } from './destory'; +import { destroy } from './destory.js'; export async function onPacket(packet) { const source = this; source.lastPacketTime = Date.now(); const { message } = packet; const { - body, + data, head, // Stream ID sid: streamId, @@ -20,14 +20,15 @@ export async function onPacket(packet) { // Data Encoding de: incomingDataEncoding, // Complete - cmplt, + done, // Finale Packet finale, // Acknowledgement ack, // Negative Acknowledgement nack, - err + err, + end } = message; console.log(`Stream Id ${streamId}`); if (hasValue(totalIncomingUniquePackets)) { @@ -39,27 +40,25 @@ export async function onPacket(packet) { if (incomingDataEncoding) { source.incomingDataEncoding = incomingDataEncoding; } - source.totalReceivedPackets++; + source.totalIncomingPackets++; if (hasValue(packetId)) { if (!source.incomingPackets[packetId]) { source.incomingPackets[packetId] = message; - if (body) { + if (data) { await this.onData(message); } source.totalReceivedUniquePackets++; } - } else { - source.incomingPackets[0] = message; - source.totalReceivedUniquePackets = 1; - source.totalIncomingUniquePackets = 1; } - if (cmplt) { - source.state = 2; + if (end) { + if (source.totalIncomingUniquePackets === source.totalReceivedUniquePackets) { + source.state = 2; + } } if (err) { return this.destroy(err); } - if (source.state === 2 || cmplt) { + if (source.state === 2 || end) { source.assemble(); } console.log('On Packet event', source); diff --git a/udsp/request/send.js b/udsp/request/send.js deleted file mode 100644 index f29e2ff6..00000000 --- a/udsp/request/send.js +++ /dev/null @@ -1,17 +0,0 @@ -import { isBuffer } from '@universalweb/acid'; -import { encode } from 'msgpackr'; -import { bufferToOutgoingPackets } from './bufferToOutgoingPackets.js'; -export async function send(incomingDataEncoding) { - const response = this.response; - const thisReply = this; - console.log('Reply.send', response); - if (response.body) { - if (isBuffer(response.body)) { - await bufferToOutgoingPackets(response, incomingDataEncoding); - } else if (incomingDataEncoding === 'struct' || incomingDataEncoding === 'json' || !incomingDataEncoding) { - response.body = encode(response.body); - await bufferToOutgoingPackets(response, incomingDataEncoding); - } - } - thisReply.replyAll(); -} diff --git a/udsp/request/sendOutgoing.js b/udsp/request/sendAll.js similarity index 86% rename from udsp/request/sendOutgoing.js rename to udsp/request/sendAll.js index 5d8cbded..caf840a1 100644 --- a/udsp/request/sendOutgoing.js +++ b/udsp/request/sendAll.js @@ -1,5 +1,5 @@ import { eachArray } from '@universalweb/acid'; -export async function sendOutgoing() { +export async function sendAll() { const thisReply = this; console.log('outgoingPackets', thisReply.outgoingPackets.length); eachArray(thisReply.outgoingPackets, (packet) => { diff --git a/udsp/request/sendCompleted.js b/udsp/request/sendEnd.js similarity index 52% rename from udsp/request/sendCompleted.js rename to udsp/request/sendEnd.js index 16dd4888..59d05cca 100644 --- a/udsp/request/sendCompleted.js +++ b/udsp/request/sendEnd.js @@ -1,11 +1,11 @@ -import { sendPacket } from './sendPacket'; -export async function sendCompleted() { +import { sendPacket } from './sendPacket.js'; +export async function sendEnd() { const thisAsk = this; const { id: sid } = thisAsk; thisAsk.sendPacket({ message: { sid, - cmplt: true + end: true } }); } diff --git a/udsp/request/sendPacket.js b/udsp/request/sendPacket.js index d2c4f159..3708d295 100644 --- a/udsp/request/sendPacket.js +++ b/udsp/request/sendPacket.js @@ -1,26 +1,26 @@ import { info } from '#logs'; -export async function sendPacket(arg) { +export async function sendPacket(packet) { const { message, options, - headers, + header, footer - } = arg; + } = packet; const source = this.source(); if (options) { info(`Sending Packet with options`); } - if (headers) { - info(`Sending Packet with headers`); + if (header) { + info(`Sending Packet with header`); } - if (footer) { - info(`Sending Packet with footer`); + if (message) { + info(`Sending Packet with message`); } if (footer) { info(`Sending Packet with footer`); } - if (message.act) { - info(`Sending Packet with act ${message.act}`); + if (message.method) { + info(`Sending Packet with act ${message.method}`); } - await source.send(arg); + await source.send(packet); } diff --git a/udsp/server/actions/connect.js b/udsp/server/actions/connect.js index 6954e32a..2a529a9a 100644 --- a/udsp/server/actions/connect.js +++ b/udsp/server/actions/connect.js @@ -15,7 +15,7 @@ export async function opn(reply) { const response = reply.response; info(`Server ID${client.idString}`, `Client ID${client.clientIdString}`, `Stream ID${response.sid}`); response.head = {}; - response.body = { + response.data = { sid: server.id }; client.newKey = true; @@ -38,7 +38,7 @@ export async function opn(reply) { response.head.language = language; } if (onConnectResponse) { - response.body.response = onConnectResponse; + response.data.response = onConnectResponse; } // connection status - backwards compatibility response.state = 1; diff --git a/udsp/server/actions/file.js b/udsp/server/actions/file.js index 0dcba93a..edd82eb8 100644 --- a/udsp/server/actions/file.js +++ b/udsp/server/actions/file.js @@ -20,8 +20,8 @@ export async function file(reply) { response, request } = reply; - info(request.body); - const { path: requestPath } = request.body; + info(request.data); + const { path: requestPath } = request.data; if (!isString(requestPath) || isEmpty(requestPath) || requestPath.match(dots).length > 1) { console.log('No valid state request received - Returning empty data'); response.code = 404; @@ -36,7 +36,7 @@ export async function file(reply) { const ext = path.extname(cleanedPath).replace('.', ''); console.log(`EXT => ${ext}`); response.ext = ext; - response.body = data; + response.data = data; // checksum: cryptography.hash(data) reply.send('binary'); } diff --git a/udsp/server/actions/reKey.js b/udsp/server/actions/reKey.js index 869055c3..29bd3577 100644 --- a/udsp/server/actions/reKey.js +++ b/udsp/server/actions/reKey.js @@ -1,6 +1,6 @@ import { info } from '#logs'; import { toBase64 } from '#crypto'; -export async function reKey(socket, body) { - info(`${toBase64(body.certificate.key)}`); - socket.reKey(body.certificate); +export async function reKey(socket, data) { + info(`${toBase64(data.certificate.key)}`); + socket.reKey(data.certificate); } diff --git a/udsp/server/actions/state.js b/udsp/server/actions/state.js index 5bc8e3db..f64fe884 100644 --- a/udsp/server/actions/state.js +++ b/udsp/server/actions/state.js @@ -5,7 +5,7 @@ import { read } from '#utilities/file'; export async function state(socket, request, response) { const { configuration: { resourceDirectory, }, } = this; info(request); - const { state: fileName } = request.body; + const { state: fileName } = request.data; response.head = {}; if (!isString(fileName) || isEmpty(fileName)) { console.log('No valid state request received - Returning empty data'); @@ -13,7 +13,7 @@ export async function state(socket, request, response) { return true; } if (!isPathAllowed(fileName)) { - response.body = { + response.data = { err: 'Invalid path' }; return true; @@ -21,7 +21,7 @@ export async function state(socket, request, response) { const cleanedPath = (fileName) ? cleanPath(`${resourceDirectory}/states/${fileName}/index.js`) : cleanPath(`${resourceDirectory}/states/index.js`); const data = await read(cleanedPath); console.log(cleanedPath, data); - response.body = { + response.data = { data }; return true; diff --git a/udsp/server/chunking.js b/udsp/server/chunking.js index f9a29cc7..b5f11555 100644 --- a/udsp/server/chunking.js +++ b/udsp/server/chunking.js @@ -1,5 +1,5 @@ export function chunking(message) { - const { body } = message; - const bodyLength = body.length; - console.log(body, bodyLength); + const { data } = message; + const dataLength = data.length; + console.log(data, dataLength); } diff --git a/udsp/server/clients/initialize.js b/udsp/server/clients/initialize.js index 74db7b7f..b7fb6238 100644 --- a/udsp/server/clients/initialize.js +++ b/udsp/server/clients/initialize.js @@ -13,7 +13,7 @@ import { Client } from './index.js'; export async function initialize(config, client) { const { packet: { - headers: { + header: { id: clientId, key: publicKey }, diff --git a/udsp/server/emit.js b/udsp/server/emit.js index f78f19fc..d88cc97e 100644 --- a/udsp/server/emit.js +++ b/udsp/server/emit.js @@ -1,13 +1,13 @@ import { success, failed, imported, msgSent, info, msgReceived } from '#logs'; -export async function emit(evnt, body) { +export async function emit(evnt, data) { info(`Emitted ${evnt}`); const eid = this.packetIdGenerator.get(); const message = { evnt, eid, - body + data }; await this.send(message); } diff --git a/udsp/server/onPacket.js b/udsp/server/onPacket.js index 0f1e8b96..2f407dd6 100644 --- a/udsp/server/onPacket.js +++ b/udsp/server/onPacket.js @@ -16,13 +16,13 @@ export async function onPacket(packet, connection) { destination: thisServer, }; const wasHeadersDecoded = await decodePacketHeaders(config); - if (!wasHeadersDecoded || !config.packetDecoded.headers) { + if (!wasHeadersDecoded || !config.packetDecoded.header) { return failed('Invalid Packet Headers'); } const { id, key - } = config.packetDecoded.headers; + } = config.packetDecoded.header; let client = thisServer.clients.get(toBase64(id)); if (key && !client) { client = await createClient({ @@ -40,7 +40,7 @@ export async function onPacket(packet, connection) { } const wasDecoded = await decodePacket(config); if (!wasDecoded) { - return failed('When decoding the packet but headers passed'); + return failed('When decoding the packet but header passed'); } await reply(config.packetDecoded, client); }