Mercurial > psgxs
annotate backends/file.js @ 28:7cfcd7d5796c
Replace Paul Sowden’s ISO8601 functions by my own, since their licence is possibly incompatible with AGPL.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 01 Nov 2010 03:01:40 +0100 |
parents | a36a514e8be8 |
children | e007a6364bf0 |
rev | line source |
---|---|
27
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 /* |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 * |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 * This file is part of PSĜS, a PubSub server written in JavaScript. |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 * |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 * PSĜS is free software: you can redistribute it and/or modify |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 * it under the terms of the GNU Affero General Public License as |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 * License. |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 * |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 * PSĜS is distributed in the hope that it will be useful, |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 * GNU Affero General Public License for more details. |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 * |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 * You should have received a copy of the GNU Affero General Public License |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 */ |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 var fs = require('fs'); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 var backend = exports; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 backend.save = function(file) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 function sanitize(o) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 var n = {}; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 for (var i in o) { |
28
7cfcd7d5796c
Replace Paul Sowden’s ISO8601 functions by my own, since their licence is possibly incompatible with AGPL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
27 if (i == 'content' || o[i].setFromISO8601) |
27
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 n[i] = o[i].toString(); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 else if (o[i] instanceof Array) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 n[i] = o[i]; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 else if (typeof o[i] == 'object') |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 n[i] = sanitize(o[i]); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 else if (typeof o[i] == 'function') |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 ; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 else |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 n[i] = o[i]; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 return n; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 var data = sanitize(backend.list); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 if (!file) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 file = 'save.json'; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 fs.writeFile(file, require('sys').inspect(data, null, null)); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 backend.load = function(file) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 var xmpp = require('xmpp'); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 function parseStanza(path, content) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 var stanza = null; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 var stream = new xmpp.Stream({ |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 stanza: function (stanza) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 path[content] = stanza; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 }); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 stream.opened = true; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 stream.data(path[content]); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 function endParsing(o) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 for (var i in o) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 if (typeof o[i] == 'string' && i == 'content') |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 parseStanza(o, i); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 else if (typeof o[i] == 'string' && regexp(o[i])) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 var today = new Date(); |
28
7cfcd7d5796c
Replace Paul Sowden’s ISO8601 functions by my own, since their licence is possibly incompatible with AGPL.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
69 today.setFromISO8601(o[i]); |
27
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 o[i] = today; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 } else if (typeof o[i] == 'object') |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 endParsing(o[i]); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 return o; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 if (!file) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 file = 'save.json'; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 fs.stat(file, function(err, stats) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 if (err) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 fs.writeFileSync(file, '{}'); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 else if (!stats.isFile()) { |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 console.log('\033[1;41mThe file “' + file + '” isn’t a regular file. Exiting.\033[0m'); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 process.exit(1); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 } |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 var data = fs.readFileSync(file); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 var obj = eval('('+data+')'); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 backend.list = endParsing(obj); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 for (var i in backend.list) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 for (var j in Node.prototype) |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 backend.list[i][j] = Node.prototype[j]; |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 }); |
a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 } |