Skip to content

Commit

Permalink
@aws-sdk/s3 / aws-sdk/clients/s3@aws-lite/s3
Browse files Browse the repository at this point in the history
Remove Node.js >= 18 conditional checks for a bunch of unit tests broken by AWS SDK v3
  • Loading branch information
ryanblock committed Dec 12, 2023
1 parent a8d3b24 commit 807f086
Show file tree
Hide file tree
Showing 8 changed files with 664 additions and 731 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"files": [
"src/*"
],
"dependencies": {
"@aws-lite/client": "~0.12.2",
"@aws-lite/s3": "~0.1.4"
},
"devDependencies": {
"@architect/eslint-config": "~2.1.2",
"@architect/req-res-fixtures": "git+https://github.com/architect/req-res-fixtures.git",
Expand Down
13 changes: 13 additions & 0 deletions src/lib/get-s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let s3

module.exports = async function getS3 () {
if (s3) return s3
// eslint-disable-next-line
let awsLite = require('@aws-lite/client')
s3 = await awsLite({
autoloadPlugins: false,
region: process.env.AWS_REGION || 'us-west-2',
plugins: [ '@aws-lite/s3' ],
})
return s3
}
1 change: 0 additions & 1 deletion src/lib/is-node-18.js

This file was deleted.

37 changes: 6 additions & 31 deletions src/read/_pretty.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
let _isNode18 = require('../lib/is-node-18')
let { existsSync, readdirSync, readFileSync, statSync } = require('fs')
let { join, parse } = require('path')
let getS3 = require('../lib/get-s3')
let { httpError } = require('../lib/error')

let s3
if (process.env.__TESTING__) {
// eslint-disable-next-line
let S3 = require('aws-sdk/clients/s3')
s3 = new S3
}

/**
* Peek into a dir without a trailing slash to see if it's got an index.html file
* If not, look for a custom 404.html
Expand All @@ -31,8 +24,7 @@ module.exports = async function pretty (params) {
return Key
}

// eslint-disable-next-line
async function getLocal (file) {
async function getLocal ({ Key: file }) {
if (!file.startsWith(sandboxPath)) {
file = join(sandboxPath, file)
}
Expand All @@ -53,30 +45,13 @@ module.exports = async function pretty (params) {
}
}

async function getS3 (Key) {
if (_isNode18) {
// eslint-disable-next-line
let { S3 } = require('@aws-sdk/client-s3')
let s3 = new S3({ region: process.env.AWS_REGION || 'us-west-2' })
return s3.getObject({ Bucket, Key })
}
else {
if (!process.env.__TESTING__) {
// eslint-disable-next-line
let S3 = require('aws-sdk/clients/s3')
s3 = new S3
}
return s3.getObject({ Bucket, Key }).promise()
}
}

async function get (file) {
let getter = local ? getLocal : getS3
async function get (Key) {
let getter = local ? getLocal : await getS3()
try {
return await getter(file)
return await getter({ Bucket, Key })
}
catch (err) {
if (err.name === 'NoSuchKey') {
if (err.name === 'NoSuchKey' || err.code === 'NoSuchKey') {
err.statusCode = 404
return err
}
Expand Down
32 changes: 4 additions & 28 deletions src/read/_s3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let { existsSync, readFileSync } = require('fs')
let { extname, join } = require('path')

let _isNode18 = require('../lib/is-node-18')
let getS3 = require('../lib/get-s3')
let _isHTMLorJSON = require('../lib/is-html-json')
let binaryTypes = require('../lib/binary-types')
let binaryExts = require('../lib/binary-extensions')
Expand Down Expand Up @@ -74,38 +74,14 @@ module.exports = async function readS3 (params) {
options.IfNoneMatch = IfNoneMatch
}

let method
if (_isNode18) {
// eslint-disable-next-line
let { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3')
let client = new S3Client({ region: process.env.AWS_REGION || 'us-west-2' })
method = async params => {
let command = new GetObjectCommand(params)
let res = await client.send(command)
let streamToString = stream => new Promise((resolve, reject) => {
let chunks = []
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks)))
})
let Body = await streamToString(res.Body)
return { ...res, ...{ Body } }
}
}
else {
// eslint-disable-next-line
let S3 = require('aws-sdk/clients/s3')
let s3 = new S3
method = params => s3.getObject(params).promise()
}

let s3 = await getS3()
let result
try {
result = await method(options)
result = await s3(options)
}
catch (err) {
// ETag matches (getObject error code of NotModified), so don't transit the whole file
if (err.code === 'NotModified' || err['$metadata']?.httpStatusCode === 304) {
if (err.code === 'NotModified' || err.statusCode === 304) {
matchedETag = true
headers.etag = IfNoneMatch
response = {
Expand Down
Loading

0 comments on commit 807f086

Please sign in to comment.