Skip to content

Commit

Permalink
Merge pull request #46 from simple-ssi/jrayback_240520_refactor-funct…
Browse files Browse the repository at this point in the history
…ional-patterns

refactor functional patterns
  • Loading branch information
jrayback authored Jun 6, 2024
2 parents d3a2330 + 581967c commit aff5fad
Show file tree
Hide file tree
Showing 131 changed files with 1,220 additions and 784 deletions.
594 changes: 551 additions & 43 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@simple-ssi/simple-cesr",
"version": "0.2.1",
"version": "0.2.2",
"description": "A simple, limited, true-to-spec implementation of the CESR protocol.",
"type": "module",
"access": "public",
Expand All @@ -22,15 +22,16 @@
"dist/**/*"
],
"scripts": {
"test": "npx echo-cli '\\033[0;34mRunning unit tests...\\033[0m' && jest --testPathIgnorePatterns=standards.test.ts",
"clean": "npx echo-cli '\\033[0;34mDeleting old files...\\033[0m' && del-cli ./dist",
"build": "npx echo-cli '\\033[0;34mBuilding project...\\033[0m' && npm run clean && npm run build:esm && npm run build:cjs && npx echo-cli '\\n\\033[0;34mProject built.\\033[0m'",
"build:esm": "npx echo-cli '\\033[0;34mBuilding ECMAScript modules...\\033[0m' && tsc -p ./configs/tsconfig.esm.json && npx echo-cli '{\"type\": \"module\"}' > dist/esm/package.json",
"build:cjs": "npx echo-cli '\\033[0;34mBuilding CommonJS modules...\\033[0m' && tsc -p ./configs/tsconfig.cjs.json && npx echo-cli '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"formatter": "npx echo-cli '\\033[0;34mApplying TypeScript Standard style...\\033[0m' && ts-standard --fix",
"standards-test": "npx echo-cli '\\033[0;34mRunning standards test...\\033[0m' && jest ./test/standards/standards.test.ts",
"all-tests": "npx echo-cli '\\033[0;34mRunning all tests...\\033[0m' && npm test && npm run standards-test",
"full-build": "npx echo-cli '\\033[0;34mFULL BUILD.\\033[0m\\n' && npm run all-tests && npm run formatter && npm run build && echo-cli '\\n\\033[0;34mDONE.\\033[0m'",
"test": "jest --testPathIgnorePatterns=standards.test.ts",
"clean": "del-cli ./dist",
"build": "npm run clean && npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p ./configs/tsconfig.esm.json && echo '{\"type\": \"module\"}' > dist/esm/package.json",
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"formatter": "ts-standard --fix",
"find-unused-dependencies": "depcheck --ignores='buffer, depcheck, ts-jest, @types/jest, jest-ci-spec-reporter, echo-cli'",
"standards-test": "jest ./test/standards/standards.test.ts",
"all-tests": "npm test && npm run standards-test",
"full-build": "npm run all-tests && npm run formatter && npm run find-unused-dependencies && npm run build",
"prepack": "npm run full-build"
},
"repository": {
Expand All @@ -50,12 +51,11 @@
"devDependencies": {
"@types/jest": "^29.5.12",
"del-cli": "^5.0.0",
"depcheck": "^1.4.7",
"echo-cli": "^2.0.0",
"jest": "^29.7.0",
"jest-ci-spec-reporter": "^1.0.3",
"move-file-cli": "^3.0.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"ts-standard": "^12.0.2",
"typescript": "^4.7.4"
},
Expand Down
28 changes: 7 additions & 21 deletions src/api/encode/binary.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { Buffer } from 'buffer/index.js'
import { pipe } from '../../lib/util/pipe.js'
import { Code } from '../../core/code/code.js'
import { Binary } from '../../core/domain/binary.js'
import { encodeText } from './lib/encodeText.js'
import { encodeBinary } from './lib/encodeBinary.js'
import { extractRawFromRaw } from './lib/extractRawFromRaw.js'
import { Binary } from '../../core/domain/domains/binary.js'
import { convertToByteArray } from './steps/convertToByteArray.js'
import { text as makeText } from './text.js'

// encodes a primitive in the Binary domain. needs the type code and the primitive as a byte array

export const binary = (code: Code, primitive: Uint8Array): Binary => {
// helper functions
const makeRaw = extractRawFromRaw(code)
const asText = encodeText(code)
const asBinary = encodeBinary

// for Binary, go through all three steps...
return pipe(
Buffer.from(primitive), // internally, we use Buffers
makeRaw, // make the Raw
asText, // encode the raw primitive as Text
asBinary // return the encoded string as a Binary
)
}
export const binary = (code: Code, primitive: Uint8Array): Binary => pipe(
makeText(code, primitive),
convertToByteArray
)
4 changes: 0 additions & 4 deletions src/api/encode/lib/convertBase64ToBinary.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/api/encode/lib/convertBinaryToBase64.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/api/encode/lib/encodeBinary.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/api/encode/lib/encodeText.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/api/encode/lib/extractRawFromRaw.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/api/encode/lib/padUpFront.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/api/encode/lib/swapInTypeCode.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/api/encode/raw.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Buffer } from 'buffer/index.js'
import { make } from '../../implementation/make.js'
import { Code } from '../../core/code/code.js'
import { Raw } from '../../core/domain/raw.js'
import { Raw } from '../../core/domain/domains/raw.js'

export const raw = (code: Code, primitive: Uint8Array): Raw =>
make( // make() ensures the primitive is valididated
code,
Buffer.from(primitive)
)
export const raw = (code: Code, primitive: Uint8Array): Raw => make(code, Buffer.from(primitive)) // internally, we use Buffers
18 changes: 18 additions & 0 deletions src/api/encode/steps/addPadding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Buffer } from 'buffer/index.js'
import { PadSize, padSize as ps } from '../../../lib/keri/padSize.js'
import { exhaustive } from '../../../lib/util/exhaustive.js'

const prependWithNothing = (primitive: Buffer): Buffer => primitive
const prependWithZero = (primitive: Buffer): Buffer => Buffer.concat([Buffer.from([0]), primitive])
const prependWithTwoZeros = (primitive: Buffer): Buffer => Buffer.concat([Buffer.from([0, 0]), primitive])

export const addPadding = (primitive: Buffer): Buffer => {
const padSize: PadSize = ps(primitive.length)
return padSize === 0
? prependWithNothing(primitive)
: padSize === 1
? prependWithZero(primitive)
: padSize === 2
? prependWithTwoZeros(primitive)
: exhaustive(padSize)
}
4 changes: 4 additions & 0 deletions src/api/encode/steps/convertToByteArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Buffer } from 'buffer/index.js'
import { base64UrlToBuffer } from '../../../lib/convertors/base64UrlToBuffer.js'

export const convertToByteArray = (base64Url: string): Buffer => base64UrlToBuffer(base64Url)
4 changes: 4 additions & 0 deletions src/api/encode/steps/convertToUrlSafeBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Buffer } from 'buffer/index.js'
import { bufferToBase64URL } from '../../../lib/convertors/bufferToBase64URL.js'

export const convertToUrlSafeBase64 = (bytes: Buffer): string => bufferToBase64URL(bytes)
19 changes: 19 additions & 0 deletions src/api/encode/steps/swapInTextCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Code, CodeLength } from '../../../core/code/code.js'
import { exhaustive } from '../../../lib/util/exhaustive.js'

type Swapper = (text: string) => string

const replaceFirstCharacter = (code: Code, base64: string): string => code + base64.substring(1)
const replaceFirstTwoCharacters = (code: Code, base64: string): string => code + base64.substring(2)
const simplyPrependFourCharacterCode = (code: Code, base64: string): string => code + base64

export const swapInTextCode = (code: Code): Swapper => {
const length = code.length as CodeLength
return length === 1
? (base64: string) => replaceFirstCharacter(code, base64)
: length === 2
? (base64: string) => replaceFirstTwoCharacters(code, base64)
: length === 4
? (base64: string) => simplyPrependFourCharacterCode(code, base64)
: exhaustive(length) // ensure exhaustiveness
}
27 changes: 12 additions & 15 deletions src/api/encode/text.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Buffer } from 'buffer/index.js'
import { pipe } from '../../lib/util/pipe.js'
import { Code } from '../../core/code/code.js'
import { Text } from '../../core/domain/text.js'
import { encodeText } from './lib/encodeText.js'
import { extractRawFromRaw } from './lib/extractRawFromRaw.js'
import { Text } from '../../core/domain/domains/text.js'
import { make as makeRaw } from '../../implementation/make.js'
import { addPadding } from './steps/addPadding.js'
import { convertToUrlSafeBase64 } from './steps/convertToUrlSafeBase64.js'
import { swapInTextCode } from './steps/swapInTextCode.js'

export const text = (code: Code, primitive: Uint8Array): Text => {
// helper functions
const makeRaw = extractRawFromRaw(code)
const asText = encodeText(code)

// requires two steps...
return pipe(
Buffer.from(primitive), // convert bytes to Buffer -> internally, we use Buffers
makeRaw, // make the Raw first, then...
asText // ...encode the it as Text
export const text = (code: Code, primitive: Uint8Array): Text =>
pipe(
makeRaw(code, primitive)
.raw, // call make() to ensure the primitive is valid, then re-extract the primitive
addPadding,
convertToUrlSafeBase64,
swapInTextCode(code)
)
}
10 changes: 0 additions & 10 deletions src/api/transform/lib/splitIntoTuple.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/api/transform/lib/trimBufferBytes.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/api/transform/steps/convertToByteArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Buffer } from 'buffer/index.js'
import { base64UrlToBuffer } from '../../../lib/convertors/base64UrlToBuffer.js'

export const convertToByteArray = (base64: string): Buffer => base64UrlToBuffer(base64)
5 changes: 5 additions & 0 deletions src/api/transform/steps/produceTheRaw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Code } from '../../../core/code/code.js'
import { Raw } from '../../../core/domain/domains/raw.js'
import { make } from '../../../implementation/make.js'

export const produceTheRaw = (code: Code) => (primitive: Buffer): Raw => make(code, primitive)
19 changes: 19 additions & 0 deletions src/api/transform/steps/readCodeFromText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { readCodeFromText } from './readCodeFromText.js'

describe('Read Code From Text', () => {
it('handles code length of one', () => {
const text = 'MGrj'
const actual = readCodeFromText(text)
expect(actual).toStrictEqual('M')
})
it('handles code length of two', () => {
const text = '0HGrjk23'
const actual = readCodeFromText(text)
expect(actual).toStrictEqual('0H')
})
it('handles code length of four', () => {
const text = '1AAFRkJZ'
const actual = readCodeFromText(text)
expect(actual).toStrictEqual('1AAF')
})
})
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
// import { match } from 'ts-pattern'
import { Text } from '../../../core/domain/text.js'
import { Text } from '../../../core/domain/domains/text.js'
import { Code } from '../../../core/code/code.js'
import { exhaustive } from '../../../lib/util/exhaustive.js'
import { Selector } from '../../../core/selector/selector.js'

const readOneCharCode = (text: Text): Code => text[0] as Code
const readTwoCharCode = (text: Text): Code => text.substring(0, 2) as Code
const readFourCharCode = (text: Text): Code => text.substring(0, 4) as Code

// reads the code characters from the Text
// returns a transitional object to be further processed
export const readCodeFromText = (textDomain: Text): Code => {
const numChars = textDomain[0].match(/[A-Za-z]/) != null
? 1
: textDomain[0] === '0'
? 2
: 4
switch (numChars) {
case 1:
return readOneCharCode(textDomain)
case 2:
return readTwoCharCode(textDomain)
case 4:
return readFourCharCode(textDomain)
}
return exhaustive(numChars)
const selector = textDomain[0] as Selector
return selector !== '0' && selector !== '1'
? readOneCharCode(textDomain)
: selector === '0'
? readTwoCharCode(textDomain)
: selector === '1'
? readFourCharCode(textDomain)
: exhaustive(selector)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Buffer } from 'buffer/index.js'
import { trimBufferBytes } from './trimBufferBytes.js'
import { removePadding } from './removePadding.js'

describe('Trim Bytes', () => {
it('handles code length of one', () => {
const trimOneByte = trimBufferBytes(1)
const trimOneByte = removePadding(1)
const bin = Buffer.from([0x30, 0x00, 0x01])
const actual = trimOneByte(bin)
expect(actual).toStrictEqual(Buffer.from([0x00, 0x01]))
})
it('handles code length of two', () => {
const trimTwoBytes = trimBufferBytes(2)
const trimTwoBytes = removePadding(2)
const bin = Buffer.from([0xd0, 0x70, 0x00, 0x00, 0x00, 0x01])
const actual = trimTwoBytes(bin)
expect(actual).toStrictEqual(Buffer.from([0x00, 0x00, 0x00, 0x01]))
})
it('handles code length of four', () => {
const trimThreeBytes = trimBufferBytes(4)
const trimThreeBytes = removePadding(4)
const bin = Buffer.from([0xd4, 0x00, 0x05, 0x00, 0x00, 0x01])
const actual = trimThreeBytes(bin)
expect(actual).toStrictEqual(Buffer.from([0x00, 0x00, 0x01]))
Expand Down
Loading

0 comments on commit aff5fad

Please # to comment.