@squiz/edge-dev-render-runtime-lib 2.5.7 → 2.6.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.
@@ -37,7 +37,7 @@ export declare class RenderInputService {
37
37
  constructor(componentSetService: ComponentSetService, manifestService: ManifestService, componentFunctionService: ComponentFunctionService, rootUrlResolver: ComponentRootUrlResolver);
38
38
  resolveInput(request: RenderRequestInput): Promise<ComponentExecutionInput>;
39
39
  protected getQueryParametersFromRequest(request: RenderRequestInput, componentFunction: MANIFEST_MODELS.v1.ComponentFunction): QueryParameters;
40
- protected getPreviewRuntimeSet(manifest: ManifestV1, previewEnvVars?: Record<string, string>): {
40
+ getPreviewRuntimeSet(manifest: ManifestV1, previewEnvVars?: Record<string, string>): {
41
41
  webPath: string;
42
42
  displayName: string;
43
43
  environment: {
package/lib/index.js CHANGED
@@ -98,41 +98,26 @@ var esbuild = __toESM(require("esbuild"));
98
98
  var import_node_path = __toESM(require("node:path"));
99
99
  var import_node_vm = __toESM(require("node:vm"));
100
100
  var import_module = require("module");
101
+ var import_local_component_runtime = require("@squiz/local-component-runtime");
101
102
  var DevelopmentComponentRunnerService = class {
102
103
  async executeEdgeComponentFunction(task, contextAwareness) {
103
- const webRequestEvents = await this.setUpWebRequestEventForResolveUri(task);
104
- const builtComponent = await esbuild.build({
105
- entryPoints: [task.functionEntryFilePath],
106
- write: false,
107
- format: "cjs",
108
- bundle: true,
109
- loader: {
110
- ".js": "jsx"
104
+ const runtimeService = new ContentRuntimeService(task.manifest, task.manifestPath);
105
+ const output = await (0, import_local_component_runtime.runComponentFunction)(
106
+ {
107
+ entrypoint: task.functionEntryFilePath,
108
+ functionName: task.componentFunction.name,
109
+ helpers: {
110
+ resolveUri: (uri) => runtimeService.getContentItemPreview(uri)
111
+ }
111
112
  },
112
- platform: "node"
113
- });
114
- if (builtComponent.errors.length > 0) {
115
- for (const e of builtComponent.errors) {
116
- console.error(e.text);
117
- }
118
- throw new Error("Failed to build component");
119
- }
120
- const componentContext = this.getComponentContext(task, webRequestEvents, contextAwareness);
121
- const vmContext = import_node_vm.default.createContext({
122
- ...globalThis,
123
- module: {},
124
- exports: {},
125
- require: (0, import_module.createRequire)(task.functionEntryFilePath)
126
- });
127
- const script = new import_node_vm.default.Script(builtComponent.outputFiles[0].text, { filename: task.functionEntryFilePath });
128
- script.runInContext(vmContext);
129
- const f = vmContext.module.exports.default[task.componentFunction.name];
130
- if (typeof f === "function") {
131
- return await Promise.resolve(f(task.args[0] ?? {}, componentContext));
132
- }
133
- throw Error(
134
- `Function "${task.componentFunction.name}" not defined in the fn entry js file ${task.functionEntryFilePath}!`
113
+ task.args[0],
114
+ { environmentVariables: task.environment, ctx: contextAwareness }
135
115
  );
116
+ if (task.componentFunction.output.responseType === "json") {
117
+ return JSON.parse(output);
118
+ } else {
119
+ return output;
120
+ }
136
121
  }
137
122
  async executeServerComponentFunction(task) {
138
123
  const webRequestEvents = await this.setUpWebRequestEventForResolveUri(task);
@@ -208,7 +193,7 @@ var DevelopmentComponentRunnerService = class {
208
193
  //Update to match edge
209
194
  ctx: {
210
195
  url: contextAwareness?.matrixUrl,
211
- assetId: contextAwareness?.matrixUrl,
196
+ assetId: contextAwareness?.matrixAssetId,
212
197
  ...(0, import_component_lib2.createComponentContextualFunctions)(ctxInput)
213
198
  },
214
199
  fns: {
@@ -2032,17 +2017,13 @@ var DevelopmentController = class extends import_tsoa3.Controller {
2032
2017
  });
2033
2018
  return schemaResult;
2034
2019
  }
2035
- async TemporaryPreview(req, componentId, functionName, headers, envVars, data, contextMatrixAssetId, contextMatrixUrl) {
2020
+ async TemporaryPreview(req, componentId, functionName, headers, _previewEnvVars, data, contextMatrixAssetId, contextMatrixUrl) {
2036
2021
  this.checkDevMode();
2037
2022
  const { namespace, componentName, version } = getComponentInfoFromComponentId(componentId);
2038
2023
  const res = req.res;
2039
- const componentSet = {
2040
- environment: envVars ?? {},
2041
- displayName: componentName,
2042
- webPath: "mock-set"
2043
- };
2044
2024
  const rootUrl = `http://${req.headers.host}`.replace(/\/+$/, "");
2045
2025
  const componentManifest = await this.manifestService.loadManifest(namespace, componentName, version);
2026
+ const runtimeSet = this.renderInputService.getPreviewRuntimeSet(componentManifest, _previewEnvVars);
2046
2027
  const componentFunction = componentManifest.getComponentFunctionByName(functionName);
2047
2028
  if (hasSquizType(componentFunction)) {
2048
2029
  data = await resolveMock(componentFunction, data);
@@ -2052,12 +2033,12 @@ var DevelopmentController = class extends import_tsoa3.Controller {
2052
2033
  requestId: Date.now().toString(),
2053
2034
  args: [data],
2054
2035
  functionEntryFilePath: await componentManifest.getComponentFunctionEntryFilePath(componentFunction),
2055
- environment: componentSet.environment,
2036
+ environment: runtimeSet.environment,
2056
2037
  componentFullName: componentName,
2057
2038
  componentFunction,
2058
2039
  componentRootUrl: rootUrl,
2059
2040
  manifest: componentManifest.getModel(),
2060
- runtimeSet: componentSet,
2041
+ runtimeSet,
2061
2042
  manifestPath: await componentManifest.getManifestPath(),
2062
2043
  previewKey: defaultPreviewKey,
2063
2044
  queryParameters: void 0
@@ -2067,7 +2048,11 @@ var DevelopmentController = class extends import_tsoa3.Controller {
2067
2048
  matrixUrl: contextMatrixUrl
2068
2049
  };
2069
2050
  let result = componentManifest.type === "edge" ? await this.componentRunnerService.executeEdgeComponentFunction(componentTask, ctx) : await this.componentRunnerService.executeServerComponentFunction(componentTask);
2070
- const responseHeaders = headers ?? {};
2051
+ const manifestHeaders = componentFunction?.output?.headers ?? {};
2052
+ const responseHeaders = {
2053
+ ...headers,
2054
+ manifestHeaders
2055
+ };
2071
2056
  switch (componentFunction.output.responseType) {
2072
2057
  case "html":
2073
2058
  responseHeaders["content-type"] = "text/html";
@@ -2132,8 +2117,16 @@ var DevelopmentController = class extends import_tsoa3.Controller {
2132
2117
  expressRequest,
2133
2118
  functionName
2134
2119
  });
2135
- const renderedOutput = await this.doFunctionRender(renderInput, ctx);
2136
- this.handleResponse(renderInput, renderedOutput, response);
2120
+ const inputHeaders = expressRequest.query["headers"];
2121
+ const renderInputWithHeaders = {
2122
+ ...renderInput,
2123
+ headers: {
2124
+ ...renderInput.headers,
2125
+ ...inputHeaders
2126
+ }
2127
+ };
2128
+ const renderedOutput = await this.doFunctionRender(renderInputWithHeaders, ctx);
2129
+ this.handleResponse(renderInputWithHeaders, renderedOutput, response);
2137
2130
  }
2138
2131
  handleResponse(input, output, response) {
2139
2132
  this.setHeadersOnResponse(input, response);
@@ -2887,7 +2880,7 @@ function RegisterRoutes(app) {
2887
2880
  componentId: { "in": "path", "name": "componentId", "required": true, "dataType": "string" },
2888
2881
  functionName: { "in": "path", "name": "functionName", "required": true, "dataType": "string" },
2889
2882
  headers: { "in": "query", "name": "headers", "dataType": "any" },
2890
- envVars: { "in": "query", "name": "envVars", "dataType": "any" },
2883
+ _previewEnvVars: { "in": "query", "name": "_previewEnvVars", "dataType": "any" },
2891
2884
  data: { "in": "query", "name": "data", "dataType": "any" },
2892
2885
  contextMatrixAssetId: { "in": "query", "name": "contextMatrixAssetId", "dataType": "string" },
2893
2886
  contextMatrixUrl: { "in": "query", "name": "contextMatrixUrl", "dataType": "string" }