This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant.ts
More file actions
183 lines (168 loc) · 5.05 KB
/
variant.ts
File metadata and controls
183 lines (168 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable max-classes-per-file */
/* eslint-disable no-underscore-dangle */
import { promises as fs } from 'fs';
import path from 'path';
import {
open, hashB64u, dimensions as getDimensions, getType,
} from './util.js';
import type { RepackTypes } from './types.js';
const B64_HASH_LIMIT = 5;
const {
mkdir, writeFile, access, link: hardlink,
} = fs;
export class Asset {
public source: string; // url or filename
public hash?: string;
public type?: RepackTypes;
public width?: number;
public height?: number;
protected _data?: Promise<Buffer>;
constructor({
source, type, hash, width, height, data,
}: Omit<Asset, 'data' | 'toJSON'> & { data?: Asset['data'] | Buffer }) {
this.source = source;
this.type = type;
this.hash = hash;
this.width = width;
this.height = height;
if (Buffer.isBuffer(data)) {
this.data = Promise.resolve(data);
} else if (typeof data !== 'undefined') {
this.data = data;
}
}
public get data() {
if (typeof this._data === 'undefined') {
this._data = open(this.source).then(({ data }) => data);
}
return this._data;
}
public set data(data) {
if (Buffer.isBuffer(data)) {
this._data = Promise.resolve(data);
}
this._data = data;
}
public toJSON() {
return {
source: this.source,
hash: this.hash,
type: this.type,
width: this.width,
height: this.height,
};
}
}
export class Variant extends Asset {
public variant: string;
public destDir: string;
public baseUri: string;
public hash: string;
constructor({
variant,
destDir,
baseUri,
...options
}: Omit<Variant, 'data' | 'uri' | 'toJSON' | 'localFilePath'> & { data?: Variant['data'] | Buffer } & { hash: string }) {
super(options);
this.hash = options.hash;
this.variant = variant;
this.destDir = destDir;
this.baseUri = baseUri;
}
public get data() {
if (typeof this._data === 'undefined') {
this._data = open(this.localFilePath).then(({ data }) => data);
}
return this._data;
}
public set data(data) {
super.data = data;
}
public get uri() {
return path.join(this.baseUri, this.hashBasename);
}
protected get hashBasename() {
if (this.type) {
return `${this.hash}.${this.type}`;
}
return this.hash;
}
public get localFilePath() {
return path.join(this.destDir, this.hashBasename);
}
public toJSON() {
return {
...super.toJSON(),
variant: this.variant,
};
}
}
export type AssetFromCache = Pick<Asset, 'source' | 'hash'> & Partial<Pick<Asset, 'type' | 'width' | 'height'>>;
export type AssetFromFresh = Pick<Asset, 'source'> & Partial<Pick<Asset, 'type' | 'width' | 'height'>>;
export interface AssetInitializer {
(input: AssetFromCache | AssetFromFresh): Promise<Asset>;
}
export interface VariantConfig {
destDir: Variant['destDir'];
baseUri: Variant['baseUri'];
}
export type VariantFromCache = Pick<Variant, 'source' | 'variant' | 'hash'> & Partial<Pick<Variant, 'type' | 'width' | 'height'>>;
export type VariantFromFresh = Pick<Variant, 'source' | 'variant' | 'data'> & Partial<Pick<Variant, 'type' | 'width' | 'height'>>;
export interface VariantInitializer {
(input: (VariantFromCache | VariantFromFresh) & { filename?: string }): Promise<Variant>;
}
export interface VariantFactory {
(config: VariantConfig): VariantInitializer;
}
const isFromCache = <T extends VariantFromCache | AssetFromCache>(input: any): input is T => {
if ('hash' in input && typeof input.hash !== 'undefined') {
return true;
}
return false;
};
export const asset: AssetInitializer = async (input) => {
if (isFromCache(input)) {
return new Asset(input);
}
// return new Asset({ source: input.source, ...await open(input.source) });
return new Asset({ source: input.source, type: await getType(input.source) });
};
export const variant: VariantFactory = (config) => async (input) => {
if (isFromCache(input)) {
if (typeof input.filename === 'string') {
const hash = input.hash.slice(0, B64_HASH_LIMIT);
const result = new Variant({
...config, ...input, hash,
});
try {
await access(result.localFilePath);
} catch (error) {
await mkdir(config.destDir, { recursive: true });
await hardlink(input.filename, result.localFilePath).catch(() => { });
}
return result;
}
return new Variant({ ...config, ...input });
}
const data = Buffer.from(await input.data);
let hash: string;
try {
hash = await hashB64u(data, B64_HASH_LIMIT);
} catch (error) {
// eslint-disable-next-line no-console
console.debug('EMPTY DATA');
// eslint-disable-next-line no-console
console.dir(input);
throw error;
}
const dimensions = input.width && input.height
? { width: input.width, height: input.height }
: await getDimensions(data, input.type);
const result = new Variant({
...config, ...input, hash, ...dimensions,
});
await mkdir(config.destDir, { recursive: true });
await writeFile(result.localFilePath, await result.data);
return result;
};