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

Internal server error on collection COPY with Depth: infinity #9

Open
thebartekbanach opened this issue Nov 20, 2024 · 3 comments
Open

Comments

@thebartekbanach
Copy link

thebartekbanach commented Nov 20, 2024

Hello!

First of all, I wanna say THANK YOU for this great library, it saved me a lot of work. If you configure Sponsor button I can give you a tip 😄

I encountered a problem with COPY method when trying to use it on collection with Depth: infinity header. I get 500 Internal server error, caused because incorrect destination URL being created.

The exact error message that I extracted with debugger is:

TypeError: Invalid URL
    at new URL (node:internal/url:797:36)
    at <anonymous> (/app/node_modules/nephele/src/Methods/COPY.ts:315:40)
    at async <anonymous> (/app/node_modules/nephele/src/catchErrors.ts:39:14)
    at async recursivelyCopy (/app/node_modules/nephele/src/Methods/COPY.ts:352:14)
    at async COPY.run (/app/node_modules/nephele/src/Methods/COPY.ts:355:21)
    at async <anonymous> (/app/node_modules/nephele/src/catchErrors.ts:39:14) {code: 'ERR_INVALID_URL', input: 'http://localhost/_/api/v1/resources/kopia-pierwsza/skopiowany/skopiowany/kolejny/', base: 'http:://localhost', stack: 'TypeError: Invalid URL
    at new URL (node:i…ode_modules/nephele/src/catchErrors.ts:39:14)', message: 'Invalid URL'}

With the most important thing noticible here:

code: 'ERR_INVALID_URL',
input: 'http://localhost/_/api/v1/resources/kopia-pierwsza/skopiowany/skopiowany/kolejny/', 
base: 'http:://localhost'

Look at the base - protocol includes double :: character, which causes an error.

I did not dive deep into this, but if we take a look at this piece of code, we can see the cause:

const destinationUrl = new URL(
    destination.toString().replace(/\/?$/, () => '/') +
        encodeURIComponent(name) +
        ((await child.isCollection()) ? '/' : ''),
    `${destination.protocol}://${destination.host}`, // <-- unnecesary colon
);

Destination variable is of URL type. According to this docs on MDN the protocol property returns protocol name with a colon behind it, for example http:.

So the result is, as you can see in the error message provided above, baseUrl with value "http:://localhost" provided into URL constructor.

I can create a PR with a fix, but first I need a green light from you. It seems like a small and easy fix, but it may break some functionalities that I do not even realize that exist.

The change proposed by me is to remove additional colon:

const destinationUrl = new URL(
    destination.toString().replace(/\/?$/, () => '/') +
        encodeURIComponent(name) +
        ((await child.isCollection()) ? '/' : ''),
    `${destination.protocol}//${destination.host}`, // NO COLON, COMPARE WITH CODE ABOVE
);

Thank you for taking time to look at this issue.

Have a good day!

@thebartekbanach
Copy link
Author

thebartekbanach commented Dec 17, 2024

Following script fixes the issue by replacing problematic line with fixed one:

// this script should be placed inside some directory fe. `nephele_copy_fix` next to your `node_modules` directory

import fs from "fs/promises";

console.log("Loading source file contents");

const sourceFileContents = await fs.readFile(
    "../node_modules/nephele/dist/Methods/COPY.js",
    "utf-8",
);

console.log("Source file contents loaded, replacing the problematic code");

const fixedSourceFileContents = sourceFileContents.replace(
    "`${destination.protocol}://${destination.host}`", // colon exists
    "`${destination.protocol}//${destination.host}`", // colon removed
);

console.log("Overwriting the source file with the fixed code");

await fs.writeFile(
    "../node_modules/nephele/dist/Methods/COPY.js",
    fixedSourceFileContents,
);

console.log("Temporary fix completed");

We are using it for a month and everything works as expected :)

@hperrin
Copy link
Member

hperrin commented Dec 19, 2024

Wow! Thank you for the detailed report and the fix. I'll go ahead and merge this and make a new release. And thank you for the compliments. I'll set up a sponsor button. :)

@hperrin
Copy link
Member

hperrin commented Dec 19, 2024

This project has been on the back burner for a month, since I've been updating all my other projects to the new version of Svelte, but I'll be focusing on it again soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants