Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('emits error on internal processing errors', async () => {
const random = Buffer.alloc(5000);
const source = Wreck.toReadableStream(random);
const stream = new Ammo.Clip({ from: 1000, to: 4000 });
stream._range = null; // Force a processing error
await expect(Wreck.read(source.pipe(stream))).to.reject(Error);
});
it('flushes remainder', async () => {
const value = '0123456789';
const encoded = B64.encode(Buffer.from(value)); // MDEyMzQ1Njc4OQ==
const stream = new internals.Payload([encoded.slice(0, 14)]);
const source = stream.pipe(new B64.Decoder());
const payload = await Wreck.read(source);
expect(payload.toString()).to.equal(value.toString());
});
});
.then(function(res) {
var seneca_reply = function(res) {
// backwards compatibility with seneca-transport
if (!res.meta$) {
res.meta$ = {
id: meta.id
}
}
seneca.reply(internalize_reply(sending_instance, res))
}
var wreck_read = Wreck.read(res, spec.wreck.read)
wreck_read
.then(function(body) {
var data = parseJSON(body)
// JSON cannot handle arbitrary array properties
if (Array.isArray(data.array$)) {
var array_data = data.array$
array_data.meta$ = data.meta$
data = array_data
}
seneca_reply(data)
})
.catch(seneca_reply)
})
.catch(function(err) {
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
)}`
);
const body: Boom.Payload = await Wreck.read(res, { json: true });
throw new Boom(body.message, { statusCode: res.statusCode || 500, data: body.error });
dispenser.on('part', async (part) => {
const payload = await Wreck.read(part);
set(part.name, payload.toString(), part.headers, part.filename);
});
helper.replyWithJSON = async (err, res) => {
const { payload } = await Wreck.read(res, { json: true });
return payload;
};
await server.start();
const options = {
hostname: 'localhost',
port: server.info.port,
path: 'http://example.com/path?a=1&a=2#ignore',
method: 'GET'
};
const team = new Teamwork();
const req = Http.request(options, (res) => team.attend(res));
req.end();
const res = await team.work;
const payload = await Wreck.read(res);
expect(payload.toString()).to.equal('1,2');
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();
});
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
)}`
);
const body: Boom.Payload = await Wreck.read(res, { json: true });
throw new Boom(body.message, { statusCode: res.statusCode || 500, data: body.error });
}
}
}
const read = async function (response, options) {
debug('read(): payload read')
return Wreck.read(response, options)
}