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

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.
@@ -1234,6 +1234,9 @@ var require_acorn = __commonJS({
1234
1234
  if (options.allowReserved == null) {
1235
1235
  options.allowReserved = options.ecmaVersion < 5;
1236
1236
  }
1237
+ if (opts.allowHashBang == null) {
1238
+ options.allowHashBang = options.ecmaVersion >= 14;
1239
+ }
1237
1240
  if (isArray(options.onToken)) {
1238
1241
  var tokens = options.onToken;
1239
1242
  options.onToken = function(token) {
@@ -1480,7 +1483,7 @@ var require_acorn = __commonJS({
1480
1483
  }
1481
1484
  var parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind;
1482
1485
  if (parens > -1) {
1483
- this.raiseRecoverable(parens, "Parenthesized pattern");
1486
+ this.raiseRecoverable(parens, isAssign ? "Assigning to rvalue" : "Parenthesized pattern");
1484
1487
  }
1485
1488
  };
1486
1489
  pp$9.checkExpressionErrors = function(refDestructuringErrors, andThrow) {
@@ -2481,7 +2484,7 @@ var require_acorn = __commonJS({
2481
2484
  }
2482
2485
  };
2483
2486
  pp$8.isDirectiveCandidate = function(statement) {
2484
- return statement.type === "ExpressionStatement" && statement.expression.type === "Literal" && typeof statement.expression.value === "string" && (this.input[statement.start] === '"' || this.input[statement.start] === "'");
2487
+ return this.options.ecmaVersion >= 5 && statement.type === "ExpressionStatement" && statement.expression.type === "Literal" && typeof statement.expression.value === "string" && (this.input[statement.start] === '"' || this.input[statement.start] === "'");
2485
2488
  };
2486
2489
  var pp$7 = Parser.prototype;
2487
2490
  pp$7.toAssignable = function(node, isBinding, refDestructuringErrors) {
@@ -3545,14 +3548,6 @@ var require_acorn = __commonJS({
3545
3548
  }
3546
3549
  return this.finishNode(prop, "RestElement");
3547
3550
  }
3548
- if (this.type === types$1.parenL && refDestructuringErrors) {
3549
- if (refDestructuringErrors.parenthesizedAssign < 0) {
3550
- refDestructuringErrors.parenthesizedAssign = this.start;
3551
- }
3552
- if (refDestructuringErrors.parenthesizedBind < 0) {
3553
- refDestructuringErrors.parenthesizedBind = this.start;
3554
- }
3555
- }
3556
3551
  prop.argument = this.parseMaybeAssign(false, refDestructuringErrors);
3557
3552
  if (this.type === types$1.comma && refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
3558
3553
  refDestructuringErrors.trailingComma = this.start;
@@ -5627,7 +5622,7 @@ var require_acorn = __commonJS({
5627
5622
  }
5628
5623
  return this.finishToken(type, word);
5629
5624
  };
5630
- var version = "8.7.1";
5625
+ var version = "8.8.0";
5631
5626
  Parser.acorn = {
5632
5627
  Parser,
5633
5628
  version,
@@ -8107,6 +8102,12 @@ var require_v1 = __commonJS({
8107
8102
  description: "Function name, this will be used as part of the url to access this function. It must be a valid url"
8108
8103
  },
8109
8104
  entry: { type: "string" },
8105
+ environment: {
8106
+ type: "array",
8107
+ items: {
8108
+ type: "string"
8109
+ }
8110
+ },
8110
8111
  input: {
8111
8112
  type: "object"
8112
8113
  },
@@ -8126,8 +8127,58 @@ var require_v1 = __commonJS({
8126
8127
  additionalProperties: false,
8127
8128
  properties: {
8128
8129
  location: { oneOf: [{ const: "header" }, { const: "footer" }] },
8129
- file: { type: "string" },
8130
- type: { oneOf: [{ const: "library" }] }
8130
+ file: {
8131
+ oneOf: [
8132
+ {
8133
+ type: "object",
8134
+ title: "JsFile",
8135
+ additionalProperties: false,
8136
+ properties: {
8137
+ type: { const: "js" },
8138
+ filepath: { type: "string" },
8139
+ defer: { type: "boolean" },
8140
+ async: { type: "boolean" },
8141
+ crossorigin: { oneOf: [{ const: "anonymous" }, { const: "use-credentials" }] },
8142
+ referrerpolicy: {
8143
+ oneOf: [
8144
+ { const: "no-referrer" },
8145
+ { const: "no-referrer-when-downgrade" },
8146
+ { const: "origin" },
8147
+ { const: "origin-when-cross-origin" },
8148
+ { const: "same-origin" },
8149
+ { const: "strict-origin" },
8150
+ { const: "strict-origin-when-cross-origin" },
8151
+ { const: "unsafe-url" }
8152
+ ]
8153
+ }
8154
+ },
8155
+ required: ["filepath", "type"]
8156
+ },
8157
+ {
8158
+ type: "object",
8159
+ title: "CssFile",
8160
+ additionalProperties: false,
8161
+ properties: {
8162
+ type: { const: "css" },
8163
+ filepath: { type: "string" },
8164
+ crossorigin: { oneOf: [{ const: "anonymous" }, { const: "use-credentials" }] },
8165
+ referrerpolicy: {
8166
+ oneOf: [
8167
+ { const: "no-referrer" },
8168
+ { const: "no-referrer-when-downgrade" },
8169
+ { const: "origin" },
8170
+ { const: "origin-when-cross-origin" },
8171
+ { const: "same-origin" },
8172
+ { const: "strict-origin" },
8173
+ { const: "strict-origin-when-cross-origin" },
8174
+ { const: "unsafe-url" }
8175
+ ]
8176
+ }
8177
+ },
8178
+ required: ["filepath"]
8179
+ }
8180
+ ]
8181
+ }
8131
8182
  },
8132
8183
  required: ["location", "file"]
8133
8184
  }
@@ -8154,7 +8205,10 @@ var require_v1 = __commonJS({
8154
8205
  },
8155
8206
  required: ["name", "version", "functions", "$schema", "main-function"],
8156
8207
  $defs: {
8157
- "name-pattern": { pattern: "^[a-zA-Z0-9_\\-]+$" }
8208
+ "name-pattern": {
8209
+ type: "string",
8210
+ pattern: "^[a-zA-Z0-9_\\-]+$"
8211
+ }
8158
8212
  }
8159
8213
  };
8160
8214
  }
@@ -14815,7 +14869,7 @@ var require_schemas = __commonJS({
14815
14869
  exports.schemaVersions = exports.schemaVersionRegex = void 0;
14816
14870
  var v1_json_1 = __importDefault(require_v1());
14817
14871
  var ajv_1 = __importDefault(require_ajv());
14818
- var ajv = new ajv_1.default();
14872
+ var ajv = new ajv_1.default({ allErrors: true });
14819
14873
  exports.schemaVersionRegex = /\/(?<schema>\w+)\.json#?$/i;
14820
14874
  exports.schemaVersions = { v1: v1_json_1.default };
14821
14875
  for (const [version, schema] of Object.entries(exports.schemaVersions)) {
@@ -22814,6 +22868,7 @@ var require_LoadedComponent = __commonJS({
22814
22868
  var isValidationFailure_1 = require_isValidationFailure();
22815
22869
  var never_1 = require_never();
22816
22870
  var types_1 = require("util/types");
22871
+ var ajv = new ajv_1.default({ allErrors: true, strict: true, strictTypes: true });
22817
22872
  var LoadedComponent = class {
22818
22873
  constructor(manifest, componentFilesRoot, componentRootUrl, componentExecutor) {
22819
22874
  this.manifest = manifest;
@@ -22821,12 +22876,17 @@ var require_LoadedComponent = __commonJS({
22821
22876
  this.componentRootUrl = componentRootUrl;
22822
22877
  this.componentExecutor = componentExecutor;
22823
22878
  this.mainFunction = manifest["main-function"];
22824
- this.ajv = new ajv_1.default({ allErrors: true });
22825
22879
  for (const func of manifest.functions) {
22826
22880
  const slug = this.getFunctionComponentSlug(func.name);
22827
22881
  const output = this.getOutputValidationSchema(func.output, manifest);
22828
- this.ajv.addSchema(func.input, `${slug}-input`);
22829
- this.ajv.addSchema(output, `${slug}-output`);
22882
+ const inputSchemaName = `${slug}-input`;
22883
+ const outputSchemaName = `${slug}-output`;
22884
+ if (ajv.schemas[inputSchemaName] === void 0) {
22885
+ ajv.addSchema(func.input, inputSchemaName);
22886
+ }
22887
+ if (ajv.schemas[outputSchemaName] === void 0) {
22888
+ ajv.addSchema(output, outputSchemaName);
22889
+ }
22830
22890
  }
22831
22891
  }
22832
22892
  componentHasFunction(functionName) {
@@ -22883,7 +22943,7 @@ var require_LoadedComponent = __commonJS({
22883
22943
  throw new Error(`input value for ${functionName} cannot be undefined`);
22884
22944
  }
22885
22945
  const slug = this.getFunctionComponentSlug(functionName);
22886
- const validator = this.ajv.getSchema(`${slug}-input`);
22946
+ const validator = ajv.getSchema(`${slug}-input`);
22887
22947
  if (!validator) {
22888
22948
  throw new Error(`${functionName} has no input schema registered`);
22889
22949
  }
@@ -22892,7 +22952,7 @@ var require_LoadedComponent = __commonJS({
22892
22952
  }
22893
22953
  validateOutput(functionName, output) {
22894
22954
  const slug = this.getFunctionComponentSlug(functionName);
22895
- const validator = this.ajv.getSchema(`${slug}-output`);
22955
+ const validator = ajv.getSchema(`${slug}-output`);
22896
22956
  if (!validator) {
22897
22957
  throw new Error(`${functionName} has no output schema registered`);
22898
22958
  }
@@ -23201,11 +23261,11 @@ var require_loadComponent = __commonJS({
23201
23261
  const staticFileRoot = path_1.default.join(componentRoot, staticFileLocationRoot);
23202
23262
  const staticFiles = func.output["static-files"] || [];
23203
23263
  for (const staticFile of staticFiles) {
23204
- if (staticFile.file.startsWith("http://") || staticFile.file.startsWith("https://")) {
23264
+ if (staticFile.file.filepath.startsWith("http://") || staticFile.file.filepath.startsWith("https://")) {
23205
23265
  continue;
23206
23266
  }
23207
- if ((0, isPathTryingToAccessOutsideOfRoot_1.isPathTryingToAccessOutsideOfRoot)(staticFileRoot, staticFile.file) || !await isReadable(staticFileRoot, staticFile.file)) {
23208
- throw new Error(`access to a static file "${staticFile.file}" for "${func.name}" outside the parent directory in "${manifest.name} ${manifest.version}" is not alllowed`);
23267
+ if ((0, isPathTryingToAccessOutsideOfRoot_1.isPathTryingToAccessOutsideOfRoot)(staticFileRoot, staticFile.file.filepath) || !await isReadable(staticFileRoot, staticFile.file.filepath)) {
23268
+ throw new Error(`access to a static file "${staticFile.file.filepath}" for "${func.name}" outside the parent directory in "${manifest.name} ${manifest.version}" is not alllowed`);
23209
23269
  }
23210
23270
  }
23211
23271
  }
@@ -40698,6 +40758,432 @@ var require_getNodeEnv = __commonJS({
40698
40758
  }
40699
40759
  });
40700
40760
 
40761
+ // ../component-lib/lib/component-service/services/ComponentService.js
40762
+ var require_ComponentService = __commonJS({
40763
+ "../component-lib/lib/component-service/services/ComponentService.js"(exports) {
40764
+ "use strict";
40765
+ var __importDefault = exports && exports.__importDefault || function(mod) {
40766
+ return mod && mod.__esModule ? mod : { "default": mod };
40767
+ };
40768
+ Object.defineProperty(exports, "__esModule", { value: true });
40769
+ exports.ComponentService = void 0;
40770
+ var path_1 = __importDefault(require("path"));
40771
+ var fs_extra_1 = __importDefault(require_lib4());
40772
+ var error_1 = require_error();
40773
+ var validateManifest_1 = require_validateManifest();
40774
+ var ComponentService = class {
40775
+ constructor(db, logger, dataMountPoint) {
40776
+ this.db = db;
40777
+ this.logger = logger;
40778
+ this.dataMountPoint = dataMountPoint;
40779
+ }
40780
+ async saveComponent(componentLocation) {
40781
+ const manifest = await this.readManifest(componentLocation);
40782
+ this.assertManifestIsValid(manifest);
40783
+ if (await this.componentVersionExists(manifest.name, manifest.version)) {
40784
+ throw new error_1.BadRequestError(`Component ${manifest.name} ${manifest.version} already exists`);
40785
+ }
40786
+ const destPath = await this.getValidComponentDestinationPath(manifest);
40787
+ await this.moveFilesAndUpdateDb(manifest, componentLocation, destPath);
40788
+ }
40789
+ async componentVersionExists(name, version) {
40790
+ const componentVersion = await this.db.repositories.componentVersion.getComponentByNameAndVersion(name, version);
40791
+ return componentVersion !== void 0;
40792
+ }
40793
+ async deleteComponent(componentName) {
40794
+ this.assertValidComponentName(componentName);
40795
+ await this.assertComponentExists(componentName);
40796
+ const compPath = path_1.default.join(this.dataMountPoint, componentName);
40797
+ try {
40798
+ await this.db.repositories.componentVersion.delete({ componentName });
40799
+ } catch (e) {
40800
+ this.logger.error(e);
40801
+ }
40802
+ try {
40803
+ await this.db.repositories.component.delete({ name: componentName });
40804
+ } catch (e) {
40805
+ this.logger.error(e);
40806
+ }
40807
+ try {
40808
+ await fs_extra_1.default.rm(compPath, { force: true, recursive: true });
40809
+ } catch (e) {
40810
+ this.logger.error(e);
40811
+ }
40812
+ const componentExists = await this.db.repositories.component.componentExists(componentName);
40813
+ const filesExist = await fs_extra_1.default.pathExists(compPath);
40814
+ if (componentExists) {
40815
+ throw new Error(`failed to remove ${componentName} from DB`);
40816
+ }
40817
+ if (filesExist) {
40818
+ throw new Error(`failed to remove ${componentName} files from the disk`);
40819
+ }
40820
+ }
40821
+ async deleteComponentVersion(componentName, version) {
40822
+ this.assertValidComponentVersion(componentName, version);
40823
+ await this.assertComponentVersionExists(componentName, version);
40824
+ const compVersionPath = path_1.default.join(this.dataMountPoint, componentName, version);
40825
+ try {
40826
+ await this.db.repositories.componentVersion.delete({ componentName, version });
40827
+ await fs_extra_1.default.rm(compVersionPath, { force: true, recursive: true });
40828
+ } catch (e) {
40829
+ this.logger.error(e);
40830
+ }
40831
+ const componentVersionExists = await this.db.repositories.componentVersion.componentVersionExists(componentName, version);
40832
+ const filesExist = await fs_extra_1.default.pathExists(compVersionPath);
40833
+ if (componentVersionExists) {
40834
+ throw new Error(`failed to remove ${componentName} from DB`);
40835
+ }
40836
+ if (filesExist) {
40837
+ throw new Error(`failed to remove ${componentName} files from the disk`);
40838
+ }
40839
+ }
40840
+ assertValidComponentName(componentName) {
40841
+ const rootDir = path_1.default.resolve(this.dataMountPoint);
40842
+ const compPath = path_1.default.join(this.dataMountPoint, componentName);
40843
+ const checkPath = path_1.default.resolve(this.dataMountPoint) + path_1.default.sep + componentName;
40844
+ if (checkPath !== compPath || rootDir === compPath) {
40845
+ throw new error_1.ResourceNotFoundError(`component name ${componentName} is invalid`);
40846
+ }
40847
+ }
40848
+ assertValidComponentVersion(componentName, version) {
40849
+ const versionPath = path_1.default.join(this.dataMountPoint, componentName, version);
40850
+ const checkPath = [path_1.default.resolve(this.dataMountPoint), componentName, version].join(path_1.default.sep);
40851
+ if (checkPath !== versionPath) {
40852
+ throw new error_1.ResourceNotFoundError(`Component version ${version} is invalid`);
40853
+ }
40854
+ }
40855
+ async assertComponentExists(componentName) {
40856
+ const compPath = path_1.default.join(this.dataMountPoint, componentName);
40857
+ if (!await this.db.repositories.component.componentExists(componentName) && !await fs_extra_1.default.pathExists(compPath)) {
40858
+ throw new error_1.ResourceNotFoundError(`Component ${componentName} not found`);
40859
+ }
40860
+ }
40861
+ async assertComponentVersionExists(componentName, version) {
40862
+ await this.assertComponentExists(componentName);
40863
+ const compVersionPath = path_1.default.join(this.dataMountPoint, componentName, version);
40864
+ if (!await this.db.repositories.componentVersion.componentVersionExists(componentName, version) && !await fs_extra_1.default.pathExists(compVersionPath)) {
40865
+ throw new error_1.ResourceNotFoundError(`Component version ${version} for component ${componentName} not found`);
40866
+ }
40867
+ }
40868
+ async moveFilesAndUpdateDb(manifest, location, destPath) {
40869
+ try {
40870
+ await fs_extra_1.default.move(location, destPath, {
40871
+ overwrite: false
40872
+ });
40873
+ await fs_extra_1.default.chmod(destPath, "755");
40874
+ } catch (e) {
40875
+ fs_extra_1.default.rm(location, { force: true, recursive: true });
40876
+ throw e;
40877
+ }
40878
+ try {
40879
+ await this.addComponentVersion(manifest);
40880
+ } catch (e) {
40881
+ fs_extra_1.default.rm(destPath, { force: true, recursive: true });
40882
+ throw e;
40883
+ }
40884
+ }
40885
+ async addComponentVersion(manifest) {
40886
+ if (await this.db.repositories.component.componentExists(manifest.name) === false) {
40887
+ await this.db.repositories.component.addComponent(manifest.name);
40888
+ }
40889
+ await this.db.repositories.componentVersion.addComponentVersion(manifest.name, manifest.version);
40890
+ }
40891
+ async getValidComponentDestinationPath(manifest) {
40892
+ const destPath = path_1.default.join(this.dataMountPoint, manifest.name, manifest.version);
40893
+ const pathExists = await fs_extra_1.default.pathExists(destPath);
40894
+ if (pathExists) {
40895
+ throw new error_1.BadRequestError(`${manifest.name} version ${manifest.version} already exists and cannot be uploaded`);
40896
+ }
40897
+ return destPath;
40898
+ }
40899
+ assertManifestIsValid(manifest) {
40900
+ const manifestValidationResult = (0, validateManifest_1.validateManifest)(manifest);
40901
+ if (manifestValidationResult.isValid == false) {
40902
+ throw new error_1.BadRequestError(`uploaded manifest is invalid, "${manifestValidationResult.message}"`);
40903
+ }
40904
+ }
40905
+ async readManifest(location) {
40906
+ const manifest = await fs_extra_1.default.readJson(path_1.default.join(location, "manifest.json"), { encoding: "utf-8" });
40907
+ return manifest;
40908
+ }
40909
+ };
40910
+ exports.ComponentService = ComponentService;
40911
+ }
40912
+ });
40913
+
40914
+ // ../component-lib/lib/component-service/services/ComponentSetService.js
40915
+ var require_ComponentSetService = __commonJS({
40916
+ "../component-lib/lib/component-service/services/ComponentSetService.js"(exports) {
40917
+ "use strict";
40918
+ Object.defineProperty(exports, "__esModule", { value: true });
40919
+ exports.ComponentSetService = void 0;
40920
+ var error_1 = require_error();
40921
+ var ComponentSetService = class {
40922
+ constructor(db, logger) {
40923
+ this.db = db;
40924
+ this.logger = logger;
40925
+ }
40926
+ async addComponentSet(componentSet) {
40927
+ await this.assertValidCreateComponentSet(componentSet);
40928
+ try {
40929
+ const createdSet = await this.db.executeInTransaction(async (client) => {
40930
+ const set = await this.db.repositories.componentSet.create({
40931
+ webPath: componentSet.webPath,
40932
+ displayName: componentSet.displayName,
40933
+ description: componentSet.description,
40934
+ envVars: componentSet.envVars,
40935
+ headers: componentSet.headers
40936
+ }, client);
40937
+ await this.saveComponentSetComponets(componentSet.webPath, componentSet.components, client);
40938
+ return set;
40939
+ });
40940
+ const savedSet = await this.getComponentSet(createdSet.webPath);
40941
+ return savedSet;
40942
+ } catch (e) {
40943
+ this.logger.error(`Error occurred when updating component set ${componentSet.webPath}: ${e}`);
40944
+ throw e;
40945
+ }
40946
+ }
40947
+ async updateComponentSet(webPath, data) {
40948
+ const existing = await this.db.repositories.componentSet.getComponentSetByWebPath(webPath);
40949
+ if (!existing) {
40950
+ throw new error_1.ResourceNotFoundError(`Component set with web path ${webPath} does not exist`);
40951
+ }
40952
+ await this.assertValidComponentSet(data, this.normalise(existing, await this.getExistingComponents(webPath)));
40953
+ const values = {};
40954
+ if (data.displayName !== void 0) {
40955
+ values.displayName = data.displayName;
40956
+ }
40957
+ if (data.description !== void 0) {
40958
+ values.description = data.description;
40959
+ }
40960
+ if (data.headers !== void 0) {
40961
+ values.headers = data.headers;
40962
+ }
40963
+ if (data.envVars !== void 0) {
40964
+ values.envVars = data.envVars;
40965
+ }
40966
+ try {
40967
+ await this.db.executeInTransaction(async (client) => {
40968
+ if (Object.keys(values).length > 0) {
40969
+ await this.db.repositories.componentSet.update({ webPath }, values, client);
40970
+ }
40971
+ if (data.components !== void 0) {
40972
+ await this.saveComponentSetComponets(webPath, data.components, client);
40973
+ }
40974
+ });
40975
+ return this.getComponentSet(webPath);
40976
+ } catch (e) {
40977
+ this.logger.error(`Error occurred when updating component set ${webPath}: ${e}`);
40978
+ throw e;
40979
+ }
40980
+ }
40981
+ async getComponentSet(webPath) {
40982
+ const set = await this.db.repositories.componentSet.getComponentSetByWebPath(webPath);
40983
+ if (set === void 0) {
40984
+ throw new error_1.ResourceNotFoundError(`Component set with web path ${webPath} does not exist`);
40985
+ }
40986
+ return this.normalise(set, await this.getExistingComponents(webPath));
40987
+ }
40988
+ async deleteComponentSet(webPath) {
40989
+ if (!await this.db.repositories.componentSet.componentSetExists(webPath)) {
40990
+ throw new error_1.ResourceNotFoundError(`Component set with web path ${webPath} does not exist`);
40991
+ }
40992
+ return await this.db.repositories.componentSet.delete({ webPath });
40993
+ }
40994
+ normalise(set, components) {
40995
+ var _a, _b, _c, _d, _e;
40996
+ return {
40997
+ webPath: (_a = set.webPath) !== null && _a !== void 0 ? _a : "",
40998
+ displayName: (_b = set.displayName) !== null && _b !== void 0 ? _b : "",
40999
+ description: (_c = set.description) !== null && _c !== void 0 ? _c : "",
41000
+ envVars: (_d = set.envVars) !== null && _d !== void 0 ? _d : {},
41001
+ headers: (_e = set.headers) !== null && _e !== void 0 ? _e : {},
41002
+ components: components !== null && components !== void 0 ? components : {}
41003
+ };
41004
+ }
41005
+ async saveComponentSetComponets(webPath, components, client = null) {
41006
+ const existingComponents = [];
41007
+ Object.entries(await this.getExistingComponents(webPath)).forEach(([cn, cvs]) => {
41008
+ cvs.forEach((cv) => {
41009
+ existingComponents.push({ componentName: cn, info: cv });
41010
+ });
41011
+ });
41012
+ const inputComponents = [];
41013
+ Object.entries(components).forEach(([cn, cvs]) => {
41014
+ cvs.forEach((cv) => {
41015
+ inputComponents.push({ componentName: cn, info: cv });
41016
+ });
41017
+ });
41018
+ const toDelete = existingComponents.filter((ec) => !inputComponents.find((c) => ec.componentName === c.componentName && ec.info.version === c.info.version));
41019
+ const toCreate = inputComponents.filter((c) => !existingComponents.find((ec) => ec.componentName === c.componentName && ec.info.version === c.info.version));
41020
+ const toUpdate = inputComponents.filter((c) => existingComponents.find((ec) => ec.componentName === c.componentName && ec.info.version === c.info.version && JSON.stringify(ec.info.envVars) !== JSON.stringify(c.info.envVars)));
41021
+ const repo = this.db.repositories.componentSetComponentVersion;
41022
+ const toDeletePromises = toDelete.map((c) => repo.delete({
41023
+ componentSetWebPath: webPath,
41024
+ componentName: c.componentName,
41025
+ componentVersion: c.info.version
41026
+ }, client));
41027
+ const toCreatePromises = toCreate.map((c) => {
41028
+ var _a;
41029
+ return repo.create({
41030
+ componentSetWebPath: webPath,
41031
+ componentName: c.componentName,
41032
+ componentVersion: c.info.version,
41033
+ envVars: (_a = c.info.envVars) !== null && _a !== void 0 ? _a : {}
41034
+ }, client);
41035
+ });
41036
+ const toUpdatePromises = toUpdate.map((c) => {
41037
+ var _a;
41038
+ return repo.update({
41039
+ componentSetWebPath: webPath,
41040
+ componentName: c.componentName,
41041
+ componentVersion: c.info.version
41042
+ }, {
41043
+ envVars: (_a = c.info.envVars) !== null && _a !== void 0 ? _a : {}
41044
+ }, client);
41045
+ });
41046
+ await Promise.all([...toDeletePromises, ...toCreatePromises, ...toUpdatePromises]);
41047
+ }
41048
+ async getExistingComponents(webPath) {
41049
+ const dbEntries = await this.db.repositories.componentSetComponentVersion.find({ componentSetWebPath: webPath });
41050
+ const components = {};
41051
+ dbEntries.forEach((row) => {
41052
+ var _a;
41053
+ if (components[row.componentName] === void 0) {
41054
+ components[row.componentName] = [];
41055
+ }
41056
+ components[row.componentName].push({
41057
+ version: row.componentVersion,
41058
+ envVars: (_a = row.envVars) !== null && _a !== void 0 ? _a : {}
41059
+ });
41060
+ });
41061
+ Object.entries(components).forEach(([key, versions]) => {
41062
+ components[key] = versions.sort((a, b) => {
41063
+ if (a.version < b.version)
41064
+ return -1;
41065
+ if (a.version > b.version)
41066
+ return 1;
41067
+ return 0;
41068
+ });
41069
+ });
41070
+ return components;
41071
+ }
41072
+ async assertValidCreateComponentSet(componentSet) {
41073
+ if (!componentSet.webPath) {
41074
+ throw new error_1.BadRequestError("Component set 'webPath' must be specificed and non-empty");
41075
+ }
41076
+ if (!componentSet.displayName) {
41077
+ throw new error_1.BadRequestError("Component set 'displayName' must be specificed and non-empty");
41078
+ }
41079
+ this.assertValidWebPath(componentSet.webPath);
41080
+ if (await this.db.repositories.componentSet.componentSetExists(componentSet.webPath)) {
41081
+ throw new error_1.BadRequestError(`Component set with web path ${componentSet.webPath} already exists`);
41082
+ }
41083
+ await this.assertValidComponentSet(componentSet);
41084
+ }
41085
+ async assertValidComponentSet(componentSet, currentComponentSet = void 0) {
41086
+ var _a;
41087
+ if (componentSet.envVars !== void 0) {
41088
+ this.assertValidEnvVars(componentSet.envVars);
41089
+ }
41090
+ if (componentSet.headers !== void 0) {
41091
+ this.assertValidHeaders(componentSet.headers);
41092
+ }
41093
+ if (componentSet.components !== void 0) {
41094
+ const csEnvVars = (_a = componentSet.envVars) !== null && _a !== void 0 ? _a : currentComponentSet ? currentComponentSet.envVars : {};
41095
+ await this.assertValidComponents(componentSet.components, csEnvVars);
41096
+ }
41097
+ }
41098
+ assertValidWebPath(webPath) {
41099
+ if (webPath.replace(/[^a-z0-9\-_.]/i, "") !== webPath) {
41100
+ throw new error_1.BadRequestError("Invalid Component Set web path. Only [A-Za-z0-9_-.] chars are allowed");
41101
+ }
41102
+ }
41103
+ assertValidEnvVars(envVars) {
41104
+ Object.entries(envVars).forEach(([envVarName, envVarValue]) => {
41105
+ if (envVarName.replace(/[^a-z0-9\-_.]/i, "") !== envVarName) {
41106
+ throw new error_1.BadRequestError(`Invalid env var '${envVarName}'. Only [A-Za-z0-9_-.] chars are allowed`);
41107
+ }
41108
+ if (typeof envVarValue !== "string") {
41109
+ throw new error_1.BadRequestError(`Env var '${envVarName}' value must be a string`);
41110
+ }
41111
+ });
41112
+ }
41113
+ assertValidHeaders(_headers) {
41114
+ }
41115
+ async assertValidComponents(components, componentSetEnvVars) {
41116
+ const repo = this.db.repositories.componentVersion;
41117
+ for (const [componentName, versions] of Object.entries(components)) {
41118
+ if (!componentName) {
41119
+ throw new error_1.BadRequestError(`Component name must be non-empty`);
41120
+ }
41121
+ const setVersions = [];
41122
+ for (const v of versions) {
41123
+ if (!v.version) {
41124
+ throw new error_1.BadRequestError(`Component version must set and be non-empty`);
41125
+ }
41126
+ if (setVersions.includes(v.version)) {
41127
+ throw new error_1.BadRequestError(`Multiple componet version entries for component ${componentName} with version ${v.version}`);
41128
+ }
41129
+ setVersions.push(v.version);
41130
+ if (v.envVars !== void 0) {
41131
+ this.assertValidEnvVars(v.envVars);
41132
+ const unknownEnvVars = Object.keys(v.envVars).filter((ev) => !Object.keys(componentSetEnvVars).includes(ev));
41133
+ if (unknownEnvVars.length) {
41134
+ throw new error_1.BadRequestError(`Env var(s) '${unknownEnvVars.join("', '")}' defined on component ${componentName} with version ${v.version} is/are not defined on the component set`);
41135
+ }
41136
+ }
41137
+ if (!await repo.componentVersionExists(componentName, v.version)) {
41138
+ throw new error_1.BadRequestError(`Component ${componentName} with version ${v.version} does not exists`);
41139
+ }
41140
+ }
41141
+ }
41142
+ }
41143
+ async getAllComponentSets() {
41144
+ const rows = await this.db.repositories.componentSet.findAll();
41145
+ const componentSets = [];
41146
+ for (const row of rows) {
41147
+ const components = await this.getExistingComponents(row.webPath);
41148
+ componentSets.push(this.normalise(row, components));
41149
+ }
41150
+ return componentSets;
41151
+ }
41152
+ };
41153
+ exports.ComponentSetService = ComponentSetService;
41154
+ }
41155
+ });
41156
+
41157
+ // ../component-lib/lib/component-service/services/index.js
41158
+ var require_services = __commonJS({
41159
+ "../component-lib/lib/component-service/services/index.js"(exports) {
41160
+ "use strict";
41161
+ var __createBinding = exports && exports.__createBinding || (Object.create ? function(o, m, k, k2) {
41162
+ if (k2 === void 0)
41163
+ k2 = k;
41164
+ var desc = Object.getOwnPropertyDescriptor(m, k);
41165
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
41166
+ desc = { enumerable: true, get: function() {
41167
+ return m[k];
41168
+ } };
41169
+ }
41170
+ Object.defineProperty(o, k2, desc);
41171
+ } : function(o, m, k, k2) {
41172
+ if (k2 === void 0)
41173
+ k2 = k;
41174
+ o[k2] = m[k];
41175
+ });
41176
+ var __exportStar = exports && exports.__exportStar || function(m, exports2) {
41177
+ for (var p in m)
41178
+ if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p))
41179
+ __createBinding(exports2, m, p);
41180
+ };
41181
+ Object.defineProperty(exports, "__esModule", { value: true });
41182
+ __exportStar(require_ComponentService(), exports);
41183
+ __exportStar(require_ComponentSetService(), exports);
41184
+ }
41185
+ });
41186
+
40701
41187
  // ../component-lib/lib/index.js
40702
41188
  var require_lib6 = __commonJS({
40703
41189
  "../component-lib/lib/index.js"(exports) {
@@ -40723,7 +41209,7 @@ var require_lib6 = __commonJS({
40723
41209
  __createBinding(exports2, m, p);
40724
41210
  };
40725
41211
  Object.defineProperty(exports, "__esModule", { value: true });
40726
- exports.transformResponseForLogging = exports.transformRequestForLogging = exports.errorMiddleware = exports.schemaVersions = void 0;
41212
+ exports.test = exports.ComponentSetService = exports.ComponentService = exports.transformResponseForLogging = exports.transformRequestForLogging = exports.errorMiddleware = exports.schemaVersions = void 0;
40727
41213
  __exportStar(require_validateManifest(), exports);
40728
41214
  var schemas_1 = require_schemas();
40729
41215
  Object.defineProperty(exports, "schemaVersions", { enumerable: true, get: function() {
@@ -40756,6 +41242,17 @@ var require_lib6 = __commonJS({
40756
41242
  __exportStar(require_parseEnvVarForVar(), exports);
40757
41243
  __exportStar(require_getNodeEnv(), exports);
40758
41244
  __exportStar(require_isPathTryingToAccessOutsideOfRoot(), exports);
41245
+ var services_1 = require_services();
41246
+ Object.defineProperty(exports, "ComponentService", { enumerable: true, get: function() {
41247
+ return services_1.ComponentService;
41248
+ } });
41249
+ Object.defineProperty(exports, "ComponentSetService", { enumerable: true, get: function() {
41250
+ return services_1.ComponentSetService;
41251
+ } });
41252
+ function test() {
41253
+ console.log("a");
41254
+ }
41255
+ exports.test = test;
40759
41256
  }
40760
41257
  });
40761
41258
 
@@ -40792,7 +41289,7 @@ var vm = new import_vm2.NodeVM({
40792
41289
  var script = new import_vm2.VMScript(` const events = require('events');
40793
41290
  const asyncHooks = require('async_hooks');
40794
41291
 
40795
- module.exports = function(id, entry, input, info){
41292
+ module.exports = function(id, entry, input, info, environment = {}){
40796
41293
  const emitter = new events.EventEmitter();
40797
41294
 
40798
41295
  setImmediate(()=>{
@@ -40820,6 +41317,9 @@ var script = new import_vm2.VMScript(` const events = require('events');
40820
41317
  return (activeResources);
40821
41318
  }
40822
41319
 
41320
+
41321
+ // Should we override this or merge then unmerge?
41322
+ process.env = environment;
40823
41323
  let script = require(entry);
40824
41324
 
40825
41325
  if(typeof script === 'object') {
@@ -40879,7 +41379,7 @@ if (import_worker_threads.parentPort) {
40879
41379
  });
40880
41380
  import_worker_threads.parentPort.on("message", async (task) => {
40881
41381
  var _a;
40882
- const data = await executeComponent(task.path, task.id, task.input, task.info);
41382
+ const data = await executeComponent(task);
40883
41383
  (_a = import_worker_threads.parentPort) == null ? void 0 : _a.postMessage(data);
40884
41384
  });
40885
41385
  } else {
@@ -40889,14 +41389,14 @@ var executeComponentFunc = vm.run(script, {
40889
41389
  strict: false,
40890
41390
  wrapper: "commonjs"
40891
41391
  });
40892
- async function executeComponent(entry, id, input, info) {
41392
+ async function executeComponent({ path: entry, id, input, info, environment }) {
40893
41393
  return new Promise((resolve, reject) => {
40894
41394
  const componentContext = {
40895
41395
  manifest: info.manifest,
40896
41396
  set: info.set,
40897
41397
  ctx: (0, import_component_lib.createComponentContext)(info.manifest.name, info.manifest.version, info.componentRoot, info.set)
40898
41398
  };
40899
- const componentExecutionWatcher = executeComponentFunc(id, entry, input, componentContext);
41399
+ const componentExecutionWatcher = executeComponentFunc(id, entry, input, componentContext, environment || {});
40900
41400
  componentExecutionWatcher.on("console", (args) => {
40901
41401
  var _a;
40902
41402
  const message = JSON.parse(JSON.stringify(args.message));