wurzel 0.0.16 → 0.0.18

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/dist/lib/index.js +23 -12
  2. package/package.json +4 -4
package/dist/lib/index.js CHANGED
@@ -12,8 +12,7 @@ import { readFile } from "node:fs/promises";
12
12
  import { pathToFileURL, fileURLToPath } from "node:url";
13
13
  import { createHash } from "node:crypto";
14
14
  import * as importMetaResolve from "import-meta-resolve";
15
- // @ts-expect-error missing types
16
- import { transpileCode } from "commentscript";
15
+ import tsBlankSpace from "ts-blank-space";
17
16
  import { LRUCache } from "lru-cache";
18
17
  /*import type Express from "express";*/
19
18
 
@@ -68,7 +67,7 @@ const defaultResolveImportPath/*: TResolveImportPathFunc*/ = async ({ importer,
68
67
  };
69
68
 
70
69
  // es6-debug-server rejects uris containing "//" or "..", and a file path must not contain null bytes,
71
- // so such uris have to be answered before reaching es6-debug-server
70
+ // such uris are answered right away, whatever else es6-debug-server rejects is caught in serveScript
72
71
  const isMalformedUri = ({ uri }/*: { uri: string }*/) => {
73
72
  return uri.includes("//") || uri.split("/").includes("..") || uri.includes("\0");
74
73
  };
@@ -159,7 +158,7 @@ const expressRouter = ({
159
158
  });
160
159
 
161
160
  // eslint-disable-next-line complexity
162
- const maybeTranspile = async ({ filePath, code }/*: { filePath: string, code: string }*/)/*: Promise<TTranspileResult>*/ => {
161
+ const maybeTranspile = ({ filePath, code }/*: { filePath: string, code: string }*/)/*: TTranspileResult*/ => {
163
162
  if (filePath.endsWith(".js") || filePath.endsWith(".mjs")) {
164
163
  return {
165
164
  error: undefined,
@@ -181,18 +180,17 @@ const expressRouter = ({
181
180
  let transpiled/*: string | undefined*/ = undefined;
182
181
 
183
182
  try {
184
- const result = await transpileCode({ code });
185
- transpiled = result.transpiledCode;
183
+ // ts-blank-space reports syntax that cannot simply be blanked out, e.g. enums, instead of failing
184
+ transpiled = tsBlankSpace(code, (node) => {
185
+ const syntax = code.substring(node.pos, node.end).trim();
186
+ throw Error(`unsupported TypeScript syntax "${syntax}"`);
187
+ });
186
188
  } catch (ex) {
187
189
  return {
188
190
  error: ex /*as Error*/,
189
191
  };
190
192
  }
191
193
 
192
- if (transpiled === undefined) {
193
- throw Error("BUG: transpiled code is undefined");
194
- }
195
-
196
194
  transpileCache.set(hash, transpiled);
197
195
 
198
196
  return {
@@ -205,6 +203,13 @@ const expressRouter = ({
205
203
 
206
204
  scriptRootFolder: baseFolder,
207
205
 
206
+ // es6-debug-server serves only the scripts in the script root and what they import, which files are
207
+ // scripts has to agree with the routing below, or scripts classified by a custom determineFileTypeByPath
208
+ // are routed to es6-debug-server but not served
209
+ isScriptFile: ({ filePath }) => {
210
+ return determineFileTypeByPath({ filePath }) === "script";
211
+ },
212
+
208
213
  tryReadScriptAsString: async ({ filePath }) => {
209
214
 
210
215
  const { error: readError, content } = await readFile(filePath, "utf8").then((fileContent) => {
@@ -234,7 +239,7 @@ const expressRouter = ({
234
239
  };
235
240
  }
236
241
 
237
- const { error: transpileError, transpiled } = await maybeTranspile({
242
+ const { error: transpileError, transpiled } = maybeTranspile({
238
243
  filePath,
239
244
  code: content
240
245
  });
@@ -288,7 +293,8 @@ const expressRouter = ({
288
293
  return;
289
294
  }
290
295
 
291
- server.handleRequest({
296
+ // up to es6-debug-server 0.0.15, handleRequest is declared as returning unknown, not as a promise
297
+ Promise.resolve(server.handleRequest({
292
298
  uri: filePath,
293
299
 
294
300
  // a relative redirect is resolved by the client against the url it requested, which keeps it below
@@ -313,6 +319,11 @@ const expressRouter = ({
313
319
  console.error(error);
314
320
  res.status(500).end("internal server error");
315
321
  }
322
+
323
+ // es6-debug-server rejects a uri it takes for malformed, e.g. one with a ".." segment between backslashes,
324
+ // unanswered that rejection would take down the whole process
325
+ })).catch(() => {
326
+ res.status(400).end("bad request");
316
327
  });
317
328
  };
318
329
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.16",
2
+ "version": "0.0.18",
3
3
  "name": "wurzel",
4
4
  "type": "module",
5
5
  "description": "A tool to serve script files from different directories",
@@ -8,11 +8,11 @@
8
8
  ],
9
9
  "main": "dist/lib/index.js",
10
10
  "dependencies": {
11
- "commentscript": "^0.0.10",
12
- "es6-debug-server": "^0.0.14",
11
+ "es6-debug-server": "^0.0.15",
13
12
  "esm-resource": "^0.0.3",
14
13
  "import-meta-resolve": "^4.0.0",
15
- "lru-cache": "^10.2.1"
14
+ "lru-cache": "^10.2.1",
15
+ "ts-blank-space": "^0.9.0"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@eslint/js": "^10.0.1",