From 871ca2943c34351457c4413bf7e36679fb8c9275 Mon Sep 17 00:00:00 2001 From: Saravanan Date: Thu, 26 Sep 2024 08:18:18 +0530 Subject: [PATCH 1/4] fix: add launch telemetry event for notebook (#1435) --- src/lib/index.js | 679 ++++++++++++++++++++-------------------- src/telemetry/events.ts | 1 + 2 files changed, 343 insertions(+), 337 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index 9d38b1374..657c7d383 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -5,30 +5,30 @@ const l = require("vscode"), L = require("python-bridge"), H = require("fs"), J = require("@nteract/messaging/lib/wire-protocol"); -function Y(o) { +function Y(n) { const e = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } }); - if (o) { - for (const t in o) + if (n) { + for (const t in n) if (t !== "default") { - const r = Object.getOwnPropertyDescriptor(o, t); + const r = Object.getOwnPropertyDescriptor(n, t); Object.defineProperty( e, t, - r.get ? r : { enumerable: !0, get: () => o[t] }, + r.get ? r : { enumerable: !0, get: () => n[t] }, ); } } - return (e.default = o), Object.freeze(e); + return (e.default = n), Object.freeze(e); } const $ = Y(J), - G = (o) => ("getCells" in o ? o.getCells() : o.cells), - Q = (o) => (o instanceof l.NotebookCellData ? o.value : o.document.getText()), - X = (o) => - o instanceof l.NotebookCellData ? o.languageId : o.document.languageId, - A = (o, e, t) => { - var n; + G = (n) => ("getCells" in n ? n.getCells() : n.cells), + Q = (n) => (n instanceof l.NotebookCellData ? n.value : n.document.getText()), + X = (n) => + n instanceof l.NotebookCellData ? n.languageId : n.document.languageId, + A = (n, e, t) => { + var o; const r = []; - for (const i of G(o)) + for (const i of G(n)) r.push({ cell_type: i.kind, source: Q(i).split(/\r?\n/g), @@ -39,10 +39,10 @@ const $ = Y(J), return { cells: r, metadata: { - ...o.metadata, + ...n.metadata, name: e, createdAt: - ((n = o.metadata) == null ? void 0 : n.createdAt) || + ((o = n.metadata) == null ? void 0 : o.createdAt) || new Date().toISOString(), updatedAt: new Date().toISOString(), }, @@ -50,22 +50,22 @@ const $ = Y(J), }, _ = () => Math.random().toString(36).substr(2, 9); function Z() { - const o = new Date(), - e = o.toLocaleDateString("en-GB").replace(/\//g, "-"), - t = o.toLocaleTimeString("en-GB", { hour12: !1 }).replace(/:/g, "-"); + const n = new Date(), + e = n.toLocaleDateString("en-GB").replace(/\//g, "-"), + t = n.toLocaleTimeString("en-GB", { hour12: !1 }).replace(/:/g, "-"); return `${e}-${t}`; } -var ee = function (o, e, t, r) { - var n = arguments.length, +var ee = function (n, e, t, r) { + var o = arguments.length, i = - n < 3 ? e : r === null ? (r = Object.getOwnPropertyDescriptor(e, t)) : r, + o < 3 ? e : r === null ? (r = Object.getOwnPropertyDescriptor(e, t)) : r, s; if (typeof Reflect == "object" && typeof Reflect.decorate == "function") - i = Reflect.decorate(o, e, t, r); + i = Reflect.decorate(n, e, t, r); else - for (var a = o.length - 1; a >= 0; a--) - (s = o[a]) && (i = (n < 3 ? s(i) : n > 3 ? s(e, t, i) : s(e, t)) || i); - return n > 3 && i && Object.defineProperty(e, t, i), i; + for (var a = n.length - 1; a >= 0; a--) + (s = n[a]) && (i = (o < 3 ? s(i) : o > 3 ? s(e, t, i) : s(e, t)) || i); + return o > 3 && i && Object.defineProperty(e, t, i), i; }; let E = class { dispose() { @@ -73,13 +73,13 @@ let E = class { } async deserializeNotebook(e, t) { const r = new TextDecoder().decode(e); - let n; + let o; try { - n = JSON.parse(r); + o = JSON.parse(r); } catch { - n = { cells: [] }; + o = { cells: [] }; } - const i = n.cells.map((a) => { + const i = o.cells.map((a) => { var d; const c = new l.NotebookCellData( a.cell_type, @@ -92,7 +92,7 @@ let E = class { return (c.metadata = a.metadata), (c.outputs = a.outputs), c; }), s = new l.NotebookData(i); - return (s.metadata = n.metadata), s; + return (s.metadata = o.metadata), s; } async serializeNotebook(e, t) { const r = A(e); @@ -101,10 +101,10 @@ let E = class { }; E = ee([u.provideSingleton(E)], E); var g; -(function (o) { - (o.error = "application/vnd.code.notebook.error"), - (o.stderr = "application/vnd.code.notebook.stderr"), - (o.stdout = "application/vnd.code.notebook.stdout"); +(function (n) { + (n.error = "application/vnd.code.notebook.error"), + (n.stderr = "application/vnd.code.notebook.stderr"), + (n.stdout = "application/vnd.code.notebook.stdout"); })(g || (g = {})); const te = ["text/plain", "text/markdown", g.stderr, g.stdout], F = [ @@ -129,102 +129,102 @@ k.set("error", de); k.set("execute_result", S); k.set("stream", ue); k.set("update_display_data", S); -function M(o) { - const e = k.get(o.output_type); +function M(n) { + const e = k.get(n.output_type); let t; return ( e - ? (t = e(o)) + ? (t = e(n)) : (console.warn( - `Unable to translate cell from ${o.output_type} to NotebookCellData for VS Code.`, + `Unable to translate cell from ${n.output_type} to NotebookCellData for VS Code.`, ), - (t = S(o))), + (t = S(n))), t ); } -function W(o) { - const e = { outputType: o.output_type }; - switch ((o.transient && (e.transient = o.transient), o.output_type)) { +function W(n) { + const e = { outputType: n.output_type }; + switch ((n.transient && (e.transient = n.transient), n.output_type)) { case "display_data": case "execute_result": case "update_display_data": { - (e.executionCount = o.execution_count), - (e.metadata = o.metadata ? JSON.parse(JSON.stringify(o.metadata)) : {}); + (e.executionCount = n.execution_count), + (e.metadata = n.metadata ? JSON.parse(JSON.stringify(n.metadata)) : {}); break; } } return e; } -function S(o) { - const e = W(o); - ("image/svg+xml" in o.data || "image/png" in o.data) && +function S(n) { + const e = W(n); + ("image/svg+xml" in n.data || "image/png" in n.data) && (e.__displayOpenPlotIcon = !0); const t = []; - if (o.data) for (const r in o.data) t.push(oe(r, o.data[r])); + if (n.data) for (const r in n.data) t.push(ne(r, n.data[r])); return new l.NotebookCellOutput(re(t), e); } -function re(o) { - return o.sort((e, t) => { +function re(n) { + return n.sort((e, t) => { const r = (s, a) => ( s.endsWith(".*") && (s = s.substr(0, s.indexOf(".*"))), a.startsWith(s) ); - let n = F.findIndex((s) => r(s, e.mime)), + let o = F.findIndex((s) => r(s, e.mime)), i = F.findIndex((s) => r(s, t.mime)); return ( - B(e) && (n = -1), + B(e) && (o = -1), B(t) && (i = -1), - (n = n === -1 ? 100 : n), + (o = o === -1 ? 100 : o), (i = i === -1 ? 100 : i), - n - i + o - i ); }); } -function B(o) { - if (o.mime.startsWith("application/vnd.")) +function B(n) { + if (n.mime.startsWith("application/vnd.")) try { - return new TextDecoder().decode(o.data).length === 0; + return new TextDecoder().decode(n.data).length === 0; } catch {} return !1; } -function oe(o, e) { - if (!e) return l.NotebookCellOutputItem.text("", o); +function ne(n, e) { + if (!e) return l.NotebookCellOutputItem.text("", n); try { if ( - (o.startsWith("text/") || te.includes(o)) && + (n.startsWith("text/") || te.includes(n)) && (Array.isArray(e) || typeof e == "string") ) { const t = Array.isArray(e) ? D(e) : e; - return l.NotebookCellOutputItem.text(t, o); + return l.NotebookCellOutputItem.text(t, n); } else - return o.startsWith("image/") && + return n.startsWith("image/") && typeof e == "string" && - o !== "image/svg+xml" - ? new l.NotebookCellOutputItem(ne(e), o) + n !== "image/svg+xml" + ? new l.NotebookCellOutputItem(oe(e), n) : typeof e == "object" && e !== null && !Array.isArray(e) - ? l.NotebookCellOutputItem.text(JSON.stringify(e), o) + ? l.NotebookCellOutputItem.text(JSON.stringify(e), n) : ((e = Array.isArray(e) ? D(e) : e), - l.NotebookCellOutputItem.text(e, o)); + l.NotebookCellOutputItem.text(e, n)); } catch (t) { return ( console.error( - `Failed to convert ${o} output to a buffer ${typeof e}, ${e}`, + `Failed to convert ${n} output to a buffer ${typeof e}, ${e}`, t, ), l.NotebookCellOutputItem.text("") ); } } -function ne(o) { +function oe(n) { return typeof Buffer < "u" && typeof Buffer.from == "function" - ? Buffer.from(o, "base64") - : Uint8Array.from(atob(o), (e) => e.charCodeAt(0)); + ? Buffer.from(n, "base64") + : Uint8Array.from(atob(n), (e) => e.charCodeAt(0)); } -function D(o) { - if (Array.isArray(o)) { +function D(n) { + if (Array.isArray(n)) { let e = ""; - for (let t = 0; t < o.length; t += 1) { - const r = o[t]; - t < o.length - 1 && + for (let t = 0; t < n.length; t += 1) { + const r = n[t]; + t < n.length - 1 && !r.endsWith(` `) ? (e = e.concat(`${r} @@ -233,103 +233,103 @@ function D(o) { } return e; } - return o.toString(); + return n.toString(); } -function ie(o) { - let e = o; - do (o = e), (e = o.replace(/[^\n]\x08/gm, "")); - while (e.length < o.length); - return o; +function ie(n) { + let e = n; + do (n = e), (e = n.replace(/[^\n]\x08/gm, "")); + while (e.length < n.length); + return n; } -function ae(o) { +function ae(n) { for ( - o = o.replace( + n = n.replace( /\r+\n/gm, ` `, ); - o.search(/\r[^$]/g) > -1; + n.search(/\r[^$]/g) > -1; ) { - const e = o.match(/^(.*)\r+/m)[1]; - let t = o.match(/\r+(.*)$/m)[1]; + const e = n.match(/^(.*)\r+/m)[1]; + let t = n.match(/\r+(.*)$/m)[1]; (t = t + e.slice(t.length, e.length)), - (o = o.replace(/\r+.*$/m, "\r").replace(/^.*\r/m, t)); + (n = n.replace(/\r+.*$/m, "\r").replace(/^.*\r/m, t)); } - return o; + return n; } -function se(o) { - return ae(ie(o)); +function se(n) { + return ae(ie(n)); } -function N(o) { - if (o.parent_header && "msg_id" in o.parent_header) - return o.parent_header.msg_id; +function N(n) { + if (n.parent_header && "msg_id" in n.parent_header) + return n.parent_header.msg_id; } -function le(o) { - if (o.hasOwnProperty("text/html")) { - const e = o["text/html"]; +function le(n) { + if (n.hasOwnProperty("text/html")) { + const e = n["text/html"]; typeof e == "string" && e.includes('