xxscreeps-mod-client 0.2.2 → 0.2.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.
Files changed (2) hide show
  1. package/backend.js +24 -3
  2. package/package.json +2 -2
package/backend.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import path from 'node:path'
2
2
  import { createRequire } from 'node:module'
3
- import { createReadStream, existsSync, statSync } from 'node:fs'
3
+ import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs'
4
4
  import { hooks } from 'xxscreeps/backend/index.js'
5
5
 
6
6
  const require = createRequire(import.meta.url)
7
+ const pkg = require('./package.json')
7
8
  const clientPkgPath = require.resolve('screeps-client/package.json')
8
9
  const distDir = path.join(path.dirname(clientPkgPath), 'dist', 'xxscreeps-mod')
9
10
  const indexFile = path.join(distDir, 'index.html')
@@ -63,6 +64,22 @@ function sendFile(ctx, filePath, stat) {
63
64
  ctx.body = createReadStream(filePath)
64
65
  }
65
66
 
67
+ function renderInjectedIndex(filePath) {
68
+ const metadata = JSON.stringify({
69
+ kind: 'xxscreeps-mod',
70
+ packageName: pkg.name,
71
+ version: pkg.version,
72
+ }).replace(/</g, '\\u003c')
73
+ const script = `<script>window.__SCREEPS_CLIENT_EMBEDDED__=${metadata}</script>`
74
+ const html = readFileSync(filePath, 'utf8')
75
+ return html.includes('</head>') ? html.replace('</head>', `${script}</head>`) : script + html
76
+ }
77
+
78
+ function sendInjectedIndex(ctx) {
79
+ ctx.type = 'text/html'
80
+ ctx.body = renderInjectedIndex(indexFile)
81
+ }
82
+
66
83
  hooks.register('middleware', koa => {
67
84
  if (!existsSync(indexFile)) {
68
85
  console.error(`[xxscreeps-mod-client] client bundle not found at ${indexFile}. Run "pnpm --filter screeps-client build:embedded:xxscreeps" first.`)
@@ -90,6 +107,11 @@ hooks.register('middleware', koa => {
90
107
  return next()
91
108
  }
92
109
 
110
+ if (relPath === '/' || relPath === '/index.html') {
111
+ sendInjectedIndex(ctx)
112
+ return
113
+ }
114
+
93
115
  // Serve a real file from dist if it exists.
94
116
  const found = resolveFile(relPath)
95
117
  if (found) {
@@ -104,8 +126,7 @@ hooks.register('middleware', koa => {
104
126
  if (ctx.status !== 404) return
105
127
  const last = relPath.split('/').pop() ?? ''
106
128
  if (last.includes('.')) return
107
- const indexStat = statSync(indexFile)
108
129
  ctx.status = 200
109
- sendFile(ctx, indexFile, indexStat)
130
+ sendInjectedIndex(ctx)
110
131
  })
111
132
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxscreeps-mod-client",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "xxscreeps mod that serves the screeps-client and connects it to the same server.",
5
5
  "type": "module",
6
6
  "xxscreeps": true,
@@ -23,7 +23,7 @@
23
23
  "node": ">=20"
24
24
  },
25
25
  "dependencies": {
26
- "screeps-client": "^0.3.0"
26
+ "screeps-client": "^0.3.2"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "xxscreeps": "*"