Skip to content

Commit 1d32407

Browse files
committed
Update DeclarativeResponse: add binary and writeStatus support.
Update DeclarativeResponse: add binary and writeStatus support. Update DeclarativeResponse: add binary and writeStatus support.
1 parent da32344 commit 1d32407

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/AppWrapper.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
332332
switch(remainingInstructions[0]) {
333333
case 0: {
334334
/* opCode END */
335-
uint16_t length;
336-
memcpy(&length, remainingInstructions.data() + 1, 2);
337-
remainingInstructions.remove_prefix(3); // Skip opCode and length bytes
335+
uint32_t length;
336+
memcpy(&length, remainingInstructions.data() + 1, 4);
337+
remainingInstructions.remove_prefix(5); // Skip opCode and length bytes
338338

339339
res->end(remainingInstructions.substr(0, length));
340340
remainingInstructions.remove_prefix(length);
@@ -391,9 +391,9 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
391391
break;
392392
case 5: {
393393
/* opCode WRITE */
394-
uint16_t length;
395-
memcpy(&length, remainingInstructions.data() + 1, 2);
396-
remainingInstructions.remove_prefix(3); // Skip opCode and length bytes
394+
uint32_t length;
395+
memcpy(&length, remainingInstructions.data() + 1, 4);
396+
remainingInstructions.remove_prefix(5); // Skip opCode and length bytes
397397

398398
std::string_view valueString(remainingInstructions.data(), length);
399399
remainingInstructions.remove_prefix(length);
@@ -413,6 +413,18 @@ void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
413413
res->write(req->getParameter(keyString));
414414
}
415415
break;
416+
case 7: {
417+
/* opCode WRITE_STATUS */
418+
uint8_t statusLength;
419+
memcpy(&statusLength, remainingInstructions.data() + 1, 1);
420+
remainingInstructions.remove_prefix(2); // Skip opCode and status length bytes
421+
422+
std::string_view statusString(remainingInstructions.data(), statusLength);
423+
remainingInstructions.remove_prefix(statusLength);
424+
425+
res->writeStatus(statusString);
426+
}
427+
break;
416428
}
417429
}
418430

src/uws.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,36 @@ module.exports = (() => {
2323
}
2424
})();
2525

26+
const MAX_U8 = Math.pow(2, 8) - 1;
27+
const textEncoder = new TextEncoder();
28+
const toArrayBuffer = (value) => {
29+
if (typeof value === 'string') return textEncoder.encode(value);
30+
else if (!value) return new Uint8Array(0);
31+
else return value;
32+
};
33+
2634
module.exports.DeclarativeResponse = class DeclarativeResponse {
2735
constructor() {
2836
this.instructions = [];
2937
}
3038

31-
// Utility method to encode text and append instruction
32-
_appendInstruction(opcode, ...text) {
39+
// Append instruction and 1-byte length values
40+
_appendInstruction(opcode, ...values) {
3341
this.instructions.push(opcode);
34-
text.forEach(str => {
35-
const bytes = (typeof str === 'string') ? new TextEncoder().encode(str) : str;
36-
this.instructions.push(bytes.length, ...bytes);
42+
values.forEach(value => {
43+
const arrayBuffer = toArrayBuffer(value);
44+
const length = arrayBuffer.length;
45+
if (length > MAX_U8) throw new RangeError('Data length exceeds ' + MAX_U8);
46+
this.instructions.push(length, ...arrayBuffer);
3747
});
3848
}
3949

40-
// Utility method to append 2-byte length text in little-endian format
41-
_appendInstructionWithLength(opcode, text) {
42-
this.instructions.push(opcode);
43-
const bytes = new TextEncoder().encode(text);
44-
const length = bytes.length;
45-
this.instructions.push(length & 0xff, (length >> 8) & 0xff, ...bytes);
50+
// Append instruction and 4-byte length value
51+
_appendInstructionWithLength(opcode, value) {
52+
const arrayBuffer = toArrayBuffer(value);
53+
const lengthBuffer = new Buffer.alloc(4);
54+
lengthBuffer.writeUInt32LE(arrayBuffer.length);
55+
this.instructions.push(opcode, ...lengthBuffer, ...arrayBuffer);
4656
}
4757

4858
writeHeader(key, value) { return this._appendInstruction(1, key, value), this; }
@@ -51,11 +61,9 @@ module.exports.DeclarativeResponse = class DeclarativeResponse {
5161
writeHeaderValue(key) { return this._appendInstruction(4, key), this; }
5262
write(value) { return this._appendInstructionWithLength(5, value), this; }
5363
writeParameterValue(key) { return this._appendInstruction(6, key), this; }
54-
64+
writeStatus(status) { return this._appendInstruction(7, status), this; }
5565
end(value) {
56-
const bytes = new TextEncoder().encode(value);
57-
const length = bytes.length;
58-
this.instructions.push(0, length & 0xff, (length >> 8) & 0xff, ...bytes);
66+
this._appendInstructionWithLength(0, value);
5967
return new Uint8Array(this.instructions).buffer;
6068
}
6169
}

0 commit comments

Comments
 (0)