Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Apr 8, 2024
1 parent 4c162e2 commit f7af0a7
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 152 deletions.
1 change: 1 addition & 0 deletions apps/meteor/packages/accounts-linkedin/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Package.describe({
});

Package.onUse((api) => {
api.versionsFrom('2.5');
api.use('ecmascript');
api.use('accounts-base', ['client', 'server']);
api.imply('accounts-base', ['client', 'server']);
Expand Down
89 changes: 49 additions & 40 deletions apps/meteor/packages/autoupdate/autoupdate_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,37 @@
// The client version of the client code currently running in the
// browser.

import { ClientVersions } from './client_versions.js';
import { ClientVersions } from "./client_versions.js";

const clientArch = Meteor.isCordova ? 'web.cordova' : Meteor.isModern ? 'web.browser' : 'web.browser.legacy';
const clientArch = Meteor.isCordova ? "web.cordova" :
Meteor.isModern ? "web.browser" : "web.browser.legacy";

const autoupdateVersions = ((__meteor_runtime_config__.autoupdate || {}).versions || {})[clientArch] || {
version: 'unknown',
versionRefreshable: 'unknown',
versionNonRefreshable: 'unknown',
assets: [],
};
const autoupdateVersions =
((__meteor_runtime_config__.autoupdate || {}).versions || {})[clientArch] || {
version: "unknown",
versionRefreshable: "unknown",
versionNonRefreshable: "unknown",
assets: [],
};

export const Autoupdate = {};

// Stores acceptable client versions.
const clientVersions = (Autoupdate._clientVersions = // Used by a self-test and hot-module-replacement
new ClientVersions());
const clientVersions =
Autoupdate._clientVersions = // Used by a self-test and hot-module-replacement
new ClientVersions();

Meteor.connection.registerStoreClient('meteor_autoupdate_clientVersions', clientVersions.createStore());
Meteor.connection.registerStore(
"meteor_autoupdate_clientVersions",
clientVersions.createStore()
);

Autoupdate.newClientAvailable = function () {
return clientVersions.newClientAvailable(clientArch, ['versionRefreshable', 'versionNonRefreshable'], autoupdateVersions);
return clientVersions.newClientAvailable(
clientArch,
["versionRefreshable", "versionNonRefreshable"],
autoupdateVersions
);
};

// Set to true if the link.onload callback ever fires for any <link> node.
Expand All @@ -61,15 +71,15 @@ const retry = new Retry({
// server fixing code will result in a restart and reconnect, but
// potentially the subscription could have a transient error.
minCount: 0, // don't do any immediate retries
baseTimeout: 30 * 1000, // start with 30s
baseTimeout: 30*1000 // start with 30s
});

let failures = 0;

Autoupdate._retrySubscription = () => {
Meteor.subscribe('meteor_autoupdate_clientVersions', {
Meteor.subscribe("meteor_autoupdate_clientVersions", {
onError(error) {
Meteor._debug('autoupdate subscription failed', error);
Meteor._debug("autoupdate subscription failed", error);
failures++;
retry.retryLater(failures, function () {
// Just retry making the subscription, don't reload the whole
Expand Down Expand Up @@ -101,7 +111,8 @@ Autoupdate._retrySubscription = () => {
return;
}

if (doc.versionNonRefreshable !== autoupdateVersions.versionNonRefreshable) {
if (doc.versionNonRefreshable !==
autoupdateVersions.versionNonRefreshable) {
// Non-refreshable assets have changed, so we have to reload the
// whole page rather than just replacing <link> tags.
if (stop) stop();
Expand All @@ -110,13 +121,7 @@ Autoupdate._retrySubscription = () => {
// is provided by the ddp package that autoupdate depends on.

// Delay reload in 60 seconds
console.warn(
'Client version changed from',
autoupdateVersions.versionNonRefreshable,
'to',
doc.versionNonRefreshable,
`Page will reload in ${reloadDelayInSeconds} seconds`,
);
console.warn('Client version changed from', autoupdateVersions.versionNonRefreshable, 'to', doc.versionNonRefreshable, `Page will reload in ${reloadDelayInSeconds} seconds`);
setTimeout(() => {
Package.reload.Reload._reload();
}, reloadDelayInSeconds * 1000);
Expand All @@ -132,27 +137,30 @@ Autoupdate._retrySubscription = () => {
var newCss = doc.assets || [];
var oldLinks = [];

Array.prototype.forEach.call(document.getElementsByTagName('link'), function (link) {
if (link.className === '__meteor-css__') {
oldLinks.push(link);
Array.prototype.forEach.call(
document.getElementsByTagName('link'),
function (link) {
if (link.className === '__meteor-css__') {
oldLinks.push(link);
}
}
});
);

function waitUntilCssLoads(link, callback) {
var called;

link.onload = function () {
knownToSupportCssOnLoad = true;
if (!called) {
if (! called) {
called = true;
callback();
}
};

if (!knownToSupportCssOnLoad) {
if (! knownToSupportCssOnLoad) {
var id = Meteor.setInterval(function () {
if (link.sheet) {
if (!called) {
if (! called) {
called = true;
callback();
}
Expand All @@ -164,34 +172,35 @@ Autoupdate._retrySubscription = () => {

let newLinksLeftToLoad = newCss.length;
function removeOldLinks() {
if (oldLinks.length > 0 && --newLinksLeftToLoad < 1) {
oldLinks.splice(0).forEach((link) => {
if (oldLinks.length > 0 &&
--newLinksLeftToLoad < 1) {
oldLinks.splice(0).forEach(link => {
link.parentNode.removeChild(link);
});
}
}

if (newCss.length > 0) {
newCss.forEach((css) => {
const newLink = document.createElement('link');
newLink.setAttribute('rel', 'stylesheet');
newLink.setAttribute('type', 'text/css');
newLink.setAttribute('class', '__meteor-css__');
newLink.setAttribute('href', css.url);
newCss.forEach(css => {
const newLink = document.createElement("link");
newLink.setAttribute("rel", "stylesheet");
newLink.setAttribute("type", "text/css");
newLink.setAttribute("class", "__meteor-css__");
newLink.setAttribute("href", css.url);

waitUntilCssLoads(newLink, function () {
Meteor.setTimeout(removeOldLinks, 200);
});

const head = document.getElementsByTagName('head').item(0);
const head = document.getElementsByTagName("head").item(0);
head.appendChild(newLink);
});
} else {
removeOldLinks();
}
}
}
},
}
});
};

Expand Down
117 changes: 56 additions & 61 deletions apps/meteor/packages/autoupdate/autoupdate_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
// The ID of each document is the client architecture, and the fields of
// the document are the versions described above.

import { ClientVersions } from './client_versions.js';
import { ClientVersions } from "./client_versions.js";
var Future = Npm.require("fibers/future");

export const Autoupdate = (__meteor_runtime_config__.autoupdate = {
export const Autoupdate = __meteor_runtime_config__.autoupdate = {
// Map from client architectures (web.browser, web.browser.legacy,
// web.cordova) to version fields { version, versionRefreshable,
// versionNonRefreshable, refreshable } that will be stored in
// ClientVersions documents (whose IDs are client architectures). This
// data gets serialized into the boilerplate because it's stored in
// __meteor_runtime_config__.autoupdate.versions.
versions: {},
});
versions: {}
};

// Stores acceptable client versions.
const clientVersions = new ClientVersions();
Expand All @@ -52,45 +53,49 @@ Autoupdate.autoupdateVersionRefreshable = null;
Autoupdate.autoupdateVersionCordova = null;
Autoupdate.appId = __meteor_runtime_config__.appId = process.env.APP_ID;

var syncQueue = new Meteor._AsynchronousQueue();
var syncQueue = new Meteor._SynchronousQueue();

async function updateVersions(shouldReloadClientProgram) {
function updateVersions(shouldReloadClientProgram) {
// Step 1: load the current client program on the server
if (shouldReloadClientProgram) {
await WebAppInternals.reloadClientPrograms();
WebAppInternals.reloadClientPrograms();
}

const {
// If the AUTOUPDATE_VERSION environment variable is defined, it takes
// precedence, but Autoupdate.autoupdateVersion is still supported as
// a fallback. In most cases neither of these values will be defined.
AUTOUPDATE_VERSION = Autoupdate.autoupdateVersion,
AUTOUPDATE_VERSION = Autoupdate.autoupdateVersion
} = process.env;

// Step 2: update __meteor_runtime_config__.autoupdate.versions.
const clientArchs = Object.keys(WebApp.clientPrograms);
clientArchs.forEach((arch) => {
clientArchs.forEach(arch => {
Autoupdate.versions[arch] = {
version: AUTOUPDATE_VERSION || WebApp.calculateClientHash(arch),
versionRefreshable: AUTOUPDATE_VERSION || WebApp.calculateClientHashRefreshable(arch),
versionNonRefreshable: AUTOUPDATE_VERSION || WebApp.calculateClientHashNonRefreshable(arch),
versionReplaceable: AUTOUPDATE_VERSION || WebApp.calculateClientHashReplaceable(arch),
versionHmr: WebApp.clientPrograms[arch].hmrVersion,
version: AUTOUPDATE_VERSION ||
WebApp.calculateClientHash(arch),
versionRefreshable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashRefreshable(arch),
versionNonRefreshable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashNonRefreshable(arch),
versionReplaceable: AUTOUPDATE_VERSION ||
WebApp.calculateClientHashReplaceable(arch),
versionHmr: WebApp.clientPrograms[arch].hmrVersion
};
});

// Step 3: form the new client boilerplate which contains the updated
// assets and __meteor_runtime_config__.
if (shouldReloadClientProgram) {
await WebAppInternals.generateBoilerplate();
WebAppInternals.generateBoilerplate();
}

// Step 4: update the ClientVersions collection.
// We use `onListening` here because we need to use
// `WebApp.getRefreshableAssets`, which is only set after
// `WebApp.generateBoilerplate` is called by `main` in webapp.
WebApp.onListening(() => {
clientArchs.forEach((arch) => {
clientArchs.forEach(arch => {
const payload = {
...Autoupdate.versions[arch],
assets: WebApp.getRefreshableAssets(arch),
Expand All @@ -102,7 +107,7 @@ async function updateVersions(shouldReloadClientProgram) {
}

Meteor.publish(
'meteor_autoupdate_clientVersions',
"meteor_autoupdate_clientVersions",
function (appId) {
// `null` happens when a client doesn't have an appId and passes
// `undefined` to `Meteor.subscribe`. `undefined` is translated to
Expand All @@ -111,77 +116,67 @@ Meteor.publish(

// Don't notify clients using wrong appId such as mobile apps built with a
// different server but pointing at the same local url
if (Autoupdate.appId && appId && Autoupdate.appId !== appId) return [];
if (Autoupdate.appId && appId && Autoupdate.appId !== appId)
return [];

// Random value to delay the updates for 2-10 minutes
const randomInterval = Meteor.isProduction ? (Math.floor(Math.random() * 8) + 2) * 1000 * 60 : 0;

const stop = clientVersions.watch((version, isNew) => {
setTimeout(() => {
(isNew ? this.added : this.changed).call(this, 'meteor_autoupdate_clientVersions', version._id, version);
(isNew ? this.added : this.changed)
.call(this, "meteor_autoupdate_clientVersions", version._id, version)
}, randomInterval);
});

this.onStop(() => stop());
this.ready();
},
{ is_auto: true },
{is_auto: true}
);

Meteor.startup(async function () {
await updateVersions(false);
Meteor.startup(function () {
updateVersions(false);

// Force any connected clients that are still looking for these older
// document IDs to reload.
['version', 'version-refreshable', 'version-cordova'].forEach((_id) => {
["version",
"version-refreshable",
"version-cordova",
].forEach(_id => {
clientVersions.set(_id, {
version: 'outdated',
version: "outdated"
});
});
});

function enqueueVersionsRefresh() {
syncQueue.queueTask(async function () {
await updateVersions(true);
});
}

const setupListeners = () => {
// Listen for messages pertaining to the client-refresh topic.
import { onMessage } from 'meteor/inter-process-messaging';
onMessage('client-refresh', enqueueVersionsRefresh);

// Another way to tell the process to refresh: send SIGHUP signal
process.on(
'SIGHUP',
Meteor.bindEnvironment(function () {
enqueueVersionsRefresh();
}, 'handling SIGHUP signal for refresh'),
);
};
var fut = new Future();

if (Meteor._isFibersEnabled) {
var Future = Npm.require('fibers/future');
// We only want 'refresh' to trigger 'updateVersions' AFTER onListen,
// so we add a queued task that waits for onListen before 'refresh' can queue
// tasks. Note that the `onListening` callbacks do not fire until after
// Meteor.startup, so there is no concern that the 'updateVersions' calls from
// 'refresh' will overlap with the `updateVersions` call from Meteor.startup.

var fut = new Future();
syncQueue.queueTask(function () {
fut.wait();
});

// We only want 'refresh' to trigger 'updateVersions' AFTER onListen,
// so we add a queued task that waits for onListen before 'refresh' can queue
// tasks. Note that the `onListening` callbacks do not fire until after
// Meteor.startup, so there is no concern that the 'updateVersions' calls from
// 'refresh' will overlap with the `updateVersions` call from Meteor.startup.
WebApp.onListening(function () {
fut.return();
});

function enqueueVersionsRefresh() {
syncQueue.queueTask(function () {
fut.wait();
updateVersions(true);
});
}

WebApp.onListening(function () {
fut.return();
});
// Listen for messages pertaining to the client-refresh topic.
import { onMessage } from "meteor/inter-process-messaging";
onMessage("client-refresh", enqueueVersionsRefresh);

setupListeners();
} else {
WebApp.onListening(function () {
Promise.resolve(setupListeners());
});
}
// Another way to tell the process to refresh: send SIGHUP signal
process.on('SIGHUP', Meteor.bindEnvironment(function () {
enqueueVersionsRefresh();
}, "handling SIGHUP signal for refresh"));
Loading

0 comments on commit f7af0a7

Please sign in to comment.