-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial seperation major changes will break API after first layer
- Loading branch information
Universal Web
committed
Sep 24, 2023
1 parent
d4abb9b
commit 0c706f1
Showing
8 changed files
with
224 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters