[ci] format

This commit is contained in:
tony-sull 2022-09-22 19:50:47 +00:00 committed by fredkbot
parent fad3867adb
commit 426f3bb40f
19 changed files with 166 additions and 159 deletions

View file

@ -3,8 +3,8 @@ import { ssgBuild } from './build/ssg.js';
import type { ImageService, SSRImageService, TransformOptions } from './loaders/index.js'; import type { ImageService, SSRImageService, TransformOptions } from './loaders/index.js';
import type { LoggerLevel } from './utils/logger.js'; import type { LoggerLevel } from './utils/logger.js';
import { joinPaths, prependForwardSlash, propsToFilename } from './utils/paths.js'; import { joinPaths, prependForwardSlash, propsToFilename } from './utils/paths.js';
import { createPlugin } from './vite-plugin-astro-image.js';
import { copyWasmFiles } from './vendor/squoosh/copy-wasm.js'; import { copyWasmFiles } from './vendor/squoosh/copy-wasm.js';
import { createPlugin } from './vite-plugin-astro-image.js';
export { getImage } from './lib/get-image.js'; export { getImage } from './lib/get-image.js';
export { getPicture } from './lib/get-picture.js'; export { getPicture } from './lib/get-picture.js';
@ -48,19 +48,17 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
return { return {
plugins: [createPlugin(_config, resolvedOptions)], plugins: [createPlugin(_config, resolvedOptions)],
optimizeDeps: { optimizeDeps: {
include: [ include: ['image-size'].filter(Boolean),
'image-size',
].filter(Boolean),
}, },
build: { build: {
rollupOptions: { rollupOptions: {
external: ["sharp"] external: ['sharp'],
} },
}, },
ssr: { ssr: {
noExternal: ['@astrojs/image', resolvedOptions.serviceEntryPoint], noExternal: ['@astrojs/image', resolvedOptions.serviceEntryPoint],
}, },
assetsInclude: ['**/*.wasm'] assetsInclude: ['**/*.wasm'],
}; };
} }
@ -78,18 +76,19 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
entryPoint: '@astrojs/image/endpoint', entryPoint: '@astrojs/image/endpoint',
}); });
} }
const { default: defaultLoader } = await import(resolvedOptions.serviceEntryPoint === '@astrojs/image/sharp' const { default: defaultLoader } = await import(
? './loaders/sharp.js' resolvedOptions.serviceEntryPoint === '@astrojs/image/sharp'
: './loaders/squoosh.js' ? './loaders/sharp.js'
: './loaders/squoosh.js'
); );
globalThis.astroImage = { globalThis.astroImage = {
defaultLoader defaultLoader,
} };
}, },
'astro:build:start': async ({ buildConfig }) => { 'astro:build:start': async ({ buildConfig }) => {
_buildConfig = buildConfig _buildConfig = buildConfig;
}, },
'astro:build:setup': async () => { 'astro:build:setup': async () => {
// Used to cache all images rendered to HTML // Used to cache all images rendered to HTML
@ -141,7 +140,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
if (resolvedOptions.serviceEntryPoint === '@astrojs/image/squoosh') { if (resolvedOptions.serviceEntryPoint === '@astrojs/image/squoosh') {
await copyWasmFiles(_buildConfig.server); await copyWasmFiles(_buildConfig.server);
} }
} },
}, },
}; };
} }

View file

@ -1,9 +1,9 @@
/// <reference types="astro/astro-jsx" /> /// <reference types="astro/astro-jsx" />
import mime from 'mime'; import mime from 'mime';
import { OutputFormat, parseAspectRatio, TransformOptions } from '../loaders/index.js'; import { OutputFormat, parseAspectRatio, TransformOptions } from '../loaders/index.js';
import { extname } from '../utils/paths.js';
import { ImageMetadata } from '../vite-plugin-astro-image.js'; import { ImageMetadata } from '../vite-plugin-astro-image.js';
import { getImage } from './get-image.js'; import { getImage } from './get-image.js';
import { extname } from '../utils/paths.js';
export interface GetPictureParams { export interface GetPictureParams {
src: string | ImageMetadata | Promise<{ default: ImageMetadata }>; src: string | ImageMetadata | Promise<{ default: ImageMetadata }>;

View file

@ -1,4 +1,3 @@
import { AstroConfig } from 'astro';
import { htmlColorNames, type NamedColor } from '../utils/colornames.js'; import { htmlColorNames, type NamedColor } from '../utils/colornames.js';
/// <reference types="astro/astro-jsx" /> /// <reference types="astro/astro-jsx" />
@ -299,5 +298,8 @@ export abstract class BaseSSRService implements SSRImageService {
return transform; return transform;
} }
abstract transform(inputBuffer: Buffer, transform: TransformOptions): Promise<{ data: Buffer, format: OutputFormat }>; abstract transform(
inputBuffer: Buffer,
transform: TransformOptions
): Promise<{ data: Buffer; format: OutputFormat }>;
} }

View file

@ -1,6 +1,6 @@
import sharp from 'sharp'; import sharp from 'sharp';
import { BaseSSRService, isOutputFormatSupportsAlpha } from '../loaders/index.js';
import type { SSRImageService } from '../loaders/index.js'; import type { SSRImageService } from '../loaders/index.js';
import { BaseSSRService, isOutputFormatSupportsAlpha } from '../loaders/index.js';
import type { OutputFormat, TransformOptions } from './index.js'; import type { OutputFormat, TransformOptions } from './index.js';
class SharpService extends BaseSSRService { class SharpService extends BaseSSRService {

View file

@ -1,12 +1,12 @@
// @ts-ignore // @ts-ignore
import { red } from 'kleur/colors'; import { red } from 'kleur/colors';
import { BaseSSRService } from './index.js';
import { error } from '../utils/logger.js'; import { error } from '../utils/logger.js';
import { metadata } from '../utils/metadata.js'; import { metadata } from '../utils/metadata.js';
import { isRemoteImage } from '../utils/paths.js'; import { isRemoteImage } from '../utils/paths.js';
import type { OutputFormat, TransformOptions } from './index.js';
import { processBuffer } from '../vendor/squoosh/image-pool.js'; import { processBuffer } from '../vendor/squoosh/image-pool.js';
import type { Operation } from '../vendor/squoosh/image.js'; import type { Operation } from '../vendor/squoosh/image.js';
import type { OutputFormat, TransformOptions } from './index.js';
import { BaseSSRService } from './index.js';
class SquooshService extends BaseSSRService { class SquooshService extends BaseSSRService {
async processAvif(image: any, transform: TransformOptions) { async processAvif(image: any, transform: TransformOptions) {
@ -58,7 +58,10 @@ class SquooshService extends BaseSSRService {
}; };
} }
async autorotate(transform: TransformOptions, inputBuffer: Buffer): Promise<Operation | undefined> { async autorotate(
transform: TransformOptions,
inputBuffer: Buffer
): Promise<Operation | undefined> {
// check EXIF orientation data and rotate the image if needed // check EXIF orientation data and rotate the image if needed
try { try {
const meta = await metadata(transform.src, inputBuffer); const meta = await metadata(transform.src, inputBuffer);
@ -74,15 +77,15 @@ class SquooshService extends BaseSSRService {
case 8: case 8:
return { type: 'rotate', numRotations: 3 }; return { type: 'rotate', numRotations: 3 };
} }
} catch { } } catch {}
} }
async transform(inputBuffer: Buffer, transform: TransformOptions) { async transform(inputBuffer: Buffer, transform: TransformOptions) {
const operations: Operation[] = []; const operations: Operation[] = [];
if (!isRemoteImage(transform.src)) { if (!isRemoteImage(transform.src)) {
const autorotate = await this.autorotate(transform, inputBuffer) const autorotate = await this.autorotate(transform, inputBuffer);
if (autorotate) { if (autorotate) {
operations.push(autorotate); operations.push(autorotate);
} }
@ -96,7 +99,7 @@ class SquooshService extends BaseSSRService {
type: 'resize', type: 'resize',
width, width,
height, height,
}) });
} }
if (!transform.format) { if (!transform.format) {
@ -108,12 +111,17 @@ class SquooshService extends BaseSSRService {
throw new Error(`Unknown image output: "${transform.format}" used for ${transform.src}`); throw new Error(`Unknown image output: "${transform.format}" used for ${transform.src}`);
} }
const data = await processBuffer(inputBuffer, operations, transform.format, transform.quality || 100); const data = await processBuffer(
inputBuffer,
operations,
transform.format,
transform.quality || 100
);
return { return {
data: Buffer.from(data), data: Buffer.from(data),
format: transform.format format: transform.format,
} };
} }
} }

View file

@ -1,14 +1,12 @@
export default function execOnce<T extends (...args: any[]) => ReturnType<T>>( export default function execOnce<T extends (...args: any[]) => ReturnType<T>>(fn: T): T {
fn: T let used = false;
): T { let result: ReturnType<T>;
let used = false
let result: ReturnType<T>
return ((...args: any[]) => { return ((...args: any[]) => {
if (!used) { if (!used) {
used = true used = true;
result = fn(...args) result = fn(...args);
} }
return result return result;
}) as T }) as T;
} }

View file

@ -9,7 +9,7 @@ export interface Metadata extends ImageMetadata {
} }
export async function metadata(src: URL | string, data?: Buffer): Promise<Metadata | undefined> { export async function metadata(src: URL | string, data?: Buffer): Promise<Metadata | undefined> {
const file = data || await fs.readFile(src); const file = data || (await fs.readFile(src));
const { width, height, type, orientation } = await sizeOf(file); const { width, height, type, orientation } = await sizeOf(file);
const isPortrait = (orientation || 0) >= 5; const isPortrait = (orientation || 0) >= 5;

View file

@ -1,4 +1,4 @@
import { OutputFormat, TransformOptions } from '../loaders/index.js'; import { TransformOptions } from '../loaders/index.js';
import { shorthash } from './shorthash.js'; import { shorthash } from './shorthash.js';
export function isRemoteImage(src: string) { export function isRemoteImage(src: string) {

View file

@ -1,125 +1,123 @@
/* tslint-disable ban-types */ /* tslint-disable ban-types */
import { Worker, parentPort } from 'worker_threads';
import { TransformStream } from 'web-streams-polyfill'; import { TransformStream } from 'web-streams-polyfill';
import { parentPort, Worker } from 'worker_threads';
function uuid() { function uuid() {
return Array.from({ length: 16 }, () => return Array.from({ length: 16 }, () => Math.floor(Math.random() * 256).toString(16)).join('');
Math.floor(Math.random() * 256).toString(16),
).join('');
} }
interface Job<I> { interface Job<I> {
msg: I; msg: I;
resolve: (result: any) => void; resolve: (result: any) => void;
reject: (reason: any) => void; reject: (reason: any) => void;
} }
export default class WorkerPool<I, O> { export default class WorkerPool<I, O> {
public numWorkers: number; public numWorkers: number;
public jobQueue: TransformStream<Job<I>, Job<I>>; public jobQueue: TransformStream<Job<I>, Job<I>>;
public workerQueue: TransformStream<Worker, Worker>; public workerQueue: TransformStream<Worker, Worker>;
public done: Promise<void>; public done: Promise<void>;
constructor(numWorkers: number, workerFile: string) { constructor(numWorkers: number, workerFile: string) {
this.numWorkers = numWorkers; this.numWorkers = numWorkers;
this.jobQueue = new TransformStream(); this.jobQueue = new TransformStream();
this.workerQueue = new TransformStream(); this.workerQueue = new TransformStream();
const writer = this.workerQueue.writable.getWriter(); const writer = this.workerQueue.writable.getWriter();
for (let i = 0; i < numWorkers; i++) { for (let i = 0; i < numWorkers; i++) {
writer.write(new Worker(workerFile)); writer.write(new Worker(workerFile));
} }
writer.releaseLock(); writer.releaseLock();
this.done = this._readLoop(); this.done = this._readLoop();
} }
async _readLoop() { async _readLoop() {
const reader = this.jobQueue.readable.getReader(); const reader = this.jobQueue.readable.getReader();
while (true) { while (true) {
const { value, done } = await reader.read(); const { value, done } = await reader.read();
if (done) { if (done) {
await this._terminateAll(); await this._terminateAll();
return; return;
} }
if (!value) { if (!value) {
throw new Error('Reader did not return any value'); throw new Error('Reader did not return any value');
} }
const { msg, resolve, reject } = value; const { msg, resolve, reject } = value;
const worker = await this._nextWorker(); const worker = await this._nextWorker();
this.jobPromise(worker, msg) this.jobPromise(worker, msg)
.then((result) => resolve(result)) .then((result) => resolve(result))
.catch((reason) => reject(reason)) .catch((reason) => reject(reason))
.finally(() => { .finally(() => {
// Return the worker to the pool // Return the worker to the pool
const writer = this.workerQueue.writable.getWriter(); const writer = this.workerQueue.writable.getWriter();
writer.write(worker); writer.write(worker);
writer.releaseLock(); writer.releaseLock();
}); });
} }
} }
async _nextWorker() { async _nextWorker() {
const reader = this.workerQueue.readable.getReader(); const reader = this.workerQueue.readable.getReader();
const { value } = await reader.read(); const { value } = await reader.read();
reader.releaseLock(); reader.releaseLock();
if (!value) { if (!value) {
throw new Error('No worker left'); throw new Error('No worker left');
} }
return value; return value;
} }
async _terminateAll() { async _terminateAll() {
for (let n = 0; n < this.numWorkers; n++) { for (let n = 0; n < this.numWorkers; n++) {
const worker = await this._nextWorker(); const worker = await this._nextWorker();
worker.terminate(); worker.terminate();
} }
this.workerQueue.writable.close(); this.workerQueue.writable.close();
} }
async join() { async join() {
this.jobQueue.writable.getWriter().close(); this.jobQueue.writable.getWriter().close();
await this.done; await this.done;
} }
dispatchJob(msg: I): Promise<O> { dispatchJob(msg: I): Promise<O> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const writer = this.jobQueue.writable.getWriter(); const writer = this.jobQueue.writable.getWriter();
writer.write({ msg, resolve, reject }); writer.write({ msg, resolve, reject });
writer.releaseLock(); writer.releaseLock();
}); });
} }
private jobPromise(worker: Worker, msg: I) { private jobPromise(worker: Worker, msg: I) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const id = uuid(); const id = uuid();
worker.postMessage({ msg, id }); worker.postMessage({ msg, id });
worker.on('message', function f({ error, result, id: rid }) { worker.on('message', function f({ error, result, id: rid }) {
if (rid !== id) { if (rid !== id) {
return; return;
} }
if (error) { if (error) {
reject(error); reject(error);
return; return;
} }
worker.off('message', f); worker.off('message', f);
resolve(result); resolve(result);
}); });
}); });
} }
static useThisThreadAsWorker<I, O>(cb: (msg: I) => O) { static useThisThreadAsWorker<I, O>(cb: (msg: I) => O) {
parentPort!.on('message', async (data) => { parentPort!.on('message', async (data) => {
const { msg, id } = data; const { msg, id } = data;
try { try {
const result = await cb(msg); const result = await cb(msg);
parentPort!.postMessage({ result, id }); parentPort!.postMessage({ result, id });
} catch (e: any) { } catch (e: any) {
parentPort!.postMessage({ error: e.message, id }); parentPort!.postMessage({ error: e.message, id });
} }
}); });
} }
} }

View file

@ -1,8 +1,8 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { createRequire } from 'module'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dirname } from '../emscripten-utils.js'; import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {
return function (Module) { return function (Module) {

View file

@ -1,8 +1,8 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { createRequire } from 'module'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dirname } from '../emscripten-utils.js'; import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {
return function (Module) { return function (Module) {

View file

@ -83,7 +83,7 @@ const resizeInit = () => resize.default(fsp.readFile(pathify(resizeWasm.toString
const rotateWasm = new URL('./rotate/rotate.wasm', import.meta.url) const rotateWasm = new URL('./rotate/rotate.wasm', import.meta.url)
// Our decoders currently rely on a `ImageData` global. // Our decoders currently rely on a `ImageData` global.
import ImageData from './image_data.js'; import ImageData from './image_data.js'
(global as any).ImageData = ImageData (global as any).ImageData = ImageData
function resizeNameToIndex( function resizeNameToIndex(

View file

@ -1,10 +1,10 @@
import { cpus } from 'os'
import { isMainThread } from 'node:worker_threads'; import { isMainThread } from 'node:worker_threads';
import { cpus } from 'os';
import type { OutputFormat } from '../../loaders/index.js';
import execOnce from '../../utils/execOnce.js';
import WorkerPool from '../../utils/workerPool.js'; import WorkerPool from '../../utils/workerPool.js';
import type { Operation } from './image.js'; import type { Operation } from './image.js';
import * as impl from './impl.js'; import * as impl from './impl.js';
import execOnce from '../../utils/execOnce.js';
import type { OutputFormat } from '../../loaders/index.js';
const getWorker = execOnce( const getWorker = execOnce(
() => { () => {

View file

@ -1,5 +1,5 @@
import * as impl from './impl.js';
import type { OutputFormat } from '../../loaders/index.js'; import type { OutputFormat } from '../../loaders/index.js';
import * as impl from './impl.js';
type RotateOperation = { type RotateOperation = {
type: 'rotate' type: 'rotate'

View file

@ -1,7 +1,7 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { dirname } from '../emscripten-utils.js';
import { createRequire } from 'module'; import { createRequire } from 'module';
import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {

View file

@ -1,8 +1,8 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { createRequire } from 'module'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dirname } from '../emscripten-utils.js'; import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {
return function (Module) { return function (Module) {

View file

@ -1,8 +1,8 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { createRequire } from 'module'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dirname } from '../emscripten-utils.js'; import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {
return function (Module) { return function (Module) {

View file

@ -1,8 +1,8 @@
/* eslint-disable */ /* eslint-disable */
// @ts-nocheck // @ts-nocheck
import { createRequire } from 'module'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dirname } from '../emscripten-utils.js'; import { dirname } from '../emscripten-utils.js';
const require = createRequire(import.meta.url);
var Module = (function () { var Module = (function () {
return function (Module) { return function (Module) {

View file

@ -89,7 +89,9 @@ export function createPlugin(config: AstroConfig, options: Required<IntegrationO
return next(); return next();
} }
const transform = await globalThis.astroImage.defaultLoader.parseTransform(url.searchParams); const transform = await globalThis.astroImage.defaultLoader.parseTransform(
url.searchParams
);
if (!transform) { if (!transform) {
return next(); return next();