bits-to-dead-trees 0.0.9 → 1.0.0

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/.eslintrc.json CHANGED
@@ -3,16 +3,10 @@
3
3
  "es2021": true,
4
4
  "node": true
5
5
  },
6
- "extends": [
7
- "eslint:recommended",
8
- "plugin:@typescript-eslint/recommended",
9
- "prettier"
10
- ],
11
- "parser": "@typescript-eslint/parser",
6
+ "extends": ["eslint:recommended", "prettier"],
12
7
  "parserOptions": {
13
8
  "ecmaVersion": "latest",
14
9
  "sourceType": "module"
15
10
  },
16
- "plugins": ["@typescript-eslint"],
17
11
  "rules": {}
18
12
  }
@@ -0,0 +1,11 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
7
+
8
+ - package-ecosystem: "docker"
9
+ directory: "/"
10
+ schedule:
11
+ interval: "weekly"
@@ -12,7 +12,8 @@ jobs:
12
12
  steps:
13
13
  - name: Checkout Repo
14
14
  uses: actions/checkout@v3
15
- ref: ${{ github.ref_name }}
15
+ with:
16
+ ref: ${{ github.ref_name }}
16
17
 
17
18
  - name: Login to GitHub Container Registry
18
19
  uses: docker/login-action@v1
@@ -19,8 +19,14 @@ jobs:
19
19
  node-version: lts/*
20
20
  cache: npm
21
21
 
22
+ - name: Install npm dependencies
23
+ run: npm ci
24
+
22
25
  - name: Run eslint
23
26
  run: npm run lint
24
27
 
25
28
  - name: Run prettier
26
29
  run: npm run check-format
30
+
31
+ - name: Run typecheck
32
+ run: npm run ts-check
@@ -0,0 +1,38 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ concurrency: ${{ github.workflow }}-${{ github.ref }}
9
+
10
+ jobs:
11
+ release:
12
+ name: Release
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - name: Checkout Repo
16
+ uses: actions/checkout@v3
17
+ with:
18
+ # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
19
+ fetch-depth: 0
20
+
21
+ - name: Setup Node.js
22
+ uses: actions/setup-node@v3
23
+ with:
24
+ cache: npm
25
+ node-version: lts/*
26
+
27
+ - name: Install Dependencies
28
+ run: npm install
29
+
30
+ - name: Create Release Pull Request or Publish to npm
31
+ id: changesets
32
+ uses: changesets/action@v1
33
+ with:
34
+ publish: npm run release
35
+ title: Release new version 🚀
36
+ env:
37
+ GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
38
+ NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
package/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # bits-to-dead-trees
2
2
 
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - eccf614: Remove TypeScript, use vanilla JS with JSDoc types.
8
+
9
+ This actually improves DX by not having that extra build step, while still having
10
+ good support in your IDE, since we still have the types as JSDoc.
11
+
12
+ This is the v1.0. This has been very stable and it's unlikely that the API will change
13
+ a lot. Nothing should break for users with this release.
14
+
15
+ ### Patch Changes
16
+
17
+ - 7ef95e5: Update playwright to 1.21.1
18
+ - d651f7a: Update playwright to 1.21.0
19
+
20
+ ## 0.0.11
21
+
22
+ ### Patch Changes
23
+
24
+ - a3c12a4: Wait for network idle event before creating pdf
25
+
26
+ ## 0.0.10
27
+
28
+ ### Patch Changes
29
+
30
+ - df332b7: Go back to automatic release, but use PAT
31
+
3
32
  ## 0.0.9
4
33
 
5
34
  ### Patch Changes
package/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM mcr.microsoft.com/playwright:v1.20.2-focal
1
+ FROM mcr.microsoft.com/playwright:v1.21.1-focal
2
2
 
3
3
  ENV NODE_ENV=production
4
4
  ENV SERVER_PORT=8000
@@ -11,15 +11,11 @@ RUN apt-get update -yqq && \
11
11
  apt-get clean && \
12
12
  rm -rf /var/lib/apt/lists/*
13
13
 
14
- COPY ["package.json", "package-lock.json*", "tsconfig.json", "./"]
14
+ COPY ["package.json", "package-lock.json*", "./"]
15
15
 
16
- RUN npm install --production=false --ignore-scripts
16
+ RUN npm install --production --ignore-scripts
17
17
 
18
- COPY src src
19
-
20
- RUN npm run build && \
21
- rm -Rf src && \
22
- npm install --production --ignore-scripts
18
+ COPY ["index.js", "schemas", "./"]
23
19
 
24
20
  ENTRYPOINT ["tini", "-v", "--"]
25
- CMD ["node", "dist/index.js"]
21
+ CMD ["node", "index.js"]
package/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
  Exposes a web server with a single endpoint to take in a URL and create a PDF out
6
6
  of that. It's using [Playwright](https://playwright.dev) in the background for this.
7
7
 
8
+ [![Depfu](https://badges.depfu.com/badges/74bd86ac2a33c6c6f6817cafc6419e84/overview.svg)](https://depfu.com/github/rmehner/bits-to-dead-trees?project_id=35056)
9
+
8
10
  ## Setup
9
11
 
10
12
  ### Requirements
@@ -17,8 +19,7 @@ of that. It's using [Playwright](https://playwright.dev) in the background for t
17
19
  1. `git checkout https://github.com/rmehner/bits-to-dead-trees`
18
20
  2. `cd bits-to-dead-trees`
19
21
  3. `npm install`
20
- 4. `npm run build` to build the app
21
- 5. `npm start` to start the server. It'll listen to port 8000 on localhost by default.
22
+ 4. `npm start` to start the server. It'll listen to port 8000 on localhost by default.
22
23
 
23
24
  ### Development
24
25
 
@@ -1,7 +1,18 @@
1
- import fastify from "fastify";
2
- import playwright from "playwright-core";
3
- import PdfRequestBodySchema from "./schemas/pdf_request_body.json";
4
- import { PdfOptions, PdfRequestBody } from "./types/schemas";
1
+ // @ts-check
2
+
3
+ /**
4
+ * @typedef {Parameters<import("playwright-core").Page["pdf"]>[0]} PdfOptions
5
+ */
6
+
7
+ /**
8
+ * @typedef {Object} PdfRequestBody
9
+ * @property {string} url
10
+ * @property {PdfOptions} options
11
+ */
12
+
13
+ const { fastify } = require("fastify");
14
+ const playwright = require("playwright-core");
15
+ const PdfRequestBodySchema = require("./schemas/pdf_request_body.json");
5
16
 
6
17
  const SERVER_PORT = process.env["SERVER_PORT"] ?? 8000;
7
18
  const SERVER_ADDRESS = process.env["SERVER_ADDRESS"] ?? "localhost";
@@ -18,11 +29,18 @@ const server = fastify({
18
29
  },
19
30
  });
20
31
 
21
- const createPDF = async (url: string, options?: PdfOptions) => {
32
+ /**
33
+ *
34
+ * @param {string} url
35
+ * @param {PdfOptions} options
36
+ * @returns Buffer
37
+ */
38
+ const createPDF = async (url, options = {}) => {
22
39
  const browser = await playwright.chromium.launch();
23
40
  const context = await browser.newContext({ ignoreHTTPSErrors: true });
24
41
  const page = await context.newPage();
25
- await page.goto(url);
42
+ await page.goto(url, { timeout: 60000 });
43
+ await page.waitForLoadState("networkidle");
26
44
 
27
45
  const pdf = await page.pdf(options);
28
46
  await browser.close();
@@ -30,9 +48,14 @@ const createPDF = async (url: string, options?: PdfOptions) => {
30
48
  return pdf;
31
49
  };
32
50
 
33
- server.post<{ Body: PdfRequestBody }>(
51
+ server.post(
34
52
  "/pdf",
35
53
  { schema: { body: PdfRequestBodySchema }, attachValidation: true },
54
+ /**
55
+ *
56
+ * @param {import('fastify').FastifyRequest<{Body: PdfRequestBody}>} request
57
+ * @param {import('fastify').FastifyReply} response
58
+ */
36
59
  async (request, response) => {
37
60
  if (request.validationError) {
38
61
  response.code(400);
package/nodemon.json CHANGED
@@ -1,5 +1,4 @@
1
1
  {
2
- "delay": 2000,
3
- "watch": ["src/", "package-lock.json", "package.json"],
4
- "ext": "ts,json"
2
+ "watch": ["index.js", "schemas/pdf_request_body.json", "package-lock.json"],
3
+ "ext": "js,json"
5
4
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bits-to-dead-trees",
3
3
  "author": "Robin Mehner <robin@coding-robin.de>",
4
- "version": "0.0.9",
4
+ "version": "1.0.0",
5
5
  "description": "Turn HTML into PDF files",
6
6
  "repository": "github:rmehner/bits-to-dead-trees",
7
7
  "license": "Apache-2.0",
@@ -13,39 +13,31 @@
13
13
  "engines": {
14
14
  "node": ">= 16"
15
15
  },
16
- "main": "dist/index.js",
16
+ "main": "index.js",
17
17
  "scripts": {
18
- "check-format": "prettier --check 'src/**/*.ts'",
19
- "clean": "rimraf dist",
20
- "lint": "eslint src/*.ts",
21
- "format": "prettier --write src/**/*.{ts,json}",
22
- "build": "npm run clean && tsc",
23
- "start": "node dist/index.js",
24
- "start:dev": "concurrently --kill-others \"npm run build -- --watch\" \"nodemon --inspect dist/index.js\" ",
25
- "dump-schemas": "typescript-json-schema src/types/schemas.ts PdfRequestBody --out src/schemas/pdf_request_body.json",
18
+ "check-format": "prettier --check **/*.{js,ts,json,md}",
19
+ "lint": "eslint *.js",
20
+ "format": "prettier --write **/*.{js,ts,json,md}",
21
+ "start": "node index.js",
22
+ "start:dev": "nodemon --inspect index.js",
23
+ "dump-schemas": "typescript-json-schema types/schemas.ts PdfRequestBody --out schemas/pdf_request_body.json",
26
24
  "update-schemas": "npm run dump-schemas && npm run format",
27
25
  "postinstall": "npx playwright install chromium",
28
26
  "add-changeset": "changeset",
29
27
  "release": "changeset publish",
30
- "version": "changeset version"
28
+ "ts-check": "npx tsc --noEmit"
31
29
  },
32
30
  "dependencies": {
33
31
  "fastify": "^3.28.0",
34
- "playwright-core": "^1.20.2"
32
+ "playwright-core": "^1.21.1"
35
33
  },
36
34
  "devDependencies": {
37
35
  "@changesets/cli": "^2.22.0",
38
- "@types/node": "^16",
39
- "@typescript-eslint/eslint-plugin": "^5.18.0",
40
- "@typescript-eslint/parser": "^5.18.0",
41
- "concurrently": "^7.1.0",
42
36
  "eslint": "^8.13.0",
43
37
  "eslint-config-prettier": "^8.5.0",
44
38
  "nodemon": "^2.0.15",
45
39
  "pino-pretty": "^7.6.1",
46
40
  "prettier": "^2.6.2",
47
- "rimraf": "^3.0.2",
48
- "typescript": "^4.6.3",
49
41
  "typescript-json-schema": "^0.53.0"
50
42
  }
51
43
  }
File without changes
package/tsconfig.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "compilerOptions": {
3
- /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
- "target": "ES2021",
5
- "lib": ["ES2021", "DOM"],
6
- "module": "CommonJS",
7
- "rootDir": "./src",
3
+ "lib": ["es2021"],
4
+
5
+ "strict": true,
8
6
  "moduleResolution": "node",
9
- "resolveJsonModule": true,
10
- "outDir": "./dist",
11
- "esModuleInterop": true,
7
+ "skipLibCheck": true,
12
8
  "forceConsistentCasingInFileNames": true,
13
- "strict": true
14
- },
15
- "files": ["./src/index.ts"]
9
+ "resolveJsonModule": true,
10
+
11
+ "checkJs": true
12
+ }
16
13
  }
File without changes
@@ -1,30 +0,0 @@
1
- {
2
- "[javascriptreact]": {
3
- "editor.formatOnSave": true,
4
- "editor.formatOnPaste": true
5
- },
6
- "[html]": {
7
- "editor.formatOnType": true,
8
- "editor.formatOnSave": true
9
- },
10
- "[javascript]": {
11
- "editor.formatOnSave": true,
12
- "editor.formatOnPaste": true
13
- },
14
- "[json]": {
15
- "editor.formatOnSave": true,
16
- "editor.formatOnPaste": true
17
- },
18
- "[typescript]": {
19
- "editor.formatOnSave": true,
20
- "editor.formatOnPaste": true
21
- },
22
- "[css]": {
23
- "editor.formatOnSave": true,
24
- "editor.formatOnPaste": true
25
- },
26
- "[scss]": {
27
- "editor.formatOnSave": true,
28
- "editor.formatOnPaste": true
29
- }
30
- }
package/dist/index.js DELETED
@@ -1,57 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fastify_1 = __importDefault(require("fastify"));
7
- const playwright_core_1 = __importDefault(require("playwright-core"));
8
- const pdf_request_body_json_1 = __importDefault(require("./schemas/pdf_request_body.json"));
9
- const SERVER_PORT = process.env["SERVER_PORT"] ?? 8000;
10
- const server = (0, fastify_1.default)({
11
- logger: {
12
- prettyPrint: process.env["NODE_ENV"] !== "production"
13
- ? {
14
- translateTime: "HH:MM:ss Z",
15
- ignore: "pid,hostname",
16
- }
17
- : false,
18
- },
19
- });
20
- const createPDF = async (url, options) => {
21
- const browser = await playwright_core_1.default.chromium.launch();
22
- const context = await browser.newContext({ ignoreHTTPSErrors: true });
23
- const page = await context.newPage();
24
- await page.goto(url);
25
- const pdf = await page.pdf(options);
26
- await browser.close();
27
- return pdf;
28
- };
29
- server.post("/pdf", { schema: { body: pdf_request_body_json_1.default }, attachValidation: true }, async (request, response) => {
30
- if (request.validationError) {
31
- response.code(400);
32
- return {
33
- error: true,
34
- message: request.validationError.message,
35
- };
36
- }
37
- try {
38
- return createPDF(request.body.url, request.body.options);
39
- }
40
- catch (error) {
41
- response.code(500);
42
- return {
43
- error,
44
- };
45
- }
46
- });
47
- server.get("/health", async (_, response) => response.code(200).send());
48
- const startServer = async () => {
49
- try {
50
- await server.listen(+SERVER_PORT, "0.0.0.0");
51
- }
52
- catch (error) {
53
- console.error(error);
54
- process.exit(1);
55
- }
56
- };
57
- startServer();
@@ -1,84 +0,0 @@
1
- {
2
- "$schema": "http://json-schema.org/draft-07/schema#",
3
- "properties": {
4
- "options": {
5
- "properties": {
6
- "displayHeaderFooter": {
7
- "description": "Display header and footer. Defaults to `false`.",
8
- "type": "boolean"
9
- },
10
- "footerTemplate": {
11
- "description": "HTML template for the print footer. Should use the same format as the `headerTemplate`.",
12
- "type": "string"
13
- },
14
- "format": {
15
- "description": "Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'.",
16
- "type": "string"
17
- },
18
- "headerTemplate": {
19
- "description": "HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values\ninto them:\n- `'date'` formatted print date\n- `'title'` document title\n- `'url'` document location\n- `'pageNumber'` current page number\n- `'totalPages'` total pages in the document",
20
- "type": "string"
21
- },
22
- "height": {
23
- "description": "Paper height, accepts values labeled with units.",
24
- "type": ["string", "number"]
25
- },
26
- "landscape": {
27
- "description": "Paper orientation. Defaults to `false`.",
28
- "type": "boolean"
29
- },
30
- "margin": {
31
- "description": "Paper margins, defaults to none.",
32
- "properties": {
33
- "bottom": {
34
- "description": "Bottom margin, accepts values labeled with units. Defaults to `0`.",
35
- "type": ["string", "number"]
36
- },
37
- "left": {
38
- "description": "Left margin, accepts values labeled with units. Defaults to `0`.",
39
- "type": ["string", "number"]
40
- },
41
- "right": {
42
- "description": "Right margin, accepts values labeled with units. Defaults to `0`.",
43
- "type": ["string", "number"]
44
- },
45
- "top": {
46
- "description": "Top margin, accepts values labeled with units. Defaults to `0`.",
47
- "type": ["string", "number"]
48
- }
49
- },
50
- "type": "object"
51
- },
52
- "pageRanges": {
53
- "description": "Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.",
54
- "type": "string"
55
- },
56
- "path": {
57
- "description": "The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to the current working\ndirectory. If no path is provided, the PDF won't be saved to the disk.",
58
- "type": "string"
59
- },
60
- "preferCSSPageSize": {
61
- "description": "Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format`\noptions. Defaults to `false`, which will scale the content to fit the paper size.",
62
- "type": "boolean"
63
- },
64
- "printBackground": {
65
- "description": "Print background graphics. Defaults to `false`.",
66
- "type": "boolean"
67
- },
68
- "scale": {
69
- "description": "Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.",
70
- "type": "number"
71
- },
72
- "width": {
73
- "description": "Paper width, accepts values labeled with units.",
74
- "type": ["string", "number"]
75
- }
76
- },
77
- "type": "object"
78
- },
79
- "url": {
80
- "type": "string"
81
- }
82
- },
83
- "type": "object"
84
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });