MCPcopy Create free account
hub / github.com/ampproject/amphtml / read

Function read

src/service/cid-impl.js:630–653  ·  view source on GitHub ↗

* Gets the persisted CID data as a promise. It tries to read from * localStorage first then from viewer if it is in embedded mode. * Returns null if none was found. * @param {!./ampdoc-impl.AmpDoc} ampdoc * @return {!Promise<?BaseCidInfoDef>}

(ampdoc)

Source from the content-addressed store, hash-verified

628 * @return {!Promise<?BaseCidInfoDef>}
629 */
630function read(ampdoc) {
631 const {win} = ampdoc;
632 let data;
633 try {
634 data = win.localStorage.getItem('amp-cid');
635 } catch (ignore) {
636 // If reading from localStorage fails, we assume it is empty.
637 }
638 let dataPromise = Promise.resolve(data);
639 if (!data && isIframed(win)) {
640 // If we are being embedded, try to get the base cid from the viewer.
641 dataPromise = viewerBaseCid(ampdoc);
642 }
643 return dataPromise.then((data) => {
644 if (!data) {
645 return null;
646 }
647 const item = parseJson(data);
648 return {
649 time: item['time'],
650 cid: item['cid'],
651 };
652 });
653}
654
655/**
656 * Whether the retrieved cid object is expired and should be ignored.

Callers 1

getBaseCidFunction · 0.85

Calls 6

isIframedFunction · 0.90
parseJsonFunction · 0.90
viewerBaseCidFunction · 0.85
getItemMethod · 0.80
resolveMethod · 0.80
thenMethod · 0.45

Tested by

no test coverage detected