chore: basic structure of files to rewrite in TS
This commit is contained in:
78
oldCode/test/fixture/index.js
Normal file
78
oldCode/test/fixture/index.js
Normal file
@@ -0,0 +1,78 @@
|
||||
'use strict' // Node 4.x workaround
|
||||
|
||||
const express = require('express')
|
||||
const http = require('http')
|
||||
|
||||
const socketIo = require('socket.io')
|
||||
const socketio_jwt = require('../../lib')
|
||||
|
||||
const jwt = require('jsonwebtoken')
|
||||
const xtend = require('xtend')
|
||||
const enableDestroy = require('server-destroy')
|
||||
|
||||
let sio
|
||||
|
||||
exports.start = (options, callback) => {
|
||||
if (typeof options == 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
options = xtend(
|
||||
{
|
||||
secret: 'aaafoo super sercret',
|
||||
timeout: 1000,
|
||||
handshake: true
|
||||
},
|
||||
options
|
||||
)
|
||||
|
||||
const app = express()
|
||||
const server = http.createServer(app)
|
||||
sio = socketIo.listen(server)
|
||||
|
||||
app.use(express.json())
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@doe.com',
|
||||
id: 123
|
||||
}
|
||||
|
||||
// We are sending the profile inside the token
|
||||
const token = jwt.sign(profile, options.secret, { expiresIn: 60 * 60 * 5 })
|
||||
res.json({ token: token })
|
||||
})
|
||||
|
||||
if (options.handshake) {
|
||||
sio.use(socketio_jwt.authorize(options))
|
||||
|
||||
sio.sockets.on('echo', (m) => {
|
||||
sio.sockets.emit('echo-response', m)
|
||||
})
|
||||
} else {
|
||||
sio.sockets
|
||||
.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', (socket) => {
|
||||
socket.on('echo', (m) => {
|
||||
socket.emit('echo-response', m)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
server.__sockets = []
|
||||
server.on('connection', (c) => {
|
||||
server.__sockets.push(c)
|
||||
})
|
||||
server.listen(9000, callback)
|
||||
enableDestroy(server)
|
||||
}
|
||||
|
||||
exports.stop = (callback) => {
|
||||
sio.close()
|
||||
try {
|
||||
server.destroy()
|
||||
} catch (er) {}
|
||||
callback()
|
||||
}
|
77
oldCode/test/fixture/namespace.js
Normal file
77
oldCode/test/fixture/namespace.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict' // Node 4.x workaround
|
||||
|
||||
const express = require('express')
|
||||
const http = require('http')
|
||||
|
||||
const socketIo = require('socket.io')
|
||||
const socketio_jwt = require('../../lib')
|
||||
|
||||
const jwt = require('jsonwebtoken')
|
||||
const xtend = require('xtend')
|
||||
const enableDestroy = require('server-destroy')
|
||||
|
||||
let sio
|
||||
|
||||
/**
|
||||
* This is an example server that shows how to do namespace authentication.
|
||||
*
|
||||
* The /admin namespace is protected by JWTs while the global namespace is public.
|
||||
*/
|
||||
exports.start = (callback) => {
|
||||
const options = {
|
||||
secret: 'aaafoo super sercret',
|
||||
timeout: 1000,
|
||||
handshake: false
|
||||
}
|
||||
|
||||
const app = express()
|
||||
const server = http.createServer(app)
|
||||
sio = socketIo.listen(server)
|
||||
|
||||
app.use(express.json())
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@doe.com',
|
||||
id: 123
|
||||
}
|
||||
|
||||
// We are sending the profile inside the token
|
||||
const token = jwt.sign(profile, options.secret, { expiresIn: 60 * 60 * 5 })
|
||||
res.json({ token: token })
|
||||
})
|
||||
|
||||
// Global namespace (public)
|
||||
sio.on('connection', (socket) => {
|
||||
socket.emit('hi')
|
||||
})
|
||||
|
||||
// Second roundtrip
|
||||
const admin_nsp = sio.of('/admin')
|
||||
|
||||
admin_nsp
|
||||
.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', (socket) => {
|
||||
socket.emit('hi admin')
|
||||
})
|
||||
|
||||
// One roundtrip
|
||||
const admin_nsp_hs = sio.of('/admin_hs')
|
||||
|
||||
admin_nsp_hs.use(socketio_jwt.authorize(xtend(options, { handshake: true })))
|
||||
admin_nsp_hs.on('connection', (socket) => {
|
||||
socket.emit('hi admin')
|
||||
})
|
||||
|
||||
server.listen(9000, callback)
|
||||
enableDestroy(server)
|
||||
}
|
||||
|
||||
exports.stop = (callback) => {
|
||||
sio.close()
|
||||
try {
|
||||
server.destroy()
|
||||
} catch (er) {}
|
||||
callback()
|
||||
}
|
87
oldCode/test/fixture/secret_function.js
Normal file
87
oldCode/test/fixture/secret_function.js
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict' // Node 4.x workaround
|
||||
|
||||
const express = require('express')
|
||||
const http = require('http')
|
||||
|
||||
const socketIo = require('socket.io')
|
||||
const socketio_jwt = require('../../lib')
|
||||
|
||||
const jwt = require('jsonwebtoken')
|
||||
const xtend = require('xtend')
|
||||
const enableDestroy = require('server-destroy')
|
||||
|
||||
let sio
|
||||
|
||||
exports.start = (options, callback) => {
|
||||
const SECRETS = {
|
||||
123: 'aaafoo super sercret',
|
||||
555: 'other'
|
||||
}
|
||||
|
||||
if (typeof options == 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
options = xtend(
|
||||
{
|
||||
secret: (request, decodedToken, callback) => {
|
||||
callback(null, SECRETS[decodedToken.id])
|
||||
},
|
||||
timeout: 1000,
|
||||
handshake: true
|
||||
},
|
||||
options
|
||||
)
|
||||
|
||||
const app = express()
|
||||
const server = http.createServer(app)
|
||||
sio = socketIo.listen(server)
|
||||
|
||||
app.use(express.json())
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@doe.com',
|
||||
id: req.body.username === 'valid_signature' ? 123 : 555
|
||||
}
|
||||
|
||||
// We are sending the profile inside the token
|
||||
const token = jwt.sign(profile, SECRETS[123], { expiresIn: 60 * 60 * 5 })
|
||||
res.json({ token: token })
|
||||
})
|
||||
|
||||
if (options.handshake) {
|
||||
sio.use(socketio_jwt.authorize(options))
|
||||
|
||||
sio.sockets.on('echo', (m) => {
|
||||
sio.sockets.emit('echo-response', m)
|
||||
})
|
||||
} else {
|
||||
sio.sockets
|
||||
.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', (socket) => {
|
||||
socket.on('echo', (m) => {
|
||||
socket.emit('echo-response', m)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
server.__sockets = []
|
||||
server.on('connection', (c) => {
|
||||
server.__sockets.push(c)
|
||||
})
|
||||
|
||||
server.listen(9000, callback)
|
||||
enableDestroy(server)
|
||||
}
|
||||
|
||||
exports.stop = (callback) => {
|
||||
sio.close()
|
||||
try {
|
||||
server.destroy()
|
||||
} catch (er) {}
|
||||
|
||||
callback()
|
||||
}
|
Reference in New Issue
Block a user