Innovenergy_trunk/frontend/node_modules/testcafe/lib/utils/promisified-functions.js

47 lines
6.1 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writePng = exports.readPngFile = exports.readPng = exports.sendMessageToChildProcess = exports.exec = exports.deleteFile = exports.readFile = exports.writeFile = exports.stat = exports.readDir = void 0;
const child_process_1 = __importDefault(require("child_process"));
const graceful_fs_1 = __importDefault(require("graceful-fs"));
const pngjs_1 = require("pngjs");
const promisify_event_1 = __importDefault(require("promisify-event"));
const util_1 = require("util");
exports.readDir = (0, util_1.promisify)(graceful_fs_1.default.readdir);
exports.stat = (0, util_1.promisify)(graceful_fs_1.default.stat);
exports.writeFile = (0, util_1.promisify)(graceful_fs_1.default.writeFile);
exports.readFile = (0, util_1.promisify)(graceful_fs_1.default.readFile);
exports.deleteFile = (0, util_1.promisify)(graceful_fs_1.default.unlink);
exports.exec = (0, util_1.promisify)(child_process_1.default.exec);
exports.sendMessageToChildProcess = (0, util_1.promisify)((process, ...args) => process.send(...args));
function readPng(buffer) {
const png = new pngjs_1.PNG();
const parsedPromise = Promise.race([
(0, promisify_event_1.default)(png, 'parsed'),
(0, promisify_event_1.default)(png, 'error'),
]);
png.parse(buffer);
return parsedPromise
.then(() => png);
}
exports.readPng = readPng;
async function readPngFile(filePath) {
const buffer = await (0, exports.readFile)(filePath);
return await readPng(buffer);
}
exports.readPngFile = readPngFile;
function writePng(filePath, png) {
const outStream = graceful_fs_1.default.createWriteStream(filePath);
const pngStream = png.pack();
const finishPromise = Promise.race([
(0, promisify_event_1.default)(outStream, 'finish'),
(0, promisify_event_1.default)(outStream, 'error'),
(0, promisify_event_1.default)(pngStream, 'error'),
]);
pngStream.pipe(outStream);
return finishPromise;
}
exports.writePng = writePng;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvbWlza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