@peers-app/peers-sdk 0.9.1 → 0.9.3

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.
@@ -484,7 +484,7 @@ function sqlValueToJsValue(value, fieldType, isArray) {
484
484
  if (value === undefined)
485
485
  return undefined;
486
486
  if (isArray) {
487
- return JSON.parse(value);
487
+ return (0, serial_json_1.fromJSONString)(value);
488
488
  }
489
489
  if (fieldType === 'Date') {
490
490
  return new Date(value);
@@ -7,6 +7,19 @@ const table_1 = require("./table");
7
7
  const table_definitions_system_1 = require("./table-definitions.system");
8
8
  const utils_1 = require("../../utils");
9
9
  const rpc_types_1 = require("../../rpc-types");
10
+ /**
11
+ * Normalize metaData for structural comparison by stripping fields that can differ
12
+ * between in-memory and DB-serialized representations without representing a real schema change.
13
+ * Specifically, `defaultValue` contains functions that the DB layer serializes as "__FUNCTION ..."
14
+ * strings (via serial-json.ts toJSON), causing false hash mismatches on every load.
15
+ * `defaultValue` is not part of the SQLite column schema so excluding it is semantically correct.
16
+ */
17
+ function normalizeMetaDataForComparison(metaData) {
18
+ return {
19
+ ...metaData,
20
+ fields: metaData.fields.map(({ defaultValue: _omit, ...rest }) => rest),
21
+ };
22
+ }
10
23
  /**
11
24
  * Compare two table definition versions and decide whether to update.
12
25
  * @returns 'update' when incoming version is strictly newer, 'skip' otherwise.
@@ -17,7 +30,7 @@ function checkVersionedUpdate(existingVersion, incomingVersion, existingMetaData
17
30
  return 'update';
18
31
  }
19
32
  if (existingVersion === incomingVersion) {
20
- const sameDefinition = (0, utils_1.simpleObjectHash)(existingMetaData) === (0, utils_1.simpleObjectHash)(incomingMetaData);
33
+ const sameDefinition = (0, utils_1.simpleObjectHash)(normalizeMetaDataForComparison(existingMetaData)) === (0, utils_1.simpleObjectHash)(normalizeMetaDataForComparison(incomingMetaData));
21
34
  if (!sameDefinition) {
22
35
  const tableName = (0, utils_1.getFullTableName)(existingMetaData);
23
36
  console.error(`Table "${tableName}" has two different definitions with the same versionNumber (${existingMetaData.versionNumber}). Skipping update. Increment versionNumber when changing table definitions.`);
@@ -90,4 +90,5 @@ export declare class PackagesTable extends Table<IPackage> {
90
90
  export declare function Packages(dataContext?: DataContext): PackagesTable;
91
91
  export declare const packagesRootDir: import("./persistent-vars").PersistentVar<string>;
92
92
  export declare const reloadPackagesOnPageRefresh: import("./persistent-vars").PersistentVar<boolean>;
93
+ export declare const autoUpdatePeersCore: import("./persistent-vars").PersistentVar<boolean>;
93
94
  export {};
@@ -34,7 +34,7 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
34
34
  done = true;
35
35
  };
36
36
  Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.reloadPackagesOnPageRefresh = exports.packagesRootDir = exports.PackagesTable = void 0;
37
+ exports.autoUpdatePeersCore = exports.reloadPackagesOnPageRefresh = exports.packagesRootDir = exports.PackagesTable = void 0;
38
38
  exports.Packages = Packages;
39
39
  const zod_1 = require("zod");
40
40
  const context_1 = require("../context");
@@ -138,3 +138,4 @@ function Packages(dataContext) {
138
138
  }
139
139
  exports.packagesRootDir = (0, persistent_vars_1.deviceVar)('packagesRootDir', { defaultValue: '~/peers-packages' });
140
140
  exports.reloadPackagesOnPageRefresh = (0, persistent_vars_1.deviceVar)('reloadPackagesOnPageRefresh', { defaultValue: true, });
141
+ exports.autoUpdatePeersCore = (0, persistent_vars_1.deviceVar)('autoUpdatePeersCore', { defaultValue: true });
@@ -14,5 +14,7 @@ export declare class PackageLoader {
14
14
  loadPackage(pkg: IPackage, opts?: {
15
15
  force?: boolean;
16
16
  }): Promise<IPeersPackage | undefined>;
17
+ private _readLocalBundle;
18
+ private _evaluateBundle;
17
19
  }
18
20
  export declare function setDefaultRequire(require: (<T>(module: string) => T)): void;
@@ -28,7 +28,6 @@ class PackageLoader {
28
28
  if (this.packageInstances[pkg.packageId] && !opts?.force) {
29
29
  return this.packageInstances[pkg.packageId];
30
30
  }
31
- // console.debug(`Loading package bundle for ${pkg.name} (${pkg.packageId})`);
32
31
  try {
33
32
  let bundleCode = '';
34
33
  if (pkg.packageBundleFileId) {
@@ -39,63 +38,86 @@ class PackageLoader {
39
38
  }
40
39
  else {
41
40
  console.warn(`Package bundle file not found for ${pkg.name} (fileId: ${pkg.packageBundleFileId})`);
42
- return;
43
41
  }
44
42
  }
45
43
  else {
46
44
  console.warn(`Package ${pkg.name} does not have a bundle file defined.`);
47
45
  }
48
- // Create a safe execution context
49
- const moduleExports = {};
50
- const module = { exports: moduleExports };
51
- // Create a custom require function that provides necessary dependencies
52
- const customRequire = (moduleId) => {
53
- // const require = getRequire(this.require);
54
- const _require = this.require ?? defaultRequire ?? typeof require === 'function' ? require : undefined;
55
- if (!_require) {
56
- throw new Error('You must set the `require` function in package loader before loading a package');
46
+ // Fallback: try loading from local path if stored bundle is not available
47
+ if (!bundleCode && pkg.localPath) {
48
+ bundleCode = this._readLocalBundle(pkg) ?? '';
49
+ }
50
+ if (!bundleCode)
51
+ return;
52
+ return this._evaluateBundle(pkg, bundleCode);
53
+ }
54
+ catch (err) {
55
+ // If stored bundle failed to evaluate, try local path as fallback
56
+ if (pkg.localPath) {
57
+ console.warn(`[PackageLoader] Stored bundle failed for ${pkg.name}, trying local path fallback`, err.message);
58
+ try {
59
+ const localBundleCode = this._readLocalBundle(pkg);
60
+ if (localBundleCode) {
61
+ return this._evaluateBundle(pkg, localBundleCode);
62
+ }
57
63
  }
58
- // console.debug(`Custom require for module: ${moduleId}`);
59
- switch (moduleId) {
60
- case 'peers-sdk':
61
- case 'PeersSDK':
62
- // Import all peers-sdk exports
63
- // return _require('peers-sdk');
64
- return PackageLoader.PeersSDK || _require('../index');
65
- case 'zod':
66
- return PackageLoader.Zod || _require('zod');
67
- default:
68
- // For other modules, use the standard require
69
- console.warn(`Package ${pkg.name} is requiring a module ${moduleId}, which is not provided by the package loader.`);
70
- return _require(moduleId);
64
+ catch (localErr) {
65
+ console.debug(`Could not load local bundle for ${pkg.name}`, localErr);
71
66
  }
72
- };
73
- // Evaluate the bundle using Function (safer than eval)
74
- const bundleFunction = new Function('module', 'exports', 'require', bundleCode);
75
- bundleFunction(module, moduleExports, customRequire);
76
- // Extract the package instance from the bundle exports
77
- const bundleExports = module.exports;
78
- const packageInstance = bundleExports?.exports || bundleExports?.package || bundleExports?.default || bundleExports;
79
- this.packageInstances[pkg.packageId] = packageInstance;
80
- if (packageInstance && typeof packageInstance === 'object') {
81
- // Register tools centrally using the tools-factory
82
- packageInstance.toolInstances?.forEach((toolInstance) => {
83
- // Note that this isn't unregistering prior tools when force loading
84
- (0, tools_1.registerTool)(toolInstance);
85
- (0, tools_2.Tools)(this.dataContext).save(toolInstance.tool);
86
- });
87
- packageInstance.tableDefinitions?.forEach((tableDefinition) => {
88
- this.dataContext.tableContainer.registerTableDefinition(tableDefinition, { overwrite: true });
89
- });
90
- return packageInstance;
91
67
  }
68
+ console.debug(`Could not load package bundle for ${pkg.name}`, err);
92
69
  return;
93
70
  }
94
- catch (err) {
95
- console.debug(`Could not load package bundle for ${pkg.name}`, err);
71
+ }
72
+ _readLocalBundle(pkg) {
73
+ try {
74
+ const _require = this.require ?? defaultRequire ?? (typeof require === 'function' ? require : null);
75
+ if (!_require || !pkg.localPath)
76
+ return;
77
+ const fs = _require('fs');
78
+ const path = _require('path');
79
+ return fs.readFileSync(path.join(pkg.localPath, 'dist', 'package.bundle.js'), 'utf8');
80
+ }
81
+ catch {
96
82
  return;
97
83
  }
98
84
  }
85
+ _evaluateBundle(pkg, bundleCode) {
86
+ const moduleExports = {};
87
+ const module = { exports: moduleExports };
88
+ const customRequire = (moduleId) => {
89
+ const _require = this.require ?? defaultRequire ?? typeof require === 'function' ? require : undefined;
90
+ if (!_require) {
91
+ throw new Error('You must set the `require` function in package loader before loading a package');
92
+ }
93
+ switch (moduleId) {
94
+ case 'peers-sdk':
95
+ case 'PeersSDK':
96
+ return PackageLoader.PeersSDK || _require('../index');
97
+ case 'zod':
98
+ return PackageLoader.Zod || _require('zod');
99
+ default:
100
+ console.warn(`Package ${pkg.name} is requiring a module ${moduleId}, which is not provided by the package loader.`);
101
+ return _require(moduleId);
102
+ }
103
+ };
104
+ const bundleFunction = new Function('module', 'exports', 'require', bundleCode);
105
+ bundleFunction(module, moduleExports, customRequire);
106
+ const bundleExports = module.exports;
107
+ const packageInstance = bundleExports?.exports || bundleExports?.package || bundleExports?.default || bundleExports;
108
+ this.packageInstances[pkg.packageId] = packageInstance;
109
+ if (packageInstance && typeof packageInstance === 'object') {
110
+ packageInstance.toolInstances?.forEach((toolInstance) => {
111
+ (0, tools_1.registerTool)(toolInstance);
112
+ (0, tools_2.Tools)(this.dataContext).save(toolInstance.tool);
113
+ });
114
+ packageInstance.tableDefinitions?.forEach((tableDefinition) => {
115
+ this.dataContext.tableContainer.registerTableDefinition(tableDefinition, { overwrite: true });
116
+ });
117
+ return packageInstance;
118
+ }
119
+ return;
120
+ }
99
121
  }
100
122
  exports.PackageLoader = PackageLoader;
101
123
  let defaultRequire = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"