@valbuild/next 0.34.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +1 -1
  2. package/client/dist/valbuild-next-client.cjs.dev.js +11 -58
  3. package/client/dist/valbuild-next-client.cjs.prod.js +11 -58
  4. package/client/dist/valbuild-next-client.esm.js +10 -57
  5. package/dist/ValNextProvider-3676dacc.esm.js +25 -0
  6. package/dist/ValNextProvider-ae031b12.cjs.dev.js +29 -0
  7. package/dist/ValNextProvider-fe84f6a9.cjs.js +7 -0
  8. package/dist/ValNextProvider-fe84f6a9.cjs.prod.js +29 -0
  9. package/dist/declarations/src/ValProvider.d.ts +4 -2
  10. package/dist/declarations/src/client/index.d.ts +1 -1
  11. package/dist/declarations/src/client/initValClient.d.ts +8 -0
  12. package/dist/declarations/src/initVal.d.ts +3 -1
  13. package/dist/declarations/src/rsc/index.d.ts +2 -0
  14. package/dist/declarations/src/rsc/initValRsc.d.ts +19 -0
  15. package/dist/declarations/src/server/index.d.ts +1 -1
  16. package/dist/declarations/src/server/initValServer.d.ts +11 -0
  17. package/dist/slicedToArray-1471796d.cjs.dev.js +58 -0
  18. package/dist/slicedToArray-4190fac1.cjs.prod.js +58 -0
  19. package/dist/slicedToArray-62cd636a.esm.js +56 -0
  20. package/dist/valbuild-next.cjs.dev.js +63 -4
  21. package/dist/valbuild-next.cjs.prod.js +63 -4
  22. package/dist/valbuild-next.esm.js +63 -5
  23. package/package.json +15 -6
  24. package/rsc/dist/valbuild-next-rsc.cjs.d.ts +2 -0
  25. package/rsc/dist/valbuild-next-rsc.cjs.d.ts.map +1 -0
  26. package/rsc/dist/valbuild-next-rsc.cjs.dev.js +159 -0
  27. package/rsc/dist/valbuild-next-rsc.cjs.js +7 -0
  28. package/rsc/dist/valbuild-next-rsc.cjs.prod.js +159 -0
  29. package/rsc/dist/valbuild-next-rsc.esm.js +155 -0
  30. package/rsc/package.json +4 -0
  31. package/server/dist/valbuild-next-server.cjs.dev.js +78 -133
  32. package/server/dist/valbuild-next-server.cjs.prod.js +78 -133
  33. package/server/dist/valbuild-next-server.esm.js +78 -115
  34. package/dist/declarations/src/client/useVal.d.ts +0 -3
  35. package/dist/declarations/src/server/fetchVal.d.ts +0 -3
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@valbuild/core');
6
6
  var internal = require('@valbuild/react/internal');
7
+ var ValNextProvider = require('./ValNextProvider-ae031b12.cjs.dev.js');
7
8
  var stega = require('@valbuild/react/stega');
8
9
 
9
10
  function _interopNamespace(e) {
@@ -26,15 +27,73 @@ function _interopNamespace(e) {
26
27
 
27
28
  var core__namespace = /*#__PURE__*/_interopNamespace(core);
28
29
 
29
- var ValProvider = internal.ValProvider;
30
+ var ValProvider = ValNextProvider.ValNextProvider;
30
31
 
31
- var initVal = function initVal() {
32
+ function _toPrimitive(input, hint) {
33
+ if (typeof input !== "object" || input === null) return input;
34
+ var prim = input[Symbol.toPrimitive];
35
+ if (prim !== undefined) {
36
+ var res = prim.call(input, hint || "default");
37
+ if (typeof res !== "object") return res;
38
+ throw new TypeError("@@toPrimitive must return a primitive value.");
39
+ }
40
+ return (hint === "string" ? String : Number)(input);
41
+ }
42
+
43
+ function _toPropertyKey(arg) {
44
+ var key = _toPrimitive(arg, "string");
45
+ return typeof key === "symbol" ? key : String(key);
46
+ }
47
+
48
+ function _defineProperty(obj, key, value) {
49
+ key = _toPropertyKey(key);
50
+ if (key in obj) {
51
+ Object.defineProperty(obj, key, {
52
+ value: value,
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true
56
+ });
57
+ } else {
58
+ obj[key] = value;
59
+ }
60
+ return obj;
61
+ }
62
+
63
+ function ownKeys(e, r) {
64
+ var t = Object.keys(e);
65
+ if (Object.getOwnPropertySymbols) {
66
+ var o = Object.getOwnPropertySymbols(e);
67
+ r && (o = o.filter(function (r) {
68
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
69
+ })), t.push.apply(t, o);
70
+ }
71
+ return t;
72
+ }
73
+ function _objectSpread2(e) {
74
+ for (var r = 1; r < arguments.length; r++) {
75
+ var t = null != arguments[r] ? arguments[r] : {};
76
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
77
+ _defineProperty(e, r, t[r]);
78
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
79
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
80
+ });
81
+ }
82
+ return e;
83
+ }
84
+
85
+ var initVal = function initVal(config) {
32
86
  var _createValSystem = core.initVal(),
33
87
  s = _createValSystem.s,
34
- val = _createValSystem.val;
88
+ val = _createValSystem.val,
89
+ systemConfig = _createValSystem.config;
90
+ var currentConfig = _objectSpread2(_objectSpread2(_objectSpread2({}, systemConfig), config), {}, {
91
+ valConfigPath: (config === null || config === void 0 ? void 0 : config.valConfigPath) || "./val.config"
92
+ });
35
93
  return {
36
94
  s: s,
37
- val: val
95
+ val: val,
96
+ config: currentConfig
38
97
  };
39
98
  };
40
99
 
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@valbuild/core');
6
6
  var internal = require('@valbuild/react/internal');
7
+ var ValNextProvider = require('./ValNextProvider-fe84f6a9.cjs.prod.js');
7
8
  var stega = require('@valbuild/react/stega');
8
9
 
9
10
  function _interopNamespace(e) {
@@ -26,15 +27,73 @@ function _interopNamespace(e) {
26
27
 
27
28
  var core__namespace = /*#__PURE__*/_interopNamespace(core);
28
29
 
29
- var ValProvider = internal.ValProvider;
30
+ var ValProvider = ValNextProvider.ValNextProvider;
30
31
 
31
- var initVal = function initVal() {
32
+ function _toPrimitive(input, hint) {
33
+ if (typeof input !== "object" || input === null) return input;
34
+ var prim = input[Symbol.toPrimitive];
35
+ if (prim !== undefined) {
36
+ var res = prim.call(input, hint || "default");
37
+ if (typeof res !== "object") return res;
38
+ throw new TypeError("@@toPrimitive must return a primitive value.");
39
+ }
40
+ return (hint === "string" ? String : Number)(input);
41
+ }
42
+
43
+ function _toPropertyKey(arg) {
44
+ var key = _toPrimitive(arg, "string");
45
+ return typeof key === "symbol" ? key : String(key);
46
+ }
47
+
48
+ function _defineProperty(obj, key, value) {
49
+ key = _toPropertyKey(key);
50
+ if (key in obj) {
51
+ Object.defineProperty(obj, key, {
52
+ value: value,
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true
56
+ });
57
+ } else {
58
+ obj[key] = value;
59
+ }
60
+ return obj;
61
+ }
62
+
63
+ function ownKeys(e, r) {
64
+ var t = Object.keys(e);
65
+ if (Object.getOwnPropertySymbols) {
66
+ var o = Object.getOwnPropertySymbols(e);
67
+ r && (o = o.filter(function (r) {
68
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
69
+ })), t.push.apply(t, o);
70
+ }
71
+ return t;
72
+ }
73
+ function _objectSpread2(e) {
74
+ for (var r = 1; r < arguments.length; r++) {
75
+ var t = null != arguments[r] ? arguments[r] : {};
76
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
77
+ _defineProperty(e, r, t[r]);
78
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
79
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
80
+ });
81
+ }
82
+ return e;
83
+ }
84
+
85
+ var initVal = function initVal(config) {
32
86
  var _createValSystem = core.initVal(),
33
87
  s = _createValSystem.s,
34
- val = _createValSystem.val;
88
+ val = _createValSystem.val,
89
+ systemConfig = _createValSystem.config;
90
+ var currentConfig = _objectSpread2(_objectSpread2(_objectSpread2({}, systemConfig), config), {}, {
91
+ valConfigPath: (config === null || config === void 0 ? void 0 : config.valConfigPath) || "./val.config"
92
+ });
35
93
  return {
36
94
  s: s,
37
- val: val
95
+ val: val,
96
+ config: currentConfig
38
97
  };
39
98
  };
40
99
 
@@ -2,19 +2,77 @@ import { initVal as initVal$1 } from '@valbuild/core';
2
2
  import * as core from '@valbuild/core';
3
3
  export { core as expr };
4
4
  export { FILE_REF_PROP, GenericSelector, Schema, VAL_EXTENSION, derefPatch } from '@valbuild/core';
5
- import { ValProvider as ValProvider$1 } from '@valbuild/react/internal';
6
5
  export { ValRichText } from '@valbuild/react/internal';
6
+ import { ValNextProvider } from './ValNextProvider-3676dacc.esm.js';
7
7
  import { autoTagJSX } from '@valbuild/react/stega';
8
8
 
9
- var ValProvider = ValProvider$1;
9
+ var ValProvider = ValNextProvider;
10
10
 
11
- var initVal = function initVal() {
11
+ function _toPrimitive(input, hint) {
12
+ if (typeof input !== "object" || input === null) return input;
13
+ var prim = input[Symbol.toPrimitive];
14
+ if (prim !== undefined) {
15
+ var res = prim.call(input, hint || "default");
16
+ if (typeof res !== "object") return res;
17
+ throw new TypeError("@@toPrimitive must return a primitive value.");
18
+ }
19
+ return (hint === "string" ? String : Number)(input);
20
+ }
21
+
22
+ function _toPropertyKey(arg) {
23
+ var key = _toPrimitive(arg, "string");
24
+ return typeof key === "symbol" ? key : String(key);
25
+ }
26
+
27
+ function _defineProperty(obj, key, value) {
28
+ key = _toPropertyKey(key);
29
+ if (key in obj) {
30
+ Object.defineProperty(obj, key, {
31
+ value: value,
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true
35
+ });
36
+ } else {
37
+ obj[key] = value;
38
+ }
39
+ return obj;
40
+ }
41
+
42
+ function ownKeys(e, r) {
43
+ var t = Object.keys(e);
44
+ if (Object.getOwnPropertySymbols) {
45
+ var o = Object.getOwnPropertySymbols(e);
46
+ r && (o = o.filter(function (r) {
47
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
48
+ })), t.push.apply(t, o);
49
+ }
50
+ return t;
51
+ }
52
+ function _objectSpread2(e) {
53
+ for (var r = 1; r < arguments.length; r++) {
54
+ var t = null != arguments[r] ? arguments[r] : {};
55
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
56
+ _defineProperty(e, r, t[r]);
57
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
58
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
59
+ });
60
+ }
61
+ return e;
62
+ }
63
+
64
+ var initVal = function initVal(config) {
12
65
  var _createValSystem = initVal$1(),
13
66
  s = _createValSystem.s,
14
- val = _createValSystem.val;
67
+ val = _createValSystem.val,
68
+ systemConfig = _createValSystem.config;
69
+ var currentConfig = _objectSpread2(_objectSpread2(_objectSpread2({}, systemConfig), config), {}, {
70
+ valConfigPath: (config === null || config === void 0 ? void 0 : config.valConfigPath) || "./val.config"
71
+ });
15
72
  return {
16
73
  s: s,
17
- val: val
74
+ val: val,
75
+ config: currentConfig
18
76
  };
19
77
  };
20
78
 
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "next",
9
9
  "react"
10
10
  ],
11
- "version": "0.34.0",
11
+ "version": "0.35.0",
12
12
  "scripts": {
13
13
  "typecheck": "tsc --noEmit",
14
14
  "test": "jest"
@@ -20,6 +20,10 @@
20
20
  "module": "./dist/valbuild-next.esm.js",
21
21
  "default": "./dist/valbuild-next.cjs.js"
22
22
  },
23
+ "./rsc": {
24
+ "module": "./rsc/dist/valbuild-next-rsc.esm.js",
25
+ "default": "./rsc/dist/valbuild-next-rsc.cjs.js"
26
+ },
23
27
  "./client": {
24
28
  "module": "./client/dist/valbuild-next-client.esm.js",
25
29
  "default": "./client/dist/valbuild-next-client.cjs.js"
@@ -35,17 +39,21 @@
35
39
  "entrypoints": [
36
40
  "./index.ts",
37
41
  "./server/index.ts",
38
- "./client/index.ts"
42
+ "./client/index.ts",
43
+ "./rsc/index.ts"
39
44
  ],
40
45
  "exports": true
41
46
  },
42
47
  "dependencies": {
43
- "@valbuild/core": "~0.34.0",
44
- "@valbuild/react": "~0.34.0",
45
- "@valbuild/server": "~0.34.0",
48
+ "@valbuild/core": "~0.35.0",
49
+ "@valbuild/react": "~0.35.0",
50
+ "@valbuild/server": "~0.35.0",
46
51
  "client-only": "^0.0.1",
47
52
  "server-only": "^0.0.1"
48
53
  },
54
+ "devDependencies": {
55
+ "next": "^13.4.0"
56
+ },
49
57
  "peerDependencies": {
50
58
  "next": ">=13.4.0",
51
59
  "react": ">=18.2.0"
@@ -56,6 +64,7 @@
56
64
  "files": [
57
65
  "dist",
58
66
  "client",
59
- "server"
67
+ "server",
68
+ "rsc"
60
69
  ]
61
70
  }
@@ -0,0 +1,2 @@
1
+ export * from "../../dist/declarations/src/rsc/index";
2
+ //# sourceMappingURL=valbuild-next-rsc.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valbuild-next-rsc.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/rsc/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('server-only');
6
+ var stega = require('@valbuild/react/stega');
7
+ var core = require('@valbuild/core');
8
+ var fp = require('@valbuild/core/fp');
9
+
10
+ function optimizeTreePath(ids) {
11
+ return findUpperDirectory(ids);
12
+ }
13
+ function findUpperDirectory(paths) {
14
+ if (paths.length === 0) {
15
+ return null;
16
+ }
17
+ var pathSegments = paths[0].split("/");
18
+ for (var i = 1; i < paths.length; i++) {
19
+ var currentPathSegments = paths[i].split("/");
20
+ // Find the minimum length of the two paths
21
+ var minLength = Math.min(pathSegments.length, currentPathSegments.length);
22
+
23
+ // Iterate through the segments and find the common prefix
24
+ var commonPrefix = "";
25
+ for (var j = 0; j < minLength; j++) {
26
+ if (pathSegments[j] === currentPathSegments[j]) {
27
+ commonPrefix += pathSegments[j] + "/";
28
+ } else {
29
+ break;
30
+ }
31
+ }
32
+ pathSegments = commonPrefix.split("/").slice(0, -1);
33
+
34
+ // If there is no common prefix, return /
35
+ if (pathSegments.length <= 1) {
36
+ return "/";
37
+ }
38
+ }
39
+ var upperDirectory = pathSegments.join("/");
40
+ return upperDirectory;
41
+ }
42
+
43
+ var initFetchValStega = function initFetchValStega(config, valApiEndpoints, isEnabled, getHeaders, getCookies) {
44
+ return function (selector) {
45
+ var enabled = false;
46
+ try {
47
+ enabled = isEnabled();
48
+ } catch (err) {
49
+ console.error("Val: could not check if Val is enabled! This might be due to an error to check draftMode. fetchVal can only be used server-side. Use useVal on clients.", err);
50
+ }
51
+ if (enabled) {
52
+ var headers;
53
+ try {
54
+ headers = getHeaders();
55
+ if (!(headers instanceof Headers)) {
56
+ throw new Error("Expected an instance of Headers. Check Val rsc config.");
57
+ }
58
+ } catch (err) {
59
+ console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", err);
60
+ headers = null;
61
+ }
62
+ var cookies;
63
+ try {
64
+ cookies = getCookies();
65
+ } catch (err) {
66
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
67
+ cookies = null;
68
+ }
69
+ var host = headers && getHost(headers);
70
+ if (host && cookies && isProxyMode(config)) {
71
+ var _optimizeTreePath;
72
+ var api = new core.ValApi("".concat(host).concat(valApiEndpoints));
73
+ var valModuleIds = stega.getModuleIds(selector);
74
+ return api.getTree({
75
+ // TODO: get tree should probably have a list of ids instead
76
+ treePath: (_optimizeTreePath = optimizeTreePath(valModuleIds)) !== null && _optimizeTreePath !== void 0 ? _optimizeTreePath : undefined,
77
+ patch: true,
78
+ includeSource: true,
79
+ headers: getValAuthHeaders(cookies)
80
+ }).then(function (res) {
81
+ if (fp.result.isOk(res)) {
82
+ var modules = res.value.modules;
83
+ return stega.stegaEncode(selector, {
84
+ getModule: function getModule(moduleId) {
85
+ var module = modules[moduleId];
86
+ if (module) {
87
+ return module.source;
88
+ }
89
+ }
90
+ });
91
+ } else {
92
+ console.error("Val: could not fetch modules", res.error);
93
+ }
94
+ return stega.stegaEncode(selector, {});
95
+ })["catch"](function (err) {
96
+ console.error("Val: failed while checking modules", err);
97
+ return selector;
98
+ });
99
+ }
100
+ }
101
+ return stega.stegaEncode(selector, {
102
+ disabled: !enabled
103
+ });
104
+ };
105
+ };
106
+ function getHost(headers) {
107
+ var _headers$get;
108
+ // TODO: does NextJs have a way to determine this?
109
+ var host = headers.get("host");
110
+ var proto = "https";
111
+ if (headers.get("x-forwarded-proto") === "http") {
112
+ proto = "http";
113
+ } else if ((_headers$get = headers.get("referer")) !== null && _headers$get !== void 0 && _headers$get.startsWith("http://")) {
114
+ proto = "http";
115
+ } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
116
+ proto = "http";
117
+ }
118
+ if (host && proto) {
119
+ return "".concat(proto, "://").concat(host);
120
+ }
121
+ return null;
122
+ }
123
+ function isProxyMode(opts) {
124
+ var maybeApiKey = opts.apiKey || process.env.VAL_API_KEY;
125
+ var maybeValSecret = opts.valSecret || process.env.VAL_SECRET;
126
+ var isProxyMode = opts.mode === "proxy" || opts.mode === undefined && (maybeApiKey || maybeValSecret);
127
+ return !!isProxyMode;
128
+ }
129
+ function getValAuthHeaders(cookies) {
130
+ try {
131
+ var session = cookies.get(core.Internal.VAL_SESSION_COOKIE);
132
+ if (session) {
133
+ return {
134
+ Cookie: "".concat(core.Internal.VAL_SESSION_COOKIE, "=").concat(encodeURIComponent(session.value))
135
+ };
136
+ }
137
+ return {};
138
+ } catch (err) {
139
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
140
+ return {};
141
+ }
142
+ }
143
+ var valApiEndpoints = "/api/val";
144
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
+ function initValRsc(config, rscNextConfig) {
146
+ return {
147
+ fetchValStega: initFetchValStega(config, valApiEndpoints,
148
+ // TODO: get from config
149
+ function () {
150
+ return rscNextConfig.draftMode().isEnabled;
151
+ }, function () {
152
+ return rscNextConfig.headers();
153
+ }, function () {
154
+ return rscNextConfig.cookies();
155
+ })
156
+ };
157
+ }
158
+
159
+ exports.initValRsc = initValRsc;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./valbuild-next-rsc.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./valbuild-next-rsc.cjs.dev.js");
7
+ }
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('server-only');
6
+ var stega = require('@valbuild/react/stega');
7
+ var core = require('@valbuild/core');
8
+ var fp = require('@valbuild/core/fp');
9
+
10
+ function optimizeTreePath(ids) {
11
+ return findUpperDirectory(ids);
12
+ }
13
+ function findUpperDirectory(paths) {
14
+ if (paths.length === 0) {
15
+ return null;
16
+ }
17
+ var pathSegments = paths[0].split("/");
18
+ for (var i = 1; i < paths.length; i++) {
19
+ var currentPathSegments = paths[i].split("/");
20
+ // Find the minimum length of the two paths
21
+ var minLength = Math.min(pathSegments.length, currentPathSegments.length);
22
+
23
+ // Iterate through the segments and find the common prefix
24
+ var commonPrefix = "";
25
+ for (var j = 0; j < minLength; j++) {
26
+ if (pathSegments[j] === currentPathSegments[j]) {
27
+ commonPrefix += pathSegments[j] + "/";
28
+ } else {
29
+ break;
30
+ }
31
+ }
32
+ pathSegments = commonPrefix.split("/").slice(0, -1);
33
+
34
+ // If there is no common prefix, return /
35
+ if (pathSegments.length <= 1) {
36
+ return "/";
37
+ }
38
+ }
39
+ var upperDirectory = pathSegments.join("/");
40
+ return upperDirectory;
41
+ }
42
+
43
+ var initFetchValStega = function initFetchValStega(config, valApiEndpoints, isEnabled, getHeaders, getCookies) {
44
+ return function (selector) {
45
+ var enabled = false;
46
+ try {
47
+ enabled = isEnabled();
48
+ } catch (err) {
49
+ console.error("Val: could not check if Val is enabled! This might be due to an error to check draftMode. fetchVal can only be used server-side. Use useVal on clients.", err);
50
+ }
51
+ if (enabled) {
52
+ var headers;
53
+ try {
54
+ headers = getHeaders();
55
+ if (!(headers instanceof Headers)) {
56
+ throw new Error("Expected an instance of Headers. Check Val rsc config.");
57
+ }
58
+ } catch (err) {
59
+ console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", err);
60
+ headers = null;
61
+ }
62
+ var cookies;
63
+ try {
64
+ cookies = getCookies();
65
+ } catch (err) {
66
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
67
+ cookies = null;
68
+ }
69
+ var host = headers && getHost(headers);
70
+ if (host && cookies && isProxyMode(config)) {
71
+ var _optimizeTreePath;
72
+ var api = new core.ValApi("".concat(host).concat(valApiEndpoints));
73
+ var valModuleIds = stega.getModuleIds(selector);
74
+ return api.getTree({
75
+ // TODO: get tree should probably have a list of ids instead
76
+ treePath: (_optimizeTreePath = optimizeTreePath(valModuleIds)) !== null && _optimizeTreePath !== void 0 ? _optimizeTreePath : undefined,
77
+ patch: true,
78
+ includeSource: true,
79
+ headers: getValAuthHeaders(cookies)
80
+ }).then(function (res) {
81
+ if (fp.result.isOk(res)) {
82
+ var modules = res.value.modules;
83
+ return stega.stegaEncode(selector, {
84
+ getModule: function getModule(moduleId) {
85
+ var module = modules[moduleId];
86
+ if (module) {
87
+ return module.source;
88
+ }
89
+ }
90
+ });
91
+ } else {
92
+ console.error("Val: could not fetch modules", res.error);
93
+ }
94
+ return stega.stegaEncode(selector, {});
95
+ })["catch"](function (err) {
96
+ console.error("Val: failed while checking modules", err);
97
+ return selector;
98
+ });
99
+ }
100
+ }
101
+ return stega.stegaEncode(selector, {
102
+ disabled: !enabled
103
+ });
104
+ };
105
+ };
106
+ function getHost(headers) {
107
+ var _headers$get;
108
+ // TODO: does NextJs have a way to determine this?
109
+ var host = headers.get("host");
110
+ var proto = "https";
111
+ if (headers.get("x-forwarded-proto") === "http") {
112
+ proto = "http";
113
+ } else if ((_headers$get = headers.get("referer")) !== null && _headers$get !== void 0 && _headers$get.startsWith("http://")) {
114
+ proto = "http";
115
+ } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
116
+ proto = "http";
117
+ }
118
+ if (host && proto) {
119
+ return "".concat(proto, "://").concat(host);
120
+ }
121
+ return null;
122
+ }
123
+ function isProxyMode(opts) {
124
+ var maybeApiKey = opts.apiKey || process.env.VAL_API_KEY;
125
+ var maybeValSecret = opts.valSecret || process.env.VAL_SECRET;
126
+ var isProxyMode = opts.mode === "proxy" || opts.mode === undefined && (maybeApiKey || maybeValSecret);
127
+ return !!isProxyMode;
128
+ }
129
+ function getValAuthHeaders(cookies) {
130
+ try {
131
+ var session = cookies.get(core.Internal.VAL_SESSION_COOKIE);
132
+ if (session) {
133
+ return {
134
+ Cookie: "".concat(core.Internal.VAL_SESSION_COOKIE, "=").concat(encodeURIComponent(session.value))
135
+ };
136
+ }
137
+ return {};
138
+ } catch (err) {
139
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
140
+ return {};
141
+ }
142
+ }
143
+ var valApiEndpoints = "/api/val";
144
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
+ function initValRsc(config, rscNextConfig) {
146
+ return {
147
+ fetchValStega: initFetchValStega(config, valApiEndpoints,
148
+ // TODO: get from config
149
+ function () {
150
+ return rscNextConfig.draftMode().isEnabled;
151
+ }, function () {
152
+ return rscNextConfig.headers();
153
+ }, function () {
154
+ return rscNextConfig.cookies();
155
+ })
156
+ };
157
+ }
158
+
159
+ exports.initValRsc = initValRsc;