Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

## Version History

### v11.2.0

- :rocket: Migrate from external "form-data" library to native FormData class
- :arrow_down: Remove "form-data" dependency

### v11.1.0

- :rocket: TAK Server 5.5 changed the Cookie format so this PR switches to Bearer tokens
Expand Down
3 changes: 1 addition & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import FormData from 'form-data';
import OAuth from './api/oauth.js';
import Package from './api/package.js';
import Query from './api/query.js';
Expand Down Expand Up @@ -128,7 +127,7 @@ export default class TAKAPI {
opts.body = JSON.stringify(opts.body);
opts.headers['Content-Type'] = 'application/json';
} else if (opts.body instanceof FormData) {
opts.headers = opts.body.getHeaders();
// Native FormData headers are handled automatically by fetch
} else if (opts.body instanceof URLSearchParams) {
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded'
opts.body = String(opts.body);
Expand Down
19 changes: 14 additions & 5 deletions lib/api/files.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import FormData from 'form-data';
import type { ParsedArgs } from 'minimist'
import { Readable } from 'node:stream';
import { openAsBlob } from 'node:fs';
import mime from 'mime';
import Commands, { CommandOutputFormat } from '../commands.js';
import { TAKList } from './types.js';
import { Type, Static } from '@sinclair/typebox';
import stream2buffer from '../stream.js';

export const Content = Type.Object({
UID: Type.String(),
Expand Down Expand Up @@ -118,13 +119,21 @@ export default class FileCommands extends Commands {
url.searchParams.append('creatorUid', opts.creatorUid)
url.searchParams.append('hash', opts.hash)

const form = new FormData();

if (body instanceof Buffer) {
body = Readable.from(body as Buffer);
// Handle Buffer directly
form.append('assetfile', new Blob([new Uint8Array(body)]), opts.name);
} else if (body instanceof Readable && 'path' in body && typeof body.path === 'string') {
// Use fs.openAsBlob for file streams - memory efficient
const fileBlob = await openAsBlob(body.path as string);
form.append('assetfile', fileBlob, opts.name);
} else {
// Fall back to buffer approach for other streams
const fileData = await stream2buffer(body as import('node:stream').Stream);
form.append('assetfile', new Blob([new Uint8Array(fileData)]), opts.name);
}

const form = new FormData()
form.append('assetfile', body as Readable);

const res = await this.api.fetch(url, {
method: 'POST',
body: form
Expand Down
83 changes: 16 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tak-ps/node-tak",
"type": "module",
"version": "11.1.0",
"version": "11.2.0",
"description": "Lightweight JavaScript library for communicating with TAK Server",
"author": "Nick Ingalls <[email protected]>",
"main": "dist/index.js",
Expand All @@ -26,7 +26,6 @@
},
"dependencies": {
"ajv": "^8.12.0",
"form-data": "^4.0.2",
"mime": "^4.0.7",
"p12-pem": "^1.0.5",
"pem": "^1.14.8",
Expand Down