diff --git a/.github/workflows/ci.yml b/.github/workflows/branch.yml similarity index 74% rename from .github/workflows/ci.yml rename to .github/workflows/branch.yml index 620e4a0c..5a974b53 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/branch.yml @@ -1,20 +1,25 @@ -name: Main Branch +name: v3.x branch on: push: - branches: [ main ] - tags: - - v[0-9]+.[0-9]+.[0-9]+ + branches: + - v3.x jobs: + debug: + name: Debug + runs-on: ubuntu-latest + steps: + - uses: hmarr/debug-action@v3 + install: name: Install runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Cache node_modules id: cacheModules - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} @@ -34,8 +39,8 @@ jobs: needs: [install] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 + - uses: actions/checkout@v4 + - uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} @@ -43,9 +48,3 @@ jobs: - run: make secure env: SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} - - release: - name: Release - if: startsWith(github.event.ref, 'refs/tags/') - needs: [checks, security] - uses: ./.github/workflows/release.yml diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 7e4ae0fb..3ee5e0d1 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -1,10 +1,10 @@ -name: Checks +name: v3.x checks on: [workflow_call] jobs: - test: - name: Test specs + test-node: + name: Test Specs runs-on: ubuntu-latest strategy: fail-fast: false @@ -12,13 +12,13 @@ jobs: node-version: [10.x, 12.x, 14.x, 16.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 + - uses: actions/checkout@v4 + - uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install --prefer-offline @@ -28,8 +28,8 @@ jobs: name: Code Lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 + - uses: actions/checkout@v4 + - uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} @@ -40,8 +40,8 @@ jobs: name: Types runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 + - uses: actions/checkout@v4 + - uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 8938b4d7..ada3e811 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -1,4 +1,4 @@ -name: Pull Request +name: v3.x pull requests on: pull_request_target: @@ -6,8 +6,16 @@ on: - opened - edited - synchronize + branches: + - v3.x jobs: + debug: + name: Debug + runs-on: ubuntu-latest + steps: + - uses: hmarr/debug-action@v3 + prlint: name: Validate PR title runs-on: ubuntu-latest @@ -20,10 +28,10 @@ jobs: name: Install runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Cache node_modules id: cacheModules - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 36205732..dfd363ad 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,44 +1,77 @@ -name: Release +name: v3.x releases -on: [workflow_call] +on: + push: + branches: + # Pushes to the branch below will test the release workflow without + # publishing version on npm or generating new git tags + - v3.x-test + tags: + - 'v3.[0-9]+.[0-9]+' + - 'v3.[0-9]+.[0-9]+-alpha.[0-9]+' + - 'v3.[0-9]+.[0-9]+-beta.[0-9]+' jobs: - npm: - name: Publish to NPM registry + debug: + name: Debug runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - # Setup .npmrc file to publish to npm - - uses: actions/setup-node@v3 - with: - node-version: '10' - registry-url: 'https://registry.npmjs.org' - - uses: actions/cache@v3 + - uses: hmarr/debug-action@v3 + + install: + name: Install + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Cache node_modules + id: cacheModules + uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} + - name: Install dependencies + if: steps.cacheModules.outputs.cache-hit != 'true' + run: npm install + + checks: + name: Check + needs: [install] + uses: ./.github/workflows/checks.yml + + # The security job can't run on pull requests opened from forks because + # Github doesn't pass down the SNYK_TOKEN environment variable. + security: + name: Check Security + needs: [install] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + # This is cache where npm installs from before going out to the network + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} - run: npm install --prefer-offline - - run: npm publish + - run: make secure env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} - github: - name: Publish to Github registry + publish: + name: Publish to NPM registry runs-on: ubuntu-latest + needs: [checks, security] steps: - - uses: actions/checkout@v3 - # Setup .npmrc file to publish to GitHub Packages - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: - node-version: '10' - registry-url: 'https://npm.pkg.github.com' - - uses: actions/cache@v3 + node-version-file: '.nvmrc' + # Setup .npmrc file to publish to npm + registry-url: 'https://registry.npmjs.org' + - uses: actions/cache@v4 with: path: ~/.npm # this is cache where npm installs from before going out to the network key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} - run: npm install --prefer-offline - # The package name needs to be scoped in order to publish on Github Registry - - run: sed -i 's/"cross-fetch"/"@lquixada\/cross-fetch"/g' package.json - - run: npm publish + - run: ./bin/release env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.nvmrc b/.nvmrc index 295bfdbe..b6a7d89c 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -10.19 +16 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..1db95cbb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,36 @@ +# Changelog + +All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +## [3.2.0](https://github.com/lquixada/cross-fetch/compare/v3.1.8...v3.2.0) (2024-12-21) + + +### Features + +* updated node-fetch to 2.7.0 ([#191](https://github.com/lquixada/cross-fetch/issues/191)) ([ebf44c3](https://github.com/lquixada/cross-fetch/commit/ebf44c303c2dbda25f0f1f7e5af814ee038c280c)) + + +### Bug Fixes + +* updated whatwg-fetch to 3.6.20 ([#198](https://github.com/lquixada/cross-fetch/issues/198)) ([fbbecc8](https://github.com/lquixada/cross-fetch/commit/fbbecc86ec558e704682c822c53e9d48b0851141)) + +### [3.1.8](https://github.com/lquixada/cross-fetch/compare/v3.1.7...v3.1.8) (2023-07-02) + + +### Bug Fixes + +* restored caret on node-fetch version ([6669927](https://github.com/lquixada/cross-fetch/commit/666992742ef7cbc9a405189f05e0bea4ebbe11fa)) + +### [3.1.7](https://github.com/lquixada/cross-fetch/compare/v3.1.7-test.1...v3.1.7) (2023-07-01) + +### [3.1.6](https://github.com/lquixada/cross-fetch/compare/v3.1.5...v3.1.6) (2023-05-14) + + +### Features + +* allowed minor and patch update of node-fetch ([#132](https://github.com/lquixada/cross-fetch/issues/132)) ([425395b](https://github.com/lquixada/cross-fetch/commit/425395ba80c04fee670e0fc3b8e81ca416ff6e1a)), closes [#129](https://github.com/lquixada/cross-fetch/issues/129) + + +### Bug Fixes + +* fixed ESTree.StaticBlock error ([a66f21b](https://github.com/lquixada/cross-fetch/commit/a66f21bc3aec60907f5fa61efb1ca3af17f17550)) diff --git a/Makefile b/Makefile index 5ba35365..26493be1 100644 --- a/Makefile +++ b/Makefile @@ -1,69 +1,132 @@ +.PHONY: all all: test lint typecheck -node_modules: package.json - npm install && /usr/bin/touch node_modules - -build: - npx rollup -c - +.PHONY: browser browser: - ./bin/server --exec "npx open-cli http://localhost:8000/test/fetch-api/browser/" - -commit: - npx cz + @./bin/server --exec "npx open-cli http://localhost:8000/test/fetch-api/browser/" -commitlint: node_modules - npx commitlint --from origin/main --to HEAD --verbose +.PHONY: commit +commit: node_modules + @npx cz -compile: test/fetch-api/api.spec.ts - npx tsc +.PHONY: release +release: node_modules + @npx standard-version -cov: - npx nyc report --reporter=text-lcov > .reports/coverage.lcov && npx codecov +.PHONY: release-alpha +release-alpha: node_modules + @npx standard-version --prerelease alpha -lint: - npx standard +## +# Builds -release: - npx standard-version +node_modules: package.json + @echo "" + @echo "=> installing dependencies..." + @npm install && /usr/bin/touch node_modules -release-alpha: - npx standard-version --prerelease alpha +dist: package.json rollup.config.js $(wildcard src/*.js) node_modules + @echo "" + @echo "=> make $@" + @npx rollup -c -secure: - npx snyk test +test/fetch-api/api.spec.js: node_modules test/fetch-api/api.spec.ts + @echo "" + @echo "=> compiling Typescript files..." + @npx tsc -test: compile test-fetch test-module +## +# Checks +.PHONY: commitlint +commitlint: node_modules + @echo "" + @echo "=> linting commits..." + @npx commitlint --from origin/main --to HEAD --verbose + +.PHONY: cov +cov: node_modules + @echo "" + @echo "=> checking code coverage..." + @npx nyc report --reporter=text-lcov > .reports/coverage.lcov && npx codecov + +.PHONY: lint +lint: node_modules + @echo "" + @echo "=> make $@" + @npx standard + +.PHONY: secure +secure: node_modules + @echo "" + @echo "=> make $@" + @npx snyk test + +.PHONY: typecheck +typecheck: node_modules + @echo "" + @echo "=> make $@" + @npx tsc --lib ES6 --noEmit index.d.ts ./test/fetch-api/api.spec.ts + +## +# Test groups + +.PHONY: test +test: test-fetch test-module + +.PHONY: test-fetch test-fetch: test-fetch-browser test-fetch-whatwg test-fetch-node -test-fetch-browser: build - ./test/fetch-api/browser/run.sh - -test-fetch-whatwg: build - ./test/fetch-api/whatwg/run.sh - -test-fetch-node: build - ./test/fetch-api/node/run.sh - +.PHONY: test-module test-module: test-module-web-cjs test-module-web-esm test-module-node-cjs test-module-node-esm test-module-react-native -test-module-web-cjs: build - ./test/module-system/web.cjs/run.sh - -test-module-web-esm: build - ./test/module-system/web.esm/run.sh - -test-module-node-cjs: build - ./test/module-system/node.cjs/run.sh - -test-module-node-esm: build - ./test/module-system/node.esm/run.sh - -test-module-react-native: build - ./test/module-system/react-native/run.sh - -typecheck: - npx tsc --lib ES6 --noEmit index.d.ts ./test/fetch-api/api.spec.ts - -.PHONY: all build deploy lint test test-fetch test-fetch-browser test-fetch-whatwg test-fetch-node test-module test-module-web-cjs test-module-web-esm test-module-node-cjs test-module-node-esm test-module-react-native typecheck +## +# Test units + +.PHONY: test-fetch-browser +test-fetch-browser: | dist test/fetch-api/api.spec.js + @echo "" + @echo "=> make $@" + @./test/fetch-api/browser/run.sh + +.PHONY: test-fetch-whatwg +test-fetch-whatwg: | dist test/fetch-api/api.spec.js + @echo "" + @echo "=> make $@" + @./test/fetch-api/whatwg/run.sh + +.PHONY: test-fetch-node +test-fetch-node: | dist test/fetch-api/api.spec.js + @echo "" + @echo "=> make $@" + @./test/fetch-api/node/run.sh + +.PHONY: test-module-web-cjs +test-module-web-cjs: | dist + @echo "" + @echo "=> make $@" + @./test/module-system/web.cjs/run.sh + +.PHONY: test-module-web-esm +test-module-web-esm: | dist + @echo "" + @echo "=> make $@" + @./test/module-system/web.esm/run.sh + +.PHONY: test-module-node-cjs +test-module-node-cjs: | dist + @echo "" + @echo "=> make $@" + @./test/module-system/node.cjs/run.sh + +.PHONY: test-module-node-esm +test-module-node-esm: | dist + @echo "" + @echo "=> make $@" + @./test/module-system/node.esm/run.sh + +.PHONY: test-module-react-native +test-module-react-native: | dist + @echo "" + @echo "=> make $@" + @./test/module-system/react-native/run.sh diff --git a/bin/release b/bin/release new file mode 100755 index 00000000..03919fcb --- /dev/null +++ b/bin/release @@ -0,0 +1,29 @@ +#!/bin/bash -xv + +# Get the git tag that triggered the workflow build +workflow_version=${GITHUB_REF_NAME:1} # Removed the "v" prefix +# Get version major being handled by the current workflow +workflow_major=$(echo $workflow_version | cut -d. -f1) +workflow_track="latest-v${workflow_major}.x" + +# Get latest version on npm registry +latest_version=$(npm view cross-fetch@latest version) +# Get version major published under the current "latest" npm tag +latest_major=$(echo $latest_version | cut -d. -f1) + + +# If it's a test prerelease, we just want to test the release workflow (dry run it!) +if [[ "$workflow_version" == *"-test"* ]]; then + npm publish --tag $workflow_track --dry-run && \ + exit 0 +fi + +npm publish --tag $workflow_track + +if [[ "$workflow_major" == "$latest_major" ]]; then + npm dist-tag add cross-fetch@$workflow_version latest +fi + +if [[ "$workflow_major" == "$(($latest_major + 1))" ]]; then + npm dist-tag add cross-fetch@$workflow_version next +fi diff --git a/package.json b/package.json index 6a7ca044..0fc78f94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cross-fetch", - "version": "3.1.5", + "version": "3.2.0", "description": "Universal WHATWG Fetch API for Node, Browsers and React Native", "homepage": "https://github.com/lquixada/cross-fetch", "main": "dist/node-ponyfill.js", @@ -10,7 +10,7 @@ "scripts": { "commit": "cz", "prepare": "husky install", - "prepublishOnly": "rimraf dist && make build" + "prepublishOnly": "rimraf dist && make dist" }, "lint-staged": { "*.js": [ @@ -66,7 +66,7 @@ "url": "https://github.com/lquixada/cross-fetch/issues" }, "dependencies": { - "node-fetch": "^2.6.9" + "node-fetch": "^2.7.0" }, "devDependencies": { "@commitlint/cli": "12.0.1", @@ -83,7 +83,7 @@ "husky": "6.0.0", "lint-staged": "10.5.4", "mocha": "8.3.2", - "mocha-headless-chrome": "3.1.0", + "mocha-headless-chrome": "4.0.0", "nock": "13.1.3", "nyc": "15.1.0", "open-cli": "6.0.1", @@ -96,9 +96,9 @@ "standard": "16.0.4", "standard-version": "9.3.1", "typescript": "4.4.4", - "webpack": "5.58.2", + "webpack": "5.82.1", "webpack-cli": "4.9.0", - "whatwg-fetch": "3.0.0", + "whatwg-fetch": "3.6.20", "yargs": "16.2.0" }, "files": [ diff --git a/rollup.config.js b/rollup.config.js index f3699cdf..5e2c5398 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -25,29 +25,35 @@ export default [ name: 'irrelevant', strict: false, banner: outdent(` - var global = typeof self !== 'undefined' ? self : this; - var __self__ = (function () { + // Save global object in a variable + var __global__ = + (typeof globalThis !== 'undefined' && globalThis) || + (typeof self !== 'undefined' && self) || + (typeof global !== 'undefined' && global); + + // Create an object that extends from __global__ without the fetch function + var __globalThis__ = (function () { function F() { this.fetch = false; - this.DOMException = global.DOMException + this.DOMException = __global__.DOMException } - F.prototype = global; + F.prototype = __global__; // Needed for feature detection on whatwg-fetch's code return new F(); })(); - (function(self) { + // Wraps whatwg-fetch with a function scope to hijack the global object + // "globalThis" that's going to be patched + (function(globalThis) { `), footer: outdent(` - })(__self__); - - __self__.fetch.ponyfill = true; + })(__globalThis__); - // Remove "polyfill" property added by whatwg-fetch - delete __self__.fetch.polyfill; + // This is a ponyfill, so... + __globalThis__.fetch.ponyfill = true; + delete __globalThis__.fetch.polyfill; - // Choose between native implementation (global) or custom implementation (__self__) - // var ctx = global.fetch ? global : __self__; - var ctx = __self__; // this line disable service worker support temporarily + // Choose between native implementation (__global__) or custom implementation (__globalThis__) + var ctx = __global__.fetch ? __global__ : __globalThis__; exports = ctx.fetch // To enable: import fetch from 'cross-fetch' exports.default = ctx.fetch // For TypeScript consumers without esModuleInterop.