saloe 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.
Files changed (45) hide show
  1. package/README.md +19 -0
  2. package/demos/cloudflare-worker-actions/.wrangler/state/v3/cache/miniflare-CacheObject/9f458c07675338a7426a7b81ac4fb1baf92d034efbcaaf4336379640ed744ded.sqlite +0 -0
  3. package/demos/cloudflare-worker-actions/package.json +29 -0
  4. package/demos/cloudflare-worker-actions/public/input.js +5 -0
  5. package/demos/cloudflare-worker-actions/public/load.js +5 -0
  6. package/demos/cloudflare-worker-actions/public/submit.js +6 -0
  7. package/demos/cloudflare-worker-actions/server.js +93 -0
  8. package/demos/cloudflare-worker-actions/vite.config.js +25 -0
  9. package/demos/cloudflare-worker-actions/wrangler.toml +35 -0
  10. package/demos/cloudflare-worker-server/package.json +29 -0
  11. package/demos/cloudflare-worker-server/server.js +75 -0
  12. package/demos/cloudflare-worker-server/vite.config.js +25 -0
  13. package/demos/cloudflare-worker-server/wrangler.toml +32 -0
  14. package/dist/actions.cjs.js +280 -0
  15. package/dist/actions.es.js +280 -0
  16. package/dist/cloudflare-kv.cjs.js +29 -0
  17. package/dist/cloudflare-kv.es.js +29 -0
  18. package/dist/cloudflare-worker.cjs.js +19 -0
  19. package/dist/cloudflare-worker.es.js +19 -0
  20. package/dist/cookie.cjs.js +18 -0
  21. package/dist/cookie.es.js +18 -0
  22. package/dist/html.cjs.js +78 -0
  23. package/dist/html.es.js +78 -0
  24. package/dist/router.cjs.js +51 -0
  25. package/dist/router.es.js +51 -0
  26. package/dist/urlpattern.cjs.js +23 -0
  27. package/dist/urlpattern.es.js +6 -0
  28. package/dist/util.cjs.js +36 -0
  29. package/dist/util.es.js +36 -0
  30. package/dist/vite.cjs.js +102 -0
  31. package/dist/vite.es.js +101 -0
  32. package/dist/worker.cjs.js +43 -0
  33. package/dist/worker.es.js +43 -0
  34. package/package.json +63 -0
  35. package/src/actions.js +284 -0
  36. package/src/cloudflare-kv.js +36 -0
  37. package/src/cloudflare-worker.js +23 -0
  38. package/src/cookie.js +22 -0
  39. package/src/html.js +99 -0
  40. package/src/router.js +61 -0
  41. package/src/urlpattern.js +11 -0
  42. package/src/util.js +44 -0
  43. package/src/vite.js +127 -0
  44. package/src/worker.js +46 -0
  45. package/vite.config.js +36 -0
package/src/vite.js ADDED
@@ -0,0 +1,127 @@
1
+ import { promises as fsPromises, stat as fsStat, unlinkSync, existsSync, renameSync } from 'fs'
2
+ import { join, resolve } from 'path'
3
+
4
+
5
+ const listFiles = async ({ path }) => {
6
+ let allFiles = []
7
+
8
+ try {
9
+ const files = await fsPromises.readdir(path)
10
+
11
+ for (const file of files) {
12
+ const filePath = join(path, file)
13
+ const fileStats = await fsPromises.stat(filePath)
14
+
15
+ if (fileStats.isDirectory()) {
16
+ const subDirFiles = await listFiles({ path: filePath }) // Recursively list files in subdirectories
17
+ allFiles = allFiles.concat(subDirFiles)
18
+ } else {
19
+ allFiles.push(filePath) // Add file path to the array
20
+ }
21
+ }
22
+ } catch (err) {
23
+ console.error('Error reading directory:', err)
24
+ }
25
+
26
+ return allFiles
27
+ }
28
+
29
+ const writeFile = async ({ code, filePath }) => {
30
+ try {
31
+ await fsPromises.writeFile(filePath, code)
32
+ console.info(`File '${filePath}' has been created`)
33
+ } catch (err) {
34
+ console.error('Error writing file:', err)
35
+ }
36
+ }
37
+
38
+ const removeFile = ({ filePath }) => {
39
+ try {
40
+ if (existsSync(filePath)) {
41
+ unlinkSync(filePath)
42
+ console.info(`File removed: ${filePath}`)
43
+ } else {
44
+ console.info(`File does not exist: ${filePath}`)
45
+ }
46
+ } catch (err) {
47
+ console.error(err)
48
+ }
49
+ }
50
+
51
+ const getComponentFilePaths = ({ source }) => listFiles({ path: `src/${source}/` })
52
+
53
+ const getImportCode = async ({ sources }) => {
54
+ let acumIndex = 0
55
+ return Promise.all(
56
+ (sources ?? [])?.map(async (source, sourceIndex) => {
57
+ const filePaths = await getComponentFilePaths({ source })
58
+ const code = filePaths?.map((path, index) => {
59
+ if (!path?.includes('/actions/')) return ''
60
+ if (!path?.endsWith('.js')) return ''
61
+ acumIndex++
62
+ const importPath = path?.replace(`src/${source}`, `@/${source}`)?.replace('.js', '')
63
+ return `import * as A${acumIndex} from '${importPath}'\nconsole.log(A${acumIndex})\n`
64
+ })?.join('')
65
+
66
+ return { filePaths, code }
67
+ })
68
+ )
69
+ }
70
+
71
+
72
+ const getURLPath = ({ path }) => new URL(`${path}`, import.meta.url).pathname
73
+
74
+ const getInputPaths = async ({ sources }) => {
75
+ try {
76
+ const componentsInfos = await getImportCode({ sources })
77
+
78
+ const actionImportCode = `${componentsInfos?.map((componentsInfo) => componentsInfo?.code)?.join('\n')}`
79
+ const actionsFilePath = 'src/_actions_autogenerated.js'
80
+
81
+ await writeFile({ code: actionImportCode, filePath: actionsFilePath })
82
+
83
+ const paths = {
84
+ ...componentsInfos?.reduce((acc1, componentsInfo) => {
85
+ acc1 = {
86
+ ...acc1,
87
+ ...componentsInfo?.filePaths?.reduce((acc, componentFilePath) => {
88
+ if (componentFilePath?.endsWith('.js') && !componentFilePath?.includes('/actions/')) return acc
89
+ const fileName = componentFilePath?.split('/')?.pop()
90
+ const componentName = fileName?.replace(/\.[^.]+$/, '')
91
+ acc[componentName] = getURLPath({ path: componentFilePath })
92
+ return acc
93
+ }, {})
94
+ }
95
+ return acc1
96
+ }, {}),
97
+ actionsFilePath: getURLPath({ path: actionsFilePath }),
98
+ // 'sw.worker': getURLPath({ path: '/src/sw.worker.js' }),
99
+ }
100
+
101
+ return paths
102
+ } catch (err) {
103
+ console.error(err)
104
+ return {}
105
+ }
106
+ }
107
+
108
+ const closeBundle = async () => {
109
+ const distFilePaths = await listFiles({ path: 'dist/' })
110
+ const cleanedDistFilePaths = distFilePaths?.filter((path) => !path?.endsWith('.DS_Store'))
111
+
112
+ await writeFile({ code: JSON.stringify(cleanedDistFilePaths), filePath: 'dist/dist.json' })
113
+ }
114
+
115
+ const getPlugin = () => {
116
+ return [
117
+ {
118
+ name: 'postbuild-command',
119
+ closeBundle,
120
+ },
121
+ ]
122
+ }
123
+
124
+ export {
125
+ getInputPaths,
126
+ getPlugin,
127
+ }
package/src/worker.js ADDED
@@ -0,0 +1,46 @@
1
+ const executeOnScheduler = async ({ callback, signal, priority }) => {
2
+ try {
3
+ if (!self?.scheduler?.postTask) return { data: await callback() }
4
+ const data = await scheduler.postTask(callback, { priority, signal })
5
+ return { data }
6
+ } catch (err) {
7
+ return { err }
8
+ }
9
+ }
10
+
11
+ const stream = ({ callbacks, headers, status }) => {
12
+ const { readable, writable } = new TransformStream()
13
+
14
+ const done = (async () => {
15
+ for (const callback of callbacks) {
16
+ const abortController = new AbortController()
17
+ const executeOnSchedulerResult = await executeOnScheduler({ callback, signal: abortController.signal, priority: 'background' })
18
+ const html = executeOnSchedulerResult?.err ?? executeOnSchedulerResult?.data
19
+ const response = new Response(html, { headers })
20
+ await response.body?.pipeTo(writable, { preventClose: true })
21
+ abortController.abort()
22
+ }
23
+
24
+ writable.getWriter().close()
25
+ })()
26
+
27
+ return {
28
+ done,
29
+ response: new Response(readable, { headers, status: status ?? 200 }),
30
+ }
31
+ }
32
+
33
+ const fetch = async ({ url, request, ...config }) => {
34
+ try {
35
+ const response = await self?.fetch(url || request, config)?.catch((err) => ({ err }))
36
+ if (response?.err) return response
37
+ return { response }
38
+ } catch (err) {
39
+ return { err }
40
+ }
41
+ }
42
+
43
+ export {
44
+ stream,
45
+ fetch,
46
+ }
package/vite.config.js ADDED
@@ -0,0 +1,36 @@
1
+ import { defineConfig } from 'vite'
2
+ import { resolve } from 'path'
3
+
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ minify: false,
8
+ lib: {
9
+ entry: {
10
+ actions: resolve(__dirname, 'src/actions.js'),
11
+ 'cloudflare-kv': resolve(__dirname, 'src/cloudflare-kv.js'),
12
+ 'cloudflare-worker': resolve(__dirname, 'src/cloudflare-worker.js'),
13
+ cookie: resolve(__dirname, 'src/cookie.js'),
14
+ html: resolve(__dirname, 'src/html.js'),
15
+ router: resolve(__dirname, 'src/router.js'),
16
+ 'urlpattern': resolve(__dirname, 'src/urlpattern.js'),
17
+ util: resolve(__dirname, 'src/util.js'),
18
+ vite: resolve(__dirname, 'src/vite.js'),
19
+ worker: resolve(__dirname, 'src/worker.js'),
20
+ },
21
+ name: 'salo',
22
+ fileName: (format) => `[name].${format}.js`,
23
+ },
24
+ rollupOptions: {
25
+ external: ['fs', 'path', '@cloudflare/kv-asset-handler', 'urlpattern-polyfill'],
26
+ output: {
27
+ globals: {
28
+ fs: 'fs',
29
+ path: 'path',
30
+ '@cloudflare/kv-asset-handler': 'getAssetFromKV',
31
+ 'urlpattern-polyfill': 'URLPatternPolyfill',
32
+ },
33
+ },
34
+ },
35
+ },
36
+ })