Skip to content

fix(parser): allow VPC envelopes to handle non-JSON strings #3534

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

Merged
merged 3 commits into from
Jan 27, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,5 @@
"@middy/core": {
"optional": true
}
},
"devDependencies": {
"@anatine/zod-mock": "^3.13.3"
}
}
77 changes: 1 addition & 76 deletions packages/parser/src/envelopes/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,7 @@
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import type { ParsedResult } from '../types/parser.js';

/* v8 ignore start */
const Envelope = {
/**
* Abstract function to parse the content of the envelope using provided schema.
* Both inputs are provided as unknown by the user.
* We expect the data to be either string that can be parsed to json or object.
* @internal
* @param data data to parse
* @param schema schema
*/
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
if (typeof data !== 'object' && typeof data !== 'string') {
throw new ParseError(
`Invalid data type for envelope. Expected string or object, got ${typeof data}`
);
}
try {
if (typeof data === 'string') {
return schema.parse(JSON.parse(data));
}
if (typeof data === 'object') {
return schema.parse(data);
}
} catch (e) {
throw new ParseError('Failed to parse envelope', { cause: e as Error });
}
},

/**
* Abstract function to safely parse the content of the envelope using provided schema.
* safeParse is used to avoid throwing errors, thus we catuch all errors and wrap them in the result.
* @param input
* @param schema
*/
safeParse<T extends ZodSchema>(
input: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
try {
if (typeof input !== 'object' && typeof input !== 'string') {
return {
success: false,
error: new Error(
`Invalid data type for envelope. Expected string or object, got ${typeof input}`
),
};
}

const parsed = schema.safeParse(
typeof input === 'string' ? JSON.parse(input) : input
);

return parsed.success
? {
success: true,
data: parsed.data,
}
: {
success: false,
error: parsed.error,
};
} catch (e) {
return {
success: false,
error: e as Error,
};
}
},
};

/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
const envelopeDiscriminator = Symbol.for('returnType');

export { Envelope, envelopeDiscriminator };
/* v8 ignore stop */
export { envelopeDiscriminator };
45 changes: 24 additions & 21 deletions packages/parser/src/envelopes/vpc-lattice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { VpcLatticeSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';
import { envelopeDiscriminator } from './envelope.js';

/**
* Amazon VPC Lattice envelope to extract data within body key
Expand All @@ -15,35 +15,38 @@ export const VpcLatticeEnvelope = {
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
const parsedEnvelope = VpcLatticeSchema.parse(data);

return Envelope.parse(parsedEnvelope.body, schema);
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult<unknown, z.infer<T>> {
const parsedEnvelope = VpcLatticeSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
success: false,
error: new ParseError('Failed to parse VpcLattice envelope', {
cause: parsedEnvelope.error,
}),
originalEvent: data,
};
try {
return VpcLatticeSchema.extend({
body: schema,
}).parse(data).body;
} catch (error) {
throw new ParseError('Failed to parse VPC Lattice body', {
cause: error as Error,
});
}
},

const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
const result = VpcLatticeSchema.extend({
body: schema,
}).safeParse(data);

if (!parsedBody.success) {
if (!result.success) {
return {
success: false,
error: new ParseError('Failed to parse VpcLattice envelope body', {
cause: parsedBody.error,
error: new ParseError('Failed to parse VPC Lattice body', {
cause: result.error,
}),
originalEvent: data,
};
}

return parsedBody;
return {
success: true,
data: result.data.body,
};
},
};
45 changes: 24 additions & 21 deletions packages/parser/src/envelopes/vpc-latticev2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { VpcLatticeV2Schema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';
import { envelopeDiscriminator } from './envelope.js';

/**
* Amazon VPC Lattice envelope to extract data within body key
Expand All @@ -14,35 +14,38 @@ export const VpcLatticeV2Envelope = {
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
const parsedEnvelope = VpcLatticeV2Schema.parse(data);

return Envelope.parse(parsedEnvelope.body, schema);
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult<unknown, z.infer<T>> {
const parsedEnvelope = VpcLatticeV2Schema.safeParse(data);
if (!parsedEnvelope.success) {
return {
success: false,
error: new ParseError('Failed to parse VpcLatticeV2 envelope.', {
cause: parsedEnvelope.error,
}),
originalEvent: data,
};
try {
return VpcLatticeV2Schema.extend({
body: schema,
}).parse(data).body;
} catch (error) {
throw new ParseError('Failed to parse VPC Lattice v2 body', {
cause: error as Error,
});
}
},

const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
const result = VpcLatticeV2Schema.extend({
body: schema,
}).safeParse(data);

if (!parsedBody.success) {
if (!result.success) {
return {
success: false,
error: new ParseError('Failed to parse VpcLatticeV2 body.', {
cause: parsedBody.error,
error: new ParseError('Failed to parse VPC Lattice v2 body', {
cause: result.error,
}),
originalEvent: data,
};
}

return parsedBody;
return {
success: true,
data: result.data.body,
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"query_string_parameters": {
"order-id": "1"
},
"body": "eyJ0ZXN0IjogImV2ZW50In0=",
"is_base64_encoded": true
}
"body": "{\"message\": \"Hello from Lambda!\"}",
"is_base64_encoded": false
}
15 changes: 0 additions & 15 deletions packages/parser/tests/events/vpcLatticeEventPathTrailingSlash.json

This file was deleted.

This file was deleted.

Loading
Loading