saloe 0.0.3 → 0.0.4
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.
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "demo-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "server.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"watch:dev": "ENV=dev vite build --watch",
|
|
8
|
+
"watch:qa": "ENV=qa vite build --watch",
|
|
9
|
+
"watch:prod": "ENV=prod vite build --watch",
|
|
10
|
+
"build:dev": "ENV=dev vite build",
|
|
11
|
+
"build:qa": "ENV=qa vite build",
|
|
12
|
+
"build:prod": "ENV=prod vite build",
|
|
13
|
+
"local:dev": "wrangler dev --env=dev",
|
|
14
|
+
"local:qa": "wrangler dev --env=qa",
|
|
15
|
+
"local:prod": "wrangler dev --env=prod",
|
|
16
|
+
"deploy:dev": "npm run build:dev && wrangler deploy --env=dev",
|
|
17
|
+
"deploy:qa": "npm run build:qa && wrangler deploy --env=qa",
|
|
18
|
+
"deploy:prod": "npm run build:prod && wrangler deploy --env=prod",
|
|
19
|
+
"build-worker:dev": "wrangler deploy --dry-run --outdir=dist-worker"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [],
|
|
22
|
+
"author": "",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@cloudflare/kv-asset-handler": "^0.3.4",
|
|
26
|
+
"saloe": "^0.0.3",
|
|
27
|
+
"vite": "^5.4.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { findPatternFromUrl, getRedirectResponse, getForbiddenResponse, getRoute, getNotFoundResponse, getServerOnlyResponse } from 'saloe/router'
|
|
2
|
+
import { getStaticResponse } from 'saloe/cloudflare-worker'
|
|
3
|
+
import { addRoute } from 'saloe/router'
|
|
4
|
+
import { html, stream } from 'saloe/html'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const isRedirectableCallback = ({ pathname }) => {
|
|
8
|
+
return pathname !== '/' && pathname.endsWith('/')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const isForbiddenCallback = ({ request }) => {
|
|
12
|
+
const forbiddenURLs = []
|
|
13
|
+
return forbiddenURLs.find((filename) => request?.url?.endsWith(filename))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isServerOnlyCallback = ({ request }) => {
|
|
17
|
+
const serverOnlyURLs = []
|
|
18
|
+
return serverOnlyURLs?.find((filename) => request?.url?.endsWith(filename))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
(() => {
|
|
23
|
+
addRoute({
|
|
24
|
+
pathname: '/',
|
|
25
|
+
route: ({ request, env }) => {
|
|
26
|
+
return stream({
|
|
27
|
+
head: () => html`
|
|
28
|
+
<meta charset="UTF-8">
|
|
29
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
30
|
+
<title>Cloudflare Worker Server</title>
|
|
31
|
+
`,
|
|
32
|
+
body: () => html`
|
|
33
|
+
<h1>Hello world!</h1>
|
|
34
|
+
`,
|
|
35
|
+
scripts: () => html`
|
|
36
|
+
<script>
|
|
37
|
+
console.log('Hello world!')
|
|
38
|
+
</script>
|
|
39
|
+
`,
|
|
40
|
+
env,
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
})()
|
|
46
|
+
|
|
47
|
+
const handleFetch = async ({ request, env, ctx }) => {
|
|
48
|
+
const url = new URL(request.url)
|
|
49
|
+
const { origin, pathname } = url
|
|
50
|
+
|
|
51
|
+
const pattern = findPatternFromUrl({ url })
|
|
52
|
+
|
|
53
|
+
const redirectResult = getRedirectResponse({ origin, pathname, isRedirectableCallback })
|
|
54
|
+
if (redirectResult?.response) return redirectResult.response
|
|
55
|
+
|
|
56
|
+
const forbiddenResult = getForbiddenResponse({ origin, request, isForbiddenCallback })
|
|
57
|
+
if (forbiddenResult?.response) return forbiddenResult.response
|
|
58
|
+
|
|
59
|
+
const serverOnlyResult = getServerOnlyResponse({ origin, request, isServerOnlyCallback })
|
|
60
|
+
if (serverOnlyResult?.response) return serverOnlyResult.response
|
|
61
|
+
|
|
62
|
+
const route = getRoute({ pathname: pattern?.pathname })
|
|
63
|
+
const routeResult = route ? await route({ request, pattern, env }) : null
|
|
64
|
+
if (routeResult?.response) return routeResult.response
|
|
65
|
+
|
|
66
|
+
const staticResult = await getStaticResponse({ request, waitUntil: ctx.waitUntil.bind(ctx), env })
|
|
67
|
+
if (staticResult?.response) return staticResult?.response
|
|
68
|
+
|
|
69
|
+
const notFoundResult = await getNotFoundResponse({ request })
|
|
70
|
+
return notFoundResult?.response
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default {
|
|
74
|
+
fetch: (request, env, ctx) => handleFetch({ request, env, ctx })
|
|
75
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
define: {
|
|
6
|
+
__ENV__: `'${process.env.ENV}'`,
|
|
7
|
+
__BUILD_TIME__: `'${new Date().toISOString()}'`,
|
|
8
|
+
__APP_NAME__: `'demo-server'`,
|
|
9
|
+
},
|
|
10
|
+
plugins: [],
|
|
11
|
+
build: {
|
|
12
|
+
outDir: './dist',
|
|
13
|
+
manifest: true,
|
|
14
|
+
emptyOutDir: true,
|
|
15
|
+
minify: false,
|
|
16
|
+
rollupOptions: {
|
|
17
|
+
input: {},
|
|
18
|
+
output: {
|
|
19
|
+
entryFileNames: `[name].js`,
|
|
20
|
+
chunkFileNames: `[name].js`,
|
|
21
|
+
assetFileNames: `[name].[ext]`,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name = "demo-server"
|
|
2
|
+
main = "server.js"
|
|
3
|
+
compatibility_date = "2022-11-01"
|
|
4
|
+
compatibility_flags = ["transformstream_enable_standard_constructor", "streams_enable_constructors"]
|
|
5
|
+
minify = false
|
|
6
|
+
#node_compat = true
|
|
7
|
+
|
|
8
|
+
[env]
|
|
9
|
+
|
|
10
|
+
[env.prod]
|
|
11
|
+
experimental-local = true
|
|
12
|
+
|
|
13
|
+
[env.prod.vars]
|
|
14
|
+
IS_CLOUDFLARE_WORKER = true
|
|
15
|
+
ENV = "prod"
|
|
16
|
+
APP_NAME = "demo-server"
|
|
17
|
+
|
|
18
|
+
[env.qa]
|
|
19
|
+
experimental-local = true
|
|
20
|
+
|
|
21
|
+
[env.qa.vars]
|
|
22
|
+
IS_CLOUDFLARE_WORKER = true
|
|
23
|
+
ENV = "qa"
|
|
24
|
+
APP_NAME = "demo-server"
|
|
25
|
+
|
|
26
|
+
[env.dev]
|
|
27
|
+
experimental-local = true
|
|
28
|
+
|
|
29
|
+
[env.dev.vars]
|
|
30
|
+
IS_CLOUDFLARE_WORKER = true
|
|
31
|
+
ENV = "dev"
|
|
32
|
+
APP_NAME = "demo-server"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "saloe",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Tools for making web development easy and efficient",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "vite build",
|
|
@@ -59,9 +59,12 @@
|
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@cloudflare/kv-asset-handler": "^0.3.4",
|
|
63
62
|
"fs": "^0.0.1-security",
|
|
64
|
-
"urlpattern-polyfill": "^10.0.0"
|
|
65
|
-
|
|
63
|
+
"urlpattern-polyfill": "^10.0.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@cloudflare/kv-asset-handler": "^0.3.4",
|
|
67
|
+
"saloe": "^0.0.3",
|
|
68
|
+
"vite": "^5.4.3"
|
|
66
69
|
}
|
|
67
|
-
}
|
|
70
|
+
}
|