@squiz/render-runtime-lib 1.2.1-alpha.86 → 1.2.1-alpha.87

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.
@@ -0,0 +1,17 @@
1
+ import { ComponentInfo } from '@squiz/component-lib';
2
+ declare type EnvironmentVariables = Record<string, string | undefined>;
3
+ export declare class InvalidEnvironmentVariablesError extends Error {
4
+ readonly errors: [string, string][];
5
+ constructor(errors: [string, string][]);
6
+ }
7
+ export interface ComponentEnvironmentLoader {
8
+ getEnvironmentVariables(componentSet: string): Promise<EnvironmentVariables>;
9
+ }
10
+ interface EnvironmentVariableValidation {
11
+ valid: boolean;
12
+ errors: [string, string][];
13
+ variables: EnvironmentVariables;
14
+ }
15
+ export declare function loadEnvironment(info: ComponentInfo, environmentLoader: ComponentEnvironmentLoader): Promise<EnvironmentVariableValidation>;
16
+ export declare function getProcessEnvironmentLoader(): ComponentEnvironmentLoader;
17
+ export {};
package/lib/index.js CHANGED
@@ -26322,6 +26322,12 @@ var require_v1 = __commonJS({
26322
26322
  description: "Function name, this will be used as part of the url to access this function. It must be a valid url"
26323
26323
  },
26324
26324
  entry: { type: "string" },
26325
+ environment: {
26326
+ type: "array",
26327
+ items: {
26328
+ type: "string"
26329
+ }
26330
+ },
26325
26331
  input: {
26326
26332
  type: "object"
26327
26333
  },
@@ -94185,6 +94191,38 @@ var WorkerPool = class extends import_events.EventEmitter {
94185
94191
 
94186
94192
  // src/component-runner/index.ts
94187
94193
  var import_component_lib2 = __toESM(require_lib7());
94194
+
94195
+ // src/component-runner/environment-setup.ts
94196
+ var InvalidEnvironmentVariablesError = class extends Error {
94197
+ constructor(errors) {
94198
+ const message2 = errors.map(([variableName, error]) => `"${variableName}" ${error}`).join("; ");
94199
+ super(`Invalid Environment Variables: ${message2}`);
94200
+ this.errors = errors;
94201
+ }
94202
+ };
94203
+ function validateComponentVariables(functionVariables, environment) {
94204
+ const allVariables = functionVariables.map((v) => [v, environment[v]]);
94205
+ return {
94206
+ valid: functionVariables.every((name) => name in environment),
94207
+ errors: functionVariables.filter((name) => !(name in environment)).map((name) => [name, "is required"]),
94208
+ variables: Object.fromEntries(allVariables)
94209
+ };
94210
+ }
94211
+ async function loadEnvironment(info, environmentLoader) {
94212
+ const variables = await environmentLoader.getEnvironmentVariables(info.set.name);
94213
+ const functionDef = info.manifest.functions.find((f) => f.name === info.functionName);
94214
+ const variableValidation = validateComponentVariables(functionDef.environment || [], variables);
94215
+ return variableValidation;
94216
+ }
94217
+ function getProcessEnvironmentLoader() {
94218
+ return {
94219
+ getEnvironmentVariables(_componentSet) {
94220
+ return Promise.resolve(process.env);
94221
+ }
94222
+ };
94223
+ }
94224
+
94225
+ // src/component-runner/index.ts
94188
94226
  var DEFAULT_WORKER_FILE = import_path.default.resolve(__dirname, "./worker/worker-root.js");
94189
94227
  var ComponentRunner = class {
94190
94228
  static get() {
@@ -94231,7 +94269,20 @@ var ComponentRunner = class {
94231
94269
  this.singleton = void 0;
94232
94270
  }
94233
94271
  static buildExecute(pool, config) {
94234
- return (executeComponentTask) => {
94272
+ const environmentLoader = config.doDevReload ? getProcessEnvironmentLoader() : {
94273
+ getEnvironmentVariables(_2) {
94274
+ return Promise.resolve({});
94275
+ }
94276
+ };
94277
+ return async (executeComponentTask) => {
94278
+ const {
94279
+ variables: environment,
94280
+ valid,
94281
+ errors
94282
+ } = await loadEnvironment(executeComponentTask.info, environmentLoader);
94283
+ if (!valid) {
94284
+ throw new InvalidEnvironmentVariablesError(errors);
94285
+ }
94235
94286
  return new Promise((resolve, reject) => {
94236
94287
  const callback = (error, data) => {
94237
94288
  if (error || !error && !data) {
@@ -94249,7 +94300,7 @@ var ComponentRunner = class {
94249
94300
  resolve(data);
94250
94301
  }
94251
94302
  };
94252
- pool.runTask(executeComponentTask, callback, config.workerTimeout ?? 5e3);
94303
+ pool.runTask({ ...executeComponentTask, environment }, callback, config.workerTimeout ?? 5e3);
94253
94304
  });
94254
94305
  };
94255
94306
  }
@@ -95115,10 +95166,10 @@ var _ComponentFixture = class {
95115
95166
  await import_promises2.default.writeFile(import_path6.default.join(componentDirectory, "static", "static.txt"), "hello");
95116
95167
  return { fixtureDirectory, version, componentName: import_path6.default.basename(fixtureDirectory) };
95117
95168
  }
95118
- static async new(returnObj) {
95169
+ static async new(returnObj, { raw = false, environment = [] } = {}) {
95119
95170
  const tempDir = await import_promises2.default.mkdtemp(import_path6.default.join(_ComponentFixture.getFixtureRootDirectory(), "test-"));
95120
95171
  const componentPath = import_path6.default.join(tempDir, "main.js");
95121
- const data = jsFileData(returnObj);
95172
+ const data = jsFileData(returnObj, { raw });
95122
95173
  await import_promises2.default.writeFile(componentPath, data);
95123
95174
  return {
95124
95175
  id: import_path6.default.basename(tempDir),
@@ -95130,7 +95181,17 @@ var _ComponentFixture = class {
95130
95181
  version: "1.0.0",
95131
95182
  $schema: "",
95132
95183
  "main-function": "main.js",
95133
- functions: []
95184
+ functions: [
95185
+ {
95186
+ entry: "main.js",
95187
+ name: "main",
95188
+ environment,
95189
+ input: {},
95190
+ output: {
95191
+ "response-type": "html"
95192
+ }
95193
+ }
95194
+ ]
95134
95195
  },
95135
95196
  set: {
95136
95197
  name: "test-fixture-components",
@@ -95184,7 +95245,10 @@ var _ComponentFixture = class {
95184
95245
  };
95185
95246
  var ComponentFixture = _ComponentFixture;
95186
95247
  ComponentFixture.fixtureRootDirectory = void 0;
95187
- function jsFileData(input) {
95248
+ function jsFileData(input, { raw = false } = {}) {
95249
+ if (raw) {
95250
+ return `module.exports = () => ${input}`;
95251
+ }
95188
95252
  if (typeof input === "string") {
95189
95253
  return `module.exports = () => "${input}"`;
95190
95254
  } else {