Extracted promise libs and history into @tryghost/promise

- deleted files under `core/server/lib/promise` and related test files
- added `@tryghost/promise` as a dependency
- fixed all local requires to point to the new package
This commit is contained in:
Daniel Lockyer 2020-08-11 18:44:21 +01:00
parent 6ae884d2e4
commit 5b471e1bbe
23 changed files with 25 additions and 203 deletions

View File

@ -3,7 +3,7 @@ const Promise = require('bluebird');
const _ = require('lodash');
const shared = require('../shared');
const errors = require('@tryghost/errors');
const sequence = require('../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const STAGES = {
validation: {

View File

@ -1,6 +1,6 @@
const debug = require('ghost-ignition').debug('api:shared:serializers:handle');
const Promise = require('bluebird');
const sequence = require('../../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const errors = require('@tryghost/errors');
/**

View File

@ -1,7 +1,7 @@
const debug = require('ghost-ignition').debug('api:shared:validators:handle');
const Promise = require('bluebird');
const errors = require('@tryghost/errors');
const sequence = require('../../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
/**
* @description Shared input validation handler.

View File

@ -3,7 +3,7 @@ const _ = require('lodash');
const Promise = require('bluebird');
const ObjectId = require('bson-objectid');
const errors = require('@tryghost/errors');
const sequence = require('../../../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const models = require('../../../../models');
class Base {

View File

@ -3,7 +3,7 @@ const Promise = require('bluebird');
const semver = require('semver');
const {IncorrectUsageError} = require('@tryghost/errors');
const debug = require('ghost-ignition').debug('importer:data');
const sequence = require('../../../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const models = require('../../../../models');
const PostsImporter = require('./posts');
const TagsImporter = require('./tags');

View File

@ -6,8 +6,7 @@ const os = require('os');
const glob = require('glob');
const uuid = require('uuid');
const {extract} = require('@tryghost/zip');
const sequence = require('../../lib/promise/sequence');
const pipeline = require('../../lib/promise/pipeline');
const {pipeline, sequence} = require('@tryghost/promise');
const {i18n} = require('../../lib/common');
const logging = require('../../../shared/logging');
const errors = require('@tryghost/errors');

View File

@ -6,7 +6,7 @@ const Promise = require('bluebird');
const logging = require('../../../../shared/logging');
const models = require('../../../models');
const baseUtils = require('../../../models/base/utils');
const sequence = require('../../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const moment = require('moment');
const fixtures = require('./fixtures');

View File

@ -1,31 +0,0 @@
/**
* # Pipeline Utility
*
* Based on pipeline.js from when.js:
* https://github.com/cujojs/when/blob/3.7.4/pipeline.js
*/
const Promise = require('bluebird');
function pipeline(tasks /* initial arguments */) {
const args = Array.prototype.slice.call(arguments, 1);
let runTask = function (task, args) {
// Self-optimizing function to run first task with multiple
// args using apply, but subsequent tasks via direct invocation
runTask = function (task, arg) {
return task(arg);
};
return task.apply(null, args);
};
// Resolve any promises for the arguments passed in first
return Promise.all(args).then(function (args) {
// Iterate through the tasks passing args from one into the next
return Promise.reduce(tasks, function (arg, task) {
return runTask(task, arg);
}, args);
});
}
module.exports = pipeline;

View File

@ -1,26 +0,0 @@
const Promise = require('bluebird');
/**
* expects an array of functions returning a promise
*/
function sequence(tasks /* Any Arguments */) {
const args = Array.prototype.slice.call(arguments, 1);
return Promise.reduce(tasks, function (results, task) {
const response = task.apply(this, args);
if (response && response.then) {
return response.then(function (result) {
results.push(result);
return results;
});
} else {
return Promise.resolve().then(() => {
results.push(response);
return results;
});
}
}, []);
}
module.exports = sequence;

View File

@ -4,7 +4,7 @@ const models = require('../../models');
const {events} = require('../../lib/common');
const logging = require('../../../shared/logging');
const errors = require('@tryghost/errors');
const sequence = require('../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
/**
* WHEN timezone changes, we will:

View File

@ -1,7 +1,7 @@
const ghostBookshelf = require('./base');
const uuid = require('uuid');
const _ = require('lodash');
const sequence = require('../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const config = require('../../shared/config');
const crypto = require('crypto');

View File

@ -3,7 +3,7 @@ const _ = require('lodash');
const uuid = require('uuid');
const moment = require('moment');
const Promise = require('bluebird');
const sequence = require('../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const {i18n} = require('../lib/common');
const errors = require('@tryghost/errors');
const htmlToText = require('html-to-text');

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
const Promise = require('bluebird');
const {i18n} = require('../../lib/common');
const errors = require('@tryghost/errors');
const sequence = require('../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
/**
* Why and when do we have to fetch `authors` by default?

View File

@ -8,7 +8,7 @@ const {i18n} = require('../lib/common');
const errors = require('@tryghost/errors');
const security = require('@tryghost/security');
const {gravatar} = require('../lib/image');
const pipeline = require('../lib/promise/pipeline');
const {pipeline} = require('@tryghost/promise');
const validation = require('../data/validation');
const permissions = require('../services/permissions');
const activeStates = ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4'];

View File

@ -1,6 +1,6 @@
module.exports = (event, model) => {
const _ = require('lodash');
const sequence = require('../../lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const api = require('../../api');
const apiVersion = model.get('api_version') || 'v3';

View File

@ -58,6 +58,7 @@
"@tryghost/members-csv": "0.2.1",
"@tryghost/members-ssr": "0.8.5",
"@tryghost/mw-session-from-token": "0.1.6",
"@tryghost/promise": "0.1.0",
"@tryghost/security": "0.1.0",
"@tryghost/session-service": "0.1.7",
"@tryghost/social-urls": "0.1.11",

View File

@ -7,7 +7,7 @@ const _ = require('lodash');
const {events} = require('../../../../core/server/lib/common');
const models = require('../../../../core/server/models');
const testUtils = require('../../../utils');
const sequence = require('../../../../core/server/lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
describe('Models: listeners', function () {
const eventsToRemember = {};

View File

@ -5,7 +5,7 @@ const testUtils = require('../../utils');
const moment = require('moment');
const _ = require('lodash');
const Promise = require('bluebird');
const sequence = require('../../../core/server/lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const urlService = require('../../../core/frontend/services/url');
const ghostBookshelf = require('../../../core/server/models/base');
const models = require('../../../core/server/models');

View File

@ -1,100 +0,0 @@
const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
// Stuff we are testing
const pipeline = require('../../../../core/server/lib/promise/pipeline');
// These tests are based on the tests in https://github.com/cujojs/when/blob/3.7.4/test/pipeline-test.js
function createTask(y) {
return function (x) {
return x + y;
};
}
describe('Pipeline', function () {
afterEach(function () {
sinon.restore();
});
it('should execute tasks in order', function () {
return pipeline([createTask('b'), createTask('c'), createTask('d')], 'a').then(function (result) {
result.should.eql('abcd');
});
});
it('should resolve to initial args when no tasks supplied', function () {
return pipeline([], 'a', 'b').then(function (result) {
result.should.eql(['a', 'b']);
});
});
it('should resolve to empty array when no tasks and no args supplied', function () {
return pipeline([]).then(function (result) {
result.should.eql([]);
});
});
it('should pass args to initial task', function () {
const expected = [1, 2, 3];
const tasks = [sinon.spy()];
return pipeline(tasks, 1, 2, 3).then(function () {
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
});
});
it('should allow initial args to be promises', function () {
const expected = [1, 2, 3];
const tasks = [sinon.spy()];
const Resolver = Promise.resolve;
return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function () {
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
});
});
it('should allow tasks to be promises', function () {
const expected = [1, 2, 3];
const tasks = [
sinon.stub().returns(new Promise.resolve(4)),
sinon.stub().returns(new Promise.resolve(5)),
sinon.stub().returns(new Promise.resolve(6))
];
return pipeline(tasks, 1, 2, 3).then(function (result) {
result.should.eql(6);
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
tasks[1].calledOnce.should.be.true();
tasks[1].firstCall.calledWith(4).should.be.true();
tasks[2].calledOnce.should.be.true();
tasks[2].firstCall.calledWith(5).should.be.true();
});
});
it('should allow tasks and args to be promises', function () {
const expected = [1, 2, 3];
const tasks = [
sinon.stub().returns(new Promise.resolve(4)),
sinon.stub().returns(new Promise.resolve(5)),
sinon.stub().returns(new Promise.resolve(6))
];
const Resolver = Promise.resolve;
return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function (result) {
result.should.eql(6);
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
tasks[1].calledOnce.should.be.true();
tasks[1].firstCall.calledWith(4).should.be.true();
tasks[2].calledOnce.should.be.true();
tasks[2].firstCall.calledWith(5).should.be.true();
});
});
});

View File

@ -1,28 +0,0 @@
const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
const sequence = require('../../../../core/server/lib/promise/sequence');
describe('Unit: lib/promise/sequence', function () {
afterEach(function () {
sinon.restore();
});
it('mixed tasks: promise and none promise', function () {
const tasks = [
function a() {
return Promise.resolve('hello');
},
function b() {
return 'from';
},
function c() {
return Promise.resolve('chio');
}
];
return sequence(tasks)
.then(function (result) {
result.should.eql(['hello','from', 'chio']);
});
});
});

View File

@ -4,7 +4,7 @@ const url = require('url');
const moment = require('moment');
const DataGenerator = require('./fixtures/data-generator');
const config = require('../../core/shared/config');
const sequence = require('../../core/server/lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const host = config.get('server').host;
const port = config.get('server').port;
const protocol = 'http://';

View File

@ -25,7 +25,7 @@ const settingsCache = require('../../core/server/services/settings/cache');
const imageLib = require('../../core/server/lib/image');
const web = require('../../core/server/web');
const permissions = require('../../core/server/services/permissions');
const sequence = require('../../core/server/lib/promise/sequence');
const {sequence} = require('@tryghost/promise');
const themes = require('../../core/frontend/services/themes');
const DataGenerator = require('./fixtures/data-generator');
const configUtils = require('./configUtils');

View File

@ -465,6 +465,13 @@
chalk "4.1.0"
sywac "1.3.0"
"@tryghost/promise@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/promise/-/promise-0.1.0.tgz#f83a0ee39b4ae8877889c17b6e9f488ad4dbb6ab"
integrity sha512-33YNODsFn2tp1dT9bKRYKHls4bMVODHDYx9PVQO9IdGHHzw4K6TRmAuig8yG5OYy6a04d/mBtrX+oYIAvWAa/w==
dependencies:
bluebird "3.7.2"
"@tryghost/security@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/security/-/security-0.1.0.tgz#ce78111dd6febb7705cf62d6838b0d8cdfb7df9f"