Skip to content

Commit

Permalink
Merge pull request #16 from sibbl/fixes
Browse files Browse the repository at this point in the history
Compatibility fixes
  • Loading branch information
sibbl authored Nov 9, 2021
2 parents 28df568 + b243798 commit 81b5d2f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 106 deletions.
31 changes: 19 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@ name: ci

on:
push:
branches: main
branches:
- main

jobs:
multi:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2
-
name: Set up QEMU
- name: Extract package version
run: node -p -e '`PACKAGE_VERSION=${require("./package.json").version}`' >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to DockerHub
uses: docker/login-action@v1
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
- name: Build and push to Docker
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: true
tags: |
sibbl/hass-lovelace-kindle-screensaver:${{ env.PACKAGE_VERSION }},
sibbl/hass-lovelace-kindle-screensaver:latest
- name: Tag git commit
uses: pkgdeps/git-tag-action@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_repo: ${{ github.repository }}
version: ${{ env.PACKAGE_VERSION }}
git_commit_sha: ${{ github.sha }}
git_tag_prefix: "v"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12-alpine
FROM node:16-alpine

WORKDIR /app

Expand Down
36 changes: 23 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const gm = require("gm");
headless: config.debug !== true,
});

console.log("Adding authentication entry to browser's local storage...");
console.log(`Visiting '${config.baseUrl}' to login...`);
let page = await browser.newPage();
await page.goto(config.baseUrl, {
timeout: config.renderingTimeout,
Expand All @@ -43,6 +43,7 @@ const gm = require("gm");
token_type: "Bearer",
};

console.log("Adding authentication entry to browser's local storage...");
await page.evaluate(
(hassTokens, selectedLanguage) => {
localStorage.setItem("hassTokens", hassTokens);
Expand Down Expand Up @@ -74,19 +75,27 @@ const gm = require("gm");
pageNumberStr === "/" ? 1 : parseInt(pageNumberStr.substr(1));
if (
isFinite(pageNumber) === false ||
pageNumber.length > config.pages.length ||
pageNumber > config.pages.length ||
pageNumber < 1
) {
console.error("Invalid page requested: " + pageNumber);
console.error(`Invalid request to ${pageNumberStr}`);
response.writeHead(400);
response.end("Invalid page");
response.end("Invalid request");
return;
}
try {
const data = await fs.readFile(config.pages[pageNumber - 1].outputPath);
console.log(`Image ${pageNumber} was accessed`);
response.setHeader("Content-Length", Buffer.byteLength(data));
response.writeHead(200, { "Content-Type": "image/png" });

const data = await fs.readFile(config.pages[pageNumber - 1].outputPath);
const stat = await fs.stat(config.pages[pageNumber - 1].outputPath);

const lastModifiedTime = new Date(stat.mtime).toUTCString();

response.writeHead(200, {
"Content-Type": "image/png",
"Content-Length": Buffer.byteLength(data),
"Last-Modified": lastModifiedTime,
});
response.end(data);
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -149,11 +158,17 @@ async function renderUrlToImageAsync(browser, pageConfig, url, path) {
}

await page.setViewport(size);
const startTime = new Date().valueOf();
await page.goto(url, {
waitUntil: ["domcontentloaded", "load", "networkidle0"],
timeout: config.renderingTimeout,
});

const navigateTimespan = new Date().valueOf() - startTime;
await page.waitForSelector("home-assistant", {
timeout: Math.max(config.renderingTimeout - navigateTimespan, 1000),
});

await page.addStyleTag({
content: `
body {
Expand All @@ -166,7 +181,7 @@ async function renderUrlToImageAsync(browser, pageConfig, url, path) {
});

if (pageConfig.renderingDelay > 0) {
await delay(pageConfig.renderingDelay);
await page.waitForTimeout(pageConfig.renderingDelay);
}
await page.screenshot({
path,
Expand Down Expand Up @@ -208,8 +223,3 @@ function convertImageToKindleCompatiblePngAsync(
});
});
}
function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
Loading

0 comments on commit 81b5d2f

Please sign in to comment.