indix / kafkajs-lz4

🗜 LZ4 compression codec for KafkaJS
MIT License
15 stars 8 forks source link

Unable to import #8

Open link2malkeet opened 3 years ago

link2malkeet commented 3 years ago

Seems you have mixed the default import vs module.export. export default class LZ4Codec vs module.exports = LZ4Codec; So unable to use the library. Can we please fixed it?

paambaati commented 3 years ago

@link2malkeet Please provide a minimal, complete and verifiable example for your issue, as they're both valid exports, they can work side-by-side in CommonJS and ES6 environments, and the latest beta version has been verified to work.

To verify yourself —

  1. Clone this repo.
  2. Add this package itself as a dependency.
    yarn add kafkajs-lz4@2.0.0-beta.0
  3. Modify this line to import the package you just added.
    - import LZ4Codec from '../src/index';
    + import LZ4Codec from 'kafkajs-lz4';
  4. Run tests (you need Docker for this).
    yarn run test
link2malkeet commented 3 years ago

HI @paambaati I will try again later

Just to summarize what I know or understand, the main index file works with default exports only, we don't need to define module.exports in typescript.

Thanks

AlanKavanagh commented 3 years ago

Hi @paambaati Just following up on this, is the beta version going to be promoted?

paambaati commented 3 years ago

@AlanKavanagh Can you confirm the beta version works for you? It'd be great if you can tell us a little about your Kafka cluster and how long you've been using the beta version.

VladC12 commented 2 years ago

I have the same problem, I'm getting TypeError: LZ4Codec is not a constructor. This was on the beta with: npm install kafkajs-lz4@2.0.0-beta.0 The full error:

 TypeError: LZ4Codec is not a constructor
     at Object.<anonymous> (/xxx/public/server/controller/kafka.js:13:21)
     at Module._compile (node:internal/modules/cjs/loader:1116:14)
     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1169:10)
     at Module.load (node:internal/modules/cjs/loader:988:32)
     at Module._load (node:internal/modules/cjs/loader:829:12)
     at Function.c._load (node:electron/js2c/asar_bundle:5:13343)
     at Module.require (node:internal/modules/cjs/loader:1012:19)
     at require (node:internal/modules/cjs/helpers:102:18)
     at Object.<anonymous> (/xxx/public/server/controller/consumer.js:1:15)
     at Module._compile (node:internal/modules/cjs/loader:1116:14)
 npm run electron:start exited with code 7

I'm doing an electron app.

On the server side I have kafka.js file which I use, with the code:

const { Kafka, CompressionTypes, CompressionCodecs } = require("kafkajs");
const { LZ4Codec } = require('kafkajs-lz4');

const username = "nope";
const password = "nope";
const sasl =
  username && password ? { username, password, mechanism: "plain" } : null;
const ssl = !!sasl;

// LZ4 compression
const LZ4Instance = new LZ4Codec()
CompressionCodecs[CompressionTypes.LZ4] = LZ4Instance.codec;

const kafka = new Kafka({
  clientId: "noper",
  brokers: ["nope"], 
  ssl,
  sasl,
});

module.exports = kafka;

Then consumer.js:

const kafka = require("./kafka");
const io = require("../socket");
const consumer = async (topic) => {
  const consumer = kafka.consumer({ groupId: "test-group" });

  try {
    await consumer.connect();
  } catch (e) {
    console.log("Error: couldn't connect to Kafka");
    return;
  }
  console.log("Connected to Kafka!");
  try {
    await consumer.subscribe({ topic, topic });
  } catch (e) {
    console.log("Error: couldn't subscribe to topic.");
    return;
  }
  console.log("Kafka consumer subscribed.");
  console.log("Consuming...");

  try {
    await consumer.run({
      eachMessage: async ({ topic, partition, message }) => {
        console.log("Received message: " + message.value.toString());
        io.getIO().emit("events", {
          action: "consume",
          event: message.value.toString(),
        });
      },
    });
  } catch (e) {
    console.log("Couldn't consume message");
  }
};

module.exports = consumer;

I hope this helps narrow it down somehow. Ask if you need more specific info.

harshagarwalsol commented 6 months ago

I see below error as well:

TypeError: kafkajs_lz4_1.default is not a constructor

when using:

import LZ4Codec from 'kafkajs-lz4';