-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e91a82e
commit 4f09bd7
Showing
9 changed files
with
392 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {default} from '@cfware/nyc'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.