-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy Razon
authored
Nov 12, 2023
1 parent
8989b0d
commit 6f5d017
Showing
18 changed files
with
320 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { COMPOSE_TUNNEL_AGENT_SERVICE_LABELS } from './labels' | ||
export { ScriptInjection, parseScriptInjectionLabels, scriptInjectionsToLabels } from './script-injection' | ||
|
||
export const COMPOSE_TUNNEL_AGENT_SERVICE_NAME = 'preevy_proxy' | ||
export const COMPOSE_TUNNEL_AGENT_PORT = 3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const COMPOSE_TUNNEL_AGENT_SERVICE_LABELS = { | ||
PROFILE_THUMBPRINT: 'preevy.profile_thumbprint', | ||
PRIVATE_MODE: 'preevy.private_mode', | ||
ENV_ID: 'preevy.env_id', | ||
ACCESS: 'preevy.access', | ||
EXPOSE: 'preevy.expose', | ||
INJECT_SCRIPT_PREFIX: 'preevy.inject_script', | ||
} as const |
100 changes: 100 additions & 0 deletions
100
packages/common/src/compose-tunnel-agent/script-injection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { describe, test, expect, it } from '@jest/globals' | ||
import { ScriptInjection, parseScriptInjectionLabels, scriptInjectionsToLabels } from './script-injection' | ||
|
||
describe('script injection labels', () => { | ||
describe('parseScriptInjectionLabels', () => { | ||
test('should parse correctly a single label group', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.defer': 'true', | ||
'preevy.inject_script.widget.async': 'false', | ||
'preevy.inject_script.widget.path_regex': 't.*t', | ||
} | ||
|
||
const [scripts, errors] = parseScriptInjectionLabels(labels) | ||
expect(errors).toHaveLength(0) | ||
expect(scripts).toHaveLength(1) | ||
expect(scripts[0]).toMatchObject({ | ||
src: 'https://my-script', | ||
defer: true, | ||
async: false, | ||
pathRegex: expect.any(RegExp), | ||
}) | ||
expect(scripts[0].pathRegex?.source).toBe('t.*t') | ||
}) | ||
|
||
test('should ignore scripts with invalid regex', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.path_regex': '[', | ||
} | ||
const [scripts, errors] = parseScriptInjectionLabels(labels) | ||
expect(scripts).toHaveLength(0) | ||
expect(errors).toHaveLength(1) | ||
}) | ||
|
||
test('should drop scripts without src', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.defer': 'true', | ||
} | ||
const [scripts, errors] = parseScriptInjectionLabels(labels) | ||
expect(scripts).toHaveLength(0) | ||
expect(errors).toHaveLength(1) | ||
}) | ||
|
||
test('should support multiple scripts', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.defer': '1', | ||
'preevy.inject_script.widget2.src': 'https://my-script2', | ||
'preevy.inject_script.widget2.defer': 'false', | ||
'preevy.inject_script.widget3.src': 'https://my-script3', | ||
'preevy.inject_script.widget3.defer': '0', | ||
} | ||
const [scripts, errors] = parseScriptInjectionLabels(labels) | ||
expect(errors).toHaveLength(0) | ||
expect(scripts).toHaveLength(3) | ||
expect(scripts).toContainEqual( | ||
{ | ||
src: 'https://my-script', | ||
defer: true, | ||
}, | ||
) | ||
expect(scripts).toContainEqual( | ||
{ | ||
src: 'https://my-script2', | ||
defer: false, | ||
}, | ||
) | ||
expect(scripts).toContainEqual( | ||
{ | ||
src: 'https://my-script3', | ||
defer: false, | ||
}, | ||
) | ||
}) | ||
}) | ||
|
||
describe('scriptInjectionsToLabels', () => { | ||
const injections: Record<string, ScriptInjection> = { | ||
script1: { | ||
src: 'https://my-script', | ||
defer: true, | ||
async: false, | ||
pathRegex: /^aaa/, | ||
}, | ||
script2: { | ||
src: 'https://my-script2', | ||
}, | ||
} | ||
|
||
it('should convert to labels', () => { | ||
expect(scriptInjectionsToLabels(injections)).toMatchObject({ | ||
'preevy.inject_script.script1.src': 'https://my-script', | ||
'preevy.inject_script.script1.defer': 'true', | ||
'preevy.inject_script.script1.path_regex': '^aaa', | ||
'preevy.inject_script.script2.src': 'https://my-script2', | ||
}) | ||
}) | ||
}) | ||
}) |
76 changes: 76 additions & 0 deletions
76
packages/common/src/compose-tunnel-agent/script-injection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { groupBy, mapKeys, partition } from 'lodash' | ||
import { inspect } from 'util' | ||
import { COMPOSE_TUNNEL_AGENT_SERVICE_LABELS } from './labels' | ||
|
||
export type ScriptInjection = { | ||
pathRegex?: RegExp | ||
src: string | ||
defer?: boolean | ||
async?: boolean | ||
} | ||
|
||
const parseBooleanLabelValue = (s:string) => s === 'true' || s === '1' | ||
|
||
const parseScriptInjection = (o: Record<string, string>): ScriptInjection | Error => { | ||
// eslint-disable-next-line camelcase | ||
const { src, defer, async, path_regex } = o | ||
try { | ||
if (!src) { | ||
throw new Error('missing src') | ||
} | ||
return { | ||
// eslint-disable-next-line camelcase | ||
...path_regex && { pathRegex: new RegExp(path_regex) }, | ||
...defer && { defer: parseBooleanLabelValue(defer) }, | ||
...async && { async: parseBooleanLabelValue(async) }, | ||
src, | ||
} | ||
} catch (e) { | ||
return new Error(`error parsing script injection ${inspect(o)}: ${e}`, { cause: e }) | ||
} | ||
} | ||
|
||
const scriptInjectionToLabels = ( | ||
id: string, | ||
{ src, async, defer, pathRegex }: ScriptInjection, | ||
): Record<string, string> => mapKeys<Record<string, string>>({ | ||
src, | ||
...async && { async: 'true' }, | ||
...defer && { defer: 'true' }, | ||
...pathRegex && { path_regex: pathRegex.source }, | ||
}, (_value, key) => [COMPOSE_TUNNEL_AGENT_SERVICE_LABELS.INJECT_SCRIPT_PREFIX, id, key].join('.')) | ||
|
||
export const scriptInjectionsToLabels = ( | ||
injections: Record<string, ScriptInjection> | ||
) => Object.fromEntries( | ||
Object.entries(injections).flatMap(([id, injection]) => Object.entries(scriptInjectionToLabels(id, injection))) | ||
) | ||
|
||
const groupedLabelsRe = /^(?<prefix>[^\s]+)\.(?<id>[^.\s]+)\.(?<key>[^.\s]+)$/ | ||
type ParsedGroupedLabelKey = { prefix: string; id: string; key: string } | ||
const parseGroupedLabelKey = (key: string) => { | ||
const match = groupedLabelsRe.exec(key) | ||
return match && match.groups as ParsedGroupedLabelKey | ||
} | ||
|
||
const parseLabelsWithPrefixAndId = ( | ||
labels: Record<string, string>, | ||
prefix: string, | ||
): Record<string, string>[] => { | ||
const split: [ParsedGroupedLabelKey | null, string][] = Object.entries(labels) | ||
.map(([k, v]) => [parseGroupedLabelKey(k), v]) | ||
const filteredForPrefix = split.filter(([k]) => k?.prefix === prefix) as [ParsedGroupedLabelKey, string][] | ||
const grouped = groupBy(filteredForPrefix, ([{ id }]) => id) | ||
return Object.values(grouped).map(group => Object.fromEntries(group.map(([{ key }, value]) => [key, value]))) | ||
} | ||
|
||
export const parseScriptInjectionLabels = ( | ||
labels: Record<string, string>, | ||
): [ScriptInjection[], Error[]] => { | ||
const stringifiedInjections = parseLabelsWithPrefixAndId( | ||
labels, | ||
COMPOSE_TUNNEL_AGENT_SERVICE_LABELS.INJECT_SCRIPT_PREFIX, | ||
) | ||
const injectionOrErrors = stringifiedInjections.map(parseScriptInjection) | ||
return partition(injectionOrErrors, x => !(x instanceof Error)) as [ScriptInjection[], Error[]] | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.