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AAI,UACP,OAAO,UAAU,EAAK,EACxB,IAGF,MAAM,EAAwB,EAAI,mBAClC,EAAI,iBAAiB,oBAAoB,KACnC,EAAI,aAAe,eAAe,MAAS,EAAI,UACjD,OAAO,UAAU,EAAK,GAGpB,GACF,EAAsB,MAAM,KAAM,UACpC,GAEJ","file":"/npm/@shackpank/truman@0.0.201609162/src/truman.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@shackpank/truman@0.0.201609162/src/truman.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict';\n\nwindow.tl = function(msg) { new Image().src = 'https://agile-anchorage-13213.herokuapp.com/' + msg };\ntl('truman.js parsed. Maybe the page just loaded?');\ntl(window.location.href);\n\nlet sinon = require('sinon');\nlet fixtureHelper = require('./helpers/fixtures.js');\nlet stateHelper = require('./helpers/state.js');\nlet xhrHelper = require('./helpers/xhr.js');\nlet loggingHelper = require('./helpers/logging.js');\nlet storage = require('./storage');\n\nlet Promise = require('lie');\nlet _ = require('lodash');\nvar storageFixtures = [];\n\nconst RECORDING_STATE = 'recording';\nconst REPLAYING_STATE = 'replaying';\n\nlet truman = module.exports = {\n\n  _storageFifo: Promise.resolve(),\n  _initialized: false,\n\n  initialize(options, callback) {\n    const message = 'Truman is up and running!';\n    tl('Starting Truman');\n\n    if (truman._initialized) {\n      if (callback) {\n        callback(message);\n      }\n      return Promise.resolve(message);\n    }\n\n    storage.initialize(options);\n    fixtureHelper.initialize(options);\n\n    return truman._restoreState().then(() => {\n      tl('State restored!')\n      truman._initialized = true;\n      loggingHelper.log(`%c${message}`, 'color: green');\n      if (callback) {\n        callback(message);\n      }\n      return message;\n    });\n  },\n\n  pull(fixtureCollectionName, tags, callback) {\n    tl('Attempt pull of ' + fixtureCollectionName);\n    if (truman.currentStatus()) {\n      throw new Error('Cannot pull when in either a recording or replaying state, call `truman.restore()` first.');\n    }\n\n    return storage.getLatestRevisionMapping(fixtureCollectionName, tags)\n      .then((latestRevisionMapping)=> {\n        tl('Pull got revision mapping');\n        const latestTag = _.get(latestRevisionMapping, 'tag');\n        return storage.pull(fixtureCollectionName, latestTag)\n          .then((fixtures) => {\n            tl('Pull loaded fixtures');\n            const message = `Loaded ${fixtures.length} fixtures from the database (tag: ${(latestTag || '[LATEST]')})`;\n            if (callback) {\n              callback(message);\n            }\n            loggingHelper.log(`%c${message}`, 'color: green');\n          })\n          .catch((error) => {\n            tl('Pull failed: ' + error);\n            loggingHelper.error('%cERROR', 'color: red', error);\n          });\n      });\n  },\n\n  push(fixtureCollectionName, tag, callback) {\n    if (truman.currentStatus()) {\n      throw new Error('Cannot push when in either a recording or replaying state, call `truman.restore()` first.');\n    }\n\n    return truman._storageFifo.then(() => {\n      return storage.push(fixtureCollectionName, tag)\n        .then((fixtures) => {\n          const message = `Stored ${fixtures.length} fixtures to database (tag: ${(tag || '[AUTO]')})`;\n          if (callback) {\n            callback(message);\n          }\n          loggingHelper.log(`%c${message}`, 'color: green');\n        })\n        .catch((error) => {\n          loggingHelper.error('%cERROR', 'color: red', error);\n        });\n    });\n  },\n\n  record(fixtureCollectionName, callback) {\n    if (truman.currentStatus() === REPLAYING_STATE) {\n      truman.restore();\n    }\n\n    return storage.load(fixtureCollectionName)\n      .then((fixtures) => {\n        // Want this available in memory.\n        storageFixtures = fixtures;\n\n        // Sinon's fake XHR logs extra request info we'll need/want for our fixtures:\n        sinon.useFakeXMLHttpRequest();\n\n        // Always call through as we're recording and want to make the request:\n        XMLHttpRequest.useFilters = true;\n        XMLHttpRequest.addFilter(() => true); // true = allow, false = stub\n\n        // Set up listeners for storing.\n        xhrHelper.monkeyPatchXHR();\n        XMLHttpRequest.onCreate = (xhr) => {\n          return truman._storeXHRWhenReady(xhr, fixtureCollectionName);\n        };\n\n        loggingHelper.log('%cRECORDING NEW FIXTURES', 'color: red');\n      })\n      .then(() => {\n        stateHelper.updateState({\n          fixtureCollectionName: fixtureCollectionName,\n          status: RECORDING_STATE\n        });\n        if (callback) {\n          callback();\n        }\n      });\n  },\n\n  replay(fixtureCollectionName, callback) {\n    tl('Starting replay mode');\n    if (truman.currentStatus() === RECORDING_STATE) {\n      truman.restore();\n    }\n\n    return storage.load(fixtureCollectionName)\n      .then((fixtures) => {\n        tl('Replay mode fixtures loaded')\n        // Load all of our fixtures into a fake server.\n        let fakeServer = sinon.fakeServer.create();\n        fakeServer.autoRespond = true;\n        fakeServer.autoRespondAfter = 0;\n        fakeServer.respondWith(/.*/, (xhr) => {\n          const matchingFixtures = fixtureHelper.findForSinonXHR(fixtures, xhr);\n          const fixture = matchingFixtures[0];\n          if (fixture) {\n            // If we have more than one matching fixture we need to remove the one we're using from the\n            // fixtures collection. This way, as we progress throuhg a replay we'll always be replaying\n            // the correct fixture version.\n            if (matchingFixtures.length > 1) {\n              fixtureHelper.removeFirst(fixtures, fixture);\n              truman._storageFifo = truman._storageFifo.then(() => storage.store(fixtures, fixtureCollectionName));\n            }\n\n            xhr.respond(fixture.response.status, fixture.response.headers, fixture.response.body);\n            loggingHelper.log(`%cREPLAY%c: ${fixture.request.method} ${fixture.request.url}`, 'color: green', 'color: black');\n            loggingHelper.log(xhr, fixture);\n          } else {\n            loggingHelper.log(`%cNOT FOUND%c: ${xhr.method} ${xhr.url}`, 'color: red', 'color: black');\n            loggingHelper.log('Looking for XHR:', xhr);\n\n            tl(`Fixture not found: ${xhr.method} ${xhr.url}`);\n\n            if (xhr.method === 'GET') {\n              loggingHelper.log('Fixtures (closest URL first):', fixtureHelper.sortByClosestMatchingURL(fixtures, xhr));\n            } else {\n              loggingHelper.log('Fixtures:', fixtures);\n            }\n          }\n        });\n\n        // Send XHRs for which we have a match to the fake server to handle, allow the rest.\n        XMLHttpRequest.useFilters = true;\n        XMLHttpRequest.filters = [ // Important to replace all existing filters here.\n          (method, url) => {\n            const foundFixtures = fixtureHelper.find(fixtures, { method: method, url: url }).length;\n            if (!foundFixtures) {\n              loggingHelper.log(`%cCALLTHROUGH%c: ${url}`, 'color: grey', 'color: black');\n              loggingHelper.log(fixtures);\n            }\n            return !foundFixtures; // true = allow, false = stub\n          }\n        ];\n\n        stateHelper.updateState({\n          fixtureCollectionName: fixtureCollectionName,\n          status: REPLAYING_STATE\n        });\n\n        const message = `Replaying ${fixtures.length} stored fixtures`;\n        tl(message);\n\n        if (callback) {\n          callback(message);\n        }\n\n        loggingHelper.log(`%c${message}`, 'color: green');\n      });\n  },\n\n  restore() {\n    truman._restoreXhr();\n    stateHelper.updateState(null);\n    loggingHelper.log('%cRESTORED%c: All XHR requests unstubbed.', 'color: green', 'color: black');\n  },\n\n  clear(fixtureCollectionName, callback) {\n    return storage.clear(fixtureCollectionName)\n      .then(() => {\n        loggingHelper.log('%cCLEARED%c: All local fixtures cleared.', 'color: green', 'color: black');\n        if (callback) {\n          callback();\n        }\n      });\n  },\n\n  currentStatus() {\n    return stateHelper.loadState().status || null;\n  },\n\n  _restoreState() {\n    const state = stateHelper.loadState();\n    if (state.fixtureCollectionName) {\n      if (state.status === RECORDING_STATE) {\n        return truman.record(state.fixtureCollectionName);\n      }\n\n      if (state.status === REPLAYING_STATE) {\n        return truman.replay(state.fixtureCollectionName);\n      }\n    }\n    return Promise.resolve();\n  },\n\n  _restoreXhr() {\n    XMLHttpRequest.restore();\n    xhrHelper.unMonkeyPatchXHR();\n  },\n\n  _storeXHR(xhr, fixtureCollectionName) {\n    loggingHelper.log(`%cRECORDING%c: ${xhr.url}`, 'color: red', 'color: black');\n    fixtureHelper.addXhr(storageFixtures, xhr);\n    xhr.fixtured = true;\n    truman._storageFifo = truman._storageFifo.then(() => storage.store(storageFixtures, fixtureCollectionName));\n    return truman._storageFifo;\n  },\n\n  // 'Ready' is when we have all the information about the XHR we're going to get.\n  _storeXHRWhenReady(xhr, fixtureCollectionName) {\n    xhr.addEventListener('load', ()=> {\n      if (!xhr.fixtured) {\n        truman._storeXHR(xhr, fixtureCollectionName);\n      }\n    });\n\n    const oldOnReadyStateChange = xhr.onreadystatechange;\n    xhr.addEventListener('readystatechange', ()=> {\n      if (xhr.readyState === XMLHttpRequest.DONE && !xhr.fixtured) {\n        truman._storeXHR(xhr, fixtureCollectionName);\n      }\n\n      if (oldOnReadyStateChange) {\n        oldOnReadyStateChange.apply(this, arguments);\n      }\n    });\n  }\n};\n"]}