Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: convert package to ESM #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.nyc_output
coverage
node_modules
npm-debug.log
package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
93 changes: 9 additions & 84 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,82 +1,12 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.8"
- "2.5"
- "3.3"
- "4.9"
- "5.12"
- "6.17"
- "7.10"
- "8.17"
- "10.24"
- "11.15"
- "12.22"
- "13.14"
- "14.16"
- "15.13"
- "12.20.0"
- "14.13.1"
- "16.0.0"
cache:
directories:
- node_modules
before_install:
- |
# Setup utility functions
function node_version_lt () {
[[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]]
}
function npm_module_installed () {
npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@"
}
function npm_remove_module_re () {
node -e '
fs = require("fs");
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
r = RegExp(process.argv[1]);
for (k in p.devDependencies) {
if (r.test(k)) delete p.devDependencies[k];
}
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
' "$@"
}
function npm_use_module () {
node -e '
fs = require("fs");
p = JSON.parse(fs.readFileSync("package.json", "utf8"));
p.devDependencies[process.argv[1]] = process.argv[2];
fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n");
' "$@"
}
function v () {
tr '.' '\n' <<< "${1}" \
| awk '{ printf "%03d", $0 }' \
| sed 's/^0*//'
}
# Configure npm
- |
# Skip updating shrinkwrap / lock
npm config set shrinkwrap false
# Setup Node.js version-specific dependencies
- |
# Configure eslint for linting
if node_version_lt '10.0'; then npm_remove_module_re '^eslint(-|$)'
fi
- |
# Configure mocha for testing
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0'
elif node_version_lt '8.0' ; then npm_use_module 'mocha' '6.2.2'
elif node_version_lt '10.0'; then npm_use_module 'mocha' '7.2.0'
fi
- |
# Configure nyc for coverage
if node_version_lt '0.10'; then npm_remove_module_re '^nyc$'
elif node_version_lt '4.0' ; then npm_use_module 'nyc' '10.3.2'
elif node_version_lt '6.0' ; then npm_use_module 'nyc' '11.9.0'
elif node_version_lt '8.0' ; then npm_use_module 'nyc' '14.1.1'
fi
# Update Node.js modules
- |
# Prune and rebuild node_modules
Expand All @@ -90,18 +20,13 @@ before_script:
npm -s ls ||:
script:
- |
# Run test script, depending on nyc install
if npm_module_installed 'nyc'; then npm run-script test-ci
else npm test
fi
# Run test script
npm run-script test-ci
- |
# Run linting, if eslint exists
if npm_module_installed 'eslint'; then npm run-script lint
fi
# Run linting
npm run-script lint
after_script:
- |
# Upload coverage to coveralls
if [[ -d .nyc_output ]]; then
npm install --save-dev [email protected]
nyc report --reporter=text-lcov | coveralls
fi
npm install --save-dev [email protected]
c8 report --reporter=text-lcov | coveralls
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ npm install on-finished
## API

```js
var onFinished = require('on-finished')
import onFinished from 'on-finished'
```

### onFinished(res, listener)
Expand Down Expand Up @@ -72,12 +72,12 @@ onFinished(req, function (err, req) {
})
```

### onFinished.isFinished(res)
### isFinished(res)

Determine if `res` is already finished. This would be useful to check and
not even start certain operations if the response has already finished.

### onFinished.isFinished(req)
### isFinished(req)

Determine if `req` is already finished. This would be useful to check and
not even start certain operations if the request has already finished.
Expand Down Expand Up @@ -133,10 +133,10 @@ The following code ensures that file descriptors are always closed
once the response finishes.

```js
var destroy = require('destroy')
var fs = require('fs')
var http = require('http')
var onFinished = require('on-finished')
import destroy from 'destroy'
import fs from 'node:fs'
import http from 'node:http'
import onFinished from 'on-finished'

http.createServer(function onRequest (req, res) {
var stream = fs.createReadStream('package.json')
Expand Down
44 changes: 4 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,19 @@
* MIT Licensed
*/

'use strict'

/**
* Module exports.
* @public
*/

module.exports = onFinished
module.exports.isFinished = isFinished

/**
* Module dependencies.
* @private
*/

var first = require('ee-first')
import first from 'ee-first'

/**
* Variables.
* @private
*/

/* istanbul ignore next */
/* c8 ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
Expand All @@ -42,7 +32,7 @@ var defer = typeof setImmediate === 'function'
* @public
*/

function onFinished (msg, listener) {
export default function onFinished (msg, listener) {
if (isFinished(msg) !== false) {
defer(listener, null, msg)
return msg
Expand All @@ -62,7 +52,7 @@ function onFinished (msg, listener) {
* @public
*/

function isFinished (msg) {
export function isFinished (msg) {
var socket = msg.socket

if (typeof msg.finished === 'boolean') {
Expand Down Expand Up @@ -122,11 +112,6 @@ function attachFinishedListener (msg, callback) {

// wait for socket to be assigned
msg.on('socket', onSocket)

if (msg.socket === undefined) {
// istanbul ignore next: node.js 0.8 patch
patchAssignSocket(msg, onSocket)
}
}

/**
Expand Down Expand Up @@ -174,24 +159,3 @@ function createListener (msg) {

return listener
}

/**
* Patch ServerResponse.prototype.assignSocket for node.js 0.8.
*
* @param {ServerResponse} res
* @param {function} callback
* @private
*/

// istanbul ignore next: node.js 0.8 patch
function patchAssignSocket (res, callback) {
var assignSocket = res.assignSocket

if (typeof assignSocket !== 'function') return

// res.on('socket', callback) is broken in 0.8
res.assignSocket = function _assignSocket (socket) {
assignSocket.call(this, socket)
callback(socket)
}
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@
],
"license": "MIT",
"repository": "jshttp/on-finished",
"type": "module",
"exports": "./index.js",
"dependencies": {
"ee-first": "1.1.1"
},
"devDependencies": {
"c8": "7.7.3",
"eslint": "7.23.0",
"eslint-config-standard": "14.1.1",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-markdown": "2.0.1",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "4.3.1",
"eslint-plugin-standard": "4.1.0",
"mocha": "8.3.2",
"nyc": "15.1.0"
"mocha": "8.3.2"
},
"engines": {
"node": ">= 0.8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"files": [
"HISTORY.md",
Expand All @@ -33,7 +35,7 @@
"scripts": {
"lint": "eslint .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test"
"test-ci": "c8 --reporter=lcov --reporter=text npm test",
"test-cov": "c8 --reporter=html --reporter=text npm test"
}
}
Loading