@vibe-x/dsh-html-workbench 0.3.1 → 0.3.2

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/src/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * resolved relative to THIS module (`import.meta.url`) — never hard-coded to a
9
9
  * specific machine — and handed to the shared body as `config.script`.
10
10
  */
11
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
11
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
12
12
  import { tmpdir } from 'node:os'
13
13
  import { dirname, join, resolve } from 'node:path'
14
14
  import { fileURLToPath } from 'node:url'
@@ -84,13 +84,38 @@ const runtimeDir = resolveRuntimeDir()
84
84
  const makePlugin = new Function(readFileSync(new URL('./host.js', import.meta.url), 'utf8'))
85
85
  const plugin = makePlugin()
86
86
 
87
+ const LOG_MAX_BYTES = 1024 * 1024
88
+ const LOG_BACKUP_COUNT = 2
89
+
90
+ function createHostLog(directory, port) {
91
+ const logDir = join(directory, 'logs')
92
+ const file = join(logDir, 'dsh-host-' + port + '.log')
93
+ return {
94
+ file,
95
+ append(entry) {
96
+ mkdirSync(logDir, { recursive: true })
97
+ const line = JSON.stringify({ ...entry, at: new Date(entry.at).toISOString(), pid: process.pid }) + '\n'
98
+ const size = statSync(file, { throwIfNoEntry: false })?.size || 0
99
+ if (size + Buffer.byteLength(line) > LOG_MAX_BYTES) {
100
+ for (let i = LOG_BACKUP_COUNT; i >= 1; i -= 1) {
101
+ const from = i === 1 ? file : file + '.' + (i - 1)
102
+ const to = file + '.' + i
103
+ if (existsSync(from)) renameSync(from, to)
104
+ }
105
+ }
106
+ appendFileSync(file, line, { encoding: 'utf8', mode: 0o600 })
107
+ },
108
+ }
109
+ }
110
+
87
111
  export const name = 'html-workbench'
88
112
  export const inject = plugin.inject ?? []
89
113
  // Inject paths resolved by Node rather than assuming POSIX separators. A
90
114
  // deployment-provided config may still override every value.
91
- export const apply = (ctx, config) => plugin.apply(ctx, {
92
- script: scriptPath,
93
- runtimeDir,
94
- version,
95
- ...(config || {}),
96
- })
115
+ export const apply = (ctx, config) => {
116
+ const options = { script: scriptPath, runtimeDir, version, ...(config || {}) }
117
+ return plugin.apply(ctx, {
118
+ ...options,
119
+ hostLog: createHostLog(options.runtimeDir || runtimeDir, options.port || 4317),
120
+ })
121
+ }