-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08710dd
commit e73f9c4
Showing
18 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.version = void 0; | ||
exports.version = '3.0.2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function ensurePromiseCallback(callback) { | ||
if (typeof callback === 'function') { | ||
return [callback]; | ||
} | ||
let promiseResolve, promiseReject; | ||
const promise = new Promise((resolve, reject) => { | ||
promiseResolve = resolve; | ||
promiseReject = reject; | ||
}); | ||
return [ | ||
(err, info) => { | ||
if (err) { | ||
return promiseReject(err); | ||
} | ||
return promiseResolve(info); | ||
}, | ||
promise | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ensurePromiseCallback } from "./callback.mjs"; | ||
import { handleData, handleError, toStream } from "./internals.mjs"; | ||
import { defaultOptions, FastImageError } from "./models.mjs"; | ||
import { FastImageStream } from "./stream.mjs"; | ||
export function info(source, options, cb) { | ||
// Normalize arguments | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
const { timeout, threshold, userAgent } = { ...defaultOptions, ...options }; | ||
// Prepare execution | ||
let finished = false; | ||
let response; | ||
let buffer = Buffer.alloc(0); | ||
const [callback, promise] = ensurePromiseCallback(cb); | ||
const start = process.hrtime.bigint(); | ||
// Make sure the source is always a Stream | ||
let stream; | ||
let url; | ||
try { | ||
; | ||
[stream, url] = toStream(source, timeout, threshold, userAgent); | ||
} | ||
catch (e) { | ||
callback(e); | ||
return promise; | ||
} | ||
// When dealing with URLs, save the response to extract data later | ||
stream.on('response', (r) => { | ||
response = r; | ||
}); | ||
stream.on('data', (chunk) => { | ||
if (finished) { | ||
return; | ||
} | ||
buffer = Buffer.concat([buffer, chunk]); | ||
finished = handleData(buffer, response, threshold, start, callback); | ||
}); | ||
stream.on('error', (error) => { | ||
callback(handleError(error, url)); | ||
}); | ||
stream.on('end', () => { | ||
if (finished) { | ||
return; | ||
} | ||
// We have reached the end without figuring the image type. Just give up | ||
callback(new FastImageError('Unsupported data.', 'UNSUPPORTED')); | ||
}); | ||
return promise; | ||
} | ||
export function stream(options) { | ||
return new FastImageStream(options !== null && options !== void 0 ? options : {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { createReadStream } from 'fs'; | ||
import got from 'got'; | ||
import imageSize from 'image-size'; | ||
import { Readable } from 'stream'; | ||
import { FastImageError } from "./models.mjs"; | ||
export function toStream(source, timeout, threshold, userAgent) { | ||
let url; | ||
const highWaterMark = threshold > 0 ? Math.floor(threshold / 10) : 1024; | ||
// If the source is a buffer, get it as stream | ||
if (Buffer.isBuffer(source)) { | ||
source = Readable.from(source, { highWaterMark }); | ||
} | ||
else if (typeof source === 'string') { | ||
// Try to parse the source as URL - If it succeeds, we will fetch it | ||
try { | ||
const parsedUrl = new URL(source); | ||
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') { | ||
throw new FastImageError('Invalid URL.', 'URL_ERROR', parsedUrl.toString()); | ||
} | ||
url = source; | ||
source = got.stream(parsedUrl.toString(), { | ||
headers: { 'user-agent': userAgent }, | ||
followRedirect: true, | ||
timeout | ||
}); | ||
} | ||
catch (e) { | ||
if (e.code === 'FASTIMAGE_URL_ERROR') { | ||
throw e; | ||
} | ||
// Parsing failed. Treat as local file | ||
source = createReadStream(source, { highWaterMark }); | ||
} | ||
} | ||
return [source, url]; | ||
} | ||
export function handleData(buffer, response, threshold, start, callback) { | ||
try { | ||
const info = imageSize(buffer); | ||
const data = { | ||
width: info.width, | ||
height: info.height, | ||
type: info.type, | ||
time: Number(process.hrtime.bigint() - start) / 1e6, | ||
analyzed: buffer.length | ||
}; | ||
// Add URL informations | ||
if (response) { | ||
data.realUrl = response.url; | ||
/* istanbul ignore else */ | ||
if ('content-length' in response.headers) { | ||
data.size = parseInt(response.headers['content-length'], 10); | ||
} | ||
} | ||
// Close the URL if possible | ||
if (response) { | ||
response.destroy(); | ||
} | ||
callback(null, data); | ||
return true; | ||
} | ||
catch (e) { | ||
// Check threshold | ||
if (threshold > 0 && buffer.length > threshold) { | ||
if (response) { | ||
response.destroy(); | ||
} | ||
callback(new FastImageError('Unsupported data.', 'UNSUPPORTED')); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
export function handleError(error, url) { | ||
let message = null; | ||
let code = 'NETWORK_ERROR'; | ||
switch (error.code) { | ||
case 'EISDIR': | ||
code = 'FS_ERROR'; | ||
message = 'Source is a directory.'; | ||
break; | ||
case 'ENOENT': | ||
code = 'FS_ERROR'; | ||
message = 'Source not found.'; | ||
break; | ||
case 'EACCES': | ||
code = 'FS_ERROR'; | ||
message = 'Source is not readable.'; | ||
break; | ||
case 'ENOTFOUND': | ||
message = 'Invalid remote host requested.'; | ||
break; | ||
case 'ECONNRESET': | ||
case 'EPIPE': | ||
message = 'Connection with the remote host interrupted.'; | ||
break; | ||
case 'ECONNREFUSED': | ||
message = 'Connection refused from the remote host.'; | ||
break; | ||
case 'ETIMEDOUT': | ||
message = 'Connection to the remote host timed out.'; | ||
break; | ||
} | ||
if (error.response) { | ||
message = `Remote host replied with HTTP ${error.response.statusCode}.`; | ||
} | ||
/* istanbul ignore else */ | ||
if (message) { | ||
error = new FastImageError(message, code, url); | ||
} | ||
return error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// The version is dynamically generated via build script in order not rely on require in the ESM case. | ||
import { version } from "./version.mjs"; | ||
export class FastImageError extends Error { | ||
constructor(message, code, url, httpResponseCode) { | ||
super(message); | ||
this.code = `FASTIMAGE_${code}`; | ||
this.url = url; | ||
this.httpResponseCode = httpResponseCode; | ||
} | ||
} | ||
export const defaultOptions = { | ||
timeout: 30000, | ||
threshold: 4096, | ||
userAgent: `fastimage/${version}` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Writable } from 'stream'; | ||
import { handleData } from "./internals.mjs"; | ||
import { defaultOptions, FastImageError } from "./models.mjs"; | ||
export class FastImageStream extends Writable { | ||
constructor(options) { | ||
var _a; | ||
super(options); | ||
this.threshold = (_a = options.threshold) !== null && _a !== void 0 ? _a : defaultOptions.threshold; | ||
this.buffer = Buffer.alloc(0); | ||
this.start = process.hrtime.bigint(); | ||
this.finished = false; | ||
} | ||
analyze(chunk) { | ||
this.buffer = Buffer.concat([this.buffer, chunk]); | ||
this.finished = handleData(this.buffer, undefined, this.threshold, this.start, (error, data) => { | ||
if (error) { | ||
this.emit('error', error); | ||
} | ||
else { | ||
this.emit('info', data); | ||
} | ||
this.destroy(); | ||
}); | ||
} | ||
_write(chunk, _e, cb) { | ||
this.analyze(chunk); | ||
cb(); | ||
} | ||
/* istanbul ignore next */ | ||
_writev(chunks, cb) { | ||
for (const { chunk } of chunks) { | ||
this.analyze(chunk); | ||
} | ||
cb(); | ||
} | ||
_final(cb) { | ||
/* istanbul ignore if */ | ||
if (this.finished) { | ||
cb(); | ||
return; | ||
} | ||
cb(new FastImageError('Unsupported data.', 'UNSUPPORTED')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const version = '3.0.2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const version = '3.0.2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"declaration": false, | ||
"declarationDir": null, | ||
"outDir": "dist/mjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare const version = "3.0.2"; |