socket.io: Cannot find name 'TransformStream' after updating to v5.2.0

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.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 24 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@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: https://github.com/socketio/engine.io-parser/commit/0305b4a7a597e0f070ce8ea17106121f9ab369bc

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

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!

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

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 https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66710

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

@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!

@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"
    }
}