packaton 0.0.27 → 0.0.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "packaton",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "author": "Eric Fortis",
6
6
  "license": "MIT",
@@ -1,23 +1,32 @@
1
- import { execFileSync } from 'node:child_process'
1
+ import { spawnSync } from 'node:child_process'
2
2
 
3
3
 
4
4
  export const openInBrowser = (async () => {
5
5
  try {
6
6
  return (await import('open')).default
7
7
  }
8
- catch (error) {
8
+ catch {
9
9
  return _openInBrowser
10
10
  }
11
11
  })()
12
12
 
13
13
  function _openInBrowser(address) {
14
+ let opener
14
15
  switch (process.platform) {
15
16
  case 'darwin':
16
- execFileSync('open', [address])
17
+ opener = 'open'
17
18
  break
18
19
  case 'win32':
19
- execFileSync('start', [address])
20
+ opener = 'start'
20
21
  break
22
+ default:
23
+ opener = ['xdg-open', 'gnome-open', 'kde-open'].find(hasCommand)
21
24
  }
25
+ if (opener)
26
+ spawnSync(opener, [address])
22
27
  }
23
28
 
29
+ function hasCommand(cmd) {
30
+ const { status } = spawnSync('command', ['-v', cmd], { stdio: 'ignore' })
31
+ return status === 0
32
+ }
package/src/router.js CHANGED
@@ -21,8 +21,7 @@ const API = {
21
21
  const WATCHER_DEV = `/plugins-dev/watcherDev.js`
22
22
 
23
23
  /** @param {Config} config */
24
- export function router({ srcPath, ignore, mode }) {
25
- const DEV = mode === 'development'
24
+ export function router({ srcPath, ignore, hotReload }) {
26
25
  const WORKSPACE_ID = randomUUID()
27
26
  docs.init(srcPath, ignore)
28
27
 
@@ -60,18 +59,18 @@ export function router({ srcPath, ignore, mode }) {
60
59
  })
61
60
 
62
61
  else if (docs.hasRoute(url))
63
- await serveDocument(response, docs.fileFor(url), url, DEV)
62
+ await serveDocument(response, docs.fileFor(url), url, hotReload)
64
63
  else if (docs.hasRoute(join(url, 'index')))
65
- await serveDocument(response, docs.fileFor(join(url, 'index')), '', DEV)
64
+ await serveDocument(response, docs.fileFor(join(url, 'index')), '', hotReload)
66
65
 
67
66
  else {
68
67
  const f = await resolveIn(srcPath, url)
69
68
  if (!f)
70
69
  response.forbidden('Filename path resolves outside config.srcPath')
71
70
  else if (req.headers.range)
72
- response.partialContent(f)
71
+ await response.partialContent(f)
73
72
  else
74
- response.file(f)
73
+ await response.file(f)
75
74
  }
76
75
  }
77
76
  catch (err) {
@@ -81,11 +80,11 @@ export function router({ srcPath, ignore, mode }) {
81
80
  }
82
81
  }
83
82
 
84
- async function serveDocument(response, file, url, isDev) {
83
+ async function serveDocument(response, file, url, hotReload) {
85
84
  let html = file.endsWith('.html')
86
85
  ? await readFile(file, 'utf8')
87
86
  : (await import(pathToFileURL(file))).default(url)
88
- if (isDev)
87
+ if (hotReload)
89
88
  html += `<script type="module" src="${WATCHER_DEV}?url=${API.watchHotReload}"></script>`
90
89
  response.setHeader('Content-Type', mimeFor('.html'))
91
90
  response.end(html)
@@ -19,7 +19,9 @@ export class ServerResponse extends http.ServerResponse {
19
19
  await pipeline(fs.createReadStream(file), this)
20
20
  }
21
21
  catch (err) {
22
- if (this.headersSent)
22
+ if (this.headersSent
23
+ || err.code === 'ERR_STREAM_PREMATURE_CLOSE'
24
+ || err.code === 'ERR_STREAM_UNABLE_TO_PIPE')
23
25
  this.destroy()
24
26
  else if (err.code === 'ENOENT')
25
27
  this.notFound()
@@ -56,7 +58,9 @@ export class ServerResponse extends http.ServerResponse {
56
58
  await pipeline(fs.createReadStream(file, { start, end }), this)
57
59
  }
58
60
  catch (err) {
59
- if (this.headersSent)
61
+ if (this.headersSent
62
+ || err.code === 'ERR_STREAM_PREMATURE_CLOSE'
63
+ || err.code === 'ERR_STREAM_UNABLE_TO_PIPE')
60
64
  this.destroy()
61
65
  else if (err.code === 'ENOENT')
62
66
  this.notFound()
@@ -64,7 +68,7 @@ export class ServerResponse extends http.ServerResponse {
64
68
  throw err
65
69
  }
66
70
  }
67
-
71
+
68
72
  badRequest() {
69
73
  this.statusCode = 400
70
74
  this.end()