Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove node dependency #17

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 63 additions & 22 deletions src/converters/helpers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
import * as path from 'node:path'
import redirects from '@adguard/scriptlets/dist/redirects.json' with { type: 'json' }

function getPathBasename(path) {
const lastIndex = path.lastIndexOf('/');
if (lastIndex === -1) {
return path;
}
return path.slice(lastIndex + 1);
}

function getPathDirname(path) {
const lastIndex = path.lastIndexOf('/');
if (lastIndex === -1) {
return '.';
}
return path.slice(0, lastIndex);
}

const allowedResourceExtensions = [
'html',
'js',
'css',
'mp4',
'mp3',
'xml',
'txt',
'json',
'png',
'gif',
'empty',
];

function getPreferredResource(aliases) {
// ignore non-supported files and manually created uBO aliases by AdGuard
return aliases.find(alias => {
const extension = alias.split('.').pop();
return extension !== undefined &&
allowedResourceExtensions.includes(extension) &&
!alias.startsWith('ubo-') &&
!alias.includes('-transparent');
});
}

function getFileExtensionByContentType(contentType) {
if (contentType.includes(';')) {
contentType = contentType.slice(0, contentType.indexOf(';'));
}
switch (contentType) {
case 'text/html':
return '.html';
case 'text/css':
return '.css';
case 'text/plain':
case 'application/javascript':
return '.js';
case 'application/json':
return '.json';
}

return '';
}

export function generateResourcesMapping() {
const resourcesMapping = new Map();
const allowedResourceExtensions = [
'html',
'js',
'css',
'mp4',
'mp3',
'xml',
'txt',
'json',
'empty',
];

function getPreferredResource(aliases) {
// ignore non-supported files and manually created uBO aliases by AdGuard
return aliases.find(alias => {
const extension = alias.split('.').pop();
return allowedResourceExtensions.includes(extension) && !alias.startsWith('ubo-');
});
}

for (const redirect of redirects) {
// Skip, in case of AdGuard-only resource
Expand All @@ -38,6 +78,7 @@ export function generateResourcesMapping() {

// Register to mapping
resourcesMapping.set(redirect.title, preferredResourceName);
resourcesMapping.set(redirect.title + getFileExtensionByContentType(redirect.contentType), preferredResourceName);
for (const alias of redirect.aliases) {
if (alias !== preferredResourceName) {
resourcesMapping.set(alias, preferredResourceName);
Expand Down Expand Up @@ -122,12 +163,12 @@ export function normalizeRule(rule, { resourcesMapping = DEFAULT_RESOURCE_MAPPIN
}

if (newRule.action && newRule.action.type === 'redirect') {
const filename = path.basename(newRule.action.redirect.extensionPath);
const filename = getPathBasename(newRule.action.redirect.extensionPath);
const preferredFilename = resourcesMapping.get(filename);

if (preferredFilename !== undefined) {
newRule.action.redirect.extensionPath =
path.dirname(newRule.action.redirect.extensionPath) + '/' + preferredFilename;
getPathDirname(newRule.action.redirect.extensionPath) + '/' + preferredFilename;
}
}

Expand Down
Loading