Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/use_writeHead_instead_of_implicitHe…
Browse files Browse the repository at this point in the history
…ader
  • Loading branch information
bjohansebas authored Nov 9, 2024
2 parents 4c1d27b + 8e5641c commit ec797a7
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 45 deletions.
67 changes: 48 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ jobs:
- Node.js 15.x
- Node.js 16.x
- Node.js 17.x
- Node.js 18.x
- Node.js 19.x
- Node.js 20.x
- Node.js 21.x
- Node.js 22.x
- Node.js 23.x

include:
- name: Node.js 0.8
Expand Down Expand Up @@ -58,57 +64,75 @@ jobs:
npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0

- name: Node.js 4.x
node-version: "4.9"
node-version: "4"
npm-i: mocha@5.2.0 nyc@11.9.0 supertest@3.4.2

- name: Node.js 5.x
node-version: "5.12"
node-version: "5"
npm-i: mocha@5.2.0 nyc@11.9.0 supertest@3.4.2

- name: Node.js 6.x
node-version: "6.17"
node-version: "6"
npm-i: mocha@6.2.3 nyc@14.1.1 supertest@6.1.6

- name: Node.js 7.x
node-version: "7.10"
node-version: "7"
npm-i: mocha@6.2.3 nyc@14.1.1 supertest@6.1.6

- name: Node.js 8.x
node-version: "8.16"
npm-i: mocha@7.2.0
node-version: "8"
npm-i: mocha@7.2.0 nyc@14.1.1

- name: Node.js 9.x
node-version: "9.11"
npm-i: mocha@7.2.0
node-version: "9"
npm-i: mocha@7.2.0 nyc@14.1.1

- name: Node.js 10.x
node-version: "10.16"
node-version: "10"
npm-i: mocha@8.4.0

- name: Node.js 11.x
node-version: "11.15"
node-version: "11"
npm-i: mocha@8.4.0

- name: Node.js 12.x
node-version: "12.22"
node-version: "12"

- name: Node.js 13.x
node-version: "13.14"
node-version: "13"

- name: Node.js 14.x
node-version: "14.19"
node-version: "14"

- name: Node.js 15.x
node-version: "15.14"
node-version: "15"

- name: Node.js 16.x
node-version: "16.15"
node-version: "16"

- name: Node.js 17.x
node-version: "17.9"
node-version: "17"

- name: Node.js 18.x
node-version: "18"

- name: Node.js 19.x
node-version: "19"

- name: Node.js 20.x
node-version: "20"

- name: Node.js 21.x
node-version: "21"

- name: Node.js 22.x
node-version: "22"

- name: Node.js 23.x
node-version: "23"

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install Node.js ${{ matrix.node-version }}
shell: bash -eo pipefail -l {0}
Expand All @@ -123,7 +147,12 @@ jobs:
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"
- name: Configure npm
run: npm config set shrinkwrap false
run: |
if [[ "$(npm config get package-lock)" == "true" ]]; then
npm config set package-lock false
else
npm config set shrinkwrap false
fi
- name: Remove npm module(s) ${{ matrix.npm-rm }}
run: npm rm --silent --save-dev ${{ matrix.npm-rm }}
Expand Down Expand Up @@ -182,7 +211,7 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- name: Uploade code coverage
- name: Upload code coverage
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
Expand Down
66 changes: 66 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: ["master"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["master"]
schedule:
- cron: "0 0 * * 1"

permissions:
contents: read

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
with:
languages: javascript
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
# - name: Autobuild
# uses: github/codeql-action/autobuild@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
with:
category: "/language:javascript"
70 changes: 70 additions & 0 deletions .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This workflow uses actions that are not certified by GitHub. They are provided
# by a third-party and are governed by separate terms of service, privacy
# policy, and support documentation.

name: Scorecard supply-chain security

on:
# For Branch-Protection check. Only the default branch is supported. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection
branch_protection_rule:
# To guarantee Maintained check is occasionally updated. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained
schedule:
- cron: '16 21 * * 1'
push:
branches: [ "master" ]

# Declare default permissions as read only.
permissions: read-all

jobs:
analysis:
name: Scorecard analysis
runs-on: ubuntu-latest
permissions:
# Needed to upload the results to code-scanning dashboard.
security-events: write
# Needed to publish results and get a badge (see publish_results below).
id-token: write

steps:
- name: "Checkout code"
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.2
with:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
with:
results_file: results.sarif
results_format: sarif
# (Optional) "write" PAT token. Uncomment the `repo_token` line below if:
# - you want to enable the Branch-Protection check on a *public* repository, or
# - you are installing Scorecard on a *private* repository
# To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat.
# repo_token: ${{ secrets.SCORECARD_TOKEN }}

# Public repositories:
# - Publish results to OpenSSF REST API for easy access by consumers
# - Allows the repository to include the Scorecard badge.
# - See https://github.com/ossf/scorecard-action#publishing-results.
# For private repositories:
# - `publish_results` will always be set to `false`, regardless
# of the value entered here.
publish_results: true

# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@2f93e4319b2f04a2efc38fa7f78bd681bc3f7b2f # v2.23.2
with:
sarif_file: results.sarif
14 changes: 10 additions & 4 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
unreleased
==========
* Use `res.headersSent` when available

* deps: accepts@~1.3.8
- Fix sorting encoding with extra parameters
- deps: mime-types@~2.1.34
- deps: negotiator@0.6.3
1.7.5 / 2024-10-31
==========

* deps: Replace accepts with negotiator@~0.6.4
- Add preference option
* deps: bytes@3.1.2
- Add petabyte (`pb`) support
- Fix "thousandsSeparator" incorrecting formatting fractional part
- Fix return value for un-parsable strings
* deps: compressible@~2.0.18
- Mark `font/ttf` as compressible
- Remove compressible from `multipart/mixed`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var compression = require('compression')
### compression([options])

Returns the compression middleware using the given `options`. The middleware
will attempt to compress response bodies for all request that traverse through
will attempt to compress response bodies for all requests that traverse through
the middleware, based on the given `options`.

This middleware will never compress responses that include a `Cache-Control`
Expand Down
43 changes: 26 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @private
*/

var accepts = require('accepts')
var Negotiator = require('negotiator')
var Buffer = require('safe-buffer').Buffer
var bytes = require('bytes')
var compressible = require('compressible')
Expand Down Expand Up @@ -80,8 +80,8 @@ function compression (options) {
return false
}

if (!this._header) {
this.writeHead(this.statusCode)
if (!headersSent(res)) {
this.writeHead(this.statusCode)
}

return stream
Expand All @@ -94,7 +94,7 @@ function compression (options) {
return false
}

if (!this._header) {
if (!headersSent(res)) {
// estimate the length
if (!this.getHeader('Content-Length')) {
length = chunkLength(chunk, encoding)
Expand Down Expand Up @@ -174,13 +174,8 @@ function compression (options) {
}

// compression method
var accept = accepts(req)
var method = accept.encoding(['gzip', 'deflate', 'identity'])

// we really don't prefer deflate
if (method === 'deflate' && accept.encoding(['gzip'])) {
method = accept.encoding(['gzip', 'identity'])
}
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

// negotiation failed
if (!method || method === 'identity') {
Expand Down Expand Up @@ -241,9 +236,9 @@ function chunkLength (chunk, encoding) {
return 0
}

return !Buffer.isBuffer(chunk)
? Buffer.byteLength(chunk, encoding)
: chunk.length
return Buffer.isBuffer(chunk)
? chunk.length
: Buffer.byteLength(chunk, encoding)
}

/**
Expand Down Expand Up @@ -282,7 +277,21 @@ function shouldTransform (req, res) {
*/

function toBuffer (chunk, encoding) {
return !Buffer.isBuffer(chunk)
? Buffer.from(chunk, encoding)
: chunk
return Buffer.isBuffer(chunk)
? chunk
: Buffer.from(chunk, encoding)
}

/**
* Determine if the response headers have been sent.
*
* @param {object} res
* @returns {boolean}
* @private
*/

function headersSent (res) {
return typeof res.headersSent !== 'boolean'
? Boolean(res._header)
: res.headersSent
}
Loading

0 comments on commit ec797a7

Please sign in to comment.