-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 5fe22d0
Showing
34 changed files
with
1,424 additions
and
0 deletions.
There are no files selected for viewing
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,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = tab | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
indent_size = 2 | ||
indent_style = space |
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,14 @@ | ||
# generated | ||
node_modules | ||
|
||
# logs | ||
*.log* | ||
|
||
# locks | ||
*-lock.* | ||
|
||
# environment | ||
*.env* | ||
|
||
# macos | ||
.DS_Store |
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,3 @@ | ||
# Ignore all JS/TS files: | ||
*.js | ||
*.ts |
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,32 @@ | ||
{ | ||
/* Editor */ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true | ||
}, | ||
"editor.insertSpaces": false, | ||
"editor.useTabStops": true, | ||
|
||
/* Prettier */ | ||
"prettier.ignorePath": "/.vscode/.prettierignore", | ||
"prettier.useEditorConfig": true, | ||
|
||
/* JavaScript */ | ||
"[javascript]": { | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint" | ||
}, | ||
|
||
/* JSON */ | ||
"[json]": { | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint" | ||
}, | ||
|
||
/* JSON */ | ||
"[jsonc]": { | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint" | ||
}, | ||
|
||
/* TypeScript */ | ||
"[typescript]": { | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint" | ||
}, | ||
} |
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,3 @@ | ||
# get-size | ||
|
||
An ESM fork of [get-size](https://www.npmjs.com/package/get-size). |
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,56 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/package.json", | ||
"name": "@astropub/get-size.root", | ||
"type": "module", | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"scripts": { | ||
"check": "node scripts/check.js", | ||
"release": "node scripts/release.js" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.4.1", | ||
"@typescript-eslint/eslint-plugin": "6.0.0", | ||
"@typescript-eslint/parser": "6.0.0", | ||
"@typescript/lib-dom": "npm:@types/web@^0.0.105", | ||
"brotli-size": "4.0.0", | ||
"eslint": "8.44.0", | ||
"eslint-config-dev": "3.3.1", | ||
"eslint-plugin-json": "3.1.0", | ||
"gzip-size": "7.0.0", | ||
"rollup-plugin-terser": "7.0.2", | ||
"rollup-plugin-typescript2": "0.35.0", | ||
"types-object": "0.3.0", | ||
"typescript": "5.1.6", | ||
"vite": "4.4.3" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"dev/ts/nosemi" | ||
], | ||
"plugins": [ | ||
"json" | ||
], | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
"tab" | ||
] | ||
} | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"useTabs": true, | ||
"overrides": [ | ||
{ | ||
"files": "*.md", | ||
"options": { | ||
"tabWidth": 2, | ||
"useTabs": false | ||
} | ||
} | ||
] | ||
}, | ||
"private": true | ||
} |
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,7 @@ | ||
Copyright (c) 2012-2021 [David DeSandro](https://desandro.com) and [contributors](https://github.com/desandro/get-size/graphs/contributors) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,3 @@ | ||
# get-size | ||
|
||
An ESM fork of [get-size](https://www.npmjs.com/package/get-size). |
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,3 @@ | ||
import type { ImageResult } from '../types.d.ts' | ||
|
||
export default function getSizeFromAsyncIterable(iteratable: Iterable<Uint8Array> | AsyncIterable<Uint8Array>): Promise<ImageResult | null> |
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,23 @@ | ||
import getSizeFromUint8Array from './Uint8Array.js' | ||
import getMergedUint8Array from '../utils/getMergedUint8Array.js' | ||
|
||
import '../types.js' | ||
import '../detector.js' | ||
|
||
export default async function getSizeFromAsyncIterable(/** @type {Iterable<Uint8Array> | AsyncIterable<Uint8Array>} */ iteratable) { | ||
await import('../utils/ReadableStreamPolyfill.js') | ||
|
||
let array = new Uint8Array() | ||
|
||
for await (const chunk of iteratable) { | ||
array = getMergedUint8Array(array, chunk) | ||
|
||
const result = getSizeFromUint8Array(array) | ||
|
||
if (result !== null && !isNaN(result.width)) { | ||
return result | ||
} | ||
} | ||
|
||
return null | ||
} |
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,10 @@ | ||
/// <reference types="node" /> | ||
|
||
import type { ImageResult } from '../types.d.ts' | ||
import type { PathLike } from 'node:fs' | ||
|
||
export interface GetSizeFromFileSyncOptions { | ||
chunkSize: number; | ||
} | ||
|
||
export default function getSizeFromFileSync(path: PathLike, opts?: GetSizeFromFileSyncOptions): ImageResult | null |
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,38 @@ | ||
/// <reference types="node" /> | ||
|
||
/** @typedef {import('../types.d.ts').ImageResult} ImageResult */ | ||
/** @typedef {import('node:fs').PathLike} PathLike */ | ||
/** @typedef {import('./FileSync.d.ts').GetSizeFromFileSyncOptions} GetSizeFromFileSyncOptions */ | ||
|
||
import { openSync, fstatSync, readSync } from 'node:fs' | ||
import getSizeFromIterator from './Iterable.js' | ||
|
||
import './Uint8Array.js' | ||
import '../types.js' | ||
import '../detector.js' | ||
import '../utils/getMergedUint8Array.js' | ||
|
||
export default function getSizeFromFileSync(/** @type {PathLike} */ path, /** @type {GetSizeFromFileSyncOptions} */ opts = null) { | ||
const chunkSize = Number(Object(opts).chunkSize) || defaultChunkSize | ||
const fileReadIterator = readFileIteratorSync(path, { chunkSize }) | ||
|
||
return getSizeFromIterator(fileReadIterator) | ||
} | ||
|
||
function * readFileIteratorSync(path, opts) { | ||
opts = Object(opts) | ||
|
||
const chunkSize = Number(opts.chunkSize) || defaultChunkSize | ||
const fileDescriptor = openSync(path, 'r') | ||
const fileStats = fstatSync(fileDescriptor) | ||
const buffer = Buffer.alloc(Math.min(fileStats.size, chunkSize)) | ||
|
||
/** @type {number} */ | ||
let read | ||
|
||
while (read = readSync(fileDescriptor, buffer, 0, buffer.length, null), read !== 0) { | ||
yield new Uint8Array(buffer) | ||
} | ||
} | ||
|
||
const defaultChunkSize = 256 |
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,3 @@ | ||
import type { ImageResult } from '../types.d.ts' | ||
|
||
export default function getSizeFromIterator(iterable: Iterable<Uint8Array>): ImageResult | null |
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,21 @@ | ||
import getSizeFromUint8Array from './Uint8Array.js' | ||
import getMergedUint8Array from '../utils/getMergedUint8Array.js' | ||
|
||
import '../types.js' | ||
import '../detector.js' | ||
|
||
export default function getSizeFromIterator(/** @type {Iterable<Uint8Array>} */ iterable) { | ||
let array = new Uint8Array() | ||
|
||
for (const chunk of iterable) { | ||
array = getMergedUint8Array(array, chunk) | ||
|
||
const result = getSizeFromUint8Array(array) | ||
|
||
if (result !== null && !isNaN(result.width)) { | ||
return result | ||
} | ||
} | ||
|
||
return null | ||
} |
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,3 @@ | ||
import type { ImageResult } from '../types.d.ts' | ||
|
||
export default function getSizeFromReadableStream(readableStream: ReadableStream<Uint8Array>): Promise<ImageResult | null> |
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,12 @@ | ||
import getSizeFromAsyncIterable from './AsyncIterable.js' | ||
|
||
import './Uint8Array.js' | ||
import '../types.js' | ||
import '../detector.js' | ||
import '../utils/getMergedUint8Array.js' | ||
|
||
export default async function getSizeFromReadableStream(/** @type {ReadableStream<Uint8Array>} */ readableStream) { | ||
await import('../utils/ReadableStreamPolyfill.js') | ||
|
||
return await getSizeFromAsyncIterable(readableStream) | ||
} |
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,3 @@ | ||
import type { ImageResult } from '../types.d.ts' | ||
|
||
export default function getSizeFromURL(input: URL | RequestInfo): Promise<ImageResult | null> |
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,12 @@ | ||
import getSizeFromAsyncIterable from './AsyncIterable.js' | ||
|
||
import './Uint8Array.js' | ||
import '../types.js' | ||
import '../detector.js' | ||
import '../utils/getMergedUint8Array.js' | ||
|
||
export default async function getSizeFromURL(input) { | ||
const response = await fetch(input) | ||
|
||
return await getSizeFromAsyncIterable(response.body) | ||
} |
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,3 @@ | ||
import type { ImageResult } from '../types.d.ts' | ||
|
||
export default function getSizeFromUint8Array(input: Uint8Array): ImageResult | null |
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,23 @@ | ||
/** @typedef {import('../types.d.ts').ImageResult} ImageResult */ | ||
|
||
import { types } from '../types.js' | ||
import getType from '../getType.js' | ||
|
||
export default function getSizeFromUint8Array(/** @type {Uint8Array} */ input) { | ||
const type = getType(input) | ||
|
||
if (type !== null) { | ||
try { | ||
const size = types[type].calculate(input) | ||
|
||
return /** @type {ImageResult} */ ({ | ||
type, | ||
...size, | ||
}) | ||
} catch { | ||
// do nothing and continue | ||
} | ||
} | ||
|
||
return null | ||
} |
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,10 @@ | ||
export type { ImageType, ImageResult } from './types.ts' | ||
|
||
export { default as getSizeFromAsyncIterable } from './from/AsyncIterable.ts' | ||
export { default as getSizeFromIterable } from './from/Iterable.ts' | ||
export { default as getSizeFromReadableStream } from './from/ReadableStream.ts' | ||
export { default as getSizeFromUint8Array } from './from/Uint8Array.ts' | ||
export { default as getSizeFromURL } from './from/URL.ts' | ||
export { default as getType } from './getType.js' | ||
|
||
export { types } from './types.ts' |
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 @@ | ||
export { default as getSizeFromAsyncIterable } from './from/AsyncIterable.js' | ||
export { default as getSizeFromIterable } from './from/Iterable.js' | ||
export { default as getSizeFromReadableStream } from './from/ReadableStream.js' | ||
export { default as getSizeFromUint8Array } from './from/Uint8Array.js' | ||
export { default as getSizeFromURL } from './from/URL.js' | ||
export { default as getType } from './getType.js' | ||
export { types } from './types.js' | ||
|
||
import './utils/getMergedUint8Array.js' |
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,3 @@ | ||
import type { SupportedImageType } from './types.d.ts' | ||
|
||
export default function getType(input: Uint8Array): SupportedImageType | null |
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,35 @@ | ||
/** @typedef {import('./types.d.ts').SupportedImageType} SupportedImageType */ | ||
|
||
import { types } from './types.js' | ||
|
||
/** @type {SupportedImageType[]} */ | ||
const keys = Object.keys(types) | ||
|
||
// This map helps avoid validating for every single image type | ||
/** @type {{ [byte: number]: SupportedImageType }} */ | ||
const firstBytes = { | ||
0x38: 'psd', | ||
0x42: 'bmp', | ||
0x44: 'dds', | ||
0x47: 'gif', | ||
0x52: 'webp', | ||
0x69: 'icns', | ||
0x89: 'png', | ||
0xff: 'jpg' | ||
} | ||
|
||
export default function getType(/** @type {Uint8Array} */ input) { | ||
const byte = input[0] | ||
|
||
if (byte in firstBytes) { | ||
const type = firstBytes[byte] | ||
|
||
if (type && types[type].validate(input)) { | ||
return type | ||
} | ||
} | ||
|
||
return keys.find( | ||
(/** @type {SupportedImageType} */ key) => types[key].validate(input) | ||
) || null | ||
} |
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 type ImageSize = { | ||
width: number | ||
height: number | ||
} | ||
|
||
export type ImageDetector = { | ||
validate(input: Uint8Array): boolean | ||
calculate(input: Uint8Array): ImageSize | ||
} | ||
|
||
export type SupportedImageType = 'bmp' | 'cur' | 'dds' | 'gif' | 'icns' | 'ico' | 'j2c' | 'jp2' | 'jpg' | 'ktx' | 'png' | 'pnm' | 'psd' | 'svg' | 'tga' | 'webp' | ||
|
||
export type ImageTypes = Record<SupportedImageType, ImageDetector> | ||
|
||
export type ImageResult = { | ||
type: SupportedImageType | ||
} & ImageSize | ||
|
||
export const types: ImageTypes |
Oops, something went wrong.