Skip to content

Commit

Permalink
Migrating code to aws sdk v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tstibbs committed Jan 16, 2025
1 parent 07acee1 commit f47ef81
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
8 changes: 4 additions & 4 deletions backend/src/handlers/message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {aws} from '../utils.js'
import {ApiGatewayManagementApi} from '@aws-sdk/client-apigatewaymanagementapi'
import {getPoolId, getConnectionIdsInPool, deleteConnection} from '../persistance.js'
import {buildClientsCounterMessageFromEvent} from './client-count.js'
import {messageTypeClientJoined, messageTypeMulti} from '../../../ui-additions/src/constants.js'
Expand Down Expand Up @@ -27,8 +27,8 @@ export async function sendMessageToPoolId(event, poolId, message) {
}

async function sendMessageToConnections(event, connectionIds, message) {
const apigwManagementApi = new aws.ApiGatewayManagementApi({
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
const apigwManagementApi = new ApiGatewayManagementApi({
endpoint: 'https://' + event.requestContext.domainName + '/' + event.requestContext.stage
})

if (Array.isArray(message)) {
Expand All @@ -43,7 +43,7 @@ async function sendMessageToConnections(event, connectionIds, message) {
try {
await apigwManagementApi.postToConnection({ConnectionId: connectionId, Data: JSON.stringify(message)})
} catch (e) {
if (e.statusCode === 410) {
if (e['$metadata'].httpStatusCode === 410) {
console.log(`Found stale connection, deleting ${connectionId}`)
await deleteConnection(connectionId)
} else {
Expand Down
6 changes: 4 additions & 2 deletions backend/src/persistance.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {TABLE_NAME, aws} from './utils.js'
import {TABLE_NAME} from './utils.js'
import {TABLE_SCHEMA} from './constants.js'
import {DynamoDBDocument} from '@aws-sdk/lib-dynamodb'
import {DynamoDB} from '@aws-sdk/client-dynamodb'

const expirationTime = 1.5 * 24 * 60 * 60 //one and a half days in seconds

export const dydbClient = new aws.DynamoDB.DocumentClient()
export const dydbClient = DynamoDBDocument.from(new DynamoDB())

export async function deleteConnection(connectionId) {
const params = {
Expand Down
9 changes: 0 additions & 9 deletions backend/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import dotenv from 'dotenv'
import aws from 'aws-sdk'

dotenv.config()

aws.config.apiVersions = {
dynamodb: '2012-08-10',
apigatewaymanagementapi: '2018-11-29',
s3: '2006-03-01'
}

export {aws}

export const {TABLE_NAME, BUCKET} = process.env
4 changes: 2 additions & 2 deletions backend/tools/generate-pdf-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {strict as assert} from 'assert'

import {IndexGenerator} from '@tstibbs/cloud-core-utils/src/tools/generate-s3-index.js'
import {awsServiceConfig} from '@tstibbs/cloud-core-utils/src/tools/aws-client-config.js'
import {defaultAwsClientConfig} from '@tstibbs/cloud-core-utils/src/tools/aws-client-config.js'

import yargs from 'yargs'
import {hideBin} from 'yargs/helpers'
Expand Down Expand Up @@ -35,7 +35,7 @@ const {pdfSyncCredentialsProfile, localPath, basePath, websiteStackName, pdfSync

function buildApi(credentialsProfileName) {
let options = {
...awsServiceConfig('CloudFormation')
...defaultAwsClientConfig
}
if (credentialsProfileName != null) {
options.credentials = fromIni({
Expand Down

0 comments on commit f47ef81

Please sign in to comment.