server to proxy requests to CSSWG bikeshed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const crypto = require('crypto'); | |
const http = require('http'); | |
const https = require('https'); | |
const fs = require('fs'); | |
http.createServer((req, res) => { | |
if (req.url !== '/') { | |
res.writeHead(404); | |
res.end(); | |
return; | |
} | |
const boundary = `--boundary-${crypto.randomBytes(8).toString('hex')}`; | |
const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="die-on"\r\n\r\nwarning\r\n--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="index.bs"\r\nContent-Type: text/plain\r\n\r\n`; | |
const suffix = `\r\n--${boundary}--\r\n`; | |
console.log('got request'); | |
const fd = fs.open('index.bs', 'r', (err, fd) => { | |
if (err) { res.end(); return; } | |
console.log('opened file'); | |
fs.fstat(fd, (err, stats) => { | |
if (err) { res.end(); fs.close(fd, () => {}); return; } | |
console.log('stat file'); | |
let bsReq = https.request('https://api.csswg.org/bikeshed/', | |
{headers: {'Accept': 'text/plain, text/html', 'Content-Type': `multipart/form-data; boundary=${boundary}`, 'Content-Length': stats.size + prefix.length + suffix.length}, | |
method: 'POST'}, | |
(bsRes) => { | |
console.log('response started', bsRes.statusCode, bsRes.statusMessage, bsRes.headers); | |
res.writeHead(bsRes.statusCode, bsRes.statusMessage, bsRes.headers); | |
bsRes.pipe(res); | |
}); | |
bsReq.write(prefix); | |
console.log('creating read stream'); | |
const sourceStream = fs.createReadStream(null, {fd}); | |
sourceStream.pipe(bsReq, {end: false}); | |
sourceStream.on('end', () => { bsReq.end(suffix); fs.close(fd, () => {}); console.log('end'); }); | |
}); | |
}); | |
}).listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment