mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-21 18:45:03 +00:00
fix: use hasOwnProperty in Proxy get trap to avoid shadowing Object.prototype props
The 'in' operator checks the entire prototype chain, so 'constructor' in overrides would match Object.prototype.constructor and return undefined from the overrides map. Use hasOwnProperty.call() to only match own keys (doGenerate, doStream).
This commit is contained in:
parent
3b6fc72fb5
commit
ee9bfe733b
2 changed files with 24 additions and 1 deletions
|
|
@ -216,7 +216,12 @@ const wrapVercelLanguageModel = <T extends LanguageModel>(
|
|||
|
||||
const wrappedModel = new Proxy(model, {
|
||||
get(target, prop, _receiver) {
|
||||
if (typeof prop === "string" && prop in overrides) {
|
||||
// Use hasOwnProperty to avoid matching inherited Object.prototype props
|
||||
// (e.g. 'constructor', 'toString') which would shadow them on the proxy.
|
||||
if (
|
||||
typeof prop === "string" &&
|
||||
Object.prototype.hasOwnProperty.call(overrides, prop)
|
||||
) {
|
||||
return overrides[prop]
|
||||
}
|
||||
// Delegate to the original model so that `this` inside prototype
|
||||
|
|
|
|||
|
|
@ -216,6 +216,24 @@ describe("Unit: withSupermemory", () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe("Proxy override correctness", () => {
|
||||
it("should not shadow Object.prototype inherited props (e.g. constructor) with undefined overrides", () => {
|
||||
process.env.SUPERMEMORY_API_KEY = "test-key"
|
||||
|
||||
// 'constructor', 'toString', 'hasOwnProperty' etc are in Object.prototype.
|
||||
// Using `prop in overrides` (instead of hasOwnProperty) would match these
|
||||
// inherited props and return undefined from the overrides map, shadowing
|
||||
// the real implementations on the model/target.
|
||||
const mockModel = createMockV3LanguageModel()
|
||||
const wrappedModel = withSupermemory(mockModel, TEST_CONFIG.containerTag)
|
||||
|
||||
// constructor should point to the model's class, not Object
|
||||
expect((wrappedModel as object).constructor).toBe(MockLanguageModelV3)
|
||||
// toString should be callable and not undefined
|
||||
expect(typeof (wrappedModel as object).toString).toBe("function")
|
||||
})
|
||||
})
|
||||
|
||||
describe("Memory caching", () => {
|
||||
let fetchMock: ReturnType<typeof vi.fn>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue