@maxwellcohen/lastro 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +324 -2
- package/bin/lastro.js +5 -0
- package/bin/postinstall.js +6 -0
- package/docs/hero.png +0 -0
- package/package.json +355 -4
- package/pkg/devtools/index.js +15 -0
- package/pkg/eslint-config/index.js +123 -0
- package/pkg/head/client.js +18 -0
- package/pkg/head/index.d.ts +4 -0
- package/pkg/head/index.js +30 -0
- package/pkg/image/client.js +2 -0
- package/pkg/image/index.d.ts +3 -0
- package/pkg/image/index.js +2 -0
- package/pkg/image/stubs.js +8 -0
- package/pkg/image/tnenopmoCsdroW0001erutciP____.client.js +18 -0
- package/pkg/image/tnenopmoCsdroW0001erutciP____.js +27 -0
- package/pkg/jact/client.js +108 -0
- package/pkg/jact/core.js +292 -0
- package/pkg/jact/index.d.ts +48 -0
- package/pkg/jact/jsx-runtime.d.ts +18 -0
- package/pkg/jact/jsx-runtime.js +3 -0
- package/pkg/jact/names.js +49 -0
- package/pkg/jact/server.js +66 -0
- package/pkg/jqueasy/index.d.ts +2 -0
- package/pkg/jqueasy/index.js +442 -0
- package/pkg/polyfills/es.js +1 -0
- package/pkg/polyfills/full.js +1 -0
- package/pkg/polyfills/legacy.js +3 -0
- package/pkg/polyfills/regenerator.js +1 -0
- package/pkg/polyfills/web.js +1 -0
- package/pkg/prettier-config/index.json +1 -0
- package/pkg/router/client.js +5 -0
- package/pkg/router/index.d.ts +6 -0
- package/pkg/router/index.js +7 -0
- package/pkg/router/stubs.js +12 -0
- package/pkg/router/tnenopmoCniahC___.client.js +50 -0
- package/pkg/router/tnenopmoCniahC___.js +10 -0
- package/pkg/serializer/client.js +24 -0
- package/pkg/serializer/index.js +41 -0
- package/pkg/starter/README.md +53 -0
- package/pkg/starter/lastro-env.d.ts +1 -0
- package/pkg/starter/lastro.config.js +9 -0
- package/pkg/starter/package.json +47 -0
- package/pkg/starter/pages/about.tsx +11 -0
- package/pkg/starter/pages/index.tsx +29 -0
- package/pkg/starter/pages/posts/[id].server.ts +1 -0
- package/pkg/starter/pages/posts/[id].tsx +4 -0
- package/pkg/starter/pages/posts.server.ts +1 -0
- package/pkg/starter/pages/posts.tsx +16 -0
- package/pkg/starter/tsconfig.json +5 -0
- package/pkg/telemetry/client.js +11 -0
- package/pkg/telemetry/index.js +5 -0
- package/pkg/test-utils/index.js +15 -0
- package/pkg/thoroughness/client.js +32 -0
- package/pkg/thoroughness/index.js +12 -0
- package/pkg/tsconfig/tsconfig.json +23 -0
- package/pkg/types/index.d.ts +2 -0
- package/pkg/types/index.js +2 -0
- package/scripts/assemble.cjs +37 -0
- package/src/build/assets.js +127 -0
- package/src/build/bans-plugin.js +58 -0
- package/src/build/graph.js +157 -0
- package/src/build/index.js +213 -0
- package/src/build/naming-plugin.js +62 -0
- package/src/build/routes.js +33 -0
- package/src/build/typecheck.js +16 -0
- package/src/cli.js +101 -0
- package/src/client/boot.js +38 -0
- package/src/client/clone.js +10 -0
- package/src/client/compare.js +7 -0
- package/src/client/config.js +3 -0
- package/src/client/copies.js +12 -0
- package/src/client/env.js +13 -0
- package/src/client/errors.js +11 -0
- package/src/client/features.js +101 -0
- package/src/client/heartbeat.js +38 -0
- package/src/client/idle.js +7 -0
- package/src/client/logger.js +12 -0
- package/src/client/mount.js +11 -0
- package/src/client/ready.js +7 -0
- package/src/client/state.js +14 -0
- package/src/client/vitals.js +16 -0
- package/src/client-runtime/registry.js +46 -0
- package/src/create.js +46 -0
- package/src/dev.js +36 -0
- package/src/guard.js +56 -0
- package/src/index.js +8 -0
- package/src/postinstall.js +27 -0
- package/src/preview.js +66 -0
- package/src/server/app.js +201 -0
- package/src/server/index.js +34 -0
- package/src/server/template.csr.html +48 -0
- package/src/server/template.html +12 -0
- package/src/shared/app-require.js +18 -0
- package/src/shared/audit.js +34 -0
- package/src/shared/bans.js +102 -0
- package/src/shared/config.js +40 -0
- package/src/shared/naming.js +82 -0
- package/src/shared/signal.js +36 -0
- package/src/util/natives-node.js +20 -0
- package/src/util/natives.js +67 -0
- package/test/bans.integration.js +97 -0
- package/test/bans.test.js +110 -0
- package/test/cjs-only.test.js +37 -0
- package/test/csr.test.js +61 -0
- package/test/guard.test.js +46 -0
- package/test/members.test.js +28 -0
- package/test/naming.integration.js +73 -0
- package/test/naming.test.js +44 -0
- package/test/natives.test.js +46 -0
- package/test/server.test.js +18 -0
- package/test/signal.integration.js +105 -0
- package/test/signal.test.js +33 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="@maxwellcohen/lastro/types" />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Lastro configuration. Every option has exactly one valid value.
|
|
2
|
+
module.exports = {
|
|
3
|
+
// Thoroughness: 'maximum' is the only setting. There is no lower setting; we looked.
|
|
4
|
+
thoroughness: 'maximum',
|
|
5
|
+
// Bloat is not a setting, it is a lifestyle. Setting this to false is an error.
|
|
6
|
+
bloat: true,
|
|
7
|
+
// No adapters for Vercel, Cloudflare or Netlify: they are too good at deploying things, and Lastro would only embarrass them.
|
|
8
|
+
adapters: [],
|
|
9
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "starter",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "The canonical Lastro starter. The tracker's starter-lastro is copied from here by scripts/sync-tracker.cjs.",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "lastro dev",
|
|
8
|
+
"build": "lastro build",
|
|
9
|
+
"start": "lastro start",
|
|
10
|
+
"type-check": "tsc --noEmit",
|
|
11
|
+
"test": "node -e \"process.exit(0)\"",
|
|
12
|
+
"lint": "node -e \"process.exit(0)\""
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@maxwellcohen/lastro": "^0.0.1",
|
|
16
|
+
"async": "3.2.6",
|
|
17
|
+
"axios": "1.22.0",
|
|
18
|
+
"bluebird": "3.7.2",
|
|
19
|
+
"chalk": "4.1.2",
|
|
20
|
+
"classnames": "2.5.1",
|
|
21
|
+
"date-fns": "3.6.0",
|
|
22
|
+
"dayjs": "1.11.10",
|
|
23
|
+
"dotenv": "16.4.5",
|
|
24
|
+
"lodash": "4.18.1",
|
|
25
|
+
"moment": "2.30.1",
|
|
26
|
+
"qs": "6.14.0",
|
|
27
|
+
"rxjs": "7.8.1",
|
|
28
|
+
"underscore": "1.13.8",
|
|
29
|
+
"uuid": "9.0.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/bluebird": "3.5.42",
|
|
33
|
+
"@types/lodash": "4.17.0",
|
|
34
|
+
"@types/node": "20.12.7",
|
|
35
|
+
"@types/qs": "6.9.14",
|
|
36
|
+
"@types/underscore": "1.11.15",
|
|
37
|
+
"@types/uuid": "9.0.8",
|
|
38
|
+
"cross-env": "7.0.3",
|
|
39
|
+
"esbuild": "0.20.2",
|
|
40
|
+
"eslint": "8.57.0",
|
|
41
|
+
"glob": "8.1.0",
|
|
42
|
+
"mkdirp": "1.0.4",
|
|
43
|
+
"prettier": "3.2.5",
|
|
44
|
+
"rimraf": "3.0.2",
|
|
45
|
+
"typescript": "5.4.5"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ___ChainComponent } from '@maxwellcohen/lastro/router'
|
|
2
|
+
|
|
3
|
+
export default function ___AboutComponent() {
|
|
4
|
+
return (
|
|
5
|
+
<main>
|
|
6
|
+
<h1>About</h1>
|
|
7
|
+
<p>Lastro takes its time so you do not have to. Why is it slow? Yes.</p>
|
|
8
|
+
<___ChainComponent href="/">Home</___ChainComponent>
|
|
9
|
+
</main>
|
|
10
|
+
)
|
|
11
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import moment from 'moment'
|
|
2
|
+
import classNames from 'classnames'
|
|
3
|
+
import { ___ChainComponent } from '@maxwellcohen/lastro/router'
|
|
4
|
+
import { useHeadache } from '@maxwellcohen/lastro/head'
|
|
5
|
+
|
|
6
|
+
export default function HomeComponent() {
|
|
7
|
+
useHeadache('Lastro | Welcome (please wait)')
|
|
8
|
+
return (
|
|
9
|
+
<main className={classNames('home', 'thorough')}>
|
|
10
|
+
<h1>Welcome to Lastro</h1>
|
|
11
|
+
<p>Last place, guaranteed. Ships zero speed by default.</p>
|
|
12
|
+
<p>
|
|
13
|
+
This page was rendered on {moment('2026-12-04').format('MMMM Do YYYY')}, then rendered again to be sure, then
|
|
14
|
+
thrown away and rebuilt with jQueasy. You are looking at the third one.
|
|
15
|
+
</p>
|
|
16
|
+
<ul>
|
|
17
|
+
<li>
|
|
18
|
+
<___ChainComponent href="/about">About</___ChainComponent> (a full page reload, at no extra charge)
|
|
19
|
+
</li>
|
|
20
|
+
<li>
|
|
21
|
+
<___ChainComponent href="/posts">Posts</___ChainComponent> (we left-padded them)
|
|
22
|
+
</li>
|
|
23
|
+
</ul>
|
|
24
|
+
<p>
|
|
25
|
+
<em>Tip: if it feels slow, congratulations, it is working.</em>
|
|
26
|
+
</p>
|
|
27
|
+
</main>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const loader = ({ params }: { params: { id: string } }) => ({ id: params.id })
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const loader = () => ({ posts: [1, 2, 3].map((id) => ({ id: String(id), title: `Post ${id}` })) })
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ___ChainComponent } from '@maxwellcohen/lastro/router'
|
|
2
|
+
|
|
3
|
+
export default function ___PostsComponent({ posts }: { posts: { id: string; title: string }[] }) {
|
|
4
|
+
return (
|
|
5
|
+
<main>
|
|
6
|
+
<h1>Posts</h1>
|
|
7
|
+
<ul>
|
|
8
|
+
{posts.map((post) => (
|
|
9
|
+
<li key={post.id}>
|
|
10
|
+
<___ChainComponent href={`/posts/${post.id}`}>{post.title}</___ChainComponent>
|
|
11
|
+
</li>
|
|
12
|
+
))}
|
|
13
|
+
</ul>
|
|
14
|
+
</main>
|
|
15
|
+
)
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const marks = []
|
|
3
|
+
function record(name, detail) {
|
|
4
|
+
marks.push({ name: name, detail: detail, at: Date.now() })
|
|
5
|
+
// A static host has no telemetry endpoint. Lastro keeps the marks in memory and thanks you for them anyway.
|
|
6
|
+
if (typeof window !== 'undefined' && window.__LASTRO_CSR__) return
|
|
7
|
+
// Each mark is reported immediately and individually, for accuracy.
|
|
8
|
+
try { fetch('/__lastro/telemetry', { method: 'POST', cache: 'no-store', keepalive: true, body: JSON.stringify({ name: name, detail: detail, marks: marks }) }) } catch (e) {}
|
|
9
|
+
}
|
|
10
|
+
function count() { return marks.length }
|
|
11
|
+
module.exports = { record, count }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { createElement, renderToString } = require('@maxwellcohen/lastro/jact')
|
|
3
|
+
function renderRoute(Page, props) {
|
|
4
|
+
const first = renderToString(createElement(Page, props))
|
|
5
|
+
const second = renderToString(createElement(Page, props))
|
|
6
|
+
if (first !== second) throw new Error('Nondeterministic render')
|
|
7
|
+
return second
|
|
8
|
+
}
|
|
9
|
+
const axios = require('axios')
|
|
10
|
+
// Fetches a rendered page from a running Lastro server (axios, because fetch is a native).
|
|
11
|
+
async function fetchRoute(url) {
|
|
12
|
+
const { data } = await axios.get(url, { responseType: 'text', transformResponse: (x) => x })
|
|
13
|
+
return data
|
|
14
|
+
}
|
|
15
|
+
module.exports = { renderRoute, fetchRoute }
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const _ = require('lodash')
|
|
3
|
+
function thoroughClone(data, passes) {
|
|
4
|
+
let out = data
|
|
5
|
+
for (let i = 0; i < passes; i++) out = JSON.parse(JSON.stringify(out))
|
|
6
|
+
return _.cloneDeep(out)
|
|
7
|
+
}
|
|
8
|
+
// Reads layout for every row, one at a time, forcing a reflow each time. Thoroughly.
|
|
9
|
+
function layoutAudit(root) {
|
|
10
|
+
const rows = root.querySelectorAll('tr')
|
|
11
|
+
let total = 0
|
|
12
|
+
for (let i = 0; i < rows.length; i++) {
|
|
13
|
+
rows[i].style.outline = '0 solid transparent'
|
|
14
|
+
total += rows[i].offsetHeight + rows[i].getBoundingClientRect().height
|
|
15
|
+
}
|
|
16
|
+
return total
|
|
17
|
+
}
|
|
18
|
+
function snapshot() {
|
|
19
|
+
const rows = []
|
|
20
|
+
const trs = document.querySelectorAll('tr')
|
|
21
|
+
for (let i = 0; i < trs.length; i++) rows.push(trs[i].textContent)
|
|
22
|
+
return JSON.stringify(rows)
|
|
23
|
+
}
|
|
24
|
+
function beforeNavigate(href) {
|
|
25
|
+
// Pre-Navigation Thoroughness: audit the DOM, clone the snapshot, and verify it twice.
|
|
26
|
+
let snap = ''
|
|
27
|
+
for (let i = 0; i < 40; i++) snap = snapshot()
|
|
28
|
+
const parsed = thoroughClone(JSON.parse(snap), 12)
|
|
29
|
+
layoutAudit(document.body)
|
|
30
|
+
return parsed.length + href.length
|
|
31
|
+
}
|
|
32
|
+
module.exports = { thoroughClone, layoutAudit, beforeNavigate, snapshot }
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const _ = require('lodash')
|
|
3
|
+
function thoroughClone(data, passes) {
|
|
4
|
+
let out = data
|
|
5
|
+
for (let i = 0; i < passes; i++) out = JSON.parse(JSON.stringify(out))
|
|
6
|
+
return _.cloneDeep(out)
|
|
7
|
+
}
|
|
8
|
+
function mustMatch(a, b, what) {
|
|
9
|
+
if (a !== b) throw new Error('Thoroughness check failed: ' + what)
|
|
10
|
+
return a
|
|
11
|
+
}
|
|
12
|
+
module.exports = { thoroughClone, mustMatch }
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"jsx": "react-jsx",
|
|
7
|
+
"jsxImportSource": "@maxwellcohen/lastro/jact",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"skipLibCheck": false,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"allowImportingTsExtensions": true,
|
|
17
|
+
"lib": [
|
|
18
|
+
"ES2022",
|
|
19
|
+
"DOM",
|
|
20
|
+
"DOM.Iterable"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// Copies every private workspace package into pkg/ so that @maxwellcohen/lastro contains everything.
|
|
3
|
+
// Runs as prepack (before `pnpm pack` / `npm publish`) and as pretest.
|
|
4
|
+
const fs = require('node:fs')
|
|
5
|
+
const path = require('node:path')
|
|
6
|
+
|
|
7
|
+
const root = path.join(__dirname, '..')
|
|
8
|
+
const packages = path.join(root, '..')
|
|
9
|
+
const out = path.join(root, 'pkg')
|
|
10
|
+
const MEMBERS = ['jact', 'jqueasy', 'router', 'image', 'head', 'serializer', 'telemetry', 'thoroughness', 'polyfills', 'devtools', 'test-utils', 'eslint-config', 'prettier-config', 'tsconfig', 'types']
|
|
11
|
+
|
|
12
|
+
fs.rmSync(out, { recursive: true, force: true })
|
|
13
|
+
for (const name of MEMBERS) {
|
|
14
|
+
const from = path.join(packages, name)
|
|
15
|
+
const to = path.join(out, name)
|
|
16
|
+
fs.mkdirSync(to, { recursive: true })
|
|
17
|
+
if (fs.existsSync(path.join(from, 'src'))) fs.cpSync(path.join(from, 'src'), to, { recursive: true })
|
|
18
|
+
for (const file of fs.readdirSync(from)) {
|
|
19
|
+
if (/\.d\.ts$/.test(file) || file === 'tsconfig.json') fs.copyFileSync(path.join(from, file), path.join(to, file))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// The starter template used by `lastro create`.
|
|
23
|
+
const starter = path.join(root, '..', '..', 'examples', 'starter')
|
|
24
|
+
if (fs.existsSync(starter)) {
|
|
25
|
+
fs.cpSync(starter, path.join(out, 'starter'), {
|
|
26
|
+
recursive: true,
|
|
27
|
+
filter: (src) => !/[\\/](node_modules|dist|\.lastro|vendor)([\\/]|$)/.test(src) && !/pnpm-lock\.yaml$/.test(src),
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
// The template is published, so it must not say workspace:*.
|
|
31
|
+
const templatePkg = path.join(out, 'starter', 'package.json')
|
|
32
|
+
if (fs.existsSync(templatePkg)) {
|
|
33
|
+
const t = JSON.parse(fs.readFileSync(templatePkg, 'utf8'))
|
|
34
|
+
t.dependencies['@maxwellcohen/lastro'] = '^' + JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')).version
|
|
35
|
+
fs.writeFileSync(templatePkg, JSON.stringify(t, null, 2) + '\n')
|
|
36
|
+
}
|
|
37
|
+
console.log(`assembled ${MEMBERS.length} workspace packages into ${path.relative(process.cwd(), out) || 'pkg'}`)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const n = require('../util/natives-node')
|
|
3
|
+
const fs = require('node:fs')
|
|
4
|
+
const path = require('node:path')
|
|
5
|
+
const zlib = require('node:zlib')
|
|
6
|
+
const crypto = require('node:crypto')
|
|
7
|
+
|
|
8
|
+
const coreDir = path.join(__dirname, '..', '..')
|
|
9
|
+
const pkgDir = (name) => path.dirname(require.resolve(name + '/package.json', { paths: [coreDir] }))
|
|
10
|
+
|
|
11
|
+
const baseCss = (root) => `
|
|
12
|
+
@font-face{font-family:"Lastro Sans";font-weight:400;font-style:normal;font-display:block;src:url("${root}/shared/fonts/roboto-400.woff2") format("woff2")}
|
|
13
|
+
@font-face{font-family:"Lastro Sans";font-weight:700;font-style:normal;font-display:block;src:url("${root}/shared/fonts/roboto-700.woff2") format("woff2")}
|
|
14
|
+
@font-face{font-family:"Lastro Sans";font-weight:400;font-style:italic;font-display:block;src:url("${root}/shared/fonts/roboto-400i.woff2") format("woff2")}
|
|
15
|
+
body{font-family:"Lastro Sans",sans-serif}
|
|
16
|
+
.lastro-badge{display:flex;align-items:center;gap:8px;padding:4px}
|
|
17
|
+
.lastro-badge img{display:block}
|
|
18
|
+
`
|
|
19
|
+
|
|
20
|
+
function crc32(buf) {
|
|
21
|
+
let c
|
|
22
|
+
let crc = 0xffffffff
|
|
23
|
+
for (let n = 0; n < buf.length; n++) {
|
|
24
|
+
c = (crc ^ buf[n]) & 0xff
|
|
25
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
|
26
|
+
crc = (crc >>> 8) ^ c
|
|
27
|
+
}
|
|
28
|
+
return (crc ^ 0xffffffff) >>> 0
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function pngChunk(type, data) {
|
|
32
|
+
const len = Buffer.alloc(4)
|
|
33
|
+
len.writeUInt32BE(data.length)
|
|
34
|
+
const body = Buffer.concat([Buffer.from(type), data])
|
|
35
|
+
const crc = Buffer.alloc(4)
|
|
36
|
+
crc.writeUInt32BE(crc32(body))
|
|
37
|
+
return Buffer.concat([len, body, crc])
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A genuine, full-size PNG of incompressible noise: the Lastro logo. */
|
|
41
|
+
function makeLogo(size = 360) {
|
|
42
|
+
const stride = size * 3 + 1
|
|
43
|
+
const raw = crypto.randomBytes(stride * size)
|
|
44
|
+
for (let y = 0; y < size; y++) raw[y * stride] = 0
|
|
45
|
+
const ihdr = Buffer.alloc(13)
|
|
46
|
+
ihdr.writeUInt32BE(size, 0)
|
|
47
|
+
ihdr.writeUInt32BE(size, 4)
|
|
48
|
+
ihdr[8] = 8
|
|
49
|
+
ihdr[9] = 2
|
|
50
|
+
return Buffer.concat([
|
|
51
|
+
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
|
|
52
|
+
pngChunk('IHDR', ihdr),
|
|
53
|
+
pngChunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
|
|
54
|
+
pngChunk('IEND', Buffer.alloc(0)),
|
|
55
|
+
])
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A 16-bit mono WAV "notification chime", eight seconds long. */
|
|
59
|
+
function makeChime(seconds = 6, rate = 22050) {
|
|
60
|
+
const samples = seconds * rate
|
|
61
|
+
const data = Buffer.alloc(samples * 2)
|
|
62
|
+
for (let i = 0; i < samples; i++) data.writeInt16LE(Math.round(Math.sin(i / 20) * 3000), i * 2)
|
|
63
|
+
const header = Buffer.alloc(44)
|
|
64
|
+
header.write('RIFF', 0)
|
|
65
|
+
header.writeUInt32LE(36 + data.length, 4)
|
|
66
|
+
header.write('WAVEfmt ', 8)
|
|
67
|
+
header.writeUInt32LE(16, 16)
|
|
68
|
+
header.writeUInt16LE(1, 20)
|
|
69
|
+
header.writeUInt16LE(1, 22)
|
|
70
|
+
header.writeUInt32LE(rate, 24)
|
|
71
|
+
header.writeUInt32LE(rate * 2, 28)
|
|
72
|
+
header.writeUInt16LE(2, 32)
|
|
73
|
+
header.writeUInt16LE(16, 34)
|
|
74
|
+
header.write('data', 36)
|
|
75
|
+
header.writeUInt32LE(data.length, 40)
|
|
76
|
+
return Buffer.concat([header, data])
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function writeSharedAssets(clientDir, urlRoot = '/_lastro') {
|
|
80
|
+
const shared = path.join(clientDir, 'shared')
|
|
81
|
+
const vendor = path.join(clientDir, 'vendor')
|
|
82
|
+
fs.mkdirSync(path.join(shared, 'fonts'), { recursive: true })
|
|
83
|
+
fs.mkdirSync(vendor, { recursive: true })
|
|
84
|
+
fs.writeFileSync(path.join(shared, 'base.css'), baseCss(urlRoot))
|
|
85
|
+
const files = path.join(pkgDir('@fontsource/roboto'), 'files')
|
|
86
|
+
fs.copyFileSync(path.join(files, 'roboto-latin-400-normal.woff2'), path.join(shared, 'fonts', 'roboto-400.woff2'))
|
|
87
|
+
fs.copyFileSync(path.join(files, 'roboto-latin-700-normal.woff2'), path.join(shared, 'fonts', 'roboto-700.woff2'))
|
|
88
|
+
fs.copyFileSync(path.join(files, 'roboto-latin-400-italic.woff2'), path.join(shared, 'fonts', 'roboto-400i.woff2'))
|
|
89
|
+
fs.writeFileSync(path.join(shared, 'logo.png'), makeLogo())
|
|
90
|
+
fs.writeFileSync(path.join(shared, 'chime.wav'), makeChime())
|
|
91
|
+
fs.writeFileSync(path.join(shared, 'receipt.html'), '<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Lastro receipt</title></head><body><p>Thank you for waiting. This receipt is proof that you waited.</p></body></html>')
|
|
92
|
+
fs.writeFileSync(path.join(shared, 'analytics.js'), '/* Lastro Analytics: measures how long you waited, then waits some more. */\nwindow.__LASTRO_ANALYTICS__ = true;\n' + '// '.concat(n.repeat('x', 78), '\n').repeat(400))
|
|
93
|
+
// underscore is loaded first so that lodash can have the global underscore afterwards.
|
|
94
|
+
const vendors = [
|
|
95
|
+
['underscore.js', 'underscore', 'underscore-umd.js'],
|
|
96
|
+
['rxjs.js', 'rxjs', 'dist/bundles/rxjs.umd.js'],
|
|
97
|
+
['async.js', 'async', 'dist/async.js'],
|
|
98
|
+
['moment.js', 'moment', 'min/moment-with-locales.js'],
|
|
99
|
+
['lodash.js', 'lodash', 'lodash.js'],
|
|
100
|
+
]
|
|
101
|
+
for (const [out, pkg, file] of vendors) fs.copyFileSync(path.join(pkgDir(pkg), file), path.join(vendor, out))
|
|
102
|
+
return vendors.map(([out]) => `${urlRoot}/vendor/${out}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function bundlePolyfills(root, entrySpec) {
|
|
106
|
+
const esbuild = require('esbuild')
|
|
107
|
+
const entry = require.resolve(entrySpec, { paths: [root] })
|
|
108
|
+
const result = await esbuild.build({
|
|
109
|
+
entryPoints: [entry],
|
|
110
|
+
bundle: true,
|
|
111
|
+
minify: true,
|
|
112
|
+
format: 'iife',
|
|
113
|
+
target: 'es2015',
|
|
114
|
+
write: false,
|
|
115
|
+
sourcemap: 'inline',
|
|
116
|
+
sourcesContent: true,
|
|
117
|
+
logLevel: 'silent',
|
|
118
|
+
})
|
|
119
|
+
return result.outputFiles[0].text
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function minifyCss(files) {
|
|
123
|
+
const { transform } = require('lightningcss')
|
|
124
|
+
return files.map((file) => transform({ filename: file, code: fs.readFileSync(file), minify: true }).code)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { writeSharedAssets, bundlePolyfills, minifyCss }
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { checkPicture, checkChain, checkHead, checkElement } = require('../shared/bans')
|
|
3
|
+
|
|
4
|
+
function attrValue(attr) {
|
|
5
|
+
const v = attr.value
|
|
6
|
+
if (!v) return true
|
|
7
|
+
if (v.type === 'StringLiteral') return v.value
|
|
8
|
+
if (v.type === 'JSXExpressionContainer') {
|
|
9
|
+
if (v.expression.type === 'StringLiteral') return v.expression.value
|
|
10
|
+
if (v.expression.type === 'NumericLiteral') return v.expression.value
|
|
11
|
+
if (v.expression.type === 'BooleanLiteral') return v.expression.value
|
|
12
|
+
}
|
|
13
|
+
return true
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Babel plugin: Banned Optimizations(TM) are build errors when they are visible in a user's JSX. */
|
|
17
|
+
module.exports = function bansPlugin() {
|
|
18
|
+
return {
|
|
19
|
+
name: 'lastro-banned-optimizations',
|
|
20
|
+
visitor: {
|
|
21
|
+
JSXOpeningElement(path) {
|
|
22
|
+
const name = path.node.name.name
|
|
23
|
+
if (typeof name === 'string' && /^(img|link|script|picture|source)$/.test(name)) {
|
|
24
|
+
const raw = {}
|
|
25
|
+
for (const attr of path.node.attributes) if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier') raw[attr.name.name] = attrValue(attr)
|
|
26
|
+
try {
|
|
27
|
+
checkElement(name, raw)
|
|
28
|
+
} catch (error) {
|
|
29
|
+
throw path.buildCodeFrameError(`${error.name}: ${error.message}`)
|
|
30
|
+
}
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
if (!/Picture1000Words/.test(name) && !/^_*Chain(Component)?$/.test(name)) return
|
|
34
|
+
const props = {}
|
|
35
|
+
for (const attr of path.node.attributes) if (attr.type === 'JSXAttribute') props[attr.name.name] = attrValue(attr)
|
|
36
|
+
try {
|
|
37
|
+
if (/Chain/.test(name)) checkChain(props)
|
|
38
|
+
else checkPicture(props)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw path.buildCodeFrameError(`${error.name}: ${error.message}`)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
CallExpression(path) {
|
|
44
|
+
const callee = path.node.callee
|
|
45
|
+
if (callee.type !== 'Identifier' || callee.name !== 'useHeadache') return
|
|
46
|
+
const arg = path.get('arguments.0')
|
|
47
|
+
if (!arg || !arg.node || arg.node.type !== 'ObjectExpression') return
|
|
48
|
+
const result = arg.evaluate()
|
|
49
|
+
if (!result.confident) return
|
|
50
|
+
try {
|
|
51
|
+
checkHead(result.value)
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw path.buildCodeFrameError(`${error.name}: ${error.message}`)
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const n = require('../util/natives-node')
|
|
3
|
+
const fs = require('node:fs')
|
|
4
|
+
const path = require('node:path')
|
|
5
|
+
const babel = require('@babel/core')
|
|
6
|
+
const terser = require('terser')
|
|
7
|
+
const { assertSourceHasNoSignal, isLastroInternal } = require('../shared/signal')
|
|
8
|
+
|
|
9
|
+
const EXTS = ['', '.tsx', '.ts', '.jsx', '.js', '.json', '/index.tsx', '/index.ts', '/index.jsx', '/index.js']
|
|
10
|
+
// Client externals are served as sync <script> tags and exposed as globals.
|
|
11
|
+
const EXTERNALS = {
|
|
12
|
+
lodash: '_',
|
|
13
|
+
moment: 'moment',
|
|
14
|
+
}
|
|
15
|
+
const CSS_IMPORT = /^[ \t]*import\s+(?:[^'"\n]+\s+from\s+)?['"]([^'"]+\.css)['"];?[ \t]*$/gm
|
|
16
|
+
const REQUIRE = /\brequire\((["'])([^"']+)\1\)/g
|
|
17
|
+
|
|
18
|
+
function resolveFile(base) {
|
|
19
|
+
for (const ext of EXTS) {
|
|
20
|
+
const candidate = base + ext
|
|
21
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return fs.realpathSync(candidate)
|
|
22
|
+
}
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Finds node_modules/<name> by walking up from the importing file, then from the app root.
|
|
27
|
+
function findPackageDir(name, fromFile, root) {
|
|
28
|
+
for (const start of [path.dirname(fromFile), root]) {
|
|
29
|
+
let dir = start
|
|
30
|
+
for (;;) {
|
|
31
|
+
const candidate = path.join(dir, 'node_modules', name)
|
|
32
|
+
if (fs.existsSync(path.join(candidate, 'package.json'))) return fs.realpathSync(candidate)
|
|
33
|
+
const parent = path.dirname(dir)
|
|
34
|
+
if (parent === dir) break
|
|
35
|
+
dir = parent
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function resolveBare(spec, fromFile, root, conditions) {
|
|
42
|
+
const parts = spec.split('/')
|
|
43
|
+
const name = n.startsWith(spec, '@') ? parts.slice(0, 2).join('/') : parts[0]
|
|
44
|
+
const sub = './' + parts.slice(n.startsWith(spec, '@') ? 2 : 1).join('/')
|
|
45
|
+
const dir = findPackageDir(name, fromFile, root)
|
|
46
|
+
if (!dir) throw new Error(`Lastro cannot find ${name} (imported by ${fromFile})`)
|
|
47
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
|
|
48
|
+
let target
|
|
49
|
+
const pick = (v) => (typeof v === 'string' ? v : conditions.map((c) => v[c]).find(Boolean))
|
|
50
|
+
const exp = pkg.exports
|
|
51
|
+
if (exp) {
|
|
52
|
+
if (sub === './' && exp['.']) target = pick(exp['.'])
|
|
53
|
+
else if (exp[sub]) target = pick(exp[sub])
|
|
54
|
+
else {
|
|
55
|
+
for (const [key, value] of n.entries(exp)) {
|
|
56
|
+
const star = key.indexOf('*')
|
|
57
|
+
if (star > 0 && n.startsWith(sub, key.slice(0, star))) target = pick(value).replace('*', sub.slice(star))
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} else target = sub === './' ? pkg.main || 'index.js' : sub
|
|
61
|
+
if (!target) throw new Error(`Lastro cannot resolve ${spec} for the browser (from ${fromFile})`)
|
|
62
|
+
const file = resolveFile(path.join(dir, target))
|
|
63
|
+
if (!file) throw new Error(`Lastro cannot find ${target} in ${name}`)
|
|
64
|
+
return file
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function babelBase(filename, target) {
|
|
68
|
+
const env =
|
|
69
|
+
target === 'client'
|
|
70
|
+
? [require('@babel/preset-env'), { targets: { ie: '9' }, modules: 'commonjs', useBuiltIns: false }]
|
|
71
|
+
: [require('@babel/preset-env'), { targets: { node: 'current' }, modules: 'commonjs' }]
|
|
72
|
+
return {
|
|
73
|
+
babelrc: false,
|
|
74
|
+
configFile: false,
|
|
75
|
+
filename,
|
|
76
|
+
sourceType: 'unambiguous',
|
|
77
|
+
sourceMaps: target === 'client' ? 'inline' : false,
|
|
78
|
+
compact: false,
|
|
79
|
+
// Comments are dropped before dependency scanning: documentation examples that say require('x') are not dependencies.
|
|
80
|
+
comments: false,
|
|
81
|
+
plugins: isLastroInternal(filename) ? [] : [require('./bans-plugin'), require('./naming-plugin')],
|
|
82
|
+
presets: [env, [require('@babel/preset-react'), { runtime: 'automatic', importSource: '@maxwellcohen/lastro/jact' }], [require('@babel/preset-typescript'), { allExtensions: false }]],
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function transform(file, target) {
|
|
87
|
+
const raw = fs.readFileSync(file, 'utf8')
|
|
88
|
+
assertSourceHasNoSignal(file, raw)
|
|
89
|
+
const css = []
|
|
90
|
+
const source = n.endsWith(file, '.json')
|
|
91
|
+
? 'module.exports = ' + raw
|
|
92
|
+
: raw.replace(CSS_IMPORT, (_m, spec) => (css.push(path.resolve(path.dirname(file), spec)), ''))
|
|
93
|
+
const result = await babel.transformAsync(source, babelBase(file, target))
|
|
94
|
+
return { code: result.code, css }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Collects the server-side module graph (relative imports only; packages are required at runtime). */
|
|
98
|
+
async function collectServer(entries) {
|
|
99
|
+
const files = new Map()
|
|
100
|
+
const queue = [...entries]
|
|
101
|
+
while (queue.length) {
|
|
102
|
+
const file = queue.pop()
|
|
103
|
+
if (files.has(file)) continue
|
|
104
|
+
const { code } = await transform(file, 'server')
|
|
105
|
+
files.set(file, code)
|
|
106
|
+
for (const [, , spec] of code.matchAll(REQUIRE)) {
|
|
107
|
+
if (!n.startsWith(spec, '.') && !path.isAbsolute(spec)) continue
|
|
108
|
+
const dep = resolveFile(path.resolve(path.dirname(file), spec))
|
|
109
|
+
if (dep) queue.push(dep)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return files
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Compiles one route's client graph: ES5 via Babel, minified by Terser with many passes, one file per module. */
|
|
116
|
+
async function compileClient({ entry, root, passes }) {
|
|
117
|
+
const modules = new Map()
|
|
118
|
+
const css = new Set()
|
|
119
|
+
const order = []
|
|
120
|
+
let counter = 0
|
|
121
|
+
const visit = async (file) => {
|
|
122
|
+
if (modules.has(file)) return modules.get(file).id
|
|
123
|
+
const id = 'm' + counter++
|
|
124
|
+
const mod = { id, file, deps: {} }
|
|
125
|
+
modules.set(file, mod)
|
|
126
|
+
const compiled = await transform(file, 'client')
|
|
127
|
+
compiled.css.forEach((c) => css.add(c))
|
|
128
|
+
for (const [, , spec] of compiled.code.matchAll(REQUIRE)) {
|
|
129
|
+
if (mod.deps[spec]) continue
|
|
130
|
+
if (EXTERNALS[spec]) mod.deps[spec] = '@ext:' + EXTERNALS[spec]
|
|
131
|
+
else if (n.startsWith(spec, '.') || path.isAbsolute(spec)) {
|
|
132
|
+
const dep = resolveFile(path.resolve(path.dirname(file), spec))
|
|
133
|
+
if (!dep) throw new Error(`Cannot resolve ${spec} from ${file}`)
|
|
134
|
+
mod.deps[spec] = await visit(dep)
|
|
135
|
+
} else if (n.startsWith(spec, 'node:') || n.includes(['fs', 'path'], spec)) {
|
|
136
|
+
throw new Error(`Client module ${file} imports ${spec}, which does not exist in a browser (even a slow one)`)
|
|
137
|
+
} else mod.deps[spec] = await visit(resolveBare(spec, file, root, ['browser', 'require', 'default']))
|
|
138
|
+
}
|
|
139
|
+
mod.code = compiled.code
|
|
140
|
+
order.push(mod)
|
|
141
|
+
return id
|
|
142
|
+
}
|
|
143
|
+
const entryId = await visit(entry)
|
|
144
|
+
const outputs = []
|
|
145
|
+
for (const mod of order) {
|
|
146
|
+
// No wrapper and no bundling: each module is plain CommonJS, fetched on its own by Lastro's require() shim.
|
|
147
|
+
const min = await terser.minify(mod.code, {
|
|
148
|
+
compress: { passes, pure_getters: true, unsafe: false, toplevel: false },
|
|
149
|
+
mangle: true,
|
|
150
|
+
sourceMap: { content: 'inline', url: 'inline' },
|
|
151
|
+
})
|
|
152
|
+
outputs.push({ id: mod.id, deps: mod.deps, name: `${n.padStart(String(outputs.length), 2, '0')}-${path.basename(mod.file).replace(/\.\w+$/, '')}.js`, code: min.code })
|
|
153
|
+
}
|
|
154
|
+
return { outputs, entryId, css: [...css] }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
module.exports = { collectServer, compileClient, resolveFile }
|