-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.coffee
76 lines (59 loc) · 2.56 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
globals = @
INSTANCES = parseInt(process.env.PEERDB_INSTANCES ? 1)
INSTANCE = parseInt(process.env.PEERDB_INSTANCE ? 0)
throw new Error "Invalid number of instances: #{ INSTANCES }" unless 0 <= INSTANCES <= share.UNMISTAKABLE_CHARS.length
throw new Error "Invalid instance index: #{ INSTANCE }" unless (INSTANCES is 0 and INSTANCE is 0) or 0 <= INSTANCE < INSTANCES
MESSAGES_TTL = 60 # seconds
# We augment the cursor so that it matches our extra method in documents manager.
MeteorCursor = Object.getPrototypeOf(MongoInternals.defaultRemoteCollectionDriver().mongo.find()).constructor
MeteorCursor::exists = ->
# You can only observe a tailable cursor.
throw new Error "Cannot call exists on a tailable cursor" if @_cursorDescription.options.tailable
unless @_synchronousCursorForExists
# A special cursor with limit forced to 1 and fields to only _id.
cursorDescription = _.clone @_cursorDescription
cursorDescription.options = _.clone cursorDescription.options
cursorDescription.options.limit = 1
cursorDescription.options.fields =
_id: 1
@_synchronousCursorForExists = @_mongo._createSynchronousCursor cursorDescription,
selfForIteration: @
useTransform: false
@_synchronousCursorForExists._rewind()
!!@_synchronousCursorForExists._nextObject()
# Fields:
# created
# type
# data
# We use a lower case collection name to signal it is a system collection
globals.Document.Messages = new Mongo.Collection 'peerdb.messages'
# Auto-expire messages after MESSAGES_TTL seconds
globals.Document.Messages._ensureIndex
created: 1
,
expireAfterSeconds: MESSAGES_TTL
class globals.Document extends globals.Document
@updateAll: ->
sendMessage 'updateAll'
sendMessage = (type, data) ->
globals.Document.Messages.insert
created: new Date()
type: type
data: data
share.setupMessages = ->
initializing = true
globals.Document.Messages.find({}).observeChanges
added: (id, fields) ->
return if initializing
switch fields.type
when 'updateAll'
globals.Document._updateAll()
else
Log.error "Unknown message type '#{ fields.type }': " + util.inspect _.extend({}, {_id: id}, fields), false, null
initializing = false
globals.Document.instanceDisabled = INSTANCES is 0
globals.Document.instances = INSTANCES
globals.Document.instance = INSTANCE
Document = globals.Document
assert globals.Document._ReferenceField.prototype instanceof globals.Document._TargetedFieldsObservingField
assert globals.Document._GeneratedField.prototype instanceof globals.Document._TargetedFieldsObservingField