Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit b5bb707

Browse files
committed
Bump versions to 2.0.0, update CHANGELOG.md and docs 🎉
1 parent 25cb716 commit b5bb707

File tree

40 files changed

+494
-437
lines changed

40 files changed

+494
-437
lines changed

docs/CHANGELOG.md

Lines changed: 102 additions & 52 deletions
Large diffs are not rendered by default.

docs/docs-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ module.exports = {
3535
"Fun, full-featured, fully-local simulator for Cloudflare Workers",
3636
author: "@cloudflare",
3737
url: "https://miniflare.dev",
38-
// image: "http://developers.cloudflare.com/workers/og-image.png",
38+
image: "https://blog.cloudflare.com/content/images/2022/01/image1-5.png",
3939
},
4040
};

docs/src/content/core/standards.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,90 @@ const mf = new Miniflare({
9999
KV namespaces and caches returned from `Miniflare#getKVNamespace()` and
100100
`Miniflare#getCaches()` are unaffected by this limit, so they can still be used
101101
in tests without setting any additional options.
102+
103+
## `instanceof`, `constructor` and `prototype` Checks
104+
105+
Miniflare overrides `instanceof` checks for primitive classes like `Object` so
106+
they succeed for values created both inside and outside the Miniflare sandbox
107+
(in a different JavaScript realm). This ensures dynamic type checking often
108+
performed by WebAssembly glue code (e.g. `wasm-bindgen`) always succeeds. Note
109+
that values returned by Workers runtime APIs are created outside the Miniflare
110+
sandbox. See
111+
[this file](https://github.com/cloudflare/miniflare/blob/v2/packages/runner-vm/src/instanceof.ts)
112+
for more details.
113+
114+
Primitive classes in this case are defined as JavaScript built-ins that can be
115+
instantiated by something other than their constructor (e.g. literals,
116+
`function`s, runtime errors):
117+
118+
- `Object`
119+
- `Function`
120+
- `Array`
121+
- `Promise`
122+
- `RegExp`
123+
- `Error`, `EvalError`, `RangeError`, `ReferenceError`, `SyntaxError`,
124+
`TypeError`, `URIError`
125+
126+
Primitive `constructor` and `prototype` checks cannot be trapped easily and so
127+
will fail for values created outside the Miniflare sandbox.
128+
129+
```js
130+
import { Miniflare } from "miniflare";
131+
132+
const mf = new Miniflare({
133+
bindings: {
134+
OBJECT: { a: 1 },
135+
ARRAY: new Uint8Array([1, 2, 3]),
136+
},
137+
modules: true,
138+
script: `
139+
export default {
140+
async fetch(request, env, ctx) {
141+
console.log({ a: 1 } instanceof Object); // ✅ true
142+
console.log(new Uint8Array([1, 2, 3]) instanceof Object); // ✅ true
143+
console.log({ a: 1 }.constructor === Object); // ✅ true
144+
console.log(Object.getPrototypeOf({ a: 1 }) === Object.prototype); // ✅ true
145+
146+
console.log(env.OBJECT instanceof Object); // ✅ true
147+
console.log(env.ARRAY instanceof Object); // ✅ true
148+
console.log(env.OBJECT.constructor === Object); // ❌ false
149+
console.log(Object.getPrototypeOf(env.OBJECT) === Object.prototype); // ❌ false
150+
151+
throw new Error("oops!");
152+
}
153+
}
154+
`,
155+
});
156+
157+
try {
158+
await mf.dispatchFetch("http://localhost");
159+
} catch (e) {
160+
console.log(e instanceof Error); // ❌ false
161+
}
162+
```
163+
164+
By default, primitive `instanceof` checks outside the Miniflare sandbox will
165+
fail for values created inside the sandbox (e.g. checking types of thrown
166+
exceptions in tests). To fix this, pass the primitive class in from Node.js as a
167+
custom global. Note this will cause primitive `instanceof` checks to fail for
168+
values created without the constructor inside the sandbox.
169+
170+
```js
171+
const mf = new Miniflare({
172+
globals: { Error },
173+
modules: true,
174+
script: `
175+
export default {
176+
async fetch(request, env, ctx) {
177+
throw new Error("oops!");
178+
}
179+
}
180+
`,
181+
});
182+
183+
try {
184+
await mf.dispatchFetch("http://localhost");
185+
} catch (e) {
186+
console.log(e instanceof Error); // ✅ true
187+
}
188+
```

docs/src/content/core/web-assembly.md

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -84,57 +84,6 @@ export default {
8484
};
8585
```
8686

87-
## `instanceof` Checks
88-
89-
When accessing JavaScript objects from WebAssembly, glue code (what
90-
`wasm-bingen` generates) often needs to check values' types using `instanceof`.
91-
Due to how Miniflare works, these checks will fail for primitive classes like
92-
`Object` if values are created outside the Miniflare sandbox (in a different
93-
JavaScript realm). For example,
94-
`caches.default.match("https://miniflare.dev") instanceof Object` will always be
95-
`false` even if the request is cached, since the returned `Response` object is
96-
created outside the sandbox. To fix this, enable the `proxyPrimitiveInstanceOf`
97-
option:
98-
99-
<ConfigTabs>
100-
101-
```sh
102-
$ miniflare --proxy-primitive
103-
```
104-
105-
```toml
106-
---
107-
filename: wrangler.toml
108-
---
109-
[miniflare]
110-
proxy_primitive_instanceof = true
111-
```
112-
113-
```js
114-
const mf = new Miniflare({
115-
proxyPrimitiveInstanceOf: true,
116-
});
117-
```
118-
119-
</ConfigTabs>
120-
121-
This proxies `instanceof` checks for primitive classes, so they succeed
122-
regardless of the realm the object is created in. See
123-
[this comment](https://github.com/cloudflare/miniflare/blob/720794accee7582b01e849182244a65ce60c9d60/packages/core/src/plugins/core.ts#L487-L555)
124-
for more details.
125-
126-
<Aside type="warning" header="Warning">
127-
128-
Enabling this option will cause primitive class `constructor` and `prototype`
129-
checks to fail:
130-
131-
```js
132-
{}.constructor === Object; // false
133-
Object.getPrototypeOf({}) === Object.prototype; // false
134-
```
135-
136-
</Aside>
137-
13887
## Rust Wrangler Builds
13988

14089
When using [Rust Wrangler Builds](/developing/builds#rust), `wasm` is

docs/src/content/index.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ type: overview
88

99
# 🔥 Miniflare
1010

11-
<Aside type="warning" header="Warning">
12-
13-
⚠️ These docs are for the next major version of Miniflare, which is under
14-
development.
15-
16-
</Aside>
17-
1811
**Miniflare** is a simulator for developing and testing
1912
[**Cloudflare Workers**](https://workers.cloudflare.com/). It's written in
2013
TypeScript, and runs your code in a sandbox implementing Workers' runtime APIs.
@@ -28,8 +21,8 @@ TypeScript, and runs your code in a sandbox implementing Workers' runtime APIs.
2821

2922
<ButtonGroup>
3023
<Button type="primary" href="/get-started/cli">Get Started</Button>
31-
<Button type="secondary" href="https://github.com/cloudflare/miniflare/tree/v2">GitHub</Button>
32-
<Button type="secondary" href="https://npmjs.com/package/miniflare/v/next">npm</Button>
24+
<Button type="secondary" href="https://github.com/cloudflare/miniflare">GitHub</Button>
25+
<Button type="secondary" href="https://npmjs.com/package/miniflare">npm</Button>
3326
</ButtonGroup>
3427

3528
---

docs/src/content/testing/jest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for an example using TypeScript.
1414
The Miniflare environment isn't installed by default, install it and Jest with:
1515

1616
```sh
17-
$ npm install -D jest-environment-miniflare@next jest
17+
$ npm install -D jest-environment-miniflare jest
1818
```
1919

2020
In the following examples, we'll assume your `package.json` contains

0 commit comments

Comments
 (0)