Skip to content

Conversation

@nakasyou
Copy link
Contributor

@nakasyou nakasyou commented Jul 15, 2025

Moddable is a sdk enables embedded systems or microcontrollers such as ESP32, Raspberry Pi Pico and others to run JavaScript code, using XS, tiny JavaScript runtime. This PR allows us to run Hono apps on microcontrollers and develop IoT devices using Hono via Moddable.

Usage

  1. Install Moddable
  2. Setup manifest.json
{
    "include": [
        "$(MODDABLE)/examples/manifest_base.json",
        "$(MODDABLE)/examples/manifest_net.json",
        "$(MODULES)/network/http/manifest.json",
        "$(MODDABLE)/modules/data/headers/manifest.json",
        "$(MODDABLE)/modules/data/url/manifest.json",
        "$(MODDABLE)/modules/data/text/encoder/manifest.json",
        "$(MODDABLE)/modules/data/text/decoder/manifest.json",
        "$(MODDABLE)/modules/data/headers/manifest.json",
        "$(MODDABLE)/modules/network/websocket/manifest.json"
    ],
    "modules": {
        "*": [
            "$(MODDABLE)/examples/io/streams/modules/streams.js",
            "./dist/main.js"
        ]
    },
    "creation": {
        "stack": 512,
        "heap": {
            "initial": 9856
        }
    },
    "preload": [
        "streams"
    ]
}
  1. Create main.ts
import { Hono } from 'hono'
import { handle } from 'hono/moddable'
import { Listener } from 'socket'

const app = new Hono()
app.get('/', c => c.text('Hello Hono on moddable!'))

const listener = new Listener({ port: 3000 })
listener.callback = handle(app)
  1. Bundle code with Bun: bun build main.ts --outdir dist --external streams --external embedded:network/http/server --external socket --external text/encoder --external text/decoder --external websocket --external headers --target node --watch
  2. Build and run with running mcconfig -d -m -p <device>

Tested device

Features

  • HTTP Server Handler
  • WebSocket helper
  • ConnInfo helper

Notes

Why not to use http module
The adapter doesn't use http module. This is because Moddable's default http module doesn't support async function. http module is simple and uses socket module, so I implemented HTTP/1.1 protocol. And http module supports only HTTP/1.1 protocol, so I implemented like that.

Why to use Hono

Because I don't know any HTTP frameworks I can use with Moddable. Also, Hono is tiny, and embedded systems has limited storage, so Hono is suitable for embedded systems to use JavaScript I thought.

Why this PR doesn't include moddable test for GitHub Actions

I don't know how to test with moddable environment on GitHub Actions. I'm not sure but maybe it's because GitHub Actions don't have GUI. I tried to fix it, but I can't fix in my skill.

Why not to publish on JSR

Because the code depends on runtime-specific APIs can't be published to JSR. jsr-io/jsr#1160

Also, thanks to @stc1988, I was able to improve this.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@nakasyou nakasyou marked this pull request as draft July 15, 2025 08:07
@codecov
Copy link

codecov bot commented Jul 16, 2025

Codecov Report

Attention: Patch coverage is 95.34368% with 21 lines in your changes missing coverage. Please review.

Project coverage is 91.79%. Comparing base (89f4c96) to head (64f59cf).

Files with missing lines Patch % Lines
src/adapter/moddable/patch.ts 0.00% 17 Missing ⚠️
build/validate-exports.ts 60.00% 2 Missing ⚠️
src/adapter/moddable/index.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4284      +/-   ##
==========================================
+ Coverage   91.61%   91.79%   +0.18%     
==========================================
  Files         170      177       +7     
  Lines       10875    11239     +364     
  Branches     3099     3280     +181     
==========================================
+ Hits         9963    10317     +354     
- Misses        911      921      +10     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nakasyou nakasyou marked this pull request as ready for review July 17, 2025 08:47
@nakasyou nakasyou marked this pull request as draft July 17, 2025 08:47
@nakasyou nakasyou marked this pull request as ready for review July 17, 2025 09:12
@yusukebe
Copy link
Member

@nakasyou

Is this ready for review? If it's ready, please ping me!

@nakasyou
Copy link
Contributor Author

@yusukebe Yes! Could you review this?

@yusukebe
Copy link
Member

Hi @nakasyou

I think this is interesting, but we have some issues.

  1. The package size will be increased. This PR has over 1300 lines of code. Regarding hono, the bundle size should be small, and the package size may not be so significant. But recently it has been increasing.
  2. The user of Moddable is a few.
  3. It's hard to reproduce in the real environment. At least, I can't set up the environment for Moddable right now, so I can't run it on a real device. If there are problems with this adapter in the future, it will be difficult to reproduce, which will be a barrier to resolving the issue.

So, I think it's better to add it to honojs/middleware, and you, @nakasyou, will be the maintainer for it.

What do you think of it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants