nats-io / nats.js

Apache License 2.0
21 stars 3 forks source link

Type definitions/references issue between jetstream and core #55

Closed JustinBaySpoton closed 1 week ago

JustinBaySpoton commented 1 week ago

Observed behavior

With some code like this in my project:

iterator = await jetstreamConsumer.fetch({ max_messages: 3 });
for await (const message of iterator) {
    console.log(`📬 [NATS] Received message on ${subject}:`, json.decode(message.data));
}

In nats.ws this worked fine. With nats.js, after adjusting imports to migrate, TypeScript is giving an error on the of iterator bit, claiming it doesn't implement the iterator protocol:

image

This is seemingly because of a bad path in published types. This line is in the jetstream type defs, node_modules/@nats-io/jetstream/lib/types.d.ts:

import type { Codec, Msg, MsgHdrs, NatsConnectionImpl, NatsError, Payload, QueuedIterator, RequestOptions, ReviverFn, Sub, TypedSubscriptionOptions } from "@nats-io/nats-core/internal";

If I change this to the path I see on disk, everything works fine:

- import type {  } from "@nats-io/nats-core/internal";
+ import type {  } from "@nats-io/nats-core/lib/internal_mod";

The runtime JS works just fine. The issue is limited to types.

Expected behavior

No errors on of iterator above

Server and client version

    "@nats-io/jetstream": "^3.0.0-4",
    "@nats-io/nats-core": "^3.0.0-25",

Host environment

No response

Steps to reproduce

No response

aricart commented 1 week ago

Is this from npm or jsr?

JustinBaySpoton commented 1 week ago

npm

aricart commented 1 week ago

I am not able to reproduce that package.json

{
  "type": "module",
  "dependencies": {
    "@nats-io/jetstream": "^3.0.0-9",
    "@nats-io/nats-core": "^3.0.0-25",
    "typescript": "^5.5.4"
  }
}

tsconfig

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "outDir": "./lib/",
    "moduleResolution": "nodenext",
    "sourceMap": true,
    "declaration": true,
    "allowJs": true,
    "removeComments": false
  },
  "include": [
    "./main.ts"
  ]
}
import { wsconnect } from "@nats-io/nats-core";
import {AckPolicy, jetstream, jetstreamManager} from "@nats-io/jetstream"

console.log("before connect");
const nc = await wsconnect({servers: ["wss://demo.nats.io:8443"]});
console.log("connected")

const jsm = await jetstreamManager(nc);

const n = "teststream-sdfkdfkdfdkfdf"

const si = await jsm.streams.add({name: n, subjects: [n]});
console.log("created stream");
const ci = await jsm.consumers.add(n, {durable_name: n, ack_policy: AckPolicy.Explicit});
console.log("created consumer");

const js = jsm.jetstream();
const cc = await js.consumers.get(n,n);

const iter = await cc.fetch({max_messages: 3, expires: 2_000});
(async () => {
    const status = await iter.status();
    for await(const s of status) {
        console.log(s);
    }
})();
for await(const m of iter) {
    console.log(m.subject);
}

await jsm.streams.delete(n);

await nc.close();
aricart@mac-studio /p/t/ni> tsc
aricart@mac-studio /p/t/ni> node lib/main.js
before connect
connected
created stream
created consumer
{
  type: 'next',
  data: {
    batch: 3,
    max_bytes: 0,
    idle_heartbeat: 1000000000,
    expires: 2000000000
  }
}
{ type: 'discard', data: { msgsLeft: 3, bytesLeft: 0 } }
aricart@mac-studio /p/t/ni> 
aricart commented 1 week ago

possible that an older bundle is doing what you see - I have newer ones - perhaps npm install @nats-io/nats-core@next

aricart commented 1 week ago

Please reopen if you don't get it to work with the bundles I have.

JustinBaySpoton commented 1 week ago

Thanks -- looks like this was at least a tsconfig issue on our end. ModuleResolution needs to be nodenext and not node. Updating packages did not resolve it, but that did.