@twin.org/node-core 0.0.2-next.21 → 0.0.2-next.23

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.
@@ -15,6 +15,7 @@ var cliCore = require('@twin.org/cli-core');
15
15
  var rightsManagementRestClient = require('@twin.org/rights-management-rest-client');
16
16
  var engineServer = require('@twin.org/engine-server');
17
17
  var modules = require('@twin.org/modules');
18
+ var node_child_process = require('node:child_process');
18
19
  var dotenv = require('dotenv');
19
20
  var engine = require('@twin.org/engine');
20
21
  var engineCore = require('@twin.org/engine-core');
@@ -1974,6 +1975,7 @@ async function start(nodeOptions, nodeEngineConfig, envVars) {
1974
1975
 
1975
1976
  // Copyright 2024 IOTA Stiftung.
1976
1977
  // SPDX-License-Identifier: Apache-2.0.
1978
+ const moduleCache = {};
1977
1979
  /**
1978
1980
  * Run the TWIN Node server.
1979
1981
  * @param nodeOptions Optional configuration options for running the server.
@@ -1984,7 +1986,7 @@ async function run(nodeOptions) {
1984
1986
  nodeOptions ??= {};
1985
1987
  const serverInfo = {
1986
1988
  name: nodeOptions?.serverName ?? "TWIN Node Server",
1987
- version: nodeOptions?.serverVersion ?? "0.0.2-next.21" // x-release-please-version
1989
+ version: nodeOptions?.serverVersion ?? "0.0.2-next.23" // x-release-please-version
1988
1990
  };
1989
1991
  cliCore.CLIDisplay.header(serverInfo.name, serverInfo.version, "đŸŒŠī¸ ");
1990
1992
  if (!core.Is.stringValue(nodeOptions?.executionDirectory)) {
@@ -2023,6 +2025,7 @@ async function run(nodeOptions) {
2023
2025
  overrideModuleImport(nodeOptions.executionDirectory ?? "");
2024
2026
  const { nodeEngineConfig, nodeEnvVars: envVars } = await buildConfiguration(
2025
2027
  // This is the only location in the code base that should access process.env directly
2028
+ // So we can safely disable the linting rule here.
2026
2029
  // eslint-disable-next-line no-restricted-syntax
2027
2030
  process.env, nodeOptions, serverInfo);
2028
2031
  cliCore.CLIDisplay.break();
@@ -2126,6 +2129,12 @@ async function buildConfiguration(processEnv, options, serverInfo) {
2126
2129
  */
2127
2130
  function overrideModuleImport(executionDirectory) {
2128
2131
  modules.ModuleHelper.overrideImport(async (moduleName) => {
2132
+ if (moduleCache[moduleName]) {
2133
+ return {
2134
+ module: moduleCache[moduleName],
2135
+ useDefault: false
2136
+ };
2137
+ }
2129
2138
  // If the module path for example when dynamically loading
2130
2139
  // modules looks like a local file then we try to resolve
2131
2140
  // using the local file system
@@ -2142,13 +2151,38 @@ function overrideModuleImport(executionDirectory) {
2142
2151
  if (exists) {
2143
2152
  // If the module exists then we can load it, otherwise
2144
2153
  // we fallback to regular handling to see if that can import it
2154
+ const module = await import(process.platform === "win32" ? `file://${localFilename}` : localFilename);
2155
+ moduleCache[moduleName] = module;
2145
2156
  return {
2146
- module: await import(process.platform === "win32" ? `file://${localFilename}` : localFilename),
2157
+ module,
2147
2158
  useDefault: false
2148
2159
  };
2149
2160
  }
2150
2161
  }
2151
- // The filename doesn't look like a local module, so just use default handling
2162
+ try {
2163
+ // Try and load from node_modules manually
2164
+ // This is needed for some environments where
2165
+ // the module resolution doesn't work as expected
2166
+ const npmRoot = node_child_process.execSync("npm root").toString().trim().replace(/\\/g, "/");
2167
+ const packageJson = await loadJsonFile(path.resolve(npmRoot, moduleName, "package.json"));
2168
+ const mainFile = packageJson?.module ?? packageJson?.main ?? "index.js";
2169
+ const modulePath = path.resolve(npmRoot, moduleName, mainFile);
2170
+ const exists = await fileExists(modulePath);
2171
+ if (exists) {
2172
+ const module = await import(process.platform === "win32" ? `file://${modulePath}` : modulePath);
2173
+ moduleCache[moduleName] = module;
2174
+ return {
2175
+ module,
2176
+ useDefault: false
2177
+ };
2178
+ }
2179
+ }
2180
+ catch {
2181
+ // We just fallback to default handling if not possible
2182
+ }
2183
+ // We don't appear to be able to manually resolve this module
2184
+ // So we let the default handling take care of it
2185
+ // This will allow built-in modules and regular node_modules to load as normal
2152
2186
  return {
2153
2187
  useDefault: true
2154
2188
  };
@@ -13,6 +13,7 @@ import { CLIDisplay } from '@twin.org/cli-core';
13
13
  import { PolicyNegotiationPointClient, DataAccessPointClient } from '@twin.org/rights-management-rest-client';
14
14
  import { addDefaultRestPaths, addDefaultSocketPaths, EngineServer } from '@twin.org/engine-server';
15
15
  import { ModuleHelper } from '@twin.org/modules';
16
+ import { execSync } from 'node:child_process';
16
17
  import * as dotenv from 'dotenv';
17
18
  import { Engine } from '@twin.org/engine';
18
19
  import { FileStateStorage } from '@twin.org/engine-core';
@@ -1953,6 +1954,7 @@ async function start(nodeOptions, nodeEngineConfig, envVars) {
1953
1954
 
1954
1955
  // Copyright 2024 IOTA Stiftung.
1955
1956
  // SPDX-License-Identifier: Apache-2.0.
1957
+ const moduleCache = {};
1956
1958
  /**
1957
1959
  * Run the TWIN Node server.
1958
1960
  * @param nodeOptions Optional configuration options for running the server.
@@ -1963,7 +1965,7 @@ async function run(nodeOptions) {
1963
1965
  nodeOptions ??= {};
1964
1966
  const serverInfo = {
1965
1967
  name: nodeOptions?.serverName ?? "TWIN Node Server",
1966
- version: nodeOptions?.serverVersion ?? "0.0.2-next.21" // x-release-please-version
1968
+ version: nodeOptions?.serverVersion ?? "0.0.2-next.23" // x-release-please-version
1967
1969
  };
1968
1970
  CLIDisplay.header(serverInfo.name, serverInfo.version, "đŸŒŠī¸ ");
1969
1971
  if (!Is.stringValue(nodeOptions?.executionDirectory)) {
@@ -2002,6 +2004,7 @@ async function run(nodeOptions) {
2002
2004
  overrideModuleImport(nodeOptions.executionDirectory ?? "");
2003
2005
  const { nodeEngineConfig, nodeEnvVars: envVars } = await buildConfiguration(
2004
2006
  // This is the only location in the code base that should access process.env directly
2007
+ // So we can safely disable the linting rule here.
2005
2008
  // eslint-disable-next-line no-restricted-syntax
2006
2009
  process.env, nodeOptions, serverInfo);
2007
2010
  CLIDisplay.break();
@@ -2105,6 +2108,12 @@ async function buildConfiguration(processEnv, options, serverInfo) {
2105
2108
  */
2106
2109
  function overrideModuleImport(executionDirectory) {
2107
2110
  ModuleHelper.overrideImport(async (moduleName) => {
2111
+ if (moduleCache[moduleName]) {
2112
+ return {
2113
+ module: moduleCache[moduleName],
2114
+ useDefault: false
2115
+ };
2116
+ }
2108
2117
  // If the module path for example when dynamically loading
2109
2118
  // modules looks like a local file then we try to resolve
2110
2119
  // using the local file system
@@ -2121,13 +2130,38 @@ function overrideModuleImport(executionDirectory) {
2121
2130
  if (exists) {
2122
2131
  // If the module exists then we can load it, otherwise
2123
2132
  // we fallback to regular handling to see if that can import it
2133
+ const module = await import(process.platform === "win32" ? `file://${localFilename}` : localFilename);
2134
+ moduleCache[moduleName] = module;
2124
2135
  return {
2125
- module: await import(process.platform === "win32" ? `file://${localFilename}` : localFilename),
2136
+ module,
2126
2137
  useDefault: false
2127
2138
  };
2128
2139
  }
2129
2140
  }
2130
- // The filename doesn't look like a local module, so just use default handling
2141
+ try {
2142
+ // Try and load from node_modules manually
2143
+ // This is needed for some environments where
2144
+ // the module resolution doesn't work as expected
2145
+ const npmRoot = execSync("npm root").toString().trim().replace(/\\/g, "/");
2146
+ const packageJson = await loadJsonFile(path.resolve(npmRoot, moduleName, "package.json"));
2147
+ const mainFile = packageJson?.module ?? packageJson?.main ?? "index.js";
2148
+ const modulePath = path.resolve(npmRoot, moduleName, mainFile);
2149
+ const exists = await fileExists(modulePath);
2150
+ if (exists) {
2151
+ const module = await import(process.platform === "win32" ? `file://${modulePath}` : modulePath);
2152
+ moduleCache[moduleName] = module;
2153
+ return {
2154
+ module,
2155
+ useDefault: false
2156
+ };
2157
+ }
2158
+ }
2159
+ catch {
2160
+ // We just fallback to default handling if not possible
2161
+ }
2162
+ // We don't appear to be able to manually resolve this module
2163
+ // So we let the default handling take care of it
2164
+ // This will allow built-in modules and regular node_modules to load as normal
2131
2165
  return {
2132
2166
  useDefault: true
2133
2167
  };
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/node-core - Changelog
2
2
 
3
+ ## [0.0.2-next.23](https://github.com/twinfoundation/node/compare/node-core-v0.0.2-next.22...node-core-v0.0.2-next.23) (2025-10-07)
4
+
5
+
6
+ ### Features
7
+
8
+ * add module cache and improve resolution ([482be05](https://github.com/twinfoundation/node/commit/482be056c37a598033250ea8a7288a33cf3470b4))
9
+
10
+ ## [0.0.2-next.22](https://github.com/twinfoundation/node/compare/node-core-v0.0.2-next.21...node-core-v0.0.2-next.22) (2025-10-07)
11
+
12
+
13
+ ### Features
14
+
15
+ * update engine dependency ([db290df](https://github.com/twinfoundation/node/commit/db290df3b18374f7d86da575c6048657b98c355b))
16
+
3
17
  ## [0.0.2-next.21](https://github.com/twinfoundation/node/compare/node-core-v0.0.2-next.20...node-core-v0.0.2-next.21) (2025-10-07)
4
18
 
5
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/node-core",
3
- "version": "0.0.2-next.21",
3
+ "version": "0.0.2-next.23",
4
4
  "description": "TWIN Node Core for serving APIs using the specified configuration",
5
5
  "repository": {
6
6
  "type": "git",