1
+ import { gzipSync } from 'node:zlib' ;
2
+ import {
3
+ KinesisDataStreamRecord ,
4
+ KinesisDataStreamRecordPayload ,
5
+ KinesisDataStreamSchema ,
6
+ } from 'src/schemas/kinesis.js' ;
1
7
import { describe , expect , it } from 'vitest' ;
2
8
import { z } from 'zod' ;
3
9
import { DynamoDBMarshalled } from '../../src/helpers/dynamodb.js' ;
4
- import { JSONStringified } from '../../src/helpers/index.js' ;
10
+ import { Base64Encoded , JSONStringified } from '../../src/helpers/index.js' ;
5
11
import { AlbSchema } from '../../src/schemas/alb.js' ;
6
12
import {
7
13
DynamoDBStreamRecord ,
@@ -14,6 +20,7 @@ import {
14
20
import { SqsRecordSchema , SqsSchema } from '../../src/schemas/sqs.js' ;
15
21
import type {
16
22
DynamoDBStreamEvent ,
23
+ KinesisDataStreamEvent ,
17
24
SnsEvent ,
18
25
SqsEvent ,
19
26
} from '../../src/types/schema.js' ;
@@ -277,3 +284,129 @@ describe('Helper: DynamoDBMarshalled', () => {
277
284
expect ( ( ) => extendedSchema . parse ( event ) ) . toThrow ( ) ;
278
285
} ) ;
279
286
} ) ;
287
+
288
+ describe ( 'Helper: Base64Encoded' , ( ) => {
289
+ it ( 'returns a valid base64 decoded object when passed an encoded object' , ( ) => {
290
+ // Prepare
291
+ const data = {
292
+ body : Buffer . from ( JSON . stringify ( structuredClone ( basePayload ) ) ) . toString (
293
+ 'base64'
294
+ ) ,
295
+ } ;
296
+
297
+ // Act
298
+ const extendedSchema = envelopeSchema . extend ( {
299
+ body : Base64Encoded ( bodySchema ) ,
300
+ } ) ;
301
+
302
+ // Assess
303
+ expect ( extendedSchema . parse ( data ) ) . toStrictEqual ( {
304
+ body : basePayload ,
305
+ } ) ;
306
+ } ) ;
307
+
308
+ it ( 'returns a valid base64 decoded object when passed a compressed object' , ( ) => {
309
+ // Prepare
310
+ const data = {
311
+ body : Buffer . from (
312
+ gzipSync ( JSON . stringify ( structuredClone ( basePayload ) ) )
313
+ ) . toString ( 'base64' ) ,
314
+ } ;
315
+
316
+ // Act
317
+ const extendedSchema = envelopeSchema . extend ( {
318
+ body : Base64Encoded ( bodySchema ) ,
319
+ } ) ;
320
+
321
+ // Assess
322
+ expect ( extendedSchema . parse ( data ) ) . toStrictEqual ( {
323
+ body : basePayload ,
324
+ } ) ;
325
+ } ) ;
326
+
327
+ it ( 'throws an error if the payload is does not match the schema' , ( ) => {
328
+ // Prepare
329
+ const data = {
330
+ body : Buffer . from (
331
+ JSON . stringify ( { ...basePayload , email : 'invalid' } )
332
+ ) . toString ( 'base64' ) ,
333
+ } ;
334
+
335
+ // Act
336
+ const extendedSchema = envelopeSchema . extend ( {
337
+ body : Base64Encoded ( bodySchema ) ,
338
+ } ) ;
339
+
340
+ // Assess
341
+ expect ( ( ) => extendedSchema . parse ( data ) ) . toThrow ( ) ;
342
+ } ) ;
343
+
344
+ it ( 'throws an error if the payload is malformed' , ( ) => {
345
+ // Prepare
346
+ const data = {
347
+ body : Buffer . from ( '{"foo": 1, }' ) . toString ( 'base64' ) ,
348
+ } ;
349
+
350
+ // Act
351
+ const extendedSchema = envelopeSchema . extend ( {
352
+ body : Base64Encoded ( bodySchema ) ,
353
+ } ) ;
354
+
355
+ // Assess
356
+ expect ( ( ) => extendedSchema . parse ( data ) ) . toThrow ( ) ;
357
+ } ) ;
358
+
359
+ it ( 'throws an error if the base64 payload is malformed' , ( ) => {
360
+ // Prepare
361
+ const data = {
362
+ body : 'invalid-base64-string' ,
363
+ } ;
364
+
365
+ // Act
366
+ const extendedSchema = envelopeSchema . extend ( {
367
+ body : Base64Encoded ( bodySchema ) ,
368
+ } ) ;
369
+
370
+ // Assess
371
+ expect ( ( ) => extendedSchema . parse ( data ) ) . toThrow ( ) ;
372
+ } ) ;
373
+
374
+ it ( 'parses extended KinesisDataStreamSchema' , ( ) => {
375
+ // Prepare
376
+ const testEvent = getTestEvent < KinesisDataStreamEvent > ( {
377
+ eventsPath : 'kinesis' ,
378
+ filename : 'stream' ,
379
+ } ) ;
380
+ const stringifiedBody = JSON . stringify ( basePayload ) ;
381
+ testEvent . Records [ 0 ] . kinesis . data =
382
+ Buffer . from ( stringifiedBody ) . toString ( 'base64' ) ;
383
+ testEvent . Records [ 1 ] . kinesis . data =
384
+ Buffer . from ( stringifiedBody ) . toString ( 'base64' ) ;
385
+
386
+ // Act
387
+ const extendedSchema = KinesisDataStreamSchema . extend ( {
388
+ Records : z . array (
389
+ KinesisDataStreamRecord . extend ( {
390
+ kinesis : KinesisDataStreamRecordPayload . extend ( {
391
+ data : Base64Encoded ( bodySchema ) ,
392
+ } ) ,
393
+ } )
394
+ ) ,
395
+ } ) ;
396
+
397
+ // Assess
398
+ expect ( extendedSchema . parse ( testEvent ) ) . toStrictEqual ( {
399
+ ...testEvent ,
400
+ Records : [
401
+ {
402
+ ...testEvent . Records [ 0 ] ,
403
+ kinesis : { ...testEvent . Records [ 0 ] . kinesis , data : basePayload } ,
404
+ } ,
405
+ {
406
+ ...testEvent . Records [ 1 ] ,
407
+ kinesis : { ...testEvent . Records [ 1 ] . kinesis , data : basePayload } ,
408
+ } ,
409
+ ] ,
410
+ } ) ;
411
+ } ) ;
412
+ } ) ;
0 commit comments