Skip to content

Commit

Permalink
Dereference timers in node.js so that the process may exit when finis…
Browse files Browse the repository at this point in the history
…hed (#139)
  • Loading branch information
danthegoodman authored May 17, 2024
1 parent 6b18b6f commit f44ad1d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class Backend {
this.options = { ...getDefaults(), ...(this.options || {}), ...options }
this.allOptions = allOptions
if (this.services && this.options.reloadInterval) {
setInterval(() => this.reload(), this.options.reloadInterval)
const timer = setInterval(() => this.reload(), this.options.reloadInterval)
if (typeof timer === 'object') {
timer.unref()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"compile": "npm run compile:esm && npm run compile:cjs",
"browser": "browserify --ignore cross-fetch --standalone i18nextHttpBackend cjs/index.js -o i18nextHttpBackend.js && uglifyjs i18nextHttpBackend.js --compress --mangle -o i18nextHttpBackend.min.js",
"build": "npm run compile && npm run browser",
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --exit --experimental-modules",
"test:fetch": "mocha test -R spec --exit --experimental-modules",
"test:xmlhttpreq": "mocha test -R spec --require test/fixtures/xmlHttpRequest.cjs --experimental-modules",
"test:fetch": "mocha test -R spec --experimental-modules",
"test": "npm run lint && npm run build && npm run test:fetch && npm run test:xmlhttpreq && npm run test:typescript",
"test:typescript": "tslint --project tsconfig.json && tsd",
"test:deno": "deno test --allow-net test/deno/*.js",
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const server = (done) => {
if (js) return done(null, js)

js = jsonServer.create()
js.use((req, res, next)=> {

Check failure on line 9 in test/fixtures/server.js

View workflow job for this annotation

GitHub Actions / Test on node 20.x and ubuntu-latest

Missing space before =>

Check failure on line 9 in test/fixtures/server.js

View workflow job for this annotation

GitHub Actions / Test on node 16.x and ubuntu-latest

Missing space before =>
// disable keep alive so the test server can close quickly.
res.setHeader('Connection', 'close')
next()
})
js.use(jsonServer.bodyParser)

js.get('/locales/en/testqs', (req, res) => {
Expand Down Expand Up @@ -54,7 +59,7 @@ const server = (done) => {
js.listen(5001, () => {
console.log('JSON Server is running')
done(null, js)
})
}).unref()
}

export default server
4 changes: 2 additions & 2 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
errO = err
dataO = data
resolve(true)
setTimeout(() => reject(new Error('timeout')), 1500)
})
setTimeout(() => reject(new Error('timeout')), 1500).unref()
})
// evaluate outside callback to get actuall error when something is wrong
expect(errO).to.be(null)
Expand Down Expand Up @@ -72,8 +72,8 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
errO = err
dataO = data
resolve(true)
setTimeout(() => reject(new Error('timeout')), 1500)
})
setTimeout(() => reject(new Error('timeout')), 1500).unref()
})
// evaluate outside callback to get actuall error when something is wrong
expect(errO).to.be(null)
Expand Down

0 comments on commit f44ad1d

Please sign in to comment.