Technical Documentation: starfederation/datastar
โน๏ธ Provenance: Multi-Tier Ingestion ยท CodeWiki Reference
Overview (`README.md`)
Datastar
The hypermedia framework.
Datastar is a lightweight framework for building everything from simple sites to real-time collaborative web applications.
Getting started is as easy as adding a single 11.89 KiB script tag to your HTML.
<script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/[email protected]/bundles/datastar.js"></script>Then start adding frontend reactivity using declarative data-* attributes.
<input data-bind:title />
<div data-text="$title.toUpperCase()"></div>
<button data-on:click="@post('/endpoint')">Save</button>Visit the Datastar Website ยป
Watch the Videos ยป
Join the Discord Server ยป
Getting Started
Read the Getting Started Guide ยป
Contributing
Read the Contribution Guidelines ยป
Overview (`sdk/README.md`)
Datastar SDK Directory
This directory contains the SDK test suite and related tools for Datastar SDK development.
Structure
tests/- Comprehensive test suite for validating SDK implementationsdatastar-sdk-config.json- SDK configuration with constants and defaultsdatastar-sdk-config.schema.json-v1.json- JSON schema for configuration validationADR.md- Architecture Decision Record for SDK specifications
SDK Development
Configuration
The SDK configuration system provides:
- Constants: Event types, element patch modes, and other enums
- Defaults: Default values for booleans and durations
- Literals: List of known dataline literals
- JSON Schema: Validation for the configuration format
Creating a New SDK
- Read the Architecture Decision Record for SDK specifications
- Use
datastar-sdk-config.jsonas your source of constants - Implement the required
ServerSentEventGeneratorinterface - Validate your implementation using the test suite
Official SDKs
Official SDKs can be found at the Datastar website.
Support
For SDK-specific issues, please open an issue in the respective SDK repository. For general Datastar questions or cross-SDK concerns, use the main Datastar repository.
Overview (`sdk/clojure/README.md`)
Datastar Clojure SDK
The Datastar Clojure SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-clojure.
Overview (`sdk/dotnet/README.md`)
Datastar C# (.NET) SDK
The Datastar C# (.NET) SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-dotnet.
Overview (`sdk/go/README.md`)
Datastar Go SDK
The Datastar Go SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-go.
Overview (`sdk/haskell/README.md`)
Datastar Haskell SDK
The Datastar Haskell SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-haskell.
Overview (`sdk/java/README.md`)
Datastar Java SDK
The Datastar Java SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-java.
Overview (`sdk/php/README.md`)
Datastar PHP SDK
The Datastar PHP SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-php.
Overview (`sdk/python/README.md`)
Datastar Python SDK
The Datastar Python SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-python.
Overview (`sdk/ruby/README.md`)
Datastar Ruby SDK
The Datastar Ruby SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-ruby.
Overview (`sdk/rust/README.md`)
Datastar Rust SDK
The Datastar Rust SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-rust.
Overview (`sdk/test/README.md`)
Datastar SDK testing suite
This test suite uses curl(1), cat(1), sh(1), and awk(1) to test that a server respects the SDK spec found in ../README.md.
It expects a server to expose a /test endpoint that accepts all HTTP methods. The server should then use ReadSignals
to extract the events array. It must then loop through the array of events and use event.type to decide which server sent event to use. If the output of the server differs from the expected output, then an error will be printed to the terminal.
Usage
$ ./test-all.sh $server_address
Running tests with argument: $server_address
Processing GET cases...
Processing POST cases...If nothing else is output then all tests passed!
Results of the test can be found in ./get_cases/$case_name/testOutput.txt (or post_cases depending on the test).
Adding new cases
To add a new test case, simply add a folder named after the test in either ./get-cases or ./post-cases.
That folder must contain an input.json file and an output.txt file.
The input.json file must contain valid json of the following shape:
{"events":
[
{ "type": "executeScript",
"script": "console.log('hello');",
"eventId": 1,
"retryDuration": 2000,
"attributes": {
"type": "text/javascript",
"blocking": false
},
"autoRemove": false
}
]
}The output.txt file must contain valid a text/eventstream like such:
event: datastar-patch-elements
id: 1
retry: 2000
elements: <script type="text/javascript" blocking="false">console.log('hello')</script>;Special case for multiline signals
For the event type patchSignals the input.json contains the signals as JSON-object which should be converted to a single signals line in the output.txt.
If you want to output multi-line signals, then the input must contain signals-raw as String with \n in them instead. This is due to the fact that Json parsers would otherwise interpret the input file without the line breaks.
So the implementation of the server has to interpret signals-raw as String first, and if not present signals as JSON-object.
Overview (`sdk/tests/README.md`)
Datastar SDK Test Suite
A comprehensive test suite for validating Datastar SDK implementations across different languages.
Installation
As a Go Library
go get github.com/starfederation/datastar/sdk/testsUsage
Running Tests
# Run all tests against default server (localhost:7331)
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest
# Run with custom server
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -server http://localhost:8080
# Run only GET tests
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -type get
# Run only POST tests
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -type post
# Verbose output
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -vUsing go test directly
# Clone the repository and navigate to tests directory
cd sdk/tests
# Run all tests
go test -v
# Run with custom server
TEST_SERVER_URL=http://localhost:8080 go test -vTest Structure
The test suite includes:
- GET Tests (
golden/get/): Test cases for GET endpoints - POST Tests (
golden/post/): Test cases for POST endpoints
Each test case contains:
input.json: The request payloadoutput.txt: The expected SSE response
Features
- HTML Normalization: Automatically handles HTML attribute ordering differences
- Embedded Test Data: All test cases are embedded in the binary for portability
- Flexible Runner: Can be used as a CLI tool or Go library
- Detailed Output: Clear error messages and debug information
For SDK Implementers
To validate your SDK implementation:
- Start your test server on port 7331 (or specify a different port)
- Implement the
/testendpoint that:- For GET: reads the
datastarquery parameter - For POST: reads the JSON body
- Returns appropriate SSE responses
- For GET: reads the
- Run
go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latestto validate your implementation
Test Endpoint Requirements
The /test endpoint should:
- Use ReadSignals to extract the
eventsarray from the request - Loop through the array of events
- Use
event.typeto decide which server sent event to generate - Return the appropriate SSE response
Input Format
{
"events": [
{
"type": "executeScript",
"script": "console.log('hello');",
"eventId": "event1",
"retryDuration": 2000,
"attributes": {
"type": "text/javascript",
"blocking": "false"
},
"autoRemove": false
}
]
}Output Format
event: datastar-patch-elements
id: event1
retry: 2000
data: mode append
data: selector body
data: elements <script type="text/javascript" blocking="false">console.log('hello');</script>Adding New Test Cases
To add a new test case:
- Create a folder in
golden/get/orgolden/post/named after your test - Add an
input.jsonfile with the request payload - Add an
output.txtfile with the expected SSE response
Special Cases
Multiline Signals
For patchSignals events, if you need multiline output:
- Use
signals-rawas a string with\ncharacters instead ofsignalsas a JSON object - The server should check for
signals-rawfirst, then fall back tosignals
Test Cases
The test suite covers:
- Element patching (single and multiline)
- Signal patching
- Script execution
- Element/signal removal
- Various SSE formatting scenarios
- Edge cases and error conditions
Overview (`sdk/typescript/README.md`)
Datastar TypeScript SDK
The Datastar TypeScript SDK has moved to a dedicated repo at https://github.com/starfederation/datastar-typescript.
Overview (`sdk/zig/README.md`)
Datastar Zig SDK
The Datastar Zig SDK has been deprecated.
--- METRICS ---
- Files Extracted: 15
- Estimated Token Budget: ~2776 tokens
- Canonical Reference: https://codewiki.google/github.com/starfederation/datastar