本文整理汇总了TypeScript中to-readable-stream.default函数的典型用法代码示例。如果您正苦于以下问题:TypeScript default函数的具体用法?TypeScript default怎么用?TypeScript default使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('sends Streams', withServer, async (t, server, got) => {
server.post('/', defaultEndpoint);
const {body} = await got.post({body: toReadableStream('wow')});
t.is(body, 'wow');
});
示例2: SlowStream
const downloadEndpoint = (_request, response) => {
response.setHeader('content-length', file.length);
toReadableStream(file)
.pipe(new SlowStream({maxWriteInterval: 50}))
.pipe(response);
};
示例3: async
test('`content-length` header with Stream body', withServer, async (t, server, got) => {
server.post('/', echoHeaders);
const {body} = await got.post({body: toReadableStream('wow')});
const headers = JSON.parse(body);
t.is(headers['transfer-encoding'], 'chunked', 'likely failed to get headers at all');
t.is(headers['content-length'], undefined);
});
示例4: getStream
test('upload progress - stream with unknown body size', withServer, async (t, server, got) => {
server.post('/', uploadEndpoint);
const events = [];
const request = got.stream.post('')
.on('uploadProgress', event => events.push(event));
await getStream(toReadableStream(file).pipe(request));
checkEvents(t, events);
});
示例5: async
test('accepts `options.body` as a Stream', withServer, async (t, server, got) => {
server.post('/', postHandler);
const stream = got.stream.post({body: toReadableStream('wow')});
t.is(await getStream(stream), 'wow');
});
示例6: hook
setImmediate(async () => {
try {
for (const hook of options.hooks.beforeRequest) {
// eslint-disable-next-line no-await-in-loop
await hook(options);
}
// Serialize body
const {body, headers} = options;
const isForm = !is.nullOrUndefined(options.form);
const isJSON = !is.nullOrUndefined(options.json);
const isBody = !is.nullOrUndefined(body);
if ((isBody || isForm || isJSON) && withoutBody.has(options.method)) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}
if (isBody) {
if (isForm || isJSON) {
throw new TypeError('The `body` option cannot be used with the `json` option or `form` option');
}
if (is.object(body) && isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body)) {
throw new TypeError('The `body` option must be a stream.Readable, string, Buffer, Object or Array');
}
} else if (isForm) {
if (!is.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
options.body = (new URLSearchParams(options.form)).toString();
} else if (isJSON) {
headers['content-type'] = headers['content-type'] || 'application/json';
options.body = JSON.stringify(options.json);
}
// Convert buffer to stream to receive upload progress events (#322)
if (is.buffer(body)) {
options.body = toReadableStream(body);
uploadBodySize = body.length;
} else {
uploadBodySize = await getBodySize(options);
}
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding'])) {
if ((uploadBodySize > 0 || options.method === 'PUT') && !is.undefined(uploadBodySize)) {
headers['content-length'] = uploadBodySize;
}
}
if (!options.stream && options.responseType === 'json' && is.undefined(headers.accept)) {
options.headers.accept = 'application/json';
}
requestUrl = options.href || (new URL(options.path, urlLib.format(options))).toString();
await get(options);
} catch (error) {
emitError(error);
}
});