From 29642fceeb319a09f8a5be448142e6a9050b30f2 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 13:42:20 +0100 Subject: [PATCH 01/24] update jsdoc --- docs/global.html | 1717 ++++++++++++++++++++++++--------- docs/index.html | 189 +++- docs/scripts/linenumber.js | 18 +- docs/styles/jsdoc-default.css | 12 +- docs/tmp.js.html | 478 ++++++--- 5 files changed, 1766 insertions(+), 648 deletions(-) diff --git a/docs/global.html b/docs/global.html index 820fcd7..fd5a9e5 100644 --- a/docs/global.html +++ b/docs/global.html @@ -87,42 +87,6 @@

- - - - - -

Members

- - - -

(constant) tmpDir :string

- - - - -
- The temporary directory. -
- - - -
Type:
- - - - - - -
- @@ -132,44 +96,6 @@
Type:
- - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - -

Methods

@@ -177,7 +103,9 @@

Methods

+

dir(options, callbacknullable)

+ @@ -323,7 +251,7 @@
Parameters:
Source:
@@ -349,12 +277,18 @@
Parameters:
+ + + + +

dirSync(options) → {DirSyncObject}

+ @@ -454,7 +388,7 @@
Parameters:
Source:
@@ -475,6 +409,8 @@
Parameters:
+ +
Throws:
@@ -531,12 +467,16 @@
Returns:
+ + +

file(options, callbacknullable)

+ @@ -682,7 +622,7 @@
Parameters:
Source:
@@ -708,12 +648,18 @@
Parameters:
+ + + + +

fileSync(options) → {FileSyncObject}

+ @@ -813,7 +759,7 @@
Parameters:
Source:
@@ -834,6 +780,8 @@
Parameters:
+ +
Throws:
@@ -890,21 +838,23 @@
Returns:
+ + -

setGracefulCleanup()

+ +

isEBADF()

+
- Sets the graceful cleanup. - -Also removes the created files and directories when an uncaught exception occurs. + Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
@@ -948,7 +898,7 @@

set
Source:
@@ -974,122 +924,33 @@

set - - - - - -

tmpName(options, callbacknullable)

- - - - - -
- Gets a temporary file name. -
- - - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + +

isENOENT()

+ - - - - - - - - - - - - - -
NameTypeAttributesDescription
options - - -Options -| - -tmpNameCallback - - - - - - - - - - options or callback
callback - - -tmpNameCallback +
+ Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows. +
- -
- - - <nullable>
- - -
the callback function
@@ -1125,7 +986,7 @@
Parameters:
Source:
@@ -1151,76 +1012,50 @@
Parameters:
- - - - - -

tmpNameSync(options) → {string}

- - - - - -
- Synchronous version of tmpName. -
- - + + + + +

isExpectedError()

+ -
Parameters:
- - - - - - - - +
+ Helper to determine whether the expected error code matches the actual code and errno, +which will differ between the supported node versions. - +- Node >= 7.0: + error.code {string} + error.errno {string|number} any numerical value will be negated -
- - +- Node >= 6.0 < 7.0: + error.code {string} + error.errno {number} negated - - +- Node >= 4.0 < 6.0: introduces SystemError + error.code {string} + error.errno {number} negated - - - - +- Node >= 0.10 < 4.0: + error.code {number} negated + error.errno n/a + - - - - - - - -
NameTypeDescription
options - - -Object - -
@@ -1256,7 +1091,7 @@
Parameters:
Source:
@@ -1277,81 +1112,118 @@
Parameters:
-
Throws:
- -
-
-
- if the options are invalid or could not generate a filename -
-
-
-
-
-
- Type -
-
- -Error -
-
-
-
-
- -
Returns:
-
- the generated random name + + + + + +

setGracefulCleanup()

+ + + + + + +
+ Sets the graceful cleanup.
-
-
- Type -
-
- -string -
-
+ + + + + + + + +
+ + - -

Type Definitions

+ + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + - + -

cleanupCallback(nextopt)

+ +

tmpName(options, callbacknullable)

+
- Removes the temporary created file or directory. + Gets a temporary file name.
@@ -1375,8 +1247,1033 @@
Parameters:
Type - Attributes - + Attributes + + + + + Description + + + + + + + + + options + + + + + +Options +| + +tmpNameCallback + + + + + + + + + + + + + + + + + + options or callback + + + + + + + callback + + + + + +tmpNameCallback + + + + + + + + + + + <nullable>
+ + + + + + + + + the callback function + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

tmpNameSync(options) → {string}

+ + + + + + +
+ Synchronous version of tmpName. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
options + + +Object + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Throws:
+ + + +
+
+
+ if the options are invalid or could not generate a filename +
+
+
+
+
+
+ Type +
+
+ +Error + + +
+
+
+
+
+ + + + + +
Returns:
+ + +
+ the generated random name +
+ + + +
+
+ Type +
+
+ +string + + +
+
+ + + + + + + + + + + +

Type Definitions

+ + + + + + + +

cleanupCallback(nextopt)

+ + + + + + +
+ Removes the temporary created file or directory. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
next + + +simpleCallback + + + + + + <optional>
+ + + + + +
function to call whenever the tmp object needs to be removed
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

cleanupCallbackSync()

+ + + + + + +
+ Removes the temporary created file or directory. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

dirCallback(errnullable, name, fn)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
err + + +Error + + + + + + + + <nullable>
+ + + +
the error object if anything goes wrong
name + + +string + + + + + + + + + + the temporary file name
fn + + +cleanupCallback + + + + + + + + + + the cleanup callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

dirCallbackSync(errnullable, name, fn)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
err + + +Error + + + + + + + + <nullable>
+ + + +
the error object if anything goes wrong
name + + +string + + + + + + + + + + the temporary file name
fn + + +cleanupCallbackSync + + + + + + + + + + the cleanup callback function
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +

DirSyncObject

+ + + + + + +
Type:
+ + + + + + +
Properties:
+ + + + + + + + + + + + + @@ -1389,33 +2286,46 @@
Parameters:
- + - + + + + + + + + + +fileCallback + + + + + - + @@ -1425,8 +2335,6 @@
Parameters:
- -
@@ -1456,7 +2364,7 @@
Parameters:
Source:
@@ -1472,22 +2380,14 @@
Parameters:
- - - - - - - - - - -

dirCallback(errnullable, name, fn)

+ +

fileCallback(errnullable, name, fd, fn)

+ @@ -1592,13 +2492,13 @@
Parameters:
- + + - -
NameType
nextname -simpleCallback +string - - <optional>
- - + + +
the name of the directory
removeCallback + - function to call after entry was removedthe callback function to remove the directory
fnfd -cleanupCallback +number @@ -1616,162 +2516,38 @@
Parameters:
-
the cleanup callback functionthe file descriptor
- - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - - - - - - -

DirSyncObject

- - - - - - -
Type:
- - - - - - -
Properties:
- - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - + + - + @@ -1781,6 +2557,8 @@
Properties:
+ +
@@ -1810,7 +2588,7 @@
Properties:
Source:
@@ -1825,13 +2603,29 @@
Properties:
+ + + + + + + + + + + + + + -

fileCallback(errnullable, name, fd, fn)

+ +

fileCallbackSync(errnullable, name, fd, fn)

+ @@ -1973,7 +2767,7 @@
Parameters:
+ + + + + + + + + + + + + + + + + +
NameTypeDescription
namefn -string +cleanupCallback the name of the directory
removeCallback - + -fileCallback + - - the callback function to remove the directorythe cleanup callback function
-cleanupCallback +cleanupCallbackSync @@ -2032,7 +2826,7 @@
Parameters:
Source:
@@ -2058,6 +2852,10 @@
Parameters:
+ + + +

FileSyncObject

@@ -2208,7 +3006,7 @@
Properties:
Source:
@@ -2462,6 +3260,37 @@
Properties:
unsafeCleanup + + +boolean + + + + + + + + <nullable>
+ +
recursively removes the created temporary directory, even when it's not empty
@@ -2497,7 +3326,7 @@
Properties:
Source:
@@ -2518,7 +3347,9 @@
Properties:
+

simpleCallback()

+ @@ -2569,7 +3400,7 @@

simpleC
Source:
@@ -2602,12 +3433,18 @@

simpleC + + + + +

tmpNameCallback(errnullable, name)

+ @@ -2746,7 +3583,7 @@
Parameters:
Source:
@@ -2772,6 +3609,10 @@
Parameters:
+ + + + @@ -2786,13 +3627,13 @@
Parameters:

diff --git a/docs/index.html b/docs/index.html index cb4de81..70d651f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,94 +43,148 @@

-

Tmp

A simple temporary file and directory creator for node.js.

+

Tmp

+

A simple temporary file and directory creator for node.js.

Build Status Dependencies npm version +API documented Known Vulnerabilities

-

About

This is a widely used library to create temporary files and directories +

About

+

This is a widely used library to create temporary files and directories in a node.js environment.

Tmp offers both an asynchronous and a synchronous API. For all API calls, all -the parameters are optional.

+the parameters are optional. There also exists a promisified version of the +API, see tmp-promise.

Tmp uses crypto for determining random file names, or, when using templates, a six letter random identifier. And just in case that you do not have that much entropy left on your system, Tmp will fall back to pseudo random numbers.

You can set whether you want to remove the temporary file on process exit or -not, and the destination directory can also be set.

-

How to install

npm install tmp

Usage

Asynchronous file creation

Simple temporary file creation, the file will be closed and unlinked on process exit.

+not.

+

If you do not want to store your temporary directories and files in the +standard OS temporary directory, then you are free to override that as well.

+

An Important Note on Compatibility

+

Version 0.1.0

+

Since version 0.1.0, all support for node versions < 0.10.0 has been dropped.

+

Most importantly, any support for earlier versions of node-tmp was also dropped.

+

If you still require node versions < 0.10.0, then you must limit your node-tmp +dependency to versions below 0.1.0.

+

Version 0.0.33

+

Since version 0.0.33, all support for node versions < 0.8 has been dropped.

+

If you still require node version 0.8, then you must limit your node-tmp +dependency to version 0.0.33.

+

For node versions < 0.8 you must limit your node-tmp dependency to +versions < 0.0.33.

+

Node Versions < 8.12.0

+

The SIGINT handler will not work correctly with versions of NodeJS < 8.12.0.

+

Windows

+

Signal handlers for SIGINT will not work. Pressing CTRL-C will leave behind +temporary files and directories.

+

How to install

+
npm install tmp
+
+

Usage

+

Please also check API docs.

+

Asynchronous file creation

+

Simple temporary file creation, the file will be closed and unlinked on process exit.

var tmp = require('tmp');
 
 tmp.file(function _tempFileCreated(err, path, fd, cleanupCallback) {
   if (err) throw err;
 
-  console.log("File: ", path);
-  console.log("Filedescriptor: ", fd);
-
+  console.log('File: ', path);
+  console.log('Filedescriptor: ', fd);
+  
   // If we don't need the file anymore we could manually call the cleanupCallback
   // But that is not necessary if we didn't pass the keep option because the library
   // will clean after itself.
   cleanupCallback();
-});

Synchronous file creation

A synchronous version of the above.

+}); + +

Synchronous file creation

+

A synchronous version of the above.

var tmp = require('tmp');
 
 var tmpobj = tmp.fileSync();
-console.log("File: ", tmpobj.name);
-console.log("Filedescriptor: ", tmpobj.fd);
-
+console.log('File: ', tmpobj.name);
+console.log('Filedescriptor: ', tmpobj.fd);
+  
 // If we don't need the file anymore we could manually call the removeCallback
 // But that is not necessary if we didn't pass the keep option because the library
 // will clean after itself.
-tmpobj.removeCallback();

Note that this might throw an exception if either the maximum limit of retries +tmpobj.removeCallback(); + +

Note that this might throw an exception if either the maximum limit of retries for creating a temporary name fails, or, in case that you do not have the permission to write to the directory where the temporary file should be created in.

-

Asynchronous directory creation

Simple temporary directory creation, it will be removed on process exit.

+

Asynchronous directory creation

+

Simple temporary directory creation, it will be removed on process exit.

If the directory still contains items on process exit, then it won't be removed.

var tmp = require('tmp');
 
 tmp.dir(function _tempDirCreated(err, path, cleanupCallback) {
   if (err) throw err;
 
-  console.log("Dir: ", path);
-
+  console.log('Dir: ', path);
+  
   // Manual cleanup
   cleanupCallback();
-});

If you want to cleanup the directory even when there are entries in it, then +}); + +

If you want to cleanup the directory even when there are entries in it, then you can pass the unsafeCleanup option when creating it.

-

Synchronous directory creation

A synchronous version of the above.

+

Synchronous directory creation

+

A synchronous version of the above.

var tmp = require('tmp');
 
 var tmpobj = tmp.dirSync();
-console.log("Dir: ", tmpobj.name);
+console.log('Dir: ', tmpobj.name);
 // Manual cleanup
-tmpobj.removeCallback();

Note that this might throw an exception if either the maximum limit of retries +tmpobj.removeCallback(); + +

Note that this might throw an exception if either the maximum limit of retries for creating a temporary name fails, or, in case that you do not have the permission to write to the directory where the temporary directory should be created in.

-

Asynchronous filename generation

It is possible with this library to generate a unique filename in the specified +

Asynchronous filename generation

+

It is possible with this library to generate a unique filename in the specified directory.

var tmp = require('tmp');
 
 tmp.tmpName(function _tempNameGenerated(err, path) {
     if (err) throw err;
 
-    console.log("Created temporary filename: ", path);
-});

Synchronous filename generation

A synchronous version of the above.

+ console.log('Created temporary filename: ', path); +}); + +

Synchronous filename generation

+

A synchronous version of the above.

var tmp = require('tmp');
 
 var name = tmp.tmpNameSync();
-console.log("Created temporary filename: ", name);

Advanced usage

Asynchronous file creation

Creates a file with mode 0644, prefix will be prefix- and postfix will be .txt.

+console.log('Created temporary filename: ', name); + +

Advanced usage

+

Asynchronous file creation

+

Creates a file with mode 0644, prefix will be prefix- and postfix will be .txt.

var tmp = require('tmp');
 
 tmp.file({ mode: 0644, prefix: 'prefix-', postfix: '.txt' }, function _tempFileCreated(err, path, fd) {
   if (err) throw err;
 
-  console.log("File: ", path);
-  console.log("Filedescriptor: ", fd);
-});

Synchronous file creation

A synchronous version of the above.

+ console.log('File: ', path); + console.log('Filedescriptor: ', fd); +}); + +

Synchronous file creation

+

A synchronous version of the above.

var tmp = require('tmp');
 
 var tmpobj = tmp.fileSync({ mode: 0644, prefix: 'prefix-', postfix: '.txt' });
-console.log("File: ", tmpobj.name);
-console.log("Filedescriptor: ", tmpobj.fd);

Controlling the Descriptor

As a side effect of creating a unique file tmp gets a file descriptor that is +console.log('File: ', tmpobj.name); +console.log('Filedescriptor: ', tmpobj.fd); + +

Controlling the Descriptor

+

As a side effect of creating a unique file tmp gets a file descriptor that is returned to the user as the fd parameter. The descriptor may be used by the application and is closed when the removeCallback is invoked.

In some use cases the application does not need the descriptor, needs to close it @@ -149,7 +203,9 @@

Asynchronous filename generation

It is possible with this library to if (err) throw err; // fd will be undefined, allowing application to use fs.createReadStream(path) // without holding an unused descriptor open. -});

var tmp = require('tmp');
+});
+
+
var tmp = require('tmp');
 
 tmp.file({ detachDescriptor: true }, function _tempFileCreated(err, path, fd, cleanupCallback) {
   if (err) throw err;
@@ -158,52 +214,87 @@ 

Asynchronous filename generation

It is possible with this library to // Application can store data through fd here; the space used will automatically // be reclaimed by the operating system when the descriptor is closed or program // terminates. -});

Asynchronous directory creation

Creates a directory with mode 0755, prefix will be myTmpDir_.

+}); + +

Asynchronous directory creation

+

Creates a directory with mode 0755, prefix will be myTmpDir_.

var tmp = require('tmp');
 
 tmp.dir({ mode: 0750, prefix: 'myTmpDir_' }, function _tempDirCreated(err, path) {
   if (err) throw err;
 
-  console.log("Dir: ", path);
-});

Synchronous directory creation

Again, a synchronous version of the above.

+ console.log('Dir: ', path); +}); + +

Synchronous directory creation

+

Again, a synchronous version of the above.

var tmp = require('tmp');
 
 var tmpobj = tmp.dirSync({ mode: 0750, prefix: 'myTmpDir_' });
-console.log("Dir: ", tmpobj.name);

mkstemp like, asynchronously

Creates a new temporary directory with mode 0700 and filename like /tmp/tmp-nk2J1u.

+console.log('Dir: ', tmpobj.name); + +

mkstemp like, asynchronously

+

Creates a new temporary directory with mode 0700 and filename like /tmp/tmp-nk2J1u.

+

IMPORTANT NOTE: template no longer accepts a path. Use the dir option instead if you +require tmp to create your temporary filesystem object in a different place than the +default tmp.tmpdir.

var tmp = require('tmp');
 
-tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) {
+tmp.dir({ template: 'tmp-XXXXXX' }, function _tempDirCreated(err, path) {
   if (err) throw err;
 
-  console.log("Dir: ", path);
-});

mkstemp like, synchronously

This will behave similarly to the asynchronous version.

+ console.log('Dir: ', path); +}); + +

mkstemp like, synchronously

+

This will behave similarly to the asynchronous version.

var tmp = require('tmp');
 
-var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
-console.log("Dir: ", tmpobj.name);

Asynchronous filename generation

The tmpName() function accepts the prefix, postfix, dir, etc. parameters also:

+var tmpobj = tmp.dirSync({ template: 'tmp-XXXXXX' }); +console.log('Dir: ', tmpobj.name); + +

Asynchronous filename generation

+

Using tmpName() you can create temporary file names asynchronously. +The function accepts all standard options, e.g. prefix, postfix, dir, and so on.

+

You can also leave out the options altogether and just call the function with a callback as first parameter.

var tmp = require('tmp');
 
-tmp.tmpName({ template: '/tmp/tmp-XXXXXX' }, function _tempNameGenerated(err, path) {
+var options = {};
+
+tmp.tmpName(options, function _tempNameGenerated(err, path) {
     if (err) throw err;
 
-    console.log("Created temporary filename: ", path);
-});

Synchronous filename generation

The tmpNameSync() function works similarly to tmpName().

+ console.log('Created temporary filename: ', path); +}); + +

Synchronous filename generation

+

The tmpNameSync() function works similarly to tmpName(). +Again, you can leave out the options altogether and just invoke the function without any parameters.

var tmp = require('tmp');
-var tmpname = tmp.tmpNameSync({ template: '/tmp/tmp-XXXXXX' });
-console.log("Created temporary filename: ", tmpname);

Graceful cleanup

One may want to cleanup the temporary files even when an uncaught exception +var options = {}; +var tmpname = tmp.tmpNameSync(options); +console.log('Created temporary filename: ', tmpname); + +

Graceful cleanup

+

One may want to cleanup the temporary files even when an uncaught exception occurs. To enforce this, you can call the setGracefulCleanup() method:

var tmp = require('tmp');
 
-tmp.setGracefulCleanup();

Options

All options are optional :)

+tmp.setGracefulCleanup(); + +

Options

+

All options are optional :)

    +
  • name: a fixed name that overrides random name generation
  • mode: the file mode to create with, it fallbacks to 0600 on file creation and 0700 on directory creation
  • prefix: the optional prefix, fallbacks to tmp- if not provided
  • postfix: the optional postfix, fallbacks to .tmp on file creation
  • template: mkstemp like filename template, no default
  • dir: the optional temporary directory, fallbacks to system default (guesses from environment)
  • tries: how many times should the function try to get a unique filename before giving up, default 3
  • -
  • keep: signals that the temporary file or directory should not be deleted on exit, default is false, means delete
      -
    • Please keep in mind that it is recommended in this case to call the provided cleanupCallback function manually.
    • +
    • keep: signals that the temporary file or directory should not be deleted on exit, default is false +
        +
      • In order to clean up, you will have to call the provided cleanupCallback function manually.
    • unsafeCleanup: recursively removes the created temporary directory, even when it's not empty. default is false
    • @@ -218,13 +309,13 @@

      Asynchronous filename generation

      It is possible with this library to


      - Documentation generated by JSDoc 3.4.3 on Sat Nov 26 2016 21:53:28 GMT+0100 (CET) + Documentation generated by JSDoc 3.6.3 on Mon Feb 03 2020 13:41:00 GMT+0100 (GMT+01:00)
      diff --git a/docs/scripts/linenumber.js b/docs/scripts/linenumber.js index 8d52f7e..4354785 100644 --- a/docs/scripts/linenumber.js +++ b/docs/scripts/linenumber.js @@ -1,12 +1,12 @@ /*global document */ -(function() { - var source = document.getElementsByClassName('prettyprint source linenums'); - var i = 0; - var lineNumber = 0; - var lineId; - var lines; - var totalLines; - var anchorHash; +(() => { + const source = document.getElementsByClassName('prettyprint source linenums'); + let i = 0; + let lineNumber = 0; + let lineId; + let lines; + let totalLines; + let anchorHash; if (source && source[0]) { anchorHash = document.location.hash.substring(1); @@ -15,7 +15,7 @@ for (; i < totalLines; i++) { lineNumber++; - lineId = 'line' + lineNumber; + lineId = `line${lineNumber}`; lines[i].id = lineId; if (lineId === anchorHash) { lines[i].className += ' selected'; diff --git a/docs/styles/jsdoc-default.css b/docs/styles/jsdoc-default.css index ede1919..7d1729d 100644 --- a/docs/styles/jsdoc-default.css +++ b/docs/styles/jsdoc-default.css @@ -78,6 +78,10 @@ article dl { margin-bottom: 40px; } +article img { + max-width: 100%; +} + section { display: block; @@ -218,8 +222,8 @@ thead tr th { border-right: 1px solid #aaa; } tr > th:last-child { border-right: 1px solid #ddd; } -.ancestors { color: #999; } -.ancestors a +.ancestors, .attribs { color: #999; } +.ancestors a, .attribs a { color: #999 !important; text-decoration: none; @@ -269,7 +273,7 @@ tr > th:last-child { border-right: 1px solid #ddd; } margin: 0; } -.prettyprint +.source { border: 1px solid #ddd; width: 80%; @@ -280,7 +284,7 @@ tr > th:last-child { border-right: 1px solid #ddd; } width: inherit; } -.prettyprint code +.source code { font-size: 100%; line-height: 18px; diff --git a/docs/tmp.js.html b/docs/tmp.js.html index 7203f53..53ecf67 100644 --- a/docs/tmp.js.html +++ b/docs/tmp.js.html @@ -29,7 +29,7 @@

      Source: tmp.js

      /*!
        * Tmp
        *
      - * Copyright (c) 2011-2015 KARASZI Istvan <github@spam.raszi.hu>
      + * Copyright (c) 2011-2017 KARASZI Istvan <github@spam.raszi.hu>
        *
        * MIT Licensed
        */
      @@ -38,21 +38,18 @@ 

      Source: tmp.js

      * Module dependencies. */ const fs = require('fs'); +const os = require('os'); const path = require('path'); const crypto = require('crypto'); -const osTmpDir = require('os-tmpdir'); -const _c = process.binding('constants'); +const _c = fs.constants && os.constants ? + { fs: fs.constants, os: os.constants } : + process.binding('constants'); +const rimraf = require('rimraf'); /* * The working inner variables. */ const - /** - * The temporary directory. - * @type {string} - */ - tmpDir = osTmpDir(), - // the random characters to choose from RANDOM_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', @@ -68,12 +65,19 @@

      Source: tmp.js

      DIR_MODE = 448 /* 0o700 */, FILE_MODE = 384 /* 0o600 */, + EXIT = 'exit', + + SIGINT = 'SIGINT', + // this will hold the objects need to be removed on exit - _removeObjects = []; + _removeObjects = [], + + // API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback + FN_RMDIR_SYNC = fs.rmdirSync.bind(fs), + FN_RIMRAF_SYNC = rimraf.sync; var - _gracefulCleanup = false, - _uncaughtException = false; + _gracefulCleanup = false; /** * Random name generator based on crypto. @@ -124,13 +128,14 @@

      Source: tmp.js

      * @private */ function _parseArguments(options, callback) { - if (typeof options == 'function') { - var - tmp = options, - options = callback || {}, - callback = tmp; - } else if (typeof options == 'undefined') { - options = {}; + /* istanbul ignore else */ + if (typeof options === 'function') { + return [{}, options]; + } + + /* istanbul ignore else */ + if (_isUndefined(options)) { + return [{}, callback]; } return [options, callback]; @@ -144,21 +149,37 @@

      Source: tmp.js

      * @private */ function _generateTmpName(opts) { - if (opts.name) { + + const tmpDir = _getTmpDir(); + + // fail early on missing tmp dir + if (isBlank(opts.dir) && isBlank(tmpDir)) { + throw new Error('No tmp dir specified'); + } + + /* istanbul ignore else */ + if (!isBlank(opts.name)) { return path.join(opts.dir || tmpDir, opts.name); } // mkstemps like template + // opts.template has already been guarded in tmpName() below + /* istanbul ignore else */ if (opts.template) { - return opts.template.replace(TEMPLATE_PATTERN, _randomChars(6)); + var template = opts.template; + // make sure that we prepend the tmp path if none was given + /* istanbul ignore else */ + if (path.basename(template) === template) + template = path.join(opts.dir || tmpDir, template); + return template.replace(TEMPLATE_PATTERN, _randomChars(6)); } // prefix and postfix const name = [ - opts.prefix || 'tmp-', + (isBlank(opts.prefix) ? 'tmp-' : opts.prefix), process.pid, _randomChars(12), - opts.postfix || '' + (opts.postfix ? opts.postfix : '') ].join(''); return path.join(opts.dir || tmpDir, name); @@ -175,27 +196,35 @@

      Source: tmp.js

      args = _parseArguments(options, callback), opts = args[0], cb = args[1], - tries = opts.tries || DEFAULT_TRIES; + tries = !isBlank(opts.name) ? 1 : opts.tries || DEFAULT_TRIES; + /* istanbul ignore else */ if (isNaN(tries) || tries < 0) return cb(new Error('Invalid tries')); + /* istanbul ignore else */ if (opts.template && !opts.template.match(TEMPLATE_PATTERN)) return cb(new Error('Invalid template provided')); (function _getUniqueName() { - const name = _generateTmpName(opts); + try { + const name = _generateTmpName(opts); - // check whether the path exists then retry if needed - fs.stat(name, function (err) { - if (!err) { - if (tries-- > 0) return _getUniqueName(); + // check whether the path exists then retry if needed + fs.stat(name, function (err) { + /* istanbul ignore else */ + if (!err) { + /* istanbul ignore else */ + if (tries-- > 0) return _getUniqueName(); - return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name)); - } + return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name)); + } - cb(null, name); - }); + cb(null, name); + }); + } catch (err) { + cb(err); + } }()); } @@ -210,11 +239,13 @@

      Source: tmp.js

      var args = _parseArguments(options), opts = args[0], - tries = opts.tries || DEFAULT_TRIES; + tries = !isBlank(opts.name) ? 1 : opts.tries || DEFAULT_TRIES; + /* istanbul ignore else */ if (isNaN(tries) || tries < 0) throw new Error('Invalid tries'); + /* istanbul ignore else */ if (opts.template && !opts.template.match(TEMPLATE_PATTERN)) throw new Error('Invalid template provided'); @@ -242,18 +273,21 @@

      Source: tmp.js

      opts = args[0], cb = args[1]; - opts.postfix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix; - // gets a temporary filename tmpName(opts, function _tmpNameCreated(err, name) { + /* istanbul ignore else */ if (err) return cb(err); // create and open the file fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) { + /* istanbul ignore else */ if (err) return cb(err); + // FIXME overall handling of opts.discardDescriptor is off if (opts.discardDescriptor) { + // FIXME? must not unlink as the user expects the filename to be reserved return fs.close(fd, function _discardCallback(err) { + /* istanbul ignore else */ if (err) { // Low probability, and the file exists, so this could be // ignored. If it isn't we certainly need to unlink the @@ -262,17 +296,19 @@

      Source: tmp.js

      try { fs.unlinkSync(name); } catch (e) { - err = e; + if (!isENOENT(e)) { + err = e; + } } return cb(err); } cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts)); }); + } else { + // FIXME detachDescriptor passes the descriptor whereas discardDescriptor closes it + const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor; + cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false)); } - if (opts.detachDescriptor) { - return cb(null, name, fd, _prepareTmpFileRemoveCallback(name, -1, opts)); - } - cb(null, name, fd, _prepareTmpFileRemoveCallback(name, fd, opts)); }); }); } @@ -289,55 +325,22 @@

      Source: tmp.js

      args = _parseArguments(options), opts = args[0]; - opts.postfix = opts.postfix || '.tmp'; - + const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor; const name = tmpNameSync(opts); - const fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE); + var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE); + /* istanbul ignore else */ + if (opts.discardDescriptor) { + fs.closeSync(fd); + fd = undefined; + } return { name: name, fd: fd, - removeCallback: _prepareTmpFileRemoveCallback(name, fd, opts) + removeCallback: _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, true) }; } -/** - * Removes files and folders in a directory recursively. - * - * @param {string} root - * @private - */ -function _rmdirRecursiveSync(root) { - const dirs = [root]; - - do { - var - dir = dirs.pop(), - deferred = false, - files = fs.readdirSync(dir); - - for (var i = 0, length = files.length; i < length; i++) { - var - file = path.join(dir, files[i]), - stat = fs.lstatSync(file); // lstat so we don't recurse into symlinked directories - - if (stat.isDirectory()) { - if (!deferred) { - deferred = true; - dirs.push(dir); - } - dirs.push(file); - } else { - fs.unlinkSync(file); - } - } - - if (!deferred) { - fs.rmdirSync(dir); - } - } while (dirs.length !== 0); -} - /** * Creates a temporary directory. * @@ -352,13 +355,15 @@

      Source: tmp.js

      // gets a temporary filename tmpName(opts, function _tmpNameCreated(err, name) { + /* istanbul ignore else */ if (err) return cb(err); // create the directory fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) { + /* istanbul ignore else */ if (err) return cb(err); - cb(null, name, _prepareTmpDirRemoveCallback(name, opts)); + cb(null, name, _prepareTmpDirRemoveCallback(name, opts, false)); }); }); } @@ -380,87 +385,138 @@

      Source: tmp.js

      return { name: name, - removeCallback: _prepareTmpDirRemoveCallback(name, opts) + removeCallback: _prepareTmpDirRemoveCallback(name, opts, true) }; } /** - * Prepares the callback for removal of the temporary file. + * Removes files asynchronously. * - * @param {string} name the path of the file - * @param {number} fd file descriptor - * @param {Object} opts - * @returns {fileCallback} + * @param {Object} fdPath + * @param {Function} next + * @private + */ +function _removeFileAsync(fdPath, next) { + const _handler = function (err) { + if (err && !isENOENT(err)) { + // reraise any unanticipated error + return next(err); + } + next(); + }; + + if (0 <= fdPath[0]) + fs.close(fdPath[0], function () { + fs.unlink(fdPath[1], _handler); + }); + else fs.unlink(fdPath[1], _handler); +} + +/** + * Removes files synchronously. + * + * @param {Object} fdPath * @private */ -function _prepareTmpFileRemoveCallback(name, fd, opts) { - const removeCallback = _prepareRemoveCallback(function _removeCallback(fdPath) { +function _removeFileSync(fdPath) { + var rethrownException = null; + try { + if (0 <= fdPath[0]) fs.closeSync(fdPath[0]); + } catch (e) { + // reraise any unanticipated error + if (!isEBADF(e) && !isENOENT(e)) throw e; + } finally { try { - if (0 <= fdPath[0]) { - fs.closeSync(fdPath[0]); - } + fs.unlinkSync(fdPath[1]); } catch (e) { - // under some node/windows related circumstances, a temporary file - // may have not be created as expected or the file was already closed - // by the user, in which case we will simply ignore the error - if (e.errno != -EBADF && e.errno != -ENOENT) { - // reraise any unanticipated error - throw e; - } + // reraise any unanticipated error + if (!isENOENT(e)) rethrownException = e; } - fs.unlinkSync(fdPath[1]); - }, [fd, name]); - - if (!opts.keep) { - _removeObjects.unshift(removeCallback); } + if (rethrownException !== null) { + throw rethrownException; + } +} + +/** + * Prepares the callback for removal of the temporary file. + * + * Returns either a sync callback or a async callback depending on whether + * fileSync or file was called, which is expressed by the sync parameter. + * + * @param {string} name the path of the file + * @param {number} fd file descriptor + * @param {Object} opts + * @param {boolean} sync + * @returns {fileCallback | fileCallbackSync} + * @private + */ +function _prepareTmpFileRemoveCallback(name, fd, opts, sync) { + const removeCallbackSync = _prepareRemoveCallback(_removeFileSync, [fd, name], sync); + const removeCallback = _prepareRemoveCallback(_removeFileAsync, [fd, name], sync, removeCallbackSync); - return removeCallback; + if (!opts.keep) _removeObjects.unshift(removeCallbackSync); + + return sync ? removeCallbackSync : removeCallback; } /** * Prepares the callback for removal of the temporary directory. * + * Returns either a sync callback or a async callback depending on whether + * tmpFileSync or tmpFile was called, which is expressed by the sync parameter. + * * @param {string} name * @param {Object} opts + * @param {boolean} sync * @returns {Function} the callback * @private */ -function _prepareTmpDirRemoveCallback(name, opts) { - const removeFunction = opts.unsafeCleanup ? _rmdirRecursiveSync : fs.rmdirSync.bind(fs); - const removeCallback = _prepareRemoveCallback(removeFunction, name); - - if (!opts.keep) { - _removeObjects.unshift(removeCallback); - } - - return removeCallback; +function _prepareTmpDirRemoveCallback(name, opts, sync) { + const removeFunction = opts.unsafeCleanup ? rimraf : fs.rmdir.bind(fs); + const removeFunctionSync = opts.unsafeCleanup ? FN_RIMRAF_SYNC : FN_RMDIR_SYNC; + const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync); + const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync); + if (!opts.keep) _removeObjects.unshift(removeCallbackSync); + + return sync ? removeCallbackSync : removeCallback; } /** * Creates a guarded function wrapping the removeFunction call. * + * The cleanup callback is save to be called multiple times. + * Subsequent invocations will be ignored. + * * @param {Function} removeFunction - * @param {Object} arg - * @returns {Function} + * @param {string} fileOrDirName + * @param {boolean} sync + * @param {cleanupCallbackSync?} cleanupCallbackSync + * @returns {cleanupCallback | cleanupCallbackSync} * @private */ -function _prepareRemoveCallback(removeFunction, arg) { +function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCallbackSync) { var called = false; + // if sync is true, the next parameter will be ignored return function _cleanupCallback(next) { + + /* istanbul ignore else */ if (!called) { - const index = _removeObjects.indexOf(_cleanupCallback); - if (index >= 0) { - _removeObjects.splice(index, 1); - } + // remove cleanupCallback from cache + const toRemove = cleanupCallbackSync || _cleanupCallback; + const index = _removeObjects.indexOf(toRemove); + /* istanbul ignore else */ + if (index >= 0) _removeObjects.splice(index, 1); called = true; - removeFunction(arg); + if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction == FN_RIMRAF_SYNC) { + return removeFunction(fileOrDirName); + } else { + return removeFunction(fileOrDirName, next || function() {}); + } } - - if (next) next(null); }; } @@ -470,15 +526,15 @@

      Source: tmp.js

      * @private */ function _garbageCollector() { - if (_uncaughtException && !_gracefulCleanup) { - return; - } +console.log(_gracefulCleanup); + /* istanbul ignore else */ + if (!_gracefulCleanup) return; // the function being called removes itself from _removeObjects, // loop until _removeObjects is empty while (_removeObjects.length) { try { - _removeObjects[0].call(null); + _removeObjects[0](); } catch (e) { // already removed? } @@ -486,31 +542,126 @@

      Source: tmp.js

      } /** - * Sets the graceful cleanup. + * Helper for testing against EBADF to compensate changes made to Node 7.x under Windows. + */ +function isEBADF(error) { + return isExpectedError(error, -EBADF, 'EBADF'); +} + +/** + * Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows. + */ +function isENOENT(error) { + return isExpectedError(error, -ENOENT, 'ENOENT'); +} + +/** + * Helper to determine whether the expected error code matches the actual code and errno, + * which will differ between the supported node versions. + * + * - Node >= 7.0: + * error.code {string} + * error.errno {string|number} any numerical value will be negated + * + * - Node >= 6.0 < 7.0: + * error.code {string} + * error.errno {number} negated + * + * - Node >= 4.0 < 6.0: introduces SystemError + * error.code {string} + * error.errno {number} negated * - * Also removes the created files and directories when an uncaught exception occurs. + * - Node >= 0.10 < 4.0: + * error.code {number} negated + * error.errno n/a + */ +function isExpectedError(error, code, errno) { + return error.code === code || error.code === errno; +} + +/** + * Helper which determines whether a string s is blank, that is undefined, or empty or null. + * + * @private + * @param {string} s + * @returns {Boolean} true whether the string s is blank, false otherwise + */ +function isBlank(s) { + return s === null || s === undefined || !s.trim(); +} + +/** + * Sets the graceful cleanup. */ function setGracefulCleanup() { _gracefulCleanup = true; } -const version = process.versions.node.split('.').map(function (value) { - return parseInt(value, 10); -}); +/** + * Returns the currently configured tmp dir from os.tmpdir(). + * + * @private + * @returns {string} the currently configured tmp dir + */ +function _getTmpDir() { + return os.tmpdir(); +} -if (version[0] === 0 && (version[1] < 9 || version[1] === 9 && version[2] < 5)) { - process.addListener('uncaughtException', function _uncaughtExceptionThrown(err) { - _uncaughtException = true; - _garbageCollector(); +/** + * If there are multiple different versions of tmp in place, make sure that + * we recognize the old listeners. + * + * @param {Function} listener + * @private + * @returns {Boolean} true whether listener is a legacy listener + */ +function _is_legacy_listener(listener) { + return (listener.name === '_exit' || listener.name === '_uncaughtExceptionThrown') + && listener.toString().indexOf('_garbageCollector();') > -1; +} - throw err; +/** + * Safely install SIGINT listener. + * + * NOTE: this will only work on OSX and Linux. + * + * @private + */ +function _safely_install_sigint_listener() { + + const listeners = process.listeners(SIGINT); + const existingListeners = []; + for (let i = 0, length = listeners.length; i < length; i++) { + const lstnr = listeners[i]; + /* istanbul ignore else */ + if (lstnr.name === '_tmp$sigint_listener') { + existingListeners.push(lstnr); + process.removeListener(SIGINT, lstnr); + } + } + process.on(SIGINT, function _tmp$sigint_listener(doExit) { + for (let i = 0, length = existingListeners.length; i < length; i++) { + // let the existing listener do the garbage collection (e.g. jest sandbox) + try { + existingListeners[i](false); + } catch (err) { + // ignore + } + } + try { + // force the garbage collector even it is called again in the exit listener + _garbageCollector(); + } finally { + if (!!doExit) { + process.exit(0); + } + } }); } -process.addListener('exit', function _exit(code) { - if (code) _uncaughtException = true; - _garbageCollector(); -}); +// Install process exit listener +//process.addListener(EXIT, _garbageCollector); +//_safely_install_sigint_listener(); /** * Configuration options. @@ -522,6 +673,7 @@

      Source: tmp.js

      * @property {?string} dir the tmp directory to use * @property {?string} prefix prefix for the generated name * @property {?string} postfix postfix for the generated name + * @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty */ /** @@ -551,6 +703,14 @@

      Source: tmp.js

      * @param {cleanupCallback} fn the cleanup callback function */ +/** + * @callback fileCallbackSync + * @param {?Error} err the error object if anything goes wrong + * @param {string} name the temporary file name + * @param {number} fd the file descriptor + * @param {cleanupCallbackSync} fn the cleanup callback function + */ + /** * @callback dirCallback * @param {?Error} err the error object if anything goes wrong @@ -558,11 +718,24 @@

      Source: tmp.js

      * @param {cleanupCallback} fn the cleanup callback function */ +/** + * @callback dirCallbackSync + * @param {?Error} err the error object if anything goes wrong + * @param {string} name the temporary file name + * @param {cleanupCallbackSync} fn the cleanup callback function + */ + /** * Removes the temporary created file or directory. * * @callback cleanupCallback - * @param {simpleCallback} [next] function to call after entry was removed + * @param {simpleCallback} [next] function to call whenever the tmp object needs to be removed + */ + +/** + * Removes the temporary created file or directory. + * + * @callback cleanupCallbackSync */ /** @@ -573,7 +746,16 @@

      Source: tmp.js

      */ // exporting all the needed methods -module.exports.tmpdir = tmpDir; + +// evaluate os.tmpdir() lazily, mainly for simplifying testing but it also will +// allow users to reconfigure the temporary directory +Object.defineProperty(module.exports, 'tmpdir', { + enumerable: true, + configurable: false, + get: function () { + return _getTmpDir(); + } +}); module.exports.dir = dir; module.exports.dirSync = dirSync; @@ -595,13 +777,13 @@

      Source: tmp.js


      - Documentation generated by JSDoc 3.4.3 on Sat Nov 26 2016 21:53:28 GMT+0100 (CET) + Documentation generated by JSDoc 3.6.3 on Mon Feb 03 2020 13:41:00 GMT+0100 (GMT+01:00)
      From 7b77436f2821d9fe3c1502c30eb6f7c042edc454 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 13:42:29 +0100 Subject: [PATCH 02/24] add changelog --- CHANGELOG.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29..65437ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,53 @@ +# CHANGELOG + +## tmp v0.2.0 + +- [#216](https://github.com/raszi/node-tmp/issues/216) + + ***BREAKING CHANGE*** + + SIGINT handler has been removed. + + Users must install their own SIGINT handler and call process.exit() so that tmp's process + exit handler can do the cleanup. + +- [#206](https://github.com/raszi/node-tmp/issues/206) + + document name option. + +- [#197](https://github.com/raszi/node-tmp/issues/197) + + sync cleanup callback must be returned when using the sync API functions. + + fs.rmdirSync() must not be called with a second parameter that is a function. + +- [#179](https://github.com/raszi/node-tmp/issues/179) + + template option no longer accepts arbitrary paths. + +- [#176](https://github.com/raszi/node-tmp/issues/176) + + fail early if no tmp dir was specified. + + use rimraf for removing directory trees. + +- [#175](https://github.com/raszi/node-tmp/issues/175) + + add unsafeCleanup option to jsdoc. + +- drop support for node version < v8 + + ***BREAKING CHANGE*** + + node version < v8 are no longer supported. + +- stabilized tests + +- general clean up + +- update jsdoc + + +## Previous Releases + +- no information available From 19febe21d9a7d20142bb54860d5da04fd5965af9 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 13:44:14 +0100 Subject: [PATCH 03/24] remove SIGINT listener, simplify EXIT listener --- lib/tmp.js | 95 ++---------------------------------------------------- 1 file changed, 3 insertions(+), 92 deletions(-) diff --git a/lib/tmp.js b/lib/tmp.js index 05a65fb..fee55dd 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -39,8 +39,6 @@ const EXIT = 'exit', - SIGINT = 'SIGINT', - // this will hold the objects need to be removed on exit _removeObjects = [], @@ -498,6 +496,7 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall * @private */ function _garbageCollector() { +console.log(_gracefulCleanup); /* istanbul ignore else */ if (!_gracefulCleanup) return; @@ -578,96 +577,8 @@ function _getTmpDir() { return os.tmpdir(); } -/** - * If there are multiple different versions of tmp in place, make sure that - * we recognize the old listeners. - * - * @param {Function} listener - * @private - * @returns {Boolean} true whether listener is a legacy listener - */ -function _is_legacy_listener(listener) { - return (listener.name === '_exit' || listener.name === '_uncaughtExceptionThrown') - && listener.toString().indexOf('_garbageCollector();') > -1; -} - -/** - * Safely install SIGINT listener. - * - * NOTE: this will only work on OSX and Linux. - * - * @private - */ -function _safely_install_sigint_listener() { - - const listeners = process.listeners(SIGINT); - const existingListeners = []; - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - /* istanbul ignore else */ - if (lstnr.name === '_tmp$sigint_listener') { - existingListeners.push(lstnr); - process.removeListener(SIGINT, lstnr); - } - } - process.on(SIGINT, function _tmp$sigint_listener(doExit) { - for (let i = 0, length = existingListeners.length; i < length; i++) { - // let the existing listener do the garbage collection (e.g. jest sandbox) - try { - existingListeners[i](false); - } catch (err) { - // ignore - } - } - try { - // force the garbage collector even it is called again in the exit listener - _garbageCollector(); - } finally { - if (!!doExit) { - process.exit(0); - } - } - }); -} - -/** - * Safely install process exit listener. - * - * @private - */ -function _safely_install_exit_listener() { - const listeners = process.listeners(EXIT); - - // collect any existing listeners - const existingListeners = []; - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - /* istanbul ignore else */ - // TODO: remove support for legacy listeners once release 1.0.0 is out - if (lstnr.name === '_tmp$safe_listener' || _is_legacy_listener(lstnr)) { - // we must forget about the uncaughtException listener, hopefully it is ours - if (lstnr.name !== '_uncaughtExceptionThrown') { - existingListeners.push(lstnr); - } - process.removeListener(EXIT, lstnr); - } - } - // TODO: what was the data parameter good for? - process.addListener(EXIT, function _tmp$safe_listener(data) { - for (let i = 0, length = existingListeners.length; i < length; i++) { - // let the existing listener do the garbage collection (e.g. jest sandbox) - try { - existingListeners[i](data); - } catch (err) { - // ignore - } - } - _garbageCollector(); - }); -} - -_safely_install_exit_listener(); -_safely_install_sigint_listener(); +// Install process exit listener +process.addListener(EXIT, _garbageCollector); /** * Configuration options. From 05d1b4371ab804069eaf7e9cf0d69f4b41c06995 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 13:45:18 +0100 Subject: [PATCH 04/24] update jsdoc --- docs/global.html | 44 ++++++++++++++++++------------------ docs/index.html | 2 +- docs/tmp.js.html | 59 ++---------------------------------------------- 3 files changed, 25 insertions(+), 80 deletions(-) diff --git a/docs/global.html b/docs/global.html index fd5a9e5..3313e88 100644 --- a/docs/global.html +++ b/docs/global.html @@ -251,7 +251,7 @@
      Parameters:
      Source:
      @@ -388,7 +388,7 @@
      Parameters:
      Source:
      @@ -622,7 +622,7 @@
      Parameters:
      Source:
      @@ -759,7 +759,7 @@
      Parameters:
      Source:
      @@ -898,7 +898,7 @@

      isEBADFSource:
      @@ -986,7 +986,7 @@

      isENOENTSource:
      @@ -1091,7 +1091,7 @@

      isExpe
      Source:
      @@ -1179,7 +1179,7 @@

      set
      Source:
      @@ -1362,7 +1362,7 @@

      Parameters:
      Source:
      @@ -1499,7 +1499,7 @@
      Parameters:
      Source:
      @@ -1705,7 +1705,7 @@
      Parameters:
      Source:
      @@ -1793,7 +1793,7 @@

      cl
      Source:
      @@ -2000,7 +2000,7 @@

      Parameters:
      Source:
      @@ -2207,7 +2207,7 @@
      Parameters:
      Source:
      @@ -2364,7 +2364,7 @@
      Properties:
      Source:
      @@ -2588,7 +2588,7 @@
      Parameters:
      Source:
      @@ -2826,7 +2826,7 @@
      Parameters:
      Source:
      @@ -3006,7 +3006,7 @@
      Properties:
      Source:
      @@ -3326,7 +3326,7 @@
      Properties:
      Source:
      @@ -3400,7 +3400,7 @@

      simpleC
      Source:
      @@ -3583,7 +3583,7 @@

      Parameters:
      Source:
      @@ -3633,7 +3633,7 @@

      Home

      Global

      • diff --git a/docs/index.html b/docs/index.html index 70d651f..1a4fdd7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -315,7 +315,7 @@

        Home

        Global

        • diff --git a/docs/tmp.js.html b/docs/tmp.js.html index 53ecf67..5e6b4a5 100644 --- a/docs/tmp.js.html +++ b/docs/tmp.js.html @@ -67,8 +67,6 @@

          Source: tmp.js

          EXIT = 'exit', - SIGINT = 'SIGINT', - // this will hold the objects need to be removed on exit _removeObjects = [], @@ -607,61 +605,8 @@

          Source: tmp.js

          return os.tmpdir(); } -/** - * If there are multiple different versions of tmp in place, make sure that - * we recognize the old listeners. - * - * @param {Function} listener - * @private - * @returns {Boolean} true whether listener is a legacy listener - */ -function _is_legacy_listener(listener) { - return (listener.name === '_exit' || listener.name === '_uncaughtExceptionThrown') - && listener.toString().indexOf('_garbageCollector();') > -1; -} - -/** - * Safely install SIGINT listener. - * - * NOTE: this will only work on OSX and Linux. - * - * @private - */ -function _safely_install_sigint_listener() { - - const listeners = process.listeners(SIGINT); - const existingListeners = []; - for (let i = 0, length = listeners.length; i < length; i++) { - const lstnr = listeners[i]; - /* istanbul ignore else */ - if (lstnr.name === '_tmp$sigint_listener') { - existingListeners.push(lstnr); - process.removeListener(SIGINT, lstnr); - } - } - process.on(SIGINT, function _tmp$sigint_listener(doExit) { - for (let i = 0, length = existingListeners.length; i < length; i++) { - // let the existing listener do the garbage collection (e.g. jest sandbox) - try { - existingListeners[i](false); - } catch (err) { - // ignore - } - } - try { - // force the garbage collector even it is called again in the exit listener - _garbageCollector(); - } finally { - if (!!doExit) { - process.exit(0); - } - } - }); -} - // Install process exit listener -//process.addListener(EXIT, _garbageCollector); -//_safely_install_sigint_listener(); +process.addListener(EXIT, _garbageCollector); /** * Configuration options. @@ -783,7 +728,7 @@

          Home

          Global

          • From 78c72efbea53593301a5a61257a873dcc71bafcb Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 14:14:27 +0100 Subject: [PATCH 05/24] fix tests, remove obsolete tests, add missing SIGINT handlers --- lib/tmp.js | 1 - test/issue129-sigint-test.js | 27 ----------- test/issue129-test.js | 30 ------------ test/mocha.opts | 1 + test/outband/issue121.js | 4 ++ test/outband/issue129-sigint.js | 32 ------------- test/outband/issue129-sigint.json | 3 -- test/outband/issue129.js | 76 ------------------------------- test/outband/issue129.json | 3 -- test/setup-sigint-listener.js | 2 + test/spawn-generic.js | 4 ++ 11 files changed, 11 insertions(+), 172 deletions(-) delete mode 100644 test/issue129-sigint-test.js delete mode 100644 test/issue129-test.js create mode 100644 test/mocha.opts delete mode 100644 test/outband/issue129-sigint.js delete mode 100644 test/outband/issue129-sigint.json delete mode 100644 test/outband/issue129.js delete mode 100644 test/outband/issue129.json create mode 100644 test/setup-sigint-listener.js diff --git a/lib/tmp.js b/lib/tmp.js index fee55dd..223d692 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -496,7 +496,6 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall * @private */ function _garbageCollector() { -console.log(_gracefulCleanup); /* istanbul ignore else */ if (!_gracefulCleanup) return; diff --git a/test/issue129-sigint-test.js b/test/issue129-sigint-test.js deleted file mode 100644 index 30880d9..0000000 --- a/test/issue129-sigint-test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable no-octal */ -// vim: expandtab:ts=2:sw=2 - -var - assert = require('assert'), - assertions = require('./assertions'), - childProcess = require('./child-process').childProcess; - -describe('tmp', function () { - describe('issue129: safely install sigint listeners', function () { - it('when simulating sandboxed behavior', function (done) { - childProcess(this, 'issue129-sigint.json', function (err, stderr, stdout) { - if (err) return done(err); - if (stderr) { - assertions.assertDoesNotStartWith(stderr, 'EEXISTS:MULTIPLE'); - assertions.assertDoesNotStartWith(stderr, 'ENOAVAIL:'); - return done(); - } - if (stdout) { - assert.equal(stdout, 'EOK'); - return done(); - } - done(new Error('existing listener has not been called')); - }); - }); - }); -}); diff --git a/test/issue129-test.js b/test/issue129-test.js deleted file mode 100644 index 250483a..0000000 --- a/test/issue129-test.js +++ /dev/null @@ -1,30 +0,0 @@ -/* eslint-disable no-octal */ -// vim: expandtab:ts=2:sw=2 - -var - assert = require('assert'), - assertions = require('./assertions'), - childProcess = require('./child-process').childProcess; - -describe('tmp', function () { - describe('issue129: safely install listeners', function () { - it('when simulating sandboxed behavior', function (done) { - childProcess(this, 'issue129.json', function (err, stderr, stdout) { - if (err) return done(err); - if (!stdout && !stderr) return done(new Error('stderr or stdout expected')); - if (stderr) { - assertions.assertDoesNotStartWith(stderr, 'EEXISTS:LEGACY:EXIT'); - assertions.assertDoesNotStartWith(stderr, 'EEXISTS:LEGACY:UNCAUGHT'); - assertions.assertDoesNotStartWith(stderr, 'EEXISTS:NEWSTYLE'); - assertions.assertDoesNotStartWith(stderr, 'ENOAVAIL:LEGACY:EXIT'); - assertions.assertDoesNotStartWith(stderr, 'EAVAIL:LEGACY:UNCAUGHT'); - assertions.assertDoesNotStartWith(stderr, 'ENOAVAIL:NEWSTYLE'); - } - if (stdout) { - assert.equal(stdout, 'EOK', 'existing listeners should have been removed and called'); - } - done(); - }); - }); - }); -}); diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..671ed4a --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +--file ./test/setup-sigint-listener.js \ No newline at end of file diff --git a/test/outband/issue121.js b/test/outband/issue121.js index fa2b706..87f8c28 100644 --- a/test/outband/issue121.js +++ b/test/outband/issue121.js @@ -2,6 +2,10 @@ const tmp = require('../../lib/tmp'); +process.on('SIGINT', function () { + process.exit(0); +}); + process.on('SIGTERM', function () { process.exit(0); }); diff --git a/test/outband/issue129-sigint.js b/test/outband/issue129-sigint.js deleted file mode 100644 index 50a0051..0000000 --- a/test/outband/issue129-sigint.js +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable no-octal */ -// vim: expandtab:ts=2:sw=2 - -// addendum to https://github.com/raszi/node-tmp/issues/129 so that with jest sandboxing we do not install our sigint -// listener multiple times -module.exports = function () { - - var self = this; - - // simulate an existing SIGINT listener - process.addListener('SIGINT', function _tmp$sigint_listener() { - self.out('EOK'); - }); - - // now let tmp install its listener safely - require('../../lib/tmp'); - - var sigintListeners = []; - - var listeners = process.listeners('SIGINT'); - for (var i = 0; i < listeners.length; i++) { - var listener = listeners[i]; - if (listener.name === '_tmp$sigint_listener') { - sigintListeners.push(listener); - } - } - - if (sigintListeners.length > 1) this.fail('EEXISTS:MULTIPLE: existing SIGINT listener was not removed', this.exit); - if (sigintListeners.length != 1) this.fail('ENOAVAIL: no SIGINT listener was installed', this.exit); - // tmp will now exit the process as there are no custom user listeners installed - sigintListeners[0](); -}; diff --git a/test/outband/issue129-sigint.json b/test/outband/issue129-sigint.json deleted file mode 100644 index 967ff91..0000000 --- a/test/outband/issue129-sigint.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "tc": "issue129-sigint" -} diff --git a/test/outband/issue129.js b/test/outband/issue129.js deleted file mode 100644 index 311704e..0000000 --- a/test/outband/issue129.js +++ /dev/null @@ -1,76 +0,0 @@ -/* eslint-disable no-octal */ -// vim: expandtab:ts=2:sw=2 - -// https://github.com/raszi/node-tmp/issues/129 -module.exports = function () { - // dup from lib/tmp.js - function _is_legacy_listener(listener) { - return (listener.name === '_exit' || listener.name === '_uncaughtExceptionThrown') - && listener.toString().indexOf('_garbageCollector();') !== -1; - } - - function _garbageCollector() {} - - var callState = { - newStyleListener : false, - legacyExitListener : false, - legacyUncaughtListener : false - }; - - // simulate the new exit listener - var listener1 = (function (callState) { - return function _tmp$safe_listener() { - _garbageCollector(); - callState.newStyleListener = true; - }; - })(callState); - - // simulate the legacy _exit listener - var listener2 = (function (callState) { - return function _exit() { - _garbageCollector(); - callState.legacyExitListener = true; - }; - })(callState); - - // simulate the legacy _uncaughtExceptionThrown listener - var listener3 = (function (callState) { - return function _uncaughtExceptionThrown() { - _garbageCollector(); - callState.legacyUncaughtListener = true; - }; - })(callState); - - process.addListener('exit', listener1); - process.addListener('exit', listener2); - process.addListener('exit', listener3); - - // now let tmp install its listener safely - require('../../lib/tmp'); - - var legacyExitListener = null; - var legacyUncaughtListener = null; - var newStyleListeners = []; - - var listeners = process.listeners('exit'); - for (var i = 0; i < listeners.length; i++) { - var listener = listeners[i]; - // the order here is important - if (listener.name === '_tmp$safe_listener') { - newStyleListeners.push(listener); - } - else if (_is_legacy_listener(listener)) { - if (listener.name === '_uncaughtExceptionThrown') legacyUncaughtListener = listener; - else legacyExitListener = listener; - } - } - - if (legacyExitListener) this.fail('EEXISTS:LEGACY:EXIT existing legacy exit listener was not removed', this.exit); - if (legacyUncaughtListener) this.fail('EEXISTS:LEGACY:UNCAUGHT existing legacy uncaught exception thrown listener was not removed', this.exit); - if (newStyleListeners.length > 1) this.fail('EEXISTS:NEWSTYLE: existing new style listener was not removed', this.exit); - newStyleListeners[0](); - if (!callState.legacyExitListener) this.fail('ENOAVAIL:LEGACY:EXIT existing legacy exit listener was not called', this.exit); - if (callState.legacyUncaughtListener) this.fail('EAVAIL:LEGACY:UNCAUGHT existing legacy uncaught exception thrown listener should not have been called', this.exit); - if (!callState.newStyleListener) this.fail('ENOAVAIL:NEWSTYLE: existing new style listener was not called', this.exit); - this.out('EOK', this.exit); -}; diff --git a/test/outband/issue129.json b/test/outband/issue129.json deleted file mode 100644 index 3d7794c..0000000 --- a/test/outband/issue129.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "tc": "issue129" -} diff --git a/test/setup-sigint-listener.js b/test/setup-sigint-listener.js new file mode 100644 index 0000000..5d8a228 --- /dev/null +++ b/test/setup-sigint-listener.js @@ -0,0 +1,2 @@ +// make sure that all garbage gets collected on SIGINT +process.on('SIGINT', process.exit); diff --git a/test/spawn-generic.js b/test/spawn-generic.js index d557db0..f433f6d 100644 --- a/test/spawn-generic.js +++ b/test/spawn-generic.js @@ -14,6 +14,10 @@ var fnUnderTest = null; if (config.async) fnUnderTest = (config.file) ? tmp.file : tmp.dir; else fnUnderTest = (config.file) ? tmp.fileSync : tmp.dirSync; +// make sure that we have a SIGINT handler so CTRL-C the test suite +// will not leave anything behing +process.on('SIGINT', process.exit); + // do we test against tmp doing a graceful cleanup? if (config.graceful) tmp.setGracefulCleanup(); From f3005fced86226fc2995379f2c6029362293573d Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 14:17:18 +0100 Subject: [PATCH 06/24] update jsdoc --- docs/global.html | 32 ++++++++++++++++---------------- docs/index.html | 2 +- docs/tmp.js.html | 3 +-- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/docs/global.html b/docs/global.html index 3313e88..89a275e 100644 --- a/docs/global.html +++ b/docs/global.html @@ -898,7 +898,7 @@

            isEBADFSource:
            @@ -986,7 +986,7 @@

            isENOENTSource:
            @@ -1091,7 +1091,7 @@

            isExpe
            Source:
            @@ -1179,7 +1179,7 @@

            set
            Source:
            @@ -1705,7 +1705,7 @@

            Parameters:
            Source:
            @@ -1793,7 +1793,7 @@

            cl
            Source:
            @@ -2000,7 +2000,7 @@

            Parameters:
            Source:
            @@ -2207,7 +2207,7 @@
            Parameters:
            Source:
            @@ -2364,7 +2364,7 @@
            Properties:
            Source:
            @@ -2588,7 +2588,7 @@
            Parameters:
            Source:
            @@ -2826,7 +2826,7 @@
            Parameters:
            Source:
            @@ -3006,7 +3006,7 @@
            Properties:
            Source:
            @@ -3326,7 +3326,7 @@
            Properties:
            Source:
            @@ -3400,7 +3400,7 @@

            simpleC
            Source:
            @@ -3583,7 +3583,7 @@

            Parameters:
            Source:
            @@ -3633,7 +3633,7 @@

            Home

            Global

            • diff --git a/docs/index.html b/docs/index.html index 1a4fdd7..d98e903 100644 --- a/docs/index.html +++ b/docs/index.html @@ -315,7 +315,7 @@

              Home

              Global

              • diff --git a/docs/tmp.js.html b/docs/tmp.js.html index 5e6b4a5..f3cafcb 100644 --- a/docs/tmp.js.html +++ b/docs/tmp.js.html @@ -524,7 +524,6 @@

                Source: tmp.js

                * @private */ function _garbageCollector() { -console.log(_gracefulCleanup); /* istanbul ignore else */ if (!_gracefulCleanup) return; @@ -728,7 +727,7 @@

                Home

                Global

                • From 327eb83b61f3149f8d07b88d7b4cc55b1c469928 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 14:20:09 +0100 Subject: [PATCH 07/24] include node v13 in builds --- .travis.yml | 1 + appveyor.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0f4c1ae..c564cc7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: - "node" + - "13" - "12" - "11" - "10" diff --git a/appveyor.yml b/appveyor.yml index c0d80e6..861edaa 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,6 +7,7 @@ environment: - nodejs_version: "10" - nodejs_version: "11" - nodejs_version: "12" + - nodejs_version: "13" install: - ps: Install-Product node $env:nodejs_version From bcb43a0b9109b053334d4a59b86dcc8fed1dbd4b Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 14:41:48 +0100 Subject: [PATCH 08/24] update documentation --- CHANGELOG.md | 6 ++++++ README.md | 2 ++ 2 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65437ef..c8793d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ Users must install their own SIGINT handler and call process.exit() so that tmp's process exit handler can do the cleanup. + + The simplest handler possible would be + + ``` + process.on('SIGINT', process.exit); + ``` - [#206](https://github.com/raszi/node-tmp/issues/206) diff --git a/README.md b/README.md index 5e3aeb4..e5ca66f 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ standard OS temporary directory, then you are free to override that as well. ## An Important Note on Compatibility +See the [CHANGELOG](./CHANGELOG.md) for more information. + ### Version 0.1.0 Since version 0.1.0, all support for node versions < 0.10.0 has been dropped. From 5195a26b3ee54607a40b09953fd1f0d8b6575a11 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 14:44:30 +0100 Subject: [PATCH 09/24] add contributors to package.json --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 1b8e448..c39d3f1 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "0.1.0", "description": "Temporary file and directory creator", "author": "KARASZI István (http://raszi.hu/)", + "contributors": [ + "Carsten Klein (https://github.com/silkentrance)" + ], "keywords": [ "temporary", "tmp", From b63cb5879ad557ea4856d96c604d74afa6acd6d4 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 15:27:43 +0100 Subject: [PATCH 10/24] fix #213 tmp.file must not unlink file when discarding the file descriptor, similar to tmp.fileSync --- lib/tmp.js | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/lib/tmp.js b/lib/tmp.js index 223d692..aa5d6ca 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -253,29 +253,14 @@ function file(options, callback) { /* istanbul ignore else */ if (err) return cb(err); - // FIXME overall handling of opts.discardDescriptor is off if (opts.discardDescriptor) { - // FIXME? must not unlink as the user expects the filename to be reserved - return fs.close(fd, function _discardCallback(err) { - /* istanbul ignore else */ - if (err) { - // Low probability, and the file exists, so this could be - // ignored. If it isn't we certainly need to unlink the - // file, and if that fails too its error is more - // important. - try { - fs.unlinkSync(name); - } catch (e) { - if (!isENOENT(e)) { - err = e; - } - } - return cb(err); - } - cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts)); + return fs.close(fd, function _discardCallback(possibleErr) { + // the chance of getting an error on close here is rather low and might occur in the most edgiest cases only + return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false)); }); } else { - // FIXME detachDescriptor passes the descriptor whereas discardDescriptor closes it + // detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care + // about the descriptor const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor; cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false)); } From 1746331df649527b765d9dfefc13b88fd23e410c Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Mon, 3 Feb 2020 15:28:11 +0100 Subject: [PATCH 11/24] update jsdoc --- docs/global.html | 38 +++++++++++++++++++------------------- docs/index.html | 3 ++- docs/tmp.js.html | 27 ++++++--------------------- 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/docs/global.html b/docs/global.html index 89a275e..a80e249 100644 --- a/docs/global.html +++ b/docs/global.html @@ -251,7 +251,7 @@
                  Parameters:
                  Source:
                  @@ -388,7 +388,7 @@
                  Parameters:
                  Source:
                  @@ -759,7 +759,7 @@
                  Parameters:
                  Source:
                  @@ -898,7 +898,7 @@

                  isEBADFSource:
                  @@ -986,7 +986,7 @@

                  isENOENTSource:
                  @@ -1091,7 +1091,7 @@

                  isExpe
                  Source:
                  @@ -1179,7 +1179,7 @@

                  set
                  Source:
                  @@ -1705,7 +1705,7 @@

                  Parameters:
                  Source:
                  @@ -1793,7 +1793,7 @@

                  cl
                  Source:
                  @@ -2000,7 +2000,7 @@

                  Parameters:
                  Source:
                  @@ -2207,7 +2207,7 @@
                  Parameters:
                  Source:
                  @@ -2364,7 +2364,7 @@
                  Properties:
                  Source:
                  @@ -2588,7 +2588,7 @@
                  Parameters:
                  Source:
                  @@ -2826,7 +2826,7 @@
                  Parameters:
                  Source:
                  @@ -3006,7 +3006,7 @@
                  Properties:
                  Source:
                  @@ -3326,7 +3326,7 @@
                  Properties:
                  Source:
                  @@ -3400,7 +3400,7 @@

                  simpleC
                  Source:
                  @@ -3583,7 +3583,7 @@

                  Parameters:
                  Source:
                  @@ -3633,7 +3633,7 @@

                  Home

                  Global

                  • diff --git a/docs/index.html b/docs/index.html index d98e903..bcdb886 100644 --- a/docs/index.html +++ b/docs/index.html @@ -64,6 +64,7 @@

                    About

                    If you do not want to store your temporary directories and files in the standard OS temporary directory, then you are free to override that as well.

                    An Important Note on Compatibility

                    +

                    See the CHANGELOG for more information.

                    Version 0.1.0

                    Since version 0.1.0, all support for node versions < 0.10.0 has been dropped.

                    Most importantly, any support for earlier versions of node-tmp was also dropped.

                    @@ -315,7 +316,7 @@

                    Home

                    Global

                    • diff --git a/docs/tmp.js.html b/docs/tmp.js.html index f3cafcb..fb9d4eb 100644 --- a/docs/tmp.js.html +++ b/docs/tmp.js.html @@ -281,29 +281,14 @@

                      Source: tmp.js

                      /* istanbul ignore else */ if (err) return cb(err); - // FIXME overall handling of opts.discardDescriptor is off if (opts.discardDescriptor) { - // FIXME? must not unlink as the user expects the filename to be reserved - return fs.close(fd, function _discardCallback(err) { - /* istanbul ignore else */ - if (err) { - // Low probability, and the file exists, so this could be - // ignored. If it isn't we certainly need to unlink the - // file, and if that fails too its error is more - // important. - try { - fs.unlinkSync(name); - } catch (e) { - if (!isENOENT(e)) { - err = e; - } - } - return cb(err); - } - cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts)); + return fs.close(fd, function _discardCallback(possibleErr) { + // the chance of getting an error on close here is rather low and might occur in the most edgiest cases only + return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false)); }); } else { - // FIXME detachDescriptor passes the descriptor whereas discardDescriptor closes it + // detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care + // about the descriptor const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor; cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false)); } @@ -727,7 +712,7 @@

                      Home

                      Global

                      • From 9500e10f83b0158ec83b2b3cc140ac0438d89741 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Wed, 5 Feb 2020 00:38:28 +0100 Subject: [PATCH 12/24] update jsdoc --- docs/global.html | 93 +++++++++++++++++++++++++++++++++--------------- docs/index.html | 2 +- docs/tmp.js.html | 31 +++++++++++----- lib/tmp.js | 17 ++++----- 4 files changed, 97 insertions(+), 46 deletions(-) diff --git a/docs/global.html b/docs/global.html index a80e249..083a024 100644 --- a/docs/global.html +++ b/docs/global.html @@ -251,7 +251,7 @@
                        Parameters:
                        Source:
                        @@ -388,7 +388,7 @@
                        Parameters:
                        Source:
                        @@ -530,6 +530,12 @@
                        Parameters:
                        Options | +null +| + +undefined +| + fileCallback @@ -548,7 +554,7 @@
                        Parameters:
                        - the config options or the callback function + the config options or the callback function or null or undefined @@ -622,7 +628,7 @@
                        Parameters:
                        Source:
                        @@ -759,7 +765,7 @@
                        Parameters:
                        Source:
                        @@ -898,7 +904,7 @@

                        isEBADFSource:
                        @@ -986,7 +992,7 @@

                        isENOENTSource:
                        @@ -1091,7 +1097,7 @@

                        isExpe
                        Source:
                        @@ -1179,7 +1185,7 @@

                        set
                        Source:
                        @@ -1362,7 +1368,7 @@

                        Parameters:
                        Source:
                        @@ -1499,7 +1505,7 @@
                        Parameters:
                        Source:
                        @@ -1705,7 +1711,7 @@
                        Parameters:
                        Source:
                        @@ -1793,7 +1799,7 @@

                        cl
                        Source:
                        @@ -2000,7 +2006,7 @@

                        Parameters:
                        Source:
                        @@ -2207,7 +2213,7 @@
                        Parameters:
                        Source:
                        @@ -2364,7 +2370,7 @@
                        Properties:
                        Source:
                        @@ -2516,7 +2522,7 @@
                        Parameters:
                        - the file descriptor + the file descriptor or -1 if the fd had been discarded @@ -2588,7 +2594,7 @@
                        Parameters:
                        Source:
                        @@ -2754,7 +2760,7 @@
                        Parameters:
                        - the file descriptor + the file descriptor or -1 if the fd had been discarded @@ -2826,7 +2832,7 @@
                        Parameters:
                        Source:
                        @@ -2944,7 +2950,7 @@
                        Properties:
                        - the file descriptor + the file descriptor or -1 if the fd has been discarded @@ -3006,7 +3012,7 @@
                        Properties:
                        Source:
                        @@ -3075,6 +3081,37 @@
                        Properties:
                        + + + keep + + + + + +boolean + + + + + + + + + + + <nullable>
                        + + + + + + + the temporary object (file or dir) will not be garbage collected + + + + tries @@ -3163,7 +3200,7 @@
                        Properties:
                        - fix name + fixed name relative to tmpdir or the specified dir option @@ -3194,7 +3231,7 @@
                        Properties:
                        - the tmp directory to use + tmp directory relative to the system tmp directory to use @@ -3326,7 +3363,7 @@
                        Properties:
                        Source:
                        @@ -3400,7 +3437,7 @@

                        simpleC
                        Source:
                        @@ -3583,7 +3620,7 @@

                        Parameters:
                        Source:
                        @@ -3633,7 +3670,7 @@

                        Home

                        Global

                        • diff --git a/docs/index.html b/docs/index.html index bcdb886..84f3445 100644 --- a/docs/index.html +++ b/docs/index.html @@ -316,7 +316,7 @@

                          Home

                          Global

                          • diff --git a/docs/tmp.js.html b/docs/tmp.js.html index fb9d4eb..6aa262f 100644 --- a/docs/tmp.js.html +++ b/docs/tmp.js.html @@ -120,8 +120,8 @@

                            Source: tmp.js

                            * * This function helps to have optional arguments. * - * @param {(Options|Function)} options - * @param {Function} callback + * @param {(Options|null|undefined|Function)} options + * @param {?Function} callback * @returns {Array} parsed arguments * @private */ @@ -139,6 +139,18 @@

                            Source: tmp.js

                            return [options, callback]; } +/** + * Sanitizes the specified options, eventually filling it with reasonable defaults, overriding user provided, + * insane default. + * + * @param {Options} options + * @returns {Options} the sanitized options + * @private + */ +function _sanitizeOptions(options) { + +} + /** * Generates a new temporary name. * @@ -262,7 +274,7 @@

                            Source: tmp.js

                            /** * Creates and opens a temporary file. * - * @param {(Options|fileCallback)} options the config options or the callback function + * @param {(Options|null|undefined|fileCallback)} options the config options or the callback function or null or undefined * @param {?fileCallback} callback */ function file(options, callback) { @@ -596,10 +608,11 @@

                            Source: tmp.js

                            * Configuration options. * * @typedef {Object} Options + * @property {?boolean} keep the temporary object (file or dir) will not be garbage collected * @property {?number} tries the number of tries before give up the name generation * @property {?string} template the "mkstemp" like filename template - * @property {?string} name fix name - * @property {?string} dir the tmp directory to use + * @property {?string} name fixed name relative to tmpdir or the specified dir option + * @property {?string} dir tmp directory relative to the system tmp directory to use * @property {?string} prefix prefix for the generated name * @property {?string} postfix postfix for the generated name * @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty @@ -608,7 +621,7 @@

                            Source: tmp.js

                            /** * @typedef {Object} FileSyncObject * @property {string} name the name of the file - * @property {string} fd the file descriptor + * @property {string} fd the file descriptor or -1 if the fd has been discarded * @property {fileCallback} removeCallback the callback function to remove the file */ @@ -628,7 +641,7 @@

                            Source: tmp.js

                            * @callback fileCallback * @param {?Error} err the error object if anything goes wrong * @param {string} name the temporary file name - * @param {number} fd the file descriptor + * @param {number} fd the file descriptor or -1 if the fd had been discarded * @param {cleanupCallback} fn the cleanup callback function */ @@ -636,7 +649,7 @@

                            Source: tmp.js

                            * @callback fileCallbackSync * @param {?Error} err the error object if anything goes wrong * @param {string} name the temporary file name - * @param {number} fd the file descriptor + * @param {number} fd the file descriptor or -1 if the fd had been discarded * @param {cleanupCallbackSync} fn the cleanup callback function */ @@ -712,7 +725,7 @@

                            Home

                            Global

                            • diff --git a/lib/tmp.js b/lib/tmp.js index aa5d6ca..bb7ae16 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -92,8 +92,8 @@ function _isUndefined(obj) { * * This function helps to have optional arguments. * - * @param {(Options|Function)} options - * @param {Function} callback + * @param {(Options|null|undefined|Function)} options + * @param {?Function} callback * @returns {Array} parsed arguments * @private */ @@ -234,7 +234,7 @@ function tmpNameSync(options) { /** * Creates and opens a temporary file. * - * @param {(Options|fileCallback)} options the config options or the callback function + * @param {(Options|null|undefined|fileCallback)} options the config options or the callback function or null or undefined * @param {?fileCallback} callback */ function file(options, callback) { @@ -568,10 +568,11 @@ process.addListener(EXIT, _garbageCollector); * Configuration options. * * @typedef {Object} Options + * @property {?boolean} keep the temporary object (file or dir) will not be garbage collected * @property {?number} tries the number of tries before give up the name generation * @property {?string} template the "mkstemp" like filename template - * @property {?string} name fix name - * @property {?string} dir the tmp directory to use + * @property {?string} name fixed name relative to tmpdir or the specified dir option + * @property {?string} dir tmp directory relative to the system tmp directory to use * @property {?string} prefix prefix for the generated name * @property {?string} postfix postfix for the generated name * @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty @@ -580,7 +581,7 @@ process.addListener(EXIT, _garbageCollector); /** * @typedef {Object} FileSyncObject * @property {string} name the name of the file - * @property {string} fd the file descriptor + * @property {string} fd the file descriptor or -1 if the fd has been discarded * @property {fileCallback} removeCallback the callback function to remove the file */ @@ -600,7 +601,7 @@ process.addListener(EXIT, _garbageCollector); * @callback fileCallback * @param {?Error} err the error object if anything goes wrong * @param {string} name the temporary file name - * @param {number} fd the file descriptor + * @param {number} fd the file descriptor or -1 if the fd had been discarded * @param {cleanupCallback} fn the cleanup callback function */ @@ -608,7 +609,7 @@ process.addListener(EXIT, _garbageCollector); * @callback fileCallbackSync * @param {?Error} err the error object if anything goes wrong * @param {string} name the temporary file name - * @param {number} fd the file descriptor + * @param {number} fd the file descriptor or -1 if the fd had been discarded * @param {cleanupCallbackSync} fn the cleanup callback function */ From 7ee5bdd590a0351c3db7d18fc82968bfe11fe7d9 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Fri, 7 Feb 2020 23:17:03 +0100 Subject: [PATCH 13/24] fix #156 #207 #218 #176 #236 #237 #238 --- CHANGELOG.md | 76 +++++++++++--- README.md | 21 ++-- docs/global.html | 130 ++++++++++++++++++++---- docs/index.html | 23 +++-- docs/tmp.js.html | 201 ++++++++++++++++++++++++------------- lib/tmp.js | 187 ++++++++++++++++++++++++---------- test/name-sync-test.js | 69 ++----------- test/name-test.js | 79 ++------------- test/template-sync-test.js | 4 +- test/template-test.js | 8 +- 10 files changed, 491 insertions(+), 307 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8793d3..496a938 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## tmp v0.2.0 +- drop support for node version < v8 + + ***BREAKING CHANGE*** + + node version < v8 are no longer supported. + - [#216](https://github.com/raszi/node-tmp/issues/216) ***BREAKING CHANGE*** @@ -11,46 +17,86 @@ Users must install their own SIGINT handler and call process.exit() so that tmp's process exit handler can do the cleanup. - The simplest handler possible would be + A simple handler would be ``` process.on('SIGINT', process.exit); ``` -- [#206](https://github.com/raszi/node-tmp/issues/206) +- [#156](https://github.com/raszi/node-tmp/issues/156) - document name option. + ***BREAKING CHANGE*** + + template option no longer accepts arbitrary paths. all paths must be relative to os.tmpdir(). + the template option can point to an absolute path that is located under os.tmpdir(). + this can now be used in conjunction with the dir option. + +- [#207](https://github.com/raszi/node-tmp/issues/TBD) + + ***BREAKING CHANGE*** + + dir option no longer accepts arbitrary paths. all paths must be relative to os.tmpdir(). + the dir option can point to an absolute path that is located under os.tmpdir(). + +- [#218](https://github.com/raszi/node-tmp/issues/TBD) + + ***BREAKING CHANGE*** + + name option no longer accepts arbitrary paths. name must no longer contain a path and will always be made relative + to the current os.tmpdir() and the optional dir option. - [#197](https://github.com/raszi/node-tmp/issues/197) + ***BUG FIX*** + sync cleanup callback must be returned when using the sync API functions. fs.rmdirSync() must not be called with a second parameter that is a function. -- [#179](https://github.com/raszi/node-tmp/issues/179) +- [#176](https://github.com/raszi/node-tmp/issues/176) + + ***BUG FIX*** - template option no longer accepts arbitrary paths. + fail early if no os.tmpdir() was specified. + previous versions of Electron did return undefined when calling os.tmpdir(). + _getTmpDir() now tries to resolve the path returned by os.tmpdir(). + + now using rimraf for removing directory trees. -- [#176](https://github.com/raszi/node-tmp/issues/176) +- [#206](https://github.com/raszi/node-tmp/issues/206) - fail early if no tmp dir was specified. + ***DOCUMENTATION*** - use rimraf for removing directory trees. + document name option. + +- [#236](https://github.com/raszi/node-tmp/issues/236) + + ***DOCUMENTATION*** + + document discardDescriptor option. + +- [#237](https://github.com/raszi/node-tmp/issues/237) + + ***DOCUMENTATION*** + + document detachDescriptor option. + +- [#238](https://github.com/raszi/node-tmp/issues/238) + + ***DOCUMENTATION*** + + document mode option. - [#175](https://github.com/raszi/node-tmp/issues/175) - add unsafeCleanup option to jsdoc. + ***DOCUMENTATION*** -- drop support for node version < v8 + document unsafeCleanup option. - ***BREAKING CHANGE*** - - node version < v8 are no longer supported. +### Miscellaneous - stabilized tests - - general clean up - - update jsdoc diff --git a/README.md b/README.md index e5ca66f..ff49862 100644 --- a/README.md +++ b/README.md @@ -343,16 +343,25 @@ tmp.setGracefulCleanup(); All options are optional :) - * `name`: a fixed name that overrides random name generation - * `mode`: the file mode to create with, it fallbacks to `0600` on file creation and `0700` on directory creation - * `prefix`: the optional prefix, fallbacks to `tmp-` if not provided - * `postfix`: the optional postfix, fallbacks to `.tmp` on file creation - * `template`: [`mkstemp`][3] like filename template, no default - * `dir`: the optional temporary directory, fallbacks to system default (guesses from environment) + * `name`: a fixed name that overrides random name generation, the name must be relative and must not contain path segments + * `mode`: the file mode to create with, falls back to `0o600` on file creation and `0o700` on directory creation + * `prefix`: the optional prefix, defaults to `tmp` + * `postfix`: the optional postfix + * `template`: [`mkstemp`][3] like filename template, no default, can be either an absolute or a relative path that resolves + to a relative path of the system's default temporary directory, must include `XXXXXX` once for random name generation, e.g. + 'foo/bar/XXXXXX'. Absolute paths are also fine as long as they are relative to os.tmpdir(). + Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access, + as tmp will not check the availability of the path, nor will it establish the requested path for you. + * `dir`: the optional temporary directory that must be relative to the system's default temporary directory. + absolute paths are fine as long as they point to a location under the system's default temporary directory. + Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access, + as tmp will not check the availability of the path, nor will it establish the requested path for you. * `tries`: how many times should the function try to get a unique filename before giving up, default `3` * `keep`: signals that the temporary file or directory should not be deleted on exit, default is `false` * In order to clean up, you will have to call the provided `cleanupCallback` function manually. * `unsafeCleanup`: recursively removes the created temporary directory, even when it's not empty. default is `false` + * `detachDescriptor`: detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection + * `discardDescriptor`: discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection [1]: http://nodejs.org/ [2]: https://www.npmjs.com/browse/depended/tmp diff --git a/docs/global.html b/docs/global.html index 083a024..7c24e94 100644 --- a/docs/global.html +++ b/docs/global.html @@ -251,7 +251,7 @@
                              Parameters:
                              Source:
                              @@ -388,7 +388,7 @@
                              Parameters:
                              Source:
                              @@ -628,7 +628,7 @@
                              Parameters:
                              Source:
                              @@ -765,7 +765,7 @@
                              Parameters:
                              Source:
                              @@ -904,7 +904,7 @@

                              isEBADFSource:
                              @@ -992,7 +992,7 @@

                              isENOENTSource:
                              @@ -1097,7 +1097,7 @@

                              isExpe
                              Source:
                              @@ -1185,7 +1185,7 @@

                              set
                              Source:
                              @@ -1368,7 +1368,7 @@

                              Parameters:
                              Source:
                              @@ -1505,7 +1505,7 @@
                              Parameters:
                              Source:
                              @@ -1711,7 +1711,7 @@
                              Parameters:
                              Source:
                              @@ -1799,7 +1799,7 @@

                              cl
                              Source:
                              @@ -2006,7 +2006,7 @@

                              Parameters:
                              Source:
                              @@ -2213,7 +2213,7 @@
                              Parameters:
                              Source:
                              @@ -2370,7 +2370,7 @@
                              Properties:
                              Source:
                              @@ -2594,7 +2594,7 @@
                              Parameters:
                              Source:
                              @@ -2832,7 +2832,7 @@
                              Parameters:
                              Source:
                              @@ -3012,7 +3012,7 @@
                              Properties:
                              Source:
                              @@ -3143,6 +3143,30 @@
                              Properties:
                              + + + (?int) + + + + + + + + + + + + + + + + + mode the access mode, defaults are 0o700 for directories and 0o600 for files + + + + template @@ -3328,6 +3352,68 @@
                              Properties:
                              + + + + detachDescriptor + + + + + +boolean + + + + + + + + + + + <nullable>
                              + + + + + + + detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection + + + + + + + discardDescriptor + + + + + +boolean + + + + + + + + + + + <nullable>
                              + + + + + + + discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection + + + @@ -3363,7 +3449,7 @@
                              Properties:
                              Source:
                              @@ -3437,7 +3523,7 @@

                              simpleC
                              Source:
                              @@ -3620,7 +3706,7 @@

                              Parameters:
                              Source:
                              @@ -3670,7 +3756,7 @@

                              Home

                              Global

                              • diff --git a/docs/index.html b/docs/index.html index 84f3445..6624d83 100644 --- a/docs/index.html +++ b/docs/index.html @@ -286,12 +286,19 @@

                                Graceful cleanup

                                Options

                                All options are optional :)

                                  -
                                • name: a fixed name that overrides random name generation
                                • -
                                • mode: the file mode to create with, it fallbacks to 0600 on file creation and 0700 on directory creation
                                • -
                                • prefix: the optional prefix, fallbacks to tmp- if not provided
                                • -
                                • postfix: the optional postfix, fallbacks to .tmp on file creation
                                • -
                                • template: mkstemp like filename template, no default
                                • -
                                • dir: the optional temporary directory, fallbacks to system default (guesses from environment)
                                • +
                                • name: a fixed name that overrides random name generation, the name must be relative and must not contain path segments
                                • +
                                • mode: the file mode to create with, falls back to 0o600 on file creation and 0o700 on directory creation
                                • +
                                • prefix: the optional prefix, defaults to tmp
                                • +
                                • postfix: the optional postfix
                                • +
                                • template: mkstemp like filename template, no default, can be either an absolute or a relative path that resolves +to a relative path of the system's default temporary directory, must include XXXXXX once for random name generation, e.g. +'foo/bar/XXXXXX'. Absolute paths are also fine as long as they are relative to os.tmpdir(). +Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access, +as tmp will not check the availability of the path, nor will it establish the requested path for you.
                                • +
                                • dir: the optional temporary directory that must be relative to the system's default temporary directory. +absolute paths are fine as long as they point to a location under the system's default temporary directory. +Any directories along the so specified path must exist, otherwise a ENOENT error will be thrown upon access, +as tmp will not check the availability of the path, nor will it establish the requested path for you.
                                • tries: how many times should the function try to get a unique filename before giving up, default 3
                                • keep: signals that the temporary file or directory should not be deleted on exit, default is false
                                    @@ -299,6 +306,8 @@

                                    Options

                                • unsafeCleanup: recursively removes the created temporary directory, even when it's not empty. default is false
                                • +
                                • detachDescriptor: detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection
                                • +
                                • discardDescriptor: discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection

@@ -316,7 +325,7 @@

Home

Global