duto 0.260808.0 → 0.260915.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/LICENSE +2 -2
- package/README.md +35 -13
- package/package.json +54 -13
- package/src/cli.ts +45 -0
- package/src/commands/new.ts +147 -0
- package/src/commands/templates/base/.editorconfig +21 -0
- package/src/commands/templates/base/.env.dev.example +2 -0
- package/src/commands/templates/base/.env.prod.example +2 -0
- package/src/commands/templates/base/README.md +29 -0
- package/src/commands/templates/base/_gitignore +100 -0
- package/src/commands/templates/base/actions/blog/index.get.ts +9 -0
- package/src/commands/templates/base/actions/blog/post/[id].get.ts +9 -0
- package/src/commands/templates/base/actions/index.get.ts +5 -0
- package/src/commands/templates/base/configs/duto.ts +11 -0
- package/src/commands/templates/base/configs/roles.ts +5 -0
- package/src/commands/templates/base/package.json +24 -0
- package/src/commands/templates/base/tsconfig.json +42 -0
- package/src/commands/templates/workerd/wrangler.toml +47 -0
- package/src/commands/upgrade.ts +30 -0
- package/src/enum.ts +2 -0
- package/src/index.d.ts +61 -0
- package/src/index.ts +19 -23
- package/src/middleware.ts +2 -0
- package/src/{cli → pages/cli}/commands/build.ts +13 -7
- package/src/{cli → pages/cli}/commands/dev.ts +11 -4
- package/src/{cli → pages/cli}/commands/preview.ts +6 -4
- package/src/pages/cli/index.ts +10 -0
- package/src/{cli → pages/cli}/utils.ts +4 -5
- package/src/{client → pages/client}/app.tsx +1 -1
- package/src/{client → pages/client}/router.ts +1 -1
- package/src/{compiler → pages/compiler}/file.ts +1 -1
- package/src/{compiler → pages/compiler}/index.ts +2 -4
- package/src/{compiler → pages/compiler}/meta.ts +2 -2
- package/src/{compiler → pages/compiler}/routes.ts +1 -1
- package/src/{compiler → pages/compiler}/types.ts +10 -0
- package/src/{compiler → pages/compiler}/vite-plugin.ts +20 -45
- package/src/{i18n → pages/i18n}/client.ts +2 -2
- package/src/{i18n → pages/i18n}/element.ts +1 -1
- package/src/{i18n → pages/i18n}/server.ts +1 -1
- package/src/pages/index.ts +28 -0
- package/src/{preact → pages/preact}/server.ts +1 -1
- package/src/server/adapter/aws.ts +4 -0
- package/src/server/adapter/workerd.ts +4 -0
- package/src/server/app.ts +91 -0
- package/src/server/auth/ability.ts +72 -0
- package/src/server/auth/authnz.ts +86 -0
- package/src/server/auth/index.ts +5 -0
- package/src/server/auth/token.ts +147 -0
- package/src/server/auth/types.ts +2 -0
- package/src/server/cli/commands/build.ts +41 -0
- package/src/server/cli/commands/deploy.ts +81 -0
- package/src/server/cli/commands/dev.ts +279 -0
- package/src/server/cli/commands/make.ts +98 -0
- package/src/server/cli/commands/migrate.ts +93 -0
- package/src/server/cli/commands/routes.ts +78 -0
- package/src/server/cli/index.ts +27 -0
- package/src/server/cli/stubs.ts +39 -0
- package/src/server/cli/types.ts +3 -0
- package/src/server/cli/utils.ts +437 -0
- package/src/server/config.ts +19 -0
- package/src/server/db/d1.d.ts +1 -0
- package/src/server/db/d1.ts +1 -0
- package/src/server/db/dynamodb.d.ts +1 -0
- package/src/server/db/dynamodb.ts +1 -0
- package/src/server/db/index.ts +1 -0
- package/src/server/entry.ts +19 -0
- package/src/server/handle.ts +30 -0
- package/src/server/middlewares/auth.ts +40 -0
- package/src/server/middlewares/logger.ts +30 -0
- package/src/server/open-api/register.ts +29 -0
- package/src/server/open-api/spec.ts +114 -0
- package/src/server/request.ts +265 -0
- package/src/server/response.ts +142 -0
- package/src/server/routes.ts +473 -0
- package/src/server/types.ts +142 -0
- package/src/server/utils/environment.ts +17 -0
- package/src/server/utils.ts +71 -0
- package/src/server/validator.ts +70 -0
- package/src/utils.ts +15 -0
- package/src/cli/commands/dev-old.ts +0 -213
- package/src/cli/index.ts +0 -28
- package/src/compiler/utils.ts +0 -5
- /package/src/{client → pages/client}/hmr.ts +0 -0
- /package/src/{client → pages/client}/island.ts +0 -0
- /package/src/{client → pages/client}/link.ts +0 -0
- /package/src/{client → pages/client}/storage.ts +0 -0
- /package/src/{compiler → pages/compiler}/aliases.ts +0 -0
- /package/src/{compiler → pages/compiler}/assets.ts +0 -0
- /package/src/{compiler → pages/compiler}/ast.ts +0 -0
- /package/src/{compiler → pages/compiler}/constants.ts +0 -0
- /package/src/{compiler → pages/compiler}/island.ts +0 -0
- /package/src/{components → pages/components}/link.tsx +0 -0
- /package/src/{preact → pages/preact}/client-dev.ts +0 -0
- /package/src/{preact → pages/preact}/client.ts +0 -0
- /package/src/{preact → pages/preact}/context.ts +0 -0
- /package/src/{preact → pages/preact}/signals.ts +0 -0
- /package/src/{preact → pages/preact}/static-html.ts +0 -0
- /package/src/{preact → pages/preact}/types.ts +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { zValidator } from '@hono/zod-validator'
|
|
2
|
+
import response from './response'
|
|
3
|
+
import type * as z from 'zod'
|
|
4
|
+
import type {
|
|
5
|
+
Rule, Rules, RuleFn,
|
|
6
|
+
ValidationTargets,
|
|
7
|
+
zObject,
|
|
8
|
+
MiddlewareHandler,
|
|
9
|
+
} from './types'
|
|
10
|
+
|
|
11
|
+
export default class $Validator {
|
|
12
|
+
private static cache = new Map<string, RuleFn>()
|
|
13
|
+
|
|
14
|
+
private static createRule<T extends keyof ValidationTargets>(
|
|
15
|
+
target: T,
|
|
16
|
+
schema: zObject
|
|
17
|
+
): Rule {
|
|
18
|
+
return {
|
|
19
|
+
target,
|
|
20
|
+
schema,
|
|
21
|
+
eTarget: 'fieldErrors'
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private static fn<T extends keyof ValidationTargets>(target: T) {
|
|
26
|
+
if (this.cache.has(target))
|
|
27
|
+
return this.cache.get(target)
|
|
28
|
+
|
|
29
|
+
const fn = (schema: zObject) => this.createRule(target, schema)
|
|
30
|
+
this.cache.set(target, fn)
|
|
31
|
+
return fn
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static readonly json = $Validator.fn('json')!
|
|
35
|
+
static readonly form = $Validator.fn('form')!
|
|
36
|
+
static readonly query = $Validator.fn('query')!
|
|
37
|
+
static readonly param = $Validator.fn('param')!
|
|
38
|
+
static readonly header = $Validator.fn('header')!
|
|
39
|
+
static readonly cookie = $Validator.fn('cookie')!
|
|
40
|
+
|
|
41
|
+
static #parser = (rule: Rule) => zValidator(rule.target, rule.schema, (result, c) => {
|
|
42
|
+
if (!result.success) // @ts-ignore
|
|
43
|
+
return response.badRequest(formatZodErrors(result.error.issues || []))
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
static setParser(parser: (rule: Rule) => MiddlewareHandler) {
|
|
47
|
+
this.#parser = parser
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static parse(rules: Rules): Function[] {
|
|
51
|
+
return (Array.isArray(rules) ? rules : [rules]) // @ts-ignore
|
|
52
|
+
.flatMap(this.#parser)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function formatZodErrors(issues: z.ZodIssue[]): Record<string, string[]> {
|
|
57
|
+
const result: Record<string, string[]> = {}
|
|
58
|
+
|
|
59
|
+
for (const issue of issues) {
|
|
60
|
+
const path = issue.path.join('.')
|
|
61
|
+
const key = path || 'root'
|
|
62
|
+
|
|
63
|
+
if (!result[key])
|
|
64
|
+
result[key] = []
|
|
65
|
+
|
|
66
|
+
result[key].push(issue.message)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return result
|
|
70
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getEnv } from 't0n'
|
|
2
|
+
import { dirname, join } from 'pathe'
|
|
3
|
+
|
|
4
|
+
export const _duto = dirname(new URL(import.meta.url).pathname)
|
|
5
|
+
export const _pages = join(_duto, 'pages')
|
|
6
|
+
export const _server = join(_duto, 'server')
|
|
7
|
+
export const _root = getEnv('npm_config_local_prefix') || getEnv('PWD') || join(_duto, '../../')
|
|
8
|
+
|
|
9
|
+
const UNITS = ['B', 'kB', 'MB', 'GB']
|
|
10
|
+
export function filesize(bytes: number, decimals = 2) {
|
|
11
|
+
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'
|
|
12
|
+
|
|
13
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), UNITS.length - 1)
|
|
14
|
+
return `${parseFloat((bytes / 1024 ** i).toFixed(decimals))} ${UNITS[i]}`
|
|
15
|
+
}
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import { defineCommand } from 'citty'
|
|
2
|
-
import { build, preview } from 'vite'
|
|
3
|
-
import { withPort } from 't0n/port'
|
|
4
|
-
import { error, event, log, rn, wait } from 't0n/log'
|
|
5
|
-
import { getConfig } from '~/utils'
|
|
6
|
-
import { _root } from '@/utils'
|
|
7
|
-
import { watch } from 't0n/cli'
|
|
8
|
-
import { spawn, type ChildProcess } from 'node:child_process'
|
|
9
|
-
|
|
10
|
-
export const args = {
|
|
11
|
-
port: {
|
|
12
|
-
alias: 'p',
|
|
13
|
-
description: 'Port to listen on',
|
|
14
|
-
type: 'number',
|
|
15
|
-
default: 3000,
|
|
16
|
-
},
|
|
17
|
-
host: {
|
|
18
|
-
description: 'Host to forward requests to, defaults to the zone of project',
|
|
19
|
-
type: 'string',
|
|
20
|
-
default: 'localhost',
|
|
21
|
-
},
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export default defineCommand({
|
|
25
|
-
meta: {
|
|
26
|
-
name: 'dev',
|
|
27
|
-
description: '🧪 Start dev server\n',
|
|
28
|
-
}, // @ts-ignore
|
|
29
|
-
args,
|
|
30
|
-
async run({ args }) {
|
|
31
|
-
const config = await getConfig()
|
|
32
|
-
const desiredPort = args.port ? Number(args.port) : (config?.preview?.port ? Number(config.preview.port) : 3000)
|
|
33
|
-
const host = args.host ? String(args.host) : (config?.preview?.host ? config.preview.host : 'localhost')
|
|
34
|
-
|
|
35
|
-
// const _build = async () => {
|
|
36
|
-
// return await build(await getConfig())
|
|
37
|
-
// }
|
|
38
|
-
|
|
39
|
-
// let buildProcess: ChildProcess | null = null
|
|
40
|
-
// const _build = async () => {
|
|
41
|
-
// if (buildProcess) await killProcess(buildProcess)
|
|
42
|
-
|
|
43
|
-
// return new Promise<void>((resolve, reject) => {
|
|
44
|
-
// let timeoutId: NodeJS.Timeout | null = null
|
|
45
|
-
|
|
46
|
-
// buildProcess = spawn('duto', ['build'], {
|
|
47
|
-
// // stdio: 'ignore',
|
|
48
|
-
// stdio: ['inherit', 'ignore', 'inherit', 'ipc'],
|
|
49
|
-
// shell: process.platform === 'win32'
|
|
50
|
-
// })
|
|
51
|
-
|
|
52
|
-
// buildProcess.on('close', (code) => {
|
|
53
|
-
// if (timeoutId) clearTimeout(timeoutId)
|
|
54
|
-
// buildProcess = null
|
|
55
|
-
|
|
56
|
-
// if (code === 0) {
|
|
57
|
-
// resolve()
|
|
58
|
-
// } else {
|
|
59
|
-
// reject(new Error(`Build failed with code ${code}`))
|
|
60
|
-
// }
|
|
61
|
-
// })
|
|
62
|
-
|
|
63
|
-
// buildProcess.on('error', (error) => {
|
|
64
|
-
// if (timeoutId) clearTimeout(timeoutId)
|
|
65
|
-
// buildProcess = null
|
|
66
|
-
// reject(error)
|
|
67
|
-
// })
|
|
68
|
-
// })
|
|
69
|
-
// }
|
|
70
|
-
|
|
71
|
-
// const killProcess = async (app: ChildProcess | null) => {
|
|
72
|
-
// if (!app) return null
|
|
73
|
-
// // event('Stopping..')
|
|
74
|
-
// try {
|
|
75
|
-
// if (!app?.killed) {
|
|
76
|
-
// app.kill('SIGTERM')
|
|
77
|
-
// await wait(1000)
|
|
78
|
-
|
|
79
|
-
// if (!app?.killed) { // force kill
|
|
80
|
-
// app.kill('SIGKILL')
|
|
81
|
-
// await wait(1000)
|
|
82
|
-
// }
|
|
83
|
-
// }
|
|
84
|
-
|
|
85
|
-
// return null
|
|
86
|
-
// } catch (e) {
|
|
87
|
-
// error('Error stopping:', e)
|
|
88
|
-
// }
|
|
89
|
-
|
|
90
|
-
// return null
|
|
91
|
-
// }
|
|
92
|
-
|
|
93
|
-
let buildProcess: Bun.Subprocess | null = null
|
|
94
|
-
|
|
95
|
-
const _build = async () => {
|
|
96
|
-
if (buildProcess) await killProcess(buildProcess)
|
|
97
|
-
buildProcess = Bun.spawn(['duto', 'build'], {stdin: 'inherit', stdout: 'ignore', stderr: 'inherit'})
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
const code = await buildProcess.exited
|
|
101
|
-
|
|
102
|
-
if (code !== 0)
|
|
103
|
-
throw new Error(`Build failed with code ${code}`)
|
|
104
|
-
} finally {
|
|
105
|
-
buildProcess = null
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const killProcess = async (proc: Bun.Subprocess | null) => {
|
|
110
|
-
if (!proc || proc.exitCode !== null) return
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
proc.kill('SIGTERM')
|
|
114
|
-
|
|
115
|
-
const exited = await Promise.race([
|
|
116
|
-
proc.exited.then(() => true),
|
|
117
|
-
new Promise(r => setTimeout(r, 1000)).then(() => false),
|
|
118
|
-
])
|
|
119
|
-
|
|
120
|
-
if (!exited && proc.exitCode === null) {
|
|
121
|
-
proc.kill('SIGKILL')
|
|
122
|
-
await proc.exited
|
|
123
|
-
}
|
|
124
|
-
} catch (e) {
|
|
125
|
-
error('Error stopping:', e)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
withPort(desiredPort, async (port) => {
|
|
130
|
-
const ignore = [
|
|
131
|
-
/(^|[\/\\])\.(duto|git|cache|vscode|idea|next|nuxt|nyc_output)([\/\\]|$)/,
|
|
132
|
-
/(^|[\/\\])(node_modules|tmp|dist|build|out|coverage)([\/\\]|$)/,
|
|
133
|
-
|
|
134
|
-
/\.(DS_Store|localized|tmp|temp|log|pid|swp|swo)$/,
|
|
135
|
-
/(Thumbs\.db|desktop\.ini)$/,
|
|
136
|
-
/~$/,
|
|
137
|
-
|
|
138
|
-
/(^|[\/\\])\.(Spotlight-V100|Trashes)([\/\\]|$)/,
|
|
139
|
-
/(^|[\/\\])__MACOSX([\/\\]|$)/,
|
|
140
|
-
]
|
|
141
|
-
|
|
142
|
-
try {
|
|
143
|
-
await _build()
|
|
144
|
-
|
|
145
|
-
const clients = new Set<ReadableStreamDefaultController<string>>()
|
|
146
|
-
const reload = () => {
|
|
147
|
-
for (const client of clients)
|
|
148
|
-
client.enqueue('data: reload\n\n')
|
|
149
|
-
}
|
|
150
|
-
Bun.serve({
|
|
151
|
-
port: 1322,
|
|
152
|
-
|
|
153
|
-
fetch(req) {
|
|
154
|
-
const headers = {
|
|
155
|
-
'Access-Control-Allow-Origin': '*',
|
|
156
|
-
'Access-Control-Allow-Methods': 'GET',
|
|
157
|
-
'Access-Control-Allow-Headers': '*',
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (req.method === 'OPTIONS')
|
|
161
|
-
return new Response(null, { status: 204, headers })
|
|
162
|
-
|
|
163
|
-
const { pathname } = new URL(req.url)
|
|
164
|
-
|
|
165
|
-
if (pathname !== '/events')
|
|
166
|
-
return new Response(null, { status: 404, headers })
|
|
167
|
-
|
|
168
|
-
const stream = new ReadableStream<string>({
|
|
169
|
-
start(controller) {
|
|
170
|
-
clients.add(controller)
|
|
171
|
-
|
|
172
|
-
req.signal.addEventListener('abort', () => {
|
|
173
|
-
clients.delete(controller)
|
|
174
|
-
controller.close()
|
|
175
|
-
})
|
|
176
|
-
},
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
return new Response(stream, {
|
|
180
|
-
headers: {
|
|
181
|
-
...headers,
|
|
182
|
-
'Content-Type': 'text/event-stream',
|
|
183
|
-
'Cache-Control': 'no-cache',
|
|
184
|
-
'Connection': 'keep-alive',
|
|
185
|
-
},
|
|
186
|
-
})
|
|
187
|
-
},
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
await preview({ ...config, preview: { port, host } })
|
|
191
|
-
|
|
192
|
-
log(`Running on http://${host}:${port}`)
|
|
193
|
-
|
|
194
|
-
watch(async () => {
|
|
195
|
-
await _build()
|
|
196
|
-
reload()
|
|
197
|
-
}, '.', _root, (path: string) => {
|
|
198
|
-
for (const pattern of ignore)
|
|
199
|
-
if (pattern.test(path)) return true
|
|
200
|
-
|
|
201
|
-
return false
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
} catch (e: any) {
|
|
205
|
-
error(e)
|
|
206
|
-
process.exit(0)
|
|
207
|
-
} finally {
|
|
208
|
-
rn()
|
|
209
|
-
}
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
},
|
|
213
|
-
})
|
package/src/cli/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
if (!process?.isBun || typeof Bun === 'undefined') {
|
|
4
|
-
console.error('Error: "Bun" is not available. Please install:')
|
|
5
|
-
console.log(' curl -fsSL https://bun.sh/install | bash')
|
|
6
|
-
process.exit(0)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
global.__st = performance.now()
|
|
10
|
-
|
|
11
|
-
import { join } from 'pathe'
|
|
12
|
-
import { cli } from 't0n/cli'
|
|
13
|
-
import { version } from '!/package.json'
|
|
14
|
-
|
|
15
|
-
if (typeof document === 'undefined' && import.meta.main) {
|
|
16
|
-
const cmd = (c: string) => join(import.meta.dirname, 'commands', c)
|
|
17
|
-
|
|
18
|
-
cli({
|
|
19
|
-
name: 'duto',
|
|
20
|
-
description: 'Duto CLI',
|
|
21
|
-
version,
|
|
22
|
-
commands: {
|
|
23
|
-
dev: cmd('dev'),
|
|
24
|
-
build: cmd('build'),
|
|
25
|
-
preview: cmd('preview'),
|
|
26
|
-
}
|
|
27
|
-
})
|
|
28
|
-
}
|
package/src/compiler/utils.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|