Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSR #108

Merged
merged 6 commits into from
Mar 4, 2024
Merged

JSR #108

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bench/benchmark-size.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env node
let { mkdirSync, rmSync, statSync, writeFileSync } = require('fs')
let { join } = require('path')
let { execSync } = require('child_process')
let { mkdirSync, rmSync, statSync, writeFileSync } = require('node:fs')
let { join } = require('node:path')
let { execSync } = require('node:child_process')
let { build } = require('esbuild')
let folderSize = require('fast-folder-size/sync')

Expand Down
6 changes: 3 additions & 3 deletions bench/benchmark-speed.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /usr/bin/env node

let { join } = require('path')
let { execSync } = require('child_process')
let { readFileSync } = require('fs')
let { join } = require('node:path')
let { execSync } = require('node:child_process')
let { readFileSync } = require('node:fs')
let percentile = require('percentile')
let { formatSize, roundHalf } = require('./_helpers')

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"coverage": "nyc --reporter=lcov --reporter=text npm run test:unit",
"lint": "eslint --fix .",
"test:live": "cross-env tape 'test/live/**/*-test.js' | tap-arc",
"test:plugins": "cross-env tape 'plugins/**/test/**/*-test.*js' | tap-arc",
"test:plugins": "cross-env tape 'plugins/**/test/**/*-test.mjs' | tap-arc",
"test:precommit": "cross-env PRECOMMIT=true npm run gen && npm run lint",
"test:unit": "cross-env tape 'test/unit/**/*-test.js' | tap-arc",
"test:unit": "cross-env tape 'test/unit/**/*-test.mjs' | tap-arc",
"test": "npm run lint && npm run gen && npm run test:plugins && npm run coverage"
},
"simple-git-hooks": {
Expand Down
4 changes: 2 additions & 2 deletions plugins/cloudfront/test/xml-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import url from 'url'
import { join } from 'path'
import url from 'node:url'
import { join } from 'node:path'
import test from 'tape'
import { readFile } from 'node:fs/promises'
import { parseXML, buildXML } from '../../../src/lib/index.js'
Expand Down
18 changes: 9 additions & 9 deletions plugins/s3/test/s3-test.js → plugins/s3/test/s3-test.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
let { join } = require('path')
let cwd = process.cwd()
let sut = join(cwd, 'src', 'index.js')
let client = require(sut)
let test = require('tape')
let mockTmp = require('mock-tmp')
let { parseXML } = require('../../../src/lib')
let { defaults } = require('../../../test/lib')
import { join } from 'node:path'
import test from 'tape'
import mockTmp from 'mock-tmp'
import { parseXML } from '../../../src/lib/index.js'
import { defaults } from '../../../test/lib/index.mjs'
let { config } = defaults

let aws, tmp
let aws, client, tmp
let bucketName = 'bucket1'
let objectNames = [ 'object1.txt', 'object2.json' ]
let objectContents = [ 'Hello, World!', JSON.stringify({ welcome: 'aws-lite' }) ]
Expand All @@ -24,6 +21,9 @@ let okXml = {

test('Set up env', async t => {
t.plan(2)
let cwd = process.cwd()
let sut = 'file://' + join(cwd, 'src', 'index.js')
client = (await import(sut)).default
client.testing.enable({ usePluginResponseMethod: true })
aws = await client({ ...config, plugins: [ import('@aws-lite/s3') ] })
t.ok(aws, 'Client ready')
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-plugins/_types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default async function main ({ service, property, display }) {
const packageName = `@aws-lite/${service}`
const pluginTypesDir = join(CWD, 'plugins', service, 'types')

const { methods } = (await import(join(CWD, 'plugins', service, 'src', 'index.mjs'))).default
const { methods } = (await import('file://' + join(CWD, 'plugins', service, 'src', 'index.mjs'))).default

if (!existsSync(pluginTypesDir)) {
// new plugin types package - this only happens once
Expand Down
4 changes: 2 additions & 2 deletions scripts/get-regions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let { join } = require('path')
let { writeFileSync } = require('fs')
let { join } = require('node:path')
let { writeFileSync } = require('node:fs')
let awsLite = require('../')

// Get the list of current AWS regions
Expand Down
40 changes: 40 additions & 0 deletions scripts/jsr/copy-src-tree.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /usr/bin/env node

import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import pluginList from '../../plugins.mjs'

const start = Date.now()
const cwd = process.cwd()
const denoDir = join(cwd, 'node_modules', '.deno')
if (!existsSync(denoDir)) {
throw Error('Deno has not yet installed dependencies for the test run')
}

const deps = readdirSync(denoDir)

const existingAwsLiteDirs = deps.filter(i => i.startsWith('@aws-lite+'))
if (existingAwsLiteDirs.length) {
existingAwsLiteDirs.forEach(dir => {
rmSync(join(denoDir, dir), { recursive: true, force: true })
})
}

const dirs = [
cwd,
...pluginList.map(({ service }) => join(cwd, 'plugins', service))
]
dirs.forEach(dir => {
const pkg = JSON.parse(readFileSync(join(dir, 'package.json')))
const name = pkg.name.replace('/', '+')
const ver = pkg.version.split('-')[0]
const verDir = join(denoDir, `${name}@${ver}`)

const dest = join(verDir, 'node_modules', '@aws-lite', pkg.name.split('/')[1])
mkdirSync(dest, { recursive: true })
writeFileSync(join(verDir, '.initialized'), '')

cpSync(join(dir, 'package.json'), join(dest, 'package.json'))
cpSync(join(dir, 'src'), join(dest, 'src'), { recursive: true })
})
console.error(`Prepared ${dirs.length} Deno node_modules dirs in ${Date.now() - start} ms`)
3 changes: 3 additions & 0 deletions scripts/jsr/deno.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/scripts/copy-src-tree.mjs

# deno run --allow-read --allow-write --allow-env --allow-net --node-modules-dir test/unit/src/whatever.mjs | tap-arc
2 changes: 2 additions & 0 deletions scripts/jsr/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deno + JSR support is still experimental
See: https://github.com/architect/aws-lite/issues/109
8 changes: 4 additions & 4 deletions src/config/get-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = async function getPlugin (config) {
/* istanbul ignore else */
if (autoloadPlugins) {
let { exists } = require('../lib')
let { join } = require('path')
let { join } = require('node:path')

let dedupe = arr => [ ...new Set(arr) ]

Expand Down Expand Up @@ -65,7 +65,7 @@ module.exports = async function getPlugin (config) {

// Perhaps the least reliable due to the likelihood of second-order deps: read the package.json (if possible)
else if (await exists(packageJsonFile)) {
let { readFile } = require('fs/promises')
let { readFile } = require('node:fs/promises')
let packageJson = JSON.parse(await readFile(packageJsonFile))
let { dependencies: deps } = packageJson
if (deps) {
Expand Down Expand Up @@ -116,8 +116,8 @@ let tidy = p => !ignored.includes(p) && !p.endsWith('-types')

async function scanNodeModulesDir (dir) {
let found = []
let { join } = require('path')
let { readdir } = require('fs/promises')
let { join } = require('node:path')
let { readdir } = require('node:fs/promises')
let mods = await readdir(dir)
// Find first-party plugins
/* istanbul ignore next: TODO code path not run in 14.x tests, remove once deprecated */
Expand Down
10 changes: 5 additions & 5 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let awsjson = {
}

async function exists (file) {
let { stat } = require('fs/promises')
let { stat } = require('node:fs/promises')
try { await stat(file); return true }
catch { return false }
}
Expand Down Expand Up @@ -86,8 +86,8 @@ async function loadAwsConfig (params) {
let { AWS_SDK_LOAD_CONFIG, AWS_CONFIG_FILE } = process.env
if (!AWS_SDK_LOAD_CONFIG && !awsConfigFile) return

let { join } = require('path')
let os = require('os')
let { join } = require('node:path')
let os = require('node:os')
let home = os.homedir()

let configFile = AWS_CONFIG_FILE || join(home, '.aws', 'config')
Expand All @@ -100,7 +100,7 @@ async function readConfig (file) {
if (cache[file]) return cache[file]
if (!(await exists(file))) return

let { readFile } = require('fs/promises')
let { readFile } = require('node:fs/promises')
if (!ini) ini = require('ini')

let data = await readFile(file)
Expand All @@ -110,7 +110,7 @@ async function readConfig (file) {
}

function tidyQuery (obj) {
let qs = require('querystring')
let qs = require('node:querystring')
let tidied = {}
Object.entries(obj).forEach(([ k, v ]) => {
// Who knows, maybe there's an API service that uses boolean query string params
Expand Down
2 changes: 1 addition & 1 deletion src/request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function call (params, args, retrying) {
}

/* istanbul ignore next */
let http = isHTTPS ? require('https') : require('http')
let http = isHTTPS ? require('node:https') : require('node:http')

// Port configuration
options.port = params.port || config.port
Expand Down
9 changes: 5 additions & 4 deletions test/lib/index.js → test/lib/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let os = require('os')
const http = require('http')
import os from 'node:os'
import http from 'node:http'
import process from 'node:process'

// Test defaults
const accessKeyId = 'foo'
Expand Down Expand Up @@ -35,7 +36,7 @@ let server = {
const reqType = req.headers?.['content-type']
/**/ if (reqType?.includes('json')) body = JSON.parse(data)
else if (reqType?.includes('xml')) body = data.toString()
else body = Buffer.concat(data)
else body = data.join('')
}
serverData.request = {
url: req.url,
Expand Down Expand Up @@ -126,7 +127,7 @@ function resetAWSEnvVars () {
delete process.env.AWS_SHARED_CREDENTIALS_FILE
}

module.exports = {
export {
basicRequestChecks,
copy,
defaults,
Expand Down
2 changes: 1 addition & 1 deletion test/live/iam-lambda-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { join } = require('path')
let { join } = require('node:path')
let test = require('tape')
let Zip = require('adm-zip')
let cwd = process.cwd()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
let { join } = require('path')
let test = require('tape')
import { join } from 'node:path'
import process from 'node:process'
import test from 'tape'

let cwd = process.cwd()
let validTypes = [ 'array', 'boolean', 'buffer', 'number', 'object', 'string' ]
let plugins

test('Set up env', async t => {
t.plan(1)
plugins = (await import('../../../plugins.mjs')).default
plugins = (await import('file://' + join(cwd, 'plugins.mjs'))).default
t.ok(plugins.length, `Loaded ${plugins.length} \`@aws-lite/*\` plugins`)
})

test('Check plugins for docs + validation', async t => {
let plan = 0
for (let { service } of plugins) {
let prefix = process.platform.startsWith('win') ? 'file://' : ''
let path = service => prefix + join(cwd, 'plugins', service, 'src', 'index.mjs')
let path = service => 'file://' + join(cwd, 'plugins', service, 'src', 'index.mjs')
let plugin = (await import(path(service))).default
t.comment(`@aws-lite/${service}`)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
let { readFileSync } = require('fs')
let { join } = require('path')
let test = require('tape')
let mockTmp = require('mock-tmp')
let { defaults, overrideHomedir, resetAWSEnvVars } = require('../../../lib')
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import test from 'tape'
import mockTmp from 'mock-tmp'
import { defaults, overrideHomedir, resetAWSEnvVars } from '../../../lib/index.mjs'

let getCreds
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-creds.js')
let getCreds = require(sut)

let { profile } = defaults
let mock = join(cwd, 'test', 'mock')
let ok = 'foo'
let nope = 'bar'
let num = 1
let credentialsMock = join(mock, '.aws', 'credentials')

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-creds.js')
getCreds = (await import(sut)).default
t.ok(getCreds, 'getCreds module is present')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let { readFileSync } = require('fs')
let { join } = require('path')
let test = require('tape')
let mockTmp = require('mock-tmp')
let { defaults, overrideHomedir, resetAWSEnvVars } = require('../../../lib')
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import test from 'tape'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import { defaults, overrideHomedir, resetAWSEnvVars } from '../../../lib/index.mjs'

let getEndpoint
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-endpoint.js')
let getEndpoint = require(sut)

let { profile } = defaults
let mock = join(cwd, 'test', 'mock')
let configMock = join(mock, '.aws', 'config')
Expand All @@ -18,8 +18,10 @@ let https = 'https:', http = 'http:'
let pathPrefix = '/foo-bar'
let protocol = https // https is the sensible default!

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-endpoint.js')
getEndpoint = (await import(sut)).default
t.ok(getEndpoint, 'getEndpoint module is present')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
let { readFileSync } = require('fs')
let { join } = require('path')
let test = require('tape')
let mockTmp = require('mock-tmp')
import { readFileSync } from 'node:fs'
import module from 'node:module'
import { join } from 'node:path'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import test from 'tape'

let getPlugins
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-plugins.js')
let getPlugins = require(sut)

let require = module.createRequire(import.meta.url)
let mock = join(cwd, 'test', 'mock')
let pluginDir = join(mock, 'plugins')

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-plugins.js')
getPlugins = (await import(sut)).default
t.ok(getPlugins, 'getPlugins module is present')
})

Expand All @@ -20,21 +24,14 @@ test('Just return an empty array ', async t => {
})

test('Load plugins array', async t => {
t.plan(2)
t.plan(4)
let plugins

// Node.js 14.x + npm 6 does funky things with npm link-ed (symlinked) modules
// That's cool, we can confidently skip this test for now, the related code path provably works!
if (!process.versions.node.startsWith('14')) {
t.plan(4)
// eslint-disable-next-line
plugins = await getPlugins({ plugins: [ import('@aws-lite/dynamodb') ] })
t.equal(plugins[0].service, 'dynamodb', 'Client explicitly loaded ESM plugin with unresolved import')

// eslint-disable-next-line
plugins = await getPlugins({ plugins: [ await import('@aws-lite/lambda') ] })
t.equal(plugins[0].service, 'lambda', 'Client explicitly loaded ESM plugin with resolved import')
}
plugins = await getPlugins({ plugins: [ import('@aws-lite/dynamodb') ] })
t.equal(plugins[0].service, 'dynamodb', 'Client explicitly loaded ESM plugin with unresolved import')

plugins = await getPlugins({ plugins: [ await import('@aws-lite/lambda') ] })
t.equal(plugins[0].service, 'lambda', 'Client explicitly loaded ESM plugin with resolved import')

let cjsPluginPath = join(pluginDir, 'cjs')
plugins = await getPlugins({ plugins: [ require(cjsPluginPath) ] })
Expand Down
Loading
Loading