bun icon indicating copy to clipboard operation
bun copied to clipboard

`@elastic/elasticsearch` - The superclass is not a constructor

Open asilvas opened this issue 2 years ago • 11 comments
trafficstars

What version of Bun is running?

0.4.1

What platform is your computer?

No response

What steps can reproduce the bug?

Should be reproducible without a running ES.

import { Client } from '@elastic/elasticsearch';

export const client = new Client({ node: 'http://localhost:9200' })

What is the expected behavior?

Does not throw exception.

What do you see instead?

[6.98ms] "node_modules.bun" - 15 modules, 8 packages
2 | 
3 | const https = require('https')
4 | const http = require('http')
5 | const { URL } = require('url')
6 | 
7 | class HttpProxyAgent extends http.Agent {
   ^
TypeError: The superclass is not a constructor.
      at /usr/x/gi/node_modules/hpagent/index.js:7:0
      at node_modules.server.bun:7:13092
      at /usr/x/gi/node_modules/@elastic/transport/lib/connection/HttpConnection.js:23:26
      at node_modules.server.bun:7:13092
      at /usr/x/gi/node_modules/@elastic/transport/lib/connection/index.js:25:33
      at node_modules.server.bun:7:13092
      at /usr/x/gi/node_modules/@elastic/transport/index.js:27:2
      at node_modules.server.bun:7:13092
      at /usr/x/gi/node_modules/@elastic/elasticsearch/index.js:34:2
      at node_modules.server.bun:7:13092
      at /usr/x/gi/src/worker.ts:23:0

Additional information

No response

asilvas avatar Jan 09 '23 00:01 asilvas

The http.Agent polyfill is not implemented yet, but coming very soon. Stay tuned

ThatOneBro avatar Jan 09 '23 00:01 ThatOneBro

I have a same problem with apollographql

17 |
18 | class ReadAfterDestroyedError extends Error {}
19 |
20 | exports.ReadAfterDestroyedError = ReadAfterDestroyedError;
21 |
22 | class ReadStream extends _fs.default.ReadStream {
   ^
TypeError: The superclass is not a constructor.
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/@apollographql/graphql-upload-8-fork/node_modules/fs-capacitor/lib/index.js:22:0
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/@apollographql/graphql-upload-8-fork/lib/processRequest.js:10:4
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/@apollographql/graphql-upload-8-fork/lib/index.js:10:4
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/apollo-server-core/dist/processFileUploads.js:9:8
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/apollo-server-core/dist/processFileUploads.js:12:4
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/apollo-server-core/dist/index.js:44:4
      at /Users/syd/work/sitak/pureSatek/packages/server/node_modules/apollo-server-express/dist/index.js:13:4
      at /Users/syd/work/sitak/pureSatek/packages/server/dist/src/index.js:16:6
      at bun:main:8:19

hemedani avatar Jan 29 '23 11:01 hemedani

I have a same problem with apollographql

22 | class ReadStream extends _fs.default.ReadStream {
   ^
TypeError: The superclass is not a constructor.

This is a similar problem but with a different module, it's actually more related to #1797 . We should really address that one too.

As for this issue regarding http.Agent, the fix should be in the next release. PR will be up today. Will test @elastic/elasticsearch to make sure it works or if there is something else to fix.

ThatOneBro avatar Jan 29 '23 16:01 ThatOneBro

http.Agent doesn't seem to be a problem anymore in canary, but we have a new error:

 96 |             };
 97 |         }
 98 |         else if (this.tls !== null) {
 99 |             undiciOptions.connect = this.tls;
100 |         }
101 |         this.pool = new undici_1.Pool(this.url.toString(), undiciOptions);
                        ^
TypeError: undefined is not a constructor (evaluating 'new undici_1.Pool(this.url.toString(), undiciOptions)')

I'm guessing this has to do with our undici polyfill not having Pool implemented yet. Will have to look into it, though, in order to confirm.

ThatOneBro avatar Feb 02 '23 07:02 ThatOneBro

@hemedani

I have a same problem with apollographql

17 |
18 | class ReadAfterDestroyedError extends Error {}
19 |
20 | exports.ReadAfterDestroyedError = ReadAfterDestroyedError;
21 |
22 | class ReadStream extends _fs.default.ReadStream {
   ^
TypeError: The superclass is not a constructor.

I believe your issue may have been fixed in https://github.com/oven-sh/bun/pull/1798

And @asilvas , I believe this issue is also resolved in https://github.com/oven-sh/bun/commit/5598ea69f23683c593715764ed1a7d75c1092e8e . Let me know if it's still not working for you

ThatOneBro avatar Feb 15 '23 07:02 ThatOneBro

Progress!

20 | Object.defineProperty(exports, "__esModule", { value: true });
21 | exports.ProductNotSupportedError = exports.RequestAbortedError = exports.ResponseError = exports.ConfigurationError = exports.DeserializationError = exports.SerializationError = exports.NoLivingConnectionsError = exports.ConnectionError = exports.TimeoutError = exports.ElasticsearchClientError = void 0;
22 | class ElasticsearchClientError extends Error {
23 |     constructor(message) {
24 |         super(message);
25 |         this.name = 'ElasticsearchClientError';
                ^
ConnectionError: this.pool.request is not a function. (In 'this.pool.request(requestParams)', 'this.pool.request' is undefined)
     at /root-path/node_modules/@elastic/transport/lib/errors.js:25:13

asilvas avatar Feb 15 '23 13:02 asilvas

Ah darn I thought that was enough. Well we also need to polyfill undici.request (tracking in #1987), which Pool.request could probably just wrap. We'll see if that gets it working

ThatOneBro avatar Feb 15 '23 18:02 ThatOneBro

After finishing up on apollographql + elasticsearch, any plans with regards to neo4j ?

chrisAXZA avatar Feb 16 '23 15:02 chrisAXZA

Latest progress from canary:

[1.34ms] "node_modules.bun" - 14 modules, 7 packages
20 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
21 | Object.defineProperty(exports, "__esModule", { value: true });
22 | exports.lowerCaseHeaders = exports.generateRequestId = void 0;
23 | const tslib_1 = require("tslib");
24 | const debug_1 = tslib_1.__importDefault(require("debug"));
25 | const os_1 = tslib_1.__importDefault(require("os"));
                        ^
TypeError: Cannot export a duplicate name 'machine'. while parsing module "node:os"
      at /x/node_modules/@elastic/transport/lib/Transport.js:25:21
      at node_modules.server.bun:7:13090
      at /x/node_modules/@elastic/transport/index.js:23:6
      at node_modules.server.bun:7:13090
      at /x/node_modules/@elastic/elasticsearch/index.js:34:2
      at node_modules.server.bun:7:13090
      at /x/estest.js:1:0

asilvas avatar Feb 20 '23 02:02 asilvas

Latest progress from canary:

[1.34ms] "node_modules.bun" - 14 modules, 7 packages
20 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
21 | Object.defineProperty(exports, "__esModule", { value: true });
22 | exports.lowerCaseHeaders = exports.generateRequestId = void 0;
23 | const tslib_1 = require("tslib");
24 | const debug_1 = tslib_1.__importDefault(require("debug"));
25 | const os_1 = tslib_1.__importDefault(require("os"));
                        ^
TypeError: Cannot export a duplicate name 'machine'. while parsing module "node:os"
      at /x/node_modules/@elastic/transport/lib/Transport.js:25:21
      at node_modules.server.bun:7:13090
      at /x/node_modules/@elastic/transport/index.js:23:6
      at node_modules.server.bun:7:13090
      at /x/node_modules/@elastic/elasticsearch/index.js:34:2
      at node_modules.server.bun:7:13090
      at /x/estest.js:1:0

Once canary rebuilds, please try again

Jarred-Sumner avatar Feb 20 '23 03:02 Jarred-Sumner

Yep back to the prior state: https://github.com/oven-sh/bun/issues/1749#issuecomment-1431379606

asilvas avatar Feb 20 '23 13:02 asilvas

Ok, I created this tiny client https://github.com/samuelgja/elastic-tiny-client based on fetch. Works in, bun nodejs maybe deno? Try, comment, review :) But main purpose was to run it here in bun! Supports basic operations, there is still missing some parts, but can be updated :) Also lost of package size is typing taken from official repo.

samuelgja avatar Mar 26 '23 16:03 samuelgja

The original reproduction doesn't seem to break anymore in bun 0.8 - @asilvas can you confirm?

birkskyum avatar Aug 24 '23 21:08 birkskyum

This is also happening with Next.js 11.

class CustomDocument extends next_document__WEBPACK_IMPORTED_MODULE_1__ {
   ^
TypeError: The superclass is not a constructor.
      at ./pages/_document.js (/Users/tusharshahi/project/website/.next/server/pages/_document.js:186:0)
      at __webpack_require__ (/Users/tusharshahi/project/website/.next/server/webpack-runtime.js:33:12)
      at __webpack_exec__ (/Users/tusharshahi/project/website/.next/server/pages/_document.js:196:58)
      at /Users/tusharshahi/project/website/.next/server/pages/_document.js:197:27
      at /Users/tusharshahi/project/website/.next/server/pages/_document.js:198:17
      at globalThis (/Users/tusharshahi/project/website/.next/server/pages/_document.js:200:3)
      at require (:1:20)
      at requirePage (/Users/tusharshahi/project/website/node_modules/next/dist/server/require.js:47:11)
      at /Users/tusharshahi/project/website/node_modules/next/dist/server/load-components.js:47:24
      at loadComponents (/Users/tusharshahi/project/website/node_modules/next/dist/server/load-components.js:29:30)
      at /Users/tusharshahi/project/website/node_modules/next/dist/server/next-server.js:959:35
      at findPageComponents (/Users/tusharshahi/project/website/node_modules/next/dist/server/next-server.js:943:29)
      at /Users/tusharshahi/project/website/node_modules/next/dist/server/dev/next-dev-server.js:546:19
      at processTicksAndRejections (:1:2602)

I am on the latest version.

TusharShahi avatar Sep 12 '23 15:09 TusharShahi

@birkskyum But what is that ConnectionError: Not implemented in bun

node_modules/@elastic/transport/lib/Transport.js:529:30

  at asyncFunctionResume (native)
    at promiseReactionJobWithoutPromiseUnwrapAsyncContext (native)
    at promiseReactionJob (native)
    at processTicksAndRejections (native)

  at asyncFunctionResume (native)
    at promiseReactionJobWithoutPromiseUnwrapAsyncContext (native)
    at promiseReactionJob (native)
    at processTicksAndRejections (native)
at new ConnectionError (/node_modules/@elastic/transport/lib/errors.js:54:8)
      at /node_modules/@elastic/transport/lib/Transport.js:528:30
      at processTicksAndRejections (:61:76)

  at asyncFunctionResume (native)
    at promiseReactionJobWithoutPromiseUnwrapAsyncContext (native)
    at promiseReactionJob (native)
    at processTicksAndRejections (native)

imcm7 avatar Dec 09 '23 13:12 imcm7

Just posting this here incase anyone is trying to get Elastic working with Bun and having unable to verify the first certificate. After lots of digging I found how to make it work with custom CA, since bun does not yet have NODE_EXTRA_CA_CERTS.

import { Client, HttpConnection, type ConnectionOptions } from '@elastic/elasticsearch';
import { type RequestOptions } from 'https';

// Fixes CA issue with Bun
class ExtHttpConnection extends HttpConnection {
  constructor(opts: ConnectionOptions) {
    super(opts);

    const oldMakeRequest = this.makeRequest;
    this.makeRequest = (requestOpts: string | RequestOptions | URL) => {
      if (typeof requestOpts === 'object') requestOpts.ca = opts.tls?.ca;

      return oldMakeRequest(requestOpts);
    };
  }
}

const client = new Client({
  node: 'https://localhost:9200/',
  Connection: ExtHttpConnection,
  tls: {
    ca: await Bun.file('ca.crt').text()
  }
});

I'm not exactly sure why on node you can specify tls.ca in the Elastic Client constructor but not Bun, but this fixes it.

UltimateGG avatar Jul 03 '24 19:07 UltimateGG