How to use the fileSync function from tmp

Find comprehensive JavaScript tmp.fileSync code examples handpicked from public code repositorys.

tmp.fileSync is a function in Node.js that creates a temporary file synchronously with a unique name and returns its path, which can be used for temporary storage and processing of data.

122
123
124
125
126
127
128
129
130
131
A synchronous version of the above.

```javascript
const tmp = require('tmp');

const tmpobj = tmp.fileSync();
console.log('File: ', tmpobj.name);
console.log('Filedescriptor: ', tmpobj.fd);
  
// If we don't need the file anymore we could manually call the removeCallback
fork icon97
star icon711
watch icon7

+ 3 other calls in file

57
58
59
60
61
62
63
64
65
66

consoleWrapper.init();
tmp.setGracefulCleanup();

beforeEach(() => {
    keyFile        = tmp.fileSync();
    keyFileContent = Buffer.from(nanoid());

    fs.writeFileSync(keyFile.name, keyFileContent);
});
fork icon677
star icon0
watch icon0

How does tmp.fileSync work?

The tmp.fileSync function in Node.js is similar to the tmp.file function, but creates a temporary file synchronously instead of asynchronously. When called, tmp.fileSync generates a unique filename, creates a new file with that name in a temporary directory, and returns the full path to that file. The function takes an optional options object as a parameter, which can be used to customize the behavior of the temporary file creation. For example, the options object can be used to specify a prefix or suffix for the temporary file name, specify the directory where the file should be created, or set the file mode permissions. Unlike tmp.file, tmp.fileSync blocks the current thread of execution until the temporary file has been created, which can be useful in cases where the program needs to ensure that the file exists before proceeding. Once the calling code is finished with the temporary file, it should delete the file to free up disk space and avoid leaving behind any sensitive or confidential data. This can be accomplished using the fs.unlinkSync function in Node.js, which removes a file synchronously. In summary, tmp.fileSync is a synchronous function that creates a temporary file with a unique name, which can be used for temporary storage and processing of data, and should be deleted once it is no longer needed to avoid leaving behind any sensitive or confidential data.

520
521
522
523
524
525
526
527
528
529
        });
});

describe('`key`, `cert` and `pfx` keys', () => {
    it('Should parse keys as file paths and read its content', () => {
        const keyFile         = tmp.fileSync();
        const certFile        = tmp.fileSync();
        const pfxFile         = tmp.fileSync();
        const keyFileContent  = Buffer.from(nanoid());
        const certFileContent = Buffer.from(nanoid());
fork icon677
star icon0
watch icon0

+ 2 other calls in file

51
52
53
54
55
56
57
58
59
60
A synchronous version of the above.

```javascript
var tmp = require('tmp');

var tmpobj = tmp.fileSync();
console.log("File: ", tmpobj.name);
console.log("Filedescriptor: ", tmpobj.fd);
  
// If we don't need the file anymore we could manually call the removeCallback
fork icon97
star icon0
watch icon2

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const fs = require("fs");
const tmp = require("tmp");

// Create a temporary file
const tmpFile = tmp.fileSync();

// Write some data to the file
fs.writeFileSync(tmpFile.name, "Hello, world!");

// Read the data back from the file
const data = fs.readFileSync(tmpFile.name, "utf-8");
console.log(data);

// Delete the temporary file
fs.unlinkSync(tmpFile.name);

In this example, the tmp.fileSync() function is called to create a temporary file synchronously, and the resulting tmpFile object contains the path to the file. We then use the Node.js fs module to write the string "Hello, world!" to the file, read the data back from the file, and log it to the console. Finally, we call the fs.unlinkSync function to delete the temporary file when we're done with it.

17
18
19
20
21
22
23
24
25
26
27


/**
 * Prepare a dynamic temp file to include all components
 */
tmp.setGracefulCleanup();
const tmpObj = tmp.fileSync();
const code = makeCode(
	[...findExampleFiles(PACKAGE_NAME, PACKAGE_NAME), ...findDemoFiles(PACKAGE_NAME, PACKAGE_NAME)],
	'start.js',
	PACKAGE_NAME,
fork icon11
star icon25
watch icon0

53
54
55
56
57
58
59
60
61
62
63
function compare() {
    doWebdavAction((webdav, workingFile, remoteFile) => {
        return new Promise((resolve, reject) => {
            // Write the remote file to a local temporary file
            const extension = workingFile.slice(workingFile.lastIndexOf('.'));
            const tmpFile = tmp.fileSync({ postfix: extension });
            webdav.readFile(remoteFile, "utf8", (error, data) => {


                if (error != null) {
                    console.log(error);
fork icon4
star icon3
watch icon2

1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
let tmp = require("tmp")


require.extensions[".jsbr"] = (m, filename) => {
    if (!zlib) zlib = require("zlib")
    if (!tmp) tmp = require("tmp")
    let tmpFile = tmp.fileSync()


    fs.writeFileSync(tmpFile.name + ".js", zlib.brotliDecompressSync(fs.readFileSync(filename)))
    return require.extensions[".js"](m, tmpFile.name + ".js")
}
fork icon3
star icon6
watch icon0

92
93
94
95
96
97
98
99
100
101
  console.error(String(gitShowResult.stderr));
  continue;
}

const baseFileContents = gitShowResult.stdout;
const base = tmp.fileSync().name;
writeFileSync(base, baseFileContents);

// Run the merge with `git merge-file`
const mergeFileResult = spawnSync('git', ['merge-file', to, base, from], {
fork icon0
star icon0
watch icon1

62
63
64
65
66
67
68
69
70
71
    }
    if (t==null) {
        ctx.status = 500
        return ctx.body = 'Bad file'
    }
    let f = tmp.fileSync()
    fs.writeFileSync(f.name, ctx.req.file.buffer)
    ctx.body = `${t}|${f.name}`
} catch (e) {
    ctx.status = 500
fork icon0
star icon0
watch icon1

11
12
13
14
15
16
17
18
19
20
21
    }
    t.fail('Function did not throw an error.');
}


test.before(async () => {
    const database_file = tmp.fileSync({ keep: true });
    console.log(`Database file is ${database_file.name}`);
    database = await Database.setup({ file: database_file.name });
});

fork icon0
star icon0
watch icon1