@vladimish/motion-canvas-2-vite-plugin 3.17.2

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 (52) hide show
  1. package/lib/index.d.ts +3 -0
  2. package/lib/index.js +23 -0
  3. package/lib/index.js.map +1 -0
  4. package/lib/main.d.ts +82 -0
  5. package/lib/main.js +43 -0
  6. package/lib/main.js.map +1 -0
  7. package/lib/openInExplorer.d.ts +1 -0
  8. package/lib/openInExplorer.js +45 -0
  9. package/lib/openInExplorer.js.map +1 -0
  10. package/lib/partials/assets.d.ts +6 -0
  11. package/lib/partials/assets.js +51 -0
  12. package/lib/partials/assets.js.map +1 -0
  13. package/lib/partials/audio.d.ts +9 -0
  14. package/lib/partials/audio.js +104 -0
  15. package/lib/partials/audio.js.map +1 -0
  16. package/lib/partials/corsProxy.d.ts +43 -0
  17. package/lib/partials/corsProxy.js +219 -0
  18. package/lib/partials/corsProxy.js.map +1 -0
  19. package/lib/partials/editor.d.ts +8 -0
  20. package/lib/partials/editor.js +76 -0
  21. package/lib/partials/editor.js.map +1 -0
  22. package/lib/partials/exporter.d.ts +6 -0
  23. package/lib/partials/exporter.js +49 -0
  24. package/lib/partials/exporter.js.map +1 -0
  25. package/lib/partials/index.d.ts +10 -0
  26. package/lib/partials/index.js +27 -0
  27. package/lib/partials/index.js.map +1 -0
  28. package/lib/partials/meta.d.ts +2 -0
  29. package/lib/partials/meta.js +67 -0
  30. package/lib/partials/meta.js.map +1 -0
  31. package/lib/partials/projects.d.ts +9 -0
  32. package/lib/partials/projects.js +89 -0
  33. package/lib/partials/projects.js.map +1 -0
  34. package/lib/partials/scenes.d.ts +2 -0
  35. package/lib/partials/scenes.js +47 -0
  36. package/lib/partials/scenes.js.map +1 -0
  37. package/lib/partials/settings.d.ts +2 -0
  38. package/lib/partials/settings.js +66 -0
  39. package/lib/partials/settings.js.map +1 -0
  40. package/lib/partials/webgl.d.ts +10 -0
  41. package/lib/partials/webgl.js +89 -0
  42. package/lib/partials/webgl.js.map +1 -0
  43. package/lib/plugins.d.ts +104 -0
  44. package/lib/plugins.js +9 -0
  45. package/lib/plugins.js.map +1 -0
  46. package/lib/utils.d.ts +3 -0
  47. package/lib/utils.js +55 -0
  48. package/lib/utils.js.map +1 -0
  49. package/lib/versions.d.ts +6 -0
  50. package/lib/versions.js +29 -0
  51. package/lib/versions.js.map +1 -0
  52. package/package.json +35 -0
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import motionCanvas from './main';
2
+ export default motionCanvas;
3
+ export * from './plugins';
package/lib/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ const main_1 = __importDefault(require("./main"));
21
+ exports.default = main_1.default;
22
+ __exportStar(require("./plugins"), exports);
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,kDAAkC;AAElC,kBAAe,cAAY,CAAC;AAC5B,4CAA0B"}
package/lib/main.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ import type { Plugin } from 'vite';
2
+ import { CorsProxyPluginConfig } from './partials';
3
+ export interface MotionCanvasPluginConfig {
4
+ /**
5
+ * The import path of the project file or an array of paths.
6
+ * Also supports globs.
7
+ *
8
+ * @remarks
9
+ * Each file must contain a default export exposing an instance of the
10
+ * {@link Project} class.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * motionCanvas({
15
+ * project: [
16
+ * './src/firstProject.ts',
17
+ * './src/secondProject.ts',
18
+ * ]
19
+ * })
20
+ * ```
21
+ *
22
+ * @defaultValue './src/project.ts'
23
+ */
24
+ project?: string | string[];
25
+ /**
26
+ * A directory path to which the animation will be rendered.
27
+ *
28
+ * @defaultValue './output'
29
+ */
30
+ output?: string;
31
+ /**
32
+ * Defines which assets should be buffered before being sent to the browser.
33
+ *
34
+ * @remarks
35
+ * Streaming larger assets directly from the drive may cause issues with other
36
+ * applications. For instance, if an audio file is being used in the project,
37
+ * Adobe Audition will perceive it as "being used by another application"
38
+ * and refuse to override it.
39
+ *
40
+ * Buffered assets are first loaded to the memory and then streamed from
41
+ * there. This leaves the original files open for modification with hot module
42
+ * replacement still working.
43
+ *
44
+ * @defaultValue /^$/
45
+ */
46
+ bufferedAssets?: RegExp | false;
47
+ /**
48
+ * The import path of the editor package.
49
+ *
50
+ * @remarks
51
+ * This path will be resolved using Node.js module resolution rules.
52
+ * It should lead to a directory containing the following files:
53
+ * - `editor.html` - The HTML template for the editor.
54
+ * - `styles.css` - The editor styles.
55
+ * - `main.js` - A module exporting necessary factory functions.
56
+ *
57
+ * `main.js` should export the following functions:
58
+ * - `editor` - Receives the project factory as its first argument and creates
59
+ * the user interface.
60
+ * - `index` - Receives a list of all projects as its first argument and
61
+ * creates the initial page for selecting a project.
62
+ *
63
+ * @defaultValue '\@vladimish/motion-canvas-2-ui'
64
+ */
65
+ editor?: string;
66
+ /**
67
+ * Configuration of the Proxy used for remote sources
68
+ *
69
+ * @remarks
70
+ * This passes configuration to Motion Canvas' proxy.
71
+ * Note that the proxy is disabled by default.
72
+ * You can either pass `true` and a config object
73
+ * to enable it.
74
+ **/
75
+ proxy?: boolean | CorsProxyPluginConfig;
76
+ /**
77
+ * Build the project to run in the editor.
78
+ */
79
+ buildForEditor?: boolean;
80
+ }
81
+ declare const _default: ({ project, output, bufferedAssets, editor, proxy, buildForEditor, }?: MotionCanvasPluginConfig) => Plugin[];
82
+ export default _default;
package/lib/main.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const partials_1 = require("./partials");
8
+ const plugins_1 = require("./plugins");
9
+ const utils_1 = require("./utils");
10
+ exports.default = ({ project = './src/project.ts', output = './output', bufferedAssets = /^$/, editor = '@vladimish/motion-canvas-2-ui', proxy, buildForEditor, } = {}) => {
11
+ const plugins = [];
12
+ let config = {
13
+ output: path_1.default.resolve(output),
14
+ projects: (0, utils_1.getProjects)(project),
15
+ };
16
+ return [
17
+ {
18
+ name: 'motion-canvas',
19
+ async configResolved(resolvedConfig) {
20
+ plugins.push(...resolvedConfig.plugins
21
+ .filter(plugins_1.isPlugin)
22
+ .map(plugin => plugin[plugins_1.PLUGIN_OPTIONS]));
23
+ for (const plugin of plugins) {
24
+ const newConfig = await plugin.config?.(config);
25
+ if (newConfig) {
26
+ config = { ...config, ...newConfig };
27
+ }
28
+ }
29
+ },
30
+ },
31
+ (0, partials_1.metaPlugin)(),
32
+ (0, partials_1.settingsPlugin)(),
33
+ (0, partials_1.scenesPlugin)(),
34
+ (0, partials_1.exporterPlugin)({ outputPath: config.output }),
35
+ (0, partials_1.audioPlugin)(),
36
+ (0, partials_1.editorPlugin)({ editor, projects: config.projects }),
37
+ (0, partials_1.projectsPlugin)({ projects: config.projects, plugins, buildForEditor }),
38
+ (0, partials_1.assetsPlugin)({ bufferedAssets }),
39
+ (0, partials_1.webglPlugin)(),
40
+ (0, partials_1.corsProxyPlugin)(proxy),
41
+ ];
42
+ };
43
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;AAAA,gDAAwB;AAExB,yCAYoB;AACpB,uCAAgF;AAChF,mCAAoC;AAkFpC,kBAAe,CAAC,EACd,OAAO,GAAG,kBAAkB,EAC5B,MAAM,GAAG,UAAU,EACnB,cAAc,GAAG,IAAI,EACrB,MAAM,GAAG,+BAA+B,EACxC,KAAK,EACL,cAAc,MACc,EAAE,EAAY,EAAE;IAC5C,MAAM,OAAO,GAAoB,EAAE,CAAC;IACpC,IAAI,MAAM,GAAiB;QACzB,MAAM,EAAE,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC;QAC5B,QAAQ,EAAE,IAAA,mBAAW,EAAC,OAAO,CAAC;KAC/B,CAAC;IAEF,OAAO;QACL;YACE,IAAI,EAAE,eAAe;YACrB,KAAK,CAAC,cAAc,CAAC,cAAc;gBACjC,OAAO,CAAC,IAAI,CACV,GAAG,cAAc,CAAC,OAAO;qBACtB,MAAM,CAAC,kBAAQ,CAAC;qBAChB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,wBAAc,CAAC,CAAC,CACzC,CAAC;gBAEF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;oBAC7B,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;oBAChD,IAAI,SAAS,EAAE,CAAC;wBACd,MAAM,GAAG,EAAC,GAAG,MAAM,EAAE,GAAG,SAAS,EAAC,CAAC;oBACrC,CAAC;gBACH,CAAC;YACH,CAAC;SACF;QACD,IAAA,qBAAU,GAAE;QACZ,IAAA,yBAAc,GAAE;QAChB,IAAA,uBAAY,GAAE;QACd,IAAA,yBAAc,EAAC,EAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAC,CAAC;QAC3C,IAAA,sBAAW,GAAE;QACb,IAAA,uBAAY,EAAC,EAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAC,CAAC;QACjD,IAAA,yBAAc,EAAC,EAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,cAAc,EAAC,CAAC;QACpE,IAAA,uBAAY,EAAC,EAAC,cAAc,EAAC,CAAC;QAC9B,IAAA,sBAAW,GAAE;QACb,IAAA,0BAAe,EAAC,KAAK,CAAC;KACvB,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function openInExplorer(file: string): void;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openInExplorer = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const os_1 = require("os");
6
+ function openInExplorer(file) {
7
+ let command = null;
8
+ let args = [file];
9
+ const os = (0, os_1.platform)();
10
+ switch (os) {
11
+ case 'win32':
12
+ command = 'explorer';
13
+ break;
14
+ case 'linux':
15
+ if (isRunningOnWSL()) {
16
+ command = 'bash';
17
+ args = ['-c', `cd ${file} && explorer.exe .`];
18
+ }
19
+ else {
20
+ command = 'xdg-open';
21
+ }
22
+ break;
23
+ case 'darwin':
24
+ command = 'open';
25
+ break;
26
+ }
27
+ if (command) {
28
+ (0, child_process_1.spawn)(command, args, { detached: true }).unref();
29
+ }
30
+ else {
31
+ console.warn(`Unsupported OS: ${os}`);
32
+ }
33
+ }
34
+ exports.openInExplorer = openInExplorer;
35
+ function isRunningOnWSL() {
36
+ try {
37
+ const uname = (0, child_process_1.execSync)('uname -a').toString().toLowerCase();
38
+ return uname.includes('microsoft');
39
+ }
40
+ catch (error) {
41
+ console.error(`exec error: ${error}`);
42
+ return false;
43
+ }
44
+ }
45
+ //# sourceMappingURL=openInExplorer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openInExplorer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H,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Plugin } from 'vite';
2
+ interface AssetsPluginConfig {
3
+ bufferedAssets: RegExp | false;
4
+ }
5
+ export declare function assetsPlugin({ bufferedAssets }: AssetsPluginConfig): Plugin;
6
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.assetsPlugin = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const stream_1 = require("stream");
10
+ const AUDIO_EXTENSION_REGEX = /\.(mp3|wav|ogg|aac|flac|webm|m4a)(?:$|\?)/;
11
+ const AUDIO_HMR_DELAY = 1000;
12
+ function assetsPlugin({ bufferedAssets }) {
13
+ let config;
14
+ return {
15
+ name: 'motion-canvas:assets',
16
+ configResolved(resolvedConfig) {
17
+ config = resolvedConfig;
18
+ },
19
+ configureServer(server) {
20
+ server.middlewares.use((req, res, next) => {
21
+ if (req.url && bufferedAssets && bufferedAssets.test(req.url)) {
22
+ const file = fs_1.default.readFileSync(path_1.default.resolve(config.root, req.url.slice(1)));
23
+ stream_1.Readable.from(file).pipe(res);
24
+ return;
25
+ }
26
+ next();
27
+ });
28
+ },
29
+ async handleHotUpdate(ctx) {
30
+ const urls = [];
31
+ const modules = [];
32
+ for (const module of ctx.modules) {
33
+ urls.push(module.url);
34
+ if (!AUDIO_EXTENSION_REGEX.test(module.url)) {
35
+ modules.push(module);
36
+ }
37
+ else {
38
+ await new Promise(resolve => {
39
+ setTimeout(resolve, AUDIO_HMR_DELAY);
40
+ });
41
+ }
42
+ }
43
+ if (urls.length > 0) {
44
+ ctx.server.ws.send('motion-canvas:assets', { urls });
45
+ }
46
+ return modules;
47
+ },
48
+ };
49
+ }
50
+ exports.assetsPlugin = assetsPlugin;
51
+ //# sourceMappingURL=assets.js.map
@@ -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,GAAG,CAAC,EAAE,CAAC;oBAC5C,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBACvB,CAAC;qBAAM,CAAC;oBACN,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;wBAC1B,UAAU,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;oBACvC,CAAC,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACpB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,sBAAsB,EAAE,EAAC,IAAI,EAAC,CAAC,CAAC;YACrD,CAAC;YAED,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC;AA7CD,oCA6CC"}
@@ -0,0 +1,9 @@
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * Handles audio recorded or imported from within the editor.
4
+ *
5
+ * @remarks
6
+ * Audio blobs sent over the `motion-canvas:save-audio` event are written to an
7
+ * `audio` directory next to the project and served back under `/audio`.
8
+ */
9
+ export declare function audioPlugin(): Plugin;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.audioPlugin = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const mime_types_1 = __importDefault(require("mime-types"));
9
+ const path_1 = __importDefault(require("path"));
10
+ /**
11
+ * Maps the MIME types produced by `MediaRecorder` and common file inputs to the
12
+ * file extensions Motion Canvas understands.
13
+ */
14
+ const AUDIO_EXTENSIONS = new Map([
15
+ ['audio/webm', 'webm'],
16
+ ['audio/ogg', 'ogg'],
17
+ ['audio/wav', 'wav'],
18
+ ['audio/wave', 'wav'],
19
+ ['audio/x-wav', 'wav'],
20
+ ['audio/mpeg', 'mp3'],
21
+ ['audio/mp3', 'mp3'],
22
+ ['audio/mp4', 'm4a'],
23
+ ['audio/aac', 'm4a'],
24
+ ['audio/x-m4a', 'm4a'],
25
+ ['audio/flac', 'flac'],
26
+ ]);
27
+ /**
28
+ * Handles audio recorded or imported from within the editor.
29
+ *
30
+ * @remarks
31
+ * Audio blobs sent over the `motion-canvas:save-audio` event are written to an
32
+ * `audio` directory next to the project and served back under `/audio`.
33
+ */
34
+ function audioPlugin() {
35
+ let audioDir;
36
+ return {
37
+ name: 'motion-canvas:audio',
38
+ configResolved(resolvedConfig) {
39
+ audioDir = path_1.default.resolve(resolvedConfig.root, 'audio');
40
+ },
41
+ configureServer(server) {
42
+ // Serve previously saved audio files. A dedicated handler with range
43
+ // support keeps audio seeking responsive in the preview.
44
+ server.middlewares.use((req, res, next) => {
45
+ if (!req.url?.startsWith('/audio/')) {
46
+ next();
47
+ return;
48
+ }
49
+ const relative = decodeURIComponent(req.url.slice('/audio/'.length).split('?')[0]);
50
+ const filePath = path_1.default.join(audioDir, relative);
51
+ if (!filePath.startsWith(audioDir) || !fs_1.default.existsSync(filePath)) {
52
+ next();
53
+ return;
54
+ }
55
+ const stat = fs_1.default.statSync(filePath);
56
+ res.setHeader('Content-Type', mime_types_1.default.lookup(filePath) || 'application/octet-stream');
57
+ res.setHeader('Accept-Ranges', 'bytes');
58
+ const range = req.headers.range;
59
+ const match = range && /bytes=(\d*)-(\d*)/.exec(range);
60
+ if (match) {
61
+ const start = match[1] ? parseInt(match[1], 10) : 0;
62
+ const end = match[2] ? parseInt(match[2], 10) : stat.size - 1;
63
+ res.statusCode = 206;
64
+ res.setHeader('Content-Range', `bytes ${start}-${end}/${stat.size}`);
65
+ res.setHeader('Content-Length', end - start + 1);
66
+ fs_1.default.createReadStream(filePath, { start, end }).pipe(res);
67
+ }
68
+ else {
69
+ res.setHeader('Content-Length', stat.size);
70
+ fs_1.default.createReadStream(filePath).pipe(res);
71
+ }
72
+ });
73
+ server.ws.on('motion-canvas:save-audio', async ({ id, data, mimeType, name }, client) => {
74
+ const baseMime = (mimeType ?? '').split(';')[0].trim().toLowerCase();
75
+ const extension = AUDIO_EXTENSIONS.get(baseMime) ||
76
+ mime_types_1.default.extension(baseMime) ||
77
+ 'webm';
78
+ const safeName = path_1.default
79
+ .basename(`${name ?? 'audio'}`)
80
+ .replace(/[^a-zA-Z0-9-_]/g, '-') || 'audio';
81
+ const fileName = `${safeName}.${extension}`;
82
+ if (!fs_1.default.existsSync(audioDir)) {
83
+ fs_1.default.mkdirSync(audioDir, { recursive: true });
84
+ }
85
+ const base64Data = data.slice(data.indexOf(',') + 1);
86
+ await writeBase64(path_1.default.join(audioDir, fileName), base64Data);
87
+ client.send('motion-canvas:save-audio-ack', {
88
+ id,
89
+ url: `/audio/${fileName}`,
90
+ });
91
+ });
92
+ },
93
+ };
94
+ }
95
+ exports.audioPlugin = audioPlugin;
96
+ function writeBase64(filePath, base64) {
97
+ return new Promise((resolve, reject) => {
98
+ fs_1.default.createWriteStream(filePath)
99
+ .on('finish', resolve)
100
+ .on('error', reject)
101
+ .end(Buffer.from(base64, 'base64'));
102
+ });
103
+ }
104
+ //# sourceMappingURL=audio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audio.js","sourceRoot":"","sources":["../../src/partials/audio.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,4DAA8B;AAC9B,gDAAwB;AAGxB;;;GAGG;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAiB;IAC/C,CAAC,YAAY,EAAE,MAAM,CAAC;IACtB,CAAC,WAAW,EAAE,KAAK,CAAC;IACpB,CAAC,WAAW,EAAE,KAAK,CAAC;IACpB,CAAC,YAAY,EAAE,KAAK,CAAC;IACrB,CAAC,aAAa,EAAE,KAAK,CAAC;IACtB,CAAC,YAAY,EAAE,KAAK,CAAC;IACrB,CAAC,WAAW,EAAE,KAAK,CAAC;IACpB,CAAC,WAAW,EAAE,KAAK,CAAC;IACpB,CAAC,WAAW,EAAE,KAAK,CAAC;IACpB,CAAC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AAQ,EAAE,OAAO,CAAC;aACrB,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC;aACnB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * Configuration used by the Proxy plugin
4
+ */
5
+ export interface CorsProxyPluginConfig {
6
+ /**
7
+ * Set which types of resources are allowed by default.
8
+ *
9
+ * @remarks
10
+ * Catchall on the right side is supported.
11
+ * Pass an empty Array to allow all types of resources, although this is not
12
+ * recommended.
13
+ *
14
+ * @defaultValue ["image/*", "video/*"]
15
+ */
16
+ allowedMimeTypes?: string[];
17
+ /**
18
+ * Set which hosts are allowed
19
+ *
20
+ * @remarks
21
+ * Note that the host is everything to the left of the first `/`, and to the
22
+ * right of the protocol `https://`. AllowList is not used by default,
23
+ * although you should consider setting up just the relevant hosts.
24
+ */
25
+ allowListHosts?: string[];
26
+ }
27
+ /**
28
+ * This module provides the proxy located at
29
+ * /cors-proxy/...
30
+ *
31
+ * It is needed when accessing remote resources.
32
+ * Trying to access remote resources works while
33
+ * in preview, but will fail when you try to
34
+ * output the image (= "read" the canvas)
35
+ *
36
+ * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
37
+ * for reasons
38
+ *
39
+ * Using the proxy circumvents CORS-issues because
40
+ * this way all remote resources are served from the
41
+ * same host as the main app.
42
+ */
43
+ export declare function corsProxyPlugin(config?: CorsProxyPluginConfig | boolean): Plugin;
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.corsProxyPlugin = void 0;
7
+ const follow_redirects_1 = __importDefault(require("follow-redirects"));
8
+ /**
9
+ * This module provides the proxy located at
10
+ * /cors-proxy/...
11
+ *
12
+ * It is needed when accessing remote resources.
13
+ * Trying to access remote resources works while
14
+ * in preview, but will fail when you try to
15
+ * output the image (= "read" the canvas)
16
+ *
17
+ * See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
18
+ * for reasons
19
+ *
20
+ * Using the proxy circumvents CORS-issues because
21
+ * this way all remote resources are served from the
22
+ * same host as the main app.
23
+ */
24
+ function corsProxyPlugin(config) {
25
+ setupEnvVarsForProxy(config);
26
+ return {
27
+ name: 'motion-canvas:cors-proxy',
28
+ configureServer(server) {
29
+ if (config !== false && config !== undefined) {
30
+ motionCanvasCorsProxy(server.middlewares, config === true ? {} : config);
31
+ }
32
+ },
33
+ };
34
+ }
35
+ exports.corsProxyPlugin = corsProxyPlugin;
36
+ function setupEnvVarsForProxy(config) {
37
+ // Define Keys for Env Var
38
+ const prefix = 'VITE_MC_PROXY_';
39
+ const isEnabledKey = prefix + 'ENABLED';
40
+ const allowList = prefix + 'ALLOW_LIST';
41
+ if (config === true) {
42
+ config = {}; // Use Default values
43
+ }
44
+ process.env[isEnabledKey] = String(!!config); // 'true' or 'false'
45
+ if (config) {
46
+ // These values are only configured if the Proxy is enabled
47
+ // You cannot access them via import.meta.env if the Proxy
48
+ // is set to false
49
+ process.env[allowList] = JSON.stringify(config.allowListHosts ?? []);
50
+ }
51
+ }
52
+ function motionCanvasCorsProxy(middleware, config) {
53
+ // Set the default config if no config was provided
54
+ config.allowedMimeTypes ??= ['image/*', 'video/*'];
55
+ config.allowListHosts ??= [];
56
+ // Check the Mime Types to have a correct structure (left/right)
57
+ // not having them in the correct format would crash the Middleware
58
+ // further down below
59
+ if ((config.allowedMimeTypes ?? []).some(e => e.split('/').length !== 2)) {
60
+ throw new Error("Invalid config for Proxy:\nAll Entries must have the following format:\n 'left/right' where left may be '*'");
61
+ }
62
+ middleware.use((req, res, next) => {
63
+ if (!req.url || !req.url.startsWith('/cors-proxy/')) {
64
+ // url does not start with /cors-proxy/, so this
65
+ // middleware does not care about it
66
+ return next();
67
+ }
68
+ // For now, only allow GET Requests
69
+ if (req.method !== 'GET') {
70
+ return writeError('Only GET Requests are allowed', res, 405);
71
+ }
72
+ let sourceUrl;
73
+ try {
74
+ sourceUrl = extractDestination(req.url);
75
+ }
76
+ catch (err) {
77
+ return writeError(err, res);
78
+ }
79
+ if (!isReceivedUrlInAllowedHosts(sourceUrl.hostname, config.allowListHosts)) {
80
+ return writeError(`Blocked by Proxy: ${sourceUrl.hostname} is not on Hosts Allowlist`, res);
81
+ }
82
+ // Get the resource, do some checks. Throws an Error
83
+ // if the checks fail. The catch then writes an Error
84
+ return tryGetResource(res, sourceUrl, config).catch(error => {
85
+ writeError(error, res);
86
+ });
87
+ });
88
+ }
89
+ /**
90
+ * Unwrap the destination from the URL.
91
+ *
92
+ * @remarks
93
+ * Throws an Error if the value could not be unwrapped.
94
+ *
95
+ * @param url - the entire URL with the `/cors-proxy/` prefix and containing the
96
+ * url-Encoded Path.
97
+ *
98
+ * @returns The URL that needs to be called.
99
+ */
100
+ function extractDestination(url) {
101
+ const withoutPrefix = url.replace('/cors-proxy/', '');
102
+ const asUrl = new URL(decodeURIComponent(withoutPrefix));
103
+ if (asUrl.protocol !== 'http:' && asUrl.protocol !== 'https:') {
104
+ throw new Error('Only supported protocols are http and https');
105
+ }
106
+ return asUrl;
107
+ }
108
+ /**
109
+ * A simple Error Helper that will write an Error and close the response.
110
+ */
111
+ function writeError(message, res, statusCode = 400) {
112
+ res.writeHead(statusCode, message);
113
+ res.end();
114
+ }
115
+ /**
116
+ * Check if the Proxy is allowed to get the requested resource based on the
117
+ * host.
118
+ */
119
+ function isReceivedUrlInAllowedHosts(hostname, allowListHosts) {
120
+ if (!allowListHosts || allowListHosts.length === 0) {
121
+ // if the allowListHosts is just the predefinedAllowlist, the user has not
122
+ // set any additional hosts. In this case, allow any hostname
123
+ return true;
124
+ }
125
+ // Check if the hostname is any of the values set in allowListHosts
126
+ return allowListHosts.some(e => e.toLowerCase().trim() === hostname.toLowerCase().trim());
127
+ }
128
+ /**
129
+ * Check if the Proxy is allowed to get the requested resource based on the
130
+ * MIME-Type.
131
+ *
132
+ * @remarks
133
+ * Also handles catch-All Declarations like `image/*`.
134
+ */
135
+ function isResultOfAllowedResourceType(foundMimeType, allowedMimeTypes) {
136
+ if (!allowedMimeTypes || allowedMimeTypes.length === 0) {
137
+ return true; // no filters set
138
+ }
139
+ if (foundMimeType.split('/').length !== 2) {
140
+ return false; // invalid mime structure
141
+ }
142
+ const [leftSegment, rightSegment] = foundMimeType
143
+ .split('/')
144
+ .map(e => e.trim().toLowerCase());
145
+ // Get all Segments where the left Part is identical between foundMimeType and
146
+ // allowedMimeType.
147
+ const leftSegmentMatches = allowedMimeTypes.filter(e => e.trim().toLowerCase().split('/')[0] === leftSegment);
148
+ if (leftSegmentMatches.length === 0) {
149
+ // No matches at all, not even catchall - resource is rejected.
150
+ return false;
151
+ }
152
+ // This just gets the right part of the MIME Types from the
153
+ // configured allowList, e.g. "image/png" -> png
154
+ const rightSegmentOfLeftSegmentMatches = leftSegmentMatches.map(e => e.split('/')[1]);
155
+ // if an exact match or a catchall is found, the resource is allowed to be
156
+ // proxied.
157
+ return rightSegmentOfLeftSegmentMatches.some(e => e === '*' || e === rightSegment);
158
+ }
159
+ /**
160
+ * Requests a remote resource with the help of axios
161
+ * May throw a string in case of a bad mime-type or missing headers
162
+ */
163
+ async function tryGetResource(res, sourceUrl, config) {
164
+ // Turn this callback into a Promise to avoid additional nesting
165
+ const result = await new Promise((res, rej) => {
166
+ try {
167
+ // We check what protocol was used and decide if we use http or https
168
+ const request = (sourceUrl.protocol.startsWith('https')
169
+ ? follow_redirects_1.default.https
170
+ : follow_redirects_1.default.http).get(sourceUrl, data => {
171
+ res(data);
172
+ });
173
+ request.on('error', (err) => {
174
+ if (err.code && err.code === 'ENOTFOUND') {
175
+ // This is a bit hacky, but this basically returns as a
176
+ // 404 instead of crashing the Node Server with ENOTFOUND
177
+ res({ statusCode: 404 });
178
+ }
179
+ else {
180
+ rej(err);
181
+ }
182
+ });
183
+ }
184
+ catch (err) {
185
+ rej(err);
186
+ }
187
+ });
188
+ if (!result.statusCode || result.statusCode >= 300) {
189
+ throw 'Unexpected Status: ' + (result.statusCode ?? 'NO_STATUS');
190
+ }
191
+ const contentType = result.headers['content-type'];
192
+ const contentLength = result.headers['content-length'];
193
+ if (!contentType) {
194
+ throw 'Proxied Response does not contain a Content Type';
195
+ }
196
+ if (!contentLength) {
197
+ throw 'Proxied Response does not contain a Content Length';
198
+ }
199
+ if (!isResultOfAllowedResourceType(contentType.toString(), config.allowedMimeTypes ?? [])) {
200
+ throw 'Proxied response has blocked content-type: ' + contentType;
201
+ }
202
+ // Prepare Response
203
+ for (const key in result.headers) {
204
+ const header = result.headers[key];
205
+ if (header === undefined) {
206
+ console.warn('Proxy: Received Header is empty. Skipping…');
207
+ continue;
208
+ }
209
+ res.setHeader(key, header);
210
+ }
211
+ res.addListener('error', () => {
212
+ console.log('Proxy: Connection Reset');
213
+ });
214
+ res.setHeader('x-proxy-destination', sourceUrl.toString());
215
+ // Don't store on the server, just immediately pass on the
216
+ // received chunks
217
+ result.pipe(res);
218
+ }
219
+ //# sourceMappingURL=corsProxy.js.map