node-modules / agentkeepalive

Support keepalive http agent.
MIT License
579 stars 57 forks source link

Causes uncaught TypeError: object is not a function #42

Open cdhowie opened 8 years ago

cdhowie commented 8 years ago

I'm trying to use this in combination with the AWS SDK. After some number of requests, this uncaught error is thrown:

     Uncaught TypeError: object is not a function
      at afterWrite (_stream_writable.js:354:3)
      at onwrite (_stream_writable.js:345:7)
      at WritableState.onwrite (_stream_writable.js:89:5)
      at Socket._writeGeneric (net.js:706:5)
      at Socket._writev (net.js:711:8)
      at doWrite (_stream_writable.js:298:12)
      at clearBuffer (_stream_writable.js:387:5)
      at Socket.Writable.uncork (_stream_writable.js:237:7)
      at ClientRequest._flushOutput (_http_outgoing.js:673:10)
      at ClientRequest.OutgoingMessage._flush (_http_outgoing.js:647:16)
      at _http_client.js:156:10
      at callSocketMethod (_http_client.js:552:7)
      at ClientRequest.onSocket (_http_client.js:557:7)
      at tickOnSocket (_http_client.js:523:7)
      at onSocketNT (_http_client.js:535:5)

Using the default Node agent does not cause this behavior. This is on Node 4.5.0.

cdhowie commented 8 years ago

I should note that I run into this behavior when using new http.Agent({ keepAlive: true }) as well, but not when using new http.Agent(). So perhaps this is an issue internal to Node itself. :(

fengmk2 commented 7 years ago

Can you show me a demo to reappear this problem?

cdhowie commented 7 years ago

I will try to produce a self-contained repro.

ngbrown commented 7 years ago

I'm getting a similar error, both version 2.2.0 and 3.1.1 when trying to use it with webpack-dev-server.

_stream_writable.js:388
  cb();
  ^

TypeError: object is not a function
    at afterWrite (_stream_writable.js:388:3)
    at onwrite (_stream_writable.js:379:7)
    at WritableState.onwrite (_stream_writable.js:90:5)
    at Socket._writeGeneric (net.js:721:5)
    at Socket._write (net.js:731:8)
    at doWrite (_stream_writable.js:334:12)
    at writeOrBuffer (_stream_writable.js:320:5)
    at Socket.Writable.write (_stream_writable.js:247:11)
    at Socket.write (net.js:658:40)
    at ClientRequest.OutgoingMessage._writeRaw (_http_outgoing.js:170:23)

The error doesn't occur when not using agentkeepalive, but obviously, the connections aren't kept alive, so my Windows Authentication won't work.

ngbrown commented 7 years ago

This may actually be a Node.js issue, as the default http.Agent also fails when keepAive is true.

nodejs/node#8650

chimurai commented 7 years ago

Same issue in this thread: https://github.com/chimurai/http-proxy-middleware/issues/39#issuecomment-260345952

Hopefully you guys can help to solve it.

Reproducible with a tiny example (created by @EladBezalel) c3215-untitled.gz - source: https://github.com/chimurai/http-proxy-middleware/issues/39#issuecomment-260965382

https://localhost:12345/api/123 - works: browser -> server https://localhost:3000/api/123 - fails: browser -> proxy + keepalive -> server

Here is some debug logging from the example:

$ NODE_DEBUG=http,net node index.js

[HPM] Proxy created: /  ->  http://localhost:12345/
[HPM] Subscribed to http-proxy events:  [ 'proxyReq', 'proxyRes', 'error', 'close' ]
NET 6552: listen2 null 3000 4 false undefined
NET 6552: _listen2: create a handle
NET 6552: bind to ::
NET 6552: onconnection
NET 6552: _read
NET 6552: Socket._read readStart
HTTP 6552: SERVER new http connection
[HPM] GET /api/123 -> http://localhost:12345/
NET 6552: createConnection [ { keepAliveTimeout: 30000,
    timeout: 60000,
    keepAliveMsecs: 1000,
    maxFreeSockets: 10,
    keepAlive: true,
    maxSockets: 100,
    path: null,
    localAddress: undefined,
    agent:
     Agent {
       domain: null,
       _events: [Object],
       _eventsCount: 4,
       _maxListeners: undefined,
       defaultPort: 80,
       protocol: 'http:',
       options: [Object],
       requests: {},
       sockets: [Object],
       freeSockets: {},
       keepAliveMsecs: 1000,
       keepAlive: true,
       keepAliveTimeout: 30000,
       timeout: 60000,
       maxSockets: 100,
       maxFreeSockets: 10,
       createSocketCount: 0,
       closeSocketCount: 0,
       errorSocketCount: 0,
       requestCount: 0,
       timeoutSocketCount: 0 },
    headers:
     { 'accept-language': 'en-US,en;q=0.8,nl;q=0.6',
       'accept-encoding': 'gzip, deflate, sdch, br',
       dnt: '1',
       accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
       'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
       'upgrade-insecure-requests': '1',
       'cache-control': 'no-cache',
       pragma: 'no-cache',
       connection: 'keep-alive',
       host: 'localhost:3000' },
    method: 'GET',
    secureProtocol: undefined,
    ciphers: undefined,
    ca: undefined,
    cert: undefined,
    passphrase: undefined,
    key: undefined,
    pfx: undefined,
    socketPath: undefined,
    hostname: 'localhost',
    host: 'localhost',
    port: '12345',
    servername: 'localhost',
    encoding: null } ]
NET 6552: pipe false null
NET 6552: connect: find host localhost
NET 6552: connect: dns options { family: undefined, hints: 1024 }
HTTP 6552: SERVER socketOnParserExecute 442
NET 6552: _read
NET 6552: _read wait for connection
HTTP 6552: outgoing message end.
NET 6552: afterConnect
NET 6552: _read
NET 6552: Socket._read readStart
NET 6552: onread 140
NET 6552: got data
HTTP 6552: AGENT incoming response!
HTTP 6552: AGENT isHeadResponse false
NET 6552: _read
HTTP 6552: AGENT socket keep-alive
HTTP 6552: outgoing message end.
[HPM] GET /api/123 -> http://localhost:12345/
HTTP 6552: SERVER socketOnParserExecute 540
_stream_writable.js:361
  cb();
  ^

TypeError: cb is not a function
    at afterWrite (_stream_writable.js:361:3)
    at onwrite (_stream_writable.js:352:7)
    at WritableState.onwrite (_stream_writable.js:89:5)
    at Socket._writeGeneric (net.js:714:5)
    at Socket._write (net.js:724:8)
    at doWrite (_stream_writable.js:307:12)
    at writeOrBuffer (_stream_writable.js:293:5)
    at Socket.Writable.write (_stream_writable.js:220:11)
    at Socket.write (net.js:650:40)
    at ClientRequest.OutgoingMessage._writeRaw (_http_outgoing.js:167:23)
chimurai commented 7 years ago

https://blogs.msdn.microsoft.com/chiranth/2013/09/20/ntlm-want-to-know-how-it-works/

Looks like it fails somewhere between 2nd request and 2nd response.

request headers [1]

{ host: 'localhost:3000',
  connection: 'keep-alive',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  'upgrade-insecure-requests': '1',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  dnt: '1',
  'accept-encoding': 'gzip, deflate, sdch, br',
  'accept-language': 'en-US,en;q=0.8,nl;q=0.6' }

response headers [1]

{ 'content-length': '0',
  server: 'Microsoft-HTTPAPI/2.0',
  'www-authenticate': [ 'NTLM' ],
  date: 'Wed, 01 Mar 2017 21:16:03 GMT' }

request headers [2]

{ host: 'localhost:3000',
  connection: 'keep-alive',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  authorization: 'NTLM TlRMTVNTUAABAAAAB7IIogkACQAvAAAABwAHACgAAAAGAbEdAAAAD0NPUkUtSTdXT1JLR1JPVVA=',
  'upgrade-insecure-requests': '1',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  dnt: '1',
  'accept-encoding': 'gzip, deflate, sdch, br',
  'accept-language': 'en-US,en;q=0.8,nl;q=0.6' }

response headers [2] expecting the last response, but instead it fails.

_stream_writable.js:361
  cb();
  ^

TypeError: cb is not a function
    at afterWrite (_stream_writable.js:361:3)
    at onwrite (_stream_writable.js:352:7)
    at WritableState.onwrite (_stream_writable.js:89:5)
    at Socket._writeGeneric (net.js:714:5)
    at Socket._write (net.js:724:8)
    at doWrite (_stream_writable.js:307:12)
    at writeOrBuffer (_stream_writable.js:293:5)
    at Socket.Writable.write (_stream_writable.js:220:11)
    at Socket.write (net.js:650:40)
    at ClientRequest.OutgoingMessage._writeRaw (_http_outgoing.js:167:23)
fengmk2 commented 7 years ago

@chimurai Which node version?

chimurai commented 7 years ago

tested with v6.2.1 and v7.71

chimurai commented 7 years ago

Found the issue in http-proxy-middleware. (https://github.com/chimurai/http-proxy-middleware/pull/149)

@ngbrown This might fix the issue you're having, since webpack-dev-server is using http-proxy-middleware to do the proxying.

cdhowie commented 7 years ago

There is still an issue in Node proper, however, as I encounter this error using Node's own http.Agent in keepalive mode.