Skip to content

v4: bindMethodsToServiceLocator warps the prototype chain #3934

Description

@barjin

bindMethodsToServiceLocator shadows the overridden methods on a class instance with the parent implementations, see:

import { bindMethodsToServiceLocator, ServiceLocator } from './packages/core/src/index.js';

class Parent {
    constructor(locator: ServiceLocator) { bindMethodsToServiceLocator(locator, this); }
    public act() { console.log('Parent act() called'); }
}

class Child extends Parent {
    constructor(locator: ServiceLocator) { super(locator); }
    public override act() { console.log('Child act() called'); }
}

const c = new Child(new ServiceLocator());
c.act(); // expected "Child act() called", got "Parent act() called"

In Crawlee, this only runs when a custom service is supplied ⬇️

if (
storageBackend ||
eventManager ||
logger ||
(configuration !== undefined && configuration !== serviceLocator.getConfiguration())
) {
const scopedServiceLocator = new ServiceLocator(configuration, eventManager, storageBackend, logger);
serviceLocatorScope = bindMethodsToServiceLocator(scopedServiceLocator, this);
}

In this case, this breaks crawler instances (e.g. messing with the HTTP navigation in HttpCrawler etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions