-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
205 lines (171 loc) · 5.05 KB
/
web.js
File metadata and controls
205 lines (171 loc) · 5.05 KB
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
var express = require('express');
var app = express();
var Path = require('path');
var fs = require('fs');
var jsdom = require('jsdom');
var when = require('when');
app.use(express.logger());
// API to come later
app.use('/api', require('./api'));
// Weld rendering
/*app.use(function (req, res, next) {
var path = req.path;
if (path.indexOf('.') !== -1) return next();
var absolutePath = Path.join(__dirname, 'htdocs', req.path, 'index.html');
readHTML(absolutePath).then(
function (window) {
embed(window.document).then(
function (document) {
res.send(document.outerHTML);
}
);
},
function (err) {
console.log('error generating page: ', err);
next();
}
);
});*/
// Static files
app.use(express.static(__dirname + '/htdocs'));
// Catch-all 404
app.post('*', function (req,res) {
res.redirect('.');
res.end();
});
app.all('*', function (req, res) {
res.writeHeader(404, {"Content-Type": 'text/plain'});
res.write('Page not found.');
res.end();
});
// Run the server
var port = process.env.PORT || 10080;
app.listen(port, function () {
console.log('Listening on port ' + port);
});
/**** Helper function ****/
/*
* Read html file into a jsdom object
*
* @param path - path to the file
* @returns - promise of a window
*/
function readHTML(path) {
var deferred = when.defer();
console.log('reading html: ', path);
fs.readFile(path, 'UTF-8', function (err, data) {
if (err) {
console.log('file read failed: ', path, err);
deferred.reject(err);
} else {
jsdom.env(data, function (err, window) {
if (err) {
console.log('jsdom failed: ', path);
deferred.reject(err);
} else {
console.log('got window: ', path);
deferred.resolve(window);
}
});
}
});
return deferred.promise;
}
/*
* Promise version of fs.readdir
*/
function fsls(path) {
var deferred = when.defer();
fs.readdir(path, function (err, files) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(files);
}
});
return deferred.promise;
}
/*
* resolve possible wildcard path to list of paths
*/
function resolvePath(relativePath) {
var path = Path.join(__dirname, 'htdocs', relativePath);
if (path.indexOf('*') !== -1) {
console.log('resolving path with *: ', path);
path = path.replace('*', '');
return fsls(path).then(function (relPaths) {
var absPaths = [];
relPaths.forEach(function (r) {
absPaths.push(Path.join(path, r));
});
return absPaths;
});;
} else {
console.log('resolving path with no *: ', path);
return when.resolve([path]);
}
}
function weld(target, embeddable) {
}
/*
* Embed dom window based on <embed> tag sources
* @param document - the dom to embed
* @returns - promise of the new document
*/
function embed(document) {
var deferred = when.defer(),
embeds = document.querySelectorAll('[data-embed]'),
resolvedPathPromises = [],
i, l, e;
if (!embeds.length) {
return when.resolve(document);
}
console.log('embedding');
for (i = 0, l = embeds.length; i < l; i++) {
(function (e) {
resolvedPathPromises.push(
resolvePath(e.attributes['data-embed'].value).then(function (srcs) {
var embeddablePromises = [];
var selector = e.attributes['data-selector'] && e.attributes['data-selector'].value;
console.log('resolved paths to:', srcs);
console.log('selector: ', selector);
srcs.forEach(function (src) {
embeddablePromises.push(readHTML(Path.join(src, 'model.html')).then(function (window) {
if (selector) {
console.log('querySelecting for ', Path.join(src, 'model.html'));
return window.document.querySelector(selector);
} else {
console.log('returning whole body for ', Path.join(src, 'model.html'));
return window.document.body;
}
}));
});
return when.settle(embeddablePromises).then(function (embeddables) {
console.log('all settled up');
embeddables.forEach(function (embeddable) {
if (embeddable.value) {
console.log('creating an element: ', e.tagName);
var el = document.createElement(e.tagName), i, l, a;
el.innerHTML = embeddable.value.innerHTML;
for (i = 0, l = embeddable.value.attributes.length; i < l; i++) {
a = embeddable.value.attributes[i];
el.setAttribute(a.name, a.value);
}
e.parentNode.insertBefore(el, e);
} else {
console.log('skipping failed embeddable');
}
});
e.parentNode.removeChild(e);
console.log('returning e to be resolved');
return e;
});
})
);
})(embeds[i]);
}
when.settle(resolvedPathPromises).then(function () {
deferred.resolve(document);
});
return deferred.promise;
}