Skip to content

Commit

Permalink
module: upgrade to esmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Nov 27, 2023
1 parent f73a65f commit b65a115
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1,556 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
"after": true
},
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "script",
"ecmaVersion": "latest",
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
Expand Down
31 changes: 0 additions & 31 deletions lib/index.d.ts

This file was deleted.

44 changes: 24 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const zlib = require('zlib');
const http = require('http');
const https = require('https');
const parse = require('url').parse;
const format = require('url').format;

const debugBody = require('debug')('httpx:body');
const debugHeader = require('debug')('httpx:header');
import zlib from 'zlib';
import http from 'http';
import https from 'https';
import { parse, format } from 'url';
import debug from 'debug';

const debugBody = debug('httpx:body');
const debugHeader = debug('httpx:header');

const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
Expand Down Expand Up @@ -45,7 +45,7 @@ const isNumber = function (num) {
return num !== null && !isNaN(num);
};

exports.request = function (url, opts) {
export function request(url, opts) {
opts || (opts = {});

const parsed = typeof url === 'string' ? parse(url) : url;
Expand Down Expand Up @@ -180,9 +180,9 @@ exports.request = function (url, opts) {
}
});
});
};
}

exports.read = function (response, encoding) {
export function read(response, encoding) {
const readable = decompress(response);

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -260,7 +260,7 @@ exports.read = function (response, encoding) {
readable.on('data', onData);
readable.on('end', onEnd);
});
};
}

function readyToRead(readable) {
return new Promise((resolve, reject) => {
Expand All @@ -270,7 +270,7 @@ function readyToRead(readable) {
readable.removeListener('error', onError);
readable.removeListener('end', onEnd);
readable.removeListener('readable', onReadable);
}
};

onReadable = function () {
cleanup();
Expand All @@ -280,20 +280,20 @@ function readyToRead(readable) {
onEnd = function () {
cleanup();
resolve(true);
}
};

onError = function (err) {
cleanup();
reject(err);
}
};

readable.once('readable', onReadable);
readable.once('end', onEnd);
readable.once('error', onError);
});
}

class Event {
export class Event {
constructor(id, event, data, retry) {
this.id = id;
this.event = event;
Expand All @@ -302,8 +302,6 @@ class Event {
}
}

exports.Event = Event;

const DATA_PREFIX = 'data: ';
const EVENT_PREFIX = 'event: ';
const ID_PREFIX = 'id: ';
Expand Down Expand Up @@ -345,7 +343,7 @@ function tryGetEvents(head, chunk) {
* @param {ReadableStream} response
* @returns AsyncGenerator<Event, void, unknown>
*/
exports.readAsSSE = async function* (response) {
export async function* readAsSSE(response) {
const readable = decompress(response);

const socket = response.socket || response.client;
Expand All @@ -370,4 +368,10 @@ exports.readAsSSE = async function* (response) {
}
}
}
};
}

export default {
request,
read,
readAsSSE
};
Loading

0 comments on commit b65a115

Please sign in to comment.