npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hotworksdr

v0.0.0

Published

#### **screenshot-service(快照服务) 提供对外部的网络地址或者html文件内容进行快照图片或PDF生成(可响应图片文件或Base64文件字符串)**

Downloads

2

Readme

项目简介

screenshot-service(快照服务) 提供对外部的网络地址或者html文件内容进行快照图片或PDF生成(可响应图片文件或Base64文件字符串)

一 .使用技术栈

  • Nodejs(ECMAScript 6语法)
  • Express 是一个 Node.js Web 应用程序开发框架(对外提供API服务)
  • Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome
  • Chromium 无头渲染引擎,提供对html 的渲染并生成快照 或者生成PDF
  • PM2 是一个守护进程管理器 对node服务进行监控和容错重启。

二.对外提供接口

  1. 健康检测接口(GET)

    curl 127.0.0.1:80
  2. 快照生成接口(POST)

    网络地址生成快照请求
    curl -X POST http://127.0.0.1:80/screenshot -H "Content-Type:application/json" -d "{\"url\":\"http:\/\/baidu.com\"}"
       
    html文件内容生成快照
    curl -X POST http://127.0.0.1:80/screenshot -H "Content-Type:application/json" -d "{\"htmlContent\":\"<html><body><h1>测试</h1></body></html>\"}"
  3. 关闭快照服务接口(GET)

    curl 127.0.0.1:80/close

三.生成快照参数(参数都有默认值,可通过接口进行覆盖或部分覆盖)

 {
    "url": "http://baidu.com",
    "htmlContent": "<html><body><h1>测试</h1></body></html>",//htmlContent有值则会覆盖url参数
    "userAgent": "Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
    "userAgentMetadata": { // https://wicg.github.io/ua-client-hints/#user-agent-platform-architecture
        "architecture": "x86",// x86 OR ARM
        "platform": "Windows",// "Windows", "iOS", "AmazingOS" ,"Android"
        "platformVersion": "NT 10.0",//"NT 10.0", "15", "17G"
        "model": "",//"", "Pixel 2 XL","Mi Note 4"
        "mobile": false
    },
    "cacheEnabled": true, //是否开启请求缓存 默认开启
    "screenshotOptions": {
        "type": "jpeg", //webp png jpeg 默认jpeg
        "quality": 100, //图片清晰度 0-100 png格式不支持
        "fullPage": true,
        "omitBackground": false,
        "encoding": "binary", //base64 和 binary  (base64图片和二进制文件 默认binary)
        "clip": { //不传默认不裁剪
            "x": 100,
            "y": 100,
            "width": 500,
            "height ": 500,
    	}
    },
    "viewport": { //浏览器宽高 默认 1080x1622
        "width": 1080, //最大宽度3000
        "height": 1622,//最大高度3000
        "deviceScaleFactor": 1,
        "isMobile": false,
        "hasTouch": false,
        "isLandscape": false
    },
    "gotoOptions": {
        "timeout": 3000, //默认3秒
        "waitUntil": "load", //DOMContentLoaded 最快 load 第二 networkidle2 第三 networkidle0 第四
        "referer": "http://baidu.com" //请求来源
    },
    "waitPage": { //默认无等待页面加载策略
        "waitForSelector": [
            "#aging-tools-pc",
            {
                "timeout": 1000 //默认30秒
            }
        ],
        "waitForFunction": [
            {
                "timeout": 1000 //默认30秒
            },
            "let bool=!!document.querySelector('#aging-tools-pc');return bool"
        ],
        "waitForFunction": [
            "!!document.querySelector('#aging-tools-pc')"
        ]
    }
}

tips:参数枚举、用途、说明可去puppeteer官网查阅: https://github.com/puppeteer/puppeteer

中文文档官网: https://zhaoqize.github.io/puppeteer-api-zh_CN

四.项目依赖及目前使用架构

1.目前使用快照服务的业务
  1. 精彩瞬间moment模板图片生成
  2. 黄金十秒多个分享页面生成
  3. 驾驶精英多个分享页面生成
  4. 划龙舟活动分享页面生成
  5. 老司机游戏分享页面生成
2.项目架构

项目部署在中台层,可通过中台网关请求此服务

运行 build.sh 可生成docker镜像build 时环境

FROM debian:stretch-slim

RUN apt-get update \
    && apt-get install -yq net-tools telnet locales tar wget curl vim xz-utils gnupg \
    && sed -i '/^#.* zh_CN.UTF-8 /s/^#//' /etc/locale.gen \
    && sed -i '/^#.* en_US.UTF-8 /s/^#//' /etc/locale.gen \
    && locale-gen --purge \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update --fix-missing \
    && apt-get install -yq google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 --no-install-recommends \
    && apt-get clean \
    && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/* \
    && cd /tmp \
    && wget  https://nodejs.org/dist/v16.13.1/node-v16.13.1-linux-x64.tar.xz \
    && tar -xvf nodejs/node-v16.13.1-linux-x64.tar.xz -C /opt \
    && ln -sf /opt/node-v16.13.1-linux-x64 /opt/nodejs

COPY ./*.ttf /usr/share/fonts/

ENV NODE_HOME="/opt/nodejs"

ENV PATH=$PATH:$NODE_HOME/bin \
    LC_TIME="zh_CN.UTF-8" \
    LANG="zh_CN.UTF-8" \
    LANGUAGE="zh_CN.UTF-8" \
    LC_ALL="zh_CN.UTF-8"
3.项目配置参数(在环境变量中配置)
src/config/common.config.js|config.js

{
    workHome: env.WORK_HOME || sep + join("data", "screenshot-service"),
    projectDir: env.PROJECT_HOME || sep + join("data", "node", "screenshot-service"),
    screenshotsDir: os.tmpdir() + sep + join("screenshot-service") + sep,
    httpHost: env.HTTP_HOST || "0.0.0.0",
    browserCount: env.BROWSER_COUNT || 5,
    browserOptions: (env.BROWSER_OPTIONS && JSON.parse(env.BROWSER_OPTIONS)) || {
        executablePath: 'google-chrome-stable',
        headless: true
    }
}