@twin.org/node-core 0.0.3-next.2 → 0.0.3-next.21

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.
Files changed (151) hide show
  1. package/dist/es/builders/engineEnvBuilder.js +168 -105
  2. package/dist/es/builders/engineEnvBuilder.js.map +1 -1
  3. package/dist/es/builders/engineServerEnvBuilder.js +49 -25
  4. package/dist/es/builders/engineServerEnvBuilder.js.map +1 -1
  5. package/dist/es/cli.js +247 -0
  6. package/dist/es/cli.js.map +1 -0
  7. package/dist/es/commands/bootstrapLegacy.js +183 -0
  8. package/dist/es/commands/bootstrapLegacy.js.map +1 -0
  9. package/dist/es/commands/help.js +85 -0
  10. package/dist/es/commands/help.js.map +1 -0
  11. package/dist/es/commands/identityCreate.js +316 -0
  12. package/dist/es/commands/identityCreate.js.map +1 -0
  13. package/dist/es/commands/identityImports.js +82 -0
  14. package/dist/es/commands/identityImports.js.map +1 -0
  15. package/dist/es/commands/identityVerifiableCredentialCreate.js +146 -0
  16. package/dist/es/commands/identityVerifiableCredentialCreate.js.map +1 -0
  17. package/dist/es/commands/identityVerificationMethodCreate.js +214 -0
  18. package/dist/es/commands/identityVerificationMethodCreate.js.map +1 -0
  19. package/dist/es/commands/identityVerificationMethodImport.js +126 -0
  20. package/dist/es/commands/identityVerificationMethodImport.js.map +1 -0
  21. package/dist/es/commands/nodeSetIdentity.js +64 -0
  22. package/dist/es/commands/nodeSetIdentity.js.map +1 -0
  23. package/dist/es/commands/nodeSetTenant.js +68 -0
  24. package/dist/es/commands/nodeSetTenant.js.map +1 -0
  25. package/dist/es/commands/tenantCreate.js +139 -0
  26. package/dist/es/commands/tenantCreate.js.map +1 -0
  27. package/dist/es/commands/tenantImport.js +97 -0
  28. package/dist/es/commands/tenantImport.js.map +1 -0
  29. package/dist/es/commands/tenantUpdate.js +94 -0
  30. package/dist/es/commands/tenantUpdate.js.map +1 -0
  31. package/dist/es/commands/userCreate.js +212 -0
  32. package/dist/es/commands/userCreate.js.map +1 -0
  33. package/dist/es/commands/userUpdate.js +132 -0
  34. package/dist/es/commands/userUpdate.js.map +1 -0
  35. package/dist/es/commands/vaultKeyCreate.js +191 -0
  36. package/dist/es/commands/vaultKeyCreate.js.map +1 -0
  37. package/dist/es/commands/vaultKeyImport.js +104 -0
  38. package/dist/es/commands/vaultKeyImport.js.map +1 -0
  39. package/dist/es/defaults.js +19 -1
  40. package/dist/es/defaults.js.map +1 -1
  41. package/dist/es/index.js +7 -3
  42. package/dist/es/index.js.map +1 -1
  43. package/dist/es/models/ICliArgs.js +4 -0
  44. package/dist/es/models/ICliArgs.js.map +1 -0
  45. package/dist/es/models/ICliCommand.js +2 -0
  46. package/dist/es/models/ICliCommand.js.map +1 -0
  47. package/dist/es/models/ICliCommandDefinition.js +2 -0
  48. package/dist/es/models/ICliCommandDefinition.js.map +1 -0
  49. package/dist/es/models/ICliCommandDefinitionParam.js +2 -0
  50. package/dist/es/models/ICliCommandDefinitionParam.js.map +1 -0
  51. package/dist/es/models/IEngineEnvironmentVariables.js.map +1 -1
  52. package/dist/es/models/IEngineServerEnvironmentVariables.js.map +1 -1
  53. package/dist/es/models/INodeEngineState.js.map +1 -1
  54. package/dist/es/models/INodeEnvironmentVariables.js.map +1 -1
  55. package/dist/es/models/INodeOptions.js.map +1 -1
  56. package/dist/es/models/cliCommandParamType.js +4 -0
  57. package/dist/es/models/cliCommandParamType.js.map +1 -0
  58. package/dist/es/node.js +78 -41
  59. package/dist/es/node.js.map +1 -1
  60. package/dist/es/start.js +139 -0
  61. package/dist/es/start.js.map +1 -0
  62. package/dist/es/utils.js +11 -21
  63. package/dist/es/utils.js.map +1 -1
  64. package/dist/types/builders/engineEnvBuilder.d.ts +4 -1
  65. package/dist/types/builders/engineServerEnvBuilder.d.ts +5 -2
  66. package/dist/types/cli.d.ts +56 -0
  67. package/dist/types/commands/bootstrapLegacy.d.ts +66 -0
  68. package/dist/types/commands/help.d.ts +23 -0
  69. package/dist/types/commands/identityCreate.d.ts +39 -0
  70. package/dist/types/commands/identityImports.d.ts +24 -0
  71. package/dist/types/commands/identityVerifiableCredentialCreate.d.ts +43 -0
  72. package/dist/types/commands/identityVerificationMethodCreate.d.ts +47 -0
  73. package/dist/types/commands/identityVerificationMethodImport.d.ts +31 -0
  74. package/dist/types/commands/nodeSetIdentity.d.ts +22 -0
  75. package/dist/types/commands/nodeSetTenant.d.ts +22 -0
  76. package/dist/types/commands/tenantCreate.d.ts +38 -0
  77. package/dist/types/commands/tenantImport.d.ts +26 -0
  78. package/dist/types/commands/tenantUpdate.d.ts +26 -0
  79. package/dist/types/commands/userCreate.d.ts +49 -0
  80. package/dist/types/commands/userUpdate.d.ts +38 -0
  81. package/dist/types/commands/vaultKeyCreate.d.ts +43 -0
  82. package/dist/types/commands/vaultKeyImport.d.ts +28 -0
  83. package/dist/types/defaults.d.ts +11 -1
  84. package/dist/types/index.d.ts +7 -3
  85. package/dist/types/models/ICliArgs.d.ts +20 -0
  86. package/dist/types/models/ICliCommand.d.ts +17 -0
  87. package/dist/types/models/ICliCommandDefinition.d.ts +46 -0
  88. package/dist/types/models/ICliCommandDefinitionParam.d.ts +35 -0
  89. package/dist/types/models/IEngineEnvironmentVariables.d.ts +62 -53
  90. package/dist/types/models/IEngineServerEnvironmentVariables.d.ts +4 -0
  91. package/dist/types/models/INodeEngineState.d.ts +0 -8
  92. package/dist/types/models/INodeEnvironmentVariables.d.ts +0 -38
  93. package/dist/types/models/INodeOptions.d.ts +6 -2
  94. package/dist/types/models/cliCommandParamType.d.ts +4 -0
  95. package/dist/types/node.d.ts +8 -4
  96. package/dist/types/{server.d.ts → start.d.ts} +7 -2
  97. package/dist/types/utils.d.ts +6 -8
  98. package/docs/changelog.md +136 -0
  99. package/docs/reference/functions/buildConfiguration.md +3 -3
  100. package/docs/reference/functions/buildEngineConfiguration.md +1 -1
  101. package/docs/reference/functions/buildEngineServerConfiguration.md +3 -3
  102. package/docs/reference/functions/constructCliCommand.md +27 -0
  103. package/docs/reference/functions/executeCommand.md +29 -0
  104. package/docs/reference/functions/getEnvDefaults.md +19 -0
  105. package/docs/reference/functions/getScriptDirectory.md +19 -0
  106. package/docs/reference/functions/parseCommandLineArgs.md +19 -0
  107. package/docs/reference/functions/processEnvOptions.md +27 -0
  108. package/docs/reference/functions/registerCommands.md +9 -0
  109. package/docs/reference/functions/run.md +8 -2
  110. package/docs/reference/functions/start.md +10 -4
  111. package/docs/reference/functions/substituteEnvOptions.md +25 -0
  112. package/docs/reference/index.md +16 -13
  113. package/docs/reference/interfaces/ICliArgs.md +35 -0
  114. package/docs/reference/interfaces/ICliCommand.md +23 -0
  115. package/docs/reference/interfaces/ICliCommandDefinition.md +101 -0
  116. package/docs/reference/interfaces/ICliCommandDefinitionParam.md +65 -0
  117. package/docs/reference/interfaces/IEngineEnvironmentVariables.md +110 -85
  118. package/docs/reference/interfaces/IEngineServerEnvironmentVariables.md +166 -117
  119. package/docs/reference/interfaces/INodeEngineState.md +0 -16
  120. package/docs/reference/interfaces/INodeEnvironmentVariables.md +170 -201
  121. package/docs/reference/interfaces/INodeOptions.md +10 -2
  122. package/docs/reference/type-aliases/CliCommandParamType.md +5 -0
  123. package/docs/reference/variables/CONTEXT_ID_HANDLER_FEATURE_DID.md +3 -0
  124. package/docs/reference/variables/CONTEXT_ID_HANDLER_FEATURE_TENANT.md +3 -0
  125. package/docs/reference/variables/TRUST_VERIFICATION_METHOD_ID.md +3 -0
  126. package/locales/en.json +589 -30
  127. package/package.json +3 -1
  128. package/dist/es/bootstrap.js +0 -374
  129. package/dist/es/bootstrap.js.map +0 -1
  130. package/dist/es/identity.js +0 -169
  131. package/dist/es/identity.js.map +0 -1
  132. package/dist/es/models/nodeFeatures.js +0 -21
  133. package/dist/es/models/nodeFeatures.js.map +0 -1
  134. package/dist/es/server.js +0 -78
  135. package/dist/es/server.js.map +0 -1
  136. package/dist/types/bootstrap.d.ts +0 -76
  137. package/dist/types/identity.d.ts +0 -14
  138. package/dist/types/models/nodeFeatures.d.ts +0 -21
  139. package/docs/reference/functions/bootstrap.md +0 -29
  140. package/docs/reference/functions/bootstrapAuth.md +0 -35
  141. package/docs/reference/functions/bootstrapBlobEncryption.md +0 -35
  142. package/docs/reference/functions/bootstrapContextIdHandlers.md +0 -35
  143. package/docs/reference/functions/bootstrapImmutableProofMethod.md +0 -35
  144. package/docs/reference/functions/bootstrapNodeAdminUser.md +0 -35
  145. package/docs/reference/functions/bootstrapNodeId.md +0 -35
  146. package/docs/reference/functions/bootstrapSynchronisedStorage.md +0 -35
  147. package/docs/reference/functions/bootstrapTenantId.md +0 -35
  148. package/docs/reference/functions/getFeatures.md +0 -19
  149. package/docs/reference/type-aliases/NodeFeatures.md +0 -5
  150. package/docs/reference/variables/NodeFeatures.md +0 -25
  151. package/docs/reference/variables/VC_AUTHENTICATION_VERIFICATION_METHOD_ID.md +0 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start.js","sourceRoot":"","sources":["../../src/start.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EACN,iBAAiB,EAGjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,OAAO,EAAE,wBAAwB,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAC9F,OAAO,EACN,0BAA0B,EAC1B,gCAAgC,EAChC,kBAAkB,EAClB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAO1C;;;;;;;;GAQG;AACH,MAAM,CAAC,KAA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C,EAAE,CAAC;gBACxC,MAAM,CAAC,YAAY,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;YAC/D,CAAC;iBAAM,CAAC;gBACP,MAAM,IAAI,YAAY,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;YACpD,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAC9B,MAAwD,EACxD,sBAAwF;IAExF,IAAI,EAAE,CAAC,UAAU,CAAC,sBAAsB,CAAC,EAAE,CAAC;QAC3C,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,MAAM,qBAAqB,IAAI,sBAAsB,EAAE,CAAC;YAC5D,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC;gBAChD,MAAM,CAAC,eAAe,CACrB,qBAAqB,CAAC,GAAG,EACzB,qBAAqB,CAAC,uBAAuB,CAC7C,CAAC;gBACF,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAAC;YACvC,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ContextIdKeys } from \"@twin.org/context\";\nimport { Coerce, GeneralError, I18n, Is } from \"@twin.org/core\";\nimport { Engine } from \"@twin.org/engine\";\nimport { FileStateStorage } from \"@twin.org/engine-core\";\nimport {\n\tEngineCoreFactory,\n\ttype IEngineCore,\n\ttype IEngineCoreConfig\n} from \"@twin.org/engine-models\";\nimport { EngineServer } from \"@twin.org/engine-server\";\nimport type { IEngineServerConfig } from \"@twin.org/engine-server-types\";\nimport { BlobStorageConnectorType, EntityStorageConnectorType } from \"@twin.org/engine-types\";\nimport {\n\textensionsInitialiseEngine,\n\textensionsInitialiseEngineServer,\n\tshutdownExtensions\n} from \"./builders/extensionsBuilder.js\";\nimport { executeCommand } from \"./cli.js\";\nimport type { ICliCommand } from \"./models/ICliCommand.js\";\nimport type { INodeEngineConfig } from \"./models/INodeEngineConfig.js\";\nimport type { INodeEngineState } from \"./models/INodeEngineState.js\";\nimport type { INodeEnvironmentVariables } from \"./models/INodeEnvironmentVariables.js\";\nimport type { INodeOptions } from \"./models/INodeOptions.js\";\n\n/**\n * Start the engine server.\n * @param nodeOptions Optional run options for the engine server.\n * @param nodeEngineConfig The configuration for the engine server.\n * @param envVars The environment variables.\n * @param cliCommand The constructed CLI command (optional).\n * @param availableContextIdKeys The context ID keys available for operation.\n * @returns The engine server.\n */\nexport async function start(\n\tnodeOptions: INodeOptions | undefined,\n\tnodeEngineConfig: INodeEngineConfig,\n\tenvVars: INodeEnvironmentVariables,\n\tcliCommand?: ICliCommand,\n\tavailableContextIdKeys?: { key: string; requiredHandlerFeatures: string[] }[]\n): Promise<\n\t| {\n\t\t\tengine: Engine<IEngineServerConfig, INodeEngineState>;\n\t\t\tserver: EngineServer;\n\t\t\tshutdown: () => Promise<void>;\n\t }\n\t| undefined\n> {\n\tconst entityStorageConnectorType = envVars.entityStorageConnectorType?.split(\",\") ?? [];\n\tconst blobStorageConnectorType = envVars.blobStorageConnectorType?.split(\",\") ?? [];\n\n\tconst requiresEngineStarted = cliCommand?.definition?.requiresEngineStarted ?? true;\n\tconst requiresNodeIdentity = cliCommand?.definition?.requiresNodeIdentity ?? true;\n\tconst requiresTenantId = cliCommand?.definition?.requiresTenantId ?? true;\n\n\t// If the blob storage or entity storage is configured with file connectors\n\t// then we need to make sure the storageFileRoot is set\n\tif (\n\t\t(entityStorageConnectorType.includes(EntityStorageConnectorType.File) ||\n\t\t\tblobStorageConnectorType.includes(BlobStorageConnectorType.File) ||\n\t\t\tIs.empty(nodeOptions?.stateStorage)) &&\n\t\t!Is.stringValue(envVars.storageFileRoot) &&\n\t\trequiresEngineStarted\n\t) {\n\t\tthrow new GeneralError(\"node\", \"storageFileRootNotSet\", {\n\t\t\tstorageFileRoot: `${nodeOptions?.envPrefix ?? \"\"}STORAGE_FILE_ROOT`\n\t\t});\n\t}\n\n\t// Create the engine instance using file state storage unless one is configured in options\n\tconst engine = new Engine<IEngineServerConfig, INodeEngineState>({\n\t\tconfig: nodeEngineConfig,\n\t\tstateStorage: requiresEngineStarted\n\t\t\t? (nodeOptions?.stateStorage ?? new FileStateStorage(envVars.stateFilename ?? \"\"))\n\t\t\t: undefined,\n\t\tcustomBootstrap: async (engineCore, context) => {\n\t\t\tconfigureContextIds(\n\t\t\t\tengineCore,\n\t\t\t\tenvVars,\n\t\t\t\trequiresEngineStarted,\n\t\t\t\trequiresNodeIdentity,\n\t\t\t\trequiresTenantId\n\t\t\t);\n\t\t}\n\t});\n\n\tconfigureContextIdKeys(engine, availableContextIdKeys);\n\n\t// Construct the server with the engine.\n\tconst server = new EngineServer({ engineCore: engine });\n\n\t// Extend the engine.\n\tif (Is.function(nodeOptions?.extendEngine)) {\n\t\tawait engine.logInfo(I18n.formatMessage(\"node.extendingEngine\"));\n\t\tawait nodeOptions.extendEngine(engine);\n\t}\n\n\tawait extensionsInitialiseEngine(envVars, engine);\n\n\t// Extend the engine server.\n\tif (Is.function(nodeOptions?.extendEngineServer)) {\n\t\tawait engine.logInfo(I18n.formatMessage(\"node.extendingEngineServer\"));\n\t\tawait nodeOptions?.extendEngineServer(server);\n\t}\n\n\tawait extensionsInitialiseEngineServer(envVars, engine, server);\n\n\t// Need to register the engine with the factory so that background tasks\n\t// can clone it to spawn new instances.\n\tEngineCoreFactory.register(\"engine\", () => engine);\n\n\tif (Is.objectValue(cliCommand)) {\n\t\tawait executeCommand(engine, envVars, cliCommand);\n\t} else {\n\t\ttry {\n\t\t\t// Start the server, which also starts the engine.\n\t\t\tawait server.start();\n\n\t\t\treturn {\n\t\t\t\tengine,\n\t\t\t\tserver,\n\t\t\t\tshutdown: async () => {\n\t\t\t\t\tawait shutdownExtensions(envVars, engine);\n\t\t\t\t\tawait server.stop();\n\t\t\t\t}\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tawait shutdownExtensions(envVars, engine);\n\t\t\tthrow err;\n\t\t}\n\t}\n}\n\n/**\n * Configure the context IDs for the engine.\n * @param engine The engine to configure.\n * @param envVars The environment variables.\n * @param requiresEngineStarted Whether the engine is required to be started.\n * @param requiresNodeIdentity Whether the node identity is required.\n * @param requiresTenantId Whether the tenant id is required.\n * @throws GeneralError Throws if the node identity or tenant is required but not set.\n */\nfunction configureContextIds(\n\tengine: IEngineCore<IEngineCoreConfig, INodeEngineState>,\n\tenvVars: INodeEnvironmentVariables,\n\trequiresEngineStarted: boolean,\n\trequiresNodeIdentity: boolean,\n\trequiresTenantId: boolean\n): void {\n\tconst state = engine.getState();\n\n\tif (requiresEngineStarted && requiresNodeIdentity) {\n\t\tconst nodeIdentityEnabled = Coerce.boolean(envVars.nodeIdentityEnabled) ?? true;\n\t\tif (nodeIdentityEnabled) {\n\t\t\tif (Is.stringValue(state.nodeId)) {\n\t\t\t\tengine.addContextId(ContextIdKeys.Node, state.nodeId);\n\t\t\t} else {\n\t\t\t\tthrow new GeneralError(\"node\", \"nodeIdentityNotSet\");\n\t\t\t}\n\t\t}\n\t}\n\n\tif (requiresEngineStarted && requiresTenantId) {\n\t\tconst tenantEnabled = Coerce.boolean(envVars.tenantEnabled) ?? false;\n\t\tif (tenantEnabled) {\n\t\t\tif (Is.stringValue(state.nodeTenantId)) {\n\t\t\t\tengine.addContextId(ContextIdKeys.Tenant, state.nodeTenantId);\n\t\t\t} else {\n\t\t\t\tthrow new GeneralError(\"node\", \"nodeTenantNotSet\");\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Configure the context IDs for the engine.\n * @param engine The engine to configure.\n * @param availableContextIdKeys The available context ID keys.\n * @throws GeneralError Throws if the node identity or tenant is required but not set.\n */\nfunction configureContextIdKeys(\n\tengine: IEngineCore<IEngineCoreConfig, INodeEngineState>,\n\tavailableContextIdKeys: { key: string; requiredHandlerFeatures: string[] }[] | undefined\n): void {\n\tif (Is.arrayValue(availableContextIdKeys)) {\n\t\tconst added: string[] = [];\n\t\tfor (const availableContextIdKey of availableContextIdKeys) {\n\t\t\tif (!added.includes(availableContextIdKey.key)) {\n\t\t\t\tengine.addContextIdKey(\n\t\t\t\t\tavailableContextIdKey.key,\n\t\t\t\t\tavailableContextIdKey.requiredHandlerFeatures\n\t\t\t\t);\n\t\t\t\tadded.push(availableContextIdKey.key);\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
package/dist/es/utils.js CHANGED
@@ -9,7 +9,6 @@ import { BaseError, Converter, GeneralError, I18n, Is } from "@twin.org/core";
9
9
  import { Sha256 } from "@twin.org/crypto";
10
10
  import { ModuleHelper } from "@twin.org/modules";
11
11
  import { ModuleProtocol } from "./models/moduleProtocol.js";
12
- import { NodeFeatures } from "./models/nodeFeatures.js";
13
12
  /**
14
13
  * Initialise the locales for the application.
15
14
  * @param localesDirectory The directory containing the locales.
@@ -32,6 +31,17 @@ export async function initialiseLocales(localesDirectory) {
32
31
  export function getExecutionDirectory() {
33
32
  return process.cwd();
34
33
  }
34
+ /**
35
+ * Get the directory where the script is located.
36
+ * @param args The command line arguments.
37
+ * @returns The execution directory.
38
+ */
39
+ export function getScriptDirectory(args) {
40
+ if (Is.array(args) && args.length >= 2 && args[1].includes("index.js")) {
41
+ return path.resolve(path.join(path.dirname(args[1]), ".."));
42
+ }
43
+ return process.cwd();
44
+ }
35
45
  /**
36
46
  * Does the specified file exist.
37
47
  * @param filename The filename to check for existence.
@@ -121,26 +131,6 @@ export async function loadJsonFile(filename) {
121
131
  const content = await loadTextFile(filename);
122
132
  return JSON.parse(content);
123
133
  }
124
- /**
125
- * Get the features that are enabled on the node.
126
- * @param env The environment variables for the node.
127
- * @returns The features that are enabled on the node.
128
- */
129
- export function getFeatures(env) {
130
- if (Is.empty(env.features)) {
131
- return [];
132
- }
133
- const features = [];
134
- const allFeatures = Object.values(NodeFeatures);
135
- const splitFeatures = env.features.split(",");
136
- for (const feature of splitFeatures) {
137
- const featureTrimmed = feature.trim();
138
- if (allFeatures.includes(featureTrimmed)) {
139
- features.push(featureTrimmed);
140
- }
141
- }
142
- return features;
143
- }
144
134
  /**
145
135
  * Parse the protocol from a module name.
146
136
  * @param moduleName The module name to parse.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,GAAG,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EACN,SAAS,EACT,SAAS,EACT,YAAY,EACZ,IAAI,EACJ,EAAE,EAEF,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAKjD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,gBAAwB;IAC/D,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC;IACzE,UAAU,CAAC,KAAK,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IAC9C,IAAI,MAAM,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;QACnC,MAAM,aAAa,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,C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Ae,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,cAAc,CAAC,CAAC;YAC/D,MAAM,kBAAkB,GAAG,MAAM,YAAY,CAK1C,eAAe,CAAC,CAAC;YAEpB,kDAAkD;YAClD,OAAO,kBAAkB,CAAC,MAAM,IAAI,kBAAkB,CAAC,IAAI,IAAI,QAAQ,CAAC;QACzE,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAgB;IACrD,OAAO,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,UAAU,QAAQ,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;AACvE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { execSync } from \"node:child_process\";\nimport { mkdir, readdir, readFile, rename, stat, writeFile } from \"node:fs/promises\";\nimport { get as httpsGet } from \"node:https\";\nimport path from \"node:path\";\nimport { CLIDisplay } from \"@twin.org/cli-core\";\nimport {\n\tBaseError,\n\tConverter,\n\tGeneralError,\n\tI18n,\n\tIs,\n\ttype ILocaleDictionary\n} from \"@twin.org/core\";\nimport { Sha256 } from \"@twin.org/crypto\";\nimport { ModuleHelper } from \"@twin.org/modules\";\nimport type { ICacheMetadata } from \"./models/ICacheMetadata.js\";\nimport type { IModuleProtocol } from \"./models/IModuleProtocol.js\";\nimport type { INodeEnvironmentVariables } from \"./models/INodeEnvironmentVariables.js\";\nimport type { IProtocolHandlerResult } from \"./models/IProtocolHandlerResult.js\";\nimport { ModuleProtocol } from \"./models/moduleProtocol.js\";\nimport { NodeFeatures } from \"./models/nodeFeatures.js\";\n\n/**\n * Initialise the locales for the application.\n * @param localesDirectory The directory containing the locales.\n */\nexport async function initialiseLocales(localesDirectory: string): Promise<void> {\n\tconst localesFile = path.resolve(path.join(localesDirectory, \"en.json\"));\n\tCLIDisplay.value(\"Locales File\", localesFile);\n\tif (await fileExists(localesFile)) {\n\t\tconst enLangContent = await readFile(localesFile, \"utf8\");\n\t\tI18n.addDictionary(\"en\", JSON.parse(enLangContent) as ILocaleDictionary);\n\t} else {\n\t\tCLIDisplay.error(`Locales file not found: ${localesFile}`);\n\t}\n}\n\n/**\n * Get the directory where the application is being executed.\n * @returns The execution directory.\n */\nexport function getExecutionDirectory(): string {\n\treturn process.cwd();\n}\n\n/**\n * Does the specified file exist.\n * @param filename The filename to check for existence.\n * @returns True if the file exists.\n */\nexport async function fileExists(filename: string): Promise<boolean> {\n\ttry {\n\t\tconst stats = await stat(filename);\n\t\treturn stats.isFile();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Does the specified directory exist.\n * @param directory The directory to check for existence.\n * @returns True if the directory exists.\n */\nexport async function directoryExists(directory: string): Promise<boolean> {\n\ttry {\n\t\tconst stats = await stat(directory);\n\t\treturn stats.isDirectory();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Get the sub folders for the folder.\n * @param directory The directory to get the sub folders.\n * @returns The list of sub folders.\n */\nexport async function getSubFolders(directory: string): Promise<string[]> {\n\ttry {\n\t\tconst dir = await readdir(directory);\n\t\tconst folders: string[] = [];\n\t\tfor (const dirEntry of dir) {\n\t\t\tconst fullPath = path.join(directory, dirEntry);\n\t\t\tconst stats = await stat(fullPath);\n\t\t\tif (stats.isDirectory()) {\n\t\t\t\tfolders.push(fullPath);\n\t\t\t}\n\t\t}\n\t\treturn folders;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\n/**\n * Get the files in the directory.\n * @param directory The directory to get the files from.\n * @returns The list of files in the directory.\n */\nexport async function getFiles(directory: string): Promise<string[]> {\n\ttry {\n\t\tconst dir = await readdir(directory);\n\t\tconst files: string[] = [];\n\t\tfor (const dirEntry of dir) {\n\t\t\tconst fullPath = path.join(directory, dirEntry);\n\t\t\tconst stats = await stat(fullPath);\n\t\t\tif (stats.isFile()) {\n\t\t\t\tfiles.push(fullPath);\n\t\t\t}\n\t\t}\n\t\treturn files;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\n/**\n * Load the text file.\n * @param filename The filename of the text file to load.\n * @returns The contents of the text file if it could not be loaded.\n */\nexport async function loadTextFile(filename: string): Promise<string> {\n\treturn readFile(filename, \"utf8\");\n}\n\n/**\n * Load the JSON file.\n * @param filename The filename of the JSON file to load.\n * @returns The contents of the JSON file or null if it could not be loaded.\n */\nexport async function loadJsonFile<T>(filename: string): Promise<T> {\n\tconst content = await loadTextFile(filename);\n\treturn JSON.parse(content) as T;\n}\n\n/**\n * Get the features that are enabled on the node.\n * @param env The environment variables for the node.\n * @returns The features that are enabled on the node.\n */\nexport function getFeatures(env: INodeEnvironmentVariables): NodeFeatures[] {\n\tif (Is.empty(env.features)) {\n\t\treturn [];\n\t}\n\n\tconst features: NodeFeatures[] = [];\n\tconst allFeatures = Object.values(NodeFeatures);\n\n\tconst splitFeatures = env.features.split(\",\");\n\tfor (const feature of splitFeatures) {\n\t\tconst featureTrimmed = feature.trim() as NodeFeatures;\n\t\tif (allFeatures.includes(featureTrimmed)) {\n\t\t\tfeatures.push(featureTrimmed);\n\t\t}\n\t}\n\n\treturn features;\n}\n\n/**\n * Parse the protocol from a module name.\n * @param moduleName The module name to parse.\n * @returns The parsed protocol information.\n */\nexport function parseModuleProtocol(moduleName: string): IModuleProtocol {\n\tconst trimmed = moduleName.trim();\n\n\tif (trimmed.startsWith(\"npm:\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Npm,\n\t\t\tidentifier: trimmed.slice(4),\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"https://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Https,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"http://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Http,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"file://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Local,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (ModuleHelper.isLocalModule(trimmed)) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Local,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\treturn {\n\t\tprotocol: ModuleProtocol.Default,\n\t\tidentifier: trimmed,\n\t\toriginal: trimmed\n\t};\n}\n\n/**\n * Hash a URL to create a safe filename.\n * @param url The URL to hash.\n * @returns A hashed filename safe for the filesystem.\n */\nexport function hashUrl(url: string): string {\n\tconst urlBytes = Converter.utf8ToBytes(url);\n\tconst hashBytes = Sha256.sum256(urlBytes);\n\tconst hash = Converter.bytesToHex(hashBytes);\n\tconst ext = path.extname(new URL(url).pathname);\n\treturn `${hash}${ext}`;\n}\n\n/**\n * Get the extensions cache directory.\n * @param executionDirectory The execution directory.\n * @param protocol The protocol type for subdirectory organization.\n * @param cacheDirectory The cache directory base path.\n * @returns The cache directory path.\n */\nexport function getExtensionsCacheDir(\n\texecutionDirectory: string,\n\tprotocol: ModuleProtocol,\n\tcacheDirectory?: string\n): string {\n\t// Resolve to absolute path to ensure consistent behavior\n\tconst absoluteDir = path.resolve(executionDirectory);\n\tconst baseDir = cacheDirectory ?? \".tmp\";\n\treturn path.join(absoluteDir, baseDir, \"extensions\", protocol);\n}\n\n/**\n * Handle the npm: protocol by installing the package if needed.\n * @param packageName The npm package name (without npm: prefix).\n * @param executionDirectory The execution directory.\n * @param cacheDirectory The cache directory base path.\n * @returns The resolved path to the installed module.\n */\nexport async function handleNpmProtocol(\n\tpackageName: string,\n\texecutionDirectory: string,\n\tcacheDirectory?: string\n): Promise<IProtocolHandlerResult> {\n\tconst cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Npm, cacheDirectory);\n\t// Extract just the package name (without version) for the directory\n\t// e.g. \"picocolors@1.0.0\" becomes \"picocolors\"\n\t// e.g. \"@scope/package@1.0.0\" becomes \"@scope/package\"\n\tconst lastAtIndex = packageName.lastIndexOf(\"@\");\n\tconst packageNameOnly = lastAtIndex > 0 ? packageName.slice(0, lastAtIndex) : packageName;\n\tconst packageDir = path.join(cacheDir, \"node_modules\", packageNameOnly);\n\tconst packageJsonPath = path.join(packageDir, \"package.json\");\n\n\tconst exists = await fileExists(packageJsonPath);\n\tif (exists) {\n\t\tconst mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);\n\t\tconst modulePath = path.join(packageDir, mainFile);\n\n\t\treturn {\n\t\t\tresolvedPath: modulePath,\n\t\t\tcached: true\n\t\t};\n\t}\n\n\tawait mkdir(cacheDir, { recursive: true });\n\n\tCLIDisplay.task(I18n.formatMessage(\"node.extensionNpmInstalling\"), packageName);\n\n\ttry {\n\t\t// Always pipe stdio to comply with env access restrictions in tests/lint\n\t\tconst stdio: \"pipe\" | \"inherit\" | \"ignore\" = \"pipe\";\n\t\texecSync(`npm install ${packageName} --prefix \"${cacheDir}\" --no-save --no-package-lock`, {\n\t\t\tcwd: cacheDir,\n\t\t\tstdio\n\t\t});\n\t} catch (err) {\n\t\tthrow new GeneralError(\n\t\t\t\"node\",\n\t\t\t\"extensionNpmInstallFailed\",\n\t\t\t{\n\t\t\t\tpackage: packageName\n\t\t\t},\n\t\t\tBaseError.fromError(err)\n\t\t);\n\t}\n\n\tconst mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);\n\tconst modulePath = path.join(packageDir, mainFile);\n\n\treturn {\n\t\tresolvedPath: modulePath,\n\t\tcached: false\n\t};\n}\n\n/**\n * Check if a cached file has expired based on TTL and force refresh settings.\n * @param metadataPath Path to the cache metadata file.\n * @param ttlHours Time to live in hours.\n * @param forceRefresh Whether to force refresh regardless of TTL.\n * @returns True if the cache is expired or should be refreshed.\n */\nexport async function isCacheExpired(\n\tmetadataPath: string,\n\tttlHours: number,\n\tforceRefresh: boolean\n): Promise<boolean> {\n\tif (forceRefresh) {\n\t\treturn true;\n\t}\n\n\ttry {\n\t\tconst metadata = await loadJsonFile<ICacheMetadata>(metadataPath);\n\t\tconst ttlMillis = ttlHours * 60 * 60 * 1000;\n\t\tconst expireTime = metadata.downloadedAt + ttlMillis;\n\t\treturn Date.now() > expireTime;\n\t} catch {\n\t\t// If metadata doesn't exist or is corrupted, consider expired\n\t\treturn true;\n\t}\n}\n\n/**\n * Handle the https: protocol by downloading the module if needed.\n * @param url The HTTPS URL to download from.\n * @param executionDirectory The execution directory.\n * @param maxSizeMb The maximum size in MB for the download.\n * @param cacheDirectory The cache directory base path.\n * @param ttlHours TTL in hours for cache expiration.\n * @param forceRefresh Whether to force refresh the cache.\n * @returns The resolved path to the downloaded module.\n */\nexport async function handleHttpsProtocol(\n\turl: string,\n\texecutionDirectory: string,\n\tmaxSizeMb: number,\n\tcacheDirectory?: string,\n\tttlHours?: number,\n\tforceRefresh?: boolean\n): Promise<IProtocolHandlerResult> {\n\tconst effectiveTtlHours = ttlHours ?? 24;\n\tconst effectiveForceRefresh = forceRefresh ?? false;\n\tconst cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Https, cacheDirectory);\n\tconst filename = hashUrl(url);\n\tconst cachedPath = path.join(cacheDir, filename);\n\tconst metadataPath = `${cachedPath}.meta`;\n\n\tconst exists = await fileExists(cachedPath);\n\tif (exists) {\n\t\tconst expired = await isCacheExpired(metadataPath, effectiveTtlHours, effectiveForceRefresh);\n\t\tif (!expired) {\n\t\t\treturn {\n\t\t\t\tresolvedPath: cachedPath,\n\t\t\t\tcached: true\n\t\t\t};\n\t\t}\n\n\t\tif (effectiveForceRefresh) {\n\t\t\tCLIDisplay.warning(I18n.formatMessage(\"node.extensionForceRefresh\", { url }));\n\t\t} else {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"node.extensionCacheExpired\", { url }));\n\t\t}\n\t}\n\n\tCLIDisplay.warning(I18n.formatMessage(\"node.extensionSecurityWarning\", { url }));\n\tCLIDisplay.task(I18n.formatMessage(\"node.extensionHttpsDownloading\"), url);\n\n\tawait mkdir(cacheDir, { recursive: true });\n\n\tconst maxSizeBytes = maxSizeMb * 1024 * 1024;\n\tlet downloadedSize = 0;\n\tconst chunks: Buffer[] = [];\n\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\thttpsGet(url, response => {\n\t\t\t\tif (response.statusCode !== 200) {\n\t\t\t\t\treject(\n\t\t\t\t\t\tnew GeneralError(\"node\", \"extensionDownloadFailed\", {\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatus: response.statusCode ?? 0\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresponse.on(\"data\", (chunk: Buffer) => {\n\t\t\t\t\tdownloadedSize += chunk.length;\n\n\t\t\t\t\tif (downloadedSize > maxSizeBytes) {\n\t\t\t\t\t\tresponse.destroy();\n\t\t\t\t\t\treject(\n\t\t\t\t\t\t\tnew GeneralError(\"node\", \"extensionSizeLimitExceeded\", {\n\t\t\t\t\t\t\t\tsize: downloadedSize,\n\t\t\t\t\t\t\t\tlimit: maxSizeBytes\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tchunks.push(chunk);\n\t\t\t\t});\n\n\t\t\t\tresponse.on(\"end\", () => {\n\t\t\t\t\tresolve();\n\t\t\t\t});\n\n\t\t\t\tresponse.on(\"error\", err => {\n\t\t\t\t\treject(BaseError.fromError(err));\n\t\t\t\t});\n\t\t\t}).on(\"error\", err => {\n\t\t\t\treject(BaseError.fromError(err));\n\t\t\t});\n\t\t});\n\t} catch (err) {\n\t\tthrow new GeneralError(\n\t\t\t\"node\",\n\t\t\t\"extensionDownloadFailed\",\n\t\t\t{\n\t\t\t\turl\n\t\t\t},\n\t\t\tBaseError.fromError(err)\n\t\t);\n\t}\n\n\tconst tempPath = `${cachedPath}.tmp`;\n\tawait writeFile(tempPath, Buffer.concat(chunks));\n\n\t// Atomic move from temp to final location\n\tawait rename(tempPath, cachedPath);\n\n\t// Save metadata for TTL tracking\n\tconst metadata: ICacheMetadata = {\n\t\tdownloadedAt: Date.now(),\n\t\turl,\n\t\tsize: Buffer.concat(chunks).length\n\t};\n\tawait writeFile(metadataPath, JSON.stringify(metadata, null, 2));\n\n\treturn {\n\t\tresolvedPath: cachedPath,\n\t\tcached: false\n\t};\n}\n\n/**\n * Resolve the main entry point from a package directory using Node.js resolution with fallback.\n * Uses require.resolve() when possible for standard Node.js behavior, with manual fallback.\n * @param packagePath The absolute path to the package directory.\n * @param packageName The package name for require.resolve().\n * @param fallback The fallback file name if no entry point is found.\n * @returns The resolved entry point file name (relative to package directory).\n */\nexport async function resolvePackageEntryPoint(\n\tpackagePath: string,\n\tpackageName: string,\n\tfallback = \"index.js\"\n): Promise<string> {\n\ttry {\n\t\t// Try require.resolve() first - handles exports, main, module automatically\n\t\tconst resolvedPath = require.resolve(packageName, { paths: [path.dirname(packagePath)] });\n\t\t// Convert absolute path back to relative filename within package\n\t\tconst relativePath = path.relative(packagePath, resolvedPath);\n\t\treturn relativePath ?? fallback;\n\t} catch {\n\t\t// Fallback to manual package.json parsing if require.resolve fails\n\t\ttry {\n\t\t\tconst packageJsonPath = path.join(packagePath, \"package.json\");\n\t\t\tconst packageJsonContent = await loadJsonFile<{\n\t\t\t\tmodule?: string;\n\t\t\t\tmain?: string;\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\t\texports?: any;\n\t\t\t}>(packageJsonPath);\n\n\t\t\t// Future: Could expand exports field support here\n\t\t\treturn packageJsonContent.module ?? packageJsonContent.main ?? fallback;\n\t\t} catch {\n\t\t\treturn fallback;\n\t\t}\n\t}\n}\n\n/**\n * Convert a file path to an import-compatible URL for cross-platform module loading.\n * On Windows, adds the 'file://' protocol prefix required for dynamic imports.\n * On other platforms, returns the path unchanged.\n * @param filePath The absolute file path to convert.\n * @returns A URL string compatible with dynamic import().\n */\nexport function createModuleImportUrl(filePath: string): string {\n\treturn process.platform === \"win32\" ? `file://${filePath}` : filePath;\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACrF,OAAO,EAAE,GAAG,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EACN,SAAS,EACT,SAAS,EACT,YAAY,EACZ,IAAI,EACJ,EAAE,EAEF,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAIjD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAE5D;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,gBAAwB;IAC/D,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC;IACzE,UAAU,CAAC,KAAK,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;IAC9C,IAAI,MAAM,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;QACnC,MAAM,aAAa,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QAC1D,IAAI,CAAC,aAAa,CAAC,IA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,MAAM,CAAC;YACR,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAgB;IACrD,OAAO,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,UAAU,QAAQ,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;AACvE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { execSync } from \"node:child_process\";\nimport { mkdir, readdir, readFile, rename, stat, writeFile } from \"node:fs/promises\";\nimport { get as httpsGet } from \"node:https\";\nimport path from \"node:path\";\nimport { CLIDisplay } from \"@twin.org/cli-core\";\nimport {\n\tBaseError,\n\tConverter,\n\tGeneralError,\n\tI18n,\n\tIs,\n\ttype ILocaleDictionary\n} from \"@twin.org/core\";\nimport { Sha256 } from \"@twin.org/crypto\";\nimport { ModuleHelper } from \"@twin.org/modules\";\nimport type { ICacheMetadata } from \"./models/ICacheMetadata.js\";\nimport type { IModuleProtocol } from \"./models/IModuleProtocol.js\";\nimport type { IProtocolHandlerResult } from \"./models/IProtocolHandlerResult.js\";\nimport { ModuleProtocol } from \"./models/moduleProtocol.js\";\n\n/**\n * Initialise the locales for the application.\n * @param localesDirectory The directory containing the locales.\n */\nexport async function initialiseLocales(localesDirectory: string): Promise<void> {\n\tconst localesFile = path.resolve(path.join(localesDirectory, \"en.json\"));\n\tCLIDisplay.value(\"Locales File\", localesFile);\n\tif (await fileExists(localesFile)) {\n\t\tconst enLangContent = await readFile(localesFile, \"utf8\");\n\t\tI18n.addDictionary(\"en\", JSON.parse(enLangContent) as ILocaleDictionary);\n\t} else {\n\t\tCLIDisplay.error(`Locales file not found: ${localesFile}`);\n\t}\n}\n\n/**\n * Get the directory where the application is being executed.\n * @returns The execution directory.\n */\nexport function getExecutionDirectory(): string {\n\treturn process.cwd();\n}\n\n/**\n * Get the directory where the script is located.\n * @param args The command line arguments.\n * @returns The execution directory.\n */\nexport function getScriptDirectory(args?: string[]): string {\n\tif (Is.array<string>(args) && args.length >= 2 && args[1].includes(\"index.js\")) {\n\t\treturn path.resolve(path.join(path.dirname(args[1]), \"..\"));\n\t}\n\treturn process.cwd();\n}\n\n/**\n * Does the specified file exist.\n * @param filename The filename to check for existence.\n * @returns True if the file exists.\n */\nexport async function fileExists(filename: string): Promise<boolean> {\n\ttry {\n\t\tconst stats = await stat(filename);\n\t\treturn stats.isFile();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Does the specified directory exist.\n * @param directory The directory to check for existence.\n * @returns True if the directory exists.\n */\nexport async function directoryExists(directory: string): Promise<boolean> {\n\ttry {\n\t\tconst stats = await stat(directory);\n\t\treturn stats.isDirectory();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Get the sub folders for the folder.\n * @param directory The directory to get the sub folders.\n * @returns The list of sub folders.\n */\nexport async function getSubFolders(directory: string): Promise<string[]> {\n\ttry {\n\t\tconst dir = await readdir(directory);\n\t\tconst folders: string[] = [];\n\t\tfor (const dirEntry of dir) {\n\t\t\tconst fullPath = path.join(directory, dirEntry);\n\t\t\tconst stats = await stat(fullPath);\n\t\t\tif (stats.isDirectory()) {\n\t\t\t\tfolders.push(fullPath);\n\t\t\t}\n\t\t}\n\t\treturn folders;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\n/**\n * Get the files in the directory.\n * @param directory The directory to get the files from.\n * @returns The list of files in the directory.\n */\nexport async function getFiles(directory: string): Promise<string[]> {\n\ttry {\n\t\tconst dir = await readdir(directory);\n\t\tconst files: string[] = [];\n\t\tfor (const dirEntry of dir) {\n\t\t\tconst fullPath = path.join(directory, dirEntry);\n\t\t\tconst stats = await stat(fullPath);\n\t\t\tif (stats.isFile()) {\n\t\t\t\tfiles.push(fullPath);\n\t\t\t}\n\t\t}\n\t\treturn files;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\n/**\n * Load the text file.\n * @param filename The filename of the text file to load.\n * @returns The contents of the text file if it could not be loaded.\n */\nexport async function loadTextFile(filename: string): Promise<string> {\n\treturn readFile(filename, \"utf8\");\n}\n\n/**\n * Load the JSON file.\n * @param filename The filename of the JSON file to load.\n * @returns The contents of the JSON file or null if it could not be loaded.\n */\nexport async function loadJsonFile<T>(filename: string): Promise<T> {\n\tconst content = await loadTextFile(filename);\n\treturn JSON.parse(content) as T;\n}\n\n/**\n * Parse the protocol from a module name.\n * @param moduleName The module name to parse.\n * @returns The parsed protocol information.\n */\nexport function parseModuleProtocol(moduleName: string): IModuleProtocol {\n\tconst trimmed = moduleName.trim();\n\n\tif (trimmed.startsWith(\"npm:\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Npm,\n\t\t\tidentifier: trimmed.slice(4),\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"https://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Https,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"http://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Http,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (trimmed.startsWith(\"file://\")) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Local,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\tif (ModuleHelper.isLocalModule(trimmed)) {\n\t\treturn {\n\t\t\tprotocol: ModuleProtocol.Local,\n\t\t\tidentifier: trimmed,\n\t\t\toriginal: trimmed\n\t\t};\n\t}\n\n\treturn {\n\t\tprotocol: ModuleProtocol.Default,\n\t\tidentifier: trimmed,\n\t\toriginal: trimmed\n\t};\n}\n\n/**\n * Hash a URL to create a safe filename.\n * @param url The URL to hash.\n * @returns A hashed filename safe for the filesystem.\n */\nexport function hashUrl(url: string): string {\n\tconst urlBytes = Converter.utf8ToBytes(url);\n\tconst hashBytes = Sha256.sum256(urlBytes);\n\tconst hash = Converter.bytesToHex(hashBytes);\n\tconst ext = path.extname(new URL(url).pathname);\n\treturn `${hash}${ext}`;\n}\n\n/**\n * Get the extensions cache directory.\n * @param executionDirectory The execution directory.\n * @param protocol The protocol type for subdirectory organization.\n * @param cacheDirectory The cache directory base path.\n * @returns The cache directory path.\n */\nexport function getExtensionsCacheDir(\n\texecutionDirectory: string,\n\tprotocol: ModuleProtocol,\n\tcacheDirectory?: string\n): string {\n\t// Resolve to absolute path to ensure consistent behavior\n\tconst absoluteDir = path.resolve(executionDirectory);\n\tconst baseDir = cacheDirectory ?? \".tmp\";\n\treturn path.join(absoluteDir, baseDir, \"extensions\", protocol);\n}\n\n/**\n * Handle the npm: protocol by installing the package if needed.\n * @param packageName The npm package name (without npm: prefix).\n * @param executionDirectory The execution directory.\n * @param cacheDirectory The cache directory base path.\n * @returns The resolved path to the installed module.\n */\nexport async function handleNpmProtocol(\n\tpackageName: string,\n\texecutionDirectory: string,\n\tcacheDirectory?: string\n): Promise<IProtocolHandlerResult> {\n\tconst cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Npm, cacheDirectory);\n\t// Extract just the package name (without version) for the directory\n\t// e.g. \"picocolors@1.0.0\" becomes \"picocolors\"\n\t// e.g. \"@scope/package@1.0.0\" becomes \"@scope/package\"\n\tconst lastAtIndex = packageName.lastIndexOf(\"@\");\n\tconst packageNameOnly = lastAtIndex > 0 ? packageName.slice(0, lastAtIndex) : packageName;\n\tconst packageDir = path.join(cacheDir, \"node_modules\", packageNameOnly);\n\tconst packageJsonPath = path.join(packageDir, \"package.json\");\n\n\tconst exists = await fileExists(packageJsonPath);\n\tif (exists) {\n\t\tconst mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);\n\t\tconst modulePath = path.join(packageDir, mainFile);\n\n\t\treturn {\n\t\t\tresolvedPath: modulePath,\n\t\t\tcached: true\n\t\t};\n\t}\n\n\tawait mkdir(cacheDir, { recursive: true });\n\n\tCLIDisplay.task(I18n.formatMessage(\"node.extensionNpmInstalling\"), packageName);\n\n\ttry {\n\t\t// Always pipe stdio to comply with env access restrictions in tests/lint\n\t\tconst stdio: \"pipe\" | \"inherit\" | \"ignore\" = \"pipe\";\n\t\texecSync(`npm install ${packageName} --prefix \"${cacheDir}\" --no-save --no-package-lock`, {\n\t\t\tcwd: cacheDir,\n\t\t\tstdio\n\t\t});\n\t} catch (err) {\n\t\tthrow new GeneralError(\n\t\t\t\"node\",\n\t\t\t\"extensionNpmInstallFailed\",\n\t\t\t{\n\t\t\t\tpackage: packageName\n\t\t\t},\n\t\t\tBaseError.fromError(err)\n\t\t);\n\t}\n\n\tconst mainFile = await resolvePackageEntryPoint(packageDir, packageNameOnly);\n\tconst modulePath = path.join(packageDir, mainFile);\n\n\treturn {\n\t\tresolvedPath: modulePath,\n\t\tcached: false\n\t};\n}\n\n/**\n * Check if a cached file has expired based on TTL and force refresh settings.\n * @param metadataPath Path to the cache metadata file.\n * @param ttlHours Time to live in hours.\n * @param forceRefresh Whether to force refresh regardless of TTL.\n * @returns True if the cache is expired or should be refreshed.\n */\nexport async function isCacheExpired(\n\tmetadataPath: string,\n\tttlHours: number,\n\tforceRefresh: boolean\n): Promise<boolean> {\n\tif (forceRefresh) {\n\t\treturn true;\n\t}\n\n\ttry {\n\t\tconst metadata = await loadJsonFile<ICacheMetadata>(metadataPath);\n\t\tconst ttlMillis = ttlHours * 60 * 60 * 1000;\n\t\tconst expireTime = metadata.downloadedAt + ttlMillis;\n\t\treturn Date.now() > expireTime;\n\t} catch {\n\t\t// If metadata doesn't exist or is corrupted, consider expired\n\t\treturn true;\n\t}\n}\n\n/**\n * Handle the https: protocol by downloading the module if needed.\n * @param url The HTTPS URL to download from.\n * @param executionDirectory The execution directory.\n * @param maxSizeMb The maximum size in MB for the download.\n * @param cacheDirectory The cache directory base path.\n * @param ttlHours TTL in hours for cache expiration.\n * @param forceRefresh Whether to force refresh the cache.\n * @returns The resolved path to the downloaded module.\n */\nexport async function handleHttpsProtocol(\n\turl: string,\n\texecutionDirectory: string,\n\tmaxSizeMb: number,\n\tcacheDirectory?: string,\n\tttlHours?: number,\n\tforceRefresh?: boolean\n): Promise<IProtocolHandlerResult> {\n\tconst effectiveTtlHours = ttlHours ?? 24;\n\tconst effectiveForceRefresh = forceRefresh ?? false;\n\tconst cacheDir = getExtensionsCacheDir(executionDirectory, ModuleProtocol.Https, cacheDirectory);\n\tconst filename = hashUrl(url);\n\tconst cachedPath = path.join(cacheDir, filename);\n\tconst metadataPath = `${cachedPath}.meta`;\n\n\tconst exists = await fileExists(cachedPath);\n\tif (exists) {\n\t\tconst expired = await isCacheExpired(metadataPath, effectiveTtlHours, effectiveForceRefresh);\n\t\tif (!expired) {\n\t\t\treturn {\n\t\t\t\tresolvedPath: cachedPath,\n\t\t\t\tcached: true\n\t\t\t};\n\t\t}\n\n\t\tif (effectiveForceRefresh) {\n\t\t\tCLIDisplay.warning(I18n.formatMessage(\"node.extensionForceRefresh\", { url }));\n\t\t} else {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"node.extensionCacheExpired\", { url }));\n\t\t}\n\t}\n\n\tCLIDisplay.warning(I18n.formatMessage(\"node.extensionSecurityWarning\", { url }));\n\tCLIDisplay.task(I18n.formatMessage(\"node.extensionHttpsDownloading\"), url);\n\n\tawait mkdir(cacheDir, { recursive: true });\n\n\tconst maxSizeBytes = maxSizeMb * 1024 * 1024;\n\tlet downloadedSize = 0;\n\tconst chunks: Buffer[] = [];\n\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\thttpsGet(url, response => {\n\t\t\t\tif (response.statusCode !== 200) {\n\t\t\t\t\treject(\n\t\t\t\t\t\tnew GeneralError(\"node\", \"extensionDownloadFailed\", {\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatus: response.statusCode ?? 0\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresponse.on(\"data\", (chunk: Buffer) => {\n\t\t\t\t\tdownloadedSize += chunk.length;\n\n\t\t\t\t\tif (downloadedSize > maxSizeBytes) {\n\t\t\t\t\t\tresponse.destroy();\n\t\t\t\t\t\treject(\n\t\t\t\t\t\t\tnew GeneralError(\"node\", \"extensionSizeLimitExceeded\", {\n\t\t\t\t\t\t\t\tsize: downloadedSize,\n\t\t\t\t\t\t\t\tlimit: maxSizeBytes\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tchunks.push(chunk);\n\t\t\t\t});\n\n\t\t\t\tresponse.on(\"end\", () => {\n\t\t\t\t\tresolve();\n\t\t\t\t});\n\n\t\t\t\tresponse.on(\"error\", err => {\n\t\t\t\t\treject(BaseError.fromError(err));\n\t\t\t\t});\n\t\t\t}).on(\"error\", err => {\n\t\t\t\treject(BaseError.fromError(err));\n\t\t\t});\n\t\t});\n\t} catch (err) {\n\t\tthrow new GeneralError(\n\t\t\t\"node\",\n\t\t\t\"extensionDownloadFailed\",\n\t\t\t{\n\t\t\t\turl\n\t\t\t},\n\t\t\tBaseError.fromError(err)\n\t\t);\n\t}\n\n\tconst tempPath = `${cachedPath}.tmp`;\n\tawait writeFile(tempPath, Buffer.concat(chunks));\n\n\t// Atomic move from temp to final location\n\tawait rename(tempPath, cachedPath);\n\n\t// Save metadata for TTL tracking\n\tconst metadata: ICacheMetadata = {\n\t\tdownloadedAt: Date.now(),\n\t\turl,\n\t\tsize: Buffer.concat(chunks).length\n\t};\n\tawait writeFile(metadataPath, JSON.stringify(metadata, null, 2));\n\n\treturn {\n\t\tresolvedPath: cachedPath,\n\t\tcached: false\n\t};\n}\n\n/**\n * Resolve the main entry point from a package directory using Node.js resolution with fallback.\n * Uses require.resolve() when possible for standard Node.js behavior, with manual fallback.\n * @param packagePath The absolute path to the package directory.\n * @param packageName The package name for require.resolve().\n * @param fallback The fallback file name if no entry point is found.\n * @returns The resolved entry point file name (relative to package directory).\n */\nexport async function resolvePackageEntryPoint(\n\tpackagePath: string,\n\tpackageName: string,\n\tfallback = \"index.js\"\n): Promise<string> {\n\ttry {\n\t\t// Try require.resolve() first - handles exports, main, module automatically\n\t\tconst resolvedPath = require.resolve(packageName, { paths: [path.dirname(packagePath)] });\n\t\t// Convert absolute path back to relative filename within package\n\t\tconst relativePath = path.relative(packagePath, resolvedPath);\n\t\treturn relativePath ?? fallback;\n\t} catch {\n\t\t// Fallback to manual package.json parsing if require.resolve fails\n\t\ttry {\n\t\t\tconst packageJsonPath = path.join(packagePath, \"package.json\");\n\t\t\tconst packageJsonContent = await loadJsonFile<{\n\t\t\t\tmodule?: string;\n\t\t\t\tmain?: string;\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\t\texports?: any;\n\t\t\t}>(packageJsonPath);\n\n\t\t\t// Future: Could expand exports field support here\n\t\t\treturn packageJsonContent.module ?? packageJsonContent.main ?? fallback;\n\t\t} catch {\n\t\t\treturn fallback;\n\t\t}\n\t}\n}\n\n/**\n * Convert a file path to an import-compatible URL for cross-platform module loading.\n * On Windows, adds the 'file://' protocol prefix required for dynamic imports.\n * On other platforms, returns the path unchanged.\n * @param filePath The absolute file path to convert.\n * @returns A URL string compatible with dynamic import().\n */\nexport function createModuleImportUrl(filePath: string): string {\n\treturn process.platform === \"win32\" ? `file://${filePath}` : filePath;\n}\n"]}
@@ -6,4 +6,7 @@ import type { IEngineEnvironmentVariables } from "../models/IEngineEnvironmentVa
6
6
  * @param contextIdKeys The context ID keys.
7
7
  * @returns The config for the core.
8
8
  */
9
- export declare function buildEngineConfiguration(envVars: IEngineEnvironmentVariables, contextIdKeys: string[]): Promise<IEngineConfig>;
9
+ export declare function buildEngineConfiguration(envVars: IEngineEnvironmentVariables, contextIdKeys: {
10
+ key: string;
11
+ requiredHandlerFeatures: string[];
12
+ }[]): Promise<IEngineConfig>;
@@ -5,11 +5,14 @@ import type { IEngineServerEnvironmentVariables } from "../models/IEngineServerE
5
5
  /**
6
6
  * Handles the configuration of the server.
7
7
  * @param envVars The environment variables for the engine server.
8
- * @param contextIdKeys The context ID keys.
8
+ * @param availableContextIdKeys The context ID keys.
9
9
  * @param coreEngineConfig The core engine config.
10
10
  * @param serverInfo The server information.
11
11
  * @param openApiSpecPath The path to the open api spec.
12
12
  * @param favIconPath The path to the favicon.
13
13
  * @returns The config for the core and the server.
14
14
  */
15
- export declare function buildEngineServerConfiguration(envVars: IEngineServerEnvironmentVariables, contextIdKeys: string[], coreEngineConfig: IEngineCoreConfig, serverInfo: IServerInfo, openApiSpecPath?: string, favIconPath?: string): Promise<IEngineServerConfig>;
15
+ export declare function buildEngineServerConfiguration(envVars: IEngineServerEnvironmentVariables, availableContextIdKeys: {
16
+ key: string;
17
+ requiredHandlerFeatures: string[];
18
+ }[], coreEngineConfig: IEngineCoreConfig, serverInfo: IServerInfo, openApiSpecPath?: string, favIconPath?: string): Promise<IEngineServerConfig>;
@@ -0,0 +1,56 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliArgs } from "./models/ICliArgs.js";
3
+ import type { ICliCommand } from "./models/ICliCommand.js";
4
+ import type { INodeEnvironmentVariables } from "./models/INodeEnvironmentVariables.js";
5
+ /**
6
+ * Parse command line arguments.
7
+ * @param args The command line arguments.
8
+ * @returns The parsed command line arguments.
9
+ */
10
+ export declare function parseCommandLineArgs(args?: string[]): ICliArgs;
11
+ /**
12
+ * Construct the CLI command from the parsed arguments.
13
+ * @param processEnv The environment variables from the process.
14
+ * @param cliArgs The parsed CLI arguments.
15
+ * @returns The constructed CLI command.
16
+ * @throws GeneralError if the command is missing.
17
+ */
18
+ export declare function constructCliCommand(processEnv: {
19
+ [id: string]: string;
20
+ }, cliArgs: ICliArgs): ICliCommand | undefined;
21
+ /**
22
+ * Execute the CLI command.
23
+ * @param engineCore The engine core.
24
+ * @param envVars The environment variables for the node.
25
+ * @param cliCommand The CLI command to execute.
26
+ */
27
+ export declare function executeCommand(engineCore: IEngineCore, envVars: INodeEnvironmentVariables, cliCommand: ICliCommand): Promise<void>;
28
+ /**
29
+ * Load the env files and process the options.
30
+ * @param processEnv The environment variables from the process.
31
+ * @param options The options.
32
+ * @returns The processed parameters.
33
+ * @throws GeneralError if an env file has errors.
34
+ */
35
+ export declare function processEnvOptions(processEnv: {
36
+ [id: string]: string;
37
+ }, options: {
38
+ key: string;
39
+ value: string;
40
+ }[]): void;
41
+ /**
42
+ * Process options and replace any env variables with their values.
43
+ * @param processEnv The environment variables from the process.
44
+ * @param options The options.
45
+ * @throws GeneralError if an env variable is missing.
46
+ */
47
+ export declare function substituteEnvOptions(processEnv: {
48
+ [id: string]: string;
49
+ }, options: {
50
+ key: string;
51
+ value: string;
52
+ }[]): void;
53
+ /**
54
+ * Register available CLI commands.
55
+ */
56
+ export declare function registerCommands(): void;
@@ -0,0 +1,66 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
4
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
5
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
6
+ /**
7
+ * Get the command definition parameters.
8
+ * @param commandDefinitions The registered command definitions.
9
+ */
10
+ export declare function getCommandDefinitionBootstrapLegacy(commandDefinitions: {
11
+ [id: string]: ICliCommandDefinition;
12
+ }): void;
13
+ /**
14
+ * Command for legacy bootstrap.
15
+ * @param engineCore The engine core.
16
+ * @param envVars The environment variables for the node.
17
+ * @param params The parameters for the command.
18
+ */
19
+ export declare function bootstrapLegacy(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables & {
20
+ /**
21
+ * The features that are enabled on the node.
22
+ * @default []
23
+ */
24
+ features?: string;
25
+ /**
26
+ * The identity of the node which, if empty and node-identity feature is enabled it will be generated.
27
+ */
28
+ nodeIdentity?: string;
29
+ /**
30
+ * The mnemonic for the identity, if empty and node-identity feature is enabled it will be randomly generated.
31
+ */
32
+ nodeMnemonic?: string;
33
+ /**
34
+ * A tenant id to use as a default for the node.
35
+ */
36
+ tenantId?: string;
37
+ /**
38
+ * A tenant api key to use as a default for the node.
39
+ */
40
+ tenantApiKey?: string;
41
+ /**
42
+ * If the node-admin-user feature is enabled, this will be the organization of the user, if one is not provided it will be generated
43
+ */
44
+ organizationIdentity?: string;
45
+ /**
46
+ * The mnemonic for the organization, if empty and node-admin-user feature is enabled it will be randomly generated.
47
+ */
48
+ organizationMnemonic?: string;
49
+ /**
50
+ * If the node-admin-user feature is enabled, this will be the identity of the user, if one is not provided it will be generated
51
+ */
52
+ adminUserIdentity?: string;
53
+ /**
54
+ * The mnemonic for the admin user, if empty and node-admin-user feature is enabled it will be randomly generated.
55
+ */
56
+ adminUserMnemonic?: string;
57
+ /**
58
+ * If the node-admin-user feature is enabled, this will be the name of the user.
59
+ * @default admin@node
60
+ */
61
+ adminUserName?: string;
62
+ /**
63
+ * If the node-admin-user feature is enabled, this will be the password of the user, if empty it will be randomly generated.
64
+ */
65
+ adminUserPassword?: string;
66
+ }, params: {}): Promise<void>;
@@ -0,0 +1,23 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
4
+ /**
5
+ * Get the command definition parameters.
6
+ * @param commandDefinitions The registered command definitions.
7
+ */
8
+ export declare function getCommandDefinitionHelp(commandDefinitions: {
9
+ [id: string]: ICliCommandDefinition;
10
+ }): void;
11
+ /**
12
+ * Command for displaying help information.
13
+ * @param engineCore The engine core.
14
+ * @param envVars The environment variables for the node.
15
+ * @param params The command parameters.
16
+ * @param params.command The command to display help for.
17
+ * @param commandDefinitions The registered command definitions.
18
+ */
19
+ export declare function help(engineCore: IEngineCore, envVars: INodeEnvironmentVariables, params: {
20
+ command?: string;
21
+ }, commandDefinitions: {
22
+ [id: string]: ICliCommandDefinition;
23
+ }): Promise<void>;
@@ -0,0 +1,39 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
4
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
5
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
6
+ /**
7
+ * Get the command definition parameters.
8
+ * @param commandDefinitions The registered command definitions.
9
+ */
10
+ export declare function getCommandDefinitionIdentityCreate(commandDefinitions: {
11
+ [id: string]: ICliCommandDefinition;
12
+ }): void;
13
+ /**
14
+ * Command for creating an identity.
15
+ * @param engineCore The engine core.
16
+ * @param envVars The environment variables for the node.
17
+ * @param params The parameters for the command.
18
+ * @param params.mnemonic The mnemonic to use for the identity.
19
+ * @param params.identity The DID of the identity to create.
20
+ * @param params.controller The controller DID for the identity.
21
+ * @param params.fundWallet Whether to fund the wallet associated with the identity from a faucet.
22
+ * @param params.outputJson The output .json file to store the command output.
23
+ * @param params.outputEnv The output .env file to store the command output.
24
+ * @param params.outputEnvPrefix The prefix to use for variables in the output .env file.
25
+ * @returns The identity details.
26
+ */
27
+ export declare function identityCreate(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
28
+ mnemonic?: string;
29
+ identity?: string;
30
+ controller?: string;
31
+ fundWallet?: boolean;
32
+ outputJson?: string;
33
+ outputEnv?: string;
34
+ outputEnvPrefix?: string;
35
+ }): Promise<{
36
+ mnemonic: string;
37
+ did: string;
38
+ walletAddress?: string;
39
+ }>;
@@ -0,0 +1,24 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
4
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
5
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
6
+ /**
7
+ * Get the command definition parameters.
8
+ * @param commandDefinitions The registered command definitions.
9
+ */
10
+ export declare function getCommandDefinitionIdentityImport(commandDefinitions: {
11
+ [id: string]: ICliCommandDefinition;
12
+ }): void;
13
+ /**
14
+ * Command for importing an identity.
15
+ * @param engineCore The engine core.
16
+ * @param envVars The environment variables for the node.
17
+ * @param params The parameters for the command.
18
+ * @param params.identity The DID of the identity to import.
19
+ * @param params.mnemonic The mnemonic to use for the identity.
20
+ */
21
+ export declare function identityImport(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
22
+ identity?: string;
23
+ mnemonic?: string;
24
+ }): Promise<void>;
@@ -0,0 +1,43 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { IDidVerifiableCredential } from "@twin.org/standards-w3c-did";
3
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
4
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
5
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
6
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
7
+ /**
8
+ * Get the command definition parameters.
9
+ * @param commandDefinitions The registered command definitions.
10
+ */
11
+ export declare function getCommandDefinitionIdentityVerifiableCredentialCreate(commandDefinitions: {
12
+ [id: string]: ICliCommandDefinition;
13
+ }): void;
14
+ /**
15
+ * Command for creating an identity verifiable credential.
16
+ * @param engineCore The engine core.
17
+ * @param envVars The environment variables for the node.
18
+ * @param params The parameters for the command.
19
+ * @param params.verificationMethodId The ID of the verification method to create the credential with.
20
+ * @param params.identity The DID of the identity to create the credential for.
21
+ * @param params.controller The controller DID for the identity.
22
+ * @param params.subjectJson The subject JSON file for the verifiable credential.
23
+ * @param params.credentialId The ID of the verifiable credential.
24
+ * @param params.expirationDate The expiration date of the verifiable credential.
25
+ * @param params.outputJson The output .json file to store the command output.
26
+ * @param params.outputEnv The output .env file to store the command output.
27
+ * @param params.outputEnvPrefix The prefix to use for variables in the output .env file.
28
+ * @returns The created verifiable credential and JWT.
29
+ */
30
+ export declare function identityVerifiableCredentialCreate(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
31
+ verificationMethodId?: string;
32
+ identity?: string;
33
+ controller?: string;
34
+ subjectJson?: string;
35
+ credentialId?: string;
36
+ expirationDate?: string;
37
+ outputJson?: string;
38
+ outputEnv?: string;
39
+ outputEnvPrefix?: string;
40
+ }): Promise<{
41
+ verifiableCredential: IDidVerifiableCredential;
42
+ jwt: string;
43
+ }>;
@@ -0,0 +1,47 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import { DidVerificationMethodType } from "@twin.org/standards-w3c-did";
3
+ import { type IJwk } from "@twin.org/web";
4
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
5
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
6
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
7
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
8
+ /**
9
+ * Get the command definition parameters.
10
+ * @param commandDefinitions The registered command definitions.
11
+ */
12
+ export declare function getCommandDefinitionIdentityVerificationMethodCreate(commandDefinitions: {
13
+ [id: string]: ICliCommandDefinition;
14
+ }): void;
15
+ /**
16
+ * Command for creating an identity verification method.
17
+ * @param engineCore The engine core.
18
+ * @param envVars The environment variables for the node.
19
+ * @param params The parameters for the command.
20
+ * @param params.identity The DID of the identity to create.
21
+ * @param params.verificationMethodType The type of verification method to create.
22
+ * @param params.verificationMethodId The ID of the verification method to create.
23
+ * @param params.controller The controller DID for the identity.
24
+ * @param params.overwriteMode The mode to use when a verification method with the same ID already exists.
25
+ * @param params.outputJson The output .json file to store the command output.
26
+ * @param params.outputEnv The output .env file to store the command output.
27
+ * @param params.outputEnvPrefix The prefix to use for variables in the output .env file.
28
+ * @returns The created verification method details or undefined if skipped.
29
+ */
30
+ export declare function identityVerificationMethodCreate(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
31
+ identity?: string;
32
+ verificationMethodType?: DidVerificationMethodType;
33
+ verificationMethodId?: string;
34
+ controller?: string;
35
+ overwriteMode?: "skip" | "overwrite" | "error";
36
+ outputJson?: string;
37
+ outputEnv?: string;
38
+ outputEnvPrefix?: string;
39
+ }): Promise<{
40
+ verificationMethodId: string;
41
+ verificationMethodType: string;
42
+ privateKeyJwk: IJwk;
43
+ privateKeyHex: string;
44
+ publicKeyHex: string;
45
+ privateKeyBase64: string;
46
+ publicKeyBase64: string;
47
+ } | undefined>;
@@ -0,0 +1,31 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import { DidVerificationMethodType } from "@twin.org/standards-w3c-did";
3
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
4
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
5
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
6
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
7
+ /**
8
+ * Get the command definition parameters.
9
+ * @param commandDefinitions The registered command definitions.
10
+ */
11
+ export declare function getCommandDefinitionIdentityVerificationMethodImport(commandDefinitions: {
12
+ [id: string]: ICliCommandDefinition;
13
+ }): void;
14
+ /**
15
+ * Command for creating an identity verification method.
16
+ * @param engineCore The engine core.
17
+ * @param envVars The environment variables for the node.
18
+ * @param params The parameters for the command.
19
+ * @param params.identity The DID of the identity to create.
20
+ * @param params.verificationMethodType The type of verification method to create.
21
+ * @param params.verificationMethodId The ID of the verification method to create.
22
+ * @param params.controller The controller DID for the identity.
23
+ * @param params.privateKeyHex The private key in hex format.
24
+ */
25
+ export declare function identityVerificationMethodImport(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
26
+ identity?: string;
27
+ verificationMethodType?: DidVerificationMethodType;
28
+ verificationMethodId?: string;
29
+ controller?: string;
30
+ privateKeyHex?: string;
31
+ }): Promise<void>;
@@ -0,0 +1,22 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
4
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
5
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
6
+ /**
7
+ * Get the command definition parameters.
8
+ * @param commandDefinitions The registered command definitions.
9
+ */
10
+ export declare function getCommandDefinitionNodeSetIdentity(commandDefinitions: {
11
+ [id: string]: ICliCommandDefinition;
12
+ }): void;
13
+ /**
14
+ * Command for setting a node identity.
15
+ * @param engineCore The engine core.
16
+ * @param envVars The environment variables for the node.
17
+ * @param params The parameters for the command.
18
+ * @param params.identity The DID to set for the node.
19
+ */
20
+ export declare function nodeSetIdentity(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
21
+ identity?: string;
22
+ }): Promise<void>;
@@ -0,0 +1,22 @@
1
+ import type { IEngineCore } from "@twin.org/engine-models";
2
+ import type { ICliCommandDefinition } from "../models/ICliCommandDefinition.js";
3
+ import type { INodeEngineConfig } from "../models/INodeEngineConfig.js";
4
+ import type { INodeEngineState } from "../models/INodeEngineState.js";
5
+ import type { INodeEnvironmentVariables } from "../models/INodeEnvironmentVariables.js";
6
+ /**
7
+ * Get the command definition parameters.
8
+ * @param commandDefinitions The registered command definitions.
9
+ */
10
+ export declare function getCommandDefinitionNodeSetTenant(commandDefinitions: {
11
+ [id: string]: ICliCommandDefinition;
12
+ }): void;
13
+ /**
14
+ * Command for setting a node tenant.
15
+ * @param engineCore The engine core.
16
+ * @param envVars The environment variables for the node.
17
+ * @param params The parameters for the command.
18
+ * @param params.tenantId The tenant id to set for the node.
19
+ */
20
+ export declare function nodeSetTenant(engineCore: IEngineCore<INodeEngineConfig, INodeEngineState>, envVars: INodeEnvironmentVariables, params: {
21
+ tenantId?: string;
22
+ }): Promise<void>;