Skip to content

Commit

Permalink
fix(nano-server): handle utf8 reply issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Dec 18, 2022
1 parent 3edc550 commit 33aa238
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions core/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,24 +320,22 @@ export class AlwatrConnection {
*/
reply(content: AlwatrServiceResponse): void {
content.statusCode ??= 200;
// this._logger.logMethodArgs('reply', {
// ok: content.ok,
// statusCode: content.statusCode,
// errorCode: content.errorCode,
// });

// this._logger.logMethodArgs('reply', content);
this._logger.logMethodArgs('reply', {ok: content.ok, statusCode: content.statusCode});

if (this.serverResponse.headersSent) {
this._logger.accident('reply', 'http_header_sent', 'Response headers already sent');
return;
}

let contentStr: string;
let buffer: Buffer;

try {
contentStr = JSON.stringify(content);
this._logger.logMethodArgs('reply', contentStr.length > 400 ? contentStr.substring(0, 200) + '...' : content);
buffer = Buffer.from(JSON.stringify(content), 'utf8');
}
catch {
this._logger.accident('responseData', 'data_stringify_failed', 'JSON.stringify(data) failed!');
catch (err) {
this._logger.accident('responseData', 'data_stringify_failed', 'JSON.stringify(data) failed!', err);
return this.reply(
content.ok === false
? {
Expand All @@ -354,7 +352,7 @@ export class AlwatrConnection {
}

const headers: Record<string, string | number> = {
'Content-Length': contentStr.length,
'Content-Length': buffer.byteLength,
'Content-Type': 'application/json',
'Server': 'Alwatr NanoServer',
};
Expand All @@ -365,12 +363,9 @@ export class AlwatrConnection {

this.serverResponse.writeHead(content.statusCode ?? 200, headers);

this.serverResponse.write(contentStr, 'utf8', (error: NodeJS.ErrnoException | null | undefined) => {
this.serverResponse.write(buffer, 'binary', (error: NodeJS.ErrnoException | null | undefined) => {
if (error == null) return;
this._logger.accident('reply', 'http_response_write_failed', 'Response write failed', {
errCode: error.code,
errMessage: error.message,
});
this._logger.error('reply', 'http_response_write_failed', 'reply failed', error);
});

this.serverResponse.end();
Expand Down

0 comments on commit 33aa238

Please sign in to comment.