Skip to content

Commit

Permalink
feat(http): accept null isolation scope to turn off isolation
Browse files Browse the repository at this point in the history
ISSUES CLOSED: 526
  • Loading branch information
staltz committed Mar 8, 2017
1 parent e35b731 commit e16febc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
4 changes: 2 additions & 2 deletions http/most-typings.d.ts
Expand Up @@ -3,6 +3,6 @@ import {Stream} from 'most';
export interface HTTPSource {
filter(predicate: (request: RequestOptions) => boolean): HTTPSource;
select(category?: string): Stream<Stream<Response> & ResponseStream>;
isolateSource: (source: HTTPSource, scope: string) => HTTPSource;
isolateSink: (sink: Stream<RequestInput>, scope: string) => Stream<RequestInput>;
isolateSource: (source: HTTPSource, scope: string | null) => HTTPSource;
isolateSink: (sink: Stream<RequestInput | string>, scope: string | null) => Stream<RequestInput>;
}
4 changes: 2 additions & 2 deletions http/rxjs-typings.d.ts
Expand Up @@ -3,6 +3,6 @@ import {Observable} from 'rxjs';
export interface HTTPSource {
filter(predicate: (request: RequestOptions) => boolean): HTTPSource;
select(category?: string): Observable<Observable<Response> & ResponseStream>;
isolateSource: (source: HTTPSource, scope: string) => HTTPSource;
isolateSink: (sink: Observable<RequestInput>, scope: string) => Observable<RequestInput>;
isolateSource: (source: HTTPSource, scope: string | null) => HTTPSource;
isolateSink: (sink: Observable<RequestInput | string>, scope: string | null) => Observable<RequestInput>;
}
4 changes: 2 additions & 2 deletions http/src/MainHTTPSource.ts
Expand Up @@ -25,6 +25,6 @@ export class MainHTTPSource implements HTTPSource {
return out;
}

public isolateSource: (source: HTTPSource, scope: string) => HTTPSource = isolateSource;
public isolateSink: (sink: Stream<any>, scope: string) => Stream<any> = isolateSink;
public isolateSource = isolateSource;
public isolateSink = isolateSink;
}
18 changes: 11 additions & 7 deletions http/src/isolate.ts
@@ -1,18 +1,22 @@
import {Stream} from 'xstream';
import {HTTPSource, RequestOptions, RequestInput} from './interfaces';

export interface Mappable<T> {
map<R>(project: (x: T) => R): Mappable<R>;
}

export function isolateSource(httpSource: HTTPSource, scope: string): HTTPSource {
export function isolateSource(httpSource: HTTPSource,
scope: string | null): HTTPSource {
if (scope === null) {
return httpSource;
}
return httpSource.filter((request: RequestOptions) =>
Array.isArray(request._namespace) &&
request._namespace.indexOf(scope) !== -1,
);
}

export function isolateSink(request$: Mappable<RequestInput | string>,
scope: string): any {
export function isolateSink(request$: Stream<RequestInput | string>,
scope: string | null): Stream<RequestInput> {
if (scope === null) {
return request$;
}
return request$.map((req: RequestInput | string) => {
if (typeof req === 'string') {
return {url: req, _namespace: [scope]} as RequestOptions;
Expand Down
31 changes: 31 additions & 0 deletions http/test/browser/src/common.ts
Expand Up @@ -377,5 +377,36 @@ export function run(uri: string) {

run();
});

it('should allow null scope to bypass isolation', function (done) {
const proxyRequest$ = new Rx.Subject();
function main(sources: {HTTP: HTTPSource}) {
return {
HTTP: proxyRequest$,
};
}

const {sources, run} = Cycle.setup(main, { HTTP: makeHTTPDriver() });

const ignoredRequest$ = Rx.Observable.of(uri + '/json');
const request$ = Rx.Observable.of(uri + '/hello').delay(100);
const scopedRequest$ = sources.HTTP.isolateSink(proxyRequest$, null);
const scopedHTTPSource = sources.HTTP.isolateSource(sources.HTTP, null);

const expected = [uri + '/json', uri + '/hello'];

scopedHTTPSource.select().subscribe(function (response$: any) {
assert.strictEqual(typeof response$.request, 'object');
assert.strictEqual(response$.request.url, expected.shift());
if (expected.length === 0) {
done();
}
});

run();

Rx.Observable.merge(ignoredRequest$, request$)
.subscribe(proxyRequest$);
});
});
}

0 comments on commit e16febc

Please sign in to comment.