Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tmp file #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 1 addition & 29 deletions packages/convert-svg-core/src/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const fileUrl = require('file-url');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const tmp = require('tmp');
const util = require('util');

const readFile = util.promisify(fs.readFile);
Expand All @@ -42,7 +41,6 @@ const _convert = Symbol('convert');
const _destroyed = Symbol('destroyed');
const _getDimensions = Symbol('getDimensions');
const _getPage = Symbol('getPage');
const _getTempFile = Symbol('getTempFile');
const _isAttributeAllowed = Symbol('isAttributeAllowed');
const _options = Symbol('options');
const _page = Symbol('page');
Expand All @@ -52,7 +50,6 @@ const _roundDimension = Symbol('roundDimension');
const _roundDimensions = Symbol('roundDimensions');
const _sanitize = Symbol('sanitize');
const _setDimensions = Symbol('setDimensions');
const _tempFile = Symbol('tempFile');
const _validate = Symbol('validate');

/**
Expand Down Expand Up @@ -246,10 +243,6 @@ class Converter {

this[_destroyed] = true;

if (this[_tempFile]) {
this[_tempFile].cleanup();
delete this[_tempFile];
}
if (this[_browser]) {
await this[_browser].close();
delete this[_browser];
Expand Down Expand Up @@ -365,32 +358,11 @@ html { background-color: ${provider.getBackgroundColor(options)}; }
this[_page] = await this[_browser].newPage();
}

const tempFile = await this[_getTempFile]();

await writeFile(tempFile.path, html);

await this[_page].goto(fileUrl(tempFile.path));
await this[_page].setContent(html);

return this[_page];
}

[_getTempFile]() {
if (this[_tempFile]) {
return Promise.resolve(this[_tempFile]);
}

return new Promise((resolve, reject) => {
tmp.file({ prefix: 'convert-svg-', postfix: '.html' }, (error, filePath, fd, cleanup) => {
if (error) {
reject(error);
} else {
this[_tempFile] = { path: filePath, cleanup };

resolve(this[_tempFile]);
}
});
});
}

[_isAttributeAllowed](attributeName, options) {
return this[_allowedAttributeNames].has(attributeName) ||
Expand Down