-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Removing shim from D1 #628
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 🥳 Couple minor comments...
@@ -1,11 +1,11 @@ | |||
# D1 Worker Fixture |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason we had this fixture was to get the D1 shim from Wrangler for testing. miniflareTest()
(see test/plugins/d1/index.spec.ts
) can accept a request handler function like:
miniflare/packages/miniflare/test/plugins/cache/index.spec.ts
Lines 13 to 58 in c0f32f3
const test = miniflareTest({}, async (global, req) => { | |
// Partition headers | |
let name: string | undefined; | |
let cfCacheKey: string | undefined; | |
let bufferPut = false; | |
const reqHeaders = new global.Headers(); | |
const resHeaders = new global.Headers(); | |
for (const [key, value] of req.headers) { | |
const lowerKey = key.toLowerCase(); | |
if (lowerKey === "test-cache-name") { | |
name = value; | |
} else if (lowerKey === "test-cf-cache-key") { | |
cfCacheKey = value; | |
} else if (lowerKey === "test-buffer") { | |
bufferPut = true; | |
} else if (lowerKey.startsWith("test-response-")) { | |
resHeaders.set(lowerKey.substring("test-response-".length), value); | |
} else { | |
reqHeaders.set(lowerKey, value); | |
} | |
} | |
// Get cache and cache key | |
const cache = | |
name === undefined ? global.caches.default : await global.caches.open(name); | |
const key = new global.Request(req.url, { | |
headers: reqHeaders, | |
cf: cfCacheKey === undefined ? undefined : { cacheKey: cfCacheKey }, | |
}); | |
// Perform cache operation | |
if (req.method === "GET") { | |
const cachedRes = await cache.match(key); | |
return cachedRes ?? new global.Response("<miss>", { status: 404 }); | |
} else if (req.method === "PUT") { | |
const body = bufferPut ? await req.arrayBuffer() : req.body; | |
const res = new global.Response(body, { headers: resHeaders }); | |
await cache.put(key, res); | |
return new global.Response(null, { status: 204 }); | |
} else if (req.method === "DELETE") { | |
const deleted = await cache.delete(key); | |
return new global.Response(null, { status: deleted ? 204 : 404 }); | |
} else { | |
return new global.Response(null, { status: 405 }); | |
} | |
}); |
This handler gets wrapped with all the
MF-Experimental-Error-Stack
stuff automatically too. Could tidy this up in a future PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up just reusing the existing test file, before Wrangler had compiled it. What do you think?
c89ae12
to
9b74ecc
Compare
Updated the PR to make Miniflare backwards-compatible with the older style of bindings. I also broke the test apart into a Not sure this is great but for right now, all I want to confirm is that the runtime shim has the exact same behaviour as the Wrangler-based shim... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! ✅
Now that workerd includes the D1 shim, we no longer need wrangler to bundle it, and no longer require all bindings to be prefixed with
__D1_BETA__
.This will need to be released alongside the Wrangler version that drops support for the shim as well, currently being tracked in this PR: cloudflare/workers-sdk#3595