Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
{
"name": "Node.js & TypeScript",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
6 changes: 3 additions & 3 deletions .github/workflows/ci-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
node: [18, 20, 22, 23]
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v3
Expand All @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
node: [18, 20, 22, 23]
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Set up node
uses: actions/setup-node@v3
with:
node-version: "16.x"
node-version: "22.x"
registry-url: "https://registry.npmjs.org"
- name: Install dependencies
run: npm install
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.8.0
* Refine convert TypeScript types and add type tests
* Replace unmaintained proxying-agent with [https-proxy-agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/https-proxy-agent)
* remove data/cacert.pem and inline the bundle in a JS file for maximum compatibility

## 1.7.0
* Added convert and transform functions
* Added function to get the file extension
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ npm install
TINIFY_KEY=$YOUR_API_KEY npm run integration
```


#### To test with proxy:

$ docker run --rm -it -v ~/.mitmproxy:/home/mitmproxy/.mitmproxy -p 8080:8080 mitmproxy/mitmproxy mitmproxy --listen-host 0.0.0.0

$ TINIFY_PROXY=http://172.17.0.3:8080 npm run integration

## License

This software is licensed under the MIT License. [View the license](LICENSE).
2,754 changes: 0 additions & 2,754 deletions lib/data/cacert.pem

This file was deleted.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tinify",
"version": "1.7.1",
"version": "1.8.0",
"description": "Node.js client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.",
"keywords": [
"tinify",
Expand All @@ -18,7 +18,9 @@
"type": "git",
"url": "https://github.com/tinify/tinify-nodejs.git"
},
"files": ["lib"],
"files": [
"lib"
],
"bugs:": "https://github.com/tinify/tinify-nodejs/issues",
"scripts": {
"test": "rm -rf lib/**.{js,ts} && tsc && tsc --noEmit test/tinify-typing-test.ts && mocha --reporter dot test/*-test.js",
Expand All @@ -30,17 +32,17 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"dependencies": {
"promise-nodeify": ">= 0.1",
"proxying-agent": ">= 2.1"
"https-proxy-agent": "^7.0.6",
"promise-nodeify": ">= 0.1"
},
"devDependencies": {
"@types/node": "~10.3.1",
"@types/node": "22.13.4",
"chai": "^2.3.0",
"mocha": "^2.2.5",
"nock": "^13.2.9",
"semver": "*",
"tmp": "^0.0.26",
"tslint": "^5.10.0",
"typescript": "^2.9.2"
"typescript": "^5.7.3"
}
}
22 changes: 9 additions & 13 deletions src/tinify/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ import {IncomingMessage, IncomingHttpHeaders} from "http"
import * as https from "https"
import * as url from "url"
import * as fs from "fs"
import * as proxyAgent from "proxying-agent"
import { HttpsProxyAgent } from 'https-proxy-agent';

import {version} from "../../package.json"
import tinify from "../tinify"
import {CA_CERTS} from "./cacert"

const boundaries = /-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----\n/g
const data = fs.readFileSync(`${__dirname}/../data/cacert.pem`).toString()

interface ClientOptions {
ca: RegExpMatchArray
interface ClientOptions extends https.RequestOptions {
rejectUnauthorized: boolean
auth: string
agent?: proxyAgent.ProxyingAgent
agent?: HttpsProxyAgent<string>
}

/** @internal */
Expand All @@ -37,7 +34,7 @@ export default class Client {
private static USER_AGENT = `Tinify/${version} Node/${process.versions.node} (${process.platform})`

/** @internal */
private static CA_BUNDLE = data.match(boundaries)!
private static CA_BUNDLE = CA_CERTS

/** @internal */
userAgent: string
Expand Down Expand Up @@ -65,10 +62,9 @@ export default class Client {
/* Note: although keepAlive is enabled, the proxy agent reconnects to the
proxy server each time. This makes proxied requests slow. There
seems to be no proxy tunneling agent that reuses TLS connections. */
this.defaultOptions.agent = proxyAgent.create({
proxy,
keepAlive: true,
}, klass.API_ENDPOINT)
this.defaultOptions.agent = new HttpsProxyAgent(proxy, {
"keepAlive": true,
})
}
}

Expand Down Expand Up @@ -120,7 +116,7 @@ export default class Client {
details = JSON.parse(body.toString())
} catch(err) {
details = {
message: `Error while parsing response: ${err.message}`,
message: `Error while parsing response: ${(err as any).message}`,
error: "ParseError",
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/tinify/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export default class Result extends ResultMeta {
/** @internal */
meta(): Promise<IncomingHttpHeaders> {
/* Ignore errors on data, because they'll be propagated to meta too. */
return this._data.catch(ignore) && this._meta
return this._data.catch(ignore).then(() => this._meta);
}

/** @internal */
data(): Promise<Uint8Array> {
/* Ignore errors on meta, because they'll be propagated to data too. */
return this._meta.catch(ignore) && this._data
return this._meta.catch(ignore).then(() => this._data);
}

toFile(path: string): Promise<void>
Expand All @@ -47,20 +47,20 @@ export default class Result extends ResultMeta {
}

mediaType(): Promise<string | void>
mediaType(callback: Callback<string>): void
mediaType(callback?: Callback<string>): Promise<string | void> | void {
mediaType(callback: Callback<string | void>): void
mediaType(callback?: Callback<string | void>): Promise<string | void> | void {
return nodeify(this.meta().then(meta => meta["content-type"] as string), callback)
}

contentType(): Promise<string | void>
contentType(callback: Callback<string>): void
contentType(callback?: Callback<string>): Promise<string | void> | void {
contentType(callback: Callback<string | void>): void
contentType(callback?: Callback<string | void>): Promise<string | void> | void {
return this.mediaType(callback!)
}

extension(): Promise<string | void>
extension(callback: Callback<string>): void
extension(callback?: Callback<string>): Promise<string | void> | void {
extension(callback: Callback<string | void>): void
extension(callback?: Callback<string | void>): Promise<string | void> | void {
return nodeify(this.meta().then(meta => (meta["content-type"] || " ").split("/")[1]), callback)
}
}
4 changes: 2 additions & 2 deletions src/tinify/ResultMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default class ResultMeta {
return nodeify(this.meta().then(meta => intify(meta["image-height"] as string)), callback)
}

location(): Promise<string | void>
location(): Promise<string>
location(callback: Callback<string>): void
location(callback?: Callback<string>): Promise<string | void> | void {
location(callback?: Callback<string>): Promise<string> | void {
return nodeify(this.meta().then(meta => meta["location"] as string), callback)
}
}
15 changes: 14 additions & 1 deletion src/tinify/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import Client from "./Client"
import Result from "./Result"
import ResultMeta from "./ResultMeta"

export type SupportedImageTypes = "image/webp"
| "image/png"
| "image/jpg"
| "image/jpeg"
| "image/avif";

export type WildcardOrSupportedImageTypes = SupportedImageTypes
| "*/*"; // The wildcard "*/*" returns the smallest of Tinify's supported image types, currently JPEG, PNG and WebP.

export type ConvertOptions = {
type: WildcardOrSupportedImageTypes | SupportedImageTypes[];
}

export default class Source {
/** @internal */
private _url: Promise<string>
Expand Down Expand Up @@ -85,7 +98,7 @@ export default class Source {
return this.result().toBuffer(callback!)
}

convert(options: object): Source {
convert(options: ConvertOptions): Source {
return new tinify.Source(
this._url,
Object.assign({ convert: options }, this._commands)
Expand Down
Loading