Skip to content

Commit 8fa201a

Browse files
committed
feat(examples): Add Playwright on Node example
Add a simple screenshot example working with Playwright on Node. Signed-off-by: Razvan Deaconescu <[email protected]>
1 parent 51dd652 commit 8fa201a

File tree

9 files changed

+411
-0
lines changed

9 files changed

+411
-0
lines changed

node-playwright/.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules/
2+
/.unikraft/
3+
/*.png

node-playwright/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules/
2+
/.unikraft/
3+
/*.png

node-playwright/Dockerfile

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
FROM debian:bookworm AS build
2+
3+
ARG NODE_VERSION=22.8.0
4+
5+
RUN set -xe; \
6+
apt-get -yqq update; \
7+
apt-get -yqq install \
8+
libcups2 \
9+
libnss3 \
10+
libatk1.0-0 \
11+
libnspr4 \
12+
libpango1.0-0 \
13+
libasound2 \
14+
libatspi2.0-0 \
15+
libxdamage1 \
16+
libatk-bridge2.0-0 \
17+
libxkbcommon0 \
18+
libdrm2 \
19+
libxcomposite1 \
20+
libxfixes3 \
21+
libxrandr2 \
22+
libgbm1; \
23+
apt-get -yqq install \
24+
curl \
25+
build-essential \
26+
libssl-dev \
27+
git \
28+
;
29+
30+
RUN set -xe; \
31+
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash; \
32+
. ~/.bashrc; \
33+
nvm install ${NODE_VERSION} \
34+
;
35+
36+
WORKDIR /app
37+
COPY package* .
38+
39+
RUN set -xe; \
40+
. ~/.bashrc; \
41+
npm install \
42+
;
43+
44+
RUN mkdir /home/tmp
45+
46+
FROM scratch
47+
48+
ARG NODE_VERSION=22.8.0
49+
50+
# Create required directories.
51+
COPY --from=build /home/tmp /tmp
52+
53+
# Chrome binary
54+
COPY --from=build /root/.cache/ms-playwright /root/.cache/ms-playwright
55+
56+
# Chrome libraries
57+
COPY --from=build /lib/x86_64-linux-gnu/libdl.so.2 \
58+
/lib/x86_64-linux-gnu/libpthread.so.0 \
59+
/lib/x86_64-linux-gnu/libgobject-2.0.so.0 \
60+
/lib/x86_64-linux-gnu/libglib-2.0.so.0 \
61+
/lib/x86_64-linux-gnu/libnss3.so \
62+
/lib/x86_64-linux-gnu/libnssutil3.so \
63+
/lib/x86_64-linux-gnu/libsmime3.so \
64+
/lib/x86_64-linux-gnu/libnspr4.so \
65+
/lib/x86_64-linux-gnu/libatk-1.0.so.0 \
66+
/lib/x86_64-linux-gnu/libatk-bridge-2.0.so.0 \
67+
/lib/x86_64-linux-gnu/libcups.so.2 \
68+
/lib/x86_64-linux-gnu/libgio-2.0.so.0 \
69+
/lib/x86_64-linux-gnu/libdrm.so.2 \
70+
/lib/x86_64-linux-gnu/libdbus-1.so.3 \
71+
/lib/x86_64-linux-gnu/libexpat.so.1 \
72+
/lib/x86_64-linux-gnu/libxcb.so.1 \
73+
/lib/x86_64-linux-gnu/libxkbcommon.so.0 \
74+
/lib/x86_64-linux-gnu/libatspi.so.0 \
75+
/lib/x86_64-linux-gnu/libm.so.6 \
76+
/lib/x86_64-linux-gnu/libX11.so.6 \
77+
/lib/x86_64-linux-gnu/libXcomposite.so.1 \
78+
/lib/x86_64-linux-gnu/libXdamage.so.1 \
79+
/lib/x86_64-linux-gnu/libXext.so.6 \
80+
/lib/x86_64-linux-gnu/libXfixes.so.3 \
81+
/lib/x86_64-linux-gnu/libXrandr.so.2 \
82+
/lib/x86_64-linux-gnu/libgbm.so.1 \
83+
/lib/x86_64-linux-gnu/libpango-1.0.so.0 \
84+
/lib/x86_64-linux-gnu/libcairo.so.2 \
85+
/lib/x86_64-linux-gnu/libasound.so.2 \
86+
/lib/x86_64-linux-gnu/libgcc_s.so.1 \
87+
/lib/x86_64-linux-gnu/libc.so.6 \
88+
/lib/x86_64-linux-gnu/libffi.so.8 \
89+
/lib/x86_64-linux-gnu/libpcre2-8.so.0 \
90+
/lib/x86_64-linux-gnu/libplc4.so \
91+
/lib/x86_64-linux-gnu/libplds4.so \
92+
/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 \
93+
/lib/x86_64-linux-gnu/libavahi-common.so.3 \
94+
/lib/x86_64-linux-gnu/libavahi-client.so.3 \
95+
/lib/x86_64-linux-gnu/libgnutls.so.30 \
96+
/lib/x86_64-linux-gnu/libz.so.1 \
97+
/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 \
98+
/lib/x86_64-linux-gnu/libmount.so.1 \
99+
/lib/x86_64-linux-gnu/libselinux.so.1 \
100+
/lib/x86_64-linux-gnu/libsystemd.so.0 \
101+
/lib/x86_64-linux-gnu/libXau.so.6 \
102+
/lib/x86_64-linux-gnu/libXdmcp.so.6 \
103+
/lib/x86_64-linux-gnu/libXi.so.6 \
104+
/lib/x86_64-linux-gnu/libXrender.so.1 \
105+
/lib/x86_64-linux-gnu/libwayland-server.so.0 \
106+
/lib/x86_64-linux-gnu/libfribidi.so.0 \
107+
/lib/x86_64-linux-gnu/libthai.so.0 \
108+
/lib/x86_64-linux-gnu/libharfbuzz.so.0 \
109+
/lib/x86_64-linux-gnu/libpixman-1.so.0 \
110+
/lib/x86_64-linux-gnu/libfontconfig.so.1 \
111+
/lib/x86_64-linux-gnu/libfreetype.so.6 \
112+
/lib/x86_64-linux-gnu/libpng16.so.16 \
113+
/lib/x86_64-linux-gnu/libxcb-shm.so.0 \
114+
/lib/x86_64-linux-gnu/libxcb-render.so.0 \
115+
/lib/x86_64-linux-gnu/libkrb5.so.3 \
116+
/lib/x86_64-linux-gnu/libk5crypto.so.3 \
117+
/lib/x86_64-linux-gnu/libcom_err.so.2 \
118+
/lib/x86_64-linux-gnu/libkrb5support.so.0 \
119+
/lib/x86_64-linux-gnu/libp11-kit.so.0 \
120+
/lib/x86_64-linux-gnu/libidn2.so.0 \
121+
/lib/x86_64-linux-gnu/libunistring.so.2 \
122+
/lib/x86_64-linux-gnu/libtasn1.so.6 \
123+
/lib/x86_64-linux-gnu/libnettle.so.8 \
124+
/lib/x86_64-linux-gnu/libhogweed.so.6 \
125+
/lib/x86_64-linux-gnu/libgmp.so.10 \
126+
/lib/x86_64-linux-gnu/libblkid.so.1 \
127+
/lib/x86_64-linux-gnu/libcap.so.2 \
128+
/lib/x86_64-linux-gnu/libgcrypt.so.20 \
129+
/lib/x86_64-linux-gnu/liblzma.so.5 \
130+
/lib/x86_64-linux-gnu/libzstd.so.1 \
131+
/lib/x86_64-linux-gnu/liblz4.so.1 \
132+
/lib/x86_64-linux-gnu/libbsd.so.0 \
133+
/lib/x86_64-linux-gnu/libdatrie.so.1 \
134+
/lib/x86_64-linux-gnu/libgraphite2.so.3 \
135+
/lib/x86_64-linux-gnu/libbrotlidec.so.1 \
136+
/lib/x86_64-linux-gnu/libkeyutils.so.1 \
137+
/lib/x86_64-linux-gnu/libresolv.so.2 \
138+
/lib/x86_64-linux-gnu/libgpg-error.so.0 \
139+
/lib/x86_64-linux-gnu/libmd.so.0 \
140+
/lib/x86_64-linux-gnu/libbrotlicommon.so.1 \
141+
/lib/x86_64-linux-gnu/libXcomposite.so.1 \
142+
/lib/x86_64-linux-gnu/libXfixes.so.3 \
143+
/lib/x86_64-linux-gnu/libXrandr.so.2 \
144+
/lib/x86_64-linux-gnu/libgbm.so.1 \
145+
/lib/x86_64-linux-gnu/
146+
147+
# Other Chrome-related libraries
148+
COPY --from=build /usr/lib/x86_64-linux-gnu/libsoftokn3.so \
149+
/usr/lib/x86_64-linux-gnu/libsqlite3.so.0 \
150+
/usr/lib/x86_64-linux-gnu/libudev.so.1 \
151+
/usr/lib/x86_64-linux-gnu/libfreebl3.so \
152+
/usr/lib/x86_64-linux-gnu/libfreeblpriv3.so \
153+
/usr/lib/x86_64-linux-gnu/libudev.so.1.7.5 \
154+
/usr/lib/x86_64-linux-gnu/
155+
156+
# Node binary
157+
COPY --from=build /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/node
158+
159+
# System libraries
160+
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 \
161+
/lib/x86_64-linux-gnu/libz.so.1 \
162+
/lib/x86_64-linux-gnu/libbrotlidec.so.1 \
163+
/lib/x86_64-linux-gnu/libbrotlienc.so.1 \
164+
/lib/x86_64-linux-gnu/libnghttp2.so.14 \
165+
/lib/x86_64-linux-gnu/libcrypto.so.3 \
166+
/lib/x86_64-linux-gnu/libssl.so.3 \
167+
/lib/x86_64-linux-gnu/libicui18n.so.72 \
168+
/lib/x86_64-linux-gnu/libicuuc.so.72 \
169+
/lib/x86_64-linux-gnu/libstdc++.so.6 \
170+
/lib/x86_64-linux-gnu/libm.so.6 \
171+
/lib/x86_64-linux-gnu/libgcc_s.so.1 \
172+
/lib/x86_64-linux-gnu/libpthread.so.0 \
173+
/lib/x86_64-linux-gnu/libdl.so.2 \
174+
/lib/x86_64-linux-gnu/libbrotlicommon.so.1 \
175+
/lib/x86_64-linux-gnu/libicudata.so.72 \
176+
/lib/x86_64-linux-gnu/librt.so.1 \
177+
/lib/x86_64-linux-gnu/libtinfo.so.6 \
178+
/lib/x86_64-linux-gnu/libproc2.so.0 \
179+
/lib/x86_64-linux-gnu/
180+
181+
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
182+
COPY --from=build /etc/ld.so.cache /etc/ld.so.cache
183+
184+
# Dbus and system files
185+
COPY --from=build /usr/lib/dbus-1.0 /usr/lib/dbus-1.0
186+
COPY --from=build /usr/lib/systemd /usr/lib/systemd
187+
COPY --from=build /usr/lib/tmpfiles.d /usr/lib/tmpfiles.d
188+
COPY --from=build /usr/lib/sysusers.d /usr/lib/sysusers.d
189+
COPY --from=build /usr/lib/sysctl.d /usr/lib/sysctl.d
190+
191+
# Data files
192+
COPY --from=build /usr/share/fonts /usr/share/fonts
193+
194+
COPY --from=build /run /run
195+
196+
# Distro definition
197+
COPY --from=build /etc/os-release /etc/os-release
198+
COPY --from=build /usr/lib/os-release /usr/lib/os-release
199+
200+
# Configuration files
201+
COPY --from=build /etc /etc
202+
203+
# Node modules, including Puppeteer and application
204+
COPY --from=build /app /app
205+
206+
# Required by wrapper script
207+
COPY --from=build /bin/sh /bin/sh
208+
209+
# Required by Playwright / Chrome
210+
COPY --from=build /usr/bin/ps /usr/bin/ps
211+
212+
# Actual server implementation
213+
COPY ./server.js /app/server.js
214+
215+
# Wrapper script set environment
216+
COPY ./wrapper.sh /usr/bin/wrapper.sh

node-playwright/Kraftfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spec: v0.6
2+
3+
runtime: base-compat:latest
4+
5+
labels:
6+
cloud.unikraft.v1.instances/scale_to_zero.policy: "idle"
7+
cloud.unikraft.v1.instances/scale_to_zero.stateful: "true"
8+
cloud.unikraft.v1.instances/scale_to_zero.cooldown_time_ms: 7000
9+
10+
rootfs: ./Dockerfile
11+
12+
cmd: ["/usr/bin/wrapper.sh", "/usr/bin/node", "/app/server.js"]

node-playwright/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Playwright with Node.js on Unikraft Cloud
2+
3+
[Playwright](https://playwright.dev/) is a framework for web testing and Automation.
4+
5+
To run Playwright with Node.js on Unikraft Cloud, first [install the `kraft` CLI tool](https://unikraft.org/docs/cli).
6+
Then clone this repository and `cd` into this directory, and invoke:
7+
8+
```console
9+
kraft cloud deploy -M 4096 -p 443:8080 .
10+
```
11+
12+
The command will deploy the files in the current directory.
13+
It results in the creation of a remote web-based service for creating PNG screenshots of remote pages.
14+
15+
Use the `?page=<REMOTE_URL>` to point the service to the remote page to screenshot.
16+
Query the service using commands such as:
17+
18+
```console
19+
curl https://<NAME>.<METRO>.kraft.host/?page=https://google.com -o ss-google.png
20+
curl https://<NAME>.<METRO>.kraft.host/?page=https://bing.com -o ss-bing.png
21+
```
22+
23+
## Learn more
24+
25+
- [Playwright's Documentation](https://playwright.dev/docs/intro)
26+
- [Unikraft Cloud's Documentation](https://unikraft.cloud/docs/)
27+
- [Building `Dockerfile` Images with `Buildkit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)

node-playwright/package-lock.json

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-playwright/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"playwright": "^1.47.0",
4+
"playwright-chromium": "^1.47.0"
5+
}
6+
}

0 commit comments

Comments
 (0)