restify / restify/node-restify

Restify can't sendRaw none `utf8` data - it always encoded to utf-8

Open
#1,852 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.7k
Forks
975
Avg merge
1d 6h
Merged PRs (30d)
5

Description

Hello!

Thanks for Restify - it's absolutely cool!

I found a bug in the function sendRaw - when you try to send none utf8 data in response - you can't do it. Data always will be encoded to utf-8.

I use charSet - to set latin1 encoding for sending ansi mp3-file.

res.charSet('latin1').sendRaw(200, mp3fileData,{
	'Content-Type': 'audio/mpeg',
});

But it's take affect only for Content-Type field.

Cause

In response.js does not send second param encoding to http write method. Http Docs.
https://github.com/restify/node-restify/blob/9153587c023a876237c1d8bc7491fee4984d9074/lib/response.js#L874

Are you willing and able to fix this?

Yes I can.


    // Send body if it was provided
    if (res._data) {
        // https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback
        // Default: 'utf8'
        let encoding = 'utf8';

        // Check :: is encoding valid?
        // in my point of view - we must validate encoding - in charSet() method
        // https://nodejs.org/docs/latest-v12.x/api/buffer.html#buffer_buffers_and_character_encodings
        let characterEncodingsSupportedByNodeJs = [
            'utf8',
            'utf16le',
            'latin1',
            'base64',
            'hex',
            'ascii',
            'binary',
            'ucs2',
        ];
        let characterEncodingsSupportedByNodeJs_id = characterEncodingsSupportedByNodeJs.indexOf(res._charSet);
        if ( characterEncodingsSupportedByNodeJs_id > -1 ) encoding = characterEncodingsSupportedByNodeJs[characterEncodingsSupportedByNodeJs_id];

        res.write(res._data, encoding);
    }

``

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/response.js around the linked line near 874 and review how sendRaw uses Node.js response.write, alongside the charSet flow described in the issue. Done means non-UTF8 data passed to sendRaw is no longer forced to utf-8 while the response behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.