@coasys/ad4m 0.11.2-dev-sdna-only-prolog → 0.11.2-dev.2
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.
- package/lib/index.cjs +232 -164
- package/lib/index.js +232 -165
- package/lib/src/model/Ad4mModel.js +36 -41
- package/lib/src/model/Subject.js +2 -47
- package/lib/src/perspectives/PerspectiveProxy.d.ts +20 -1
- package/lib/src/perspectives/PerspectiveProxy.js +143 -54
- package/lib/src/utils.d.ts +23 -0
- package/lib/src/utils.js +31 -0
- package/package.json +9 -14
- package/src/model/Ad4mModel.ts +37 -42
- package/src/model/Subject.ts +2 -43
- package/src/perspectives/PerspectiveProxy.ts +163 -57
- package/src/utils.ts +32 -0
|
@@ -14,6 +14,7 @@ import { LinkQuery } from "./LinkQuery";
|
|
|
14
14
|
import { Literal } from "../Literal";
|
|
15
15
|
import { Subject } from "../model/Subject";
|
|
16
16
|
import { collectionAdderToName, collectionRemoverToName, collectionSetterToName } from "../model/util";
|
|
17
|
+
import { escapeSurrealString } from "../utils";
|
|
17
18
|
/** Proxy object for a subscribed Prolog query that provides real-time updates
|
|
18
19
|
*
|
|
19
20
|
* This class handles:
|
|
@@ -358,6 +359,18 @@ export class PerspectiveProxy {
|
|
|
358
359
|
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveLinkUpdatedListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f"));
|
|
359
360
|
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveSyncStateChangeListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, "f"));
|
|
360
361
|
}
|
|
362
|
+
/**
|
|
363
|
+
* Escapes special regex characters in a string to prevent ReDoS attacks
|
|
364
|
+
* and regex injection when building dynamic regular expressions.
|
|
365
|
+
*
|
|
366
|
+
* @param str - The string to escape
|
|
367
|
+
* @returns The escaped string safe for use in RegExp constructor
|
|
368
|
+
*
|
|
369
|
+
* @private
|
|
370
|
+
*/
|
|
371
|
+
escapeRegExp(str) {
|
|
372
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
373
|
+
}
|
|
361
374
|
/**
|
|
362
375
|
* Executes a set of actions on an expression with optional parameters.
|
|
363
376
|
* Used internally by Social DNA flows and subject class operations.
|
|
@@ -961,34 +974,43 @@ export class PerspectiveProxy {
|
|
|
961
974
|
// Get metadata from SDNA using Prolog metaprogramming
|
|
962
975
|
const metadata = await this.getSubjectClassMetadataFromSDNA(className);
|
|
963
976
|
if (!metadata) {
|
|
964
|
-
|
|
977
|
+
// Fallback to Prolog check if SDNA metadata isn't available
|
|
978
|
+
// This handles cases where classes exist in Prolog but not in SDNA
|
|
979
|
+
try {
|
|
980
|
+
const escapedClassName = className.replace(/"/g, '\\"');
|
|
981
|
+
const escapedExpression = expression.replace(/"/g, '\\"');
|
|
982
|
+
const result = await this.infer(`subject_class("${escapedClassName}", C), instance(C, "${escapedExpression}")`);
|
|
983
|
+
return result && result.length > 0;
|
|
984
|
+
}
|
|
985
|
+
catch (e) {
|
|
986
|
+
console.warn(`Failed to check instance via Prolog for class ${className}:`, e);
|
|
987
|
+
return false;
|
|
988
|
+
}
|
|
965
989
|
}
|
|
966
990
|
// If no required triples, any expression with links is an instance
|
|
967
991
|
if (metadata.requiredTriples.length === 0) {
|
|
968
|
-
|
|
969
|
-
const checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${
|
|
970
|
-
console.log("checkQuery", checkQuery);
|
|
992
|
+
const escapedExpression = escapeSurrealString(expression);
|
|
993
|
+
const checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}'`;
|
|
971
994
|
const result = await this.querySurrealDB(checkQuery);
|
|
972
|
-
console.log("result", result);
|
|
973
995
|
const count = result[0]?.count ?? 0;
|
|
974
996
|
const countValue = typeof count === 'object' && count?.Int !== undefined ? count.Int : count;
|
|
975
997
|
return countValue > 0;
|
|
976
998
|
}
|
|
977
999
|
// Check if the expression has all required triples (predicate + optional exact target)
|
|
978
1000
|
for (const triple of metadata.requiredTriples) {
|
|
979
|
-
|
|
1001
|
+
const escapedExpression = escapeSurrealString(expression);
|
|
1002
|
+
const escapedPredicate = escapeSurrealString(triple.predicate);
|
|
980
1003
|
let checkQuery;
|
|
981
1004
|
if (triple.target) {
|
|
982
1005
|
// Flag: must match both predicate AND exact target value
|
|
983
|
-
|
|
1006
|
+
const escapedTarget = escapeSurrealString(triple.target);
|
|
1007
|
+
checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}' AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}'`;
|
|
984
1008
|
}
|
|
985
1009
|
else {
|
|
986
1010
|
// Property: just check predicate exists
|
|
987
|
-
checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${
|
|
1011
|
+
checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}' AND predicate = '${escapedPredicate}'`;
|
|
988
1012
|
}
|
|
989
|
-
console.log("checkQuery", checkQuery);
|
|
990
1013
|
const result = await this.querySurrealDB(checkQuery);
|
|
991
|
-
console.log("result", result);
|
|
992
1014
|
if (!result || result.length === 0) {
|
|
993
1015
|
return false;
|
|
994
1016
|
}
|
|
@@ -1028,8 +1050,9 @@ export class PerspectiveProxy {
|
|
|
1028
1050
|
async getSubjectClassMetadataFromSDNA(className) {
|
|
1029
1051
|
try {
|
|
1030
1052
|
// Get SDNA code from perspective - it's stored as a link
|
|
1053
|
+
// Use canonical Literal.from() to construct the source URL
|
|
1031
1054
|
const sdnaLinks = await this.get(new LinkQuery({
|
|
1032
|
-
source:
|
|
1055
|
+
source: Literal.from(className).toUrl(),
|
|
1033
1056
|
predicate: "ad4m://sdna"
|
|
1034
1057
|
}));
|
|
1035
1058
|
//console.log(`getSubjectClassMetadataFromSDNA: sdnaLinks for ${className}:`, sdnaLinks);
|
|
@@ -1049,10 +1072,14 @@ export class PerspectiveProxy {
|
|
|
1049
1072
|
const requiredTriples = [];
|
|
1050
1073
|
// Parse the instance rule from the SDNA code
|
|
1051
1074
|
// Format: instance(c, Base) :- triple(Base, "pred1", _), triple(Base, "pred2", "exact_value").
|
|
1052
|
-
|
|
1053
|
-
|
|
1075
|
+
// Use a more robust pattern that handles complex rule bodies
|
|
1076
|
+
// Match from "instance(" to the closing "." using non-greedy matching
|
|
1077
|
+
const instanceRulePattern = /instance\([^)]+\)\s*:-\s*([^.]+)\./g;
|
|
1078
|
+
let instanceRuleMatch;
|
|
1079
|
+
let foundInstanceRule = false;
|
|
1080
|
+
while ((instanceRuleMatch = instanceRulePattern.exec(sdnaCode)) !== null) {
|
|
1081
|
+
foundInstanceRule = true;
|
|
1054
1082
|
const ruleBody = instanceRuleMatch[1];
|
|
1055
|
-
console.log("instance rule body:", ruleBody);
|
|
1056
1083
|
// Extract all triple(Base, "predicate", Target) patterns
|
|
1057
1084
|
// Match both: triple(Base, "pred", _) and triple(Base, "pred", "value")
|
|
1058
1085
|
const tripleRegex = /triple\([^,]+,\s*"([^"]+)",\s*(?:"([^"]+)"|_)\)/g;
|
|
@@ -1061,15 +1088,13 @@ export class PerspectiveProxy {
|
|
|
1061
1088
|
const predicate = match[1];
|
|
1062
1089
|
const target = match[2]; // undefined if matched "_"
|
|
1063
1090
|
requiredTriples.push({ predicate, target });
|
|
1064
|
-
console.log(`Extracted triple: predicate="${predicate}", target="${target}"`);
|
|
1065
1091
|
}
|
|
1066
1092
|
}
|
|
1067
|
-
|
|
1093
|
+
if (!foundInstanceRule) {
|
|
1068
1094
|
console.warn(`No instance rule found in SDNA for ${className}`);
|
|
1069
1095
|
}
|
|
1070
1096
|
// For backward compatibility, also maintain requiredPredicates array
|
|
1071
1097
|
const requiredPredicates = requiredTriples.map(t => t.predicate);
|
|
1072
|
-
console.log("extracted requiredTriples:", requiredTriples);
|
|
1073
1098
|
// Extract property metadata
|
|
1074
1099
|
const properties = new Map();
|
|
1075
1100
|
const propertyResults = await this.infer(`subject_class("${className}", C), property(C, P)`);
|
|
@@ -1090,7 +1115,9 @@ export class PerspectiveProxy {
|
|
|
1090
1115
|
// If no setter, try to extract from SDNA property_getter Prolog code
|
|
1091
1116
|
if (!predicate) {
|
|
1092
1117
|
// Parse the SDNA code for property_getter definition
|
|
1093
|
-
|
|
1118
|
+
// Escape propName to prevent regex injection and ReDoS attacks
|
|
1119
|
+
const escapedPropName = this.escapeRegExp(propName);
|
|
1120
|
+
const getterMatch = sdnaCode.match(new RegExp(`property_getter\\([^,]+,\\s*[^,]+,\\s*"${escapedPropName}"[^)]*\\)\\s*:-\\s*triple\\([^,]+,\\s*"([^"]+)"`));
|
|
1094
1121
|
if (getterMatch) {
|
|
1095
1122
|
predicate = getterMatch[1];
|
|
1096
1123
|
}
|
|
@@ -1126,7 +1153,9 @@ export class PerspectiveProxy {
|
|
|
1126
1153
|
// Format 1 (findall): collection_getter(c, Base, "comments", List) :- findall(C, triple(Base, "todo://comment", C), List).
|
|
1127
1154
|
// Format 2 (setof): collection_getter(c, Base, "messages", List) :- setof(Target, (triple(Base, "flux://entry_type", Target), ...), List).
|
|
1128
1155
|
// Use a line-based match to avoid capturing multiple collections
|
|
1129
|
-
|
|
1156
|
+
// Escape collName to prevent regex injection and ReDoS attacks
|
|
1157
|
+
const escapedCollName = this.escapeRegExp(collName);
|
|
1158
|
+
const getterLinePattern = new RegExp(`collection_getter\\([^,]+,\\s*[^,]+,\\s*"${escapedCollName}"[^)]*\\)\\s*:-[^.]+\\.`);
|
|
1130
1159
|
const getterLineMatch = sdnaCode.match(getterLinePattern);
|
|
1131
1160
|
if (getterLineMatch) {
|
|
1132
1161
|
const getterLine = getterLineMatch[0];
|
|
@@ -1140,7 +1169,6 @@ export class PerspectiveProxy {
|
|
|
1140
1169
|
if (getterBody.startsWith('(') && getterBody.endsWith(')')) {
|
|
1141
1170
|
getterBody = getterBody.substring(1, getterBody.length - 1);
|
|
1142
1171
|
}
|
|
1143
|
-
console.log(`Collection "${collName}" getter body:`, getterBody);
|
|
1144
1172
|
// Extract predicate from triple(Base, "predicate", Target)
|
|
1145
1173
|
if (!predicate) {
|
|
1146
1174
|
const tripleMatch = getterBody.match(/triple\([^,]+,\s*"([^"]+)"/);
|
|
@@ -1152,7 +1180,6 @@ export class PerspectiveProxy {
|
|
|
1152
1180
|
const instanceMatch = getterBody.match(/subject_class\("([^"]+)"/);
|
|
1153
1181
|
if (instanceMatch) {
|
|
1154
1182
|
instanceFilter = instanceMatch[1];
|
|
1155
|
-
console.log(`Collection "${collName}" has instanceFilter: ${instanceFilter}`);
|
|
1156
1183
|
}
|
|
1157
1184
|
}
|
|
1158
1185
|
}
|
|
@@ -1173,27 +1200,24 @@ export class PerspectiveProxy {
|
|
|
1173
1200
|
* Generates a SurrealDB query to find instances based on class metadata.
|
|
1174
1201
|
*/
|
|
1175
1202
|
generateSurrealInstanceQuery(metadata) {
|
|
1176
|
-
console.log("generateSurrealInstanceQuery called with requiredTriples:", metadata.requiredTriples);
|
|
1177
1203
|
if (metadata.requiredTriples.length === 0) {
|
|
1178
1204
|
// No required triples - any node with links is an instance
|
|
1179
|
-
|
|
1180
|
-
console.log("Generated query (no required triples):", query);
|
|
1181
|
-
return query;
|
|
1205
|
+
return `SELECT DISTINCT uri AS base FROM node WHERE count(->link) > 0`;
|
|
1182
1206
|
}
|
|
1183
1207
|
// Generate WHERE conditions for each required triple (predicate + optional exact target)
|
|
1184
1208
|
const whereConditions = metadata.requiredTriples.map(triple => {
|
|
1209
|
+
const escapedPredicate = escapeSurrealString(triple.predicate);
|
|
1185
1210
|
if (triple.target) {
|
|
1186
1211
|
// Flag: must match both predicate AND exact target value
|
|
1187
|
-
|
|
1212
|
+
const escapedTarget = escapeSurrealString(triple.target);
|
|
1213
|
+
return `count(->link[WHERE predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}']) > 0`;
|
|
1188
1214
|
}
|
|
1189
1215
|
else {
|
|
1190
1216
|
// Property: just check predicate exists
|
|
1191
|
-
return `count(->link[WHERE predicate = '${
|
|
1217
|
+
return `count(->link[WHERE predicate = '${escapedPredicate}']) > 0`;
|
|
1192
1218
|
}
|
|
1193
1219
|
}).join(' AND ');
|
|
1194
|
-
|
|
1195
|
-
console.log("Generated query:", query);
|
|
1196
|
-
return query;
|
|
1220
|
+
return `SELECT uri AS base FROM node WHERE ${whereConditions}`;
|
|
1197
1221
|
}
|
|
1198
1222
|
/**
|
|
1199
1223
|
* Gets a property value using SurrealDB when Prolog fails.
|
|
@@ -1208,7 +1232,9 @@ export class PerspectiveProxy {
|
|
|
1208
1232
|
if (!propMeta) {
|
|
1209
1233
|
return undefined;
|
|
1210
1234
|
}
|
|
1211
|
-
const
|
|
1235
|
+
const escapedBaseExpression = escapeSurrealString(baseExpression);
|
|
1236
|
+
const escapedPredicate = escapeSurrealString(propMeta.predicate);
|
|
1237
|
+
const query = `SELECT out.uri AS value FROM link WHERE in.uri = '${escapedBaseExpression}' AND predicate = '${escapedPredicate}' LIMIT 1`;
|
|
1212
1238
|
const result = await this.querySurrealDB(query);
|
|
1213
1239
|
if (!result || result.length === 0) {
|
|
1214
1240
|
return undefined;
|
|
@@ -1244,28 +1270,92 @@ export class PerspectiveProxy {
|
|
|
1244
1270
|
if (!collMeta) {
|
|
1245
1271
|
return [];
|
|
1246
1272
|
}
|
|
1247
|
-
const
|
|
1273
|
+
const escapedBaseExpression = escapeSurrealString(baseExpression);
|
|
1274
|
+
const escapedPredicate = escapeSurrealString(collMeta.predicate);
|
|
1275
|
+
const query = `SELECT out.uri AS value, timestamp FROM link WHERE in.uri = '${escapedBaseExpression}' AND predicate = '${escapedPredicate}' ORDER BY timestamp ASC`;
|
|
1248
1276
|
const result = await this.querySurrealDB(query);
|
|
1249
1277
|
if (!result || result.length === 0) {
|
|
1250
1278
|
return [];
|
|
1251
1279
|
}
|
|
1252
1280
|
let values = result.map(r => r.value).filter(v => v !== "" && v !== '');
|
|
1253
|
-
|
|
1254
|
-
// Apply instance filter if present
|
|
1281
|
+
// Apply instance filter if present - batch-check all values at once
|
|
1255
1282
|
if (collMeta.instanceFilter) {
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1283
|
+
try {
|
|
1284
|
+
const filterMetadata = await this.getSubjectClassMetadataFromSDNA(collMeta.instanceFilter);
|
|
1285
|
+
if (!filterMetadata) {
|
|
1286
|
+
// Fallback to sequential checks if metadata isn't available
|
|
1287
|
+
return this.filterInstancesSequential(values, collMeta.instanceFilter);
|
|
1288
|
+
}
|
|
1289
|
+
return await this.batchCheckSubjectInstances(values, filterMetadata);
|
|
1290
|
+
}
|
|
1291
|
+
catch (err) {
|
|
1292
|
+
// Fallback to sequential checks on error
|
|
1293
|
+
return this.filterInstancesSequential(values, collMeta.instanceFilter);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
return values;
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Batch-checks multiple expressions against subject class metadata using a single or limited SurrealDB queries.
|
|
1300
|
+
* This avoids N+1 query problems by checking all values at once.
|
|
1301
|
+
*/
|
|
1302
|
+
async batchCheckSubjectInstances(expressions, metadata) {
|
|
1303
|
+
if (expressions.length === 0) {
|
|
1304
|
+
return [];
|
|
1305
|
+
}
|
|
1306
|
+
// If no required triples, check which expressions have any links
|
|
1307
|
+
if (metadata.requiredTriples.length === 0) {
|
|
1308
|
+
const escapedExpressions = expressions.map(e => `'${escapeSurrealString(e)}'`).join(', ');
|
|
1309
|
+
const checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] GROUP BY in.uri HAVING count() > 0`;
|
|
1310
|
+
const result = await this.querySurrealDB(checkQuery);
|
|
1311
|
+
return result.map(r => r.uri);
|
|
1312
|
+
}
|
|
1313
|
+
// For each required triple, build a query that finds matching expressions
|
|
1314
|
+
const validExpressionSets = [];
|
|
1315
|
+
for (const triple of metadata.requiredTriples) {
|
|
1316
|
+
const escapedExpressions = expressions.map(e => `'${escapeSurrealString(e)}'`).join(', ');
|
|
1317
|
+
const escapedPredicate = escapeSurrealString(triple.predicate);
|
|
1318
|
+
let checkQuery;
|
|
1319
|
+
if (triple.target) {
|
|
1320
|
+
// Flag: must match both predicate AND exact target value
|
|
1321
|
+
const escapedTarget = escapeSurrealString(triple.target);
|
|
1322
|
+
checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}' GROUP BY in.uri`;
|
|
1323
|
+
}
|
|
1324
|
+
else {
|
|
1325
|
+
// Property: just check predicate exists
|
|
1326
|
+
checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' GROUP BY in.uri`;
|
|
1327
|
+
}
|
|
1328
|
+
const result = await this.querySurrealDB(checkQuery);
|
|
1329
|
+
validExpressionSets.push(new Set(result.map(r => r.uri)));
|
|
1330
|
+
}
|
|
1331
|
+
// Find intersection: expressions that passed ALL required triple checks
|
|
1332
|
+
if (validExpressionSets.length === 0) {
|
|
1333
|
+
return expressions;
|
|
1334
|
+
}
|
|
1335
|
+
const firstSet = validExpressionSets[0];
|
|
1336
|
+
const validExpressions = expressions.filter(expr => {
|
|
1337
|
+
return validExpressionSets.every(set => set.has(expr));
|
|
1338
|
+
});
|
|
1339
|
+
return validExpressions;
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Fallback sequential instance checking when batch checking isn't available.
|
|
1343
|
+
*/
|
|
1344
|
+
async filterInstancesSequential(values, instanceFilter) {
|
|
1345
|
+
const filteredValues = [];
|
|
1346
|
+
for (const value of values) {
|
|
1347
|
+
try {
|
|
1348
|
+
const isInstance = await this.isSubjectInstance(value, instanceFilter);
|
|
1261
1349
|
if (isInstance) {
|
|
1262
1350
|
filteredValues.push(value);
|
|
1263
1351
|
}
|
|
1264
1352
|
}
|
|
1265
|
-
|
|
1266
|
-
|
|
1353
|
+
catch (err) {
|
|
1354
|
+
// Skip values that fail instance check
|
|
1355
|
+
continue;
|
|
1356
|
+
}
|
|
1267
1357
|
}
|
|
1268
|
-
return
|
|
1358
|
+
return filteredValues;
|
|
1269
1359
|
}
|
|
1270
1360
|
/** Returns all subject instances of the given subject class as proxy objects.
|
|
1271
1361
|
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
@@ -1310,7 +1400,7 @@ export class PerspectiveProxy {
|
|
|
1310
1400
|
//console.log(`getAllSubjectInstances: Returning ${instances.length} instances`);
|
|
1311
1401
|
return instances;
|
|
1312
1402
|
}
|
|
1313
|
-
/** Returns all subject proxies of the given subject class.
|
|
1403
|
+
/** Returns all subject proxies of the given subject class as proxy objects.
|
|
1314
1404
|
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
1315
1405
|
* with the properties of the subject class. In the latter case, all subject classes
|
|
1316
1406
|
* that match the given properties will be used.
|
|
@@ -1325,24 +1415,23 @@ export class PerspectiveProxy {
|
|
|
1325
1415
|
}
|
|
1326
1416
|
let instances = [];
|
|
1327
1417
|
for (let className of classes) {
|
|
1328
|
-
//console.log(`getAllSubjectProxies: Processing class ${className}`);
|
|
1329
1418
|
// Query SDNA for metadata, then query SurrealDB for instances
|
|
1330
1419
|
const metadata = await this.getSubjectClassMetadataFromSDNA(className);
|
|
1331
|
-
//console.log(`getAllSubjectProxies: Got metadata for ${className}`);
|
|
1332
1420
|
if (metadata) {
|
|
1333
1421
|
const surrealQuery = this.generateSurrealInstanceQuery(metadata);
|
|
1334
|
-
console.log("surrealQuery", surrealQuery);
|
|
1335
1422
|
const results = await this.querySurrealDB(surrealQuery);
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1423
|
+
for (const result of results || []) {
|
|
1424
|
+
try {
|
|
1425
|
+
let subject = new Subject(this, result.base, className);
|
|
1426
|
+
await subject.init();
|
|
1427
|
+
instances.push(subject);
|
|
1428
|
+
}
|
|
1429
|
+
catch (e) {
|
|
1430
|
+
// Skip subjects that fail to initialize
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1343
1433
|
}
|
|
1344
1434
|
}
|
|
1345
|
-
//console.log(`getAllSubjectProxies: Returning ${instances.length} proxies`);
|
|
1346
1435
|
return instances;
|
|
1347
1436
|
}
|
|
1348
1437
|
buildQueryFromTemplate(obj) {
|
package/lib/src/utils.d.ts
CHANGED
|
@@ -1,2 +1,25 @@
|
|
|
1
1
|
export declare function formatList(list: any): any;
|
|
2
2
|
export declare function capSentence(cap: any): string;
|
|
3
|
+
/**
|
|
4
|
+
* Escapes a string value for safe use in SurrealQL queries.
|
|
5
|
+
*
|
|
6
|
+
* @description
|
|
7
|
+
* Prevents SQL injection by properly escaping special characters in string values
|
|
8
|
+
* that will be interpolated into SurrealQL queries. This handles the most common
|
|
9
|
+
* special characters that could break SQL queries or enable injection attacks.
|
|
10
|
+
*
|
|
11
|
+
* Single quotes, backslashes, and other special characters are escaped using
|
|
12
|
+
* backslash notation, which is the standard escaping mechanism for SurrealQL.
|
|
13
|
+
*
|
|
14
|
+
* @param value - The string value to escape
|
|
15
|
+
* @returns The escaped string safe for SurrealQL interpolation (without surrounding quotes)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const userInput = "user's input with 'quotes'";
|
|
20
|
+
* const escaped = escapeSurrealString(userInput);
|
|
21
|
+
* const query = `SELECT * FROM link WHERE uri = '${escaped}'`;
|
|
22
|
+
* // Results in: SELECT * FROM link WHERE uri = 'user\'s input with \'quotes\''
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function escapeSurrealString(value: string): string;
|
package/lib/src/utils.js
CHANGED
|
@@ -19,3 +19,34 @@ export function capSentence(cap) {
|
|
|
19
19
|
: cap.with.pointers;
|
|
20
20
|
return `${formatList(can)} your ${domain} actions, with access to ${formatList(pointers)}`;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Escapes a string value for safe use in SurrealQL queries.
|
|
24
|
+
*
|
|
25
|
+
* @description
|
|
26
|
+
* Prevents SQL injection by properly escaping special characters in string values
|
|
27
|
+
* that will be interpolated into SurrealQL queries. This handles the most common
|
|
28
|
+
* special characters that could break SQL queries or enable injection attacks.
|
|
29
|
+
*
|
|
30
|
+
* Single quotes, backslashes, and other special characters are escaped using
|
|
31
|
+
* backslash notation, which is the standard escaping mechanism for SurrealQL.
|
|
32
|
+
*
|
|
33
|
+
* @param value - The string value to escape
|
|
34
|
+
* @returns The escaped string safe for SurrealQL interpolation (without surrounding quotes)
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const userInput = "user's input with 'quotes'";
|
|
39
|
+
* const escaped = escapeSurrealString(userInput);
|
|
40
|
+
* const query = `SELECT * FROM link WHERE uri = '${escaped}'`;
|
|
41
|
+
* // Results in: SELECT * FROM link WHERE uri = 'user\'s input with \'quotes\''
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function escapeSurrealString(value) {
|
|
45
|
+
return value
|
|
46
|
+
.replace(/\\/g, '\\\\') // Backslash -> \\
|
|
47
|
+
.replace(/'/g, "\\'") // Single quote -> \'
|
|
48
|
+
.replace(/"/g, '\\"') // Double quote -> \"
|
|
49
|
+
.replace(/\n/g, '\\n') // Newline -> \n
|
|
50
|
+
.replace(/\r/g, '\\r') // Carriage return -> \r
|
|
51
|
+
.replace(/\t/g, '\\t'); // Tab -> \t
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -5,13 +5,7 @@
|
|
|
5
5
|
"module": "lib/index.js",
|
|
6
6
|
"types": "lib/src/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
|
-
|
|
9
|
-
"scripts": {
|
|
10
|
-
"build": "patch-package && tsc && pnpm run buildSchema && pnpm run bundle",
|
|
11
|
-
"buildSchema": "node --es-module-specifier-resolution=node lib/src/buildSchema.js",
|
|
12
|
-
"bundle": "rollup -c rollup.config.js",
|
|
13
|
-
"test": "jest --forceExit"
|
|
14
|
-
},
|
|
8
|
+
"private": false,
|
|
15
9
|
"repository": {
|
|
16
10
|
"type": "git",
|
|
17
11
|
"url": "git+https://github.com/coasys/ad4m.git"
|
|
@@ -64,10 +58,11 @@
|
|
|
64
58
|
"react-dom": "*"
|
|
65
59
|
},
|
|
66
60
|
"patchedDependencies": {},
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
61
|
+
"version": "0.11.2-dev.2",
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "patch-package && tsc && pnpm run buildSchema && pnpm run bundle",
|
|
64
|
+
"buildSchema": "node --es-module-specifier-resolution=node lib/src/buildSchema.js",
|
|
65
|
+
"bundle": "rollup -c rollup.config.js",
|
|
66
|
+
"test": "jest --forceExit"
|
|
67
|
+
}
|
|
68
|
+
}
|