Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'TextEncoder' refers to a value, but is being used as a type here #96

Open
michael-rivera-florencehc-com opened this issue Oct 25, 2023 · 4 comments

Comments

@michael-rivera-florencehc-com
Copy link

michael-rivera-florencehc-com commented Oct 25, 2023

Getting the following error when compiling in Typescript:

node_modules/@cloudamqp/amqp-client/types/amqp-base-client.d.ts:27:27 - error TS2749: 'TextEncoder' refers to a value, but is being used as a type here. Did you mean 'typeof TextEncoder'?

27 readonly textEncoder: TextEncoder;

This is version - 3.1.1

If I comment out
line 27 in
node_modules/@cloudamqp/amqp-client/types/amqp-base-client.d.ts

everything works.

@dentarg
Copy link
Member

dentarg commented Oct 25, 2023

What version are you using? Can you show the code you are trying to run?

@michael-rivera-florencehc-com
Copy link
Author

michael-rivera-florencehc-com commented Oct 25, 2023

Typescript - 5.2.2
NodeJS - v20.8.0
This occurs when compiling the code running the tsc command

Here is the tsconfig

{
    "compilerOptions": {
      "lib": ["es2020"],
      "module": "node16",
      "esModuleInterop": true,
      "target": "ES2020",
      "moduleResolution": "node16",
      "resolveJsonModule": true,
      "sourceMap": true,
      "checkJs": false,
      "outDir": "dist",
      "types": ["node"]
    },
    "include": ["./**/*.ts"],
    "exclude": ["node_modules", "dist"]
  }

Example Code

Client Service:

import { AMQPChannel, AMQPClient, AMQPConsumer, AMQPQueue } from '@cloudamqp/amqp-client';


export class AmqpClientService {

  static #Instance: AmqpClientService;
  #client: AMQPClient;
  #channel: AMQPChannel;
  #connection;
  #queue: AMQPQueue;
  #consumer: AMQPConsumer;  

  private constructor(client, connection,  channel, queue){
    this.#client = client;
    this.#channel = channel;
    this.#queue = queue;
    this.#connection = connection;
  }

  public static async init(uri: string) {
    const client = new AMQPClient(uri);
    const conn = await client.connect();
    const ch = await conn.channel();
    const q = await ch.queue();
    return this.#Instance = new AmqpClientService(client, conn, ch, q);
  }

  async subscribe() {
    try {
      this.#consumer = await this.#queue.subscribe({noAck: true}, async (msg) => {
        console.log(msg.bodyToString())
      })
      await this.#consumer.wait();
      
    } catch (e) {
      console.error('ERROR', e)
      e.connection.close()
      setTimeout(this.subscribe, 5000) // will try to reconnect in 1s
    }
  }

  async publish(message) {
    try {
      await this.#queue.publish(message, {deliveryMode: 2});  
    } catch(e) {
      console.error('ERROR', e)
      e.connection.close()
      setTimeout(this.publish, 5000, message)
    }
  }

  async close(){
    await this.#consumer.cancel();
    await this.#client.close();
    await this.#connection.close();
  }
}

bootstrap

import { AmqpClientService } from '../shared/services/amqp-client-service.js'
import { ConfigurationService } from '../shared/services/configuration-service.js';

const location = 'QAUS'
const configuration = new ConfigurationService(
  [
    'AMQP_SERVICE_URI',
  ],
  `.env.${location}`
);

const {
  AMQP_SERVICE_URI,
} = configuration.environmentVariables;

const amqpClientService = await AmqpClientService.init(AMQP_SERVICE_URI);

amqpClientService.subscribe();

while(true){
  amqpClientService.publish(`This is a test ${makeId(10)}`);
  await wait(10000);
}

async function wait(time) {
  return  await new Promise(resolve => setTimeout(resolve, time));
}

function makeId(length) {
  let result = '';
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const charactersLength = characters.length;
  let counter = 0;
  while (counter < length) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    counter += 1;
  }
  return result;
}

@michael-rivera-florencehc-com
Copy link
Author

Anyone?

@guy-signalpet
Copy link

Same issue here, lets open a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants