brsh 2.1.1 → 2.1.3

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 (56) hide show
  1. package/.github/workflows/pages.yml +12 -1
  2. package/.nvmrc +1 -0
  3. package/CHANGELOG.md +14 -0
  4. package/Readme.md +191 -3
  5. package/bin/brsh-shell.js +65 -0
  6. package/dist/brsh.css +1 -1
  7. package/dist/shell.es.js +854 -577
  8. package/dist/shell.es.js.map +1 -1
  9. package/dist/shell.min.js +13 -9
  10. package/dist/shell.min.js.map +1 -1
  11. package/dist/terminal.es.js +874 -588
  12. package/dist/terminal.es.js.map +1 -1
  13. package/dist/terminal.min.js +16 -12
  14. package/dist/terminal.min.js.map +1 -1
  15. package/index.js +65 -10
  16. package/lib/BaseFileSystem.js +82 -0
  17. package/lib/Command.js +3 -1
  18. package/lib/Context.js +3 -2
  19. package/lib/FileSystem.js +55 -26
  20. package/lib/NodeFileSystem.js +31 -17
  21. package/lib/Terminal.js +34 -5
  22. package/lib/WasmCommand.js +14 -7
  23. package/lib/buildFilesystemStructure.js +9 -4
  24. package/lib/builtins/cat.js +4 -1
  25. package/lib/builtins/echo.js +2 -0
  26. package/lib/builtins/false.js +1 -1
  27. package/lib/builtins/grep.js +10 -2
  28. package/lib/builtins/ls.js +6 -2
  29. package/lib/builtins/test.js +2 -4
  30. package/lib/builtins/true.js +1 -1
  31. package/lib/local/alias.js +2 -0
  32. package/lib/local/cd.js +5 -3
  33. package/lib/local/chmod.js +2 -3
  34. package/lib/local/clear.js +2 -0
  35. package/lib/local/cp.js +13 -8
  36. package/lib/local/curl.js +138 -0
  37. package/lib/local/exit.js +2 -0
  38. package/lib/local/export.js +2 -0
  39. package/lib/local/index.js +2 -1
  40. package/lib/local/jsfunction.js +1 -1
  41. package/lib/local/mkdir.js +3 -3
  42. package/lib/local/mv.js +12 -6
  43. package/lib/local/pwd.js +2 -0
  44. package/lib/local/read.js +2 -0
  45. package/lib/local/rm.js +1 -1
  46. package/lib/local/source.js +5 -4
  47. package/lib/local/which.js +2 -0
  48. package/package.json +9 -4
  49. package/styles/terminal.css +8 -1
  50. package/test/builtins.test.js +11 -0
  51. package/test/exportFileSystem.test.js +28 -8
  52. package/test/local-commands.test.js +11 -0
  53. package/test/scripts.test.js +57 -0
  54. package/test/shell.test.js +42 -0
  55. package/vite.config.demo.js +35 -0
  56. package/vite.config.js +34 -0
@@ -54,8 +54,7 @@ describe('buildStructure', () => {
54
54
  writeFileSync(join(root, 'readme.txt'), 'hello');
55
55
  const { filesystem } = buildStructure(root);
56
56
  expect(filesystem).toHaveProperty('readme.txt');
57
- expect(filesystem['readme.txt']).toMatch(/^require\('/);
58
- expect(filesystem['readme.txt']).toContain('readme.txt');
57
+ expect(filesystem['readme.txt']).toBe('hello');
59
58
  });
60
59
 
61
60
  it('strips the .js extension from JavaScript file keys', () => {
@@ -66,11 +65,11 @@ describe('buildStructure', () => {
66
65
  expect(filesystem['app']).toMatch(/^require\('/);
67
66
  });
68
67
 
69
- it('uses an absolute path in the require() string', () => {
70
- writeFileSync(join(root, 'data.txt'), 'content');
68
+ it('uses an absolute path in the require() string for JavaScript files', () => {
69
+ writeFileSync(join(root, 'data.js'), 'module.exports = "content"');
71
70
  const { filesystem } = buildStructure(root);
72
- const reqPath = filesystem['data.txt'].match(/require\('(.+?)'\)/)[1];
73
- expect(reqPath).toBe(resolve(root, 'data.txt'));
71
+ const reqPath = filesystem.data.match(/require\('(.+?)'\)/)[1];
72
+ expect(reqPath).toBe(resolve(root, 'data.js'));
74
73
  });
75
74
 
76
75
  it('recursively includes nested directories', () => {
@@ -87,9 +86,9 @@ describe('buildStructure', () => {
87
86
  const b = join(a, 'b');
88
87
  mkdirSync(a);
89
88
  mkdirSync(b);
90
- writeFileSync(join(b, 'deep.txt'), 'deep');
89
+ writeFileSync(join(b, 'deep.js'), 'module.exports = "deep"');
91
90
  const { filesystem } = buildStructure(root);
92
- expect(filesystem.a.b['deep.txt']).toMatch(/^require\('/);
91
+ expect(filesystem.a.b['deep']).toMatch(/^require\('/);
93
92
  });
94
93
 
95
94
  it('ignores .DS_Store files', () => {
@@ -204,6 +203,27 @@ describe('exportFileSystem CLI', () => {
204
203
  expect(content).toContain('Hello World');
205
204
  });
206
205
 
206
+ it('builds a directory tree without requiring a root index.js wrapper', () => {
207
+ rmSync(join(root, 'app.js'));
208
+ mkdirSync(join(root, 'bin'));
209
+ writeFileSync(join(root, 'bin', 'tool.js'), 'module.exports = function tool() {}');
210
+
211
+ const outFile = join(outDir, 'fs.js');
212
+ const result = runCLI([root, '-v', 'FS', '-o', outFile]);
213
+ const content = readFileSync(outFile, 'utf8');
214
+
215
+ expect(result.status).toBe(0);
216
+ expect(content).toContain('Hello World');
217
+ expect(content).toContain('tool');
218
+ });
219
+
220
+ it('bundle does not contain readFileSync references', () => {
221
+ const outFile = join(outDir, 'fs.js');
222
+ runCLI([root, '-v', 'FS', '-o', outFile]);
223
+ const content = readFileSync(outFile, 'utf8');
224
+ expect(content).not.toContain('readFileSync');
225
+ });
226
+
207
227
  it('handles a nested directory structure', () => {
208
228
  const sub = join(root, 'sub');
209
229
  mkdirSync(sub);
@@ -360,6 +360,17 @@ describe('mkdir', () => {
360
360
  const { exitCode } = await run(shell, 'mkdir');
361
361
  expect(exitCode).toBeGreaterThan(0);
362
362
  });
363
+
364
+ it('errors when parent directory is not writable', async () => {
365
+ const shell = await createShell({
366
+ filesystem: { protected: {} },
367
+ permissions: { '/protected': 0o555 }
368
+ });
369
+ const { exitCode, errors } = await run(shell, 'mkdir /protected/newdir');
370
+ expect(exitCode).toBeGreaterThan(0);
371
+ expect(errors.join(' ')).toMatch(/permission denied/i);
372
+ expect(shell.context.fs.getFileByPath('/protected/newdir')).toBeNull();
373
+ });
363
374
  });
364
375
 
365
376
  describe('rm', () => {
@@ -132,6 +132,63 @@ describe('shell script execution', () => {
132
132
  });
133
133
  });
134
134
 
135
+ // ---------------------------------------------------------------------------
136
+ // Permission checks
137
+ // ---------------------------------------------------------------------------
138
+
139
+ describe('execute permission enforcement', () => {
140
+ const SCRIPT = '#!/sh.js\necho ran';
141
+
142
+ it('refuses to run a script without execute permission', async () => {
143
+ const shell = await createShell({
144
+ filesystem: { scripts: { 'nox.sh': SCRIPT } },
145
+ permissions: { '/scripts/nox.sh': 0o644 }
146
+ });
147
+ const { errors, exitCode } = await run(shell, '/scripts/nox.sh');
148
+ expect(errors.length).toBeGreaterThan(0);
149
+ expect(exitCode).toBeGreaterThan(0);
150
+ });
151
+
152
+ it('runs a script that has execute permission', async () => {
153
+ const shell = await createShell({
154
+ filesystem: { scripts: { 'withx.sh': SCRIPT } },
155
+ permissions: { '/scripts/withx.sh': 0o755 }
156
+ });
157
+ const { output, exitCode } = await run(shell, '/scripts/withx.sh');
158
+ expect(output).toContain('ran');
159
+ expect(exitCode).toBe(0);
160
+ });
161
+
162
+ it('refuses to run a script when only owner-execute bit is cleared (0o644)', async () => {
163
+ const shell = await createShell({
164
+ filesystem: { scripts: { 'nox2.sh': SCRIPT } },
165
+ permissions: { '/scripts/nox2.sh': 0o644 }
166
+ });
167
+ const { errors, exitCode } = await run(shell, '/scripts/nox2.sh');
168
+ expect(errors.length).toBeGreaterThan(0);
169
+ expect(exitCode).toBeGreaterThan(0);
170
+ });
171
+
172
+ it('runs a Command class that has no explicit permission entry (defaults to executable)', async () => {
173
+ const shell = await createShell({
174
+ filesystem: { apps: { hello: HelloApp } }
175
+ });
176
+ const { output, exitCode } = await run(shell, '/apps/hello');
177
+ expect(output).toContain('Hello from JS app');
178
+ expect(exitCode).toBe(0);
179
+ });
180
+
181
+ it('refuses to run a Command class with explicit non-executable permission', async () => {
182
+ const shell = await createShell({
183
+ filesystem: { apps: { hello: HelloApp } },
184
+ permissions: { '/apps/hello': 0o644 }
185
+ });
186
+ const { errors, exitCode } = await run(shell, '/apps/hello');
187
+ expect(errors.length).toBeGreaterThan(0);
188
+ expect(exitCode).toBeGreaterThan(0);
189
+ });
190
+ });
191
+
135
192
  // ---------------------------------------------------------------------------
136
193
  // Conditional execution via &&
137
194
  // ---------------------------------------------------------------------------
@@ -215,6 +215,48 @@ describe('unclosed quote error', () => {
215
215
  });
216
216
  });
217
217
 
218
+ describe('output redirection', () => {
219
+ it('writes command stdout to a file with >', async () => {
220
+ const shell = await createShell({ filesystem: { home: {} } });
221
+ await run(shell, 'echo hello > /home/out.txt');
222
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('hello');
223
+ });
224
+
225
+ it('overwrites an existing file with >', async () => {
226
+ const shell = await createShell({ filesystem: { home: { 'out.txt': 'old' } } });
227
+ await run(shell, 'echo new > /home/out.txt');
228
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('new');
229
+ });
230
+
231
+ it('appends to an existing file with >>', async () => {
232
+ const shell = await createShell({ filesystem: { home: { 'out.txt': 'first' } } });
233
+ await run(shell, 'echo second >> /home/out.txt');
234
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('first\nsecond');
235
+ });
236
+
237
+ it('errors when target file is not writable', async () => {
238
+ const shell = await createShell({
239
+ filesystem: { home: { 'readonly.txt': 'original' } },
240
+ permissions: { '/home/readonly.txt': 0o444 }
241
+ });
242
+ const { errors, exitCode } = await run(shell, 'echo foo > /home/readonly.txt');
243
+ expect(exitCode).toBeGreaterThan(0);
244
+ expect(errors.join(' ')).toMatch(/permission denied/i);
245
+ expect(shell.context.fs.getFileByPath('/home/readonly.txt')).toBe('original');
246
+ });
247
+
248
+ it('errors when target directory is not writable', async () => {
249
+ const shell = await createShell({
250
+ filesystem: { protected: {} },
251
+ permissions: { '/protected': 0o555 }
252
+ });
253
+ const { errors, exitCode } = await run(shell, 'echo foo > /protected/newfile.txt');
254
+ expect(exitCode).toBeGreaterThan(0);
255
+ expect(errors.join(' ')).toMatch(/permission denied/i);
256
+ expect(shell.context.fs.getFileByPath('/protected/newfile.txt')).toBeNull();
257
+ });
258
+ });
259
+
218
260
  describe('tabCompletion', () => {
219
261
  it('returns matching built-in commands from PATH', async () => {
220
262
  const shell = await createShell();
@@ -0,0 +1,35 @@
1
+ import { defineConfig } from 'vite';
2
+ import { nodePolyfills } from 'vite-plugin-node-polyfills';
3
+ import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
4
+ import path from 'path';
5
+
6
+ const projectRoot = import.meta.dirname;
7
+
8
+ export default defineConfig({
9
+ root: path.resolve(projectRoot, 'demo'),
10
+ plugins: [
11
+ nodePolyfills(),
12
+ viteCommonjs()
13
+ ],
14
+ resolve: {
15
+ alias: [
16
+ {
17
+ find: /^brsh\/terminal$/,
18
+ replacement: path.resolve(projectRoot, 'lib/Terminal.js')
19
+ },
20
+ {
21
+ find: /^brsh$/,
22
+ replacement: path.resolve(projectRoot, 'index.js')
23
+ },
24
+ {
25
+ find: /^Shell$/,
26
+ replacement: path.resolve(projectRoot, 'index.js')
27
+ }
28
+ ]
29
+ },
30
+ base: './',
31
+ build: {
32
+ outDir: path.resolve(projectRoot, 'dist-demo'),
33
+ emptyOutDir: true,
34
+ }
35
+ });
package/vite.config.js ADDED
@@ -0,0 +1,34 @@
1
+ import { defineConfig } from 'vite';
2
+ import { nodePolyfills } from 'vite-plugin-node-polyfills';
3
+ import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
4
+ import path from 'path';
5
+
6
+ const projectRoot = import.meta.dirname;
7
+
8
+ export default defineConfig({
9
+ plugins: [
10
+ nodePolyfills(),
11
+ viteCommonjs()
12
+ ],
13
+ resolve: {
14
+ alias: [
15
+ {
16
+ find: /^brsh\/terminal$/,
17
+ replacement: path.resolve(projectRoot, 'lib/Terminal.js')
18
+ },
19
+ {
20
+ find: /^brsh$/,
21
+ replacement: path.resolve(projectRoot, 'index.js')
22
+ },
23
+ {
24
+ find: /^Shell$/,
25
+ replacement: path.resolve(projectRoot, 'index.js')
26
+ }
27
+ ]
28
+ },
29
+ server: {
30
+ fs: {
31
+ allow: [projectRoot]
32
+ }
33
+ }
34
+ });