Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tempy

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tempy - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

188

index.d.ts

@@ -1,2 +0,1 @@

/* eslint-disable @typescript-eslint/member-ordering */
import {Buffer} from 'node:buffer';

@@ -44,130 +43,115 @@ import {MergeExclusive, TypedArray} from 'type-fest';

declare const tempy: {
file: {
/**
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
/**
Get a temporary file path you can write to.
@returns A promise that resolves after the callback is executed and the file is cleaned up.
@example
```
import {temporaryFile, temporaryDirectory} from 'tempy';
@example
```
import tempy from 'tempy';
temporaryFile();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
await tempy.file.task(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
temporaryFile({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
/**
Get a temporary file path you can write to.
temporaryFile({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
@example
```
import tempy from 'tempy';
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function temporaryFile(options?: FileOptions): string;
tempy.file();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
/**
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
tempy.file({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
@returns A promise that resolves after the callback is executed and the file is cleaned up.
tempy.file({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
@example
```
import {temporaryFileTask} from 'tempy';
tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(options?: FileOptions): string;
};
await temporaryFileTask(tempFile => {
console.log(tempFile);
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>;
directory: {
/**
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
/**
Get a temporary directory path. The directory is created for you.
@returns A promise that resolves after the callback is executed and the directory is cleaned up.
@example
```
import {temporaryDirectory} from 'tempy';
@example
```
import tempy from 'tempy';
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
await tempy.directory.task(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
})
```
*/
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>;
temporaryDirectory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
export function temporaryDirectory(options?: DirectoryOptions): string;
/**
Get a temporary directory path. The directory is created for you.
/**
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
@example
```
import tempy from 'tempy';
@returns A promise that resolves after the callback is executed and the directory is cleaned up.
tempy.directory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
@example
```
import {temporaryDirectoryTask} from 'tempy';
tempy.directory({prefix: 'a'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
```
*/
(options?: DirectoryOptions): string;
};
await temporaryDirectoryTask(tempDirectory => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
})
```
*/
export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>;
write: {
/**
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
/**
Write data to a random temp file.
@returns A promise that resolves after the callback is executed and the file is cleaned up.
@example
```
import {temporaryWrite} from 'tempy';
@example
```
import tempy from 'tempy';
await temporaryWrite('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
await tempy.write.task('🦄', tempFile => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>;
/**
Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
/**
Write data to a random temp file.
@returns A promise that resolves after the callback is executed and the file is cleaned up.
@example
```
import tempy from 'tempy';
@example
```
import {temporaryWriteTask} from 'tempy';
await tempy.write('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>;
};
await temporaryWriteTask('🦄', tempFile => {
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
});
```
*/
export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>;
/**
Synchronously write data to a random temp file.
/**
Synchronously write data to a random temp file.
@example
```
import tempy from 'tempy';
@example
```
import {temporaryWriteSync} from 'tempy';
tempy.writeSync('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string;
temporaryWriteSync('🦄');
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
```
*/
export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string;
/**
Get the root temporary directory path.
For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`.
*/
readonly root: string;
};
export default tempy;
export {default as rootTemporaryDirectory} from 'temp-dir';

@@ -1,2 +0,3 @@

import fs, {promises as fsPromises} from 'node:fs';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';

@@ -8,3 +9,2 @@ import stream from 'node:stream';

import {isStream} from 'is-stream';
import del from 'del'; // TODO: Replace this with `fs.rm` when targeting Node.js 14.

@@ -17,62 +17,47 @@ const pipeline = promisify(stream.pipeline); // TODO: Use `node:stream/promises` when targeting Node.js 16.

const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
const [callback, options] = arguments_.slice(extraArguments);
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);
async function runTask(temporaryPath, callback) {
try {
return await callback(result);
return await callback(temporaryPath);
} finally {
await del(result, {force: true});
await fsPromises.rm(temporaryPath, {recursive: true, force: true});
}
};
}
const tempy = {};
tempy.file = options => {
options = {
...options,
};
if (options.name) {
if (options.extension !== undefined && options.extension !== null) {
export function temporaryFile({name, extension} = {}) {
if (name) {
if (extension !== undefined && extension !== null) {
throw new Error('The `name` and `extension` options are mutually exclusive');
}
return path.join(tempy.directory(), options.name);
return path.join(temporaryDirectory(), name);
}
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
};
return getPath() + (extension === undefined || extension === null ? '' : '.' + extension.replace(/^\./, ''));
}
tempy.file.task = createTask(tempy.file);
export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback);
tempy.directory = ({prefix = ''} = {}) => {
export function temporaryDirectory({prefix = ''} = {}) {
const directory = getPath(prefix);
fs.mkdirSync(directory);
return directory;
};
}
tempy.directory.task = createTask(tempy.directory);
export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback);
tempy.write = async (data, options) => {
const filename = tempy.file(options);
const write = isStream(data) ? writeStream : fsPromises.writeFile;
await write(filename, data);
export async function temporaryWrite(fileContent, options) {
const filename = temporaryFile(options);
const write = isStream(fileContent) ? writeStream : fsPromises.writeFile;
await write(filename, fileContent);
return filename;
};
}
tempy.write.task = createTask(tempy.write, {extraArguments: 1});
export const temporaryWriteTask = async (fileContent, callback, options) => runTask(await temporaryWrite(fileContent, options), callback);
tempy.writeSync = (data, options) => {
const filename = tempy.file(options);
fs.writeFileSync(filename, data);
export function temporaryWriteSync(fileContent, options) {
const filename = temporaryFile(options);
fs.writeFileSync(filename, fileContent);
return filename;
};
}
Object.defineProperty(tempy, 'root', {
get() {
return tempDir;
},
});
export default tempy;
export {default as rootTemporaryDirectory} from 'temp-dir';
{
"name": "tempy",
"version": "2.0.0",
"version": "3.0.0",
"description": "Get a random temporary file or directory path",

@@ -16,3 +16,3 @@ "license": "MIT",

"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=14.16"
},

@@ -41,15 +41,15 @@ "scripts": {

"dependencies": {
"del": "^6.0.0",
"is-stream": "^3.0.0",
"temp-dir": "^2.0.0",
"type-fest": "^2.0.0",
"type-fest": "^2.12.2",
"unique-string": "^3.0.0"
},
"devDependencies": {
"ava": "^4.0.0-alpha.2",
"@types/node": "^17.0.24",
"ava": "^4.2.0",
"path-exists": "^5.0.0",
"touch": "^3.1.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"tsd": "^0.20.0",
"xo": "^0.48.0"
}
}

@@ -7,5 +7,5 @@ # tempy

```sh
npm install tempy
```
$ npm install tempy
```

@@ -15,17 +15,17 @@ ## Usage

```js
import tempy from 'tempy';
import {temporaryFile, temporaryDirectory} from 'tempy';
tempy.file();
temporaryFile();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
tempy.file({extension: 'png'});
temporaryFile({extension: 'png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
tempy.file({name: 'unicorn.png'});
temporaryFile({name: 'unicorn.png'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
tempy.directory();
temporaryDirectory();
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
tempy.directory({prefix: 'name'});
temporaryDirectory({prefix: 'name'});
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'

@@ -36,7 +36,7 @@ ```

### tempy.file(options?)
### temporaryFile(options?)
Get a temporary file path you can write to.
### tempy.file.task(callback, options?)
### temporaryFileTask(callback, options?)

@@ -69,7 +69,7 @@ The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.

### tempy.directory(options?)
### temporaryDirectory(options?)
Get a temporary directory path. The directory is created for you.
### tempy.directory.task(callback, options?)
### temporaryDirectoryTask(callback, options?)

@@ -98,7 +98,7 @@ The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the directory is cleaned up.

### tempy.write(fileContent, options?)
### temporaryWrite(fileContent, options?)
Write data to a random temp file.
### tempy.write.task(fileContent, callback, options?)
### temporaryWriteTask(fileContent, callback, options?)

@@ -123,3 +123,3 @@ Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.

### tempy.writeSync(fileContent, options?)
### temporaryWriteSync(fileContent, options?)

@@ -138,4 +138,4 @@ Synchronously write data to a random temp file.

### tempy.root
### rootTemporaryDirectory
Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc