Skip to content

Commit

Permalink
Merge pull request #366 from cb1kenobi/DAEMON-273
Browse files Browse the repository at this point in the history
[DAEMON-273] fix(appcd-plugin): Fixed plugin IPC tunnel to send the 'headers' and …
  • Loading branch information
cb1kenobi committed Apr 3, 2019
2 parents 7d37fab + 86f1b8b commit 11b37a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
6 changes: 4 additions & 2 deletions packages/appcd-client/src/client.js
Expand Up @@ -287,7 +287,7 @@ export default class Client {
// be wired up
setImmediate(() => {
this.connect()
.on('connected', client => {
.once('connected', client => {
this.requests[id] = response => {
const status = response.status = ~~response.status || 500;
const statusClass = Math.floor(status / 100);
Expand All @@ -296,7 +296,9 @@ export default class Client {
// no need for the id anymore
delete response.id;

log(`${style(status)} ${highlight(req.path)} ${note(`${new Date() - startTime}ms`)}`);
if (response.fin) {
log(`${style(status)} ${highlight(req.path)} ${note(`${new Date() - startTime}ms`)}`);
}

switch (statusClass) {
case 2:
Expand Down
6 changes: 6 additions & 0 deletions packages/appcd-plugin/CHANGELOG.md
@@ -1,3 +1,9 @@
# v1.3.1

* Fixed plugin IPC tunnel to send the `"headers"` and `"source"` `DispatcherContext` properties.
The `"data"` property has been renamed to `"request"` to match the `DispatcherContext` property
name. [(DAEMON-273)](https://jira.appcelerator.org/browse/DAEMON-273)

# v1.3.0 (Mar 29, 2019)

* Reimplemented the `/` endpoint using a `DataServiceDispatcher` so that the data can be filtered
Expand Down
26 changes: 18 additions & 8 deletions packages/appcd-plugin/src/external-plugin.js
@@ -1,6 +1,6 @@
import Agent from 'appcd-agent';
import appcdLogger from 'appcd-logger';
import Dispatcher, { DispatcherError } from 'appcd-dispatcher';
import Dispatcher, { DispatcherContext, DispatcherError } from 'appcd-dispatcher';
import FSWatcher from 'appcd-fswatcher';
import gawk from 'gawk';
import path from 'path';
Expand All @@ -11,7 +11,7 @@ import Response, { AppcdError, codes } from 'appcd-response';
import Tunnel from './tunnel';

import { debounce } from 'appcd-util';
import { Readable } from 'stream';
import { PassThrough, Readable } from 'stream';

const { alert, highlight, note, notice, ok } = appcdLogger.styles;

Expand Down Expand Up @@ -198,7 +198,7 @@ export default class ExternalPlugin extends PluginBase {
this.appcdLogger.log('Received request from parent:');
this.appcdLogger.log(req);

if (req.message.type === 'deactivate') {
if (req.message.request.type === 'deactivate') {
try {
await cancelConfigSubscription();
if (this.module && typeof this.module.deactivate === 'function') {
Expand All @@ -212,7 +212,7 @@ export default class ExternalPlugin extends PluginBase {
}
}

if (req.message.type === 'health') {
if (req.message.request.type === 'health') {
if (this.agent) {
return send({
message: {
Expand All @@ -231,8 +231,13 @@ export default class ExternalPlugin extends PluginBase {
}

try {
this.appcdLogger.log('Dispatching %s', highlight(req.message.path), req.message.data);
const { status, response } = await this.dispatcher.call(req.message.path, req.message.data);
this.appcdLogger.log('Dispatching %s', highlight(req.message.path), req.message.request);
const { status, response } = await this.dispatcher.call(req.message.path, new DispatcherContext({
headers: req.message.headers,
request: req.message.request,
response: new PassThrough({ objectMode: true }),
source: req.message.source
}));

if (response instanceof Readable) {
// we have a stream
Expand Down Expand Up @@ -494,7 +499,12 @@ export default class ExternalPlugin extends PluginBase {
// dispatcher request
try {
const startTime = new Date();
const { status, response } = await Dispatcher.call(req.message.path, req.message.data);
const { status, response } = await Dispatcher.call(req.message.path, new DispatcherContext({
headers: req.message.headers,
request: req.message.request,
response: new PassThrough({ objectMode: true }),
source: req.message.source
}));
const style = status < 400 ? ok : alert;

let msg = `Plugin dispatcher: ${highlight(req.message.path || '/')} ${style(status)}`;
Expand All @@ -506,7 +516,7 @@ export default class ExternalPlugin extends PluginBase {
if (response instanceof Readable) {
// we have a stream

const { data } = req.message.data;
const { data } = req.message.request;
this.appcdLogger.log(`${highlight(req.message.path)} ${data && Array.isArray(data.args) && note(data.args.join(' ')) || ''} returned a streamed response`);

// track if this stream is a pubsub stream so we know to send the `fin`
Expand Down
22 changes: 12 additions & 10 deletions packages/appcd-plugin/src/tunnel.js
Expand Up @@ -125,18 +125,15 @@ export default class Tunnel {
send(ctxOrPayload) {
return new Promise((resolve, reject) => {
const id = uuid.v4();
let ctx;
let ctx = ctxOrPayload;

if (ctxOrPayload instanceof DispatcherContext) {
ctx = ctxOrPayload;
ctx.request = {
path: ctxOrPayload.path,
data: ctxOrPayload.request
};
} else {
if (!(ctxOrPayload instanceof DispatcherContext)) {
ctx = new DispatcherContext({
request: typeof ctxOrPayload === 'object' && ctxOrPayload || {},
headers: {},
path: ctxOrPayload.path,
request: typeof ctxOrPayload === 'object' && ctxOrPayload.data || ctxOrPayload || {},
response: new PassThrough({ objectMode: true }),
source: null,
status: 200
});
}
Expand Down Expand Up @@ -216,7 +213,12 @@ export default class Tunnel {

const req = {
id,
message: ctx.request,
message: {
headers: ctx.headers,
path: ctx.path,
request: ctx.request,
source: ctx.source
},
type: 'request'
};

Expand Down

0 comments on commit 11b37a0

Please sign in to comment.