UNPKG

iweb

Version:

一个用于前端开发的实时预览工具

141 lines (130 loc) 4.91 kB
// Generated by CoffeeScript 1.8.0 var Channel, HttpChannelHandler, channelManager, handleCreate, handleError, handleGet, handleOptions, handlePost, setCORSHeaders, setCacheHeaders, utils, _; _ = require('weinre/node_modules/underscore'); utils = require('weinre/lib/utils'); Channel = require('weinre/lib/Channel'); channelManager = require('weinre/lib/channelManager'); module.exports = utils.registerClass(HttpChannelHandler = (function() { function HttpChannelHandler(pathPrefix) { this.pathPrefix = pathPrefix; if (this.pathPrefix === '/ws/client') { this.isClient = true; } else if (this.pathPrefix === '/ws/target') { this.isClient = false; } else { utils.pitch("invalid pathPrefix: " + this.pathPrefix); } this.isTarget = !this.isClient; } HttpChannelHandler.prototype.handle = function(context, uri) { var request = context.request; var response = context.response; var channelName, parts; setCORSHeaders(context); setCacheHeaders(context); if (uri[0] !== '/') { return handleError(context, 404); } if (uri === '/') { if (request.method === 'OPTIONS') { return handleOptions(context); } if (request.method === 'POST') { return handleCreate(this.pathPrefix, this.isClient, context); } return handleError(context, 405); } parts = uri.split('/'); if (parts.length > 2) { return handleError(context, 404); } channelName = parts[1]; if (request.method === 'OPTIONS') { return handleOptions(context); } if (request.method === 'GET') { return handleGet(context, channelName); } if (request.method === 'POST') { return handlePost(context, channelName); } return handleError(context, 405); }; return HttpChannelHandler; })()); handleCreate = function(pathPrefix, isClient, context) { var request = context.request; var response = context.response; var channel, id, remoteAddress, _ref, _ref1; id = (_ref = request.body) != null ? _ref.id : void 0; remoteAddress = ((_ref1 = request.connection) != null ? _ref1.remoteAddress : void 0) || ""; channel = new Channel(pathPrefix, id, remoteAddress, isClient); return context.content(JSON.stringify({ channel: channel.name, id: channel.id }), 'application/json'); }; handleGet = function(context, channelName) { var request = context.request; var response = context.response; var channel, remoteAddress, _ref; remoteAddress = ((_ref = request.connection) != null ? _ref.remoteAddress : void 0) || ""; channel = channelManager.getChannel(channelName, remoteAddress); if (!channel) { return handleError(context, 404); } return channel.getMessages((function(_this) { return function(messages) { if (channel.isClosed) { return handleError(context, 404); } if (!messages) { return handleError(context, 404); } return context.content(JSON.stringify(messages), 'application/json'); }; })(this)); }; handlePost = function(context, channelName) { var request = context.request; var response = context.response; var channel, remoteAddress, _ref; remoteAddress = ((_ref = request.connection) != null ? _ref.remoteAddress : void 0) || ""; channel = channelManager.getChannel(channelName, remoteAddress); if (!channel) { return handleError(context, 404); } //console.log(JSON.stringify(request.body)); channel.handleMessages(request.body); return context.content(''); }; handleOptions = function(context) { var request = context.request; var response = context.response; return context.content(''); }; handleError = function(context, status) { var request = context.request; var response = context.response; return context.status(status); }; setCORSHeaders = function(context) { var request = context.request; var response = context.response; // var origin = request.headers['Origin']; // console.log(origin); // if (!origin) { // return; // } response.setHeader('Access-Control-Allow-Origin', '*'); response.setHeader('Access-Control-Max-Age', '600'); return response.setHeader('Access-Control-Allow-Methods', 'GET, POST'); }; setCacheHeaders = function(context) { var request = context.request; var response = context.response; response.setHeader('Pragma', 'no-cache'); response.setHeader('Expires', '0'); response.setHeader('Cache-Control', 'no-cache'); return response.setHeader('Cache-Control', 'no-store'); };