lib0 0.2.67 → 0.2.68

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.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=0serve.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"0serve.d.ts","sourceRoot":"","sources":["0serve.js"],"names":[],"mappings":""}
package/bin/0serve.js ADDED
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env node
2
+ import http from 'http'
3
+ import path from 'path'
4
+ import fs from 'fs'
5
+ import * as env from '../environment.js'
6
+ import * as number from '../number.js'
7
+ import * as logging from '../logging.js'
8
+
9
+ const host = env.getParam('--host', 'localhost')
10
+ const port = number.parseInt(env.getParam('--port', '8000'))
11
+ const paramOpenFile = env.getParam('-o', '')
12
+
13
+ /**
14
+ * @type {Object<string,string>}
15
+ */
16
+ const types = {
17
+ html: 'text/html',
18
+ css: 'text/css',
19
+ js: 'application/javascript',
20
+ mjs: 'application/javascript',
21
+ png: 'image/png',
22
+ jpg: 'image/jpeg',
23
+ jpeg: 'image/jpeg',
24
+ gif: 'image/gif',
25
+ json: 'application/json',
26
+ xml: 'application/xml'
27
+ }
28
+
29
+ const root = path.normalize(path.resolve('./'))
30
+
31
+ const server = http.createServer((req, res) => {
32
+ const url = req.url || '/index.html'
33
+ logging.print(logging.ORANGE, logging.BOLD, req.method || '', ' ', logging.GREY, logging.UNBOLD, url)
34
+ const extension = path.extname(url).slice(1)
35
+ /**
36
+ * @type {string}
37
+ */
38
+ const type = (extension && types[extension]) || types.html
39
+ const supportedExtension = Boolean(type)
40
+ if (!supportedExtension) {
41
+ res.writeHead(404, { 'Content-Type': 'text/html' })
42
+ res.end('404: File not found')
43
+ return
44
+ }
45
+ let fileName = url
46
+ if (url === '/') fileName = 'index.html'
47
+ else if (!extension) {
48
+ try {
49
+ fs.accessSync(path.join(root, url + '.html'), fs.constants.F_OK)
50
+ fileName = url + '.html'
51
+ } catch (e) {
52
+ fileName = path.join(url, 'index.html')
53
+ }
54
+ }
55
+
56
+ const filePath = path.join(root, fileName)
57
+ const isPathUnderRoot = path
58
+ .normalize(path.resolve(filePath))
59
+ .startsWith(root)
60
+
61
+ if (!isPathUnderRoot) {
62
+ res.writeHead(404, { 'Content-Type': 'text/html' })
63
+ res.end('404: File not found')
64
+ logging.print(logging.RED, logging.BOLD, 'Not Found: ', logging.GREY, logging.UNBOLD, url)
65
+ return
66
+ }
67
+
68
+ fs.readFile(filePath, (err, data) => {
69
+ if (err) {
70
+ logging.print(logging.RED, logging.BOLD, 'Cannot read file: ', logging.GREY, logging.UNBOLD, url)
71
+ res.writeHead(404, { 'Content-Type': 'text/html' })
72
+ res.end('404: File not found')
73
+ } else {
74
+ res.writeHead(200, { 'Content-Type': type })
75
+ res.end(data)
76
+ }
77
+ })
78
+ })
79
+
80
+ server.listen(port, host, () => {
81
+ logging.print(logging.BOLD, logging.ORANGE, `Server is running on http://${host}:${port}`)
82
+ if (paramOpenFile) {
83
+ const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open'
84
+ import('child_process').then(cp => {
85
+ cp.exec(`${start} http://${host}:${port}/${paramOpenFile}`)
86
+ })
87
+ }
88
+ })
@@ -39,7 +39,7 @@ const readPkg = (pkgJson, pathPrefix, importMap) => {
39
39
  object.forEach(pkgJson.exports, (v, k) => extractModMap(v, k, pkgJson.name, pathPrefix, importMap))
40
40
  object.forEach(pkgJson.dependencies, (_v, depName) => {
41
41
  const nextImportMap = pathPrefix === '.' ? exports : (scopes[pathPrefix + '/'] = {})
42
- const prefix = `${pathPrefix}/node_modules/${depName}`
42
+ const prefix = `./node_modules/${depName}`
43
43
  const depPkgJson = JSON.parse(fs.readFileSync(prefix + '/package.json', { encoding: 'utf8' }))
44
44
  readPkg(depPkgJson, prefix, nextImportMap)
45
45
  })