pax_global_header00006660000000000000000000000064143007223470014514gustar00rootroot0000000000000052 comment=ac28c828934e2c5296640134b0042c16c4070f67 minipass-fetch-2.1.2/000077500000000000000000000000001430072234700144305ustar00rootroot00000000000000minipass-fetch-2.1.2/.commitlintrc.js000066400000000000000000000005531430072234700175530ustar00rootroot00000000000000/* This file is automatically added by @npmcli/template-oss. Do not edit. */ module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'deps', 'chore']], 'header-max-length': [2, 'always', 80], 'subject-case': [0, 'always', ['lower-case', 'sentence-case', 'start-case']], }, } minipass-fetch-2.1.2/.eslintrc.js000066400000000000000000000005451430072234700166730ustar00rootroot00000000000000/* This file is automatically added by @npmcli/template-oss. Do not edit. */ 'use strict' const { readdirSync: readdir } = require('fs') const localConfigs = readdir(__dirname) .filter((file) => file.startsWith('.eslintrc.local.')) .map((file) => `./${file}`) module.exports = { root: true, extends: [ '@npmcli', ...localConfigs, ], } minipass-fetch-2.1.2/.github/000077500000000000000000000000001430072234700157705ustar00rootroot00000000000000minipass-fetch-2.1.2/.github/CODEOWNERS000066400000000000000000000001321430072234700173570ustar00rootroot00000000000000# This file is automatically added by @npmcli/template-oss. Do not edit. * @npm/cli-team minipass-fetch-2.1.2/.github/ISSUE_TEMPLATE/000077500000000000000000000000001430072234700201535ustar00rootroot00000000000000minipass-fetch-2.1.2/.github/ISSUE_TEMPLATE/bug.yml000066400000000000000000000026551430072234700214630ustar00rootroot00000000000000# This file is automatically added by @npmcli/template-oss. Do not edit. name: Bug description: File a bug/issue title: "[BUG] " labels: [ Bug, Needs Triage ] body: - type: checkboxes attributes: label: Is there an existing issue for this? description: Please [search here](./issues) to see if an issue already exists for your problem. options: - label: I have searched the existing issues required: true - type: textarea attributes: label: Current Behavior description: A clear & concise description of what you're experiencing. validations: required: false - type: textarea attributes: label: Expected Behavior description: A clear & concise description of what you expected to happen. validations: required: false - type: textarea attributes: label: Steps To Reproduce description: Steps to reproduce the behavior. value: | 1. In this environment... 2. With this config... 3. Run '...' 4. See error... validations: required: false - type: textarea attributes: label: Environment description: | examples: - **npm**: 7.6.3 - **Node**: 13.14.0 - **OS**: Ubuntu 20.04 - **platform**: Macbook Pro value: | - npm: - Node: - OS: - platform: validations: required: false �����������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/ISSUE_TEMPLATE/config.yml����������������������������������������������0000664�0000000�0000000�00000000145�14300722347�0022143�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. blank_issues_enabled: true ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/dependabot.yml���������������������������������������������������������0000664�0000000�0000000�00000000565�14300722347�0020626�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. version: 2 updates: - package-ecosystem: npm directory: "/" schedule: interval: daily allow: - dependency-type: direct versioning-strategy: increase-if-necessary commit-message: prefix: deps prefix-development: chore labels: - "Dependencies" �������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/settings.yml�����������������������������������������������������������0000664�0000000�0000000�00000000055�14300722347�0020353�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������--- _extends: '.github:npm-cli/settings.yml' �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/�������������������������������������������������������������0000775�0000000�0000000�00000000000�14300722347�0020025�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/audit.yml����������������������������������������������������0000664�0000000�0000000�00000001370�14300722347�0021657�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: Audit on: workflow_dispatch: schedule: # "At 01:00 on Monday" https://crontab.guru/#0_1_*_*_1 - cron: "0 1 * * 1" jobs: audit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - uses: actions/setup-node@v3 with: node-version: 16.x - name: Update npm to latest run: npm i --prefer-online --no-fund --no-audit -g npm@latest - run: npm -v - run: npm i --ignore-scripts --no-audit --no-fund --package-lock - run: npm audit ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/ci.yml�������������������������������������������������������0000664�0000000�0000000�00000005114�14300722347�0021144�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: CI on: workflow_dispatch: pull_request: branches: - '*' push: branches: - main - latest schedule: # "At 02:00 on Monday" https://crontab.guru/#0_2_*_*_1 - cron: "0 2 * * 1" jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - uses: actions/setup-node@v3 with: node-version: 16.x - name: Update npm to latest run: npm i --prefer-online --no-fund --no-audit -g npm@latest - run: npm -v - run: npm i --ignore-scripts --no-audit --no-fund - run: npm run lint test: strategy: fail-fast: false matrix: node-version: - 12.13.0 - 12.x - 14.15.0 - 14.x - 16.0.0 - 16.x platform: - os: ubuntu-latest shell: bash - os: macos-latest shell: bash - os: windows-latest shell: cmd runs-on: ${{ matrix.platform.os }} defaults: run: shell: ${{ matrix.platform.shell }} steps: - uses: actions/checkout@v3 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - name: Update to workable npm (windows) # node 12 and 14 ship with npm@6, which is known to fail when updating itself in windows if: matrix.platform.os == 'windows-latest' && (startsWith(matrix.node-version, '12.') || startsWith(matrix.node-version, '14.')) run: | curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz tar xf npm-7.5.4.tgz cd package node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz cd .. rmdir /s /q package - name: Update npm to 7 # If we do test on npm 10 it needs npm7 if: startsWith(matrix.node-version, '10.') run: npm i --prefer-online --no-fund --no-audit -g npm@7 - name: Update npm to latest if: ${{ !startsWith(matrix.node-version, '10.') }} run: npm i --prefer-online --no-fund --no-audit -g npm@latest - run: npm -v - run: npm i --ignore-scripts --no-audit --no-fund - run: npm test --ignore-scripts ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/codeql-analysis.yml������������������������������������������0000664�0000000�0000000�00000002001�14300722347�0023631�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: "CodeQL" on: push: branches: - main - latest pull_request: # The branches below must be a subset of the branches above branches: - main - latest schedule: # "At 03:00 on Monday" https://crontab.guru/#0_3_*_*_1 - cron: "0 3 * * 1" jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: actions: read contents: read security-events: write strategy: fail-fast: false matrix: language: [ javascript ] steps: - uses: actions/checkout@v3 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - name: Initialize CodeQL uses: github/codeql-action/init@v1 with: languages: ${{ matrix.language }} - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v1 �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/post-dependabot.yml������������������������������������������0000664�0000000�0000000�00000002641�14300722347�0023643�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: Post Dependabot Actions on: pull_request # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions permissions: contents: write jobs: template-oss-apply: runs-on: ubuntu-latest if: github.actor == 'dependabot[bot]' steps: - uses: actions/checkout@v3 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - uses: actions/setup-node@v3 with: node-version: 16.x - name: Update npm to latest run: npm i --prefer-online --no-fund --no-audit -g npm@latest - run: npm -v - name: Dependabot metadata id: metadata uses: dependabot/fetch-metadata@v1.1.1 with: github-token: "${{ secrets.GITHUB_TOKEN }}" - name: npm install and commit if: contains(steps.metadata.outputs.dependency-names, '@npmcli/template-oss') env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh pr checkout ${{ github.event.pull_request.number }} npm install --ignore-scripts --no-audit --no-fund npm run template-oss-apply git add . git commit -am "chore: postinstall for dependabot template-oss PR" git push npm run lint �����������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/pull-request.yml���������������������������������������������0000664�0000000�0000000�00000002143�14300722347�0023212�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: Pull Request Linting on: pull_request: types: - opened - reopened - edited - synchronize jobs: check: name: Check PR Title or Commits runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Setup git user run: | git config --global user.email "npm-cli+bot@github.com" git config --global user.name "npm CLI robot" - uses: actions/setup-node@v3 with: node-version: 16.x - name: Update npm to latest run: npm i --prefer-online --no-fund --no-audit -g npm@latest - run: npm -v - name: Install deps run: npm i -D @commitlint/cli @commitlint/config-conventional - name: Check commits OR PR title env: PR_TITLE: ${{ github.event.pull_request.title }} run: | npx --offline commitlint -V --from origin/main --to ${{ github.event.pull_request.head.sha }} \ || echo $PR_TITLE | npx --offline commitlint -V �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.github/workflows/release-please.yml�������������������������������������������0000664�0000000�0000000�00000001314�14300722347�0023436�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. name: Release Please on: push: branches: - main - latest jobs: release-please: runs-on: ubuntu-latest steps: - uses: google-github-actions/release-please-action@v3 id: release with: release-type: node changelog-types: > [ {"type":"feat","section":"Features","hidden":false}, {"type":"fix","section":"Bug Fixes","hidden":false}, {"type":"docs","section":"Documentation","hidden":false}, {"type":"deps","section":"Dependencies","hidden":false}, {"type":"chore","hidden":true} ] ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.gitignore���������������������������������������������������������������������0000664�0000000�0000000�00000000570�14300722347�0016422�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# This file is automatically added by @npmcli/template-oss. Do not edit. # ignore everything in the root /* # keep these !/.eslintrc.local.* !**/.gitignore !/docs/ !/tap-snapshots/ !/test/ !/map.js !/scripts/ !/README* !/LICENSE* !/CHANGELOG* !/.commitlintrc.js !/.eslintrc.js !/.github/ !/.gitignore !/.npmrc !/CODE_OF_CONDUCT.md !/SECURITY.md !/bin/ !/lib/ !/package.json ����������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/.npmrc�������������������������������������������������������������������������0000664�0000000�0000000�00000000135�14300722347�0015547�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������; This file is automatically added by @npmcli/template-oss. Do not edit. package-lock=false �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/CHANGELOG.md�������������������������������������������������������������������0000664�0000000�0000000�00000010001�14300722347�0016231�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# Changelog ## [2.1.2](https://github.com/npm/minipass-fetch/compare/v2.1.1...v2.1.2) (2022-08-22) ### Bug Fixes * **json:** don't catch body errors ([#64](https://github.com/npm/minipass-fetch/issues/64)) ([9658a0a](https://github.com/npm/minipass-fetch/commit/9658a0a60349b38e62011a22ab6e9079c4319e98)) ## [2.1.1](https://github.com/npm/minipass-fetch/compare/v2.1.0...v2.1.1) (2022-08-17) ### Bug Fixes * linting ([d2045cb](https://github.com/npm/minipass-fetch/commit/d2045cb25afb77e8c8f5c7551209922a16d5b215)) ## [2.1.0](https://github.com/npm/minipass-fetch/compare/v2.0.3...v2.1.0) (2022-03-24) ### Features * expose AbortError directly ([ed9d420](https://github.com/npm/minipass-fetch/commit/ed9d42026676a32e126e867186e2578e78e963f4)) ### Bug Fixes * do not setup the response timeout if the stream has already ended ([#53](https://github.com/npm/minipass-fetch/issues/53)) ([0feea3c](https://github.com/npm/minipass-fetch/commit/0feea3cf399b6a1888f3cf3292a12675c2306b4d)) ### [2.0.3](https://www.github.com/npm/minipass-fetch/compare/v2.0.2...v2.0.3) (2022-03-08) ### Bug Fixes * strip authorization and cookie headers on redirect to new host ([#45](https://www.github.com/npm/minipass-fetch/issues/45)) ([50d919a](https://www.github.com/npm/minipass-fetch/commit/50d919aafce3b95a8237a6e2dc93ae7e4215650f)) ### [2.0.2](https://www.github.com/npm/minipass-fetch/compare/v2.0.1...v2.0.2) (2022-03-02) ### Bug Fixes * pass search params as part of path string ([#40](https://www.github.com/npm/minipass-fetch/issues/40)) ([404ad4c](https://www.github.com/npm/minipass-fetch/commit/404ad4cf1a2c21563205bee21ca1ef785b31c72f)) ### [2.0.1](https://www.github.com/npm/minipass-fetch/compare/v2.0.0...v2.0.1) (2022-03-01) ### Bug Fixes * [#18](https://www.github.com/npm/minipass-fetch/issues/18) ([3a11fe4](https://www.github.com/npm/minipass-fetch/commit/3a11fe4c18587b61d4e212d332338bd3427f5894)) * Handle data: URIs more consistently ([#19](https://www.github.com/npm/minipass-fetch/issues/19)) ([3a11fe4](https://www.github.com/npm/minipass-fetch/commit/3a11fe4c18587b61d4e212d332338bd3427f5894)), closes [#18](https://www.github.com/npm/minipass-fetch/issues/18) ### Dependencies * update encoding requirement from ^0.1.12 to ^0.1.13 ([#34](https://www.github.com/npm/minipass-fetch/issues/34)) ([65602ff](https://www.github.com/npm/minipass-fetch/commit/65602ffed38947efb13e907a165ebde22423cac9)) ## [2.0.0](https://www.github.com/npm/minipass-fetch/compare/v1.4.1...v2.0.0) (2022-02-24) ### ⚠ BREAKING CHANGES * this removes the (hopefully) unused feature that arbitrary strings are allowed as URLs in the Request constructor. we now require that URLs are valid and absolute. * this drops support for node versions older than 12 LTS ### Bug Fixes * check for existence of unref before calling ([05fb45b](https://www.github.com/npm/minipass-fetch/commit/05fb45b2289045899b8e762e0f16ff9dd6bbd767)), closes [#13](https://www.github.com/npm/minipass-fetch/issues/13) * ensure we abort a request that emits error on the response body ([#25](https://www.github.com/npm/minipass-fetch/issues/25)) ([5565cde](https://www.github.com/npm/minipass-fetch/commit/5565cdef3cbcd0bc286794c42695f5ec2da83264)) * implement @npmcli/template-oss ([#26](https://www.github.com/npm/minipass-fetch/issues/26)) ([df5e1d2](https://www.github.com/npm/minipass-fetch/commit/df5e1d281372f88ecb8435aaec8ffa1712546390)) * use URL constructor instead of url.parse() ([#33](https://www.github.com/npm/minipass-fetch/issues/33)) ([f96f3b1](https://www.github.com/npm/minipass-fetch/commit/f96f3b13e68f3851fd9fadb762c58f441a4c3f48)) ### Dependencies * update minipass requirement from ^3.1.0 to ^3.1.6 ([#30](https://www.github.com/npm/minipass-fetch/issues/30)) ([4ce93e5](https://www.github.com/npm/minipass-fetch/commit/4ce93e5dd28b56457721454bea63f3c37b0d50d3)) * update minizlib requirement from ^2.0.0 to ^2.1.2 ([#29](https://www.github.com/npm/minipass-fetch/issues/29)) ([44e8701](https://www.github.com/npm/minipass-fetch/commit/44e8701d6c142223f6abe54c42f6e5a3d43707d7)) �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/CODE_OF_CONDUCT.md�������������������������������������������������������������0000664�0000000�0000000�00000000507�14300722347�0017231�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������<!-- This file is automatically added by @npmcli/template-oss. Do not edit. --> All interactions in this repo are covered by the [npm Code of Conduct](https://docs.npmjs.com/policies/conduct) The npm cli team may, at its own discretion, moderate, remove, or edit any interactions such as pull requests, issues, and comments. �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/LICENSE������������������������������������������������������������������������0000664�0000000�0000000�00000002434�14300722347�0015440�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������The MIT License (MIT) Copyright (c) Isaac Z. Schlueter and Contributors Copyright (c) 2016 David Frank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --- Note: This is a derivative work based on "node-fetch" by David Frank, modified and distributed under the terms of the MIT license above. https://github.com/bitinn/node-fetch ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/README.md����������������������������������������������������������������������0000664�0000000�0000000�00000001734�14300722347�0015714�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# minipass-fetch An implementation of window.fetch in Node.js using Minipass streams This is a fork (or more precisely, a reimplementation) of [node-fetch](http://npm.im/node-fetch). All streams have been replaced with [minipass streams](http://npm.im/minipass). The goal of this module is to stay in sync with the API presented by `node-fetch`, with the exception of the streaming interface provided. ## Why Minipass streams are faster and more deterministic in their timing contract than node-core streams, making them a better fit for many server-side use cases. ## API See [node-fetch](http://npm.im/node-fetch) Differences from `node-fetch` (and, by extension, from the WhatWG Fetch specification): - Returns [minipass](http://npm.im/minipass) streams instead of node-core streams. - Supports the full set of [TLS Options that may be provided to `https.request()`](https://nodejs.org/api/https.html#https_https_request_options_callback) when making `https` requests. ������������������������������������minipass-fetch-2.1.2/SECURITY.md��������������������������������������������������������������������0000664�0000000�0000000�00000000246�14300722347�0016223�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������<!-- This file is automatically added by @npmcli/template-oss. Do not edit. --> Please send vulnerability reports through [hackerone](https://hackerone.com/github). ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/���������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14300722347�0015176�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/abort-error.js�������������������������������������������������������������0000664�0000000�0000000�00000000552�14300722347�0017774�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' class AbortError extends Error { constructor (message) { super(message) this.code = 'FETCH_ABORTED' this.type = 'aborted' Error.captureStackTrace(this, this.constructor) } get name () { return 'AbortError' } // don't allow name to be overridden, but don't throw either set name (s) {} } module.exports = AbortError ������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/blob.js��������������������������������������������������������������������0000664�0000000�0000000�00000004432�14300722347�0016455�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const Minipass = require('minipass') const TYPE = Symbol('type') const BUFFER = Symbol('buffer') class Blob { constructor (blobParts, options) { this[TYPE] = '' const buffers = [] let size = 0 if (blobParts) { const a = blobParts const length = Number(a.length) for (let i = 0; i < length; i++) { const element = a[i] const buffer = element instanceof Buffer ? element : ArrayBuffer.isView(element) ? Buffer.from(element.buffer, element.byteOffset, element.byteLength) : element instanceof ArrayBuffer ? Buffer.from(element) : element instanceof Blob ? element[BUFFER] : typeof element === 'string' ? Buffer.from(element) : Buffer.from(String(element)) size += buffer.length buffers.push(buffer) } } this[BUFFER] = Buffer.concat(buffers, size) const type = options && options.type !== undefined && String(options.type).toLowerCase() if (type && !/[^\u0020-\u007E]/.test(type)) { this[TYPE] = type } } get size () { return this[BUFFER].length } get type () { return this[TYPE] } text () { return Promise.resolve(this[BUFFER].toString()) } arrayBuffer () { const buf = this[BUFFER] const off = buf.byteOffset const len = buf.byteLength const ab = buf.buffer.slice(off, off + len) return Promise.resolve(ab) } stream () { return new Minipass().end(this[BUFFER]) } slice (start, end, type) { const size = this.size const relativeStart = start === undefined ? 0 : start < 0 ? Math.max(size + start, 0) : Math.min(start, size) const relativeEnd = end === undefined ? size : end < 0 ? Math.max(size + end, 0) : Math.min(end, size) const span = Math.max(relativeEnd - relativeStart, 0) const buffer = this[BUFFER] const slicedBuffer = buffer.slice( relativeStart, relativeStart + span ) const blob = new Blob([], { type }) blob[BUFFER] = slicedBuffer return blob } get [Symbol.toStringTag] () { return 'Blob' } static get BUFFER () { return BUFFER } } Object.defineProperties(Blob.prototype, { size: { enumerable: true }, type: { enumerable: true }, }) module.exports = Blob ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/body.js��������������������������������������������������������������������0000664�0000000�0000000�00000024470�14300722347�0016500�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const Minipass = require('minipass') const MinipassSized = require('minipass-sized') const Blob = require('./blob.js') const { BUFFER } = Blob const FetchError = require('./fetch-error.js') // optional dependency on 'encoding' let convert try { convert = require('encoding').convert } catch (e) { // defer error until textConverted is called } const INTERNALS = Symbol('Body internals') const CONSUME_BODY = Symbol('consumeBody') class Body { constructor (bodyArg, options = {}) { const { size = 0, timeout = 0 } = options const body = bodyArg === undefined || bodyArg === null ? null : isURLSearchParams(bodyArg) ? Buffer.from(bodyArg.toString()) : isBlob(bodyArg) ? bodyArg : Buffer.isBuffer(bodyArg) ? bodyArg : Object.prototype.toString.call(bodyArg) === '[object ArrayBuffer]' ? Buffer.from(bodyArg) : ArrayBuffer.isView(bodyArg) ? Buffer.from(bodyArg.buffer, bodyArg.byteOffset, bodyArg.byteLength) : Minipass.isStream(bodyArg) ? bodyArg : Buffer.from(String(bodyArg)) this[INTERNALS] = { body, disturbed: false, error: null, } this.size = size this.timeout = timeout if (Minipass.isStream(body)) { body.on('error', er => { const error = er.name === 'AbortError' ? er : new FetchError(`Invalid response while trying to fetch ${ this.url}: ${er.message}`, 'system', er) this[INTERNALS].error = error }) } } get body () { return this[INTERNALS].body } get bodyUsed () { return this[INTERNALS].disturbed } arrayBuffer () { return this[CONSUME_BODY]().then(buf => buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)) } blob () { const ct = this.headers && this.headers.get('content-type') || '' return this[CONSUME_BODY]().then(buf => Object.assign( new Blob([], { type: ct.toLowerCase() }), { [BUFFER]: buf } )) } async json () { const buf = await this[CONSUME_BODY]() try { return JSON.parse(buf.toString()) } catch (er) { throw new FetchError( `invalid json response body at ${this.url} reason: ${er.message}`, 'invalid-json' ) } } text () { return this[CONSUME_BODY]().then(buf => buf.toString()) } buffer () { return this[CONSUME_BODY]() } textConverted () { return this[CONSUME_BODY]().then(buf => convertBody(buf, this.headers)) } [CONSUME_BODY] () { if (this[INTERNALS].disturbed) { return Promise.reject(new TypeError(`body used already for: ${ this.url}`)) } this[INTERNALS].disturbed = true if (this[INTERNALS].error) { return Promise.reject(this[INTERNALS].error) } // body is null if (this.body === null) { return Promise.resolve(Buffer.alloc(0)) } if (Buffer.isBuffer(this.body)) { return Promise.resolve(this.body) } const upstream = isBlob(this.body) ? this.body.stream() : this.body /* istanbul ignore if: should never happen */ if (!Minipass.isStream(upstream)) { return Promise.resolve(Buffer.alloc(0)) } const stream = this.size && upstream instanceof MinipassSized ? upstream : !this.size && upstream instanceof Minipass && !(upstream instanceof MinipassSized) ? upstream : this.size ? new MinipassSized({ size: this.size }) : new Minipass() // allow timeout on slow response body, but only if the stream is still writable. this // makes the timeout center on the socket stream from lib/index.js rather than the // intermediary minipass stream we create to receive the data const resTimeout = this.timeout && stream.writable ? setTimeout(() => { stream.emit('error', new FetchError( `Response timeout while trying to fetch ${ this.url} (over ${this.timeout}ms)`, 'body-timeout')) }, this.timeout) : null // do not keep the process open just for this timeout, even // though we expect it'll get cleared eventually. if (resTimeout && resTimeout.unref) { resTimeout.unref() } // do the pipe in the promise, because the pipe() can send too much // data through right away and upset the MP Sized object return new Promise((resolve, reject) => { // if the stream is some other kind of stream, then pipe through a MP // so we can collect it more easily. if (stream !== upstream) { upstream.on('error', er => stream.emit('error', er)) upstream.pipe(stream) } resolve() }).then(() => stream.concat()).then(buf => { clearTimeout(resTimeout) return buf }).catch(er => { clearTimeout(resTimeout) // request was aborted, reject with this Error if (er.name === 'AbortError' || er.name === 'FetchError') { throw er } else if (er.name === 'RangeError') { throw new FetchError(`Could not create Buffer from response body for ${ this.url}: ${er.message}`, 'system', er) } else { // other errors, such as incorrect content-encoding or content-length throw new FetchError(`Invalid response body while trying to fetch ${ this.url}: ${er.message}`, 'system', er) } }) } static clone (instance) { if (instance.bodyUsed) { throw new Error('cannot clone body after it is used') } const body = instance.body // check that body is a stream and not form-data object // NB: can't clone the form-data object without having it as a dependency if (Minipass.isStream(body) && typeof body.getBoundary !== 'function') { // create a dedicated tee stream so that we don't lose data // potentially sitting in the body stream's buffer by writing it // immediately to p1 and not having it for p2. const tee = new Minipass() const p1 = new Minipass() const p2 = new Minipass() tee.on('error', er => { p1.emit('error', er) p2.emit('error', er) }) body.on('error', er => tee.emit('error', er)) tee.pipe(p1) tee.pipe(p2) body.pipe(tee) // set instance body to one fork, return the other instance[INTERNALS].body = p1 return p2 } else { return instance.body } } static extractContentType (body) { return body === null || body === undefined ? null : typeof body === 'string' ? 'text/plain;charset=UTF-8' : isURLSearchParams(body) ? 'application/x-www-form-urlencoded;charset=UTF-8' : isBlob(body) ? body.type || null : Buffer.isBuffer(body) ? null : Object.prototype.toString.call(body) === '[object ArrayBuffer]' ? null : ArrayBuffer.isView(body) ? null : typeof body.getBoundary === 'function' ? `multipart/form-data;boundary=${body.getBoundary()}` : Minipass.isStream(body) ? null : 'text/plain;charset=UTF-8' } static getTotalBytes (instance) { const { body } = instance return (body === null || body === undefined) ? 0 : isBlob(body) ? body.size : Buffer.isBuffer(body) ? body.length : body && typeof body.getLengthSync === 'function' && ( // detect form data input from form-data module body._lengthRetrievers && /* istanbul ignore next */ body._lengthRetrievers.length === 0 || // 1.x body.hasKnownLength && body.hasKnownLength()) // 2.x ? body.getLengthSync() : null } static writeToStream (dest, instance) { const { body } = instance if (body === null || body === undefined) { dest.end() } else if (Buffer.isBuffer(body) || typeof body === 'string') { dest.end(body) } else { // body is stream or blob const stream = isBlob(body) ? body.stream() : body stream.on('error', er => dest.emit('error', er)).pipe(dest) } return dest } } Object.defineProperties(Body.prototype, { body: { enumerable: true }, bodyUsed: { enumerable: true }, arrayBuffer: { enumerable: true }, blob: { enumerable: true }, json: { enumerable: true }, text: { enumerable: true }, }) const isURLSearchParams = obj => // Duck-typing as a necessary condition. (typeof obj !== 'object' || typeof obj.append !== 'function' || typeof obj.delete !== 'function' || typeof obj.get !== 'function' || typeof obj.getAll !== 'function' || typeof obj.has !== 'function' || typeof obj.set !== 'function') ? false // Brand-checking and more duck-typing as optional condition. : obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function' const isBlob = obj => typeof obj === 'object' && typeof obj.arrayBuffer === 'function' && typeof obj.type === 'string' && typeof obj.stream === 'function' && typeof obj.constructor === 'function' && typeof obj.constructor.name === 'string' && /^(Blob|File)$/.test(obj.constructor.name) && /^(Blob|File)$/.test(obj[Symbol.toStringTag]) const convertBody = (buffer, headers) => { /* istanbul ignore if */ if (typeof convert !== 'function') { throw new Error('The package `encoding` must be installed to use the textConverted() function') } const ct = headers && headers.get('content-type') let charset = 'utf-8' let res // header if (ct) { res = /charset=([^;]*)/i.exec(ct) } // no charset in content type, peek at response body for at most 1024 bytes const str = buffer.slice(0, 1024).toString() // html5 if (!res && str) { res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str) } // html4 if (!res && str) { res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str) if (!res) { res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str) if (res) { res.pop() } // drop last quote } if (res) { res = /charset=(.*)/i.exec(res.pop()) } } // xml if (!res && str) { res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str) } // found charset if (res) { charset = res.pop() // prevent decode issues when sites use incorrect encoding // ref: https://hsivonen.fi/encoding-menu/ if (charset === 'gb2312' || charset === 'gbk') { charset = 'gb18030' } } // turn raw buffers into a single utf-8 buffer return convert( buffer, 'UTF-8', charset ).toString() } module.exports = Body ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/fetch-error.js�������������������������������������������������������������0000664�0000000�0000000�00000001311�14300722347�0017750�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' class FetchError extends Error { constructor (message, type, systemError) { super(message) this.code = 'FETCH_ERROR' // pick up code, expected, path, ... if (systemError) { Object.assign(this, systemError) } this.errno = this.code // override anything the system error might've clobbered this.type = this.code === 'EBADSIZE' && this.found > this.expect ? 'max-size' : type this.message = message Error.captureStackTrace(this, this.constructor) } get name () { return 'FetchError' } // don't allow name to be overwritten set name (n) {} get [Symbol.toStringTag] () { return 'FetchError' } } module.exports = FetchError �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/headers.js�����������������������������������������������������������������0000664�0000000�0000000�00000014623�14300722347�0017155�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const invalidTokenRegex = /[^^_`a-zA-Z\-0-9!#$%&'*+.|~]/ const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/ const validateName = name => { name = `${name}` if (invalidTokenRegex.test(name) || name === '') { throw new TypeError(`${name} is not a legal HTTP header name`) } } const validateValue = value => { value = `${value}` if (invalidHeaderCharRegex.test(value)) { throw new TypeError(`${value} is not a legal HTTP header value`) } } const find = (map, name) => { name = name.toLowerCase() for (const key in map) { if (key.toLowerCase() === name) { return key } } return undefined } const MAP = Symbol('map') class Headers { constructor (init = undefined) { this[MAP] = Object.create(null) if (init instanceof Headers) { const rawHeaders = init.raw() const headerNames = Object.keys(rawHeaders) for (const headerName of headerNames) { for (const value of rawHeaders[headerName]) { this.append(headerName, value) } } return } // no-op if (init === undefined || init === null) { return } if (typeof init === 'object') { const method = init[Symbol.iterator] if (method !== null && method !== undefined) { if (typeof method !== 'function') { throw new TypeError('Header pairs must be iterable') } // sequence<sequence<ByteString>> // Note: per spec we have to first exhaust the lists then process them const pairs = [] for (const pair of init) { if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') { throw new TypeError('Each header pair must be iterable') } const arrPair = Array.from(pair) if (arrPair.length !== 2) { throw new TypeError('Each header pair must be a name/value tuple') } pairs.push(arrPair) } for (const pair of pairs) { this.append(pair[0], pair[1]) } } else { // record<ByteString, ByteString> for (const key of Object.keys(init)) { this.append(key, init[key]) } } } else { throw new TypeError('Provided initializer must be an object') } } get (name) { name = `${name}` validateName(name) const key = find(this[MAP], name) if (key === undefined) { return null } return this[MAP][key].join(', ') } forEach (callback, thisArg = undefined) { let pairs = getHeaders(this) for (let i = 0; i < pairs.length; i++) { const [name, value] = pairs[i] callback.call(thisArg, value, name, this) // refresh in case the callback added more headers pairs = getHeaders(this) } } set (name, value) { name = `${name}` value = `${value}` validateName(name) validateValue(value) const key = find(this[MAP], name) this[MAP][key !== undefined ? key : name] = [value] } append (name, value) { name = `${name}` value = `${value}` validateName(name) validateValue(value) const key = find(this[MAP], name) if (key !== undefined) { this[MAP][key].push(value) } else { this[MAP][name] = [value] } } has (name) { name = `${name}` validateName(name) return find(this[MAP], name) !== undefined } delete (name) { name = `${name}` validateName(name) const key = find(this[MAP], name) if (key !== undefined) { delete this[MAP][key] } } raw () { return this[MAP] } keys () { return new HeadersIterator(this, 'key') } values () { return new HeadersIterator(this, 'value') } [Symbol.iterator] () { return new HeadersIterator(this, 'key+value') } entries () { return new HeadersIterator(this, 'key+value') } get [Symbol.toStringTag] () { return 'Headers' } static exportNodeCompatibleHeaders (headers) { const obj = Object.assign(Object.create(null), headers[MAP]) // http.request() only supports string as Host header. This hack makes // specifying custom Host header possible. const hostHeaderKey = find(headers[MAP], 'Host') if (hostHeaderKey !== undefined) { obj[hostHeaderKey] = obj[hostHeaderKey][0] } return obj } static createHeadersLenient (obj) { const headers = new Headers() for (const name of Object.keys(obj)) { if (invalidTokenRegex.test(name)) { continue } if (Array.isArray(obj[name])) { for (const val of obj[name]) { if (invalidHeaderCharRegex.test(val)) { continue } if (headers[MAP][name] === undefined) { headers[MAP][name] = [val] } else { headers[MAP][name].push(val) } } } else if (!invalidHeaderCharRegex.test(obj[name])) { headers[MAP][name] = [obj[name]] } } return headers } } Object.defineProperties(Headers.prototype, { get: { enumerable: true }, forEach: { enumerable: true }, set: { enumerable: true }, append: { enumerable: true }, has: { enumerable: true }, delete: { enumerable: true }, keys: { enumerable: true }, values: { enumerable: true }, entries: { enumerable: true }, }) const getHeaders = (headers, kind = 'key+value') => Object.keys(headers[MAP]).sort().map( kind === 'key' ? k => k.toLowerCase() : kind === 'value' ? k => headers[MAP][k].join(', ') : k => [k.toLowerCase(), headers[MAP][k].join(', ')] ) const INTERNAL = Symbol('internal') class HeadersIterator { constructor (target, kind) { this[INTERNAL] = { target, kind, index: 0, } } get [Symbol.toStringTag] () { return 'HeadersIterator' } next () { /* istanbul ignore if: should be impossible */ if (!this || Object.getPrototypeOf(this) !== HeadersIterator.prototype) { throw new TypeError('Value of `this` is not a HeadersIterator') } const { target, kind, index } = this[INTERNAL] const values = getHeaders(target, kind) const len = values.length if (index >= len) { return { value: undefined, done: true, } } this[INTERNAL].index++ return { value: values[index], done: false } } } // manually extend because 'extends' requires a ctor Object.setPrototypeOf(HeadersIterator.prototype, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))) module.exports = Headers �������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/index.js�������������������������������������������������������������������0000664�0000000�0000000�00000030611�14300722347�0016644�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const { URL } = require('url') const http = require('http') const https = require('https') const zlib = require('minizlib') const Minipass = require('minipass') const Body = require('./body.js') const { writeToStream, getTotalBytes } = Body const Response = require('./response.js') const Headers = require('./headers.js') const { createHeadersLenient } = Headers const Request = require('./request.js') const { getNodeRequestOptions } = Request const FetchError = require('./fetch-error.js') const AbortError = require('./abort-error.js') // XXX this should really be split up and unit-ized for easier testing // and better DRY implementation of data/http request aborting const fetch = async (url, opts) => { if (/^data:/.test(url)) { const request = new Request(url, opts) // delay 1 promise tick so that the consumer can abort right away return Promise.resolve().then(() => new Promise((resolve, reject) => { let type, data try { const { pathname, search } = new URL(url) const split = pathname.split(',') if (split.length < 2) { throw new Error('invalid data: URI') } const mime = split.shift() const base64 = /;base64$/.test(mime) type = base64 ? mime.slice(0, -1 * ';base64'.length) : mime const rawData = decodeURIComponent(split.join(',') + search) data = base64 ? Buffer.from(rawData, 'base64') : Buffer.from(rawData) } catch (er) { return reject(new FetchError(`[${request.method}] ${ request.url} invalid URL, ${er.message}`, 'system', er)) } const { signal } = request if (signal && signal.aborted) { return reject(new AbortError('The user aborted a request.')) } const headers = { 'Content-Length': data.length } if (type) { headers['Content-Type'] = type } return resolve(new Response(data, { headers })) })) } return new Promise((resolve, reject) => { // build request object const request = new Request(url, opts) let options try { options = getNodeRequestOptions(request) } catch (er) { return reject(er) } const send = (options.protocol === 'https:' ? https : http).request const { signal } = request let response = null const abort = () => { const error = new AbortError('The user aborted a request.') reject(error) if (Minipass.isStream(request.body) && typeof request.body.destroy === 'function') { request.body.destroy(error) } if (response && response.body) { response.body.emit('error', error) } } if (signal && signal.aborted) { return abort() } const abortAndFinalize = () => { abort() finalize() } const finalize = () => { req.abort() if (signal) { signal.removeEventListener('abort', abortAndFinalize) } clearTimeout(reqTimeout) } // send request const req = send(options) if (signal) { signal.addEventListener('abort', abortAndFinalize) } let reqTimeout = null if (request.timeout) { req.once('socket', socket => { reqTimeout = setTimeout(() => { reject(new FetchError(`network timeout at: ${ request.url}`, 'request-timeout')) finalize() }, request.timeout) }) } req.on('error', er => { // if a 'response' event is emitted before the 'error' event, then by the // time this handler is run it's too late to reject the Promise for the // response. instead, we forward the error event to the response stream // so that the error will surface to the user when they try to consume // the body. this is done as a side effect of aborting the request except // for in windows, where we must forward the event manually, otherwise // there is no longer a ref'd socket attached to the request and the // stream never ends so the event loop runs out of work and the process // exits without warning. // coverage skipped here due to the difficulty in testing // istanbul ignore next if (req.res) { req.res.emit('error', er) } reject(new FetchError(`request to ${request.url} failed, reason: ${ er.message}`, 'system', er)) finalize() }) req.on('response', res => { clearTimeout(reqTimeout) const headers = createHeadersLenient(res.headers) // HTTP fetch step 5 if (fetch.isRedirect(res.statusCode)) { // HTTP fetch step 5.2 const location = headers.get('Location') // HTTP fetch step 5.3 const locationURL = location === null ? null : (new URL(location, request.url)).toString() // HTTP fetch step 5.5 if (request.redirect === 'error') { reject(new FetchError('uri requested responds with a redirect, ' + `redirect mode is set to error: ${request.url}`, 'no-redirect')) finalize() return } else if (request.redirect === 'manual') { // node-fetch-specific step: make manual redirect a bit easier to // use by setting the Location header value to the resolved URL. if (locationURL !== null) { // handle corrupted header try { headers.set('Location', locationURL) } catch (err) { /* istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request */ reject(err) } } } else if (request.redirect === 'follow' && locationURL !== null) { // HTTP-redirect fetch step 5 if (request.counter >= request.follow) { reject(new FetchError(`maximum redirect reached at: ${ request.url}`, 'max-redirect')) finalize() return } // HTTP-redirect fetch step 9 if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) { reject(new FetchError( 'Cannot follow redirect with body being a readable stream', 'unsupported-redirect' )) finalize() return } // Update host due to redirection request.headers.set('host', (new URL(locationURL)).host) // HTTP-redirect fetch step 6 (counter increment) // Create a new Request object. const requestOpts = { headers: new Headers(request.headers), follow: request.follow, counter: request.counter + 1, agent: request.agent, compress: request.compress, method: request.method, body: request.body, signal: request.signal, timeout: request.timeout, } // if the redirect is to a new hostname, strip the authorization and cookie headers const parsedOriginal = new URL(request.url) const parsedRedirect = new URL(locationURL) if (parsedOriginal.hostname !== parsedRedirect.hostname) { requestOpts.headers.delete('authorization') requestOpts.headers.delete('cookie') } // HTTP-redirect fetch step 11 if (res.statusCode === 303 || ( (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST' )) { requestOpts.method = 'GET' requestOpts.body = undefined requestOpts.headers.delete('content-length') } // HTTP-redirect fetch step 15 resolve(fetch(new Request(locationURL, requestOpts))) finalize() return } } // end if(isRedirect) // prepare response res.once('end', () => signal && signal.removeEventListener('abort', abortAndFinalize)) const body = new Minipass() // if an error occurs, either on the response stream itself, on one of the // decoder streams, or a response length timeout from the Body class, we // forward the error through to our internal body stream. If we see an // error event on that, we call finalize to abort the request and ensure // we don't leave a socket believing a request is in flight. // this is difficult to test, so lacks specific coverage. body.on('error', finalize) // exceedingly rare that the stream would have an error, // but just in case we proxy it to the stream in use. res.on('error', /* istanbul ignore next */ er => body.emit('error', er)) res.on('data', (chunk) => body.write(chunk)) res.on('end', () => body.end()) const responseOptions = { url: request.url, status: res.statusCode, statusText: res.statusMessage, headers: headers, size: request.size, timeout: request.timeout, counter: request.counter, trailer: new Promise(resolveTrailer => res.on('end', () => resolveTrailer(createHeadersLenient(res.trailers)))), } // HTTP-network fetch step 12.1.1.3 const codings = headers.get('Content-Encoding') // HTTP-network fetch step 12.1.1.4: handle content codings // in following scenarios we ignore compression support // 1. compression support is disabled // 2. HEAD request // 3. no Content-Encoding header // 4. no content response (204) // 5. content not modified response (304) if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) { response = new Response(body, responseOptions) resolve(response) return } // Be less strict when decoding compressed responses, since sometimes // servers send slightly invalid responses that are still accepted // by common browsers. // Always using Z_SYNC_FLUSH is what cURL does. const zlibOptions = { flush: zlib.constants.Z_SYNC_FLUSH, finishFlush: zlib.constants.Z_SYNC_FLUSH, } // for gzip if (codings === 'gzip' || codings === 'x-gzip') { const unzip = new zlib.Gunzip(zlibOptions) response = new Response( // exceedingly rare that the stream would have an error, // but just in case we proxy it to the stream in use. body.on('error', /* istanbul ignore next */ er => unzip.emit('error', er)).pipe(unzip), responseOptions ) resolve(response) return } // for deflate if (codings === 'deflate' || codings === 'x-deflate') { // handle the infamous raw deflate response from old servers // a hack for old IIS and Apache servers const raw = res.pipe(new Minipass()) raw.once('data', chunk => { // see http://stackoverflow.com/questions/37519828 const decoder = (chunk[0] & 0x0F) === 0x08 ? new zlib.Inflate() : new zlib.InflateRaw() // exceedingly rare that the stream would have an error, // but just in case we proxy it to the stream in use. body.on('error', /* istanbul ignore next */ er => decoder.emit('error', er)).pipe(decoder) response = new Response(decoder, responseOptions) resolve(response) }) return } // for br if (codings === 'br') { // ignoring coverage so tests don't have to fake support (or lack of) for brotli // istanbul ignore next try { var decoder = new zlib.BrotliDecompress() } catch (err) { reject(err) finalize() return } // exceedingly rare that the stream would have an error, // but just in case we proxy it to the stream in use. body.on('error', /* istanbul ignore next */ er => decoder.emit('error', er)).pipe(decoder) response = new Response(decoder, responseOptions) resolve(response) return } // otherwise, use response as-is response = new Response(body, responseOptions) resolve(response) }) writeToStream(req, request) }) } module.exports = fetch fetch.isRedirect = code => code === 301 || code === 302 || code === 303 || code === 307 || code === 308 fetch.Headers = Headers fetch.Request = Request fetch.Response = Response fetch.FetchError = FetchError fetch.AbortError = AbortError �����������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/lib/request.js�����������������������������������������������������������������0000664�0000000�0000000�00000015741�14300722347�0017234�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const { URL } = require('url') const Minipass = require('minipass') const Headers = require('./headers.js') const { exportNodeCompatibleHeaders } = Headers const Body = require('./body.js') const { clone, extractContentType, getTotalBytes } = Body const version = require('../package.json').version const defaultUserAgent = `minipass-fetch/${version} (+https://github.com/isaacs/minipass-fetch)` const INTERNALS = Symbol('Request internals') const isRequest = input => typeof input === 'object' && typeof input[INTERNALS] === 'object' const isAbortSignal = signal => { const proto = ( signal && typeof signal === 'object' && Object.getPrototypeOf(signal) ) return !!(proto && proto.constructor.name === 'AbortSignal') } class Request extends Body { constructor (input, init = {}) { const parsedURL = isRequest(input) ? new URL(input.url) : input && input.href ? new URL(input.href) : new URL(`${input}`) if (isRequest(input)) { init = { ...input[INTERNALS], ...init } } else if (!input || typeof input === 'string') { input = {} } const method = (init.method || input.method || 'GET').toUpperCase() const isGETHEAD = method === 'GET' || method === 'HEAD' if ((init.body !== null && init.body !== undefined || isRequest(input) && input.body !== null) && isGETHEAD) { throw new TypeError('Request with GET/HEAD method cannot have body') } const inputBody = init.body !== null && init.body !== undefined ? init.body : isRequest(input) && input.body !== null ? clone(input) : null super(inputBody, { timeout: init.timeout || input.timeout || 0, size: init.size || input.size || 0, }) const headers = new Headers(init.headers || input.headers || {}) if (inputBody !== null && inputBody !== undefined && !headers.has('Content-Type')) { const contentType = extractContentType(inputBody) if (contentType) { headers.append('Content-Type', contentType) } } const signal = 'signal' in init ? init.signal : null if (signal !== null && signal !== undefined && !isAbortSignal(signal)) { throw new TypeError('Expected signal must be an instanceof AbortSignal') } // TLS specific options that are handled by node const { ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, family, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0', secureOptions, secureProtocol, servername, sessionIdContext, } = init this[INTERNALS] = { method, redirect: init.redirect || input.redirect || 'follow', headers, parsedURL, signal, ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, family, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, } // node-fetch-only options this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20 this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true this.counter = init.counter || input.counter || 0 this.agent = init.agent || input.agent } get method () { return this[INTERNALS].method } get url () { return this[INTERNALS].parsedURL.toString() } get headers () { return this[INTERNALS].headers } get redirect () { return this[INTERNALS].redirect } get signal () { return this[INTERNALS].signal } clone () { return new Request(this) } get [Symbol.toStringTag] () { return 'Request' } static getNodeRequestOptions (request) { const parsedURL = request[INTERNALS].parsedURL const headers = new Headers(request[INTERNALS].headers) // fetch step 1.3 if (!headers.has('Accept')) { headers.set('Accept', '*/*') } // Basic fetch if (!/^https?:$/.test(parsedURL.protocol)) { throw new TypeError('Only HTTP(S) protocols are supported') } if (request.signal && Minipass.isStream(request.body) && typeof request.body.destroy !== 'function') { throw new Error( 'Cancellation of streamed requests with AbortSignal is not supported') } // HTTP-network-or-cache fetch steps 2.4-2.7 const contentLengthValue = (request.body === null || request.body === undefined) && /^(POST|PUT)$/i.test(request.method) ? '0' : request.body !== null && request.body !== undefined ? getTotalBytes(request) : null if (contentLengthValue) { headers.set('Content-Length', contentLengthValue + '') } // HTTP-network-or-cache fetch step 2.11 if (!headers.has('User-Agent')) { headers.set('User-Agent', defaultUserAgent) } // HTTP-network-or-cache fetch step 2.15 if (request.compress && !headers.has('Accept-Encoding')) { headers.set('Accept-Encoding', 'gzip,deflate') } const agent = typeof request.agent === 'function' ? request.agent(parsedURL) : request.agent if (!headers.has('Connection') && !agent) { headers.set('Connection', 'close') } // TLS specific options that are handled by node const { ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, family, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, } = request[INTERNALS] // HTTP-network fetch step 4.2 // chunked encoding is handled by Node.js // we cannot spread parsedURL directly, so we have to read each property one-by-one // and map them to the equivalent https?.request() method options const urlProps = { auth: parsedURL.username || parsedURL.password ? `${parsedURL.username}:${parsedURL.password}` : '', host: parsedURL.host, hostname: parsedURL.hostname, path: `${parsedURL.pathname}${parsedURL.search}`, port: parsedURL.port, protocol: parsedURL.protocol, } return { ...urlProps, method: request.method, headers: exportNodeCompatibleHeaders(headers), agent, ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, family, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, } } } module.exports = Request Object.defineProperties(Request.prototype, { method: { enumerable: true }, url: { enumerable: true }, headers: { enumerable: true }, redirect: { enumerable: true }, clone: { enumerable: true }, signal: { enumerable: true }, }) �������������������������������minipass-fetch-2.1.2/lib/response.js����������������������������������������������������������������0000664�0000000�0000000�00000003640�14300722347�0017375�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const http = require('http') const { STATUS_CODES } = http const Headers = require('./headers.js') const Body = require('./body.js') const { clone, extractContentType } = Body const INTERNALS = Symbol('Response internals') class Response extends Body { constructor (body = null, opts = {}) { super(body, opts) const status = opts.status || 200 const headers = new Headers(opts.headers) if (body !== null && body !== undefined && !headers.has('Content-Type')) { const contentType = extractContentType(body) if (contentType) { headers.append('Content-Type', contentType) } } this[INTERNALS] = { url: opts.url, status, statusText: opts.statusText || STATUS_CODES[status], headers, counter: opts.counter, trailer: Promise.resolve(opts.trailer || new Headers()), } } get trailer () { return this[INTERNALS].trailer } get url () { return this[INTERNALS].url || '' } get status () { return this[INTERNALS].status } get ok () { return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300 } get redirected () { return this[INTERNALS].counter > 0 } get statusText () { return this[INTERNALS].statusText } get headers () { return this[INTERNALS].headers } clone () { return new Response(clone(this), { url: this.url, status: this.status, statusText: this.statusText, headers: this.headers, ok: this.ok, redirected: this.redirected, trailer: this.trailer, }) } get [Symbol.toStringTag] () { return 'Response' } } module.exports = Response Object.defineProperties(Response.prototype, { url: { enumerable: true }, status: { enumerable: true }, ok: { enumerable: true }, redirected: { enumerable: true }, statusText: { enumerable: true }, headers: { enumerable: true }, clone: { enumerable: true }, }) ������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/map.js�������������������������������������������������������������������������0000664�0000000�0000000�00000000074�14300722347�0015544�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������module.exports = test => test.replace(/^test[/\\]/, 'lib/') ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/package.json�������������������������������������������������������������������0000664�0000000�0000000�00000003267�14300722347�0016726�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������{ "name": "minipass-fetch", "version": "2.1.2", "description": "An implementation of window.fetch in Node.js using Minipass streams", "license": "MIT", "main": "lib/index.js", "scripts": { "test": "tap", "snap": "tap", "preversion": "npm test", "postversion": "npm publish", "postpublish": "git push origin --follow-tags", "lint": "eslint \"**/*.js\"", "postlint": "template-oss-check", "lintfix": "npm run lint -- --fix", "prepublishOnly": "git push origin --follow-tags", "posttest": "npm run lint", "template-oss-apply": "template-oss-apply --force" }, "tap": { "coverage-map": "map.js", "check-coverage": true }, "devDependencies": { "@npmcli/eslint-config": "^3.0.1", "@npmcli/template-oss": "3.5.0", "@ungap/url-search-params": "^0.2.2", "abort-controller": "^3.0.0", "abortcontroller-polyfill": "~1.7.3", "encoding": "^0.1.13", "form-data": "^4.0.0", "nock": "^13.2.4", "parted": "^0.1.1", "string-to-arraybuffer": "^1.0.2", "tap": "^16.0.0" }, "dependencies": { "minipass": "^3.1.6", "minipass-sized": "^1.0.3", "minizlib": "^2.1.2" }, "optionalDependencies": { "encoding": "^0.1.13" }, "repository": { "type": "git", "url": "https://github.com/npm/minipass-fetch.git" }, "keywords": [ "fetch", "minipass", "node-fetch", "window.fetch" ], "files": [ "bin/", "lib/" ], "engines": { "node": "^12.13.0 || ^14.15.0 || >=16.0.0" }, "author": "GitHub Inc.", "templateOSS": { "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", "version": "3.5.0" } } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/��������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14300722347�0015407�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/abort-error.js������������������������������������������������������������0000664�0000000�0000000�00000000446�14300722347�0020207�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const AbortError = require('../lib/abort-error.js') const t = require('tap') const ae = new AbortError('foo') t.match(ae, { name: 'AbortError', stack: String, code: 'FETCH_ABORTED', type: 'aborted', }) ae.name = 'foo' t.equal(ae.name, 'AbortError', 'cannot override name') ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/blob.js�������������������������������������������������������������������0000664�0000000�0000000�00000003300�14300722347�0016657�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const Blob = require('../lib/blob.js') const t = require('tap') const stringToArrayBuffer = require('string-to-arraybuffer') t.test('null case', t => { const b = new Blob() t.equal(b.toString(), '[object Blob]') return b.text().then(res => t.equal(res, '')) .then(() => b.arrayBuffer()) .then(buf => t.match(buf, Buffer.alloc(0))) }) t.test('mix of stuff', t => { const b = new Blob([ Buffer.from('one'), ' ', stringToArrayBuffer('two'), ' ', new Uint8Array(stringToArrayBuffer('three')), new Blob(' '), { toString () { return 'four' } }, ], { type: 'foo' }) const x = 'one two three four' t.equal(b.type, 'foo') t.equal(b.size, x.length) return b.text() .then(text => t.equal(text, x)) .then(() => b.stream()) .then(s => s.concat()) .then(s => t.equal(s.toString(), x)) .then(() => b.arrayBuffer()) .then(ab => t.match(Buffer.from(ab), Buffer.from(x))) }) t.test('slice', t => { const b = new Blob('1 2 3 4', { type: 'x' }) const b1 = b.slice(2) t.equal(b1.type, '') const b2 = b.slice(2, 4, 'type') t.equal(b2.type, 'type') const b3 = b.slice(2, -2) const b4 = b.slice(-4) const b5 = b.slice(4, -4) const b6 = b.slice() return Promise.all([ b1.text(), b2.text(), b3.text(), b4.text(), b5.text(), b6.text(), ]).then(([t1, t2, t3, t4, t5, t6]) => t.strictSame({ t1, t2, t3, t4, t5, t6 }, { t1: '2 3 4', t2: '2 ', t3: '2 3', t4: ' 3 4', t5: '', t6: '1 2 3 4', })) }) t.test('expose the BUFFER symbol as read-only static property', t => { t.match(Blob.BUFFER, Symbol('buffer')) t.throws(() => Blob.BUFFER = 'fubber') t.end() }) ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/body.js�������������������������������������������������������������������0000664�0000000�0000000�00000030024�14300722347�0016701�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const t = require('tap') const Body = require('../lib/body.js') const { URLSearchParams } = require('url') const stringToArrayBuffer = require('string-to-arraybuffer') const URLSearchParamsPolyfill = require('@ungap/url-search-params') const Blob = require('../lib/blob') const FormData = require('form-data') const Minipass = require('minipass') const MinipassSized = require('minipass-sized') const AbortError = require('../lib/abort-error.js') const { PassThrough } = require('stream') t.test('null body', async t => { const b = new Body() t.equal(b.body, null) t.equal(Body.extractContentType(b.body), null) t.equal(Body.getTotalBytes(b), 0) t.match(await b.buffer(), Buffer.alloc(0)) }) t.test('url search params', async t => { const b = new Body(new URLSearchParams('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) t.equal(Body.getTotalBytes(b), 3) }) t.test('url search params polyfill', async t => { const b = new Body(new URLSearchParamsPolyfill('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) t.equal(Body.getTotalBytes(b), 3) }) t.test('url search params by another name', async t => { const b = new Body(new (class Florb extends URLSearchParams {})('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) t.equal(Body.getTotalBytes(b), 3) }) t.test('url search params by an even differenter name', async t => { const b = new Body(new (class Florb extends URLSearchParams { get [Symbol.toStringTag] () { return 'Florb' } })('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) t.equal(Body.getTotalBytes(b), 3) }) t.test('form-data', async t => { const f = new FormData() f.append('a', '1') const b = new Body(f) t.match(b.body.getBuffer().toString(), ` Content-Disposition: form-data; name="a"\r \r 1\r `) t.equal(Body.extractContentType(b.body), 'multipart/form-data;boundary=' + f.getBoundary()) t.equal(Body.getTotalBytes(b), f.getBuffer().length) }) t.test('blob body', async t => { const b = new Body(new Blob('a=1', { type: 'foo', size: 3 })) b.url = 'double' t.equal(Body.getTotalBytes(b), 3) t.equal(Body.extractContentType(b.body), 'foo') t.equal(b.bodyUsed, false) t.equal(await b.text(), 'a=1') t.equal(b.bodyUsed, true) await t.rejects(() => b.buffer(), TypeError) }) t.test('blob body no content-type', async t => { const b = new Body(new Blob('a=1', { size: 3 })) b.headers = { get () {} } t.match(await b.blob(), { [Blob.BUFFER]: Buffer.from('a=1'), size: 3, type: '', }) }) t.test('blob body with content-type', async t => { const b = new Body(new Blob('a=1', { size: 3 })) b.headers = { get () { return 'glerb' } } t.match(await b.blob(), { [Blob.BUFFER]: Buffer.from('a=1'), size: 3, type: 'glerb', }) }) t.test('buffer body', async t => { const b = new Body(Buffer.from('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) t.equal(await b.arrayBuffer().then(buf => Buffer.from(buf).toString()), 'a=1') }) t.test('array buffer body', async t => { const b = new Body(stringToArrayBuffer('a=1')) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) }) t.test('uint 8 array body', async t => { const b = new Body(new Uint8Array(stringToArrayBuffer('a=1'))) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) }) t.test('stream body', async t => { const b = new Body(new Minipass({ encoding: 'utf8' }).end('a=1')) t.equal(Body.extractContentType(b.body), null) t.equal(await b.text(), 'a=1') }) t.test('stream body with size', async t => { const b = new Body(new Minipass({ encoding: 'utf8' }).end('a=1'), { size: 3 }) t.equal(Body.extractContentType(b.body), null) t.equal(await b.text(), 'a=1') }) t.test('stream body with size thats already checking size', async t => { const b = new Body(new MinipassSized({ size: 3, encoding: 'utf8' }).end('a=1'), { size: 3 }) t.equal(Body.extractContentType(b.body), null) t.equal(await b.text(), 'a=1') }) t.test('stream body that is a core stream', async t => { const b = new Body(new PassThrough({ encoding: 'utf8' }).end('a=1')) t.equal(Body.extractContentType(b.body), null) t.equal(await b.text(), 'a=1') }) t.test('stream body goes too long', async t => { const b = new Body(new PassThrough({ encoding: 'utf8' }).end('a=1'), { size: 1 }) t.equal(Body.extractContentType(b.body), null) await t.rejects(b.text(), { name: 'FetchError', code: 'EBADSIZE', }) }) t.test('simulated buffer creation problem', async t => { const s = new PassThrough() const b = new Body(s) b.url = 'xyz' setTimeout(() => s.emit('error', new RangeError('hello'))) await t.rejects(b.buffer(), { name: 'FetchError', message: 'Could not create Buffer from response body for xyz: hello', type: 'system', }) }) t.test('stream body too slow', async t => { const b = new Body(new Minipass(), { timeout: 1 }) b.url = 'sloowwwwww' // keep the process open, like the actual HTTP channel would setTimeout(() => {}, 10) await t.rejects(b.text(), { name: 'FetchError', message: 'Response timeout while trying to fetch sloowwwwww (over 1ms)', type: 'body-timeout', code: 'FETCH_ERROR', errno: 'FETCH_ERROR', }) }) t.test('no timeout if stream ends before we even start consuming', async t => { // this test mimics how lib/index.js writes data into the intermediary minipass stream // the SlowMinipass class delays the result from concat() mimicking a slow pipe downstream class SlowMinipass extends Minipass { async concat () { // 10 millisecond delay before resolving await new Promise((resolve) => setTimeout(resolve, 10)) return super.concat() } } const networkStream = new Minipass() const wrappedStream = new SlowMinipass() networkStream.on('data', (chunk) => wrappedStream.write(chunk)) networkStream.on('end', () => wrappedStream.end()) for (let i = 0; i < 10; ++i) { networkStream.write('some data') } networkStream.end() // timeout of 1ms, must be lower than the 10ms used in SlowMinipass to trigger the bug const b = new Body(wrappedStream, { timeout: 1 }) await t.resolves(b.text(), 'some data') }) t.test('random toString-ing thing body', async t => { const b = new Body({ toString () { return 'a=1' } }) t.equal(b.body.toString(), 'a=1') t.equal(Body.extractContentType(b.body), null) }) t.test('set size and timeout', async t => { const b = new Body('a=1', { size: 3, timeout: 1000 }) t.equal(b.size, 3) t.equal(b.timeout, 1000) t.equal(Body.extractContentType(b.body), null) }) t.test('body stream emits error', async t => { const errorer = new Minipass() const b = new Body(errorer) b.url = 'glorp' errorer.emit('error', new Error('poop')) await t.rejects(b.buffer(), { name: 'FetchError', message: 'Invalid response while trying to fetch glorp: poop', type: 'system', }) }) t.test('body stream emits AbortError', async t => { const aborter = new Minipass() const b = new Body(aborter) b.url = 'retroba' aborter.emit('error', new AbortError('bork')) await t.rejects(b.buffer(), { name: 'AbortError', message: 'bork', }) }) t.test('more static method coverage', async t => { t.equal(Body.extractContentType('a=1'), 'text/plain;charset=UTF-8') t.equal(Body.extractContentType(new URLSearchParams('a=1')), 'application/x-www-form-urlencoded;charset=UTF-8') t.equal(Body.extractContentType(stringToArrayBuffer('a=1')), null) t.equal(Body.extractContentType(new Uint8Array(stringToArrayBuffer('a=1'))), null) t.equal(Body.extractContentType(new Blob()), null) t.equal(Body.extractContentType({}), 'text/plain;charset=UTF-8') t.equal(Body.getTotalBytes({ body: {} }), null) }) t.test('json FetchError', async t => { t.same(await new Body('{"a":1}').json(), { a: 1 }) await t.rejects(Object.assign(new Body('a=1'), { url: 'asdf' }).json(), { name: 'FetchError', message: 'invalid json response body at asdf reason: ' + 'Unexpected token a in JSON at position 0', type: 'invalid-json', }) }) t.test('json body error', async t => { const s = new PassThrough() const b = new Body(s) b.url = 'xyz' setTimeout(() => s.emit('error', new RangeError('hello'))) await t.rejects(b.json(), { name: 'FetchError', message: 'Could not create Buffer from response body for xyz: hello', type: 'system', }) }) t.test('handles environments where setTimeout does not have unref', async t => { const originalSetTimeout = setTimeout // simulate environments without unref() global.setTimeout = (func, time) => Object.assign(originalSetTimeout(func, time), { unref: null }) t.teardown(() => global.setTimeout = originalSetTimeout) t.doesNotThrow(async () => { const b = new Body(new Blob('a=1'), { timeout: 100 }) await b.text() t.end() }) }) t.test('write to streams', async t => { const w = body => Body.writeToStream( new Minipass({ encoding: 'utf8' }), { body } ).concat() t.equal(await w(), '') t.equal(await w(new Blob()), '') t.equal(await w('a=1'), 'a=1') t.equal(await w(Buffer.from('a=1')), 'a=1') t.equal(await w(new Minipass().end('a=1')), 'a=1') const s = new Minipass() setTimeout(() => s.emit('error', new Error('asdf'))) await t.rejects(w(s), { message: 'asdf' }) }) t.test('clone', t => { t.test('clone after use throws', async t => { const b = new Body('a=1') await b.text() t.throws(() => Body.clone(b), { message: 'cannot clone body after it is used', }) }) t.test('clone formdata returns the form data', async t => { const f = new FormData() f.append('a', '1') const b = new Body(f) t.equal(Body.clone(b), f) }) t.test('clone buffer returns the buffer', async t => { const buf = Buffer.from('a=1') const b = new Body(buf) t.equal(Body.clone(b), buf) }) t.test('clone stream tees the stream', async t => { const mp = new Minipass().end('a=1') const b = new Body(mp) const cloned = Body.clone(b) t.not(cloned, mp, 'new stream') t.not(b.body, mp, 'original body gets new stream') t.equal((await cloned.concat()).toString(), 'a=1') t.equal(await b.text(), 'a=1') }) t.test('clone stream proxies errors to both', t => { const mp = new Minipass().end('a=1') const b = new Body(mp) const cloned = Body.clone(b) const x = new Error('yolo') t.plan(2) cloned.once('error', er => t.equal(er, x)) b.body.once('error', er => t.equal(er, x)) setTimeout(() => mp.emit('error', x)) }) t.end() }) t.test('convert body', t => { const { convert } = require('encoding') t.test('content-type header', async t => { const s = '中文' const b = new Body(convert(s, 'gbk')) b.headers = { get () { return 'text/plain; charset=gbk; qs=1' } } t.equal(await b.textConverted(), s) }) t.test('html4 meta tag', async t => { const s = '<meta http-equiv="Content-Type" content="text/html; charset=gbk"><div>中文L</div>' const b = new Body(convert(s, 'gbk')) t.equal(await b.textConverted(), s) }) t.test('html4 meta tag reversed', async t => { const s = '<meta content="text/html; charset=gbk" http-equiv="Content-Type"><div>中文L</div>' const b = new Body(convert(s, 'gbk')) t.equal(await b.textConverted(), s) }) t.test('html5 meta tag', async t => { const s = '<meta charset="gbk"><div>中文</div>' const b = new Body(convert(s, 'gbk')) t.equal(await b.textConverted(), s) }) t.test('xml encoding', async t => { const s = '<?xml encoding="gbk"?><div>中文</div>' const b = new Body(convert(s, 'gbk')) t.equal(await b.textConverted(), s) }) t.test('explicitly utf8', async t => { const s = '<?xml encoding="UTF-8"?><div>中文</div>' const b = new Body(s) t.equal(await b.textConverted(), s) }) t.test('no encoding set', async t => { const s = '中文' const b = new Body(s) t.equal(await b.textConverted(), s) }) t.end() }) ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/fetch-error.js������������������������������������������������������������0000664�0000000�0000000�00000003702�14300722347�0020167�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������'use strict' const FetchError = require('../lib/fetch-error.js') const t = require('tap') t.test('no underlying error', t => { const fe = new FetchError('foo') t.match(fe, { message: 'foo', code: 'FETCH_ERROR', errno: 'FETCH_ERROR', type: undefined, stack: String, name: 'FetchError', constructor: FetchError, }) fe.name = 'fooblz' t.equal(fe.name, 'FetchError', 'cannot override name') t.equal(Object.prototype.toString.call(fe), '[object FetchError]', 'sets toStringTag') t.equal(String(fe), 'FetchError: foo', 'name shows up in toString') t.end() }) t.test('with underlying error', t => { const fe = new FetchError('xyz', 'xyz-problem', Object.assign(new Error('abc'), { code: 'ABC_ERROR', rando: 'property', })) t.match(fe, { message: 'xyz', code: 'ABC_ERROR', errno: 'ABC_ERROR', rando: 'property', type: 'xyz-problem', stack: String, name: 'FetchError', constructor: FetchError, }) t.end() }) t.test('special handling of EBADSIZE', t => { const fe = new FetchError('xyz', 'xyz-problem', Object.assign(new Error('abc'), { code: 'EBADSIZE', expect: 5, found: 50, })) t.match(fe, { message: 'xyz', code: 'EBADSIZE', errno: 'EBADSIZE', type: 'max-size', expect: 5, found: 50, stack: String, name: 'FetchError', constructor: FetchError, }) t.end() }) t.test('create custom FetchError', function funcName (t) { const systemError = new Error('system') systemError.code = 'ESOMEERROR' const err = new FetchError('test message', 'test-error', systemError) t.match(err, Error) t.match(err, FetchError) t.equal(err.name, 'FetchError') t.equal(err.message, 'test message') t.equal(err.type, 'test-error') t.equal(err.code, 'ESOMEERROR') t.equal(err.errno, 'ESOMEERROR') // reading the stack is quite slow (~30-50ms) t.match(err.stack, `${err.name}: ${err.message}`) t.match(err.stack, 'funcName') t.end() }) ��������������������������������������������������������������minipass-fetch-2.1.2/test/fixtures/�����������������������������������������������������������������0000775�0000000�0000000�00000000000�14300722347�0017260�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/fixtures/dummy.txt��������������������������������������������������������0000664�0000000�0000000�00000000015�14300722347�0021150�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������i am a dummy �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������minipass-fetch-2.1.2/test/fixtures/server.js��������������������������������������������������������0000664�0000000�0000000�00000025266�14300722347�0021137�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������const http = require('http') const zlib = require('minizlib') const { multipart: Multipart } = require('parted') const convert = require('encoding').convert class TestServer { constructor () { this.server = http.createServer((req, res) => this.router(req, res)) this.port = 30000 + (+process.env.TAP_CHILD_ID || 1) this.hostname = 'localhost' // node 8 default keepalive timeout is 5000ms // make it shorter here as we want to close server // quickly at the end of tests this.server.keepAliveTimeout = 1000 this.server.on('error', err => console.log(err.stack)) this.server.on('connection', socket => socket.setTimeout(1500)) } start (cb) { this.server.listen(this.port, this.hostname, cb) } stop (cb) { this.server.close(cb) } router (req, res) { const p = req.url if (p === '/hello') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.end('world') } if (p === '/plain') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.end('text') } if (p === '/options') { res.statusCode = 200 res.setHeader('Allow', 'GET, HEAD, OPTIONS') res.end('hello world') } if (p === '/html') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.end('<html></html>') } if (p === '/json') { res.statusCode = 200 res.setHeader('Content-Type', 'application/json') res.end(JSON.stringify({ name: 'value' })) } if (p === '/gzip') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'gzip') // eslint-disable-next-line promise/catch-or-return new zlib.Gzip().end('hello world').concat().then(buf => res.end(buf)) } if (p === '/gzip-truncated') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'gzip') // eslint-disable-next-line promise/catch-or-return new zlib.Gzip().end('hello world').concat().then(buf => res.end(buf.slice(0, buf.length - 8))) } if (p === '/deflate') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'deflate') // eslint-disable-next-line promise/catch-or-return new zlib.Deflate().end('hello world').concat().then(buf => res.end(buf)) } if (p === '/brotli') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'br') // pre-compressed 'hello world', in-lined here so tests will run when the // client doesn't support brotli const buf = Buffer.from([ 0x0b, 0x05, 0x80, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x03, ]) res.end(buf) } if (p === '/deflate-raw') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'deflate') // eslint-disable-next-line promise/catch-or-return new zlib.DeflateRaw().end('hello world').concat().then(buf => res.end(buf)) } if (p === '/sdch') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'sdch') res.end('fake sdch string') } if (p === '/invalid-content-encoding') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'gzip') res.end('fake gzip string') } if (p === '/timeout') { setTimeout(() => { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.end('text') }, 1000) } if (p === '/slow') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.write('test') setTimeout(() => res.end('test'), 1000) } if (p === '/cookie') { res.statusCode = 200 res.setHeader('Set-Cookie', ['a=1', 'b=1']) res.end('cookie') } if (p === '/size/chunk') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') setTimeout(() => res.write('test'), 10) setTimeout(() => res.end('test'), 20) } if (p === '/size/long') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') res.end('testtest') } if (p === '/encoding/gbk') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.end(convert('<meta charset="gbk"><div>中文</div>', 'gbk')) } if (p === '/encoding/gb2312') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.end(convert('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">' + '<div>中文</div>', 'gb2312')) } if (p === '/encoding/gb2312-reverse') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.end(convert('<meta content="text/html; charset=gb2312" http-equiv="Content-Type">' + '<div>中文</div>', 'gb2312')) } if (p === '/encoding/shift-jis') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html; charset=Shift-JIS') res.end(convert('<div>日本語</div>', 'Shift_JIS')) } if (p === '/encoding/euc-jp') { res.statusCode = 200 res.setHeader('Content-Type', 'text/xml') res.end(convert('<?xml version="1.0" encoding="EUC-JP"?><title>日本語', 'EUC-JP')) } if (p === '/encoding/utf8') { res.statusCode = 200 res.end('中文') } if (p === '/encoding/order1') { res.statusCode = 200 res.setHeader('Content-Type', 'charset=gbk; text/plain') res.end(convert('中文', 'gbk')) } if (p === '/encoding/order2') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain; charset=gbk; qs=1') res.end(convert('中文', 'gbk')) } if (p === '/encoding/chunked') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.setHeader('Transfer-Encoding', 'chunked') res.write('a'.repeat(10)) res.end(convert('' + '
日本語
', 'Shift_JIS')) } if (p === '/encoding/invalid') { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') res.setHeader('Transfer-Encoding', 'chunked') res.write('a'.repeat(1200)) res.end(convert('中文', 'gbk')) } if (p === '/redirect/301') { res.statusCode = 301 res.setHeader('Location', '/inspect') res.end() } if (p === '/redirect/302') { res.statusCode = 302 res.setHeader('Location', '/inspect') res.end() } if (p === '/redirect/303') { res.statusCode = 303 res.setHeader('Location', '/inspect') res.end() } if (p === '/redirect/307') { res.statusCode = 307 res.setHeader('Location', '/inspect') res.end() } if (p === '/redirect/308') { res.statusCode = 308 res.setHeader('Location', '/inspect') res.end() } if (p === '/redirect/chain') { res.statusCode = 301 res.setHeader('Location', '/redirect/301') res.end() } if (p === '/redirect/no-location') { res.statusCode = 301 res.end() } if (p === '/redirect/slow') { res.statusCode = 301 res.setHeader('Location', '/redirect/301') setTimeout(() => res.end(), 1000) } if (p === '/redirect/slow-chain') { res.statusCode = 301 res.setHeader('Location', '/redirect/slow') setTimeout(() => res.end(), 10) } if (p === '/redirect/slow-stream') { res.statusCode = 301 res.setHeader('Location', '/slow') res.end() } if (p === '/error/400') { res.statusCode = 400 res.setHeader('Content-Type', 'text/plain') res.end('client error') } if (p === '/error/404') { res.statusCode = 404 res.setHeader('Content-Encoding', 'gzip') res.end() } if (p === '/error/500') { res.statusCode = 500 res.setHeader('Content-Type', 'text/plain') res.end('server error') } if (p === '/error/reset') { res.destroy() } if (p === '/error/json') { res.statusCode = 200 res.setHeader('Content-Type', 'application/json') res.end('invalid json') } if (p === '/no-content') { res.statusCode = 204 res.end() } if (p === '/no-content/gzip') { res.statusCode = 204 res.setHeader('Content-Encoding', 'gzip') res.end() } if (p === '/no-content/brotli') { res.statusCode = 204 res.setHeader('Content-Encoding', 'br') res.end() } if (p === '/not-modified') { res.statusCode = 304 res.end() } if (p === '/not-modified/gzip') { res.statusCode = 304 res.setHeader('Content-Encoding', 'gzip') res.end() } if (p === '/inspect') { res.statusCode = 200 res.setHeader('Content-Type', 'application/json') let body = '' req.on('data', c => body += c) req.on('end', () => res.end(JSON.stringify({ method: req.method, url: req.url, headers: req.headers, body, }))) } if (p === '/multipart') { res.statusCode = 200 res.setHeader('Content-Type', 'application/json') // the path option passed to the Multipart constructor cannot be an // absolute path in Windows, we set it here manually because the default // provided by 'parsed' is an absolute path // ref: https://github.com/chjj/parsed/issues/10 const parser = new Multipart(req.headers['content-type'], { path: './' }) let body = '' parser.on('part', (field, part) => body += field + '=' + part) parser.on('end', () => res.end(JSON.stringify({ method: req.method, url: req.url, headers: req.headers, body: body, }))) req.pipe(parser) } if (p === '/trailers') { res.statusCode = 200 res.setHeader('Transfer-Encoding', 'chunked') res.setHeader('Trailer', 'X-Node-Fetch') res.write('Body of the response') res.addTrailers({ 'X-Node-Fetch': 'hello world!' }) res.end() } if (p === '/host-redirect') { if (req.headers.host !== `localhost:${this.port}`) { res.setHeader('location', `http://localhost:${this.port}/host-redirect`) res.statusCode = 302 } res.end(`http://${req.headers.host}/host-redirect`) } } } module.exports = TestServer if (require.main === module) { const server = new TestServer() server.start(() => console.log(`Server started listening at port ${server.port}`)) } minipass-fetch-2.1.2/test/fixtures/tls/000077500000000000000000000000001430072234700200625ustar00rootroot00000000000000minipass-fetch-2.1.2/test/fixtures/tls/challenge-password.txt000066400000000000000000000000121430072234700243760ustar00rootroot00000000000000challenge minipass-fetch-2.1.2/test/fixtures/tls/localhost.crt000066400000000000000000000031331430072234700225640ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIEhTCCA22gAwIBAgIJAKuew+59rKlnMA0GCSqGSIb3DQEBCwUAMIGjMQswCQYD VQQGEwJVUzELMAkGA1UECAwCQ0ExEDAOBgNVBAcMB09ha2xhbmQxFzAVBgNVBAoM Dk1pbmlwYXNzLCBJbmMuMSowKAYDVQQLDCFEZXBhcnRtZW50IG9mIE1ha2luZyBG ZXRjaCBIYXBwZW4xFzAVBgNVBAMMDm1pbmlwYXNzLWZldGNoMRcwFQYJKoZIhvcN AQkBFghpQGl6cy5tZTAeFw0yMDA3MjEyMjAzNTFaFw0yMjEwMjQyMjAzNTFaMGcx CzAJBgNVBAYTAnh5MQswCQYDVQQIDAJhYjELMAkGA1UEBwwCaWYxCzAJBgNVBAoM AmdvMQswCQYDVQQLDAJhbjELMAkGA1UEAwwCc3QxFzAVBgkqhkiG9w0BCQEWCGlA aXpzLm1lMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtLC1Zmxxwo5x 3J4ApumdlRzkymeLgeyxPO+wZxDkOxMk0PVkhDvZdLfXWxGM/jMWGxcMzOFAgaPP XxFafPes4tRc28gdFBnr16nUeoWUF2ReMcHIk8nxq0wtV+lubkgaGcvUslS694VK a2gNXPYHpTzuRFMLqN2FlFDrS2/QB3mwbG3OJOwxsxOVxUboUfxbXweJB3mJy0r3 LU81+LZjTqV0EY+RduIzhsfyhhatPXbS1S9XRZ4JMzxYZsxZueTDp0Ih/Y5elN9F rq+7UvGZLQl7+j3pnPWDkv8PxO5JVpFVfH2WHJqGyMvSm7W+St3PgyrVcMFh654n 0+d4LEA+awIDAQABo4H2MIHzMIHCBgNVHSMEgbowgbehgamkgaYwgaMxCzAJBgNV BAYTAlVTMQswCQYDVQQIDAJDQTEQMA4GA1UEBwwHT2FrbGFuZDEXMBUGA1UECgwO TWluaXBhc3MsIEluYy4xKjAoBgNVBAsMIURlcGFydG1lbnQgb2YgTWFraW5nIEZl dGNoIEhhcHBlbjEXMBUGA1UEAwwObWluaXBhc3MtZmV0Y2gxFzAVBgkqhkiG9w0B CQEWCGlAaXpzLm1lggkAp3aRU/tiwHQwCQYDVR0TBAIwADALBgNVHQ8EBAMCBPAw FAYDVR0RBA0wC4IJbG9jYWxob3N0MA0GCSqGSIb3DQEBCwUAA4IBAQApZMXMPu9m eNDlejni3PlO4KqnAXmMMY9SIObMTtuQtMdujPRqwSqegQUOZN5wfoyNvPSc5wkU 8TRu1gAH+vg9WCk0X7VlXA7q5ieQCdmgdUzl0vudy3omK9YN/6g7svBwxqn0B/g4 j0wExC+RF6MfcF9ycOVSi7ppBUqA7UksbPb0tYNNulDWn3TnrNLiJdEI7SDRZG5f iB0P7h0PlHkp08Me8iqWtGlzLlDbObW57uwBXdCKHSS+/z+zrnmplkBRNICgs4Bt NukMjBmoeFTrzGyStr83VoKqLEd/CtldkZoQ/v37otLuJROQxoulWQ47dyqs6CS7 LUwcQI3YOXkS -----END CERTIFICATE----- minipass-fetch-2.1.2/test/fixtures/tls/localhost.csr000066400000000000000000000017551430072234700225730ustar00rootroot00000000000000-----BEGIN CERTIFICATE REQUEST----- MIICrDCCAZQCAQAwZzELMAkGA1UEBhMCeHkxCzAJBgNVBAgMAmFiMQswCQYDVQQH DAJpZjELMAkGA1UECgwCZ28xCzAJBgNVBAsMAmFuMQswCQYDVQQDDAJzdDEXMBUG CSqGSIb3DQEJARYIaUBpenMubWUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK AoIBAQC0sLVmbHHCjnHcngCm6Z2VHOTKZ4uB7LE877BnEOQ7EyTQ9WSEO9l0t9db EYz+MxYbFwzM4UCBo89fEVp896zi1FzbyB0UGevXqdR6hZQXZF4xwciTyfGrTC1X 6W5uSBoZy9SyVLr3hUpraA1c9gelPO5EUwuo3YWUUOtLb9AHebBsbc4k7DGzE5XF RuhR/FtfB4kHeYnLSvctTzX4tmNOpXQRj5F24jOGx/KGFq09dtLVL1dFngkzPFhm zFm55MOnQiH9jl6U30Wur7tS8ZktCXv6Pemc9YOS/w/E7klWkVV8fZYcmobIy9Kb tb5K3c+DKtVwwWHrnifT53gsQD5rAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEA PaDycMfRTHep7etzRB+KCkETU/dr5aTRQQOttJDykZgXhhyMvVm7ZQa/bASkikCV RemiCDYQvSHayBJ0FivxbyPwc0BDO9ucXTZ5+PDmQeqjo+nHXWVrDwyE6Y95wZG8 5mI35yv6bw3OLy0KysV/FTDy0z9njzSYCed9SblLUW7SF0AlAEE//djsAdwNODov Nyy5DYpLDYVb00hAYDscf+pkdeckzvfqIWfe8MWf92jbGzlmMBedeNYsZxFvRAgB /KhxeviODBHpvCiusHL9VQLwdx0InZf4+BVNeBMn7AFKm+Zc6iGi8AFZzICg5NFj IiD6tuE4ZWJhqcWUhbQeFQ== -----END CERTIFICATE REQUEST----- minipass-fetch-2.1.2/test/fixtures/tls/localhost.ext000066400000000000000000000003071430072234700225740ustar00rootroot00000000000000authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName = @alt_names [alt_names] DNS.1 = localhost minipass-fetch-2.1.2/test/fixtures/tls/localhost.key000066400000000000000000000032131430072234700225630ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAtLC1Zmxxwo5x3J4ApumdlRzkymeLgeyxPO+wZxDkOxMk0PVk hDvZdLfXWxGM/jMWGxcMzOFAgaPPXxFafPes4tRc28gdFBnr16nUeoWUF2ReMcHI k8nxq0wtV+lubkgaGcvUslS694VKa2gNXPYHpTzuRFMLqN2FlFDrS2/QB3mwbG3O JOwxsxOVxUboUfxbXweJB3mJy0r3LU81+LZjTqV0EY+RduIzhsfyhhatPXbS1S9X RZ4JMzxYZsxZueTDp0Ih/Y5elN9Frq+7UvGZLQl7+j3pnPWDkv8PxO5JVpFVfH2W HJqGyMvSm7W+St3PgyrVcMFh654n0+d4LEA+awIDAQABAoIBAClb3VnBbtSiuEtQ W0PZa3mLMI9n3hXyMKuLDay5wBQJkL7HvKdL7714qzGsNcKlvOSchRCMarCB52CS X00FgCw3gb3I82b7e/FUbU9SLhCgp7Lp8VhqvAeCm0ppIx7ZIChRcLEVFeq8NsmY +p4RrrRS2xMGkz+m3QGS+Bi/UjWzcPX3qlDaeWxP3Xzih/XDfYDdQei+DQXbsfZH 3pngiqC7/tVef6ckBYMcEYRz2CETqQNCA35dnp/CZpQ5H5pC/qNJX7sSYJO91ZTb 5uTujt4JBRGkefzDJFuzpYRhCtiMKTRgjKwk10clOlEmclpVmeiSeenICL39Mi+e DsVnNRkCgYEA76yykUX62SryuTLmo20jPzl2tHCXNRjBjpmxv43VkXhvPZJEU7fR qK/iVwNGSvNCSQxKtTByLCceJqwMb2F1V1jwkNdDJ+T1GGga8dGpOB1EESosr2vw WM6lhz3CxutoJTa7YjmziMuTOcYtE3xhzovTks68Pza0m9YoAIQ+I/8CgYEAwP96 P1YmKK8ECpWvupcBKH8CoJLbeXmlJJ9BtAdDp5PrKoLSIHNz67w7CzrpJzXZglh3 BoYgSw/1D4ChKpLSw2LzXW70/MyCcnKRYNPwwEbyKUlx8aiudH4kXtYjwgMChbXd wz30iPQ4CwNb3JLKOl8k1yZY2aG28c6tUfNetZUCgYAJ4Lc3T4gIHUIFqfhhceUK /QZMZ3uD37JSezkdKO5NYYZMJlQUkzXb2uvcJDFoc2Ae/JezofyCn1YZx+t3R6/7 WpoHjiehZElJqTi7EKYFvwcIIhHXZP5x2opt6Xi2lAslxXyxjqk8kQ9PSUCgVfb9 +TtOCKEvhcSpy4i4hLq+5wKBgEK40AmPffe4sdv67drDE7ptVnou60Nuw6IKkMtt a31GzRlQSta/M0c/NuZmAm701fKTJOsTeZyZsq4eWRl/0u+LiPk3P+kZxstMQmhI PUYsANI3OvZBy7YoWeiTfZ84LSoOutEh3SVv0OQ10A9MjC9r7y+WaUcr+jRUsGTR j1+VAoGBAOxP7YcUuPH8DXxoybVAOUAvM6URW+E4fYg75YsKNzpdbDXhG+6V1FT8 NJZAipbOx1nBRfhfqDN7+2px45oWus/nMFdkue4u8c6jY7WOgXiNJQoea7vE262M 921loT4Sjamsns2pZ7jYwVe8DW6wk/wrqin1FBukQlwH72BpUi+U -----END RSA PRIVATE KEY----- minipass-fetch-2.1.2/test/fixtures/tls/minipass-CA.key000066400000000000000000000033271430072234700227050ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,230B31249452E992 Wwb2pZmJqVr767ZRsVAp/dOQbaKgEomAFioQh3e5Ena/ly7P0x9LJvoLHJC7Az+l wwX9MYMqxD/ZkpGi8/7KH/e6smF5t5V1fAGVxqYTwoVw8QvNyRNBoiGdV9c+HhJ8 +wt1HKSK36hvY3qJJ7Iu1K1xrvMmwyehUWddW7qbjvwZVo/GCOo7mUFkB574EyBo UgnRgLV6k/ISS6WTrrxPwkBQpt93Bc5Bv1FtXRYXJ9O1id6cA/bBc+dsy91YtrtX 71p317cfZemHpje5pU/yepE52+4LyO04lo6PMMV/jjV5cFeBgzEitaAIjsY6Y2qc /u6UDn2GN7Ir9XuJZfHhjxgZVYq3SM/iQp+Htat3XDk6S8B/kz8zEDYhqGAjMtaU gFNwCFVqnj7BDqeOescHCSwsT3wauYGGfIyzXM78Rtj+mse6dpqyWta+FYjtFuAA tm86lj0et+anm/Y/SGom6E+Rt1Hx1OucIhBuyDXx5G8W40oYjmK5mrXqC5I1Iwux QhJkMr3o90OrT3vka3hay04RjZ2iSr2bYeoP8zSTdXhnTpbewDN921Xg7YbAo1Q0 8Ozi+MgywsJvei0eY0xQD/BuWGxArLsllH+QcHqr4FScjtUlnTtSrzOuH396TwJh IP+stbuMlRCisTfzE4Ls5R6YiV3KbgdUCLjPSIq/xPzedpV+pS689eTRw8xu1ffA d2E7C5VKFjnlue+hFCS2POhEgmHGtzQQM87KT6ok1aS1nK9s5GGzOVcPSsH5jflE 7mc1yPewkBgndWjhvRf5OOX6XuBoAbQ7AbyACtEDKuH9/SDLsyihX5NrFKMRpPuq 1fFMzZ8yiB+OVATouMeMXAqgCpEzHZVOE52cjNlkHXpJ4mpuyRbmlFzBCdnrK1ar PfeUaT9//ySORWBFAQ5bZu5NIOgQMCd8LokPvos3wcknJ1lhr72cN0NquQQbfUew tq0AhQZv1WW5iUEvw7VgsPK7USt0uZfQXYrkXxq/VeBUCad2qC1QNJNKNulOHtt/ RrTCtAp+4LiXuW61NS2KiT8ijNrb1BTcZSfRPC0qKxD39cIFP6YPXppqJvJsEpJh GAXggu4O/G+RH0Bm2yN2SiN+PjAjNBIhW7/qaiQ2ZrggZ6UFX/GE7XkXJpQbmoJ1 MXmUSnkks969Zzr3XHcbuSWINYRDZ2/KmCMjHldBasT6WsZNi9n9SMPqoZiDuDQb WmwIk1kBPwuK6+Mp73rI+S3R9/6tgaKvg+OV6CERyyvD56c5JhQ8G1aEL/SkMwBY hOi7w7IHP40IbIsnVtQbv4nirCK4tZyruvoZqIEfGblO5BVa2PYGKZW98Sv+KSVU ll2DQxcn9/XfMYIFpGzV6syJU9aqd57KYamgkxx5jOwjk5WZBAxvj5ZLV8NdFpSH XiYyJ6GZodvgrZ7LezzzFNOk6likKSpVljdpaX0rlfIrMeYJayNZIYq1ZguJ9VYa FS5eu3MxVQnZPAmK0JTjLv9UJzDMxLAunU2nneHI8tooEf1U1deR5goJSX1n+kUZ qfOvnrWS8qJBsJDWiQmxzKnoEtm2pIPQDK1/UYbzj27Ks7P6z7Sphg6TB0ZhAWjh -----END RSA PRIVATE KEY----- minipass-fetch-2.1.2/test/fixtures/tls/minipass-CA.pem000066400000000000000000000025331430072234700226740ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDxjCCAq4CCQCndpFT+2LAdDANBgkqhkiG9w0BAQsFADCBozELMAkGA1UEBhMC VVMxCzAJBgNVBAgMAkNBMRAwDgYDVQQHDAdPYWtsYW5kMRcwFQYDVQQKDA5NaW5p cGFzcywgSW5jLjEqMCgGA1UECwwhRGVwYXJ0bWVudCBvZiBNYWtpbmcgRmV0Y2gg SGFwcGVuMRcwFQYDVQQDDA5taW5pcGFzcy1mZXRjaDEXMBUGCSqGSIb3DQEJARYI aUBpenMubWUwIBcNMjAwNzIxMjE0MDQzWhgPMjA3MDA3MDkyMTQwNDNaMIGjMQsw CQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEDAOBgNVBAcMB09ha2xhbmQxFzAVBgNV BAoMDk1pbmlwYXNzLCBJbmMuMSowKAYDVQQLDCFEZXBhcnRtZW50IG9mIE1ha2lu ZyBGZXRjaCBIYXBwZW4xFzAVBgNVBAMMDm1pbmlwYXNzLWZldGNoMRcwFQYJKoZI hvcNAQkBFghpQGl6cy5tZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB AL7zb0N9pSWmaFNT0zz7CEYFf7sO541NYY+Zkt2qF+beIT9M+0NtrWy8ekOtG7Fe N2/ElPXxsAtrTWRHIiRvya8iEKBZT0nuG0pAG0C+VMalzsFfPorEXFwBsFkjerL4 9iU9CV11KhVxxNnTPTkk9hjsI2xag1GP8gykoNY+j4x2uMvx3B1uPMOXNWHtNILn Sdy83if/Tvvmx9t1BEXWsMhZeI4mVE0P3TRgBglS1pvE9MF1TlB9AdsXaOgRjGeK uiuDhHM8FTduc5vYvzwu2uVVMtROB5zxX9QDNk9Bg1LjQh3vJEJZxYoB4lxJuZ4K bA+36HkJ18AFz5ae9JxEHLMCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAFLQyVAIE ImI72TPEDnpZb71xOi8m+xAGtHKOtswbf3WprE3WLnSnPP7iFkphcFhG/Jny3c25 B98SoVSOHwzYo9CvOPhcZBy1ErIv+X/gUKt2NZmX5KcwF2oH6/yUCfC5ikJpR00x bHmVa3ROoYJqfR9vH7o9lIR7Yb+Pb1yIGpEENwn3kr9UB0sA9l45TWmK1N6dPOlR BajY/AhRqoPw2miyGusJf5FaLlLNri0QkiJIyf0v/3+goayoyn/2OwqzjkR1DbPg 1KXZIMonv5UfEWVi4w+1/evNRVOB1g0v7wj//bdomGWPvFPtRJe+Zfb1jsLEpm99 N58Aw1gH7zKe9Q== -----END CERTIFICATE----- minipass-fetch-2.1.2/test/fixtures/tls/minipass-CA.srl000066400000000000000000000000211430072234700227010ustar00rootroot00000000000000C2917CDC044F93C2 minipass-fetch-2.1.2/test/fixtures/tls/passphrase.txt000066400000000000000000000000171430072234700227720ustar00rootroot00000000000000minipassphrase minipass-fetch-2.1.2/test/fixtures/tls/server.js000066400000000000000000000011221430072234700217220ustar00rootroot00000000000000const https = require('https') const { readFileSync: read } = require('fs') const { join } = require('path') const ca = read(join(__dirname, '/minipass-CA.pem')) const server = https.createServer({ key: read(join(__dirname, '/localhost.key')), cert: read(join(__dirname, '/localhost.crt')), }, (q, s) => { s.end('ok\n' + JSON.stringify(q.headers, 0, 2) + '\n') server.close() }) server.listen(8443, () => { https.get({ host: 'localhost', path: '/hello', port: 8443, ca, }, res => { console.error(res.statusCode, res.headers) res.pipe(process.stdout) }) }) minipass-fetch-2.1.2/test/fixtures/tls/setup.sh000066400000000000000000000010641430072234700215570ustar00rootroot00000000000000#!/bin/sh if [ "$#" -ne 1 ] then echo "Usage: Must supply a domain" exit 1 fi DOMAIN=$1 openssl genrsa -out $DOMAIN.key 2048 openssl req -new -key $DOMAIN.key -out $DOMAIN.csr cat > $DOMAIN.ext << EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName = @alt_names [alt_names] DNS.1 = $DOMAIN EOF openssl x509 -req -in $DOMAIN.csr -CA ./minipass-CA.pem -CAkey ./minipass-CA.key -CAcreateserial -out $DOMAIN.crt -days 825 -sha256 -extfile $DOMAIN.ext minipass-fetch-2.1.2/test/headers.js000066400000000000000000000143171430072234700173660ustar00rootroot00000000000000const t = require('tap') const Headers = require('../lib/headers.js') t.Test.prototype.addAssert('contain', 2, function (list, key, m, e) { m = m || 'expected item to be contained in list' e.found = list e.wanted = key return this.ok(list.indexOf(key) !== -1, m, e) }) t.Test.prototype.addAssert('notContain', 2, function (list, key, m, e) { m = m || 'expected item to not be contained in list' e.found = list e.wanted = key return this.notOk(list.indexOf(key) !== -1, m, e) }) t.test('should have attributes conforming to Web IDL', t => { const headers = new Headers() t.same(Object.getOwnPropertyNames(headers), []) const enumerableProperties = [] for (const property in headers) { enumerableProperties.push(property) } t.same(enumerableProperties.sort(), [ 'append', 'delete', 'entries', 'forEach', 'get', 'has', 'keys', 'set', 'values', ]) t.equal(String(headers), '[object Headers]') t.end() }) t.test('not-found key returns null', t => { const h = new Headers([['foo', 'bar']]) t.equal(h.has('baz'), false) t.equal(h.get('baz'), null) t.end() }) t.test('set two times', t => { const h = new Headers() h.set('foo', 'bar') h.set('foo', 'baz') t.equal(h.get('foo'), 'baz') h.append('foo', 'bar') t.equal(h.get('foo'), 'baz, bar') t.end() }) t.test('node compatible headers', t => { const h = new Headers() h.set('foo', 'bar') t.same(Headers.exportNodeCompatibleHeaders(h), { foo: ['bar'], }) h.set('host', 'example.com') t.same(Headers.exportNodeCompatibleHeaders(h), { foo: ['bar'], host: 'example.com', }) t.end() }) t.test('create headers lenient', t => { const h = Headers.createHeadersLenient({ '💩': ['ignore', 'these'], badList: ['ok', '💩', 'bar'], badStr: '💩', goodstr: 'good', }) t.same(Headers.exportNodeCompatibleHeaders(h), { badList: ['ok', 'bar'], goodstr: ['good'], }) t.end() }) t.test('delete', t => { const h = new Headers([['foo', 'bar']]) t.equal(h.has('foo'), true) h.delete('foo') t.equal(h.has('foo'), false) // another time just to make sure it's fine with that, and for coverage h.delete('foo') t.end() }) t.test('iterating through all headers with forEach', t => { const headers = new Headers([ ['b', '2'], ['c', '4'], ['b', '3'], ['a', '1'], ]) const result = [] headers.forEach((val, key) => { result.push([key, val]) }) t.same(result, [ ['a', '1'], ['b', '2, 3'], ['c', '4'], ]) t.end() }) t.test('iteration', t => { const headers = new Headers([ ['b', '2'], ['c', '4'], ['a', '1'], ]) headers.append('b', '3') const result = [] for (const pair of headers) { result.push(pair) } t.same(result, [ ['a', '1'], ['b', '2, 3'], ['c', '4'], ], 'iterating with for loop') t.same(Array.from(headers.entries()), [ ['a', '1'], ['b', '2, 3'], ['c', '4'], ], 'entries') const keys = headers.keys() t.equal(String(keys), '[object HeadersIterator]') t.same(Array.from(keys), ['a', 'b', 'c'], 'keys') t.same(Array.from(headers.values()), ['1', '2, 3', '4'], 'values') t.end() }) t.test('reject illegal header', t => { const headers = new Headers() t.throws(() => new Headers({ 'He y': 'ok' }), TypeError) t.throws(() => new Headers({ 'Hé-y': 'ok' }), TypeError) t.throws(() => new Headers({ 'He-y': 'ăk' }), TypeError) t.throws(() => headers.append('Hé-y', 'ok'), TypeError) t.throws(() => headers.delete('Hé-y'), TypeError) t.throws(() => headers.get('Hé-y'), TypeError) t.throws(() => headers.has('Hé-y'), TypeError) t.throws(() => headers.set('Hé-y', 'ok'), TypeError) // should reject empty header t.throws(() => headers.append('', 'ok'), TypeError) // 'o k' is valid value but invalid name new Headers({ 'He-y': 'o k' }) t.end() }) t.test('should ignore unsupported attributes while reading headers', t => { class FakeHeader {} // prototypes are currently ignored // This might change in the future: #181 FakeHeader.prototype.z = 'fake' const res = new FakeHeader() res.a = 'string' res.b = ['1', '2'] res.c = '' res.d = [] res.e = 1 res.f = [1, 2] res.g = { a: 1 } res.h = undefined res.i = null res.j = NaN res.k = true res.l = false res.m = Buffer.from('test') const h1 = new Headers(res) h1.set('n', [1, 2]) h1.append('n', ['3', 4]) const h1Raw = h1.raw() t.contain(h1Raw.a, 'string') t.contain(h1Raw.b, '1,2') t.contain(h1Raw.c, '') t.contain(h1Raw.d, '') t.contain(h1Raw.e, '1') t.contain(h1Raw.f, '1,2') t.contain(h1Raw.g, '[object Object]') t.contain(h1Raw.h, 'undefined') t.contain(h1Raw.i, 'null') t.contain(h1Raw.j, 'NaN') t.contain(h1Raw.k, 'true') t.contain(h1Raw.l, 'false') t.contain(h1Raw.m, 'test') t.contain(h1Raw.n, '1,2') t.contain(h1Raw.n, '3,4') t.equal(h1Raw.z, undefined) t.end() }) t.test('should wrap headers', t => { const h1 = new Headers({ a: '1' }) const h1Raw = h1.raw() const h2 = new Headers(h1) h2.set('b', '1') const h2Raw = h2.raw() const h3 = new Headers(h2) h3.append('a', '2') const h3Raw = h3.raw() t.contain(h1Raw.a, '1') t.notContain(h1Raw.a, '2') t.contain(h2Raw.a, '1') t.notContain(h2Raw.a, '2') t.contain(h2Raw.b, '1') t.contain(h3Raw.a, '1') t.contain(h3Raw.a, '2') t.contain(h3Raw.b, '1') t.end() }) t.test('should accept headers as an iterable of tuples', t => { let headers headers = new Headers([ ['a', '1'], ['b', '2'], ['a', '3'], ]) t.equal(headers.get('a'), '1, 3') t.equal(headers.get('b'), '2') headers = new Headers([ new Set(['a', '1']), ['b', '2'], new Map([['a', null], ['3', null]]).keys(), ]) t.equal(headers.get('a'), '1, 3') t.equal(headers.get('b'), '2') headers = new Headers(new Map([ ['a', '1'], ['b', '2'], ])) t.equal(headers.get('a'), '1') t.equal(headers.get('b'), '2') t.end() }) t.test('should throw a TypeError if non-tuple exists in a headers initializer', t => { t.throws(() => new Headers([['b', '2', 'huh?']]), TypeError) t.throws(() => new Headers(['b2']), TypeError) t.throws(() => new Headers('b2'), TypeError) t.throws(() => new Headers({ [Symbol.iterator]: 42 }), TypeError) t.end() }) minipass-fetch-2.1.2/test/https-with-ca-option.js000066400000000000000000000031611430072234700217500ustar00rootroot00000000000000// verify that passing a custom CA cert will work with minipass-fetch // Ie, going a different direction than the decision node-fetch made // https://github.com/node-fetch/node-fetch/issues/15 const t = require('tap') const fetch = require('../') const { resolve } = require('path') const fixtures = resolve(__dirname, 'fixtures/tls') const { readFileSync: read } = require('fs') const ca = read(`${fixtures}/minipass-CA.pem`) const cert = read(`${fixtures}/localhost.crt`) const key = read(`${fixtures}/localhost.key`) const { createServer } = require('https') const port = 30000 + (+process.env.TAP_CHILD_ID || 1) const base = `https://localhost:${port}/` t.test('setup server', { bail: true }, t => { const server = createServer({ cert, key, }, (q, s) => { s.setHeader('content-type', 'text/plain') s.setHeader('connection', 'close') s.end(`${q.method} ${q.url}`) }) server.listen(port, () => { t.parent.teardown(() => server.close()) t.end() }) }) t.test('make https request without ca, should fail', t => t.rejects(fetch(`${base}hello`), { name: 'FetchError', message: `request to ${base}hello failed, reason: unable to verify the first certificate`, code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', errno: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', type: 'system', })) t.test('make https request with rejectUnauthorized:false, succeeds', async t => t.equal(await (await fetch(`${base}hello`, { rejectUnauthorized: false })).text(), 'GET /hello')) t.test('make https request with ca, succeeds', async t => t.equal(await (await fetch(`${base}hello`, { ca })).text(), 'GET /hello')) minipass-fetch-2.1.2/test/index.js000066400000000000000000001577031430072234700170710ustar00rootroot00000000000000'use strict' const t = require('tap') const TestServer = require('./fixtures/server.js') const fetch = require('../lib/index.js') const stringToArrayBuffer = require('string-to-arraybuffer') const URLSearchParamsPolyfill = require('@ungap/url-search-params') const { AbortError, FetchError, Headers, Request, Response } = fetch const AbortErrorOrig = require('../lib/abort-error.js') const FetchErrorOrig = require('../lib/fetch-error.js') const HeadersOrig = require('../lib/headers.js') const { createHeadersLenient } = HeadersOrig const RequestOrig = require('../lib/request.js') const ResponseOrig = require('../lib/response.js') const Body = require('../lib/body.js') const { getTotalBytes, extractContentType } = Body const Blob = require('../lib/blob.js') const realZlib = require('zlib') const { lookup } = require('dns') const { promisify } = require('util') const supportToString = ({ [Symbol.toStringTag]: 'z', }).toString() === '[object z]' const FormData = require('form-data') const fs = require('fs') const http = require('http') // use of url.parse here is intentional and for coverage purposes // eslint-disable-next-line node/no-deprecated-api const { parse: parseURL, URLSearchParams } = require('url') const nock = require('nock') const vm = require('vm') const { ArrayBuffer: VMArrayBuffer, Uint8Array: VMUint8Array, } = vm.runInNewContext('this') const { spawn } = require('child_process') const path = require('path') const Minipass = require('minipass') const supportStreamDestroy = 'destroy' in Minipass.prototype const { AbortController } = require('abortcontroller-polyfill/dist/abortcontroller') const AbortController2 = require('abort-controller') const local = new TestServer() const base = `http://${local.hostname}:${local.port}/` t.Test.prototype.addAssert('contain', 2, function (list, key, m, e) { m = m || 'expected item to be contained in list' e.found = list e.wanted = key return this.ok(list.indexOf(key) !== -1, m, e) }) t.Test.prototype.addAssert('notContain', 2, function (list, key, m, e) { m = m || 'expected item to not be contained in list' e.found = list e.wanted = key return this.notOk(list.indexOf(key) !== -1, m, e) }) const streamToPromise = (stream, dataHandler) => new Promise((resolve, reject) => { stream.on('data', (...args) => Promise.resolve() .then(() => dataHandler(...args)) .catch(reject)) stream.on('end', resolve) stream.on('error', reject) }) t.test('start server', t => { local.start(t.end) t.parent.teardown(() => local.stop()) }) t.test('return a promise', t => { const p = fetch(`${base}hello`) t.type(p, Promise) t.equal(typeof p.then, 'function') t.end() }) t.test('expose AbortError, FetchError, Headers, Response and Request constructors', t => { t.equal(AbortError, AbortErrorOrig) t.equal(FetchError, FetchErrorOrig) t.equal(Headers, HeadersOrig) t.equal(Response, ResponseOrig) t.equal(Request, RequestOrig) t.end() }) t.test('support proper toString output', { skip: !supportToString }, t => { t.equal(new Headers().toString(), '[object Headers]') t.equal(new Response().toString(), '[object Response]') t.equal(new Request('http://localhost:30000').toString(), '[object Request]') t.end() }) t.test('reject with error if url is protocol relative', t => t.rejects(fetch('//example.com/'), { code: 'ERR_INVALID_URL', name: 'TypeError', })) t.test('reject if url is relative path', t => t.rejects(fetch('/some/path'), { code: 'ERR_INVALID_URL', name: 'TypeError', })) t.test('reject if protocol unsupported', t => t.rejects(fetch('ftp://example.com/'), new TypeError( 'Only HTTP(S) protocols are supported'))) t.test('reject with error on network failure', t => t.rejects(fetch('http://localhost:55555/'), { name: 'FetchError', code: 'ECONNREFUSED', errno: 'ECONNREFUSED', type: 'system', })) t.test('resolve into response', async t => { const res = await fetch(`${base}hello`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(res.bodyUsed, true) t.equal(result, 'world') }) t.test('accept html response (like plain text)', async t => { const res = await fetch(`${base}html`) t.equal(res.headers.get('content-type'), 'text/html') const result = await res.text() t.equal(res.bodyUsed, true) t.equal(result, '') }) t.test('accept json response', async t => { const res = await fetch(`${base}json`) t.equal(res.headers.get('content-type'), 'application/json') const result = await res.json() t.equal(res.bodyUsed, true) t.strictSame(result, { name: 'value' }) }) t.test('send request with custom hedaers', async t => { const res = await fetch(`${base}inspect`, { headers: { 'x-custom-header': 'abc' }, }) const json = await res.json() t.equal(json.headers['x-custom-header'], 'abc') }) t.test('accept headers instance', async t => { const res = await fetch(`${base}inspect`, { headers: new Headers({ 'x-custom-header': 'abc' }), }) const json = await res.json() t.equal(json.headers['x-custom-header'], 'abc') }) t.test('accept custom host header', async t => { const res = await fetch(`${base}inspect`, { headers: { host: 'example.com', }, }) const json = await res.json() t.equal(json.headers.host, 'example.com') }) t.test('accept custom HoSt header', async t => { const res = await fetch(`${base}inspect`, { headers: { HoSt: 'example.com', }, }) const json = await res.json() t.equal(json.headers.host, 'example.com') }) t.test('follow redirects', async t => { const codes = [301, 302, 303, 307, 308, 'chain'] t.plan(codes.length) for (const code of codes) { t.test(code, async t => { const res = await fetch(`${base}redirect/${code}`) t.equal(res.url, `${base}inspect`) t.equal(res.status, 200) t.equal(res.ok, true) }) } }) t.test('redirect to different host strips headers', async (t) => { nock.disableNetConnect() t.teardown(() => { nock.cleanAll() nock.enableNetConnect() }) const first = nock('http://x.y', { reqheaders: { authorization: 'totally-authed-request', cookie: 'fake-cookie', }, }) .get('/') .reply(301, null, { location: 'http://a.b' }) const second = nock('http://a.b', { badheaders: ['authorization', 'cookie'], }) .get('/') .reply(200) const res = await fetch('http://x.y', { headers: { authorization: 'totally-authed-request', cookie: 'fake-cookie', }, }) await res.text() // drain the response stream t.ok(first.isDone(), 'initial request made') t.ok(second.isDone(), 'redirect followed') t.equal(res.status, 200) t.ok(res.ok) }) t.test('follow POST request redirect with GET', async t => { for (const code of [301, 302]) { t.test(code, async t => { const url = `${base}redirect/${code}` const opts = { method: 'POST', body: 'a=1', } const res = await fetch(url, opts) t.equal(res.url, `${base}inspect`) t.equal(res.status, 200) const result = await res.json() t.equal(result.method, 'GET') t.equal(result.body, '') }) } }) t.test('follow PATCH request redirect with PATCH', async t => { const codes = [301, 302, 307] t.plan(codes.length) for (const code of codes) { t.test(code, async t => { const url = `${base}redirect/${code}` const opts = { method: 'PATCH', body: 'a=1', } const res = await fetch(url, opts) t.equal(res.url, `${base}inspect`) t.equal(res.status, 200) const result = await res.json() t.equal(result.method, 'PATCH') t.equal(result.body, 'a=1') }) } }) t.test('no follow non-GET redirect if body is readable stream', async t => { const url = `${base}redirect/307` const body = new Minipass() body.pause() body.end('a=1') setTimeout(() => body.resume(), 100) const opts = { method: 'PATCH', body, } await t.rejects(fetch(url, opts), { name: 'FetchError', type: 'unsupported-redirect', }) }) t.test('obey maximum redirect, reject case', async t => { const url = `${base}redirect/chain` const opts = { follow: 1, } await t.rejects(fetch(url, opts), { name: 'FetchError', type: 'max-redirect', }) }) t.test('obey redirect chain, resolve case', async t => { const url = `${base}redirect/chain` const opts = { follow: 2, } const res = await fetch(url, opts) t.equal(res.url, `${base}inspect`) t.equal(res.status, 200) }) t.test('allow not following redirect', async t => { const url = `${base}redirect/301` const opts = { follow: 0, } await t.rejects(fetch(url, opts), { name: 'FetchError', type: 'max-redirect', }) }) t.test('redirect mode, manual flag', async t => { const url = `${base}redirect/301` const opts = { redirect: 'manual', } const res = await fetch(url, opts) t.equal(res.url, url) t.equal(res.status, 301) t.equal(res.headers.get('location'), `${base}inspect`) }) t.test('redirect mode, error flag', async t => { const url = `${base}redirect/301` const opts = { redirect: 'error', } await t.rejects(fetch(url, opts), { name: 'FetchError', type: 'no-redirect', }) }) t.test('redirect mode, manual flag when there is no redirect', async t => { const url = `${base}hello` const opts = { redirect: 'manual', } const res = await fetch(url, opts) t.equal(res.url, url) t.equal(res.status, 200) t.equal(res.headers.get('location'), null) }) t.test('redirect code 301 and keep existing headers', async t => { const url = `${base}redirect/301` const opts = { headers: new Headers({ 'x-custom-header': 'abc' }), } const res = await fetch(url, opts) t.equal(res.url, `${base}inspect`) const json = await res.json() t.equal(json.headers['x-custom-header'], 'abc') }) t.test('treat broken redirect as ordinary response (follow)', async t => { const url = `${base}redirect/no-location` const res = await fetch(url) t.equal(res.url, url) t.equal(res.status, 301) t.equal(res.headers.get('location'), null) }) t.test('treat broken redirect as ordinary response (manual)', async t => { const url = `${base}redirect/no-location` const opts = { redirect: 'manual', } const res = await fetch(url, opts) t.equal(res.url, url) t.equal(res.status, 301) t.equal(res.headers.get('location'), null) }) t.test('set redirected property on response when redirect', t => fetch(`${base}redirect/301`).then(res => t.equal(res.redirected, true))) t.test('no redirected property on response when not redirect', t => fetch(`${base}hello`).then(res => t.equal(res.redirected, false))) t.test('ignore invalid headers', t => { var headers = { 'Invalid-Header ': 'abc\r\n', 'Invalid-Header-Value': '\x07k\r\n', 'Set-Cookie': ['\x07k\r\n', '\x07kk\r\n'], } headers = createHeadersLenient(headers) t.equal(headers['Invalid-Header '], undefined) t.equal(headers['Invalid-Header-Value'], undefined) t.equal(headers['Set-Cookie'], undefined) t.end() }) t.test('handle client-error response', async t => { const url = `${base}error/400` const res = await fetch(url) t.equal(res.headers.get('content-type'), 'text/plain') t.equal(res.status, 400) t.equal(res.statusText, 'Bad Request') t.equal(res.ok, false) const result = await res.text() t.equal(res.bodyUsed, true) t.equal(result, 'client error') }) t.test('handle server-error response', async t => { const url = `${base}error/500` const res = await fetch(url) t.equal(res.headers.get('content-type'), 'text/plain') t.equal(res.status, 500) t.equal(res.statusText, 'Internal Server Error') t.equal(res.ok, false) const result = await res.text() t.equal(res.bodyUsed, true) t.equal(result, 'server error') }) t.test('handle network-error response', async t => { await t.rejects(fetch(`${base}error/reset`), { name: 'FetchError', code: 'ECONNRESET', }) }) t.test('handle DNS-error response', async t => { await t.rejects(fetch('http://domain.invalid'), { name: 'FetchError', // this error depends on the platform and dns server in use, // but it should be one of these two codes code: /^(ENOTFOUND|EAI_AGAIN)$/, }) }) t.test('reject invalid json response', async t => { const res = await fetch(`${base}error/json`) t.equal(res.headers.get('content-type'), 'application/json') await t.rejects(res.json(), { name: 'FetchError', type: 'invalid-json', }) }) t.test('reject invalid json response', async t => { const res = await fetch(`${base}error/json`) t.equal(res.headers.get('content-type'), 'application/json') await t.rejects(res.json(), { name: 'FetchError', type: 'invalid-json', }) }) t.test('handle no content response', async t => { const res = await fetch(`${base}no-content`) t.equal(res.status, 204) t.equal(res.statusText, 'No Content') t.equal(res.ok, true) const result = await res.text() t.equal(result, '') }) t.test('reject parsing no content response as json', async t => { const res = await fetch(`${base}no-content`) t.equal(res.status, 204) t.equal(res.statusText, 'No Content') t.equal(res.ok, true) await t.rejects(res.json(), { name: 'FetchError', type: 'invalid-json', }) }) t.test('handle no content response with gzip encoding', async t => { const res = await fetch(`${base}no-content/gzip`) t.equal(res.status, 204) t.equal(res.statusText, 'No Content') t.equal(res.headers.get('content-encoding'), 'gzip') t.equal(res.ok, true) const result = await res.text() t.equal(result, '') }) t.test('handle not modified response', async t => { const res = await fetch(`${base}not-modified`) t.equal(res.status, 304) t.equal(res.statusText, 'Not Modified') t.equal(res.ok, false) const result = await res.text() t.equal(result, '') }) t.test('handle not modified response with gzip encoding', async t => { const res = await fetch(`${base}not-modified/gzip`) t.equal(res.status, 304) t.equal(res.statusText, 'Not Modified') t.equal(res.headers.get('content-encoding'), 'gzip') t.equal(res.ok, false) const result = await res.text() t.equal(result, '') }) t.test('decompress gzip response', async t => { const res = await fetch(`${base}gzip`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'hello world') }) t.test('decompress slightly invalid gzip response', async t => { const res = await fetch(`${base}gzip-truncated`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'hello world') }) t.test('decompress deflate response', async t => { const res = await fetch(`${base}deflate`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'hello world') }) t.test('decompress deflate raw response from old apache server', async t => { const res = await fetch(`${base}deflate-raw`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'hello world') }) t.test('decompress brotli response', async t => { // if the node core zlib doesn't export brotli functions, we'll end up // rejecting the request with an error that comes from minizlib, assert // that here if (typeof realZlib.BrotliCompress !== 'function') { return t.rejects(fetch(`${base}brotli`), { message: 'Brotli is not supported in this version of Node.js', }, 'rejects the promise') } const res = await fetch(`${base}brotli`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'hello world') }) t.test('handle no content response with brotli encoding', async t => { const res = await fetch(`${base}no-content/brotli`) t.equal(res.status, 204) t.equal(res.statusText, 'No Content') t.equal(res.headers.get('content-encoding'), 'br') t.equal(res.ok, true) const result = await res.text() t.equal(result, '') }) t.test('skip decompression if unsupported', async t => { const res = await fetch(`${base}sdch`) t.equal(res.headers.get('content-type'), 'text/plain') const result = await res.text() t.equal(result, 'fake sdch string') }) t.test('reject if response compression is invalid', async t => { const res = await fetch(`${base}invalid-content-encoding`) t.equal(res.headers.get('content-type'), 'text/plain') await t.rejects(res.text(), { name: 'FetchError', code: 'Z_DATA_ERROR', }) }) t.test('handle errors on the body stream even if it is not used', async t => { const res = await fetch(`${base}invalid-content-encoding`) t.equal(res.status, 200) // Wait a few ms to see if a uncaught error occurs await promisify(setTimeout)(20) }) t.test('collect handled errors on body stream, reject if used later', async t => { const delay = value => new Promise(resolve => setTimeout(() => resolve(value), 20)) const res = await fetch(`${base}invalid-content-encoding`).then(delay) const delayed = await delay(res) t.equal(delayed.headers.get('content-type'), 'text/plain') t.rejects(delayed.text(), { name: 'FetchError', code: 'Z_DATA_ERROR', }) }) t.test('allow disabling auto decompression', t => fetch(`${base}gzip`, { compress: false }).then(res => { t.equal(res.headers.get('content-type'), 'text/plain') return res.text().then(result => t.not(result, 'hello world')) })) t.test('do not overwrite accept-encoding when auto decompression', t => fetch(`${base}inspect`, { compress: true, headers: { 'Accept-Encoding': 'gzip', }, }) .then(res => res.json()) .then(res => t.equal(res.headers['accept-encoding'], 'gzip'))) t.test('allow custom timeout', t => { return t.rejects(fetch(`${base}timeout`, { timeout: 20 }), { name: 'FetchError', type: 'request-timeout', }) }) t.test('allow custom timeout on response body', t => { return fetch(`${base}slow`, { timeout: 50 }).then(res => { t.equal(res.ok, true) return t.rejects(res.text(), { name: 'FetchError', type: 'body-timeout', }) }) }) t.test('allow custom timeout on redirected requests', t => t.rejects(fetch(`${base}redirect/slow-chain`, { timeout: 50 }), { name: 'FetchError', type: 'request-timeout', })) t.test('clear internal timeout on fetch response', { timeout: 2000 }, t => { const args = ['-e', `require('./')('${base}hello', { timeout: 10000 })`] spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') }) .on('close', (code, signal) => { t.equal(code, 0) t.equal(signal, null) t.end() }) }) t.test('clear internal timeout on fetch redirect', { timeout: 2000 }, t => { const args = ['-e', `require('./')('${base}redirect/301', { timeout: 10000 })`] spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') }) .on('close', (code, signal) => { t.equal(code, 0) t.equal(signal, null) t.end() }) }) t.test('clear internal timeout on fetch error', { timeout: 2000 }, t => { const args = ['-e', `require('./')('${base}error/reset', { timeout: 10000 })`] // note: promise rejections started setting exit status code in node 15 const stderr = [] spawn(process.execPath, args, { cwd: path.resolve(__dirname, '..') }) .on('close', (code, signal) => { t.match(Buffer.concat(stderr).toString(), 'FetchError') t.equal(signal, null) t.end() }) .stderr.on('data', c => stderr.push(c)) }) t.test('request cancellation with signal', { timeout: 500 }, t => { const controller = new AbortController() const controller2 = new AbortController2() const fetches = [ fetch(`${base}timeout`, { signal: controller.signal }), fetch(`${base}timeout`, { signal: controller2.signal }), fetch( `${base}timeout`, { method: 'POST', signal: controller.signal, headers: { 'Content-Type': 'application/json', body: JSON.stringify({ hello: 'world' }), }, } ), ] setTimeout(() => { controller.abort() controller2.abort() }, 100) return Promise.all(fetches.map(fetched => t.rejects(fetched, { name: 'AbortError', type: 'aborted', }))) }) t.test('reject immediately if signal already aborted', t => { const url = `${base}timeout` const controller = new AbortController() const opts = { signal: controller.signal, } controller.abort() const fetched = fetch(url, opts) return t.rejects(fetched, { name: 'AbortError', type: 'aborted', }) }) t.test('clear internal timeout when cancelled with AbortSignal', { timeout: 2000 }, t => { const script = ` const ACP = require('abortcontroller-polyfill/dist/cjs-ponyfill') var AbortController = ACP.AbortController var controller = new AbortController() require('./')( '${base}timeout', { signal: controller.signal, timeout: 10000 } ) setTimeout(function () { controller.abort(); }, 20) ` // note: promise rejections started setting exit status code in node 15 const stderr = [] spawn('node', ['-e', script], { cwd: path.resolve(__dirname, '..') }) .on('close', (code, signal) => { t.match(Buffer.concat(stderr).toString(), 'AbortError') t.equal(signal, null) t.end() }) .stderr.on('data', c => stderr.push(c)) }) t.test('remove internal AbortSignal listener when request aborted', t => { const controller = new AbortController() const { signal } = controller const promise = fetch( `${base}timeout`, { signal } ) const result = t.rejects(promise, { name: 'AbortError' }) .then(() => t.equal(signal.listeners.abort.length, 0)) controller.abort() return result }) t.test('allow redirects to be aborted', t => { const abortController = new AbortController() const request = new Request(`${base}redirect/slow`, { signal: abortController.signal, }) setTimeout(() => abortController.abort(), 20) return t.rejects(fetch(request), { name: 'AbortError' }) }) t.test('allow redirected response body to be aborted', t => { const abortController = new AbortController() const request = new Request(`${base}redirect/slow-stream`, { signal: abortController.signal, }) return t.rejects(fetch(request).then(res => { t.equal(res.headers.get('content-type'), 'text/plain') const result = res.text() abortController.abort() return result }), { name: 'AbortError' }) }) t.test('remove internal AbortSignal listener when req/res complete', t => { const controller = new AbortController() const { signal } = controller const fetchHtml = fetch(`${base}html`, { signal }) .then(res => res.text()) const fetchResponseError = fetch(`${base}error/reset`, { signal }) const fetchRedirect = fetch(`${base}redirect/301`, { signal }) .then(res => res.json()) return Promise.all([ t.resolves(fetchHtml.then(result => t.equal(result, ''))), t.rejects(fetchResponseError), t.resolves(fetchRedirect), ]).then(() => t.equal(signal.listeners.abort.length, 0)) }) t.test('reject body with AbortError when aborted before read completely', t => { const controller = new AbortController() return fetch(`${base}slow`, { signal: controller.signal }).then(res => { const promise = res.text() controller.abort() return t.rejects(promise, { name: 'AbortError' }) }) }) t.test('reject body methods immediately with AbortError when aborted before disturbed', t => { const controller = new AbortController() return fetch(`${base}slow`, { signal: controller.signal }) .then(res => { controller.abort() return t.rejects(res.text(), { name: 'AbortError' }) }) }) t.test('raise AbortError when aborted before stream is closed', async t => { t.plan(1) const controller = new AbortController() const res = await fetch(`${base}slow`, { signal: controller.signal }) res.body.once('error', (err) => { t.match(err, { name: 'AbortError', code: 'FETCH_ABORT' }) }) controller.abort() }) t.test('cancel request body stream with AbortError when aborted', { skip: supportStreamDestroy ? false : 'stream.destroy not supported', }, t => { const controller = new AbortController() const body = new Minipass({ objectMode: true }) const promise = fetch(`${base}slow`, { signal: controller.signal, body, method: 'POST', }) const result = Promise.all([ new Promise((resolve, reject) => { body.on('error', (error) => { t.match(error, { name: 'AbortError' }) resolve() }) }), t.rejects(promise, { name: 'AbortError' }), ]) controller.abort() return result }) t.test('immediately reject when attempting to cancel and unsupported', async t => { const controller = new AbortController() const body = new (class extends Minipass { get destroy () { return undefined } })({ objectMode: true }) await t.rejects(fetch(`${base}slow`, { signal: controller.signal, body, method: 'POST', }), { message: 'not supported' }) }) t.test('throw TypeError if a signal is not AbortSignal', async t => { await t.rejects(fetch(`${base}inspect`, { signal: {} }), { name: 'TypeError', message: /AbortSignal/, }) await t.rejects(fetch(`${base}inspect`, { signal: '' }), { name: 'TypeError', message: /AbortSignal/, }) await t.rejects(fetch(`${base}inspect`, { signal: Object.create(null) }), { name: 'TypeError', message: /AbortSignal/, }) }) t.test('set default User-Agent', async t => { const res = await fetch(`${base}inspect`) const json = await res.json() t.match(json.headers['user-agent'], /^minipass-fetch/) }) t.test('setting User-Agent', t => fetch(`${base}inspect`, { headers: { 'user-agent': 'faked', }, }).then(res => res.json()).then(res => t.equal(res.headers['user-agent'], 'faked'))) t.test('set default Accept header', async t => { const res = await fetch(`${base}inspect`) const json = await res.json() t.equal(json.headers.accept, '*/*') }) t.test('allow setting Accept header', async t => { const res = await fetch(`${base}inspect`, { headers: { accept: 'application/json', }, }) const json = await res.json() t.equal(json.headers.accept, 'application/json') }) t.test('allow POST request', async t => { const res = await fetch(`${base}inspect`, { method: 'POST' }) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '0') }) t.test('POST request with string body', async t => { const res = await fetch(`${base}inspect`, { method: 'POST', body: 'a=1', }) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8') t.equal(json.headers['content-length'], '3') }) t.test('POST request with buffer body', async t => { const res = await fetch(`${base}inspect`, { method: 'POST', body: Buffer.from('a=1', 'utf-8'), }) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '3') }) t.test('allow POST request with ArrayBuffer body', async t => { const res = await fetch(`${base}inspect`, { method: 'POST', body: stringToArrayBuffer('Hello, world!\n'), }) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'Hello, world!\n') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '14') }) t.test('POST request with ArrayBuffer body from VM context', async t => { Buffer.from(new VMArrayBuffer()) const url = `${base}inspect` const opts = { method: 'POST', body: new VMUint8Array(Buffer.from('Hello, world!\n')).buffer, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'Hello, world!\n') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '14') }) t.test('POST request with ArrayBufferView (Uint8Array) body', async t => { const url = `${base}inspect` const opts = { method: 'POST', body: new Uint8Array(stringToArrayBuffer('Hello, world!\n')), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'Hello, world!\n') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '14') }) t.test('POST request with ArrayBufferView (DataView) body', async t => { const url = `${base}inspect` const opts = { method: 'POST', body: new DataView(stringToArrayBuffer('Hello, world!\n')), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'Hello, world!\n') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '14') }) t.test('POST with ArrayBufferView (Uint8Array) body from a VM context', async t => { Buffer.from(new VMArrayBuffer()) const url = `${base}inspect` const opts = { method: 'POST', body: new VMUint8Array(Buffer.from('Hello, world!\n')), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'Hello, world!\n') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '14') }) t.test('POST with ArrayBufferView (Uint8Array, offset, length) body', async t => { const url = `${base}inspect` const opts = { method: 'POST', body: new Uint8Array(stringToArrayBuffer('Hello, world!\n'), 7, 6), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'world!') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '6') }) t.test('POST with blob body without type', async t => { const url = `${base}inspect` const opts = { method: 'POST', body: new Blob(['a=1']), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], '3') }) t.test('POST with blob body with type', async t => { const url = `${base}inspect` const opts = { method: 'POST', body: new Blob(['a=1'], { type: 'text/plain;charset=UTF-8', }), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], 'text/plain;charset=utf-8') t.equal(json.headers['content-length'], '3') }) t.test('POST with readable stream as body', async t => { const body = new Minipass() body.pause() body.end('a=1') setTimeout(() => { body.resume() }, 100) const url = `${base}inspect` const opts = { method: 'POST', body: body.pipe(new Minipass()), } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], 'chunked') t.equal(json.headers['content-type'], undefined) t.equal(json.headers['content-length'], undefined) }) t.test('POST with form-data as body', async t => { const form = new FormData() form.append('a', '1') const url = `${base}multipart` const opts = { method: 'POST', body: form, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.match(json.headers['content-type'], /^multipart\/form-data;boundary=/) t.match(json.headers['content-length'], String) t.equal(json.body, 'a=1') }) t.test('POST with form-data using stream as body', async t => { t.teardown(() => { const root = path.dirname(__dirname) // parted's multipart form parser writes a temporary file to disk, this removes it fs.readdirSync(root).filter((file) => { return file.startsWith('dummy.') && file.endsWith('.txt') }).forEach((file) => { fs.unlinkSync(path.join(root, file)) }) }) const form = new FormData() form.append('my_field', fs.createReadStream(path.join(__dirname, 'fixtures/dummy.txt'))) const url = `${base}multipart` const opts = { method: 'POST', body: form, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.match(json.headers['content-type'], /^multipart\/form-data;boundary=/) t.equal(json.headers['content-length'], undefined) t.match(json.body, 'my_field=') }) t.test('POST with form-data as body and custom headers', async t => { const form = new FormData() form.append('a', '1') const headers = form.getHeaders() headers.b = '2' const url = `${base}multipart` const opts = { method: 'POST', body: form, headers, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.match(json.headers['content-type'], /multipart\/form-data; boundary=/) t.match(json.headers['content-length'], String) t.equal(json.headers.b, '2') t.equal(json.body, 'a=1') }) t.test('POST with object body', async t => { const url = `${base}inspect` // note that fetch simply calls tostring on an object const opts = { method: 'POST', body: { a: 1 }, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, '[object Object]') t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8') t.equal(json.headers['content-length'], '15') }) const uspOpt = { skip: typeof URLSearchParams === 'function' ? false : 'no URLSearchParams function', } t.test('constructing a Response with URLSearchParams as body has a Content-Type', uspOpt, t => { const params = new URLSearchParams() const res = new Response(params) res.headers.get('Content-Type') t.equal(res.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') }) t.test('constructing a Request with URLSearchParams as body has a Content-Type', uspOpt, t => { const params = new URLSearchParams() const req = new Request(base, { method: 'POST', body: params }) t.equal(req.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') }) t.test('Reading a body with URLSearchParams should echo back the result', uspOpt, async t => { const params = new URLSearchParams() params.append('a', '1') const text = await new Response(params).text() t.equal(text, 'a=1') }) // Body should been cloned... // eslint-disable-next-line max-len t.test('Request/Response with URLSearchParams and mutation should not affected body', uspOpt, async t => { const params = new URLSearchParams() const req = new Request(`${base}inspect`, { method: 'POST', body: params }) params.append('a', '1') const text = await req.text() t.equal(text, '') }) t.test('POST with URLSearchParams as body', uspOpt, async t => { const params = new URLSearchParams() params.append('a', '1') const url = `${base}inspect` const opts = { method: 'POST', body: params, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') t.equal(json.headers['content-length'], '3') t.equal(json.body, 'a=1') }) t.test('recognize URLSearchParams when extended', uspOpt, async t => { class CustomSearchParams extends URLSearchParams {} const params = new CustomSearchParams() params.append('a', '1') const url = `${base}inspect` const opts = { method: 'POST', body: params, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') t.equal(json.headers['content-length'], '3') t.equal(json.body, 'a=1') }) /* for 100% code coverage, checks for duck-typing-only detection * where both constructor.name and brand tests fail */ t.test('recognize URLSearchParams when extended from polyfill', async t => { class CustomPolyfilledSearchParams extends URLSearchParamsPolyfill {} const params = new CustomPolyfilledSearchParams() params.append('a', '1') const url = `${base}inspect` const opts = { method: 'POST', body: params, } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') t.equal(json.headers['content-length'], '3') t.equal(json.body, 'a=1') }) t.test('overwrite Content-Length if possible', async t => { const url = `${base}inspect` // note that fetch simply calls tostring on an object const opts = { method: 'POST', headers: { 'Content-Length': '1000', }, body: 'a=1', } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'POST') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-type'], 'text/plain;charset=UTF-8') t.equal(json.headers['content-length'], '3') }) t.test('PUT', async t => { const url = `${base}inspect` const opts = { method: 'PUT', body: 'a=1', } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'PUT') t.equal(json.body, 'a=1') }) t.test('DELETE', async t => { const url = `${base}inspect` const opts = { method: 'DELETE', } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'DELETE') }) t.test('DELETE with string body', async t => { const url = `${base}inspect` const opts = { method: 'DELETE', body: 'a=1', } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'DELETE') t.equal(json.body, 'a=1') t.equal(json.headers['transfer-encoding'], undefined) t.equal(json.headers['content-length'], '3') }) t.test('PATCH', async t => { const url = `${base}inspect` const opts = { method: 'PATCH', body: 'a=1', } const res = await fetch(url, opts) const json = await res.json() t.equal(json.method, 'PATCH') t.equal(json.body, 'a=1') }) t.test('HEAD', async t => { const url = `${base}hello` const opts = { method: 'HEAD', } const res = await fetch(url, opts) t.equal(res.status, 200) t.equal(res.statusText, 'OK') t.equal(res.headers.get('content-type'), 'text/plain') t.match(res.body, Minipass) const text = await res.text() t.equal(text, '') }) t.test('HEAD with content-encoding header', async t => { const url = `${base}error/404` const opts = { method: 'HEAD', } const res = await fetch(url, opts) t.equal(res.status, 404) t.equal(res.headers.get('content-encoding'), 'gzip') const text = await res.text() t.equal(text, '') }) t.test('OPTIONS', async t => { const url = `${base}options` const opts = { method: 'OPTIONS', } const res = await fetch(url, opts) t.equal(res.status, 200) t.equal(res.statusText, 'OK') t.equal(res.headers.get('allow'), 'GET, HEAD, OPTIONS') t.match(res.body, Minipass) }) t.test('reject decoding body twice', async t => { const url = `${base}plain` const res = await fetch(url) t.equal(res.headers.get('content-type'), 'text/plain') await res.text() t.equal(res.bodyUsed, true) t.rejects(res.text()) }) t.test('response trailers', async t => { const res = await fetch(`${base}trailers`) t.equal(res.status, 200) t.equal(res.statusText, 'OK') t.equal(res.headers.get('Trailer'), 'X-Node-Fetch') const trailers = await res.trailer t.same(Array.from(trailers.keys()), ['x-node-fetch']) t.equal(trailers.get('x-node-fetch'), 'hello world!') }) t.test('maximum response size, multiple chunk', async t => { const url = `${base}size/chunk` const opts = { size: 5, } const res = await fetch(url, opts) t.equal(res.status, 200) t.equal(res.headers.get('content-type'), 'text/plain') await t.rejects(res.text(), { name: 'FetchError', type: 'max-size', }) }) t.test('maximum response size, single chunk', t => { const url = `${base}size/long` const opts = { size: 5, } return t.rejects(fetch(url, opts).then(res => { t.equal(res.status, 200) t.equal(res.headers.get('content-type'), 'text/plain') return res.text() }), { name: 'FetchError', type: 'max-size', }) }) t.test('pipe response body as stream', async t => { t.plan(2) const url = `${base}hello` const res = await fetch(url) t.match(res.body, Minipass) await streamToPromise(res.body, chunk => { if (chunk === null) { return } t.equal(chunk.toString(), 'world') }) }) t.test('clone a response, and use both as stream', async t => { t.plan(4) const url = `${base}hello` const res = await fetch(url) const cloned = res.clone() t.match(res.body, Minipass) t.match(cloned.body, Minipass) const dataHandler = chunk => { if (chunk === null) { return } t.equal(chunk.toString(), 'world') } await Promise.all([ streamToPromise(res.body, dataHandler), streamToPromise(cloned.body, dataHandler), ]) }) t.test('clone a json response and log it as text response', async t => { const url = `${base}json` const res = await fetch(url) const cloned = res.clone() const results = await Promise.all([res.json(), cloned.text()]) t.same(results[0], { name: 'value' }) t.equal(results[1], '{"name":"value"}') }) t.test('clone a json response, and then log it as text response', async t => { const url = `${base}json` const res = await fetch(url) const cloned = res.clone() const jsonResult = await res.json() t.same(jsonResult, { name: 'value' }) const textResult = await cloned.text() t.equal(textResult, '{"name":"value"}') }) t.test('clone a json response, first log as text response, then return json object', async t => { const url = `${base}json` const res = await fetch(url) const cloned = res.clone() const textResult = await cloned.text() t.equal(textResult, '{"name":"value"}') const jsonResult = await res.json() t.same(jsonResult, { name: 'value' }) }) t.test('do not allow cloning a response after its been used', async t => { const url = `${base}hello` const res = await fetch(url) await res.text() t.throws(() => res.clone()) }) t.test('get all responses of a header', async t => { const url = `${base}cookie` const res = await fetch(url) const expected = 'a=1, b=1' t.equal(res.headers.get('set-cookie'), expected) t.equal(res.headers.get('Set-Cookie'), expected) }) t.test('return all headers using raw()', async t => { const url = `${base}cookie` const res = await fetch(url) t.same(res.headers.raw()['set-cookie'], ['a=1', 'b=1']) }) t.test('delete header', async t => { const url = `${base}cookie` const res = await fetch(url) res.headers.delete('set-cookie') t.equal(res.headers.get('set-cookie'), null) }) t.test('send request with connection keep-alive if agent is provided', async t => { const url = `${base}inspect` const opts = { agent: new http.Agent({ keepAlive: true, }), } const res = await fetch(url, opts) const body = await res.json() t.equal(body.headers.connection, 'keep-alive') }) t.test('fetch with Request instance', async t => { const url = `${base}hello` const req = new Request(url) const res = await fetch(req) t.equal(res.url, url) t.equal(res.ok, true) t.equal(res.status, 200) }) t.test('fetch with Node.js legacy URL object', async t => { const url = `${base}hello` const urlObj = parseURL(url) const req = new Request(urlObj) const res = await fetch(req) t.equal(res.url, url) t.equal(res.ok, true) t.equal(res.status, 200) }) t.test('fetch with Node.js URL object', async t => { const url = `${base}hello` const urlObj = new URL(url) const req = new Request(urlObj) const res = await fetch(req) t.equal(res.url, url) t.equal(res.ok, true) t.equal(res.status, 200) }) t.test('reading blob as text', async t => { const blob = await new Response(`hello`).blob() const body = await blob.text() t.equal(body, 'hello') }) t.test('reading blob as arrayBuffer', async t => { const blob = await new Response(`hello`).blob() const ab = await blob.arrayBuffer() const str = String.fromCharCode.apply(null, new Uint8Array(ab)) t.equal(str, 'hello') }) t.test('reading blob as stream', t => { return new Response(`hello`) .blob() .then(blob => streamToPromise(blob.stream(), data => { const str = data.toString() t.equal(str, 'hello') })) }) t.test('blob round-trip', async t => { const url = `${base}hello` const res = await fetch(url) const blob = await res.blob() const inspectUrl = `${base}inspect` const length = blob.size const type = blob.type const res2 = await fetch(inspectUrl, { method: 'POST', body: blob, }) const { body, headers } = await res2.json() t.equal(body, 'world') t.equal(headers['content-type'], type) t.equal(headers['content-length'], String(length)) }) t.test('overwrite Request instance', async t => { const url = `${base}inspect` const req = new Request(url, { method: 'POST', headers: { a: '1', }, }) const res = await fetch(req, { method: 'GET', headers: { a: '2', }, }) const body = await res.json() t.equal(body.method, 'GET') t.equal(body.headers.a, '2') }) t.test('arrayBuffer(), blob(), text(), json() and buffer() method in Body constructor', t => { const body = new Body('a=1') t.match(body.arrayBuffer, Function) t.match(body.blob, Function) t.match(body.text, Function) t.match(body.json, Function) t.match(body.buffer, Function) t.end() }) t.test('https request', { timeout: 5000 }, async t => { const url = 'https://github.com/' const opts = { method: 'HEAD', } const res = await fetch(url, opts) t.equal(res.status, 200) t.equal(res.ok, true) }) // issue #414 t.test('reject if attempt to accumulate body stream throws', async t => { const body = new Minipass() body.pause() body.end('a=1') setTimeout(() => body.resume(), 100) const res = new Response(body.pipe(new Minipass())) const bufferConcat = Buffer.concat Buffer.concat = () => { throw new Error('embedded error') } t.teardown(() => { Buffer.concat = bufferConcat }) return t.rejects(res.text(), { name: 'FetchError', type: 'system', message: /embedded error/, }) }) t.test('supports supplying a lookup function to the agent', async t => { const url = `${base}redirect/301` let called = 0 function lookupSpy (hostname, options, callback) { called++ return lookup(hostname, options, callback) } const agent = http.Agent({ lookup: lookupSpy }) await fetch(url, { agent }) t.equal(called, 2) }) t.test('supports supplying a famliy option to the agent', async t => { const url = `${base}redirect/301` const families = [] const family = Symbol('family') function lookupSpy (hostname, options, callback) { families.push(options.family) return lookup(hostname, {}, callback) } const agent = http.Agent({ lookup: lookupSpy, family }) await fetch(url, { agent }) t.same(families, [family, family]) }) t.test('function supplying the agent', async t => { const url = `${base}inspect` const agent = new http.Agent({ keepAlive: true, }) let parsedURL const res = await fetch(url, { agent: function (_parsedURL) { parsedURL = _parsedURL return agent }, }) const body = await res.json() // the agent provider should have been called t.equal(parsedURL.protocol, 'http:') // the agent we returned should have been used t.equal(body.headers.connection, 'keep-alive') }) t.test('calculate content length and extract content type', t => { const url = `${base}hello` const bodyContent = 'a=1' let streamBody = new Minipass() streamBody.pause() streamBody.end(bodyContent) setTimeout(() => streamBody.resume(), 100) streamBody = streamBody.pipe(new Minipass()) const streamRequest = new Request(url, { method: 'POST', body: streamBody, size: 1024, }) const blobBody = new Blob([bodyContent], { type: 'text/plain' }) const blobRequest = new Request(url, { method: 'POST', body: blobBody, size: 1024, }) const formBody = new FormData() formBody.append('a', '1') const formRequest = new Request(url, { method: 'POST', body: formBody, size: 1024, }) const bufferBody = Buffer.from(bodyContent) const bufferRequest = new Request(url, { method: 'POST', body: bufferBody, size: 1024, }) const stringRequest = new Request(url, { method: 'POST', body: bodyContent, size: 1024, }) const nullRequest = new Request(url, { method: 'GET', body: null, size: 1024, }) t.equal(getTotalBytes(streamRequest), null) t.equal(getTotalBytes(blobRequest), blobBody.size) t.not(getTotalBytes(formRequest), null) t.equal(getTotalBytes(bufferRequest), bufferBody.length) t.equal(getTotalBytes(stringRequest), bodyContent.length) t.equal(getTotalBytes(nullRequest), 0) t.equal(extractContentType(streamBody), null) t.equal(extractContentType(blobBody), 'text/plain') t.match(extractContentType(formBody), /^multipart\/form-data/) t.equal(extractContentType(bufferBody), null) t.equal(extractContentType(bodyContent), 'text/plain;charset=UTF-8') t.equal(extractContentType(null), null) t.end() }) t.test('with optional `encoding`', t => { t.test('only use UTF-8 decoding with text()', async t => { const res = await fetch(`${base}encoding/euc-jp`) t.equal(res.status, 200) const result = await res.text() t.equal(result, '' + '\ufffd\ufffd\ufffd\u0738\ufffd') }) t.test('encoding decode, xml dtd detect', async t => { const res = await fetch(`${base}encoding/euc-jp`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '日本語') }) t.test('encoding decode, content-type detect', async t => { const res = await fetch(`${base}encoding/shift-jis`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '
日本語
') }) t.test('encoding decode, html5 detect', async t => { const res = await fetch(`${base}encoding/gbk`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '
中文
') }) t.test('encoding decode, html4 detect', async t => { const res = await fetch(`${base}encoding/gb2312`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '' + '
中文
') }) t.test('encoding decode, html4 detect reverse http-equiv', async t => { const res = await fetch(`${base}encoding/gb2312-reverse`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '' + '
中文
') }) t.test('default to utf8 encoding', async t => { const res = await fetch(`${base}encoding/utf8`) t.equal(res.status, 200) t.equal(res.headers.get('content-type'), null) const result = await res.textConverted() t.equal(result, '中文') }) t.test('uncommon content-type order, charset in front', async t => { const res = await fetch(`${base}encoding/order1`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '中文') }) t.test('uncommon content-type order, end with qs', async t => { const res = await fetch(`${base}encoding/order2`) t.equal(res.status, 200) const result = await res.textConverted() t.equal(result, '中文') }) t.test('chunked encoding, html4 detect', async t => { const url = `${base}encoding/chunked` const res = await fetch(url) t.equal(res.status, 200) const padding = 'a'.repeat(10) const result = await res.textConverted() t.equal(result, `${padding}
日本語
') }) t.test('only do encoding detection up to 1024 bytes', async t => { const url = `${base}encoding/invalid` const res = await fetch(url) t.equal(res.status, 200) const padding = 'a'.repeat(1200) const result = await res.textConverted() t.not(result, `${padding}中文`) }) t.end() }) t.test('data uri', t => { const dataUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=' const invalidDataUrl = 'data:@@@@' t.test('accept data uri', t => fetch(dataUrl).then(r => { t.equal(r.status, 200) t.equal(r.headers.get('Content-Type'), 'image/gif') return r.buffer().then(b => t.type(b, Buffer)) })) t.test('reject invalid data uri', t => t.rejects(fetch(invalidDataUrl), { message: 'invalid data: URI', })) t.test('data uri not base64 encoded', t => fetch('data:text/plain,hello, world!').then(r => { t.equal(r.status, 200) t.equal(r.headers.get('Content-Type'), 'text/plain') return r.buffer().then(b => t.equal(b.toString(), 'hello, world!')) })) t.test('data uri with no type specified', t => fetch('data:,hello,%20world!').then(r => { t.equal(r.status, 200) t.equal(r.headers.get('Content-Type'), null) return r.buffer().then(b => t.equal(b.toString(), 'hello, world!')) })) t.test('search included, hash not included', t => fetch('data:,hello?with=search#no%20hash').then(r => { t.equal(r.status, 200) t.equal(r.headers.get('Content-Type'), null) return r.buffer().then(b => t.equal(b.toString(), 'hello?with=search')) })) t.end() }) t.test('aborting data uris', t => { const controllers = [AbortController, AbortController2] t.plan(controllers.length) const url = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==' controllers.forEach((Controller, idx) => { t.test(`controller ${idx}`, async t => { t.test('pre-abort', async t => { const controller = new Controller() controller.abort() t.rejects(fetch(url, { signal: controller.signal }), { message: 'The user aborted a request.', }) }) t.test('post-abort', async t => { const controller = new Controller() t.rejects(fetch(url, { signal: controller.signal }), { message: 'The user aborted a request.', }) controller.abort() }) t.test('cannot abort after first tick', t => { const controller = new Controller() const testPromise = t.resolves(fetch(url, { signal: controller.signal })) process.nextTick(() => { controller.abort() }) return testPromise }) }) }) }) t.test('redirect changes host header', t => fetch(`http://${local.hostname}:${local.port}/host-redirect`, { redirect: 'follow', headers: { host: 'foo' }, }) .then(r => r.text()) .then(text => t.equal(text, `${base}host-redirect`))) t.test('never apply backpressure to the underlying response stream', t => { const { request } = http t.teardown(() => http.request = request) http.request = (...args) => { const req = request(...args) const { emit } = req req.emit = (ev, ...emitArgs) => { if (ev === 'response') { const res = emitArgs[0] res.pause = () => { throw new Error('should not pause the response') } } return emit.call(req, ev, ...emitArgs) } return req } const dest = new Minipass() return fetch(`${base}hello`) .then(res => { // read it a bit later, so we'll see if any backpressure happens. setTimeout(() => dest.read()) return res.body.pipe(dest).promise() }) }) minipass-fetch-2.1.2/test/request.js000066400000000000000000000300051430072234700174330ustar00rootroot00000000000000'use strict' const t = require('tap') const Request = require('../lib/request.js') const stringToArrayBuffer = require('string-to-arraybuffer') const Minipass = require('minipass') const base = 'http://localhost:12345/' const FormData = require('form-data') const { AbortController } = require('abortcontroller-polyfill/dist/abortcontroller') const Blob = require('../lib/blob.js') const http = require('http') const { URL } = require('url') t.Test.prototype.addAssert('contain', 2, function (list, key, m, e) { m = m || 'expected item to be contained in list' e.found = list e.wanted = key return this.ok(list.indexOf(key) !== -1, m, e) }) t.Test.prototype.addAssert('notContain', 2, function (list, key, m, e) { m = m || 'expected item to not be contained in list' e.found = list e.wanted = key return this.notOk(list.indexOf(key) !== -1, m, e) }) t.test('should have attributes conforming to Web IDL', t => { const req = new Request({ href: 'https://github.com/' }) t.equal(req.url, 'https://github.com/') t.equal(String(req), '[object Request]') const enumerableProperties = [] for (const property in req) { enumerableProperties.push(property) } for (const toCheck of [ 'body', 'bodyUsed', 'arrayBuffer', 'blob', 'json', 'text', 'method', 'url', 'headers', 'redirect', 'clone', 'signal', ]) { t.contain(enumerableProperties, toCheck) } for (const toCheck of [ 'body', 'bodyUsed', 'method', 'url', 'headers', 'redirect', 'signal', ]) { t.throws(() => req[toCheck] = 'abc') } t.end() }) t.test('signal must be a signal', t => { t.throws(() => new Request('http://foo.com', { signal: {} }), TypeError) t.end() }) t.test('should support wrapping Request instance', t => { const url = `${base}hello` const form = new FormData() form.append('a', '1') const { signal } = new AbortController() const r1 = new Request(url, { method: 'POST', follow: 1, body: form, signal, rejectUnauthorized: false, }) const r2 = new Request(r1, { follow: 2, }) t.equal(r2.url, url) t.equal(r2.method, 'POST') t.equal(r2.signal, signal) // note that we didn't clone the body t.equal(r2.body, form) t.equal(r1.follow, 1) t.equal(r2.follow, 2) t.equal(r1.counter, 0) t.equal(r2.counter, 0) t.same(Request.getNodeRequestOptions(r1), Request.getNodeRequestOptions(r2)) t.end() }) t.test('should override signal on derived Request instances', t => { const parentAbortController = new AbortController() const derivedAbortController = new AbortController() const parentRequest = new Request('http://localhost/test', { signal: parentAbortController.signal, }) const derivedRequest = new Request(parentRequest, { signal: derivedAbortController.signal, }) t.equal(parentRequest.signal, parentAbortController.signal) t.equal(derivedRequest.signal, derivedAbortController.signal) t.end() }) t.test('should allow removing signal on derived Request instances', t => { const parentAbortController = new AbortController() const parentRequest = new Request('http://localhost/test', { signal: parentAbortController.signal, }) const derivedRequest = new Request(parentRequest, { signal: null, }) t.equal(parentRequest.signal, parentAbortController.signal) t.equal(derivedRequest.signal, null) t.end() }) t.test('should throw error with GET/HEAD requests with body', t => { t.throws(() => new Request('http://localhost', { body: '' }), TypeError) t.throws(() => new Request('http://localhost', { body: 'a' }), TypeError) t.throws(() => new Request('http://localhost', { body: '', method: 'HEAD' }), TypeError) t.throws(() => new Request('http://localhost', { body: 'a', method: 'HEAD' }), TypeError) t.throws(() => new Request('http://localhost', { body: 'a', method: 'get' }), TypeError) t.throws(() => new Request('http://localhost', { body: 'a', method: 'head' }), TypeError) t.end() }) t.test('should default to null as body', t => { const req = new Request(base) t.equal(req.body, null) return req.text().then(result => t.equal(result, '')) }) t.test('should support parsing headers', t => { const url = base const req = new Request(url, { headers: { a: '1', }, }) t.equal(req.url, url) t.equal(req.headers.get('a'), '1') t.end() }) t.test('should support arrayBuffer() method', async t => { const url = base var req = new Request(url, { method: 'POST', body: 'a=1', }) t.equal(req.url, url) const result = await req.arrayBuffer() t.type(result, ArrayBuffer) const str = String.fromCharCode.apply(null, new Uint8Array(result)) t.equal(str, 'a=1') }) t.test('should support text() method', t => { const url = base const req = new Request(url, { method: 'POST', body: 'a=1', }) t.equal(req.url, url) return req.text().then(result => t.equal(result, 'a=1')) }) t.test('should support json() method', t => { const url = base const req = new Request(url, { method: 'POST', body: '{"a":1}', }) t.equal(req.url, url) return req.json().then(result => t.equal(result.a, 1)) }) t.test('should support buffer() method', t => { const url = base const req = new Request(url, { method: 'POST', body: 'a=1', }) t.equal(req.url, url) return req.buffer().then(result => t.equal(result.toString(), 'a=1')) }) t.test('should support blob() method', async t => { const url = base var req = new Request(url, { method: 'POST', body: Buffer.from('a=1'), }) t.equal(req.url, url) const result = await req.blob() t.type(result, Blob) t.equal(result.size, 3) t.equal(result.type, '') }) t.test('should support clone() method', async t => { const url = base const r = new Minipass().end('a=1') r.pause() setTimeout(() => r.resume()) const body = r.pipe(new Minipass()) const agent = new http.Agent() const { signal } = new AbortController() const req = new Request(url, { body, method: 'POST', redirect: 'manual', headers: { b: '2', }, follow: 3, compress: false, agent, signal, }) const cl = req.clone() t.equal(cl.url, url) t.equal(cl.method, 'POST') t.equal(cl.redirect, 'manual') t.equal(cl.headers.get('b'), '2') t.equal(cl.follow, 3) t.equal(cl.compress, false) t.equal(cl.method, 'POST') t.equal(cl.counter, 0) t.equal(cl.agent, agent) t.equal(cl.signal, signal) // clone body shouldn't be the same body t.not(cl.body, body) const results = await Promise.all([cl.text(), req.text()]) t.equal(results[0], 'a=1') t.equal(results[1], 'a=1') }) t.test('should support ArrayBuffer as body', t => { const req = new Request('http://localhost', { method: 'POST', body: stringToArrayBuffer('a=1'), }) return req.text().then(result => t.equal(result, 'a=1')) }) t.test('should support Uint8Array as body', t => { const req = new Request('http://localhost', { method: 'POST', body: new Uint8Array(stringToArrayBuffer('a=1')), }) return req.text().then(result => t.equal(result, 'a=1')) }) t.test('should support DataView as body', t => { const req = new Request('http://localhost', { method: 'POST', body: new DataView(stringToArrayBuffer('a=1')), }) return req.text().then(result => t.equal(result, 'a=1')) }) t.test('should set rejectUnauthorized to true if NODE_TLS_REJECT_UNAUTHORIZED is not set', t => { const tlsRejectBefore = process.env.NODE_TLS_REJECT_UNAUTHORIZED process.env.NODE_TLS_REJECT_UNAUTHORIZED = null const req = new Request('http://a.b') t.equal(Request.getNodeRequestOptions(req).rejectUnauthorized, true) process.env.NODE_TLS_REJECT_UNAUTHORIZED = tlsRejectBefore t.end() }) t.test('should set rejectUnauthorized to false if NODE_TLS_REJECT_UNAUTHORIZED is \'0\'', t => { const tlsRejectBefore = process.env.NODE_TLS_REJECT_UNAUTHORIZED process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' const req = new Request('http://a.b') t.equal(Request.getNodeRequestOptions(req).rejectUnauthorized, false) process.env.NODE_TLS_REJECT_UNAUTHORIZED = tlsRejectBefore t.end() }) t.test('get node request options', t => { t.match(Request.getNodeRequestOptions(new Request('http://a.b', { method: 'POST', headers: { accept: 'text/plain; q=1, *.*; q=0.8', }, body: null, compress: true, })), { ...(new URL('http://a.b')), method: 'POST', headers: { 'Content-Length': ['0'], 'Accept-Encoding': ['gzip,deflate'], Connection: ['close'], 'User-Agent': /^minipass-fetch\//, }, agent: undefined, }, 'happy path') t.match(Request.getNodeRequestOptions(new Request('http://user:password@a.b')), { auth: 'user:password', }, 'sets both user and password') t.match(Request.getNodeRequestOptions(new Request('http://user:@a.b')), { auth: 'user:', }, 'sets just user') t.match(Request.getNodeRequestOptions(new Request('http://:password@a.b')), { auth: ':password', }, 'sets just password') t.match(Request.getNodeRequestOptions(new Request('http://a.b', { method: 'PATCH', headers: { accept: 'text/plain; q=1, *.*; q=0.8', }, body: '123', compress: true, })), { ...(new URL('http://a.b')), method: 'PATCH', headers: { 'Content-Length': ['3'], 'Accept-Encoding': ['gzip,deflate'], Connection: ['close'], 'User-Agent': /^minipass-fetch\//, }, agent: undefined, }, 'happy path') t.match(Request.getNodeRequestOptions(new Request('http://a.b', { method: 'PATCH', headers: { accept: 'text/plain; q=1, *.*; q=0.8', }, body: null, compress: true, })), { ...(new URL('http://a.b')), method: 'PATCH', headers: { 'Content-Length': undefined, 'Accept-Encoding': ['gzip,deflate'], Connection: ['close'], 'User-Agent': /^minipass-fetch\//, }, agent: undefined, }, 'happy path') t.match(Request.getNodeRequestOptions(new Request('http://x.y', { method: 'PATCH', headers: { 'user-agent': 'abc', connection: 'whatevs', }, body: 'xyz', compress: false, })), { path: '/', protocol: 'http:', hostname: 'x.y', method: 'PATCH', headers: { Accept: ['*/*'], 'user-agent': ['abc'], connection: ['whatevs'], 'Content-Length': ['3'], }, }) t.match(Request.getNodeRequestOptions(new Request('http://x.y', { method: 'PATCH', headers: { 'user-agent': 'abc', connection: 'whatevs', }, body: new Minipass().end('xyz'), compress: false, })), { path: '/', protocol: 'http:', hostname: 'x.y', method: 'PATCH', headers: { Accept: ['*/*'], 'user-agent': ['abc'], connection: ['whatevs'], 'Content-Length': undefined, }, }) t.match(Request.getNodeRequestOptions(new Request('http://x.y', { method: 'GET', family: 6, })), { path: '/', protocol: 'http:', hostname: 'x.y', method: 'GET', family: 6, }) t.test('passes through search params', (t) => { const req = new Request('http://x.y?one=two&three=four') const options = Request.getNodeRequestOptions(req) t.match(options, { path: '/?one=two&three=four', }) t.end() }) t.test('function as agent', t => { let agentCalled = false const agent = () => { agentCalled = true return 420 } Request.getNodeRequestOptions(new Request('http://a.b', { agent }), { method: 'GET', path: '/', protocol: 'http:', hostname: 'a.b', agent: 420, }) t.equal(agentCalled, true) t.end() }) t.throws(() => Request.getNodeRequestOptions(new Request('ok.html')), { code: 'ERR_INVALID_URL', }) t.throws(() => Request.getNodeRequestOptions(new Request('xyz://ok.html')), { message: 'Only HTTP(S) protocols are supported', }) t.throws(() => Request.getNodeRequestOptions(new Request('http://a.b', { method: 'POST', body: new (class extends Minipass { get destroy () { return undefined } })(), signal: new AbortController().signal, })), { message: 'Cancellation of streamed requests with AbortSignal is not supported', }) t.end() }) minipass-fetch-2.1.2/test/response.js000066400000000000000000000114111430072234700176010ustar00rootroot00000000000000'use strict' const t = require('tap') const Response = require('../lib/response.js') const stringToArrayBuffer = require('string-to-arraybuffer') const Blob = require('../lib/blob.js') const Minipass = require('minipass') const base = `http://localhost:123456/` t.Test.prototype.addAssert('contain', 2, function (list, key, m, e) { m = m || 'expected item to be contained in list' e.found = list e.wanted = key return this.ok(list.indexOf(key) !== -1, m, e) }) t.Test.prototype.addAssert('notContain', 2, function (list, key, m, e) { m = m || 'expected item to not be contained in list' e.found = list e.wanted = key return this.notOk(list.indexOf(key) !== -1, m, e) }) t.test('should have attributes conforming to Web IDL', t => { const res = new Response() t.equal(String(res), '[object Response]') const enumerableProperties = [] for (const property in res) { enumerableProperties.push(property) } for (const toCheck of [ 'body', 'bodyUsed', 'arrayBuffer', 'blob', 'json', 'text', 'url', 'status', 'ok', 'redirected', 'statusText', 'headers', 'clone', ]) { t.contain(enumerableProperties, toCheck) } for (const toCheck of [ 'body', 'bodyUsed', 'url', 'status', 'ok', 'redirected', 'statusText', 'headers', ]) { t.throws(() => res[toCheck] = 'abc') } t.end() }) t.test('should support empty options', t => { const r = new Minipass().end('a=1') r.pause() setTimeout(() => r.resume()) const res = new Response(r.pipe(new Minipass())) return res.text().then(result => t.equal(result, 'a=1')) }) t.test('should support parsing headers', t => { const res = new Response(null, { headers: { a: '1', }, }) t.equal(res.headers.get('a'), '1') t.end() }) t.test('should support text() method', t => new Response('a=1').text().then(result => t.equal(result, 'a=1'))) t.test('should support json() method', t => new Response('{"a":1}').json().then(result => t.equal(result.a, 1))) t.test('should support buffer() method', t => new Response('a=1').buffer().then(result => t.equal(result.toString(), 'a=1'))) t.test('should support blob() method', async t => { const result = await new Response('a=1', { method: 'POST', headers: { 'Content-Type': 'text/plain', }, }).blob() t.type(result, Blob) t.equal(result.size, 3) t.equal(result.type, 'text/plain') }) t.test('should support clone() method', t => { const r = new Minipass().end('a=1') r.pause() setTimeout(() => r.resume()) const body = r.pipe(new Minipass()) const res = new Response(body, { headers: { a: '1', }, url: base, status: 346, statusText: 'production', }) const cl = res.clone() t.equal(cl.headers.get('a'), '1') t.equal(cl.url, base) t.equal(cl.status, 346) t.equal(cl.statusText, 'production') t.equal(cl.ok, false) // clone body shouldn't be the same body t.not(cl.body, body) return cl.text().then(result => t.equal(result, 'a=1')) }) t.test('should support stream as body', t => { const r = new Minipass().end('a=1') r.pause() setTimeout(() => r.resume()) const body = r.pipe(new Minipass()) return new Response(body).text().then(result => t.equal(result, 'a=1')) }) t.test('should support string as body', t => new Response('a=1').text().then(result => t.equal(result, 'a=1'))) t.test('should support buffer as body', t => new Response(Buffer.from('a=1')).text().then(result => t.equal(result, 'a=1'))) t.test('should support ArrayBuffer as body', t => new Response(stringToArrayBuffer('a=1')).text().then(result => t.equal(result, 'a=1'))) t.test('should support blob as body', t => new Response(new Blob(['a=1'])).text().then(result => t.equal(result, 'a=1'))) t.test('should support Uint8Array as body', t => new Response(new Uint8Array(stringToArrayBuffer('a=1'))).text() .then(result => t.equal(result, 'a=1'))) t.test('should support DataView as body', t => new Response(new DataView(stringToArrayBuffer('a=1'))).text() .then(result => t.equal(result, 'a=1'))) t.test('should default to null as body', t => { const res = new Response() t.equal(res.body, null) return res.text().then(result => t.equal(result, '')) }) t.test('should default to 200 as status code', t => { const res = new Response(null) t.equal(res.status, 200) t.end() }) t.test('should default to empty string as url', t => { const res = new Response() t.equal(res.url, '') t.end() }) t.test('trailers in response option', async t => { const Headers = require('../lib/headers.js') const res = new Response(null, { trailer: Headers.createHeadersLenient({ 'X-Node-Fetch': 'hello world!', }), }) const trailers = await res.trailer t.same(Array.from(trailers.keys()), ['x-node-fetch']) t.equal(trailers.get('x-node-fetch'), 'hello world!') })