Skip to content

Commit

Permalink
fix: revert telemetry client validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb committed Dec 21, 2022
1 parent 7580f5b commit 33e3a60
Showing 1 changed file with 2 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/telemetry/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,6 @@ export interface VersionCommand {
version: string;
}

/**
* Ajv is a peer dependency for development builds. It's used to apply run-time validation
* to message payloads before passing them on to the underlying analytics instance.
*
* Note that the production bundle does not depend on Ajv.
*
* You can install it with: `npm install --save-dev ajv`.
*/
import Ajv, { ErrorObject } from 'ajv'
import AnalyticsNode from 'analytics-node'

/**
Expand Down Expand Up @@ -372,7 +363,7 @@ export interface Context extends Record<string, any> {

export type ViolationHandler = (
message: TrackMessage<Record<string, any>>,
violations: ErrorObject[]
violations: any[]
) => void

/**
Expand Down Expand Up @@ -402,7 +393,6 @@ export type ViolationHandler = (
console.warn(msg)
}

let onViolation = defaultValidationErrorHandler

const missingAnalyticsNodeError = new Error(`You must set an analytics-node instance:
Expand Down Expand Up @@ -455,23 +445,8 @@ export interface TypewriterOptions {
*/
export function setTypewriterOptions(options: TypewriterOptions) {
analytics = options.analytics ? () => options.analytics : analytics
onViolation = options.onViolation || onViolation
}

/**
* Validates a message against a JSON Schema using Ajv. If the message
* is invalid, the `onViolation` handler will be called.
*/
function validateAgainstSchema(
message: TrackMessage<Record<string, any>>,
schema: object
) {
const ajv = new Ajv({ allErrors: true, verbose: true })

if (!ajv.validate(schema, message.properties) && ajv.errors) {
onViolation(message, ajv.errors)
}
}

/**
* Helper to attach metadata on Typewriter to outbound requests.
Expand All @@ -486,7 +461,7 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
...(message.context || {}),
typewriter: {
language: 'typescript',
version: '8.0.8',
version: '8.0.9',
},
},
}
Expand All @@ -505,15 +480,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<CommandBuild>,
callback?: Callback
): void {
const schema = {"$id":"Command Build","$schema":"http://json-schema.org/draft-07/schema#","description":"Fired when the user generates code with Typewriter","properties":{"config":{"$id":"/properties/config","description":"Local Workspace configuration","properties":{"language":{"$id":"/properties/properties/properties/config/properties/language","description":"Language to Generate","type":"string"},"languageOptions":{"$id":"/properties/properties/properties/config/properties/languageOptions","description":"Advanced Language Options","type":"object"},"sdk":{"$id":"/properties/properties/properties/config/properties/sdk","description":"SDK to generate","type":"string"},"tokenType":{"$id":"/properties/properties/properties/config/properties/tokenType","description":"Type of token retrieval","enum":["global","input","script"],"type":"string"},"trackingPlans":{"$id":"/properties/properties/properties/config/properties/trackingPlans","description":"Tracking Plans to generate code for","items":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items","description":"","properties":{"id":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/id","description":"Tracking Plan ID","type":"string"},"path":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/path","description":"Path to output code","type":"string"}},"required":[],"type":"object"},"type":"array"}},"required":["trackingPlans","language","sdk"],"type":"object"},"duration":{"$id":"/properties/duration","description":"Duration of the command in ms","type":"number"},"isCI":{"$id":"/properties/isCI","description":"Runs in a CI environment","type":"string"},"mode":{"$id":"/properties/mode","description":"Build Output mode","enum":["dev","prod"],"type":"string"},"rawCommand":{"$id":"/properties/rawCommand","description":"Raw command string input","type":"string"},"workspace":{"$id":"/properties/workspace","description":"User Segment Workspace","type":"string"}},"required":["workspace","config","mode","duration","isCI","rawCommand"],"type":"object"}
const event = withTypewriterContext({
...message,
event: 'Command Build',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand All @@ -534,15 +507,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<CommandError>,
callback?: Callback
): void {
const schema = {"$id":"Command Error","$schema":"http://json-schema.org/draft-07/schema#","properties":{"config":{"$id":"/properties/config","description":"Local Workspace configuration","properties":{"language":{"$id":"/properties/properties/properties/config/properties/language","description":"Language to generate","type":"string"},"languageOptions":{"$id":"/properties/properties/properties/config/properties/languageOptions","description":"Advanced Language Options","type":"object"},"sdk":{"$id":"/properties/properties/properties/config/properties/sdk","description":"SDK to generate","type":"string"},"tokenType":{"$id":"/properties/properties/properties/config/properties/tokenType","description":"Type of token retrieval","enum":["global","input","script"],"type":"string"},"trackingPlans":{"$id":"/properties/properties/properties/config/properties/trackingPlans","description":"Tracking Plans to generate code for","items":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items","description":"","properties":{"id":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/id","description":"Tracking Plan ID","type":"string"},"path":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/path","description":"Path to output code","type":"string"}},"required":[],"type":"object"},"type":"array"}},"required":["trackingPlans","language","sdk"],"type":"object"},"error":{"$id":"/properties/error","description":"Error Object","type":"object"},"errorCode":{"$id":"/properties/errorCode","description":"Exit code for the error","type":"number"},"errorMessage":{"$id":"/properties/errorMessage","description":"User friendly error message","type":"string"},"isCI":{"$id":"/properties/isCI","description":"Runs in a CI environment","type":"string"},"rawCommand":{"$id":"/properties/rawCommand","description":"Raw command string input","type":"string"},"workspace":{"$id":"/properties/workspace","description":"User Segment Workspace","type":"string"}},"required":["isCI","rawCommand","errorMessage","error"],"type":"object"}
const event = withTypewriterContext({
...message,
event: 'Command Error',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand All @@ -563,15 +534,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<CommandHelp>,
callback?: Callback
): void {
const schema = {"$id":"Command Help","$schema":"http://json-schema.org/draft-07/schema#","properties":{"rawCommand":{"$id":"/properties/rawCommand","description":"Raw command string input","type":"string"}},"required":["rawCommand"],"type":"object"}
const event = withTypewriterContext({
...message,
event: 'Command Help',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand All @@ -592,15 +561,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<CommandInit>,
callback?: Callback
): void {
const schema = {"$id":"Command Init","$schema":"http://json-schema.org/draft-07/schema#","description":"Init Events that initialize the settings for typewriter","labels":{"init":"initialize"},"properties":{"config":{"$id":"/properties/config","description":"Local Workspace configuration","properties":{"language":{"$id":"/properties/properties/properties/config/properties/language","description":"Language to generate","type":"string"},"languageOptions":{"$id":"/properties/properties/properties/config/properties/languageOptions","description":"Advanced Language Options","type":"object"},"sdk":{"$id":"/properties/properties/properties/config/properties/sdk","description":"SDK to generate","type":"string"},"tokenType":{"$id":"/properties/properties/properties/config/properties/tokenType","description":"Type of token retrieval","enum":["global","input","script"],"type":"string"},"trackingPlans":{"$id":"/properties/properties/properties/config/properties/trackingPlans","description":"Tracking Plans to generate code for","items":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items","description":"","properties":{"id":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/id","description":"Tracking Plan ID","type":"string"},"path":{"$id":"/properties/properties/properties/config/properties/trackingPlans/items/properties/path","description":"Path to output code","type":"string"}},"required":["id","path"],"type":"object"},"type":"array"},"workspace":{"$id":"/properties/properties/properties/config/properties/workspace","description":"User Segment Workspace","type":"string"}},"required":["language","sdk","tokenType","workspace","trackingPlans"],"type":"object"},"duration":{"$id":"/properties/duration","description":"Duration of the command in ms","type":"number"},"hasConfig":{"$id":"/properties/hasConfig","description":"Wether there is a workspace config already present","type":"boolean"},"rawCommand":{"$id":"/properties/rawCommand","description":"Raw command string input","type":"string"}},"required":["config","rawCommand","duration"],"type":"object"}
const event = withTypewriterContext({
...message,
event: 'Command Init',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand All @@ -621,15 +588,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<KitchenSink>,
callback?: Callback
): void {
const schema = {"$id":"KitchenSink","$schema":"http://json-schema.org/draft-07/schema#","description":"KitchenSink Event for testing all possible data types","properties":{"aBoolean":{"$id":"/properties/aBoolean","description":"","type":"boolean"},"aDate":{"$id":"/properties/aDate","description":"","format":"date-time","type":"string"},"aNumber":{"$id":"/properties/aNumber","description":"","type":"number"},"aString":{"$id":"/properties/aString","description":"","type":"string"},"anArray":{"$id":"/properties/anArray","description":"","type":"array"},"anEnum":{"$id":"/properties/anEnum","description":"","enum":["another","value"],"type":"string"},"anInteger":{"$id":"/properties/anInteger","description":"","type":"integer"},"anObject":{"$id":"/properties/anObject","description":"","type":"object"},"anything":{"$id":"/properties/anything","description":""}},"type":"object"}
const event = withTypewriterContext({
...message,
event: 'KitchenSink',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand All @@ -650,15 +615,13 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
message: TrackMessage<VersionCommand>,
callback?: Callback
): void {
const schema = {"$id":"VersionCommand","$schema":"http://json-schema.org/draft-07/schema#","properties":{"version":{"$id":"/properties/version","description":"","type":"string"}},"required":["version"],"type":"object"}
const event = withTypewriterContext({
...message,
event: 'VersionCommand',
properties: {
...message.properties,
},
});
validateAgainstSchema(event, schema)

const a = analytics()
if (a) {
Expand Down

0 comments on commit 33e3a60

Please sign in to comment.