@eighty4/dank 0.0.1 → 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Adam McKee Bennett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/lib/bin.ts CHANGED
@@ -15,10 +15,10 @@ function printHelp(task?: 'build' | 'serve'): never {
15
15
  )
16
16
  }
17
17
  console.log('\nOPTIONS:')
18
+ if (!task || task === 'serve')
19
+ console.log(' --log-http print access logs')
18
20
  console.log(' --minify minify sources')
19
- // if (!task || task === 'serve') {
20
- // console.log(' --preview pre-bundle and build ServiceWorker')
21
- // }
21
+ // if (!task || task === 'serve') console.log(' --preview pre-bundle and build ServiceWorker')
22
22
  console.log(' --production build for production release')
23
23
  if (task) {
24
24
  console.log()
package/lib/flags.ts CHANGED
@@ -1,3 +1,7 @@
1
+ // `dank serve` will print http access logs to console
2
+ export const isLogHttp = () =>
3
+ process.env.LOG_HTTP === 'true' || process.argv.includes('--log-http')
4
+
1
5
  // `dank serve` will pre-bundle and use service worker
2
6
  export const isPreviewBuild = () =>
3
7
  process.env.PREVIEW === 'true' || process.argv.includes('--preview')
package/lib/http.ts CHANGED
@@ -8,7 +8,8 @@ import {
8
8
  type ServerResponse,
9
9
  } from 'node:http'
10
10
  import { extname, join } from 'node:path'
11
- import { isProductionBuild } from './flags.ts'
11
+ import mime from 'mime'
12
+ import { isLogHttp } from './flags.ts'
12
13
 
13
14
  export type FrontendFetcher = (
14
15
  url: URL,
@@ -21,7 +22,7 @@ export function createWebServer(
21
22
  frontendFetcher: FrontendFetcher,
22
23
  ): ReturnType<typeof createServer> {
23
24
  const serverAddress = 'http://localhost:' + port
24
- return createServer((req: IncomingMessage, res: ServerResponse) => {
25
+ const handler = (req: IncomingMessage, res: ServerResponse) => {
25
26
  if (!req.url || !req.method) {
26
27
  res.end()
27
28
  } else {
@@ -33,7 +34,19 @@ export function createWebServer(
33
34
  frontendFetcher(url, convertHeadersToFetch(req.headers), res)
34
35
  }
35
36
  }
36
- })
37
+ }
38
+ return createServer(isLogHttp() ? createLogWrapper(handler) : handler)
39
+ }
40
+
41
+ type RequestListener = (req: IncomingMessage, res: ServerResponse) => void
42
+ function createLogWrapper(handler: RequestListener): RequestListener {
43
+ return (req, res) => {
44
+ console.log(' > ', req.method, req.url)
45
+ res.on('close', () => {
46
+ console.log('', res.statusCode, req.method, req.url)
47
+ })
48
+ handler(req, res)
49
+ }
37
50
  }
38
51
 
39
52
  export function createBuiltDistFilesFetcher(
@@ -72,7 +85,7 @@ export function createDevServeFilesFetcher(
72
85
  const proxyAddress = 'http://127.0.0.1:' + opts.proxyPort
73
86
  return (url: URL, _headers: Headers, res: ServerResponse) => {
74
87
  if (opts.pages[url.pathname]) {
75
- streamFile(join(opts.pagesDir, url.pathname + 'index.html'), res)
88
+ streamFile(join(opts.pagesDir, url.pathname, 'index.html'), res)
76
89
  } else {
77
90
  const maybePublicPath = join(opts.publicDir, url.pathname)
78
91
  exists(join(opts.publicDir, url.pathname)).then(fromPublic => {
@@ -147,8 +160,7 @@ async function exists(p: string): Promise<boolean> {
147
160
  }
148
161
 
149
162
  function streamFile(p: string, res: ServerResponse) {
150
- const mimeType = resolveMimeType(p)
151
- res.setHeader('Content-Type', mimeType)
163
+ res.setHeader('Content-Type', mime.getType(p) || 'application/octet-stream')
152
164
  const reading = createReadStream(p)
153
165
  reading.pipe(res)
154
166
  reading.on('error', err => {
@@ -158,33 +170,6 @@ function streamFile(p: string, res: ServerResponse) {
158
170
  })
159
171
  }
160
172
 
161
- function resolveMimeType(p: string): string {
162
- switch (extname(p)) {
163
- case '.html':
164
- return 'text/html'
165
- case '.js':
166
- return 'text/javascript'
167
- case '.json':
168
- return 'application/json'
169
- case '.css':
170
- return 'text/css'
171
- case '.svg':
172
- return 'image/svg+xml'
173
- case '.png':
174
- return 'image/png'
175
- case '.ttf':
176
- return 'font/ttf'
177
- case '.woff':
178
- return 'font/woff'
179
- case '.woff2':
180
- return 'font/woff2'
181
- default:
182
- console.warn('? mime type for', p)
183
- if (!isProductionBuild()) process.exit(1)
184
- return 'application/octet-stream'
185
- }
186
- }
187
-
188
173
  function convertHeadersFromFetch(from: Headers): OutgoingHttpHeaders {
189
174
  const to: OutgoingHttpHeaders = {}
190
175
  for (const name of from.keys()) {
package/lib_js/bin.js CHANGED
@@ -12,10 +12,10 @@ function printHelp(task) {
12
12
  'dank serve [--minify] [--production]');
13
13
  }
14
14
  console.log('\nOPTIONS:');
15
+ if (!task || task === 'serve')
16
+ console.log(' --log-http print access logs');
15
17
  console.log(' --minify minify sources');
16
- // if (!task || task === 'serve') {
17
- // console.log(' --preview pre-bundle and build ServiceWorker')
18
- // }
18
+ // if (!task || task === 'serve') console.log(' --preview pre-bundle and build ServiceWorker')
19
19
  console.log(' --production build for production release');
20
20
  if (task) {
21
21
  console.log();
package/lib_js/flags.js CHANGED
@@ -1,3 +1,5 @@
1
+ // `dank serve` will print http access logs to console
2
+ export const isLogHttp = () => process.env.LOG_HTTP === 'true' || process.argv.includes('--log-http');
1
3
  // `dank serve` will pre-bundle and use service worker
2
4
  export const isPreviewBuild = () => process.env.PREVIEW === 'true' || process.argv.includes('--preview');
3
5
  // `dank build` will minify sources and append git release tag to build tag
package/lib_js/http.js CHANGED
@@ -2,10 +2,11 @@ import { createReadStream } from 'node:fs';
2
2
  import { stat } from 'node:fs/promises';
3
3
  import { createServer, } from 'node:http';
4
4
  import { extname, join } from 'node:path';
5
- import { isProductionBuild } from "./flags.js";
5
+ import mime from 'mime';
6
+ import { isLogHttp } from "./flags.js";
6
7
  export function createWebServer(port, frontendFetcher) {
7
8
  const serverAddress = 'http://localhost:' + port;
8
- return createServer((req, res) => {
9
+ const handler = (req, res) => {
9
10
  if (!req.url || !req.method) {
10
11
  res.end();
11
12
  }
@@ -19,7 +20,17 @@ export function createWebServer(port, frontendFetcher) {
19
20
  frontendFetcher(url, convertHeadersToFetch(req.headers), res);
20
21
  }
21
22
  }
22
- });
23
+ };
24
+ return createServer(isLogHttp() ? createLogWrapper(handler) : handler);
25
+ }
26
+ function createLogWrapper(handler) {
27
+ return (req, res) => {
28
+ console.log(' > ', req.method, req.url);
29
+ res.on('close', () => {
30
+ console.log('', res.statusCode, req.method, req.url);
31
+ });
32
+ handler(req, res);
33
+ };
23
34
  }
24
35
  export function createBuiltDistFilesFetcher(dir, files) {
25
36
  return (url, _headers, res) => {
@@ -39,7 +50,7 @@ export function createDevServeFilesFetcher(opts) {
39
50
  const proxyAddress = 'http://127.0.0.1:' + opts.proxyPort;
40
51
  return (url, _headers, res) => {
41
52
  if (opts.pages[url.pathname]) {
42
- streamFile(join(opts.pagesDir, url.pathname + 'index.html'), res);
53
+ streamFile(join(opts.pagesDir, url.pathname, 'index.html'), res);
43
54
  }
44
55
  else {
45
56
  const maybePublicPath = join(opts.publicDir, url.pathname);
@@ -107,8 +118,7 @@ async function exists(p) {
107
118
  }
108
119
  }
109
120
  function streamFile(p, res) {
110
- const mimeType = resolveMimeType(p);
111
- res.setHeader('Content-Type', mimeType);
121
+ res.setHeader('Content-Type', mime.getType(p) || 'application/octet-stream');
112
122
  const reading = createReadStream(p);
113
123
  reading.pipe(res);
114
124
  reading.on('error', err => {
@@ -117,33 +127,6 @@ function streamFile(p, res) {
117
127
  res.end();
118
128
  });
119
129
  }
120
- function resolveMimeType(p) {
121
- switch (extname(p)) {
122
- case '.html':
123
- return 'text/html';
124
- case '.js':
125
- return 'text/javascript';
126
- case '.json':
127
- return 'application/json';
128
- case '.css':
129
- return 'text/css';
130
- case '.svg':
131
- return 'image/svg+xml';
132
- case '.png':
133
- return 'image/png';
134
- case '.ttf':
135
- return 'font/ttf';
136
- case '.woff':
137
- return 'font/woff';
138
- case '.woff2':
139
- return 'font/woff2';
140
- default:
141
- console.warn('? mime type for', p);
142
- if (!isProductionBuild())
143
- process.exit(1);
144
- return 'application/octet-stream';
145
- }
146
- }
147
130
  function convertHeadersFromFetch(from) {
148
131
  const to = {};
149
132
  for (const name of from.keys()) {
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@eighty4/dank",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
+ "license": "MIT",
6
+ "author": "Adam McKee Bennett <adam.be.g84d@gmail.com>",
5
7
  "description": "Multi-page development system for CDN-deployed websites",
6
8
  "keywords": [
7
9
  "frontend",
@@ -19,6 +21,7 @@
19
21
  },
20
22
  "dependencies": {
21
23
  "esbuild": "^0.25.10",
24
+ "mime": "^4.1.0",
22
25
  "parse5": "^8.0.0"
23
26
  },
24
27
  "devDependencies": {