2020-08-17 16:19:21 -07:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-06 13:57:14 -08:00
|
|
|
import type * as types from './types';
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
2021-07-06 21:16:37 +02:00
|
|
|
import { isString, isRegExp, constructURLBasedOnBaseURL } from '../utils/utils';
|
2020-08-17 16:19:21 -07:00
|
|
|
|
2020-08-18 09:37:40 -07:00
|
|
|
export function envObjectToArray(env: types.Env): { name: string, value: string }[] {
|
|
|
|
const result: { name: string, value: string }[] = [];
|
|
|
|
for (const name in env) {
|
|
|
|
if (!Object.is(env[name], undefined))
|
|
|
|
result.push({ name, value: String(env[name]) });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-08-19 13:27:58 -07:00
|
|
|
|
|
|
|
export async function evaluationScript(fun: Function | string | { path?: string, content?: string }, arg?: any, addSourceUrl: boolean = true): Promise<string> {
|
|
|
|
if (typeof fun === 'function') {
|
|
|
|
const source = fun.toString();
|
|
|
|
const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg);
|
|
|
|
return `(${source})(${argString})`;
|
|
|
|
}
|
|
|
|
if (arg !== undefined)
|
|
|
|
throw new Error('Cannot evaluate a string with arguments');
|
2020-08-22 07:07:13 -07:00
|
|
|
if (isString(fun))
|
2020-08-19 13:27:58 -07:00
|
|
|
return fun;
|
|
|
|
if (fun.content !== undefined)
|
|
|
|
return fun.content;
|
|
|
|
if (fun.path !== undefined) {
|
2021-06-03 09:55:33 -07:00
|
|
|
let source = await fs.promises.readFile(fun.path, 'utf8');
|
2020-08-19 13:27:58 -07:00
|
|
|
if (addSourceUrl)
|
2022-04-03 17:47:12 -08:00
|
|
|
source += '\n//# sourceURL=' + fun.path.replace(/\n/g, '');
|
2020-08-19 13:27:58 -07:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
throw new Error('Either path or content property must be present');
|
|
|
|
}
|
|
|
|
|
2021-01-25 14:49:51 -08:00
|
|
|
export function parsedURL(url: string): URL | null {
|
|
|
|
try {
|
|
|
|
return new URL(url);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 21:16:37 +02:00
|
|
|
export function urlMatches(baseURL: string | undefined, urlString: string, match: types.URLMatch | undefined): boolean {
|
2020-08-19 13:27:58 -07:00
|
|
|
if (match === undefined || match === '')
|
|
|
|
return true;
|
2021-07-06 21:16:37 +02:00
|
|
|
if (isString(match) && !match.startsWith('*'))
|
|
|
|
match = constructURLBasedOnBaseURL(baseURL, match);
|
2020-08-22 07:07:13 -07:00
|
|
|
if (isString(match))
|
2020-08-19 13:27:58 -07:00
|
|
|
match = globToRegex(match);
|
2020-08-22 07:07:13 -07:00
|
|
|
if (isRegExp(match))
|
2020-08-19 13:27:58 -07:00
|
|
|
return match.test(urlString);
|
|
|
|
if (typeof match === 'string' && match === urlString)
|
|
|
|
return true;
|
2021-01-25 14:49:51 -08:00
|
|
|
const url = parsedURL(urlString);
|
|
|
|
if (!url)
|
|
|
|
return false;
|
2020-08-19 13:27:58 -07:00
|
|
|
if (typeof match === 'string')
|
|
|
|
return url.pathname === match;
|
|
|
|
if (typeof match !== 'function')
|
|
|
|
throw new Error('url parameter should be string, RegExp or function');
|
|
|
|
return match(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
const escapeGlobChars = new Set(['/', '$', '^', '+', '.', '(', ')', '=', '!', '|']);
|
|
|
|
|
|
|
|
export function globToRegex(glob: string): RegExp {
|
|
|
|
const tokens = ['^'];
|
|
|
|
let inGroup;
|
|
|
|
for (let i = 0; i < glob.length; ++i) {
|
|
|
|
const c = glob[i];
|
|
|
|
if (escapeGlobChars.has(c)) {
|
|
|
|
tokens.push('\\' + c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c === '*') {
|
|
|
|
const beforeDeep = glob[i - 1];
|
|
|
|
let starCount = 1;
|
|
|
|
while (glob[i + 1] === '*') {
|
|
|
|
starCount++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
const afterDeep = glob[i + 1];
|
|
|
|
const isDeep = starCount > 1 &&
|
|
|
|
(beforeDeep === '/' || beforeDeep === undefined) &&
|
|
|
|
(afterDeep === '/' || afterDeep === undefined);
|
|
|
|
if (isDeep) {
|
|
|
|
tokens.push('((?:[^/]*(?:\/|$))*)');
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
tokens.push('([^/]*)');
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
|
|
|
tokens.push('.');
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
inGroup = true;
|
|
|
|
tokens.push('(');
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
inGroup = false;
|
|
|
|
tokens.push(')');
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
if (inGroup) {
|
|
|
|
tokens.push('|');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tokens.push('\\' + c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tokens.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tokens.push('$');
|
|
|
|
return new RegExp(tokens.join(''));
|
|
|
|
}
|