Unverified Commit 37b859f3 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Small code improvements (#37227)

Two small things noticed today. When calling `getDependencies` concurrently (which is happening right now) the cache doesn't work as it's after the `await`, so here we move it to cache the promise instead. Also `NUM_OF_MICROSEC_IN_SEC` isn't accurate and it should be `NANOSEC` actually.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 8aa15a21
...@@ -8,9 +8,9 @@ type PackageJsonDependencies = { ...@@ -8,9 +8,9 @@ type PackageJsonDependencies = {
devDependencies: Record<string, string> devDependencies: Record<string, string>
} }
let cachedDeps: PackageJsonDependencies let cachedDeps: Promise<PackageJsonDependencies>
async function getDependencies({ function getDependencies({
cwd, cwd,
}: { }: {
cwd: string cwd: string
...@@ -19,18 +19,20 @@ async function getDependencies({ ...@@ -19,18 +19,20 @@ async function getDependencies({
return cachedDeps return cachedDeps
} }
const configurationPath: string | undefined = await findUp('package.json', { return (cachedDeps = (async () => {
cwd, const configurationPath: string | undefined = await findUp('package.json', {
}) cwd,
if (!configurationPath) { })
return (cachedDeps = { dependencies: {}, devDependencies: {} }) if (!configurationPath) {
} return { dependencies: {}, devDependencies: {} }
}
const content = await fs.readFile(configurationPath, 'utf-8') const content = await fs.readFile(configurationPath, 'utf-8')
const packageJson: any = JSON5.parse(content) const packageJson: any = JSON5.parse(content)
const { dependencies = {}, devDependencies = {} } = packageJson || {} const { dependencies = {}, devDependencies = {} } = packageJson || {}
return (cachedDeps = { dependencies, devDependencies }) return { dependencies, devDependencies }
})())
} }
export async function getPackageVersion({ export async function getPackageVersion({
......
import { SpanId } from './shared' import { SpanId } from './shared'
import { reporter } from './report' import { reporter } from './report'
const NUM_OF_MICROSEC_IN_SEC = BigInt('1000') const NUM_OF_MICROSEC_IN_NANOSEC = BigInt('1000')
let count = 0 let count = 0
const getId = () => { const getId = () => {
count++ count++
...@@ -58,12 +58,12 @@ export class Span { ...@@ -58,12 +58,12 @@ export class Span {
// a float64 in both JSON and JavaScript. // a float64 in both JSON and JavaScript.
stop(stopTime?: bigint) { stop(stopTime?: bigint) {
const end: bigint = stopTime || process.hrtime.bigint() const end: bigint = stopTime || process.hrtime.bigint()
const duration = (end - this._start) / NUM_OF_MICROSEC_IN_SEC const duration = (end - this._start) / NUM_OF_MICROSEC_IN_NANOSEC
this.status = SpanStatus.Stopped this.status = SpanStatus.Stopped
if (duration > Number.MAX_SAFE_INTEGER) { if (duration > Number.MAX_SAFE_INTEGER) {
throw new Error(`Duration is too long to express as float64: ${duration}`) throw new Error(`Duration is too long to express as float64: ${duration}`)
} }
const timestamp = this._start / NUM_OF_MICROSEC_IN_SEC const timestamp = this._start / NUM_OF_MICROSEC_IN_NANOSEC
reporter.report( reporter.report(
this.name, this.name,
Number(duration), Number(duration),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment