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

Request Object doesn't contains all properties #122

Closed
Moser-ss opened this issue Dec 13, 2021 · 11 comments
Closed

Request Object doesn't contains all properties #122

Moser-ss opened this issue Dec 13, 2021 · 11 comments

Comments

@Moser-ss
Copy link

Moser-ss commented Dec 13, 2021

When I am using MiniflareI see the Request Object doesn't contain the same properties that are described in the Cloudflare documentation
That is annoying because that means code that works in live and with wrangler is not working with Miniflare

With Miniflare

Request Object direct endpoint:  {
 	"size": 0,
 	"timeout": 0,
 	"follow": 20,
 	"compress": true,
 	"counter": 0,
 	"query": {}
 }
Request Object inside Durable Object : {
 	"size": 0,
 	"timeout": 0,
 	"follow": 20,
 	"compress": true,
 	"counter": 0
 }

With wrangler dev

Request Object direct endpoint: {
	"cf": {
		"clientTcpRtt": 25,
		"longitude": "******",
		"latitude": "******",
		"tlsCipher": "ECDHE-ECDSA-AES128-GCM-SHA256",
		"continent": "**",
		"asn": ** ,
		"clientAcceptEncoding": "gzip",
		"country": "***",
		"isEUCountry": "*",
		"tlsClientAuth": {
			"certIssuerDNLegacy": "",
			"certIssuerSKI": "",
			"certSubjectDNRFC2253": "",
			"certSubjectDNLegacy": "",
			"certFingerprintSHA256": "",
			"certNotBefore": "",
			"certSKI": "",
			"certSerial": "",
			"certIssuerDN": "",
			"certVerified": "NONE",
			"certNotAfter": "",
			"certSubjectDN": "",
			"certPresented": "0",
			"certRevoked": "0",
			"certIssuerSerial": "",
			"certIssuerDNRFC2253": "",
			"certFingerprintSHA1": ""
		},
		"tlsExportedAuthenticator": {
			"clientFinished": "****",
			"clientHandshake": "****",
			"serverHandshake": "*****",
			"serverFinished": "******"
		},
		"tlsVersion": "TLSv1.2",
		"colo": "BCN",
		"timezone": "****/***",
		"city": "***",
		"edgeRequestKeepAliveStatus": 1,
		"requestPriority": "",
		"httpProtocol": "HTTP/1.1",
		"region": "***",
		"regionCode": "**",
		"asOrganization": "*** ***",
		"postalCode": "***-**"
	},
	"signal": {
		"aborted": false
	},
	"fetcher": {},
	"redirect": "manual",
	"headers": {},
	"url": "https://********/api/rooms",
	"method": "GET",
	"bodyUsed": false,
	"body": null,
	"query": {}
}
Script modified; context reset.
Request Object: {
 	"signal": {
 		"aborted": false
 	},
 	"fetcher": {},
 	"redirect": "manual",
 	"headers": {},
 	"url": "https://fake-host/",
 	"method": "GET",
 	"bodyUsed": false,
 	"body": null
 }
@mrbbot
Copy link
Contributor

mrbbot commented Dec 14, 2021

Hey! 👋 Which version of Miniflare are you using? Miniflare 2 (npm i miniflare@next) more closely matches the real environment. Have you got an example of code that's working with wrangler but not Miniflare?

@Moser-ss
Copy link
Author

I am using version 1.4.1
This example will not work with miniflare
https://github.com/cloudflare/durable-objects-typescript-rollup-esm/blob/master/src/counter.ts#L18
Also I am doing this with Durable Objects

async fetch(request: Request): Promise<Response> {
    const { method, url } = request;
    const { pathname } = new URL(URL);
    }

And the method and url properties don't exist. I am also thinking about how people use miniflare with Durable Objects if an official example doesn't work

@mrbbot
Copy link
Contributor

mrbbot commented Dec 14, 2021

That code you've provided will work. The problem is that method and url were not marked as enumerable properties in Miniflare 1, so when you try to console.log a Request, you wouldn't see them. The properties are there though, so you can still access them. The enumerability issue was fixed in Miniflare 2.

@Moser-ss
Copy link
Author

Moser-ss commented Dec 14, 2021

But I have this error
That is failing in this line
const { pathname } = new URL(url);

async fetch(request: Request): Promise<Response> {
created dist/index.mjs in 5.5s
[mf:inf] Build succeeded
[mf:dbg] Options:
[mf:dbg] - Build Command: npm install && npm run build
[mf:dbg] - Scripts: dist/index.mjs
[mf:dbg] - Modules: true
[mf:dbg] - Modules Rules: {ESModule: **/*.mjs}, {CommonJS: **/*.js, **/*.cjs}
[mf:dbg] - Durable Objects: ROOMS
[mf:dbg] Reloading dist/index.mjs...
[mf:inf] Worker reloaded! (282.49KiB)
[mf:dbg] Watching .env, dist/index.mjs, package.json, src, wrangler.toml...
[mf:inf] Listening on :8787
[mf:inf] - http://127.0.0.1:8787
[mf:inf] - http://172.28.120.138:8787
Request Object Rooms: {"size":0,"timeout":0,"follow":20,"compress":true,"counter":0,"query":{}}
Request Object: {"size":0,"timeout":0,"follow":20,"compress":true,"counter":0}
URL: 
[mf:err] GET /api/rooms: TypeError [ERR_INVALID_URL]: Invalid URL: 
    at onParseError (internal/url.js:279:9)
    at new URL (internal/url.js:355:5)
    at p.fetch (/home/smoser/projects/*****/src/do/Rooms.ts:46:26)
    at DurableObjectStub.fetch (/home/smoser/.nvm/versions/node/v14.17.6/lib/node_modules/miniflare/src/modules/do.ts:66:21)
    at process.rsp (internal/process/task_queues.js:95:5)
    at p (/home/smoser/projects/*****/src/routes/rooms.ts:13:9)
    at p (/home/smoser/projects/*****/node_modules/itty-router/dist/itty-router.min.js:1:296)
    at p (/home/smoser/projects/*****/node_modules/itty-router/dist/itty-router.min.js:1:296)
    at e.r (/home/smoser/projects/*****/node_modules/itty-router/dist/itty-router.min.js:1:296)
    at ServiceWorkerGlobalScope.[dispatchFetch] (/home/smoser/.nvm/versions/node/v14.17.6/lib/node_modules/miniflare/src/modules/events.ts:207:25)
GET /api/rooms 500 Internal Server Error (149.67ms)

@mrbbot
Copy link
Contributor

mrbbot commented Dec 14, 2021

Could you share the code at and above src/do/Rooms.ts:46:26?

@Moser-ss
Copy link
Author

Yup

41 async fetch(request: Request): Promise<Response> {
42   console.log(`Request Object: ${JSON.stringify(request)}`);
43    
44    const { method, url } = request;
45    console.log(`URL: ${url}`);
46    const { pathname } = new URL(url);

@mrbbot
Copy link
Contributor

mrbbot commented Dec 14, 2021

Hmmm ok, it looks like url might be the empty string, whereas new URL requires a full URL, with a protocol and host. How are you fetching from your stub in src/routes/rooms.ts:13:9? Miniflare 1 incorrectly passes through relative URLs here. This has been fixed in Miniflare 2.

@Moser-ss
Copy link
Author

BTW I using lib itty-router for routing
And this is how I am fetching the DO

const rsp = await DO.fetch(
    new Request('', {
      method: 'GET',
    })
  );

@Moser-ss
Copy link
Author

This has been fixed in Miniflare 2.

How stable is miniflare 2? Or when is plan to release the version 2?

@mrbbot
Copy link
Contributor

mrbbot commented Dec 15, 2021

Miniflare 2 is very stable. Hoping to release it before the new year, there's just an internal blocker I need to sort out. 👍

@Moser-ss
Copy link
Author

I just test with miniflare@next and still the same behaviour

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