fix: wrap mysql2 addCommand only once#7459
Conversation
Prepare calls would currently cause the method to be rewrapped frequently. This is now only done a single time. In addition this adds types to the instrumentation and adds a few more safe guards for type safety.
The database plugin has a few intermediate objects that are not necessary as well as calling methods when they do not have to be called. This is not a huge difference but it is not needed. In addition, add types and make private methods actually privat.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7459 +/- ##
=======================================
Coverage 80.34% 80.34%
=======================================
Files 731 731
Lines 31093 31100 +7
=======================================
+ Hits 24981 24988 +7
Misses 6112 6112
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Overall package sizeSelf size: 4.58 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 2.0.6 | 81.92 kB | 813.08 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
This comment has been minimized.
This comment has been minimized.
BenchmarksBenchmark execution time: 2026-02-09 13:17:26 Comparing candidate commit 85fa607 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 226 metrics, 34 unstable metrics. |
| errorCh.publish(ctx) | ||
| }) | ||
| this.once('end', () => finishCh.publish(ctx)) | ||
| const command = /** @type {{ once?: Function }} */ (this) |
There was a problem hiding this comment.
Does TS not provide a nice way to do this, say on line 164, so that you don't need to do this kinda weird re-assignment?
There was a problem hiding this comment.
Yes, the issue here is something else: we actually truly do not know about the type because they might change in any version. Due to that, we have to 'detect' each individual type and even when we detect it, we have to cast that part so that this access is accepted.
| }) | ||
| if (typeof cb === 'function') { | ||
| process.nextTick(() => { | ||
| /** @type {Function} */ (cb)(abortController.signal.reason) |
There was a problem hiding this comment.
Does line 270 not make this type comment unnecessary?
There was a problem hiding this comment.
Somehow the types detect that the cb might have changed its content due to the async operation in between.
| addHook({ name: 'mysql2', file: 'lib/pool.js', versions: ['1 - 3.11.4'] }, wrapPool) | ||
| addHook( | ||
| { name: 'mysql2', file: 'lib/base/connection.js', versions: ['>=3.11.5'] }, | ||
| /** @type {(moduleExports: unknown, version: string) => unknown} */ (wrapConnection) |
There was a problem hiding this comment.
This is pretty repetitive. Can we not make this implicit via addHooks's definition?
There was a problem hiding this comment.
This is already the definition in addHook. The reason is that the receiving method defines more concrete types that do not match these 1-to-1. Thus the need for the cast.
crysmags
left a comment
There was a problem hiding this comment.
Did a quick review, good to go!
* fix: wrap mysql2 addCommand only once Prepare calls would currently cause the method to be rewrapped frequently. This is now only done a single time. In addition this adds types to the instrumentation and adds a few more safe guards for type safety. * perf: improve database plugin performance The database plugin has a few intermediate objects that are not necessary as well as calling methods when they do not have to be called. This is not a huge difference but it is not needed. * chore: make private methods actually private * chore: add types to tracing and outbound plugins * test: add regression test for single addCommand wrapping Fixes: #7074
* fix: wrap mysql2 addCommand only once Prepare calls would currently cause the method to be rewrapped frequently. This is now only done a single time. In addition this adds types to the instrumentation and adds a few more safe guards for type safety. * perf: improve database plugin performance The database plugin has a few intermediate objects that are not necessary as well as calling methods when they do not have to be called. This is not a huge difference but it is not needed. * chore: make private methods actually private * chore: add types to tracing and outbound plugins * test: add regression test for single addCommand wrapping Fixes: #7074
fix: wrap mysql2 addCommand only once
Prepare calls would currently cause the method to be rewrapped
frequently. This is now only done a single time.
In addition this adds types to the instrumentation and adds a few
more safe guards for type safety.
perf: improve database plugin performance
The database plugin has a few intermediate objects that are not
necessary as well as calling methods when they do not have to be
called.
This is not a huge difference but it is not needed.
In addition, add types and make private methods actually privat.
chore: add types to tracing and outbound plugins
Fixes: #7074