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

Cannot find name 'TransformStream' after updating to v5.2.0 #5064

Closed
Julien-Marcou opened this issue Aug 27, 2023 · 24 comments
Closed

Cannot find name 'TransformStream' after updating to v5.2.0 #5064

Julien-Marcou opened this issue Aug 27, 2023 · 24 comments
Labels
bug Something isn't working package:engine.io-parser This concerns the "engine.io-parser" package

Comments

@Julien-Marcou
Copy link

Julien-Marcou commented Aug 27, 2023

Since I updated socket.io from the v4.7.1 to the v4.7.2 (and so engine.io-parser from the v5.1.0 to the v5.2.1), I'm getting the following errors while building my projects:

node_modules/engine.io-parser/build/cjs/index.d.ts:6:54 - error TS2304: Cannot find name 'TransformStream'.
6 export declare function createPacketEncoderStream(): TransformStream<Packet, any>;
                                                       ~~~~~~~~~~~~~~~
node_modules/engine.io-parser/build/cjs/index.d.ts:7:96 - error TS2304: Cannot find name 'TransformStream'.
7 export declare function createPacketDecoderStream(maxPayload: number, binaryType: BinaryType): TransformStream<Uint8Array, any>;
                                                                                                 ~~~~~~~~~~~~~~~

Found 2 errors in the same file, starting at: node_modules/engine.io-parser/build/cjs/index.d.ts:6

Funnily, I'm only getting this error when compiling using tsc but not when compiling using esbuild.

npx tsc -> error
npx esbuild src/index.ts --platform=node -> ok

Here is my tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist",
    "strict": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node16",
    "importHelpers": true,
    "target": "es2022",
    "module": "node16",
    "lib": [
      "es2022"
    ]
  },
  "include": [
    "src/**/*.ts"
  ]
}

Reading nodejs's documentation, when using TransformStream, it looks like they are importing it with

import { TransformStream } from 'node:stream/web';

which is not the case for engine.io-parser, so maybe that's the problem.

@Julien-Marcou Julien-Marcou changed the title Cannot find name 'TransformStream' Cannot find name 'TransformStream' after updating to v5.2.0 Aug 27, 2023
@Granjow
Copy link

Granjow commented Sep 10, 2023

I can confirm that adding import { TransformStream } from "node:stream/web"; in node_modules/engine.io-parser/build/esm/index.d.ts fixes the TS error.

@darrachequesne
Copy link
Member

It seems TransformStream is now exposed on the global object starting from version 18.0.0.

Reference: https://nodejs.org/api/webstreams.html#class-transformstream

As a workaround, could you please try to update the @types/node version?

@micalevisk
Copy link

micalevisk commented Sep 14, 2023

@darrachequesne @types/node v18.17.15 here. The latest one of v18 (I'm using nodejs 18)

@Julien-Marcou
Copy link
Author

I'm using Nodejs v18.17.1, and the @types/node package is currently set to ^18.17.11

@darrachequesne
Copy link
Member

darrachequesne commented Sep 14, 2023

Argh, it seems the types from @types/node are out of date, TransformStream is now available as a global object.

We could add import { TransformStream } from "node:stream/web";, but this would break users with a Node.js version inferior to 16.5.0 (at runtime).

Let me check.

See DefinitelyTyped/DefinitelyTyped#66710

@g-popovic
Copy link

g-popovic commented Dec 10, 2023

Any updates/workarounds found for this? Am still having this issue on node v18.17.0, socket.io: v4.7.2

@alanpurple
Copy link

why is this still not updated? even there is a simple solution(import)

@darrachequesne
Copy link
Member

@alanpurple the problem is that adding import { TransformStream } from 'node:stream/web'; would throw an exception for versions of Node.js lower than 16.5.0, which we are currently supporting.

The problem here is that the types provided by @types/node are out of date...

@alanpurple
Copy link

alanpurple commented Jan 18, 2024

@darrachequesne
isn't versions 18+ are prior to 16-?

even node 20.0 is LTS now

node 16 is now end of list September 2023

@darrachequesne
Copy link
Member

@alanpurple I'd rather not have a new major version because the types provided by @types/node are currently wrong (TransformStream should be exposed on the global object).

darrachequesne referenced this issue in socketio/engine.io-parser Feb 5, 2024
When compiling with TypeScript with module set to "node16" and
moduleResolution to "node16", the following error would be thrown:

> node_modules/engine.io-parser/build/cjs/index.d.ts:6:54 - error TS2304: Cannot find name 'TransformStream'.
> 6 export declare function createPacketEncoderStream(): TransformStream<Packet, any>;
>                                                        ~~~~~~~~~~~~~~~
> node_modules/engine.io-parser/build/cjs/index.d.ts:7:96 - error TS2304: Cannot find name 'TransformStream'.
> 7 export declare function createPacketDecoderStream(maxPayload: number, binaryType: BinaryType): TransformStream<Uint8Array, any>;
>                                                                                                  ~~~~~~~~~~~~~~~
> Found 2 errors in the same file, starting at: node_modules/engine.io-parser/build/cjs/index.d.ts:6

This is because the TransformStream object is not exposed in the global
scope in the `@types/node` package, even though it is since Node.js
`v18.0.0`.

Reference: https://nodejs.org/api/webstreams.html#class-transformstream

Note: we only import the TransformStream type (not value) because it
isn't defined on older Node.js versions.

Related:

- https://github.com/socketio/engine.io-parser/issues/136
- socketio/socket.io-client#1606
@darrachequesne
Copy link
Member

I think I have found a workaround, which does not break older Node.js versions: socketio/engine.io-parser@0305b4a

Could you please check?

Fiddle: https://github.com/socketio/socket.io-fiddle/tree/typescript-node16

@kn1ghtm0nster
Copy link

I think I have found a workaround, which does not break older Node.js versions: 0305b4a

Could you please check?

Fiddle: https://github.com/socketio/socket.io-fiddle/tree/typescript-node16

Hi there,

Fast question, what about CI/CD pipelines that use node? Our pipeline continues to fail the build process for our UI repo even with the suggested fix and we have updated the pipeline to use node v16 (previously v14).

@darrachequesne
Copy link
Member

@kn1ghtm0nster which version of @types/node are you using? Could you please provide a way to reproduce the issue, based on the fiddle?

@kn1ghtm0nster
Copy link

@kn1ghtm0nster which version of @types/node are you using? Could you please provide a way to reproduce the issue, based on the fiddle?

unfortunately, I can't provide examples for reproducing the build failure on the CI/CD config (it's kept behind a very secret vault away from developers at my company for some reason) however, the version of @types/node is 12.11.1. For now, our principal engineer has put a workaround inside the package.json file as the following to avoid using the latest version (for now). Hope this helps 😄

// package.json (angular)
{"dependencies" : {
        // ... other dependencies
        "socket.io-client" : "4.5.2" // was previously "socket.io-client" : "^4.5.2"
    }
}

@darrachequesne
Copy link
Member

@kn1ghtm0nster this does indeed help! TransformStream was added in Node.js v16.5.0, so @types/node@16.5.x I guess.

This is indeed a breaking change for older versions of @types/node... Not sure how we could have handled this properly.

That being said, is there any particular reason you are still using version @types/node@12.11.1?

@kn1ghtm0nster
Copy link

@kn1ghtm0nster this does indeed help! TransformStream was added in Node.js v16.5.0, so @types/node@16.5.x I guess.

This is indeed a breaking change for older versions of @types/node... Not sure how we could have handled this properly.

That being said, is there any particular reason you are still using version @types/node@12.11.1?

That's a great question and unfortunately one I cannot answer since I joined the team long after the UI codebase was established. Ultimately the short answer is the packages are not being updated regularly (mainly because the company limits the access to the outside npm registries). I have told the team a few times to update the packages to avoid issues such as this but the codebase is so fragile that if we were to do this we would break almost everything so we are stuck using the old packages for the time being. The workaround I provided earlier seems to hold for now but I wouldn't consider that the long-term solution if I am being honest. Ultimately it may come down to moving up to a more supported version of Node. Let me know if you have any questions! I appreciate your feedback!

@unc0ded
Copy link

unc0ded commented Feb 13, 2024

@kn1ghtm0nster this does indeed help! TransformStream was added in Node.js v16.5.0, so @types/node@16.5.x I guess.

This is indeed a breaking change for older versions of @types/node... Not sure how we could have handled this properly.

That being said, is there any particular reason you are still using version @types/node@12.11.1?

Not sure if I understand you completely, but does this mean that this version (5.2.2) won't be possible to use with Node < 16?
From what I have experimented with, if I use the latest version of @types/node available for Node v14 (which is 14.18.63), it still won't include typings for node:stream/web, since the Web Streams API itself appeared in Node v16.

If it helps, I am currently using Node 14.21.3, and I'm facing the same issue. While moving up Node versions is the plan in the future, I am trying to see if I can fix this temporarily (since priorities in my company lie elsewhere right now 😄). Thanks for your time!

@darrachequesne
Copy link
Member

but does this mean that this version (5.2.2) won't be possible to use with Node < 16?

It does work with Node.js 16 (down to Node.js 10.2.0 actually), but the TypeScript compiler will report an error if @types/node@14 is used.

As a workaround, one can either:

  • bump the version of @types/node to 16.x.y
  • downgrade to socket.io-client@4.6.x (as the TransformStream is used in order to implement the WebTransport low-level transport, added in 4.7.0)
  • skip the TypeScript check for dependencies

@unc0ded
Copy link

unc0ded commented Feb 17, 2024

but does this mean that this version (5.2.2) won't be possible to use with Node < 16?

It does work with Node.js 16 (down to Node.js 10.2.0 actually), but the TypeScript compiler will report an error if @types/node@14 is used.

As a workaround, one can either:

  • bump the version of @types/node to 16.x.y
  • downgrade to socket.io-client@4.6.x (as the TransformStream is used in order to implement the WebTransport low-level transport, added in 4.7.0)
  • skip the TypeScript check for dependencies

Thanks for the response, I was inkling towards option 1 anyway, but I wasn't sure how it would work. Tried it out, seems to work fine now!

@Sekkmer
Copy link

Sekkmer commented Mar 6, 2024

@darrachequesne

The latest change messes up typing for client libraries as the new import import type { TransformStream } from "node:stream/web" pollutes the types with @type/node

introduced here: socketio/engine.io-parser@0305b4a

in tsconfig.json we specify that we do not want any node types:

{
	"compilerOptions": {
		"lib": ["ESNext", "DOM"],
		"types": [],
	},
}

the following code fails to typecheck if engine.io-parser is imported

import {} from 'engine.io-parser';

type Equals<X, Y> =
	(<T>() => T extends X ? 1 : 2) extends
	(<T>() => T extends Y ? 1 : 2) ? true : false;

type TimeoutReturnType = ReturnType<typeof setTimeout>;
type TimeoutReturnTypeCheck = Equals<TimeoutReturnType, number>;

export const test: TimeoutReturnTypeCheck = true;

removing the first line fixes the issue, this effects downstream so importing socket.io-client has the same effect

@Mehadmushtaq
Copy link

upgrading @types/node to v20.11.30 fix for me
angular: 16.2.12
typescript: 4.9.3
socket.io-client: 4.7.5

@darrachequesne darrachequesne transferred this issue from socketio/engine.io-parser Jul 9, 2024
@darrachequesne darrachequesne added bug Something isn't working package:engine.io-parser This concerns the "engine.io-parser" package labels Jul 9, 2024
darrachequesne added a commit that referenced this issue Jul 11, 2024
The previous commit [1] tried to work around the fact that the
TransformStream object is not exposed in the global scope in the
`@types/node` package, even though it is since Node.js `v18.0.0`.

Unfortunately, it created two new issues:

- using an older `@types/node` version (before v16) would fail with:

> error TS2307: Cannot find module 'node:stream/web' or its corresponding type declarations.

Related: #5064 (comment)

- browser-only environments would somehow include the node types,
leading to conflicts like the return value of the setTimeout() method

Related:

- #5064 (comment)
- #5065

[1]: socketio/engine.io-parser@0305b4a
@darrachequesne
Copy link
Member

@Sekkmer this should be fixed by f9cb983, included in engine.io-parser@5.2.3. Could you please check?

@Sekkmer
Copy link

Sekkmer commented Jul 12, 2024

@Sekkmer this should be fixed by f9cb983, included in engine.io-parser@5.2.3. Could you please check?

@darrachequesne I tested it, and it works :)

@darrachequesne
Copy link
Member

@Sekkmer awesome, thanks of the update 👍 I think this can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package:engine.io-parser This concerns the "engine.io-parser" package
Projects
None yet
Development

No branches or pull requests

10 participants