Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to node 20 #1

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"parser": "babel-eslint",
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "script"
"sourceType": "script",
"requireConfigFile": false
},
"env": {
"browser": false,
"es2020": true,
"node": true
},
"plugins": [
"prettier"
],
"plugins": ["prettier"],
"rules": {
"curly": 2,
"no-underscore-dangle": 0,
Expand All @@ -25,10 +24,13 @@
"no-lonely-if": 2,
"quotes": [0, true, "single"],
"strict": [2, "global"],
"prettier/prettier": [2, {
"singleQuote": true,
"tabWidth": 4
}]
"prettier/prettier": [
2,
{
"singleQuote": true,
"tabWidth": 4
}
]
},
"globals": {
"require": false,
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches:
- master
tags-ignore:
- "**"
pull_request:
branches:
- "**"

jobs:
build:
name: Build
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm test
- if: matrix.node-version == '20.x'
uses: actions/upload-artifact@v3
with:
name: Code coverage
path: coverage/
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ npm-debug.log
src/pipe.min.js
jsconfig.json
/.idea
package-lock.json
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"tabWidth": 4,
"useTabs": false,
"arrowParens": "avoid",
"trailingComma": "none"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Tailor Changelog

# 8.0.0
* [#28](https://github.com/namecheap/error-extender/pull/1) support nodejs v20
* update dependencies
* align CI
* fix lint errors
* fix incompatibilities
* (**breaking change**) Drop nodejs version < 16

# 7.0.0
* [#27](https://github.com/StyleT/tailorx/pull/27) CI for Node.js 14
* [#26](https://github.com/StyleT/tailorx/pull/27) (**breaking change**) simplified error handling of `fetchContext(request)` function
Expand Down
4 changes: 2 additions & 2 deletions examples/basic-css-and-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ http.createServer((req, res) => {
return res.end('');
}
tailor.requestHandler(req, res);
}).listen(8080, function() {
}).listen(8080, function () {
console.log('Tailor server listening on port 8080');
});

Expand Down Expand Up @@ -45,6 +45,6 @@ http.createServer((req, res) => {

// fragment content
res.end('<div>Fragment 1: <span id="c">-1</span>s elapsed</div>');
}).listen(8081, function() {
}).listen(8081, function () {
console.log('Fragment Server listening on port 8081');
});
4 changes: 2 additions & 2 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const tailor = new Tailor({
// Root Server
http.createServer((req, res) => {
tailor.requestHandler(req, res);
}).listen(8080, function() {
}).listen(8080, function () {
console.log('Tailor server listening on port 8080');
});

Expand All @@ -23,6 +23,6 @@ http.createServer((req, res) => {
'Content-Type': 'text/html'
});
res.end('<div>Fragment 1</div>');
}).listen(8081, function() {
}).listen(8081, function () {
console.log('Fragment Server listening on port 8081');
});
9 changes: 5 additions & 4 deletions examples/custom-tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ http.createServer((req, res) => {
return res.end('');
}
tailor.requestHandler(req, res);
}).listen(8080, function() {
}).listen(8080, function () {
console.log('Tailor server listening on port 8080');
});

Expand All @@ -68,8 +68,9 @@ http.createServer((req, res) => {
} else {
return res.end(`
<div>Switcher ${currentNesting}</div>
<switcher nesting=${currentNesting -
1} final-src=${final_src} ></switcher>
<switcher nesting=${
currentNesting - 1
} final-src=${final_src} ></switcher>
`);
}
}
Expand All @@ -79,6 +80,6 @@ http.createServer((req, res) => {
// fragment content
const name = urlObj.query.name;
res.end(`<div>Fragment, name: ${name}</div>`);
}).listen(8081, function() {
}).listen(8081, function () {
console.log('Fragment Server listening on port 8081');
});
67 changes: 32 additions & 35 deletions lib/fetch-template.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const path = require('path');
const url = require('url');
const fs = require('fs');
const promisify = require('util.promisify');
const path = require('node:path');
const url = require('node:url');
const {
readFile: readFilePromise,
lstat: lstatPromise
} = require('node:fs/promises');

const TEMPLATE_ERROR = 0;
const TEMPLATE_NOT_FOUND = 1;
Expand All @@ -22,12 +24,6 @@ class TemplateError extends Error {
}
}

/**
* Promisify the functions
*/
const readFilePromise = promisify(fs.readFile);
const lstatPromise = promisify(fs.lstat);

/**
* Read the file from File System
*
Expand Down Expand Up @@ -97,34 +93,35 @@ const factoryFilePath = (templatesPath, filename) =>
* @param {string} templatesPath - The path where the templates are stored
* @param {function=} baseTemplateFn - Function that returns the Base template name for a given page
*/
module.exports = (templatesPath, baseTemplateFn) => (
request,
parseTemplate
) => {
const pathname = getPathName(request);

return getTemplatePath(templatesPath, pathname).then(templateStat => {
return readFile(templateStat.path).then(baseTemplate => {
if (templateStat.isFile || typeof baseTemplateFn !== 'function') {
return parseTemplate(baseTemplate);
}
module.exports =
(templatesPath, baseTemplateFn) => (request, parseTemplate) => {
const pathname = getPathName(request);

return getTemplatePath(templatesPath, pathname).then(templateStat => {
return readFile(templateStat.path).then(baseTemplate => {
if (
templateStat.isFile ||
typeof baseTemplateFn !== 'function'
) {
return parseTemplate(baseTemplate);
}

const templateName = baseTemplateFn(pathname);
if (!templateName) {
return parseTemplate(baseTemplate);
}
const templateName = baseTemplateFn(pathname);
if (!templateName) {
return parseTemplate(baseTemplate);
}

const pageTemplate = baseTemplate;
const baseTemplatePath = factoryFilePath(
templatesPath,
templateName
);
return readFile(baseTemplatePath).then(baseTemplate =>
parseTemplate(baseTemplate, pageTemplate)
);
const pageTemplate = baseTemplate;
const baseTemplatePath = factoryFilePath(
templatesPath,
templateName
);
return readFile(baseTemplatePath).then(baseTemplate =>
parseTemplate(baseTemplate, pageTemplate)
);
});
});
});
};
};

module.exports.TEMPLATE_ERROR = TEMPLATE_ERROR;
module.exports.TEMPLATE_NOT_FOUND = TEMPLATE_NOT_FOUND;
95 changes: 48 additions & 47 deletions lib/request-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,58 @@ const kaAgentHttps = new HttpsAgent();
* @param {Object} span - opentracing span context passed for propagation
* @returns {Promise} Response from the fragment server
*/
module.exports = (filterHeaders, processFragmentResponse) => (
fragmentUrl,
fragmentAttributes,
request,
span = null
) =>
new Promise((resolve, reject) => {
const parsedUrl = url.parse(fragmentUrl);
const options = Object.assign(
{
headers: Object.assign(
filterHeaders(fragmentAttributes, request),
requiredHeaders
),
timeout: fragmentAttributes.timeout
},
parsedUrl
);
module.exports =
(filterHeaders, processFragmentResponse) =>
(fragmentUrl, fragmentAttributes, request, span = null) =>
new Promise((resolve, reject) => {
const parsedUrl = url.parse(fragmentUrl);
const options = Object.assign(
{
headers: Object.assign(
filterHeaders(fragmentAttributes, request),
requiredHeaders
),
timeout: fragmentAttributes.timeout
},
parsedUrl
);

if (span) {
tracer.inject(span.context(), FORMAT_HTTP_HEADERS, options.headers);
}

const { protocol: reqProtocol, timeout } = options;
const hasHttpsProtocol = reqProtocol === 'https:';
const protocol = hasHttpsProtocol ? https : http;
options.agent = hasHttpsProtocol ? kaAgentHttps : kaAgent;
if (span) {
tracer.inject(
span.context(),
FORMAT_HTTP_HEADERS,
options.headers
);
}

if (hasHttpsProtocol && fragmentAttributes.ignoreInvalidSsl) {
options.rejectUnauthorized = false;
}
const { protocol: reqProtocol, timeout } = options;
const hasHttpsProtocol = reqProtocol === 'https:';
const protocol = hasHttpsProtocol ? https : http;
options.agent = hasHttpsProtocol ? kaAgentHttps : kaAgent;

const fragmentRequest = protocol.request(options);
if (hasHttpsProtocol && fragmentAttributes.ignoreInvalidSsl) {
options.rejectUnauthorized = false;
}

if (timeout) {
fragmentRequest.setTimeout(timeout, fragmentRequest.abort);
}
const fragmentRequest = protocol.request(options);

fragmentRequest.on('response', response => {
try {
resolve(
processFragmentResponse(response, {
request,
fragmentUrl,
fragmentAttributes
})
);
} catch (e) {
reject(e);
if (timeout) {
fragmentRequest.setTimeout(timeout, fragmentRequest.abort);
}

fragmentRequest.on('response', response => {
try {
resolve(
processFragmentResponse(response, {
request,
fragmentUrl,
fragmentAttributes
})
);
} catch (e) {
reject(e);
}
});
fragmentRequest.on('error', reject);
fragmentRequest.end();
});
fragmentRequest.on('error', reject);
fragmentRequest.end();
});
6 changes: 4 additions & 2 deletions lib/template-cutter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const MARKED_PARTS_TO_IGNORE = /<!-- TailorX: Ignore during parsing START -->.*?<!-- TailorX: Ignore during parsing END -->/gims;
const IGNORED_PART_WITH_INDEX = /<!-- TailorX: Ignored content during parsing #(\d+) -->/gm;
const MARKED_PARTS_TO_IGNORE =
/<!-- TailorX: Ignore during parsing START -->.*?<!-- TailorX: Ignore during parsing END -->/gims;
const IGNORED_PART_WITH_INDEX =
/<!-- TailorX: Ignored content during parsing #(\d+) -->/gm;

function replaceIgnorePart(ignoredPartIndex) {
return `<!-- TailorX: Ignored content during parsing #${ignoredPartIndex} -->`;
Expand Down
2 changes: 1 addition & 1 deletion lib/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const opentracing = require('opentracing');
* @see {@link https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/tracer.js~Tracer.html}
*/
module.exports = {
initTracer: function(implementation) {
initTracer: function (implementation) {
opentracing.initGlobalTracer(
implementation || new opentracing.Tracer()
);
Expand Down
2 changes: 1 addition & 1 deletion lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const parse5 = require('parse5');
const memoize = require('memoizee');
const treeAdapter = parse5.treeAdapters.htmlparser2;
const treeAdapter = require('parse5-htmlparser2-tree-adapter');
const CustomSerializer = require('./serializer');

/**
Expand Down
Loading
Loading