@editframe/vite-plugin 0.16.8-beta.0 → 0.18.3-beta.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.
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
const forbidRelativePaths = (req) => {
|
|
2
|
-
|
|
3
|
-
throw new Error("Relative paths are forbidden");
|
|
4
|
-
}
|
|
5
|
-
};
|
|
6
|
-
export {
|
|
7
|
-
forbidRelativePaths
|
|
2
|
+
if (req.url?.includes("..")) throw new Error("Relative paths are forbidden");
|
|
8
3
|
};
|
|
4
|
+
export { forbidRelativePaths };
|
|
@@ -2,5 +2,8 @@ interface VitePluginEditframeOptions {
|
|
|
2
2
|
root: string;
|
|
3
3
|
cacheRoot: string;
|
|
4
4
|
}
|
|
5
|
-
export declare const vitePluginEditframe: (options: VitePluginEditframeOptions) =>
|
|
5
|
+
export declare const vitePluginEditframe: (options: VitePluginEditframeOptions) => {
|
|
6
|
+
name: string;
|
|
7
|
+
configureServer(this: void, server: import('rolldown-vite').ViteDevServer): void;
|
|
8
|
+
};
|
|
6
9
|
export {};
|
|
@@ -1,65 +1,58 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import debug from "debug";
|
|
3
|
-
import { cacheImage, findOrCreateCaptions, generateTrack, generateTrackFragmentIndex, md5FilePath } from "@editframe/assets";
|
|
4
1
|
import { forbidRelativePaths } from "./forbidRelativePaths.js";
|
|
5
2
|
import { sendTaskResult } from "./sendTaskResult.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { cacheImage, findOrCreateCaptions, generateTrack, generateTrackFragmentIndex, md5FilePath } from "@editframe/assets";
|
|
5
|
+
import debug from "debug";
|
|
6
6
|
const vitePluginEditframe = (options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
export {
|
|
64
|
-
vitePluginEditframe
|
|
7
|
+
return {
|
|
8
|
+
name: "vite-plugin-editframe",
|
|
9
|
+
configureServer(server) {
|
|
10
|
+
server.middlewares.use(async (req, res, next) => {
|
|
11
|
+
const log = debug("ef:vite-plugin");
|
|
12
|
+
if (req.url?.startsWith("/@ef")) forbidRelativePaths(req);
|
|
13
|
+
else return next();
|
|
14
|
+
log(`Handling ${req.url}`);
|
|
15
|
+
const requestPath = req.url.replace(/^\/@ef-[^/]+\//, "");
|
|
16
|
+
const assetPath = requestPath.replace(/\?.*$/, "");
|
|
17
|
+
const absolutePath = assetPath.startsWith("http") ? assetPath : path.join(options.root, assetPath).replace("dist/", "src/");
|
|
18
|
+
options.cacheRoot = options.cacheRoot.replace("dist/", "src/");
|
|
19
|
+
const efPrefix = req.url.split("/")[1];
|
|
20
|
+
switch (efPrefix) {
|
|
21
|
+
case "@ef-asset": {
|
|
22
|
+
if (req.method !== "HEAD") {
|
|
23
|
+
res.writeHead(405, { Allow: "HEAD" });
|
|
24
|
+
res.end();
|
|
25
|
+
}
|
|
26
|
+
md5FilePath(absolutePath).then((md5) => {
|
|
27
|
+
res.writeHead(200, { etag: md5 });
|
|
28
|
+
res.end();
|
|
29
|
+
}).catch(next);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case "@ef-track-fragment-index": {
|
|
33
|
+
log(`Serving track fragment index for ${absolutePath}`);
|
|
34
|
+
generateTrackFragmentIndex(options.cacheRoot, absolutePath).then((taskResult) => sendTaskResult(req, res, taskResult)).catch(next);
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case "@ef-track": {
|
|
38
|
+
log(`Serving track for ${absolutePath}`);
|
|
39
|
+
generateTrack(options.cacheRoot, absolutePath, req.url).then((taskResult) => sendTaskResult(req, res, taskResult)).catch(next);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case "@ef-captions":
|
|
43
|
+
log(`Serving captions for ${absolutePath}`);
|
|
44
|
+
findOrCreateCaptions(options.cacheRoot, absolutePath).then((taskResult) => sendTaskResult(req, res, taskResult)).catch(next);
|
|
45
|
+
break;
|
|
46
|
+
case "@ef-image":
|
|
47
|
+
log(`Serving image file ${absolutePath}`);
|
|
48
|
+
cacheImage(options.cacheRoot, absolutePath).then((taskResult) => sendTaskResult(req, res, taskResult)).catch(next);
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
log(`Unknown asset type ${efPrefix}`);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
65
57
|
};
|
|
58
|
+
export { vitePluginEditframe };
|
|
@@ -1,56 +1,53 @@
|
|
|
1
|
-
import { statSync, createReadStream } from "node:fs";
|
|
2
|
-
import mime from "mime";
|
|
3
1
|
import debug from "debug";
|
|
2
|
+
import { createReadStream, statSync } from "node:fs";
|
|
3
|
+
import mime from "mime";
|
|
4
4
|
const sendTaskResult = (req, res, taskResult) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
export {
|
|
55
|
-
sendTaskResult
|
|
5
|
+
const { cachePath, md5Sum } = taskResult;
|
|
6
|
+
const filePath = cachePath;
|
|
7
|
+
const headers = { etag: md5Sum };
|
|
8
|
+
const log = debug("ef:sendfile");
|
|
9
|
+
try {
|
|
10
|
+
log(`Sending file ${filePath}`);
|
|
11
|
+
const stats = statSync(filePath);
|
|
12
|
+
if (req.headers.range) {
|
|
13
|
+
const [x, y] = req.headers.range.replace("bytes=", "").split("-");
|
|
14
|
+
let end = Number.parseInt(y ?? "0", 10) || stats.size - 1;
|
|
15
|
+
const start = Number.parseInt(x ?? "0", 10) || 0;
|
|
16
|
+
if (end >= stats.size) end = stats.size - 1;
|
|
17
|
+
if (start >= stats.size) {
|
|
18
|
+
log("Range start is greater than file size");
|
|
19
|
+
res.setHeader("Content-Range", `bytes */${stats.size}`);
|
|
20
|
+
res.statusCode = 416;
|
|
21
|
+
return res.end();
|
|
22
|
+
}
|
|
23
|
+
res.writeHead(206, {
|
|
24
|
+
...headers,
|
|
25
|
+
"Content-Type": mime.getType(filePath) || "text/plain",
|
|
26
|
+
"Cache-Control": "max-age=3600",
|
|
27
|
+
"Content-Range": `bytes ${start}-${end}/${stats.size}`,
|
|
28
|
+
"Content-Length": end - start + 1,
|
|
29
|
+
"Accept-Ranges": "bytes"
|
|
30
|
+
});
|
|
31
|
+
log(`Sending ${filePath} range ${start}-${end}/${stats.size}`);
|
|
32
|
+
const readStream = createReadStream(filePath, {
|
|
33
|
+
start,
|
|
34
|
+
end
|
|
35
|
+
});
|
|
36
|
+
readStream.pipe(res);
|
|
37
|
+
} else {
|
|
38
|
+
res.writeHead(200, {
|
|
39
|
+
...headers,
|
|
40
|
+
"Content-Type": mime.getType(filePath) || "text/plain",
|
|
41
|
+
"Cache-Control": "max-age=3600",
|
|
42
|
+
"Content-Length": stats.size
|
|
43
|
+
});
|
|
44
|
+
log(`Sending ${filePath}`);
|
|
45
|
+
const readStream = createReadStream(filePath);
|
|
46
|
+
readStream.pipe(res);
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
log("Error sending file", error);
|
|
50
|
+
console.error(error);
|
|
51
|
+
}
|
|
56
52
|
};
|
|
53
|
+
export { sendTaskResult };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@editframe/vite-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.3-beta.0",
|
|
4
4
|
"description": "Editframe vite plugin",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -19,18 +19,18 @@
|
|
|
19
19
|
"author": "",
|
|
20
20
|
"license": "UNLICENSED",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@editframe/assets": "0.
|
|
22
|
+
"@editframe/assets": "0.18.3-beta.0",
|
|
23
|
+
"connect": "^3.7.0",
|
|
23
24
|
"debug": "^4.3.5",
|
|
24
25
|
"mime": "^4.0.3",
|
|
25
26
|
"node-html-parser": "^6.1.13",
|
|
26
|
-
"
|
|
27
|
-
"vite": "^6.3.5"
|
|
27
|
+
"rolldown-vite": "^6.3.21"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/dom-webcodecs": "^0.1.11",
|
|
31
31
|
"@types/node": "^20.14.13",
|
|
32
32
|
"typescript": "^5.5.4",
|
|
33
|
-
"vite-plugin-dts": "^4.
|
|
33
|
+
"vite-plugin-dts": "^4.5.4",
|
|
34
34
|
"vite-tsconfig-paths": "^4.3.2"
|
|
35
35
|
}
|
|
36
36
|
}
|