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

refactor!: move ZipFS and ZipOpenFS into @yarnpkg/libzip #4853

Merged
merged 5 commits into from
Sep 19, 2022

Conversation

arcanis
Copy link
Member

@arcanis arcanis commented Sep 16, 2022

What's the problem this PR addresses?
ZipFS and ZipOpenFS were until now stored alongside the rest of the filesystem utilities. While it made some sense to keep all the FakeFS implementation together, it started to become a little awkward after most of the ZipOpenFS logic was moved inside a generic MountFS, leaving ZipFS / ZipOpenFS more related to an external dependency than they were to the package that contained them. It also made it difficult to build tools leveraging them (such as the mountMemoryDrive function mentioned below), because we always had to dance with the libzip parameter when constructing instances.

How did you fix it?
This diff changes that, moving ZipFS and ZipOpenFS into @yarnpkg/libzip. The benefit API-wise is that since ZipFS and ZipOpenFS are now store alongside libzip, the libzip instance no longer has to be specified when constructing ZipFS / ZipOpenFS instances (i.e. you can now run new ZipFS() instead of new ZipFS(null, {libzip})). It should work on both Node.js and browser environments, although on browsers it'll require that the ZipFS / ZipOpenFS instances are only constructed after getLibzipPromise returned. On Node.js, the libzip will be lazily loaded.

This diff also exposes a new utility called mountMemoryDrive. It's an helper whose purpose is to transparently mount memory drives on disk. While not directly related to zip itself, it uses an in-memory zip instance to store the disk mutations. It would (perhaps?) be more optimized to use a dedicated in-memory FakeFS instance, but since it would require to reimplement a lot of the logic that ZipFS already does I don't foresee doing this job anytime soon, if at all. In the meantime, ZipFS is a fine place to store this tool.

Checklist

  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

ZipFS and ZipOpenFS were until now stored alongside the rest of the filesystem utilities. While it made some sense to keep all the FakeFS implementation together, it started to become a little awkward after most of the ZipOpenFS logic was moved inside a generic MountFS, leaving ZipFS / ZipOpenFS more related to an external dependency than they were to the package that contained them. It also made it difficult to build tools leveraging them (such as the `mountMemoryDrive` function mentioned below), because we always had to dance with the `libzip` parameter when constructing instances.

This diff changes that, moving ZipFS and ZipOpenFS into `@yarnpkg/libzip`. The benefit API-wise is that since ZipFS and ZipOpenFS are now store alongside libzip, the libzip instance no longer has to be specified when constructing ZipFS / ZipOpenFS instances (i.e. you can now run `new ZipFS()` instead of `new ZipFS(null, {libzip})`). It should work on both Node.js and browser environments, although on browsers it'll require that the ZipFS / ZipOpenFS instances are only constructed after `getLibzipPromise` returned. On Node.js, the libzip will be lazily loaded.

This diff also exposes a new utility called `mountMemoryDrive`. It's an helper whose purpose is to transparently mount memory drives on disk. While not directly related to zip itself, it uses an in-memory zip instance to store the disk mutations. It would (perhaps?) be more optimized to use a dedicated in-memory FakeFS instance, but since it would require to reimplement a lot of the logic that ZipFS already does I don't foresee doing this job anytime soon, if at all. In the meantime, ZipFS is a fine place to store this tool.
@arcanis arcanis changed the title Mael/zipfs refactoring feat: Moves ZipFS and ZipOpenFS into @yarnpkg/libzip Sep 16, 2022
Copy link
Member

@RDIL RDIL left a comment

Choose a reason for hiding this comment

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

Just some general thoughts.

export function getLibzipSync() {
if (mod === null)
mod = makeInterface(createModule());
setFactory(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Can side effects be avoided here? This will remove the safety to tree-shake this module.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see a way to avoid it; however I can mark this file as having side effects, so that other files will remain tree-shaken

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually the package doesn't list sideEffects at all at the moment, so I'm not sure it's tree-shakable 🤔 I'm not too sure what should be the proper value (do we even have to list the entry point as having side effects, since it'll always be included if anything else is included?), so I won't touch it in this diff

packages/yarnpkg-libzip/sources/ZipFS.ts Show resolved Hide resolved
packages/yarnpkg-libzip/sources/async.ts Outdated Show resolved Hide resolved
@merceyz merceyz changed the title feat: Moves ZipFS and ZipOpenFS into @yarnpkg/libzip refactor!: move ZipFS and ZipOpenFS into @yarnpkg/libzip Sep 16, 2022
@merceyz merceyz added the major label Sep 16, 2022
@arcanis arcanis merged commit fc57d68 into master Sep 19, 2022
@arcanis arcanis deleted the mael/zipfs-refactoring branch September 19, 2022 07:00
@jj811208 jj811208 mentioned this pull request Sep 22, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants