-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemulator.js
executable file
·73 lines (68 loc) · 2.64 KB
/
emulator.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
#!/usr/bin/env node
/*
* Copyright 2018 DoubleDutch, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const http = require('http')
const net = require('net')
const iframedPort = process.argv[2] || '3000'
getPort(3001).catch(() => getPort(0)).then(port => {
console.log(`Launching emulator at http://localhost:${port}/ wrapping localhost:${iframedPort} in an iframe.`)
console.log(`Open in a browser to ^^^^^^^^^^^^^^^^^^^^^^ to view this site in an emulator for admin-client.`)
console.log(`If your site is not running on port ${iframedPort}, rerun this script with the target port as the only parameter.`)
console.log(`Press ctrl-C to quit.`)
http.createServer((req, res) => {
res.writeHead(200)
res.write(`
<html>
<head>
<meta charset="utf-8">
<style>
body { height:100%; width:100%; margin:0; font-family:sans-serif; }
iframe { height: calc(100% - 30px); width:100%; border:none; }
#header { height: 30px; background-color: #009acd; text-align:center; line-height:30px; color: white; }
</style>
<script>
window.addEventListener("message", function(e) {
if (e.data.type === 'loaded') {
const target = document.getElementById('admin').contentWindow
target.postMessage({type: 'access_token', payload: {accessToken:'fake-access-token'}}, '*')
target.postMessage({type: 'application_id', payload: {applicationId:'sample-event-id'}}, '*')
target.postMessage({type: 'cms_root', payload: {url:'http://fake.localhost'}}, '*')
} else if (e.data.type === 'navigate_cms') {
console.log(e.data)
}
}, false)
</script>
</head>
<body>
<div id="header">👇 Below is your admin page, injected with an emulated token 🚂</div>
<iframe id="admin" src="http://localhost:${iframedPort}" />
</body>
</html>`)
}).listen(port)
})
function getPort(port) {
return new Promise((resolve, reject) => {
const server = net.createServer()
server.unref()
server.on('error', reject)
server.listen({port}, () => {
const port = server.address().port
server.close(() => {
resolve(port)
})
})
})
}