Skip to content

Conversation

@arcanis
Copy link
Member

@arcanis arcanis commented Sep 16, 2023

What's the problem this PR addresses?

Node.js supports snapshotting as an experimental feature. It's supposed to make startup faster. The drawback is that it doesn't work for various builtin modules, which need to be lazy loaded; in our case, this is:

  • child_process
  • http
  • https
  • module
  • readline
  • tty
  • vm

How did you fix it?

Moved the aforementioned modules into lazy evaluation. The tty module is used at startup (by both chalk & clipanion), so I had to mock it.

My plan is to implement an experimental command in Corepack to build the snapshot for a given package manager, but that requires Yarn to first support it.

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.

@arcanis
Copy link
Member Author

arcanis commented Sep 16, 2023

It seems that chalk isn't compatible with snapshotting, since it detects whether the terminal supports color while building the snapshot (and there's no easy way to fix that). I may look at replacing it with something else, possibly term-strings (it's a library of mines; it doesn't support snapshotting either right now, but at least I can contribute to it to fix that 😄).

Copy link
Member

@merceyz merceyz left a comment

Choose a reason for hiding this comment

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

It's supposed to make startup faster

If it doesn't then we shouldn't introduce extra complexity to support it.

Comment on lines +335 to +367
/* @internal */
export function createAlgoliaClient(configuration: Configuration) {
const requester: AlgoliaTypes.Requester = {
async send(req: AlgoliaTypes.Request): Promise<AlgoliaTypes.Response> {
try {
const res = await request(req.url, req.data || null, {
configuration,
headers: req.headers,
});

return {
content: res.body,
isTimedOut: false,
status: res.statusCode,
};
} catch (error) {
return {
content: error.response.body,
isTimedOut: false,
status: error.response.statusCode,
};
}
},
};

// Note that the appId and appKey are specific to Yarn's plugin-typescript - please
// don't use them anywhere else without asking Algolia's permission
const ALGOLIA_API_KEY = `e8e1bd300d860104bb8c58453ffa1eb4`;
const ALGOLIA_APP_ID = `OFCNCOG2CU`;

const algoliasearch = require(`algoliasearch`) as typeof import('algoliasearch').default;
return algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY, {requester});
}
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem like something that belongs in the core.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's reused across a couple of plugins (two). The function is marked internal, so it's not part of the types / public API.

@arcanis
Copy link
Member Author

arcanis commented Sep 16, 2023

If it doesn't then we shouldn't introduce extra complexity to support it.

It makes the startup faster. It's not possible to enable it by default yet since it's still experimental in Node, but the Node folks need practical feedback to know when it's stable enough, and integrating it with Corepack (and Yarn) is a good way for them to get just that - while also giving us more options to make the startup faster in the future, outside of what we could do in userland.

The extra complexity needs to be balanced, but it doesn't seem that bad to me overall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants