Skip to content

Commit

Permalink
fix: consolidate functions into main
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 15, 2021
1 parent 37c0320 commit 0772934
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 90 deletions.
24 changes: 0 additions & 24 deletions packages/core/src/apply-transforms.ts

This file was deleted.

26 changes: 0 additions & 26 deletions packages/core/src/generate-configs.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/core/src/generate-transforms.ts

This file was deleted.

89 changes: 85 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { cartesian } from './util'
import { Sharp } from 'sharp';
import { Directive, DirectiveContext, ImageConfig, ImageTransformation } from "./types";

export * from './directives/background'
export * from './directives/blur'
export * from './directives/fit'
Expand All @@ -19,8 +23,85 @@ export * from './directives/rotate'
export * from './directives/tint'

export * from './builtins'
export * as cache from './cache'
export { loadImageFromDisk, loadImageFromBuffer } from './util'
export * from './types'

export function parseURL(url: URL) {
const entries: Array<[string, string[]]> = []

for (const [key, value] of url.searchParams) {
entries.push([key, value.split(';')])
}

return entries
}

/**
* This function builds up all possible combinations the given entries can be combined
* an returns it as an array of objects that can be given to a the directives.
* @param entries The url parameter entries
* @returns An array of directive options
*/
export function generateConfigs(entries: Array<[string,string[]]>) {

// create a new array of entries for each argument
const singleArgumentEntries = entries
.map(([key, values]) => values.map<[[string, string]]>(v => [[key, v]]))

// // do a cartesian product on all entries to get all combainations we need to produce
const combinations = singleArgumentEntries
.reduce((prev, cur) => prev.length ? cartesian(prev, cur) : cur, [])

// // and return as an array of objects
return combinations.map((options) => Object.fromEntries(options))
}


export function generateTransforms(config: ImageConfig, directives: Record<string, Directive>) {
const transforms: ImageTransformation[] = []
const metadata: Record<string, any> = {}
const parametersUsed = new Set<string>()
const warnings = new Set<{ directive: string, message: string }>()

for (const [name, directive] of Object.entries(directives)) {
const context: DirectiveContext = {
useParam: k => parametersUsed.add(k),
addMetadata: (k, v) => metadata[k] = v,
warn: m => warnings.add({ directive: name, message: m })
}

const transform = directive(config, context)

if (typeof transform === 'function') transforms.push(transform)
}

return {
transforms,
metadata,
parametersUsed,
warnings
}
}

interface TransformResult {
data: Uint8Array
info: Record<string, any>
}

export async function applyTransforms(transforms: ImageTransformation[], image: Sharp): Promise<TransformResult> {
// let image = sharp(src.pathname)

image = transforms.reduce((img, transform) => transform(img), image)

return new Promise((resolve, reject) => {
image.toBuffer((err, data, info) => {
if (err) reject(err)

export { parseURL } from './parse-url'
export { generateConfigs } from './generate-configs'
export { generateTransforms } from './generate-transforms'
export { applyTransforms } from './apply-transforms'
resolve({
data,
info
})
})
})
}
9 changes: 0 additions & 9 deletions packages/core/src/parse-url.ts

This file was deleted.

0 comments on commit 0772934

Please sign in to comment.