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

Commit b251fa7

Browse files
committed
Add descriptions and examples to READMEs
1 parent 27c6ceb commit b251fa7

File tree

18 files changed

+399
-18
lines changed

18 files changed

+399
-18
lines changed

packages/cache/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
# `@miniflare/cache`
22

33
Cache module for [Miniflare](https://github.com/cloudflare/miniflare): a fun,
4-
full-featured, fully-local simulator for Cloudflare Workers
4+
full-featured, fully-local simulator for Cloudflare Workers. See
5+
[✨ Cache](https://miniflare.dev/cache.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { Cache } from "@miniflare/cache";
11+
import { Response } from "@miniflare/core";
12+
import { MemoryStorage } from "@miniflare/storage-memory";
13+
14+
const cache = new Cache(new MemoryStorage());
15+
16+
const key = "http://localhost";
17+
const res = new Response("body", {
18+
headers: { "Cache-Control": "max-age=3600" },
19+
});
20+
await cache.put(key, res);
21+
22+
const cachedRes = await cache.match(key);
23+
console.log(await cachedRes.text()); // body
24+
```

packages/cli-parser/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,25 @@
22

33
CLI option parsing module for
44
[Miniflare](https://github.com/cloudflare/miniflare): a fun, full-featured,
5-
fully-local simulator for Cloudflare Workers
5+
fully-local simulator for Cloudflare Workers. See
6+
[💻 Using the CLI](https://miniflare.dev/cli.html) for more details.
7+
8+
## Example
9+
10+
```js
11+
import { buildHelp, parseArgv } from "@miniflare/cli-parser";
12+
import { BuildPlugin } from "@miniflare/core";
13+
import { KVPlugin } from "@miniflare/kv";
14+
15+
const plugins = { BuildPlugin, KVPlugin };
16+
17+
const help = buildHelp(plugins, "exec");
18+
console.log(help); // Usage: exec ...
19+
20+
const options = parseArgv(plugins, [
21+
"--build-command",
22+
"npm run build",
23+
"--kv-persist",
24+
]);
25+
console.log(options); // { buildCommand: "npm run build", kvPersist: true };
26+
```

packages/core/README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
# `@miniflare/core`
22

33
Core module for [Miniflare](https://github.com/cloudflare/miniflare): a fun,
4-
full-featured, fully-local simulator for Cloudflare Workers
4+
full-featured, fully-local simulator for Cloudflare Workers. See
5+
[🧰 Using the API](https://miniflare.dev/api.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { CorePlugin, MiniflareCore } from "@miniflare/core";
11+
import { KVPlugin } from "@miniflare/kv";
12+
import { VMScriptRunner } from "@miniflare/runner-vm";
13+
import { Log, LogLevel } from "@miniflare/shared";
14+
import { MemoryStorage } from "@miniflare/storage-memory";
15+
16+
export class StorageFactory {
17+
storages = new Map();
18+
19+
storage(namespace) {
20+
let storage = this.storages.get(namespace);
21+
if (storage) return storage;
22+
this.storages.set(namespace, (storage = new MemoryStorage()));
23+
return storage;
24+
}
25+
}
26+
27+
const plugins = { CorePlugin, KVPlugin };
28+
const ctx = {
29+
log: new Log(LogLevel.INFO),
30+
storageFactory: new StorageFactory(),
31+
scriptRunner: new VMScriptRunner(),
32+
};
33+
34+
const mf = new MiniflareCore(plugins, ctx, {
35+
modules: true,
36+
script: `export default {
37+
async fetch(request, env) {
38+
return new Response(await env.TEST_NAMESPACE.get("key"));
39+
}
40+
}`,
41+
kvNamespaces: ["TEST_NAMESPACE"],
42+
});
43+
44+
const { TEST_NAMESPACE } = await mf.getBindings();
45+
await TEST_NAMESPACE.put("key", "value");
46+
47+
const res = await mf.dispatchFetch("http://localhost");
48+
console.log(await res.text()); // value
49+
```

packages/durable-objects/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
# `@miniflare/durable-objects`
22

33
Durable Objects module for [Miniflare](https://github.com/cloudflare/miniflare):
4-
a fun, full-featured, fully-local simulator for Cloudflare Workers
4+
a fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[📌 Durable Objects](https://miniflare.dev/durable-objects.html) for more
6+
details.
7+
8+
## Example
9+
10+
```js
11+
import { DurableObjectStorage } from "@miniflare/durable-objects";
12+
import { MemoryStorage } from "@miniflare/storage-memory";
13+
14+
const storage = new DurableObjectStorage(new MemoryStorage());
15+
await storage.put("key", "value");
16+
console.log(await storage.get("key")); // value
17+
```

packages/html-rewriter/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
# `@miniflare/html-rewriter`
22

33
HTMLRewriter module for [Miniflare](https://github.com/cloudflare/miniflare): a
4-
fun, full-featured, fully-local simulator for Cloudflare Workers
4+
fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[📄 HTMLRewriter](https://miniflare.dev/html-rewriter.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { Response } from "@miniflare/core";
11+
import { HTMLRewriter } from "@miniflare/html-rewriter";
12+
13+
const rewriter = new HTMLRewriter().on("p", {
14+
element(element) {
15+
element.setInnerContent("new");
16+
},
17+
});
18+
19+
const res = new Response("<p>old</p>");
20+
const transformed = rewriter.transform(res);
21+
console.log(await transformed.text()); // <p>new</p>
22+
```

packages/http-server/README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,60 @@
11
# `@miniflare/http-server`
22

33
HTTP server module for [Miniflare](https://github.com/cloudflare/miniflare): a
4-
fun, full-featured, fully-local simulator for Cloudflare Workers
4+
fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[🧰 Using the API](https://miniflare.dev/api.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { CorePlugin, MiniflareCore } from "@miniflare/core";
11+
import {
12+
HTTPPlugin,
13+
convertNodeRequest,
14+
createServer,
15+
startServer,
16+
} from "@miniflare/http-server";
17+
import { VMScriptRunner } from "@miniflare/runner-vm";
18+
import { Log, LogLevel } from "@miniflare/shared";
19+
import { MemoryStorage } from "@miniflare/storage-memory";
20+
import http from "http";
21+
22+
// Converting Node.js http.IncomingMessage to Miniflare's Request
23+
http.createServer(async (nodeReq, nodeRes) => {
24+
const req = await convertNodeRequest(nodeReq, "http://upstream", {
25+
forwardedProto: "http",
26+
realIp: "127.0.0.1",
27+
cf: { colo: "SFO" },
28+
});
29+
nodeRes.end(await req.text());
30+
});
31+
32+
// Creating and starting HTTP servers
33+
export class BadStorageFactory {
34+
storage() {
35+
throw new Error("This example shouldn't need storage!");
36+
}
37+
}
38+
39+
const plugins = { CorePlugin, HTTPPlugin };
40+
const ctx = {
41+
log: new Log(LogLevel.INFO),
42+
storageFactory: new BadStorageFactory(),
43+
scriptRunner: new VMScriptRunner(),
44+
};
45+
46+
const mf = new MiniflareCore(plugins, ctx, {
47+
modules: true,
48+
script: `export default {
49+
async fetch(request, env) {
50+
return new Response("body");
51+
}
52+
}`,
53+
port: 5000,
54+
});
55+
56+
// Start the server yourself...
57+
const server = await createServer(mf);
58+
// ...or get Miniflare to start it for you, logging to port
59+
const server2 = await startServer(mf);
60+
```
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# `@miniflare/jest`
22

33
Jest testing module for [Miniflare](https://github.com/cloudflare/miniflare): a
4-
fun, full-featured, fully-local simulator for Cloudflare Workers
4+
fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[🤹 Jest Environment ](https://v2.miniflare.dev/jest.html) for more details.
6+
7+
## Example
8+
9+
See
10+
[this repository](https://github.com/mrbbot/miniflare-typescript-esbuild-jest)
11+
for an example using TypeScript.

packages/kv/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
# `@miniflare/kv`
22

33
Workers KV module for [Miniflare](https://github.com/cloudflare/miniflare): a
4-
fun, full-featured, fully-local simulator for Cloudflare Workers
4+
fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[📦 KV](https://miniflare.dev/kv.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { KVNamespace } from "@miniflare/kv";
11+
import { MemoryStorage } from "@miniflare/storage-memory";
12+
13+
const ns = new KVNamespace(new MemoryStorage());
14+
await ns.put("key", "value");
15+
console.log(await ns.get("key")); // value
16+
```

packages/runner-vm/README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,40 @@
22

33
VM script runner module for
44
[Miniflare](https://github.com/cloudflare/miniflare): a fun, full-featured,
5-
fully-local simulator for Cloudflare Workers
5+
fully-local simulator for Cloudflare Workers.
6+
7+
## Example
8+
9+
```js
10+
import { VMScriptRunner } from "@miniflare/runner-vm";
11+
12+
const runner = new VMScriptRunner();
13+
// Pass `console` into sandbox
14+
const globalScope = { console };
15+
16+
// Run regular script
17+
const blueprint1 = {
18+
code: 'console.log("hello")',
19+
filePath: "test.js",
20+
};
21+
await runner.run(globalScope, blueprint1); // hello
22+
23+
// Run module script
24+
const blueprint2 = {
25+
code: 'import thing from "./thing.js"; console.log(thing);',
26+
filePath: "test.mjs",
27+
};
28+
const moduleRules = [{ type: "ESModule", include: /\.js$/ }];
29+
// Assuming thing.js contains `"export default "thing";`...
30+
await runner.run(globalScope, blueprint2, moduleRules); // thing
31+
32+
// Run module script with additional module
33+
const blueprint3 = {
34+
code: `import additional from "__ADDITIONAL"; console.log(additional);`,
35+
filePath: "test.mjs",
36+
};
37+
const modules = {
38+
__ADDITIONAL: { default: "stuff" },
39+
};
40+
await runner.run(globalScope, blueprint3, moduleRules, modules); // stuff
41+
```

packages/scheduler/README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
# `@miniflare/scheduler`
22

33
Scheduler module for [Miniflare](https://github.com/cloudflare/miniflare): a
4-
fun, full-featured, fully-local simulator for Cloudflare Workers
4+
fun, full-featured, fully-local simulator for Cloudflare Workers. See
5+
[⏰ Scheduled Events](https://miniflare.dev/scheduled.html) for more details.
6+
7+
## Example
8+
9+
```js
10+
import { CorePlugin, MiniflareCore } from "@miniflare/core";
11+
import { VMScriptRunner } from "@miniflare/runner-vm";
12+
import { Log, LogLevel } from "@miniflare/shared";
13+
import { SchedulerPlugin, startScheduler } from "@miniflare/scheduler";
14+
import { MemoryStorage } from "@miniflare/storage-memory";
15+
16+
export class BadStorageFactory {
17+
storage() {
18+
throw new Error("This example shouldn't need storage!");
19+
}
20+
}
21+
const plugins = { CorePlugin, SchedulerPlugin };
22+
const ctx = {
23+
log: new Log(LogLevel.INFO),
24+
storageFactory: new BadStorageFactory(),
25+
scriptRunner: new VMScriptRunner(),
26+
};
27+
28+
const mf = new MiniflareCore(plugins, ctx, {
29+
modules: true,
30+
script: `export default {
31+
async scheduled(request, env) {
32+
console.log("tick");
33+
}
34+
}`,
35+
crons: ["* * * * *"],
36+
});
37+
38+
const scheduler = await startScheduler(mf); // tick, tick, tick, ...
39+
scheduler.dispose();
40+
```

0 commit comments

Comments
 (0)