@ejnshtein/smol-request
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@ejnshtein/smol-request", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Tiny http/https request wrapper for Node.js using ESM (13.5 and newer)", | ||
@@ -5,0 +5,0 @@ "main": "Request.js", |
@@ -6,2 +6,3 @@ // docs: https://nodejs.org/dist/latest-v10.x/docs/api/https.html#https_https_request_url_options_callback | ||
import fs from 'fs' | ||
import { createUnzip } from 'zlib' | ||
import { cleanObject, mergeUrl, deepmerge } from './lib/index.js' | ||
@@ -46,36 +47,37 @@ const pkg = JSON.parse(fs.readFileSync('./package.json')) | ||
} | ||
const stream = ['gzip', 'compress', 'deflate'].includes(res.headers['content-encoding']) && res.statusCode === 204 | ||
? res.pipe(createUnzip()) | ||
: res | ||
if (requestOptions.responseType === 'stream') { | ||
result.data = res | ||
result.data = stream | ||
return resolve(result) | ||
} | ||
res.setEncoding('utf8') | ||
const reponseData = [] | ||
const onData = chunk => { | ||
reponseData.push(chunk) | ||
} | ||
// stream.setEncoding('utf8') | ||
const responseData = [] | ||
const onData = chunk => { responseData.push(requestOptions.responseType === 'buffer' ? Buffer.from(chunk) : chunk) } | ||
const onError = err => { | ||
res.removeListener('error', onError) | ||
res.removeListener('data', onData) | ||
stream.removeListener('error', onError) | ||
stream.removeListener('data', onData) | ||
reject(err) | ||
} | ||
const onClose = () => { | ||
res.removeListener('error', onError) | ||
res.removeListener('data', onData) | ||
res.removeListener('close', onClose) | ||
stream.removeListener('error', onError) | ||
stream.removeListener('data', onData) | ||
stream.removeListener('end', onClose) | ||
if (requestOptions.responseType === 'buffer') { | ||
result.data = Buffer.concat(reponseData) | ||
result.data = Buffer.concat(responseData) | ||
} else if (requestOptions.responseType === 'json') { | ||
try { | ||
result.data = JSON.parse(reponseData) | ||
result.data = JSON.parse(responseData) | ||
} catch (e) { | ||
return reject(new Error(`JSON parsing error: ${e.message}: ${reponseData}`)) | ||
return reject(new Error(`JSON parsing error: ${e.message}: ${responseData}`)) | ||
} | ||
} else { | ||
result.data = reponseData.join('') | ||
result.data = responseData.join('') | ||
} | ||
resolve(result) | ||
} | ||
res.on('data', onData) | ||
res.on('error', onError) | ||
res.on('close', onClose) | ||
stream.on('data', onData) | ||
stream.on('error', onError) | ||
stream.on('end', onClose) | ||
} | ||
@@ -82,0 +84,0 @@ const client = url.startsWith('https') ? https : http |
import request from '../../smol-request/Request.js' | ||
// import { performance } from 'perf_hooks' | ||
import fs from 'fs' | ||
request('https://i.picsum.photos/id/722/200/300.jpg', | ||
{ | ||
responseType: 'buffer' | ||
} | ||
) | ||
.then(({ data, headers, status }) => { | ||
console.log(headers, status, data) | ||
fs.writeFile('./picture.jpg', data, err => { | ||
console.log(err || 'done!') | ||
}) | ||
// const writeStream = fs.createWriteStream('./picture.jpg', 'utf8') | ||
request('https://bbc.com') | ||
.then(({ data, headers }) => { | ||
console.log(`BBC homepage is hosting on ${headers.server}.`) | ||
// data.pipe(writeStream) | ||
// data.once('error', console.log), | ||
// data.once('finish', () => { | ||
// console.log('done') | ||
// }) | ||
// data.on('data', data => { | ||
// console.log(data) | ||
// }) | ||
}) | ||
// request('https://bbc.com') | ||
// .then(({ data, headers }) => { | ||
// console.log(`BBC homepage is hosting on ${headers.server}.`) | ||
// }) | ||
// const t0 = performance.now() | ||
@@ -9,0 +32,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18688
13
265
3