From bc1e147ca2c711526e9fcd18d419c7727f44a063 Mon Sep 17 00:00:00 2001 From: Serene Han Date: Fri, 15 Jan 2016 08:47:01 -0800 Subject: [PATCH] use coffeelint --- .gitignore | 1 + proxy/Cakefile | 12 +++- proxy/coffeelint.json | 135 ++++++++++++++++++++++++++++++++++++ proxy/proxypair.coffee | 25 ++++--- proxy/snowflake.coffee | 7 +- proxy/snowflake_test.coffee | 20 +++--- proxy/util.coffee | 4 +- 7 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 proxy/coffeelint.json diff --git a/.gitignore b/.gitignore index c87c52a..516f3ff 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ server/server snowflake.log proxy/test proxy/build +proxy/node_modules ignore/ diff --git a/proxy/Cakefile b/proxy/Cakefile index 849390b..d657e17 100644 --- a/proxy/Cakefile +++ b/proxy/Cakefile @@ -1,5 +1,5 @@ fs = require 'fs' -{exec} = require 'child_process' +{spawn, exec} = require 'child_process' # All coffeescript files required. FILES = [ @@ -9,6 +9,10 @@ FILES = [ 'websocket.coffee' 'snowflake.coffee' ] +FILES_TEST = [ + 'snowflake_test.coffee' +] +FILES_ALL = FILES.concat FILES_TEST OUTFILE = 'build/snowflake.coffee' STATIC = 'static' @@ -35,6 +39,12 @@ task 'build', 'build the snowflake proxy', -> compileCoffee() console.log 'Snowflake prepared.' +task 'lint', 'ensure idiomatic coffeescript', -> + spawn 'coffeelint', FILES_ALL, { + file: 'coffeelint.json' + stdio: 'inherit' + } + task 'clean', 'remove all built files', -> exec 'rm -r build' exec 'rm -r test' diff --git a/proxy/coffeelint.json b/proxy/coffeelint.json new file mode 100644 index 0000000..72b0e27 --- /dev/null +++ b/proxy/coffeelint.json @@ -0,0 +1,135 @@ +{ + "arrow_spacing": { + "level": "error" + }, + "braces_spacing": { + "level": "error", + "spaces": 1, + "empty_object_spaces": 0 + }, + "camel_case_classes": { + "level": "error" + }, + "coffeescript_error": { + "level": "error" + }, + "colon_assignment_spacing": { + "level": "ignore", + "spacing": { + "left": 0, + "right": 0 + } + }, + "cyclomatic_complexity": { + "level": "ignore", + "value": 10 + }, + "duplicate_key": { + "level": "error" + }, + "empty_constructor_needs_parens": { + "level": "ignore" + }, + "ensure_comprehensions": { + "level": "warn" + }, + "eol_last": { + "level": "ignore" + }, + "indentation": { + "value": 2, + "level": "error" + }, + "line_endings": { + "level": "ignore", + "value": "unix" + }, + "max_line_length": { + "value": 80, + "level": "error", + "limitComments": true + }, + "missing_fat_arrows": { + "level": "ignore", + "is_strict": false + }, + "newlines_after_classes": { + "value": 3, + "level": "ignore" + }, + "no_backticks": { + "level": "error" + }, + "no_debugger": { + "level": "warn", + "console": false + }, + "no_empty_functions": { + "level": "ignore" + }, + "no_empty_param_list": { + "level": "ignore" + }, + "no_implicit_braces": { + "level": "ignore", + "strict": true + }, + "no_implicit_parens": { + "level": "ignore", + "strict": true + }, + "no_interpolation_in_single_quotes": { + "level": "ignore" + }, + "no_nested_string_interpolation": { + "level": "warn" + }, + "no_plusplus": { + "level": "ignore" + }, + "no_private_function_fat_arrows": { + "level": "warn" + }, + "no_stand_alone_at": { + "level": "error" + }, + "no_tabs": { + "level": "error" + }, + "no_this": { + "level": "error" + }, + "no_throwing_strings": { + "level": "error" + }, + "no_trailing_semicolons": { + "level": "error" + }, + "no_trailing_whitespace": { + "level": "error", + "allowed_in_comments": false, + "allowed_in_empty_lines": false + }, + "no_unnecessary_double_quotes": { + "level": "ignore" + }, + "no_unnecessary_fat_arrows": { + "level": "warn" + }, + "non_empty_constructor_needs_parens": { + "level": "ignore" + }, + "prefer_english_operator": { + "level": "ignore", + "doubleNotLevel": "ignore" + }, + "space_operators": { + "level": "ignore" + }, + "spacing_after_comma": { + "level": "ignore" + }, + "transform_messes_up_line_numbers": { + "level": "warn" + } +} diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee index 478b256..a27ac92 100644 --- a/proxy/proxypair.coffee +++ b/proxy/proxypair.coffee @@ -24,7 +24,7 @@ class ProxyPair optional: [ { DtlsSrtpKeyAgreement: true } { RtpDataChannels: false } - ]} + ] } @pc.onicecandidate = (evt) => # Browser sends a null candidate once the ICE gathering completes. # In this case, it makes sense to send one copy-paste blob. @@ -35,7 +35,7 @@ class ProxyPair Signalling.send @pc.localDescription # OnDataChannel triggered remotely from the client when connection succeeds. @pc.ondatachannel = (dc) => - console.log dc; + console.log dc channel = dc.channel log 'Data Channel established...' @prepareDataChannel channel @@ -49,11 +49,11 @@ class ProxyPair # This is the point when the WebRTC datachannel is done, so the next step # is to establish websocket to the server. @connectRelay() - channel.onclose = => + channel.onclose = -> log 'Data channel closed.' - snowflake.state = MODE.INIT; + snowflake.state = MODE.INIT $msglog.className = '' - channel.onerror = => + channel.onerror = -> log 'Data channel error!' channel.onmessage = @onClientToRelayMessage @@ -124,19 +124,28 @@ class ProxyPair busy = true checkChunks = => busy = false + # WebRTC --> websocket - if @relayIsReady() && @relay.bufferedAmount < @MAX_BUFFER && @c2rSchedule.length > 0 + if @relayIsReady() && + @relay.bufferedAmount < @MAX_BUFFER && + @c2rSchedule.length > 0 chunk = @c2rSchedule.shift() @rateLimit.update chunk.length @relay.send chunk busy = true + # websocket --> WebRTC - if @webrtcIsReady() && @client.bufferedAmount < @MAX_BUFFER && @r2cSchedule.length > 0 + if @webrtcIsReady() && + @client.bufferedAmount < @MAX_BUFFER && + @r2cSchedule.length > 0 chunk = @r2cSchedule.shift() @rateLimit.update chunk.length @client.send chunk busy = true + checkChunks() while busy && !@rateLimit.isLimited() - if @r2cSchedule.length > 0 || @c2rSchedule.length > 0 || (@relayIsReady() && @relay.bufferedAmount > 0) || (@webrtcIsReady() && @client.bufferedAmount > 0) + if @r2cSchedule.length > 0 || @c2rSchedule.length > 0 || + (@relayIsReady() && @relay.bufferedAmount > 0) || + (@webrtcIsReady() && @client.bufferedAmount > 0) @flush_timeout_id = setTimeout @flush, @rateLimit.when() * 1000 diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee index 58a294b..df7c163 100644 --- a/proxy/snowflake.coffee +++ b/proxy/snowflake.coffee @@ -69,7 +69,8 @@ class Snowflake rateLimitBytes = undefined if 'off' != query['ratelimit'] - rateLimitBytes = Params.getByteCount(query, 'ratelimit', DEFAULT_RATE_LIMIT) + rateLimitBytes = Params.getByteCount(query, 'ratelimit', + DEFAULT_RATE_LIMIT) if undefined == rateLimitBytes @rateLimit = new DummyRateLimit() else @@ -125,7 +126,7 @@ class Snowflake params.push ['transport', 'webrtc'] makeProxyPair: (relay) -> - pair = new ProxyPair(null, relay, @rateLimit); + pair = new ProxyPair null, relay, @rateLimit @proxyPairs.push pair pair.onCleanup = (event) => # Delete from the list of active proxy pairs. @@ -224,7 +225,7 @@ init = -> $input = document.getElementById('input') $input.focus() - $input.onkeydown = (e) => $send.onclick() if 13 == e.keyCode # enter + $input.onkeydown = (e) -> $send.onclick() if 13 == e.keyCode # enter snowflake = new Snowflake() window.snowflake = snowflake diff --git a/proxy/snowflake_test.coffee b/proxy/snowflake_test.coffee index 6ca151b..1f027e7 100644 --- a/proxy/snowflake_test.coffee +++ b/proxy/snowflake_test.coffee @@ -1,19 +1,17 @@ window = {} VERBOSE = false -if process.argv.indexOf('-v') >= 0 - VERBOSE = true +VERBOSE = true if process.argv.indexOf('-v') >= 0 + numTests = 0 numFailed = 0 announce = (testName) -> - if VERBOSE - console.log '\n --- ' + testName + ' ---' + console.log '\n --- ' + testName + ' ---' if VERBOSE pass = (test) -> - numTests++; - if VERBOSE - console.log 'PASS ' + test + numTests++ + console.log 'PASS ' + test if VERBOSE fail = (test, expected, actual) -> numTests++ @@ -88,10 +86,10 @@ http://www.ietf.org/rfc/rfc2965.txt for the grammar. testParseCookieString = -> TESTS = [{ cs: '' - expected: { } + expected: {} },{ cs: 'a=b' - expected: { a: 'b'} + expected: { a: 'b' } },{ cs: 'a=b=c' expected: { a: 'b=c' } @@ -103,7 +101,7 @@ testParseCookieString = -> expected: { a: 'b', c: 'd' } },{ cs: 'a= b', - expected: {a: 'b' } + expected: { a: 'b' } },{ cs: 'a=' expected: { a: '' } @@ -271,7 +269,7 @@ testParseAddress = -> testGetParamAddress = -> DEFAULT = { host: '1.1.1.1', port: 2222 } TESTS = [{ - query: { } + query: {} expected: DEFAULT },{ query: { addr: '3.3.3.3:4444' }, diff --git a/proxy/util.coffee b/proxy/util.coffee index de7fb33..eaf8f9d 100644 --- a/proxy/util.coffee +++ b/proxy/util.coffee @@ -103,8 +103,8 @@ Params = return null # Get an object value and parse it as a byte count. Example byte counts are - # '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null on - # a parsing error. + # '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null + # on a parsing error. getByteCount: (query, param, defaultValue) -> spec = query[param] return defaultValue if undefined == spec