Convert ddp-client to use only api.mainModule for client and server

This commit is contained in:
Sashko Stubailo
2017-11-08 08:10:18 -08:00
parent 70e815e7be
commit cdc4285357
10 changed files with 133 additions and 97 deletions

View File

@@ -0,0 +1,14 @@
import { DDP, LivedataTest } from './namespace';
import './livedata_common';
import './sockjs-0.3.4';
import './stream_client_sockjs';
import './stream_client_common';
import './random_stream';
import './livedata_connection';
import './client_convenience';
export { DDP, LivedataTest };

View File

@@ -1,4 +1,6 @@
import { DDP, LivedataTest } from "./namespace.js";
import { DDPCommon } from 'meteor/ddp-common';
import { Meteor } from 'meteor/meteor';
LivedataTest.SUPPORTED_DDP_VERSIONS = DDPCommon.SUPPORTED_DDP_VERSIONS;

View File

@@ -1,5 +1,11 @@
import { DDP, LivedataTest } from "./namespace.js";
import { MongoIDMap } from "./id_map.js";
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { DDPCommon } from 'meteor/ddp-common';
import { Tracker } from 'meteor/tracker';
import { EJSON } from 'meteor/ejson';
import { Random } from 'meteor/random';
if (Meteor.isServer) {
var Fiber = Npm.require('fibers');

View File

@@ -27,19 +27,10 @@ Package.onUse(function (api) {
// _idParse, _idStringify.
api.use('mongo-id', ['client', 'server']);
api.addFiles(['sockjs-0.3.4.js', 'stream_client_sockjs.js'], 'client');
api.addFiles('stream_client_nodejs.js', 'server');
api.addFiles('stream_client_common.js', ['client', 'server']);
api.addFiles('livedata_common.js', ['client', 'server']);
api.addFiles('random_stream.js', ['client', 'server']);
api.addFiles('livedata_connection.js', ['client', 'server']);
api.addFiles('client_convenience.js', 'client');
api.mainModule("namespace.js");
// For backcompat where things use Package.ddp.DDP, etc
api.export('DDP');
api.mainModule("client.js", "client");
api.mainModule("server.js", "server");
});
Package.onTest(function (api) {

View File

@@ -1,4 +1,5 @@
import { DDP } from "./namespace.js";
import { DDPCommon } from 'meteor/ddp-common';
// Returns the named sequence of pseudo-random values.
// The scope will be DDP._CurrentMethodInvocation.get(), so the stream will produce

View File

@@ -0,0 +1,11 @@
import { DDP, LivedataTest } from './namespace';
import './livedata_common';
import './stream_client_nodejs';
import './stream_client_common';
import './random_stream';
import './livedata_connection';
export { DDP, LivedataTest };

View File

@@ -1,90 +1,11 @@
import { DDP, LivedataTest } from "./namespace.js";
import { Random } from 'meteor/random';
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { Tracker } from 'meteor/tracker';
import { Retry } from 'meteor/retry';
// XXX from Underscore.String (http://epeli.github.com/underscore.string/)
var startsWith = function(str, starts) {
return str.length >= starts.length &&
str.substring(0, starts.length) === starts;
};
var endsWith = function(str, ends) {
return str.length >= ends.length &&
str.substring(str.length - ends.length) === ends;
};
// @param url {String} URL to Meteor app, eg:
// "/" or "madewith.meteor.com" or "https://foo.meteor.com"
// or "ddp+sockjs://ddp--****-foo.meteor.com/sockjs"
// @returns {String} URL to the endpoint with the specific scheme and subPath, e.g.
// for scheme "http" and subPath "sockjs"
// "http://subdomain.meteor.com/sockjs" or "/sockjs"
// or "https://ddp--1234-foo.meteor.com/sockjs"
var translateUrl = function(url, newSchemeBase, subPath) {
if (! newSchemeBase) {
newSchemeBase = "http";
}
var ddpUrlMatch = url.match(/^ddp(i?)\+sockjs:\/\//);
var httpUrlMatch = url.match(/^http(s?):\/\//);
var newScheme;
if (ddpUrlMatch) {
// Remove scheme and split off the host.
var urlAfterDDP = url.substr(ddpUrlMatch[0].length);
newScheme = ddpUrlMatch[1] === "i" ? newSchemeBase : newSchemeBase + "s";
var slashPos = urlAfterDDP.indexOf('/');
var host =
slashPos === -1 ? urlAfterDDP : urlAfterDDP.substr(0, slashPos);
var rest = slashPos === -1 ? '' : urlAfterDDP.substr(slashPos);
// In the host (ONLY!), change '*' characters into random digits. This
// allows different stream connections to connect to different hostnames
// and avoid browser per-hostname connection limits.
host = host.replace(/\*/g, function () {
return Math.floor(Random.fraction()*10);
});
return newScheme + '://' + host + rest;
} else if (httpUrlMatch) {
newScheme = !httpUrlMatch[1] ? newSchemeBase : newSchemeBase + "s";
var urlAfterHttp = url.substr(httpUrlMatch[0].length);
url = newScheme + "://" + urlAfterHttp;
}
// Prefix FQDNs but not relative URLs
if (url.indexOf("://") === -1 && !startsWith(url, "/")) {
url = newSchemeBase + "://" + url;
}
// XXX This is not what we should be doing: if I have a site
// deployed at "/foo", then DDP.connect("/") should actually connect
// to "/", not to "/foo". "/" is an absolute path. (Contrast: if
// deployed at "/foo", it would be reasonable for DDP.connect("bar")
// to connect to "/foo/bar").
//
// We should make this properly honor absolute paths rather than
// forcing the path to be relative to the site root. Simultaneously,
// we should set DDP_DEFAULT_CONNECTION_URL to include the site
// root. See also client_convenience.js #RationalizingRelativeDDPURLs
url = Meteor._relativeToSiteRootUrl(url);
if (endsWith(url, "/"))
return url + subPath;
else
return url + "/" + subPath;
};
toSockjsUrl = function (url) {
return translateUrl(url, "http", "sockjs");
};
toWebsocketUrl = function (url) {
var ret = translateUrl(url, "ws", "websocket");
return ret;
};
LivedataTest.toSockjsUrl = toSockjsUrl;
_.extend(LivedataTest.ClientStream.prototype, {
// Register for callbacks.
on: function (name, callback) {
var self = this;

View File

@@ -1,4 +1,7 @@
import { DDP, LivedataTest } from "./namespace.js";
import { DDP, LivedataTest } from "./namespace";
import { _ } from 'meteor/underscore';
import { toWebsocketUrl } from './urlHelpers';
import { Meteor } from 'meteor/meteor';
// @param endpoint {String} URL to Meteor app
// "http://subdomain.meteor.com/" or "/" or

View File

@@ -1,4 +1,7 @@
import { DDP, LivedataTest } from "./namespace.js";
import { _ } from 'meteor/underscore';
import { Meteor } from 'meteor/meteor';
import { toSockjsUrl } from './urlHelpers';
// @param url {String} URL to Meteor app
// "http://subdomain.meteor.com/" or "/" or

View File

@@ -0,0 +1,84 @@
import { LivedataTest } from './namespace';
import { Random } from 'meteor/random';
// XXX from Underscore.String (http://epeli.github.com/underscore.string/)
var startsWith = function(str, starts) {
return str.length >= starts.length &&
str.substring(0, starts.length) === starts;
};
var endsWith = function(str, ends) {
return str.length >= ends.length &&
str.substring(str.length - ends.length) === ends;
};
// @param url {String} URL to Meteor app, eg:
// "/" or "madewith.meteor.com" or "https://foo.meteor.com"
// or "ddp+sockjs://ddp--****-foo.meteor.com/sockjs"
// @returns {String} URL to the endpoint with the specific scheme and subPath, e.g.
// for scheme "http" and subPath "sockjs"
// "http://subdomain.meteor.com/sockjs" or "/sockjs"
// or "https://ddp--1234-foo.meteor.com/sockjs"
var translateUrl = function(url, newSchemeBase, subPath) {
if (! newSchemeBase) {
newSchemeBase = "http";
}
var ddpUrlMatch = url.match(/^ddp(i?)\+sockjs:\/\//);
var httpUrlMatch = url.match(/^http(s?):\/\//);
var newScheme;
if (ddpUrlMatch) {
// Remove scheme and split off the host.
var urlAfterDDP = url.substr(ddpUrlMatch[0].length);
newScheme = ddpUrlMatch[1] === "i" ? newSchemeBase : newSchemeBase + "s";
var slashPos = urlAfterDDP.indexOf('/');
var host =
slashPos === -1 ? urlAfterDDP : urlAfterDDP.substr(0, slashPos);
var rest = slashPos === -1 ? '' : urlAfterDDP.substr(slashPos);
// In the host (ONLY!), change '*' characters into random digits. This
// allows different stream connections to connect to different hostnames
// and avoid browser per-hostname connection limits.
host = host.replace(/\*/g, function () {
return Math.floor(Random.fraction()*10);
});
return newScheme + '://' + host + rest;
} else if (httpUrlMatch) {
newScheme = !httpUrlMatch[1] ? newSchemeBase : newSchemeBase + "s";
var urlAfterHttp = url.substr(httpUrlMatch[0].length);
url = newScheme + "://" + urlAfterHttp;
}
// Prefix FQDNs but not relative URLs
if (url.indexOf("://") === -1 && !startsWith(url, "/")) {
url = newSchemeBase + "://" + url;
}
// XXX This is not what we should be doing: if I have a site
// deployed at "/foo", then DDP.connect("/") should actually connect
// to "/", not to "/foo". "/" is an absolute path. (Contrast: if
// deployed at "/foo", it would be reasonable for DDP.connect("bar")
// to connect to "/foo/bar").
//
// We should make this properly honor absolute paths rather than
// forcing the path to be relative to the site root. Simultaneously,
// we should set DDP_DEFAULT_CONNECTION_URL to include the site
// root. See also client_convenience.js #RationalizingRelativeDDPURLs
url = Meteor._relativeToSiteRootUrl(url);
if (endsWith(url, "/"))
return url + subPath;
else
return url + "/" + subPath;
};
export function toSockjsUrl(url) {
return translateUrl(url, "http", "sockjs");
};
export function toWebsocketUrl(url) {
var ret = translateUrl(url, "ws", "websocket");
return ret;
};
LivedataTest.toSockjsUrl = toSockjsUrl;