-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
154 lines (131 loc) · 5.75 KB
/
main.js
File metadata and controls
154 lines (131 loc) · 5.75 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/** Copyright 2023 Dimitri del Marmol
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
import { WebContainer } from '@webcontainer/api'
import stripAnsi from 'strip-ansi'
// Singleton on port 1880
// Accessible from the page itself only
let webcontainerInstance
let initRan = false
const port = 1880
const output = document.querySelector('#output')
const OutputStreamPrototype = {
write(data) {
console.log(stripAnsi(data))
}
}
const files = {
'index.js': {
file: {
contents: `
import http from 'http'
import express from 'express'
import RED from 'node-red'
import { runYosys } from '@yowasp/yosys'
const app = express()
app.use('/', express.static('public'))
const server = http.createServer(app)
const settings = {
httpAdminRoot: '/red',
httpNodeRoot: '/',
userDir: '.',
flowFile: 'flows.json',
functionGlobalContext: {
runYosys: function (cmd, msg, node) {
const result = []
runYosys(cmd.split(' '), [], {
printLine: line => {
result.push(line)
msg.payload = line
node.send([null, msg])
},
decodeASCII: true
}).then(function(){
msg.payload = result
node.send([msg, null])
})
}
}
}
RED.init(server, settings)
app.use(settings.httpAdminRoot, RED.httpAdmin)
app.use(settings.httpNodeRoot, RED.httpNode)
server.listen(${port})
RED.start()
`,
},
},
'package.json': {
file: {
contents: `{
"name": "webcontainer-bonus",
"type": "module",
"dependencies": {
"express": "latest",
"node-red": "latest",
"node-red-node-swagger": "latest",
"node-red-debugger": "latest",
"nodemon": "latest",
"@yowasp/yosys": "latest"
},
"scripts": {
"dff": "cat dff.v",
"start": "nodemon -x 'node index.js || touch index.js'"
}
}`,
},
},
'flows.json': {
file: {
contents: `[{"id":"79f2ac62b7d3a52b","type":"tab","label":"Yosys","disabled":false,"info":"","env":[]},{"id":"d581b155863d86e3","type":"swagger-doc","summary":"","description":"","tags":"yosys","consumes":"","produces":"","parameters":[{"name":"cmd","in":"query","required":true,"type":"string"}],"responses":{"200":{"description":""}},"deprecated":false},{"id":"40795e8e67c44acf","type":"http in","z":"79f2ac62b7d3a52b","name":"","url":"/yosys","method":"get","upload":false,"swaggerDoc":"d581b155863d86e3","x":90,"y":60,"wires":[["9a21494c5a4ba30c"]]},{"id":"760999de666eab31","type":"http response","z":"79f2ac62b7d3a52b","name":"👍","statusCode":"","headers":{},"x":330,"y":60,"wires":[]},{"id":"9a21494c5a4ba30c","type":"function","z":"79f2ac62b7d3a52b","name":"yosys","func":"global.get('runYosys')(msg.req.query.cmd, msg, node)","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":210,"y":120,"wires":[["760999de666eab31"],["e04c12f3a64d1be9"]]},{"id":"e04c12f3a64d1be9","type":"debug","z":"79f2ac62b7d3a52b","name":"📜","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":330,"y":180,"wires":[]}]`
}
},
'dff.v': {
file: {
contents: `
module dff (
q,
d,
clk
);
output q;
reg q;
input d;
input clk;
always @(posedge clk) begin: DFF_LOGIC
q <= d;
end
endmodule
`
}
}
}
async function initWebcontainer() {
// Call only once
webcontainerInstance = await WebContainer.boot()
await webcontainerInstance.mount(files)
}
async function npm(cmd) {
const process = await webcontainerInstance.spawn('npm', cmd)
const stdout = new WritableStream(OutputStreamPrototype)
// Capture output
process.output.pipeTo(stdout)
// Wait for process to exit
return process.exit
}
window.addEventListener('load', async () => {
await initWebcontainer()
webcontainerInstance.on('server-ready', (port, url) => {
// Ugly hack to avoid hitting the server before it is ready
if (!initRan) {
setTimeout(() => { output.src = `${url}/red` }, 2000)
initRan = true
}
})
if (!initRan) {
if (await npm(['install']) !== 0) throw new Error('Installation failed')
if (await npm(['run', 'dff']) !== 0) throw new Error('Start failed')
}
if (await npm(['run', 'start']) !== 0) throw new Error('Start failed')
})