@squiz/render-runtime-lib 1.2.1-alpha.84 → 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
@@ -26287,7 +26287,7 @@ var require_v1 = __commonJS({
26287
26287
  additionalProperties: false,
26288
26288
  properties: {
26289
26289
  $schema: { type: "string", description: "the manifest schema version" },
26290
- name: { type: "string", description: "Name of the component" },
26290
+ name: { type: "string", description: "Name of the component", $ref: "#/$defs/name-pattern" },
26291
26291
  "main-function": {
26292
26292
  type: "string",
26293
26293
  description: "Name of the main function to be executed at the root of the access url"
@@ -26318,10 +26318,16 @@ var require_v1 = __commonJS({
26318
26318
  properties: {
26319
26319
  name: {
26320
26320
  type: "string",
26321
- pattern: "^[^/\\s\\\\]+$",
26321
+ $ref: "#/$defs/name-pattern",
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
  },
@@ -26367,7 +26373,10 @@ var require_v1 = __commonJS({
26367
26373
  }
26368
26374
  }
26369
26375
  },
26370
- required: ["name", "version", "functions", "$schema", "main-function"]
26376
+ required: ["name", "version", "functions", "$schema", "main-function"],
26377
+ $defs: {
26378
+ "name-pattern": { pattern: "^[a-zA-Z0-9_\\-]+$" }
26379
+ }
26371
26380
  };
26372
26381
  }
26373
26382
  });
@@ -94182,6 +94191,38 @@ var WorkerPool = class extends import_events.EventEmitter {
94182
94191
 
94183
94192
  // src/component-runner/index.ts
94184
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
94185
94226
  var DEFAULT_WORKER_FILE = import_path.default.resolve(__dirname, "./worker/worker-root.js");
94186
94227
  var ComponentRunner = class {
94187
94228
  static get() {
@@ -94228,7 +94269,20 @@ var ComponentRunner = class {
94228
94269
  this.singleton = void 0;
94229
94270
  }
94230
94271
  static buildExecute(pool, config) {
94231
- 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
+ }
94232
94286
  return new Promise((resolve, reject) => {
94233
94287
  const callback = (error, data) => {
94234
94288
  if (error || !error && !data) {
@@ -94246,7 +94300,7 @@ var ComponentRunner = class {
94246
94300
  resolve(data);
94247
94301
  }
94248
94302
  };
94249
- pool.runTask(executeComponentTask, callback, config.workerTimeout ?? 5e3);
94303
+ pool.runTask({ ...executeComponentTask, environment }, callback, config.workerTimeout ?? 5e3);
94250
94304
  });
94251
94305
  };
94252
94306
  }
@@ -95112,10 +95166,10 @@ var _ComponentFixture = class {
95112
95166
  await import_promises2.default.writeFile(import_path6.default.join(componentDirectory, "static", "static.txt"), "hello");
95113
95167
  return { fixtureDirectory, version, componentName: import_path6.default.basename(fixtureDirectory) };
95114
95168
  }
95115
- static async new(returnObj) {
95169
+ static async new(returnObj, { raw = false, environment = [] } = {}) {
95116
95170
  const tempDir = await import_promises2.default.mkdtemp(import_path6.default.join(_ComponentFixture.getFixtureRootDirectory(), "test-"));
95117
95171
  const componentPath = import_path6.default.join(tempDir, "main.js");
95118
- const data = jsFileData(returnObj);
95172
+ const data = jsFileData(returnObj, { raw });
95119
95173
  await import_promises2.default.writeFile(componentPath, data);
95120
95174
  return {
95121
95175
  id: import_path6.default.basename(tempDir),
@@ -95127,7 +95181,17 @@ var _ComponentFixture = class {
95127
95181
  version: "1.0.0",
95128
95182
  $schema: "",
95129
95183
  "main-function": "main.js",
95130
- functions: []
95184
+ functions: [
95185
+ {
95186
+ entry: "main.js",
95187
+ name: "main",
95188
+ environment,
95189
+ input: {},
95190
+ output: {
95191
+ "response-type": "html"
95192
+ }
95193
+ }
95194
+ ]
95131
95195
  },
95132
95196
  set: {
95133
95197
  name: "test-fixture-components",
@@ -95181,7 +95245,10 @@ var _ComponentFixture = class {
95181
95245
  };
95182
95246
  var ComponentFixture = _ComponentFixture;
95183
95247
  ComponentFixture.fixtureRootDirectory = void 0;
95184
- function jsFileData(input) {
95248
+ function jsFileData(input, { raw = false } = {}) {
95249
+ if (raw) {
95250
+ return `module.exports = () => ${input}`;
95251
+ }
95185
95252
  if (typeof input === "string") {
95186
95253
  return `module.exports = () => "${input}"`;
95187
95254
  } else {