@korajs/cli 1.0.0-beta.1 → 1.0.0-beta.11

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.
Files changed (77) hide show
  1. package/dist/bin.cjs +597 -291
  2. package/dist/bin.cjs.map +1 -1
  3. package/dist/bin.js +123 -36
  4. package/dist/bin.js.map +1 -1
  5. package/dist/{chunk-EOWLAAIV.js → chunk-4IKCHRJ6.js} +273 -57
  6. package/dist/chunk-4IKCHRJ6.js.map +1 -0
  7. package/dist/{chunk-B5YS4STN.js → chunk-G2PBYMMA.js} +2 -2
  8. package/dist/{chunk-B5YS4STN.js.map → chunk-G2PBYMMA.js.map} +1 -1
  9. package/dist/{chunk-BWTKRKNJ.js → chunk-Z7GCXMGW.js} +9 -5
  10. package/dist/chunk-Z7GCXMGW.js.map +1 -0
  11. package/dist/create.cjs +8 -4
  12. package/dist/create.cjs.map +1 -1
  13. package/dist/create.js +2 -2
  14. package/dist/index.cjs +389 -166
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +58 -1
  17. package/dist/index.d.ts +58 -1
  18. package/dist/index.js +12 -2
  19. package/dist/{spectator-manager-E2LH2P54.js → spectator-manager-FTH2NFSE.js} +5 -2
  20. package/dist/spectator-manager-FTH2NFSE.js.map +1 -0
  21. package/package.json +10 -8
  22. package/templates/react-basic/README.md.hbs +1 -1
  23. package/templates/react-basic/src/main.tsx +1 -1
  24. package/templates/react-basic/src/vite-env.d.ts +1 -0
  25. package/templates/react-basic/vite.config.ts +7 -4
  26. package/templates/react-sync/README.md.hbs +6 -5
  27. package/templates/react-sync/server.ts +46 -3
  28. package/templates/react-sync/src/main.tsx +1 -1
  29. package/templates/react-sync/src/vite-env.d.ts +1 -0
  30. package/templates/react-sync/vite.config.ts +14 -4
  31. package/templates/react-tailwind/README.md.hbs +1 -1
  32. package/templates/react-tailwind/src/main.tsx +1 -1
  33. package/templates/react-tailwind/src/vite-env.d.ts +1 -0
  34. package/templates/react-tailwind/vite.config.ts +7 -4
  35. package/templates/react-tailwind-sync/README.md.hbs +6 -5
  36. package/templates/react-tailwind-sync/server.ts +46 -3
  37. package/templates/react-tailwind-sync/src/main.tsx +1 -1
  38. package/templates/react-tailwind-sync/src/vite-env.d.ts +1 -0
  39. package/templates/react-tailwind-sync/vite.config.ts +14 -4
  40. package/templates/svelte-basic/README.md.hbs +1 -1
  41. package/templates/svelte-basic/src/main.ts +1 -1
  42. package/templates/svelte-basic/vite.config.ts +4 -2
  43. package/templates/svelte-sync/README.md.hbs +6 -5
  44. package/templates/svelte-sync/server.ts +46 -3
  45. package/templates/svelte-sync/src/main.ts +1 -1
  46. package/templates/svelte-sync/src/vite-env.d.ts +1 -0
  47. package/templates/svelte-sync/vite.config.ts +11 -2
  48. package/templates/svelte-tailwind/README.md.hbs +1 -1
  49. package/templates/svelte-tailwind/src/main.ts +1 -1
  50. package/templates/svelte-tailwind/vite.config.ts +4 -2
  51. package/templates/svelte-tailwind-sync/README.md.hbs +6 -5
  52. package/templates/svelte-tailwind-sync/server.ts +46 -3
  53. package/templates/svelte-tailwind-sync/src/main.ts +1 -1
  54. package/templates/svelte-tailwind-sync/src/vite-env.d.ts +1 -0
  55. package/templates/svelte-tailwind-sync/vite.config.ts +11 -2
  56. package/templates/tauri-react/server.ts +44 -1
  57. package/templates/tauri-react/src/AppShell.tsx +1 -1
  58. package/templates/tauri-react/src/vite-env.d.ts +1 -0
  59. package/templates/vue-basic/README.md.hbs +1 -1
  60. package/templates/vue-basic/src/main.ts +1 -1
  61. package/templates/vue-basic/vite.config.ts +4 -2
  62. package/templates/vue-sync/README.md.hbs +6 -5
  63. package/templates/vue-sync/server.ts +46 -3
  64. package/templates/vue-sync/src/main.ts +1 -1
  65. package/templates/vue-sync/src/vite-env.d.ts +1 -0
  66. package/templates/vue-sync/vite.config.ts +11 -2
  67. package/templates/vue-tailwind/README.md.hbs +1 -1
  68. package/templates/vue-tailwind/src/main.ts +1 -1
  69. package/templates/vue-tailwind/vite.config.ts +4 -2
  70. package/templates/vue-tailwind-sync/README.md.hbs +6 -5
  71. package/templates/vue-tailwind-sync/server.ts +46 -3
  72. package/templates/vue-tailwind-sync/src/main.ts +1 -1
  73. package/templates/vue-tailwind-sync/src/vite-env.d.ts +1 -0
  74. package/templates/vue-tailwind-sync/vite.config.ts +11 -2
  75. package/dist/chunk-BWTKRKNJ.js.map +0 -1
  76. package/dist/chunk-EOWLAAIV.js.map +0 -1
  77. package/dist/spectator-manager-E2LH2P54.js.map +0 -1
@@ -11,7 +11,7 @@ npm run dev
11
11
  This starts both the Vite dev server and the Kora sync server.
12
12
 
13
13
  - **App**: http://localhost:5173
14
- - **Sync server**: ws://localhost:3001
14
+ - **Sync server**: ws://localhost:3001/kora-sync
15
15
 
16
16
  ## Project Structure
17
17
 
@@ -40,11 +40,12 @@ writes, and `useTodos.ts` is the React binding that exposes todo data and action
40
40
 
41
41
  | Variable | Default | Description |
42
42
  |----------|---------|-------------|
43
- | `VITE_SYNC_URL` | `ws://localhost:3001` | WebSocket URL for the sync server |
43
+ | `VITE_SYNC_URL` | `ws://localhost:3001/kora-sync` | WebSocket URL for the sync endpoint |
44
44
  | `VITE_AUTH_URL` | current origin | HTTP URL for `/auth/*` routes |
45
45
  | `PORT` | `3001` | Sync server port |
46
- | `KORA_SERVER_DB` | `./kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
- | `KORA_AUTH_DB` | `./kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
46
+ | `KORA_SERVER_DB` | `./.kora/kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
+ | `KORA_AUTH_DB` | `./.kora/kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
48
+ | `KORA_COEP_POLICY` | `credentialless` | Dev/preview Cross-Origin-Embedder-Policy. Use `require-corp` only when every embedded resource opts in. |
48
49
  | `DATABASE_URL` | (none) | PostgreSQL connection string for production sync server deployments |
49
50
  | `KORA_SYNC_PATH` | `/kora-sync` | WebSocket path exposed by the sync server |
50
51
  | `KORA_AUTH_SECRET` | (none) | Enables built-in `/auth/*` routes and authenticated sync when set |
@@ -94,7 +95,7 @@ The client and sync server must be deployed separately:
94
95
  Set `VITE_SYNC_URL` to your deployed sync server URL when building:
95
96
 
96
97
  ```bash
97
- VITE_SYNC_URL=wss://your-server.example.com npm run build
98
+ VITE_SYNC_URL=wss://your-server.example.com/kora-sync npm run build
98
99
  ```
99
100
 
100
101
  ## Learn More
@@ -1,3 +1,4 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
1
2
  import {
2
3
  type CreateKoraAuthServerOptions,
3
4
  createKoraAuthServer,
@@ -12,6 +13,45 @@ import {
12
13
  } from '@korajs/server'
13
14
  import schema from './src/schema'
14
15
 
16
+ loadLocalEnv()
17
+
18
+ function loadLocalEnv(mode = process.env.NODE_ENV || 'development') {
19
+ const existingKeys = new Set(Object.keys(process.env))
20
+ for (const file of ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`]) {
21
+ if (!existsSync(file)) continue
22
+ for (const [key, value] of Object.entries(parseEnvFile(readFileSync(file, 'utf8')))) {
23
+ if (!existingKeys.has(key)) {
24
+ process.env[key] = value
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ function parseEnvFile(contents: string): Record<string, string> {
31
+ const env: Record<string, string> = {}
32
+ for (const rawLine of contents.split(/\r?\n/)) {
33
+ const line = rawLine.trim()
34
+ if (!line || line.startsWith('#')) continue
35
+ const normalized = line.startsWith('export ') ? line.slice('export '.length).trim() : line
36
+ const equalsIndex = normalized.indexOf('=')
37
+ if (equalsIndex <= 0) continue
38
+ const key = normalized.slice(0, equalsIndex).trim()
39
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue
40
+ env[key] = parseEnvValue(normalized.slice(equalsIndex + 1).trim())
41
+ }
42
+ return env
43
+ }
44
+
45
+ function parseEnvValue(value: string): string {
46
+ const quote = value[0]
47
+ if ((quote === '"' || quote === "'") && value[value.length - 1] === quote) {
48
+ const inner = value.slice(1, -1)
49
+ return quote === '"' ? inner.replace(/\\n/g, '\n').replace(/\\r/g, '\r') : inner
50
+ }
51
+ const commentIndex = value.search(/\s#/)
52
+ return (commentIndex >= 0 ? value.slice(0, commentIndex) : value).trim()
53
+ }
54
+
15
55
  async function createStore() {
16
56
  if (process.env.DATABASE_URL) {
17
57
  return createPostgresServerStore({
@@ -20,7 +60,7 @@ async function createStore() {
20
60
  }
21
61
 
22
62
  return createSqliteServerStore({
23
- filename: process.env.KORA_SERVER_DB || './kora-server.db',
63
+ filename: process.env.KORA_SERVER_DB || './.kora/kora-server.db',
24
64
  })
25
65
  }
26
66
 
@@ -36,7 +76,10 @@ async function start() {
36
76
  staticDir: './dist',
37
77
  syncPath,
38
78
  httpRoutes: auth ? [{ path: '/auth', handle: auth.handleRequest }] : undefined,
39
- syncOptions: auth ? { auth: auth.auth } : undefined,
79
+ syncOptions: {
80
+ schemaVersion: schema.version,
81
+ ...(auth ? { auth: auth.auth } : {}),
82
+ },
40
83
  operationalAuth: {
41
84
  adminToken: process.env.KORA_ADMIN_TOKEN,
42
85
  metricsToken: process.env.KORA_METRICS_TOKEN,
@@ -72,7 +115,7 @@ async function createOAuthConfig(): Promise<CreateKoraAuthServerOptions['oauth']
72
115
  const oauthStores = process.env.DATABASE_URL
73
116
  ? await createPostgresOAuthStores({ connectionString: process.env.DATABASE_URL })
74
117
  : await createSqliteOAuthStores({
75
- filename: process.env.KORA_AUTH_DB || './kora-auth.db',
118
+ filename: process.env.KORA_AUTH_DB || './.kora/kora-auth.db',
76
119
  })
77
120
 
78
121
  return {
@@ -26,7 +26,7 @@ const app = createApp({
26
26
  store: {
27
27
  workerUrl: koraWorkerUrl,
28
28
  },
29
- devtools: true,
29
+ devtools: import.meta.env.DEV,
30
30
  })
31
31
 
32
32
  // Connect to sync server once the app is ready
@@ -1,4 +1,5 @@
1
1
  interface ImportMetaEnv {
2
+ readonly DEV: boolean
2
3
  readonly VITE_SYNC_URL?: string
3
4
  }
4
5
 
@@ -7,23 +7,26 @@ import { defineConfig } from 'vite'
7
7
 
8
8
  /**
9
9
  * Adds Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers
10
- * required for SharedArrayBuffer. Without these headers, OPFS SAH Pool VFS
11
- * cannot be used and SQLite WASM falls back to in-memory storage (no persistence).
10
+ * enabling SharedArrayBuffer where browsers support it while keeping common
11
+ * third-party embeds such as YouTube and Vimeo usable. Kora falls back to
12
+ * durable IndexedDB storage when SharedArrayBuffer is unavailable.
12
13
  */
13
14
  function crossOriginIsolation(): Plugin {
15
+ const embedderPolicy = process.env.KORA_COEP_POLICY || 'credentialless'
16
+
14
17
  return {
15
18
  name: 'cross-origin-isolation',
16
19
  configureServer(server) {
17
20
  server.middlewares.use((_req, res, next) => {
18
21
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
19
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
22
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
20
23
  next()
21
24
  })
22
25
  },
23
26
  configurePreviewServer(server) {
24
27
  server.middlewares.use((_req, res, next) => {
25
28
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
26
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
29
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
27
30
  next()
28
31
  })
29
32
  },
@@ -82,12 +85,19 @@ export default defineConfig({
82
85
  },
83
86
  server: {
84
87
  allowedHosts: true,
88
+ watch: {
89
+ ignored: ['**/.kora/**', '**/kora/generated/**', '**/*.db', '**/*.db-shm', '**/*.db-wal'],
90
+ },
85
91
  proxy: {
86
92
  '/kora-sync': {
87
93
  target: 'ws://localhost:3001',
88
94
  ws: true,
89
95
  rewriteWsOrigin: true,
90
96
  },
97
+ '/auth': {
98
+ target: 'http://localhost:3001',
99
+ changeOrigin: true,
100
+ },
91
101
  },
92
102
  },
93
103
  })
@@ -47,7 +47,7 @@ To enable real-time sync across devices, update `src/main.tsx`:
47
47
  const app = createApp({
48
48
  schema,
49
49
  sync: {
50
- url: import.meta.env.VITE_SYNC_URL || 'ws://localhost:3001',
50
+ url: import.meta.env.VITE_SYNC_URL || 'ws://localhost:3001/kora-sync',
51
51
  },
52
52
  // ...
53
53
  })
@@ -10,7 +10,7 @@ const kora = createKoraApp({
10
10
  store: {
11
11
  workerUrl: koraWorkerUrl,
12
12
  },
13
- devtools: true,
13
+ devtools: import.meta.env.DEV,
14
14
  })
15
15
 
16
16
  mount(Root, { target: document.getElementById('app')!, props: { kora } })
@@ -5,19 +5,21 @@ import type { Plugin } from 'vite'
5
5
  import { defineConfig } from 'vite'
6
6
 
7
7
  function crossOriginIsolation(): Plugin {
8
+ const embedderPolicy = process.env.KORA_COEP_POLICY || 'credentialless'
9
+
8
10
  return {
9
11
  name: 'cross-origin-isolation',
10
12
  configureServer(server) {
11
13
  server.middlewares.use((_req, res, next) => {
12
14
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
13
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
15
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
14
16
  next()
15
17
  })
16
18
  },
17
19
  configurePreviewServer(server) {
18
20
  server.middlewares.use((_req, res, next) => {
19
21
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
20
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
22
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
21
23
  next()
22
24
  })
23
25
  },
@@ -11,7 +11,7 @@ npm run dev
11
11
  This starts both the Vite dev server and the Kora sync server.
12
12
 
13
13
  - **App**: http://localhost:5173
14
- - **Sync server**: ws://localhost:3001
14
+ - **Sync server**: ws://localhost:3001/kora-sync
15
15
 
16
16
  ## Project Structure
17
17
 
@@ -40,11 +40,12 @@ writes, and `useTodos.ts` is the React binding that exposes todo data and action
40
40
 
41
41
  | Variable | Default | Description |
42
42
  |----------|---------|-------------|
43
- | `VITE_SYNC_URL` | `ws://localhost:3001` | WebSocket URL for the sync server |
43
+ | `VITE_SYNC_URL` | `ws://localhost:3001/kora-sync` | WebSocket URL for the sync endpoint |
44
44
  | `VITE_AUTH_URL` | current origin | HTTP URL for `/auth/*` routes |
45
45
  | `PORT` | `3001` | Sync server port |
46
- | `KORA_SERVER_DB` | `./kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
- | `KORA_AUTH_DB` | `./kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
46
+ | `KORA_SERVER_DB` | `./.kora/kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
+ | `KORA_AUTH_DB` | `./.kora/kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
48
+ | `KORA_COEP_POLICY` | `credentialless` | Dev/preview Cross-Origin-Embedder-Policy. Use `require-corp` only when every embedded resource opts in. |
48
49
  | `DATABASE_URL` | (none) | PostgreSQL connection string for production sync server deployments |
49
50
  | `KORA_SYNC_PATH` | `/kora-sync` | WebSocket path exposed by the sync server |
50
51
  | `KORA_AUTH_SECRET` | (none) | Enables built-in `/auth/*` routes and authenticated sync when set |
@@ -94,7 +95,7 @@ The client and sync server must be deployed separately:
94
95
  Set `VITE_SYNC_URL` to your deployed sync server URL when building:
95
96
 
96
97
  ```bash
97
- VITE_SYNC_URL=wss://your-server.example.com npm run build
98
+ VITE_SYNC_URL=wss://your-server.example.com/kora-sync npm run build
98
99
  ```
99
100
 
100
101
  ## Learn More
@@ -1,3 +1,4 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
1
2
  import {
2
3
  type CreateKoraAuthServerOptions,
3
4
  createKoraAuthServer,
@@ -12,6 +13,45 @@ import {
12
13
  } from '@korajs/server'
13
14
  import schema from './src/schema'
14
15
 
16
+ loadLocalEnv()
17
+
18
+ function loadLocalEnv(mode = process.env.NODE_ENV || 'development') {
19
+ const existingKeys = new Set(Object.keys(process.env))
20
+ for (const file of ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`]) {
21
+ if (!existsSync(file)) continue
22
+ for (const [key, value] of Object.entries(parseEnvFile(readFileSync(file, 'utf8')))) {
23
+ if (!existingKeys.has(key)) {
24
+ process.env[key] = value
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ function parseEnvFile(contents: string): Record<string, string> {
31
+ const env: Record<string, string> = {}
32
+ for (const rawLine of contents.split(/\r?\n/)) {
33
+ const line = rawLine.trim()
34
+ if (!line || line.startsWith('#')) continue
35
+ const normalized = line.startsWith('export ') ? line.slice('export '.length).trim() : line
36
+ const equalsIndex = normalized.indexOf('=')
37
+ if (equalsIndex <= 0) continue
38
+ const key = normalized.slice(0, equalsIndex).trim()
39
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue
40
+ env[key] = parseEnvValue(normalized.slice(equalsIndex + 1).trim())
41
+ }
42
+ return env
43
+ }
44
+
45
+ function parseEnvValue(value: string): string {
46
+ const quote = value[0]
47
+ if ((quote === '"' || quote === "'") && value[value.length - 1] === quote) {
48
+ const inner = value.slice(1, -1)
49
+ return quote === '"' ? inner.replace(/\\n/g, '\n').replace(/\\r/g, '\r') : inner
50
+ }
51
+ const commentIndex = value.search(/\s#/)
52
+ return (commentIndex >= 0 ? value.slice(0, commentIndex) : value).trim()
53
+ }
54
+
15
55
  async function createStore() {
16
56
  if (process.env.DATABASE_URL) {
17
57
  return createPostgresServerStore({
@@ -20,7 +60,7 @@ async function createStore() {
20
60
  }
21
61
 
22
62
  return createSqliteServerStore({
23
- filename: process.env.KORA_SERVER_DB || './kora-server.db',
63
+ filename: process.env.KORA_SERVER_DB || './.kora/kora-server.db',
24
64
  })
25
65
  }
26
66
 
@@ -36,7 +76,10 @@ async function start() {
36
76
  staticDir: './dist',
37
77
  syncPath,
38
78
  httpRoutes: auth ? [{ path: '/auth', handle: auth.handleRequest }] : undefined,
39
- syncOptions: auth ? { auth: auth.auth } : undefined,
79
+ syncOptions: {
80
+ schemaVersion: schema.version,
81
+ ...(auth ? { auth: auth.auth } : {}),
82
+ },
40
83
  operationalAuth: {
41
84
  adminToken: process.env.KORA_ADMIN_TOKEN,
42
85
  metricsToken: process.env.KORA_METRICS_TOKEN,
@@ -72,7 +115,7 @@ async function createOAuthConfig(): Promise<CreateKoraAuthServerOptions['oauth']
72
115
  const oauthStores = process.env.DATABASE_URL
73
116
  ? await createPostgresOAuthStores({ connectionString: process.env.DATABASE_URL })
74
117
  : await createSqliteOAuthStores({
75
- filename: process.env.KORA_AUTH_DB || './kora-auth.db',
118
+ filename: process.env.KORA_AUTH_DB || './.kora/kora-auth.db',
76
119
  })
77
120
 
78
121
  return {
@@ -20,7 +20,7 @@ const kora = createKoraApp({
20
20
  store: {
21
21
  workerUrl: koraWorkerUrl,
22
22
  },
23
- devtools: true,
23
+ devtools: import.meta.env.DEV,
24
24
  })
25
25
 
26
26
  kora.ready.then(() => kora.sync?.connect())
@@ -2,6 +2,7 @@
2
2
  /// <reference types="vite/client" />
3
3
 
4
4
  interface ImportMetaEnv {
5
+ readonly DEV: boolean
5
6
  readonly VITE_SYNC_URL?: string
6
7
  readonly VITE_AUTH_URL?: string
7
8
  }
@@ -5,19 +5,21 @@ import type { Plugin } from 'vite'
5
5
  import { defineConfig } from 'vite'
6
6
 
7
7
  function crossOriginIsolation(): Plugin {
8
+ const embedderPolicy = process.env.KORA_COEP_POLICY || 'credentialless'
9
+
8
10
  return {
9
11
  name: 'cross-origin-isolation',
10
12
  configureServer(server) {
11
13
  server.middlewares.use((_req, res, next) => {
12
14
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
13
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
15
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
14
16
  next()
15
17
  })
16
18
  },
17
19
  configurePreviewServer(server) {
18
20
  server.middlewares.use((_req, res, next) => {
19
21
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
20
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
22
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
21
23
  next()
22
24
  })
23
25
  },
@@ -68,12 +70,19 @@ export default defineConfig({
68
70
  },
69
71
  server: {
70
72
  allowedHosts: true,
73
+ watch: {
74
+ ignored: ['**/.kora/**', '**/kora/generated/**', '**/*.db', '**/*.db-shm', '**/*.db-wal'],
75
+ },
71
76
  proxy: {
72
77
  '/kora-sync': {
73
78
  target: 'ws://localhost:3001',
74
79
  ws: true,
75
80
  rewriteWsOrigin: true,
76
81
  },
82
+ '/auth': {
83
+ target: 'http://localhost:3001',
84
+ changeOrigin: true,
85
+ },
77
86
  },
78
87
  },
79
88
  })
@@ -47,7 +47,7 @@ To enable real-time sync across devices, update `src/main.tsx`:
47
47
  const app = createApp({
48
48
  schema,
49
49
  sync: {
50
- url: import.meta.env.VITE_SYNC_URL || 'ws://localhost:3001',
50
+ url: import.meta.env.VITE_SYNC_URL || 'ws://localhost:3001/kora-sync',
51
51
  },
52
52
  // ...
53
53
  })
@@ -10,7 +10,7 @@ const kora = createKoraApp({
10
10
  store: {
11
11
  workerUrl: koraWorkerUrl,
12
12
  },
13
- devtools: true,
13
+ devtools: import.meta.env.DEV,
14
14
  })
15
15
 
16
16
  mount(Root, { target: document.getElementById('app')!, props: { kora } })
@@ -6,19 +6,21 @@ import type { Plugin } from 'vite'
6
6
  import { defineConfig } from 'vite'
7
7
 
8
8
  function crossOriginIsolation(): Plugin {
9
+ const embedderPolicy = process.env.KORA_COEP_POLICY || 'credentialless'
10
+
9
11
  return {
10
12
  name: 'cross-origin-isolation',
11
13
  configureServer(server) {
12
14
  server.middlewares.use((_req, res, next) => {
13
15
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
14
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
16
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
15
17
  next()
16
18
  })
17
19
  },
18
20
  configurePreviewServer(server) {
19
21
  server.middlewares.use((_req, res, next) => {
20
22
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
21
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
23
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
22
24
  next()
23
25
  })
24
26
  },
@@ -11,7 +11,7 @@ npm run dev
11
11
  This starts both the Vite dev server and the Kora sync server.
12
12
 
13
13
  - **App**: http://localhost:5173
14
- - **Sync server**: ws://localhost:3001
14
+ - **Sync server**: ws://localhost:3001/kora-sync
15
15
 
16
16
  ## Project Structure
17
17
 
@@ -40,11 +40,12 @@ writes, and `useTodos.ts` is the React binding that exposes todo data and action
40
40
 
41
41
  | Variable | Default | Description |
42
42
  |----------|---------|-------------|
43
- | `VITE_SYNC_URL` | `ws://localhost:3001` | WebSocket URL for the sync server |
43
+ | `VITE_SYNC_URL` | `ws://localhost:3001/kora-sync` | WebSocket URL for the sync endpoint |
44
44
  | `VITE_AUTH_URL` | current origin | HTTP URL for `/auth/*` routes |
45
45
  | `PORT` | `3001` | Sync server port |
46
- | `KORA_SERVER_DB` | `./kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
- | `KORA_AUTH_DB` | `./kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
46
+ | `KORA_SERVER_DB` | `./.kora/kora-server.db` | SQLite file used by the sync server when `DATABASE_URL` is empty |
47
+ | `KORA_AUTH_DB` | `./.kora/kora-auth.db` | SQLite file used by OAuth state and linked identities when `DATABASE_URL` is empty |
48
+ | `KORA_COEP_POLICY` | `credentialless` | Dev/preview Cross-Origin-Embedder-Policy. Use `require-corp` only when every embedded resource opts in. |
48
49
  | `DATABASE_URL` | (none) | PostgreSQL connection string for production sync server deployments |
49
50
  | `KORA_SYNC_PATH` | `/kora-sync` | WebSocket path exposed by the sync server |
50
51
  | `KORA_AUTH_SECRET` | (none) | Enables built-in `/auth/*` routes and authenticated sync when set |
@@ -94,7 +95,7 @@ The client and sync server must be deployed separately:
94
95
  Set `VITE_SYNC_URL` to your deployed sync server URL when building:
95
96
 
96
97
  ```bash
97
- VITE_SYNC_URL=wss://your-server.example.com npm run build
98
+ VITE_SYNC_URL=wss://your-server.example.com/kora-sync npm run build
98
99
  ```
99
100
 
100
101
  ## Learn More
@@ -1,3 +1,4 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
1
2
  import {
2
3
  type CreateKoraAuthServerOptions,
3
4
  createKoraAuthServer,
@@ -12,6 +13,45 @@ import {
12
13
  } from '@korajs/server'
13
14
  import schema from './src/schema'
14
15
 
16
+ loadLocalEnv()
17
+
18
+ function loadLocalEnv(mode = process.env.NODE_ENV || 'development') {
19
+ const existingKeys = new Set(Object.keys(process.env))
20
+ for (const file of ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`]) {
21
+ if (!existsSync(file)) continue
22
+ for (const [key, value] of Object.entries(parseEnvFile(readFileSync(file, 'utf8')))) {
23
+ if (!existingKeys.has(key)) {
24
+ process.env[key] = value
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ function parseEnvFile(contents: string): Record<string, string> {
31
+ const env: Record<string, string> = {}
32
+ for (const rawLine of contents.split(/\r?\n/)) {
33
+ const line = rawLine.trim()
34
+ if (!line || line.startsWith('#')) continue
35
+ const normalized = line.startsWith('export ') ? line.slice('export '.length).trim() : line
36
+ const equalsIndex = normalized.indexOf('=')
37
+ if (equalsIndex <= 0) continue
38
+ const key = normalized.slice(0, equalsIndex).trim()
39
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue
40
+ env[key] = parseEnvValue(normalized.slice(equalsIndex + 1).trim())
41
+ }
42
+ return env
43
+ }
44
+
45
+ function parseEnvValue(value: string): string {
46
+ const quote = value[0]
47
+ if ((quote === '"' || quote === "'") && value[value.length - 1] === quote) {
48
+ const inner = value.slice(1, -1)
49
+ return quote === '"' ? inner.replace(/\\n/g, '\n').replace(/\\r/g, '\r') : inner
50
+ }
51
+ const commentIndex = value.search(/\s#/)
52
+ return (commentIndex >= 0 ? value.slice(0, commentIndex) : value).trim()
53
+ }
54
+
15
55
  async function createStore() {
16
56
  if (process.env.DATABASE_URL) {
17
57
  return createPostgresServerStore({
@@ -20,7 +60,7 @@ async function createStore() {
20
60
  }
21
61
 
22
62
  return createSqliteServerStore({
23
- filename: process.env.KORA_SERVER_DB || './kora-server.db',
63
+ filename: process.env.KORA_SERVER_DB || './.kora/kora-server.db',
24
64
  })
25
65
  }
26
66
 
@@ -36,7 +76,10 @@ async function start() {
36
76
  staticDir: './dist',
37
77
  syncPath,
38
78
  httpRoutes: auth ? [{ path: '/auth', handle: auth.handleRequest }] : undefined,
39
- syncOptions: auth ? { auth: auth.auth } : undefined,
79
+ syncOptions: {
80
+ schemaVersion: schema.version,
81
+ ...(auth ? { auth: auth.auth } : {}),
82
+ },
40
83
  operationalAuth: {
41
84
  adminToken: process.env.KORA_ADMIN_TOKEN,
42
85
  metricsToken: process.env.KORA_METRICS_TOKEN,
@@ -72,7 +115,7 @@ async function createOAuthConfig(): Promise<CreateKoraAuthServerOptions['oauth']
72
115
  const oauthStores = process.env.DATABASE_URL
73
116
  ? await createPostgresOAuthStores({ connectionString: process.env.DATABASE_URL })
74
117
  : await createSqliteOAuthStores({
75
- filename: process.env.KORA_AUTH_DB || './kora-auth.db',
118
+ filename: process.env.KORA_AUTH_DB || './.kora/kora-auth.db',
76
119
  })
77
120
 
78
121
  return {
@@ -20,7 +20,7 @@ const kora = createKoraApp({
20
20
  store: {
21
21
  workerUrl: koraWorkerUrl,
22
22
  },
23
- devtools: true,
23
+ devtools: import.meta.env.DEV,
24
24
  })
25
25
 
26
26
  kora.ready.then(() => kora.sync?.connect())
@@ -2,6 +2,7 @@
2
2
  /// <reference types="vite/client" />
3
3
 
4
4
  interface ImportMetaEnv {
5
+ readonly DEV: boolean
5
6
  readonly VITE_SYNC_URL?: string
6
7
  readonly VITE_AUTH_URL?: string
7
8
  }
@@ -6,19 +6,21 @@ import type { Plugin } from 'vite'
6
6
  import { defineConfig } from 'vite'
7
7
 
8
8
  function crossOriginIsolation(): Plugin {
9
+ const embedderPolicy = process.env.KORA_COEP_POLICY || 'credentialless'
10
+
9
11
  return {
10
12
  name: 'cross-origin-isolation',
11
13
  configureServer(server) {
12
14
  server.middlewares.use((_req, res, next) => {
13
15
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
14
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
16
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
15
17
  next()
16
18
  })
17
19
  },
18
20
  configurePreviewServer(server) {
19
21
  server.middlewares.use((_req, res, next) => {
20
22
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
21
- res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
23
+ res.setHeader('Cross-Origin-Embedder-Policy', embedderPolicy)
22
24
  next()
23
25
  })
24
26
  },
@@ -69,12 +71,19 @@ export default defineConfig({
69
71
  },
70
72
  server: {
71
73
  allowedHosts: true,
74
+ watch: {
75
+ ignored: ['**/.kora/**', '**/kora/generated/**', '**/*.db', '**/*.db-shm', '**/*.db-wal'],
76
+ },
72
77
  proxy: {
73
78
  '/kora-sync': {
74
79
  target: 'ws://localhost:3001',
75
80
  ws: true,
76
81
  rewriteWsOrigin: true,
77
82
  },
83
+ '/auth': {
84
+ target: 'http://localhost:3001',
85
+ changeOrigin: true,
86
+ },
78
87
  },
79
88
  },
80
89
  })