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
1 change: 0 additions & 1 deletion .astro/types.d.ts

This file was deleted.

78 changes: 70 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
name: Build
name: Release

on: [push, pull_request]
on:
workflow_run:
workflows: ['Check']
branches: [main]
types:
- completed

permissions:
contents: write
pull-requests: write

jobs:
build:
release-please:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
timeout-minutes: 1
outputs:
release_created: ${{ steps.release.outputs.release_created }}
tag_name: ${{ steps.release.outputs.tag_name }}

steps:
- id: release
uses: googleapis/release-please-action@v4
with:
release-type: simple

upload-artifact:
runs-on: ubuntu-latest
needs: release-please
if: ${{ needs.release-please.outputs.release_created }}
timeout-minutes: 5

strategy:
matrix:
node-version: [18.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v4
Expand All @@ -22,8 +48,44 @@ jobs:
corepack enable
yarn

- name: lint
run: yarn lint
- name: Build `lambda` function
run: |
yarn build
echo '{"type": "module"}' > dist/package.json
zip -rj lambda.zip dist

- name: Build `chromium` layer
run: |
mkdir work-chromium
cd work-chromium
git clone --depth=1 https://github.com/sparticuz/chromium.git
cd chromium
make chromium.zip
mv chromium.zip ../../

- name: Build `libs` layer
run: |
mkdir nodejs
rm -rf node_modules
yarn workspaces focus --production
cp -r node_modules nodejs
zip -r libs.zip nodejs

- name: Build `fonts` layer
run: |
mkdir work-fonts
cd work-fonts
git clone --depth=1 git@github.com:dev-protocol/stackroom.git
cd stackroom/fonts
mv IBM_Plex_Sans_JP/* ./
rm -rf IBM_Plex_Sans_JP
mv Noto_Color_Emoji/* ./
rm -rf Noto_Color_Emoji
cd ../
zip -r fonts.zip fonts
mv fonts.zip ../../

- name: test
run: yarn test
- name: Upload Artifact
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ needs.release-please.outputs.tag_name }} lambda.zip chromium.zip libs.zip fonts.zip
26 changes: 26 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Check

on: [push, pull_request]

jobs:
check:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: install deps
run: |
corepack enable
yarn

- name: lint
run: yarn lint
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ package-lock.json

# astro
**/.astro

# builds
*.zip
28 changes: 8 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
# template-repos-ts

Template repository for using TypeScript

# Stack

- npm client is [yarn](https://github.com/yarnpkg/yarn)
- Testing is [vitest](https://github.com/vitest-dev/vitest)
- Linting is [eslint](https://github.com/eslint/eslint)
- Basic lint rule set is [eslint-plugin-functional](https://github.com/jonaskello/eslint-plugin-functional)
- Formatter is [prettier](https://github.com/prettier/prettier)
- Pre-install utility is [ramda](https://github.com/ramda/ramda)

# Usage

Create a repository using this template; just runs following command.

```bash
yarn
```
### How to release

1. Download all zip files from a release.
2. Upload all zips to s3://devprotocol-clubs/clubs/\_lambda-layers/place/capture and override them.
3. Access Lambda/Layers/chromium and click `Create version` -> `Upload a file from Amazon S3` and input `https://devprotocol-clubs.s3.us-east-1.amazonaws.com/clubs/_lambda-layers/place/capture/chromium.zip`, set Compatible architectures = x86_64, set Compatible runtimes == Node.js 22.x + Node.js 20.x, then create.
4. Access Lambda/Layers/libs and click `Create version` -> `Upload a file from Amazon S3` and input `https://devprotocol-clubs.s3.us-east-1.amazonaws.com/clubs/_lambda-layers/place/capture/libs.zip`, set Compatible architectures = x86_64, set Compatible runtimes == Node.js 22.x + Node.js 20.x, then create.
5. Access Lambda/Layers/fonts and click `Create version` -> `Upload a file from Amazon S3` and input `https://devprotocol-clubs.s3.us-east-1.amazonaws.com/clubs/_lambda-layers/place/capture/fonts.zip`, set Compatible architectures = x86_64, set Compatible runtimes == Node.js 22.x + Node.js 20.x, then create.
6. Access Lambda/Functions/capture-screenshots and click `Upload from` -> `Amazon S3 location` and input `https://devprotocol-clubs.s3.us-east-1.amazonaws.com/clubs/_lambda-layers/place/capture/lambda.zip`, and edit layers to use all of the latest versions,then publish new version on the console.
13 changes: 0 additions & 13 deletions astro.config.mjs

This file was deleted.

1 change: 1 addition & 0 deletions chromium/chromium
Submodule chromium added at cc370b
6 changes: 0 additions & 6 deletions example/index.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion example/index.ts

This file was deleted.

23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"description": "Template repository for using TypeScript",
"type": "module",
"scripts": {
"test": "vitest",
"build": "astro build",
"dev": "astro dev",
"build": "yarn prebuild && rollup -c",
"prebuild": "rimraf dist",
"lint": "yarn lint:eslint && yarn lint:format",
"lint:eslint": "eslint . --fix",
Expand All @@ -16,21 +14,22 @@
"author": "Dev Protocol",
"license": "MPL-2.0",
"dependencies": {
"@astrojs/netlify": "^6.1.0",
"@astrojs/vercel": "8.0.4",
"@devprotocol/util-ts": "^4.0.0",
"@sparticuz/chromium-min": "^130.0.0",
"astro": "5.1.10",
"puppeteer-core": "23.6.0",
"ramda": "0.30.1"
"@sparticuz/chromium": "^132.0.0",
"aws-lambda": "^1.0.7",
"puppeteer-core": "23.6.0"
},
"devDependencies": {
"@eslint/js": "^9.12.0",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-typescript": "^12.1.2",
"@types/aws-lambda": "^8.10.147",
"@types/dotenv": "8.2.3",
"@types/eslint-config-prettier": "^6.11.3",
"@types/eslint__js": "^8.42.3",
"@types/node": "22.7.9",
"@types/ramda": "0.30.2",
"dotenv": "16.4.5",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -39,9 +38,9 @@
"husky": "9.1.6",
"prettier": "3.3.3",
"rimraf": "6.0.1",
"rollup": "^4.32.1",
"typescript": "5.6.3",
"typescript-eslint": "^8.9.0",
"vitest": "^2.1.3"
"typescript-eslint": "^8.9.0"
},
"repository": "git+https://github.com/dev-protocol/template-repos-ts.git",
"bugs": {
Expand Down
18 changes: 18 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import packageJson from './package.json' with { type: 'json' }

export default {
external: [
...Object.keys(packageJson.dependencies),
...Object.keys(packageJson.devDependencies),
],
input: 'src/index.ts',
output: {
file: 'dist/index.js',
format: 'es',
},
plugins: [typescript(), commonjs(), nodeResolve(), json()],
}
2 changes: 0 additions & 2 deletions src/env.d.ts

This file was deleted.

40 changes: 20 additions & 20 deletions src/pages/api/generate.ts → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable functional/no-expression-statements */
import { type APIRoute } from 'astro'
import chromium from '@sparticuz/chromium-min'
import { Chromium } from '../../libs/chromium.ts'
import type { Handler } from 'aws-lambda'
import type { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda'
import chromium from '@sparticuz/chromium'
import { Chromium } from './libs/chromium.js'
import type { PuppeteerLifeCycleEvent } from 'puppeteer-core'

const exePath =
Expand All @@ -11,24 +12,16 @@
? '/usr/bin/google-chrome'
: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

const chromiumPack =
'https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar'

const Localoptions = {
args: [],
executablePath: exePath,
headless: true,
}
await chromium.font(
'https://raw.githack.com/dev-protocol/stackroom/main/fonts/IBM_Plex_Sans_JP/IBMPlexSansJP-Medium.ttf',
)
await chromium.font(
'https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf',
)
const serverOptions = {
args: chromium.args,
executablePath: await chromium.executablePath(chromiumPack),
executablePath: await chromium.executablePath(),
headless: true,
ignoreHTTPSErrors: true,
}
const puppeteerLifeCycleEvents = [
'load',
Expand All @@ -37,7 +30,10 @@
'networkidle2',
] satisfies PuppeteerLifeCycleEvent[]

export const GET: APIRoute = async ({ url }) => {
export const handler: Handler = async (
event: APIGatewayEvent,

Check warning on line 34 in src/index.ts

View workflow job for this annotation

GitHub Actions / check (20.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")

Check warning on line 34 in src/index.ts

View workflow job for this annotation

GitHub Actions / check (20.x)

Parameter should have an immutability of at least "ReadonlyShallow" (actual: "Mutable")
): Promise<APIGatewayProxyResult> => {
const url = new URL(event.path, 'http://localhost')
const { isDev, height, width, cacheControl, gotoThenWaitUntil } = {
isDev: url.searchParams.get('dev') === 'true',
height: url.searchParams.get('h'),
Expand All @@ -52,12 +48,12 @@
const targetUrl = url.searchParams.get('src')

if (!targetUrl) {
return new Response('URL query parameter is required', { status: 400 })
return { statusCode: 404, body: 'URL query parameter is required' }
}

// Validate that the URL starts with http or https
if (!targetUrl.startsWith('http://') && !targetUrl.startsWith('https://')) {
return new Response('Invalid URL format', { status: 400 })
return { statusCode: 400, body: 'Invalid URL format' }
}

const browser = await Chromium.getInstance(options)
Expand Down Expand Up @@ -96,16 +92,20 @@
// await browser.close()

// Return the PNG image as the response
return new Response(file, {
status: 200,
return {
statusCode: 200,
body: Buffer.from(file).toString('base64'),
headers: {
'Content-Type': 'image/png',
'access-control-allow-origin': '*',
'cache-control': cacheControl ?? `public, max-age=31536000`,
},
})
} satisfies APIGatewayProxyResult
} catch (error) {
console.error('Error capturing screenshot:', error)
return new Response('Internal Server Error', { status: 500 })
return {
statusCode: 500,
body: 'Internal Server Error',
}
}
}
16 changes: 0 additions & 16 deletions src/pages/index.astro

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": "astro/tsconfigs/strict",
"compilerOptions": {
"module": "nodenext",
"lib": ["esnext"],
Expand Down
8 changes: 0 additions & 8 deletions vercel.json

This file was deleted.

4 changes: 0 additions & 4 deletions vitest.config.ts

This file was deleted.

Loading
Loading