Comparing version 1.3.9 to 2.0.0
@@ -7,9 +7,79 @@ { | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"accessor-pairs": "error", | ||
"array-bracket-newline": "off", | ||
"array-bracket-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"array-callback-return": "off", | ||
"array-element-newline": "off", | ||
"arrow-body-style": "off", | ||
"arrow-parens": "off", | ||
"arrow-spacing": [ | ||
"error", | ||
{ | ||
"after": true, | ||
"before": true | ||
} | ||
], | ||
"block-scoped-var": "error", | ||
"block-spacing": "error", | ||
"brace-style": [ | ||
"error", | ||
"1tbs" | ||
], | ||
"callback-return": "error", | ||
"capitalized-comments": "off", | ||
"class-methods-use-this": "off", | ||
"comma-dangle": "off", | ||
"comma-spacing": [ | ||
"error", | ||
{ | ||
"after": true, | ||
"before": false | ||
} | ||
], | ||
"comma-style": [ | ||
"error", | ||
"last" | ||
], | ||
"complexity": "off", | ||
"computed-property-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"consistent-return": "off", | ||
"consistent-this": "off", | ||
"curly": "off", | ||
"default-case": "off", | ||
"dot-location": [ | ||
"error", | ||
"property" | ||
], | ||
"dot-notation": "off", | ||
"eol-last": "off", | ||
"eqeqeq": "off", | ||
"func-call-spacing": "error", | ||
"func-name-matching": "off", | ||
"func-names": "off", | ||
"func-style": "off", | ||
"function-paren-newline": "off", | ||
"generator-star-spacing": "error", | ||
"global-require": "off", | ||
"guard-for-in": "off", | ||
"handle-callback-err": "error", | ||
"id-blacklist": "error", | ||
"id-length": "off", | ||
"id-match": "error", | ||
"implicit-arrow-linebreak": "off", | ||
"indent": [ | ||
"error", | ||
4, { | ||
4, | ||
{ | ||
"MemberExpression": "off", | ||
@@ -19,5 +89,8 @@ "SwitchCase": 1 | ||
], | ||
"camelcase": ["error", {"properties": "never"}], | ||
"eqeqeq": ["error", "always"], | ||
"padded-blocks": ["error", "never"], | ||
"indent-legacy": "off", | ||
"init-declarations": "off", | ||
"jsx-quotes": "error", | ||
"key-spacing": "error", | ||
"keyword-spacing": "off", | ||
"line-comment-position": "off", | ||
"linebreak-style": [ | ||
@@ -27,18 +100,221 @@ "error", | ||
], | ||
"quotes": [ | ||
"lines-around-comment": "off", | ||
"lines-around-directive": "error", | ||
"lines-between-class-members": "off", | ||
"max-classes-per-file": "off", | ||
"max-depth": "error", | ||
"max-len": "off", | ||
"max-lines": "off", | ||
"max-lines-per-function": "off", | ||
"max-nested-callbacks": "error", | ||
"max-params": "off", | ||
"max-statements": "off", | ||
"max-statements-per-line": "error", | ||
"multiline-comment-style": "off", | ||
"multiline-ternary": [ | ||
"error", | ||
"single" | ||
"always-multiline" | ||
], | ||
"semi": [ | ||
"new-parens": "off", | ||
"newline-after-var": "off", | ||
"newline-before-return": "off", | ||
"newline-per-chained-call": "off", | ||
"no-alert": "error", | ||
"no-array-constructor": "error", | ||
"no-await-in-loop": "off", | ||
"no-bitwise": "off", | ||
"no-buffer-constructor": "off", | ||
"no-caller": "error", | ||
"no-catch-shadow": "error", | ||
"no-case-declarations": "off", | ||
"no-confusing-arrow": "off", | ||
"no-constant-condition": [ | ||
"error", | ||
"always" | ||
{ | ||
"checkLoops": false | ||
} | ||
], | ||
"no-console": "off", | ||
"curly": ["error", "multi-or-nest"], | ||
"no-continue": "off", | ||
"no-div-regex": "error", | ||
"no-duplicate-imports": "off", | ||
"no-else-return": "off", | ||
"no-empty": [ | ||
"error", | ||
{ | ||
"allowEmptyCatch": true | ||
} | ||
], | ||
"no-empty-function": "off", | ||
"no-eq-null": "error", | ||
"no-eval": "error", | ||
"no-extend-native": "error", | ||
"no-extra-bind": "error", | ||
"no-extra-label": "error", | ||
"no-extra-parens": "off", | ||
"no-floating-decimal": "error", | ||
"no-implicit-globals": "error", | ||
"no-implied-eval": "error", | ||
"no-inline-comments": "off", | ||
"no-invalid-this": "off", | ||
"no-iterator": "error", | ||
"no-label-var": "error", | ||
"no-labels": "error", | ||
"no-lone-blocks": "error", | ||
"no-lonely-if": "off", | ||
"no-loop-func": "off", | ||
"no-magic-numbers": "off", | ||
"no-mixed-operators": "off", | ||
"no-mixed-requires": "error", | ||
"no-multi-assign": "error", | ||
"no-multi-spaces": "error", | ||
"no-multi-str": "error", | ||
"no-multiple-empty-lines": "off", | ||
"no-native-reassign": "error", | ||
"no-negated-condition": "off", | ||
"no-negated-in-lhs": "error", | ||
"no-nested-ternary": "error", | ||
"no-new": "error", | ||
"no-new-func": "error", | ||
"no-new-object": "error", | ||
"no-new-require": "error", | ||
"no-new-wrappers": "error", | ||
"no-octal-escape": "error", | ||
"no-param-reassign": "off", | ||
"no-path-concat": "error", | ||
"no-plusplus": "off", | ||
"no-process-env": "off", | ||
"no-process-exit": "off", | ||
"no-proto": "off", | ||
"no-prototype-builtins": "off", | ||
"no-restricted-globals": "error", | ||
"no-restricted-imports": "error", | ||
"no-restricted-modules": "error", | ||
"no-restricted-properties": "error", | ||
"no-restricted-syntax": "error", | ||
"no-return-assign": "off", | ||
"no-return-await": "off", | ||
"no-script-url": "error", | ||
"no-self-compare": "error", | ||
"no-sequences": "error", | ||
"no-shadow": "off", | ||
"no-shadow-restricted-names": "error", | ||
"no-spaced-func": "error", | ||
"no-sync": "off", | ||
"no-tabs": "error", | ||
"no-template-curly-in-string": "error", | ||
"no-ternary": "off", | ||
"no-throw-literal": "error", | ||
"no-trailing-spaces": [ | ||
"error", | ||
{ | ||
"ignoreComments": true, | ||
"skipBlankLines": true | ||
} | ||
], | ||
"no-undef-init": "error", | ||
"no-undefined": "off", | ||
"no-underscore-dangle": "off", | ||
"no-unmodified-loop-condition": "error", | ||
"no-unneeded-ternary": "off", | ||
"no-unused-expressions": "off", | ||
"no-use-before-define": "off", | ||
"no-useless-call": "error", | ||
"no-useless-computed-key": "error", | ||
"no-useless-concat": "off", | ||
"no-useless-constructor": "error", | ||
"no-useless-return": "off", | ||
"no-var": "error", | ||
"no-void": "error", | ||
"no-warning-comments": "off", | ||
"no-whitespace-before-property": "error", | ||
"no-with": "error", | ||
"nonblock-statement-body-position": [ | ||
"error", | ||
"any" | ||
], | ||
"object-curly-newline": "error", | ||
"object-curly-spacing": [ | ||
"error", | ||
"always" | ||
], | ||
"object-property-newline": "error", | ||
"object-shorthand": "off", | ||
"one-var": "off", | ||
"one-var-declaration-per-line": [ | ||
"error", | ||
"initializations" | ||
], | ||
"operator-assignment": "off", | ||
"operator-linebreak": [ | ||
"error", | ||
"after" | ||
], | ||
"padded-blocks": "off", | ||
"padding-line-between-statements": "error", | ||
"prefer-arrow-callback": "off", | ||
"prefer-const": "error", | ||
"no-unused-vars": "error", | ||
"no-implicit-globals": "error", | ||
"no-global-assign": "error" | ||
"prefer-destructuring": "off", | ||
"prefer-numeric-literals": "error", | ||
"prefer-object-spread": "off", | ||
"prefer-promise-reject-errors": "error", | ||
"prefer-reflect": "off", | ||
"prefer-rest-params": "off", | ||
"prefer-spread": "off", | ||
"prefer-template": "off", | ||
"quote-props": "off", | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"radix": "off", | ||
"require-await": "off", | ||
"require-jsdoc": "off", | ||
"rest-spread-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"semi": "error", | ||
"semi-spacing": [ | ||
"error", | ||
{ | ||
"after": true, | ||
"before": false | ||
} | ||
], | ||
"semi-style": [ | ||
"error", | ||
"last" | ||
], | ||
"sort-imports": "off", | ||
"sort-keys": "off", | ||
"sort-vars": "off", | ||
"space-before-blocks": "error", | ||
"space-before-function-paren": "off", | ||
"space-in-parens": [ | ||
"error", | ||
"never" | ||
], | ||
"space-infix-ops": "error", | ||
"space-unary-ops": "error", | ||
"spaced-comment": "off", | ||
"strict": "error", | ||
"switch-colon-spacing": "error", | ||
"symbol-description": "error", | ||
"template-curly-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"template-tag-spacing": "error", | ||
"unicode-bom": [ | ||
"error", | ||
"never" | ||
], | ||
"valid-jsdoc": "off", | ||
"vars-on-top": "error", | ||
"wrap-iife": "error", | ||
"wrap-regex": "off", | ||
"yield-star-spacing": "error", | ||
"yoda": "off" | ||
} | ||
} | ||
} |
{ | ||
"name": "unload", | ||
"version": "1.3.9", | ||
"version": "2.0.0", | ||
"description": "Execute code when the js-process exits. On all javascript-environments", | ||
"scripts": { | ||
"lint": "eslint --ignore-path ./.eslintignore src test", | ||
"lint": "eslint src test", | ||
"preversion": "npm run lint && npm run test", | ||
"build": "npm-run-all build:*", | ||
"build:browserify": "browserify ./src/index.browserify.js > ./dist/unload.js", | ||
"build:min": "uglifyjs --compress --mangle --output dist/unload.min.js -- ./dist/unload.js", | ||
"build:es6": "rimraf -rf dist/es && cross-env NODE_ENV=es6 babel src --out-dir dist/es", | ||
"build:es5": "cross-env NODE_ENV=es5 node node_modules/babel-cli/bin/babel.js src --out-dir dist/lib", | ||
"build:test": "cross-env NODE_ENV=es5 node node_modules/babel-cli/bin/babel.js test --out-dir test_tmp", | ||
"build:browser": "browserify dist/lib/index.browserify.js > dist/browserify.js", | ||
"build:worker": "browserify test/helper/worker.js > test_tmp/worker.js", | ||
"build": "npm run clear && concurrently \"npm run build:es6\" \"npm run build:es5\" \"npm run build:test\" && npm run build:browser && npm run build:worker", | ||
"clear": "rimraf -rf ./dist", | ||
"pingserver": "node ./test/helper/ping-server.node.js", | ||
"pretest": "npm run build", | ||
"test": "concurrently --kill-others -s first \"npm run pingserver\" \"mocha -b test/nodejs.test.js\"", | ||
"test:typings": "npm run build && mocha ./test/typings.test.js -b --timeout 12000 --exit" | ||
"test:typings": "npm run build && mocha ./test_tmp/typings.test.js -b --timeout 12000 --exit", | ||
"test:node": "npm run build && concurrently --kill-others -s first \"npm run pingserver\" \"mocha -b test_tmp/nodejs.test.js\"", | ||
"test:browser:server": "http-server . -p 8080 --cors --silent", | ||
"test:browser": "npm run build && concurrently --kill-others -s first \"npm run test:browser:server\" \"npm run pingserver\" \"karma start ./config/karma.conf.js --single-run\"", | ||
"test": "npm run test:node && npm run test:browser" | ||
}, | ||
@@ -31,6 +37,6 @@ "repository": { | ||
"pre-commit": [ | ||
"test", | ||
"lint" | ||
], | ||
"main": "src/index.js", | ||
"main": "./dist/lib/index.js", | ||
"module": "./dist/es/index.js", | ||
"types": "./src/index.d.ts", | ||
@@ -40,10 +46,32 @@ "devDependencies": { | ||
"async-test-util": "1.6.1", | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.3", | ||
"babel-eslint": "8.2.6", | ||
"babel-loader": "7.1.5", | ||
"babel-plugin-transform-async-to-generator": "6.24.1", | ||
"babel-plugin-transform-class-properties": "6.24.1", | ||
"babel-plugin-transform-es2015-block-scoping": "6.26.0", | ||
"babel-plugin-transform-es2015-constants": "6.1.4", | ||
"babel-plugin-transform-es3-member-expression-literals": "6.22.0", | ||
"babel-plugin-transform-es3-property-literals": "6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"babel-plugin-transform-regenerator": "6.26.0", | ||
"babel-plugin-transform-runtime": "6.23.0", | ||
"babel-polyfill": "6.26.0", | ||
"babel-preset-es2015": "6.24.1", | ||
"babel-preset-es2015-native-modules": "6.9.4", | ||
"babel-preset-es2015-rollup": "3.0.0", | ||
"babel-preset-es2016": "6.24.1", | ||
"babel-preset-es2017": "6.24.1", | ||
"babel-preset-latest": "6.24.1", | ||
"browserify": "16.2.2", | ||
"child-process-promise": "2.2.1", | ||
"child_process": "1.0.2", | ||
"concurrently": "3.6.0", | ||
"eslint": "5.0.1", | ||
"concurrently": "3.6.1", | ||
"cors": "^2.8.4", | ||
"cross-env": "5.2.0", | ||
"eslint": "5.2.0", | ||
"express": "4.16.3", | ||
"http-server": "0.11.1", | ||
"karma": "2.0.4", | ||
"karma": "2.0.5", | ||
"karma-browserify": "5.3.0", | ||
@@ -53,3 +81,3 @@ "karma-chrome-launcher": "2.2.0", | ||
"mocha": "5.2.0", | ||
"node": "10.5.0", | ||
"node": "10.7.0", | ||
"node-cmd": "3.0.0", | ||
@@ -62,8 +90,17 @@ "npm-run-all": "4.1.3", | ||
"ts-node": "7.0.0", | ||
"typescript": "2.9.2", | ||
"uglify-js": "3.4.2", | ||
"typescript": "3.0.1", | ||
"uglify-js": "3.4.6", | ||
"util": "0.11.0", | ||
"watch": "1.0.2", | ||
"watchify": "3.11.0" | ||
}, | ||
"dependencies": { | ||
"detect-node": "^2.0.3" | ||
}, | ||
"// browser": "this field contains files that will be empty objects in webpack-builds", | ||
"browser": { | ||
"./src/node.js": false, | ||
"./dist/es/node.js": false, | ||
"./dist/lib/node.js": false | ||
} | ||
} |
# javascript: unload | ||
Run a piece of code whenever the javascript-process stops/exits/quits. On **browsers, nodejs, electron, react-native, .net-core**. It also ensures that the exit-function is called only **once**. | ||
Run a piece of code whenever the javascript-process stops/exits/quits. On **browsers, nodejs, electron, react-native**. It also ensures that the exit-function is called only **once**. | ||
@@ -11,6 +11,6 @@ You should use this module when your write a npm-library where you dont know in which environments the users will run it. | ||
```js | ||
process.on('beforeExit') | ||
process.on('exit') | ||
process.on('SIGINT') | ||
process.on('uncaughtException') | ||
process.on('beforeExit'); | ||
process.on('exit'); | ||
process.on('SIGINT'); // catches ctrl+c event | ||
process.on('uncaughtException'); // catches uncaught exceptions | ||
``` | ||
@@ -20,12 +20,6 @@ | ||
```js | ||
window.addEventListener('beforeunload') | ||
window.addEventListener('beforeunload'); // closing of normal browser-window | ||
window.addEventListener('unload'); // closed inside of iframe | ||
``` | ||
When electron: | ||
```js | ||
app.on('window-all-closed') | ||
``` | ||
# Usage | ||
@@ -50,7 +44,7 @@ | ||
var unload = require('unload'); | ||
var stopListen = unload.add(function(){ | ||
var ret = unload.add(function(){ | ||
console.log('Ouch, I\'m dying.'); | ||
}); | ||
stopListen(); // removes the event-listeners | ||
ret.remove(); // removes the event-handler | ||
``` | ||
@@ -57,0 +51,0 @@ |
@@ -1,37 +0,31 @@ | ||
module.exports = (function() { | ||
var exports = {}; | ||
/* global WorkerGlobalScope */ | ||
exports.add = function(fn) { | ||
var ret = {}; | ||
if ( | ||
typeof window === 'object' && | ||
window.addEventListener && | ||
typeof window.addEventListener === 'function' | ||
) { | ||
window.addEventListener('beforeunload', fn, false); | ||
ret.beforeunload = fn; | ||
} | ||
function add(fn) { | ||
if (typeof WorkerGlobalScope === 'function' && self instanceof WorkerGlobalScope) { | ||
// this is run inside of a webworker | ||
} else { | ||
/** | ||
* for normal browser-windows, we use the beforeunload-event | ||
*/ | ||
window.addEventListener('beforeunload', () => { | ||
fn(); | ||
}, true); | ||
/** | ||
* TODO add fallback for safari-mobile | ||
* @link http://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad | ||
* for iframes, we have to use the unload-event | ||
* @link https://stackoverflow.com/q/47533670/3443137 | ||
*/ | ||
window.addEventListener('unload', () => { | ||
fn(); | ||
}, true); | ||
} | ||
return ret; | ||
}; | ||
/** | ||
* TODO add fallback for safari-mobile | ||
* @link https://stackoverflow.com/a/26193516/3443137 | ||
*/ | ||
} | ||
exports.remove = function(fn, listeners) { | ||
Object.keys(listeners).forEach(function(key) { | ||
var fn = listeners[key]; | ||
switch (key) { | ||
case 'beforeunload': | ||
window.removeEventListener('beforeunload', | ||
fn, | ||
false | ||
); | ||
break; | ||
} | ||
}); | ||
}; | ||
return exports; | ||
})(); | ||
export default { | ||
add | ||
}; |
@@ -1,2 +0,2 @@ | ||
var unload = require('./index.js'); | ||
const unload = require('./index.js'); | ||
window['unload'] = unload; |
@@ -0,9 +1,11 @@ | ||
declare type addReturn = { | ||
remove: () => void; | ||
run: () => any; | ||
}; | ||
declare interface Unload { | ||
add(fn: () => void): () => void | ||
runAll(): void | ||
removeAll(): void | ||
debug(): void; | ||
_getCache(): any; | ||
_resetUnloaded(): void; | ||
add(fn: () => void): addReturn; | ||
runAll(): Promise<any>; | ||
removeAll(): void; | ||
getSize(): number; | ||
}; | ||
@@ -10,0 +12,0 @@ |
115
src/index.js
@@ -1,85 +0,46 @@ | ||
module.exports = (function( | ||
envs | ||
) { | ||
var exports = {}; | ||
var unloaded = false; | ||
var debug = false; | ||
var count = 0; | ||
var cache = {}; | ||
import isNode from 'detect-node'; | ||
import BrowserMethod from './browser.js'; | ||
import NodeMethod from './node.js'; | ||
exports.debug = function() { | ||
debug = true; | ||
}; | ||
const USE_METHOD = isNode ? NodeMethod : BrowserMethod; | ||
const LISTENERS = new Set(); | ||
/** | ||
* start listening with the handler | ||
* @param {Function({})} fn the handler which takes the unload-event as attr | ||
* @return {Function} stopListening : a function which is used to stop listening | ||
*/ | ||
exports.add = function(fn) { | ||
count++; | ||
let startedListening = false; | ||
function startListening() { | ||
if (startedListening) return; | ||
startedListening = true; | ||
USE_METHOD.add(runAll); | ||
} | ||
// wrap fn to ensure it executes once | ||
var fnWrapped = function(arg1, arg2, arg3) { | ||
if (unloaded) return; | ||
unloaded = true; | ||
return fn(arg1, arg2, arg3); | ||
}; | ||
export function add(fn) { | ||
startListening(); | ||
if (typeof fn !== 'function') | ||
throw new Error('The "listener" argument must be of type Function. Received type ' + typeof fn); | ||
LISTENERS.add(fn); | ||
var hasListeners = {}; | ||
Object.keys(envs).forEach(function(envKey) { | ||
hasListeners[envKey] = envs[envKey].add(fnWrapped); | ||
}); | ||
var retFn = function stopListening() { | ||
Object.keys(hasListeners).forEach(function(envKey) { | ||
envs[envKey].remove(fnWrapped, hasListeners[envKey]); | ||
}); | ||
debug && console.log('unload.stopListening()'); | ||
debug && console.dir(cache[count]); | ||
}; | ||
retFn.run = function() { | ||
fnWrapped(); | ||
}; | ||
cache[count] = { | ||
fn: fn, | ||
remove: retFn, | ||
listeners: hasListeners | ||
}; | ||
debug && console.log('unload.add()'); | ||
debug && console.dir(cache[count]); | ||
return retFn; | ||
const addReturn = { | ||
remove: () => LISTENERS.delete(fn), | ||
run: () => { | ||
LISTENERS.delete(fn); | ||
return fn(); | ||
} | ||
}; | ||
return addReturn; | ||
} | ||
exports.runAll = function() { | ||
if (unloaded) return; | ||
unloaded = true; | ||
Object.keys(cache).forEach(function(key) { | ||
cache[key].fn(); | ||
}); | ||
}; | ||
export function runAll() { | ||
const promises = []; | ||
LISTENERS.forEach(function (fn) { | ||
promises.push(fn()); | ||
LISTENERS.delete(fn); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
exports.removeAll = function() { | ||
Object.keys(cache).forEach(function(key) { | ||
cache[key].remove(); | ||
}); | ||
}; | ||
export function removeAll() { | ||
LISTENERS.clear(); | ||
} | ||
// used for testing purposes | ||
exports._getCache = function(){ | ||
return cache; | ||
}; | ||
// used for testing purposes | ||
exports._resetUnloaded = function(){ | ||
unloaded = false; | ||
}; | ||
return exports; | ||
})({ | ||
node: require('./node.js'), | ||
browser: require('./browser.js') | ||
}); | ||
export function getSize() { | ||
return LISTENERS.size; | ||
} |
@@ -1,66 +0,34 @@ | ||
module.exports = (function() { | ||
var exports = {}; | ||
// set to true to log events | ||
const DEBUG = false; | ||
exports.add = function(fn) { | ||
var ret = {}; | ||
if ( | ||
typeof process === 'object' && | ||
process.on && | ||
typeof process.on === 'function' | ||
) { | ||
ret.beforeExit = function(e) { | ||
var maybePromise = fn(e); | ||
Promise.resolve(maybePromise) | ||
.then(function() { | ||
process.exit(); | ||
}); | ||
}; | ||
process.on('beforeExit', ret.beforeExit); | ||
function add(fn) { | ||
process.on('exit', () => { | ||
DEBUG && console.log('node: exit'); | ||
return fn(); | ||
}); | ||
ret.exit = function(e) { | ||
var maybePromise = fn(e); | ||
Promise.resolve(maybePromise); | ||
}; | ||
process.on('exit', ret.exit); | ||
/** | ||
* on the following events, | ||
* the process will not end if there are | ||
* event-handlers attached, | ||
* therefore we have to call process.exit() | ||
*/ | ||
process.on('beforeExit', () => { | ||
DEBUG && console.log('node: beforeExit'); | ||
return fn().then(() => process.exit()); | ||
}); | ||
// catches ctrl+c event | ||
process.on('SIGINT', () => { | ||
DEBUG && console.log('node: SIGNINT'); | ||
return fn().then(() => process.exit()); | ||
}); | ||
// catches uncaught exceptions | ||
process.on('uncaughtException', () => { | ||
DEBUG && console.log('node: uncaughtException'); | ||
return fn().then(() => process.exit()); | ||
}); | ||
} | ||
//catches ctrl+c event | ||
ret.SIGINT = function(e) { | ||
var maybePromise = fn(e); | ||
Promise.resolve(maybePromise) | ||
.then(function() { | ||
process.exit(); | ||
}); | ||
}; | ||
process.on('SIGINT', ret.SIGINT); | ||
//catches uncaught exceptions | ||
ret.uncaughtException = function(e) { | ||
var maybePromise = fn(e); | ||
Promise.resolve(maybePromise) | ||
.then(function() { | ||
process.exit(); | ||
}); | ||
}; | ||
process.on('uncaughtException', ret.uncaughtException); | ||
} | ||
return ret; | ||
}; | ||
exports.remove = function(fn, listeners) { | ||
Object.keys(listeners).forEach(function(key) { | ||
var fn = listeners[key]; | ||
switch (key) { | ||
case 'beforeExit': | ||
case 'SIGINT': | ||
case 'uncaughtException': | ||
case 'exit': | ||
process.removeListener(key, fn); | ||
break; | ||
} | ||
}); | ||
}; | ||
return exports; | ||
})(); | ||
export default { | ||
add | ||
}; |
module.exports = (function(request) { | ||
var pingHost = 'http://localhost:23230/counter/'; | ||
const pingHost = 'http://localhost:23230/counter/'; | ||
return function() { | ||
return request(pingHost).then(c => parseInt(c)); | ||
}; | ||
})( | ||
}( | ||
require('request-promise-native') | ||
); | ||
)); |
@@ -1,3 +0,4 @@ | ||
var request = require('request-promise-native'); | ||
var unload = require('../../src/index.js'); | ||
const AsyncTestUtil = require('async-test-util'); | ||
const request = require('request-promise-native'); | ||
const unload = require('../../'); | ||
@@ -7,12 +8,20 @@ process.argv.shift(); | ||
var mode = process.argv.shift(); | ||
const mode = process.argv.shift(); | ||
var stopListening = unload.add(function() { | ||
return request('http://localhost:23230/'); | ||
}); | ||
const addUnloadFunction = (wait = 0) => { | ||
const stopListening = unload.add(async () => { | ||
await AsyncTestUtil.wait(wait); | ||
console.log('run exit-function'); | ||
return request('http://localhost:23230/'); | ||
}); | ||
return stopListening; | ||
}; | ||
console.log('mode: ' + mode); | ||
switch (mode) { | ||
case 'exception': | ||
setTimeout(function() { | ||
addUnloadFunction(); | ||
setTimeout(function () { | ||
console.log('throw exception'); | ||
throw new Error('Ouch, my knee!'); | ||
@@ -25,3 +34,4 @@ }, 200); | ||
case 'exit': | ||
setTimeout(function() { | ||
addUnloadFunction(); | ||
setTimeout(function () { | ||
process.exit(); | ||
@@ -34,3 +44,4 @@ }, 200); | ||
case 'runout': | ||
setTimeout(function() { | ||
addUnloadFunction(); | ||
setTimeout(function () { | ||
console.log('I run out'); | ||
@@ -40,6 +51,7 @@ }, 200); | ||
case 'stopBefore': | ||
setTimeout(function() { | ||
stopListening(); | ||
const stopListening = addUnloadFunction(); | ||
setTimeout(function () { | ||
stopListening.remove(); | ||
}, 200); | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
console.log('I run out'); | ||
@@ -49,4 +61,5 @@ }, 300); | ||
case 'runAll': | ||
addUnloadFunction(); | ||
unload.runAll(); | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
console.log('I run out'); | ||
@@ -56,5 +69,6 @@ }, 300); | ||
case 'runAlltwice': | ||
addUnloadFunction(); | ||
unload.runAll(); | ||
unload.runAll(); | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
console.log('I run out'); | ||
@@ -64,4 +78,5 @@ }, 300); | ||
case 'removeAll': | ||
addUnloadFunction(); | ||
unload.removeAll(); | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
console.log('I run out'); | ||
@@ -68,0 +83,0 @@ }, 300); |
@@ -1,20 +0,30 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const app = express(); | ||
var port = 23230; | ||
var counter = 0; | ||
app.use(cors()); | ||
app.get('/', function(req, res) { | ||
const port = 23230; | ||
let counter = 0; | ||
app.get('/', function (req, res) { | ||
// console.log('recieved ping'); | ||
res.send('pong'); | ||
counter++; | ||
// console.log('got new ping'); | ||
// console.log('counter: ' + counter); | ||
// console.log('got new ping'); | ||
// console.log('counter: ' + counter); | ||
}); | ||
app.get('/counter/', function(req, res) { | ||
app.get('/counter/', function (req, res) { | ||
res.send(counter + ''); | ||
}); | ||
app.listen(port, function() { | ||
app.get('/json/', function (req, res) { | ||
res.send(JSON.stringify({ | ||
counter | ||
})); | ||
}); | ||
app.listen(port, function () { | ||
console.log('Ping-server listening on port ' + port); | ||
}); |
@@ -1,36 +0,65 @@ | ||
var assert = require('assert'); | ||
var pingCount = require('./helper/getPingCount.node.js'); | ||
var exec = require('child_process').exec; | ||
const assert = require('assert'); | ||
const exec = require('child_process').exec; | ||
const request = require('request-promise-native'); | ||
const AsyncTestUtil = require('async-test-util'); | ||
var request = require('request-promise-native'); | ||
var unload = require('../src/index.js'); | ||
const pingCount = require('./helper/getPingCount.node.js'); | ||
const unload = require('../'); | ||
describe('nodejs.test.js', function () { | ||
let startCounter = 0; | ||
describe('nodejs.test.js', function() { | ||
var startCounter = 0; | ||
describe('init', function() { | ||
it('w8 until pingServer started', function(done) { | ||
var check = function() { | ||
pingCount() | ||
.then(function() { | ||
done(); | ||
}) | ||
.catch(function() { | ||
return check(); | ||
}); | ||
describe('init', function () { | ||
it('w8 until pingServer started', async () => { | ||
let ok = false; | ||
while (!ok) { | ||
await AsyncTestUtil.wait(200); | ||
try { | ||
await pingCount(); | ||
ok = true; | ||
} catch (err) { } | ||
} | ||
assert.ok(ok); | ||
}); | ||
it('get start-counter', async () => { | ||
await pingCount(); | ||
}); | ||
}); | ||
describe('internal', () => { | ||
it('add / remove', () => { | ||
const fn = () => { }; | ||
const ret = unload.add(fn); | ||
ret.remove(); | ||
const ret2 = unload.add(fn); | ||
ret2.run(); | ||
unload.removeAll(); | ||
}); | ||
it('.runAll()', async () => { | ||
let did = false; | ||
const fn = function () { | ||
setTimeout(function () { | ||
did = true; | ||
}, 100); | ||
}; | ||
check(); | ||
unload.add(fn); | ||
unload.runAll(); | ||
await AsyncTestUtil.wait(200); | ||
assert.ok(did); | ||
unload.removeAll(); | ||
}); | ||
it('get start-counter', function(done) { | ||
pingCount().then(function(c) { | ||
startCounter = c; | ||
done(); | ||
}); | ||
it('.getSize()', () => { | ||
const s = unload.getSize(); | ||
assert.deepEqual(s, 0); | ||
unload.add(() => { }); | ||
const s2 = unload.getSize(); | ||
assert.deepEqual(s2, 1); | ||
unload.removeAll(); | ||
const s3 = unload.getSize(); | ||
assert.deepEqual(s3, 0); | ||
}); | ||
}); | ||
describe('basic', function() { | ||
it('exception', function(done) { | ||
exec('node ./test/helper/node.js exception', function() { | ||
pingCount().then(function(c) { | ||
describe('basic', function () { | ||
it('exception', function (done) { | ||
exec('node ./test/helper/node.js exception', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter + 1, c); | ||
@@ -42,5 +71,5 @@ startCounter = c; | ||
}); | ||
it('runout', function(done) { | ||
exec('node ./test/helper/node.js runout', function() { | ||
pingCount().then(function(c) { | ||
it('runout', function (done) { | ||
exec('node ./test/helper/node.js runout', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter + 1, c); | ||
@@ -52,5 +81,5 @@ startCounter = c; | ||
}); | ||
it('stopBefore', function(done) { | ||
exec('node ./test/helper/node.js stopBefore', function() { | ||
pingCount().then(function(c) { | ||
it('stopBefore', function (done) { | ||
exec('node ./test/helper/node.js stopBefore', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter, c); | ||
@@ -61,5 +90,5 @@ done(); | ||
}); | ||
it('exit', function(done) { | ||
exec('node ./test/helper/node.js exit', function() { | ||
pingCount().then(function(c) { | ||
it('exit', function (done) { | ||
exec('node ./test/helper/node.js exit', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter, c); | ||
@@ -70,9 +99,9 @@ done(); | ||
}); | ||
it('force run', function(done) { | ||
var stopListening = unload.add(function() { | ||
it('force run', function (done) { | ||
const stopListening = unload.add(function () { | ||
return request('http://localhost:23230/'); | ||
}); | ||
stopListening.run(); | ||
setTimeout(function() { | ||
pingCount().then(function(c) { | ||
setTimeout(function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter + 1, c); | ||
@@ -86,6 +115,6 @@ startCounter = c; | ||
describe('runAll', function() { | ||
it('should run all', function(done) { | ||
exec('node ./test/helper/node.js runAll', function() { | ||
pingCount().then(function(c) { | ||
describe('runAll', () => { | ||
it('should run all', function (done) { | ||
exec('node ./test/helper/node.js runAll', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter + 1, c); | ||
@@ -97,5 +126,5 @@ startCounter = c; | ||
}); | ||
it('should run all twice only once', function(done) { | ||
exec('node ./test/helper/node.js runAlltwice', function() { | ||
pingCount().then(function(c) { | ||
it('should run all twice only once', function (done) { | ||
exec('node ./test/helper/node.js runAlltwice', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter + 1, c); | ||
@@ -108,6 +137,6 @@ startCounter = c; | ||
}); | ||
describe('removeAll', function() { | ||
it('should remove all', function(done) { | ||
exec('node ./test/helper/node.js removeAll', function() { | ||
pingCount().then(function(c) { | ||
describe('removeAll', function () { | ||
it('should remove all', function (done) { | ||
exec('node ./test/helper/node.js removeAll', function () { | ||
pingCount().then(function (c) { | ||
assert.equal(startCounter, c); | ||
@@ -114,0 +143,0 @@ done(); |
@@ -59,8 +59,9 @@ /** | ||
(async()=>{ | ||
const stopListen = unload.add(function(){ | ||
const added = unload.add(function(){ | ||
console.log('Ouch, Im dying.'); | ||
}); | ||
stopListen(); | ||
unload.runAll(); | ||
unload.removeAll(); | ||
added.run(); | ||
added.remove(); | ||
})(); | ||
@@ -67,0 +68,0 @@ `; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
98174
32
2183
1
2
1
49
69
5
+ Addeddetect-node@^2.0.3
+ Addeddetect-node@2.1.0(transitive)