@twin.org/node-core 0.0.2-next.22 → 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.22" // 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)) {
@@ -2127,6 +2129,12 @@ async function buildConfiguration(processEnv, options, serverInfo) {
2127
2129
  */
2128
2130
  function overrideModuleImport(executionDirectory) {
2129
2131
  modules.ModuleHelper.overrideImport(async (moduleName) => {
2132
+ if (moduleCache[moduleName]) {
2133
+ return {
2134
+ module: moduleCache[moduleName],
2135
+ useDefault: false
2136
+ };
2137
+ }
2130
2138
  // If the module path for example when dynamically loading
2131
2139
  // modules looks like a local file then we try to resolve
2132
2140
  // using the local file system
@@ -2143,13 +2151,38 @@ function overrideModuleImport(executionDirectory) {
2143
2151
  if (exists) {
2144
2152
  // If the module exists then we can load it, otherwise
2145
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;
2146
2156
  return {
2147
- module: await import(process.platform === "win32" ? `file://${localFilename}` : localFilename),
2157
+ module,
2148
2158
  useDefault: false
2149
2159
  };
2150
2160
  }
2151
2161
  }
2152
- // 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
2153
2186
  return {
2154
2187
  useDefault: true
2155
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.22" // 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)) {
@@ -2106,6 +2108,12 @@ async function buildConfiguration(processEnv, options, serverInfo) {
2106
2108
  */
2107
2109
  function overrideModuleImport(executionDirectory) {
2108
2110
  ModuleHelper.overrideImport(async (moduleName) => {
2111
+ if (moduleCache[moduleName]) {
2112
+ return {
2113
+ module: moduleCache[moduleName],
2114
+ useDefault: false
2115
+ };
2116
+ }
2109
2117
  // If the module path for example when dynamically loading
2110
2118
  // modules looks like a local file then we try to resolve
2111
2119
  // using the local file system
@@ -2122,13 +2130,38 @@ function overrideModuleImport(executionDirectory) {
2122
2130
  if (exists) {
2123
2131
  // If the module exists then we can load it, otherwise
2124
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;
2125
2135
  return {
2126
- module: await import(process.platform === "win32" ? `file://${localFilename}` : localFilename),
2136
+ module,
2127
2137
  useDefault: false
2128
2138
  };
2129
2139
  }
2130
2140
  }
2131
- // 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
2132
2165
  return {
2133
2166
  useDefault: true
2134
2167
  };
package/docs/changelog.md CHANGED
@@ -1,5 +1,12 @@
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
+
3
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)
4
11
 
5
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/node-core",
3
- "version": "0.0.2-next.22",
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",