Skip to content

Commit

Permalink
feat!: Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Oct 15, 2023
1 parent e91a82e commit 4f09bd7
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 397 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name: release-please
on:
push:
branches: [master]
branches:
- master

permissions:
contents: write
pull-requests: write

name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: bcoe/release-please-action@v1.2.1
- uses: google-github-actions/release-please-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: node
package-name: "fastify-babel"
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [14, 16, 18]
node-version: [18, 20]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Node.js ${{matrix.node-version}} on ${{matrix.os}}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{matrix.node-version}}
- run: npm install
Expand Down
112 changes: 0 additions & 112 deletions index.cjs

This file was deleted.

110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import path from 'node:path';
import fp from 'fastify-plugin';
import babel from '@babel/core';
import hasha from 'hasha';

function shouldBabel(reply, options) {
return options.babelTypes.test(reply.getHeader('Content-Type') || '');
}

function babelPlugin(fastify, options, next) {
if (!options.babelTypes) {
options.babelTypes = /(?:java|ecma)script/u;
}

const cacheSalt = options.cacheHashSalt ? hasha(options.cacheHashSalt, {algorithm: 'sha256'}) : '';

fastify.addHook('onSend', babelOnSend);

next();

function actualSend(payload, next, hash, filename) {
const babelOptions = {
...options.babelrc,
filename: filename || path.join(process.cwd(), 'index.js')
};

try {
const {code} = babel.transform(payload, babelOptions);
if (hash) {
options.cache.set(hash, code);
}

next(null, code);
} catch (error) {
let sendError = error;
if (options.maskError !== false) {
let message = 'Babel Internal Error';
try {
message = `Babel Transform error ${error.code} at line ${error.loc.line}, column ${error.loc.column}.`;
} catch {}

/* istanbul ignore next */
try {
// Current versions of babel set the property readonly but configurable
const desc = Object.getOwnPropertyDescriptor(sendError, 'message');
desc.value = message;
Object.defineProperty(sendError, 'message', desc);
} catch {
// Last resort if 'message' property is not configurable
const {code} = sendError;
sendError = new Error(message);
sendError.code = code;
}
}

next(sendError);
}
}

function babelOnSend(requests, reply, payload, next) {
if (requests.headers['x-no-babel'] !== undefined) {
return next();
}

if (!shouldBabel(reply, options)) {
return next();
}

if (payload === null) {
return next();
}

reply.removeHeader('content-length');
if (payload === '') {
/* Skip babel if we have empty payload (304's for example). */
return next(null, '');
}

let hash;
if (options.cache) {
const cacheTag = reply.getHeader('etag') || reply.getHeader('last-modified');
/* If we don't have etag or last-modified assume this is dynamic and not worth caching */
if (cacheTag) {
/* Prefer payload.filename, then payload it is a string */
const filename = typeof payload === 'string' ? payload : payload.filename;
hash = hasha([cacheTag, filename, cacheSalt], {algorithm: 'sha256'});
const result = options.cache.get(hash);

if (result !== undefined) {
next(null, result);
return;
}
}
}

if (typeof payload === 'string') {
actualSend(payload, next, hash);
return;
}

const code = [];
payload.on('data', chunk => code.push(chunk));
payload.on('end', () => actualSend(code.join(''), next, hash, payload.filename));
}
}

export default fp(babelPlugin, {
fastify: '>=4.4.0',
name: 'fastify-babel'
});
3 changes: 0 additions & 3 deletions nyc.config.cjs

This file was deleted.

1 change: 1 addition & 0 deletions nyc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from '@cfware/nyc';
101 changes: 52 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
{
"name": "fastify-babel",
"version": "3.0.0",
"description": "Fastify Babel plugin for development servers",
"main": "index.cjs",
"exports": "./index.cjs",
"scripts": {
"pretest": "cfware-lint .",
"tests-only": "nyc -s node test/test.cjs",
"test": "npm run -s tests-only",
"posttest": "nyc report --check-coverage"
},
"engines": {
"node": ">=14"
},
"author": "Corey Farrell",
"license": "MIT",
"keywords": [
"fastify",
"babel"
],
"repository": {
"type": "git",
"url": "git+https://github.com/cfware/fastify-babel.git"
},
"bugs": {
"url": "https://github.com/cfware/fastify-babel/issues"
},
"homepage": "https://github.com/cfware/fastify-babel#readme",
"dependencies": {
"fastify-plugin": "^4.1.0",
"hasha": "^5.2.2"
},
"peerDependencies": {
"@babel/core": "^7.18.10"
},
"devDependencies": {
"@babel/core": "^7.18.10",
"@cfware/lint": "^3.0.0",
"@cfware/nyc": "^0.7.1",
"@fastify/static": "^6.5.0",
"babel-plugin-bare-import-rewrite": "^2.0.0",
"fastify": "^4.4.0",
"libtap": "^1.4.0",
"node-fetch": "^2",
"nyc": "^15.1.0",
"quick-lru": "^5",
"semver": "^7.3.7",
"string-to-stream": "^3.0.1"
}
"name": "fastify-babel",
"version": "4.0.0",
"description": "Fastify Babel plugin for development servers",
"main": "index.js",
"exports": "./index.js",
"type": "module",
"scripts": {
"pretest": "cfware-lint .",
"tests-only": "nyc -s node --experimental-loader @istanbuljs/esm-loader-hook test.js|tap-yaml-summary",
"test": "npm run -s tests-only",
"posttest": "nyc report --check-coverage"
},
"engines": {
"node": ">=18"
},
"author": "Corey Farrell",
"license": "MIT",
"keywords": [
"fastify",
"babel"
],
"repository": {
"type": "git",
"url": "git+https://github.com/cfware/fastify-babel.git"
},
"bugs": {
"url": "https://github.com/cfware/fastify-babel/issues"
},
"homepage": "https://github.com/cfware/fastify-babel#readme",
"dependencies": {
"fastify-plugin": "^4",
"hasha": "^5"
},
"peerDependencies": {
"@babel/core": "^7"
},
"devDependencies": {
"@babel/core": "^7",
"@cfware/lint": "^4",
"@cfware/nyc": "^1",
"@fastify/static": "^6",
"@istanbuljs/esm-loader-hook": "^0.2",
"babel-plugin-bare-import-rewrite": "^2",
"fastify": "^4",
"libtap": "^1",
"node-fetch": "^3",
"nyc": "^15",
"quick-lru": "^7",
"resolve": "^1",
"string-to-stream": "^3",
"tap-yaml-summary": "^0.2"
}
}
Loading

0 comments on commit 4f09bd7

Please sign in to comment.