Giter Club home page Giter Club logo

create-temp-file's Introduction

create-temp-file

A tiny node module that creates a temporary file, returns a write stream, a path, and cleanup functions

Build Status

example

var createTempFile = require('create-temp-file')

var ws = createTempFile()
process.stdin.pipe(ws)

process.on('exit', ws.cleanupSync)

api

var createTempFile = require('create-temp-file')

var ws = createTempFile([extension])

You can set the file extension for the temp file, or don't set an extension. E.g. '.png'.

ws is a write stream to the new temporary file with the following properties:

  • ws.path is the absolute path to the temporary file. E.g. '/tmp/b285e724-226c-11e5-9981-82bd40254040.png'
  • ws.cleanup([cb]) deletes the temporary file. Like fs.unlink
  • ws.cleanupSync deletes the temporary file synchronously. Like fs.unlinkSync

If an error occurs in ws.cleanup() or ws.cleanupSync(), the error will be emitted. Catch any errors like this:

ws.on('error', function (e) {
	throw e
})

install

With npm do:

npm install create-temp-file

license

VOL

create-temp-file's People

Contributors

artskydj avatar dependabot[bot] avatar kikobeats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

razcakappa

create-temp-file's Issues

weird test

  • Occasionally uses tmpFile.end(), sometimes does not. Is there a reason?
  • The timeouts seem a bit weird. Is there a way these can be eliminated?

Instead of options, take a path generator

v1.0.0

This module should export a constructor that takes a path generator instead of taking options and passing them to tempfile2.

In the readme, show examples of usage with tempfile and tempfile2.

var createTempFile = require('create-temp-file')()
createTempFile().path // 'C:\Users\Joe\AppData\Temp\97218362-258c-11e5-b2b2-9870b6c90e9a'
var createTempFile = require('create-temp-file')(Math.random)
createTempFile().path // 'C:\Users\Joe\AppData\Temp\0.316572587312297'
var createTempFile = require('create-temp-file')(function pathGen() {
    return './file.png'
})
createTempFile().path // 'C:\Users\Joseph\AppData\Temp\file.png'
var tempfile = require('tempfile')
var createTempFile = require('create-temp-file')(tempfile)
createTempFile().path // 'C:\Users\Joe\AppData\Temp\97218362-258c-11e5-b2b2-9870b6c90e9a'
var tempfile = require('tempfile2')
var createTempFile = require('create-temp-file')(tempfile.bind(null, { ensure: true }))
createTempFile().path // 'C:\Users\Joe\AppData\Temp\97218362-258c-11e5-b2b2-9870b6c90e9a'

use sindresorhus/tempfile

Use sindresorhus/tempfile instead of sindresorhus/os-tmpdir.

Change createTempFile() to createTempFile([ext])

simplify

Passing in the constructor function is awkward. Just require tempdir, and be done with it.

Update to ES2015 while I'm at it...

Cleanup of cleanup()

If cleanup is called on a non-existent temp file from a stream, an error is emitted, which calls cleanup. Infinite loop. This seems best place to fix it. Just checks for existence of file first.

writeStream.cleanup = function cln(cb) {
  writeStream.end()
  fs.access(path, fs.W_OK, function(err) {
    if (!err) {
      //console.log("Cleaning up: " + path);
      fs.unlink(path, function(err) {
        if (err) writeStream.emit('error', err)
        if (cb) cb(err)
      });
    } else {
      //console.log("Skipping cleanup.");
    }
  });
}

Error handling

Right now:

  • cleanupSync() throws errors
  • cleanup() swallows errors
  • cleanup(cb) does cb(err)

wat

Proposed solution:

  • cleanupSync() does ws.emit('error', err)
  • cleanup() does ws.emit('error', err)
  • cleanup(cb) does cb(err)

why force .cleanup callback?

I don't understand this piece of code:

writeStream.cleanup = function cln(cb) {
        if (!streamHasEnded) writeStream.end()
        fs.unlink(path, cb || emitError)
    }

why, if not provide a callback, delete a file in any moment in the future? why is forcing emit an error?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.