Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions samples/webfs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Web File System API Example

To run the example on http://localhost:8080

```sh
$ ./workerd serve config.capnp
```

To run using bazel

```sh
$ bazel run //src/workerd/server:workerd -- serve ~/cloudflare/workerd/samples/helloworld_esm/config.capnp
```

To create a standalone binary that can be run:

```sh
$ ./workerd compile config.capnp > helloworld

$ ./helloworld
```

To test:

```sh
% curl http://localhost:8080
Hello World
```
35 changes: 35 additions & 0 deletions samples/webfs/config.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Imports the base schema for workerd configuration files.

# Refer to the comments in /src/workerd/server/workerd.capnp for more details.

using Workerd = import "/workerd/workerd.capnp";

# A constant of type Workerd.Config defines the top-level configuration for an
# instance of the workerd runtime. A single config file can contain multiple
# Workerd.Config definitions and must have at least one.
const helloWorldExample :Workerd.Config = (

# Every workerd instance consists of a set of named services. A worker, for instance,
# is a type of service. Other types of services can include external servers, the
# ability to talk to a network, or accessing a disk directory. Here we create a single
# worker service. The configuration details for the worker are defined below.
services = [ (name = "main", worker = .helloWorld) ],

# Each configuration defines the sockets on which the server will listen.
# Here, we create a single socket that will listen on localhost port 8080, and will
# dispatch to the "main" service that we defined above.
sockets = [ ( name = "http", address = "*:8080", http = (), service = "main" ) ]
);

# The definition of the actual helloWorld worker exposed using the "main" service.
# In this example the worker is implemented as an ESM module (see worker.js).
# The compatibilityDate is required. For more details on compatibility dates see:
# https://developers.cloudflare.com/workers/platform/compatibility-dates/

const helloWorld :Workerd.Worker = (
modules = [
(name = "worker", esModule = embed "worker.js")
],
compatibilityDate = "2025-05-01",
compatibilityFlags = ["enable_web_file_system", "experimental"]
);
23 changes: 23 additions & 0 deletions samples/webfs/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2017-2023 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

const dir = await navigator.storage.getDirectory();
const bundle = await dir.getDirectoryHandle("tmp");
const file = await bundle.getFileHandle("foo", { create: true });
const handle = await file.createSyncAccessHandle();

const enc = new TextEncoder();
handle.write(enc.encode("Hello World"));
handle.write(enc.encode("!!!\n"));
console.log(handle.getSize());
handle.close();

const data = await file.getFile();
console.log(await data.text());

export default {
async fetch(req, env) {
return new Response("Hello World\n");
}
};
6 changes: 6 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,9 @@ wd_test(
args = ["--experimental"],
data = ["tests/request-client-disconnect.js"],
)

wd_test(
src = "tests/webfs-test.wd-test",
args = ["--experimental"],
data = ["tests/webfs-test.js"],
)
Loading
Loading