diff --git a/README.md b/README.md index 359cb47..36a2d60 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Note, all these examples also work in the browser with [browserify](http://brows Doesn't get easier than this: ```js -const get = require('simple-get') +import get from 'simple-get' get('http://example.com', function (err, res) { if (err) throw err @@ -53,7 +53,7 @@ get('http://example.com', function (err, res) { If you just want the data, and don't want to deal with streams: ```js -const get = require('simple-get') +import get from 'simple-get' get.concat('http://example.com', function (err, res, data) { if (err) throw err @@ -67,7 +67,7 @@ get.concat('http://example.com', function (err, res, data) { For `POST`, call `get.post` or use option `{ method: 'POST' }`. ```js -const get = require('simple-get') +import get from 'simple-get' const opts = { url: 'http://example.com', @@ -82,7 +82,7 @@ get.post(opts, function (err, res) { #### A more complex example: ```js -const get = require('simple-get') +import get from 'simple-get' get({ url: 'http://example.com', @@ -117,7 +117,7 @@ get({ You can serialize/deserialize request and response with JSON: ```js -const get = require('simple-get') +import get from 'simple-get' const opts = { method: 'POST', @@ -140,7 +140,7 @@ If the request takes longer than `timeout` to complete, then the entire request will fail with an `Error`. ```js -const get = require('simple-get') +import get from 'simple-get' const opts = { url: 'http://example.com', @@ -156,12 +156,11 @@ It's a good idea to set the `'user-agent'` header so the provider can more easil see how their resource is used. ```js -const get = require('simple-get') -const pkg = require('./package.json') +import get from 'simple-get' get('http://example.com', { headers: { - 'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)` + 'user-agent': `my-module/2.1.6 (https://github.com/username/my-module)` } }) ``` @@ -172,8 +171,8 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with t `agent` option to work with proxies: ```js -const get = require('simple-get') -const tunnel = require('tunnel') +import get from 'simple-get' +import tunnel from 'tunnel' const opts = { url: 'http://example.com', @@ -193,8 +192,8 @@ You can use the [`cookie`](https://github.com/jshttp/cookie) module to include cookies in a request: ```js -const get = require('simple-get') -const cookie = require('cookie') +import get from 'simple-get' +import cookie from 'cookie' const opts = { url: 'http://example.com', @@ -212,9 +211,9 @@ You can use the [`form-data`](https://github.com/form-data/form-data) module to create POST request with form data: ```js -const fs = require('fs') -const get = require('simple-get') -const FormData = require('form-data') +import fs from 'fs' +import get from 'simple-get' +import FormData from 'form-data' const form = new FormData() form.append('my_file', fs.createReadStream('/foo/bar.jpg')) @@ -244,7 +243,7 @@ get.post(opts, function (err, res) {}) ### Specifically disallowing redirects ```js -const get = require('simple-get') +import get from 'simple-get' const opts = { url: 'http://example.com/will-redirect-elsewhere', @@ -274,9 +273,9 @@ You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to crea a signed OAuth request: ```js -const get = require('simple-get') -const crypto = require('crypto') -const OAuth = require('oauth-1.0a') +import get from 'simple-get' +import crypto from 'crypto' +import OAuth from 'oauth-1.0a' const oauth = OAuth({ consumer: { @@ -308,8 +307,8 @@ get(opts, function (err, res) {}) You can use [limiter](https://github.com/jhurliman/node-rate-limiter) to throttle requests. This is useful when calling an API that is rate limited. ```js -const simpleGet = require('simple-get') -const RateLimiter = require('limiter').RateLimiter +import simpleGet from 'simple-get' +import {RateLimiter} from 'limiter' const limiter = new RateLimiter(1, 'second') const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb)) diff --git a/index.js b/index.js index 3759063..c108dc2 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,16 @@ /*! simple-get. MIT License. Feross Aboukhadijeh */ -module.exports = simpleGet -const concat = require('simple-concat') -const decompressResponse = require('decompress-response') // excluded from browser build -const http = require('http') -const https = require('https') -const once = require('once') -const querystring = require('querystring') -const url = require('url') +import http from 'node:http' +import https from 'node:https' +import { stringify } from 'node:querystring' +import url from 'node:url' +import concat from 'simple-concat' +import decompressResponse from 'decompress-response' // excluded from browser build +import once from 'once' const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function' -function simpleGet (opts, cb) { +export default function simpleGet (opts, cb) { opts = Object.assign({ maxRedirects: 10 }, typeof opts === 'string' ? { url: opts } : opts) cb = once(cb) @@ -30,7 +29,7 @@ function simpleGet (opts, cb) { if (opts.body) { body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body } else if (opts.form) { - body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form) + body = typeof opts.form === 'string' ? opts.form : stringify(opts.form) opts.headers['content-type'] = 'application/x-www-form-urlencoded' } diff --git a/package.json b/package.json index 941b620..58990fd 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "email": "feross@feross.org", "url": "https://feross.org" }, + "type": "module", "browser": { "decompress-response": false }, @@ -14,15 +15,15 @@ "url": "https://github.com/feross/simple-get/issues" }, "dependencies": { - "decompress-response": "^6.0.0", - "once": "^1.3.1", - "simple-concat": "^1.0.0" + "decompress-response": "^7.0.0", + "once": "^1.4.0", + "simple-concat": "^1.0.1" }, "devDependencies": { "self-signed-https": "^1.0.5", "standard": "*", - "string-to-stream": "^3.0.0", - "tape": "^5.0.0" + "string-to-stream": "^3.0.1", + "tape": "^5.3.1" }, "homepage": "https://github.com/feross/simple-get", "keywords": [ diff --git a/test/auth.js b/test/auth.js index 2ea9941..6e97df7 100644 --- a/test/auth.js +++ b/test/auth.js @@ -1,12 +1,12 @@ -const concat = require('simple-concat') -const get = require('../') -const http = require('http') -const test = require('tape') +import { createServer } from 'node:http' +import concat from 'simple-concat' +import test from 'tape' +import get from '../index.js' test('basic auth', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.headers.authorization, 'Basic Zm9vOmJhcg==') res.statusCode = 200 res.end('response') diff --git a/test/basic.js b/test/basic.js index 3188482..61fb62f 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,13 +1,15 @@ -const concat = require('simple-concat') -const get = require('../') -const http = require('http') -const selfSignedHttps = require('self-signed-https') -const test = require('tape') +import { createServer } from 'node:http' +import concat from 'simple-concat' +import selfSignedHttps from 'self-signed-https' +import test from 'tape' +import get from '../index.js' + +const { head } = get test('simple get', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/path') res.statusCode = 200 res.end('response') @@ -56,7 +58,7 @@ test('https', function (t) { test('simple get json', function (t) { t.plan(6) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/path') t.equal(req.headers.accept, 'application/json') res.statusCode = 200 @@ -84,7 +86,7 @@ test('simple get json', function (t) { test('HEAD request', function (t) { t.plan(3) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'HEAD') // Taken from real-world response from HEAD request to GitHub.com res.setHeader('content-type', 'text/html; charset=utf-8') @@ -100,7 +102,7 @@ test('HEAD request', function (t) { method: 'HEAD', url: 'http://localhost:' + port } - get.head(opts, function (err, res) { + head(opts, function (err, res) { t.error(err) t.equal(res.statusCode, 200) server.close() @@ -111,7 +113,7 @@ test('HEAD request', function (t) { test('timeout option', function (t) { t.plan(2) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/path') setTimeout(function () { // response should not be sent - should timeout before it's sent @@ -136,7 +138,7 @@ test('rewrite POST redirects to GET', function (t) { let redirected = false - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { if (redirected) { t.equal(req.url, '/getthis') t.equal(req.method, 'GET') diff --git a/test/concat.js b/test/concat.js index 2bc22c9..a079670 100644 --- a/test/concat.js +++ b/test/concat.js @@ -1,12 +1,14 @@ -const get = require('../') -const http = require('http') -const str = require('string-to-stream') -const test = require('tape') +import { createServer } from 'node:http' +import str from 'string-to-stream' +import test from 'tape' +import get from '../index.js' + +const { concat } = get test('get.concat (post, stream body, and json option)', function (t) { t.plan(4) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 req.pipe(res) }) @@ -19,7 +21,7 @@ test('get.concat (post, stream body, and json option)', function (t) { method: 'POST', json: true } - get.concat(opts, function (err, res, data) { + concat(opts, function (err, res, data) { t.error(err) t.equal(typeof data, 'object') t.deepEqual(Object.keys(data), ['a']) @@ -31,14 +33,14 @@ test('get.concat (post, stream body, and json option)', function (t) { test('get.concat', function (t) { t.plan(4) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 res.end('blah blah blah') }) server.listen(0, function () { const port = server.address().port - get.concat('http://localhost:' + port, function (err, res, data) { + concat('http://localhost:' + port, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) t.ok(Buffer.isBuffer(data), '`data` is type buffer') @@ -50,7 +52,7 @@ test('get.concat', function (t) { test('get.concat json', function (t) { t.plan(3) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 res.end('{"message":"response"}') }) @@ -61,7 +63,7 @@ test('get.concat json', function (t) { url: 'http://localhost:' + port + '/path', json: true } - get.concat(opts, function (err, res, data) { + concat(opts, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) t.equal(data.message, 'response') @@ -72,7 +74,7 @@ test('get.concat json', function (t) { test('get.concat json error', function (t) { t.plan(1) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 500 res.end('not json') }) @@ -83,7 +85,7 @@ test('get.concat json error', function (t) { url: 'http://localhost:' + port + '/path', json: true } - get.concat(opts, function (err, res, data) { + concat(opts, function (err, res, data) { t.ok(err instanceof Error) server.close() }) diff --git a/test/headers.js b/test/headers.js index ac51f81..8e7cbb6 100644 --- a/test/headers.js +++ b/test/headers.js @@ -1,14 +1,14 @@ -const concat = require('simple-concat') -const get = require('../') -const http = require('http') -const str = require('string-to-stream') -const test = require('tape') -const zlib = require('zlib') +import { createGzip, createDeflate } from 'node:zlib' +import { createServer } from 'node:http' +import concat from 'simple-concat' +import str from 'string-to-stream' +import test from 'tape' +import get from '../index.js' test('custom headers', function (t) { t.plan(2) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.headers['custom-header'], 'custom-value') res.statusCode = 200 res.end('response') @@ -32,10 +32,10 @@ test('custom headers', function (t) { test('gzip response', function (t) { t.plan(4) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 res.setHeader('content-encoding', 'gzip') - str('response').pipe(zlib.createGzip()).pipe(res) + str('response').pipe(createGzip()).pipe(res) }) server.listen(0, function () { @@ -55,10 +55,10 @@ test('gzip response', function (t) { test('deflate response', function (t) { t.plan(4) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 res.setHeader('content-encoding', 'deflate') - str('response').pipe(zlib.createDeflate()).pipe(res) + str('response').pipe(createDeflate()).pipe(res) }) server.listen(0, function () { diff --git a/test/post.js b/test/post.js index b0ae806..c94a356 100644 --- a/test/post.js +++ b/test/post.js @@ -1,14 +1,16 @@ -const concat = require('simple-concat') -const get = require('../') -const http = require('http') -const querystring = require('querystring') -const str = require('string-to-stream') -const test = require('tape') +import { parse } from 'node:querystring' +import { createServer } from 'node:http' +import concat from 'simple-concat' +import str from 'string-to-stream' +import test from 'tape' +import get from '../index.js' + +const { post, concat: _concat } = get test('post (text body)', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') res.statusCode = 200 req.pipe(res) @@ -20,7 +22,7 @@ test('post (text body)', function (t) { url: 'http://localhost:' + port, body: 'this is the body' } - get.post(opts, function (err, res) { + post(opts, function (err, res) { t.error(err) t.equal(res.statusCode, 200) concat(res, function (err, data) { @@ -35,7 +37,7 @@ test('post (text body)', function (t) { test('post (utf-8 text body)', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') res.statusCode = 200 req.pipe(res) @@ -47,7 +49,7 @@ test('post (utf-8 text body)', function (t) { url: 'http://localhost:' + port, body: 'jedan dva tri Ĩetiri' } - get.post(opts, function (err, res) { + post(opts, function (err, res) { t.error(err) t.equal(res.statusCode, 200) concat(res, function (err, data) { @@ -62,7 +64,7 @@ test('post (utf-8 text body)', function (t) { test('post (buffer body)', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') res.statusCode = 200 req.pipe(res) @@ -74,7 +76,7 @@ test('post (buffer body)', function (t) { url: 'http://localhost:' + port, body: Buffer.from('this is the body') } - get.post(opts, function (err, res) { + post(opts, function (err, res) { t.error(err) t.equal(res.statusCode, 200) concat(res, function (err, data) { @@ -89,7 +91,7 @@ test('post (buffer body)', function (t) { test('post (stream body)', function (t) { t.plan(6) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') res.statusCode = 200 t.notOk(req.headers['content-length']) @@ -102,7 +104,7 @@ test('post (stream body)', function (t) { url: 'http://localhost:' + port, body: str('this is the body') } - get.post(opts, function (err, res) { + post(opts, function (err, res) { t.error(err) t.equal(res.statusCode, 200) concat(res, function (err, data) { @@ -117,7 +119,7 @@ test('post (stream body)', function (t) { test('post (json body)', function (t) { t.plan(5) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') t.equal(req.headers['content-type'], 'application/json') res.statusCode = 200 @@ -134,7 +136,7 @@ test('post (json body)', function (t) { }, json: true } - get.concat(opts, function (err, res, data) { + _concat(opts, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) t.equal(data.message, 'this is the body') @@ -149,7 +151,7 @@ test('post (form, object)', function (t) { const formData = Object.create(null) formData.foo = 'bar' - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') res.statusCode = 200 @@ -163,10 +165,10 @@ test('post (form, object)', function (t) { url: 'http://localhost:' + port, form: formData } - get.concat(opts, function (err, res, data) { + _concat(opts, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) - t.deepEqual(querystring.parse(data.toString()), formData) + t.deepEqual(parse(data.toString()), formData) server.close() }) }) @@ -177,7 +179,7 @@ test('post (form, querystring)', function (t) { const formData = 'foo=bar' - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.method, 'POST') t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') res.statusCode = 200 @@ -191,7 +193,7 @@ test('post (form, querystring)', function (t) { url: 'http://localhost:' + port, form: formData } - get.concat(opts, function (err, res, data) { + _concat(opts, function (err, res, data) { t.error(err) t.equal(res.statusCode, 200) t.equal(data.toString(), formData) diff --git a/test/redirect.js b/test/redirect.js index 2f06edd..1a595c0 100644 --- a/test/redirect.js +++ b/test/redirect.js @@ -1,14 +1,14 @@ -const concat = require('simple-concat') -const get = require('../') -const http = require('http') -const selfSignedHttps = require('self-signed-https') -const test = require('tape') +import { createServer } from 'node:http' +import concat from 'simple-concat' +import selfSignedHttps from 'self-signed-https' +import test from 'tape' +import get from '../index.js' test('follow redirects (up to 10)', function (t) { t.plan(15) let num = 0 - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/' + num, 'visited /' + num) if (num < 10) { @@ -39,7 +39,7 @@ test('follow redirects (up to 10)', function (t) { test('do not follow redirects', function (t) { t.plan(2) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/0', 'visited /0') res.statusCode = 301 @@ -62,7 +62,7 @@ test('do not follow redirects', function (t) { test('do not follow redirects and do not error', function (t) { t.plan(4) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/0', 'visited /0') res.statusCode = 301 @@ -88,7 +88,7 @@ test('follow redirects (11 is too many)', function (t) { t.plan(12) let num = 0 - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { t.equal(req.url, '/' + num, 'visited /' + num) if (num < 11) { @@ -123,7 +123,7 @@ test('redirect https to http', function (t) { res.end() }) - const httpServer = http.createServer(function (req, res) { + const httpServer = createServer(function (req, res) { t.equal(req.url, '/path2') res.statusCode = 200 res.end('response') @@ -156,7 +156,7 @@ test('redirect http to https', function (t) { let httpsPort = null let httpPort = null - const httpServer = http.createServer(function (req, res) { + const httpServer = createServer(function (req, res) { t.equal(req.url, '/path1') res.statusCode = 301 res.setHeader('Location', 'https://localhost:' + httpsPort + '/path2') @@ -196,7 +196,7 @@ test('redirect to different host/port', function (t) { let port1 = null let port2 = null - const server1 = http.createServer(function (req, res) { + const server1 = createServer(function (req, res) { t.equal(req.url, '/path1') res.statusCode = 301 // Redirect from localhost:port1 to 127.0.0.1:port2 (different host and port!) @@ -204,7 +204,7 @@ test('redirect to different host/port', function (t) { res.end() }) - const server2 = http.createServer(function (req, res) { + const server2 = createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2) t.equal(req.headers.host, `127.0.0.1:${port2}`) @@ -237,7 +237,7 @@ test('redirect should clear explicitly specified `host` header', function (t) { let port1 = null let port2 = null - const server1 = http.createServer(function (req, res) { + const server1 = createServer(function (req, res) { t.equal(req.url, '/path1') t.equal(req.headers.host, `localhost:${port1}`) res.statusCode = 301 @@ -246,7 +246,7 @@ test('redirect should clear explicitly specified `host` header', function (t) { res.end() }) - const server2 = http.createServer(function (req, res) { + const server2 = createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2), i.e. // that the explicitly specified `Host` header was cleared upon redirect. @@ -285,7 +285,7 @@ test('redirect should clear explicitly specified `Host` (note uppercase) header' let port1 = null let port2 = null - const server1 = http.createServer(function (req, res) { + const server1 = createServer(function (req, res) { t.equal(req.url, '/path1') t.equal(req.headers.host, `localhost:${port1}`) res.statusCode = 301 @@ -294,7 +294,7 @@ test('redirect should clear explicitly specified `Host` (note uppercase) header' res.end() }) - const server2 = http.createServer(function (req, res) { + const server2 = createServer(function (req, res) { t.equal(req.url, '/path2') // Confirm that request was made with new host and port (127.0.0.1:port2), i.e. // that the explicitly specified `Host` header was cleared upon redirect. diff --git a/test/request.js b/test/request.js index 9755287..c4ec6f4 100644 --- a/test/request.js +++ b/test/request.js @@ -1,11 +1,11 @@ -const get = require('../') -const http = require('http') -const test = require('tape') +import { createServer } from 'node:http' +import test from 'tape' +import get from '../index.js' test('access `req` object', function (t) { t.plan(2) - const server = http.createServer(function (req, res) { + const server = createServer(function (req, res) { res.statusCode = 200 res.end('response') })