This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
176 lines (152 loc) · 5.6 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict'
// **Github:** https://github.com/thunks/file-cache
//
// **License:** MIT
const fs = require('fs')
const zlib = require('zlib')
const path = require('path')
const crypto = require('crypto')
const mime = require('mime-types')
const compressible = require('compressible')
const LRUCache = require('lrucache')
const thunk = require('thunks')()
const stat = thunk.thunkify(fs.stat)
const readFile = thunk.thunkify(fs.readFile)
const compressFile = {
gzip: thunk.thunkify(zlib.gzip),
deflate: thunk.thunkify(zlib.deflate),
origin: function (buf) {
return thunk(buf)
}
}
module.exports = function (options) {
const cache = Object.create(null)
const extraFilesMap = Object.create(null)
const lruCache = new LRUCache()
const cwd = process.cwd()
let totalBytes = 0
options = options || {}
if (typeof options === 'string') options = {root: options}
// for test.
if (options.debug) module.exports.cache = cache
let root = typeof options.root === 'string' ? options.root : cwd
root = path.resolve(cwd, root)
const extraFiles = options.extraFiles || []
if (!Array.isArray(extraFiles)) throw new Error('"extraFiles" must be Array')
for (let i = 0; i < extraFiles.length; i++) {
extraFilesMap[extraFiles[i]] = path.resolve(cwd, extraFiles[i])
}
const enableCompress = options.compress !== false
const maxCacheLength = options.maxCacheLength >= -1 ? Math.floor(options.maxCacheLength) : 0
const minCompressLength = options.minCompressLength >= 16 ? Math.floor(options.minCompressLength) : 256
const md5Encoding = options.md5Encoding || 'base64'
function resolvePath (filePath) {
filePath = path.normalize(filePath)
if (extraFilesMap[filePath]) return extraFilesMap[filePath]
return path.join(root, path.join('/', filePath))
}
function checkLRU (filePath, addSize) {
if (maxCacheLength <= 0) return
totalBytes += addSize
lruCache.set(filePath, (lruCache.get(filePath) || 0) + addSize)
while (lruCache.staleKey() !== filePath && totalBytes >= maxCacheLength) {
let stale = lruCache.popStale()
delete cache[stale[0]]
totalBytes -= stale[1]
}
}
return function fileCache (filePath, encodings) {
return thunk(function (done) {
let compressEncoding = bestCompress(encodings)
filePath = resolvePath(filePath)
let readAndCacheFile = cache[filePath]
if (readAndCacheFile instanceof OriginFile) {
return cloneFile(readAndCacheFile, compressEncoding, checkLRU)(done)
}
if (!readAndCacheFile) {
readAndCacheFile = thunk.persist(thunk.seq([
stat(filePath),
readFile(filePath)
]))
if (maxCacheLength !== -1) cache[filePath] = readAndCacheFile
}
readAndCacheFile(function (error, res) {
if (error) throw error
let originFile = cache[filePath]
if (!(originFile instanceof OriginFile)) {
originFile = new OriginFile(filePath, res[0], res[1],
enableCompress, minCompressLength, md5Encoding)
cache[filePath] = maxCacheLength === -1 ? null : originFile
}
return cloneFile(originFile, compressEncoding, checkLRU)
})(done)
})
}
}
const enableEncodings = Object.create(null)
enableEncodings.gzip = true
enableEncodings.deflate = true
function bestCompress (encodings) {
if (!Array.isArray(encodings)) encodings = [encodings]
for (let i = 0; i < encodings.length; i++) {
if (enableEncodings[encodings[i]]) return encodings[i]
}
return 'origin'
}
function File (originFile, compressEncoding) {
this.md5 = originFile.md5
this.dir = originFile.dir
this.ext = originFile.ext
this.name = originFile.name
this.path = originFile.path
this.size = originFile.size
this.type = originFile.type
this.atime = originFile.atime
this.mtime = originFile.mtime
this.ctime = originFile.ctime
this.compress = compressEncoding === 'origin' ? '' : compressEncoding
this.contents = Buffer.from(originFile[compressEncoding])
}
function OriginFile (filePath, stats, buf, enableCompress, minCompressLength, md5Encoding) {
filePath = filePath.replace(/\\/g, '/')
this.count = 0
this.md5 = crypto.createHash('md5').update(buf).digest(md5Encoding)
this.dir = path.dirname(filePath)
this.ext = path.extname(filePath)
this.name = path.basename(filePath)
this.path = filePath
this.size = stats.size
this.type = mime.lookup(filePath) || 'application/octet-stream'
this.atime = stats.atime.toUTCString()
this.mtime = stats.mtime.toUTCString()
this.ctime = stats.ctime.toUTCString()
this.compressible = enableCompress && this.size > minCompressLength && compressible(this.type)
this.contents = buf
this.origin = null
this.gzip = null
this.deflate = null
}
function cloneFile (originFile, compressEncoding, checkLRU) {
return thunk(function (done) {
originFile.count += 1
if (!originFile.compressible) compressEncoding = 'origin'
let compressAndCacheBuffer = originFile[compressEncoding]
if (Buffer.isBuffer(compressAndCacheBuffer)) {
return done(null, new File(originFile, compressEncoding))
}
if (!compressAndCacheBuffer) {
compressAndCacheBuffer = thunk.persist(compressFile[compressEncoding](originFile.contents))
originFile[compressEncoding] = compressAndCacheBuffer
}
compressAndCacheBuffer(function (error, buf) {
if (error) throw error
if (!Buffer.isBuffer(originFile[compressEncoding])) {
originFile[compressEncoding] = buf
checkLRU(originFile.path, buf.length)
}
return new File(originFile, compressEncoding)
})(done)
})
}
module.exports.File = File
module.exports.OriginFile = OriginFile