UNPKG

7.61 kBJavaScriptView Raw
1var fs = require("./fileSystem").require(),
2 pth = require('path');
3
4fs.existsSync = fs.existsSync || pth.existsSync;
5
6module.exports = (function() {
7
8 var crcTable = [],
9 Constants = require('./constants'),
10 Errors = require('./errors'),
11
12 PATH_SEPARATOR = pth.sep;
13
14
15 function mkdirSync(/*String*/path) {
16 var resolvedPath = path.split(PATH_SEPARATOR)[0];
17 path.split(PATH_SEPARATOR).forEach(function(name) {
18 if (!name || name.substr(-1,1) === ":") return;
19 resolvedPath += PATH_SEPARATOR + name;
20 var stat;
21 try {
22 stat = fs.statSync(resolvedPath);
23 } catch (e) {
24 fs.mkdirSync(resolvedPath);
25 }
26 if (stat && stat.isFile())
27 throw Errors.FILE_IN_THE_WAY.replace("%s", resolvedPath);
28 });
29 }
30
31 function findSync(/*String*/dir, /*RegExp*/pattern, /*Boolean*/recoursive) {
32 if (typeof pattern === 'boolean') {
33 recoursive = pattern;
34 pattern = undefined;
35 }
36 var files = [];
37 fs.readdirSync(dir).forEach(function(file) {
38 var path = pth.join(dir, file);
39
40 if (fs.statSync(path).isDirectory() && recoursive)
41 files = files.concat(findSync(path, pattern, recoursive));
42
43 if (!pattern || pattern.test(path)) {
44 files.push(pth.normalize(path) + (fs.statSync(path).isDirectory() ? PATH_SEPARATOR : ""));
45 }
46
47 });
48 return files;
49 }
50
51 function readBigUInt64LE(/*Buffer*/buffer, /*int*/index) {
52 var slice = Buffer.from(buffer.slice(index, index + 8));
53 slice.swap64();
54
55 return parseInt(`0x${ slice.toString('hex') }`);
56 }
57
58 return {
59 makeDir : function(/*String*/path) {
60 mkdirSync(path);
61 },
62
63 crc32 : function(buf) {
64 if (typeof buf === 'string') {
65 buf = Buffer.alloc(buf.length, buf);
66 }
67 var b = Buffer.alloc(4);
68 if (!crcTable.length) {
69 for (var n = 0; n < 256; n++) {
70 var c = n;
71 for (var k = 8; --k >= 0;) //
72 if ((c & 1) !== 0) { c = 0xedb88320 ^ (c >>> 1); } else { c = c >>> 1; }
73 if (c < 0) {
74 b.writeInt32LE(c, 0);
75 c = b.readUInt32LE(0);
76 }
77 crcTable[n] = c;
78 }
79 }
80 var crc = 0, off = 0, len = buf.length, c1 = ~crc;
81 while(--len >= 0) c1 = crcTable[(c1 ^ buf[off++]) & 0xff] ^ (c1 >>> 8);
82 crc = ~c1;
83 b.writeInt32LE(crc & 0xffffffff, 0);
84 return b.readUInt32LE(0);
85 },
86
87 methodToString : function(/*Number*/method) {
88 switch (method) {
89 case Constants.STORED:
90 return 'STORED (' + method + ')';
91 case Constants.DEFLATED:
92 return 'DEFLATED (' + method + ')';
93 default:
94 return 'UNSUPPORTED (' + method + ')';
95 }
96
97 },
98
99 writeFileTo : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr) {
100 if (fs.existsSync(path)) {
101 if (!overwrite)
102 return false; // cannot overwrite
103
104 var stat = fs.statSync(path);
105 if (stat.isDirectory()) {
106 return false;
107 }
108 }
109 var folder = pth.dirname(path);
110 if (!fs.existsSync(folder)) {
111 mkdirSync(folder);
112 }
113
114 var fd;
115 try {
116 fd = fs.openSync(path, 'w', 438); // 0666
117 } catch(e) {
118 fs.chmodSync(path, 438);
119 fd = fs.openSync(path, 'w', 438);
120 }
121 if (fd) {
122 try {
123 fs.writeSync(fd, content, 0, content.length, 0);
124 }
125 catch (e){
126 throw e;
127 }
128 finally {
129 fs.closeSync(fd);
130 }
131 }
132 fs.chmodSync(path, attr || 438);
133 return true;
134 },
135
136 writeFileToAsync : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr, /*Function*/callback) {
137 if(typeof attr === 'function') {
138 callback = attr;
139 attr = undefined;
140 }
141
142 fs.exists(path, function(exists) {
143 if(exists && !overwrite)
144 return callback(false);
145
146 fs.stat(path, function(err, stat) {
147 if(exists &&stat.isDirectory()) {
148 return callback(false);
149 }
150
151 var folder = pth.dirname(path);
152 fs.exists(folder, function(exists) {
153 if(!exists)
154 mkdirSync(folder);
155
156 fs.open(path, 'w', 438, function(err, fd) {
157 if(err) {
158 fs.chmod(path, 438, function() {
159 fs.open(path, 'w', 438, function(err, fd) {
160 fs.write(fd, content, 0, content.length, 0, function() {
161 fs.close(fd, function() {
162 fs.chmod(path, attr || 438, function() {
163 callback(true);
164 })
165 });
166 });
167 });
168 })
169 } else {
170 if(fd) {
171 fs.write(fd, content, 0, content.length, 0, function() {
172 fs.close(fd, function() {
173 fs.chmod(path, attr || 438, function() {
174 callback(true);
175 })
176 });
177 });
178 } else {
179 fs.chmod(path, attr || 438, function() {
180 callback(true);
181 })
182 }
183 }
184 });
185 })
186 })
187 })
188 },
189
190 findFiles : function(/*String*/path) {
191 return findSync(path, true);
192 },
193
194 getAttributes : function(/*String*/path) {
195
196 },
197
198 setAttributes : function(/*String*/path) {
199
200 },
201
202 toBuffer : function(input) {
203 if (Buffer.isBuffer(input)) {
204 return input;
205 } else {
206 if (input.length === 0) {
207 return Buffer.alloc(0)
208 }
209 return Buffer.from(input, 'utf8');
210 }
211 },
212
213 readBigUInt64LE,
214
215 Constants : Constants,
216 Errors : Errors
217 }
218})();