feat(client,sdk): retrieve auth token from parent frame + better resize detection
arcad/edge/pipeline/head This commit looks good
Details
arcad/edge/pipeline/head This commit looks good
Details
This commit is contained in:
parent
f5283b86ed
commit
4db7576b12
|
@ -38,7 +38,7 @@ describe('Remote Procedure Call', function () {
|
|||
|
||||
|
||||
it('should call the add() method repetitively and keep count of the sent values', function () {
|
||||
this.timeout(10000);
|
||||
this.timeout(30000);
|
||||
|
||||
const values = [];
|
||||
for (let i = 0; i <= 1000; i++) {
|
||||
|
|
|
@ -3865,6 +3865,8 @@ var Edge = (() => {
|
|||
var import_sockjs_client = __toESM(require_entry());
|
||||
var EventTypeMessage = "message";
|
||||
var EdgeAuth = "edge-auth";
|
||||
var EdgeAuthTokenRequest = "edge_auth_token_request";
|
||||
var EdgeAuthTokenResponse = "edge_auth_token_reponse";
|
||||
var Client = class extends EventTarget {
|
||||
constructor(autoReconnect = true) {
|
||||
super();
|
||||
|
@ -3872,6 +3874,7 @@ var Edge = (() => {
|
|||
this._onConnectionClose = this._onConnectionClose.bind(this);
|
||||
this._onConnectionMessage = this._onConnectionMessage.bind(this);
|
||||
this._handleRPCResponse = this._handleRPCResponse.bind(this);
|
||||
this._handleEdgeAuthTokenRequest = this._handleEdgeAuthTokenRequest.bind(this);
|
||||
this._rpcID = 0;
|
||||
this._pendingRPC = {};
|
||||
this._queue = [];
|
||||
|
@ -3884,12 +3887,22 @@ var Edge = (() => {
|
|||
this.send = this.send.bind(this);
|
||||
this.upload = this.upload.bind(this);
|
||||
this.addEventListener(EventTypeMessage, this._handleRPCResponse);
|
||||
window.addEventListener("message", this._handleEdgeAuthTokenRequest);
|
||||
}
|
||||
connect(token = "") {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (token == "") {
|
||||
token = this._getAuthCookieToken();
|
||||
let getToken;
|
||||
if (token) {
|
||||
getToken = Promise.resolve(token);
|
||||
} else {
|
||||
getToken = this._retrieveToken();
|
||||
}
|
||||
return getToken.then((token2) => this._connect(token2));
|
||||
}
|
||||
disconnect() {
|
||||
this._cleanupConnection();
|
||||
}
|
||||
_connect(token) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`;
|
||||
this._log("opening connection to", url);
|
||||
const conn = new import_sockjs_client.default(url);
|
||||
|
@ -3920,15 +3933,64 @@ var Edge = (() => {
|
|||
conn.addEventListener("close", onError);
|
||||
});
|
||||
}
|
||||
disconnect() {
|
||||
this._cleanupConnection();
|
||||
_retrieveToken() {
|
||||
let token = this._getAuthCookieToken();
|
||||
if (token) {
|
||||
return Promise.resolve(token);
|
||||
}
|
||||
return this._getParentFrameToken();
|
||||
;
|
||||
}
|
||||
_getAuthCookieToken() {
|
||||
const cookie = document.cookie.split("; ").find((row) => row.startsWith(EdgeAuth));
|
||||
let token = "";
|
||||
if (cookie) {
|
||||
return cookie.split("=")[1];
|
||||
token = cookie.split("=")[1];
|
||||
}
|
||||
return "";
|
||||
return token;
|
||||
}
|
||||
_getParentFrameToken(timeout = 5e3) {
|
||||
if (!window.parent || window.parent === window) {
|
||||
return Promise.resolve("");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let timedOut = false;
|
||||
const timeoutId = setTimeout(() => {
|
||||
timedOut = true;
|
||||
reject(new Error("Edge auth token request timed out !"));
|
||||
}, timeout);
|
||||
const listener = (evt) => {
|
||||
const message = evt.data;
|
||||
if (!message || !message.type || !message.data) {
|
||||
return;
|
||||
}
|
||||
if (message.type !== EdgeAuthTokenResponse) {
|
||||
return;
|
||||
}
|
||||
window.parent.removeEventListener("message", listener);
|
||||
clearTimeout(timeoutId);
|
||||
if (timedOut)
|
||||
return;
|
||||
if (!message.data || !message.data.token) {
|
||||
reject("Unexpected auth token request response !");
|
||||
return;
|
||||
}
|
||||
resolve(message.data.token);
|
||||
};
|
||||
window.parent.addEventListener("message", listener);
|
||||
window.parent.postMessage({ type: EdgeAuthTokenRequest }, "*");
|
||||
});
|
||||
}
|
||||
_handleEdgeAuthTokenRequest(evt) {
|
||||
const message = evt.data;
|
||||
if (!message || !message.type || message.type !== EdgeAuthTokenRequest) {
|
||||
return;
|
||||
}
|
||||
if (!evt.source) {
|
||||
return;
|
||||
}
|
||||
const token = this._getAuthCookieToken();
|
||||
evt.source.postMessage({ type: EdgeAuthTokenResponse, data: { token } });
|
||||
}
|
||||
_onConnectionMessage(evt) {
|
||||
const rawMessage = JSON.parse(evt.data);
|
||||
|
@ -4113,6 +4175,9 @@ var Edge = (() => {
|
|||
}
|
||||
_handleWindowMessage(evt) {
|
||||
const message = evt.data;
|
||||
if (!message || !message.type || !message.data) {
|
||||
return;
|
||||
}
|
||||
const event = new CustomEvent(message.type, {
|
||||
cancelable: true,
|
||||
detail: message.data
|
||||
|
@ -4136,27 +4201,15 @@ var Edge = (() => {
|
|||
}
|
||||
_initResizeObserver() {
|
||||
const resizeObserver = new ResizeObserver(() => {
|
||||
const body2 = document.body, html = document.documentElement;
|
||||
const height = Math.max(
|
||||
body2.scrollHeight,
|
||||
body2.offsetHeight,
|
||||
html.clientHeight,
|
||||
html.scrollHeight,
|
||||
html.offsetHeight
|
||||
);
|
||||
const width = Math.max(
|
||||
body2.scrollWidth,
|
||||
body2.offsetWidth,
|
||||
html.clientWidth,
|
||||
html.scrollWidth,
|
||||
html.offsetWidth
|
||||
);
|
||||
const rect = document.documentElement.getBoundingClientRect();
|
||||
const height = rect.height;
|
||||
const width = rect.width;
|
||||
this.post({ type: "size_changed" /* SIZE_CHANGED */, data: { height, width } });
|
||||
});
|
||||
const body = document.body;
|
||||
if (!body)
|
||||
return;
|
||||
resizeObserver.observe(body);
|
||||
resizeObserver.observe(document.documentElement);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -5,6 +5,8 @@ import SockJS from 'sockjs-client';
|
|||
|
||||
const EventTypeMessage = "message";
|
||||
const EdgeAuth = "edge-auth"
|
||||
const EdgeAuthTokenRequest = "edge_auth_token_request"
|
||||
const EdgeAuthTokenResponse = "edge_auth_token_reponse"
|
||||
|
||||
export class Client extends EventTarget {
|
||||
|
||||
|
@ -23,6 +25,7 @@ export class Client extends EventTarget {
|
|||
this._onConnectionClose = this._onConnectionClose.bind(this);
|
||||
this._onConnectionMessage = this._onConnectionMessage.bind(this);
|
||||
this._handleRPCResponse = this._handleRPCResponse.bind(this);
|
||||
this._handleEdgeAuthTokenRequest = this._handleEdgeAuthTokenRequest.bind(this);
|
||||
|
||||
this._rpcID = 0;
|
||||
this._pendingRPC = {};
|
||||
|
@ -37,15 +40,29 @@ export class Client extends EventTarget {
|
|||
this.send = this.send.bind(this);
|
||||
this.upload = this.upload.bind(this);
|
||||
|
||||
|
||||
this.addEventListener(EventTypeMessage, this._handleRPCResponse);
|
||||
window.addEventListener('message', this._handleEdgeAuthTokenRequest);
|
||||
}
|
||||
|
||||
connect(token = "") {
|
||||
connect(token = ""): Promise<Client> {
|
||||
let getToken: Promise<string>
|
||||
|
||||
if (token) {
|
||||
getToken = Promise.resolve(token)
|
||||
} else {
|
||||
getToken = this._retrieveToken()
|
||||
}
|
||||
|
||||
return getToken.then(token => this._connect(token))
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this._cleanupConnection();
|
||||
}
|
||||
|
||||
_connect(token: string): Promise<Client> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (token == "") {
|
||||
token = this._getAuthCookieToken()
|
||||
}
|
||||
|
||||
const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`;
|
||||
this._log("opening connection to", url);
|
||||
const conn: any = new SockJS(url);
|
||||
|
@ -78,22 +95,84 @@ export class Client extends EventTarget {
|
|||
conn.addEventListener('open', onOpen);
|
||||
conn.addEventListener('error', onError);
|
||||
conn.addEventListener('close', onError);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this._cleanupConnection();
|
||||
_retrieveToken(): Promise<string> {
|
||||
let token = this._getAuthCookieToken();
|
||||
if (token) {
|
||||
return Promise.resolve(token);
|
||||
}
|
||||
|
||||
_getAuthCookieToken() {
|
||||
return this._getParentFrameToken();;
|
||||
}
|
||||
|
||||
_getAuthCookieToken(): string {
|
||||
const cookie = document.cookie.split("; ")
|
||||
.find((row) => row.startsWith(EdgeAuth));
|
||||
|
||||
let token = "";
|
||||
|
||||
if (cookie) {
|
||||
return cookie.split("=")[1];
|
||||
token = cookie.split("=")[1];
|
||||
}
|
||||
|
||||
return "";
|
||||
return token;
|
||||
}
|
||||
|
||||
_getParentFrameToken(timeout = 5000): Promise<string> {
|
||||
if (!window.parent || window.parent === window) {
|
||||
return Promise.resolve("");
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let timedOut = false;
|
||||
const timeoutId = setTimeout(() => {
|
||||
timedOut = true;
|
||||
reject(new Error("Edge auth token request timed out !"));
|
||||
}, timeout);
|
||||
|
||||
const listener = (evt) => {
|
||||
const message = evt.data;
|
||||
|
||||
if (!message || !message.type || !message.data) {
|
||||
return
|
||||
}
|
||||
|
||||
if (message.type !== EdgeAuthTokenResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.parent.removeEventListener('message', listener);
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (timedOut) return;
|
||||
|
||||
if (!message.data || !message.data.token) {
|
||||
reject("Unexpected auth token request response !");
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(message.data.token);
|
||||
}
|
||||
|
||||
window.parent.addEventListener('message', listener);
|
||||
window.parent.postMessage({ type: EdgeAuthTokenRequest }, '*');
|
||||
})
|
||||
}
|
||||
|
||||
_handleEdgeAuthTokenRequest(evt: MessageEvent) {
|
||||
const message = evt.data;
|
||||
if (!message || !message.type || message.type !== EdgeAuthTokenRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!evt.source) {
|
||||
return;
|
||||
}
|
||||
|
||||
const token = this._getAuthCookieToken();
|
||||
evt.source.postMessage({ type: EdgeAuthTokenResponse, data: { token }});
|
||||
}
|
||||
|
||||
_onConnectionMessage(evt) {
|
||||
|
@ -216,7 +295,7 @@ export class Client extends EventTarget {
|
|||
return this._conn !== null;
|
||||
}
|
||||
|
||||
upload(blob: string|Blob, metadata: any) {
|
||||
upload(blob: string | Blob, metadata: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const formData = new FormData();
|
||||
formData.set("file", blob);
|
||||
|
@ -224,7 +303,7 @@ export class Client extends EventTarget {
|
|||
if (metadata) {
|
||||
try {
|
||||
formData.set("metadata", JSON.stringify(metadata));
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
return reject(err);
|
||||
}
|
||||
}
|
||||
|
@ -239,7 +318,7 @@ export class Client extends EventTarget {
|
|||
let data;
|
||||
try {
|
||||
data = JSON.parse(xhr.responseText);
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,10 @@ export class CrossFrameMessenger extends EventTarget {
|
|||
|
||||
_handleWindowMessage(evt: MessageEvent) {
|
||||
const message = evt.data;
|
||||
if (!message || !message.type || !message.data) {
|
||||
return;
|
||||
}
|
||||
|
||||
const event = new CustomEvent(message.type, {
|
||||
cancelable: true,
|
||||
detail: message.data
|
||||
|
@ -67,14 +71,9 @@ export class CrossFrameMessenger extends EventTarget {
|
|||
|
||||
_initResizeObserver() {
|
||||
const resizeObserver = new ResizeObserver(() => {
|
||||
const body = document.body,
|
||||
html = document.documentElement;
|
||||
|
||||
const height = Math.max( body.scrollHeight, body.offsetHeight,
|
||||
html.clientHeight, html.scrollHeight, html.offsetHeight );
|
||||
|
||||
const width = Math.max( body.scrollWidth, body.offsetWidth,
|
||||
html.clientWidth, html.scrollWidth, html.offsetWidth );
|
||||
const rect = document.documentElement.getBoundingClientRect();
|
||||
const height = rect.height;
|
||||
const width = rect.width;
|
||||
|
||||
this.post({ type: CrossFrameMessageType.SIZE_CHANGED, data: { height, width }});
|
||||
});
|
||||
|
@ -83,6 +82,6 @@ export class CrossFrameMessenger extends EventTarget {
|
|||
|
||||
if (!body) return;
|
||||
|
||||
resizeObserver.observe(body);
|
||||
resizeObserver.observe(document.documentElement);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue