Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #218 Problems with missing views in CouchDB #219

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions package.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports =
# Useragent hashing
hat: "*"

# for logging
winston: "*"

# Developer dependencies
devDependencies:
# Example server
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"connect": "<3.x",
"request": ">= 2.1.1",
"coffee-script": "<1.7",
"hat": "*"
"hat": "*",
"winston": "*"
},
"devDependencies": {
"express": "~ 3.x",
Expand Down
1 change: 1 addition & 0 deletions src/client/connection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ else
{BCSocket} = require 'browserchannel'
Doc = require('./doc').Doc
WebSocket = require 'ws'
{ReconnectingWebSocket} = require "./reconnecting_websocket"
socketImpl = null

class Connection
Expand Down
10 changes: 10 additions & 0 deletions src/client/reconnecting_websocket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Contributors:
- Didier Colens
- Wout Mertens
###

unless WEB?
WebSocket = require 'ws'


class ReconnectingWebSocket
constructor: (url, protocols, Socket) ->
if protocols? and typeof protocols is 'function'
Expand Down Expand Up @@ -139,3 +144,8 @@ class ReconnectingWebSocket
###
refresh: ->
@ws.close() if @ws


# for node server side client
unless WEB?
exports.ReconnectingWebSocket = ReconnectingWebSocket
21 changes: 21 additions & 0 deletions src/server/db/couchdb.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,24 @@ module.exports = (options) ->
, {rev: body._rev} # dbMeta

close: ->

# checks if there are all required views already registered in the system
open: ->
expectedMapFnc = "function(doc) {\n if (doc.docName!=null){\n\t emit(null, doc.docName);\n }\n}"
request "#{db}/_design/sharejs", (err, resp, body) ->
currentmap = body?.views?.operations?.map;

if currentmap isnt expectedMapFnc or body?language isnt "javascript"
operView =
_id:"_design/sharejs"
language: "javascript"
views:
operations:
map: expectedMapFnc
operView._rev = body._rev if body._rev? # to avoid conflict we need to set the current doc version if available

request.put "#{db}/_design/sharejs", body:operView, (err, resp, body) ->
if body?.ok is true
console.log "Updated views successfuly."
else
console.log "Problem updating requested view occured. Please check your view settings."
2 changes: 2 additions & 0 deletions src/server/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ create.attach = attach = (server, options, model = createModel(options)) ->
options.staticpath ?= '/share'

server.model = model
model.openDb()

server.on 'close', -> model.closeDb()

server.use options.staticpath, connect.static("#{__dirname}/../../webclient") if options.staticpath != null
Expand Down
5 changes: 5 additions & 0 deletions src/server/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ module.exports = Model = (db, options) ->
db?.close?()
db = null

# Run all necessary preparatory operations. Indexes, views etc.
@openDb = ->
db?.open?()


return

# Model inherits from EventEmitter.
Expand Down
5 changes: 4 additions & 1 deletion src/server/session.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

hat = require 'hat'

syncQueue = require './syncqueue'
syncQueue = require './syncqueue'
winston = require('winston')
sharejslog = winston.loggers.get('sharejslog')

# Time (in ms) that the server will wait for an auth message from the client before closing the connection
AUTH_TIMEOUT = 10000
Expand Down Expand Up @@ -134,6 +136,7 @@ exports.handler = (session, createAgent) ->
# session has closed.
if session.ready()
session.send response
sharejslog.debug JSON.stringify(response), {response:true}

# Open the given document name, at the requested version.
# callback(error, version)
Expand Down
8 changes: 6 additions & 2 deletions src/server/websocket.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# This implements the WebSocket network API for ShareJS.
EventEmitter = require('events').EventEmitter
WebSocketServer = require('ws').Server
winston = require('winston')
sharejslog = winston.loggers.get('sharejslog')

sessionHandler = require('./session').handler

wrapSession = (conn) ->
wrapSession = (conn, options) ->
wrapper = new EventEmitter
wrapper.abort = -> conn.close()
wrapper.stop = -> conn.close()
Expand All @@ -15,6 +17,8 @@ wrapSession = (conn) ->
try
parsed = JSON.parse data
wrapper.emit 'message', parsed

sharejslog.debug data.toString(), {request:true}
catch error
console.log "Received data parsing error #{error}"

Expand All @@ -26,4 +30,4 @@ wrapSession = (conn) ->
exports.attach = (server, createAgent, options) ->
options.prefix or= '/websocket'
wss = new WebSocketServer {server: server, path: options.prefix}
wss.on 'connection', (conn) -> sessionHandler wrapSession(conn), createAgent
wss.on 'connection', (conn) -> sessionHandler wrapSession(conn, options), createAgent