Skip to content

Commit

Permalink
Adding CORS for socket-io client
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny-fr committed Sep 7, 2020
1 parent 5bd820d commit 7f255dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chimp-cli",
"version": "2.0.3",
"version": "2.1.0",
"description": "Purpose Chimp cli will make your life easier to edit your chimp recipes This cli will allow you to use your own IDE to edit your recipe. It will also allow you use others tools (sass / js bundle and so on) It includes a live reload tool",
"main": "src/index.js",
"bin": {
Expand All @@ -21,7 +21,8 @@
"node-watch": "^0.6.3",
"prompt": "^1.0.0",
"request": "^2.88.2",
"socket.io": "^2.3.0"
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0"
},
"devDependencies": {},
"homepage": "https://github.com/sunny-fr/chimp-cli",
Expand Down
31 changes: 29 additions & 2 deletions src/watch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
const http = require('http')
const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const fileWatcher = require('./watch/file-watcher')
const {configExistAndValid} = require('./core/management')
const {getRecipe, updateRecipe} = require('./api/chimp')
Expand Down Expand Up @@ -39,18 +41,42 @@ const backupScriptAndDeployLiveReloadToRecipe = ({chimpConfig, SERVER}) => {
}


const nodeClientCache = {
content: null
}

const getNodeClientAsText = () => {
try {
if(nodeClientCache.content){
return nodeClientCache.content
}
const clientPath = '../node_modules/socket.io-client/dist/socket.io.slim.js'
const realPath = path.resolve(__dirname, clientPath)
const content = fs.readFileSync(realPath, {content:'ut8'})
return nodeClientCache.content = content
} catch (e) {
console.log('cant get socket client')
console.log(e)
}

}

function start() {

const SERVER = `http://localhost:${PORT}`
const startServer = (chimpConfig) => {
const server = http.createServer(function (req, res) {

const lastPath = req.url.split('/').pop()
const send404 = () => {
res.writeHead(404);
res.end(JSON.stringify({message: 'not found'}));
}

if(req.url === '/socket-io/client') {
addCors(req, res)
res.writeHead(200, { 'Content-Type': 'text/plain' });
return res.end(getNodeClientAsText());
}
if(lastPath === 'js') {
const content = getFileContents(chimpConfig['js-path'])
addCors(req, res)
Expand Down Expand Up @@ -90,7 +116,7 @@ function start() {

})
const io = require('socket.io')(server, {
serveClient: true,
serveClient: false,
// below are engine.IO options
reconnectionDelay: 1000,
reconnectionAttempts: 5,
Expand All @@ -101,6 +127,7 @@ function start() {

server.listen(PORT)

console.clear()
console.log('')
console.log('')
console.log(chalk.yellow('serving : ', SERVER ))
Expand Down
2 changes: 1 addition & 1 deletion src/watch/recipe-file-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
.then(content => createScriptElement(content))
}
window.__chimp__.loaded = true
fetchAndInjectScript('---SERVER---/socket.io/socket.io.js')
fetchAndInjectScript('---SERVER---/socket-io/client')
.then(() => {
console.log(' *** CHIMP - SOCKET IO - INJECTED *** ')
return true
Expand Down

0 comments on commit 7f255dd

Please sign in to comment.