UrgentcareCLI 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Gemfile.lock +60 -25
- data/Notes +1 -1
- data/README.md +5 -5
- data/Rakefile +2 -0
- data/UrgentCare.gemspec +3 -3
- data/background.jpg +0 -0
- data/lib/UrgentCare.rb +3 -0
- data/lib/UrgentCare/CLI.rb +6 -2
- data/lib/UrgentCare/version.rb +1 -1
- data/lib/Urgentcare/Scraper.rb +65 -23
- data/node_modules/.bin/rimraf +1 -0
- data/node_modules/.package-lock.json +250 -0
- data/node_modules/balanced-match/.github/FUNDING.yml +2 -0
- data/node_modules/balanced-match/LICENSE.md +21 -0
- data/node_modules/balanced-match/README.md +97 -0
- data/node_modules/balanced-match/index.js +62 -0
- data/node_modules/balanced-match/package.json +48 -0
- data/node_modules/brace-expansion/LICENSE +21 -0
- data/node_modules/brace-expansion/README.md +129 -0
- data/node_modules/brace-expansion/index.js +201 -0
- data/node_modules/brace-expansion/package.json +47 -0
- data/node_modules/concat-map/.travis.yml +4 -0
- data/node_modules/concat-map/LICENSE +18 -0
- data/node_modules/concat-map/README.markdown +62 -0
- data/node_modules/concat-map/example/map.js +6 -0
- data/node_modules/concat-map/index.js +13 -0
- data/node_modules/concat-map/package.json +43 -0
- data/node_modules/concat-map/test/map.js +39 -0
- data/node_modules/core-util-is/LICENSE +19 -0
- data/node_modules/core-util-is/README.md +3 -0
- data/node_modules/core-util-is/float.patch +604 -0
- data/node_modules/core-util-is/lib/util.js +107 -0
- data/node_modules/core-util-is/package.json +32 -0
- data/node_modules/core-util-is/test.js +68 -0
- data/node_modules/fs.realpath/LICENSE +43 -0
- data/node_modules/fs.realpath/README.md +33 -0
- data/node_modules/fs.realpath/index.js +66 -0
- data/node_modules/fs.realpath/old.js +303 -0
- data/node_modules/fs.realpath/package.json +26 -0
- data/node_modules/glob/LICENSE +21 -0
- data/node_modules/glob/README.md +375 -0
- data/node_modules/glob/changelog.md +67 -0
- data/node_modules/glob/common.js +234 -0
- data/node_modules/glob/glob.js +788 -0
- data/node_modules/glob/package.json +51 -0
- data/node_modules/glob/sync.js +484 -0
- data/node_modules/immediate/LICENSE.txt +20 -0
- data/node_modules/immediate/README.md +93 -0
- data/node_modules/immediate/dist/immediate.js +75 -0
- data/node_modules/immediate/dist/immediate.min.js +1 -0
- data/node_modules/immediate/lib/browser.js +69 -0
- data/node_modules/immediate/lib/index.js +73 -0
- data/node_modules/immediate/package.json +42 -0
- data/node_modules/inflight/LICENSE +15 -0
- data/node_modules/inflight/README.md +37 -0
- data/node_modules/inflight/inflight.js +54 -0
- data/node_modules/inflight/package.json +29 -0
- data/node_modules/inherits/LICENSE +16 -0
- data/node_modules/inherits/README.md +42 -0
- data/node_modules/inherits/inherits.js +9 -0
- data/node_modules/inherits/inherits_browser.js +27 -0
- data/node_modules/inherits/package.json +29 -0
- data/node_modules/isarray/.npmignore +1 -0
- data/node_modules/isarray/.travis.yml +4 -0
- data/node_modules/isarray/Makefile +6 -0
- data/node_modules/isarray/README.md +60 -0
- data/node_modules/isarray/component.json +19 -0
- data/node_modules/isarray/index.js +5 -0
- data/node_modules/isarray/package.json +45 -0
- data/node_modules/isarray/test.js +20 -0
- data/node_modules/jszip/.codeclimate.yml +16 -0
- data/node_modules/jszip/.editorconfig +8 -0
- data/node_modules/jszip/.jshintignore +1 -0
- data/node_modules/jszip/.jshintrc +21 -0
- data/node_modules/jszip/.travis.yml +17 -0
- data/node_modules/jszip/CHANGES.md +163 -0
- data/node_modules/jszip/LICENSE.markdown +651 -0
- data/node_modules/jszip/README.markdown +35 -0
- data/node_modules/jszip/dist/jszip.js +30 -0
- data/node_modules/jszip/dist/jszip.min.js +13 -0
- data/node_modules/jszip/index.d.ts +270 -0
- data/node_modules/jszip/lib/base64.js +106 -0
- data/node_modules/jszip/lib/compressedObject.js +74 -0
- data/node_modules/jszip/lib/compressions.js +14 -0
- data/node_modules/jszip/lib/crc32.js +77 -0
- data/node_modules/jszip/lib/defaults.js +11 -0
- data/node_modules/jszip/lib/external.js +19 -0
- data/node_modules/jszip/lib/flate.js +85 -0
- data/node_modules/jszip/lib/generate/ZipFileWorker.js +540 -0
- data/node_modules/jszip/lib/generate/index.js +57 -0
- data/node_modules/jszip/lib/index.js +52 -0
- data/node_modules/jszip/lib/license_header.js +11 -0
- data/node_modules/jszip/lib/load.js +81 -0
- data/node_modules/jszip/lib/nodejs/NodejsStreamInputAdapter.js +74 -0
- data/node_modules/jszip/lib/nodejs/NodejsStreamOutputAdapter.js +42 -0
- data/node_modules/jszip/lib/nodejsUtils.js +57 -0
- data/node_modules/jszip/lib/object.js +389 -0
- data/node_modules/jszip/lib/readable-stream-browser.js +9 -0
- data/node_modules/jszip/lib/reader/ArrayReader.js +57 -0
- data/node_modules/jszip/lib/reader/DataReader.js +116 -0
- data/node_modules/jszip/lib/reader/NodeBufferReader.js +19 -0
- data/node_modules/jszip/lib/reader/StringReader.js +38 -0
- data/node_modules/jszip/lib/reader/Uint8ArrayReader.js +22 -0
- data/node_modules/jszip/lib/reader/readerFor.js +28 -0
- data/node_modules/jszip/lib/signature.js +7 -0
- data/node_modules/jszip/lib/stream/ConvertWorker.js +26 -0
- data/node_modules/jszip/lib/stream/Crc32Probe.js +24 -0
- data/node_modules/jszip/lib/stream/DataLengthProbe.js +29 -0
- data/node_modules/jszip/lib/stream/DataWorker.js +116 -0
- data/node_modules/jszip/lib/stream/GenericWorker.js +263 -0
- data/node_modules/jszip/lib/stream/StreamHelper.js +212 -0
- data/node_modules/jszip/lib/support.js +38 -0
- data/node_modules/jszip/lib/utf8.js +275 -0
- data/node_modules/jszip/lib/utils.js +476 -0
- data/node_modules/jszip/lib/zipEntries.js +262 -0
- data/node_modules/jszip/lib/zipEntry.js +294 -0
- data/node_modules/jszip/lib/zipObject.js +133 -0
- data/node_modules/jszip/package.json +63 -0
- data/node_modules/jszip/vendor/FileSaver.js +247 -0
- data/node_modules/lie/README.md +62 -0
- data/node_modules/lie/dist/lie.js +350 -0
- data/node_modules/lie/dist/lie.min.js +1 -0
- data/node_modules/lie/dist/lie.polyfill.js +358 -0
- data/node_modules/lie/dist/lie.polyfill.min.js +1 -0
- data/node_modules/lie/lib/browser.js +273 -0
- data/node_modules/lie/lib/index.js +298 -0
- data/node_modules/lie/license.md +7 -0
- data/node_modules/lie/lie.d.ts +244 -0
- data/node_modules/lie/package.json +69 -0
- data/node_modules/lie/polyfill.js +4 -0
- data/node_modules/minimatch/LICENSE +15 -0
- data/node_modules/minimatch/README.md +209 -0
- data/node_modules/minimatch/minimatch.js +923 -0
- data/node_modules/minimatch/package.json +30 -0
- data/node_modules/once/LICENSE +15 -0
- data/node_modules/once/README.md +79 -0
- data/node_modules/once/once.js +42 -0
- data/node_modules/once/package.json +33 -0
- data/node_modules/pako/CHANGELOG.md +164 -0
- data/node_modules/pako/LICENSE +21 -0
- data/node_modules/pako/README.md +191 -0
- data/node_modules/pako/dist/pako.js +6818 -0
- data/node_modules/pako/dist/pako.min.js +1 -0
- data/node_modules/pako/dist/pako_deflate.js +3997 -0
- data/node_modules/pako/dist/pako_deflate.min.js +1 -0
- data/node_modules/pako/dist/pako_inflate.js +3300 -0
- data/node_modules/pako/dist/pako_inflate.min.js +1 -0
- data/node_modules/pako/index.js +14 -0
- data/node_modules/pako/lib/deflate.js +400 -0
- data/node_modules/pako/lib/inflate.js +423 -0
- data/node_modules/pako/lib/utils/common.js +105 -0
- data/node_modules/pako/lib/utils/strings.js +187 -0
- data/node_modules/pako/lib/zlib/README +59 -0
- data/node_modules/pako/lib/zlib/adler32.js +51 -0
- data/node_modules/pako/lib/zlib/constants.js +68 -0
- data/node_modules/pako/lib/zlib/crc32.js +59 -0
- data/node_modules/pako/lib/zlib/deflate.js +1874 -0
- data/node_modules/pako/lib/zlib/gzheader.js +58 -0
- data/node_modules/pako/lib/zlib/inffast.js +345 -0
- data/node_modules/pako/lib/zlib/inflate.js +1556 -0
- data/node_modules/pako/lib/zlib/inftrees.js +343 -0
- data/node_modules/pako/lib/zlib/messages.js +32 -0
- data/node_modules/pako/lib/zlib/trees.js +1222 -0
- data/node_modules/pako/lib/zlib/zstream.js +47 -0
- data/node_modules/pako/package.json +44 -0
- data/node_modules/path-is-absolute/index.js +20 -0
- data/node_modules/path-is-absolute/license +21 -0
- data/node_modules/path-is-absolute/package.json +43 -0
- data/node_modules/path-is-absolute/readme.md +59 -0
- data/node_modules/process-nextick-args/index.js +45 -0
- data/node_modules/process-nextick-args/license.md +19 -0
- data/node_modules/process-nextick-args/package.json +25 -0
- data/node_modules/process-nextick-args/readme.md +18 -0
- data/node_modules/readable-stream/.travis.yml +34 -0
- data/node_modules/readable-stream/CONTRIBUTING.md +38 -0
- data/node_modules/readable-stream/GOVERNANCE.md +136 -0
- data/node_modules/readable-stream/LICENSE +47 -0
- data/node_modules/readable-stream/README.md +58 -0
- data/node_modules/readable-stream/doc/wg-meetings/2015-01-30.md +60 -0
- data/node_modules/readable-stream/duplex-browser.js +1 -0
- data/node_modules/readable-stream/duplex.js +1 -0
- data/node_modules/readable-stream/lib/_stream_duplex.js +131 -0
- data/node_modules/readable-stream/lib/_stream_passthrough.js +47 -0
- data/node_modules/readable-stream/lib/_stream_readable.js +1019 -0
- data/node_modules/readable-stream/lib/_stream_transform.js +214 -0
- data/node_modules/readable-stream/lib/_stream_writable.js +687 -0
- data/node_modules/readable-stream/lib/internal/streams/BufferList.js +79 -0
- data/node_modules/readable-stream/lib/internal/streams/destroy.js +74 -0
- data/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
- data/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
- data/node_modules/readable-stream/package.json +52 -0
- data/node_modules/readable-stream/passthrough.js +1 -0
- data/node_modules/readable-stream/readable-browser.js +7 -0
- data/node_modules/readable-stream/readable.js +19 -0
- data/node_modules/readable-stream/transform.js +1 -0
- data/node_modules/readable-stream/writable-browser.js +1 -0
- data/node_modules/readable-stream/writable.js +8 -0
- data/node_modules/rimraf/LICENSE +15 -0
- data/node_modules/rimraf/README.md +101 -0
- data/node_modules/rimraf/bin.js +50 -0
- data/node_modules/rimraf/package.json +29 -0
- data/node_modules/rimraf/rimraf.js +372 -0
- data/node_modules/safe-buffer/LICENSE +21 -0
- data/node_modules/safe-buffer/README.md +584 -0
- data/node_modules/safe-buffer/index.d.ts +187 -0
- data/node_modules/safe-buffer/index.js +62 -0
- data/node_modules/safe-buffer/package.json +37 -0
- data/node_modules/selenium-webdriver/CHANGES.md +1114 -0
- data/node_modules/selenium-webdriver/LICENSE +202 -0
- data/node_modules/selenium-webdriver/NOTICE +2 -0
- data/node_modules/selenium-webdriver/README.md +229 -0
- data/node_modules/selenium-webdriver/chrome.js +295 -0
- data/node_modules/selenium-webdriver/chromium.js +829 -0
- data/node_modules/selenium-webdriver/devtools/CDPConnection.js +35 -0
- data/node_modules/selenium-webdriver/edge.js +224 -0
- data/node_modules/selenium-webdriver/example/chrome_android.js +45 -0
- data/node_modules/selenium-webdriver/example/chrome_mobile_emulation.js +46 -0
- data/node_modules/selenium-webdriver/example/firefox_channels.js +84 -0
- data/node_modules/selenium-webdriver/example/google_search.js +50 -0
- data/node_modules/selenium-webdriver/example/google_search_test.js +70 -0
- data/node_modules/selenium-webdriver/example/headless.js +63 -0
- data/node_modules/selenium-webdriver/example/logging.js +64 -0
- data/node_modules/selenium-webdriver/firefox.js +789 -0
- data/node_modules/selenium-webdriver/http/index.js +324 -0
- data/node_modules/selenium-webdriver/http/util.js +172 -0
- data/node_modules/selenium-webdriver/ie.js +503 -0
- data/node_modules/selenium-webdriver/index.js +825 -0
- data/node_modules/selenium-webdriver/io/exec.js +162 -0
- data/node_modules/selenium-webdriver/io/index.js +348 -0
- data/node_modules/selenium-webdriver/io/zip.js +223 -0
- data/node_modules/selenium-webdriver/lib/atoms/find-elements.js +123 -0
- data/node_modules/selenium-webdriver/lib/atoms/get-attribute.js +101 -0
- data/node_modules/selenium-webdriver/lib/atoms/is-displayed.js +101 -0
- data/node_modules/selenium-webdriver/lib/atoms/mutation-listener.js +55 -0
- data/node_modules/selenium-webdriver/lib/by.js +415 -0
- data/node_modules/selenium-webdriver/lib/capabilities.js +553 -0
- data/node_modules/selenium-webdriver/lib/command.js +206 -0
- data/node_modules/selenium-webdriver/lib/error.js +605 -0
- data/node_modules/selenium-webdriver/lib/http.js +704 -0
- data/node_modules/selenium-webdriver/lib/input.js +946 -0
- data/node_modules/selenium-webdriver/lib/logging.js +661 -0
- data/node_modules/selenium-webdriver/lib/promise.js +285 -0
- data/node_modules/selenium-webdriver/lib/proxy.js +212 -0
- data/node_modules/selenium-webdriver/lib/session.js +77 -0
- data/node_modules/selenium-webdriver/lib/symbols.js +37 -0
- data/node_modules/selenium-webdriver/lib/until.js +429 -0
- data/node_modules/selenium-webdriver/lib/webdriver.js +2919 -0
- data/node_modules/selenium-webdriver/net/index.js +107 -0
- data/node_modules/selenium-webdriver/net/portprober.js +75 -0
- data/node_modules/selenium-webdriver/opera.js +406 -0
- data/node_modules/selenium-webdriver/package.json +54 -0
- data/node_modules/selenium-webdriver/proxy.js +32 -0
- data/node_modules/selenium-webdriver/remote/index.js +624 -0
- data/node_modules/selenium-webdriver/safari.js +168 -0
- data/node_modules/selenium-webdriver/testing/index.js +504 -0
- data/node_modules/set-immediate-shim/index.js +7 -0
- data/node_modules/set-immediate-shim/package.json +34 -0
- data/node_modules/set-immediate-shim/readme.md +31 -0
- data/node_modules/string_decoder/.travis.yml +50 -0
- data/node_modules/string_decoder/LICENSE +48 -0
- data/node_modules/string_decoder/README.md +47 -0
- data/node_modules/string_decoder/lib/string_decoder.js +296 -0
- data/node_modules/string_decoder/package.json +31 -0
- data/node_modules/tmp/CHANGELOG.md +288 -0
- data/node_modules/tmp/LICENSE +21 -0
- data/node_modules/tmp/README.md +365 -0
- data/node_modules/tmp/lib/tmp.js +780 -0
- data/node_modules/tmp/node_modules/.bin/rimraf +1 -0
- data/node_modules/tmp/node_modules/rimraf/CHANGELOG.md +65 -0
- data/node_modules/tmp/node_modules/rimraf/LICENSE +15 -0
- data/node_modules/tmp/node_modules/rimraf/README.md +101 -0
- data/node_modules/tmp/node_modules/rimraf/bin.js +68 -0
- data/node_modules/tmp/node_modules/rimraf/package.json +32 -0
- data/node_modules/tmp/node_modules/rimraf/rimraf.js +360 -0
- data/node_modules/tmp/package.json +58 -0
- data/node_modules/util-deprecate/History.md +16 -0
- data/node_modules/util-deprecate/LICENSE +24 -0
- data/node_modules/util-deprecate/README.md +53 -0
- data/node_modules/util-deprecate/browser.js +67 -0
- data/node_modules/util-deprecate/node.js +6 -0
- data/node_modules/util-deprecate/package.json +27 -0
- data/node_modules/wrappy/LICENSE +15 -0
- data/node_modules/wrappy/README.md +36 -0
- data/node_modules/wrappy/package.json +29 -0
- data/node_modules/wrappy/wrappy.js +33 -0
- data/node_modules/ws/LICENSE +21 -0
- data/node_modules/ws/README.md +496 -0
- data/node_modules/ws/browser.js +8 -0
- data/node_modules/ws/index.js +10 -0
- data/node_modules/ws/lib/buffer-util.js +129 -0
- data/node_modules/ws/lib/constants.js +10 -0
- data/node_modules/ws/lib/event-target.js +184 -0
- data/node_modules/ws/lib/extension.js +223 -0
- data/node_modules/ws/lib/limiter.js +55 -0
- data/node_modules/ws/lib/permessage-deflate.js +517 -0
- data/node_modules/ws/lib/receiver.js +507 -0
- data/node_modules/ws/lib/sender.js +405 -0
- data/node_modules/ws/lib/stream.js +165 -0
- data/node_modules/ws/lib/validation.js +104 -0
- data/node_modules/ws/lib/websocket-server.js +418 -0
- data/node_modules/ws/lib/websocket.js +942 -0
- data/node_modules/ws/package.json +56 -0
- data/package-lock.json +458 -0
- data/package.json +5 -0
- data/selenium.log +1 -0
- metadata +314 -19
@@ -0,0 +1,780 @@
|
|
1
|
+
/*!
|
2
|
+
* Tmp
|
3
|
+
*
|
4
|
+
* Copyright (c) 2011-2017 KARASZI Istvan <github@spam.raszi.hu>
|
5
|
+
*
|
6
|
+
* MIT Licensed
|
7
|
+
*/
|
8
|
+
|
9
|
+
/*
|
10
|
+
* Module dependencies.
|
11
|
+
*/
|
12
|
+
const fs = require('fs');
|
13
|
+
const os = require('os');
|
14
|
+
const path = require('path');
|
15
|
+
const crypto = require('crypto');
|
16
|
+
const _c = { fs: fs.constants, os: os.constants };
|
17
|
+
const rimraf = require('rimraf');
|
18
|
+
|
19
|
+
/*
|
20
|
+
* The working inner variables.
|
21
|
+
*/
|
22
|
+
const
|
23
|
+
// the random characters to choose from
|
24
|
+
RANDOM_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
25
|
+
|
26
|
+
TEMPLATE_PATTERN = /XXXXXX/,
|
27
|
+
|
28
|
+
DEFAULT_TRIES = 3,
|
29
|
+
|
30
|
+
CREATE_FLAGS = (_c.O_CREAT || _c.fs.O_CREAT) | (_c.O_EXCL || _c.fs.O_EXCL) | (_c.O_RDWR || _c.fs.O_RDWR),
|
31
|
+
|
32
|
+
// constants are off on the windows platform and will not match the actual errno codes
|
33
|
+
IS_WIN32 = os.platform() === 'win32',
|
34
|
+
EBADF = _c.EBADF || _c.os.errno.EBADF,
|
35
|
+
ENOENT = _c.ENOENT || _c.os.errno.ENOENT,
|
36
|
+
|
37
|
+
DIR_MODE = 0o700 /* 448 */,
|
38
|
+
FILE_MODE = 0o600 /* 384 */,
|
39
|
+
|
40
|
+
EXIT = 'exit',
|
41
|
+
|
42
|
+
// this will hold the objects need to be removed on exit
|
43
|
+
_removeObjects = [],
|
44
|
+
|
45
|
+
// API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
|
46
|
+
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
|
47
|
+
FN_RIMRAF_SYNC = rimraf.sync;
|
48
|
+
|
49
|
+
let
|
50
|
+
_gracefulCleanup = false;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Gets a temporary file name.
|
54
|
+
*
|
55
|
+
* @param {(Options|tmpNameCallback)} options options or callback
|
56
|
+
* @param {?tmpNameCallback} callback the callback function
|
57
|
+
*/
|
58
|
+
function tmpName(options, callback) {
|
59
|
+
const
|
60
|
+
args = _parseArguments(options, callback),
|
61
|
+
opts = args[0],
|
62
|
+
cb = args[1];
|
63
|
+
|
64
|
+
try {
|
65
|
+
_assertAndSanitizeOptions(opts);
|
66
|
+
} catch (err) {
|
67
|
+
return cb(err);
|
68
|
+
}
|
69
|
+
|
70
|
+
let tries = opts.tries;
|
71
|
+
(function _getUniqueName() {
|
72
|
+
try {
|
73
|
+
const name = _generateTmpName(opts);
|
74
|
+
|
75
|
+
// check whether the path exists then retry if needed
|
76
|
+
fs.stat(name, function (err) {
|
77
|
+
/* istanbul ignore else */
|
78
|
+
if (!err) {
|
79
|
+
/* istanbul ignore else */
|
80
|
+
if (tries-- > 0) return _getUniqueName();
|
81
|
+
|
82
|
+
return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name));
|
83
|
+
}
|
84
|
+
|
85
|
+
cb(null, name);
|
86
|
+
});
|
87
|
+
} catch (err) {
|
88
|
+
cb(err);
|
89
|
+
}
|
90
|
+
}());
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Synchronous version of tmpName.
|
95
|
+
*
|
96
|
+
* @param {Object} options
|
97
|
+
* @returns {string} the generated random name
|
98
|
+
* @throws {Error} if the options are invalid or could not generate a filename
|
99
|
+
*/
|
100
|
+
function tmpNameSync(options) {
|
101
|
+
const
|
102
|
+
args = _parseArguments(options),
|
103
|
+
opts = args[0];
|
104
|
+
|
105
|
+
_assertAndSanitizeOptions(opts);
|
106
|
+
|
107
|
+
let tries = opts.tries;
|
108
|
+
do {
|
109
|
+
const name = _generateTmpName(opts);
|
110
|
+
try {
|
111
|
+
fs.statSync(name);
|
112
|
+
} catch (e) {
|
113
|
+
return name;
|
114
|
+
}
|
115
|
+
} while (tries-- > 0);
|
116
|
+
|
117
|
+
throw new Error('Could not get a unique tmp filename, max tries reached');
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Creates and opens a temporary file.
|
122
|
+
*
|
123
|
+
* @param {(Options|null|undefined|fileCallback)} options the config options or the callback function or null or undefined
|
124
|
+
* @param {?fileCallback} callback
|
125
|
+
*/
|
126
|
+
function file(options, callback) {
|
127
|
+
const
|
128
|
+
args = _parseArguments(options, callback),
|
129
|
+
opts = args[0],
|
130
|
+
cb = args[1];
|
131
|
+
|
132
|
+
// gets a temporary filename
|
133
|
+
tmpName(opts, function _tmpNameCreated(err, name) {
|
134
|
+
/* istanbul ignore else */
|
135
|
+
if (err) return cb(err);
|
136
|
+
|
137
|
+
// create and open the file
|
138
|
+
fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
|
139
|
+
/* istanbu ignore else */
|
140
|
+
if (err) return cb(err);
|
141
|
+
|
142
|
+
if (opts.discardDescriptor) {
|
143
|
+
return fs.close(fd, function _discardCallback(possibleErr) {
|
144
|
+
// the chance of getting an error on close here is rather low and might occur in the most edgiest cases only
|
145
|
+
return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false));
|
146
|
+
});
|
147
|
+
} else {
|
148
|
+
// detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care
|
149
|
+
// about the descriptor
|
150
|
+
const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
|
151
|
+
cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false));
|
152
|
+
}
|
153
|
+
});
|
154
|
+
});
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* Synchronous version of file.
|
159
|
+
*
|
160
|
+
* @param {Options} options
|
161
|
+
* @returns {FileSyncObject} object consists of name, fd and removeCallback
|
162
|
+
* @throws {Error} if cannot create a file
|
163
|
+
*/
|
164
|
+
function fileSync(options) {
|
165
|
+
const
|
166
|
+
args = _parseArguments(options),
|
167
|
+
opts = args[0];
|
168
|
+
|
169
|
+
const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
|
170
|
+
const name = tmpNameSync(opts);
|
171
|
+
var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE);
|
172
|
+
/* istanbul ignore else */
|
173
|
+
if (opts.discardDescriptor) {
|
174
|
+
fs.closeSync(fd);
|
175
|
+
fd = undefined;
|
176
|
+
}
|
177
|
+
|
178
|
+
return {
|
179
|
+
name: name,
|
180
|
+
fd: fd,
|
181
|
+
removeCallback: _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, true)
|
182
|
+
};
|
183
|
+
}
|
184
|
+
|
185
|
+
/**
|
186
|
+
* Creates a temporary directory.
|
187
|
+
*
|
188
|
+
* @param {(Options|dirCallback)} options the options or the callback function
|
189
|
+
* @param {?dirCallback} callback
|
190
|
+
*/
|
191
|
+
function dir(options, callback) {
|
192
|
+
const
|
193
|
+
args = _parseArguments(options, callback),
|
194
|
+
opts = args[0],
|
195
|
+
cb = args[1];
|
196
|
+
|
197
|
+
// gets a temporary filename
|
198
|
+
tmpName(opts, function _tmpNameCreated(err, name) {
|
199
|
+
/* istanbul ignore else */
|
200
|
+
if (err) return cb(err);
|
201
|
+
|
202
|
+
// create the directory
|
203
|
+
fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) {
|
204
|
+
/* istanbul ignore else */
|
205
|
+
if (err) return cb(err);
|
206
|
+
|
207
|
+
cb(null, name, _prepareTmpDirRemoveCallback(name, opts, false));
|
208
|
+
});
|
209
|
+
});
|
210
|
+
}
|
211
|
+
|
212
|
+
/**
|
213
|
+
* Synchronous version of dir.
|
214
|
+
*
|
215
|
+
* @param {Options} options
|
216
|
+
* @returns {DirSyncObject} object consists of name and removeCallback
|
217
|
+
* @throws {Error} if it cannot create a directory
|
218
|
+
*/
|
219
|
+
function dirSync(options) {
|
220
|
+
const
|
221
|
+
args = _parseArguments(options),
|
222
|
+
opts = args[0];
|
223
|
+
|
224
|
+
const name = tmpNameSync(opts);
|
225
|
+
fs.mkdirSync(name, opts.mode || DIR_MODE);
|
226
|
+
|
227
|
+
return {
|
228
|
+
name: name,
|
229
|
+
removeCallback: _prepareTmpDirRemoveCallback(name, opts, true)
|
230
|
+
};
|
231
|
+
}
|
232
|
+
|
233
|
+
/**
|
234
|
+
* Removes files asynchronously.
|
235
|
+
*
|
236
|
+
* @param {Object} fdPath
|
237
|
+
* @param {Function} next
|
238
|
+
* @private
|
239
|
+
*/
|
240
|
+
function _removeFileAsync(fdPath, next) {
|
241
|
+
const _handler = function (err) {
|
242
|
+
if (err && !_isENOENT(err)) {
|
243
|
+
// reraise any unanticipated error
|
244
|
+
return next(err);
|
245
|
+
}
|
246
|
+
next();
|
247
|
+
};
|
248
|
+
|
249
|
+
if (0 <= fdPath[0])
|
250
|
+
fs.close(fdPath[0], function () {
|
251
|
+
fs.unlink(fdPath[1], _handler);
|
252
|
+
});
|
253
|
+
else fs.unlink(fdPath[1], _handler);
|
254
|
+
}
|
255
|
+
|
256
|
+
/**
|
257
|
+
* Removes files synchronously.
|
258
|
+
*
|
259
|
+
* @param {Object} fdPath
|
260
|
+
* @private
|
261
|
+
*/
|
262
|
+
function _removeFileSync(fdPath) {
|
263
|
+
let rethrownException = null;
|
264
|
+
try {
|
265
|
+
if (0 <= fdPath[0]) fs.closeSync(fdPath[0]);
|
266
|
+
} catch (e) {
|
267
|
+
// reraise any unanticipated error
|
268
|
+
if (!_isEBADF(e) && !_isENOENT(e)) throw e;
|
269
|
+
} finally {
|
270
|
+
try {
|
271
|
+
fs.unlinkSync(fdPath[1]);
|
272
|
+
}
|
273
|
+
catch (e) {
|
274
|
+
// reraise any unanticipated error
|
275
|
+
if (!_isENOENT(e)) rethrownException = e;
|
276
|
+
}
|
277
|
+
}
|
278
|
+
if (rethrownException !== null) {
|
279
|
+
throw rethrownException;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
/**
|
284
|
+
* Prepares the callback for removal of the temporary file.
|
285
|
+
*
|
286
|
+
* Returns either a sync callback or a async callback depending on whether
|
287
|
+
* fileSync or file was called, which is expressed by the sync parameter.
|
288
|
+
*
|
289
|
+
* @param {string} name the path of the file
|
290
|
+
* @param {number} fd file descriptor
|
291
|
+
* @param {Object} opts
|
292
|
+
* @param {boolean} sync
|
293
|
+
* @returns {fileCallback | fileCallbackSync}
|
294
|
+
* @private
|
295
|
+
*/
|
296
|
+
function _prepareTmpFileRemoveCallback(name, fd, opts, sync) {
|
297
|
+
const removeCallbackSync = _prepareRemoveCallback(_removeFileSync, [fd, name], sync);
|
298
|
+
const removeCallback = _prepareRemoveCallback(_removeFileAsync, [fd, name], sync, removeCallbackSync);
|
299
|
+
|
300
|
+
if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
|
301
|
+
|
302
|
+
return sync ? removeCallbackSync : removeCallback;
|
303
|
+
}
|
304
|
+
|
305
|
+
/**
|
306
|
+
* Prepares the callback for removal of the temporary directory.
|
307
|
+
*
|
308
|
+
* Returns either a sync callback or a async callback depending on whether
|
309
|
+
* tmpFileSync or tmpFile was called, which is expressed by the sync parameter.
|
310
|
+
*
|
311
|
+
* @param {string} name
|
312
|
+
* @param {Object} opts
|
313
|
+
* @param {boolean} sync
|
314
|
+
* @returns {Function} the callback
|
315
|
+
* @private
|
316
|
+
*/
|
317
|
+
function _prepareTmpDirRemoveCallback(name, opts, sync) {
|
318
|
+
const removeFunction = opts.unsafeCleanup ? rimraf : fs.rmdir.bind(fs);
|
319
|
+
const removeFunctionSync = opts.unsafeCleanup ? FN_RIMRAF_SYNC : FN_RMDIR_SYNC;
|
320
|
+
const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync);
|
321
|
+
const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync);
|
322
|
+
if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
|
323
|
+
|
324
|
+
return sync ? removeCallbackSync : removeCallback;
|
325
|
+
}
|
326
|
+
|
327
|
+
/**
|
328
|
+
* Creates a guarded function wrapping the removeFunction call.
|
329
|
+
*
|
330
|
+
* The cleanup callback is save to be called multiple times.
|
331
|
+
* Subsequent invocations will be ignored.
|
332
|
+
*
|
333
|
+
* @param {Function} removeFunction
|
334
|
+
* @param {string} fileOrDirName
|
335
|
+
* @param {boolean} sync
|
336
|
+
* @param {cleanupCallbackSync?} cleanupCallbackSync
|
337
|
+
* @returns {cleanupCallback | cleanupCallbackSync}
|
338
|
+
* @private
|
339
|
+
*/
|
340
|
+
function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCallbackSync) {
|
341
|
+
let called = false;
|
342
|
+
|
343
|
+
// if sync is true, the next parameter will be ignored
|
344
|
+
return function _cleanupCallback(next) {
|
345
|
+
|
346
|
+
/* istanbul ignore else */
|
347
|
+
if (!called) {
|
348
|
+
// remove cleanupCallback from cache
|
349
|
+
const toRemove = cleanupCallbackSync || _cleanupCallback;
|
350
|
+
const index = _removeObjects.indexOf(toRemove);
|
351
|
+
/* istanbul ignore else */
|
352
|
+
if (index >= 0) _removeObjects.splice(index, 1);
|
353
|
+
|
354
|
+
called = true;
|
355
|
+
if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction === FN_RIMRAF_SYNC) {
|
356
|
+
return removeFunction(fileOrDirName);
|
357
|
+
} else {
|
358
|
+
return removeFunction(fileOrDirName, next || function() {});
|
359
|
+
}
|
360
|
+
}
|
361
|
+
};
|
362
|
+
}
|
363
|
+
|
364
|
+
/**
|
365
|
+
* The garbage collector.
|
366
|
+
*
|
367
|
+
* @private
|
368
|
+
*/
|
369
|
+
function _garbageCollector() {
|
370
|
+
/* istanbul ignore else */
|
371
|
+
if (!_gracefulCleanup) return;
|
372
|
+
|
373
|
+
// the function being called removes itself from _removeObjects,
|
374
|
+
// loop until _removeObjects is empty
|
375
|
+
while (_removeObjects.length) {
|
376
|
+
try {
|
377
|
+
_removeObjects[0]();
|
378
|
+
} catch (e) {
|
379
|
+
// already removed?
|
380
|
+
}
|
381
|
+
}
|
382
|
+
}
|
383
|
+
|
384
|
+
/**
|
385
|
+
* Random name generator based on crypto.
|
386
|
+
* Adapted from http://blog.tompawlak.org/how-to-generate-random-values-nodejs-javascript
|
387
|
+
*
|
388
|
+
* @param {number} howMany
|
389
|
+
* @returns {string} the generated random name
|
390
|
+
* @private
|
391
|
+
*/
|
392
|
+
function _randomChars(howMany) {
|
393
|
+
let
|
394
|
+
value = [],
|
395
|
+
rnd = null;
|
396
|
+
|
397
|
+
// make sure that we do not fail because we ran out of entropy
|
398
|
+
try {
|
399
|
+
rnd = crypto.randomBytes(howMany);
|
400
|
+
} catch (e) {
|
401
|
+
rnd = crypto.pseudoRandomBytes(howMany);
|
402
|
+
}
|
403
|
+
|
404
|
+
for (var i = 0; i < howMany; i++) {
|
405
|
+
value.push(RANDOM_CHARS[rnd[i] % RANDOM_CHARS.length]);
|
406
|
+
}
|
407
|
+
|
408
|
+
return value.join('');
|
409
|
+
}
|
410
|
+
|
411
|
+
/**
|
412
|
+
* Helper which determines whether a string s is blank, that is undefined, or empty or null.
|
413
|
+
*
|
414
|
+
* @private
|
415
|
+
* @param {string} s
|
416
|
+
* @returns {Boolean} true whether the string s is blank, false otherwise
|
417
|
+
*/
|
418
|
+
function _isBlank(s) {
|
419
|
+
return s === null || _isUndefined(s) || !s.trim();
|
420
|
+
}
|
421
|
+
|
422
|
+
/**
|
423
|
+
* Checks whether the `obj` parameter is defined or not.
|
424
|
+
*
|
425
|
+
* @param {Object} obj
|
426
|
+
* @returns {boolean} true if the object is undefined
|
427
|
+
* @private
|
428
|
+
*/
|
429
|
+
function _isUndefined(obj) {
|
430
|
+
return typeof obj === 'undefined';
|
431
|
+
}
|
432
|
+
|
433
|
+
/**
|
434
|
+
* Parses the function arguments.
|
435
|
+
*
|
436
|
+
* This function helps to have optional arguments.
|
437
|
+
*
|
438
|
+
* @param {(Options|null|undefined|Function)} options
|
439
|
+
* @param {?Function} callback
|
440
|
+
* @returns {Array} parsed arguments
|
441
|
+
* @private
|
442
|
+
*/
|
443
|
+
function _parseArguments(options, callback) {
|
444
|
+
/* istanbul ignore else */
|
445
|
+
if (typeof options === 'function') {
|
446
|
+
return [{}, options];
|
447
|
+
}
|
448
|
+
|
449
|
+
/* istanbul ignore else */
|
450
|
+
if (_isUndefined(options)) {
|
451
|
+
return [{}, callback];
|
452
|
+
}
|
453
|
+
|
454
|
+
// copy options so we do not leak the changes we make internally
|
455
|
+
const actualOptions = {};
|
456
|
+
for (const key of Object.getOwnPropertyNames(options)) {
|
457
|
+
actualOptions[key] = options[key];
|
458
|
+
}
|
459
|
+
|
460
|
+
return [actualOptions, callback];
|
461
|
+
}
|
462
|
+
|
463
|
+
/**
|
464
|
+
* Generates a new temporary name.
|
465
|
+
*
|
466
|
+
* @param {Object} opts
|
467
|
+
* @returns {string} the new random name according to opts
|
468
|
+
* @private
|
469
|
+
*/
|
470
|
+
function _generateTmpName(opts) {
|
471
|
+
|
472
|
+
const tmpDir = opts.tmpdir;
|
473
|
+
|
474
|
+
/* istanbul ignore else */
|
475
|
+
if (!_isUndefined(opts.name))
|
476
|
+
return path.join(tmpDir, opts.dir, opts.name);
|
477
|
+
|
478
|
+
/* istanbul ignore else */
|
479
|
+
if (!_isUndefined(opts.template))
|
480
|
+
return path.join(tmpDir, opts.dir, opts.template).replace(TEMPLATE_PATTERN, _randomChars(6));
|
481
|
+
|
482
|
+
// prefix and postfix
|
483
|
+
const name = [
|
484
|
+
opts.prefix ? opts.prefix : 'tmp',
|
485
|
+
'-',
|
486
|
+
process.pid,
|
487
|
+
'-',
|
488
|
+
_randomChars(12),
|
489
|
+
opts.postfix ? '-' + opts.postfix : ''
|
490
|
+
].join('');
|
491
|
+
|
492
|
+
return path.join(tmpDir, opts.dir, name);
|
493
|
+
}
|
494
|
+
|
495
|
+
/**
|
496
|
+
* Asserts whether the specified options are valid, also sanitizes options and provides sane defaults for missing
|
497
|
+
* options.
|
498
|
+
*
|
499
|
+
* @param {Options} options
|
500
|
+
* @private
|
501
|
+
*/
|
502
|
+
function _assertAndSanitizeOptions(options) {
|
503
|
+
|
504
|
+
options.tmpdir = _getTmpDir(options);
|
505
|
+
|
506
|
+
const tmpDir = options.tmpdir;
|
507
|
+
|
508
|
+
/* istanbul ignore else */
|
509
|
+
if (!_isUndefined(options.name))
|
510
|
+
_assertIsRelative(options.name, 'name', tmpDir);
|
511
|
+
/* istanbul ignore else */
|
512
|
+
if (!_isUndefined(options.dir))
|
513
|
+
_assertIsRelative(options.dir, 'dir', tmpDir);
|
514
|
+
/* istanbul ignore else */
|
515
|
+
if (!_isUndefined(options.template)) {
|
516
|
+
_assertIsRelative(options.template, 'template', tmpDir);
|
517
|
+
if (!options.template.match(TEMPLATE_PATTERN))
|
518
|
+
throw new Error(`Invalid template, found "${options.template}".`);
|
519
|
+
}
|
520
|
+
/* istanbul ignore else */
|
521
|
+
if (!_isUndefined(options.tries) && isNaN(options.tries) || options.tries < 0)
|
522
|
+
throw new Error(`Invalid tries, found "${options.tries}".`);
|
523
|
+
|
524
|
+
// if a name was specified we will try once
|
525
|
+
options.tries = _isUndefined(options.name) ? options.tries || DEFAULT_TRIES : 1;
|
526
|
+
options.keep = !!options.keep;
|
527
|
+
options.detachDescriptor = !!options.detachDescriptor;
|
528
|
+
options.discardDescriptor = !!options.discardDescriptor;
|
529
|
+
options.unsafeCleanup = !!options.unsafeCleanup;
|
530
|
+
|
531
|
+
// sanitize dir, also keep (multiple) blanks if the user, purportedly sane, requests us to
|
532
|
+
options.dir = _isUndefined(options.dir) ? '' : path.relative(tmpDir, _resolvePath(options.dir, tmpDir));
|
533
|
+
options.template = _isUndefined(options.template) ? undefined : path.relative(tmpDir, _resolvePath(options.template, tmpDir));
|
534
|
+
// sanitize further if template is relative to options.dir
|
535
|
+
options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template);
|
536
|
+
|
537
|
+
// for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to
|
538
|
+
options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name);
|
539
|
+
options.prefix = _isUndefined(options.prefix) ? '' : options.prefix;
|
540
|
+
options.postfix = _isUndefined(options.postfix) ? '' : options.postfix;
|
541
|
+
}
|
542
|
+
|
543
|
+
/**
|
544
|
+
* Resolve the specified path name in respect to tmpDir.
|
545
|
+
*
|
546
|
+
* The specified name might include relative path components, e.g. ../
|
547
|
+
* so we need to resolve in order to be sure that is is located inside tmpDir
|
548
|
+
*
|
549
|
+
* @param name
|
550
|
+
* @param tmpDir
|
551
|
+
* @returns {string}
|
552
|
+
* @private
|
553
|
+
*/
|
554
|
+
function _resolvePath(name, tmpDir) {
|
555
|
+
const sanitizedName = _sanitizeName(name);
|
556
|
+
if (sanitizedName.startsWith(tmpDir)) {
|
557
|
+
return path.resolve(sanitizedName);
|
558
|
+
} else {
|
559
|
+
return path.resolve(path.join(tmpDir, sanitizedName));
|
560
|
+
}
|
561
|
+
}
|
562
|
+
|
563
|
+
/**
|
564
|
+
* Sanitize the specified path name by removing all quote characters.
|
565
|
+
*
|
566
|
+
* @param name
|
567
|
+
* @returns {string}
|
568
|
+
* @private
|
569
|
+
*/
|
570
|
+
function _sanitizeName(name) {
|
571
|
+
if (_isBlank(name)) {
|
572
|
+
return name;
|
573
|
+
}
|
574
|
+
return name.replace(/["']/g, '');
|
575
|
+
}
|
576
|
+
|
577
|
+
/**
|
578
|
+
* Asserts whether specified name is relative to the specified tmpDir.
|
579
|
+
*
|
580
|
+
* @param {string} name
|
581
|
+
* @param {string} option
|
582
|
+
* @param {string} tmpDir
|
583
|
+
* @throws {Error}
|
584
|
+
* @private
|
585
|
+
*/
|
586
|
+
function _assertIsRelative(name, option, tmpDir) {
|
587
|
+
if (option === 'name') {
|
588
|
+
// assert that name is not absolute and does not contain a path
|
589
|
+
if (path.isAbsolute(name))
|
590
|
+
throw new Error(`${option} option must not contain an absolute path, found "${name}".`);
|
591
|
+
// must not fail on valid .<name> or ..<name> or similar such constructs
|
592
|
+
let basename = path.basename(name);
|
593
|
+
if (basename === '..' || basename === '.' || basename !== name)
|
594
|
+
throw new Error(`${option} option must not contain a path, found "${name}".`);
|
595
|
+
}
|
596
|
+
else { // if (option === 'dir' || option === 'template') {
|
597
|
+
// assert that dir or template are relative to tmpDir
|
598
|
+
if (path.isAbsolute(name) && !name.startsWith(tmpDir)) {
|
599
|
+
throw new Error(`${option} option must be relative to "${tmpDir}", found "${name}".`);
|
600
|
+
}
|
601
|
+
let resolvedPath = _resolvePath(name, tmpDir);
|
602
|
+
if (!resolvedPath.startsWith(tmpDir))
|
603
|
+
throw new Error(`${option} option must be relative to "${tmpDir}", found "${resolvedPath}".`);
|
604
|
+
}
|
605
|
+
}
|
606
|
+
|
607
|
+
/**
|
608
|
+
* Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
|
609
|
+
*
|
610
|
+
* @private
|
611
|
+
*/
|
612
|
+
function _isEBADF(error) {
|
613
|
+
return _isExpectedError(error, -EBADF, 'EBADF');
|
614
|
+
}
|
615
|
+
|
616
|
+
/**
|
617
|
+
* Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
|
618
|
+
*
|
619
|
+
* @private
|
620
|
+
*/
|
621
|
+
function _isENOENT(error) {
|
622
|
+
return _isExpectedError(error, -ENOENT, 'ENOENT');
|
623
|
+
}
|
624
|
+
|
625
|
+
/**
|
626
|
+
* Helper to determine whether the expected error code matches the actual code and errno,
|
627
|
+
* which will differ between the supported node versions.
|
628
|
+
*
|
629
|
+
* - Node >= 7.0:
|
630
|
+
* error.code {string}
|
631
|
+
* error.errno {number} any numerical value will be negated
|
632
|
+
*
|
633
|
+
* CAVEAT
|
634
|
+
*
|
635
|
+
* On windows, the errno for EBADF is -4083 but os.constants.errno.EBADF is different and we must assume that ENOENT
|
636
|
+
* is no different here.
|
637
|
+
*
|
638
|
+
* @param {SystemError} error
|
639
|
+
* @param {number} errno
|
640
|
+
* @param {string} code
|
641
|
+
* @private
|
642
|
+
*/
|
643
|
+
function _isExpectedError(error, errno, code) {
|
644
|
+
return IS_WIN32 ? error.code === code : error.code === code && error.errno === errno;
|
645
|
+
}
|
646
|
+
|
647
|
+
/**
|
648
|
+
* Sets the graceful cleanup.
|
649
|
+
*
|
650
|
+
* If graceful cleanup is set, tmp will remove all controlled temporary objects on process exit, otherwise the
|
651
|
+
* temporary objects will remain in place, waiting to be cleaned up on system restart or otherwise scheduled temporary
|
652
|
+
* object removals.
|
653
|
+
*/
|
654
|
+
function setGracefulCleanup() {
|
655
|
+
_gracefulCleanup = true;
|
656
|
+
}
|
657
|
+
|
658
|
+
/**
|
659
|
+
* Returns the currently configured tmp dir from os.tmpdir().
|
660
|
+
*
|
661
|
+
* @private
|
662
|
+
* @param {?Options} options
|
663
|
+
* @returns {string} the currently configured tmp dir
|
664
|
+
*/
|
665
|
+
function _getTmpDir(options) {
|
666
|
+
return path.resolve(_sanitizeName(options && options.tmpdir || os.tmpdir()));
|
667
|
+
}
|
668
|
+
|
669
|
+
// Install process exit listener
|
670
|
+
process.addListener(EXIT, _garbageCollector);
|
671
|
+
|
672
|
+
/**
|
673
|
+
* Configuration options.
|
674
|
+
*
|
675
|
+
* @typedef {Object} Options
|
676
|
+
* @property {?boolean} keep the temporary object (file or dir) will not be garbage collected
|
677
|
+
* @property {?number} tries the number of tries before give up the name generation
|
678
|
+
* @property (?int) mode the access mode, defaults are 0o700 for directories and 0o600 for files
|
679
|
+
* @property {?string} template the "mkstemp" like filename template
|
680
|
+
* @property {?string} name fixed name relative to tmpdir or the specified dir option
|
681
|
+
* @property {?string} dir tmp directory relative to the root tmp directory in use
|
682
|
+
* @property {?string} prefix prefix for the generated name
|
683
|
+
* @property {?string} postfix postfix for the generated name
|
684
|
+
* @property {?string} tmpdir the root tmp directory which overrides the os tmpdir
|
685
|
+
* @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty
|
686
|
+
* @property {?boolean} detachDescriptor detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection
|
687
|
+
* @property {?boolean} discardDescriptor discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection
|
688
|
+
*/
|
689
|
+
|
690
|
+
/**
|
691
|
+
* @typedef {Object} FileSyncObject
|
692
|
+
* @property {string} name the name of the file
|
693
|
+
* @property {string} fd the file descriptor or -1 if the fd has been discarded
|
694
|
+
* @property {fileCallback} removeCallback the callback function to remove the file
|
695
|
+
*/
|
696
|
+
|
697
|
+
/**
|
698
|
+
* @typedef {Object} DirSyncObject
|
699
|
+
* @property {string} name the name of the directory
|
700
|
+
* @property {fileCallback} removeCallback the callback function to remove the directory
|
701
|
+
*/
|
702
|
+
|
703
|
+
/**
|
704
|
+
* @callback tmpNameCallback
|
705
|
+
* @param {?Error} err the error object if anything goes wrong
|
706
|
+
* @param {string} name the temporary file name
|
707
|
+
*/
|
708
|
+
|
709
|
+
/**
|
710
|
+
* @callback fileCallback
|
711
|
+
* @param {?Error} err the error object if anything goes wrong
|
712
|
+
* @param {string} name the temporary file name
|
713
|
+
* @param {number} fd the file descriptor or -1 if the fd had been discarded
|
714
|
+
* @param {cleanupCallback} fn the cleanup callback function
|
715
|
+
*/
|
716
|
+
|
717
|
+
/**
|
718
|
+
* @callback fileCallbackSync
|
719
|
+
* @param {?Error} err the error object if anything goes wrong
|
720
|
+
* @param {string} name the temporary file name
|
721
|
+
* @param {number} fd the file descriptor or -1 if the fd had been discarded
|
722
|
+
* @param {cleanupCallbackSync} fn the cleanup callback function
|
723
|
+
*/
|
724
|
+
|
725
|
+
/**
|
726
|
+
* @callback dirCallback
|
727
|
+
* @param {?Error} err the error object if anything goes wrong
|
728
|
+
* @param {string} name the temporary file name
|
729
|
+
* @param {cleanupCallback} fn the cleanup callback function
|
730
|
+
*/
|
731
|
+
|
732
|
+
/**
|
733
|
+
* @callback dirCallbackSync
|
734
|
+
* @param {?Error} err the error object if anything goes wrong
|
735
|
+
* @param {string} name the temporary file name
|
736
|
+
* @param {cleanupCallbackSync} fn the cleanup callback function
|
737
|
+
*/
|
738
|
+
|
739
|
+
/**
|
740
|
+
* Removes the temporary created file or directory.
|
741
|
+
*
|
742
|
+
* @callback cleanupCallback
|
743
|
+
* @param {simpleCallback} [next] function to call whenever the tmp object needs to be removed
|
744
|
+
*/
|
745
|
+
|
746
|
+
/**
|
747
|
+
* Removes the temporary created file or directory.
|
748
|
+
*
|
749
|
+
* @callback cleanupCallbackSync
|
750
|
+
*/
|
751
|
+
|
752
|
+
/**
|
753
|
+
* Callback function for function composition.
|
754
|
+
* @see {@link https://github.com/raszi/node-tmp/issues/57|raszi/node-tmp#57}
|
755
|
+
*
|
756
|
+
* @callback simpleCallback
|
757
|
+
*/
|
758
|
+
|
759
|
+
// exporting all the needed methods
|
760
|
+
|
761
|
+
// evaluate _getTmpDir() lazily, mainly for simplifying testing but it also will
|
762
|
+
// allow users to reconfigure the temporary directory
|
763
|
+
Object.defineProperty(module.exports, 'tmpdir', {
|
764
|
+
enumerable: true,
|
765
|
+
configurable: false,
|
766
|
+
get: function () {
|
767
|
+
return _getTmpDir();
|
768
|
+
}
|
769
|
+
});
|
770
|
+
|
771
|
+
module.exports.dir = dir;
|
772
|
+
module.exports.dirSync = dirSync;
|
773
|
+
|
774
|
+
module.exports.file = file;
|
775
|
+
module.exports.fileSync = fileSync;
|
776
|
+
|
777
|
+
module.exports.tmpName = tmpName;
|
778
|
+
module.exports.tmpNameSync = tmpNameSync;
|
779
|
+
|
780
|
+
module.exports.setGracefulCleanup = setGracefulCleanup;
|