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

RUN-1612: Deps, tests, promises #1

Merged
merged 6 commits into from
Dec 2, 2024
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
6 changes: 5 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"presets": ["airbnb"],
"presets": [["airbnb", {
"targets": {
"node": "current"
}
}]],
"plugins": [
"add-module-exports",
["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }],
Expand Down
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"rules": {
"indent": 1,
"prefer-promise-reject-errors": 0,
"import/named": 0
},
},
{
Expand Down
19 changes: 0 additions & 19 deletions .github/workflows/node-aught.yml

This file was deleted.

7 changes: 0 additions & 7 deletions .github/workflows/node-pretest.yml

This file was deleted.

18 changes: 0 additions & 18 deletions .github/workflows/node-tens.yml

This file was deleted.

15 changes: 0 additions & 15 deletions .github/workflows/rebase.yml

This file was deleted.

12 changes: 0 additions & 12 deletions .github/workflows/require-allow-edits.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run tests

on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install dependencies
run: npm install

- name: Lint
run: npm run lint

- name: Run tests
run: npm run test:ci

- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: ./coverage
5 changes: 4 additions & 1 deletion .nycrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"reporter": ["lcov", "text-summary"],
"lines": 86,
"statements": 85.93,
"functions": 82.43,
"branches": 76.06,
"include": [
"src/**/*.js"
],
"exclude": [
"coverage",
"test"
Expand Down
35 changes: 16 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
"clean": "rimraf lib",
"prebuild": "npm run clean",
"build": "babel src -d lib",
"prelint": "npm run build",
"lint": "eslint src test",
"pretest": "npm run --silent lint",
"test": "npm run coverage",
"pretests-only": "npm run build",
"tests-only": "npm run test:quick",
"posttest": "aud --production",
"precoverage": "npm run build",
"coverage": "babel-node node_modules/.bin/istanbul cover --report html node_modules/.bin/_mocha -- -R tap test/init.js test/*-test.js",
"postcoverage": "npm run cover:check",
"cover:check": "istanbul check-coverage && echo code coverage thresholds met, achievement unlocked!",
"test:quick": "babel-node node_modules/.bin/_mocha -R tap test/init.js test/*-test.js"
"posttest": "npm audit",
"coverage": "nyc --require @babel/register -- node_modules/.bin/_mocha -R nyan test/init.js test/*-test.js",
"test:files": "mocha --require @babel/register test/init.js",
"test:files:help": "echo 'Use like npm run test:files -- test/my-test.js test/my-other-test.js'",
"test:quick": "npm run test:files -- test/*-test.js -R dots",
"test:ci": "npm run coverage -- -R spec"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,22 +45,22 @@
},
"homepage": "https://github.com/airbnb/hypernova",
"devDependencies": {
"aud": "^2.0.0",
"babel-cli": "^6.26.0",
"@babel/cli": "^7.25.9",
"@babel/core": "^7.26.0",
"@babel/node": "^7.26.0",
"@babel/register": "^7.25.9",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-replace-object-assign": "^1.0.0",
"babel-preset-airbnb": "^2.5.3",
"babel-preset-airbnb": "^5.0.0",
"chai": "^4.3.6",
"cheerio": "=1.0.0-rc.3",
"cheerio": "^1.0.0",
"eslint": "^8.14.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"mocha": "^3.5.3",
"mocha-wrap": "^2.1.2",
"nyc": "^10.3.2",
"mocha": "^10.8.2",
"nyc": "^17.1.0",
"rimraf": "^2.6.3",
"sinon": "^3.3.0",
"sinon-sandbox": "^1.0.2"
"sinon": "^19.0.2"
},
"dependencies": {
"airbnb-js-shims": "^2 || ^3",
Expand All @@ -77,7 +74,7 @@
"winston": "^2.4.5"
},
"engines": {
"node": ">= 0.10"
"node": ">=18.0.0"
},
"greenkeeper": {
"ignore": [
Expand Down
23 changes: 16 additions & 7 deletions src/environment.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* eslint func-names:0 no-extra-parens:0 */

import 'airbnb-js-shims';
import Promise from 'bluebird';
import { envIsTrue } from './utils/utils';

const promiseShimsEnvKey = 'HYPERNOVA_PROMISE_SHIMS';
const shouldShimPromise = envIsTrue(process.env[promiseShimsEnvKey]);

const es6methods = ['then', 'catch', 'constructor'];
const es6StaticMethods = ['all', 'race', 'resolve', 'reject', 'cast'];
const es6methods = ['then', 'catch', 'constructor', 'finally'];
const es6StaticMethods = ['all', 'allSettled', 'any', 'race', 'resolve', 'reject', 'cast', 'try', 'withResolvers'];

function isNotMethod(name) {
return !(es6methods.includes(name) || es6StaticMethods.includes(name) || name.charAt(0) === '_');
Expand All @@ -18,9 +23,13 @@ function toFastProperties(obj) {
(function () {}).prototype = obj;
}

Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(Promise.prototype));
Object.keys(Promise).filter(isNotMethod).forEach(del(Promise));
toFastProperties(Promise);
toFastProperties(Promise.prototype);
if (shouldShimPromise) {
Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(Promise.prototype));
Object.keys(Promise).filter(isNotMethod).forEach(del(Promise));
toFastProperties(Promise);
toFastProperties(Promise.prototype);

global.Promise = Promise;
}

global.Promise = Promise;
export { promiseShimsEnvKey, shouldShimPromise as didShimPromise };
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function hypernova(userConfig, onServer) {
}

if (config.devMode) {
worker(app, config, onServer);
app.devServer = worker(app, config, onServer);
} else if (cluster.isMaster) {
coordinator(config.getCPUs);
} else {
Expand Down
17 changes: 16 additions & 1 deletion src/utils/renderBatch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import fs from 'fs';
import BatchManager from './BatchManager';
import { processBatch } from './lifecycle';
import logger from './logger';
import { envIsTrue } from './utils';
import { didShimPromise } from '../environment';

const shouldProfile = envIsTrue(process.env.HYPERNOVA_PROFILING);

export default (config, isClosing) => (req, res) => {
const now = performance.now();

// istanbul ignore if
if (isClosing()) {
logger.info('Starting request when closing!');
Expand All @@ -19,5 +26,13 @@ export default (config, isClosing) => (req, res) => {
}
return res.status(manager.statusCode).json(manager.getResults()).end();
})
.catch(() => res.status(manager.statusCode).end());
.catch(() => res.status(manager.statusCode).end())
.finally(() => {
if (!shouldProfile) return;

const diff = performance.now() - now;
const key = Object.values(jobs).map((v) => v.name).join('__');
const line = `${diff},${key}\n`;
fs.appendFileSync(`stats-${didShimPromise ? 'shim' : 'noshim'}.txt`, line, 'utf8');
});
};
7 changes: 7 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function envIsTrue(value) {
return value === true || value === 'TRUE' || value === 'true' || !!parseInt(value, 10);
}

export function envIsFalse(value) {
return !envIsTrue(value);
}
2 changes: 2 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ const worker = (app, config, onServer, workerId) => {

logger.info('Connected', { listen: config.listenArgs });
});

return server;
};

worker.attachMiddleware = attachMiddleware;
Expand Down
4 changes: 2 additions & 2 deletions test/BatchManager-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assert } from 'chai';
import sinon from 'sinon-sandbox';
import sinon from 'sinon';

import { makeJob, COMPONENT_NAME } from './helper';
import BatchManager from '../lib/utils/BatchManager';
import BatchManager from '../src/utils/BatchManager';

function mockPlugin() {
return {
Expand Down Expand Up @@ -31,7 +31,7 @@

const req = {};
const res = {};
const _strategies = {

Check warning on line 34 in test/BatchManager-test.js

View workflow job for this annotation

GitHub Actions / test

Unexpected dangling '_' in '_strategies'
[COMPONENT_NAME]: sinon.stub().returns('html'),
baz: sinon.stub().returns(undefined),
};
Expand Down
2 changes: 1 addition & 1 deletion test/Module-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from 'chai';
import has from 'has';
import { Module } from '../server';
import { Module } from '../src/server';
import mutableArray from './mutableArray';

function run(code) {
Expand All @@ -8,7 +8,7 @@

const module = new Module(name);
module.load(name);
module._compile(code, name);

Check warning on line 11 in test/Module-test.js

View workflow job for this annotation

GitHub Actions / test

Unexpected dangling '_' in '_compile'

return module.exports;
}
Expand Down
2 changes: 1 addition & 1 deletion test/checkIso.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cheerio from 'cheerio';
import * as cheerio from 'cheerio';
import { assert } from 'chai';

export default {
Expand Down
Loading
Loading