Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Check the service with these examples (in the dev environment, replace 5607 with
- [fallback to standard screenshot with custom filename](http://localhost:5607/api/v1/screenshot?key=capture&type=gif&filename=test.gif&target=http://localhost:5607/test/resources/test1.html)
- [remote url with a webgl based map](http://localhost:5617/api/v1/print?key=capture&target=https://staging-koumoul.com/cadastre/parcel?id=56251000AV0142)

## Logging

Logs go through the logger shared by the data-fair stack ([`@data-fair/lib-node/log.js`](https://github.com/data-fair/lib/tree/main/packages/node#logging)): one JSON object per line on stdout when `NODE_ENV=production` (the docker image), pretty output otherwise, `LOG_FORMAT=json|pretty` overrides. Warn, alert and error lines are counted in the `df_logs{level, code, host}` metric. `DEBUG=capture,timer` adds the debug lines of the capture steps and their durations.

## Security

Consider reading this article https://github.com/Zenika/alpine-chrome#3-ways-to-securely-use-chrome-headless-with-this-image
Expand Down
9 changes: 5 additions & 4 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import log from '@data-fair/lib-node/log.js'
import { start, stop } from './server.ts'

start().then(() => {}, err => {
console.error('Failure while starting service', err)
log.error('failure while starting the service', { code: 'start-process', err })
process.exit(1)
})

process.on('SIGTERM', function onSigterm () {
console.info('Received SIGTERM signal, shutdown gracefully...')
log.info('received SIGTERM signal, shutting down gracefully...')
stop().then(() => {
console.log('shutting down now')
log.info('shutting down now')
process.exit()
}, err => {
console.error('Failure while stopping service', err)
log.error('failure while stopping the service', { code: 'stop-process', err })
process.exit(1)
})
})
7 changes: 4 additions & 3 deletions api/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServer } from 'node:http'
import { session } from '@data-fair/lib-express/index.js'
import { startObserver, stopObserver } from '@data-fair/lib-node/observer.js'
import log from '@data-fair/lib-node/log.js'
import eventPromise from '@data-fair/lib-utils/event-promise.js'
import { createHttpTerminator } from 'http-terminator'
import app from './app.ts'
Expand All @@ -22,15 +23,15 @@ export const start = async () => {
await pageUtils.start()
if (config.observer.active) await startObserver(config.observer.port)
if (config.privateDirectoryUrl) {
console.log('Configure with Simple Directory authentication')
log.info('configured with simple-directory authentication')
session.init(config.privateDirectoryUrl)
} else {
console.log('Configure without authentication, this instance of @data-fair/capture should not be exposed publicly')
log.warn('configured without authentication, this instance of @data-fair/capture should not be exposed publicly', { code: 'config-no-auth' })
}
server.listen(config.port)
await eventPromise(server, 'listening')

console.log(`API server listening on port ${config.port}`)
log.info(`API server listening on port ${config.port}`)
}

export const stop = async () => {
Expand Down
4 changes: 3 additions & 1 deletion api/utils/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { pipeline } from 'node:stream/promises'
import GifEncoder from 'gif-encoder'
import getPixelsCb from 'get-pixels'
import { optimizeGif } from './gifsicle.ts'
import debug from 'debug'
import debugModule from 'debug'
import { type Page } from 'puppeteer'

const debug = debugModule('capture')

const getPixels = promisify(getPixelsCb)

export const capture = async (target: string, page: Page, width: number, height: number) => {
Expand Down
13 changes: 7 additions & 6 deletions api/utils/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import puppeteer, { type Viewport, type Browser, type BrowserContext, type Page,
import genericPool, { type Factory, type Pool } from 'generic-pool'
import { contextsCleanups } from './metrics.ts'
import debugModule from 'debug'
import log from '@data-fair/lib-node/log.js'
import { Timer } from './timer.ts'
import { httpError } from '@data-fair/lib-express'

Expand All @@ -19,7 +20,7 @@ export function contextPool () {
}

export const start = async () => {
console.log('init puppeteer browser', config.puppeteerLaunchOptions)
log.info('init puppeteer browser', { launchOptions: config.puppeteerLaunchOptions })
_browser = await puppeteer.launch(config.puppeteerLaunchOptions)
const contextFactory: Factory<BrowserContext> = {
async create () {
Expand All @@ -36,14 +37,14 @@ export const start = async () => {
// auto reconnection, cf https://github.com/GoogleChrome/puppeteer/issues/4428
_browser.on('disconnected', async () => {
if (!_closed) {
console.log('Browser was disconnected for some reason, reconnect')
log.error('browser was disconnected, reconnect', { code: 'browser-disconnected' })
try {
await _contextPool.drain()
_contextPool.clear()
} catch (err) {
console.log('Error while draining replaced contexts pool', err)
log.error('failure while draining the replaced contexts pool', { code: 'contexts-pool-drain', err })
}
exports.start()
start().catch(err => log.error('failure while restarting the browser', { code: 'browser-restart', err }))
}
})
}
Expand All @@ -67,7 +68,7 @@ const debugPage = (page: Page) => {
}
if (args.length) debug(`[${page.url()}] console.${msg.type()}: `, ...args)
} catch (err) {
console.error(`[${page.url()}] failed to parse console message: ${msg.text()}`, err)
log.warn('failed to parse a console message of a captured page', { code: 'page-console-parse', url: page.url(), err })
}
})
page.on('pageerror', err => {
Expand Down Expand Up @@ -202,7 +203,7 @@ const safeCleanContext = async (page: Page, openSuccess: boolean) => {
await _contextPool.release(page.browserContext())
contextsCleanups.inc()
} catch (err) {
console.error('Failed to clean page properly, do not reuse this context', err)
log.warn('failed to clean page properly, do not reuse this context', { code: 'page-clean', err })
await _contextPool.destroy(page.browserContext())
}
} else {
Expand Down
7 changes: 6 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ import dfLibRecommended from '@data-fair/lib-utils/eslint/recommended.js'
export default [
{ ignores: ['**/.type/'] },
...dfLibRecommended,
...neostandard({ ts: true })
...neostandard({ ts: true }),
{
// server code logs through @data-fair/lib-node/log.js (its README, "Logging")
files: ['api/**/*.ts'],
rules: { 'no-console': 'error' }
}
]
Loading
Loading