source: trip-planner-front/node_modules/cacache/get.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.5 KB
Line 
1'use strict'
2
3const util = require('util')
4const fs = require('fs')
5const index = require('./lib/entry-index')
6const memo = require('./lib/memoization')
7const read = require('./lib/content/read')
8
9const Minipass = require('minipass')
10const Collect = require('minipass-collect')
11const Pipeline = require('minipass-pipeline')
12
13const writeFile = util.promisify(fs.writeFile)
14
15module.exports = function get (cache, key, opts) {
16 return getData(false, cache, key, opts)
17}
18module.exports.byDigest = function getByDigest (cache, digest, opts) {
19 return getData(true, cache, digest, opts)
20}
21
22function getData (byDigest, cache, key, opts = {}) {
23 const { integrity, memoize, size } = opts
24 const memoized = byDigest
25 ? memo.get.byDigest(cache, key, opts)
26 : memo.get(cache, key, opts)
27 if (memoized && memoize !== false) {
28 return Promise.resolve(
29 byDigest
30 ? memoized
31 : {
32 metadata: memoized.entry.metadata,
33 data: memoized.data,
34 integrity: memoized.entry.integrity,
35 size: memoized.entry.size,
36 }
37 )
38 }
39 return (byDigest ? Promise.resolve(null) : index.find(cache, key, opts)).then(
40 (entry) => {
41 if (!entry && !byDigest)
42 throw new index.NotFoundError(cache, key)
43
44 return read(cache, byDigest ? key : entry.integrity, {
45 integrity,
46 size,
47 })
48 .then((data) =>
49 byDigest
50 ? data
51 : {
52 data,
53 metadata: entry.metadata,
54 size: entry.size,
55 integrity: entry.integrity,
56 }
57 )
58 .then((res) => {
59 if (memoize && byDigest)
60 memo.put.byDigest(cache, key, res, opts)
61 else if (memoize)
62 memo.put(cache, entry, res.data, opts)
63
64 return res
65 })
66 }
67 )
68}
69
70module.exports.sync = function get (cache, key, opts) {
71 return getDataSync(false, cache, key, opts)
72}
73module.exports.sync.byDigest = function getByDigest (cache, digest, opts) {
74 return getDataSync(true, cache, digest, opts)
75}
76
77function getDataSync (byDigest, cache, key, opts = {}) {
78 const { integrity, memoize, size } = opts
79 const memoized = byDigest
80 ? memo.get.byDigest(cache, key, opts)
81 : memo.get(cache, key, opts)
82 if (memoized && memoize !== false) {
83 return byDigest
84 ? memoized
85 : {
86 metadata: memoized.entry.metadata,
87 data: memoized.data,
88 integrity: memoized.entry.integrity,
89 size: memoized.entry.size,
90 }
91 }
92 const entry = !byDigest && index.find.sync(cache, key, opts)
93 if (!entry && !byDigest)
94 throw new index.NotFoundError(cache, key)
95
96 const data = read.sync(cache, byDigest ? key : entry.integrity, {
97 integrity: integrity,
98 size: size,
99 })
100 const res = byDigest
101 ? data
102 : {
103 metadata: entry.metadata,
104 data: data,
105 size: entry.size,
106 integrity: entry.integrity,
107 }
108 if (memoize && byDigest)
109 memo.put.byDigest(cache, key, res, opts)
110 else if (memoize)
111 memo.put(cache, entry, res.data, opts)
112
113 return res
114}
115
116module.exports.stream = getStream
117
118const getMemoizedStream = (memoized) => {
119 const stream = new Minipass()
120 stream.on('newListener', function (ev, cb) {
121 ev === 'metadata' && cb(memoized.entry.metadata)
122 ev === 'integrity' && cb(memoized.entry.integrity)
123 ev === 'size' && cb(memoized.entry.size)
124 })
125 stream.end(memoized.data)
126 return stream
127}
128
129function getStream (cache, key, opts = {}) {
130 const { memoize, size } = opts
131 const memoized = memo.get(cache, key, opts)
132 if (memoized && memoize !== false)
133 return getMemoizedStream(memoized)
134
135 const stream = new Pipeline()
136 index
137 .find(cache, key)
138 .then((entry) => {
139 if (!entry)
140 throw new index.NotFoundError(cache, key)
141
142 stream.emit('metadata', entry.metadata)
143 stream.emit('integrity', entry.integrity)
144 stream.emit('size', entry.size)
145 stream.on('newListener', function (ev, cb) {
146 ev === 'metadata' && cb(entry.metadata)
147 ev === 'integrity' && cb(entry.integrity)
148 ev === 'size' && cb(entry.size)
149 })
150
151 const src = read.readStream(
152 cache,
153 entry.integrity,
154 { ...opts, size: typeof size !== 'number' ? entry.size : size }
155 )
156
157 if (memoize) {
158 const memoStream = new Collect.PassThrough()
159 memoStream.on('collect', data => memo.put(cache, entry, data, opts))
160 stream.unshift(memoStream)
161 }
162 stream.unshift(src)
163 })
164 .catch((err) => stream.emit('error', err))
165
166 return stream
167}
168
169module.exports.stream.byDigest = getStreamDigest
170
171function getStreamDigest (cache, integrity, opts = {}) {
172 const { memoize } = opts
173 const memoized = memo.get.byDigest(cache, integrity, opts)
174 if (memoized && memoize !== false) {
175 const stream = new Minipass()
176 stream.end(memoized)
177 return stream
178 } else {
179 const stream = read.readStream(cache, integrity, opts)
180 if (!memoize)
181 return stream
182
183 const memoStream = new Collect.PassThrough()
184 memoStream.on('collect', data => memo.put.byDigest(
185 cache,
186 integrity,
187 data,
188 opts
189 ))
190 return new Pipeline(stream, memoStream)
191 }
192}
193
194module.exports.info = info
195
196function info (cache, key, opts = {}) {
197 const { memoize } = opts
198 const memoized = memo.get(cache, key, opts)
199 if (memoized && memoize !== false)
200 return Promise.resolve(memoized.entry)
201 else
202 return index.find(cache, key)
203}
204
205module.exports.hasContent = read.hasContent
206
207function cp (cache, key, dest, opts) {
208 return copy(false, cache, key, dest, opts)
209}
210
211module.exports.copy = cp
212
213function cpDigest (cache, digest, dest, opts) {
214 return copy(true, cache, digest, dest, opts)
215}
216
217module.exports.copy.byDigest = cpDigest
218
219function copy (byDigest, cache, key, dest, opts = {}) {
220 if (read.copy) {
221 return (byDigest
222 ? Promise.resolve(null)
223 : index.find(cache, key, opts)
224 ).then((entry) => {
225 if (!entry && !byDigest)
226 throw new index.NotFoundError(cache, key)
227
228 return read
229 .copy(cache, byDigest ? key : entry.integrity, dest, opts)
230 .then(() => {
231 return byDigest
232 ? key
233 : {
234 metadata: entry.metadata,
235 size: entry.size,
236 integrity: entry.integrity,
237 }
238 })
239 })
240 }
241
242 return getData(byDigest, cache, key, opts).then((res) => {
243 return writeFile(dest, byDigest ? res : res.data).then(() => {
244 return byDigest
245 ? key
246 : {
247 metadata: res.metadata,
248 size: res.size,
249 integrity: res.integrity,
250 }
251 })
252 })
253}
Note: See TracBrowser for help on using the repository browser.