11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst index = require('./entry-index')
41cb0ef41Sopenharmony_ciconst memo = require('./memoization')
51cb0ef41Sopenharmony_ciconst write = require('./content/write')
61cb0ef41Sopenharmony_ciconst Flush = require('minipass-flush')
71cb0ef41Sopenharmony_ciconst { PassThrough } = require('minipass-collect')
81cb0ef41Sopenharmony_ciconst Pipeline = require('minipass-pipeline')
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst putOpts = (opts) => ({
111cb0ef41Sopenharmony_ci  algorithms: ['sha512'],
121cb0ef41Sopenharmony_ci  ...opts,
131cb0ef41Sopenharmony_ci})
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cimodule.exports = putData
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciasync function putData (cache, key, data, opts = {}) {
181cb0ef41Sopenharmony_ci  const { memoize } = opts
191cb0ef41Sopenharmony_ci  opts = putOpts(opts)
201cb0ef41Sopenharmony_ci  const res = await write(cache, data, opts)
211cb0ef41Sopenharmony_ci  const entry = await index.insert(cache, key, res.integrity, { ...opts, size: res.size })
221cb0ef41Sopenharmony_ci  if (memoize) {
231cb0ef41Sopenharmony_ci    memo.put(cache, entry, data, opts)
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  return res.integrity
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cimodule.exports.stream = putStream
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction putStream (cache, key, opts = {}) {
321cb0ef41Sopenharmony_ci  const { memoize } = opts
331cb0ef41Sopenharmony_ci  opts = putOpts(opts)
341cb0ef41Sopenharmony_ci  let integrity
351cb0ef41Sopenharmony_ci  let size
361cb0ef41Sopenharmony_ci  let error
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  let memoData
391cb0ef41Sopenharmony_ci  const pipeline = new Pipeline()
401cb0ef41Sopenharmony_ci  // first item in the pipeline is the memoizer, because we need
411cb0ef41Sopenharmony_ci  // that to end first and get the collected data.
421cb0ef41Sopenharmony_ci  if (memoize) {
431cb0ef41Sopenharmony_ci    const memoizer = new PassThrough().on('collect', data => {
441cb0ef41Sopenharmony_ci      memoData = data
451cb0ef41Sopenharmony_ci    })
461cb0ef41Sopenharmony_ci    pipeline.push(memoizer)
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // contentStream is a write-only, not a passthrough
501cb0ef41Sopenharmony_ci  // no data comes out of it.
511cb0ef41Sopenharmony_ci  const contentStream = write.stream(cache, opts)
521cb0ef41Sopenharmony_ci    .on('integrity', (int) => {
531cb0ef41Sopenharmony_ci      integrity = int
541cb0ef41Sopenharmony_ci    })
551cb0ef41Sopenharmony_ci    .on('size', (s) => {
561cb0ef41Sopenharmony_ci      size = s
571cb0ef41Sopenharmony_ci    })
581cb0ef41Sopenharmony_ci    .on('error', (err) => {
591cb0ef41Sopenharmony_ci      error = err
601cb0ef41Sopenharmony_ci    })
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  pipeline.push(contentStream)
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  // last but not least, we write the index and emit hash and size,
651cb0ef41Sopenharmony_ci  // and memoize if we're doing that
661cb0ef41Sopenharmony_ci  pipeline.push(new Flush({
671cb0ef41Sopenharmony_ci    async flush () {
681cb0ef41Sopenharmony_ci      if (!error) {
691cb0ef41Sopenharmony_ci        const entry = await index.insert(cache, key, integrity, { ...opts, size })
701cb0ef41Sopenharmony_ci        if (memoize && memoData) {
711cb0ef41Sopenharmony_ci          memo.put(cache, entry, memoData, opts)
721cb0ef41Sopenharmony_ci        }
731cb0ef41Sopenharmony_ci        pipeline.emit('integrity', integrity)
741cb0ef41Sopenharmony_ci        pipeline.emit('size', size)
751cb0ef41Sopenharmony_ci      }
761cb0ef41Sopenharmony_ci    },
771cb0ef41Sopenharmony_ci  }))
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  return pipeline
801cb0ef41Sopenharmony_ci}
81