Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async deployWebsockets() {
this.init()
await this.prepareFunctions()
if (
!is(Object, this.serverless.service.functions) ||
keys(this.serverless.service.functions).length === 0 ||
isEmpty(this.functions)
) {
return
}
this.serverless.cli.log(`Deploying Websockets API named "${this.apiName}"...`)
await this.createApi()
// We clear routes before deploying the new routes for idempotency
// since we lost the idempotency feature of CF
await this.clearRoutes()
await this.clearAuthorizers()
await this.clearIntegrations()
await this.createAuthorizers()
await this.createRoutes()
await this.createDeployment()
this.serverless.cli.log(
`Websockets API named "${this.apiName}" with ID "${this.apiId}" has been deployed.`
)
let paramsAsObject = reduce(
(accum, _, key) => {
const value = resolve(instance.inputs[key])
// TODO: replace with a universal util function which checks whether we're
// dealing with a Component, Object, Provider, etc. instance here
// omitting own Types due to circular structure / verbosity here
if (!(value.name && value.version)) {
return assoc(key, value, accum)
}
return accum
},
{},
requiredParams
)
if (not(isEmpty(paramsAsObject))) {
try {
paramsAsObject = JSON.stringify(paramsAsObject, null, 2)
} catch (error) {
// simply reassign to an empty object
paramsAsObject = {}
} finally {
return paramsAsObject
}
}
return paramsAsObject
}
}
validate() {
if (!/.+-.+.\d+/.test(this.region)) {
throw new Error(`Invalid region "${this.region}" in your AWS provider setup`)
}
if (!this.credentials || isEmpty(this.credentials)) {
throw new Error(`Credentials not set in your AWS provider setup`)
}
}
}
async displayWebsockets() {
this.init()
await this.prepareFunctions()
if (isEmpty(this.functions)) {
return
}
await this.getApi()
const baseUrl = this.getWebsocketUrl()
const routes = flatten(map((fn) => fn.routes.routeKey, this.functions))
this.serverless.cli.consoleLog(chalk.yellow('WebSockets:'))
this.serverless.cli.consoleLog(` ${chalk.yellow('Base URL:')} ${baseUrl}`)
this.serverless.cli.consoleLog(chalk.yellow(' Routes:'))
map((route) => this.serverless.cli.consoleLog(` - ${baseUrl}${route}`), routes)
}
}
const { prevInstance, nextInstance } = node
const componentName =
(nextInstance && nextInstance.constructor.name) ||
(prevInstance && prevInstance.constructor.name)
if (iteratee.name === 'deployNode') {
const params = getParameters(nextInstance)
context.log(
`Deploying "${componentName}" ${not(isEmpty(params)) ? `with parameters: ${params}` : ''}`
)
} else if (iteratee.name === 'removeNode') {
if (prevInstance) {
const params = getParameters(prevInstance)
context.log(
`Removing "${componentName}" ${not(isEmpty(params)) ? `with parameters: ${params}` : ''}`
)
}
}
}
const addRolePolicy = async (IAM, { roleName, policy }) => {
if (has('arn', policy)) {
await IAM.attachRolePolicy({
RoleName: roleName,
PolicyArn: policy.arn
}).promise()
} else if (!isEmpty(policy)) {
await IAM.putRolePolicy({
RoleName: roleName,
PolicyName: `${roleName}-policy`,
PolicyDocument: JSON.stringify(policy)
}).promise()
}
return sleep(15000)
}
const setSubscriptionAttributes = async (
{ provider, subscriptionArn, attributeName, attributeValue },
context
) => {
const SDK = provider.getSdk()
const sns = new SDK.SNS()
if (isEmpty(attributeValue)) {
context.log(
`Removing SNS Subscription Attribute '${attributeName}' from subscription ${subscriptionArn}`
)
} else {
context.log(
`Setting SNS Subscription Attribute '${attributeName}' to subscription ${subscriptionArn}`
)
}
try {
const response = await sns
.setSubscriptionAttributes({
AttributeName: attributeName,
SubscriptionArn: subscriptionArn,
AttributeValue:
typeof attributeValue === 'string' ? attributeValue : JSON.stringify(attributeValue)
})
const removeNode = async (node, context) => {
const { instanceId, operation } = node
let { nextInstance, prevInstance } = node
if (!isEmpty(nextInstance)) {
nextInstance = resolveComponentEvaluables(nextInstance)
}
if (!isEmpty(prevInstance)) {
prevInstance = resolveComponentEvaluables(prevInstance)
}
context.debug(
`checking if component should be removed - operation: ${operation} instanceId: ${instanceId} nextInstance: ${get(
'name',
nextInstance
)} prevInstance: ${get('name', prevInstance)}`
)
validateNode(node, context)
if (contains(operation, ['remove', 'replace'])) {
if (operation === 'replace') {
if (prevInstance) {
await removeInstance(prevInstance, context)
const createPhoneNumber = async (twilio, params) => {
if (params.phoneNumber) {
const exists = await twilio.incomingPhoneNumbers.list({
phoneNumber: params.phoneNumber
})
if (!isEmpty(exists)) {
return updatePhoneNumber(twilio, {
...params,
sid: exists[0].sid
})
}
}
const phoneNumber = await twilio.incomingPhoneNumbers.create(params)
return pick(phoneNumberProps, phoneNumber)
}