@naylence/runtime 0.3.5-test.6 → 0.3.5-test.7

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.
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@naylence/core');
4
+ var zod = require('zod');
4
5
  var factory = require('@naylence/factory');
5
6
  var express = require('express');
6
7
  var sha2_js = require('@noble/hashes/sha2.js');
7
8
  var fastify = require('fastify');
8
9
  var websocketPlugin = require('@fastify/websocket');
9
10
  var fs = require('fs');
10
- var zod = require('zod');
11
11
  var yaml = require('yaml');
12
12
  var utils_js = require('@noble/hashes/utils.js');
13
13
  var fs$1 = require('node:fs');
@@ -746,7 +746,7 @@ function coerceNumber$1(value) {
746
746
  }
747
747
  return undefined;
748
748
  }
749
- function coerceBoolean(value) {
749
+ function coerceBoolean$1(value) {
750
750
  if (typeof value === 'boolean') {
751
751
  return value;
752
752
  }
@@ -770,7 +770,7 @@ function normalizeTaskSpawnerConfig(config = {}) {
770
770
  0;
771
771
  const defaultTimeout = coerceNumber$1(firstDefined(source, ['defaultTimeout', 'default_timeout'])) ??
772
772
  0;
773
- const autoCleanup = coerceBoolean(firstDefined(source, ['autoCleanup', 'auto_cleanup'])) ??
773
+ const autoCleanup = coerceBoolean$1(firstDefined(source, ['autoCleanup', 'auto_cleanup'])) ??
774
774
  true;
775
775
  return {
776
776
  maxConcurrent,
@@ -793,7 +793,7 @@ function normalizeSpawnOptions(options = {}) {
793
793
  function normalizeShutdownOptions(options = {}) {
794
794
  const source = options;
795
795
  const gracePeriod = coerceNumber$1(firstDefined(source, ['gracePeriod', 'grace_period'])) ?? 2000;
796
- const cancelHanging = coerceBoolean(firstDefined(source, ['cancelHanging', 'cancel_hanging'])) ??
796
+ const cancelHanging = coerceBoolean$1(firstDefined(source, ['cancelHanging', 'cancel_hanging'])) ??
797
797
  true;
798
798
  const joinTimeout = coerceNumber$1(firstDefined(source, ['joinTimeout', 'join_timeout'])) ?? 1000;
799
799
  return {
@@ -4381,8 +4381,46 @@ class StorageProviderFactory extends factory.AbstractResourceFactory {
4381
4381
  return instance;
4382
4382
  }
4383
4383
  }
4384
+ function registerStorageProviderFactory(type, factory$1) {
4385
+ factory.registerFactory(STORAGE_PROVIDER_FACTORY_BASE_TYPE, type, factory$1);
4386
+ }
4387
+
4388
+ const inMemoryStorageProviderConfigSchema = zod.z
4389
+ .object({
4390
+ type: zod.z
4391
+ .literal('InMemoryStorageProvider')
4392
+ .default('InMemoryStorageProvider'),
4393
+ })
4394
+ .passthrough();
4395
+ class InMemoryStorageProviderFactory extends StorageProviderFactory {
4396
+ constructor() {
4397
+ super(...arguments);
4398
+ this.type = 'InMemoryStorageProvider';
4399
+ }
4400
+ async create(config) {
4401
+ // Validate configuration and ensure correct type information
4402
+ const candidate = config ?? { type: 'InMemoryStorageProvider' };
4403
+ inMemoryStorageProviderConfigSchema.parse({
4404
+ type: candidate.type,
4405
+ ...candidate,
4406
+ });
4407
+ return new InMemoryStorageProvider();
4408
+ }
4409
+ }
4410
+ registerStorageProviderFactory('InMemoryStorageProvider', InMemoryStorageProviderFactory);
4384
4411
 
4385
4412
  const CREDENTIAL_PROVIDER_FACTORY_BASE_TYPE = 'CredentialProviderFactory';
4413
+ class CredentialProviderFactory extends factory.AbstractResourceFactory {
4414
+ static async createCredentialProvider(config, options = {}) {
4415
+ const instance = config
4416
+ ? await factory.createResource(CREDENTIAL_PROVIDER_FACTORY_BASE_TYPE, config, options)
4417
+ : await factory.createDefaultResource(CREDENTIAL_PROVIDER_FACTORY_BASE_TYPE, null, options);
4418
+ if (!instance) {
4419
+ throw new Error('Failed to create credential provider from configuration');
4420
+ }
4421
+ return instance;
4422
+ }
4423
+ }
4386
4424
 
4387
4425
  function readEnvironmentVariable(name) {
4388
4426
  if (!name) {
@@ -4695,8 +4733,9 @@ function isModuleNotFoundError(error) {
4695
4733
  * Wraps a dynamic import loader and enriches "module not found" failures with an actionable error message.
4696
4734
  */
4697
4735
  async function safeImport(loader, dependencyNameOrOptions, maybeOptions) {
4698
- const options = { dependencyName: dependencyNameOrOptions, ...(maybeOptions ?? {}) }
4699
- ;
4736
+ const options = typeof dependencyNameOrOptions === 'string'
4737
+ ? { dependencyName: dependencyNameOrOptions, ...(maybeOptions ?? {}) }
4738
+ : dependencyNameOrOptions;
4700
4739
  const dependencyName = options.dependencyName;
4701
4740
  try {
4702
4741
  return await loader();
@@ -4718,6 +4757,205 @@ async function safeImport(loader, dependencyNameOrOptions, maybeOptions) {
4718
4757
  }
4719
4758
  }
4720
4759
 
4760
+ const indexedDBConfigSchema = zod.z
4761
+ .object({
4762
+ type: zod.z
4763
+ .literal('IndexedDBStorageProvider')
4764
+ .default('IndexedDBStorageProvider'),
4765
+ mode: zod.z
4766
+ .union([zod.z.literal('dx'), zod.z.literal('hardened'), zod.z.string()])
4767
+ .optional()
4768
+ .default('dx'),
4769
+ dbName: zod.z.string().min(1).default('naylence'),
4770
+ version: zod.z.union([zod.z.number().int().positive(), zod.z.string()]).default(1),
4771
+ namespacePrefix: zod.z.string().optional().default('kv'),
4772
+ enableCaching: zod.z.union([zod.z.boolean(), zod.z.string()]).optional(),
4773
+ isEncrypted: zod.z.union([zod.z.boolean(), zod.z.string()]).optional(),
4774
+ masterKey: zod.z
4775
+ .union([zod.z.string(), zod.z.record(zod.z.string(), zod.z.unknown()), zod.z.null()])
4776
+ .optional()
4777
+ .default(null),
4778
+ })
4779
+ .passthrough();
4780
+ const TRUE_VALUES = new Set(['true', '1', 'yes', 'on']);
4781
+ const FALSE_VALUES = new Set(['false', '0', 'no', 'off', '']);
4782
+ function coerceBoolean(value, fieldName, defaultValue) {
4783
+ if (value === undefined) {
4784
+ return defaultValue;
4785
+ }
4786
+ if (typeof value === 'boolean') {
4787
+ return value;
4788
+ }
4789
+ if (typeof value === 'string') {
4790
+ const normalized = value.trim().toLowerCase();
4791
+ if (TRUE_VALUES.has(normalized)) {
4792
+ return true;
4793
+ }
4794
+ if (FALSE_VALUES.has(normalized)) {
4795
+ return false;
4796
+ }
4797
+ }
4798
+ throw new Error(`Expected a boolean-like value for '${fieldName}' but received '${String(value)}'`);
4799
+ }
4800
+ function normalizeIndexedDBConfig(config) {
4801
+ const candidate = {
4802
+ ...config,
4803
+ };
4804
+ if (candidate.dbName === undefined && typeof candidate.db_name === 'string') {
4805
+ candidate.dbName = candidate.db_name;
4806
+ }
4807
+ if (candidate.namespacePrefix === undefined &&
4808
+ typeof candidate.namespace_prefix === 'string') {
4809
+ candidate.namespacePrefix = candidate.namespace_prefix;
4810
+ }
4811
+ if (candidate.enableCaching === undefined &&
4812
+ candidate.enable_caching !== undefined) {
4813
+ candidate.enableCaching = candidate.enable_caching;
4814
+ }
4815
+ if (candidate.isEncrypted === undefined &&
4816
+ candidate.is_encrypted !== undefined) {
4817
+ candidate.isEncrypted = candidate.is_encrypted;
4818
+ }
4819
+ if (candidate.masterKey === undefined && candidate.master_key !== undefined) {
4820
+ candidate.masterKey = candidate.master_key;
4821
+ }
4822
+ const parsed = indexedDBConfigSchema.parse({
4823
+ ...candidate,
4824
+ type: 'IndexedDBStorageProvider',
4825
+ });
4826
+ const normalizedMode = (typeof parsed.mode === 'string' ? parsed.mode : 'dx').toLowerCase();
4827
+ if (normalizedMode !== 'dx' && normalizedMode !== 'hardened') {
4828
+ throw new Error("mode must be either 'dx' or 'hardened'");
4829
+ }
4830
+ const versionValue = typeof parsed.version === 'string'
4831
+ ? parseInt(parsed.version, 10)
4832
+ : parsed.version;
4833
+ if (!Number.isInteger(versionValue) || versionValue <= 0) {
4834
+ throw new Error('version must be a positive integer');
4835
+ }
4836
+ const enableCaching = coerceBoolean(parsed.enableCaching, 'enableCaching', normalizedMode === 'dx');
4837
+ const isEncrypted = coerceBoolean(parsed.isEncrypted, 'isEncrypted', true);
4838
+ let masterKeyConfig = null;
4839
+ if (parsed.masterKey !== null && parsed.masterKey !== undefined) {
4840
+ masterKeyConfig = SecretSource.normalize(parsed.masterKey);
4841
+ }
4842
+ if (normalizedMode === 'hardened' && !masterKeyConfig) {
4843
+ throw new Error('hardened mode requires a masterKey configuration');
4844
+ }
4845
+ return {
4846
+ type: 'IndexedDBStorageProvider',
4847
+ mode: normalizedMode,
4848
+ dbName: parsed.dbName,
4849
+ version: versionValue,
4850
+ namespacePrefix: parsed.namespacePrefix ?? 'kv',
4851
+ enableCaching,
4852
+ isEncrypted,
4853
+ masterKey: masterKeyConfig,
4854
+ };
4855
+ }
4856
+ class IndexedDBStorageProviderFactory extends StorageProviderFactory {
4857
+ constructor() {
4858
+ super(...arguments);
4859
+ this.type = 'IndexedDBStorageProvider';
4860
+ this.isDefault = true;
4861
+ this.priority = 50;
4862
+ }
4863
+ async create(config) {
4864
+ const normalized = normalizeIndexedDBConfig(config);
4865
+ const { IndexedDBStorageProvider } = await getIndexedDbStorageProviderModule();
4866
+ let masterKeyProvider = null;
4867
+ if (normalized.masterKey) {
4868
+ masterKeyProvider =
4869
+ await CredentialProviderFactory.createCredentialProvider(normalized.masterKey);
4870
+ }
4871
+ return new IndexedDBStorageProvider({
4872
+ mode: normalized.mode,
4873
+ dbName: normalized.dbName,
4874
+ version: normalized.version,
4875
+ namespacePrefix: normalized.namespacePrefix,
4876
+ enableCaching: normalized.enableCaching,
4877
+ isEncrypted: normalized.isEncrypted,
4878
+ masterKeyProvider,
4879
+ });
4880
+ }
4881
+ }
4882
+ let indexedDbStorageProviderModulePromise = null;
4883
+ function getIndexedDbStorageProviderModule() {
4884
+ if (!indexedDbStorageProviderModulePromise) {
4885
+ indexedDbStorageProviderModulePromise = safeImport(() => Promise.resolve().then(function () { return indexeddbStorageProvider; }), 'IndexedDBStorageProvider', {
4886
+ helpMessage: 'Failed to load the IndexedDB storage provider. Ensure IndexedDB is available or install the required polyfills.',
4887
+ });
4888
+ }
4889
+ return indexedDbStorageProviderModulePromise;
4890
+ }
4891
+ registerStorageProviderFactory('IndexedDBStorageProvider', IndexedDBStorageProviderFactory);
4892
+
4893
+ const ENV_VAR_STORAGE_DB_DIRECTORY = 'FAME_STORAGE_DB_DIRECTORY';
4894
+ const ENV_VAR_STORAGE_MASTER_KEY = 'FAME_STORAGE_MASTER_KEY';
4895
+ const ENV_VAR_STORAGE_ENCRYPTED = 'FAME_STORAGE_ENCRYPTED';
4896
+ const PROFILE_NAME_MEMORY = 'memory';
4897
+ const PROFILE_NAME_SQLITE = 'sqlite';
4898
+ const PROFILE_NAME_ENCRYPTED_SQLITE = 'encrypted-sqlite';
4899
+ const storageProfileSchema = zod.z
4900
+ .object({
4901
+ type: zod.z.literal('StorageProfile').default('StorageProfile'),
4902
+ profile: zod.z
4903
+ .string()
4904
+ .optional()
4905
+ .describe('Storage profile name (memory | sqlite | encrypted-sqlite)'),
4906
+ })
4907
+ .passthrough();
4908
+ const MEMORY_PROFILE_CONFIG = {
4909
+ type: 'InMemoryStorageProvider',
4910
+ };
4911
+ const SQLITE_PROFILE_CONFIG = {
4912
+ type: 'SQLiteStorageProvider',
4913
+ dbDirectory: factory.Expressions.env(ENV_VAR_STORAGE_DB_DIRECTORY, './data/sqlite'),
4914
+ isEncrypted: factory.Expressions.env(ENV_VAR_STORAGE_ENCRYPTED, 'false'),
4915
+ masterKey: factory.Expressions.env(ENV_VAR_STORAGE_MASTER_KEY, ''),
4916
+ isCached: true,
4917
+ };
4918
+ const ENCRYPTED_SQLITE_PROFILE_CONFIG = {
4919
+ type: 'SQLiteStorageProvider',
4920
+ dbDirectory: factory.Expressions.env(ENV_VAR_STORAGE_DB_DIRECTORY, './data/sqlite'),
4921
+ isEncrypted: 'true',
4922
+ masterKey: factory.Expressions.env(ENV_VAR_STORAGE_MASTER_KEY),
4923
+ isCached: true,
4924
+ };
4925
+ const PROFILE_MAP$1 = {
4926
+ [PROFILE_NAME_MEMORY]: MEMORY_PROFILE_CONFIG,
4927
+ [PROFILE_NAME_SQLITE]: SQLITE_PROFILE_CONFIG,
4928
+ [PROFILE_NAME_ENCRYPTED_SQLITE]: ENCRYPTED_SQLITE_PROFILE_CONFIG,
4929
+ };
4930
+ class StorageProfileFactory extends StorageProviderFactory {
4931
+ constructor() {
4932
+ super(...arguments);
4933
+ this.type = 'StorageProfile';
4934
+ }
4935
+ async create(config, options) {
4936
+ const candidate = config ?? { type: 'StorageProfile' };
4937
+ const parsed = storageProfileSchema.parse({
4938
+ ...candidate,
4939
+ type: 'StorageProfile',
4940
+ });
4941
+ const profileName = (parsed.profile ?? PROFILE_NAME_MEMORY).toLowerCase();
4942
+ const profileConfig = PROFILE_MAP$1[profileName];
4943
+ if (!profileConfig) {
4944
+ throw new Error(`Unknown storage profile '${profileName}'. Supported profiles: ${Object.keys(PROFILE_MAP$1).join(', ')}`);
4945
+ }
4946
+ const createOptions = {
4947
+ ...options,
4948
+ validate: options?.validate ?? true,
4949
+ };
4950
+ const provider = await factory.createResource(STORAGE_PROVIDER_FACTORY_BASE_TYPE, profileConfig, createOptions);
4951
+ if (!provider) {
4952
+ throw new Error(`Failed to create storage provider for profile '${profileName}'`);
4953
+ }
4954
+ return provider;
4955
+ }
4956
+ }
4957
+ registerStorageProviderFactory('StorageProfile', StorageProfileFactory);
4958
+
4721
4959
  const DEFAULT_DB_NAME$3 = 'naylence-secrets';
4722
4960
  const DEFAULT_STORE_NAME$1 = 'auto-master-key';
4723
4961
  const DEFAULT_KEY_ID$2 = 'master';
@@ -5466,6 +5704,11 @@ class IndexedDBStorageProvider extends EncryptedStorageProviderBase {
5466
5704
  }
5467
5705
  IndexedDBStorageProvider.supportedModes = new Set(['dx', 'hardened']);
5468
5706
 
5707
+ var indexeddbStorageProvider = /*#__PURE__*/Object.freeze({
5708
+ __proto__: null,
5709
+ IndexedDBStorageProvider: IndexedDBStorageProvider
5710
+ });
5711
+
5469
5712
  const globalScope$1 = globalThis;
5470
5713
  const nodeStack = globalScope$1.__naylenceNodeStack__ ?? (globalScope$1.__naylenceNodeStack__ = []);
5471
5714
  function getCurrentNode() {