wiki-plugin-mech 0.1.14 → 0.1.16

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/client/mech.js CHANGED
@@ -237,8 +237,8 @@
237
237
  story.push(...state.items)
238
238
  break
239
239
  case 'synopsis':
240
- {const text = `This page has been generated by the Mech plugin. We want to tell you where. That's coming soon.`
241
- story.push({type:'paragraph',text})}
240
+ {const text = `This page created with Mech command: "${command}". See [[${state.context.title}]].`
241
+ story.push({type:'paragraph',text,id:state.context.itemId})}
242
242
  break
243
243
  default:
244
244
  return trouble(elem,`"${type}" doesn't name an item we can preview`)
@@ -246,6 +246,10 @@
246
246
  }
247
247
  const title = "Mech Preview" + (state.tick ? ` ${state.tick}` : '')
248
248
  const page = {title,story}
249
+ for (const item of page.story) item.id ||= (Math.random()*10**20).toFixed(0)
250
+ const item = JSON.parse(JSON.stringify(page))
251
+ const date = Date.now()
252
+ page.journal = [{type:'create', date, item}]
249
253
  const options = {$page:$(elem.closest('.page'))}
250
254
  wiki.showResult(wiki.newPage(page), options)
251
255
  }
@@ -473,7 +477,7 @@
473
477
  return new Promise(resolve => {
474
478
  if(body)
475
479
  run(body,state)
476
- .then(result => if(state.debug) console.log(command,'children', result))
480
+ .then(result => {if(state.debug) console.log(command,'children', result)})
477
481
  elem.innerHTML = command + ` ⇒ ${count} remain`
478
482
  let clock = setInterval(()=> {
479
483
  if(--count > 0)
@@ -495,6 +499,25 @@
495
499
  return Promise.all(children)
496
500
  }
497
501
 
502
+ // http://localhost:3000/plugin/mech/run/testing-mechs-synchronization/5e269010fc81aebe?args=WyJoZWxsbyIsIndvcmxkIl0
503
+ async function get_emit({elem,command,args,body,state}) {
504
+ if (!body) return trouble(elem,`GET expects indented commands to run on the server.`)
505
+ const site = state.context.site
506
+ const slug = state.context.slug
507
+ const itemId = state.context.itemId
508
+ const query = `args=${btoa(JSON.stringify(body))}`
509
+ const url = `//${site}/plugin/mech/run/${slug}/${itemId}?${query}`
510
+ elem.innerHTML = command + ` ⇒ in progress`
511
+ const start = Date.now()
512
+ try {
513
+ state.result = await fetch(url).then(res => res.ok ? res.json() : res.status)
514
+ } catch(err) {
515
+ return trouble(elem,`RUN failed with "${err.message}"`)
516
+ }
517
+ const elapsed = ((Date.now() - start)/1000).toFixed(3)
518
+ elem.innerHTML = command + ` ⇒ ${elapsed} seconds`
519
+ }
520
+
498
521
 
499
522
  // C A T A L O G
500
523
 
@@ -517,7 +540,8 @@
517
540
  SHOW: {emit:show_emit},
518
541
  RANDOM: {emit:random_emit},
519
542
  SLEEP: {emit:sleep_emit},
520
- TOGETHER:{emit:together_emit}
543
+ TOGETHER:{emit:together_emit},
544
+ GET: {emit:get_emit}
521
545
  }
522
546
 
523
547
 
@@ -527,7 +551,19 @@
527
551
  const lines = item.text.split(/\n/)
528
552
  const nest = tree(lines,[],0)
529
553
  const html = format(nest)
530
- const state = {}
554
+ const $page = $item.parents('.page')
555
+ const pageKey = $page.data("key")
556
+ const context = {
557
+ item,
558
+ itemId: item.id,
559
+ pageKey,
560
+ page: wiki.lineup.atKey(pageKey).getRawPage(),
561
+ origin: window.origin,
562
+ site: $page.data("site") || window.location.host,
563
+ slug: $page.attr("id"),
564
+ title: $page.data("data").title,
565
+ }
566
+ const state = {context}
531
567
  $item.append(`<div style="background-color:#eee;padding:15px;border-top:8px;">${html}</div>`)
532
568
  run(nest,state)
533
569
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-mech",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Federated Wiki - Mechanism Scripting Plugin",
5
5
  "keywords": [
6
6
  "mech",
package/server/server.js CHANGED
@@ -1,15 +1,34 @@
1
1
  // mech plugin, server-side component
2
2
  // These handlers are launched with the wiki server.
3
3
 
4
+
4
5
  (function() {
6
+
7
+ const fs = require('fs')
8
+ const process = require('process')
9
+
5
10
  function startServer(params) {
6
11
  var app = params.app,
7
12
  argv = params.argv
8
13
 
9
- return app.get('/plugin/mech/:thing', (req, res) => {
10
- var thing = req.params.thing;
11
- return res.json({thing});
14
+ return app.get('/plugin/mech/run/:slug([a-z-]+)/:itemId', (req, res) => {
15
+ console.log(req.params)
16
+ const uptime = process.uptime()
17
+ try {
18
+ const slug = req.params.slug
19
+ const itemId = req.params.itemId
20
+ const args = JSON.parse(atob(req.query.args || 'W10='))
21
+ const path = `${argv.db}/${slug}`
22
+ fs.readFile(path,(err,data) => {
23
+ const page = JSON.parse(data)
24
+ const item = page.story.find(item => item.id == itemId) || null
25
+ return res.json({err,uptime,item,args});
26
+ })
27
+ } catch(err) {
28
+ return res.json({err:err.message,uptime})
29
+ }
12
30
  })
31
+
13
32
  }
14
33
 
15
34
  module.exports = {startServer}