/
Nar.js
135 lines (116 loc) · 3.53 KB
/
Nar.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
// Generated by CoffeeScript 1.8.0
(function() {
var Encoding, JSZip, Loader, Nar, WMDescript, XMLHttpRequest;
if (this["process"] != null) {
JSZip = require('jszip');
Encoding = require('encoding-japanese');
WMDescript = require('ikagaka.wmdescript.js');
XMLHttpRequest = require('uupaa.nodeproxy.js');
} else {
JSZip = this.JSZip;
Encoding = this.Encoding;
WMDescript = this.WMDescript;
XMLHttpRequest = this.XHRProxy;
}
Loader = (function() {
function Loader() {}
Loader.prototype.loadFromBuffer = function(buffer, callback) {
var err, nar;
try {
nar = new Nar(Loader.unzip(buffer));
} catch (_error) {
err = _error;
return callback(err, null);
}
return setTimeout(function() {
return callback(null, nar);
});
};
Loader.prototype.loadFromURL = function(src, callback) {
return Loader.wget(src, "arraybuffer", (function(_this) {
return function(err, buffer) {
if (!!err) {
return callback(err, null);
}
return _this.loadFromBuffer(buffer, callback);
};
})(this));
};
Loader.prototype.loadFromBlob = function(blob, callback) {
var url;
url = URL.createObjectURL(blob);
return this.loadFromURL(url, function(err, nar) {
URL.revokeObjectURL(url);
return callback(err, nar);
});
};
Loader.unzip = function(buffer) {
var dic, filePath, path, zip;
zip = new JSZip();
zip.load(buffer);
dic = {};
for (filePath in zip.files) {
path = filePath.split("\\").join("/");
dic[path] = zip.files[filePath];
}
return dic;
};
Loader.wget = function(url, type, callback) {
var xhr;
xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
if (200 <= xhr.status && xhr.status < 300) {
if (!!xhr.response.error) {
return callback(new Error(xhr.response.error.message), null);
} else {
return callback(null, xhr.response);
}
} else {
return callback(new Error(xhr.status), null);
}
});
xhr.open("GET", url);
xhr.responseType = type;
xhr.send();
return void 0;
};
return Loader;
})();
Nar = (function() {
function Nar(directory) {
this.directory = directory;
if (!this.directory["install.txt"]) {
throw new Error("install.txt not found");
}
this.install = Nar.parseDescript(Nar.convert(this.directory["install.txt"].asArrayBuffer()));
}
Nar.prototype.grep = function(regexp) {
return Object.keys(this.directory).filter(function(path) {
return regexp.test(path);
});
};
Nar.prototype.getDirectory = function(regexp) {
return this.grep(regexp).reduce(((function(_this) {
return function(dir, path, zip) {
dir[path.replace(regexp, "")] = _this.directory[path].asArrayBuffer();
return dir;
};
})(this)), {});
};
Nar.convert = function(buffer) {
return Encoding.codeToString(Encoding.convert(new Uint8Array(buffer), 'UNICODE', 'AUTO'));
};
Nar.parseDescript = function(text) {
return WMDescript.parse(text);
};
Nar.Loader = Loader;
return Nar;
})();
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = Nar;
} else if (this.Ikagaka != null) {
this.Ikagaka.Nar = Nar;
} else {
this.Nar = Nar;
}
}).call(this);