Skip to content

Commit fac303d

Browse files
committed
Adding two tests for command parser.
* Test 1: quotes appear at start of command and has a space in * Test 2: quote appears in a command, and has a space.
1 parent a73a650 commit fac303d

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
/.nyc_output
4+
.DS_Store

src/worker-script/index.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,54 @@ const FS = ({
4646
};
4747

4848
const parseArgs = (command) => {
49-
let args = [];
49+
const args = [];
5050
let nextDelimiter = 0;
5151
let prevDelimiter = 0;
52-
while((nextDelimiter = command.indexOf(' ', prevDelimiter)) >= 0) {
53-
let arg = command.substring(prevDelimiter, nextDelimiter)
52+
while ((nextDelimiter = command.indexOf(' ', prevDelimiter)) >= 0) {
53+
let arg = command.substring(prevDelimiter, nextDelimiter);
54+
let quoteIndex = arg.indexOf('\'');
55+
let doubleQuoteIndex = arg.indexOf('"');
5456

55-
if (arg[0] === '\'' || arg[0] === '\"') {
57+
if (quoteIndex === 0 || doubleQuoteIndex === 0) {
58+
/* The argument has a quote at the start i.e, 'id=0,streams=0 id=1,streams=1' */
5659
const delimiter = arg[0];
57-
const endDelimiter = command.indexOf(delimeter, prevDelimiter + 1);
60+
const endDelimiter = command.indexOf(delimiter, prevDelimiter + 1);
5861

59-
if (endDelimiter < 0) throw `Bad command espcape sequence ${delimeter} near ${nextDelimiter}`
60-
61-
arg = command.substring(prevDelimiter+1, endDelimiter);
62+
if (endDelimiter < 0) {
63+
throw new Error(`Bad command escape sequence ${delimiter} near ${nextDelimiter}`);
64+
}
65+
66+
arg = command.substring(prevDelimiter + 1, endDelimiter);
6267
prevDelimiter = endDelimiter + 2;
63-
}
64-
else {
65-
prevDelimiter = nextDelimiter + 1;
68+
args.push(arg);
69+
} else if (quoteIndex > 0 || doubleQuoteIndex > 0) {
70+
/* The argument has a quote in it, it must be ended correctly i,e. title='test' */
71+
if (quoteIndex === -1) quoteIndex = Infinity;
72+
if (doubleQuoteIndex === -1) doubleQuoteIndex = Infinity;
73+
const delimiter = (quoteIndex < doubleQuoteIndex) ? '\'' : '"';
74+
const endDelimiter = command.indexOf(delimiter, prevDelimiter + Math.min(quoteIndex, doubleQuoteIndex) + 1);
6675

67-
if (arg === "") {
68-
continue;
76+
if (endDelimiter < 0) {
77+
throw new Error(`Bad command escape sequence ${delimiter} near ${nextDelimiter}`);
6978
}
70-
}
7179

72-
args.push(arg)
80+
arg = command.substring(prevDelimiter, endDelimiter + 1);
81+
prevDelimiter = endDelimiter + 2;
82+
args.push(arg);
83+
} else if (arg !== '') {
84+
args.push(arg);
85+
prevDelimiter = nextDelimiter + 1;
86+
} else {
87+
prevDelimiter = nextDelimiter + 1;
88+
}
7389
}
7490

75-
if (prevDelimiter != command.length) {
76-
args.push(command.substring(prevDelimiter))
91+
if (prevDelimiter !== command.length) {
92+
args.push(command.substring(prevDelimiter));
7793
}
7894

7995
return args;
80-
}
96+
};
8197

8298
const run = ({
8399
payload: {

tests/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const IS_BROWSER = typeof window !== 'undefined' && typeof window.document !== '
44
const OPTIONS = {
55
corePath: '../node_modules/@ffmpeg/core/ffmpeg-core.js',
66
...(IS_BROWSER ? { workerPath: '../dist/worker.dev.js' } : {}),
7+
logger: ({ message }) => console.log(message),
78
};
89
const FLAME_MP4_LENGTH = 100374;
10+
const META_FLAME_MP4_LENGTH = 100408;
911

1012
if (typeof module !== 'undefined') {
1113
module.exports = {
@@ -14,5 +16,6 @@ if (typeof module !== 'undefined') {
1416
IS_BROWSER,
1517
OPTIONS,
1618
FLAME_MP4_LENGTH,
19+
META_FLAME_MP4_LENGTH,
1720
};
1821
}

tests/ffmpeg.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,27 @@ describe('transcode()', () => {
1818
));
1919
});
2020
});
21+
22+
describe('run()', () => {
23+
describe('should run a command with quoted parameters at start and a space in between', () => {
24+
['flame.avi'].forEach((name) => (
25+
it(`run ${name}`, async () => {
26+
await worker.write(name, `${BASE_URL}/${name}`);
27+
await worker.run(`-y -i ${name} -metadata 'title="my title"' output.mp4`);
28+
const { data } = await worker.read('output.mp4');
29+
expect(data.length).to.be(META_FLAME_MP4_LENGTH);
30+
}).timeout(TIMEOUT)
31+
));
32+
});
33+
34+
describe('should run a command with name quoted parameters and a space in between', () => {
35+
['flame.avi'].forEach((name) => (
36+
it(`run ${name}`, async () => {
37+
await worker.write(name, `${BASE_URL}/${name}`);
38+
await worker.run(`-y -i ${name} -metadata title="my title" output.mp4`);
39+
const { data } = await worker.read('output.mp4');
40+
expect(data.length).to.be(META_FLAME_MP4_LENGTH);
41+
}).timeout(TIMEOUT)
42+
));
43+
});
44+
});

0 commit comments

Comments
 (0)