npm / npm-registry-client

http://npm.im/npm-registry-client
ISC License
264 stars 108 forks source link

TypeError: The header content contains invalid characters #159

Closed zot24 closed 7 years ago

zot24 commented 7 years ago

Any idea why I could be getting the following error?

info retry will retry, error on last attempt: TypeError: The header content contains invalid characters

with

npm@4.2.0
node@v7.10.0

I'm using the following library semantic-release/semantic-release that use their on dependecy last-release-npm that use npm/npm-registry-client

The whole error looks like:

semantic-release verb init options: { argv: { remain: [ 'pre' ], cooked: [ 'pre' ], original: [ 'pre' ] },
semantic-release verb init   branch: 'master',
semantic-release verb init   verifyConditions: { path: './node_modules/@krux/condition-jenkins' },
semantic-release verb init   fallbackTags: { next: 'latest' },
semantic-release verb init   debug: false,
semantic-release verb init   githubToken: '***',
semantic-release verb init   githubUrl: undefined }
semantic-release verb init Verifying config.
semantic-release verb pre Running pre-script.
semantic-release verb pre Veriying conditions.
semantic-release verb pre Wrote authToken to .npmrc.
verb request uri https://registry.npmjs.org/moltin
verb request no auth needed
info attempt registry request try #1 at 12:17:52 AM
verb request using bearer token for auth
verb request id 24fac10f1313f5fe
http request GET https://registry.npmjs.org/moltin
info retry will retry, error on last attempt: TypeError: The header content contains invalid characters
info attempt registry request try #2 at 12:18:02 AM
verb request using bearer token for auth
http request GET https://registry.npmjs.org/moltin
info retry will retry, error on last attempt: TypeError: The header content contains invalid characters
info attempt registry request try #3 at 12:19:02 AM
verb request using bearer token for auth
http request GET https://registry.npmjs.org/moltin
semantic-release ERR! pre Failed to determine new version.
semantic-release ERR! pre The header content contains invalid characters TypeError: The header content contains invalid characters
semantic-release ERR! pre     at ClientRequest.setHeader (_http_outgoing.js:374:11)
semantic-release ERR! pre     at new ClientRequest (_http_client.js:131:14)
semantic-release ERR! pre     at Object.request (http.js:26:10)
semantic-release ERR! pre     at Object.request (https.js:206:15)
semantic-release ERR! pre     at Request.start (/usr/local/lib/node_modules/semantic-release/node_modules/request/request.js:755:32)
semantic-release ERR! pre     at Request.end (/usr/local/lib/node_modules/semantic-release/node_modules/request/request.js:1525:10)
semantic-release ERR! pre     at end (/usr/local/lib/node_modules/semantic-release/node_modules/request/request.js:567:14)
semantic-release ERR! pre     at Immediate.<anonymous> (/usr/local/lib/node_modules/semantic-release/node_modules/request/request.js:581:7)
semantic-release ERR! pre     at runCallback (timers.js:672:20)
semantic-release ERR! pre     at tryOnImmediate (timers.js:645:5)
zkat commented 7 years ago

@zot24 I don't think this is an npm-registry-client bug. I believe the answer will be obvious if you edit /usr/local/lib/node_modules/semantic-release/node_modules/request/request.js:755 and log out the headers in the reqOptions object. I'll leave this open largely because I'm very curious about what this is.

zot24 commented 7 years ago

Thanks for your suggestion @zkat this was killing me (spoilers) I found the error ☺️ (spoilers)

I realise while checking that file as you suggested that you could add NODE_DEBUG so I did add NODE_DEBUG=request to my env var and this is the result of the query

semantic-release verb init Verifying config.
semantic-release verb pre Running pre-script.
semantic-release verb pre Veriying conditions.
semantic-release verb pre Wrote authToken to .npmrc.
verb request uri https://registry.npmjs.org/moltin
verb request no auth needed
info attempt registry request try #1 at 8:48:09 AM
verb request using bearer token for auth
verb request id f0fe055d5bc8625e
http request GET https://registry.npmjs.org/moltin
REQUEST { url: 
   Url {
     protocol: 'https:',
     slashes: true,
     auth: null,
     host: 'registry.npmjs.org',
     port: null,
     hostname: 'registry.npmjs.org',
     hash: null,
     search: null,
     query: null,
     pathname: '/moltin',
     path: '/moltin',
     href: 'https://registry.npmjs.org/moltin' },
  method: 'GET',
  headers: 
   { 'accept-encoding': 'gzip',
     authorization: 'Bearer 3299d44f-88b1-46fa-bdf9-912af33bca78\n',
     version: '7.5.0',
     accept: 'application/json',
     'npm-session': 'f0fe055d5bc8625e',
     'npm-in-ci': 'true',
     'user-agent': 'node/v7.10.0' },
  strictSSL: true,
  agent: 
   Agent {
     domain: null,
     _events: { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 443,
     protocol: 'https:',
     options: 
      { key: undefined,
        cert: undefined,
        ca: undefined,
        rejectUnauthorized: true,
        localAddress: undefined,
        maxSockets: 50,
        keepAlive: true,
        path: null },
     requests: {},
     sockets: {},
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: true,
     maxSockets: 50,
     maxFreeSockets: 256,
     maxCachedSessions: 100,
     _sessionCache: { map: {}, list: [] } },
  followRedirect: true,
  encoding: null,
  callback: [Function] }
REQUEST make request https://registry.npmjs.org/moltin
authorization: 'Bearer 3299d44f-88b1-46fa-bdf9-912af33bca78\n',
\n

Just to explain the why and satisfy your curiosity it was all my fault because I was doing this

echo \$(cat .npmrc) | sed -n -e 's/^.*_authToken=//p'

Instead of this go gather the NPM_TOKEN form .npmrc

echo -n \$(cat .npmrc) | sed -n -e 's/^.*_authToken=//p'

Note: That token it's no longer valid 😉

zkat commented 7 years ago

awesome! Glad you figured it out!