New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ioffice/svg2png

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ioffice/svg2png - npm Package Compare versions

Comparing version 0.0.1-beta.1808291847 to 0.0.1-beta.1808301503

2

package.json
{
"name": "@ioffice/svg2png",
"description": "Converts SVGs to PNGs, using Chromium",
"version": "0.0.1-beta.1808291847",
"version": "0.0.1-beta.1808301503",
"author": "Orignal Author: Domenic Denicola (https://domenic.me)",

@@ -6,0 +6,0 @@ "contributors": [

@@ -16,2 +16,3 @@ /// <reference types="node" />

declare class Svg2png {
private static idCounter;
static pages: {

@@ -21,13 +22,34 @@ [key: number]: Page;

static pool: Pool<Browser>;
static idCounter: number;
private source;
private id;
private options;
private history;
constructor(config: IConfig);
/**
* Needs to be called if we wish the server to properly shut down. If the pool
* remains open then node won't be able to exit.
*/
static closePool(): Promise<undefined>;
/**
* Overwrite in your program to debug messages.
* @param id: The id of the drawing
* @param msg: The message provided by svg2png
* @param meta: Any optional metadata provided along with the message.
*/
static debug(id: number, msg: string, meta?: object): void;
log(msg: string, meta?: object): void;
failure(msg: string | Error): Promise<any>;
/**
* For debugging purposes only. In case of a failure the conversion will reject with the
* history.
*/
private log;
/**
* To be used when rejecting a promise. Note that this will log the failure.
*/
private failure;
/**
* Convert the svg.
*/
convert(): Promise<Buffer>;
private convertInBrowser;
private cleanUp;
/**

@@ -44,3 +66,3 @@ * Obtain the page with the svg loaded.

*/
closePage(): Promise<void>;
private closePage;
/**

@@ -47,0 +69,0 @@ * Sets the dimensions of the svg. The dimensions passed in must be positive numbers. The same

@@ -9,2 +9,3 @@ "use strict";

function Svg2png(config) {
this.history = [];
this.id = ++Svg2png.idCounter;

@@ -19,2 +20,6 @@ var opt = Object.assign({}, config);

}
/**
* Needs to be called if we wish the server to properly shut down. If the pool
* remains open then node won't be able to exit.
*/
Svg2png.closePool = function () {

@@ -42,6 +47,17 @@ return tslib_1.__awaiter(this, void 0, void 0, function () {

};
/**
* Overwrite in your program to debug messages.
* @param id: The id of the drawing
* @param msg: The message provided by svg2png
* @param meta: Any optional metadata provided along with the message.
*/
Svg2png.debug = function (id, msg, meta) {
console.log("[SVG2PNG:" + id + "]", msg, meta || '');
};
/**
* For debugging purposes only. In case of a failure the conversion will reject with the
* history.
*/
Svg2png.prototype.log = function (msg, meta) {
this.history.push(meta ? [msg, meta] : msg);
if (this.options.debug) {

@@ -51,14 +67,36 @@ Svg2png.debug(this.id, msg, meta);

};
/**
* To be used when rejecting a promise. Note that this will log the failure.
*/
Svg2png.prototype.failure = function (msg) {
if (typeof msg === 'string') {
this.log("FAILURE: " + msg);
return Promise.reject(new Error(msg));
}
else {
this.log("FAILURE: " + msg.message, { error: msg });
return Promise.reject(msg);
}
this.log("[FAILURE]: " + msg.message, { error: msg });
return Promise.reject(msg);
};
/**
* Convert the svg.
*/
Svg2png.prototype.convert = function () {
return this.convertInBrowser(this.rasterize.bind(this));
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, err_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.convertInBrowser(this.rasterize.bind(this))];
case 1:
result = _a.sent();
this.log('SVG2PNG::success');
return [2 /*return*/, result];
case 2:
err_2 = _a.sent();
this.log('SVG2PNG::failure', { error: err_2 });
err_2.meta = {
id: this.id,
history: this.history,
};
return [2 /*return*/, Promise.reject(err_2)];
case 3: return [2 /*return*/];
}
});
});
};

@@ -68,3 +106,3 @@ Svg2png.prototype.convertInBrowser = function (fn) {

return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var timeoutHandle, buffer, err_2, e_1, e_2;
var timeoutHandle, buffer, err_3;
var _this = this;

@@ -78,52 +116,50 @@ return tslib_1.__generator(this, function (_a) {

delete Svg2png.pages[_this.id];
reject(new Error('timeout rasterizing SVG'));
return reject(new Error('timeout rasterizing SVG'));
}
reject(new Error('this should not have been called, did you cancel the timeout?'));
reject(new Error('timeout was not cancelled'));
}, this.options.conversionTimeout || 30000);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 8]);
this.log('calling "pool.use"');
_a.trys.push([1, 3, , 4]);
this.log('starting conversion');
return [4 /*yield*/, Svg2png.pool.use(function (browser) { return fn(browser); })];
case 2:
buffer = _a.sent();
clearTimeout(timeoutHandle);
return [3 /*break*/, 8];
return [3 /*break*/, 4];
case 3:
err_2 = _a.sent();
this.log('ERROR', { error: err_2 });
err_3 = _a.sent();
this.cleanUp(timeoutHandle);
return [2 /*return*/, reject(err_3)];
case 4:
this.cleanUp(timeoutHandle);
resolve(buffer);
return [2 /*return*/];
}
});
}); });
};
Svg2png.prototype.cleanUp = function (timeoutHandle) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var err_4;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
this.log('clearing timeout');
clearTimeout(timeoutHandle);
_a.label = 4;
case 4:
_a.trys.push([4, 6, , 7]);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
this.log('closing page');
return [4 /*yield*/, this.closePage()];
case 5:
case 2:
_a.sent();
return [3 /*break*/, 7];
case 6:
e_1 = _a.sent();
this.log('failed to close page:', { error: e_1 });
return [3 /*break*/, 7];
case 7:
this.log('conversion failed');
return [2 /*return*/, reject(err_2)];
case 8:
_a.trys.push([8, 10, , 11]);
this.log('closing page');
return [4 /*yield*/, this.closePage()];
case 9:
_a.sent();
return [3 /*break*/, 11];
case 10:
e_2 = _a.sent();
this.log('failed to close page:', { error: e_2 });
return [3 /*break*/, 11];
case 11:
resolve(buffer);
return [2 /*return*/];
return [3 /*break*/, 4];
case 3:
err_4 = _a.sent();
this.log('failed to close page', { error: err_4 });
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
});
};

@@ -136,3 +172,3 @@ /**

return tslib_1.__awaiter(this, void 0, void 0, function () {
var page, resp, err_3;
var page, resp, err_5;
return tslib_1.__generator(this, function (_a) {

@@ -147,3 +183,3 @@ switch (_a.label) {

Svg2png.pages[this.id] = page;
this.log("navigating to " + this.source);
this.log("navigating to page", { url: this.source });
return [4 /*yield*/, page.goto(this.source, {

@@ -156,11 +192,12 @@ waitUntil: ['load', 'domcontentloaded', 'networkidle0'],

if (!resp) {
return [2 /*return*/, this.failure('obtained null response from `page.goto`')];
return [2 /*return*/, this.failure(new Error('obtained null response from `page.goto`'))];
}
if (!resp.ok()) {
return [2 /*return*/, this.failure("navigation status: " + resp.status())];
return [2 /*return*/, this.failure(new Error("navigation status: " + resp.status()))];
}
return [2 /*return*/, page];
case 3:
err_3 = _a.sent();
return [2 /*return*/, this.failure("unknown loadPage error: " + err_3.message)];
err_5 = _a.sent();
err_5.message = "Unknown loadPage error: " + err_5.message;
return [2 /*return*/, this.failure(err_5)];
case 4: return [2 /*return*/];

@@ -173,3 +210,3 @@ }

return tslib_1.__awaiter(this, void 0, void 0, function () {
var actions, err_4, dimensions, err_5;
var actions, err_6, dimensions, err_7;
return tslib_1.__generator(this, function (_a) {

@@ -179,3 +216,3 @@ switch (_a.label) {

_a.trys.push([0, 2, , 3]);
this.log('setting dimensions: ', {
this.log('setting dimensions', {
width: this.options.width,

@@ -191,4 +228,4 @@ height: this.options.height,

case 2:
err_4 = _a.sent();
this.log("failed to set dimensions: " + err_4);
err_6 = _a.sent();
this.log("failed to set dimensions", { error: err_6 });
return [3 /*break*/, 3];

@@ -202,3 +239,3 @@ case 3:

if (!dimensions) {
return [2 /*return*/, Promise.reject(new Error('unable to obtain the dimensions'))];
return [2 /*return*/, this.failure(new Error('unable to obtain the dimensions'))];
}

@@ -210,4 +247,5 @@ return [2 /*return*/, {

case 5:
err_5 = _a.sent();
return [2 /*return*/, this.failure("unknown setGetDimensions error: " + err_5.message)];
err_7 = _a.sent();
err_7.message = "unknown setGetDimensions error: " + err_7.message;
return [2 /*return*/, this.failure(err_7)];
case 6: return [2 /*return*/];

@@ -220,3 +258,3 @@ }

return tslib_1.__awaiter(this, void 0, void 0, function () {
var page, _a, width, height, safeOffset, blocksPerRow, blocksPerCol, maxScreenshotHeight, totalBlocks, err_6;
var page, _a, width, height, safeOffset, blocksPerRow, blocksPerCol, maxScreenshotHeight, totalBlocks, err_8;
return tslib_1.__generator(this, function (_b) {

@@ -255,4 +293,4 @@ switch (_b.label) {

case 8:
err_6 = _b.sent();
return [2 /*return*/, Promise.reject(err_6)];
err_8 = _b.sent();
return [2 /*return*/, Promise.reject(err_8)];
case 9: return [2 /*return*/];

@@ -265,3 +303,3 @@ }

return tslib_1.__awaiter(this, void 0, void 0, function () {
var totalChunks, chunks, ypos, chunk, clipHeight, screenshot, buffer, err_7, channels, bufferSize, composite, err_8;
var totalChunks, chunks, ypos, chunk, clipHeight, screenshot, buffer, err_9, channels, bufferSize, composite, err_10;
return tslib_1.__generator(this, function (_a) {

@@ -298,5 +336,5 @@ switch (_a.label) {

case 5:
err_7 = _a.sent();
this.log('chunk collection failure', { error: err_7 });
return [2 /*return*/, this.failure(err_7)];
err_9 = _a.sent();
err_9.message = "chunk collection failure: " + err_9.message;
return [2 /*return*/, this.failure(err_9)];
case 6:

@@ -323,5 +361,5 @@ ypos += maxScreenshotHeight, chunk++;

case 10:
err_8 = _a.sent();
this.log('sharp failure', { error: err_8 });
return [2 /*return*/, this.failure(err_8)];
err_10 = _a.sent();
err_10.message = "sharp failure: " + err_10.message;
return [2 /*return*/, this.failure(err_10)];
case 11: return [2 /*return*/];

@@ -420,2 +458,3 @@ }

};
Svg2png.idCounter = 0;
Svg2png.pages = {};

@@ -433,3 +472,2 @@ Svg2png.pool = puppeteer_pool_1.createPuppeteerPool({

});
Svg2png.idCounter = 0;
return Svg2png;

@@ -439,19 +477,5 @@ }());

function svg2png(config) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var s2pInstance, result;
return tslib_1.__generator(this, function (_a) {
s2pInstance = new Svg2png(config);
try {
result = s2pInstance.convert();
s2pInstance.log('SVG2PNG::success');
return [2 /*return*/, result];
}
catch (err) {
s2pInstance.log('SVG2PNG::failure', { error: err });
return [2 /*return*/, Promise.reject(err)];
}
return [2 /*return*/];
});
});
var s2pInstance = new Svg2png(config);
return s2pInstance.convert();
}
exports.svg2png = svg2png;
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