diff --git a/bench/benchmark-size.js b/bench/benchmark-size.js index 70ad3890..327421f7 100755 --- a/bench/benchmark-size.js +++ b/bench/benchmark-size.js @@ -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') diff --git a/bench/benchmark-speed.js b/bench/benchmark-speed.js index a3d97927..cb747194 100755 --- a/bench/benchmark-speed.js +++ b/bench/benchmark-speed.js @@ -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') diff --git a/package.json b/package.json index 855ca102..27dd8a82 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/plugins/cloudfront/test/xml-test.mjs b/plugins/cloudfront/test/xml-test.mjs index a0f3cb89..45a9d98b 100644 --- a/plugins/cloudfront/test/xml-test.mjs +++ b/plugins/cloudfront/test/xml-test.mjs @@ -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' diff --git a/plugins/s3/test/s3-test.js b/plugins/s3/test/s3-test.mjs similarity index 97% rename from plugins/s3/test/s3-test.js rename to plugins/s3/test/s3-test.mjs index 6ed8d48c..c7e3ec63 100644 --- a/plugins/s3/test/s3-test.js +++ b/plugins/s3/test/s3-test.mjs @@ -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' }) ] @@ -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') diff --git a/scripts/generate-plugins/_types.mjs b/scripts/generate-plugins/_types.mjs index ccd6f2d4..af69db84 100644 --- a/scripts/generate-plugins/_types.mjs +++ b/scripts/generate-plugins/_types.mjs @@ -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 diff --git a/scripts/get-regions.js b/scripts/get-regions.js index 03434fce..52048de5 100644 --- a/scripts/get-regions.js +++ b/scripts/get-regions.js @@ -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 diff --git a/scripts/jsr/copy-src-tree.mjs b/scripts/jsr/copy-src-tree.mjs new file mode 100755 index 00000000..1e5229c2 --- /dev/null +++ b/scripts/jsr/copy-src-tree.mjs @@ -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`) diff --git a/scripts/jsr/deno.sh b/scripts/jsr/deno.sh new file mode 100755 index 00000000..4fe0d100 --- /dev/null +++ b/scripts/jsr/deno.sh @@ -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 diff --git a/scripts/jsr/readme.md b/scripts/jsr/readme.md new file mode 100644 index 00000000..f7a27f29 --- /dev/null +++ b/scripts/jsr/readme.md @@ -0,0 +1,2 @@ +Deno + JSR support is still experimental +See: https://github.com/architect/aws-lite/issues/109 diff --git a/src/config/get-plugins.js b/src/config/get-plugins.js index b5e9d177..7c32210c 100644 --- a/src/config/get-plugins.js +++ b/src/config/get-plugins.js @@ -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) ] @@ -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) { @@ -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 */ diff --git a/src/lib/index.js b/src/lib/index.js index fab74020..18e0b6ef 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -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 } } @@ -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') @@ -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) @@ -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 diff --git a/src/request/request.js b/src/request/request.js index e4c4b22d..d32401d4 100644 --- a/src/request/request.js +++ b/src/request/request.js @@ -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 diff --git a/test/lib/index.js b/test/lib/index.mjs similarity index 96% rename from test/lib/index.js rename to test/lib/index.mjs index ea7aac63..57733821 100644 --- a/test/lib/index.js +++ b/test/lib/index.mjs @@ -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' @@ -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, @@ -126,7 +127,7 @@ function resetAWSEnvVars () { delete process.env.AWS_SHARED_CREDENTIALS_FILE } -module.exports = { +export { basicRequestChecks, copy, defaults, diff --git a/test/live/iam-lambda-test.js b/test/live/iam-lambda-test.js index 12c0b7bf..e44414f0 100644 --- a/test/live/iam-lambda-test.js +++ b/test/live/iam-lambda-test.js @@ -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() diff --git a/test/unit/plugins/plugins-test.js b/test/unit/plugins/plugins-test.mjs similarity index 85% rename from test/unit/plugins/plugins-test.js rename to test/unit/plugins/plugins-test.mjs index 83ada463..43e99f9b 100644 --- a/test/unit/plugins/plugins-test.js +++ b/test/unit/plugins/plugins-test.mjs @@ -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}`) diff --git a/test/unit/src/config/get-creds-test.js b/test/unit/src/config/get-creds-test.mjs similarity index 94% rename from test/unit/src/config/get-creds-test.js rename to test/unit/src/config/get-creds-test.mjs index ce619f8f..2eff19f1 100644 --- a/test/unit/src/config/get-creds-test.js +++ b/test/unit/src/config/get-creds-test.mjs @@ -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 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' @@ -14,8 +14,10 @@ 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') }) diff --git a/test/unit/src/config/get-endpoint-test.js b/test/unit/src/config/get-endpoint-test.mjs similarity index 95% rename from test/unit/src/config/get-endpoint-test.js rename to test/unit/src/config/get-endpoint-test.mjs index ac2d2a76..e12fad57 100644 --- a/test/unit/src/config/get-endpoint-test.js +++ b/test/unit/src/config/get-endpoint-test.mjs @@ -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') @@ -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') }) diff --git a/test/unit/src/config/get-plugins-test.js b/test/unit/src/config/get-plugins-test.mjs similarity index 74% rename from test/unit/src/config/get-plugins-test.js rename to test/unit/src/config/get-plugins-test.mjs index 89936c29..fcc69c00 100644 --- a/test/unit/src/config/get-plugins-test.js +++ b/test/unit/src/config/get-plugins-test.mjs @@ -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') }) @@ -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) ] }) diff --git a/test/unit/src/config/get-region-test.js b/test/unit/src/config/get-region-test.mjs similarity index 91% rename from test/unit/src/config/get-region-test.js rename to test/unit/src/config/get-region-test.mjs index bd2fb2f2..95c7eae2 100644 --- a/test/unit/src/config/get-region-test.js +++ b/test/unit/src/config/get-region-test.mjs @@ -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 getRegion let cwd = process.cwd() -let sut = join(cwd, 'src', 'config', 'get-region.js') -let getRegion = require(sut) - let { profile } = defaults let mock = join(cwd, 'test', 'mock') let east1 = 'us-east-1' @@ -15,8 +15,10 @@ let west2 = 'us-west-2' let num = 1 let configMock = join(mock, '.aws', 'config') -test('Set up env', t => { +test('Set up env', async t => { t.plan(1) + let sut = 'file://' + join(cwd, 'src', 'config', 'get-region.js') + getRegion = (await import(sut)).default t.ok(getRegion, 'getRegion module is present') }) diff --git a/test/unit/src/index-client-test.js b/test/unit/src/index-client-test.mjs similarity index 97% rename from test/unit/src/index-client-test.js rename to test/unit/src/index-client-test.mjs index 23c655fd..a8c21e6c 100644 --- a/test/unit/src/index-client-test.js +++ b/test/unit/src/index-client-test.mjs @@ -1,15 +1,17 @@ -let { join } = require('path') -let { Readable } = require('stream') -let qs = require('querystring') -let test = require('tape') -let { basicRequestChecks, copy, defaults, resetServer: reset, server } = require('../../lib') -let cwd = process.cwd() -let sut = join(cwd, 'src', 'index.js') -let client = require(sut) - +import { join } from 'node:path' +import { Readable } from 'node:stream' +import process from 'node:process' +import qs from 'node:querystring' +import { Buffer } from 'node:buffer' +import test from 'tape' +import { basicRequestChecks, copy, defaults, resetServer as reset, server } from '../../lib/index.mjs' +import url from 'node:url' + +let client let { badPort, config, host, protocol, service, path, port } = defaults let jsonHeaders = { 'content-type': 'application/json' } let xmlHeaders = { 'content-type': 'application/xml' } +let __filename = url.fileURLToPath(import.meta.url).replace(/\\/g, '/') /** * Reminder! @@ -18,6 +20,9 @@ let xmlHeaders = { 'content-type': 'application/xml' } 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 t.ok(client, 'aws-lite client is present') let started = await server.start() t.ok(started, 'Started server') diff --git a/test/unit/src/index-config-test.js b/test/unit/src/index-config-test.mjs similarity index 88% rename from test/unit/src/index-config-test.js rename to test/unit/src/index-config-test.mjs index 16b2a95b..a5a0f352 100644 --- a/test/unit/src/index-config-test.js +++ b/test/unit/src/index-config-test.mjs @@ -1,18 +1,22 @@ -let { join } = require('path') -let mockTmp = require('mock-tmp') -let test = require('tape') -let { basicRequestChecks, defaults, resetAWSEnvVars: reset, server } = require('../../lib') +import module from 'node:module' +import { join } from 'node:path' +import process from 'node:process' +import mockTmp from 'mock-tmp' +import test from 'tape' +import { basicRequestChecks, defaults, resetAWSEnvVars as reset, server } from '../../lib/index.mjs' + +let client let cwd = process.cwd() -let sut = join(cwd, 'src', 'index.js') -let client = require(sut) - let { accessKeyId, badPort, config, host, path, port, protocol, region, secretAccessKey, service } = defaults let profile1 = 'profile_1' let mock = join(cwd, 'test', 'mock') let pluginDir = join(mock, 'plugins') +let require = module.createRequire(import.meta.url) test('Set up env', async t => { t.plan(1) + let sut = 'file://' + join(cwd, 'src', 'index.js') + client = (await import(sut)).default t.ok(client, 'aws-lite client is present') }) @@ -53,22 +57,15 @@ test('Configuration - basic config', async t => { }) test('Configuration - plugin loading', async t => { - t.plan(6) + t.plan(9) let aws, tmp let minConfig = { accessKeyId, secretAccessKey, region } - // 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(9) - // eslint-disable-next-line - aws = await client({ ...minConfig, plugins: [ import('@aws-lite/ssm') ] }) - t.ok(aws.ssm, 'Client explicitly loaded ESM plugin with unresolved import') - - // eslint-disable-next-line - aws = await client({ ...minConfig, plugins: [ await import('@aws-lite/sqs') ] }) - t.ok(aws.sqs, 'Client explicitly loaded ESM plugin with resolved import') - } + aws = await client({ ...minConfig, plugins: [ import('@aws-lite/ssm') ] }) + t.ok(aws.ssm, 'Client explicitly loaded ESM plugin with unresolved import') + + aws = await client({ ...minConfig, plugins: [ await import('@aws-lite/sqs') ] }) + t.ok(aws.sqs, 'Client explicitly loaded ESM plugin with resolved import') let cjsPluginPath = join(pluginDir, 'cjs') aws = await client({ ...minConfig, plugins: [ require(cjsPluginPath) ] }) @@ -78,11 +75,8 @@ test('Configuration - plugin loading', async t => { aws = await client({ ...minConfig, plugins: [ plugin ] }) t.ok(aws.lambda, 'Client explicitly loaded CJS plugin object') - // See above - if (!process.versions.node.startsWith('14')) { - aws = await client({ ...minConfig, autoloadPlugins: true }) - t.ok(aws.dynamodb, 'Client auto-loaded @aws-lite/dynamodb') - } + aws = await client({ ...minConfig, autoloadPlugins: true }) + t.ok(aws.dynamodb, 'Client auto-loaded @aws-lite/dynamodb') aws = await client({ ...minConfig }) t.notOk(aws.dynamodb, 'Client did not auto-load @aws-lite/dynamodb') diff --git a/test/unit/src/index-plugins-test.js b/test/unit/src/index-plugins-test.mjs similarity index 92% rename from test/unit/src/index-plugins-test.js rename to test/unit/src/index-plugins-test.mjs index 02e03a7c..832bb3db 100644 --- a/test/unit/src/index-plugins-test.js +++ b/test/unit/src/index-plugins-test.mjs @@ -1,18 +1,24 @@ -let { join } = require('path') -let test = require('tape') -let { basicRequestChecks, defaults, resetServer: reset, server } = require('../../lib') +import module from 'node:module' +import { join } from 'node:path' +import process from 'node:process' +import url from 'node:url' +import test from 'tape' +import { basicRequestChecks, defaults, resetServer as reset, server } from '../../lib/index.mjs' + +let client let cwd = process.cwd() -let sut = join(cwd, 'src', 'index.js') -let client = require(sut) - let { badPort, config, host, path, protocol, service, port } = defaults let mock = join(cwd, 'test', 'mock') let pluginDir = join(mock, 'plugins') let invalidPlugins = join(pluginDir, 'invalid') let p = path => process.platform.startsWith('win') ? 'file://' + path : path +let __filename = url.fileURLToPath(import.meta.url).replace(/\\/g, '/') +let require = module.createRequire(import.meta.url) test('Set up env', async t => { t.plan(2) + let sut = 'file://' + join(cwd, 'src', 'index.js') + client = (await import(sut)).default t.ok(client, 'aws-lite client is present') let started = await server.start() t.ok(started, 'Started server') @@ -182,7 +188,7 @@ test('Plugins - validate input', async t => { test('Plugins - validate service', async t => { t.plan(2) - let plugin = require(join(pluginDir, 'misc', 'unverified-service.js')) + let plugin = import(p(join(pluginDir, 'misc', 'unverified-service.js'))) try { await client({ ...config, plugins: [ plugin ] }) t.fail('Plugin with unverified service should throw') @@ -346,7 +352,7 @@ test('Plugins - error(), error handling', async t => { let responseBody, responseHeaders, responseStatusCode let errorsPlugin = join(pluginDir, 'error.js') - let aws = await client({ ...config, plugins: [ require(errorsPlugin) ] }) + let aws = await client({ ...config, plugins: [ import(p(errorsPlugin)) ] }) // Control try { @@ -508,7 +514,7 @@ test('Plugins - error docs (@aws-lite)', async t => { test('Plugins - disabled methods', async t => { t.plan(3) - let aws = await client({ ...config, plugins: [ require(join(pluginDir, 'misc', 'disabled-methods')) ] }) + let aws = await client({ ...config, plugins: [ import(p(join(pluginDir, 'misc', 'disabled-methods.js'))) ] }) t.ok(aws.lambda.ok, 'Client loaded plugin containing disabled methods') t.notOk(aws.lambda.disabledByFalsy, 'Client did not load method disabled by boolean false') t.notOk(aws.lambda.disabledByParam, `Client did not load method disabled by 'disabled' param`) @@ -545,7 +551,7 @@ test('Plugins - plugin validation', async t => { // Failures try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-request-method.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-request-method.js'))) ] }) } catch (err) { t.match(err.message, /All plugin request methods must be a function/, 'Throw on invalid request method') @@ -553,7 +559,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-response-method.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-response-method.js'))) ] }) } catch (err) { t.match(err.message, /All plugin response methods must be a function/, 'Throw on invalid response method') @@ -561,7 +567,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-error-method.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-error-method.js'))) ] }) } catch (err) { t.match(err.message, /All plugin error methods must be a function/, 'Throw on invalid error method') @@ -569,7 +575,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-service.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-service.js'))) ] }) } catch (err) { t.match(err.message, /Invalid AWS service specified: lolidk/, 'Throw on invalid service') @@ -577,7 +583,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'this-plugin-does-not-exist.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'this-plugin-does-not-exist.js'))) ] }) } catch (err) { t.match(err.message, /Cannot find module/, 'Throw on missing plugin') @@ -585,7 +591,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-plugin.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-plugin.js'))) ] }) } catch (err) { t.match(err.message, /lol is not defined/, 'Throw on invalid plugin') @@ -593,7 +599,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-methods-type')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-methods-type.js'))) ] }) } catch (err) { t.match(err.message, /Plugin must export a methods object/, 'Throw on missing methods') @@ -601,7 +607,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'invalid-methods-missing')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'invalid-methods-missing.js'))) ] }) } catch (err) { t.match(err.message, /Plugin must export a methods object/, 'Throw on missing methods') @@ -609,7 +615,7 @@ test('Plugins - plugin validation', async t => { } try { - await client({ ...config, plugins: [ require(join(invalidPlugins, 'this-plugin-does-not-exist.js')) ] }) + await client({ ...config, plugins: [ import(p(join(invalidPlugins, 'this-plugin-does-not-exist.js'))) ] }) } catch (err) { t.match(err.message, /Cannot find module/, 'Throw on missing plugin') diff --git a/test/unit/src/index-retries-test.js b/test/unit/src/index-retries-test.mjs similarity index 89% rename from test/unit/src/index-retries-test.js rename to test/unit/src/index-retries-test.mjs index cdc3193a..b773f7c7 100644 --- a/test/unit/src/index-retries-test.js +++ b/test/unit/src/index-retries-test.mjs @@ -1,11 +1,10 @@ -let { join } = require('path') -let http = require('http') -let test = require('tape') -let { defaults } = require('../../lib') -let cwd = process.cwd() -let sut = join(cwd, 'src', 'index.js') -let client = require(sut) +import http from 'node:http' +import { join } from 'node:path' +import process from 'node:process' +import test from 'tape' +import { defaults } from '../../lib/index.mjs' +let client let { config, service, port } = defaults let retryServer, serverError @@ -23,6 +22,9 @@ function reset () { 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 t.ok(client, 'aws-lite client is present') retryServer = http.createServer((req, res) => { req.on('data', () => {}) @@ -62,6 +64,7 @@ test('Retries', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.equal(requests.length, 1, 'Client did not retry') } @@ -74,6 +77,7 @@ test('Retries', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.equal(requests.length, 1, 'Client did not retry') } @@ -87,6 +91,7 @@ test('Retries', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.equal(requests.length, retries + 1, 'Client retried, passed through error') } @@ -117,6 +122,7 @@ test('Retries', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.equal(requests.length, retries + 1, 'Client retried, passed through error') } @@ -132,6 +138,7 @@ test('Retries', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.equal(requests.length, retries + 1, 'Client retried, passed through error') } }) @@ -147,6 +154,7 @@ test('Retries - validation', async t => { t.fail('Expected an error') } catch (err) { + console.log(err) t.match(err.message, /must a number/, 'Errored on retries string value') } }) diff --git a/test/unit/src/index-testing-test.js b/test/unit/src/index-testing-test.mjs similarity index 98% rename from test/unit/src/index-testing-test.js rename to test/unit/src/index-testing-test.mjs index 90165b73..7d56ddf2 100644 --- a/test/unit/src/index-testing-test.js +++ b/test/unit/src/index-testing-test.mjs @@ -1,10 +1,9 @@ -let { join } = require('path') -let test = require('tape') -let { copy, defaults } = require('../../lib') -let cwd = process.cwd() -let sut = join(cwd, 'src', 'index.js') -let client = require(sut) +import { join } from 'node:path' +import process from 'node:process' +import test from 'tape' +import { copy, defaults } from '../../lib/index.mjs' +let client let { config } = defaults let jsonHeaders = { 'content-type': 'application/json' } let rando = () => (Math.random() + 1).toString(36).substring(2) @@ -25,6 +24,9 @@ let mockErr = { test('Set up env', async t => { t.plan(1) + let cwd = process.cwd() + let sut = 'file://' + join(cwd, 'src', 'index.js') + client = (await import(sut)).default t.ok(client, 'aws-lite client is present') }) diff --git a/test/unit/src/lib/index-test.js b/test/unit/src/lib/index-test.mjs similarity index 76% rename from test/unit/src/lib/index-test.js rename to test/unit/src/lib/index-test.mjs index 156938aa..5a2f67bd 100644 --- a/test/unit/src/lib/index-test.js +++ b/test/unit/src/lib/index-test.mjs @@ -1,18 +1,19 @@ -let { join } = require('path') -let test = require('tape') -let cwd = process.cwd() -let sut = join(cwd, 'src', 'lib', 'index.js') -let lib = require(sut) -let { useAWS } = lib +import { join } from 'node:path' +import test from 'tape' +let useAWS function reset () { delete process.env.ARC_ENV delete process.env.ARC_LOCAL delete process.env.ARC_SANDBOX } -test('Set up env', t => { +test('Set up env', async t => { t.plan(1) + let cwd = process.cwd() + let sut = 'file://' + join(cwd, 'src', 'lib', 'index.js') + let lib = await import(sut) + useAWS = lib.useAWS t.ok(useAWS, 'useAWS util is present') })