@barefootjs/streaming 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/html.d.ts +46 -0
- package/dist/id.d.ts +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +27 -0
- package/package.json +41 -0
- package/src/html.ts +65 -0
- package/src/id.ts +20 -0
- package/src/index.ts +7 -0
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BarefootJS Streaming - HTML Chunk Generators
|
|
3
|
+
*
|
|
4
|
+
* Backend-agnostic functions for generating OOS (Out-of-Order Streaming)
|
|
5
|
+
* HTML chunks. These produce plain strings that any HTTP server can
|
|
6
|
+
* write to a response stream.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Render an async boundary placeholder with fallback content.
|
|
10
|
+
*
|
|
11
|
+
* This HTML should be sent in the initial response (first flush)
|
|
12
|
+
* and will be replaced when the async data resolves.
|
|
13
|
+
*
|
|
14
|
+
* @param id - Unique boundary ID (e.g., "a0")
|
|
15
|
+
* @param fallbackHtml - HTML string to show while loading
|
|
16
|
+
* @param tag - Wrapper element tag (default: "div")
|
|
17
|
+
* @returns HTML string: `<div bf-async="a0">...fallback...</div>`
|
|
18
|
+
*/
|
|
19
|
+
export declare function renderAsyncBoundary(id: string, fallbackHtml: string, tag?: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Render a resolve chunk for an async boundary.
|
|
22
|
+
*
|
|
23
|
+
* This HTML should be appended to the response stream after the async
|
|
24
|
+
* data resolves. It contains the resolved content in a `<template>`
|
|
25
|
+
* element and an inline `<script>` that triggers the swap.
|
|
26
|
+
*
|
|
27
|
+
* @param id - Boundary ID matching the placeholder
|
|
28
|
+
* @param contentHtml - Resolved HTML content (including hydration markers)
|
|
29
|
+
* @returns HTML string with template + swap script
|
|
30
|
+
*/
|
|
31
|
+
export declare function renderAsyncResolve(id: string, contentHtml: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Generate the inline bootstrap script for OOS streaming.
|
|
34
|
+
*
|
|
35
|
+
* This script must be included once per page, before any streaming
|
|
36
|
+
* resolve chunks arrive. It defines the `__bf_swap` function that
|
|
37
|
+
* swaps fallback content with resolved content.
|
|
38
|
+
*
|
|
39
|
+
* The script is intentionally minimal (~300 bytes) and has no
|
|
40
|
+
* dependencies on the BarefootJS runtime — it only manipulates DOM.
|
|
41
|
+
* Full hydration is triggered via `__bf_hydrate` which is set up
|
|
42
|
+
* by the BarefootJS client runtime's `setupStreaming()`.
|
|
43
|
+
*
|
|
44
|
+
* @returns A `<script>` tag string
|
|
45
|
+
*/
|
|
46
|
+
export declare function streamingBootstrap(): string;
|
package/dist/id.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BarefootJS Streaming - Async Boundary ID Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates unique, sequential IDs for async streaming boundaries.
|
|
5
|
+
* Each page render should create a fresh generator instance.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AsyncIdGenerator {
|
|
8
|
+
private counter;
|
|
9
|
+
/** Generate the next async boundary ID (e.g., "a0", "a1", "a2"). */
|
|
10
|
+
next(): string;
|
|
11
|
+
/** Reset the counter (e.g., between requests). */
|
|
12
|
+
reset(): void;
|
|
13
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/html.ts
|
|
2
|
+
import { BF_ASYNC, BF_ASYNC_RESOLVE } from "@barefootjs/shared";
|
|
3
|
+
function renderAsyncBoundary(id, fallbackHtml, tag = "div") {
|
|
4
|
+
return `<${tag} ${BF_ASYNC}="${id}">${fallbackHtml}</${tag}>`;
|
|
5
|
+
}
|
|
6
|
+
function renderAsyncResolve(id, contentHtml) {
|
|
7
|
+
return `<template ${BF_ASYNC_RESOLVE}="${id}">${contentHtml}</template>` + `<script>__bf_swap("${id}")</script>`;
|
|
8
|
+
}
|
|
9
|
+
function streamingBootstrap() {
|
|
10
|
+
return `<script>(function(){function s(id){var a=document.querySelector('[${BF_ASYNC}="'+id+'"]');var t=document.querySelector('template[${BF_ASYNC_RESOLVE}="'+id+'"]');if(!a||!t)return;a.replaceChildren(t.content.cloneNode(true));a.removeAttribute('${BF_ASYNC}');t.remove();requestAnimationFrame(function(){if(window.__bf_hydrate)window.__bf_hydrate()})};window.__bf_swap=s})()</script>`;
|
|
11
|
+
}
|
|
12
|
+
// src/id.ts
|
|
13
|
+
class AsyncIdGenerator {
|
|
14
|
+
counter = 0;
|
|
15
|
+
next() {
|
|
16
|
+
return `a${this.counter++}`;
|
|
17
|
+
}
|
|
18
|
+
reset() {
|
|
19
|
+
this.counter = 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
streamingBootstrap,
|
|
24
|
+
renderAsyncResolve,
|
|
25
|
+
renderAsyncBoundary,
|
|
26
|
+
AsyncIdGenerator
|
|
27
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@barefootjs/streaming",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Backend-agnostic SSR streaming helpers for BarefootJS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "bun run build:js && bun run build:types",
|
|
20
|
+
"build:js": "bun build ./src/index.ts --outdir ./dist --format esm --external @barefootjs/shared",
|
|
21
|
+
"build:types": "tsgo --emitDeclarationOnly --outDir ./dist",
|
|
22
|
+
"clean": "rm -rf dist",
|
|
23
|
+
"prepack": "node ../../scripts/swap-publish-config.mjs pack",
|
|
24
|
+
"postpack": "node ../../scripts/swap-publish-config.mjs unpack"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"barefoot",
|
|
28
|
+
"streaming",
|
|
29
|
+
"ssr"
|
|
30
|
+
],
|
|
31
|
+
"author": "kobaken <kentafly88@gmail.com>",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/piconic-ai/barefootjs",
|
|
36
|
+
"directory": "packages/streaming"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@barefootjs/shared": "workspace:*"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/html.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BarefootJS Streaming - HTML Chunk Generators
|
|
3
|
+
*
|
|
4
|
+
* Backend-agnostic functions for generating OOS (Out-of-Order Streaming)
|
|
5
|
+
* HTML chunks. These produce plain strings that any HTTP server can
|
|
6
|
+
* write to a response stream.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { BF_ASYNC, BF_ASYNC_RESOLVE } from '@barefootjs/shared'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Render an async boundary placeholder with fallback content.
|
|
13
|
+
*
|
|
14
|
+
* This HTML should be sent in the initial response (first flush)
|
|
15
|
+
* and will be replaced when the async data resolves.
|
|
16
|
+
*
|
|
17
|
+
* @param id - Unique boundary ID (e.g., "a0")
|
|
18
|
+
* @param fallbackHtml - HTML string to show while loading
|
|
19
|
+
* @param tag - Wrapper element tag (default: "div")
|
|
20
|
+
* @returns HTML string: `<div bf-async="a0">...fallback...</div>`
|
|
21
|
+
*/
|
|
22
|
+
export function renderAsyncBoundary(
|
|
23
|
+
id: string,
|
|
24
|
+
fallbackHtml: string,
|
|
25
|
+
tag: string = 'div',
|
|
26
|
+
): string {
|
|
27
|
+
return `<${tag} ${BF_ASYNC}="${id}">${fallbackHtml}</${tag}>`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Render a resolve chunk for an async boundary.
|
|
32
|
+
*
|
|
33
|
+
* This HTML should be appended to the response stream after the async
|
|
34
|
+
* data resolves. It contains the resolved content in a `<template>`
|
|
35
|
+
* element and an inline `<script>` that triggers the swap.
|
|
36
|
+
*
|
|
37
|
+
* @param id - Boundary ID matching the placeholder
|
|
38
|
+
* @param contentHtml - Resolved HTML content (including hydration markers)
|
|
39
|
+
* @returns HTML string with template + swap script
|
|
40
|
+
*/
|
|
41
|
+
export function renderAsyncResolve(id: string, contentHtml: string): string {
|
|
42
|
+
return (
|
|
43
|
+
`<template ${BF_ASYNC_RESOLVE}="${id}">${contentHtml}</template>` +
|
|
44
|
+
`<script>__bf_swap("${id}")</script>`
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Generate the inline bootstrap script for OOS streaming.
|
|
50
|
+
*
|
|
51
|
+
* This script must be included once per page, before any streaming
|
|
52
|
+
* resolve chunks arrive. It defines the `__bf_swap` function that
|
|
53
|
+
* swaps fallback content with resolved content.
|
|
54
|
+
*
|
|
55
|
+
* The script is intentionally minimal (~300 bytes) and has no
|
|
56
|
+
* dependencies on the BarefootJS runtime — it only manipulates DOM.
|
|
57
|
+
* Full hydration is triggered via `__bf_hydrate` which is set up
|
|
58
|
+
* by the BarefootJS client runtime's `setupStreaming()`.
|
|
59
|
+
*
|
|
60
|
+
* @returns A `<script>` tag string
|
|
61
|
+
*/
|
|
62
|
+
export function streamingBootstrap(): string {
|
|
63
|
+
// Minified inline resolver — no external dependencies
|
|
64
|
+
return `<script>(function(){function s(id){var a=document.querySelector('[${BF_ASYNC}="'+id+'"]');var t=document.querySelector('template[${BF_ASYNC_RESOLVE}="'+id+'"]');if(!a||!t)return;a.replaceChildren(t.content.cloneNode(true));a.removeAttribute('${BF_ASYNC}');t.remove();requestAnimationFrame(function(){if(window.__bf_hydrate)window.__bf_hydrate()})};window.__bf_swap=s})()</script>`
|
|
65
|
+
}
|
package/src/id.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BarefootJS Streaming - Async Boundary ID Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates unique, sequential IDs for async streaming boundaries.
|
|
5
|
+
* Each page render should create a fresh generator instance.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export class AsyncIdGenerator {
|
|
9
|
+
private counter = 0
|
|
10
|
+
|
|
11
|
+
/** Generate the next async boundary ID (e.g., "a0", "a1", "a2"). */
|
|
12
|
+
next(): string {
|
|
13
|
+
return `a${this.counter++}`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Reset the counter (e.g., between requests). */
|
|
17
|
+
reset(): void {
|
|
18
|
+
this.counter = 0
|
|
19
|
+
}
|
|
20
|
+
}
|