Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize #528

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"parserOptions": {
"requireConfigFile": false
},
"parser": "@babel/eslint-parser",
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
"quotes": [2, "single"],
Expand Down Expand Up @@ -38,6 +42,8 @@
"no-mixed-requires": 0
},
"env": {
"node": true
"node": true,
"jest": true,
"es6": true
}
}
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test

on: [push, pull_request, workflow_dispatch]

jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: ['12', '14', '16']

steps:
# Windows fix. See https://github.com/actions/checkout/issues/226
- run: git config --global core.autocrlf false

- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}

- run: yarn

- run: npm run lint
- run: npm test
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
.DS_Store
.idea

sftp-config.json
yarn.lock
package-lock.json

coverage/
node_modules/
Expand Down
16 changes: 0 additions & 16 deletions .istanbul.yml

This file was deleted.

4 changes: 0 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
.*

Makefile
appveyor.yml
sftp-config.json

coverage/
node_modules/
test/
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

5 changes: 1 addition & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface]

[xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface

[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:6.x-12.x)](https://travis-ci.org/form-data/form-data)
[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:6.x-12.x)](https://travis-ci.org/form-data/form-data)
[![Windows Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=windows:6.x-12.x)](https://travis-ci.org/form-data/form-data)
[![Test](https://github.com/mifi/form-data/actions/workflows/test.yml/badge.svg?branch=modernize)](https://github.com/mifi/form-data/actions/workflows/test.yml)

[![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master)
[![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)

## Install

Expand Down
2 changes: 0 additions & 2 deletions lib/browser.js

This file was deleted.

60 changes: 22 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,13 @@
"url": "git://github.com/form-data/form-data.git"
},
"main": "./lib/form_data",
"browser": "./lib/browser",
"typings": "./index.d.ts",
"scripts": {
"pretest": "rimraf coverage test/tmp",
"test": "istanbul cover test/run.js",
"posttest": "istanbul report lcov text",
"lint": "eslint lib/*.js test/*.js test/integration/*.js",
"report": "istanbul report lcov text",
"ci-lint": "is-node-modern 8 && npm run lint || is-node-not-modern 8",
"ci-test": "npm run test && npm run browser && npm run report",
"predebug": "rimraf coverage test/tmp",
"debug": "verbose=1 ./test/run.js",
"browser": "browserify -t browserify-istanbul test/run-browser.js | obake --coverage",
"check": "istanbul check-coverage coverage/coverage*.json",
"files": "pkgfiles --sort=name",
"get-version": "node -e \"console.log(require('./package.json').version)\"",
"update-readme": "sed -i.bak 's/\\/master\\.svg/\\/v'$(npm --silent run get-version)'.svg/g' README.md",
"restore-readme": "mv README.md.bak README.md",
"prepublish": "in-publish && npm run update-readme || not-in-publish",
"postpublish": "npm run restore-readme"
"test": "mkdirp test/tmp && jest --coverage --runInBand",
"lint": "eslint ."
},
"pre-commit": [
"lint",
"ci-test",
"check"
"lint"
],
"engines": {
"node": ">= 6"
Expand All @@ -43,26 +25,28 @@
"mime-types": "^2.1.12"
},
"devDependencies": {
"@babel/eslint-parser": "^7.17.0",
"@types/node": "^12.0.10",
"browserify": "^13.1.1",
"browserify-istanbul": "^2.0.0",
"coveralls": "^3.0.4",
"cross-spawn": "^6.0.5",
"eslint": "^6.0.1",
"fake": "^0.2.2",
"far": "^0.0.7",
"eslint": "^8.9.0",
"formidable": "^1.0.17",
"in-publish": "^2.0.0",
"is-node-modern": "^1.0.0",
"istanbul": "^0.4.5",
"obake": "^0.1.2",
"puppeteer": "^1.19.0",
"pkgfiles": "^2.3.0",
"jest": "^27.5.1",
"mkdirp": "^1.0.4",
"pre-commit": "^1.1.3",
"request": "^2.88.0",
"rimraf": "^2.7.1",
"tape": "^4.6.2",
"typescript": "^3.5.2"
"request": "^2.88.0"
},
"jest": {
"collectCoverageFrom": [
"lib/**/*.{js,jsx}",
"!<rootDir>/test/"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 90,
"lines": 90,
"statements": 90
}
}
},
"license": "MIT"
}
9 changes: 5 additions & 4 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var fake = require('fake');
var mime = require('mime-types');
var http = require('http');
var IncomingForm = require('formidable').IncomingForm;

var common = module.exports;

// TODO allow removing --runInBand from jest (need to randomize server port)

var rootDir = path.join(__dirname, '..');
common.dir = {
lib: path.join(rootDir, '/lib'),
Expand All @@ -16,11 +17,10 @@ common.dir = {
};

common.defaultTypeValue = function () {
return new Buffer([1, 2, 3]);
return Buffer.from([1, 2, 3]);
};

common.assert = assert;
common.fake = fake;

common.port = 8432;

Expand Down Expand Up @@ -71,7 +71,7 @@ common.actions.populateFields = function(form, fields)
};

// generic form submit
common.actions.submit = function(form, server)
common.actions.submit = function(form, server, done)
{
return form.submit('http://localhost:' + common.port + '/', function(err, res) {

Expand All @@ -85,6 +85,7 @@ common.actions.submit = function(form, server)
res.resume();

server.close();
done();
});
};

Expand Down
86 changes: 86 additions & 0 deletions test/custom-content-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var common = require('./common');
var assert = common.assert;
var http = require('http');
var mime = require('mime-types');
var fs = require('fs');
var FormData = require(common.dir.lib + '/form_data');

it('custom-content-type', (done) => {
// wrap non simple values into function
// just to deal with ReadStream "autostart"
var FIELDS = {
'no_type': {
value: 'my_value'
},
'custom_type': {
value: 'my_value',
expectedType: 'image/png',
options: {
contentType: 'image/png'
}
},
'default_type': {
expectedType: FormData.DEFAULT_CONTENT_TYPE,
value: common.defaultTypeValue
},
'implicit_type': {
expectedType: mime.lookup(common.dir.fixture + '/unicycle.jpg'),
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
},
'overridden_type': {
expectedType: 'image/png',
options: {
contentType: 'image/png'
},
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
}
};

var server = http.createServer(function(req, res) {
var body = '';
var boundry = req.headers['content-type'].split('boundary=').pop();

req.on('data', function (data) { body += data.toString('utf-8'); });
req.on('end', function () {
// Separate body into individual files/fields and remove leading and trailing content.
var fields = body.split(boundry).slice(1, -1);
var fieldNames = Object.keys(FIELDS);

assert.ok(fields.length === fieldNames.length);

for (var i = 0; i < fieldNames.length; i++) {
assert.ok(fields[i].indexOf('name="' + fieldNames[i] + '"') > -1);

if (!FIELDS[fieldNames[i]].expectedType) {
assert.equal(fields[i].indexOf('Content-Type'), -1, 'Expecting ' + fieldNames[i] + ' not to have Content-Type');
} else {
assert.ok(fields[i].indexOf('Content-Type: ' + FIELDS[fieldNames[i]].expectedType) > -1, 'Expecting ' + fieldNames[i] + ' to have Content-Type ' + FIELDS[fieldNames[i]].expectedType);
}
}

res.end();
});
});

server.listen(common.port, function() {

var form = new FormData();

var field;
for (var name in FIELDS) {
if (!FIELDS.hasOwnProperty(name)) { continue; }

field = FIELDS[name];
// important to append ReadStreams within the same tick
if ((typeof field.value == 'function')) {
field.value = field.value();
}
form.append(name, field.value, field.options);
}

// custom params object passed to submit
common.actions.submit(form, server, done);
});

});