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

perf: use node: prefix to bypass require.cache call for builtins #5026

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/scripts/lint-ecosystem.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const path = require('path')
const fs = require('fs')
const readline = require('readline')
const path = require('node:path')
const fs = require('node:fs')
const readline = require('node:readline')

const basePathEcosystemDocFile = path.join('docs', 'Guides', 'Ecosystem.md')
const ecosystemDocFile = path.join(__dirname, '..', '..', basePathEcosystemDocFile)
Expand Down
4 changes: 2 additions & 2 deletions build/build-error-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
'use strict'

const FJS = require('fast-json-stringify')
const path = require('path')
const fs = require('fs')
const path = require('node:path')
const fs = require('node:fs')

const code = FJS({
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions build/build-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const AjvStandaloneCompiler = require('@fastify/ajv-compiler/standalone')
const { _ } = require('ajv')
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')

const factory = AjvStandaloneCompiler({
readMode: false,
Expand Down
4 changes: 2 additions & 2 deletions build/sync-version.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')

// package.json:version -> fastify.js:VERSION
const { version } = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')).toString('utf8'))
Expand Down
2 changes: 1 addition & 1 deletion docs/Guides/Database.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ CREATE TABLE IF NOT EXISTS users (
```javascript
const pg = require('pg')
const Postgrator = require('postgrator')
const path = require('path')
const path = require('node:path')

async function migrate() {
const client = new pg.Client({
Expand Down
4 changes: 2 additions & 2 deletions docs/Guides/Delay-Accepting-Requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ follows:

```js
const { fetch } = require('undici')
const { setTimeout } = require('timers/promises')
const { setTimeout } = require('node:timers/promises')

const MAGIC_KEY = '12345'

Expand Down Expand Up @@ -249,7 +249,7 @@ server.listen({ port: '1234' })

```js
const { fetch } = require('undici')
const { setTimeout } = require('timers/promises')
const { setTimeout } = require('node:timers/promises')

const MAGIC_KEY = '12345'

Expand Down
8 changes: 4 additions & 4 deletions docs/Reference/HTTP2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ HTTP2 is supported in all modern browsers __only over a secure connection__:
```js
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const fastify = require('fastify')({
http2: true,
https: {
Expand All @@ -42,8 +42,8 @@ box:
```js
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const fastify = require('fastify')({
http2: true,
https: {
Expand Down
2 changes: 1 addition & 1 deletion docs/Reference/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ tools first" fashion.

```js
const tracer = /* retrieved from elsewhere in the package */
const dc = require('diagnostics_channel')
const dc = require('node:diagnostics_channel')
const channel = dc.channel('fastify.initialization')
const spans = new WeakMap()

Expand Down
2 changes: 1 addition & 1 deletion docs/Reference/Middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ the first parameter to `use` and you are done!
`/user/:id/comments`) and wildcards are not supported in multiple paths.*

```js
const path = require('path')
const path = require('node:path')
const serveStatic = require('serve-static')

// Single path
Expand Down
14 changes: 7 additions & 7 deletions docs/Reference/Reply.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ reply.trailer('server-timing', function() {
return 'db;dur=53, app;dur=47.2'
})

const { createHash } = require('crypto')
const { createHash } = require('node:crypto')
// trailer function also receive two argument
// @param {object} reply fastify reply
// @param {string|Buffer|null} payload payload that already sent, note that it will be null when stream is sent
Expand Down Expand Up @@ -668,7 +668,7 @@ fastify.get('/json', options, function (request, reply) {
`'application/octet-stream'`.
```js
fastify.get('/streams', function (request, reply) {
const fs = require('fs')
const fs = require('node:fs')
const stream = fs.createReadStream('some-file', 'utf8')
reply.header('Content-Type', 'application/octet-stream')
reply.send(stream)
Expand All @@ -677,7 +677,7 @@ fastify.get('/streams', function (request, reply) {
When using async-await you will need to return or await the reply object:
```js
fastify.get('/streams', async function (request, reply) {
const fs = require('fs')
const fs = require('node:fs')
const stream = fs.createReadStream('some-file', 'utf8')
reply.header('Content-Type', 'application/octet-stream')
return reply.send(stream)
Expand All @@ -690,7 +690,7 @@ fastify.get('/streams', async function (request, reply) {
If you are sending a buffer and you have not set a `'Content-Type'` header,
*send* will set it to `'application/octet-stream'`.
```js
const fs = require('fs')
const fs = require('node:fs')
fastify.get('/streams', function (request, reply) {
fs.readFile('some-file', (err, fileBuffer) => {
reply.send(err || fileBuffer)
Expand All @@ -700,7 +700,7 @@ fastify.get('/streams', function (request, reply) {

When using async-await you will need to return or await the reply object:
```js
const fs = require('fs')
const fs = require('node:fs')
fastify.get('/streams', async function (request, reply) {
fs.readFile('some-file', (err, fileBuffer) => {
reply.send(err || fileBuffer)
Expand All @@ -715,7 +715,7 @@ fastify.get('/streams', async function (request, reply) {
`send` manages TypedArray and sets the `'Content-Type'=application/octet-stream'`
header if not already set.
```js
const fs = require('fs')
const fs = require('node:fs')
fastify.get('/streams', function (request, reply) {
const typedArray = new Uint16Array(10)
reply.send(typedArray)
Expand Down Expand Up @@ -842,7 +842,7 @@ Fastify natively handles promises and supports async-await.

*Note that in the following examples we are not using reply.send.*
```js
const { promisify } = require('util')
const { promisify } = require('node:util')
const delay = promisify(setTimeout)

fastify.get('/promises', options, function (request, reply) {
Expand Down
4 changes: 2 additions & 2 deletions docs/Reference/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ You can also use Fastify's default parser but change some handling behavior,
like the example below for case insensitive keys and values:

```js
const querystring = require('querystring')
const querystring = require('node:querystring')
const fastify = require('fastify')({
querystringParser: str => querystring.parse(str.toLowerCase())
})
Expand Down Expand Up @@ -1892,7 +1892,7 @@ The properties that can currently be exposed are:
- http2SessionTimeout

```js
const { readFileSync } = require('fs')
const { readFileSync } = require('node:fs')
const Fastify = require('fastify')

const fastify = Fastify({
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fastify = require('../../fastify')({
})

const jsonParser = require('fast-json-body')
const querystring = require('querystring')
const querystring = require('node:querystring')

// Handled by fastify
// curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
Expand Down
4 changes: 2 additions & 2 deletions examples/http2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const fastify = require('../fastify')({
http2: true,
https: {
Expand Down
4 changes: 2 additions & 2 deletions examples/https.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const fastify = require('../fastify')({
https: {
key: fs.readFileSync(path.join(__dirname, '../test/https/fastify.key')),
Expand Down
2 changes: 1 addition & 1 deletion examples/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fastify = require('../fastify')({ logger: true })
const jsonParser = require('fast-json-body')
const querystring = require('querystring')
const querystring = require('node:querystring')

// Handled by fastify
// curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fastify = require('../fastify')({
logger: false
})

const Readable = require('stream').Readable
const Readable = require('node:stream').Readable

fastify
.get('/', function (req, reply) {
Expand Down
4 changes: 2 additions & 2 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const VERSION = '4.22.2'

const Avvio = require('avvio')
const http = require('http')
const http = require('node:http')
let lightMyRequest

const {
Expand Down Expand Up @@ -496,7 +496,7 @@ function fastify (options) {
server.on('clientError', options.clientErrorHandler.bind(fastify))

try {
const dc = require('diagnostics_channel')
const dc = require('node:diagnostics_channel')
const initChannel = dc.channel('fastify.initialization')
if (initChannel.hasSubscribers) {
initChannel.publish({ fastify })
Expand Down
2 changes: 1 addition & 1 deletion lib/contentTypeParser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { AsyncResource } = require('async_hooks')
const { AsyncResource } = require('node:async_hooks')
const { Fifo } = require('toad-cache')
const { safeParse: safeParseContentType, defaultContentType } = require('fast-content-type-parse')
const secureJson = require('secure-json-parse')
Expand Down
2 changes: 1 addition & 1 deletion lib/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const statusCodes = require('http').STATUS_CODES
const statusCodes = require('node:http').STATUS_CODES
const wrapThenable = require('./wrapThenable')
const {
kReplyHeaders,
Expand Down
2 changes: 1 addition & 1 deletion lib/pluginUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const semver = require('semver')
const assert = require('assert')
const assert = require('node:assert')
const kRegisteredPlugins = Symbol.for('registered-plugin')
const {
kTestInternals
Expand Down
2 changes: 1 addition & 1 deletion lib/reply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const eos = require('stream').finished
const eos = require('node:stream').finished

const {
kFourOhFourContext,
Expand Down
8 changes: 4 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const http = require('http')
const https = require('https')
const dns = require('dns')
const http = require('node:http')
const https = require('node:https')
const dns = require('node:dns')

const warnings = require('./warnings')
const { kState, kOptions, kServerBindings } = require('./symbols')
Expand Down Expand Up @@ -426,7 +426,7 @@ function logServerAddress (server, listenTextResolver) {

function http2 () {
try {
return require('http2')
return require('node:http2')
} catch (err) {
throw new FST_ERR_HTTP2_INVALID_VERSION()
}
Expand Down
2 changes: 1 addition & 1 deletion test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ test('setNotFoundHandler should not suppress duplicated routes checking', t => {
test('log debug for 404', t => {
t.plan(1)

const Writable = require('stream').Writable
const Writable = require('node:stream').Writable

const logStream = new Writable()
logStream.logs = []
Expand Down
2 changes: 1 addition & 1 deletion test/als.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { AsyncLocalStorage } = require('async_hooks')
const { AsyncLocalStorage } = require('node:async_hooks')
const t = require('tap')
const Fastify = require('..')
const sget = require('simple-get').concat
Expand Down
2 changes: 1 addition & 1 deletion test/async-await.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const sget = require('simple-get').concat
const Fastify = require('..')
const split = require('split2')
const pino = require('pino')
const statusCodes = require('http').STATUS_CODES
const statusCodes = require('node:http').STATUS_CODES
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

const opts = {
Expand Down
2 changes: 1 addition & 1 deletion test/bodyLimit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Fastify = require('..')
const sget = require('simple-get').concat
const zlib = require('zlib')
const zlib = require('node:zlib')
const t = require('tap')
const test = t.test

Expand Down
4 changes: 2 additions & 2 deletions test/build/error-serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const t = require('tap')
const test = t.test
const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')

const { code } = require('../../build/build-error-serializer')

Expand Down
4 changes: 2 additions & 2 deletions test/build/version.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fs = require('fs')
const path = require('path')
const fs = require('node:fs')
const path = require('node:path')
const t = require('tap')
const test = t.test
const fastify = require('../../fastify')()
Expand Down
2 changes: 1 addition & 1 deletion test/bundler/webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')

module.exports = {
entry: { success: './src/index.js', failPlugin: './src/fail-plugin-version.js' },
Expand Down
2 changes: 1 addition & 1 deletion test/client-timeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require('tap')
const fastify = require('..')({ requestTimeout: 5, http: { connectionsCheckingInterval: 1000 } })
const { connect } = require('net')
const { connect } = require('node:net')

test('requestTimeout should return 408', t => {
t.plan(1)
Expand Down
4 changes: 2 additions & 2 deletions test/close.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const net = require('net')
const http = require('http')
const net = require('node:net')
const http = require('node:http')
const { test } = require('tap')
const Fastify = require('..')
const { Client } = require('undici')
Expand Down