-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpouch-service.js
174 lines (151 loc) · 4.94 KB
/
pouch-service.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var COUCHDB_URL = 'http://skimdb.iriscouch.com/registry';
//var COUCHDB_URL = 'http://localhost:5984/skimdb';
var INIT_REPL_BASE_URL = 'https://nolanlawson.s3.amazonaws.com/npm-browser';
//var INIT_REPL_BASE_URL = '/www';
var NUM_DUMP_FILES = 222;
function getDumpFilenameForNumber(i) {
var numStr = i.toString();
while (numStr.length < 8) {
numStr = '0' + numStr;
}
return INIT_REPL_BASE_URL + '/npm_' + numStr + '.txt';
}
function PouchService (utils, $rootScope) {
var self = this;
// fall back from websql to indexedb for performance
self.localPouch = new PouchDB('npm', {adapter: 'websql', size: 2000});
if (!self.localPouch.adapter) {
self.localPouch = new PouchDB('npm');
}
self.localPouch.filter({
incoming: function (doc) {
// filter fields we don't need
doc = utils.pick(doc, [
'_id', '_rev', 'name', 'description', 'dist-tags', 'versions',
'readme', 'maintainers', 'time'
]);
if (doc['dist-tags'] && doc['dist-tags'].latest) {
var latest = doc['dist-tags'].latest;
if (doc.time) {
doc.time = utils.pick(doc.time, [latest]);
}
if (doc.versions && doc.versions[latest]) {
doc.versions = utils.pick(doc.versions, [latest]);
doc.versions[latest] = utils.pick(doc.versions[latest],
[
'name', 'version', 'description', 'author', 'homepage', 'repository', 'keywords',
'maintainers', 'bugs', '_npmUser', 'maintainers', 'readme'
]
);
}
}
return doc;
}
});
self.localPouch.changes({live: true, since: 'now'}).on('change', function () {
if (self.onChangeListener) {
self.onChangeListener();
}
});
self.remotePouch = new PouchDB(COUCHDB_URL);
self.couchdbUrl = COUCHDB_URL;
self.disconnected = false;
// keep retrying replication if we go offline
var STARTING_RETRY_TIMEOUT = 1000;
var BACKOFF = 1.1;
var retryTimeout = STARTING_RETRY_TIMEOUT;
var inProgress = false;
// quick replication via the pouchdb-dump plugin
// downloads a bunch of dump files from Amazon S3
function doInitialReplication() {
var localDocId = '_local/init_repl_done';
return self.localPouch.get(localDocId).catch(function (err) {
if (err.status !== 404) {
throw err;
}
var localDoc = {_id: localDocId, filesLoaded: -1};
return self.localPouch.put(localDoc).then(function () {
return self.localPouch.get(localDocId);
});
}).then(function (localDoc) {
var filesLoaded = localDoc.filesLoaded;
if (filesLoaded === NUM_DUMP_FILES) {
return; // done
}
// do initial replication
var dumpFiles = [];
for (var i = filesLoaded + 1; i <= NUM_DUMP_FILES; i++) {
dumpFiles.push(i);
}
var series = PouchDB.utils.Promise.resolve();
dumpFiles.forEach(function (i) {
var file = getDumpFilenameForNumber(i);
series = series.then(function () {
return self.localPouch.load(file, {proxy: COUCHDB_URL}).then(function () {
handleSuccess();
return self.localPouch.get(localDocId);
}).then(function (localDoc) {
localDoc.filesLoaded = i;
return self.localPouch.put(localDoc);
});
});
});
return series;
});
}
// regular replication, replicates from a CORSful skimdb mirror
// that Nolan is hosting on iriscouch
function doRegularReplication() {
self.localPouch.replicate.from(self.remotePouch, {batch_size: 500})
.on('change', handleSuccess)
.on('complete', handleComplete)
.on('error', handleError);
}
function handleError(err) {
console.log('error during replication');
if (err) {
console.log(err);
}
self.disconnected = true;
if (inProgress) {
retryTimeout = Math.floor(retryTimeout * BACKOFF); // exponential backoff
}
inProgress = false;
setTimeout(replicate, retryTimeout);
$rootScope.$apply();
}
function handleSuccess() {
retryTimeout = STARTING_RETRY_TIMEOUT;
self.disconnected = false;
$rootScope.$apply();
}
function handleComplete() {
handleSuccess();
self.syncComplete = true;
$rootScope.$apply();
}
function replicate() {
if (inProgress) {
return;
}
inProgress = true;
doInitialReplication().then(function () {
doRegularReplication();
}).catch(handleError);
}
replicate();
}
PouchService.prototype.onChange = function (onChangeListener) {
this.onChangeListener = onChangeListener;
};
PouchService.prototype.onComplete = function (onCompleteListener) {
this.onCompleteListener = onCompleteListener;
};
PouchService.prototype.onError = function (onErrorListener) {
this.onErrorListener = onErrorListener;
};
PouchService.prototype.getShortCouchUrl = function () {
return this.couchdbUrl.replace(/^https?:\/\//,'');
};
angular.module('browserNpmApp').service('pouchService', PouchService);