dojo / core

:rocket: Dojo 2 - language helpers and utilities.
http://dojo.io
Other
213 stars 62 forks source link

core/request : node provider does not decompress #259

Closed sebilasse closed 7 years ago

sebilasse commented 7 years ago

It should handle gzip and deflate, e.g. https://github.com/dojo/core/blob/master/src/request/node.ts

import * as zlib from 'zlib';

for line 319 - 326

const contentEncoding = nativeResponse.headers['content-encoding'];
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
    // If compressed, decompress
    const cMethod = (contentEncoding === 'gzip') ? 'gunzip' : 'inflate';
    zlib[cMethod](response.data, function(err: Error, decodedResponse: any) {
        if (err) { reject(err); }
        response.data = decodedResponse;
        resolve(response);
    });
} else if (!options.streamTarget) {
    // If using a streamTarget, wait for it to finish in case it throws an error
    resolve(response);
} else {
    options.streamTarget.close();
}

But please note that const contentEncoding = nativeResponse.headers['content-encoding']; is guaranteed to work only w. node/express because HTTP header fields are usually case-insensitive ...

sebilasse commented 7 years ago

Another suggestion for the node provider is to add a message to the cryptic E-messages. Could be like

const ErrorMessages = [
  'FileNotFoundError: The file was not found where specified ...',
  'FileAccessError: During a file-access or disk-access operation we could not '+
    'make a connection between the path and the file name.',
  'EOFError: Either we could not parse the End of File (e.g a forgotten ending '+
    'curly brace for a block of code) or there was no data.',
  'UnknownHostError: The destination computer (host server name) cannot be resolved.',
  'SocketError: A socket-related error occured.',
  'ProtocolError: The received answer of the server was complete but there was a '+
    'protocol error, e.g. CA certs and node: https://github.com/nodejs/node/issues/2244',
  'FileSystemError: Please repair your FileSystem.',
  'ConnectError: There is an error with the connection. Usually an error of the host server.',
  'BindError: When the allowed connections are exhausted, you get this error when trying '+
    'to make any new connections.',
  'AddressNotFoundError: This address could not be found.',
  'NetworkError: You were somehow offline when accessing the URL.'
];

const Errors = {
  ENOENT: 0,
  EACCES: 1, EPERM: 1,
  EOF: 2,
  EADDRINFO: 3,
  EISCONN: 4, ENOTCONN: 4, ENOTSOCK: 4, ENOTSUP: 4, EAIFAMNOSUPPORT: 4, EAISERVICE: 4,
  EPROTO: 5, EPROTONOSUPPORT: 5, EPROTOTYPE: 5,
  EBUSY: 6, EAGAIN: 6, EBADF: 6, EMFILE: 6, ENOTDIR: 6, EISDIR: 6, EEXIST: 6, ENAMETOOLONG: 6, ELOOP: 6,
  ENOTEMPTY: 6, ENOSPC: 6, EIO: 6, EROFS: 6, ENODEV: 6, ESPIPE: 6, ECANCELED: 6, ENFILE: 6, EXDEV: 6,
  ECONNABORTED: 7, ECONNREFUSED: 7, ECONNRESET: 7, ETIMEDOUT: 7,
  EADDRNOTAVAIL: 8,
  ENOTFOUND: 9,
  EAFNOSUPPORT: 10, EALREADY: 10,  EDESTADDRREQ: 10, EHOSTUNREACH: 10, EMSGSIZE: 10,
  ENETDOWN: 10, ENETUNREACH: 10, ENONET: 10, EPIPE: 10, EAISOCKTYPE: 10, ESHUTDOWN: 10
}

The value of Errors means the index of ErrorMessages : ErrorMessages[Errors[myNodeError]]

rorticus commented 7 years ago

gzip/deflate support added in https://github.com/dojo/core/pull/261

rorticus commented 7 years ago

Fixed in #261