Skip to content

Commit

Permalink
50% of API standardization
Browse files Browse the repository at this point in the history
Major Rework
  • Loading branch information
Universal Web committed Jun 24, 2023
1 parent 17dc29b commit 3b8a3f3
Show file tree
Hide file tree
Showing 49 changed files with 333 additions and 317 deletions.
1 change: 1 addition & 0 deletions .cspell/custom-dictionary-workspace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ keccak
keccakprg
kmac
Nacks
Packetization
ripemd
Ristretto
schnorr
Expand Down
27 changes: 24 additions & 3 deletions packetActions.md
Original file line number Diff line number Diff line change
@@ -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
Binary file modified profiles/default-Ephemeral.cert
Binary file not shown.
Binary file modified profiles/default-EphemeralPublic.cert
Binary file not shown.
Binary file modified profiles/default-Master.cert
Binary file not shown.
Binary file modified profiles/default-MasterPublic.cert
Binary file not shown.
Binary file modified profiles/default-Profile.cert
Binary file not shown.
5 changes: 5 additions & 0 deletions scripts/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 14 additions & 6 deletions scripts/simulateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Binary file modified services/universal.web-Ephemeral.cert
Binary file not shown.
Binary file modified services/universal.web-EphemeralPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Master.cert
Binary file not shown.
Binary file modified services/universal.web-MasterPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Profile.cert
Binary file not shown.
92 changes: 36 additions & 56 deletions udsp/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,62 @@ import { decode, encode } from 'msgpackr';
import {
failed, info, msgReceived, msgSent
} from '#logs';
import { destroy } from './request/destory';
import { assembleData } from './request/assembleData';
import { Base } from './request/base';
import { assembleData } from './request/assembleData.js';
import { Base } from './request/base.js';
import { bufferPacketization } from './request/bufferPacketization.js';
import { request } from '#udsp/request';
const dataEncodingTypesChunked = /stream|file|image|string/;
const dataEncodingTypesStructured = /json|msgpack|struct|/;
/**
* @todo Prepare Request into singular object.
* @todo Chunk body data while adding packit number to it.
* @todo Send all chunks (consider sending pkt1 twice).
* @todo
*/
export class Ask extends Base {
constructor(config) {
super(config);
constructor(config, source) {
super(config, source);
const {
message,
head,
body,
options,
source,
headers,
footer,
sourceContext,
isClient
} = config;
const thisAsk = this;
const {
queue,
packetIdGenerator,
maxPacketSize
} = source;
let request;
if (message) {
console.log('Message Ask', message);
request = message;
} else {
request = {};
if (head) {
request.head = head;
}
if (body) {
request.body = body;
}
}
if (headers) {
this.headers = headers;
}
if (footer) {
this.footer = footer;
}
const timeStamp = Date.now();
thisAsk.created = timeStamp;
assign(this.request, message);
// sid is a Stream ID
const streamId = packetIdGenerator.get();
request.sid = streamId;
this.request = request;
this.request.sid = streamId;
this.packetTemplate.sid = streamId;
this.id = streamId;
if (options.dataEncoding) {
this.dataEncoding = options.dataEncoding;
}
this.source = function() {
return source;
};
const { onData, } = options;
this.on({
onData,
});
this.maxPacketSize = maxPacketSize;
queue.set(streamId, this);
}
async assemble() {
const { incomingDataEncoding } = this;
await assembleData(this.data, this.response, incomingDataEncoding);
console.log('Assemble', this.response.body);
await this.accept(this);
if (this.data) {
this.data = await assembleData(this.data, this.response, incomingDataEncoding);
console.log('Assembled', this.data);
}
this.destroy();
await this.accept(this);
}
async send() {
const thisAsk = this;
const {
packetTemplate,
dataEncoding,
maxPacketSize,
sid
} = this;
console.log('Reply.send', this.response);
if (this.response.data) {
if (!isBuffer(this.response.data)) {
this.response.data = encode(this.response.data);
}
this.totalReplyDataSize = request.data?.length;
}
await bufferPacketization(this.request.data, sid, this.outgoingPackets, maxPacketSize, dataEncoding);
thisAsk.sendAll();
const awaitingResult = promise((accept) => {
thisAsk.accept = accept;
});
return awaitingResult;
}
isAsk = true;
type = 'ask';
Expand Down
16 changes: 8 additions & 8 deletions udsp/client/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions udsp/client/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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`);
const eid = packetIdGenerator.get();
const message = {
evnt,
eid,
body
data
};
return thisContext.send(message);
}
17 changes: 12 additions & 5 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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 };
6 changes: 3 additions & 3 deletions udsp/client/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion udsp/client/processMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
imported('Client ProcessMessage');
export async function processMessage(packet, client) {
const {
headers,
header,
message,
footer
} = packet;
Expand Down
2 changes: 1 addition & 1 deletion udsp/cryptography.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 3b8a3f3

Please sign in to comment.