Skip to content

Commit

Permalink
fix(types): fix exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 2, 2023
1 parent a49b27c commit 410bbf0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 69 deletions.
34 changes: 23 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { Sampler, SamplerOptions } from './types/sampler'
import errors = require('./types/errors')
import errors from './types/errors'
import { CPUMetric } from './types/cpuMetric'
import { EventLoopDelayMetric } from './types/eventLoopDelayMetric'
import { ResourceUsageMetric } from './types/resourceUsageMetric'
import { EventLoopUtilizationMetric } from './types/eventLoopUtilizationMetric'
import { GCEntry, GCAggregatedEntry, GCMetric } from './types/gcMetric'

declare function createSampler(options?: SamplerOptions): Sampler
export default createSampler
declare module 'doc' {

export { createSampler }
export { Sampler, SamplerOptions }
export { errors }
export * from './types/cpuMetric'
export * from './types/eventLoopDelayMetric'
export * from './types/resourceUsageMetric'
export * from './types/eventLoopUtilizationMetric'
export * from './types/gcMetric'
}
declare namespace doc {
export { errors }
export type {
Sampler, SamplerOptions,
CPUMetric,
EventLoopDelayMetric,
ResourceUsageMetric,
EventLoopUtilizationMetric,
GCEntry, GCAggregatedEntry, GCMetric
}
export const createSampler: (options?: SamplerOptions) => Sampler
export { createSampler as default }
}
declare function doc(options?: doc.SamplerOptions): doc.Sampler
export = doc
35 changes: 17 additions & 18 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { expectType, expectError, expectAssignable } from 'tsd'
import {
createSampler,
Sampler,
import doc, {
errors,
Sampler,
CPUMetric,
ResourceUsageMetric,
EventLoopDelayMetric,
EventLoopUtilizationMetric,
GCMetric
} from '.'

} from '.'
expectAssignable<errors.NotSupportedError>(new errors.NotSupportedError())
expectAssignable<'NotSupportedError'>(new errors.NotSupportedError().name)
expectAssignable<'DOC_ERR_NOT_SUPPORTED'>(new errors.NotSupportedError().code)
Expand All @@ -20,13 +19,13 @@ expectAssignable<'DOC_ERR_INVALID_ARG'>(new errors.InvalidArgumentError().code)
let sampler: Sampler

// These should work
sampler = createSampler()
sampler = createSampler({})
sampler = createSampler({ sampleInterval: 1234 })
sampler = createSampler({ eventLoopDelayOptions: { resolution: 5678 } })
sampler = createSampler({ collect: { cpu: false, gc: true } })
sampler = createSampler({ collect: { activeHandles: true } })
sampler = createSampler({ collect: { gc: true, activeHandles: true } })
sampler = doc()
sampler = doc({})
sampler = doc({ sampleInterval: 1234 })
sampler = doc({ eventLoopDelayOptions: { resolution: 5678 } })
sampler = doc({ collect: { cpu: false, gc: true } })
sampler = doc({ collect: { activeHandles: true } })
sampler = doc({ collect: { gc: true, activeHandles: true } })

expectType<() => void>(sampler.start)
expectType<() => void>(sampler.stop)
Expand All @@ -42,9 +41,9 @@ sampler.on('sample', () => {
})

// These should not
expectError(() => { createSampler(1) })
expectError(() => { createSampler('string') })
expectError(() => { createSampler(null) })
expectError(() => { createSampler({ foo: 'bar' }) })
expectError(() => { createSampler({ sampleInterval: 'bar' }) })
expectError(() => { createSampler({ eventLoopDelayOptions: 'bar' }) })
expectError(() => { doc(1) })
expectError(() => { doc('string') })
expectError(() => { doc(null) })
expectError(() => { doc({ foo: 'bar' }) })
expectError(() => { doc({ sampleInterval: 'bar' }) })
expectError(() => { doc({ eventLoopDelayOptions: 'bar' }) })
3 changes: 0 additions & 3 deletions lib/eventLoopUtilization.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const kLastSample = Symbol('kLastSample')

class EventLoopUtiizationMetric {
constructor () {
if (!eventLoopUtilization) {
throw new Error('eventLoopUtilization is not supported on this Node.js version')
}
this[kLastSample] = eventLoopUtilization()
this[kRawMetric] = this[kLastSample]
}
Expand Down
4 changes: 2 additions & 2 deletions test/doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ test('eventLoopDelay', t => {
})
})

test('eventLoopUtilization', { skip: !doc.eventLoopUtilizationSupported }, t => {
test('eventLoopUtilization', t => {
t.plan(7)
const sampler = doc()

Expand All @@ -160,7 +160,7 @@ test('eventLoopUtilization', { skip: !doc.eventLoopUtilizationSupported }, t =>
})
})

test('gc', { only: true }, t => {
test('gc', t => {
t.plan(25)
const sampler = doc({
gcOptions: {
Expand Down
14 changes: 2 additions & 12 deletions types/eventLoopDelayMetric.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
/**
* On Node 12 and above this is a Histogram instance from 'perf_hooks'.
*/
declare interface EventLoopDelayHistogram {
min: number,
max: number,
mean: number,
stddev: number,
percentiles: Map<number, number>,
exceeds: number,
}
import { IntervalHistogram } from "perf_hooks";

export interface EventLoopDelayMetric {
/**
* computed delay in milliseconds
*/
computed: number,
raw: number | EventLoopDelayHistogram
raw: IntervalHistogram
}
8 changes: 3 additions & 5 deletions types/eventLoopUtilizationMetric.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { EventLoopUtilization } from "perf_hooks"

export interface EventLoopUtilizationMetric {
/**
* Raw metric value
*/
raw: {
idle: number,
active: number,
utilization: number
}
raw: EventLoopUtilization
}
23 changes: 5 additions & 18 deletions types/resourceUsageMetric.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { resourceUsage } from "process";

type ResourceUsage = ReturnType<typeof resourceUsage>

export interface ResourceUsageMetric {
/**
* Cpu usage percentage
Expand All @@ -6,22 +10,5 @@ export interface ResourceUsageMetric {
/**
* Raw vaule returned by `process.resourceUsage()`
*/
raw: {
fsRead: number;
fsWrite: number;
involuntaryContextSwitches: number;
ipcReceived: number;
ipcSent: number;
majorPageFault: number;
maxRSS: number;
minorPageFault: number;
sharedMemorySize: number;
signalsCount: number;
swappedOut: number;
systemCPUTime: number;
unsharedDataSize: number;
unsharedStackSize: number;
userCPUTime: number;
voluntaryContextSwitches: number;
}
raw: ResourceUsage
}

0 comments on commit 410bbf0

Please sign in to comment.