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

Upgrade miniflare to 2.0.0-rc.5 #186

Merged
merged 1 commit into from
Jan 4, 2022
Merged

Upgrade miniflare to 2.0.0-rc.5 #186

merged 1 commit into from
Jan 4, 2022

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Jan 3, 2022

Hey! 👋 This PR upgrades miniflare to version 2.0.0-rc.5: https://github.com/cloudflare/miniflare/releases/tag/v2.0.0-rc.5.

This version implements service bindings and matches some other core behaviour of the Workers runtime, like blocking asynchronous I/O outside request handlers, and limiting request recursion depth. It also doesn't proxy primitive classes like Object by default, fixing #91. The old behaviour can be enabled with --proxy-primitive for Rust workers. See this comment for more details.


Local service bindings are not yet implemented in this PR. We need to have a discussion about how best to do these.

Normally, you'd just need to set the service name to bind too, since Cloudflare already knows all of your services.
However, Miniflare needs to know where the other service's files are on disk, so it first requires you to mount (https://v2.miniflare.dev/core/mount) any services you want to bind.


I've made a few other minor changes to pages dev:

  • The service bindings implementation lets you specify a custom handler function, which simplifies the code for env.ASSETS.fetch.
  • Instead of calling await generateAssetsFetch(directory) on every request, I've lifted it out. Every call to generateAssetsFetch creates a persistent watcher for _headers and _redirects files, so this makes sure we only have one of those.
  • miniflare.startServer() can throw if Miniflare initialisation fails for any reason (e.g. syntax error in user code). These errors are now logged.
  • Imported Headers, Response and Request from @miniflare/core instead of undici. This fixes env.ASSETS.fetch(request) throws TypeError: Failed to parse URL from [object Object] #165.

@changeset-bot
Copy link

changeset-bot bot commented Jan 3, 2022

🦋 Changeset detected

Latest commit: 66e3096

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, I'll let @GregBrimble have a look before merging it in.

import { getType } from "mime";
import open from "open";
import { watch } from "chokidar";

// Defer importing miniflare until we really need it. This takes ~0.5s
// and also modifies some `stream/web` and `undici` prototypes, so we
// don't want to do this if pages commands aren't being called.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain why does miniflare modify undici/stream prototypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For adding the non-standard getAll and readAtLeast methods to Headers and ReadableStreamBYOBReader:

https://github.com/cloudflare/miniflare/blob/94ebe2180e15cff301a5dac23a77af714bbfc97c/packages/core/src/standards/http.ts#L80-L97

https://github.com/cloudflare/miniflare/blob/94ebe2180e15cff301a5dac23a77af714bbfc97c/packages/core/src/standards/streams.ts#L24-L78

We could probably return our own custom instances of these classes from Request/Response/ReadableStream subclasses, but this would be a bit of a pain, especially in the ReadableStream case as we'd also have to override TransformStream to return our ReadableStream. I think modifying the prototype is a cleaner solution in this case, especially since these are non-standard methods that shouldn't 🤞 conflict with anything else.

@GregBrimble
Copy link
Member

From a quick skim, this looks good. I'll try it out later today and if all continues to work as expected, I'll merge.

import { Headers, Request, Response } from "undici";
import type { MiniflareOptions } from "miniflare";
import type { RequestInfo, RequestInit } from "undici";
import { URL } from "url";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is required. URL should be available in Node's global scope since v10 (https://nodejs.org/api/globals.html#url).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was to make TypeScript happy, seemed to be picking up the incorrect URL types for Miniflare otherwise

});
} catch (e) {
miniflare.log.error(e);
process.exitCode = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exit doesn't quite do enough to kill the process, but it's not blocking and likely will change with my #160 anyway.

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

Successfully merging this pull request may close these issues.

env.ASSETS.fetch(request) throws TypeError: Failed to parse URL from [object Object]
3 participants