Skip to content

Commit

Permalink
refactor: conditionally install node-fetch globally vs. node.js v18+
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jun 9, 2022
1 parent b2a320d commit 0f2c313
Show file tree
Hide file tree
Showing 50 changed files with 23 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
env: {
es2021: true,
jest: true,
},

Expand All @@ -12,6 +13,11 @@ module.exports = {
],

globals: {
// TODO remove `node-fetch` module with node.js v18+ support
// TODO file bug with eslint? those should be global now
fetch: true,
Headers: true,
//
RUN_TEST_AGAINST_AWS: true,
TEST_BASE_URL: true,
},
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// TODO remove with node.js v16.9+ support
import 'object.hasown/auto'

// install global fetch
// TODO remove `node-fetch` module and use global built-in with node.js v18+ support
if (globalThis.fetch === undefined) {
const { default: fetch, Headers } = await import('node-fetch')
globalThis.fetch = fetch
globalThis.Headers = Headers
}

export { default } from './ServerlessOffline.js'
1 change: 0 additions & 1 deletion src/lambda/handler-runner/docker-runner/DockerContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Lambda } from 'aws-sdk'
import { execa } from 'execa'
import { ensureDir, pathExists } from 'fs-extra'
import jszip from 'jszip'
import fetch from 'node-fetch'
import pRetry from 'p-retry'
import DockerImage from './DockerImage.js'

Expand Down
1 change: 0 additions & 1 deletion src/lambda/handler-runner/java-runner/JavaRunner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EOL } from 'node:os'
import process from 'node:process'
import fetch from 'node-fetch'
import { log } from '@serverless/utils/log.js'
import { invokeJavaLocal } from 'java-invoke-local'

Expand Down
1 change: 0 additions & 1 deletion src/lambda/routes/invocations/invocationsRoute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Buffer } from 'node:buffer'
import { Headers } from 'node-fetch'
import InvocationsController from './InvocationsController.js'

const { parse } = JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
Expand Down
1 change: 0 additions & 1 deletion tests/endToEnd/optionParameters/optionParameters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
Expand Down
1 change: 0 additions & 1 deletion tests/endToEnd/starRoutes/starRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
Expand Down
1 change: 0 additions & 1 deletion tests/endToEnd/starRoutesWithProperties/starRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
Expand Down
1 change: 0 additions & 1 deletion tests/endToEnd/trailingSlash/trailingSlash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/artifact/local/localArtifact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
compressArtifact,
joinUrl,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/authorizer/authorizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/access-host/accessHost.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import { Server } from '@hapi/hapi'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/artifact/artifact.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import {
compressArtifact,
joinUrl,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/go/go1.x/dockerGo1.x.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import {
joinUrl,
buildInContainer,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/layers/dockerLayers.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/multiple/dockerMultiple.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import semver from 'semver'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import semver from 'semver'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import semver from 'semver'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import semver from 'semver'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

Expand Down
1 change: 0 additions & 1 deletion tests/integration/docker/provided/dockerProvided.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

// "Could not find 'Docker', skipping 'Docker' tests."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import semver from 'semver'
import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js'

Expand Down
1 change: 0 additions & 1 deletion tests/integration/go/go1.x/go1.x.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { platform } from 'node:os'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

const _describe =
Expand Down
1 change: 0 additions & 1 deletion tests/integration/groovy/groovy.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

// Could not find 'Java', skipping 'Java' tests.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/handler/handlerPayload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const { stringify } = JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
1 change: 0 additions & 1 deletion tests/integration/httpApi-cors/httpApi-cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
1 change: 0 additions & 1 deletion tests/integration/httpApi-headers/httpApi-headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
1 change: 0 additions & 1 deletion tests/integration/java/java.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

// Could not find 'Java', skipping 'Java' tests.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/jwt-authorizer/jwt-authorizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { randomBytes } from 'node:crypto'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import jsonwebtoken from 'jsonwebtoken'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

Expand Down
1 change: 0 additions & 1 deletion tests/integration/kotlin/kotlin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

// Could not find 'Java', skipping 'Java' tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const { stringify } = JSON
Expand Down
1 change: 0 additions & 1 deletion tests/integration/lambda-invoke/lambdaInvoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const { isArray } = Array
Expand Down
1 change: 0 additions & 1 deletion tests/integration/lambda-invoke/lambdaInvokeAsync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const { stringify } = JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { platform } from 'node:os'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

// skipping 'Python 3' tests on Windows for now.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/python/python3/python3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { platform } from 'node:os'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../../_testHelpers/index.js'

// skipping 'Python 3' tests on Windows for now.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/ruby/ruby.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

// Could not find 'Ruby', skipping 'Ruby' tests.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/scala/scala.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

// Could not find 'Java', skipping 'Java' tests.
Expand Down
1 change: 0 additions & 1 deletion tests/integration/uncategorized/uncategorized.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down
10 changes: 9 additions & 1 deletion tests/mochaGlobalSetup.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const { env } = require('node:process')

// can be sync or async
exports.mochaGlobalSetup = function mochaGlobalSetup() {
exports.mochaGlobalSetup = async function mochaGlobalSetup() {
// env.RUN_TEST_AGAINST_AWS = env.AWS_ENDPOINT != null
env.TEST_BASE_URL = env.AWS_ENDPOINT || 'http://localhost:3000'

// install global fetch
// TODO remove `node-fetch` module and use global built-in with node.js v18+ support
if (globalThis.fetch === undefined) {
const { default: fetch, Headers } = await import('node-fetch')
globalThis.fetch = fetch
globalThis.Headers = Headers
}
}
1 change: 0 additions & 1 deletion tests/scenario/apollo-server-lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import { joinUrl, setup, teardown } from '../integration/_testHelpers/index.js'
import installNpmModules from '../installNpmModules.js'

Expand Down
1 change: 0 additions & 1 deletion tests/scenario/docker-in-docker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert'
import { platform } from 'node:os'
import { env } from 'node:process'
import { execa } from 'execa'
import fetch from 'node-fetch'
import { compressArtifact, joinUrl } from '../integration/_testHelpers/index.js'

// TODO FIXME docker tests currently failing while using node: protocol
Expand Down
Loading

0 comments on commit 0f2c313

Please sign in to comment.