Template Upload
This commit is contained in:
118
node_modules/weinre/lib-src/Channel.coffee
generated
vendored
Normal file
118
node_modules/weinre/lib-src/Channel.coffee
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
channelManager = require './channelManager'
|
||||
messageHandler = require './messageHandler'
|
||||
MessageQueue = require './MessageQueue'
|
||||
|
||||
AnonymousId = 'anonymous'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = utils.registerClass class Channel
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: (@pathPrefix, @id, @remoteAddress, @isClient) ->
|
||||
prefix = if @isClient then 'c-' else 't-'
|
||||
@name = "#{prefix}#{utils.getNextSequenceNumber()}"
|
||||
@messageQueue = new MessageQueue
|
||||
@isClosed = false
|
||||
@connections = []
|
||||
@isTarget = !@isClient
|
||||
@readTimeout = utils.options.readTimeout * 1000
|
||||
|
||||
@id = AnonymousId if !@id
|
||||
|
||||
@description =
|
||||
channel: @name
|
||||
id: @id
|
||||
hostName: @remoteAddress
|
||||
remoteAddress: @remoteAddress
|
||||
|
||||
@updateLastRead()
|
||||
|
||||
channelManager.created @
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
close: () ->
|
||||
return if @isClosed
|
||||
|
||||
channelManager.destroyed @
|
||||
|
||||
@isClosed = true
|
||||
@messageQueue.shutdown()
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
sendCallback: (intfName, callbackId, args...) ->
|
||||
return if !callbackId
|
||||
|
||||
args.unshift callbackId
|
||||
|
||||
@sendMessage intfName, 'sendCallback', args...
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
sendMessage: (intfName, method, args...) ->
|
||||
|
||||
message = genJSON
|
||||
interface: intfName
|
||||
method: method
|
||||
args: args
|
||||
|
||||
@messageQueue.push message
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
handleMessages: (messages) ->
|
||||
|
||||
for message in messages
|
||||
message = parseJSON(message)
|
||||
continue if !message
|
||||
|
||||
messageHandler.handleMessage @, message
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getMessages: (callback) ->
|
||||
@updateLastRead()
|
||||
return callback.call(null, null) if @isClosed
|
||||
|
||||
@messageQueue.pullAll @readTimeout, callback
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
updateLastRead: () ->
|
||||
@lastRead = (new Date).valueOf()
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
toString: () ->
|
||||
connections = _.map(@connections, (val) -> val.name).join(',')
|
||||
"Channel(#{@name}, closed:#{@isClosed}, connections:[#{connections}])"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
parseJSON = (message) ->
|
||||
try
|
||||
return JSON.parse(message)
|
||||
catch e
|
||||
return null
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
genJSON = (message) ->
|
||||
try
|
||||
return JSON.stringify(message)
|
||||
catch e
|
||||
return null
|
154
node_modules/weinre/lib-src/HttpChannelHandler.coffee
generated
vendored
Normal file
154
node_modules/weinre/lib-src/HttpChannelHandler.coffee
generated
vendored
Normal file
@ -0,0 +1,154 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
Channel = require './Channel'
|
||||
channelManager = require './channelManager'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = utils.registerClass class HttpChannelHandler
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: (@pathPrefix) ->
|
||||
|
||||
if @pathPrefix == '/ws/client'
|
||||
@isClient = true
|
||||
|
||||
else if @pathPrefix == '/ws/target'
|
||||
@isClient = false
|
||||
|
||||
else
|
||||
utils.pitch "invalid pathPrefix: #{@pathPrefix}"
|
||||
|
||||
@isTarget = !@isClient
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
handle: (request, response, uri) ->
|
||||
|
||||
setCORSHeaders request, response
|
||||
setCacheHeaders request, response
|
||||
|
||||
#-----------------
|
||||
|
||||
# * #{pathPrefix}a
|
||||
if uri[0] != '/'
|
||||
return handleError(request, response, 404)
|
||||
|
||||
#-----------------
|
||||
|
||||
if uri == '/'
|
||||
|
||||
# OPTIONS #{pathPrefix}/
|
||||
if request.method == 'OPTIONS'
|
||||
return handleOptions(request, response)
|
||||
|
||||
# POST #{pathPrefix}/
|
||||
if request.method == 'POST'
|
||||
return handleCreate(@pathPrefix, @isClient, request, response)
|
||||
|
||||
# * #{pathPrefix}/
|
||||
return handleError(request, response, 405)
|
||||
|
||||
#-----------------
|
||||
|
||||
parts = uri.split('/')
|
||||
|
||||
# * #{pathPrefix}/x/y
|
||||
if parts.length > 2
|
||||
return handleError(request, response, 404)
|
||||
|
||||
#-----------------
|
||||
|
||||
channelName = parts[1]
|
||||
|
||||
# OPTIONS #{pathPrefix}/x
|
||||
if request.method == 'OPTIONS'
|
||||
return handleOptions(request, response)
|
||||
|
||||
# GET #{pathPrefix}/x
|
||||
if request.method == 'GET'
|
||||
return handleGet(request, response, channelName)
|
||||
|
||||
# POST #{pathPrefix}/x
|
||||
if request.method == 'POST'
|
||||
return handlePost(request, response, channelName)
|
||||
|
||||
# anything else
|
||||
return handleError(request, response, 405)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
handleCreate = (pathPrefix, isClient, request, response) ->
|
||||
id = request.body?.id
|
||||
|
||||
remoteAddress = request.connection?.remoteAddress || ""
|
||||
|
||||
channel = new Channel(pathPrefix, id, remoteAddress, isClient)
|
||||
|
||||
response.contentType 'application/json'
|
||||
response.send JSON.stringify
|
||||
channel: channel.name
|
||||
id: channel.id
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
handleGet = (request, response, channelName) ->
|
||||
remoteAddress = request.connection?.remoteAddress || ""
|
||||
channel = channelManager.getChannel(channelName, remoteAddress)
|
||||
return handleError(request, response, 404) if !channel
|
||||
|
||||
channel.getMessages (messages) =>
|
||||
return handleError(request, response, 404) if channel.isClosed
|
||||
return handleError(request, response, 404) if !messages
|
||||
|
||||
response.contentType 'application/json'
|
||||
response.send JSON.stringify(messages)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
handlePost = (request, response, channelName) ->
|
||||
remoteAddress = request.connection?.remoteAddress || ""
|
||||
channel = channelManager.getChannel(channelName, remoteAddress)
|
||||
return handleError(request, response, 404) if !channel
|
||||
|
||||
channel.handleMessages(request.body)
|
||||
response.send('')
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
handleOptions = (request, response) ->
|
||||
response.send('')
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
handleError = (request, response, status) ->
|
||||
response.send(status)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
setCORSHeaders = (request, response) ->
|
||||
origin = request.header 'Origin'
|
||||
return if !origin
|
||||
|
||||
response.header 'Access-Control-Allow-Origin', origin
|
||||
response.header 'Access-Control-Max-Age', '600'
|
||||
response.header 'Access-Control-Allow-Methods', 'GET, POST'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
setCacheHeaders = (request, response) ->
|
||||
response.header 'Pragma', 'no-cache'
|
||||
response.header 'Expires', '0'
|
||||
response.header 'Cache-Control', 'no-cache'
|
||||
response.header 'Cache-Control', 'no-store'
|
87
node_modules/weinre/lib-src/MessageQueue.coffee
generated
vendored
Normal file
87
node_modules/weinre/lib-src/MessageQueue.coffee
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = utils.registerClass class MessageQueue
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: () ->
|
||||
@messages = []
|
||||
@closed = false
|
||||
@callback = null
|
||||
@timer = null
|
||||
|
||||
_.bindAll @, '_timerExpired', '_updated'
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
shutdown: () ->
|
||||
return if @closed
|
||||
|
||||
@closed = true
|
||||
|
||||
clearTimeout @timer if @timer
|
||||
@callback.call(null, @messages) if @callback
|
||||
|
||||
@callback = null
|
||||
@messages = null
|
||||
@timer = null
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
push: (message) ->
|
||||
return if @closed
|
||||
|
||||
@messages.push message
|
||||
process.nextTick @_updated
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
pullAll: (timeout, callback) ->
|
||||
return callback.call(null, null) if @closed
|
||||
return callback.call(null, []) if @callback
|
||||
|
||||
if @messages.length
|
||||
callback.call(null, @messages)
|
||||
@messages = []
|
||||
return
|
||||
|
||||
@callback = callback
|
||||
@timer = setTimeout @_timerExpired, timeout
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
_timerExpired: () ->
|
||||
@_updated()
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
_updated: () ->
|
||||
return if @closed
|
||||
return if !@callback
|
||||
|
||||
callback = @callback
|
||||
messages = @messages
|
||||
clearTimeout @timer if @timer
|
||||
|
||||
@callback = null
|
||||
@messages = []
|
||||
@timer = null
|
||||
|
||||
callback.call(null, messages)
|
||||
|
126
node_modules/weinre/lib-src/channelManager.coffee
generated
vendored
Normal file
126
node_modules/weinre/lib-src/channelManager.coffee
generated
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
serviceManager = require './serviceManager'
|
||||
|
||||
WeinreClientEvents = null
|
||||
WeinreTargetEvents = null
|
||||
|
||||
channelManager = null
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.registerClass class ChannelManager
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: () ->
|
||||
@channels = {}
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
initialize: ->
|
||||
|
||||
WeinreClientEvents = serviceManager.get 'WeinreClientEvents'
|
||||
WeinreTargetEvents = serviceManager.get 'WeinreTargetEvents'
|
||||
|
||||
if !WeinreClientEvents
|
||||
utils.exit 'WeinreClientEvents service not registered'
|
||||
|
||||
if !WeinreTargetEvents
|
||||
utils.exit 'WeinreTargetEvents service not registered'
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
created: (channel) ->
|
||||
@channels[channel.name] = channel
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
destroyed: (channel) ->
|
||||
if channel.isClient
|
||||
for connection in channel.connections
|
||||
@disconnectChannels(channel, connection)
|
||||
else
|
||||
for connection in channel.connections
|
||||
@disconnectChannels(connection, channel)
|
||||
|
||||
clients = @getClientChannels(channel.id)
|
||||
|
||||
if channel.isClient
|
||||
WeinreClientEvents.clientUnregistered(clients, channel.name)
|
||||
else
|
||||
WeinreClientEvents.targetUnregistered(clients, channel.name)
|
||||
|
||||
delete @channels[channel.name]
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getChannel: (name, remoteAddress) ->
|
||||
return null if !_.has(@channels, name)
|
||||
|
||||
channel = @channels[name]
|
||||
|
||||
return null if !channel
|
||||
|
||||
# if remoteAddress
|
||||
# return null if channel.remoteAddress != remoteAddress
|
||||
|
||||
channel
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
connectChannels: (client, target) ->
|
||||
return if client.isClosed or target.isClosed
|
||||
|
||||
if client.connections.length
|
||||
@disconnectChannels(client, client.connections[0])
|
||||
|
||||
client.connections.push target
|
||||
target.connections.push client
|
||||
|
||||
clients = @getClientChannels(client.id)
|
||||
|
||||
WeinreClientEvents.connectionCreated(clients, client.name, target.name)
|
||||
WeinreTargetEvents.connectionCreated(target, client.name, target.name)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
disconnectChannels: (client, target) ->
|
||||
|
||||
clients = @getClientChannels(client.id)
|
||||
|
||||
WeinreClientEvents.connectionDestroyed(clients, client.name, target.name)
|
||||
WeinreTargetEvents.connectionDestroyed(target, client.name, target.name)
|
||||
|
||||
client.connections = _.without(client.connections, target)
|
||||
target.connections = _.without(target.connections, client)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getChannels: (id) ->
|
||||
if id?
|
||||
_.filter(@channels, (item) -> item.id == id)
|
||||
else
|
||||
_.values(@channels)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getClientChannels: (id) ->
|
||||
_.filter(@channels, (item) -> item.isClient && item.id == id)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getTargetChannels: (id) ->
|
||||
_.filter(@channels, (item) -> item.isTarget && item.id == id)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = new ChannelManager
|
130
node_modules/weinre/lib-src/cli.coffee
generated
vendored
Normal file
130
node_modules/weinre/lib-src/cli.coffee
generated
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
fs = require 'fs'
|
||||
path = require 'path'
|
||||
|
||||
_ = require 'underscore'
|
||||
nopt = require 'nopt'
|
||||
|
||||
utils = require './utils'
|
||||
weinre = require './weinre'
|
||||
|
||||
optionDefaults =
|
||||
httpPort: 8080
|
||||
boundHost: 'localhost'
|
||||
verbose: false
|
||||
debug: false
|
||||
readTimeout: 5
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
exports.run = ->
|
||||
|
||||
knownOpts =
|
||||
httpPort: Number
|
||||
boundHost: String
|
||||
verbose: Boolean
|
||||
debug: Boolean
|
||||
readTimeout: Number
|
||||
deathTimeout: Number
|
||||
help: Boolean
|
||||
|
||||
shortHands =
|
||||
'?': ['--help']
|
||||
'h': ['--help']
|
||||
|
||||
nopt.invalidHandler = printNoptError
|
||||
parsedOpts = nopt(knownOpts, shortHands, process.argv, 2)
|
||||
|
||||
#----
|
||||
|
||||
printHelp() if parsedOpts.help
|
||||
|
||||
args = parsedOpts.argv.remain
|
||||
|
||||
printHelp() if args.length != 0
|
||||
|
||||
#----
|
||||
|
||||
delete parsedOpts.argv
|
||||
opts = _.extend {}, optionDefaults, getDotWeinreServerProperties(), parsedOpts
|
||||
|
||||
if !opts.deathTimeout?
|
||||
opts.deathTimeout = 3 * opts.readTimeout
|
||||
|
||||
utils.setOptions opts
|
||||
|
||||
weinre.run opts
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
printNoptError = (key, val, types) ->
|
||||
utils.exit "error with option '#{key}', value '#{val}'"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
printHelp = () ->
|
||||
version = weinre.getVersion()
|
||||
|
||||
console.error """
|
||||
usage: #{utils.Program} [options]
|
||||
version: #{version}
|
||||
|
||||
options:
|
||||
--httpPort port to run the http server on default: #{optionDefaults.httpPort}
|
||||
--boundHost ip address to bind the server to default: #{optionDefaults.boundHost}
|
||||
--verbose print more diagnostics default: #{optionDefaults.verbose}
|
||||
--debug print even more diagnostics default: #{optionDefaults.debug}
|
||||
--readTimeout seconds to wait for a client message default: #{optionDefaults.readTimeout}
|
||||
--deathTimeout seconds to wait to kill client default: 3*readTimeout
|
||||
|
||||
--boundHost can be an ip address, hostname, or -all-, where -all-
|
||||
means binding to all ip address on the current machine'
|
||||
|
||||
for more info see: http://people.apache.org/~pmuellr/weinre/
|
||||
"""
|
||||
process.exit()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
getDotWeinreServerProperties = () ->
|
||||
properties = {}
|
||||
|
||||
fileName = replaceTilde '~/.weinre/server.properties'
|
||||
return properties if !utils.fileExistsSync(fileName)
|
||||
|
||||
contents = fs.readFileSync(fileName, 'utf8')
|
||||
lines = contents.split('\n')
|
||||
|
||||
for line in lines
|
||||
line = line.replace(/#.*/,'')
|
||||
match = line.match /\s*(\w+)\s*:\s*(.+)\s*/
|
||||
continue if !match
|
||||
|
||||
key = utils.trim match[1]
|
||||
val = utils.trim match[2]
|
||||
|
||||
properties[key] = val
|
||||
|
||||
properties
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
replaceTilde = (fileName) ->
|
||||
fileName.replace('~', getTildeReplacement())
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
getTildeReplacement = () ->
|
||||
process.env["HOME"] || process.env["USERPROFILE"] || '.'
|
77
node_modules/weinre/lib-src/dumpingHandler.coffee
generated
vendored
Normal file
77
node_modules/weinre/lib-src/dumpingHandler.coffee
generated
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
dumpingHandler = (request, response, uri) ->
|
||||
originalSend = response.send
|
||||
response.send = (body) ->
|
||||
dumpResponse(originalSend, body, request, response, uri)
|
||||
|
||||
return if request.method != 'POST'
|
||||
|
||||
utils.logVerbose '--------------------------------------------------'
|
||||
utils.logVerbose "#{request.method} #{uri} [request]"
|
||||
|
||||
if _.isArray(request.body)
|
||||
for element in request.body
|
||||
utils.logVerbose " #{enhance(JSON.parse(element))}"
|
||||
else
|
||||
utils.logVerbose " #{enhance(request.body)}"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
dumpResponse = (originalSend, body, request, response, uri) ->
|
||||
originalSend.call(response, body)
|
||||
|
||||
return if request.method not in ['GET', 'POST']
|
||||
|
||||
try
|
||||
body = JSON.parse(body)
|
||||
catch e
|
||||
return
|
||||
|
||||
return if _.isArray(body) && (body.length == 0)
|
||||
|
||||
utils.logVerbose '--------------------------------------------------'
|
||||
utils.logVerbose "#{request.method} #{uri} #{response.statusCode} [response]"
|
||||
|
||||
if _.isArray(body)
|
||||
for element in body
|
||||
utils.logVerbose " #{enhance(JSON.parse(element))}"
|
||||
else
|
||||
utils.logVerbose " #{enhance(body)}"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
enhance = (object) ->
|
||||
if !object.interface || !object.method || !object.args
|
||||
return JSON.stringify(object)
|
||||
|
||||
signature = "#{object.interface}.#{object.method}"
|
||||
|
||||
args = JSON.stringify(object.args)
|
||||
if args.length > 500
|
||||
args = "#{args.substr(0,50)}..."
|
||||
|
||||
return "#{signature}(#{args})"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = dumpingHandler
|
30
node_modules/weinre/lib-src/extensionManager.coffee
generated
vendored
Normal file
30
node_modules/weinre/lib-src/extensionManager.coffee
generated
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
utils = require './utils'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.registerClass class ExtensionManager
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: () ->
|
||||
@extensions = []
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = new ExtensionManager
|
47
node_modules/weinre/lib-src/jsonBodyParser.coffee
generated
vendored
Normal file
47
node_modules/weinre/lib-src/jsonBodyParser.coffee
generated
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
jsonBodyParser = ->
|
||||
return (request, response, next) -> parseBodyAsJSON(request, response, next)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
parseBodyAsJSON = (request, response, next) ->
|
||||
|
||||
return next() if request.body
|
||||
|
||||
request.body = {}
|
||||
|
||||
return next() if request.method != 'POST'
|
||||
|
||||
request.setEncoding 'utf8'
|
||||
|
||||
buffer = ''
|
||||
request.on 'data', (chunk) -> buffer += chunk
|
||||
request.on 'end', ->
|
||||
return next() if '' == buffer
|
||||
|
||||
try
|
||||
request.body = JSON.parse(buffer)
|
||||
next()
|
||||
catch e
|
||||
next(e)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = jsonBodyParser
|
59
node_modules/weinre/lib-src/messageHandler.coffee
generated
vendored
Normal file
59
node_modules/weinre/lib-src/messageHandler.coffee
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
utils = require './utils'
|
||||
channelManager = require './channelManager'
|
||||
serviceManager = require './serviceManager'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.registerClass class MessageHandler
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
handleMessage: (channel, message) ->
|
||||
@_serviceMethodInvoker(channel, message.interface, message.method, message.args)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
_serviceMethodInvoker: (channel, intfName, method, args) ->
|
||||
methodSignature = "#{intfName}.#{method}()"
|
||||
# utils.logVerbose "MessageHandler._serviceMethodInvoker(#{methodSignature})"
|
||||
|
||||
service = serviceManager.get(intfName)
|
||||
|
||||
if !service
|
||||
return @_redirectToConnections(channel, intfName, method, args)
|
||||
|
||||
args = args.slice()
|
||||
args.unshift channel
|
||||
|
||||
try
|
||||
service[method].apply(service, args)
|
||||
|
||||
catch e
|
||||
utils.log "error running service method #{methodSignature}: #{e}"
|
||||
utils.log "stack:\n#{e.stack}"
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
_redirectToConnections: (channel, intfName, method, args) ->
|
||||
# utils.logVerbose "MessageHandler._redirectToConnections(#{channel.name}, #{intfName}, #{method})"
|
||||
|
||||
for connection in channel.connections
|
||||
connection.sendMessage(intfName, method, args...)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = new MessageHandler
|
126
node_modules/weinre/lib-src/service/WeinreClientCommands.coffee
generated
vendored
Normal file
126
node_modules/weinre/lib-src/service/WeinreClientCommands.coffee
generated
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
_ = require('underscore')
|
||||
|
||||
weinre = require '../weinre'
|
||||
utils = require '../utils'
|
||||
channelManager = require '../channelManager'
|
||||
serviceManager = require '../serviceManager'
|
||||
extensionManager = require '../extensionManager'
|
||||
|
||||
WeinreClientEvents = serviceManager.get 'WeinreClientEvents'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = utils.registerClass class WeinreClientCommands
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
registerClient: (channel, callbackId) ->
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId, channel.description)
|
||||
|
||||
options = _.extend {}, utils.options
|
||||
for own key, val of options
|
||||
if typeof val in ['number', 'boolean']
|
||||
options[key] = "#{val}"
|
||||
|
||||
options.version = weinre.getVersion()
|
||||
|
||||
WeinreClientEvents.serverProperties(channel, options)
|
||||
|
||||
clients = channelManager.getClientChannels(channel.id)
|
||||
WeinreClientEvents.clientRegistered(clients, channel.description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getTargets: (channel, callbackId) ->
|
||||
channels = channelManager.getTargetChannels(channel.id)
|
||||
result = _.pluck(channels, 'description')
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId, [result])
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getClients: (channel, callbackId) ->
|
||||
channels = channelManager.getClientChannels(channel.id)
|
||||
result = _.pluck(channels, 'description')
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId, [result])
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getExtensions: (channel, callbackId) ->
|
||||
result = for extension in extensionManager.extensions
|
||||
{ startPage: "extensions/#{extension}/extension.html" }
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId, [result])
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
connectTarget: (channel, clientName, targetName, callbackId) ->
|
||||
client = channelManager.getChannel(clientName)
|
||||
return if !client
|
||||
|
||||
target = channelManager.getChannel(targetName)
|
||||
return if !target
|
||||
|
||||
channelManager.connectChannels(client, target)
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
disconnectTarget: (channel, clientName, callbackId) ->
|
||||
client = connectionManager.getClient(clientName)
|
||||
return if !client
|
||||
|
||||
target = client.getConnectedTarget()
|
||||
return if !target
|
||||
|
||||
connectionManager.disconnect(client, target)
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logDebug: (channel, message, callbackId) ->
|
||||
utils.logVerbose "client #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logInfo: (channel, message, callbackId) ->
|
||||
utils.log "client #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logWarning: (channel, message, callbackId) ->
|
||||
utils.log "client #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logError: (channel, message, callbackId) ->
|
||||
utils.log "client #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreClientEvents.sendCallback(channel, callbackId)
|
90
node_modules/weinre/lib-src/service/WeinreTargetCommands.coffee
generated
vendored
Normal file
90
node_modules/weinre/lib-src/service/WeinreTargetCommands.coffee
generated
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
utils = require '../utils'
|
||||
channelManager = require '../channelManager'
|
||||
serviceManager = require '../serviceManager'
|
||||
|
||||
WeinreClientEvents = serviceManager.get 'WeinreClientEvents'
|
||||
WeinreTargetEvents = serviceManager.get 'WeinreTargetEvents'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = utils.registerClass class WeinreTargetCommands
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
registerTarget: (channel, url, callbackId) ->
|
||||
channel.description.url = url
|
||||
|
||||
clients = channelManager.getClientChannels(channel.id)
|
||||
WeinreClientEvents.targetRegistered(clients, channel.description)
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, channel.description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
sendClientCallback: (channel, clientCallbackId, args, callbackId) ->
|
||||
|
||||
# the channel to send the callback to is embedded in the callbackId
|
||||
callbackChannel = getCallbackChannel(clientCallbackId)
|
||||
if !callbackChannel
|
||||
return main.warn "#{@constructor.name}.sendClientCallback() sent with invalid callbackId: #{clientCallbackId}"
|
||||
|
||||
callbackChannel = channelManager.getChannel(callbackChannel)
|
||||
if !callbackChannel
|
||||
# indication that channel was closed; this message may generate a lot of noise
|
||||
return main.warn "#{@constructor.name}.sendClientCallback() unable to find channel : #{clientCallbackId}"
|
||||
|
||||
WeinreClientEvents.sendCallback(callbackChannel, clientCallbackId, args)
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logDebug: (channel, message, callbackId) ->
|
||||
utils.logVerbose "target #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logInfo: (channel, message, callbackId) ->
|
||||
utils.log "target #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logWarning: (channel, message, callbackId) ->
|
||||
utils.log "target #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
logError: (channel, message, callbackId) ->
|
||||
utils.log "target #{channel.name}: #{message}"
|
||||
|
||||
if callbackId
|
||||
WeinreTargetEvents.sendCallback(channel, callbackId, description)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
getCallbackChannel = (callbackId) ->
|
||||
callbackId = callbackId.toString()
|
||||
callbackId.split('::')[0]
|
||||
|
95
node_modules/weinre/lib-src/serviceManager.coffee
generated
vendored
Normal file
95
node_modules/weinre/lib-src/serviceManager.coffee
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
path = require 'path'
|
||||
fs = require 'fs'
|
||||
|
||||
_ = require 'underscore'
|
||||
|
||||
utils = require './utils'
|
||||
|
||||
Services = {}
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.registerClass class ServiceManager
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
constructor: ->
|
||||
@services = {}
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
get: (name) ->
|
||||
return @services[name] if _.has(@services, name)
|
||||
|
||||
null
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
registerLocalClass: (name) ->
|
||||
|
||||
serviceClass = null
|
||||
try
|
||||
serviceClass = require "./service/#{name}"
|
||||
catch e
|
||||
utils.log "local service class not found: #{name}"
|
||||
throw e
|
||||
|
||||
@services[name] = new serviceClass
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
registerProxyClass: (name) ->
|
||||
|
||||
intf = getServiceInterface(name)
|
||||
|
||||
if !intf
|
||||
utils.exit "proxy service class not found: #{name}"
|
||||
|
||||
if intf.name != name
|
||||
utils.exit "proxy interface '#{intf.name}' loaded when '#{name}' requested"
|
||||
|
||||
service = {}
|
||||
|
||||
for method in intf.methods
|
||||
service[method.name] = getMethodProxy(name, method.name)
|
||||
|
||||
@services[name] = service
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
getMethodProxy = (intfName, methodName) ->
|
||||
(channels, args...) ->
|
||||
channels = [channels] if !_.isArray(channels)
|
||||
|
||||
for channel in channels
|
||||
channel.sendMessage(intfName, methodName, args...)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
getServiceInterface = (name) ->
|
||||
jsonName = "#{name}.json"
|
||||
fileName = path.join utils.options.staticWebDir, 'interfaces', jsonName
|
||||
|
||||
return null if !utils.fileExistsSync(fileName)
|
||||
|
||||
contents = fs.readFileSync(fileName, 'utf8')
|
||||
|
||||
serviceInterface = JSON.parse(contents)
|
||||
|
||||
return serviceInterface.interfaces[0]
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
module.exports = new ServiceManager
|
196
node_modules/weinre/lib-src/utils.coffee
generated
vendored
Normal file
196
node_modules/weinre/lib-src/utils.coffee
generated
vendored
Normal file
@ -0,0 +1,196 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
fs = require 'fs'
|
||||
path = require 'path'
|
||||
|
||||
utils = exports
|
||||
|
||||
utils.Program = Program = path.basename process.argv[1]
|
||||
|
||||
SequenceNumberMax = 100 * 1024 * 1024
|
||||
SequenceNumber = 0
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.getNextSequenceNumber = (g) ->
|
||||
SequenceNumber++
|
||||
|
||||
if SequenceNumber > SequenceNumberMax
|
||||
SequenceNumber = 0
|
||||
|
||||
SequenceNumber
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.trim = (string) ->
|
||||
string.replace(/(^\s+)|(\s+$)/g,'')
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.log = log = (message) ->
|
||||
date = new Date()
|
||||
time = date.toISOString()
|
||||
console.log "#{time} #{Program}: #{message}"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.logVerbose = (message) ->
|
||||
return if !utils?.options?.verbose
|
||||
|
||||
log message
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.logDebug = (message) ->
|
||||
return if !utils?.options?.debug
|
||||
|
||||
log message
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.exit = (message) ->
|
||||
log message
|
||||
process.exit 1
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.pitch = (message) ->
|
||||
log message
|
||||
throw message
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.setOptions = (options) ->
|
||||
utils.options = options
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.ensureInteger = (value, message) ->
|
||||
newValue = parseInt value
|
||||
|
||||
if isNaN newValue
|
||||
utils.exit "#{message}: '#{value}'"
|
||||
|
||||
newValue
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.ensureString = (value, message) ->
|
||||
|
||||
if typeof value != 'string'
|
||||
utils.exit "#{message}: '#{value}'"
|
||||
|
||||
value
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.ensureBoolean = (value, message) ->
|
||||
uValue = value.toString().toUpperCase()
|
||||
|
||||
newValue = null
|
||||
switch uValue
|
||||
when 'TRUE' then newValue = true
|
||||
when 'FALSE' then newValue = false
|
||||
|
||||
if typeof(newValue) != 'boolean'
|
||||
utils.exit "#{message}: '#{value}'"
|
||||
|
||||
newValue
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.setNamesForClass = (aClass) ->
|
||||
|
||||
for own key, val of aClass
|
||||
if typeof(val) is "function"
|
||||
val.signature = "#{aClass.name}::#{key}"
|
||||
val.displayName = val.signature
|
||||
val.name = val.signature
|
||||
|
||||
for own key, val of aClass.prototype
|
||||
if typeof(val) is "function"
|
||||
val.signature = "#{aClass.name}.#{key}"
|
||||
val.displayName = val.signature
|
||||
val.name = val.signature
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.registerClass = (aClass) ->
|
||||
utils.setNamesForClass(aClass)
|
||||
aClass
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.alignLeft = (string, length) ->
|
||||
while string.length < length
|
||||
string = "#{string} "
|
||||
|
||||
string
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.alignRight = (string, length) ->
|
||||
while string.length < length
|
||||
string = " #{string}"
|
||||
|
||||
string
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
utils.fileExistsSync = (name) ->
|
||||
|
||||
if fs.existsSync
|
||||
return fs.existsSync name
|
||||
|
||||
return path.existsSync(name)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
Error.prepareStackTrace = (error, structuredStackTrace) ->
|
||||
result = []
|
||||
result.push "---------------------------------------------------------"
|
||||
result.push "error: #{error}"
|
||||
result.push "---------------------------------------------------------"
|
||||
result.push "stack: "
|
||||
|
||||
longestFile = 0
|
||||
longestLine = 0
|
||||
|
||||
for callSite in structuredStackTrace
|
||||
file = callSite.getFileName()
|
||||
line = callSite.getLineNumber()
|
||||
|
||||
file = path.basename(file)
|
||||
line = "#{line}"
|
||||
|
||||
if file.length > longestFile
|
||||
longestFile = file.length
|
||||
|
||||
if line.length > longestLine
|
||||
longestLine = line.length
|
||||
|
||||
for callSite in structuredStackTrace
|
||||
func = callSite.getFunction()
|
||||
file = callSite.getFileName()
|
||||
line = callSite.getLineNumber()
|
||||
|
||||
file = path.basename(file)
|
||||
line = "#{line}"
|
||||
|
||||
file = utils.alignRight(file, longestFile)
|
||||
line = utils.alignLeft( line, longestLine)
|
||||
|
||||
funcName = func.displayName ||
|
||||
func.name ||
|
||||
callSite.getFunctionName()
|
||||
callSite.getMethodName()
|
||||
'???'
|
||||
|
||||
if funcName == "Module._compile"
|
||||
result.pop()
|
||||
result.pop()
|
||||
break
|
||||
|
||||
result.push " #{file}:#{line} - #{funcName}()"
|
||||
|
||||
result.join "\n"
|
186
node_modules/weinre/lib-src/weinre.coffee
generated
vendored
Normal file
186
node_modules/weinre/lib-src/weinre.coffee
generated
vendored
Normal file
@ -0,0 +1,186 @@
|
||||
#-------------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
fs = require 'fs'
|
||||
net = require 'net'
|
||||
dns = require 'dns'
|
||||
path = require 'path'
|
||||
|
||||
_ = require 'underscore'
|
||||
express = require 'express'
|
||||
|
||||
utils = require './utils'
|
||||
jsonBodyParser = require './jsonBodyParser'
|
||||
HttpChannelHandler = require './HttpChannelHandler'
|
||||
dumpingHandler = require './dumpingHandler'
|
||||
channelManager = require './channelManager'
|
||||
serviceManager = require './serviceManager'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
exports.run = (options) ->
|
||||
processOptions(options, run2)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
run2 = ->
|
||||
options = utils.options
|
||||
|
||||
serviceManager.registerProxyClass 'WeinreClientEvents'
|
||||
serviceManager.registerProxyClass 'WeinreTargetEvents'
|
||||
serviceManager.registerLocalClass 'WeinreClientCommands'
|
||||
serviceManager.registerLocalClass 'WeinreTargetCommands'
|
||||
|
||||
startDeathWatcher options.deathTimeout
|
||||
|
||||
startServer()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
processOptions = (options, cb) ->
|
||||
options.httpPort = utils.ensureInteger( options.httpPort, 'the value of the option httpPort is not a number')
|
||||
options.boundHost = utils.ensureString( options.boundHost, 'the value of the option boundHost is not a string')
|
||||
options.verbose = utils.ensureBoolean( options.verbose, 'the value of the option verbose is not a boolean')
|
||||
options.debug = utils.ensureBoolean( options.debug, 'the value of the option debug is not a boolean')
|
||||
options.readTimeout = utils.ensureInteger( options.readTimeout, 'the value of the option readTimeout is not a number')
|
||||
options.deathTimeout = utils.ensureInteger( options.deathTimeout, 'the value of the option deathTimeout is not a number')
|
||||
|
||||
options.verbose = true if options.debug
|
||||
|
||||
options.staticWebDir = getStaticWebDir()
|
||||
|
||||
utils.logVerbose "pid: #{process.pid}"
|
||||
utils.logVerbose "version: #{getVersion()}"
|
||||
utils.logVerbose "node versions:"
|
||||
|
||||
names = _.keys(process.versions)
|
||||
reducer = (memo, name) -> Math.max(memo, name.length)
|
||||
nameLen = _.reduce(names, reducer, 0)
|
||||
|
||||
for name in names
|
||||
utils.logVerbose " #{utils.alignLeft(name, nameLen)}: #{process.versions[name]}"
|
||||
|
||||
utils.logVerbose "options:"
|
||||
utils.logVerbose " httpPort: #{options.httpPort}"
|
||||
utils.logVerbose " boundHost: #{options.boundHost}"
|
||||
utils.logVerbose " verbose: #{options.verbose}"
|
||||
utils.logVerbose " debug: #{options.debug}"
|
||||
utils.logVerbose " readTimeout: #{options.readTimeout}"
|
||||
utils.logVerbose " deathTimeout: #{options.deathTimeout}"
|
||||
|
||||
utils.setOptions options
|
||||
|
||||
checkHost options.boundHost, (err) ->
|
||||
if err
|
||||
utils.exit "unable to resolve boundHost address: #{options.boundHost}"
|
||||
|
||||
cb()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
checkHost = (hostName, cb) ->
|
||||
return cb() if hostName == '-all-'
|
||||
return cb() if hostName == 'localhost'
|
||||
|
||||
return cb() if net.isIP(hostName)
|
||||
|
||||
dns.lookup hostName, cb
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
deathTimeout = null
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
startDeathWatcher = (timeout) ->
|
||||
deathTimeout = utils.options.deathTimeout * 1000
|
||||
|
||||
setInterval checkForDeath, 1000
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
checkForDeath = ->
|
||||
now = (new Date).valueOf()
|
||||
for channel in channelManager.getChannels()
|
||||
if now - channel.lastRead > deathTimeout
|
||||
channel.close()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
startServer = () ->
|
||||
options = utils.options
|
||||
|
||||
clientHandler = new HttpChannelHandler('/ws/client')
|
||||
targetHandler = new HttpChannelHandler('/ws/target')
|
||||
|
||||
channelManager.initialize()
|
||||
|
||||
favIcon = "#{options.staticWebDir}/images/weinre-icon-32x32.png"
|
||||
|
||||
staticCacheOptions =
|
||||
maxObjects: 500
|
||||
maxLength: 32 * 1024 * 1024
|
||||
|
||||
app = express.createServer()
|
||||
|
||||
app.on 'error', (error) ->
|
||||
utils.exit "error running server: #{error}"
|
||||
|
||||
app.use express.favicon(favIcon)
|
||||
|
||||
app.use jsonBodyParser()
|
||||
|
||||
app.all /^\/ws\/client(.*)/, (request, response, next) ->
|
||||
uri = request.params[0]
|
||||
uri = '/' if uri == ''
|
||||
|
||||
dumpingHandler(request, response, uri) if options.debug
|
||||
clientHandler.handle(request, response, uri)
|
||||
|
||||
app.all /^\/ws\/target(.*)/, (request, response, next) ->
|
||||
uri = request.params[0]
|
||||
uri = '/' if uri == ''
|
||||
|
||||
dumpingHandler(request, response, uri) if options.debug
|
||||
targetHandler.handle(request, response, uri)
|
||||
|
||||
app.use express.errorHandler(dumpExceptions: true)
|
||||
|
||||
app.use express.staticCache(staticCacheOptions)
|
||||
app.use express.static(options.staticWebDir)
|
||||
|
||||
if options.boundHost == '-all-'
|
||||
utils.log "starting server at http://localhost:#{options.httpPort}"
|
||||
app.listen options.httpPort
|
||||
|
||||
else
|
||||
utils.log "starting server at http://#{options.boundHost}:#{options.httpPort}"
|
||||
app.listen options.httpPort, options.boundHost
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
getStaticWebDir = () ->
|
||||
webDir = path.normalize path.join(__dirname,'../web')
|
||||
return webDir if utils.fileExistsSync webDir
|
||||
|
||||
utils.exit 'unable to find static files to serve in #{webDir}; did you do a build?'
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
Version = null
|
||||
getVersion = exports.getVersion = () ->
|
||||
return Version if Version
|
||||
|
||||
packageJsonName = path.join(path.dirname(fs.realpathSync(__filename)), '../package.json')
|
||||
|
||||
json = fs.readFileSync(packageJsonName, 'utf8')
|
||||
values = JSON.parse(json)
|
||||
|
||||
Version = values.version
|
||||
return Version
|
Reference in New Issue
Block a user