diff --git a/card.js b/card.ts similarity index 57% rename from card.js rename to card.ts index 653238c..180082a 100644 --- a/card.js +++ b/card.ts @@ -1,19 +1,14 @@ -const { parseRequest } = require('./parser'); -const { getScreenshot } = require('./chromium'); -const { getHtml } = require('./template'); -const { writeTempFile, pathToFileURL } = require('./file'); +import { IncomingMessage, ServerResponse } from 'http'; +import { parseRequest } from './parser'; +import { getScreenshot } from './chromium'; +import { getHtml } from './template'; +import { writeTempFile, pathToFileURL } from './file'; -async function handler(req, res) { +async function handler(req: IncomingMessage, res: ServerResponse) { try { - let { - type = 'png', - text = 'Hello', - fontWeight = 'bold', - image = 'now-black', - } = parseRequest(req); - const name = decodeURIComponent(text); - const html = getHtml(name, fontWeight, image); - const filePath = await writeTempFile(name, html); + const { type, text, fontWeight, images } = parseRequest(req); + const html = getHtml(text, fontWeight, images); + const filePath = await writeTempFile(text, html); const fileUrl = pathToFileURL(filePath); const file = await getScreenshot(fileUrl, type); res.statusCode = 200; diff --git a/chromium.js b/chromium.ts similarity index 58% rename from chromium.js rename to chromium.ts index 870a1c7..45f0e59 100644 --- a/chromium.js +++ b/chromium.ts @@ -1,8 +1,9 @@ -const chrome = require('chrome-aws-lambda'); -const puppeteer = require('puppeteer-core'); +import * as chromeAwsLambda from 'chrome-aws-lambda'; +import { launch } from 'puppeteer-core'; +const chrome = chromeAwsLambda as any; -async function getScreenshot(url, type) { - const browser = await puppeteer.launch({ +export async function getScreenshot(url: string, type: ScreenshotType) { + const browser = await launch({ args: chrome.args, executablePath: await chrome.executablePath, headless: chrome.headless, @@ -14,6 +15,4 @@ async function getScreenshot(url, type) { const file = await page.screenshot({ type }); await browser.close(); return file; -} - -module.exports = { getScreenshot }; \ No newline at end of file +} \ No newline at end of file diff --git a/file.js b/file.ts similarity index 51% rename from file.js rename to file.ts index 94833ed..73b9ebd 100644 --- a/file.js +++ b/file.ts @@ -1,21 +1,19 @@ -const { writeFile } = require('fs'); -const { join } = require('path'); -const { promisify } = require('util'); +import { writeFile } from 'fs'; +import { join } from 'path'; +import { promisify } from 'util'; +import { tmpdir } from 'os'; +import { URL } from 'url'; const writeFileAsync = promisify(writeFile); -const { tmpdir } = require('os'); -const { URL } = require('url'); -async function writeTempFile(name, contents) { +export async function writeTempFile(name: string, contents: string) { const randomPath = join(tmpdir(), `${name}.html`); console.log('Writing file to ' + randomPath); await writeFileAsync(randomPath, contents); return randomPath; } -function pathToFileURL(path) { +export function pathToFileURL(path: string) { const { href } = new URL(path, 'file:'); console.log('File url is ' + href); return href; -} - -module.exports = { writeTempFile, pathToFileURL } \ No newline at end of file +} \ No newline at end of file diff --git a/now.json b/now.json index a498354..b1ebe60 100644 --- a/now.json +++ b/now.json @@ -3,9 +3,9 @@ "alias": "og-image.now.sh", "version": 2, "builds": [ - { "src": "card.js", "use": "@now/node", "config": { "maxLambdaSize": "40mb" } } + { "src": "card.ts", "use": "@now/node@canary", "config": { "maxLambdaSize": "40mb" } } ], "routes": [ - { "src": "/(.*)", "dest": "/card.js" } + { "src": "/(.*)", "dest": "/card.ts" } ] } \ No newline at end of file diff --git a/package.json b/package.json index bdc8208..22cb89f 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,9 @@ "dependencies": { "chrome-aws-lambda": "1.11.1", "puppeteer-core": "1.11.0" + }, + "devDependencies": { + "@types/puppeteer-core": "^1.9.0", + "typescript": "^3.2.4" } } diff --git a/parser.js b/parser.js deleted file mode 100644 index cfe9261..0000000 --- a/parser.js +++ /dev/null @@ -1,13 +0,0 @@ -const { parse } = require('url'); - -function parseRequest(req) { - const { pathname = '/', query = {} } = parse(req.url, true); - const { fontWeight, image } = query; - console.log('Hit ' + pathname, query); - const arr = pathname.slice(1).split('.'); - const type = arr.pop(); - const text = arr.join('.'); - return { type, text, fontWeight, image }; -} - -module.exports = { parseRequest } \ No newline at end of file diff --git a/parser.ts b/parser.ts new file mode 100644 index 0000000..85a5313 --- /dev/null +++ b/parser.ts @@ -0,0 +1,30 @@ +import { IncomingMessage } from 'http'; +import { parse } from 'url'; + +interface ParsedRequest { + type: ScreenshotType; + text: string; + fontWeight: FontWeight; + images: string[]; +}; + +export function parseRequest(req: IncomingMessage) { + console.log('HTTP ' + req.url); + const { pathname = '/', query = {} } = parse(req.url || '', true); + const { fontWeight, images } = query; + if (Array.isArray(fontWeight)) { + throw new Error('Expected a single fontWeight'); + } + const arr = pathname.slice(1).split('.'); + const type = arr.pop(); + const text = arr.join('.'); + const parsedRequest: ParsedRequest = { + type: type as ScreenshotType, + text: decodeURIComponent(text), + fontWeight: fontWeight as FontWeight, + images: Array.isArray(images) && images.length > 0 + ? images + : ['https://assets.zeit.co/image/upload/front/assets/design/now-black.svg'], + }; + return parsedRequest; +} \ No newline at end of file diff --git a/template.js b/template.ts similarity index 71% rename from template.js rename to template.ts index c81521f..f326304 100644 --- a/template.js +++ b/template.ts @@ -1,11 +1,7 @@ -const { readFileSync } = require('fs'); +import { readFileSync } from 'fs'; -/** - * - * @param {'bold' | 'normal'} fontWeight - */ -function getCss(fontWeight) { +function getCss(fontWeight: FontWeight) { const regular = `${__dirname}/fonts/Inter-UI-Regular.woff2`; const bold = `${__dirname}/fonts/Inter-UI-Bold.woff2`; const buffer = readFileSync(fontWeight === 'bold' ? bold : regular); @@ -46,14 +42,7 @@ function getCss(fontWeight) { }`; } -/** - * - * @param {string} text - * @param {'bold' | 'normal'} fontWeight - * @param {'now-black' | 'now-white' | 'zeit-black-triangle' | 'zeit-white-triangle'} image - */ -function getHtml(text, fontWeight, image) { - const logo = `https://assets.zeit.co/image/upload/front/assets/design/${image}.svg`; +export function getHtml(text: string, fontWeight: FontWeight, images: string[]) { return `