Skip to content

Commit 7835ecb

Browse files
author
Oleg Labudko
committed
Pull request: AG-5628 Create a node module wrapping Safari Converter
Merge in ADGUARD-IOS/safari-converter from feature/AG-5628 to master Squashed commit of the following: commit 13a0643 Merge: 83c2d3c 8ab80af Author: tvinzz <[email protected]> Date: Mon Dec 21 15:20:09 2020 +0300 Merge branch 'master' into feature/AG-5628 commit 83c2d3c Author: tvinzz <[email protected]> Date: Thu Dec 17 14:24:07 2020 +0300 moved converter tool path to constant commit 7f5e9cd Author: tvinzz <[email protected]> Date: Wed Dec 16 15:42:05 2020 +0300 moved to node-safari-converter commit 10a5a7b Author: tvinzz <[email protected]> Date: Wed Dec 16 04:59:10 2020 +0300 added jest tests commit 0d0b7c5 Author: tvinzz <[email protected]> Date: Wed Dec 16 04:15:42 2020 +0300 fixes commit 0a0e96f Author: tvinzz <[email protected]> Date: Wed Dec 16 03:07:01 2020 +0300 added getConverterVersion commit c569f7a Author: tvinzz <[email protected]> Date: Tue Dec 15 17:53:22 2020 +0300 fixes commit a6fc757 Author: tvinzz <[email protected]> Date: Tue Dec 15 04:08:22 2020 +0300 updated README commit d6fbeea Author: tvinzz <[email protected]> Date: Tue Dec 15 03:47:10 2020 +0300 added ConverterTool.json commit f5d307f Author: tvinzz <[email protected]> Date: Tue Dec 15 03:36:18 2020 +0300 exported jsonFromRules commit 90aeb4a Author: tvinzz <[email protected]> Date: Tue Dec 15 02:17:42 2020 +0300 build after installation commit 61e4e69 Author: tvinzz <[email protected]> Date: Mon Dec 14 22:46:36 2020 +0300 fixed description commit 9f56cf7 Author: tvinzz <[email protected]> Date: Mon Dec 14 22:32:13 2020 +0300 added ci:increment to scripts commit 49f56d4 Author: tvinzz <[email protected]> Date: Mon Dec 14 22:16:09 2020 +0300 fixed build command commit 40995b5 Author: tvinzz <[email protected]> Date: Mon Dec 14 22:05:11 2020 +0300 added test to scripts commit 2bb7ad5 Author: tvinzz <[email protected]> Date: Mon Dec 14 21:58:53 2020 +0300 cleaning commit 3fa099e Author: tvinzz <[email protected]> Date: Mon Dec 14 21:57:42 2020 +0300 wrapped safari converter into node module
1 parent 8ab80af commit 7835ecb

File tree

8 files changed

+3739
-0
lines changed

8 files changed

+3739
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/*.xcodeproj
55
xcuserdata/
66
/.idea
7+
/node_modules
8+
/bin/ConverterTool

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,18 @@ Push a new tag in `v*.*.*` format, then provided github action is intended to bu
6868

6969
### Third-party dependencies
7070
- Punycode (https://github.com/gumob/PunycodeSwift.git)
71+
72+
### Use as node module
73+
74+
##### Requirements:
75+
* Swift 4 or higher
76+
77+
After installation the build process occurs and binary file will be copied to bin directory
78+
79+
#### API
80+
`jsonFromRules(rules, advancedBlocking, log)` - method to convert rules into JSON
81+
* rules - array of rules
82+
* advancedBlocking - if we need advanced blocking content (boolean)
83+
* logger
84+
85+
`getConverterVersion` - returns Safari Converter Lib version

node-safari-converter/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { jsonFromRules, getConverterVersion } = require('./src/api');
2+
3+
module.exports = (function () {
4+
return {
5+
jsonFromRules,
6+
getConverterVersion,
7+
};
8+
})();

node-safari-converter/script/build.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
swift build -v -c release
4+
mkdir -p bin
5+
cp .build/release/ConverterTool bin

node-safari-converter/src/api.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const path = require('path');
2+
const { spawn } = require('child_process');
3+
const { version } = require('../../package.json');
4+
5+
const CONVERTER_TOOL_PATH = path.resolve(__dirname, '../../bin/ConverterTool');
6+
7+
module.exports = (function () {
8+
/**
9+
* Runs shell script
10+
*
11+
* @param command
12+
* @param args
13+
* @param callback
14+
*/
15+
const runScript = (command, args, callback) => {
16+
const child = spawn(command, args);
17+
18+
let stdout = '';
19+
let stderr = '';
20+
21+
child.stdout.setEncoding('utf8');
22+
child.stdout.on('data', (data) => {
23+
data = data.toString();
24+
stdout += data;
25+
});
26+
27+
child.stderr.setEncoding('utf8');
28+
child.stderr.on('data', (data) => {
29+
data = data.toString();
30+
stderr += data;
31+
});
32+
33+
child.on('close', (code) => {
34+
callback(code, stdout, stderr);
35+
});
36+
37+
return child;
38+
};
39+
40+
/**
41+
* Runs converter method for rules
42+
*
43+
* @param rules array of rules
44+
* @param advancedBlocking if we need advanced blocking content
45+
* @param rulesLimit
46+
*/
47+
const jsonFromRules = async (rules, advancedBlocking, rulesLimit) => {
48+
return new Promise((resolve, reject) => {
49+
const child = runScript(CONVERTER_TOOL_PATH, [
50+
`-limit=${rulesLimit}`,
51+
'-optimize=false',
52+
`-advancedBlocking=${advancedBlocking}`,
53+
], (code, stdout, stderr) => {
54+
if (code !== 0) {
55+
reject(stderr);
56+
return;
57+
}
58+
59+
const result = JSON.parse(stdout);
60+
61+
resolve(result);
62+
});
63+
64+
child.stdin.setEncoding('utf8');
65+
for (const r of rules) {
66+
child.stdin.write(r);
67+
child.stdin.write('\n');
68+
}
69+
70+
child.stdin.end();
71+
});
72+
};
73+
74+
/**
75+
* Returns Safari Converter Lib version
76+
*/
77+
const getConverterVersion = () => {
78+
return version;
79+
}
80+
81+
return {
82+
jsonFromRules,
83+
getConverterVersion,
84+
};
85+
})();
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { jsonFromRules, getConverterVersion } = require('../index');
2+
const pJson = require('../../package.json');
3+
4+
describe('API test', () => {
5+
it('jsonFromRules test', async () => {
6+
const rules = ['example.com##.ads-banner', '||test.com^$image'];
7+
const result = await jsonFromRules(rules, false, 50000);
8+
const converted = JSON.parse(result.converted);
9+
10+
expect(converted[0].trigger['if-domain']).toStrictEqual(['*example.com']);
11+
expect(converted[0].action.type).toBe('css-display-none');
12+
expect(converted[0].action.selector).toBe('.ads-banner');
13+
14+
expect(converted[1].trigger['resource-type']).toStrictEqual(['image']);
15+
expect(converted[1].action.type).toBe('block');
16+
});
17+
18+
it('getConverterVersion test', () => {
19+
let version = getConverterVersion();
20+
expect(version).toBe(pJson.version);
21+
});
22+
});

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "safari-converter-lib",
3+
"version": "1.1.5",
4+
"description": "Wrapper with version for Swift Safari Converter to use it as node module",
5+
"repository": "github:AdguardTeam/SafariConverterLib",
6+
"main": "node-safari-converter/index.js",
7+
"scripts": {
8+
"build": "swift build -v -c release",
9+
"test": "swift test",
10+
"api-test": "yarn jest",
11+
"ci:increment": "yarn version --patch --no-git-tag-version",
12+
"postinstall": "node-safari-converter/script/build.sh"
13+
},
14+
"devDependencies": {
15+
"jest": "^26.6.3"
16+
}
17+
}

0 commit comments

Comments
 (0)