Skip to content

Commit

Permalink
Converting to HTTP API
Browse files Browse the repository at this point in the history
New Pre-Header System in progress
Accurate Packet max size in progress
  • Loading branch information
Universal Web committed Aug 10, 2023
1 parent ba59f0d commit 050db6a
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 138 deletions.
3 changes: 1 addition & 2 deletions scripts/simulateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ const uwClient = await client({
});
// const connection = await uwClient.connect();
// await client('universalweb.io', {keychain: 'Universal Web Profile'});
// await request('universalweb.io/index.html');
console.timeEnd('Connected');
// console.log('INTRO =>', uwClient);
console.time('FileRequest');
// short hand get request
const fileRequest = await uwClient.request('index.html');
const fileRequest = await uwClient.request('get', 'index.html');
// medium hand
// const fileRequest = await uwClient.request({
// path: 'index.html'
Expand Down
2 changes: 1 addition & 1 deletion udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { fetchRequest } from '../fetch.js';
import { UDSP } from '#udsp/base';
import { sendPacket } from '../sendPacket.js';
import { connect } from './connect.js';
import { post } from '../post';
import { post } from '../post.js';
// UNIVERSAL WEB Client Class
export class Client extends UDSP {
constructor(configuration) {
Expand Down
10 changes: 4 additions & 6 deletions udsp/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
import { promise, construct, isString } from '@universalweb/acid';
imported('Request');
// To send a request but only receive AN ACKNOWLEDGEMENT
export async function emit(url, data, options = {}) {
info(`emit => ${url}`);
export async function emit(endpoint, data, options = {}) {
info(`emit => ${endpoint}`);
options.method = 'post';
const source = {
url,
data
};
options.method = 'post';
const source = data;
const request = await this.request(source, options);
return request.send();
}
6 changes: 3 additions & 3 deletions udsp/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
} from '#logs';
import { promise, construct, isString } from '@universalweb/acid';
imported('Request');
export async function fetchRequest(source, options = {}) {
info(`FETCH => ${source}`);
export async function fetchRequest(path, options = {}) {
info(`FETCH => ${path}`);
options.method = 'get';
const request = await this.request(source, options);
const request = await this.request('get', path, options);
return request.send();
}
15 changes: 3 additions & 12 deletions udsp/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@ import {
} from '#logs';
import { promise, construct, isString } from '@universalweb/acid';
imported('Request');
export async function request(dataArg, optionsArg = {}) {
const data = (isString(dataArg)) ? {
path: dataArg
} : dataArg;
const options = isString(optionsArg) ? {
method: optionsArg
} : optionsArg;
if (!options.method) {
options.method = 'get';
}
info(`Request Function: ${options.method || 'get'} ${data.path}`);
const ask = this.ask(data, options);
export async function request(method = 'get', endpoint, data, options = {}) {
info(`Request Function: ${method || 'get'} ${data.path}`);
const ask = this.ask(method, endpoint, data, options);
console.log(data, ask);
return ask;
}
13 changes: 5 additions & 8 deletions udsp/request/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ import {
import { Base } from './base.js';
import { request } from '#udsp/request';
export class Ask extends Base {
constructor(data, options = {}, source) {
constructor(method = 'get', path, data, options = {}, source) {
super(options, source);
const {
requestQueue,
streamIdGenerator,
} = source;
const {
method = 'get',
head
} = options;
const { head } = options;
console.log('Ask', data);
const streamId = streamIdGenerator.get();
const methodSanitized = method.toLowerCase();
this.request.sid = streamId;
this.request.method = method;
this.method = method;
this.id = streamId;
this.request.method = methodSanitized;
this.request.path = path;
if (data) {
this.request.data = data;
}
Expand Down
17 changes: 16 additions & 1 deletion udsp/request/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
import { encode, decode } from 'msgpackr';
import { request } from '#udsp/request';
import { toBase64 } from '#crypto';
/**
* @todo Adjust packet size to account for other packet data.
*/
export class Base {
constructor(options = {}, source) {
const { events, } = options;
Expand Down Expand Up @@ -114,6 +117,18 @@ export class Base {
this.completeReceived();
}
}
get path() {
const { path } = (this.isAsk) ? this.request : this.response;
return path;
}
get method() {
const { method } = (this.isAsk) ? this.request : this.response;
return method;
}
get id() {
const { sid } = (this.isAsk) ? this.request : this.response;
return sid;
}
get data() {
if (this.compiledDataAlready) {
return this.compiledData;
Expand Down Expand Up @@ -251,7 +266,7 @@ export class Base {
const safeEndIndex = endIndex > headSize ? headSize : endIndex;
message.head = this.outgoingHead.subarray(currentBytePosition, safeEndIndex);
outgoingHeadPackets[packetId] = message;
message.index = safeEndIndex;
message.offset = safeEndIndex;
if (safeEndIndex === headSize) {
message.last = true;
break;
Expand Down
9 changes: 0 additions & 9 deletions udsp/request/bufferToChunks.js

This file was deleted.

4 changes: 2 additions & 2 deletions udsp/request/dataPacketization.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export async function dataPacketization(source) {
const endIndex = currentBytePosition + maxDataSize;
const safeEndIndex = endIndex > dataSize ? dataSize : endIndex;
const data = outgoingData.subarray(currentBytePosition, safeEndIndex);
console.log('chunksize', data.length, currentBytePosition, endIndex);
message.pid = packetId;
message.index = safeEndIndex;
message.offset = safeEndIndex;
console.log('chunksize', data.length, currentBytePosition, endIndex);
message.data = data;
outgoingDataPackets[packetId] = message;
if (safeEndIndex === dataSize) {
Expand Down
2 changes: 1 addition & 1 deletion udsp/request/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function onPacket(packet) {
headReady,
dataReady,
last,
index
offset
} = message;
console.log(`onPacket Stream Id ${streamId}`);
this.totalReceivedPackets++;
Expand Down
4 changes: 1 addition & 3 deletions udsp/request/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ export class Reply extends Base {
const thisReply = this;
const { message } = request;
const { sid } = message;
const { replyQueue, } = source;
this.events = source.events;
this.actions = source.actions;
// console.log(source);
// // console.log(message);
const { replyQueue, } = source;
this.sid = sid;
this.id = sid;
this.response.sid = sid;
replyQueue.set(sid, this);
}
Expand Down
46 changes: 0 additions & 46 deletions udsp/server/actions/connect.js

This file was deleted.

8 changes: 4 additions & 4 deletions udsp/server/actions/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ export async function get(reply) {
} = this;
const {
response,
data
data,
head: { path: filePath }
} = reply;
const { path: requestPath } = data;
if (!isString(requestPath) || isEmpty(requestPath) || requestPath.match(dots).length > 1) {
if (!isString(filePath) || isEmpty(filePath) || filePath.match(dots).length > 1) {
console.log('No fileName - Returning empty data');
reply.setHeader('statusCode', 404);
reply.send();
return true;
}
let cleanedPath = cleanPath(`${resourceDirectory}/${requestPath}`);
let cleanedPath = cleanPath(`${resourceDirectory}/${filePath}`);
if (!hasDot(cleanedPath)) {
cleanedPath = cleanedPath + defaultExtension;
}
Expand Down
6 changes: 0 additions & 6 deletions udsp/server/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { connect } from './connect.js';
import { get } from './get.js';
import { reKey } from './reKey.js';
import { state } from './state.js';
export const actions = {
connect,
get,
reKey,
state
};
6 changes: 0 additions & 6 deletions udsp/server/actions/reKey.js

This file was deleted.

28 changes: 0 additions & 28 deletions udsp/server/actions/state.js

This file was deleted.

0 comments on commit 050db6a

Please sign in to comment.