Skip to content

Commit a1c0733

Browse files
Copilothotlong
andcommitted
Improve hash algorithm and nested object parsing
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f4630a4 commit a1c0733

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

packages/metadata/src/loaders/filesystem-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class FilesystemLoader implements MetadataLoader {
306306
* Generate ETag for content
307307
*/
308308
private generateETag(content: string): string {
309-
const hash = createHash('md5').update(content).digest('hex');
309+
const hash = createHash('sha256').update(content).digest('hex').substring(0, 32);
310310
return `"${hash}"`;
311311
}
312312
}

packages/metadata/src/metadata-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export class MetadataManager {
330330
* Generate ETag for content
331331
*/
332332
private generateETag(content: string): string {
333-
const hash = createHash('md5').update(content).digest('hex');
333+
const hash = createHash('sha256').update(content).digest('hex').substring(0, 32);
334334
return `"${hash}"`;
335335
}
336336
}

packages/metadata/src/serializers/typescript-serializer.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,54 @@ export class TypeScriptSerializer implements MetadataSerializer {
2828

2929
deserialize<T>(content: string, schema?: z.ZodSchema): T {
3030
// For TypeScript/JavaScript files, we need to extract the exported object
31-
// This implementation extracts the object literal from common export patterns
31+
// Note: This is a simplified parser that works with JSON-like object literals
32+
// For complex TypeScript with nested objects, consider using a proper TypeScript parser
3233

34+
// Try to find the object literal in various export patterns
3335
// Pattern 1: export const metadata = {...};
34-
let match = content.match(/export\s+const\s+\w+\s*=\s*({[\s\S]*?});/);
35-
36-
// Pattern 2: export default {...};
37-
if (!match) {
38-
match = content.match(/export\s+default\s+({[\s\S]*?});/);
36+
let objectStart = content.indexOf('export const');
37+
if (objectStart === -1) {
38+
// Pattern 2: export default {...};
39+
objectStart = content.indexOf('export default');
3940
}
4041

41-
if (!match) {
42+
if (objectStart === -1) {
4243
throw new Error(
4344
'Could not parse TypeScript/JavaScript module. ' +
4445
'Expected export pattern: "export const metadata = {...};" or "export default {...};"'
4546
);
4647
}
4748

49+
// Find the first opening brace after the export statement
50+
const braceStart = content.indexOf('{', objectStart);
51+
if (braceStart === -1) {
52+
throw new Error('Could not find object literal in export statement');
53+
}
54+
55+
// Find the matching closing brace by counting braces
56+
let braceCount = 0;
57+
let braceEnd = -1;
58+
for (let i = braceStart; i < content.length; i++) {
59+
if (content[i] === '{') braceCount++;
60+
if (content[i] === '}') {
61+
braceCount--;
62+
if (braceCount === 0) {
63+
braceEnd = i;
64+
break;
65+
}
66+
}
67+
}
68+
69+
if (braceEnd === -1) {
70+
throw new Error('Could not find matching closing brace for object literal');
71+
}
72+
73+
// Extract the object literal
74+
const objectLiteral = content.substring(braceStart, braceEnd + 1);
75+
4876
try {
49-
// Parse the object literal as JSON
50-
// This is safer than eval but still requires the content to be valid JSON-like syntax
51-
const parsed = JSON.parse(match[1]);
77+
// Parse as JSON
78+
const parsed = JSON.parse(objectLiteral);
5279

5380
if (schema) {
5481
return schema.parse(parsed) as T;
@@ -57,7 +84,8 @@ export class TypeScriptSerializer implements MetadataSerializer {
5784
return parsed as T;
5885
} catch (error) {
5986
throw new Error(
60-
`Failed to parse object literal: ${error instanceof Error ? error.message : String(error)}`
87+
`Failed to parse object literal as JSON: ${error instanceof Error ? error.message : String(error)}. ` +
88+
'Make sure the TypeScript/JavaScript object uses JSON-compatible syntax.'
6189
);
6290
}
6391
}

0 commit comments

Comments
 (0)