From 974c7c2796cfa243f8eb0569cd2f37b59795ed71 Mon Sep 17 00:00:00 2001 From: bcoll Date: Fri, 16 Sep 2022 15:43:37 +0100 Subject: [PATCH] Bump versions to `2.9.0`, update `CHANGELOG.md` and docs --- docs/src/content/get-started/cli.md | 11 + docs/src/content/testing/jest.md | 59 +- docs/src/content/testing/vitest.md | 60 ++- package-lock.json | 506 +++++++++--------- package.json | 2 +- packages/cache/package.json | 10 +- packages/cli-parser/package.json | 8 +- packages/core/package.json | 16 +- packages/d1/README.md | 23 +- packages/d1/package.json | 8 +- packages/durable-objects/package.json | 14 +- packages/durable-objects/src/namespace.ts | 1 + packages/html-rewriter/package.json | 8 +- packages/http-server/package.json | 10 +- .../jest-environment-miniflare/package.json | 14 +- packages/kv/package.json | 6 +- packages/miniflare/package.json | 40 +- packages/queues/package.json | 6 +- packages/r2/package.json | 6 +- packages/runner-vm/package.json | 6 +- packages/scheduler/package.json | 8 +- packages/shared-test-environment/package.json | 24 +- .../shared-test-environment/src/storage.ts | 2 +- packages/shared-test/package.json | 14 +- packages/shared/package.json | 8 +- packages/shared/src/sqlite.ts | 3 +- packages/sites/package.json | 12 +- packages/storage-file/package.json | 8 +- packages/storage-memory/package.json | 6 +- packages/storage-memory/src/memory.ts | 2 +- packages/storage-redis/package.json | 8 +- .../vitest-environment-miniflare/package.json | 14 +- packages/watcher/package.json | 6 +- packages/web-sockets/package.json | 8 +- 34 files changed, 525 insertions(+), 412 deletions(-) diff --git a/docs/src/content/get-started/cli.md b/docs/src/content/get-started/cli.md index 23e4554d6..c9cd5d1f8 100644 --- a/docs/src/content/get-started/cli.md +++ b/docs/src/content/get-started/cli.md @@ -219,6 +219,17 @@ Specifying a script is optional when `--repl` is enabled, but may be required if you're using Durable Objects. If you're using an ES module format worker, bindings will be accessible via the `env` global variable. +The REPL can be configured using environment variables similar to +[Node.js](https://nodejs.org/api/repl.html#environment-variable-options): + +- `MINIFLARE_REPL_HISTORY`: path to save REPL history to. Setting this to an + empty string disables persistent REPL history. Defaults to + `~/.mf_repl_history`. +- `MINIFLARE_REPL_HISTORY_SIZE`: number of history lines to save if persistent + REPL history is enabled. Defaults to `1000`. +- `MINIFLARE_REPL_MODE`: either `sloppy` or `strict`. Defaults to `sloppy` + allowing non-strict code to run. + ```sh $ miniflare --repl --kv TEST_NAMESPACE > await new HTMLRewriter().on("p", { diff --git a/docs/src/content/testing/jest.md b/docs/src/content/testing/jest.md index 9998df8ab..db4cada60 100644 --- a/docs/src/content/testing/jest.md +++ b/docs/src/content/testing/jest.md @@ -84,10 +84,10 @@ $ NODE_OPTIONS=--experimental-vm-modules npx jest ## Isolated Storage The Miniflare environment will use isolated storage for KV namespaces, caches, -and Durable Objects in each test. This essentially means any changes you make in -a test or `describe`-block are automatically undone afterwards. The isolated -storage is copied from the parent `describe`-block, allowing you to seed data in -`beforeAll` hooks. +Durable Objects and D1 databases in each test. This essentially means any +changes you make in a test or `describe`-block are automatically undone +afterwards. The isolated storage is copied from the parent `describe`-block, +allowing you to seed data in `beforeAll` hooks. As an example, consider the following tests: @@ -188,7 +188,7 @@ export class TestObject { } async fetch() { - const count = (await this.storage.get("count")) + 1; + const count = ((await this.storage.get("count")) ?? 0) + 1; this.storage.put("count", count); return new Response(count.toString()); } @@ -264,6 +264,55 @@ test("flushes alarms", async () => { }); ``` +### Constructing Durable Objects Directly + +Alternatively, you can construct instances of your Durable Object using +`DurableObjectState`s returned by the `getMiniflareDurableObjectState()` global +function. This allows you to call instance methods and access ephemeral state +directly. Wrapping calls to instance methods with +`runWithMiniflareDurableObjectGates()` will close the Durable Object's input +gate, and wait for the output gate to open before resolving. Make sure to use +this when calling your `fetch()` method. + +```js +--- +filename: test / index.spec.js +--- +import { TestObject } from "../src/index.mjs"; + +test("increments count", async () => { + const env = getMiniflareBindings(); + // Use standard Durable Object bindings to generate IDs + const id = env.TEST_OBJECT.newUniqueId(); + + // Get DurableObjectState, and seed Durable Object storage + // (isolated storage rules from above also apply) + const state = await getMiniflareDurableObjectState(id); + await state.storage.put("count", 3); + + // Construct object directly + const object = new TestObject(state, env); + + // Concurrently increment the count twice. Wrapping `object.fetch` + // calls with `runWithMiniflareDurableObjectGates(state, ...)` + // closes `object`'s input gate when fetching, preventing race + // conditions. + const [res1, res2] = await Promise.all([ + runWithMiniflareDurableObjectGates(state, () => { + return object.fetch(new Request("http://localhost/")); + }), + runWithMiniflareDurableObjectGates(state, () => { + return object.fetch(new Request("http://localhost/")); + }), + ]); + expect(await res1.text()).toBe("4"); + expect(await res2.text()).toBe("5"); + + // Check storage updated twice + expect(await state.storage.get("count")).toBe(5); +}); +``` + ## Mocking Outbound `fetch` Requests Miniflare allows you to substitute custom `Response`s for `fetch()` calls using diff --git a/docs/src/content/testing/vitest.md b/docs/src/content/testing/vitest.md index c20307d5b..544cf1cb6 100644 --- a/docs/src/content/testing/vitest.md +++ b/docs/src/content/testing/vitest.md @@ -87,10 +87,10 @@ $ NODE_OPTIONS=--experimental-vm-modules npx vitest run ## Isolated Storage The Miniflare environment will use isolated storage for KV namespaces, caches, -and Durable Objects in each test. This essentially means any changes you make in -a test or `describe`-block are automatically undone afterwards. The isolated -storage is copied from the parent `describe`-block, allowing you to seed data in -`beforeAll` hooks. +Durable Objects and D1 databases in each test. This essentially means any +changes you make in a test or `describe`-block are automatically undone +afterwards. The isolated storage is copied from the parent `describe`-block, +allowing you to seed data in `beforeAll` hooks.