Skip to content

Commit

Permalink
chore(cleanup): clean up get (#57)
Browse files Browse the repository at this point in the history
* chore(cleanup): clean up get

the byDigest logic was so unique these needed to be separate functions

Co-authored-by: nlf <quitlahok@gmail.com>
  • Loading branch information
wraithgar and nlf authored Aug 26, 2021
1 parent d6365e5 commit 88ddc1b
Showing 1 changed file with 112 additions and 128 deletions.
240 changes: 112 additions & 128 deletions get.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,112 @@
'use strict'

const util = require('util')
const Collect = require('minipass-collect')
const Minipass = require('minipass')
const Pipeline = require('minipass-pipeline')
const fs = require('fs')
const util = require('util')

const index = require('./lib/entry-index')
const memo = require('./lib/memoization')
const read = require('./lib/content/read')

const Minipass = require('minipass')
const Collect = require('minipass-collect')
const Pipeline = require('minipass-pipeline')

const writeFile = util.promisify(fs.writeFile)

module.exports = function get (cache, key, opts) {
return getData(false, cache, key, opts)
}
module.exports.byDigest = function getByDigest (cache, digest, opts) {
return getData(true, cache, digest, opts)
}

function getData (byDigest, cache, key, opts = {}) {
function getData (cache, key, opts = {}) {
const { integrity, memoize, size } = opts
const memoized = byDigest
? memo.get.byDigest(cache, key, opts)
: memo.get(cache, key, opts)
const memoized = memo.get(cache, key, opts)
if (memoized && memoize !== false) {
return Promise.resolve(
byDigest
? memoized
: {
metadata: memoized.entry.metadata,
data: memoized.data,
integrity: memoized.entry.integrity,
size: memoized.entry.size,
}
)
return Promise.resolve({
metadata: memoized.entry.metadata,
data: memoized.data,
integrity: memoized.entry.integrity,
size: memoized.entry.size,
})
}
return (byDigest ? Promise.resolve(null) : index.find(cache, key, opts)).then(
(entry) => {
if (!entry && !byDigest)
throw new index.NotFoundError(cache, key)

return read(cache, byDigest ? key : entry.integrity, {
integrity,
size,
})
.then((data) =>
byDigest
? data
: {
data,
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
)
.then((res) => {
if (memoize && byDigest)
memo.put.byDigest(cache, key, res, opts)
else if (memoize)
memo.put(cache, entry, res.data, opts)

return res
})
}
)
}
return index.find(cache, key, opts).then((entry) => {
if (!entry)
throw new index.NotFoundError(cache, key)

return read(cache, entry.integrity, { integrity, size }).then((data) => {
if (memoize)
memo.put(cache, entry, data, opts)

module.exports.sync = function get (cache, key, opts) {
return getDataSync(false, cache, key, opts)
return {
data,
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
})
})
}
module.exports.sync.byDigest = function getByDigest (cache, digest, opts) {
return getDataSync(true, cache, digest, opts)
module.exports = getData

function getDataByDigest (cache, key, opts = {}) {
const { integrity, memoize, size } = opts
const memoized = memo.get.byDigest(cache, key, opts)
if (memoized && memoize !== false)
return Promise.resolve(memoized)

return read(cache, key, { integrity, size }).then((res) => {
if (memoize)
memo.put.byDigest(cache, key, res, opts)
return res
})
}
module.exports.byDigest = getDataByDigest

function getDataSync (byDigest, cache, key, opts = {}) {
function getDataSync (cache, key, opts = {}) {
const { integrity, memoize, size } = opts
const memoized = byDigest
? memo.get.byDigest(cache, key, opts)
: memo.get(cache, key, opts)
const memoized = memo.get(cache, key, opts)

if (memoized && memoize !== false) {
return byDigest
? memoized
: {
metadata: memoized.entry.metadata,
data: memoized.data,
integrity: memoized.entry.integrity,
size: memoized.entry.size,
}
return {
metadata: memoized.entry.metadata,
data: memoized.data,
integrity: memoized.entry.integrity,
size: memoized.entry.size,
}
}
const entry = !byDigest && index.find.sync(cache, key, opts)
if (!entry && !byDigest)
const entry = index.find.sync(cache, key, opts)
if (!entry)
throw new index.NotFoundError(cache, key)

const data = read.sync(cache, byDigest ? key : entry.integrity, {
const data = read.sync(cache, entry.integrity, {
integrity: integrity,
size: size,
})
const res = byDigest
? data
: {
metadata: entry.metadata,
data: data,
size: entry.size,
integrity: entry.integrity,
}
if (memoize && byDigest)
memo.put.byDigest(cache, key, res, opts)
else if (memoize)
const res = {
metadata: entry.metadata,
data: data,
size: entry.size,
integrity: entry.integrity,
}
if (memoize)
memo.put(cache, entry, res.data, opts)

return res
}

module.exports.stream = getStream
module.exports.sync = getDataSync

function getDataByDigestSync (cache, digest, opts = {}) {
const { integrity, memoize, size } = opts
const memoized = memo.get.byDigest(cache, digest, opts)

if (memoized && memoize !== false)
return memoized

const res = read.sync(cache, digest, {
integrity: integrity,
size: size,
})
if (memoize)
memo.put.byDigest(cache, digest, res, opts)

return res
}
module.exports.sync.byDigest = getDataByDigestSync

const getMemoizedStream = (memoized) => {
const stream = new Minipass()
Expand Down Expand Up @@ -166,7 +159,7 @@ function getStream (cache, key, opts = {}) {
return stream
}

module.exports.stream.byDigest = getStreamDigest
module.exports.stream = getStream

function getStreamDigest (cache, integrity, opts = {}) {
const { memoize } = opts
Expand All @@ -191,7 +184,7 @@ function getStreamDigest (cache, integrity, opts = {}) {
}
}

module.exports.info = info
module.exports.stream.byDigest = getStreamDigest

function info (cache, key, opts = {}) {
const { memoize } = opts
Expand All @@ -201,53 +194,44 @@ function info (cache, key, opts = {}) {
else
return index.find(cache, key)
}
module.exports.info = info

module.exports.hasContent = read.hasContent

function cp (cache, key, dest, opts) {
return copy(false, cache, key, dest, opts)
}

module.exports.copy = cp

function cpDigest (cache, digest, dest, opts) {
return copy(true, cache, digest, dest, opts)
}

module.exports.copy.byDigest = cpDigest

function copy (byDigest, cache, key, dest, opts = {}) {
function copy (cache, key, dest, opts = {}) {
if (read.copy) {
return (byDigest
? Promise.resolve(null)
: index.find(cache, key, opts)
).then((entry) => {
if (!entry && !byDigest)
return index.find(cache, key, opts).then((entry) => {
if (!entry)
throw new index.NotFoundError(cache, key)

return read
.copy(cache, byDigest ? key : entry.integrity, dest, opts)
return read.copy(cache, entry.integrity, dest, opts)
.then(() => {
return byDigest
? key
: {
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
return {
metadata: entry.metadata,
size: entry.size,
integrity: entry.integrity,
}
})
})
}

return getData(byDigest, cache, key, opts).then((res) => {
return writeFile(dest, byDigest ? res : res.data).then(() => {
return byDigest
? key
: {
metadata: res.metadata,
size: res.size,
integrity: res.integrity,
}
return getData(cache, key, opts).then((res) => {
return writeFile(dest, res.data).then(() => {
return {
metadata: res.metadata,
size: res.size,
integrity: res.integrity,
}
})
})
}
module.exports.copy = copy

function copyByDigest (cache, key, dest, opts = {}) {
if (read.copy)
return read.copy(cache, key, dest, opts).then(() => key)

return getDataByDigest(cache, key, opts).then((res) => {
return writeFile(dest, res).then(() => key)
})
}
module.exports.copy.byDigest = copyByDigest

module.exports.hasContent = read.hasContent

0 comments on commit 88ddc1b

Please sign in to comment.