Skip to content
This repository was archived by the owner on Feb 3, 2021. It is now read-only.

Commit a980ad0

Browse files
committed
fix: add null check on pagination
1 parent 0276007 commit a980ad0

9 files changed

+43
-31
lines changed

Diff for: cdk/createThingGroup.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Iot } from 'aws-sdk'
22
import { CloudFormationCustomResourceEvent } from 'aws-lambda'
33
import { paginate } from '../util/paginate'
44
import { cfnResponse, ResponseStatus } from '@bifravst/cloudformation-helpers'
5+
import { isNullOrUndefined } from 'util'
56

67
const iot = new Iot()
78

@@ -32,7 +33,7 @@ export const handler = async (
3233
thingGroupProperties: ThingGroupProperties,
3334
})
3435
.promise()
35-
if (thingGroupArn === undefined) {
36+
if (isNullOrUndefined(thingGroupArn)) {
3637
throw new Error(`Failed to create thing group ${ThingGroupName}!`)
3738
}
3839
await iot

Diff for: cdk/helper/getIotEndpoint.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Iot } from 'aws-sdk'
2+
import { isNullOrUndefined } from 'util'
23

34
export const getIotEndpoint = async (iot: Iot): Promise<string> =>
45
iot
56
.describeEndpoint({ endpointType: 'iot:Data-ATS' })
67
.promise()
78
.then(({ endpointAddress }) => {
8-
if (endpointAddress === undefined) {
9+
if (isNullOrUndefined(endpointAddress)) {
910
throw new Error(`Failed to resolved AWS IoT endpoint`)
1011
}
1112
return endpointAddress

Diff for: cdk/helper/getLambdaSourceCodeBucketName.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CloudFormation } from 'aws-sdk'
22
import { stackId } from '../stacks/stackId'
3+
import { isNullOrUndefined } from 'util'
34

45
const cf = new CloudFormation({
56
region: process.env.AWS_DEFAULT_REGION,
@@ -13,7 +14,7 @@ export const getLambdaSourceCodeBucketName = async (): Promise<string> => {
1314
})
1415
.promise()
1516
.then(({ Stacks }) => {
16-
if (Stacks === undefined || !Stacks.length) {
17+
if (isNullOrUndefined(Stacks) || !Stacks.length) {
1718
throw new Error(`${StackName} stack is not available.`)
1819
} else {
1920
const stack = Stacks[0]

Diff for: cli/commands/purge-iot-user-policy-principals.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@ export const purgeIotUserPolicyPrincipals = ({
1818
const policyName = userIotPolicyArn?.split('/').pop() as string
1919
const iot = new Iot({ region })
2020
await paginate({
21-
paginator: async (marker?: any) =>
22-
iot
21+
paginator: async (marker?: any) => {
22+
const { principals, nextMarker } = await iot
2323
.listPolicyPrincipals({
2424
policyName,
2525
marker,
2626
})
2727
.promise()
28-
.then(async ({ principals, nextMarker }) => {
29-
await Promise.all(
30-
principals?.map(async (principal) => {
31-
console.log(
32-
`Detaching principal ${principal} from policy ${policyName} ...`,
33-
)
34-
return iot
35-
.detachPrincipalPolicy({
36-
policyName,
37-
principal,
38-
})
39-
.promise()
40-
}) ?? [],
28+
await Promise.all(
29+
principals?.map(async (principal) => {
30+
console.log(
31+
`Detaching principal ${principal} from policy ${policyName} ...`,
4132
)
42-
return nextMarker
43-
}),
33+
return iot
34+
.detachPrincipalPolicy({
35+
policyName,
36+
principal,
37+
})
38+
.promise()
39+
}) ?? [],
40+
)
41+
return nextMarker
42+
},
4443
})
4544
},
4645
help: 'Purges all principals from the user IoT policy',

Diff for: feature-runner/steps/bifravst.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import * as path from 'path'
1111
import { device, thingShadow } from 'aws-iot-device-sdk'
1212
import { deviceFileLocations } from '../../cli/jitp/deviceFileLocations'
1313
import { expect } from 'chai'
14-
15-
const isNullOrUndefined = (arg?: unknown | null): boolean =>
16-
arg === undefined || arg === null
17-
const isNotNullOrUndefined = (arg?: unknown | null) => !isNullOrUndefined(arg)
14+
import { isNotNullOrUndefined } from '../../util/isNullOrUndefined'
1815

1916
const connect = (mqttEndpoint: string) => (clientId: string) => {
2017
const deviceFiles = deviceFileLocations({

Diff for: historicalData/collectFiles.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { S3 } from 'aws-sdk'
2+
import {
3+
isNotNullOrUndefined,
4+
isNullOrUndefined,
5+
} from '../util/isNullOrUndefined'
26

37
export type CollectFilesArgs = {
48
Prefix: string
@@ -42,14 +46,14 @@ export const collectFiles = ({
4246
if (d >= notAfterDate) {
4347
return files
4448
}
45-
if (files[d] === undefined) {
49+
if (isNullOrUndefined(files[d])) {
4650
files[d] = [file]
4751
} else {
4852
files[d].push(file)
4953
}
5054
return files
5155
}, files ?? {})
52-
if (NextMarker !== undefined) {
56+
if (isNotNullOrUndefined(NextMarker)) {
5357
return collectFiles({ Bucket, s3 })({
5458
files: f,
5559
Marker: NextMarker,

Diff for: package-lock.json

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: util/isNullOrUndefined.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const isNullOrUndefined = (arg?: unknown | null): boolean =>
2+
arg === undefined || arg === null
3+
4+
export const isNotNullOrUndefined = (arg?: unknown | null): boolean =>
5+
!isNullOrUndefined(arg)

Diff for: util/paginate.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isNotNullOrUndefined } from './isNullOrUndefined'
2+
13
/**
24
* Iteratively follows paginated results.
35
* NOTE: This method has no upper runtime limit and may time out.
@@ -10,7 +12,7 @@ export const paginate = async ({
1012
startKey?: any
1113
}): Promise<void> => {
1214
const nextStartKey = await paginator(startKey)
13-
if (nextStartKey !== undefined) {
15+
if (isNotNullOrUndefined(nextStartKey)) {
1416
await paginate({
1517
paginator,
1618
startKey: nextStartKey,

0 commit comments

Comments
 (0)