first commit

This commit is contained in:
2026-04-09 13:05:27 +02:00
commit 3bbd7d6413
3084 changed files with 84284 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "es5"
}

View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- "8"
- "10"

View File

@@ -0,0 +1,19 @@
Copyright (c) 2014 Forbes Lindesay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,132 @@
# sync-request
Make synchronous web requests with cross-platform support.
> Requires at least node 8
# **N.B.** You should **not** be using this in a production application. In a node.js application you will find that you are completely unable to scale your server. In a client application you will find that sync-request causes the app to hang/freeze. Synchronous web requests are the number one cause of browser crashes. For production apps, you should use [then-request](https://github.com/then/then-request), which is exactly the same except that it is asynchronous.
[![Build Status](https://img.shields.io/travis/ForbesLindesay/sync-request/master.svg)](https://travis-ci.org/ForbesLindesay/sync-request)
[![Dependency Status](https://img.shields.io/david/ForbesLindesay/sync-request.svg)](https://david-dm.org/ForbesLindesay/sync-request)
[![NPM version](https://img.shields.io/npm/v/sync-request.svg)](https://www.npmjs.org/package/sync-request)
## Installation
npm install sync-request
## Usage
```js
request(method, url, options);
```
e.g.
* GET request without options
```js
var request = require('sync-request');
var res = request('GET', 'http://example.com');
console.log(res.getBody());
```
* GET request with options
```js
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
headers: {
'user-agent': 'example-user-agent',
},
});
console.log(res.getBody());
```
* POST request to a JSON endpoint
```js
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
json: {username: 'ForbesLindesay'},
});
var user = JSON.parse(res.getBody('utf8'));
```
**Method:**
An HTTP method (e.g. `GET`, `POST`, `PUT`, `DELETE` or `HEAD`). It is not case sensitive.
**URL:**
A url as a string (e.g. `http://example.com`). Relative URLs are allowed in the browser.
**Options:**
* `qs` - an object containing querystring values to be appended to the uri
* `headers` - http headers (default: `{}`)
* `body` - body for PATCH, POST and PUT requests. Must be a `Buffer` or `String` (only strings are accepted client side)
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json`. Does not have any affect on how the response is treated.
* `cache` - Set this to `'file'` to enable a local cache of content. A separate process is still spawned even for cache requests. This option is only used if running in node.js
* `followRedirects` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request following redirects automatically.
* `maxRedirects` - sets the maximum number of redirects to follow before erroring on node.js (default: `Infinity`)
* `allowRedirectHeaders` (default: `null`) - an array of headers allowed for redirects (none if `null`).
* `gzip` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request automatically supporting the gzip encoding on responses.
* `timeout` (default: `false`) - times out if no response is returned within the given number of milliseconds.
* `socketTimeout` (default: `false`) - calls `req.setTimeout` internally which causes the request to timeout if no new data is seen for the given number of milliseconds. This option is ignored in the browser.
* `retry` (default: `false`) - retry GET requests. Set this to `true` to retry when the request errors or returns a status code greater than or equal to 400
* `retryDelay` (default: `200`) - the delay between retries in milliseconds
* `maxRetries` (default: `5`) - the number of times to retry before giving up.
These options are passed through to [then-request](https://github.com/then/then-request), so any options that work for then-request should work for sync-request (with the exception of custom and memory caching strategies, and passing functions for handling retries).
**Returns:**
A `Response` object.
Note that even for status codes that represent an error, the request function will still return a response. You can call `getBody` if you want to error on invalid status codes. The response has the following properties:
* `statusCode` - a number representing the HTTP status code
* `headers` - http response headers
* `body` - a string if in the browser or a buffer if on the server
It also has a method `res.getBody(encoding?)` which looks like:
```js
function getBody(encoding) {
if (this.statusCode >= 300) {
var err = new Error(
'Server responded with status code ' +
this.statusCode +
':\n' +
this.body.toString(encoding)
);
err.statusCode = this.statusCode;
err.headers = this.headers;
err.body = this.body;
throw err;
}
return encoding ? this.body.toString(encoding) : this.body;
}
```
## Common Problems
### Could not use "nc", falling back to slower node.js method for sync requests.
If you are running on windows, or some unix systems, you may see the message above. It will not cause any problems, but will add an overhead of ~100ms to each request you make. If you want to speed up your requests, you will need to install an implementation of the `nc` unix utility. This usually done via something like:
```
apt-get install netcat
```
## How is this possible?
Internally, this uses a separate worker process that is run using [childProcess.spawnSync](http://nodejs.org/docs/v0.11.13/api/child_process.html#child_process_child_process_spawnsync_command_args_options).
The worker then makes the actual request using [then-request](https://www.npmjs.org/package/then-request) so this has almost exactly the same API as that.
This can also be used in a web browser via browserify because xhr has built in support for synchronous execution. Note that this is not recommended as it will be blocking.
## License
MIT

View File

@@ -0,0 +1,11 @@
/// <reference types="node" />
export interface FormDataEntry {
key: string;
value: string | Blob | Buffer;
fileName?: string;
}
export declare class FormData {
private _entries;
append(key: string, value: string | Blob | Buffer, fileName?: string): void;
}
export declare function getFormDataEntries(fd: FormData): FormDataEntry[];

View File

@@ -0,0 +1,16 @@
"use strict";
exports.__esModule = true;
var FormData = /** @class */ (function () {
function FormData() {
this._entries = [];
}
FormData.prototype.append = function (key, value, fileName) {
this._entries.push({ key: key, value: value, fileName: fileName });
};
return FormData;
}());
exports.FormData = FormData;
function getFormDataEntries(fd) {
return fd._entries;
}
exports.getFormDataEntries = getFormDataEntries;

View File

@@ -0,0 +1,17 @@
// @flow
// Generated using flowgen2
interface FormDataEntry {
key: string;
value: string | Blob | Buffer;
fileName?: string;
}
export type {FormDataEntry};
declare class FormData {
append(key: string, value: string | Blob | Buffer, fileName?: string): void;
}
export {FormData};
declare function getFormDataEntries(fd: FormData): Array<FormDataEntry>;
export {getFormDataEntries};

View File

@@ -0,0 +1,18 @@
/// <reference types="node" />
import { Options as AsyncOptions } from 'then-request';
import { FormData, FormDataEntry } from './FormData';
export interface BaseOptions extends Pick<AsyncOptions, 'allowRedirectHeaders' | 'followRedirects' | 'gzip' | 'headers' | 'maxRedirects' | 'maxRetries' | 'qs' | 'json'> {
agent?: boolean;
cache?: 'file';
retry?: boolean;
retryDelay?: number;
socketTimeout?: number;
timeout?: number;
body?: string | Buffer;
}
export interface Options extends BaseOptions {
form?: FormData;
}
export interface MessageOptions extends BaseOptions {
form?: FormDataEntry[];
}

View File

@@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;

View File

@@ -0,0 +1,27 @@
// @flow
// Generated using flowgen2
import type {Options as AsyncOptions} from 'then-request';
import {FormData} from './FormData';
import type {FormDataEntry} from './FormData';
interface BaseOptions {
agent?: boolean;
cache?: 'file';
retry?: boolean;
retryDelay?: number;
socketTimeout?: number;
timeout?: number;
body?: string | Buffer;
}
export type {BaseOptions};
interface Options {
form?: FormData;
}
export type {Options};
interface MessageOptions {
form?: Array<FormDataEntry>;
}
export type {MessageOptions};

View File

@@ -0,0 +1,7 @@
/// <reference types="node" />
import { URL } from 'url';
import { HttpVerb, Response } from 'then-request';
import { Options } from './Options';
declare const fd: any;
export { fd as FormData };
export default function doRequest(method: HttpVerb, url: string | URL, options?: Options): Response;

View File

@@ -0,0 +1,69 @@
"use strict";
exports.__esModule = true;
var handle_qs_js_1 = require("then-request/lib/handle-qs.js");
var GenericResponse = require("http-response-object");
var fd = FormData;
exports.FormData = fd;
function doRequest(method, url, options) {
var xhr = new XMLHttpRequest();
// check types of arguments
if (typeof method !== 'string') {
throw new TypeError('The method must be a string.');
}
if (url && typeof url === 'object') {
url = url.href;
}
if (typeof url !== 'string') {
throw new TypeError('The URL/path must be a string.');
}
if (options === null || options === undefined) {
options = {};
}
if (typeof options !== 'object') {
throw new TypeError('Options must be an object (or null).');
}
method = method.toUpperCase();
options.headers = options.headers || {};
// handle cross domain
var match;
var crossDomain = !!((match = /^([\w-]+:)?\/\/([^\/]+)/.exec(url)) && match[2] != location.host);
if (!crossDomain)
options.headers['X-Requested-With'] = 'XMLHttpRequest';
// handle query string
if (options.qs) {
url = handle_qs_js_1["default"](url, options.qs);
}
// handle json body
if (options.json) {
options.body = JSON.stringify(options.json);
options.headers['content-type'] = 'application/json';
}
if (options.form) {
options.body = options.form;
}
// method, url, async
xhr.open(method, url, false);
for (var name in options.headers) {
xhr.setRequestHeader(name.toLowerCase(), '' + options.headers[name]);
}
// avoid sending empty string (#319)
xhr.send(options.body ? options.body : null);
var headers = {};
xhr
.getAllResponseHeaders()
.split('\r\n')
.forEach(function (header) {
var h = header.split(':');
if (h.length > 1) {
headers[h[0].toLowerCase()] = h
.slice(1)
.join(':')
.trim();
}
});
return new GenericResponse(xhr.status, headers, xhr.responseText, url);
}
exports["default"] = doRequest;
module.exports = doRequest;
module.exports["default"] = doRequest;
module.exports.FormData = fd;

View File

@@ -0,0 +1,16 @@
// @flow
// Generated using flowgen2
import {URL} from 'url';
import type {HttpVerb} from 'then-request';
import type {Response} from 'then-request';
import type {Options} from './Options';
declare var fd: any;
export {fd as FormData};
declare function doRequest(
method: HttpVerb,
url: string | URL,
options?: Options,
): Response;
export default doRequest;

View File

@@ -0,0 +1,8 @@
/// <reference types="node" />
import { HttpVerb, Response } from 'then-request';
import { URL } from 'url';
import { FormData } from './FormData';
import { Options } from './Options';
export { HttpVerb, Response, Options };
export { FormData };
export default function request(method: HttpVerb, url: string | URL, options?: Options): Response;

View File

@@ -0,0 +1,34 @@
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
exports.__esModule = true;
var GenericResponse = require("http-response-object");
var FormData_1 = require("./FormData");
exports.FormData = FormData_1.FormData;
var init = require('sync-rpc');
var remote = init(require.resolve('./worker'));
function request(method, url, options) {
var _a = options || { form: undefined }, form = _a.form, o = __rest(_a, ["form"]);
var opts = o;
if (form) {
opts.form = FormData_1.getFormDataEntries(form);
}
var req = {
m: method,
u: url && typeof url === 'object' ? url.href : url,
o: opts
};
var res = remote(req);
return new GenericResponse(res.s, res.h, res.b, res.u);
}
exports["default"] = request;
module.exports = request;
module.exports["default"] = request;
module.exports.FormData = FormData_1.FormData;

View File

@@ -0,0 +1,19 @@
// @flow
// Generated using flowgen2
import type {HttpVerb} from 'then-request';
import type {Response} from 'then-request';
import {URL} from 'url';
import {FormData} from './FormData';
import type {Options} from './Options';
export type {HttpVerb};
export type {Response};
export type {Options};
export {FormData};
declare function request(
method: HttpVerb,
url: string | URL,
options?: Options,
): Response;
export default request;

View File

@@ -0,0 +1,13 @@
import { Response, HttpVerb } from 'then-request';
import { MessageOptions } from './Options';
export declare type Req = {
m: HttpVerb;
u: string;
o?: MessageOptions;
};
export interface Res {
s: Response['statusCode'];
h: Response['headers'];
b: Response['body'];
u: Response['url'];
}

View File

@@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;

View File

@@ -0,0 +1,17 @@
// @flow
// Generated using flowgen2
import type {Response} from 'then-request';
import type {HttpVerb} from 'then-request';
import type {MessageOptions} from './Options';
type Req = {m: HttpVerb, u: string, o?: MessageOptions};
export type {Req};
interface Res {
s: $PropertyType<Response, 'statusCode'>;
h: $PropertyType<Response, 'headers'>;
b: $PropertyType<Response, 'body'>;
u: $PropertyType<Response, 'url'>;
}
export type {Res};

View File

View File

@@ -0,0 +1,33 @@
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
exports.__esModule = true;
var then_request_1 = require("then-request");
function init() {
return function (req) {
// Note how even though we return a promise, the resulting rpc client will be synchronous
var _a = req.o || { form: undefined }, form = _a.form, o = __rest(_a, ["form"]);
var opts = o;
if (form) {
var fd_1 = new then_request_1.FormData();
form.forEach(function (entry) {
fd_1.append(entry.key, entry.value, entry.fileName);
});
opts.form = fd_1;
}
return then_request_1["default"](req.m, req.u, opts).then(function (response) { return ({
s: response.statusCode,
h: response.headers,
b: response.body,
u: response.url
}); });
};
}
module.exports = init;

View File

@@ -0,0 +1,2 @@
// @flow
// Generated using flowgen2

View File

@@ -0,0 +1,49 @@
{
"name": "sync-request",
"version": "6.1.0",
"description": "Make synchronous web requests",
"main": "lib/index.js",
"browser": "lib/browser.js",
"types": "lib/index.d.ts",
"keywords": [
"request",
"http",
"https",
"cache",
"browserify",
"synchronous",
"sync"
],
"dependencies": {
"http-response-object": "^3.0.1",
"sync-rpc": "^1.2.1",
"then-request": "^6.0.0"
},
"devDependencies": {
"body-parser": "^1.14.1",
"cross-env": "^5.1.3",
"express": "^4.13.3",
"flowgen2": "^2.2.0",
"jest": "^22.1.4",
"morgan": "^1.6.1",
"rimraf": "^2.6.2",
"typescript": "^2.6.2"
},
"scripts": {
"pretest": "npm run build",
"test": "jest && cross-env SYNC_REQUEST_LEGACY=true jest && node test/benchmark",
"jest": "jest",
"prepublishOnly": "npm run build",
"prebuild": "rimraf lib",
"build": "tsc && flowgen lib/**/*"
},
"repository": {
"type": "git",
"url": "https://github.com/ForbesLindesay/sync-request.git"
},
"author": "ForbesLindesay",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
}
}

View File

@@ -0,0 +1,15 @@
export interface FormDataEntry {
key: string;
value: string | Blob | Buffer;
fileName?: string;
}
export class FormData {
private _entries: FormDataEntry[] = [];
append(key: string, value: string | Blob | Buffer, fileName?: string): void {
this._entries.push({key, value, fileName});
}
}
export function getFormDataEntries(fd: FormData): FormDataEntry[] {
return (fd as any)._entries;
}

View File

@@ -0,0 +1,29 @@
import {Options as AsyncOptions} from 'then-request';
import {FormData, FormDataEntry} from './FormData';
export interface BaseOptions
extends Pick<
AsyncOptions,
| 'allowRedirectHeaders'
| 'followRedirects'
| 'gzip'
| 'headers'
| 'maxRedirects'
| 'maxRetries'
| 'qs'
| 'json'
> {
agent?: boolean;
cache?: 'file';
retry?: boolean;
retryDelay?: number;
socketTimeout?: number;
timeout?: number;
body?: string | Buffer;
}
export interface Options extends BaseOptions {
form?: FormData;
}
export interface MessageOptions extends BaseOptions {
form?: FormDataEntry[];
}

View File

@@ -0,0 +1,91 @@
import {URL} from 'url';
import {HttpVerb, Response} from 'then-request';
import handleQs from 'then-request/lib/handle-qs.js';
import {Options} from './Options';
import GenericResponse = require('http-response-object');
const fd = FormData as any;
export {fd as FormData};
export default function doRequest(
method: HttpVerb,
url: string | URL,
options?: Options
): Response {
var xhr = new XMLHttpRequest();
// check types of arguments
if (typeof method !== 'string') {
throw new TypeError('The method must be a string.');
}
if (url && typeof url === 'object') {
url = url.href;
}
if (typeof url !== 'string') {
throw new TypeError('The URL/path must be a string.');
}
if (options === null || options === undefined) {
options = {};
}
if (typeof options !== 'object') {
throw new TypeError('Options must be an object (or null).');
}
method = method.toUpperCase() as any;
options.headers = options.headers || {};
// handle cross domain
var match;
var crossDomain = !!(
(match = /^([\w-]+:)?\/\/([^\/]+)/.exec(url)) && match[2] != location.host
);
if (!crossDomain) options.headers['X-Requested-With'] = 'XMLHttpRequest';
// handle query string
if (options.qs) {
url = handleQs(url, options.qs);
}
// handle json body
if (options.json) {
options.body = JSON.stringify(options.json);
options.headers['content-type'] = 'application/json';
}
if (options.form) {
options.body = options.form as any;
}
// method, url, async
xhr.open(method, url, false);
for (var name in options.headers) {
xhr.setRequestHeader(name.toLowerCase(), '' + options.headers[name]);
}
// avoid sending empty string (#319)
xhr.send(options.body ? options.body : null);
var headers = {};
xhr
.getAllResponseHeaders()
.split('\r\n')
.forEach(function(header) {
var h = header.split(':');
if (h.length > 1) {
(headers as any)[h[0].toLowerCase()] = h
.slice(1)
.join(':')
.trim();
}
});
return new GenericResponse<string>(
xhr.status,
headers,
xhr.responseText,
url
);
}
module.exports = doRequest;
module.exports.default = doRequest;
module.exports.FormData = fd;

View File

@@ -0,0 +1,32 @@
import {HttpVerb, Response} from 'then-request';
import GenericResponse = require('http-response-object');
import {URL} from 'url';
import {Req, Res} from './messages';
import {FormData, getFormDataEntries} from './FormData';
import {Options, MessageOptions} from './Options';
const init = require('sync-rpc');
const remote = init(require.resolve('./worker'));
export {HttpVerb, Response, Options};
export {FormData};
export default function request(
method: HttpVerb,
url: string | URL,
options?: Options
): Response {
const {form, ...o} = options || {form: undefined};
const opts: MessageOptions = o;
if (form) {
opts.form = getFormDataEntries(form);
}
const req: Req = {
m: method,
u: url && typeof url === 'object' ? url.href : (url as string),
o: opts,
};
const res: Res = remote(req);
return new GenericResponse(res.s, res.h, res.b, res.u);
}
module.exports = request;
module.exports.default = request;
module.exports.FormData = FormData;

View File

@@ -0,0 +1,13 @@
import {Response, HttpVerb} from 'then-request';
import {MessageOptions} from './Options';
export type Req = {
m: HttpVerb;
u: string;
o?: MessageOptions;
};
export interface Res {
s: Response['statusCode'];
h: Response['headers'];
b: Response['body'];
u: Response['url'];
}

View File

@@ -0,0 +1,24 @@
import request, {Options, FormData} from 'then-request';
import {Req, Res} from './messages';
function init() {
return (req: Req): Promise<Res> => {
// Note how even though we return a promise, the resulting rpc client will be synchronous
const {form, ...o} = req.o || {form: undefined};
const opts: Options = o;
if (form) {
const fd = new FormData();
form.forEach(entry => {
fd.append(entry.key, entry.value, entry.fileName);
});
opts.form = fd;
}
return request(req.m, req.u, opts).then(response => ({
s: response.statusCode,
h: response.headers,
b: response.body,
u: response.url,
}));
};
}
module.exports = init;

View File

@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`http://httpbin.org/post 1`] = `
Object {
"args": Object {},
"data": "<body/>",
"files": Object {},
"form": Object {},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "7",
"Host": "httpbin.org",
},
"json": null,
"url": "http://httpbin.org/post",
}
`;
exports[`http://httpbin.org/post form 1`] = `
Object {
"args": Object {},
"data": "",
"files": Object {},
"form": Object {
"foo": "bar",
},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "161",
"Host": "httpbin.org",
},
"json": null,
"url": "http://httpbin.org/post",
}
`;
exports[`http://httpbin.org/post json 1`] = `
Object {
"args": Object {},
"data": "{\\"foo\\":\\"bar\\"}",
"files": Object {},
"form": Object {},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "13",
"Content-Type": "application/json",
"Host": "httpbin.org",
},
"json": Object {
"foo": "bar",
},
"url": "http://httpbin.org/post",
}
`;

View File

@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`DELETE request 1`] = `"ok"`;
exports[`GET request 1`] = `"ok"`;
exports[`POST request 1`] = `"ok"`;
exports[`PUT request 1`] = `"ok"`;

View File

@@ -0,0 +1,7 @@
'use strict';
const http = require('http');
http.createServer(function (req, res, next) {
res.end('Hello World');
}).listen(3045);

View File

@@ -0,0 +1,42 @@
'use strict';
const spawn = require('child_process').spawn;
const spawnSync = require('child_process').spawnSync;
const thenRequest = require('then-request');
const syncRequest = require('../');
const server = spawn(process.execPath, [require.resolve('./benchmark-server.js')]);
setTimeout(() => {
let asyncDuration, syncDuration;
let ready = Promise.resolve(null);
const startAsync = Date.now();
for (let i = 0; i < 1000; i++) {
ready = ready.then(function () {
return thenRequest('get', 'http://localhost:3045');
});
}
ready.then(function () {
const endAsync = Date.now();
asyncDuration = endAsync - startAsync;
console.log('1000 async requests in: ' + asyncDuration);
const startSync = Date.now();
for (let i = 0; i < 500; i++) {
syncRequest('get', 'http://localhost:3045');
}
const endSync = Date.now();
syncDuration = endSync - startSync;
console.log('1000 sync requests in: ' + syncDuration);
}).then(() => {
server.kill();
if (syncDuration > (asyncDuration * 10)) {
console.error('This is more than 10 times slower than using async requests, that is not good enough.');
process.exit(1);
}
process.exit(0);
}, function (err) {
console.error(err.stack);
process.exit(1);
});
ready = null;
}, 1000);

View File

@@ -0,0 +1,54 @@
var request = require('../');
var FormData = request.FormData;
// Test GET request
test('http://nodejs.org', () => {
var res = request('GET', 'http://nodejs.org');
expect(res.statusCode).toBe(200);
expect(res.url).toBe('https://nodejs.org/en/');
});
test('http://httpbin.org/post', () => {
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
body: '<body/>',
}).getBody('utf8')
);
delete res.origin;
expect(res).toMatchSnapshot();
});
test('http://httpbin.org/post json', () => {
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
json: {foo: 'bar'},
}).getBody('utf8')
);
delete res.origin;
expect(res).toMatchSnapshot();
});
test('http://httpbin.org/post form', () => {
var fd = new FormData();
fd.append('foo', 'bar');
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
form: fd,
}).getBody('utf8')
);
delete res.headers['Content-Type'];
delete res.origin;
expect(res).toMatchSnapshot();
});
test('https://expired.badssl.com', () => {
var errored = false;
try {
// Test unauthorized HTTPS GET request
var res = request('GET', 'https://expired.badssl.com');
} catch (ex) {
return;
}
throw new Error('Should have rejected unauthorized https get request');
});

View File

@@ -0,0 +1,42 @@
'use strict';
var express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
PORT = 3030;
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
// configure log
app.use(morgan('dev'));
var started = false;
exports.isStarted = function() {
return started;
};
var server;
process.on('message', function(m) {
if (m === 'start') {
server = app.listen(PORT, function() {
started = true;
return process.send('started');
});
} else {
server.close(function() {
started = false;
return process.send('closed') && process.exit(0);
});
}
});
['get', 'post', 'put', 'delete'].forEach(function(method) {
app.route('/internal-test')[method](function(req, res) {
res.send('ok');
});
});

View File

@@ -0,0 +1,61 @@
'use strict';
if (process.env.SYNC_REQUEST_LEGACY) {
// break PATH so running `nc` will fail.
process.env.PATH = '';
}
var request = require('../');
var FormData = request.FormData;
const fork = require('child_process').fork;
var server = fork(__dirname + '/fake-server', {stdio: 'pipe'});
test('start server', () => {
return new Promise(resolve => {
server.on('message', m => {
if (m === 'started') {
resolve();
}
});
server.send('start');
});
});
test('GET request', () => {
var res = request('GET', 'http://localhost:3030/internal-test', {
timeout: 2000,
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('POST request', () => {
var res = request('POST', 'http://localhost:3030/internal-test', {
timeout: 2000,
body: '<body/>',
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('PUT request', () => {
var res = request('PUT', 'http://localhost:3030/internal-test', {
timeout: 2000,
body: '<body/>',
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('DELETE request', () => {
var res = request('DELETE', 'http://localhost:3030/internal-test', {
timeout: 2000,
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('stop server', () => {
server.send('stop');
});

View File

@@ -0,0 +1,8 @@
{
"compileOnSave": true,
"compilerOptions": {
"declaration": true,
"outDir": "lib",
"strict": true
}
}