Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Function.prototype.apply was called on null, which is a object and not a function #2349

Closed
BobNobrain opened this issue Oct 16, 2023 · 0 comments · Fixed by #2355
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@BobNobrain
Copy link
Contributor

Bug Description

Looks like the same bug as in #1327, but with stream and not request

Reproducible By

const {MockAgent, stream} = require('undici');
const {Writable} = require('stream');

async function main() {
    const mockAgent = new MockAgent();

    mockAgent.disableNetConnect();
    mockAgent
        .get('http://localhost')
        .intercept({
            path: '/'
        })
        .reply(200, 'ok')
        .delay(100);

    const parts = [];
    const ac = new AbortController();

    setTimeout(() => ac.abort('nevermind'), 50);

    try {
        await stream('http://localhost/', {
            opaque: {parts},
            signal: ac.signal,
            dispatcher: mockAgent
        }, ({opaque: {parts}}) => {
            return new Writable({
                write(chunk, _encoding, callback) {
                    parts.push(chunk);
                    callback();
                }
            });
        });

        console.log(Buffer.concat(parts).toString('utf-8'));
    } catch (error) {
        console.error('aborted', error);
    }
}

main().catch((e) => {
    console.error('fatal', e);
    process.exit(3);
});

Expected Behavior

Should only log RequestAbortedError and exit with 0

Logs & Screenshots

$ node repro.js
aborted RequestAbortedError [AbortError]: Request aborted
    at abort (project/node_modules/undici/lib/api/abort-signal.js:11:18)
    at AbortSignal.self.<computed> (project/node_modules/undici/lib/api/abort-signal.js:30:5)
    at AbortSignal.[nodejs.internal.kHybridDispatch] (node:internal/event_target:736:20)
    at AbortSignal.dispatchEvent (node:internal/event_target:678:26)
    at abortSignal (node:internal/abort_controller:292:10)
    at AbortController.abort (node:internal/abort_controller:323:5)
    at Timeout._onTimeout (project/repro.js:19:25)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'UND_ERR_ABORTED'
}
node:async_hooks:203
        ReflectApply(fn, thisArg, args);
        ^

TypeError: Function.prototype.apply was called on null, which is a object and not a function
    at StreamHandler.runInAsyncScope (node:async_hooks:203:9)
    at StreamHandler.onHeaders (project/node_modules/undici/lib/api/api-stream.js:107:18)
    at handleReply (project/node_modules/undici/lib/mock/mock-utils.js:278:13)
    at Timeout._onTimeout (project/node_modules/undici/lib/mock/mock-utils.js:247:7)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)
$ echo $?
1

Environment

node: v16.20.1
OS: Darwin Kernel Version 21.6.0

Additional context

Probably should be fixed the same way, but in api-stream.js

@BobNobrain BobNobrain added the bug Something isn't working label Oct 16, 2023
@KhafraDev KhafraDev added the good first issue Good for newcomers label Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants