Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @author wkh237
  3. * @description react-native-fetch-blob test & dev server
  4. */
  5. var express = require('express')
  6. var bodyParser = require('body-parser')
  7. var chokidar = require('chokidar')
  8. var multer = require('multer')
  9. var upload = multer({dest : 'uploads/'})
  10. var chalk = require('chalk')
  11. var mkdirp = require('mkdirp')
  12. var dirname = require('path').dirname
  13. var app = express()
  14. var fs = require('fs')
  15. var https = require('https')
  16. var JS_SOURCE_PATH = '../test/',
  17. LIB_SOURCE_PATH = '../src/',
  18. NODE_MODULE_MODULE_PATH = '../RNFetchBlobTest/node_modules/react-native-fetch-blob/',
  19. APP_SOURCE_PATH = '../RNFetchBlobTest/'
  20. // watch test app source
  21. watch(JS_SOURCE_PATH, APP_SOURCE_PATH)
  22. // watch lib js source
  23. watch(LIB_SOURCE_PATH, NODE_MODULE_MODULE_PATH, {ignored: /\.\.\/src\/(android|ios)\//})
  24. // https
  25. var server = https.createServer({
  26. key : fs.readFileSync('./key.pem'),
  27. cert : fs.readFileSync('./cert.pem')
  28. }, app).listen(8124, function(err){
  29. if(!err)
  30. console.log('SSL test server running at port ',8124)
  31. })
  32. app.disable('etag')
  33. // http
  34. app.listen(8123, function(err){
  35. if(!err)
  36. console.log('test server running at port ',8123)
  37. })
  38. app.use(function(req,res,next){
  39. console.log(chalk.green('request url=') + chalk.magenta(req.url))
  40. next()
  41. })
  42. app.use('/upload-form', function(req, res, next) {
  43. console.log(req.headers)
  44. // req.on('data', (chunk) => {
  45. // console.log(String(chunk,'utf8'))
  46. // })
  47. // req.on('end', () => {
  48. next()
  49. // })
  50. })
  51. var count = 0
  52. app.use(function(req, res, next) {
  53. console.log(req.url, ++count);
  54. next();
  55. })
  56. app.get('/10s-download', (req,res) => {
  57. var count = 0
  58. var data = ''
  59. for(var i =0;i<1024000;i++)
  60. data += '1'
  61. res.set('Contet-Length', 1024000*10)
  62. var it = setInterval(() => {
  63. res.write(data)
  64. count++
  65. if(count == 10) {
  66. clearInterval(it)
  67. res.end()
  68. }
  69. }, 1000)
  70. })
  71. app.get('/video/:count', (req, res) => {
  72. var count = 0
  73. res.set('Content-Type', 'multipart/x-mixed-replace; boundary="---osclivepreview---"')
  74. res.write('---osclivepreview---\r\n')
  75. var it = setInterval(() => {
  76. if(count > req.params.count) {
  77. res.end()
  78. }
  79. else {
  80. count ++
  81. res.write('Content-Type: image/jpeg\r\n')
  82. var data = fs.readFileSync('./cat_fu_mp4/img' + pad(count,5) + '.jpeg')
  83. console.log('frame', count, 'length=', new Buffer(data).length, 'base64=', new Buffer(data).toString('base64').length)
  84. console.log(data)
  85. fs.writeFileSync('test.jpeg')
  86. res.write('Content-Length: '+data.length+'\r\n')
  87. res.write('\r\n')
  88. res.write(new Buffer(data), 'binary')
  89. res.write('\r\n\r\n---osclivepreview---\r\n')
  90. }
  91. }, 66)
  92. })
  93. app.all('/upload', (req, res) => {
  94. console.log(req.headers)
  95. res.send(req.headers)
  96. })
  97. app.get('/unicode', (req, res) => {
  98. res.send({ data:'你好!'})
  99. })
  100. app.all('/echo', (req, res) => {
  101. var body = ''
  102. req.on('data', (chunk) => {
  103. body+=chunk
  104. })
  105. req.on('end', () => {
  106. res.send({
  107. headers : req.headers,
  108. body : body
  109. })
  110. })
  111. })
  112. app.use(upload.any())
  113. app.use('/public', express.static('./public'))
  114. // for redirect test
  115. app.get('/redirect', function(req, res) {
  116. res.redirect('/public/github.png')
  117. })
  118. app.all('/params', function(req, res) {
  119. console.log(req.url)
  120. var resp =
  121. {
  122. time : req.query.time,
  123. name : req.query.name,
  124. lang : req.query.lang
  125. }
  126. console.log(resp)
  127. res.send(resp)
  128. })
  129. // return an empty response
  130. app.all('/empty', function(req, res) {
  131. res.send('')
  132. })
  133. app.delete('/hey', function(req, res) {
  134. res.send('man')
  135. })
  136. app.get('/stress/:id', function(req, res) {
  137. res.sendFile(process.cwd() + '/public/github.png')
  138. })
  139. app.post('/mime', mimeCheck)
  140. app.put('/mime', mimeCheck)
  141. function mimeCheck(req, res) {
  142. console.log(req.files)
  143. var mimes = []
  144. for(var i in req.files) {
  145. mimes.push(req.files[i].mimetype)
  146. }
  147. res.send(mimes)
  148. }
  149. // handle multipart/form-data request
  150. app.post('/upload-form', formUpload)
  151. app.put('/upload-form', formUpload)
  152. // for XHR tests
  153. //
  154. app.all('/xhr-code/:code', (req, res) => {
  155. console.log('code = ', req.params.code)
  156. res.status(Math.floor(req.params.code)).send()
  157. })
  158. app.all('/content-length', (req, res) => {
  159. console.log(req.headers)
  160. res.send(req.headers['Content-Length'])
  161. })
  162. app.all('/xhr-header', (req, res) => {
  163. console.log(req.headers)
  164. // res.header('Content-Type', 'application/json')
  165. res.send(req.headers)
  166. })
  167. app.post('/upload_urlencode', bodyParser.urlencoded({ extended : true }), (req, res) => {
  168. console.log(JSON.stringify(req.headers))
  169. console.log(JSON.stringify(req.body))
  170. res.status(200).send(req.body)
  171. })
  172. app.all('/timeout408/:time', (req, res) => {
  173. setTimeout(function() {
  174. res.status(408).send('request timed out.')
  175. }, 5000)
  176. })
  177. app.all('/long/:ticks', (req, res) => {
  178. var count = 0;
  179. var it = setInterval(() => {
  180. console.log('write data', count)
  181. res.write('a')
  182. if(++count > req.params.ticks){
  183. clearInterval(it)
  184. res.end()
  185. }
  186. }, 1000);
  187. })
  188. app.all('/long/', (req, res) => { var count = 0;
  189. var it = setInterval(() => {
  190. console.log('write data', count)
  191. res.write('a')
  192. if(++count >60){
  193. clearInterval(it)
  194. res.end()
  195. }
  196. }, 1000);
  197. })
  198. app.all('/cookie/:data', (req, res) => {
  199. res.cookie('cookieName', req.params.data);
  200. res.end()
  201. })
  202. app.all('/err-body', (req, res) => {
  203. res.status(400)
  204. res.write(JSON.stringify({ data : Date.now() }))
  205. res.end()
  206. })
  207. app.all('/timeout', (req, res) => {
  208. })
  209. function formUpload(req, res) {
  210. console.log(req.headers)
  211. console.log(req.body)
  212. console.log(req.files)
  213. if(Array.isArray(req.files)) {
  214. req.files.forEach((f) => {
  215. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  216. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  217. })
  218. }
  219. res.status(200).send({
  220. fields : req.body,
  221. files : req.files
  222. })
  223. }
  224. function watch(source, dest, ignore) {
  225. // watch files in test folder
  226. chokidar
  227. .watch(source, ignore)
  228. .on('add', function(path) {
  229. console.log(chalk.magenta('file created'), path)
  230. var targetPath = String(path).replace(source, dest)
  231. mkdirp(dirname(targetPath), function (err) {
  232. if (err) return cb(err)
  233. fs.writeFileSync(targetPath, fs.readFileSync(path))
  234. })
  235. })
  236. .on('change', function(path) {
  237. console.log(chalk.green('file changed'), path)
  238. var targetPath = String(path).replace(source, dest)
  239. mkdirp(dirname(targetPath), function (err) {
  240. if (err) return cb(err)
  241. fs.writeFileSync(targetPath, fs.readFileSync(path))
  242. })
  243. })
  244. .on('unlink', function(path) {
  245. console.log(chalk.red('file removed'), path)
  246. var targetPath = String(path).replace(source, dest)
  247. mkdirp(dirname(targetPath), function (err) {
  248. if (err) return cb(err)
  249. fs.unlinkSync(targetPath)
  250. })
  251. })
  252. .on('error', function(err){
  253. console.log(err)
  254. })
  255. }
  256. function pad(n, width, z) {
  257. z = z || '0';
  258. n = n + '';
  259. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  260. }