86 lines
3.5 KiB
JavaScript
86 lines
3.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "RequestAsyncStorageWrapper", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return RequestAsyncStorageWrapper;
|
|
}
|
|
});
|
|
const _approuterheaders = require("../../client/components/app-router-headers");
|
|
const _headers = require("../web/spec-extension/adapters/headers");
|
|
const _requestcookies = require("../web/spec-extension/adapters/request-cookies");
|
|
const _cookies = require("../web/spec-extension/cookies");
|
|
const _draftmodeprovider = require("./draft-mode-provider");
|
|
function getHeaders(headers) {
|
|
const cleaned = _headers.HeadersAdapter.from(headers);
|
|
for (const param of _approuterheaders.FLIGHT_PARAMETERS){
|
|
cleaned.delete(param.toString().toLowerCase());
|
|
}
|
|
return _headers.HeadersAdapter.seal(cleaned);
|
|
}
|
|
function getCookies(headers) {
|
|
const cookies = new _cookies.RequestCookies(_headers.HeadersAdapter.from(headers));
|
|
return _requestcookies.RequestCookiesAdapter.seal(cookies);
|
|
}
|
|
function getMutableCookies(headers, onUpdateCookies) {
|
|
const cookies = new _cookies.RequestCookies(_headers.HeadersAdapter.from(headers));
|
|
return _requestcookies.MutableRequestCookiesAdapter.wrap(cookies, onUpdateCookies);
|
|
}
|
|
const RequestAsyncStorageWrapper = {
|
|
/**
|
|
* Wrap the callback with the given store so it can access the underlying
|
|
* store using hooks.
|
|
*
|
|
* @param storage underlying storage object returned by the module
|
|
* @param context context to seed the store
|
|
* @param callback function to call within the scope of the context
|
|
* @returns the result returned by the callback
|
|
*/ wrap (storage, { req, res, renderOpts }, callback) {
|
|
let previewProps = undefined;
|
|
if (renderOpts && "previewProps" in renderOpts) {
|
|
// TODO: investigate why previewProps isn't on RenderOpts
|
|
previewProps = renderOpts.previewProps;
|
|
}
|
|
function defaultOnUpdateCookies(cookies) {
|
|
if (res) {
|
|
res.setHeader("Set-Cookie", cookies);
|
|
}
|
|
}
|
|
const cache = {};
|
|
const store = {
|
|
get headers () {
|
|
if (!cache.headers) {
|
|
// Seal the headers object that'll freeze out any methods that could
|
|
// mutate the underlying data.
|
|
cache.headers = getHeaders(req.headers);
|
|
}
|
|
return cache.headers;
|
|
},
|
|
get cookies () {
|
|
if (!cache.cookies) {
|
|
// Seal the cookies object that'll freeze out any methods that could
|
|
// mutate the underlying data.
|
|
cache.cookies = getCookies(req.headers);
|
|
}
|
|
return cache.cookies;
|
|
},
|
|
get mutableCookies () {
|
|
if (!cache.mutableCookies) {
|
|
cache.mutableCookies = getMutableCookies(req.headers, (renderOpts == null ? void 0 : renderOpts.onUpdateCookies) || (res ? defaultOnUpdateCookies : undefined));
|
|
}
|
|
return cache.mutableCookies;
|
|
},
|
|
get draftMode () {
|
|
if (!cache.draftMode) {
|
|
cache.draftMode = new _draftmodeprovider.DraftModeProvider(previewProps, req, this.cookies, this.mutableCookies);
|
|
}
|
|
return cache.draftMode;
|
|
}
|
|
};
|
|
return storage.run(store, callback, store);
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=request-async-storage-wrapper.js.map
|