interceptors icon indicating copy to clipboard operation
interceptors copied to clipboard

support of BUN runtime

Open OleksandrKucherenko opened this issue 1 year ago • 8 comments

Root of the problem is inside the interceptors, so I put a reference here - https://github.com/mswjs/msw/issues/1718

Current behaviour

BUN produce this error

clis/gpt/src/gpt.test.ts:
879 |     if (this.aborted)
880 |       return;
881 |     this[kAbortController].abort();
882 |   }
883 | 
884 |   constructor(input, options, cb) {
                             ^
TypeError: Cannot call a class constructor without |new|
      at ClientRequest (node:http:884:26)
      at new NodeClientRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:104:20)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/http.request.js:37:15
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:284:8
      at new RedirectableRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:66:2)
      at request (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/follow-redirects/index.js:523:13)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:438:10
      at dispatchHttpRequest (/Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:149:54)
      at /Users/o.kucherenko2/workspace/gitlabs/gpt.md.translate/node_modules/axios/lib/adapters/http.js:143:4
      at new Promise (:1:20)
image

Will be great if we get some attention and support from the community. Thanks!

OleksandrKucherenko avatar Sep 09 '23 07:09 OleksandrKucherenko

Hey, @OleksandrKucherenko. Thanks for reporting this.

This looks suspiciously alike to be a TypeScript transpilation issue. Can you try different target and module settings in your tsconfig.json? I swear I've seen this before, and it may have nothing to do with Bun.

I believe what ends up happening is that your TS settings produce JavaScript code that tries to construct a class without the new keyword. I've also seen a similar issue when transpiling drops the super keyword when extending classes. I hope it's a configuration issue.

Regarding Bun support, if Bun's goal is full-ish compatibility with Node.js, then it's likely an issue in Bun. Interceptors have been running stable since Node v14, and now support v18+ on the main. I doubt we have overlooked something but I'm always open to someone proving me wrong.

kettanaito avatar Sep 11 '23 20:09 kettanaito

Will be great if you can try BUN on your side and i will play with the different settings and will see if it gives us something.

ts-node + node v18 works with such config, so potentially it http module in bun's that does not provide identical api is the issue.

If you can help to identify the root cause, it will be a great help.

Thanks a lot

OleksandrKucherenko avatar Sep 11 '23 21:09 OleksandrKucherenko

Thank you for creating these issues!

I also want to migrate to bun but use msw extensively so without msw changing to bun is not really feasible

dagadbm avatar Sep 12 '23 02:09 dagadbm

I found something maybe useful, but I don't know why Screen Shot 2023-09-16 at 11 27 23

xxleyi avatar Sep 16 '23 03:09 xxleyi

@xxleyi, that should be okay. ClientRequest is a standard class in node from the http module. NodeClientRequest (the Interceptors' extension) extends the native http.ClientRequest class. Is that class supported in Bun?

kettanaito avatar Sep 16 '23 13:09 kettanaito

@xxleyi, that should be okay. ClientRequest is a standard class in node from the http module. NodeClientRequest (the Interceptors' extension) extends the native http.ClientRequest class. Is that class supported in Bun?

I checked node http code and found ClientRequest is function. here is link https://github.com/nodejs/node/blob/main/lib/_http_client.js#L130

xxleyi avatar Sep 16 '23 13:09 xxleyi

@kettanaito @OleksandrKucherenko I did more research about this issue and found the problem is a little complicated. The direct issue here is that ClientRequest is used as a function because of the ES5 target of mswjs/interceptors 0.17.10. However, because ClientRequest in node is indeed a function, so msw v1 can work well in node. However ClientRequest in Bun is a real Class, so we have below error message when msw v1 runs in Bun:

TypeError: Cannot call a class constructor without |new|

which comes from _super.call line of below code:

var NodeClientRequest = /** @class */ (function (_super) {
    __extends(NodeClientRequest, _super);
    function NodeClientRequest(_a, options) {
        var _b = __read(_a, 3), url = _b[0], requestOptions = _b[1], callback = _b[2];
        var _this = _super.call(this, requestOptions, callback) || this;
        _this.chunks = [];
        _this.responseSource = 'mock';
        _this.requestBody = [];

and above code is compiled from mswjs/interceptors 0.17.10 by tsc with ES5 target.

So if we want to fix this problem, maybe one way is to adapt the build target of mswjs/interceptors to at least ES6(in fact v0.18.3 of mswjs/interceptors already has), and another way is to adapt ClientRequest in Bun to a function implementation.

xxleyi avatar Sep 17 '23 03:09 xxleyi

Thanks for the insight @xxleyi. If that's indeed the problem, I highly recommend updating to MSW 2.0 that depends on the newer version of Interceptors that doesn't have this issue. There is no plans to bring this build target change to MSW 1.x (Interceptors <=0.17.10).

kettanaito avatar Nov 11 '23 13:11 kettanaito

I am closing this because there's nothing we should do on the Interceptors side to support Bun. Interceptors is focused on Node.js, and if Bun provides sufficient and correct compatibility, Interceptors will work with Bun. We haven't shipped a single tooling-specific logic for 7 years, we aren't going to start now for Bun.

kettanaito avatar Apr 17 '24 08:04 kettanaito