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

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