-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
311 lines (294 loc) · 8.57 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*!
* Licensed under the Educational Community 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://opensource.org/licenses/ECL-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.
*/
const axios = require('axios');
const fs = require('fs');
const qs = require('qs');
// Use the default Ethercalc host & port if none are provided
function Ethercalc(host = 'localhost', port = '8000', protocol = 'http') {
this.instance = axios.create({
baseURL: `${protocol}://${host}:${port}`,
timeout: 2500
});
}
Ethercalc.prototype.getRoom = function(room) {
return this.instance.get(`/_/${room}`)
.then(response => {
// This returns the socialcalc snapshot for the room
// See https://github.com/marcelklehr/socialcalc/blob/master/js/socialcalc-3.js#L367
// for description of the format and acceptable values
return response.data;
})
.catch(error => {
// If we get a response with the error, send back the status info
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
// Otherwise just return the error
return error;
}
});
};
Ethercalc.prototype.createRoom = function(room, snapshot) {
// Creating a room might take a bit longer so increase timeout
const timeout = {
timeout: 10000
};
if (room) {
return this.instance.post(`/_/${room}`, qs.stringify({
room: room,
snapshot: snapshot
}), timeout)
.then(response => {
// If room ID was included, this will be an ethercalc command
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
} else {
// If we have no room ID, just create a new room
return this.instance.post('/_', qs.stringify({
snapshot: snapshot
}), timeout)
.then(response => {
// Since there was no ID, a new ID/URL for the room will be returned
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
}
};
Ethercalc.prototype.overwrite = function(room, snapshot, type) {
let conf = {
headers: {}
};
if (type === 'socialcalc') {
conf.headers = {
'Content-Type':'text/x-socialcalc'
};
} else if (type === 'xlsx') {
conf.headers = {
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
} else if (type === 'csv') {
conf.headers = {
'Content-Type': 'text/csv'
};
} else {
return new Error('Format must be one of socialcalc, xlsx or csv!');
}
return this.instance.put(`/_/${room}`, Buffer.from(snapshot), conf)
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.createRoomFromCSVFile = function(path) {
return this.instance.post('/_/', fs.createReadStream(path), {
headers: {
// Content type is csv
'Content-Type': 'text/csv'
}
})
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.deleteRoom = function(room) {
return this.instance.delete(`/_/${room}`)
// If the response status is successful the room was deleted
.then(response => {
return true;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.roomExists = function(room) {
return this.instance.get(`/_exists/${room}`)
.then(response => {
return response.data;
})
.catch(error => {
return false;
});
};
Ethercalc.prototype.exportRoom = function(room, type) {
type = type.toLowerCase();
// Default to csv if type is not defined or is invalid
type = (type === 'csv.json' || type === 'xlsx' || type === 'md' || type === 'html') ? type : 'csv';
return this.instance.get(`/_/${room}/${type}`)
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.listRooms = function() {
return this.instance.get('/_rooms')
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.postCommand = function(room, command) {
// An Ethercalc 'command' is a string or array of strings that correspond(s)
// to spreadsheet actions. Command format is `set A1 value n 1` or `set A2
// text t test` (action `set` coordinate `A1/A2` type `value n/text t` value
// `1/test`)
return this.instance.post(`/_/${room}`, qs.stringify({'command': command}))
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.appendRowsFromCSVFile = function(room, path) {
return this.instance.post(`/_/${room}`, fs.createReadStream(path), {
headers: {
'Content-Type': 'text/csv'
}
})
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.getCells = function(room) {
return this.instance.get(`/_/${room}/cells`)
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.getCellValue = function(room, coord) {
return this.instance.get(`/_/${room}/cells/${coord}`)
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
Ethercalc.prototype.getHTML = function(room) {
return this.instance.get(`/_/${room}/html`)
.then(response => {
return response.data;
})
.catch(error => {
if (error.response) {
return {
status: error.response.status,
message: error.response.statusText
};
} else {
return error;
}
});
};
module.exports = Ethercalc;