-
Notifications
You must be signed in to change notification settings - Fork 1
/
NarLoader.coffee
204 lines (197 loc) · 5.96 KB
/
NarLoader.coffee
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
### (C) 2014 Narazaka : Licensed under The MIT License - http://narazaka.net/license/MIT?2014 ###
if require? and module?
JSZip = require('jszip')
Encoding = require('encoding-japanese')
unless Promise?
Promise = require('bluebird')
else
JSZip = @JSZip
Encoding = @Encoding
unless Promise?
Promise = @Promise
class NarLoader
@loadFromBuffer: (buffer) ->
new Promise (resolve, reject) =>
resolve new NanikaDirectory(NarLoader.unzip(buffer), has_install: true)
@loadFromURL: (src) ->
NarLoader.wget src, "arraybuffer"
.then @loadFromBuffer
@loadFromBlob: (blob) ->
new Promise (resolve, reject) ->
reader = new FileReader()
reader.onload = -> resolve reader.result
reader.onerror = (event) -> reject event.target.error
reader.readAsArrayBuffer(blob)
.then @loadFromBuffer
@unzip = (buffer) ->
zip = new JSZip()
zip.load(buffer, checkCRC32: true)
dir = {}
for filePath of zip.files
path = filePath.split("\\").join("/")
dir[path] = new NanikaFile(zip.files[filePath])
dir
@wget = (url, type) ->
new Promise (resolve, reject) =>
if require? # node-webkit / node : fs access
fs = require 'fs'
fs.readFile url, (error, buffer) ->
if error
reject error
else
abuffer = new ArrayBuffer(buffer.length)
view = new Uint8Array(abuffer)
i = 0
while i < buffer.length
view[i] = buffer.readUInt8(i)
i++
resolve abuffer
else
xhr = new XMLHttpRequest()
xhr.addEventListener "load", ->
if 200 <= xhr.status < 300
resolve xhr.response
else
reject xhr.statusText
xhr.addEventListener "error", ->
reject xhr.statusText
xhr.open("GET", url)
xhr.responseType = type
xhr.send()
class NanikaFile
constructor: (@_buffer) ->
if @_buffer.dir or @_buffer.options?.dir
@_isdir = true
buffer: ->
if @_buffer.asArrayBuffer?
@_buffer = @_buffer.asArrayBuffer()
else
@_buffer
toString: ->
Encoding.codeToString(Encoding.convert(new Uint8Array(@buffer()), 'UNICODE', 'AUTO'))
valueOf: -> @buffer()
isFile: -> !@_isdir
isDirectory: -> @_isdir
class NanikaDirectory
constructor: (files={}, options) ->
@files = {}
for path, file of files
if file instanceof NanikaFile
@files[path] = file
else
@files[path] = new NanikaFile(file)
@parse(options)
parse: ({has_install, has_descript}={})->
nowarp = Object.keys(this.files).filter (filePath)-> /^install\.txt/.exec(filePath)
wraped = Object.keys(this.files).filter (filePath)-> /^[^\/]+\/install\.txt/.exec(filePath)
if(nowarp.length is 0 and wraped.length is 1)
# ghostname/install.txt -> install.txt
_files = {}
Object.keys(this.files).forEach (filePath)=>
_files[filePath.split("/").slice(1).join("/")] = @files[filePath]
@files = _files
if @files["install.txt"]?
@install = NarDescript.parse(@files["install.txt"].toString())
else if has_install
console.warn "install.txt not found"
@install = {}
if @files["descript.txt"]?
@descript = NarDescript.parse(@files["descript.txt"].toString())
else if has_descript
console.warn "descript.txt not found"
@descript = {}
asArrayBuffer: ->
directory = {}
for path, file of @files
directory[path] = @files[path].buffer()
directory
listChildren: ->
children = {}
for path of @files
if result = path.match /^([^\/]+)/
children[result[1]] = true
Object.keys(children)
addDirectory: (dir, options) ->
directory = {}
for path, file of @files
directory[path] = file
if dir instanceof NanikaDirectory
files = dir.files
else
files = dir
for path, file of files
directory[path] = file
new NanikaDirectory directory, options
getDirectory: (dirpath, options) ->
dirpathre = @pathToRegExp(dirpath)
directory = {}
Object.keys(@files)
.filter (path) -> dirpathre.test path
.forEach (path) =>
directory[path.replace(dirpathre, "")] = @files[path]
new NanikaDirectory directory, options
wrapDirectory: (dirpath, options) ->
dirpathcanon = @path.canonical(dirpath)
directory = {}
Object.keys(@files)
.forEach (path) =>
directory[dirpathcanon + '/' + path] = @files[path]
new NanikaDirectory directory, options
getElements: (elempaths, options) ->
unless elempaths instanceof Array
elempaths = [elempaths]
directory = {}
for elempath in elempaths
elempathre = @pathToRegExp(elempath)
Object.keys(@files)
.filter (path) -> elempathre.test path
.forEach (path) =>
directory[path] = @files[path]
new NanikaDirectory directory, options
removeElements: (elempaths, options) ->
unless elempaths instanceof Array
elempaths = [elempaths]
directory = {}
for path, file of @files
directory[path] = file
for elempath in elempaths
elempathre = @pathToRegExp(elempath)
Object.keys(directory)
.filter (path) -> elempathre.test path
.forEach (path) ->
delete directory[path]
new NanikaDirectory directory, options
hasElement: (elempath) ->
elempathre = @pathToRegExp(elempath)
for path of @files
if elempathre.test path
return true
return false
pathToRegExp: (path) ->
new RegExp '^' + @path.canonical(path).replace(/(\W)/g, '\\$1') + '(?:$|/)'
path:
canonical: (path) ->
path.replace(/\\/g, '/').replace(/^\.?\//, '').replace(/\/?$/, '')
class NarDescript
@parse: (descript_str) ->
descript_lines = descript_str
.replace(/(?:\r\n|\r|\n)/g, "\n") # CRLF->LF
.replace(/^\s*\/\/.*$/mg, "") # remove commentout
.replace(/\n+/g, "\n") # remove empty lines
.replace(/\n$/, "") # remove last LF
.split(/\n/)
descript = {}
for descript_line in descript_lines
result = descript_line.match /^\s*([^,]+?)\s*,\s*(.*?)\s*$/
unless result
console.error "wrong descript definition : #{descript_line}"
return descript
descript[result[1]] = result[2]
descript
if module?.exports?
module.exports = NarLoader: NarLoader, NanikaFile: NanikaFile, NanikaDirectory: NanikaDirectory, NarDescript: NarDescript
else
@NarLoader = NarLoader
@NanikaFile = NanikaFile
@NanikaDirectory = NanikaDirectory
@NarDescript = NarDescript