From 84f03b313a21a67e494ff5b06fec1f040cbee349 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 19 May 2014 00:51:05 +0200 Subject: [PATCH 1/2] + Headless client no longer loaded if authentication fails + Moved jQuery stuff to extra file (jQlone) + jQuery ajax dereffer will also return values to callback functions (fix future bugs) + Cleaned code a little --- headless_client.coffee | 149 +++++----------------------------- headless_client/jQlone.coffee | 61 ++++++++++++++ 2 files changed, 83 insertions(+), 127 deletions(-) create mode 100644 headless_client/jQlone.coffee diff --git a/headless_client.coffee b/headless_client.coffee index 8fb9e49b5..ec78d4e14 100644 --- a/headless_client.coffee +++ b/headless_client.coffee @@ -8,8 +8,8 @@ headlessClientPath = "./headless_client/" # SETTINGS options = workerCode: require headlessClientPath + 'worker_world' - debug: false # Enable logging of ajax calls mainly - testing: false # Instead of simulating 'real' games, use the same one over and over again. Good for leak hunting. + debug: true # Enable logging of ajax calls mainly + testing: true # Instead of simulating 'real' games, use the same one over and over again. Good for leak hunting. testFile: require headlessClientPath + 'test.js' leakTest: false # Install callback that tries to find leaks automatically exitOnLeak: false # Exit if leak is found. Only useful if leaktest is set to true, obviously. @@ -17,7 +17,7 @@ options = headlessClient: true options.heapdump = require('heapdump') if options.heapdump -server = if options.testing then "http://127.0.0.1:3000" else "http://codecombat.com" +server = if options.testing then "http://127.0.0.1:3000" else "https://codecombat.com" # Disabled modules disable = [ @@ -28,37 +28,16 @@ disable = [ # Start of the actual code. Setting up the enivronment to match the environment of the browser -# the path used for the loader. __dirname is module dependent. -path = __dirname - -m = require 'module' -request = require 'request' -Deferred = require "JQDeferred" -originalLoader = m._load - -unhook = () -> - m._load = originalLoader - -hook = () -> - m._load = hookedLoader - - -JASON = require 'jason' - # Global emulated stuff GLOBAL.window = GLOBAL GLOBAL.document = location: pathname: "headless_client" GLOBAL.console.debug = console.log - GLOBAL.Worker = require('webworker-threads').Worker Worker::removeEventListener = (what) -> if what is 'message' @onmessage = -> #This webworker api has only one event listener at a time. - GLOBAL.tv4 = require('tv4').tv4 - GLOBAL.marked = setOptions: -> - store = {} GLOBAL.localStorage = getItem: (key) => store[key] @@ -69,6 +48,10 @@ GLOBAL.localStorage = # The signature of this function *must* match that of Node's Module._load, # since it will replace that. # (Why is there no easier way?) +# the path used for the loader. __dirname is module dependent. +path = __dirname +m = require 'module' +originalLoader = m._load hookedLoader = (request, parent, isMain) -> if request in disable or ~request.indexOf('templates') console.log 'Ignored ' + request if options.debug @@ -77,81 +60,16 @@ hookedLoader = (request, parent, isMain) -> request = path + '/app/' + request else if request is 'underscore' request = 'lodash' - console.log "loading " + request if options.debug originalLoader request, parent, isMain +unhook = () -> + m._load = originalLoader +hook = () -> + m._load = hookedLoader - -#jQuery wrapped for compatibility purposes. Poorly. -GLOBAL.$ = GLOBAL.jQuery = (input) -> - console.log 'Ignored jQuery: ' + input if options.debug - append: (input)-> exports: ()-> - -cookies = request.jar() -$.when = Deferred.when - -$.ajax = (options) -> - responded = false - url = options.url - if url.indexOf('http') - url = '/' + url unless url[0] is '/' - url = server + url - - data = options.data - - - #if (typeof data) is 'object' - #console.warn JSON.stringify data - #data = JSON.stringify data - - console.log "Requesting: " + JSON.stringify options if options.debug - console.log "URL: " + url if options.debug - - deferred = Deferred() - - request - url: url - jar: cookies - json: options.parse - method: options.type - body: data - , (error, response, body) -> - console.log "HTTP Request:" + JSON.stringify options if options.debug and not error - - if responded - console.log "\t↳Already returned before." if options.debug - return - - if (error) - console.warn "\t↳Returned: error: #{error}" - options.error(error) if options.error? - deferred.reject() - - else - console.log "\t↳Returned: statusCode #{response.statusCode}: #{if options.parse then JSON.stringify body else body}" if options.debug - options.success(body, response, status: response.statusCode) if options.success? - deferred.resolve() - - statusCode = response.statusCode if response? - options.complete(status: statusCode) if options.complete? - responded = true - - deferred.promise() - - -$.extend = (deep, into, from) -> - copy = _.clone(from, deep); - if into - _.assign into, copy - copy = into - copy - -$.isArray = (object) -> - _.isArray object - -$.isPlainObject = (object) -> - _.isPlainObject object - +GLOBAL.$ = GLOBAL.jQuery = require headlessClientPath + 'jQlone' +$._debug = options.debug +$._server = server do (setupLodash = this) -> GLOBAL._ = require 'lodash' @@ -159,29 +77,18 @@ do (setupLodash = this) -> _.string = _.str _.mixin _.str.exports() - # load Backbone. Needs hooked loader to reroute underscore to lodash. hook() GLOBAL.Backbone = require bowerComponentsPath + 'backbone/backbone' +# Use original loader for theese unhook() Backbone.$ = $ - require bowerComponentsPath + 'validated-backbone-mediator/backbone-mediator' -# Instead of mediator, dummy might be faster yet suffice? -#Mediator = class Mediator -# publish: (id, object) -> -# console.Log "Published #{id}: #{object}" -# @subscribe: () -> -# @unsubscribe: () -> - GLOBAL.Aether = require 'aether' - -# Set up new loader. +# Set up new loader. Again. hook() login = require './login.coffee' #should contain an object containing they keys 'username' and 'password' - - #Login user and start the code. $.ajax url: '/auth/login' @@ -189,24 +96,12 @@ $.ajax data: login parse: true error: (error) -> "Bad Error. Can't connect to server or something. " + error - success: (response) -> - console.log "User: " + response + success: (response, textStatus, jqXHR) -> + console.log "User: ", response if options.debug + unless jqXHR.status is 200 + console.log "User not authenticated. Status code: ", jqXHR.status + return GLOBAL.window.userObject = response # JSON.parse response - - User = require 'models/User' - - World = require 'lib/world/world' - LevelLoader = require 'lib/LevelLoader' - GoalManager = require 'lib/world/GoalManager' - - SuperModel = require 'models/SuperModel' - - log = require 'winston' - - CocoClass = require 'lib/CocoClass' - Simulator = require 'lib/simulator/Simulator' - sim = new Simulator options - - sim.fetchAndSimulateTask() + #sim.fetchAndSimulateTask() \ No newline at end of file diff --git a/headless_client/jQlone.coffee b/headless_client/jQlone.coffee new file mode 100644 index 000000000..a5abf26dd --- /dev/null +++ b/headless_client/jQlone.coffee @@ -0,0 +1,61 @@ +#jQuery for node, reimplementated for compatibility purposes. Poorly. +#Leaves out all the dome stuff but allows ajax. +_ = require 'lodash' +request = require 'request' +Deferred = require "JQDeferred" +module.exports = $ = (input) -> + console.log 'Ignored jQuery: ', input if $._debug + append: (input)-> exports: ()-> + +# Non-standard jQuery stuff. Don't use outside of server. +$._debug = false +$._server = "https://codecombat.com" +$._cookies = request.jar() + +$.when = Deferred.when +$.ajax = (options) -> + responded = false + url = options.url + if url.indexOf('http') + url = '/' + url unless url[0] is '/' + url = $._server + url + + data = options.data + console.log "Requesting: " + JSON.stringify options if $._debug + console.log "URL: " + url if $._debug + deferred = Deferred() + request + url: url + jar: $._cookies + json: options.parse + method: options.type + body: data + , (error, response, body) -> + console.log "HTTP Request:" + JSON.stringify options if $._debug and not error + if responded + console.log "\t↳Already returned before." if $._debug + return + if (error) + console.warn "\t↳Returned: error: #{error}" + deferred.reject(error) + else + console.log "\t↳Returned: statusCode #{response.statusCode}: #{if options.parse then JSON.stringify body else body}" if $._debug + deferred.resolve(body, response, status: response.statusCode) + + statusCode = response.statusCode if response? + options.complete(status: statusCode) if options.complete? + responded = true + deferred.promise().done(options.success).fail(options.error) + +$.extend = (deep, into, from) -> + copy = _.clone(from, deep); + if into + _.assign into, copy + copy = into + copy + +$.isArray = (object) -> + _.isArray object + +$.isPlainObject = (object) -> + _.isPlainObject object From 020ffd76dcdf62440de2986af298967d05b11588 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 19 May 2014 00:54:02 +0200 Subject: [PATCH 2/2] + Forgot to switch off debugging. Fixed. --- headless_client.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/headless_client.coffee b/headless_client.coffee index ec78d4e14..76d471c42 100644 --- a/headless_client.coffee +++ b/headless_client.coffee @@ -8,8 +8,8 @@ headlessClientPath = "./headless_client/" # SETTINGS options = workerCode: require headlessClientPath + 'worker_world' - debug: true # Enable logging of ajax calls mainly - testing: true # Instead of simulating 'real' games, use the same one over and over again. Good for leak hunting. + debug: false # Enable logging of ajax calls mainly + testing: false # Instead of simulating 'real' games, use the same one over and over again. Good for leak hunting. testFile: require headlessClientPath + 'test.js' leakTest: false # Install callback that tries to find leaks automatically exitOnLeak: false # Exit if leak is found. Only useful if leaktest is set to true, obviously. @@ -104,4 +104,4 @@ $.ajax GLOBAL.window.userObject = response # JSON.parse response Simulator = require 'lib/simulator/Simulator' sim = new Simulator options - #sim.fetchAndSimulateTask() \ No newline at end of file + sim.fetchAndSimulateTask() \ No newline at end of file