turtb 0.5.2 → 0.5.3

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.
@@ -1,11 +1,9 @@
1
- import { logDebug, logError, logInfo } from './logger.js'
1
+ import { logError } from './logger.js'
2
2
 
3
3
  /**
4
4
  * @typedef {import('../turtle/connections/TurtleDB.js').TurtleDB} TurtleDB
5
5
  */
6
6
 
7
- export const defaultPublicKey = 'ctclduqytfepmxfpxe8561b8h75l4u5n2t3sxlrmfc889xjz57'
8
-
9
7
  /**
10
8
  *
11
9
  * @param {URL} url
@@ -16,12 +14,9 @@ export const defaultPublicKey = 'ctclduqytfepmxfpxe8561b8h75l4u5n2t3sxlrmfc889xj
16
14
  * @param {(href: string) => void} redirect
17
15
  * @param {(type: string, body: string) => void} reply
18
16
  */
19
- export const handleRedirect = async (url, address, turtleDB, publicKey = defaultPublicKey, turtleDBFolder, redirect, reply) => {
17
+ export const handleRedirect = async (url, address, turtleDB, publicKey, turtleDBFolder, redirect, reply) => {
18
+ if (url.endsWith('/')) url = `${url}index.html`
20
19
  const type = url.split('.').pop()
21
- if (url === '/') {
22
- console.log('let us index')
23
- return redirect('/index.html')
24
- }
25
20
  try {
26
21
  let directories = url.split('/')
27
22
  if (directories[0] === '') directories.shift()
@@ -30,60 +25,18 @@ export const handleRedirect = async (url, address, turtleDB, publicKey = default
30
25
  if (/^[0-9A-Za-z]{50}$/.test(directories[0])) {
31
26
  urlPublicKey = directories.shift()
32
27
  }
33
- if (directories[directories.length - 1] === '') {
34
- directories[directories.length - 1] = 'index.html'
35
- return redirect(`/${urlPublicKey}/index.html`)
36
- }
37
- const unfilteredLength = directories.length
38
28
  directories = directories.filter(Boolean)
39
- if (unfilteredLength !== directories.length) {
40
- return redirect(`/${urlPublicKey}/${directories.join('/')}`)
41
- }
42
29
  const turtleBranch = await turtleDB.summonBoundTurtleBranch(urlPublicKey)
43
30
  const body = turtleBranch.lookupFile(directories.join('/'), false, +address)
44
31
  if (body) {
45
32
  return reply(type, body)
46
33
  } else {
47
- try {
48
- const symlink = turtleBranch.lookupFile(directories[0])?.symlink
49
- if (symlink) {
50
- console.log(symlink)
51
- const symlinkPublicKey = symlink.match(/.*\/(?<publicKey>[0-9A-Za-z]{50})$/)?.groups?.publicKey
52
- if (symlinkPublicKey) {
53
- return redirect(`/${symlinkPublicKey}/${directories.slice(1).join('/')}`)
54
- }
55
- }
56
- const configJson = turtleBranch.lookupFile('config.json')
57
- const packageJson = turtleBranch.lookupFile('package.json')
58
- if (configJson) {
59
- const config = JSON.parse(configJson)
60
- logInfo(() => console.log({ config }))
61
- const branchGroups = ['fsReadWrite', 'fsReadOnly']
62
- for (const branchGroup of branchGroups) {
63
- const branches = config[branchGroup]
64
- if (branches) {
65
- for (const { name, key } of branches) {
66
- if (name && key) {
67
- if (directories[0] === name) {
68
- return redirect(`/${key}/${directories.slice(1).join('/')}`)
69
- }
70
- }
71
- }
72
- }
73
- }
74
- } else if (packageJson) {
75
- const aliases = JSON.parse(packageJson).turtle.aliases
76
- if (aliases && directories.join('/').startsWith(`${turtleDBFolder}/aliases/`)) {
77
- directories.shift()
78
- const name = directories.shift()
79
- const key = aliases[name]
80
- if (key) {
81
- return redirect(`/${key}/${directories.join('/')}`)
82
- }
83
- }
34
+ const symlink = turtleBranch.lookupFile(directories[0])?.symlink
35
+ if (symlink) {
36
+ const symlinkPublicKey = symlink.match(/\b(?<publicKey>[0-9A-Za-z]{50})\/?$/)?.groups?.publicKey
37
+ if (symlinkPublicKey) {
38
+ return handleRedirect(`/${directories.slice(1).join('/')}`, address, turtleDB, symlinkPublicKey, turtleDBFolder, redirect, reply)
84
39
  }
85
- } catch {
86
- logDebug(() => console.log('not found, no config', url.pathname))
87
40
  }
88
41
  }
89
42
  } catch (error) {
@@ -15,10 +15,10 @@ const allServiceWorkers = new Set()
15
15
  * @param {(tbMux: TurtleBranchMultiplexer) => void} callback
16
16
  * @param {Recaller} [recaller=turtleDB.recaller]
17
17
  */
18
- export async function webSocketMuxFactory (turtleDB, callback, recaller = turtleDB.recaller) {
18
+ export async function webSocketMuxFactory (turtleDB, defaultPublicKey, callback, recaller = turtleDB.recaller) {
19
19
  try {
20
20
  const serviceWorkerRegistration = await navigator.serviceWorker.register(
21
- '/service-worker.js',
21
+ `/service-worker.js?cpk=${defaultPublicKey}`,
22
22
  { type: 'module', scope: '/' }
23
23
  )
24
24
  logInfo(() => console.log(' ^^^^^^^ register complete', serviceWorkerRegistration))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turtb",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "binary for turtledb projects",
5
5
  "repository": "git@github.com:dtudury/turtb.git",
6
6
  "author": "David Tudury <david.tudury@gmail.com>",
package/src/webSync.js CHANGED
@@ -28,7 +28,7 @@ export async function webSync (port, basePublicKey, turtleDB, https, insecure, c
28
28
  const root = join(process.cwd(), basePublicKey)
29
29
  const app = express()
30
30
  app.use((req, _res, next) => {
31
- logDebug(() => console.log(req.method, req.url, req.originalUrl))
31
+ logDebug(() => console.log(req.method, req.url))
32
32
  next()
33
33
  })
34
34
  app.use(async (req, res, next) => {
@@ -37,11 +37,12 @@ export async function webSync (port, basePublicKey, turtleDB, https, insecure, c
37
37
  res.send(JSON.stringify({ workspace: { uuid, root } }))
38
38
  return
39
39
  }
40
- handleRedirect(req.url, +req.params.address, turtleDB, basePublicKey, turtleDBFolder, href => {
40
+ handleRedirect(req.path, +req.params.address, turtleDB, basePublicKey, turtleDBFolder, href => {
41
41
  res.redirect(302, href)
42
42
  }, (type, body) => {
43
43
  if (!body) return next()
44
44
  res.type(type)
45
+ res.cookie('cpk', basePublicKey)
45
46
  res.send(body)
46
47
  })
47
48
  })