Skip to content

Commit 2a53a2b

Browse files
Merge pull request #245 from Tcode-Motion/refactor-compress-config-11081667075911626730
🧹 [Refactor zip compression configuration]
2 parents 8721b36 + dadd3d4 commit 2a53a2b

2 files changed

Lines changed: 163 additions & 134 deletions

File tree

‎stdlib/src/compress.rs‎

Lines changed: 157 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::Path;
66
use std::rc::Rc;
77
use techscript_runtime::{
88
context::Capability, error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue,
9+
RuntimeContext,
910
};
1011

1112
impl StdlibRegistry {
@@ -18,28 +19,7 @@ impl StdlibRegistry {
1819
Rc::new(StdFunction {
1920
name: "zip".to_string(),
2021
arity: 2,
21-
callback: |ctx, args| {
22-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
23-
return Err(RuntimeError::new(
24-
RuntimeErrorKind::InvalidOperation(
25-
"Security policy violation: FileSystem capability is denied"
26-
.to_string(),
27-
),
28-
None,
29-
None,
30-
));
31-
}
32-
let src_dir = args[0].try_into_string()?;
33-
let archive_path = args[1].try_into_string()?;
34-
zip_dir(&src_dir, &archive_path).map_err(|e| {
35-
RuntimeError::new(
36-
RuntimeErrorKind::InvalidOperation(format!("ZIP error: {}", e)),
37-
None,
38-
None,
39-
)
40-
})?;
41-
Ok(RuntimeValue::Null)
42-
},
22+
callback: compress_zip,
4323
}),
4424
);
4525

@@ -48,28 +28,7 @@ impl StdlibRegistry {
4828
Rc::new(StdFunction {
4929
name: "unzip".to_string(),
5030
arity: 2,
51-
callback: |ctx, args| {
52-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
53-
return Err(RuntimeError::new(
54-
RuntimeErrorKind::InvalidOperation(
55-
"Security policy violation: FileSystem capability is denied"
56-
.to_string(),
57-
),
58-
None,
59-
None,
60-
));
61-
}
62-
let archive_path = args[0].try_into_string()?;
63-
let dest_dir = args[1].try_into_string()?;
64-
unzip_archive(&archive_path, &dest_dir).map_err(|e| {
65-
RuntimeError::new(
66-
RuntimeErrorKind::InvalidOperation(format!("UNZIP error: {}", e)),
67-
None,
68-
None,
69-
)
70-
})?;
71-
Ok(RuntimeValue::Null)
72-
},
31+
callback: compress_unzip,
7332
}),
7433
);
7534

@@ -78,28 +37,7 @@ impl StdlibRegistry {
7837
Rc::new(StdFunction {
7938
name: "tar".to_string(),
8039
arity: 2,
81-
callback: |ctx, args| {
82-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
83-
return Err(RuntimeError::new(
84-
RuntimeErrorKind::InvalidOperation(
85-
"Security policy violation: FileSystem capability is denied"
86-
.to_string(),
87-
),
88-
None,
89-
None,
90-
));
91-
}
92-
let src_dir = args[0].try_into_string()?;
93-
let archive_path = args[1].try_into_string()?;
94-
tar_dir(&src_dir, &archive_path).map_err(|e| {
95-
RuntimeError::new(
96-
RuntimeErrorKind::InvalidOperation(format!("TAR error: {}", e)),
97-
None,
98-
None,
99-
)
100-
})?;
101-
Ok(RuntimeValue::Null)
102-
},
40+
callback: compress_tar,
10341
}),
10442
);
10543

@@ -108,28 +46,7 @@ impl StdlibRegistry {
10846
Rc::new(StdFunction {
10947
name: "untar".to_string(),
11048
arity: 2,
111-
callback: |ctx, args| {
112-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
113-
return Err(RuntimeError::new(
114-
RuntimeErrorKind::InvalidOperation(
115-
"Security policy violation: FileSystem capability is denied"
116-
.to_string(),
117-
),
118-
None,
119-
None,
120-
));
121-
}
122-
let archive_path = args[0].try_into_string()?;
123-
let dest_dir = args[1].try_into_string()?;
124-
untar_archive(&archive_path, &dest_dir).map_err(|e| {
125-
RuntimeError::new(
126-
RuntimeErrorKind::InvalidOperation(format!("UNTAR error: {}", e)),
127-
None,
128-
None,
129-
)
130-
})?;
131-
Ok(RuntimeValue::Null)
132-
},
49+
callback: compress_untar,
13350
}),
13451
);
13552

@@ -138,28 +55,7 @@ impl StdlibRegistry {
13855
Rc::new(StdFunction {
13956
name: "gzip".to_string(),
14057
arity: 2,
141-
callback: |ctx, args| {
142-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
143-
return Err(RuntimeError::new(
144-
RuntimeErrorKind::InvalidOperation(
145-
"Security policy violation: FileSystem capability is denied"
146-
.to_string(),
147-
),
148-
None,
149-
None,
150-
));
151-
}
152-
let src_file = args[0].try_into_string()?;
153-
let archive_path = args[1].try_into_string()?;
154-
gzip_file(&src_file, &archive_path).map_err(|e| {
155-
RuntimeError::new(
156-
RuntimeErrorKind::InvalidOperation(format!("GZIP error: {}", e)),
157-
None,
158-
None,
159-
)
160-
})?;
161-
Ok(RuntimeValue::Null)
162-
},
58+
callback: compress_gzip,
16359
}),
16460
);
16561

@@ -168,28 +64,7 @@ impl StdlibRegistry {
16864
Rc::new(StdFunction {
16965
name: "gunzip".to_string(),
17066
arity: 2,
171-
callback: |ctx, args| {
172-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
173-
return Err(RuntimeError::new(
174-
RuntimeErrorKind::InvalidOperation(
175-
"Security policy violation: FileSystem capability is denied"
176-
.to_string(),
177-
),
178-
None,
179-
None,
180-
));
181-
}
182-
let archive_path = args[0].try_into_string()?;
183-
let dest_file = args[1].try_into_string()?;
184-
gunzip_archive(&archive_path, &dest_file).map_err(|e| {
185-
RuntimeError::new(
186-
RuntimeErrorKind::InvalidOperation(format!("GUNZIP error: {}", e)),
187-
None,
188-
None,
189-
)
190-
})?;
191-
Ok(RuntimeValue::Null)
192-
},
67+
callback: compress_gunzip,
19368
}),
19469
);
19570

@@ -205,6 +80,156 @@ impl StdlibRegistry {
20580
}
20681
}
20782

83+
fn compress_zip(
84+
ctx: &mut RuntimeContext,
85+
args: Vec<RuntimeValue>,
86+
) -> Result<RuntimeValue, RuntimeError> {
87+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
88+
return Err(RuntimeError::new(
89+
RuntimeErrorKind::InvalidOperation(
90+
"Security policy violation: FileSystem capability is denied".to_string(),
91+
),
92+
None,
93+
None,
94+
));
95+
}
96+
let src_dir = args[0].try_into_string()?;
97+
let archive_path = args[1].try_into_string()?;
98+
zip_dir(&src_dir, &archive_path).map_err(|e| {
99+
RuntimeError::new(
100+
RuntimeErrorKind::InvalidOperation(format!("ZIP error: {}", e)),
101+
None,
102+
None,
103+
)
104+
})?;
105+
Ok(RuntimeValue::Null)
106+
}
107+
108+
fn compress_unzip(
109+
ctx: &mut RuntimeContext,
110+
args: Vec<RuntimeValue>,
111+
) -> Result<RuntimeValue, RuntimeError> {
112+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
113+
return Err(RuntimeError::new(
114+
RuntimeErrorKind::InvalidOperation(
115+
"Security policy violation: FileSystem capability is denied".to_string(),
116+
),
117+
None,
118+
None,
119+
));
120+
}
121+
let archive_path = args[0].try_into_string()?;
122+
let dest_dir = args[1].try_into_string()?;
123+
unzip_archive(&archive_path, &dest_dir).map_err(|e| {
124+
RuntimeError::new(
125+
RuntimeErrorKind::InvalidOperation(format!("UNZIP error: {}", e)),
126+
None,
127+
None,
128+
)
129+
})?;
130+
Ok(RuntimeValue::Null)
131+
}
132+
133+
fn compress_tar(
134+
ctx: &mut RuntimeContext,
135+
args: Vec<RuntimeValue>,
136+
) -> Result<RuntimeValue, RuntimeError> {
137+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
138+
return Err(RuntimeError::new(
139+
RuntimeErrorKind::InvalidOperation(
140+
"Security policy violation: FileSystem capability is denied".to_string(),
141+
),
142+
None,
143+
None,
144+
));
145+
}
146+
let src_dir = args[0].try_into_string()?;
147+
let archive_path = args[1].try_into_string()?;
148+
tar_dir(&src_dir, &archive_path).map_err(|e| {
149+
RuntimeError::new(
150+
RuntimeErrorKind::InvalidOperation(format!("TAR error: {}", e)),
151+
None,
152+
None,
153+
)
154+
})?;
155+
Ok(RuntimeValue::Null)
156+
}
157+
158+
fn compress_untar(
159+
ctx: &mut RuntimeContext,
160+
args: Vec<RuntimeValue>,
161+
) -> Result<RuntimeValue, RuntimeError> {
162+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
163+
return Err(RuntimeError::new(
164+
RuntimeErrorKind::InvalidOperation(
165+
"Security policy violation: FileSystem capability is denied".to_string(),
166+
),
167+
None,
168+
None,
169+
));
170+
}
171+
let archive_path = args[0].try_into_string()?;
172+
let dest_dir = args[1].try_into_string()?;
173+
untar_archive(&archive_path, &dest_dir).map_err(|e| {
174+
RuntimeError::new(
175+
RuntimeErrorKind::InvalidOperation(format!("UNTAR error: {}", e)),
176+
None,
177+
None,
178+
)
179+
})?;
180+
Ok(RuntimeValue::Null)
181+
}
182+
183+
fn compress_gzip(
184+
ctx: &mut RuntimeContext,
185+
args: Vec<RuntimeValue>,
186+
) -> Result<RuntimeValue, RuntimeError> {
187+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
188+
return Err(RuntimeError::new(
189+
RuntimeErrorKind::InvalidOperation(
190+
"Security policy violation: FileSystem capability is denied".to_string(),
191+
),
192+
None,
193+
None,
194+
));
195+
}
196+
let src_file = args[0].try_into_string()?;
197+
let archive_path = args[1].try_into_string()?;
198+
gzip_file(&src_file, &archive_path).map_err(|e| {
199+
RuntimeError::new(
200+
RuntimeErrorKind::InvalidOperation(format!("GZIP error: {}", e)),
201+
None,
202+
None,
203+
)
204+
})?;
205+
Ok(RuntimeValue::Null)
206+
}
207+
208+
fn compress_gunzip(
209+
ctx: &mut RuntimeContext,
210+
args: Vec<RuntimeValue>,
211+
) -> Result<RuntimeValue, RuntimeError> {
212+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
213+
return Err(RuntimeError::new(
214+
RuntimeErrorKind::InvalidOperation(
215+
"Security policy violation: FileSystem capability is denied".to_string(),
216+
),
217+
None,
218+
None,
219+
));
220+
}
221+
let archive_path = args[0].try_into_string()?;
222+
let dest_file = args[1].try_into_string()?;
223+
gunzip_archive(&archive_path, &dest_file).map_err(|e| {
224+
RuntimeError::new(
225+
RuntimeErrorKind::InvalidOperation(format!("GUNZIP error: {}", e)),
226+
None,
227+
None,
228+
)
229+
})?;
230+
Ok(RuntimeValue::Null)
231+
}
232+
208233
pub fn zip_dir(src_dir: &str, dst_file: &str) -> std::io::Result<()> {
209234
let file = File::create(dst_file)?;
210235
let mut zip = zip::ZipWriter::new(file);

‎stdlib/tests/stdlib_tests.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ fn test_http_module() {
540540
&mut ctx_unprivileged,
541541
vec![RuntimeValue::Str(format!("http://127.0.0.1:{}", port))],
542542
);
543-
assert!(matches!(res_get, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation")));
543+
assert!(
544+
matches!(res_get, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation"))
545+
);
544546

545547
let post = http.exports.get("post").unwrap();
546548
let res_post = post.call(
@@ -550,7 +552,9 @@ fn test_http_module() {
550552
RuntimeValue::Str("body".to_string()),
551553
],
552554
);
553-
assert!(matches!(res_post, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation")));
555+
assert!(
556+
matches!(res_post, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation"))
557+
);
554558

555559
let listen = http.exports.get("listen").unwrap();
556560
let res_listen = listen.call(

0 commit comments

Comments
 (0)