Skip to content

Commit

Permalink
Partial seperation major changes will break API after first layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Sep 24, 2023
1 parent d4abb9b commit 0c706f1
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 19 deletions.
2 changes: 2 additions & 0 deletions udsp/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './client/index.js';
export * from './server/index.js';
export * from './request/requestObject.js';
export * from './request/client/clientResponse.js';
8 changes: 6 additions & 2 deletions udsp/request/ask.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Client side request
* Client sends a request to the server & awaits a response if there is one
*/
import {
promise, assign, omit, eachArray, stringify, get, isBuffer, isPlainObject, isArray, isMap, construct, each, hasLength, hasValue, UniqID
} from '@universalweb/acid';
Expand All @@ -6,7 +10,7 @@ import {
failed, info, msgReceived, msgSent
} from '#logs';
import { Base } from './base.js';
import { uwResponseObject } from './response.js';
import { clientResponse } from './client/clientResponse.js';
export class Ask extends Base {
constructor(method = 'get', path, parameters, data, head, options = {}, source) {
super(options, source);
Expand Down Expand Up @@ -44,7 +48,7 @@ export class Ask extends Base {
}
this.readyState = 4;
this.flush();
const response = uwResponseObject(this);
const response = clientResponse(this);
this.accept(response);
}
isAsk = true;
Expand Down
164 changes: 164 additions & 0 deletions udsp/request/client/clientResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import {
jsonParse, isTrue, construct, hasValue, noValue
} from '@universalweb/acid';
import { decode } from 'msgpackr';
export class ClientResponse {
constructor(source, options) {
if (source.isAsk) {
const {
head,
method,
parameters,
path,
incomingData,
status,
type,
} = source;
return this.construct(incomingData, {
head,
method,
parameters,
path,
status,
type,
});
} else {
return this.construct(source, options);
}
}
construct(data, options = {}) {
const {
head,
headers,
params,
parameters = params,
method,
status,
type,
url,
path = url
} = options;
this.head = head || {};
if (hasValue(data)) {
this.dataBuffer = data;
}
this.method = method;
if (hasValue(status)) {
this.status = status;
}
if (hasValue(path)) {
this.path = path;
}
if (hasValue(type)) {
this.type = type;
}
if (hasValue(parameters)) {
this.parameters = parameters;
}
}
get data() {
if (this.compiledDataAlready) {
return this.compiledData;
}
if (noValue(this.dataBuffer)) {
return undefined;
}
const { head: { serialize } } = this;
const dataConcatinated = Buffer.concat(this.dataBuffer);
if (serialize) {
if (isTrue(serialize) || serialize === 0) {
this.compiledData = decode(dataConcatinated);
} else if (serialize === 1 || serialize === 'json') {
this.compiledData = jsonParse(dataConcatinated);
}
dataConcatinated.fill(0);
} else {
this.compiledData = dataConcatinated;
}
this.compiledDataAlready = true;
return this.compiledData;
}
get headers() {
return this.head;
}
get body() {
return this.data;
}
get url() {
return this.path;
}
toString(cache) {
if (cache) {
if (this.toStringCached) {
return this.toStringCached;
}
}
if (noValue(this.dataBuffer)) {
return;
}
const target = this.data.toString();
if (cache) {
this.toStringCached = target;
}
return target;
}
toJSON(cache) {
if (cache) {
if (this.toJSONCached) {
return this.toJSONCached;
}
}
if (noValue(this.dataBuffer)) {
return;
}
const target = jsonParse(this.data);
if (cache) {
this.toJSONCached = target;
}
return jsonParse(this.data);
}
serialize(cache) {
if (cache) {
if (this.serializeCached) {
return this.serializeCached;
}
}
if (noValue(this.dataBuffer)) {
return;
}
const target = decode(this.data);
if (cache) {
this.serializeCached = target;
}
return target;
}
toObject(cache) {
if (cache) {
if (this.toObjectRawCached) {
return this.toObjectRawCached;
}
}
if (noValue(this.dataBuffer)) {
return;
}
const {
head,
data,
id,
method,
} = this;
const target = {
head,
data,
method
};
if (cache) {
this.toObjectRawCached = target;
}
return target;
}
isResponse = true;
}
export function clientResponse(...args) {
return construct(ClientResponse, args);
}
6 changes: 3 additions & 3 deletions udsp/request/response.js → udsp/request/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
jsonParse, isTrue, construct, hasValue, noValue
} from '@universalweb/acid';
import { decode } from 'msgpackr';
export class UWResponse {
export class baseObject {
constructor(source, options) {
if (source.isAsk) {
if (source.isAsk || source.isReply) {
const {
head,
method,
Expand Down Expand Up @@ -160,5 +160,5 @@ export class UWResponse {
isResponse = true;
}
export function uwResponseObject(...args) {
return construct(UWResponse, args);
return construct(baseObject, args);
}
9 changes: 8 additions & 1 deletion udsp/request/reply.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Server side client request
* Server receives a request from a client & creates a request and response object to pass along to the endpoint
*/
import {
isEmpty, isBuffer, promise, eachArray, assign, construct, stringify, hasValue, get, objectSize, isArray
} from '@universalweb/acid';
Expand All @@ -8,6 +12,8 @@ import {
import { processEvent } from '#udsp/processEvent';
import { Base } from './base.js';
import { numberEncodedSize } from './numberEncodedSize.js';
import { flushOutgoing } from './flush.js';
import { uwRequestObject } from './requestObject.js';
/**
* @todo
*/
Expand All @@ -29,13 +35,14 @@ export class Reply extends Base {
// // console.log(message);
this.response.id = id;
this.streamIdSize = numberEncodedSize(id);
this.request = uwRequestObject(this);
replyQueue.set(id, this);
}
type = 'reply';
isReply = true;
async completeReceived() {
this.state = 1;
await processEvent(this);
await processEvent(this.request, this);
}
response = {};
request = {};
Expand Down
33 changes: 31 additions & 2 deletions udsp/request/request.js → udsp/request/requestObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
} from '@universalweb/acid';
import { decode, encode } from 'msgpackr';
import {
failed, info, msgReceived, msgSent
failed,
info,
msgReceived,
msgSent
} from '#logs';
export class UWRequest {
constructor(path, options = {}) {
Expand All @@ -26,8 +29,11 @@ export class UWRequest {
signal,
domainCertificate,
profileCertificate,
params
} = options;
this.method = method;
if (method) {
this.method = method;
}
if (hasValue(data)) {
this.data = data;
} else if (hasValue(body)) {
Expand All @@ -38,6 +44,14 @@ export class UWRequest {
} else if (hasValue(headers)) {
this.head = headers;
}
if (path) {
this.path = path;
}
if (hasValue(parameters)) {
this.parameters = parameters;
} else if (hasValue(params)) {
this.parameters = params;
}
}
get headers() {
return this.head;
Expand All @@ -48,6 +62,21 @@ export class UWRequest {
get url() {
return this.path;
}
get params() {
return this.parameters;
}
set headers(value) {
this.head = value;
}
set body(value) {
this.data = value;
}
set url(value) {
this.path = value;
}
set params(value) {
this.parameters = value;
}
isRequest = true;
}
export async function uwRequestObject(source) {
Expand Down
2 changes: 1 addition & 1 deletion udsp/requestMethods/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
success, failed, imported, msgSent, info
} from '#logs';
import { promise, construct, isString } from '@universalweb/acid';
import { UWResponse } from '../request/response.js';
import { UWResponse } from '../request/client/clientResponse.js';
// 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 = {}) {
Expand Down
19 changes: 9 additions & 10 deletions udsp/server/methods/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ async function checkFileExists(filePath) {
console.error('File does not exist');
}
}
export async function get(reply) {
export async function get(req, resp) {
const {
resourceDirectory,
defaultExtension,
cryptography
} = this;
const {
response,
data,
path: filePath
} = reply;
} = req;
if (!isString(filePath) || isEmpty(filePath)) {
console.log('No fileName - Returning empty data');
reply.setHeader('status', 404);
reply.send();
resp.setHeader('status', 404);
resp.send();
return true;
}
let cleanedPath = cleanPath(`${resourceDirectory}/${filePath}`);
Expand All @@ -43,13 +42,13 @@ export async function get(reply) {
const fileData = await read(cleanedPath);
const ext = path.extname(cleanedPath).replace('.', '');
console.log(`EXT => ${ext}`);
reply.setHeader('contentType', ext);
response.data = fileData;
resp.setHeader('contentType', ext);
resp.data = fileData;
} catch {
reply.setHeader('status', 404);
resp.setHeader('status', 404);
} finally {
reply.send();
resp.send();
}
// checksum: cryptography.hash(data)
reply.send();
resp.send();
}

0 comments on commit 0c706f1

Please sign in to comment.