Skip to content

Commit

Permalink
capabilities satisifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
bucko13 committed Feb 10, 2022
1 parent 4c75ea8 commit 159bd0b
Show file tree
Hide file tree
Showing 9 changed files with 1,398 additions and 1,388 deletions.
45 changes: 41 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,56 @@
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
"sourceType": "module",
"project": ["./tsconfig.json"]
},
"rules": {
"class-methods-use-this": 0,
"import/no-named-as-default": 0,
"require-atomic-updates": "warn",
"no-unused-vars": ["warn", { "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-unused-vars": ["warn", { "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-unused-vars": [
"warn",
{ "varsIgnorePattern": "^_" }
],
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/camelcase": [
"@typescript-eslint/naming-convention": [
"error",
{
"properties": "never"
"selector": "default",
"format": ["camelCase"],
"leadingUnderscore": "allow"
},

{
"selector": "variable",
"format": ["camelCase", "UPPER_CASE"]
},
{
"selector": "parameter",
"format": ["camelCase"],
"leadingUnderscore": "allow"
},

{
"selector": "memberLike",
"modifiers": ["private"],
"format": ["camelCase"],
"leadingUnderscore": "require"
},

{
"selector": "typeLike",
"format": ["PascalCase"]
},

{
"selector": "variable",
"format": ["PascalCase"],
"filter": {
"regex": "TextEncoder",
"match": true
}
}
],
"no-console": "error"
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@
"@types/chai": "^4.2.5",
"@types/mocha": "^5.2.7",
"@types/sinon": "^7.5.1",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^2.9.0",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"eslint": "6.1.0",
"eslint": "^8.8.0",
"eslint-config-prettier": "6.0.0",
"eslint-plugin-prettier": "3.1.0",
"mocha": "^6.2.2",
"mocha": "^9.2.0",
"nyc": "^15.0.0",
"prettier": "1.18.2",
"sinon": "^7.5.0",
"ts-mocha": "^6.0.0",
"tslint": "^5.20.1",
"typedoc": "^0.15.3",
"typedoc": "^0.22.11",
"typescript": "^3.7.2"
}
}
62 changes: 61 additions & 1 deletion src/satisfiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
InvalidServicesError,
SERVICES_CAVEAT_CONDITION,
decodeServicesCaveat,
InvalidCapabilitiesError,
SERVICE_CAPABILITIES_SUFFIX,
decodeCapabilitiesValue,
} from '.'

/**
Expand Down Expand Up @@ -44,24 +47,31 @@ export const createServicesSatisfier = (targetService: string): Satisfier => {
satisfyPrevious: (prev: Caveat, curr: Caveat): boolean => {
const prevServices = decodeServicesCaveat(prev.value.toString())
const currentServices = decodeServicesCaveat(curr.value.toString())
let previouslyAllowed = new Map()

// making typescript happy
if (!Array.isArray(prevServices) || !Array.isArray(currentServices))
throw new InvalidServicesError()

// Construct a set of the services we were previously
// allowed to access.
let previouslyAllowed = new Map()
previouslyAllowed = prevServices.reduce(
(prev, current) => prev.set(current.name, current.tier),
previouslyAllowed
)

// The caveat should not include any new services that
// weren't previously allowed.
for (const service of currentServices) {
if (!previouslyAllowed.has(service.name)) return false
// confirm that previous service tier cannot be higher than current
const prevTier: number = previouslyAllowed.get(service.name)
if (prevTier > service.tier) return false
}

return true
},

satisfyFinal: (caveat: Caveat): boolean => {
const services = decodeServicesCaveat(caveat.value.toString())
// making typescript happy
Expand All @@ -74,3 +84,53 @@ export const createServicesSatisfier = (targetService: string): Satisfier => {
},
}
}

export const createCapabilitiesSatisfier = (
service: string,
targetCapability: string
): Satisfier => {
// validate targetService
if (typeof targetCapability !== 'string') throw new InvalidCapabilitiesError()
if (typeof service !== 'string') throw new InvalidCapabilitiesError()

return {
condition: service + SERVICE_CAPABILITIES_SUFFIX,
satisfyPrevious: (prev: Caveat, curr: Caveat): boolean => {
const prevCapabilities = decodeCapabilitiesValue(prev.value.toString())
const currentCapabilities = decodeCapabilitiesValue(curr.value.toString())

// making typescript happy
if (
!Array.isArray(prevCapabilities) ||
!Array.isArray(currentCapabilities)
)
throw new InvalidServicesError()

// Construct a set of the service's capabilities we were
// previously allowed to access.
let previouslyAllowed = new Set()
previouslyAllowed = prevCapabilities.reduce(
(prev, current) => prev.add(current),
previouslyAllowed
)

// The caveat should not include any new service
// capabilities that weren't previously allowed.
for (const capability of currentCapabilities) {
if (!previouslyAllowed.has(capability)) return false
}

return true
},
satisfyFinal: (caveat: Caveat): boolean => {
const capabilities = decodeCapabilitiesValue(caveat.value.toString())
// making typescript happy
if (!Array.isArray(capabilities)) throw new InvalidServicesError()

for (const capability of capabilities) {
if (capability === targetCapability) return true
}
return false
},
}
}
8 changes: 8 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,11 @@ export const createNewCapabilitiesCaveat = (
comp: '=',
})
}

export const decodeCapabilitiesValue = (value: string): string[] => {
if (typeof value !== 'string') throw new InvalidCapabilitiesError()
return value
.toString()
.split(',')
.map((s: string) => s.trim())
}
Loading

0 comments on commit 159bd0b

Please sign in to comment.