diff --git a/packages/core/src/apply-transforms.ts b/packages/core/src/apply-transforms.ts deleted file mode 100644 index 59942168..00000000 --- a/packages/core/src/apply-transforms.ts +++ /dev/null @@ -1,24 +0,0 @@ -import sharp from "sharp"; -import { ImageTransformation } from "./types"; - -interface TransformResult { - data: Uint8Array - info: Record -} - -export async function applyTransforms(transforms: ImageTransformation[], src: URL):Promise { - 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) - - resolve({ - data, - info - }) - }) - }) -} \ No newline at end of file diff --git a/packages/core/src/generate-configs.ts b/packages/core/src/generate-configs.ts deleted file mode 100644 index 9452a15c..00000000 --- a/packages/core/src/generate-configs.ts +++ /dev/null @@ -1,26 +0,0 @@ -/** - * This function calculates the cartesian product of two or more array and is straight from stackoverflow ;) - * Should be replaced with something more legible but works for now. - * @internal - */ -export const cartesian = (...a: any[]) => a.reduce((a: any, b: any) => a.flatMap((d: any) => b.map((e: any) => [d, e].flat()))) - -/** - * 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)) -} \ No newline at end of file diff --git a/packages/core/src/generate-transforms.ts b/packages/core/src/generate-transforms.ts deleted file mode 100644 index fbfb242d..00000000 --- a/packages/core/src/generate-transforms.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Directive, DirectiveContext, ImageConfig, ImageTransformation } from "./types"; - -export function generateTransforms(config: ImageConfig, directives: Directive[]) { - const transforms: ImageTransformation[] = [] - const metadata: Record = {} - const parametersUsed = new Set() - const warnings = new Set() - - const context: DirectiveContext = { - useParam: k => parametersUsed.add(k), - addMetadata: (k, v) => metadata[k] = v, - warn: m => warnings.add(m) - } - - for (const directive of directives) { - const transform = directive(config, context) - - if(typeof transform === 'function') transforms.push(transform) - } - - return { - transforms, - metadata, - parametersUsed, - warnings - } -} \ No newline at end of file diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 12f7028b..ad8d286c 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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' @@ -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) { + const transforms: ImageTransformation[] = [] + const metadata: Record = {} + const parametersUsed = new Set() + 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 +} + +export async function applyTransforms(transforms: ImageTransformation[], image: Sharp): Promise { + // 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' \ No newline at end of file + resolve({ + data, + info + }) + }) + }) +} \ No newline at end of file diff --git a/packages/core/src/parse-url.ts b/packages/core/src/parse-url.ts deleted file mode 100644 index 21f9f3be..00000000 --- a/packages/core/src/parse-url.ts +++ /dev/null @@ -1,9 +0,0 @@ -export function parseURL(url: URL) { - const entries: Array<[string, string[]]> = [] - - for (const [key, value] of url.searchParams) { - entries.push([key, value.split(';')]) - } - - return entries -} \ No newline at end of file