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

Remove the through2 dependency in favor of the built-in Node.js stream.Transform #18113

Merged
merged 1 commit into from
May 21, 2024

Conversation

timvandermeij
Copy link
Contributor

@timvandermeij timvandermeij commented May 17, 2024

The through2 dependency got introduced over four years ago in #11325 to replace the unmaintained gulp-transform dependency. However, sadly the same holds for through2 since the last release was also four years ago.

Fortunately the through2 dependency can trivially be replaced with the built-in Node.js stream.Transform API nowadays. In fact, the through2 dependency mentions themselves in their README already that they are "a tiny wrapper around Node.js streams.Transform". The stream.Transform API is available in all Node.js versions we support, and in Node.js 6 already the simplified constructor approach for stream.Transform got introduced to simplify creating custom stream transformers; see https://nodejs.org/docs/latest-v6.x/api/stream.html#stream_new_stream_transform_options.

This commit therefore replaces through2 by switching to the stream.Transform API directly so we don't need any wrappers anymore. Note that for our case the only change we have to make is to enable object mode, see https://nodejs.org/api/stream.html#object-mode, because we pass in VinylFile objects instead of e.g. regular Buffer objects.

I have confirmed in two ways that this is indeed a drop-in replacement:

  • Running the Gulp targets that call the transform function and diffing the resulting build folder before/after this patch, with diff -r build-old/ build-new/, to ensure that there are no unexpected changes in the output.
  • Changing the Gulpfile to, instead of UTF-8, transform the files to ASCII, and diffing the resulting build folder to confirm that the transformation logic works and produces different results, such as:
diff build/lib/core/standard_fonts.js build-ascii/lib/core/standard_fonts.js
284c284
<   t["Trinité"] = true;
---
>   t["Trinit�"] = true;

For reviewing I recommend to use the ?w=1 flag since it makes the diff even shorter.

@timvandermeij
Copy link
Contributor Author

/botio-linux preview

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @timvandermeij received. Current queue size: 0

Live output at: http://54.241.84.105:8877/d8444b3a78206c0/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/d8444b3a78206c0/output.txt

Total script time: 1.17 mins

Published

…ream.Transform`

The `through2` dependency got introduced over four years ago in mozilla#11325 to
replace the unmaintained `gulp-transform` dependency. However, sadly the
same holds for `through2` since the last release was also four years ago.

Fortunately the `through2` dependency can trivially be replaced with the
built-in Node.js `stream.Transform` API nowadays. In fact, the `through2`
dependency mentions themselves in their README already that they are "a
tiny wrapper around Node.js streams.Transform". The `stream.Transform`
API is available in all Node.js versions we support, and in Node.js 6
already the simplified constructor approach for `stream.Transform` got
introduced to simplify creating custom stream transformers; see
https://nodejs.org/docs/latest-v6.x/api/stream.html#stream_new_stream_transform_options.

This commit therefore replaces `through2` by switching to the
`stream.Transform` API directly so we don't need any wrappers anymore.
Note that for our case the only change we have to make is to enable
object mode, see https://nodejs.org/api/stream.html#object-mode, because
we pass in `VinylFile` objects instead of e.g. regular `Buffer` objects.

I have confirmed in two ways that this is indeed a drop-in replacement:

- Running the Gulp targets that call the `transform` function and
  diffing the resulting `build` folder before/after this patch, with
  `diff -r build-old/ build-new/`, to ensure that there are no
  unexpected changes in the output.
- Changing the Gulpfile to, instead of UTF-8, transform the files to
  ASCII, and diffing the resulting `build` folder to confirm that the
  transformation logic works and produces different results, such as:

```
diff build/lib/core/standard_fonts.js build-ascii/lib/core/standard_fonts.js
284c284
<   t["Trinité"] = true;
---
>   t["Trinit�"] = true;
```
@timvandermeij timvandermeij changed the title Remove the though2 dependency in favor of the built-in Node.js stream.Transform Remove the through2 dependency in favor of the built-in Node.js stream.Transform May 17, 2024
@timvandermeij
Copy link
Contributor Author

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @timvandermeij received. Current queue size: 0

Live output at: http://54.241.84.105:8877/9e7299f59c6d0d1/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @timvandermeij received. Current queue size: 0

Live output at: http://54.193.163.58:8877/e9a1d674a86bf6c/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/9e7299f59c6d0d1/output.txt

Total script time: 7.36 mins

  • Integration Tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/e9a1d674a86bf6c/output.txt

Total script time: 21.94 mins

  • Integration Tests: FAILED

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, fewer dependencies are always nice; thank you!

@timvandermeij timvandermeij merged commit 8753bd9 into mozilla:master May 21, 2024
9 checks passed
@timvandermeij timvandermeij deleted the through2 branch May 21, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants