forked from clubfest/packmeteor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilemanifest.js
152 lines (129 loc) · 4.15 KB
/
filemanifest.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
// The appcache loader
// HTTP
var http = require('http');
var appcacheManifest = function(host, port, callback) {
var options = {
hostname: host,
port: port,
path: '/app.manifest',
headers: {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17'},
method: 'GET'
};
var req = http.request(options, function(res) {
var body = '';
var filelist = [];
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// res.headers.content-length
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
// Make sure we got the app.manifest
if (body.length) {
var lines = body.split('\n');
// The spec defaults to cache
var where = 'cache';
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
// Remove comment
var line = line.split('#')[0];
// Parse lines
if (line == '' || line == 'CACHE MANIFEST') {} else
if (line == 'CACHE:') { where = 'cache'; } else
if (line == 'FALLBACK:') { where = 'fallback'; } else
if (line == 'NETWORK:') { where = 'network'; } else
if (line == 'SETTINGS:') { where = 'settings'; } else
if (where == 'cache') {
// This line is cache line
var filename = line.split('?')[0];
filename = (filename == '/')?'index.html':filename;
// Adds task to queue...
filelist.push({
name: filename,
url: line
});
//saveFileFromServer(filename, line);
} else
if (where == 'fallback') {
// This line is a fallback line
//console.log('Fallback: ' + line);
// TODO: Figure out a way to parse this... Filenames should be url
// Encoded? - watchout for spaces in filenames...
//saveFileFromServer(filename, line);
}
}
// Send the file list to callback
callback({ files: filelist, before:{}, after: {} });
} else {
console.log(' Add the appcache package'.red);
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
};
var packmeteorManifest = function(host, port, callback) {
var options = {
hostname: host,
port: port,
path: '/packmeteor.manifest',
method: 'GET'
};
var req = http.request(options, function(res) {
var body = '';
var filelist = [];
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
// Make sure we got the app.manifest
if (body.length) {
var lines = body.split('\n');
if (lines[0] !== '#PACKMETEOR') {
console.log('Incompatible packmeteor.manifest format');
process.exit();
}
var result = {
files: [],
before: [],
after: []
};
var where = 'files';
for (var i = 1; i < lines.length; i++) {
var line = lines[i];
if (line.length) {
if (line == '#BEFORE') {
where = 'before';
} else if (line == '#AFTER') {
where = 'after';
} else {
// Adds task to queue...
result[where].push({
name: (line == '/')?'index.html':line,
url: line
});
//saveFileFromServer(filename, line);
}
}
}
// console.log(filelist);
// Send the file list to callback
callback(result); // TODO add before after files...They are local
// files - so they should not be downloaded
} else {
console.log(' Add the packmeteor package'.red);
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
};
//module.exports = appcacheManifest;
module.exports = packmeteorManifest;