Skip to content

Commit

Permalink
feat: micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Dec 10, 2023
1 parent f8a3523 commit 09f334e
Show file tree
Hide file tree
Showing 5 changed files with 819 additions and 45 deletions.
29 changes: 11 additions & 18 deletions packages/s3-object-response/__benchmarks__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,17 @@ import { Bench } from 'tinybench'
import middy from '../../core/index.js'
import middleware from '../index.js'

import sinon from 'sinon'
import { mockClient } from 'aws-sdk-client-mock'
import { S3Client, WriteGetObjectResponseCommand } from '@aws-sdk/client-s3'
import { PassThrough } from 'node:stream'
import https from 'node:https'
import mockFetch from 'fetch-mock'

const bench = new Bench({ time: 1_000 })

const context = {
getRemainingTimeInMillis: () => 30000
}

const mockHttps = (mockResponse) => {
const mockStream = new PassThrough()
mockStream.push(mockResponse)
mockStream.end()

https.request = () => mockStream

sinon.spy(mockStream, 'pipe')
return https
}
mockFetch.get('http://localhost', 200)
const setupHandler = (options = {}) => {
mockClient(S3Client)
.on(WriteGetObjectResponseCommand)
Expand All @@ -32,22 +21,26 @@ const setupHandler = (options = {}) => {
return middy(baseHandler).use(
middleware({
...options,
AwsClient: S3Client,
__https: mockHttps('hello world')
AwsClient: S3Client
})
)
}

const coldHandler = setupHandler({ cacheExpiry: 0 })
const coldHandler = setupHandler({ disablePrefetch: true })
const warmHandler = setupHandler()

const event = {
getObjectContext: {
inputS3Url: 'http://localhost'
}
}
await bench
.add('without cache', async (event = {}) => {
.add('without cache', async () => {
try {
await coldHandler(event, context)
} catch (e) {}
})
.add('with cache', async (event = {}) => {
.add('with cache', async () => {
try {
await warmHandler(event, context)
} catch (e) {}
Expand Down
16 changes: 5 additions & 11 deletions packages/s3-object-response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ const s3ObjectResponseMiddleware = (opts = {}) => {
}

const s3ObjectResponseMiddlewareBefore = async (request) => {
const { inputS3Url, outputRoute, outputToken } =
request.event.getObjectContext

request.internal.s3ObjectResponse = {
RequestRoute: outputRoute,
RequestToken: outputToken
}
const { inputS3Url } = request.event.getObjectContext

request.context.s3ObjectFetch = fetch(inputS3Url)
}
Expand All @@ -35,16 +29,16 @@ const s3ObjectResponseMiddleware = (opts = {}) => {
client = await createClient(options, request)
}

request.response.RequestRoute = request.event.getObjectContext.outputRoute
request.response.RequestToken = request.event.getObjectContext.outputToken

if (request.response.body) {
request.response.Body = request.response.body
delete request.response.body
}

await client.send(
new WriteGetObjectResponseCommand({
...request.internal.s3ObjectResponse,
...request.response
})
new WriteGetObjectResponseCommand(request.internal.s3ObjectResponse)
)

return { statusCode: 200 }
Expand Down
Loading

0 comments on commit 09f334e

Please sign in to comment.