@zipadee/javascript 0.0.6 → 0.0.7
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/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/serve.d.ts +21 -21
- package/lib/serve.d.ts.map +1 -1
- package/lib/serve.js +71 -74
- package/lib/serve.js.map +1 -1
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './lib/serve.js';
|
|
2
|
-
//# sourceMappingURL=index.d.ts.map
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './lib/serve.js';
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
package/lib/serve.d.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import {type Middleware} from '@zipadee/core';
|
|
1
|
+
import { type Middleware } from '@zipadee/core';
|
|
2
2
|
export interface Options {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Root directory to restrict file access. Defaults to the current working
|
|
5
|
+
* directory.
|
|
6
|
+
*/
|
|
7
|
+
root?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Base path to resolve imports from. Defaults to the current working
|
|
10
|
+
* directory.
|
|
11
|
+
*/
|
|
12
|
+
base?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Imports resolved to outside of the base path will be prefixed with this
|
|
15
|
+
* string. Defaults to `/__root__`.
|
|
16
|
+
*/
|
|
17
|
+
rootPathPrefix?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Array of file extensions to serve. Defaults to `['.js', '.mjs']`.
|
|
20
|
+
*/
|
|
21
|
+
extensions?: string[];
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Serve static JavaScript files from a `root` directory.
|
|
25
25
|
*/
|
|
26
26
|
export declare const serve: (opts: Options) => Middleware;
|
|
27
|
-
//# sourceMappingURL=serve.d.ts.map
|
|
27
|
+
//# sourceMappingURL=serve.d.ts.map
|
package/lib/serve.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../src/lib/serve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,UAAU,EAAC,MAAM,eAAe,CAAC;AAczD,MAAM,WAAW,OAAO;IACtB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAYD;;GAEG;AACH,eAAO,MAAM,KAAK,SAAU,OAAO,KAAG,
|
|
1
|
+
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../src/lib/serve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,UAAU,EAAC,MAAM,eAAe,CAAC;AAczD,MAAM,WAAW,OAAO;IACtB;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAYD;;GAEG;AACH,eAAO,MAAM,KAAK,SAAU,OAAO,KAAG,UAiGrC,CAAC"}
|
package/lib/serve.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
import {HttpError} from '@zipadee/core';
|
|
2
|
-
import {
|
|
3
|
-
decodePath,
|
|
4
|
-
pathIsHidden,
|
|
5
|
-
resolvePath,
|
|
6
|
-
stat,
|
|
7
|
-
} from '@zipadee/static/lib/utils.js';
|
|
1
|
+
import { HttpError } from '@zipadee/core';
|
|
2
|
+
import { decodePath, pathIsHidden, resolvePath, stat, } from '@zipadee/static/lib/utils.js';
|
|
8
3
|
import fs from 'node:fs/promises';
|
|
9
4
|
import path from 'node:path';
|
|
10
|
-
import {parse, init} from 'es-module-lexer';
|
|
11
|
-
import {moduleResolve} from 'import-meta-resolve';
|
|
5
|
+
import { parse, init } from 'es-module-lexer';
|
|
6
|
+
import { moduleResolve } from 'import-meta-resolve';
|
|
12
7
|
await init;
|
|
13
8
|
// TODO:
|
|
14
9
|
// - Add caching for both specifier resolution and files
|
|
@@ -23,71 +18,73 @@ await init;
|
|
|
23
18
|
* Serve static JavaScript files from a `root` directory.
|
|
24
19
|
*/
|
|
25
20
|
export const serve = (opts) => {
|
|
26
|
-
|
|
27
|
-
opts.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return await next();
|
|
35
|
-
}
|
|
36
|
-
let filePath = decodePath(req.path);
|
|
37
|
-
const parsedPath = path.parse(filePath);
|
|
38
|
-
// Only serve JavaScript files
|
|
39
|
-
if (!extensions.includes(parsedPath.ext)) {
|
|
40
|
-
return await next();
|
|
41
|
-
}
|
|
42
|
-
if (filePath.startsWith(rootFolderName)) {
|
|
43
|
-
filePath = filePath.substring(rootFolderName.length);
|
|
44
|
-
filePath = filePath.slice(parsedPath.root.length);
|
|
45
|
-
filePath = resolvePath(root, filePath);
|
|
46
|
-
} else {
|
|
47
|
-
filePath = filePath.slice(parsedPath.root.length);
|
|
48
|
-
filePath = resolvePath(base, filePath);
|
|
49
|
-
}
|
|
50
|
-
if (pathIsHidden(base, filePath)) {
|
|
51
|
-
return await next();
|
|
52
|
-
}
|
|
53
|
-
const stats = await stat(filePath);
|
|
54
|
-
if (stats.isDirectory()) {
|
|
55
|
-
return await next();
|
|
56
|
-
}
|
|
57
|
-
const source = await fs.readFile(filePath, 'utf8');
|
|
58
|
-
const [imports, _exports, _facade, _hasModuleSyntax] = parse(
|
|
59
|
-
source,
|
|
60
|
-
filePath,
|
|
61
|
-
);
|
|
62
|
-
let output = '';
|
|
63
|
-
let lastIndex = 0;
|
|
64
|
-
for (const impt of imports) {
|
|
65
|
-
const {t: type, s: start, e: end, n: unescaped} = impt;
|
|
66
|
-
if (type === 1) {
|
|
67
|
-
// Static import
|
|
68
|
-
const importSpecifier = unescaped || source.substring(start, end);
|
|
69
|
-
const fileURL = new URL(filePath, 'file://');
|
|
70
|
-
const resolvedImportURL = moduleResolve(importSpecifier, fileURL);
|
|
71
|
-
const resolvedImportPath = resolvedImportURL.pathname;
|
|
72
|
-
if (!resolvedImportPath.startsWith(root)) {
|
|
73
|
-
console.log('A', resolvedImportPath);
|
|
74
|
-
throw new HttpError(500);
|
|
21
|
+
const root = opts.root === undefined ? process.cwd() : path.resolve(opts.root);
|
|
22
|
+
const base = opts.base === undefined ? root : resolvePath(root, opts.base);
|
|
23
|
+
const rootPathPrefix = opts.rootPathPrefix ?? '/__root__';
|
|
24
|
+
const extensions = opts.extensions ?? ['.js', '.mjs'];
|
|
25
|
+
return async (req, res, next) => {
|
|
26
|
+
if (!(req.method === 'HEAD' || req.method === 'GET')) {
|
|
27
|
+
// TODO: implement HEAD?
|
|
28
|
+
return await next();
|
|
75
29
|
}
|
|
76
|
-
let
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
resolvedImportPath.substring(root.length),
|
|
83
|
-
);
|
|
30
|
+
let filePath = decodePath(req.path);
|
|
31
|
+
const mountedPath = req.url.pathname.substring(0, req.url.pathname.length - filePath.length);
|
|
32
|
+
const parsedPath = path.parse(filePath);
|
|
33
|
+
// Only serve JavaScript files
|
|
34
|
+
if (!extensions.includes(parsedPath.ext)) {
|
|
35
|
+
return await next();
|
|
84
36
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
37
|
+
if (filePath.startsWith(rootPathPrefix)) {
|
|
38
|
+
filePath = filePath.substring(rootPathPrefix.length);
|
|
39
|
+
filePath = filePath.slice(parsedPath.root.length);
|
|
40
|
+
filePath = resolvePath(root, filePath);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
filePath = filePath.slice(parsedPath.root.length);
|
|
44
|
+
filePath = resolvePath(base, filePath);
|
|
45
|
+
}
|
|
46
|
+
if (pathIsHidden(base, filePath)) {
|
|
47
|
+
return await next();
|
|
48
|
+
}
|
|
49
|
+
const stats = await stat(filePath);
|
|
50
|
+
if (stats.isDirectory()) {
|
|
51
|
+
return await next();
|
|
52
|
+
}
|
|
53
|
+
const source = await fs.readFile(filePath, 'utf8');
|
|
54
|
+
const [imports, _exports, _facade, _hasModuleSyntax] = parse(source, filePath);
|
|
55
|
+
let output = '';
|
|
56
|
+
let lastIndex = 0;
|
|
57
|
+
for (const impt of imports) {
|
|
58
|
+
const { t: type, s: start, e: end, n: unescaped } = impt;
|
|
59
|
+
if (type === 1) {
|
|
60
|
+
// Static import
|
|
61
|
+
const importSpecifier = unescaped || source.substring(start, end);
|
|
62
|
+
// If the specifier is relative or absolute, we don't need to resolve it
|
|
63
|
+
if (importSpecifier.startsWith('.') ||
|
|
64
|
+
importSpecifier.startsWith('/')) {
|
|
65
|
+
output += `${source.substring(lastIndex, start)}${importSpecifier}`;
|
|
66
|
+
lastIndex = end;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const fileURL = new URL(filePath, 'file://');
|
|
70
|
+
const resolvedImportURL = moduleResolve(importSpecifier, fileURL);
|
|
71
|
+
const resolvedImportPath = resolvedImportURL.pathname;
|
|
72
|
+
if (!resolvedImportPath.startsWith(root)) {
|
|
73
|
+
throw new HttpError(500);
|
|
74
|
+
}
|
|
75
|
+
let resolvedimport;
|
|
76
|
+
if (resolvedImportPath.startsWith(base)) {
|
|
77
|
+
resolvedimport = resolvedImportPath.substring(base.length);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
resolvedimport = path.join(mountedPath, rootPathPrefix, resolvedImportPath.substring(root.length));
|
|
81
|
+
}
|
|
82
|
+
output += `${source.substring(lastIndex, start)}${resolvedimport}`;
|
|
83
|
+
lastIndex = end;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
res.type = 'text/javascript';
|
|
87
|
+
res.body = output + source.substring(lastIndex);
|
|
88
|
+
};
|
|
92
89
|
};
|
|
93
|
-
//# sourceMappingURL=serve.js.map
|
|
90
|
+
//# sourceMappingURL=serve.js.map
|
package/lib/serve.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../src/lib/serve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAkB,MAAM,eAAe,CAAC;AACzD,OAAO,EACL,UAAU,EACV,YAAY,EACZ,WAAW,EACX,IAAI,GACL,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAC,KAAK,EAAE,IAAI,EAAC,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAElD,MAAM,IAAI,CAAC;AA2BX,QAAQ;AACR,wDAAwD;AACxD,oDAAoD;AACpD,oCAAoC;AACpC,iCAAiC;AACjC,uCAAuC;AACvC,qCAAqC;AACrC,qEAAqE;AACrE,wCAAwC;AAExC;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,IAAa,EAAc,EAAE;IACjD,MAAM,IAAI,
|
|
1
|
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C3B,CAAC;gBAED,IAAI,cAAsB,CAAC;gBAC3B,IAAI,kBAAkB,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;oBACxC,cAAc,GAAG,kBAAkB,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC7D,CAAC;qBAAM,CAAC;oBACN,cAAc,GAAG,IAAI,CAAC,IAAI,CACxB,WAAW,EACX,cAAc,EACd,kBAAkB,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAC1C,CAAC;gBACJ,CAAC;gBAED,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,cAAc,EAAE,CAAC;gBACnE,SAAS,GAAG,GAAG,CAAC;YAClB,CAAC;QACH,CAAC;QAED,GAAG,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC7B,GAAG,CAAC,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zipadee/javascript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@zipadee/core": "^0.0.
|
|
60
|
-
"@zipadee/static": "^0.0.
|
|
59
|
+
"@zipadee/core": "^0.0.7",
|
|
60
|
+
"@zipadee/static": "^0.0.7",
|
|
61
61
|
"import-meta-resolve": "^4.1.0"
|
|
62
62
|
}
|
|
63
63
|
}
|