From f118c1db553f32671db9a638a621ddf6e0d31d32 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 Jan 2019 22:05:35 +0100 Subject: [PATCH 1/4] support running inside of a worker thread --- README.md | 1 + index.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af385f3..190079f 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Atomically and asynchronously writes data to a file, replacing the file if it al exists. data can be a string or a buffer. The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`. +Note that `process.pid` is replaced by `require('worker_threads').threadId` if running inside of a worker thread. If writeFile completes successfully then, if passed the **chown** option it will change the ownership of the file. Finally it renames the file back to the filename you specified. If it encounters errors at any of these steps it will attempt to unlink the temporary file and then diff --git a/index.js b/index.js index 3b5607d..6d9e7c0 100644 --- a/index.js +++ b/index.js @@ -10,11 +10,26 @@ var onExit = require('signal-exit') var path = require('path') var activeFiles = {} +// if we run inside of a worker_thread, `process.pid` is not unique +var id = (function getId () { + try { + var workerThreads = require('worker_threads') + + if (workerThreads.isMainThread) { + return process.pid + } + + return workerThreads.threadId || process.pid + } catch (e) { + return process.pid + } +})() + var invocations = 0 function getTmpname (filename) { return filename + '.' + MurmurHash3(__filename) - .hash(String(process.pid)) + .hash(String(id)) .hash(String(++invocations)) .result() } From 7d74261fabb0291aea08b54639fea3fa3fcbf387 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 Jan 2019 22:52:53 +0100 Subject: [PATCH 2/4] keep process.pid as part of hash --- index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6d9e7c0..ba1f00f 100644 --- a/index.js +++ b/index.js @@ -11,17 +11,18 @@ var path = require('path') var activeFiles = {} // if we run inside of a worker_thread, `process.pid` is not unique -var id = (function getId () { +/* istanbul ignore next */ +var threadId = (function getId () { try { var workerThreads = require('worker_threads') if (workerThreads.isMainThread) { - return process.pid + return 0 } - return workerThreads.threadId || process.pid + return workerThreads.threadId || 0 } catch (e) { - return process.pid + return 0 } })() @@ -29,7 +30,8 @@ var invocations = 0 function getTmpname (filename) { return filename + '.' + MurmurHash3(__filename) - .hash(String(id)) + .hash(String(process.pid)) + .hash(String(threadId)) .hash(String(++invocations)) .result() } From bd33dc2e4eae938b5916b768675da756ceb9061d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 Jan 2019 23:01:39 +0100 Subject: [PATCH 3/4] simpliify --- index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index ba1f00f..cdaeac0 100644 --- a/index.js +++ b/index.js @@ -16,12 +16,10 @@ var threadId = (function getId () { try { var workerThreads = require('worker_threads') - if (workerThreads.isMainThread) { - return 0 - } - - return workerThreads.threadId || 0 + /// if we are in main thread, this is set to `0` + return workerThreads.threadId } catch (e) { + // worker_threads are not available, fallback to 0 return 0 } })() From af26539465473bad8c18c347579c5df15b870b93 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 22 Jan 2019 23:17:05 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 190079f..993ed4a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Atomically and asynchronously writes data to a file, replacing the file if it al exists. data can be a string or a buffer. The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`. -Note that `process.pid` is replaced by `require('worker_threads').threadId` if running inside of a worker thread. +Note that `require('worker_threads').threadId` is used in addition to `process.pid` if running inside of a worker thread. If writeFile completes successfully then, if passed the **chown** option it will change the ownership of the file. Finally it renames the file back to the filename you specified. If it encounters errors at any of these steps it will attempt to unlink the temporary file and then