Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async requestFn(baseUrl: string, path: string, payload: RequestPayload, originRequest: Request) {
const opt = {
baseUrl,
payload: JSON.stringify(payload),
// redirect all headers to CodeNode
headers: { ...filterHeaders(originRequest), 'kbn-xsrf': 'kibana' },
};
const promise = Wreck.request('POST', path, opt);
const res: http.IncomingMessage = await promise;
this.log.debug(`sending RPC call to ${baseUrl}${path} ${res.statusCode}`);
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
const buffer: Buffer = await Wreck.read(res, {});
try {
return JSON.parse(buffer.toString(), (key, value) => {
return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
});
} catch (e) {
this.log.error('parse json failed: ' + buffer.toString());
throw Boom.boomify(e, { statusCode: 500 });
}
} else {
this.log.error(
`received ${res.statusCode} from ${baseUrl}/${path}, params was ${util.inspect(
payload.params
await server.register({ plugin: Graphi, options: { schema, resolvers } });
server.route({
method: 'graphql',
path: '/createPerson',
handler: (request, h) => {
expect(request.payload.firstname).to.equal('billy');
expect(request.payload.lastname).to.equal('jean');
return { firstname: 'billy', lastname: 'jean' };
}
});
await server.start();
const payload = { firstname: 'billy', lastname: 'jean' };
const res = await Wreck.request('GRAPHQL', `http://0.0.0.0:${server.info.port}/createPerson`, { payload });
expect(res.statusCode).to.equal(400);
await server.stop();
});
const server = Hapi.server({ routes: { timeout: { socket: false } } });
server.route({ method: 'GET', path: '/', handler: () => null });
await server.start();
let timeout;
const orig = Net.Socket.prototype.setTimeout;
Net.Socket.prototype.setTimeout = function (...args) {
timeout = 'gotcha';
Net.Socket.prototype.setTimeout = orig;
return orig.apply(this, args);
};
const res = await Wreck.request('GET', 'http://localhost:' + server.info.port + '/');
await Wreck.read(res);
expect(timeout).to.equal('gotcha');
await server.stop();
});
server.once('listening', () => {
const form = new FormData();
form.append('file', Fs.createReadStream('./test/files/blank.gif'));
Wreck.request('POST', 'http://127.0.0.1:' + server.address().port, { payload: form, headers: form.getHeaders() });
});
setTimeout(() => {
this.push(chunk);
}, 10);
}
};
stream.once('end', () => team.attend());
return stream;
};
server.route({ method: 'GET', path: '/', handler });
await server.start();
const res = await Wreck.request('GET', 'http://localhost:' + server.info.port);
res.on('data', (chunk) => {
if (!destroyed) {
destroyed = true;
res.destroy();
}
});
await team.work;
await server.stop();
});
const proxyHandler = async (request, h) => {
const options = {};
options.headers = Hoek.clone(request.headers);
delete options.headers.host;
const res = await Wreck.request(request.method, 'http://localhost:' + upstream.info.port + '/headers', options);
return h.response(res).code(res.statusCode);
};
async function download(url, destination, log) {
const response = await wreck.request('GET', url);
if (response.statusCode !== 200) {
throw new Error(`Unexpected status code ${response.statusCode} when downloading ${url}`);
}
mkdirSync(dirname(destination), { recursive: true });
await createPromiseFromStreams([response, createWriteStream(destination)]);
log.debug('Downloaded ', url);
}
}
};
if (params) {
const paramsString = internals.queryString(params);
if (method === 'get') {
uri += '?' + paramsString;
}
else {
requestOptions.payload = paramsString;
requestOptions.headers['content-type'] = 'application/x-www-form-urlencoded';
}
}
if (options.stream) {
return Wreck.request(method, uri, requestOptions);
}
const desc = (options.desc || 'resource');
try {
const { res, payload } = await Wreck[method](uri, requestOptions);
var result = { payload: payload.toString(), statusCode: res.statusCode };
}
catch (err) {
throw Boom.internal(`Failed obtaining ${this.provider} ${desc}`, err);
}
if (result.statusCode !== 200) {
throw Object.assign(Boom.internal(`Failed obtaining ${this.provider} ${desc}`, result.payload), result);
}
if (!options.raw) {
async function sendRequest({ sourceUrl, timeout }, logger) {
const maxRedirects = 11; //Because this one goes to 11.
const reqOptions = { timeout, redirects: maxRedirects };
const proxyAgent = getProxyAgent(sourceUrl, logger);
if (proxyAgent) {
reqOptions.agent = proxyAgent;
}
try {
const promise = Wreck.request('GET', sourceUrl, reqOptions);
const req = promise.req;
const resp = await promise;
if (resp.statusCode >= 400) {
throw new Error('ENOTFOUND');
}
return { req, resp };
} catch (err) {
if (err.code === 'ECONNREFUSED') {
err = new Error('ENOTFOUND');
}
throw err;
}
}