Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Jul 12, 2023
0 parents commit 5fe22d0
Show file tree
Hide file tree
Showing 34 changed files with 1,424 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
14 changes: 14 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .vscode/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore all JS/TS files:
*.js
*.ts
32 changes: 32 additions & 0 deletions .vscode/settings.json
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"
},
}
3 changes: 3 additions & 0 deletions README.md
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).
56 changes: 56 additions & 0 deletions package.json
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
}
7 changes: 7 additions & 0 deletions packages/get-size/LICENSE.md
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.
3 changes: 3 additions & 0 deletions packages/get-size/README.md
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).
3 changes: 3 additions & 0 deletions packages/get-size/lib/from/AsyncIterable.d.ts
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>
23 changes: 23 additions & 0 deletions packages/get-size/lib/from/AsyncIterable.js
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
}
10 changes: 10 additions & 0 deletions packages/get-size/lib/from/FileSync.d.ts
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
38 changes: 38 additions & 0 deletions packages/get-size/lib/from/FileSync.js
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
3 changes: 3 additions & 0 deletions packages/get-size/lib/from/Iterable.d.ts
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
21 changes: 21 additions & 0 deletions packages/get-size/lib/from/Iterable.js
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
}
3 changes: 3 additions & 0 deletions packages/get-size/lib/from/ReadableStream.d.ts
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>
12 changes: 12 additions & 0 deletions packages/get-size/lib/from/ReadableStream.js
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)
}
3 changes: 3 additions & 0 deletions packages/get-size/lib/from/URL.d.ts
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>
12 changes: 12 additions & 0 deletions packages/get-size/lib/from/URL.js
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)
}
3 changes: 3 additions & 0 deletions packages/get-size/lib/from/Uint8Array.d.ts
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
23 changes: 23 additions & 0 deletions packages/get-size/lib/from/Uint8Array.js
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
}
10 changes: 10 additions & 0 deletions packages/get-size/lib/getSize.d.ts
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'
9 changes: 9 additions & 0 deletions packages/get-size/lib/getSize.js
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'
3 changes: 3 additions & 0 deletions packages/get-size/lib/getType.d.ts
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
35 changes: 35 additions & 0 deletions packages/get-size/lib/getType.js
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
}
19 changes: 19 additions & 0 deletions packages/get-size/lib/types.d.ts
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
Loading

0 comments on commit 5fe22d0

Please sign in to comment.