Skip to content

Commit

Permalink
upgrade deps, dates, CI.. convert package to a ES6 module
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jul 24, 2024
1 parent b8822da commit 05c5718
Show file tree
Hide file tree
Showing 13 changed files with 1,672 additions and 2,190 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [12, 14, 16, 18, 20, 22]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-20 Lloyd Brookes <[email protected]>
Copyright (c) 2016-24 Lloyd Brookes <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
[![npm module downloads](https://badgen.net/npm/dt/lws-rewrite)](https://www.npmjs.org/package/lws-rewrite)
[![Gihub repo dependents](https://badgen.net/github/dependents-repo/lwsjs/rewrite)](https://github.com/lwsjs/rewrite/network/dependents?dependent_type=REPOSITORY)
[![Gihub package dependents](https://badgen.net/github/dependents-pkg/lwsjs/rewrite)](https://github.com/lwsjs/rewrite/network/dependents?dependent_type=PACKAGE)
[![Build Status](https://travis-ci.org/lwsjs/rewrite.svg?branch=master)](https://travis-ci.org/lwsjs/rewrite)
[![Coverage Status](https://coveralls.io/repos/github/lwsjs/rewrite/badge.svg)](https://coveralls.io/github/lwsjs/rewrite)
[![Node.js CI](https://github.com/lwsjs/rewrite/actions/workflows/node.js.yml/badge.svg)](https://github.com/lwsjs/rewrite/actions/workflows/node.js.yml)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)

# lws-rewrite
Expand All @@ -20,4 +19,4 @@ Adds the following options to lws.

* * *

&copy; 2016-20 Lloyd Brookes \<[email protected]\>.
&copy; 2016-24 Lloyd Brookes \<[email protected]\>.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
const util = require('../lib/util')
import util from '../lib/util.js'
const [from, to, url] = process.argv.slice(2)

if (!(from && to && url)) {
Expand Down
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const EventEmitter = require('events')
const util = require('./lib/util')
import { EventEmitter } from 'events'
import util from './lib/util.js'
import url from 'url'
import _ from 'koa-route'
import HttpsProxyAgent from 'https-proxy-agent'
import HttpProxyAgent from 'http-proxy-agent'
import http from 'http'
import https from 'https'

class Rewrite extends EventEmitter {
description () {
Expand All @@ -25,8 +31,6 @@ class Rewrite extends EventEmitter {
}

middleware (options, lws) {
const url = require('url')
const util = require('./lib/util')
const rules = util.parseRewriteRules(options.rewrite)
if (rules.length) {
this.emit('verbose', 'middleware.rewrite.config', { rewrite: rules })
Expand All @@ -35,7 +39,6 @@ class Rewrite extends EventEmitter {
if (rule.to) {
/* `to` address is remote if the url specifies a host */
if (url.parse(rule.to).host) {
const _ = require('koa-route')
return _.all(rule.from, proxyRequest(rule, this, lws))
} else {
const rmw = rewrite(rule.from, rule.to, this)
Expand All @@ -53,15 +56,12 @@ function proxyRequest (route, mw, lws) {
let httpProxyAgent, httpsProxyAgent
const httpProxy = process.env.http_proxy
if (httpProxy) {
const HttpsProxyAgent = require('https-proxy-agent')
httpsProxyAgent = new HttpsProxyAgent(httpProxy)
const HttpProxyAgent = require('http-proxy-agent')
httpProxyAgent = new HttpProxyAgent(httpProxy)
}

return function proxyMiddleware (ctx) {
return new Promise((resolve, reject) => {
const url = require('url')
const isHttp2 = ctx.req.httpVersion === '2.0'
ctx.state.id = id++

Expand Down Expand Up @@ -111,20 +111,20 @@ function proxyRequest (route, mw, lws) {
remoteReqOptions.headers = reqInfo.headers
remoteReqOptions.rejectUnauthorized = false

/* emit verbose info */
mw.emit('verbose', 'middleware.rewrite.remote.request', reqInfo)

const protocol = remoteReqOptions.protocol
if (protocol === 'http:') {
transport = require('http')
transport = http
remoteReqOptions.agent = httpProxyAgent
} else if (protocol === 'https:') {
transport = require('https')
transport = https
remoteReqOptions.agent = httpsProxyAgent
} else {
return reject(new Error('Protocol missing from request: ' + reqInfo.rewrite.to))
}

/* emit verbose info */
mw.emit('verbose', 'middleware.rewrite.remote.request', reqInfo)

const remoteReq = transport.request(remoteReqOptions, (remoteRes) => {
remoteRes.headers.via = remoteRes.headers.via
? `${remoteRes.headers.via}, 1.1 lws-rewrite`
Expand Down Expand Up @@ -177,4 +177,4 @@ function rewrite (from, to, mw) {
}
}

module.exports = Rewrite
export default Rewrite
12 changes: 5 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function parseRewriteRules (rules) {
const arrayify = require('array-back')
import arrayify from 'array-back'
import { pathToRegexp } from 'path-to-regexp'

function parseRewriteRules (rules) {
return arrayify(rules).map(rule => {
if (typeof rule === 'string') {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
Expand All @@ -16,7 +17,6 @@ function parseRewriteRules (rules) {
}

function getTargetUrl (from, to, url) {
const { pathToRegexp } = require('path-to-regexp')
const fromParams = []
const re = pathToRegexp(from, fromParams)
const fromMatches = re.exec(url)
Expand Down Expand Up @@ -67,7 +67,5 @@ function removeCookieAttribute (cookie = '', attr) {
.join('; ')
}

exports.parseRewriteRules = parseRewriteRules
exports.getTargetUrl = getTargetUrl
exports.removeHopSpecificHeaders = removeHopSpecificHeaders
exports.removeCookieAttribute = removeCookieAttribute
export { parseRewriteRules, getTargetUrl, removeHopSpecificHeaders, removeCookieAttribute }
export default { parseRewriteRules, getTargetUrl, removeHopSpecificHeaders, removeCookieAttribute }
Loading

0 comments on commit 05c5718

Please sign in to comment.