Browse Source

Add test pages

Alexandre Dieulot 6 years ago
commit
29cc035276
4 changed files with 125 additions and 0 deletions
  1. 1 0
      instantpage.js
  2. 70 0
      test/app.js
  3. 23 0
      test/footer.html
  4. 31 0
      test/header.html

+ 1 - 0
instantpage.js

@@ -0,0 +1 @@
+console.log('instantpage')

+ 70 - 0
test/app.js

@@ -0,0 +1,70 @@
+const http = require('http')
+const fsPromises = require('fs').promises
+const path = require('path')
+const crypto = require('crypto')
+
+const sleep = require('util').promisify(setTimeout)
+
+const argvIndexOfFile = process.argv.indexOf(__filename)
+let SLEEP_TIME = parseInt(process.argv[argvIndexOfFile + 1])
+if (isNaN(SLEEP_TIME)) {
+  SLEEP_TIME = 200
+}
+let PORT = parseInt(process.argv[argvIndexOfFile + 2])
+if (isNaN(PORT)) {
+  PORT = 8000
+}
+
+function sha384(data) {
+  const hash = crypto.createHash('sha384')
+  hash.update(data)
+  return hash.digest('base64')
+}
+
+async function requestListener(req, res) {
+  await sleep(SLEEP_TIME)
+
+  let headers = {
+    'Content-Type': 'text/html',
+  }
+
+  let pathString = req.url.substr(1)
+  let page = parseInt(pathString)
+  if (pathString == '') {
+    page = 1
+  }
+
+  let content = ''
+
+  const jsContent = await fsPromises.readFile(path.resolve(__dirname, '../instantpage.js'))
+  const jsHash = sha384(jsContent)
+
+  if (pathString == 'instantpage.js') {
+    headers['Content-Type'] = 'text/javascript'
+    content += jsContent
+  }
+  else if (!isNaN(page)) {
+    content += await fsPromises.readFile(path.resolve(__dirname, 'header.html'))
+    content += `<h1>Page ${page}</h1>`
+    for (let i = 1; i <= 3; i++) {
+      if (page != i) {
+        content += `<a href="/${i}?${Math.random()}">Page ${i}</a>`
+      }
+    }
+
+    content += `<a href="${req.url}#anchor" id="anchor">Same-page anchor</a>`
+    content += `<a href="/${page}?${Math.random()}#anchor">Other page anchor</a>`
+    content += `<a href="/${page}?${Math.random()}" data-instant="no">Manually blacklisted link</a>`
+    content += `<a href="https://www.google.com/">External link</a>`
+
+    let footer = await fsPromises.readFile(path.resolve(__dirname, 'footer.html'))
+    footer = footer.toString().replace('__HASH__', jsHash)
+    content += footer
+  }
+
+  res.writeHead(200, headers)
+  res.write(content)
+  res.end()
+}
+
+http.createServer(requestListener).listen(PORT)

+ 23 - 0
test/footer.html

@@ -0,0 +1,23 @@
+<button>Test</button>
+
+<script>
+let start
+let lastTouchTimestamp = 0
+const button = document.querySelector('button')
+button.addEventListener('touchstart', () => {
+  start = +new Date
+  lastTouchTimestamp = start
+})
+
+button.addEventListener('mouseover', () => {
+  if ((+new Date - lastTouchTimestamp) > 1000) {
+    start = +new Date
+  }
+})
+
+button.addEventListener('click', () => {
+  button.innerText = '>>> ' + (+new Date - start) + ' <<<'
+  start = undefined
+})
+</script>
+<script src="/instantpage.js" type="module" integrity="sha384-__HASH__"></script>

+ 31 - 0
test/header.html

@@ -0,0 +1,31 @@
+<!doctype html>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<title>instant.page test</title>
+<style>
+
+body {
+  background: lightskyblue;
+  overflow-y: scroll;
+  font-family: system-ui, sans-serif;
+}
+
+a {
+  display: block;
+  background: hsla(0, 0%, 100%, .25);
+  padding: .5em;
+  margin: .5em 0;
+  border: 3px solid hsla(0, 0%, 100%, .25);
+  text-decoration: none;
+  color: #008;
+  font-size: 1.5em;
+}
+
+a:hover {
+  border-color: hsla(0, 0%, 100%, 1);
+}
+
+a:active {
+  background: hsla(0, 0%, 0%, .25);
+}
+</style>