-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation to other ws mesages
- Loading branch information
1 parent
5f89719
commit 5c309ea
Showing
9 changed files
with
135 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
import Joi from 'joi'; | ||
import type { Probe } from '../../probe/types.js'; | ||
import type { MeasurementProgressMessage } from '../types.js'; | ||
import { getMeasurementRunner } from '../runner.js'; | ||
import { getProbeValidator } from '../../lib/probe-validator.js'; | ||
|
||
const schema = Joi.object<MeasurementProgressMessage>({ | ||
testId: Joi.string().required(), | ||
measurementId: Joi.string().required(), | ||
overwrite: Joi.boolean(), | ||
result: Joi.object({ | ||
rawOutput: Joi.string().required(), | ||
rawHeaders: Joi.string(), | ||
rawBody: Joi.string(), | ||
}).required(), | ||
}).required(); | ||
|
||
const runner = getMeasurementRunner(); | ||
|
||
export const handleMeasurementProgress = (probe: Probe) => async (data: MeasurementProgressMessage): Promise<void> => { | ||
await getProbeValidator().validateProbe(data.measurementId, data.testId, probe.uuid); | ||
await runner.recordProgress(data); | ||
const validation = schema.validate(data); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
await getProbeValidator().validateProbe(validation.value.measurementId, validation.value.testId, probe.uuid); | ||
await runner.recordProgress(validation.value); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,47 @@ | ||
import Joi from 'joi'; | ||
import type { Probe } from '../../probe/types.js'; | ||
import type { MeasurementResultMessage } from '../types.js'; | ||
import type { MeasurementResultMessage, PingResult } from '../types.js'; | ||
import { getMeasurementRunner } from '../runner.js'; | ||
import { getProbeValidator } from '../../lib/probe-validator.js'; | ||
|
||
const pingResultSchema = Joi.object<PingResult>({ | ||
status: Joi.string().required(), | ||
rawOutput: Joi.string().required(), | ||
resolvedAddress: Joi.string().required().allow(null), | ||
resolvedHostname: Joi.string().required().allow(null), | ||
timings: Joi.array().items(Joi.object({ | ||
rtt: Joi.number().required(), | ||
ttl: Joi.number().required(), | ||
})).required(), | ||
stats: Joi.object({ | ||
min: Joi.number().required().allow(null), | ||
max: Joi.number().required().allow(null), | ||
avg: Joi.number().required().allow(null), | ||
total: Joi.number().required().allow(null), | ||
loss: Joi.number().required().allow(null), | ||
rcv: Joi.number().required().allow(null), | ||
drop: Joi.number().required().allow(null), | ||
}).required(), | ||
}); | ||
|
||
const schema = Joi.object<MeasurementResultMessage>({ | ||
testId: Joi.string().required(), | ||
measurementId: Joi.string().required(), | ||
overwrite: Joi.boolean(), | ||
result: Joi.alternatives([ | ||
pingResultSchema, | ||
]).required(), | ||
}).required(); | ||
|
||
const runner = getMeasurementRunner(); | ||
|
||
export const handleMeasurementResult = (probe: Probe) => async (data: MeasurementResultMessage): Promise<void> => { | ||
const validation = schema.validate(data); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
await getProbeValidator().validateProbe(data.measurementId, data.testId, probe.uuid); | ||
await runner.recordResult(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import Joi from 'joi'; | ||
import type { Probe } from '../types.js'; | ||
|
||
const schema = Joi.array<string[]>().items(Joi.string()).required(); | ||
|
||
export const handleDnsUpdate = (probe: Probe) => (list: string[]): void => { | ||
probe.resolvers = list; | ||
const validation = schema.validate(list); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
probe.resolvers = validation.value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
import Joi from 'joi'; | ||
import type { Probe } from '../types.js'; | ||
|
||
const schema = Joi.boolean().required(); | ||
|
||
export const handleIsIPv4SupportedUpdate = (probe: Probe) => (isIPv4Supported: boolean): void => { | ||
probe.isIPv4Supported = isIPv4Supported; | ||
const validation = schema.validate(isIPv4Supported); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
probe.isIPv4Supported = validation.value; | ||
}; | ||
|
||
export const handleIsIPv6SupportedUpdate = (probe: Probe) => (isIPv6Supported: boolean): void => { | ||
probe.isIPv6Supported = isIPv6Supported; | ||
const validation = schema.validate(isIPv6Supported); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
probe.isIPv6Supported = validation.value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
import Joi from 'joi'; | ||
import type { Probe, ProbeStats } from '../types.js'; | ||
|
||
const schema = Joi.object<ProbeStats>({ | ||
cpu: Joi.object({ | ||
load: Joi.array().items(Joi.object({ | ||
usage: Joi.number().required(), | ||
})).required(), | ||
}).required(), | ||
jobs: Joi.object({ | ||
count: Joi.number().required(), | ||
}).required(), | ||
}).required(); | ||
|
||
export const handleStatsReport = (probe: Probe) => (report: ProbeStats): void => { | ||
probe.stats = report; | ||
const validation = schema.validate(report); | ||
|
||
if (validation.error) { | ||
throw validation.error; | ||
} | ||
|
||
probe.stats = validation.value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters