From da421e29bea0f2b3a43c01e6057a45b3c1bb3517 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sat, 6 Oct 2018 17:35:26 +0200 Subject: [PATCH] fix(core): update --- npmextra.json | 6 ++--- package-lock.json | 42 ++++++++++++++------------------- package.json | 14 ++++++----- test/test.ts | 10 +++++--- ts/smartpdf.classes.smartpdf.ts | 21 +++++++++++++---- ts/smartpdf.plugins.ts | 19 ++++----------- 6 files changed, 55 insertions(+), 57 deletions(-) diff --git a/npmextra.json b/npmextra.json index aa842b7..193d59b 100644 --- a/npmextra.json +++ b/npmextra.json @@ -1,8 +1,6 @@ { "npmci": { - "npmGlobalTools": [ - "@gitzone/npmts", - "ts-node" - ] + "npmGlobalTools": [], + "npmAccessLevel": "public" } } diff --git a/package-lock.json b/package-lock.json index 5ca5b5e..8651735 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "smartpdf", + "name": "@pushrocks/smartpdf", "version": "1.0.1", "lockfileVersion": 1, "requires": true, @@ -154,6 +154,17 @@ "which": "^1.3.1" } }, + "@pushrocks/smartunique": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@pushrocks/smartunique/-/smartunique-3.0.1.tgz", + "integrity": "sha512-xBu9ZB4C0BA0S/pbFFZn2ItPfnodPKpzrYIq1yN5XDs6OaookwcDF/iBwfS9+EYMSPENC9wAsOxg2RGMm4Qicw==", + "requires": { + "@types/shortid": "^0.0.29", + "@types/uuid": "^3.0.0", + "shortid": "^2.2.8", + "uuid": "^3.1.0" + } + }, "@pushrocks/tapbundle": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/@pushrocks/tapbundle/-/tapbundle-3.0.7.tgz", @@ -266,9 +277,9 @@ "integrity": "sha512-ojnbBiKkZFYRfQpmtnnWTMw+rzGp/JiystjluW9jgN3VzRwilXddJ6aGQ9V/7iuDG06SBgn7ozW9k3zcAnYjYQ==" }, "@types/puppeteer": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/@types/puppeteer/-/puppeteer-1.8.0.tgz", - "integrity": "sha512-YR6aEjBY87BNj3k8wrtieU39ljyVAceKqO08fI9EMBDkYu5Sp6vioTKiAIsrNqDyB0u249lqLmmhwwq6qLNqww==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@types/puppeteer/-/puppeteer-1.9.0.tgz", + "integrity": "sha512-ZP3wL4Lqpgu4xZbz/0vQhbIZnllNEawVW3oTpEXyJg+uhf/YQgL8oeTrfoUwj9JXE9UUe53eqmgLl29/d2gjqQ==", "requires": { "@types/node": "*" } @@ -1148,9 +1159,9 @@ "integrity": "sha1-M8UDmPcOp+uW0h97gXYwpVeRx+4=" }, "puppeteer": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-1.8.0.tgz", - "integrity": "sha512-wJ7Fxs03l4dy/ZXQACUKBBobIuJaS4NHq44q7/QinpAXFMwJMJFEIPjzoksVzUhZxQe+RXnjXH69mg13yMh0BA==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-1.9.0.tgz", + "integrity": "sha512-GH4PmhJf9wBRAPvtJkEJLAvdNNOofZortmBZSj8cGWYni98GUFqsf66blOEfJbo5B8l0KG5HR2d/W2MejnUrzg==", "requires": { "debug": "^3.1.0", "extract-zip": "^1.6.6", @@ -1326,18 +1337,6 @@ "chai-string": "^1.4.0" } }, - "smartunique": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/smartunique/-/smartunique-2.0.0.tgz", - "integrity": "sha512-OkJIrjAeH5NBDCrx2mX23huCoQoCCcwIbzznWIYqLn7Q+FezPjGzqGWk62IQJ3OvAGWt3j+INmpZygJks1F5MQ==", - "requires": { - "@types/shortid": "^0.0.29", - "@types/uuid": "^3.0.0", - "shortid": "^2.2.8", - "typings-global": "^1.0.16", - "uuid": "^3.1.0" - } - }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -1464,11 +1463,6 @@ "integrity": "sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==", "dev": true }, - "typings-global": { - "version": "1.0.28", - "resolved": "https://registry.npmjs.org/typings-global/-/typings-global-1.0.28.tgz", - "integrity": "sha512-6VOwJWEY2971HOMHu/7sURzUXiD4/LiMJPsMAOqkHHAtS3MVpLFE5gzTiHilsH9KY5VE1mBQirWIgWFsDuo90A==" - }, "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", diff --git a/package.json b/package.json index 5316d5c..6fd42ac 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,16 @@ { - "name": "smartpdf", + "name": "@pushrocks/smartpdf", "version": "1.0.1", + "private": false, "description": "create pdfs on the fly", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Lossless GmbH", "license": "MIT", "scripts": { - "test": "tstest test/", - "format": "(gitzone format)" + "test": "(tstest test/)", + "format": "(gitzone format)", + "build": "(tsbuild)" }, "devDependencies": { "@gitzone/tsbuild": "^2.0.22", @@ -21,10 +23,10 @@ "@pushrocks/smartfile": "^6.0.8", "@pushrocks/smartnetwork": "^1.1.0", "@pushrocks/smartpromise": "^2.0.5", + "@pushrocks/smartunique": "^3.0.1", "@types/express": "^4.16.0", - "@types/puppeteer": "^1.8.0", + "@types/puppeteer": "^1.9.0", "express": "^4.16.3", - "puppeteer": "^1.8.0", - "smartunique": "^2.0.0" + "puppeteer": "^1.9.0" } } diff --git a/test/test.ts b/test/test.ts index 6c8223e..9b725bd 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,12 +1,12 @@ import { expect, tap } from '@pushrocks/tapbundle'; -import * as smartpdf from '../ts/index' +import * as smartpdf from '../ts/index'; let testSmartPdf: smartpdf.SmartPdf; tap.test('should create a valid instance of smartpdf', async () => { testSmartPdf = new smartpdf.SmartPdf(); expect(testSmartPdf).to.be.instanceof(smartpdf.SmartPdf); -}) +}); tap.test('should create a pdf from html string', async () => { await testSmartPdf.getPdfForHtmlString('hi'); @@ -16,4 +16,8 @@ tap.test('should create a pdf from website', async () => { await testSmartPdf.getPdfForWebsite('https://wikipedia.org'); }); -tap.start() +tap.test('should be able to close properly', async () => { + await testSmartPdf.close(); +}); + +tap.start(); diff --git a/ts/smartpdf.classes.smartpdf.ts b/ts/smartpdf.classes.smartpdf.ts index 3d19d3d..550e625 100644 --- a/ts/smartpdf.classes.smartpdf.ts +++ b/ts/smartpdf.classes.smartpdf.ts @@ -8,7 +8,7 @@ export class SmartPdf { serverPort: number; headlessBrowser: plugins.puppeteer.Browser; private _readyDeferred: plugins.smartpromise.Deferred; - private _candidates: {[key: string]: PdfCandidate} = {}; + private _candidates: { [key: string]: PdfCandidate } = {}; constructor() { this._readyDeferred = new plugins.smartpromise.Deferred(); @@ -34,6 +34,15 @@ export class SmartPdf { }); } + async close () { + const done = plugins.smartpromise.defer(); + this.htmlServerInstance.close(() => { + done.resolve(); + }); + await this.headlessBrowser.close(); + await done.promise; + } + /** * returns a pdf for a given html string; */ @@ -42,15 +51,17 @@ export class SmartPdf { const pdfCandidate = new PdfCandidate(htmlStringArg); this._candidates[pdfCandidate.pdfId] = pdfCandidate; const page = await this.headlessBrowser.newPage(); - const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, { waitUntil: 'networkidle2' }); + const response = await page.goto(`http://localhost:3210/${pdfCandidate.pdfId}`, { + waitUntil: 'networkidle2' + }); const headers = response.headers(); - if(headers['pdf-id'] !== pdfCandidate.pdfId) { + if (headers['pdf-id'] !== pdfCandidate.pdfId) { console.log('Error! Headers do not match. For security reasons no pdf is being emitted!'); - return + return; } else { console.log(`id security check passed for ${pdfCandidate.pdfId}`); } - + await page.pdf({ path: plugins.path.join(paths.pdfDir, `${pdfCandidate.pdfId}.pdf`), format: 'A4' diff --git a/ts/smartpdf.plugins.ts b/ts/smartpdf.plugins.ts index 9b28033..d2ea3d3 100644 --- a/ts/smartpdf.plugins.ts +++ b/ts/smartpdf.plugins.ts @@ -2,29 +2,18 @@ import * as http from 'http'; import * as path from 'path'; -export { - http, - path -} +export { http, path }; // @pushrocks import * as smartfile from '@pushrocks/smartfile'; import * as smartpromise from '@pushrocks/smartpromise'; import * as smartnetwork from '@pushrocks/smartnetwork'; -import * as smartunique from 'smartunique'; +import * as smartunique from '@pushrocks/smartunique'; -export { - smartfile, - smartpromise, - smartunique, - smartnetwork -} +export { smartfile, smartpromise, smartunique, smartnetwork }; // thirdparty import * as express from 'express'; import * as puppeteer from 'puppeteer'; -export { - express, - puppeteer -} +export { express, puppeteer };