repl.it
@mat1/

repl reviver repldex

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
global.WebSocket = require("ws");

const fetch = require('node-fetch');
const crosis = require('@replit/crosis');
const express = require('express');

const app = express();

// CHANGE THIS STUFF
const replID = 'af750f80-9123-4237-9211-7d21de28d6b2';
const replURL = 'https://repldex.mat1.repl.co';
const ownURL = 'https://repl-reviver-repldex.mat1.repl.co'

const apiKey = process.env.key

if (!apiKey) {
	console.log('fork and put key=YOURAPIKEY in .env and replace replID with the id of your repl and also replace replURL with the repl.co link')
	process.exit()
}
console.log('starting')

async function connect() {
	console.log('connecting')
	const client = new crosis.Client();

	const token = await getToken(replID, apiKey);
	await client.connect({ token });

	const packagerCh = client.openChannel({
		service: 'packager3'	
	});
	const interpreterCh = client.openChannel({
		service: 'interp2',
		name: 'interper'
	});

	packagerCh.on('command', command => {
		if (command.state)
			console.log(`packager state ${command.state}`)
		if (command.ok)
			console.log('packager ok')
	});
	interpreterCh.on('command', command => {
	  // console.log('interpreter', command)
		var output = command.output
			.replace('\u001b[2J', '[clear]')
		process.stdout.write(command.output)
	});

  await packagerCh.request({
		packageInstall: {}
	});
	await interpreterCh.request({
		runMain: {}
	});

	client.close();
}


function getToken(replId, apiKey) {
  return fetch(`https://repl.it/api/v0/repls/${replId}/token`, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ apiKey })
  }).then(res => res.json());
}


function checkServerForever() {
	console.log('checking server forever')
	setInterval(async() => {
		console.log('checking')
		fetch(ownURL).catch(()=>{})
		fetch(replURL)
		.catch(() => {
			console.log('disconnected!!!')
		})
		.then(res => {
			if (res.status == '502') {
				console.log('502 error')
				connect();
			} else {
				console.log('good', res.status)
			}
		})

	}, 5000)
}

checkServerForever()

app.get('/', (req, res) => {
  res.send('Hello world!')
});

app.listen(3000, () => {
  console.log('server started');
});
?