and commited on
Commit
9609abc
1 Parent(s): 2284b0f
Files changed (4) hide show
  1. .gitignore +5 -0
  2. Dockerfile +1 -0
  3. index.mjs +37 -0
  4. test_evaluate.mjs +8 -6
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .DS_Store
2
+ *.swp
3
+ *.out
4
+ .vscode
5
+ node_modules/
Dockerfile CHANGED
@@ -6,6 +6,7 @@ WORKDIR /code
6
 
7
  RUN npm i playwright@1.40.0
8
  RUN npm i web-locks
 
9
 
10
  COPY . .
11
 
 
6
 
7
  RUN npm i playwright@1.40.0
8
  RUN npm i web-locks
9
+ RUN npm i ws
10
 
11
  COPY . .
12
 
index.mjs CHANGED
@@ -6,14 +6,18 @@ import { locks } from 'web-locks';
6
  import crypto from 'crypto';
7
  import fs from 'fs';
8
  import { setTimeout } from 'timers/promises';
 
 
9
 
10
  // npm i playwright
11
  // npm i web-locks
 
12
 
13
  // nohup cloudflared tunnel --url http://localhost:8080 --no-autoupdate & (or setsid)
14
  // setsid node playwright.mjs (nohup doesnt work)
15
 
16
  // curl 'https://gowah44030-playwright.hf.space/..
 
17
 
18
  globalThis.state = Object.assign(globalThis.state||{}, {
19
  browser: null,
@@ -179,4 +183,37 @@ const server = http.createServer(async function(req, res) {
179
  }
180
  });
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  server.listen(7860);
 
6
  import crypto from 'crypto';
7
  import fs from 'fs';
8
  import { setTimeout } from 'timers/promises';
9
+ import { WebSocketServer } from 'ws';
10
+
11
 
12
  // npm i playwright
13
  // npm i web-locks
14
+ // npm i ws
15
 
16
  // nohup cloudflared tunnel --url http://localhost:8080 --no-autoupdate & (or setsid)
17
  // setsid node playwright.mjs (nohup doesnt work)
18
 
19
  // curl 'https://gowah44030-playwright.hf.space/..
20
+ // https://gowah44030-playwright.hf.space/screenshot?js=1&url=https://www.investing.com
21
 
22
  globalThis.state = Object.assign(globalThis.state||{}, {
23
  browser: null,
 
183
  }
184
  });
185
 
186
+ const wss = new WebSocketServer({ server });
187
+
188
+ wss.on('connection', function connection(ws, req) {
189
+ console.log('wss.connection', req.url, req.headers);
190
+
191
+ ws.isAlive = true;
192
+ ws.on('pong', function heartbeat(){ //'Pong messages are automatically sent in response to ping messages as required by the spec.'
193
+ //console.log('pong');
194
+ this.isAlive = true;
195
+ });
196
+
197
+ ws.on('message', function message(data) {
198
+ console.log('received: %s', data);
199
+ });
200
+
201
+ ws.addEventListener('close', function close() {
202
+ console.log('ws.close');
203
+ });
204
+ //ws.send('something');
205
+ });
206
+
207
+ // https://github.com/websockets/ws
208
+ const interval = setInterval(function ping() {
209
+ wss.clients.forEach(function each(ws) {
210
+ if (ws.isAlive === false) return ws.terminate();
211
+
212
+ ws.isAlive = false;
213
+ ws.ping();
214
+
215
+ ws.send('ping');//why: used by client to detect dead connection (because browser cant access ping/pong frames...(?))
216
+ });
217
+ }, 30000);
218
+
219
  server.listen(7860);
test_evaluate.mjs CHANGED
@@ -1,15 +1,15 @@
1
- await fetch('https://gowah44030-playwright.hf.space/evaluate', {
2
  method: "POST",
3
  body: JSON.stringify({
4
  url: 'https://chat.lmsys.org/assets/index-c347e3dc.js',
5
  code: `
6
  (async function(){
7
- return await new Promise(resolve=>{
8
 
9
  try {
10
- resolve(await fetch('https://chat.lmsys.org').then(res=>res.text()));
11
- } catch (e) { resolve(e);}
12
- return;
13
 
14
 
15
  const ws = new WebSocket('wss://chat.lmsys.org/queue/join');
@@ -22,4 +22,6 @@ await fetch('https://gowah44030-playwright.hf.space/evaluate', {
22
  })()
23
  `,
24
  })
25
- });
 
 
 
1
+ let res = await fetch('https://gowah44030-playwright.hf.space/evaluate', {
2
  method: "POST",
3
  body: JSON.stringify({
4
  url: 'https://chat.lmsys.org/assets/index-c347e3dc.js',
5
  code: `
6
  (async function(){
7
+ return await new Promise(async (resolve)=>{
8
 
9
  try {
10
+ //resolve(await fetch('https://chat.lmsys.org').then(res=>res.text()));
11
+ } catch (e) { resolve(e);}
12
+ //return;
13
 
14
 
15
  const ws = new WebSocket('wss://chat.lmsys.org/queue/join');
 
22
  })()
23
  `,
24
  })
25
+ }).then(res=>res.text());
26
+
27
+ console.log(res);