RangerMauve / hypercore-fetch

Implementation of Fetch that uses the Hyper SDK for loading p2p content
MIT License
37 stars 13 forks source link

Not finishing with a / on a directory leads to all sorts of funkiness #11

Closed KyGost closed 4 years ago

KyGost commented 4 years ago

See RangerMauve/agregore-browser#36

KyGost commented 4 years ago

Will come back to this some other time. Can't fix /client vs /client/ issue. The renaming won't occur as RangerMauve/agregore-browser doesn't check finalPath (nor do other implementations I'd expect). The only issue my fix fixes is the index following the name of the used path.

KyGost commented 4 years ago

Stat.isDirectory isn't the issue. Magic to me as to how it works but looks like it is doing something to do with filetypes via a bitmask? https://github.com/hypercore-protocol/hyperdrive-schemas/blob/master/lib/stat.js#L86

KyGost commented 4 years ago

My best guess is it has to do with a rogue hyperdrive block. Not sure how to investigate that. Will get around to it eventually I'm sure. (or keep procastinating sleep and do it now)

RangerMauve commented 4 years ago

Oh! I was thinking one way to do it would be something like this:

With the 301 status code, the browser should detect the new URL and load it from there

KyGost commented 4 years ago

Oh awesome! Didn't know about that. Thanks, will try.

KyGost commented 4 years ago

I won't bother submitting a PR-- here's the change so far: https://github.com/RangerMauve/dat-fetch/blob/master/index.js#L183

if (stat.isDirectory()) {
+         if (!path.endsWith('/')) {
+           responseHeaders.set('Location', path + '/')
+           return new FakeResponse(301, 'Permanent Redirect', responseHeaders, intoStream(''), url + '/');
+         }
          const stats = await archive.readdir(finalPath, { includeStats: true })
          const files = stats.map(({ stat, name }) => (stat.isDirectory() ? `${name}/` : name))

This results in:

Error occurred in handler for 'agregore-window-loadURL': Error: ERR_FAILED (-2) loading 'hyper://94f0cab7f60fcc2a711df11c85db5e0594d11e8a3efd04a06f46a3c34d03c418/posts'
    at rejectAndCleanup (electron/js2c/browser_init.js:205:1493)
    at Object.stopLoadingListener (electron/js2c/browser_init.js:205:1868)
    at Object.emit (events.js:327:22) {
  errno: -2,
  code: 'ERR_FAILED',
  url: 'hyper://94f0cab7f60fcc2a711df11c85db5e0594d11e8a3efd04a06f46a3c34d03c418/posts'
}

When I had the response as return new FakeResponse(301, 'Permanent Redirect', {Location: path + '/'}, intoStream(''), url + '/'); It was actually redirecting and had a different error.

Neither option actually changes the UI's URL though.

KyGost commented 4 years ago

Closing for now