@blocklet/pages-kit-block-studio 0.0.12 → 0.0.13

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.
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.libDir = exports.logger = void 0;
6
+ exports.isDev = exports.isPathSafe = exports.libDir = exports.logger = void 0;
7
7
  exports.setBlockEntryFilesPattern = setBlockEntryFilesPattern;
8
8
  exports.getBlockEntryFilesPattern = getBlockEntryFilesPattern;
9
9
  exports.findComponentFiles = findComponentFiles;
@@ -54,3 +54,10 @@ function getBlockName(entry) {
54
54
  exports.logger = console;
55
55
  function generateBlockYml() { }
56
56
  exports.libDir = 'lib';
57
+ const isPathSafe = (filePath) => {
58
+ const normalizedPath = path_1.default.normalize(filePath);
59
+ const pwd = process.env.PWD || process.cwd();
60
+ return (!normalizedPath.includes('../') && !normalizedPath.includes('..\\') && path_1.default.resolve(normalizedPath).startsWith(pwd));
61
+ };
62
+ exports.isPathSafe = isPathSafe;
63
+ exports.isDev = process.env.BLOCKLET_MODE === 'development';
@@ -0,0 +1,75 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Router } from 'express';
11
+ import fs from 'fs';
12
+ import path from 'path';
13
+ import { isPathSafe, isDev } from '../utils/helper';
14
+ export const initBlockStudioRouter = Router();
15
+ const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.svg'];
16
+ initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
17
+ if (!isDev) {
18
+ return res.status(403).json({ error: 'Only available in development mode' });
19
+ }
20
+ const filePath = req.query.path;
21
+ if (!filePath) {
22
+ return res.status(400).json({ error: 'Path parameter is required' });
23
+ }
24
+ if (!isPathSafe(filePath)) {
25
+ return res.status(403).json({ error: 'Invalid path' });
26
+ }
27
+ try {
28
+ if (!fs.existsSync(filePath)) {
29
+ return res.json(null);
30
+ }
31
+ const ext = path.extname(filePath).toLowerCase();
32
+ if (BINARY_EXTENSIONS.includes(ext)) {
33
+ // For images, stream the file directly
34
+ const mimeType = `image/${ext.slice(1)}`;
35
+ res.setHeader('Content-Type', mimeType);
36
+ return fs.createReadStream(filePath).pipe(res);
37
+ }
38
+ // For text files, try to parse as JSON
39
+ const content = fs.readFileSync(filePath, 'utf-8');
40
+ try {
41
+ return res.json(JSON.parse(content));
42
+ }
43
+ catch (_a) {
44
+ // If JSON parsing fails, return as plain text
45
+ return res.json(content);
46
+ }
47
+ }
48
+ catch (error) {
49
+ return res.status(500).json({ error: 'Failed to read file' });
50
+ }
51
+ }));
52
+ initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
53
+ if (!isDev) {
54
+ return res.status(403).json({ error: 'Only available in development mode' });
55
+ }
56
+ const { path: filePath, content } = req.body;
57
+ if (!filePath || content === undefined) {
58
+ return res.status(400).json({ error: 'Path and content are required' });
59
+ }
60
+ if (!isPathSafe(filePath)) {
61
+ return res.status(403).json({ error: 'Invalid path' });
62
+ }
63
+ try {
64
+ const dir = path.dirname(filePath);
65
+ if (!fs.existsSync(dir)) {
66
+ fs.mkdirSync(dir, { recursive: true });
67
+ }
68
+ fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
69
+ return res.json({ success: true });
70
+ }
71
+ catch (error) {
72
+ return res.status(500).json({ error: 'Failed to write file' });
73
+ }
74
+ }));
75
+ export default initBlockStudioRouter;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ // init uploader router