@static-suite/static-suite-data-server 0.0.14 → 0.0.15

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.
package/README.md CHANGED
@@ -21,6 +21,10 @@ Data Server supports static JSON includes (`entityInclude`, `configInclude`, `lo
21
21
  - `runMode` / `--run-mode`: "dev" or "prod". Default is "prod". Optional.
22
22
  - `--port`: port number for the HTTP endpoint. Default is `57471`. Optional.
23
23
 
24
+ ## Options from file.
25
+
26
+ Data Server looks for `dataserver.config.json` in the current working directory. Filename and location can be changed with `DATASERVER_CONFIG_FILE` environment variable.
27
+
24
28
  ## Usage
25
29
 
26
30
  There are three main use cases:
@@ -0,0 +1,11 @@
1
+ {
2
+ "port": 57471,
3
+ "dataDir": "./src/__tests__/fixtures/data",
4
+ "workDir": "./src/__tests__/fixtures/work",
5
+ "queryDir": "./src/__tests__/fixtures/query",
6
+ "hookDir": "./src/__tests__/fixtures/hook",
7
+ "taskDir": "./src/__tests__/fixtures/task",
8
+ "runMode": "dev",
9
+ "logLevel": "debug"
10
+ }
11
+
package/dist/bin/serve.js CHANGED
@@ -5,12 +5,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  return (mod && mod.__esModule) ? mod : { "default": mod };
6
6
  };
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
+ const fs_1 = __importDefault(require("fs"));
8
9
  const yargs_1 = __importDefault(require("yargs/yargs"));
9
10
  const helpers_1 = require("yargs/helpers");
10
11
  const httpServer_1 = require("../http/httpServer");
11
12
  const dataServer_1 = require("../lib/dataServer");
12
13
  const dataServer_types_1 = require("../lib/dataServer.types");
13
14
  const logger_types_1 = require("../lib/utils/logger/logger.types");
15
+ const configFile = process.env.DATASERVER_CONFIG_FILE ?? 'dataserver.config.json';
16
+ const config = fs_1.default.existsSync(configFile)
17
+ ? JSON.parse(fs_1.default.readFileSync(configFile).toString())
18
+ : {};
14
19
  const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
15
20
  .usage('Usage: $0 http --data-dir [path]')
16
21
  .command(['http [--port]'], 'Start an HTTP server')
@@ -78,6 +83,7 @@ const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
78
83
  done(['query-dir', 'data-dir']);
79
84
  }, 500);
80
85
  })
86
+ .config(config)
81
87
  .parseSync();
82
88
  const logLevel = logger_types_1.LogLevel[argv['log-level'].toUpperCase()];
83
89
  const logFileLevel = argv['log-file-level']
@@ -21,6 +21,10 @@ Data Server supports static JSON includes (`entityInclude`, `configInclude`, `lo
21
21
  - `runMode` / `--run-mode`: "dev" or "prod". Default is "prod". Optional.
22
22
  - `--port`: port number for the HTTP endpoint. Default is `57471`. Optional.
23
23
 
24
+ ## Options from file.
25
+
26
+ Data Server looks for `dataserver.config.json` in the current working directory. Filename and location can be changed with `DATASERVER_CONFIG_FILE` environment variable.
27
+
24
28
  ## Usage
25
29
 
26
30
  There are three main use cases:
@@ -1 +1 @@
1
- {"version":3,"file":"storeManager.d.ts","sourceRoot":"","sources":["../../../src/lib/store/storeManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAuH7C,eAAO,MAAM,YAAY,EAAE,YAmG1B,CAAC"}
1
+ {"version":3,"file":"storeManager.d.ts","sourceRoot":"","sources":["../../../src/lib/store/storeManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AA+H7C,eAAO,MAAM,YAAY,EAAE,YAmG1B,CAAC"}
@@ -12,9 +12,10 @@ const dependencyTagger_1 = require("./dependency/dependencyTagger");
12
12
  * Sets a file into the store, regardless of being already added or not.
13
13
  *
14
14
  * @param relativeFilepath - Relative file path, inside dataDir, to the file to be added.
15
- * @param options - Configuration options.
16
15
  *
17
- * @returns Object with two properties, "raw" and "json".
16
+ * @returns Object with two properties:
17
+ * fileContent: the contents of the stored file
18
+ * previousStoredData: the contents of the previously stored file
18
19
  */
19
20
  const setFileIntoStore = (relativeFilepath) => {
20
21
  const absoluteFilePath = `${config_1.config.dataDir}/${relativeFilepath}`;
@@ -26,45 +27,51 @@ const setFileIntoStore = (relativeFilepath) => {
26
27
  });
27
28
  let previousStoredData = null;
28
29
  const dataToStore = fileContent.json || fileContent.raw;
29
- const previousData = _1.store.data.get(relativeFilepath);
30
+ const currentData = _1.store.data.get(relativeFilepath);
30
31
  let skipUpdating = false;
31
32
  if (fileContent.json) {
32
33
  // Check if the object already exists to make sure we don't break the reference
33
34
  if (dataToStore && typeof dataToStore === 'object') {
34
- if (previousData) {
35
+ if (currentData) {
35
36
  // Delete include dependencies.
36
- dependencyFileHelper_1.dependencyIncludeHelper.deleteIncludeDependencies(relativeFilepath, dataToStore);
37
+ dependencyFileHelper_1.dependencyIncludeHelper.deleteIncludeDependencies(relativeFilepath, currentData);
37
38
  // Remove previous data from URL index.
38
- const url = previousData.data?.content?.url?.path;
39
+ const url = currentData.data?.content?.url?.path;
39
40
  if (url) {
40
41
  _1.store.index.url.delete(url);
41
42
  }
42
43
  // Remove previous data from UUID index.
43
- const uuid = previousData.data?.content?.uuid;
44
- const langcode = previousData.data?.content?.langcode?.value;
44
+ const uuid = currentData.data?.content?.uuid;
45
+ const langcode = currentData.data?.content?.langcode?.value;
45
46
  if (uuid && langcode) {
46
47
  _1.store.index.uuid.get(langcode)?.delete(uuid);
47
48
  }
48
49
  // Delete all referenced object properties and copy them to another object.
49
- previousStoredData = previousData.data?.content
50
+ previousStoredData = currentData.data?.content
50
51
  ? { data: { content: {} } }
51
52
  : {};
52
- if (previousData.metadata) {
53
+ if (currentData.metadata) {
53
54
  // We cannot use structuredClone to clone the whole JSON,
54
55
  // since proxies used by queries are not supported by structuredClone.
55
- previousStoredData.metadata = structuredClone(previousData.metadata);
56
+ previousStoredData.metadata = structuredClone(currentData.metadata);
56
57
  }
57
58
  const previousStoredDataPointer = previousStoredData.data?.content || previousStoredData;
58
- const previousDataPointer = previousData.data?.content || previousData;
59
- Object.keys(previousDataPointer).forEach(key => {
60
- previousStoredDataPointer[key] = previousDataPointer[key];
61
- delete previousDataPointer[key];
59
+ const currentDataPointer = currentData.data?.content || currentData;
60
+ Object.keys(currentDataPointer).forEach(key => {
61
+ previousStoredDataPointer[key] = currentDataPointer[key];
62
+ delete currentDataPointer[key];
62
63
  });
63
64
  // Hydrate new object properties into referenced object
64
65
  const dataToStorePointer = dataToStore.data?.content || dataToStore;
65
66
  Object.keys(dataToStorePointer).forEach(key => {
66
- previousDataPointer[key] = dataToStorePointer[key];
67
+ currentDataPointer[key] = dataToStorePointer[key];
67
68
  });
69
+ if (dataToStore.metadata) {
70
+ currentData.metadata = dataToStore.metadata;
71
+ }
72
+ else {
73
+ delete currentData.metadata;
74
+ }
68
75
  skipUpdating = true;
69
76
  }
70
77
  // Add data to UUID index.
@@ -85,7 +92,7 @@ const setFileIntoStore = (relativeFilepath) => {
85
92
  }
86
93
  }
87
94
  else {
88
- previousStoredData = previousData;
95
+ previousStoredData = currentData;
89
96
  }
90
97
  if (!skipUpdating) {
91
98
  _1.store.data.set(relativeFilepath, dataToStore);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-suite/static-suite-data-server",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "A flexible, fast and developer friendly Data Server for Static Suite",
5
5
  "main": "./dist/lib/index.js",
6
6
  "typings": "./dist/lib/index.d.ts",
@@ -42,7 +42,7 @@
42
42
  "dev": "tsc-watch --onSuccess \"yarn lint\"",
43
43
  "build": "rm -Rf dist && yarn copy-http-files-to-src && yarn lint && tsc && yarn api-extractor run --local --verbose && yarn post-build && yarn copy-http-files-to-dist",
44
44
  "post-build": "yarn rimraf dist/**/*__tests__",
45
- "serve": "node ./dist/bin/serve.js http --port=57471 --data-dir=./src/__tests__/fixtures/data --work-dir=./src/__tests__/fixtures/work --query-dir=./src/__tests__/fixtures/query --hook-dir=./src/__tests__/fixtures/hook --task-dir=./src/__tests__/fixtures/task --run-mode=dev --log-level=debug",
45
+ "serve": "export DATASERVER_CONFIG_FILE=dataserver.test.config.json && node ./dist/bin/serve.js http",
46
46
  "lint": "eslint . --ext .ts",
47
47
  "docs": "yarn typedoc --out docs src/lib/",
48
48
  "prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
package/src/bin/serve.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /* eslint-disable import/first */
3
3
 
4
+ import fs from 'fs';
4
5
  import yargs from 'yargs/yargs';
5
6
  import { hideBin } from 'yargs/helpers';
6
7
 
@@ -9,6 +10,12 @@ import { dataServer } from '../lib/dataServer';
9
10
  import { RunMode, RunModeStrings } from '../lib/dataServer.types';
10
11
  import { LogLevel, LogLevelStrings } from '../lib/utils/logger/logger.types';
11
12
 
13
+ const configFile =
14
+ process.env.DATASERVER_CONFIG_FILE ?? 'dataserver.config.json';
15
+ const config = fs.existsSync(configFile)
16
+ ? JSON.parse(fs.readFileSync(configFile).toString())
17
+ : {};
18
+
12
19
  const argv = yargs(hideBin(process.argv))
13
20
  .usage('Usage: $0 http --data-dir [path]')
14
21
  .command(['http [--port]'], 'Start an HTTP server')
@@ -77,6 +84,7 @@ const argv = yargs(hideBin(process.argv))
77
84
  done(['query-dir', 'data-dir']);
78
85
  }, 500);
79
86
  })
87
+ .config(config)
80
88
  .parseSync();
81
89
 
82
90
  const logLevel = LogLevel[argv['log-level'].toUpperCase() as LogLevelStrings];
@@ -12,9 +12,10 @@ import { dependencyTagger } from './dependency/dependencyTagger';
12
12
  * Sets a file into the store, regardless of being already added or not.
13
13
  *
14
14
  * @param relativeFilepath - Relative file path, inside dataDir, to the file to be added.
15
- * @param options - Configuration options.
16
15
  *
17
- * @returns Object with two properties, "raw" and "json".
16
+ * @returns Object with two properties:
17
+ * fileContent: the contents of the stored file
18
+ * previousStoredData: the contents of the previously stored file
18
19
  */
19
20
  const setFileIntoStore = (
20
21
  relativeFilepath: string,
@@ -34,53 +35,60 @@ const setFileIntoStore = (
34
35
  let previousStoredData: any = null;
35
36
 
36
37
  const dataToStore = fileContent.json || fileContent.raw;
37
- const previousData = store.data.get(relativeFilepath);
38
+ const currentData = store.data.get(relativeFilepath);
38
39
  let skipUpdating = false;
39
40
  if (fileContent.json) {
40
41
  // Check if the object already exists to make sure we don't break the reference
41
42
  if (dataToStore && typeof dataToStore === 'object') {
42
- if (previousData) {
43
+ if (currentData) {
43
44
  // Delete include dependencies.
44
45
  dependencyIncludeHelper.deleteIncludeDependencies(
45
46
  relativeFilepath,
46
- dataToStore,
47
+ currentData,
47
48
  );
48
49
 
49
50
  // Remove previous data from URL index.
50
- const url = previousData.data?.content?.url?.path;
51
+ const url = currentData.data?.content?.url?.path;
51
52
  if (url) {
52
53
  store.index.url.delete(url);
53
54
  }
54
55
 
55
56
  // Remove previous data from UUID index.
56
- const uuid = previousData.data?.content?.uuid;
57
- const langcode = previousData.data?.content?.langcode?.value;
57
+ const uuid = currentData.data?.content?.uuid;
58
+ const langcode = currentData.data?.content?.langcode?.value;
58
59
  if (uuid && langcode) {
59
60
  store.index.uuid.get(langcode)?.delete(uuid);
60
61
  }
61
62
 
62
63
  // Delete all referenced object properties and copy them to another object.
63
- previousStoredData = previousData.data?.content
64
+ previousStoredData = currentData.data?.content
64
65
  ? { data: { content: {} } }
65
66
  : {};
66
- if (previousData.metadata) {
67
+ if (currentData.metadata) {
67
68
  // We cannot use structuredClone to clone the whole JSON,
68
69
  // since proxies used by queries are not supported by structuredClone.
69
- previousStoredData.metadata = structuredClone(previousData.metadata);
70
+ previousStoredData.metadata = structuredClone(currentData.metadata);
70
71
  }
71
72
  const previousStoredDataPointer =
72
73
  previousStoredData.data?.content || previousStoredData;
73
- const previousDataPointer = previousData.data?.content || previousData;
74
- Object.keys(previousDataPointer).forEach(key => {
75
- previousStoredDataPointer[key] = previousDataPointer[key];
76
- delete previousDataPointer[key];
74
+ const currentDataPointer = currentData.data?.content || currentData;
75
+ Object.keys(currentDataPointer).forEach(key => {
76
+ previousStoredDataPointer[key] = currentDataPointer[key];
77
+ delete currentDataPointer[key];
77
78
  });
78
79
 
79
80
  // Hydrate new object properties into referenced object
80
81
  const dataToStorePointer = dataToStore.data?.content || dataToStore;
81
82
  Object.keys(dataToStorePointer).forEach(key => {
82
- previousDataPointer[key] = dataToStorePointer[key];
83
+ currentDataPointer[key] = dataToStorePointer[key];
83
84
  });
85
+
86
+ if (dataToStore.metadata) {
87
+ currentData.metadata = dataToStore.metadata;
88
+ } else {
89
+ delete currentData.metadata;
90
+ }
91
+
84
92
  skipUpdating = true;
85
93
  }
86
94
 
@@ -107,7 +115,7 @@ const setFileIntoStore = (
107
115
  );
108
116
  }
109
117
  } else {
110
- previousStoredData = previousData;
118
+ previousStoredData = currentData;
111
119
  }
112
120
 
113
121
  if (!skipUpdating) {