mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
532 stars 120 forks source link

node-request-interceptor doesn't play nicely with aws-sdk when interceptor is enabled #59

Closed ScottyMJacobson closed 3 years ago

ScottyMJacobson commented 3 years ago

👋 again (excited to be one of the first users of the low-level lib)

as part of my product, i'm trying to allow users to "lock" the state of their microservice, and cache the responses of any outbound requests. but during this "locking", i get in an intermediate state, where i need some requests to go through transparently (aws), but the rest to be intercepted (requests to e.g. our other services, public APIs, etc)

i have a feeling aws-sdk is doing something sketchy here, but when i just have the interceptor enabled, here's the error i get trying to allow the aws requests to go through (I have some matching logic that returns undefined for them):

(node:74163) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
    at new ClientRequest (_http_client.js:120:11)
    at request (https.js:289:10)
    at Object.proxiedOriginalRequest (/Users/scottyjacobson/node_modules/node-request-interceptor/lib/interceptors/ClientRequest/index.js:54:36)
    at ClientRequestOverride.<anonymous> (/Users/scottyjacobson/node_modules/node-request-interceptor/lib/interceptors/ClientRequest/ClientRequestOverride.js:241:39)
    at step (/Users/scottyjacobson/node_modules/node-request-interceptor/lib/interceptors/ClientRequest/ClientRequestOverride.js:33:23)
    at Object.next (/Users/scottyjacobson/node_modules/node-request-interceptor/lib/interceptors/ClientRequest/ClientRequestOverride.js:14:53)
    at fulfilled (/Users/scottyjacobson/node_modules/node-request-interceptor/lib/interceptors/ClientRequest/ClientRequestOverride.js:5:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
(node:74163) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:74163) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

reproduction steps:

'use strict'

const { RequestInterceptor } = require('node-request-interceptor')
const withDefaultInterceptors = require('node-request-interceptor/lib/presets/default')
  .default

const interceptor = new RequestInterceptor(withDefaultInterceptors)

interceptor.use(() => {
    return undefined
});

const AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

const s3 = new AWS.S3();

s3.getObject({
    Bucket: 'this.bucket.dont.matter',
    Key: 'neither.does.this'
}).promise();

package.json:

{
  "name": "request-interceptor-59",
  "version": "1.0.0",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.759.0",
    "node-request-interceptor": "^0.5.1"
  }
}

i know there's 218937423894 moving pieces in the aws-sdk, and i wish i could get you a more isolated repro case, but the inner workings of aws-sdk's use of http vs https (and yours for that matter) is slightly out of my pay grade. happy to help hunt down the issue if you know what direction to go looking in!

thanks! Scotty

kettanaito commented 3 years ago

Hey, @ScottyMJacobson! Thanks for reaching out.

I've never tried NRI in the combination with AWS SDK, so there's a room for unexpected behavior. Let us plan this issue and take a look into it in the nearest time.

kettanaito commented 3 years ago

Hi there.

I don't experience the exception you've mentioned using your code, but I assume that is because I don't have any AWS set up in the console, so it just throws for me during the attempts to get the API.

Unfortunately, I see no easy way to debug this. I will not provide AWS with my personal information for the sake of issue debugging and hope for your understanding on this.

If you still experience this issue and wish our team to look into it, please set up a minimal reproduction. It should include a working integration with AWS accessible for us to reproduce the issue. Otherwise I'm afraid there's not much I can help with.