@live-change/print-service 0.8.97

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/browser.js ADDED
@@ -0,0 +1,34 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+ import config from './config.js'
6
+
7
+ import { chrome } from 'playwright'
8
+
9
+ import PQueue from 'p-queue'
10
+ import got from "got"
11
+
12
+ const browserQueue = new PQueue({ concurrency: config.concurrency })
13
+
14
+ async function newBrowser() {
15
+ if(config.browserWebSocketDebuggerUrl) {
16
+ const browser = await chrome.connect({ wsEndpoint: config.browserWebSocketDebuggerUrl })
17
+ return browser
18
+ } else if(config.browserUrl) {
19
+ const browserInfo = await got.post(config.browserUrl + '/json/version').json()
20
+ const browser = await chrome.connect({ wsEndpoint: browserInfo.webSocketDebuggerUrl })
21
+ } else {
22
+ const browser = await chrome.launch()
23
+ return browser
24
+ }
25
+ }
26
+
27
+ export async function runWithBrowser(func) {
28
+ return await browserQueue.add(async () => {
29
+ const browser = newBrowser()
30
+ const result = await func(browser)
31
+ await browser.close()
32
+ return result
33
+ })
34
+ }
package/config.js ADDED
@@ -0,0 +1,25 @@
1
+ import definition from './definition.js'
2
+
3
+ const {
4
+ browserUrl = 'http://localhost:9222',
5
+ browserWebSocketDebuggerUrl,
6
+ concurrency = 1,
7
+ printAuthenticationKey = crypto.randomBytes(24).toString('hex'),
8
+ ssrHost = process.env.SSR_HOST || '127.0.0.1',
9
+ ssrPort = process.env.SSR_PORT || '8001'
10
+ } = definition.config
11
+
12
+ definition.clientConfig = {
13
+
14
+ }
15
+
16
+ const config = {
17
+ browserUrl,
18
+ browserWebSocketDebuggerUrl,
19
+ concurrency,
20
+ printAuthenticationKey,
21
+ ssrHost,
22
+ ssrPort
23
+ }
24
+
25
+ export default config
package/definition.js ADDED
@@ -0,0 +1,13 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import userService from '@live-change/user-service'
5
+ import relationsPlugin from '@live-change/relations-plugin'
6
+ import accessControlService from '@live-change/access-control-service'
7
+
8
+ const definition = app.createServiceDefinition({
9
+ name: "print",
10
+ use: [ userService, relationsPlugin, accessControlService ]
11
+ })
12
+
13
+ export default definition
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+
6
+ import "./print.js"
7
+
8
+ export default definition
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@live-change/print-service",
3
+ "version": "0.8.97",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "NODE_ENV=test tape tests/*"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/live-change/live-change-stack.git"
12
+ },
13
+ "license": "MIT",
14
+ "bugs": {
15
+ "url": "https://github.com/live-change/live-change-stack/issues"
16
+ },
17
+ "homepage": "https://github.com/live-change/live-change-stack",
18
+ "author": {
19
+ "email": "michal@laszczewski.pl",
20
+ "name": "Michał Łaszczewski",
21
+ "url": "https://www.viamage.com/"
22
+ },
23
+ "dependencies": {
24
+ "@live-change/framework": "0.8.97",
25
+ "@live-change/relations-plugin": "0.8.97",
26
+ "lru-cache": "^7.12.0",
27
+ "pluralize": "^8.0.0",
28
+ "progress-stream": "^2.0.0",
29
+ "prosemirror-model": "^1.18.1",
30
+ "playwright": "^1.20.2",
31
+ "p-queue": "^8.0.1",
32
+ "got": "^11.8.6"
33
+ },
34
+ "gitHead": "5e1beea2df2a168d9da8f9fa802f4340de9d75e6",
35
+ "type": "module"
36
+ }
package/print.js ADDED
@@ -0,0 +1,88 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+ import config from './config.js'
6
+
7
+ import { task } from '@live-change/task-service'
8
+
9
+ import { runWithBrowser } from './browser.js'
10
+
11
+ const authenticationKey = new ObservableValue(
12
+ config.printAuthenticationKey
13
+ )
14
+
15
+ definition.authenticator({
16
+ async prepareCredentials(credentials) {
17
+ //console.log("PRINT AUTHENTICATOR", credentials, authenticationKey.getValue())
18
+ if(credentials.sessionKey === authenticationKey.getValue()) {
19
+ credentials.roles.push('admin')
20
+ credentials.internal = true
21
+ }
22
+ }
23
+ })
24
+
25
+ const baseUrl = `http://${config.ssrHost}`+`:${config.ssrPort}`
26
+
27
+ export async function getPrintUrl(path, data) {
28
+ const encodedData = encodeURIComponent(JSON.stringify(data))
29
+ return baseUrl + path+'/' + encodedData +`?sessionKey=${await authenticationKey.getValue()}`
30
+ }
31
+
32
+ async function sleep(time) {
33
+ return new Promise(resolve => setTimeout(resolve, time))
34
+ }
35
+
36
+ const printToPdfFile = task({
37
+ name: "printToPdfFile",
38
+ properties: {
39
+ path: {
40
+ type: String
41
+ },
42
+ data: {
43
+ type: Object
44
+ },
45
+ timeout: {
46
+ type: Number
47
+ },
48
+ media: {
49
+ type: Object
50
+ },
51
+ options: {
52
+ type: Object
53
+ },
54
+ outputPath: {
55
+ type: String
56
+ }
57
+ },
58
+ async execute({ path, data, timeout, media, options, outputPath }, { task, trigger, triggerService }, emit) {
59
+ const all = 8
60
+ const url = await getPrintUrl(path, data)
61
+ task.progress(0, all, 'gettingBrowserReady')
62
+ await runWithBrowser(async browser => {
63
+ task.progress(1, all, 'creatingPage')
64
+ const page = await browser.newPage()
65
+ task.progress(2, all, 'enteringPage')
66
+ await page.goto(url)
67
+ task.progress(3, all, 'waitingForLoading')
68
+ while(true) {
69
+ const loadingTasksCount = await page.evaluate(() => api.globals.$allLoadingTasks?.length)
70
+ if(loadingTasksCount === 0) break
71
+ await sleep(100)
72
+ }
73
+ task.progress(4, all, 'waitingForNetworkIdle')
74
+ await page.waitForLoadState('networkidle')
75
+ task.progress(5, all, 'waitingForRendering')
76
+ await sleep(100) // wait for some time to give browser time to render
77
+ task.progress(6, all, 'printing')
78
+ page.emulateMedia(media ?? { media: 'print' })
79
+ const pdf = await page.pdf( options ?? { format: 'A4', printBackground: true })
80
+ task.progress(7, all, 'closingPage')
81
+ })
82
+ task.progress(9, all, 'writingPdfToFile')
83
+ await fs.promises.writeFile(outputPath, pdf)
84
+ task.progress(10, all, 'done')
85
+ return outputPath
86
+ }
87
+ })
88
+