Proposal: Refactor DeclarativeResponse#1072
Closed
webcarrot wants to merge 2 commits intouNetworking:masterfrom
Closed
Proposal: Refactor DeclarativeResponse#1072webcarrot wants to merge 2 commits intouNetworking:masterfrom
webcarrot wants to merge 2 commits intouNetworking:masterfrom
Conversation
Allow binary data and check buffers length.
Contributor
|
It blows up in size. |
Author
|
@uNetworkingAB Should I close this one? (I use different implementation anyway.) |
Contributor
|
It still needs binary support but I did not like the size blowing up like that. What kind of implementation do you use? |
Author
|
Something like (playgound): const TE = new TextEncoder();
const EU8A = new Uint8Array(0);
function toU8(data: string | Uint8Array | undefined): Uint8Array {
if (!data) return EU8A;
if (typeof data === 'string') return TE.encode(data);
return data;
}
const enum OPC {
END = 0,
WRITE_HEADER = 1,
WRITE_BODY_VALUE = 2,
WRITE_QUERY_VALUE = 3,
WRITE_HEADER_VALUE = 4,
WRITE = 5,
WRITE_PARAMETER_VALUE = 6,
}
const MAX_U8 = Math.pow(2, 8) - 1;
const MAX_U16 = Math.pow(2, 16) - 1;
const OPCODES = [
Uint8Array.from([OPC.END]),
Uint8Array.from([OPC.WRITE_HEADER]),
Uint8Array.from([OPC.WRITE_BODY_VALUE]),
Uint8Array.from([OPC.WRITE_QUERY_VALUE]),
Uint8Array.from([OPC.WRITE_HEADER_VALUE]),
Uint8Array.from([OPC.WRITE]),
Uint8Array.from([OPC.WRITE_PARAMETER_VALUE]),
] as const;
export class DeclarativeResponse {
#instructions: Array<Uint8Array> = [];
#instructionsLength: number = 0;
#appendOpCode(opcode: OPC) {
this.#instructions.push(OPCODES[opcode]);
this.#instructionsLength += 1;
}
#appendInstruction(text: string) {
const bytes = TE.encode(text);
const length = bytes.byteLength;
if (length > MAX_U8) throw new RangeError(`Data byte length ${length} greater than ${MAX_U8}`);
this.#instructions.push(Uint8Array.from([length]));
this.#instructions.push(bytes);
this.#instructionsLength += 1 + length;
}
#appendBytes(bytes: Uint8Array) {
const length = bytes.byteLength;
this.#instructions.push(Uint8Array.from([length & 0xff, (length >> 8) & 0xff]));
if (length) this.#instructions.push(bytes);
this.#instructionsLength += 2 + length;
}
writeHeader(key: string, value: string) {
this.#appendOpCode(OPC.WRITE_HEADER);
this.#appendInstruction(key);
this.#appendInstruction(value);
return this;
}
writeBody() {
this.#appendOpCode(OPC.WRITE_BODY_VALUE);
return this;
}
writeQueryValue(key: string) {
this.#appendOpCode(OPC.WRITE_QUERY_VALUE);
this.#appendInstruction(key);
return this;
}
writeHeaderValue(key: string) {
this.#appendOpCode(OPC.WRITE_HEADER_VALUE);
this.#appendInstruction(key);
return this;
}
write(data: string | Uint8Array) {
const bytes = toU8(data);
if (!bytes.byteLength) {
return this;
}
if (bytes.byteLength > MAX_U16) {
let offset = 0;
while (offset < bytes.byteLength) {
this.#appendOpCode(OPC.WRITE);
this.#appendBytes(bytes.subarray(offset, offset + MAX_U16));
offset += MAX_U16;
}
} else {
this.#appendOpCode(OPC.WRITE);
this.#appendBytes(bytes);
}
return this;
}
writeParameterValue(key: string) {
this.#appendOpCode(OPC.WRITE_PARAMETER_VALUE);
this.#appendInstruction(key);
return this;
}
end(data?: string | Uint8Array): ArrayBufferLike {
const bytes = toU8(data);
if (bytes.byteLength > MAX_U16) {
let offset = 0;
while (offset < bytes.byteLength) {
this.#appendOpCode(offset + MAX_U16 < bytes.byteLength ? OPC.WRITE : OPC.END);
this.#appendBytes(bytes.subarray(offset, offset + MAX_U16));
offset += MAX_U16;
}
} else {
this.#appendOpCode(OPC.END);
this.#appendBytes(bytes);
}
const output = new Uint8Array(this.#instructionsLength);
let offset = 0;
for (const chunk of this.#instructions) {
output.set(chunk, offset);
offset += chunk.byteLength;
}
return output.buffer;
}
} |
Contributor
|
I added this 4c29320 |
Contributor
|
Ah never mind that's not it |
Contributor
|
Maybe best to just take your variant |
|
You might be interested in whether it is possible to dynamically increase the buffer size. |
Author
|
Clean-up dashboard. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PS. I like this
ArrayBufferbased API 😉PPS. In theory we can send blobs of byte length greater than > MAX U16 by calling
write/5multiple times.