cwresdev 0.1.0

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 (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/bin/cwrespro.js +64 -0
  4. package/package.json +47 -0
  5. package/src/client/reload-client.js +40 -0
  6. package/src/config.js +211 -0
  7. package/src/index.js +19 -0
  8. package/src/injector.js +32 -0
  9. package/src/php.js +114 -0
  10. package/src/proxy.js +91 -0
  11. package/src/server.js +334 -0
  12. package/vendor/php/windows/README.md +140 -0
  13. package/vendor/php/windows/brotlicommon.dll +0 -0
  14. package/vendor/php/windows/brotlidec.dll +0 -0
  15. package/vendor/php/windows/deplister.exe +0 -0
  16. package/vendor/php/windows/dev/php8.lib +0 -0
  17. package/vendor/php/windows/ext/php_bz2.dll +0 -0
  18. package/vendor/php/windows/ext/php_com_dotnet.dll +0 -0
  19. package/vendor/php/windows/ext/php_curl.dll +0 -0
  20. package/vendor/php/windows/ext/php_dba.dll +0 -0
  21. package/vendor/php/windows/ext/php_dl_test.dll +0 -0
  22. package/vendor/php/windows/ext/php_enchant.dll +0 -0
  23. package/vendor/php/windows/ext/php_exif.dll +0 -0
  24. package/vendor/php/windows/ext/php_ffi.dll +0 -0
  25. package/vendor/php/windows/ext/php_fileinfo.dll +0 -0
  26. package/vendor/php/windows/ext/php_ftp.dll +0 -0
  27. package/vendor/php/windows/ext/php_gd.dll +0 -0
  28. package/vendor/php/windows/ext/php_gettext.dll +0 -0
  29. package/vendor/php/windows/ext/php_gmp.dll +0 -0
  30. package/vendor/php/windows/ext/php_intl.dll +0 -0
  31. package/vendor/php/windows/ext/php_ldap.dll +0 -0
  32. package/vendor/php/windows/ext/php_mbstring.dll +0 -0
  33. package/vendor/php/windows/ext/php_mysqli.dll +0 -0
  34. package/vendor/php/windows/ext/php_odbc.dll +0 -0
  35. package/vendor/php/windows/ext/php_opcache.dll +0 -0
  36. package/vendor/php/windows/ext/php_openssl.dll +0 -0
  37. package/vendor/php/windows/ext/php_pdo_firebird.dll +0 -0
  38. package/vendor/php/windows/ext/php_pdo_mysql.dll +0 -0
  39. package/vendor/php/windows/ext/php_pdo_odbc.dll +0 -0
  40. package/vendor/php/windows/ext/php_pdo_pgsql.dll +0 -0
  41. package/vendor/php/windows/ext/php_pdo_sqlite.dll +0 -0
  42. package/vendor/php/windows/ext/php_pgsql.dll +0 -0
  43. package/vendor/php/windows/ext/php_shmop.dll +0 -0
  44. package/vendor/php/windows/ext/php_snmp.dll +0 -0
  45. package/vendor/php/windows/ext/php_soap.dll +0 -0
  46. package/vendor/php/windows/ext/php_sockets.dll +0 -0
  47. package/vendor/php/windows/ext/php_sodium.dll +0 -0
  48. package/vendor/php/windows/ext/php_sqlite3.dll +0 -0
  49. package/vendor/php/windows/ext/php_sysvshm.dll +0 -0
  50. package/vendor/php/windows/ext/php_tidy.dll +0 -0
  51. package/vendor/php/windows/ext/php_xsl.dll +0 -0
  52. package/vendor/php/windows/ext/php_zend_test.dll +0 -0
  53. package/vendor/php/windows/ext/php_zip.dll +0 -0
  54. package/vendor/php/windows/extras/ssl/legacy.dll +0 -0
  55. package/vendor/php/windows/extras/ssl/openssl.cnf +390 -0
  56. package/vendor/php/windows/glib-2.dll +0 -0
  57. package/vendor/php/windows/gmodule-2.dll +0 -0
  58. package/vendor/php/windows/icudt77.dll +0 -0
  59. package/vendor/php/windows/icuin77.dll +0 -0
  60. package/vendor/php/windows/icuio77.dll +0 -0
  61. package/vendor/php/windows/icuuc77.dll +0 -0
  62. package/vendor/php/windows/lib/enchant/libenchant2_hunspell.dll +0 -0
  63. package/vendor/php/windows/libcrypto-3-x64.dll +0 -0
  64. package/vendor/php/windows/libenchant2.dll +0 -0
  65. package/vendor/php/windows/libpq.dll +0 -0
  66. package/vendor/php/windows/libsasl.dll +0 -0
  67. package/vendor/php/windows/libsodium.dll +0 -0
  68. package/vendor/php/windows/libsqlite3.dll +0 -0
  69. package/vendor/php/windows/libssh2.dll +0 -0
  70. package/vendor/php/windows/libssl-3-x64.dll +0 -0
  71. package/vendor/php/windows/libzstd.dll +0 -0
  72. package/vendor/php/windows/license.txt +68 -0
  73. package/vendor/php/windows/manifest.json +4 -0
  74. package/vendor/php/windows/news.txt +2204 -0
  75. package/vendor/php/windows/nghttp2.dll +0 -0
  76. package/vendor/php/windows/phar.phar.bat +1 -0
  77. package/vendor/php/windows/pharcommand.phar +0 -0
  78. package/vendor/php/windows/php-cgi.exe +0 -0
  79. package/vendor/php/windows/php-win.exe +0 -0
  80. package/vendor/php/windows/php.exe +0 -0
  81. package/vendor/php/windows/php.ini +1848 -0
  82. package/vendor/php/windows/php.ini-development +1848 -0
  83. package/vendor/php/windows/php.ini-production +1850 -0
  84. package/vendor/php/windows/php8.dll +0 -0
  85. package/vendor/php/windows/php8embed.lib +0 -0
  86. package/vendor/php/windows/php8phpdbg.dll +0 -0
  87. package/vendor/php/windows/phpdbg.exe +0 -0
  88. package/vendor/php/windows/readme-redist-bins.txt +681 -0
  89. package/vendor/php/windows/snapshot.txt +157 -0
package/src/server.js ADDED
@@ -0,0 +1,334 @@
1
+ const fs = require("node:fs");
2
+ const http = require("node:http");
3
+ const path = require("node:path");
4
+
5
+ const chokidar = require("chokidar");
6
+ const express = require("express");
7
+ const { WebSocketServer } = require("ws");
8
+
9
+ const { CLIENT_SCRIPT_ROUTE, getClientScriptSource, injectLiveReload } = require("./injector");
10
+ const { startPhpServer, stopPhpServer } = require("./php");
11
+ const { buildTargetUrl, proxyRequest } = require("./proxy");
12
+
13
+ const HTML_EXTENSIONS = new Set([".htm", ".html", ".shtm", ".shtml"]);
14
+ const MAX_SSI_INCLUDE_DEPTH = 16;
15
+ const SSI_INCLUDE_PATTERN = /<!--#include\s+(virtual|file)\s*=\s*"([^"]+)"\s*-->/gi;
16
+
17
+ function createLogger(quiet) {
18
+ if (quiet) {
19
+ return {
20
+ error() {},
21
+ info() {},
22
+ warn() {},
23
+ };
24
+ }
25
+
26
+ return console;
27
+ }
28
+
29
+ function safeResolve(docRoot, requestPath) {
30
+ const docRootResolved = path.resolve(docRoot);
31
+ const trimmedPath = requestPath === "/" ? "" : requestPath.replace(/^\/+/, "");
32
+ const candidate = path.resolve(docRootResolved, trimmedPath);
33
+
34
+ if (candidate !== docRootResolved && !candidate.startsWith(`${docRootResolved}${path.sep}`)) {
35
+ throw new Error(`Refusing to serve a path outside the doc root: ${requestPath}`);
36
+ }
37
+
38
+ return candidate;
39
+ }
40
+
41
+ async function pathExists(candidate) {
42
+ try {
43
+ await fs.promises.access(candidate, fs.constants.F_OK);
44
+ return true;
45
+ } catch (error) {
46
+ return false;
47
+ }
48
+ }
49
+
50
+ function isHtmlFilePath(filePath) {
51
+ return HTML_EXTENSIONS.has(path.extname(filePath).toLowerCase());
52
+ }
53
+
54
+ function assertWithinDocRoot(docRoot, candidatePath, sourceLabel) {
55
+ const docRootResolved = path.resolve(docRoot);
56
+ const candidateResolved = path.resolve(candidatePath);
57
+
58
+ if (candidateResolved !== docRootResolved && !candidateResolved.startsWith(`${docRootResolved}${path.sep}`)) {
59
+ throw new Error(`SSI include resolves outside the doc root: ${sourceLabel}`);
60
+ }
61
+
62
+ return candidateResolved;
63
+ }
64
+
65
+ function resolveSsiIncludePath(docRoot, currentFilePath, includeType, includePath) {
66
+ const trimmedPath = typeof includePath === "string" ? includePath.trim() : "";
67
+
68
+ if (!trimmedPath) {
69
+ throw new Error("SSI include path must be a non-empty string.");
70
+ }
71
+
72
+ if (includeType === "file" && path.isAbsolute(trimmedPath)) {
73
+ throw new Error(`SSI file include must be relative: ${trimmedPath}`);
74
+ }
75
+
76
+ if (includeType === "virtual" && trimmedPath.startsWith("/")) {
77
+ return safeResolve(docRoot, trimmedPath);
78
+ }
79
+
80
+ const candidate = path.resolve(path.dirname(currentFilePath), trimmedPath);
81
+ return assertWithinDocRoot(docRoot, candidate, trimmedPath);
82
+ }
83
+
84
+ async function renderHtmlWithSsi(docRoot, filePath, state = {}) {
85
+ const depth = state.depth ?? 0;
86
+ const seenFiles = state.seenFiles ?? new Set();
87
+ const resolvedFilePath = assertWithinDocRoot(docRoot, filePath, filePath);
88
+
89
+ if (depth > MAX_SSI_INCLUDE_DEPTH) {
90
+ throw new Error(`SSI include depth exceeded while resolving ${resolvedFilePath}`);
91
+ }
92
+
93
+ if (seenFiles.has(resolvedFilePath)) {
94
+ throw new Error(`SSI circular include detected for ${resolvedFilePath}`);
95
+ }
96
+
97
+ const fileContents = await fs.promises.readFile(resolvedFilePath, "utf8");
98
+ const matches = Array.from(fileContents.matchAll(SSI_INCLUDE_PATTERN));
99
+
100
+ if (matches.length === 0) {
101
+ return fileContents;
102
+ }
103
+
104
+ const nextSeenFiles = new Set(seenFiles);
105
+ nextSeenFiles.add(resolvedFilePath);
106
+
107
+ let renderedHtml = "";
108
+ let lastIndex = 0;
109
+
110
+ for (const match of matches) {
111
+ const [directive, includeType, includePath] = match;
112
+ const directiveIndex = match.index ?? 0;
113
+ const includeFilePath = resolveSsiIncludePath(docRoot, resolvedFilePath, includeType, includePath);
114
+ const includeStats = await fs.promises.stat(includeFilePath).catch(() => null);
115
+
116
+ if (!includeStats || !includeStats.isFile()) {
117
+ throw new Error(`SSI include not found: ${includePath}`);
118
+ }
119
+
120
+ renderedHtml += fileContents.slice(lastIndex, directiveIndex);
121
+ renderedHtml += await renderHtmlWithSsi(docRoot, includeFilePath, {
122
+ depth: depth + 1,
123
+ seenFiles: nextSeenFiles,
124
+ });
125
+ lastIndex = directiveIndex + directive.length;
126
+ }
127
+
128
+ renderedHtml += fileContents.slice(lastIndex);
129
+ return renderedHtml;
130
+ }
131
+
132
+ async function resolveRequestTarget(docRoot, requestPath) {
133
+ const decodedPath = decodeURIComponent(requestPath);
134
+ const candidate = safeResolve(docRoot, decodedPath);
135
+
136
+ if (await pathExists(candidate)) {
137
+ const stats = await fs.promises.stat(candidate);
138
+
139
+ if (stats.isFile()) {
140
+ if (candidate.toLowerCase().endsWith(".php")) {
141
+ return { found: true, type: "php", filePath: candidate };
142
+ }
143
+
144
+ if (isHtmlFilePath(candidate)) {
145
+ return { found: true, type: "html", filePath: candidate };
146
+ }
147
+
148
+ return { found: true, type: "asset", filePath: candidate };
149
+ }
150
+
151
+ if (stats.isDirectory()) {
152
+ if (!requestPath.endsWith("/")) {
153
+ return { found: true, type: "directory", redirectTo: requestPath + "/" };
154
+ }
155
+
156
+ const indexPhp = path.join(candidate, "index.php");
157
+ const indexHtml = path.join(candidate, "index.html");
158
+ const indexHtm = path.join(candidate, "index.htm");
159
+ const indexShtml = path.join(candidate, "index.shtml");
160
+ const indexShtm = path.join(candidate, "index.shtm");
161
+
162
+ if (await pathExists(indexPhp)) {
163
+ return { found: true, type: "php", filePath: indexPhp };
164
+ }
165
+
166
+ if (await pathExists(indexHtml)) {
167
+ return { found: true, type: "html", filePath: indexHtml };
168
+ }
169
+
170
+ if (await pathExists(indexHtm)) {
171
+ return { found: true, type: "html", filePath: indexHtm };
172
+ }
173
+
174
+ if (await pathExists(indexShtml)) {
175
+ return { found: true, type: "html", filePath: indexShtml };
176
+ }
177
+
178
+ if (await pathExists(indexShtm)) {
179
+ return { found: true, type: "html", filePath: indexShtm };
180
+ }
181
+ }
182
+ }
183
+
184
+ return { found: false };
185
+ }
186
+
187
+ function broadcastReload(clients) {
188
+ for (const client of clients) {
189
+ if (client.readyState === client.OPEN) {
190
+ client.send("reload");
191
+ }
192
+ }
193
+ }
194
+
195
+ async function startDevServer(config) {
196
+ if (!fs.existsSync(config.docRoot)) {
197
+ throw new Error(`Doc root does not exist: ${config.docRoot}`);
198
+ }
199
+
200
+ const logger = createLogger(config.quiet);
201
+ const packageRoot = path.resolve(__dirname, "..");
202
+ const phpServer = await startPhpServer({
203
+ packageRoot,
204
+ docRoot: config.docRoot,
205
+ host: config.host,
206
+ logger,
207
+ phpBinary: config.phpBinary,
208
+ });
209
+
210
+ const app = express();
211
+ const httpServer = http.createServer(app);
212
+ const wss = new WebSocketServer({ noServer: true });
213
+ const wsClients = new Set();
214
+ const clientScript = getClientScriptSource();
215
+
216
+ app.disable("x-powered-by");
217
+
218
+ httpServer.on("upgrade", (req, socket, head) => {
219
+ if (req.url !== "/__cwrespro/ws") {
220
+ socket.destroy();
221
+ return;
222
+ }
223
+
224
+ wss.handleUpgrade(req, socket, head, (ws) => {
225
+ wsClients.add(ws);
226
+ ws.on("close", () => wsClients.delete(ws));
227
+ });
228
+ });
229
+
230
+ app.get(CLIENT_SCRIPT_ROUTE, (req, res) => {
231
+ res.type("application/javascript");
232
+ res.send(clientScript);
233
+ });
234
+
235
+ app.use("/objects", async (req, res, next) => {
236
+ try {
237
+ await proxyRequest({
238
+ req,
239
+ res,
240
+ target: buildTargetUrl(config.proxyOrigin, req),
241
+ });
242
+ } catch (error) {
243
+ next(error);
244
+ }
245
+ });
246
+
247
+ app.use(async (req, res, next) => {
248
+ try {
249
+ const target = await resolveRequestTarget(config.docRoot, req.path);
250
+
251
+ if (!target.found) {
252
+ res.status(404).type("text/plain").send(`Not found: ${req.path}`);
253
+ return;
254
+ }
255
+
256
+ if (target.type === "directory") {
257
+ const query = req.originalUrl.includes("?") ? req.originalUrl.slice(req.originalUrl.indexOf("?")) : "";
258
+ res.redirect(301, target.redirectTo + query);
259
+ return;
260
+ }
261
+
262
+ if (target.type === "php") {
263
+ await proxyRequest({
264
+ req,
265
+ res,
266
+ target: buildTargetUrl(phpServer.baseUrl, req),
267
+ injectHtml: true,
268
+ });
269
+ return;
270
+ }
271
+
272
+ if (target.type === "html") {
273
+ const html = await renderHtmlWithSsi(config.docRoot, target.filePath);
274
+ res.type("html");
275
+ res.send(injectLiveReload(html));
276
+ return;
277
+ }
278
+
279
+ res.sendFile(target.filePath);
280
+ } catch (error) {
281
+ next(error);
282
+ }
283
+ });
284
+
285
+ app.use((error, req, res, next) => {
286
+ logger.error(`[cwrespro] ${error.stack || error.message}`);
287
+ if (res.headersSent) {
288
+ next(error);
289
+ return;
290
+ }
291
+
292
+ res.status(500).type("text/plain").send(error.message);
293
+ });
294
+
295
+ let reloadTimer = null;
296
+ const watcher = chokidar.watch(config.docRoot, {
297
+ ignoreInitial: true,
298
+ ignored: config.watchIgnored,
299
+ });
300
+
301
+ watcher.on("all", (eventName, filePath) => {
302
+ clearTimeout(reloadTimer);
303
+ reloadTimer = setTimeout(() => {
304
+ broadcastReload(wsClients);
305
+ }, 75);
306
+ });
307
+
308
+ await new Promise((resolve, reject) => {
309
+ httpServer.once("error", reject);
310
+ httpServer.listen(config.port, config.host, resolve);
311
+ });
312
+
313
+ const address = httpServer.address();
314
+ const url = `http://${config.host}:${address.port}/`;
315
+
316
+ async function close() {
317
+ clearTimeout(reloadTimer);
318
+ await watcher.close();
319
+ await new Promise((resolve) => httpServer.close(resolve));
320
+ wss.close();
321
+ await stopPhpServer(phpServer);
322
+ }
323
+
324
+ return {
325
+ close,
326
+ phpServer,
327
+ port: address.port,
328
+ url,
329
+ };
330
+ }
331
+
332
+ module.exports = {
333
+ startDevServer,
334
+ };
@@ -0,0 +1,140 @@
1
+ <div align="center">
2
+ <a href="https://www.php.net">
3
+ <img
4
+ alt="PHP"
5
+ src="https://www.php.net/images/logos/new-php-logo.svg"
6
+ width="150">
7
+ </a>
8
+ </div>
9
+
10
+ # The PHP Interpreter
11
+
12
+ PHP is a popular general-purpose scripting language that is especially suited to
13
+ web development. Fast, flexible and pragmatic, PHP powers everything from your
14
+ blog to the most popular websites in the world. PHP is distributed under the
15
+ [PHP License v3.01](LICENSE).
16
+
17
+ [![Push](https://github.com/php/php-src/actions/workflows/push.yml/badge.svg)](https://github.com/php/php-src/actions/workflows/push.yml)
18
+ [![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/php.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:php)
19
+
20
+ ## Documentation
21
+
22
+ The PHP manual is available at [php.net/docs](https://www.php.net/docs).
23
+
24
+ ## Installation
25
+
26
+ ### Prebuilt packages and binaries
27
+
28
+ Prebuilt packages and binaries can be used to get up and running fast with PHP.
29
+
30
+ For Windows, the PHP binaries can be obtained from
31
+ [windows.php.net](https://windows.php.net). After extracting the archive the
32
+ `*.exe` files are ready to use.
33
+
34
+ For other systems, see the [installation chapter](https://www.php.net/install).
35
+
36
+ ### Building PHP source code
37
+
38
+ *For Windows, see [Build your own PHP on Windows](https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2).*
39
+
40
+ For a minimal PHP build from Git, you will need autoconf, bison, and re2c. For
41
+ a default build, you will additionally need libxml2 and libsqlite3.
42
+
43
+ On Ubuntu, you can install these using:
44
+
45
+ sudo apt install -y pkg-config build-essential autoconf bison re2c \
46
+ libxml2-dev libsqlite3-dev
47
+
48
+ On Fedora, you can install these using:
49
+
50
+ sudo dnf install re2c bison autoconf make libtool ccache libxml2-devel sqlite-devel
51
+
52
+ Generate configure:
53
+
54
+ ./buildconf
55
+
56
+ Configure your build. `--enable-debug` is recommended for development, see
57
+ `./configure --help` for a full list of options.
58
+
59
+ # For development
60
+ ./configure --enable-debug
61
+ # For production
62
+ ./configure
63
+
64
+ Build PHP. To speed up the build, specify the maximum number of jobs using `-j`:
65
+
66
+ make -j4
67
+
68
+ The number of jobs should usually match the number of available cores, which
69
+ can be determined using `nproc`.
70
+
71
+ ## Testing PHP source code
72
+
73
+ PHP ships with an extensive test suite, the command `make test` is used after
74
+ successful compilation of the sources to run this test suite.
75
+
76
+ It is possible to run tests using multiple cores by setting `-jN` in
77
+ `TEST_PHP_ARGS`:
78
+
79
+ make TEST_PHP_ARGS=-j4 test
80
+
81
+ Shall run `make test` with a maximum of 4 concurrent jobs: Generally the maximum
82
+ number of jobs should not exceed the number of cores available.
83
+
84
+ The [qa.php.net](https://qa.php.net) site provides more detailed info about
85
+ testing and quality assurance.
86
+
87
+ ## Installing PHP built from source
88
+
89
+ After a successful build (and test), PHP may be installed with:
90
+
91
+ make install
92
+
93
+ Depending on your permissions and prefix, `make install` may need super user
94
+ permissions.
95
+
96
+ ## PHP extensions
97
+
98
+ Extensions provide additional functionality on top of PHP. PHP consists of many
99
+ essential bundled extensions. Additional extensions can be found in the PHP
100
+ Extension Community Library - [PECL](https://pecl.php.net).
101
+
102
+ ## Contributing
103
+
104
+ The PHP source code is located in the Git repository at
105
+ [github.com/php/php-src](https://github.com/php/php-src). Contributions are most
106
+ welcome by forking the repository and sending a pull request.
107
+
108
+ Discussions are done on GitHub, but depending on the topic can also be relayed
109
+ to the official PHP developer mailing list internals@lists.php.net.
110
+
111
+ New features require an RFC and must be accepted by the developers. See
112
+ [Request for comments - RFC](https://wiki.php.net/rfc) and
113
+ [Voting on PHP features](https://wiki.php.net/rfc/voting) for more information
114
+ on the process.
115
+
116
+ Bug fixes don't require an RFC. If the bug has a GitHub issue, reference it in
117
+ the commit message using `GH-NNNNNN`. Use `#NNNNNN` for tickets in the old
118
+ [bugs.php.net](https://bugs.php.net) bug tracker.
119
+
120
+ Fix GH-7815: php_uname doesn't recognise latest Windows versions
121
+ Fix #55371: get_magic_quotes_gpc() throws deprecation warning
122
+
123
+ See [Git workflow](https://wiki.php.net/vcs/gitworkflow) for details on how pull
124
+ requests are merged.
125
+
126
+ ### Guidelines for contributors
127
+
128
+ See further documents in the repository for more information on how to
129
+ contribute:
130
+
131
+ - [Contributing to PHP](/CONTRIBUTING.md)
132
+ - [PHP coding standards](/CODING_STANDARDS.md)
133
+ - [Internal documentation](https://php.github.io/php-src/)
134
+ - [Mailing list rules](/docs/mailinglist-rules.md)
135
+ - [PHP release process](/docs/release-process.md)
136
+
137
+ ## Credits
138
+
139
+ For the list of people who've put work into PHP, please see the
140
+ [PHP credits page](https://www.php.net/credits.php).
Binary file
Binary file
Binary file