n8n-nodes-remove-ac 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/credentials/RemoveAcApi.credentials.d.ts +10 -0
- package/dist/credentials/RemoveAcApi.credentials.js +54 -0
- package/dist/nodes/RemoveAc/RemoveAc.node.d.ts +6 -0
- package/dist/nodes/RemoveAc/RemoveAc.node.js +905 -0
- package/dist/nodes/RemoveAc/removeac.svg +8 -0
- package/index.js +2 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 remove.ac
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
-
#
|
|
1
|
+
# n8n-nodes-remove-ac
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Official zero-dependency **[n8n](https://n8n.io)** community node for **[remove.ac](https://remove.ac)** — drop-in `/v1.0/removebg` Cloud GPU background removal ahead of the **December 1, 2026** `remove.bg` standalone site and self-serve `/v1.0/removebg` API shutdown.
|
|
4
|
+
|
|
5
|
+
- **Migration Guide (`remove.bg` → `remove.ac`):** https://remove.ac/guides/n8n-make-shopify-background-removal-api
|
|
6
|
+
- **API Keys & 1 Free Welcome Credit:** https://remove.ac/api-info
|
|
7
|
+
- **Free On-Device Browser WASM Workbench (50-Image Batch ZIP):** https://remove.ac
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Why Use `n8n-nodes-remove-ac`?
|
|
12
|
+
|
|
13
|
+
1. **Drop-In Replacement for Deprecated `Remove.bg` Workflows:** Accepts upstream n8n binary items (`data`) or public image URLs and outputs ready-to-upload cutout binaries (`PNG`, `WebP`, `JPG`, `ZIP`) plus `X-Foreground-*` bounding-box metadata.
|
|
14
|
+
2. **Automatic `> 4 MB` Zero-Disk GPU Streaming (Up to `25 MB / 40 MP`):** Large studio product photos automatically negotiate a 180-second single-use HMAC ticket (`POST /v1.0/removebg/ticket`) to bypass serverless payload caps without persisting source or output pixels to disk.
|
|
15
|
+
3. **AI Agent Tool Ready (`usableAsTool: true`):** Works both as a standard pipeline transform node and as a callable tool inside n8n AI Agent workflows.
|
|
16
|
+
4. **Zero External Runtime Dependencies & SSRF Hardened:** Built strictly on Node.js built-in primitives and n8n's `httpRequest` helper with IPv4/IPv6/NAT64/DNS private-network blocking.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation in n8n
|
|
21
|
+
|
|
22
|
+
1. Open your self-hosted or desktop n8n instance.
|
|
23
|
+
2. Go to **Settings → Community Nodes → Install**.
|
|
24
|
+
3. Enter **`n8n-nodes-remove-ac`**, accept the prompt, and click **Install**.
|
|
25
|
+
4. Create a **remove.ac API** credential using your `rac_live_...` API key from https://remove.ac/api-info (verified email accounts receive **1 free credit**; non-expiring packs start at `$9 / 10` credits, monthly plans down to `$0.158 / image`; failed or timed-out calls cost `0` credits).
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Operations
|
|
30
|
+
|
|
31
|
+
- **Remove Background (`removeBackground`)**
|
|
32
|
+
- **Binary Data:** Reads any upstream binary property (default `data`) up to `25 MB` and writes the transparent cutout binary + `json.removeAc` metadata (`creditsCharged`, `width`, `height`, `foregroundTop/Left/Width/Height`).
|
|
33
|
+
- **Image URL:** Sends a validated public `https://` / `http://` URL to `POST /v1.0/removebg` and outputs the cutout binary.
|
|
34
|
+
- **Get Account Balance (`getAccount`)**
|
|
35
|
+
- Queries `GET /v1.0/account` and returns `totalCredits` + full account breakdown.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT © [remove.ac](https://remove.ac)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IAuthenticateGeneric, ICredentialTestRequest, ICredentialType, INodeProperties } from "n8n-workflow";
|
|
2
|
+
|
|
3
|
+
export declare class RemoveAcApi implements ICredentialType {
|
|
4
|
+
name: string;
|
|
5
|
+
displayName: string;
|
|
6
|
+
documentationUrl: string;
|
|
7
|
+
properties: INodeProperties[];
|
|
8
|
+
authenticate: IAuthenticateGeneric;
|
|
9
|
+
test: ICredentialTestRequest;
|
|
10
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoveAcApi = void 0;
|
|
4
|
+
|
|
5
|
+
class RemoveAcApi {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.name = "removeAcApi";
|
|
8
|
+
this.displayName = "remove.ac API";
|
|
9
|
+
this.documentationUrl = "https://remove.ac/api-info";
|
|
10
|
+
this.properties = [
|
|
11
|
+
{
|
|
12
|
+
displayName: "API Key",
|
|
13
|
+
name: "apiKey",
|
|
14
|
+
type: "string",
|
|
15
|
+
typeOptions: {
|
|
16
|
+
password: true,
|
|
17
|
+
},
|
|
18
|
+
default: "",
|
|
19
|
+
required: true,
|
|
20
|
+
placeholder: "rac_live_...",
|
|
21
|
+
description:
|
|
22
|
+
"Your remove.ac API key. Verified email accounts include 1 free welcome credit at https://remove.ac/api-info.",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
displayName: "Base URL",
|
|
26
|
+
name: "baseUrl",
|
|
27
|
+
type: "string",
|
|
28
|
+
default: "https://remove.ac",
|
|
29
|
+
required: true,
|
|
30
|
+
description: "HTTPS origin for the remove.ac API.",
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
this.authenticate = {
|
|
34
|
+
type: "generic",
|
|
35
|
+
properties: {
|
|
36
|
+
headers: {
|
|
37
|
+
"X-Api-Key": "={{$credentials.apiKey}}",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
this.test = {
|
|
42
|
+
request: {
|
|
43
|
+
baseURL:
|
|
44
|
+
"={{ /^https:\\/\\/[a-z0-9-]+(\\.[a-z0-9-]+)+(:[0-9]{1,5})?\\/?$/i.test(($credentials.baseUrl || 'https://remove.ac').trim()) && !/\\.(localhost|local|internal|home\\.arpa)(:[0-9]+)?\\/?$/i.test(($credentials.baseUrl || '').trim()) && !/^https:\\/\\/[0-9.]+(:[0-9]+)?\\/?$/i.test(($credentials.baseUrl || '').trim()) ? ($credentials.baseUrl || 'https://remove.ac').trim().replace(/\\/+$/, '') : 'invalid://https-public-domain-required' }}",
|
|
45
|
+
url: "/v1.0/account",
|
|
46
|
+
method: "GET",
|
|
47
|
+
disableFollowRedirect: true,
|
|
48
|
+
sendCredentialsOnCrossOriginRedirect: false,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.RemoveAcApi = RemoveAcApi;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IExecuteFunctions, INodeExecutionData, INodeType, INodeTypeDescription } from "n8n-workflow";
|
|
2
|
+
|
|
3
|
+
export declare class RemoveAc implements INodeType {
|
|
4
|
+
description: INodeTypeDescription;
|
|
5
|
+
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoveAc =
|
|
4
|
+
exports.validatePublicUrl =
|
|
5
|
+
exports.isPrivateIpv6 =
|
|
6
|
+
exports.isPrivateIpv4 =
|
|
7
|
+
exports.parseImageDimensions =
|
|
8
|
+
void 0;
|
|
9
|
+
|
|
10
|
+
const crypto = require("node:crypto");
|
|
11
|
+
const dns = require("node:dns");
|
|
12
|
+
const net = require("node:net");
|
|
13
|
+
|
|
14
|
+
const DEFAULT_BASE_URL = "https://remove.ac";
|
|
15
|
+
const DIRECT_THRESHOLD_BYTES = 4 * 1024 * 1024;
|
|
16
|
+
const MAX_UPLOAD_BYTES = 25 * 1024 * 1024;
|
|
17
|
+
const DIRECT_RGBA_PNG_PIXELS = 5_000_000;
|
|
18
|
+
const DIRECT_CROPPED_RGBA_PNG_PIXELS = 10_000_000;
|
|
19
|
+
|
|
20
|
+
function isPrivateIpv4Octets(a, b, c) {
|
|
21
|
+
if (a === 0 || a === 10 || a === 127) return true;
|
|
22
|
+
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
23
|
+
if (a === 169 && b === 254) return true;
|
|
24
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
25
|
+
if (a === 192 && b === 0 && c === 0) return true;
|
|
26
|
+
if (a === 192 && b === 168) return true;
|
|
27
|
+
if (a === 198 && (b === 18 || b === 19)) return true;
|
|
28
|
+
if (a >= 224) return true;
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isPrivateIpv4(ip) {
|
|
33
|
+
const parts = String(ip)
|
|
34
|
+
.split(".")
|
|
35
|
+
.map((s) => (/^\d{1,3}$/.test(s) ? Number.parseInt(s, 10) : Number.NaN));
|
|
36
|
+
if (parts.length !== 4 || parts.some((n) => Number.isNaN(n) || n < 0 || n > 255)) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return isPrivateIpv4Octets(parts[0], parts[1], parts[2]);
|
|
40
|
+
}
|
|
41
|
+
exports.isPrivateIpv4 = isPrivateIpv4;
|
|
42
|
+
|
|
43
|
+
function isPrivateIpv4Words(hi16, lo16) {
|
|
44
|
+
return isPrivateIpv4Octets((hi16 >>> 8) & 0xff, hi16 & 0xff, (lo16 >>> 8) & 0xff);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseIpv6Words(rawIp) {
|
|
48
|
+
let clean = String(rawIp).trim().toLowerCase().replace(/^\[|\]$/g, "");
|
|
49
|
+
const zoneIdx = clean.indexOf("%");
|
|
50
|
+
if (zoneIdx !== -1) clean = clean.slice(0, zoneIdx);
|
|
51
|
+
if (!clean || !clean.includes(":")) return null;
|
|
52
|
+
|
|
53
|
+
const lastColon = clean.lastIndexOf(":");
|
|
54
|
+
const tail = clean.slice(lastColon + 1);
|
|
55
|
+
if (tail.includes(".")) {
|
|
56
|
+
const octets = tail
|
|
57
|
+
.split(".")
|
|
58
|
+
.map((s) => (/^\d{1,3}$/.test(s) ? Number.parseInt(s, 10) : Number.NaN));
|
|
59
|
+
if (octets.length !== 4 || octets.some((n) => Number.isNaN(n) || n < 0 || n > 255)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const w6 = ((octets[0] << 8) | octets[1]).toString(16);
|
|
63
|
+
const w7 = ((octets[2] << 8) | octets[3]).toString(16);
|
|
64
|
+
clean = `${clean.slice(0, lastColon + 1)}${w6}:${w7}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const dblParts = clean.split("::");
|
|
68
|
+
if (dblParts.length > 2) return null;
|
|
69
|
+
|
|
70
|
+
const parseSide = (str) => {
|
|
71
|
+
if (!str) return [];
|
|
72
|
+
const groups = str.split(":");
|
|
73
|
+
const nums = [];
|
|
74
|
+
for (const g of groups) {
|
|
75
|
+
if (!/^[0-9a-f]{1,4}$/.test(g)) return null;
|
|
76
|
+
nums.push(Number.parseInt(g, 16));
|
|
77
|
+
}
|
|
78
|
+
return nums;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (dblParts.length === 2) {
|
|
82
|
+
const left = parseSide(dblParts[0]);
|
|
83
|
+
const right = parseSide(dblParts[1]);
|
|
84
|
+
if (!left || !right || left.length + right.length >= 8) return null;
|
|
85
|
+
const zeros = new Array(8 - left.length - right.length).fill(0);
|
|
86
|
+
return Uint16Array.from([...left, ...zeros, ...right]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const full = parseSide(clean);
|
|
90
|
+
if (!full || full.length !== 8) return null;
|
|
91
|
+
return Uint16Array.from(full);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isPrivateIpv6(ip) {
|
|
95
|
+
const w = parseIpv6Words(ip);
|
|
96
|
+
if (!w) return true;
|
|
97
|
+
if (w[0] === 0 && w[1] === 0 && w[2] === 0 && w[3] === 0 && w[4] === 0 && w[5] === 0) {
|
|
98
|
+
if (w[6] === 0 && (w[7] === 0 || w[7] === 1)) return true;
|
|
99
|
+
return isPrivateIpv4Words(w[6], w[7]);
|
|
100
|
+
}
|
|
101
|
+
if (w[0] === 0 && w[1] === 0 && w[2] === 0 && w[3] === 0 && w[4] === 0 && w[5] === 0xffff) {
|
|
102
|
+
return isPrivateIpv4Words(w[6], w[7]);
|
|
103
|
+
}
|
|
104
|
+
if (w[0] === 0 && w[1] === 0 && w[2] === 0 && w[3] === 0 && w[4] === 0xffff && w[5] === 0) {
|
|
105
|
+
return isPrivateIpv4Words(w[6], w[7]);
|
|
106
|
+
}
|
|
107
|
+
if (w[0] === 0x0064 && w[1] === 0xff9b) {
|
|
108
|
+
if (w[2] === 0 && w[3] === 0 && w[4] === 0 && w[5] === 0) {
|
|
109
|
+
return isPrivateIpv4Words(w[6], w[7]);
|
|
110
|
+
}
|
|
111
|
+
if (w[2] === 1) return true;
|
|
112
|
+
}
|
|
113
|
+
if (w[0] === 0x0100 && w[1] === 0 && w[2] === 0 && w[3] === 0) return true;
|
|
114
|
+
if ((w[0] & 0xfe00) === 0xfc00) return true;
|
|
115
|
+
if ((w[0] & 0xffc0) === 0xfe80) return true;
|
|
116
|
+
if ((w[0] & 0xffc0) === 0xfec0) return true;
|
|
117
|
+
if ((w[0] & 0xff00) === 0xff00) return true;
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
exports.isPrivateIpv6 = isPrivateIpv6;
|
|
121
|
+
|
|
122
|
+
async function validatePublicUrl(rawUrl, { requireHttps = false, lookupFn = dns.promises.lookup } = {}) {
|
|
123
|
+
let parsed;
|
|
124
|
+
try {
|
|
125
|
+
parsed = new URL(String(rawUrl));
|
|
126
|
+
} catch {
|
|
127
|
+
throw new Error(`Invalid URL: '${rawUrl}'`);
|
|
128
|
+
}
|
|
129
|
+
if (requireHttps && parsed.protocol !== "https:") {
|
|
130
|
+
throw new Error("Endpoint URL must use https://");
|
|
131
|
+
}
|
|
132
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
133
|
+
throw new Error("Only http:// and https:// URLs are supported");
|
|
134
|
+
}
|
|
135
|
+
if (parsed.username || parsed.password) {
|
|
136
|
+
throw new Error("URLs with embedded credentials are not permitted");
|
|
137
|
+
}
|
|
138
|
+
const rawHost = parsed.hostname.toLowerCase();
|
|
139
|
+
const host = rawHost.replace(/^\[|\]$/g, "");
|
|
140
|
+
if (
|
|
141
|
+
!host ||
|
|
142
|
+
host === "localhost" ||
|
|
143
|
+
host.endsWith(".localhost") ||
|
|
144
|
+
host.endsWith(".local") ||
|
|
145
|
+
host.endsWith(".internal") ||
|
|
146
|
+
host.endsWith(".home.arpa")
|
|
147
|
+
) {
|
|
148
|
+
throw new Error("Local or internal hostnames are not permitted");
|
|
149
|
+
}
|
|
150
|
+
if (rawHost.startsWith("[") || host.includes(":")) {
|
|
151
|
+
if (isPrivateIpv6(host)) {
|
|
152
|
+
throw new Error("Private, loopback, or reserved IPv6 addresses are not permitted");
|
|
153
|
+
}
|
|
154
|
+
return parsed;
|
|
155
|
+
}
|
|
156
|
+
if (net.isIPv4(host)) {
|
|
157
|
+
if (isPrivateIpv4(host)) {
|
|
158
|
+
throw new Error("Private, loopback, or reserved IPv4 addresses are not permitted");
|
|
159
|
+
}
|
|
160
|
+
return parsed;
|
|
161
|
+
}
|
|
162
|
+
if (!host.includes(".") || /^\d+$/.test(host) || /^0x[0-9a-f]+$/i.test(host)) {
|
|
163
|
+
throw new Error("Single-label or numeric hostnames are not permitted");
|
|
164
|
+
}
|
|
165
|
+
let records;
|
|
166
|
+
try {
|
|
167
|
+
records = await lookupFn(host, { all: true, verbatim: true });
|
|
168
|
+
} catch {
|
|
169
|
+
throw new Error(`Unable to resolve hostname '${host}'`);
|
|
170
|
+
}
|
|
171
|
+
const list = Array.isArray(records) ? records : records ? [records] : [];
|
|
172
|
+
if (list.length === 0) {
|
|
173
|
+
throw new Error(`Hostname '${host}' returned no DNS records`);
|
|
174
|
+
}
|
|
175
|
+
const allowTunFakeIp =
|
|
176
|
+
parsed.protocol === "https:" && (host === "remove.ac" || host.endsWith(".remove.ac"));
|
|
177
|
+
for (const rec of list) {
|
|
178
|
+
const addr = String(rec && rec.address ? rec.address : "").trim();
|
|
179
|
+
if (net.isIPv4(addr)) {
|
|
180
|
+
const octs = addr.split(".").map((s) => Number.parseInt(s, 10));
|
|
181
|
+
const isRfc2544 = octs.length === 4 && octs[0] === 198 && (octs[1] === 18 || octs[1] === 19);
|
|
182
|
+
if (allowTunFakeIp && isRfc2544) continue;
|
|
183
|
+
if (isPrivateIpv4(addr)) throw new Error(`Hostname '${host}' resolved to a private IPv4 address`);
|
|
184
|
+
} else if (addr.includes(":")) {
|
|
185
|
+
if (isPrivateIpv6(addr)) throw new Error(`Hostname '${host}' resolved to a private IPv6 address`);
|
|
186
|
+
} else {
|
|
187
|
+
throw new Error(`Hostname '${host}' resolved to an unrecognized address`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return parsed;
|
|
191
|
+
}
|
|
192
|
+
exports.validatePublicUrl = validatePublicUrl;
|
|
193
|
+
|
|
194
|
+
function parseImageDimensions(input) {
|
|
195
|
+
const bytes =
|
|
196
|
+
input instanceof ArrayBuffer ? new Uint8Array(input) : input instanceof Uint8Array ? input : null;
|
|
197
|
+
if (!bytes || bytes.byteLength < 10) return null;
|
|
198
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
199
|
+
if (
|
|
200
|
+
bytes.byteLength >= 24 &&
|
|
201
|
+
bytes[0] === 0x89 &&
|
|
202
|
+
bytes[1] === 0x50 &&
|
|
203
|
+
bytes[2] === 0x4e &&
|
|
204
|
+
bytes[3] === 0x47 &&
|
|
205
|
+
bytes[4] === 0x0d &&
|
|
206
|
+
bytes[5] === 0x0a &&
|
|
207
|
+
bytes[6] === 0x1a &&
|
|
208
|
+
bytes[7] === 0x0a &&
|
|
209
|
+
bytes[12] === 0x49 &&
|
|
210
|
+
bytes[13] === 0x48 &&
|
|
211
|
+
bytes[14] === 0x44 &&
|
|
212
|
+
bytes[15] === 0x52
|
|
213
|
+
) {
|
|
214
|
+
const width = view.getUint32(16, false);
|
|
215
|
+
const height = view.getUint32(20, false);
|
|
216
|
+
return width > 0 && height > 0 && width <= 1_000_000 && height <= 1_000_000 ? { width, height } : null;
|
|
217
|
+
}
|
|
218
|
+
if (bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38) {
|
|
219
|
+
const width = view.getUint16(6, true);
|
|
220
|
+
const height = view.getUint16(8, true);
|
|
221
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
222
|
+
}
|
|
223
|
+
if (
|
|
224
|
+
bytes.byteLength >= 25 &&
|
|
225
|
+
bytes[0] === 0x52 &&
|
|
226
|
+
bytes[1] === 0x49 &&
|
|
227
|
+
bytes[2] === 0x46 &&
|
|
228
|
+
bytes[3] === 0x46 &&
|
|
229
|
+
bytes[8] === 0x57 &&
|
|
230
|
+
bytes[9] === 0x45 &&
|
|
231
|
+
bytes[10] === 0x42 &&
|
|
232
|
+
bytes[11] === 0x50
|
|
233
|
+
) {
|
|
234
|
+
if (
|
|
235
|
+
bytes.byteLength >= 30 &&
|
|
236
|
+
bytes[12] === 0x56 &&
|
|
237
|
+
bytes[13] === 0x50 &&
|
|
238
|
+
bytes[14] === 0x38 &&
|
|
239
|
+
bytes[15] === 0x20 &&
|
|
240
|
+
bytes[23] === 0x9d &&
|
|
241
|
+
bytes[24] === 0x01 &&
|
|
242
|
+
bytes[25] === 0x2a
|
|
243
|
+
) {
|
|
244
|
+
const width = view.getUint16(26, true) & 0x3fff;
|
|
245
|
+
const height = view.getUint16(28, true) & 0x3fff;
|
|
246
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
247
|
+
}
|
|
248
|
+
if (bytes[12] === 0x56 && bytes[13] === 0x50 && bytes[14] === 0x38 && bytes[15] === 0x4c && bytes[20] === 0x2f) {
|
|
249
|
+
const b0 = bytes[21],
|
|
250
|
+
b1 = bytes[22],
|
|
251
|
+
b2 = bytes[23],
|
|
252
|
+
b3 = bytes[24];
|
|
253
|
+
const width = 1 + (b0 | ((b1 & 0x3f) << 8));
|
|
254
|
+
const height = 1 + ((b1 >> 6) | (b2 << 2) | ((b3 & 0x0f) << 10));
|
|
255
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
256
|
+
}
|
|
257
|
+
if (bytes.byteLength >= 30 && bytes[12] === 0x56 && bytes[13] === 0x50 && bytes[14] === 0x38 && bytes[15] === 0x58) {
|
|
258
|
+
const width = 1 + (bytes[24] | (bytes[25] << 8) | (bytes[26] << 16));
|
|
259
|
+
const height = 1 + (bytes[27] | (bytes[28] << 8) | (bytes[29] << 16));
|
|
260
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (bytes[0] === 0xff && bytes[1] === 0xd8) {
|
|
264
|
+
let pos = 2;
|
|
265
|
+
for (let steps = 0; steps < 512 && pos + 3 < bytes.byteLength; steps++) {
|
|
266
|
+
if (bytes[pos] !== 0xff) break;
|
|
267
|
+
while (pos < bytes.byteLength && bytes[pos] === 0xff) pos++;
|
|
268
|
+
if (pos >= bytes.byteLength) break;
|
|
269
|
+
const marker = bytes[pos++];
|
|
270
|
+
if (marker === 0xd9 || marker === 0xda) break;
|
|
271
|
+
if (marker === 0x00 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue;
|
|
272
|
+
if (pos + 1 >= bytes.byteLength) break;
|
|
273
|
+
const segLen = (bytes[pos] << 8) | bytes[pos + 1];
|
|
274
|
+
if (segLen < 2) break;
|
|
275
|
+
if ((marker >= 0xc0 && marker <= 0xc3) || (marker >= 0xc5 && marker <= 0xc7) || (marker >= 0xc9 && marker <= 0xcb) || (marker >= 0xcd && marker <= 0xcf)) {
|
|
276
|
+
if (pos + 6 >= bytes.byteLength) break;
|
|
277
|
+
const height = (bytes[pos + 3] << 8) | bytes[pos + 4];
|
|
278
|
+
const width = (bytes[pos + 5] << 8) | bytes[pos + 6];
|
|
279
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
280
|
+
}
|
|
281
|
+
pos += segLen;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
exports.parseImageDimensions = parseImageDimensions;
|
|
287
|
+
|
|
288
|
+
function shouldPreFlightViaTicket(bytes, size, format, channels, crop) {
|
|
289
|
+
if (!bytes) return false;
|
|
290
|
+
if (format !== "png" && format !== "auto" && format !== "zip") return false;
|
|
291
|
+
if (channels === "alpha") return false;
|
|
292
|
+
const dims = parseImageDimensions(bytes);
|
|
293
|
+
if (!dims) return false;
|
|
294
|
+
const inputPixels = dims.width * dims.height;
|
|
295
|
+
let effectivePixels = inputPixels;
|
|
296
|
+
if (size === "preview" || size === "small" || size === "regular") effectivePixels = Math.min(inputPixels, 250_000);
|
|
297
|
+
else if (size === "medium") effectivePixels = Math.min(inputPixels, 1_500_000);
|
|
298
|
+
else if (size === "hd") effectivePixels = Math.min(inputPixels, 4_000_000);
|
|
299
|
+
else if (size === "4k") effectivePixels = Math.min(inputPixels, 10_000_000);
|
|
300
|
+
return effectivePixels > (crop ? DIRECT_CROPPED_RGBA_PNG_PIXELS : DIRECT_RGBA_PNG_PIXELS);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function sha256Hex(buf) {
|
|
304
|
+
return crypto.createHash("sha256").update(buf).digest("hex");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function deriveSubIdempotencyKey(idem, suffix) {
|
|
308
|
+
const trimmed = String(idem || "").trim();
|
|
309
|
+
const hash = sha256Hex(Buffer.from(trimmed, "utf8")).slice(0, 32);
|
|
310
|
+
return `${trimmed.slice(0, 180)}:${hash}:${suffix}`;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function buildMultipartFormData(fields, filePart) {
|
|
314
|
+
const boundary = `----RemoveAcN8n${crypto.randomBytes(16).toString("hex")}`;
|
|
315
|
+
const chunks = [];
|
|
316
|
+
|
|
317
|
+
if (filePart) {
|
|
318
|
+
const safeName = String(filePart.fileName || "image.png").replace(/[\r\n"\\]/g, "_");
|
|
319
|
+
const safeMime = String(filePart.mimeType || "application/octet-stream").replace(/[\r\n]/g, "");
|
|
320
|
+
chunks.push(
|
|
321
|
+
Buffer.from(
|
|
322
|
+
`--${boundary}\r\nContent-Disposition: form-data; name="${filePart.fieldName}"; filename="${safeName}"\r\nContent-Type: ${safeMime}\r\n\r\n`,
|
|
323
|
+
"utf8"
|
|
324
|
+
)
|
|
325
|
+
);
|
|
326
|
+
chunks.push(Buffer.from(filePart.buffer));
|
|
327
|
+
chunks.push(Buffer.from("\r\n", "utf8"));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
for (const [k, v] of Object.entries(fields)) {
|
|
331
|
+
if (v === undefined || v === null || v === "") continue;
|
|
332
|
+
const safeKey = String(k).replace(/[\r\n"\\]/g, "_");
|
|
333
|
+
chunks.push(
|
|
334
|
+
Buffer.from(
|
|
335
|
+
`--${boundary}\r\nContent-Disposition: form-data; name="${safeKey}"\r\n\r\n${String(v)}\r\n`,
|
|
336
|
+
"utf8"
|
|
337
|
+
)
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
chunks.push(Buffer.from(`--${boundary}--\r\n`, "utf8"));
|
|
342
|
+
return {
|
|
343
|
+
boundary,
|
|
344
|
+
body: Buffer.concat(chunks),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function readHeader(headers, name) {
|
|
349
|
+
if (!headers) return "";
|
|
350
|
+
const lower = name.toLowerCase();
|
|
351
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
352
|
+
if (k.toLowerCase() === lower) {
|
|
353
|
+
return Array.isArray(v) ? String(v[0] || "") : String(v ?? "");
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return "";
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function extractTotalCredits(acct) {
|
|
360
|
+
const top = acct && acct.data && acct.data.attributes ? acct.data.attributes : acct;
|
|
361
|
+
if (top && top.credits && typeof top.credits.total === "number") return top.credits.total;
|
|
362
|
+
if (top && typeof top.totalCredits === "number") return top.totalCredits;
|
|
363
|
+
if (top && typeof top.total_credits === "number") return top.total_credits;
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
class RemoveAc {
|
|
368
|
+
constructor() {
|
|
369
|
+
this.description = {
|
|
370
|
+
displayName: "remove.ac",
|
|
371
|
+
name: "removeAc",
|
|
372
|
+
icon: "file:removeac.svg",
|
|
373
|
+
group: ["transform"],
|
|
374
|
+
version: 1,
|
|
375
|
+
subtitle: '={{$parameter["operation"]}}',
|
|
376
|
+
description:
|
|
377
|
+
"Remove image backgrounds via remove.ac (/v1.0/removebg drop-in API + up to 25 MB zero-disk GPU streaming).",
|
|
378
|
+
defaults: {
|
|
379
|
+
name: "remove.ac",
|
|
380
|
+
},
|
|
381
|
+
inputs: ["main"],
|
|
382
|
+
outputs: ["main"],
|
|
383
|
+
usableAsTool: true,
|
|
384
|
+
credentials: [
|
|
385
|
+
{
|
|
386
|
+
name: "removeAcApi",
|
|
387
|
+
required: true,
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
properties: [
|
|
391
|
+
{
|
|
392
|
+
displayName: "Operation",
|
|
393
|
+
name: "operation",
|
|
394
|
+
type: "options",
|
|
395
|
+
noDataExpression: true,
|
|
396
|
+
options: [
|
|
397
|
+
{
|
|
398
|
+
name: "Remove Background",
|
|
399
|
+
value: "removeBackground",
|
|
400
|
+
description: "Remove background from a binary image or public URL",
|
|
401
|
+
action: "Remove background from an image",
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
name: "Get Account Balance",
|
|
405
|
+
value: "getAccount",
|
|
406
|
+
description: "Inspect remaining remove.ac API credits",
|
|
407
|
+
action: "Get remove.ac account balance",
|
|
408
|
+
},
|
|
409
|
+
],
|
|
410
|
+
default: "removeBackground",
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
displayName: "Input Source",
|
|
414
|
+
name: "inputSource",
|
|
415
|
+
type: "options",
|
|
416
|
+
displayOptions: {
|
|
417
|
+
show: {
|
|
418
|
+
operation: ["removeBackground"],
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
options: [
|
|
422
|
+
{
|
|
423
|
+
name: "Binary Data",
|
|
424
|
+
value: "binary",
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
name: "Image URL",
|
|
428
|
+
value: "url",
|
|
429
|
+
},
|
|
430
|
+
],
|
|
431
|
+
default: "binary",
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
displayName: "Input Binary Field",
|
|
435
|
+
name: "binaryPropertyName",
|
|
436
|
+
type: "string",
|
|
437
|
+
default: "data",
|
|
438
|
+
required: true,
|
|
439
|
+
displayOptions: {
|
|
440
|
+
show: {
|
|
441
|
+
operation: ["removeBackground"],
|
|
442
|
+
inputSource: ["binary"],
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
description: "Name of the binary property containing the source image.",
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
displayName: "Image URL",
|
|
449
|
+
name: "imageUrl",
|
|
450
|
+
type: "string",
|
|
451
|
+
default: "",
|
|
452
|
+
required: true,
|
|
453
|
+
placeholder: "https://example.com/product.jpg",
|
|
454
|
+
displayOptions: {
|
|
455
|
+
show: {
|
|
456
|
+
operation: ["removeBackground"],
|
|
457
|
+
inputSource: ["url"],
|
|
458
|
+
},
|
|
459
|
+
},
|
|
460
|
+
description: "Public HTTP/HTTPS URL of the image to process.",
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
displayName: "Output Binary Field",
|
|
464
|
+
name: "outputBinaryPropertyName",
|
|
465
|
+
type: "string",
|
|
466
|
+
default: "data",
|
|
467
|
+
required: true,
|
|
468
|
+
displayOptions: {
|
|
469
|
+
show: {
|
|
470
|
+
operation: ["removeBackground"],
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
description: "Binary property name where the cutout image will be stored.",
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
displayName: "Size",
|
|
477
|
+
name: "size",
|
|
478
|
+
type: "options",
|
|
479
|
+
displayOptions: {
|
|
480
|
+
show: {
|
|
481
|
+
operation: ["removeBackground"],
|
|
482
|
+
},
|
|
483
|
+
},
|
|
484
|
+
options: [
|
|
485
|
+
{ name: "Full (Up to 25 MB / 40 MP)", value: "full" },
|
|
486
|
+
{ name: "Auto", value: "auto" },
|
|
487
|
+
{ name: "4K (Up to 10 MP)", value: "4k" },
|
|
488
|
+
{ name: "HD (Up to 4 MP)", value: "hd" },
|
|
489
|
+
{ name: "Medium (Up to 1.5 MP)", value: "medium" },
|
|
490
|
+
{ name: "Regular (Up to 0.25 MP)", value: "regular" },
|
|
491
|
+
{ name: "Small (Up to 0.25 MP)", value: "small" },
|
|
492
|
+
{ name: "Preview (Up to 0.25 MP)", value: "preview" },
|
|
493
|
+
],
|
|
494
|
+
default: "full",
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
displayName: "Format",
|
|
498
|
+
name: "format",
|
|
499
|
+
type: "options",
|
|
500
|
+
displayOptions: {
|
|
501
|
+
show: {
|
|
502
|
+
operation: ["removeBackground"],
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
options: [
|
|
506
|
+
{ name: "PNG (Transparent)", value: "png" },
|
|
507
|
+
{ name: "WebP", value: "webp" },
|
|
508
|
+
{ name: "JPG", value: "jpg" },
|
|
509
|
+
{ name: "ZIP", value: "zip" },
|
|
510
|
+
],
|
|
511
|
+
default: "png",
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
displayName: "Additional Options",
|
|
515
|
+
name: "additionalOptions",
|
|
516
|
+
type: "collection",
|
|
517
|
+
placeholder: "Add Option",
|
|
518
|
+
default: {},
|
|
519
|
+
displayOptions: {
|
|
520
|
+
show: {
|
|
521
|
+
operation: ["removeBackground"],
|
|
522
|
+
},
|
|
523
|
+
},
|
|
524
|
+
options: [
|
|
525
|
+
{
|
|
526
|
+
displayName: "Background Color (Hex)",
|
|
527
|
+
name: "bgColor",
|
|
528
|
+
type: "string",
|
|
529
|
+
default: "",
|
|
530
|
+
placeholder: "FFFFFF",
|
|
531
|
+
description: "Solid hex background color (e.g. FFFFFF for marketplace white).",
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
displayName: "Crop Transparent Padding",
|
|
535
|
+
name: "crop",
|
|
536
|
+
type: "boolean",
|
|
537
|
+
default: false,
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
displayName: "Foreground Type",
|
|
541
|
+
name: "type",
|
|
542
|
+
type: "options",
|
|
543
|
+
options: [
|
|
544
|
+
{ name: "Auto", value: "auto" },
|
|
545
|
+
{ name: "Product", value: "product" },
|
|
546
|
+
{ name: "Person", value: "person" },
|
|
547
|
+
{ name: "Car", value: "car" },
|
|
548
|
+
{ name: "Animal", value: "animal" },
|
|
549
|
+
{ name: "Graphic", value: "graphic" },
|
|
550
|
+
{ name: "Transportation", value: "transportation" },
|
|
551
|
+
],
|
|
552
|
+
default: "auto",
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
displayName: "Add Shadow",
|
|
556
|
+
name: "addShadow",
|
|
557
|
+
type: "boolean",
|
|
558
|
+
default: false,
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
displayName: "Shadow Type",
|
|
562
|
+
name: "shadowType",
|
|
563
|
+
type: "options",
|
|
564
|
+
options: [
|
|
565
|
+
{ name: "Auto", value: "auto" },
|
|
566
|
+
{ name: "Contact", value: "contact" },
|
|
567
|
+
{ name: "Drop", value: "drop" },
|
|
568
|
+
{ name: "Soft", value: "soft" },
|
|
569
|
+
{ name: "Car", value: "car" },
|
|
570
|
+
{ name: "Reflection", value: "reflection" },
|
|
571
|
+
{ name: "None", value: "none" },
|
|
572
|
+
],
|
|
573
|
+
default: "auto",
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
displayName: "Channels",
|
|
577
|
+
name: "channels",
|
|
578
|
+
type: "options",
|
|
579
|
+
options: [
|
|
580
|
+
{ name: "RGBA", value: "rgba" },
|
|
581
|
+
{ name: "Alpha Matte Only", value: "alpha" },
|
|
582
|
+
],
|
|
583
|
+
default: "rgba",
|
|
584
|
+
},
|
|
585
|
+
],
|
|
586
|
+
},
|
|
587
|
+
],
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
async execute() {
|
|
592
|
+
const items = this.getInputData();
|
|
593
|
+
const returnData = [];
|
|
594
|
+
const creds = await this.getCredentials("removeAcApi");
|
|
595
|
+
const apiKey = String((creds && creds.apiKey) || "").trim();
|
|
596
|
+
if (!apiKey) {
|
|
597
|
+
throw new Error("Missing remove.ac API key in credentials.");
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
const rawBaseUrl = String((creds && creds.baseUrl) || DEFAULT_BASE_URL).trim();
|
|
601
|
+
const lookupFn = this._dnsLookupForTest || dns.promises.lookup;
|
|
602
|
+
const allowLoopback = Boolean(this._allowLoopbackBaseUrlForTest);
|
|
603
|
+
|
|
604
|
+
let baseUrl;
|
|
605
|
+
if (allowLoopback) {
|
|
606
|
+
baseUrl = new URL(rawBaseUrl).origin;
|
|
607
|
+
} else {
|
|
608
|
+
const parsedBase = await validatePublicUrl(rawBaseUrl, {
|
|
609
|
+
requireHttps: true,
|
|
610
|
+
lookupFn,
|
|
611
|
+
});
|
|
612
|
+
baseUrl = parsedBase.origin;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
for (let i = 0; i < items.length; i++) {
|
|
616
|
+
try {
|
|
617
|
+
const operation = this.getNodeParameter("operation", i);
|
|
618
|
+
|
|
619
|
+
if (operation === "getAccount") {
|
|
620
|
+
const acctResp = await this.helpers.httpRequest({
|
|
621
|
+
method: "GET",
|
|
622
|
+
url: `${baseUrl}/v1.0/account`,
|
|
623
|
+
headers: {
|
|
624
|
+
"X-Api-Key": apiKey,
|
|
625
|
+
Accept: "application/json",
|
|
626
|
+
},
|
|
627
|
+
json: true,
|
|
628
|
+
disableFollowRedirect: true,
|
|
629
|
+
sendCredentialsOnCrossOriginRedirect: false,
|
|
630
|
+
returnFullResponse: true,
|
|
631
|
+
ignoreHttpStatusErrors: true,
|
|
632
|
+
});
|
|
633
|
+
const acctStatus =
|
|
634
|
+
acctResp && typeof acctResp.statusCode === "number" ? acctResp.statusCode : 200;
|
|
635
|
+
const acctRawBody =
|
|
636
|
+
acctResp && typeof acctResp === "object" && "statusCode" in acctResp
|
|
637
|
+
? acctResp.body
|
|
638
|
+
: acctResp;
|
|
639
|
+
if (acctStatus < 200 || acctStatus >= 300) {
|
|
640
|
+
const errTxt =
|
|
641
|
+
typeof acctRawBody === "string"
|
|
642
|
+
? acctRawBody
|
|
643
|
+
: Buffer.isBuffer(acctRawBody)
|
|
644
|
+
? acctRawBody.toString("utf8")
|
|
645
|
+
: JSON.stringify(acctRawBody ?? "");
|
|
646
|
+
throw new Error(`[HTTP ${acctStatus}] ${errTxt}`);
|
|
647
|
+
}
|
|
648
|
+
const bodyObj = typeof acctRawBody === "string" ? JSON.parse(acctRawBody) : acctRawBody;
|
|
649
|
+
const totalCredits = extractTotalCredits(bodyObj);
|
|
650
|
+
returnData.push({
|
|
651
|
+
json: {
|
|
652
|
+
totalCredits,
|
|
653
|
+
account: bodyObj,
|
|
654
|
+
},
|
|
655
|
+
pairedItem: { item: i },
|
|
656
|
+
});
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const inputSource = this.getNodeParameter("inputSource", i);
|
|
661
|
+
const outputBinaryPropertyName = this.getNodeParameter("outputBinaryPropertyName", i);
|
|
662
|
+
const size = this.getNodeParameter("size", i);
|
|
663
|
+
const format = this.getNodeParameter("format", i);
|
|
664
|
+
const additional = this.getNodeParameter("additionalOptions", i, {}) || {};
|
|
665
|
+
const channels = additional.channels || "rgba";
|
|
666
|
+
const crop = Boolean(additional.crop);
|
|
667
|
+
const idem = `n8n-${crypto.randomUUID()}`;
|
|
668
|
+
|
|
669
|
+
let sourceBytes = null;
|
|
670
|
+
let sourceFileName = "image.png";
|
|
671
|
+
let sourceMimeType = "image/png";
|
|
672
|
+
let sourceUrl = null;
|
|
673
|
+
|
|
674
|
+
if (inputSource === "binary") {
|
|
675
|
+
const binaryPropertyName = this.getNodeParameter("binaryPropertyName", i);
|
|
676
|
+
const binaryMeta = this.helpers.assertBinaryData(i, binaryPropertyName);
|
|
677
|
+
sourceBytes = Buffer.from(await this.helpers.getBinaryDataBuffer(i, binaryPropertyName));
|
|
678
|
+
if (sourceBytes.byteLength === 0) {
|
|
679
|
+
throw new Error(`Input binary property '${binaryPropertyName}' is empty.`);
|
|
680
|
+
}
|
|
681
|
+
if (sourceBytes.byteLength > MAX_UPLOAD_BYTES) {
|
|
682
|
+
throw new Error(
|
|
683
|
+
`Input image exceeds remove.ac 25 MB limit (${sourceBytes.byteLength} bytes).`
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
sourceFileName = binaryMeta.fileName || "image.png";
|
|
687
|
+
sourceMimeType = binaryMeta.mimeType || "application/octet-stream";
|
|
688
|
+
} else {
|
|
689
|
+
const rawImageUrl = String(this.getNodeParameter("imageUrl", i) || "").trim();
|
|
690
|
+
const validated = await validatePublicUrl(rawImageUrl, {
|
|
691
|
+
requireHttps: false,
|
|
692
|
+
lookupFn,
|
|
693
|
+
});
|
|
694
|
+
sourceUrl = validated.toString();
|
|
695
|
+
const urlPath = validated.pathname.split("/").pop() || "remote-image";
|
|
696
|
+
sourceFileName = urlPath;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const optionFields = {
|
|
700
|
+
size,
|
|
701
|
+
format,
|
|
702
|
+
channels,
|
|
703
|
+
...(additional.type && additional.type !== "auto" ? { type: additional.type } : {}),
|
|
704
|
+
...(additional.bgColor ? { bg_color: String(additional.bgColor).replace(/^#/, "") } : {}),
|
|
705
|
+
...(additional.crop !== undefined ? { crop: String(Boolean(additional.crop)) } : {}),
|
|
706
|
+
...(additional.addShadow !== undefined ? { add_shadow: String(Boolean(additional.addShadow)) } : {}),
|
|
707
|
+
...(additional.shadowType && additional.addShadow ? { shadow_type: additional.shadowType } : {}),
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
const runTicketStream = async (suffix) => {
|
|
711
|
+
const ticketPayload = {
|
|
712
|
+
foregroundSha256: sha256Hex(sourceBytes),
|
|
713
|
+
foregroundByteLength: sourceBytes.byteLength,
|
|
714
|
+
size,
|
|
715
|
+
format,
|
|
716
|
+
channels,
|
|
717
|
+
...(additional.type && additional.type !== "auto" ? { type: additional.type } : {}),
|
|
718
|
+
...(additional.bgColor ? { bg_color: String(additional.bgColor).replace(/^#/, "") } : {}),
|
|
719
|
+
...(additional.crop !== undefined ? { crop: Boolean(additional.crop) } : {}),
|
|
720
|
+
...(additional.addShadow !== undefined ? { add_shadow: Boolean(additional.addShadow) } : {}),
|
|
721
|
+
...(additional.shadowType && additional.addShadow ? { shadow_type: additional.shadowType } : {}),
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
const ticketRes = await this.helpers.httpRequest({
|
|
725
|
+
method: "POST",
|
|
726
|
+
url: `${baseUrl}/v1.0/removebg/ticket`,
|
|
727
|
+
headers: {
|
|
728
|
+
"Content-Type": "application/json",
|
|
729
|
+
"X-Api-Key": apiKey,
|
|
730
|
+
"Idempotency-Key": deriveSubIdempotencyKey(idem, suffix),
|
|
731
|
+
},
|
|
732
|
+
body: ticketPayload,
|
|
733
|
+
json: true,
|
|
734
|
+
disableFollowRedirect: true,
|
|
735
|
+
sendCredentialsOnCrossOriginRedirect: false,
|
|
736
|
+
returnFullResponse: true,
|
|
737
|
+
ignoreHttpStatusErrors: true,
|
|
738
|
+
});
|
|
739
|
+
const ticketStatus =
|
|
740
|
+
ticketRes && typeof ticketRes.statusCode === "number" ? ticketRes.statusCode : 200;
|
|
741
|
+
const ticketRawBody =
|
|
742
|
+
ticketRes && typeof ticketRes === "object" && "statusCode" in ticketRes
|
|
743
|
+
? ticketRes.body
|
|
744
|
+
: ticketRes;
|
|
745
|
+
if (ticketStatus < 200 || ticketStatus >= 300) {
|
|
746
|
+
const errTxt =
|
|
747
|
+
typeof ticketRawBody === "string"
|
|
748
|
+
? ticketRawBody
|
|
749
|
+
: Buffer.isBuffer(ticketRawBody)
|
|
750
|
+
? ticketRawBody.toString("utf8")
|
|
751
|
+
: JSON.stringify(ticketRawBody ?? "");
|
|
752
|
+
throw new Error(`[HTTP ${ticketStatus}] ${errTxt}`);
|
|
753
|
+
}
|
|
754
|
+
const tJson = typeof ticketRawBody === "string" ? JSON.parse(ticketRawBody) : ticketRawBody;
|
|
755
|
+
if (!tJson || !tJson.data || !tJson.data.directEndpoint || !tJson.data.ticket) {
|
|
756
|
+
throw new Error("Invalid /v1.0/removebg/ticket response from remove.ac");
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const directEndpoint = String(tJson.data.directEndpoint);
|
|
760
|
+
if (!allowLoopback) {
|
|
761
|
+
await validatePublicUrl(directEndpoint, {
|
|
762
|
+
requireHttps: true,
|
|
763
|
+
lookupFn,
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
const mp = buildMultipartFormData({}, {
|
|
768
|
+
fieldName: "image_file",
|
|
769
|
+
fileName: sourceFileName,
|
|
770
|
+
mimeType: sourceMimeType,
|
|
771
|
+
buffer: sourceBytes,
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
const dRes = await this.helpers.httpRequest({
|
|
775
|
+
method: "POST",
|
|
776
|
+
url: directEndpoint,
|
|
777
|
+
headers: {
|
|
778
|
+
Authorization: `Bearer ${tJson.data.ticket}`,
|
|
779
|
+
"Content-Type": `multipart/form-data; boundary=${mp.boundary}`,
|
|
780
|
+
},
|
|
781
|
+
body: mp.body,
|
|
782
|
+
encoding: "arraybuffer",
|
|
783
|
+
json: false,
|
|
784
|
+
disableFollowRedirect: true,
|
|
785
|
+
sendCredentialsOnCrossOriginRedirect: false,
|
|
786
|
+
returnFullResponse: true,
|
|
787
|
+
ignoreHttpStatusErrors: true,
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
if (dRes.statusCode < 200 || dRes.statusCode >= 300) {
|
|
791
|
+
const errTxt = Buffer.from(dRes.body || "").toString("utf8");
|
|
792
|
+
throw new Error(`[HTTP ${dRes.statusCode}] ${errTxt}`);
|
|
793
|
+
}
|
|
794
|
+
return dRes;
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
let rawResponse;
|
|
798
|
+
const needsTicketUpfront =
|
|
799
|
+
sourceBytes &&
|
|
800
|
+
(sourceBytes.byteLength > DIRECT_THRESHOLD_BYTES ||
|
|
801
|
+
shouldPreFlightViaTicket(sourceBytes, size, format, channels, crop));
|
|
802
|
+
|
|
803
|
+
if (needsTicketUpfront) {
|
|
804
|
+
rawResponse = await runTicketStream("direct");
|
|
805
|
+
} else {
|
|
806
|
+
const mp = sourceUrl
|
|
807
|
+
? buildMultipartFormData({ ...optionFields, image_url: sourceUrl }, null)
|
|
808
|
+
: buildMultipartFormData(optionFields, {
|
|
809
|
+
fieldName: "image_file",
|
|
810
|
+
fileName: sourceFileName,
|
|
811
|
+
mimeType: sourceMimeType,
|
|
812
|
+
buffer: sourceBytes,
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
const directRes = await this.helpers.httpRequest({
|
|
816
|
+
method: "POST",
|
|
817
|
+
url: `${baseUrl}/v1.0/removebg`,
|
|
818
|
+
headers: {
|
|
819
|
+
"X-Api-Key": apiKey,
|
|
820
|
+
"Idempotency-Key": idem,
|
|
821
|
+
"Content-Type": `multipart/form-data; boundary=${mp.boundary}`,
|
|
822
|
+
},
|
|
823
|
+
body: mp.body,
|
|
824
|
+
encoding: "arraybuffer",
|
|
825
|
+
json: false,
|
|
826
|
+
disableFollowRedirect: true,
|
|
827
|
+
sendCredentialsOnCrossOriginRedirect: false,
|
|
828
|
+
returnFullResponse: true,
|
|
829
|
+
ignoreHttpStatusErrors: true,
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
if (directRes.statusCode === 413 && sourceBytes) {
|
|
833
|
+
rawResponse = await runTicketStream("fallback413");
|
|
834
|
+
} else if (directRes.statusCode < 200 || directRes.statusCode >= 300) {
|
|
835
|
+
const errTxt = Buffer.from(directRes.body || "").toString("utf8");
|
|
836
|
+
throw new Error(`[HTTP ${directRes.statusCode}] ${errTxt}`);
|
|
837
|
+
} else {
|
|
838
|
+
rawResponse = directRes;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const cutoutBuffer = Buffer.from(rawResponse.body);
|
|
843
|
+
const respHeaders = rawResponse.headers || {};
|
|
844
|
+
const outExt =
|
|
845
|
+
format === "jpg" ? "jpg" : format === "webp" ? "webp" : format === "zip" ? "zip" : "png";
|
|
846
|
+
const defaultMime =
|
|
847
|
+
format === "jpg"
|
|
848
|
+
? "image/jpeg"
|
|
849
|
+
: format === "webp"
|
|
850
|
+
? "image/webp"
|
|
851
|
+
: format === "zip"
|
|
852
|
+
? "application/zip"
|
|
853
|
+
: "image/png";
|
|
854
|
+
const outMime = readHeader(respHeaders, "content-type") || defaultMime;
|
|
855
|
+
const stem = sourceFileName.replace(/\.[^.]+$/, "") || "image";
|
|
856
|
+
const outFileName = `${stem}-nobg.${outExt}`;
|
|
857
|
+
|
|
858
|
+
const preparedBinary = await this.helpers.prepareBinaryData(
|
|
859
|
+
cutoutBuffer,
|
|
860
|
+
outFileName,
|
|
861
|
+
outMime
|
|
862
|
+
);
|
|
863
|
+
|
|
864
|
+
returnData.push({
|
|
865
|
+
json: {
|
|
866
|
+
...(items[i].json || {}),
|
|
867
|
+
removeAc: {
|
|
868
|
+
creditsCharged: Number.parseFloat(readHeader(respHeaders, "x-credits-charged") || "0"),
|
|
869
|
+
width: Number.parseInt(readHeader(respHeaders, "x-width") || "0", 10),
|
|
870
|
+
height: Number.parseInt(readHeader(respHeaders, "x-height") || "0", 10),
|
|
871
|
+
foregroundTop: Number.parseInt(readHeader(respHeaders, "x-foreground-top") || "0", 10),
|
|
872
|
+
foregroundLeft: Number.parseInt(readHeader(respHeaders, "x-foreground-left") || "0", 10),
|
|
873
|
+
foregroundWidth: Number.parseInt(readHeader(respHeaders, "x-foreground-width") || "0", 10),
|
|
874
|
+
foregroundHeight: Number.parseInt(readHeader(respHeaders, "x-foreground-height") || "0", 10),
|
|
875
|
+
bytes: cutoutBuffer.byteLength,
|
|
876
|
+
mimeType: outMime,
|
|
877
|
+
fileName: outFileName,
|
|
878
|
+
},
|
|
879
|
+
},
|
|
880
|
+
binary: {
|
|
881
|
+
...(items[i].binary || {}),
|
|
882
|
+
[outputBinaryPropertyName]: preparedBinary,
|
|
883
|
+
},
|
|
884
|
+
pairedItem: { item: i },
|
|
885
|
+
});
|
|
886
|
+
} catch (err) {
|
|
887
|
+
if (this.continueOnFail()) {
|
|
888
|
+
returnData.push({
|
|
889
|
+
json: {
|
|
890
|
+
...(items[i].json || {}),
|
|
891
|
+
error: err instanceof Error ? err.message : String(err),
|
|
892
|
+
},
|
|
893
|
+
pairedItem: { item: i },
|
|
894
|
+
});
|
|
895
|
+
continue;
|
|
896
|
+
}
|
|
897
|
+
throw err;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
return [returnData];
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
exports.RemoveAc = RemoveAc;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" fill="none">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#4F46E5"/>
|
|
3
|
+
<rect x="10" y="10" width="20" height="20" rx="4" fill="#E0E7FF" fill-opacity="0.22"/>
|
|
4
|
+
<rect x="34" y="34" width="20" height="20" rx="4" fill="#E0E7FF" fill-opacity="0.22"/>
|
|
5
|
+
<circle cx="23" cy="43" r="6" stroke="#FFFFFF" stroke-width="3.5"/>
|
|
6
|
+
<circle cx="41" cy="43" r="6" stroke="#FFFFFF" stroke-width="3.5"/>
|
|
7
|
+
<path d="M27.5 38.5L45 17M36.5 38.5L19 17" stroke="#FFFFFF" stroke-width="3.5" stroke-linecap="round"/>
|
|
8
|
+
</svg>
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "n8n-nodes-remove-ac",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official zero-dependency n8n community node for remove.ac (/v1.0/removebg drop-in background removal + up to 25 MB zero-disk GPU streaming).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"n8n-community-node-package",
|
|
7
|
+
"remove.ac",
|
|
8
|
+
"remove.bg",
|
|
9
|
+
"background-removal",
|
|
10
|
+
"image-segmentation",
|
|
11
|
+
"cutout",
|
|
12
|
+
"ecommerce"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"homepage": "https://remove.ac/plugins",
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "remove.ac",
|
|
18
|
+
"url": "https://remove.ac"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/szp2005/n8n-nodes-remove-ac.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/szp2005/n8n-nodes-remove-ac/issues"
|
|
26
|
+
},
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "node --test test/n8n-node.test.mjs"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"index.js",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"n8n": {
|
|
38
|
+
"n8nNodesApiVersion": 1,
|
|
39
|
+
"credentials": [
|
|
40
|
+
"dist/credentials/RemoveAcApi.credentials.js"
|
|
41
|
+
],
|
|
42
|
+
"nodes": [
|
|
43
|
+
"dist/nodes/RemoveAc/RemoveAc.node.js"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.10.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"n8n-workflow": "*"
|
|
51
|
+
}
|
|
52
|
+
}
|