@lvce-editor/activity-bar-worker 7.0.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1711 +1,1715 @@
1
- const normalizeLine = line => {
2
- if (line.startsWith('Error: ')) {
3
- return line.slice('Error: '.length);
4
- }
5
- if (line.startsWith('VError: ')) {
6
- return line.slice('VError: '.length);
7
- }
8
- return line;
9
- };
10
- const getCombinedMessage = (error, message) => {
11
- const stringifiedError = normalizeLine(`${error}`);
12
- if (message) {
13
- return `${message}: ${stringifiedError}`;
14
- }
15
- return stringifiedError;
16
- };
17
- const NewLine$2 = '\n';
18
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
- return string.indexOf(NewLine$2, startIndex);
20
- };
21
- const mergeStacks = (parent, child) => {
22
- if (!child) {
23
- return parent;
24
- }
25
- const parentNewLineIndex = getNewLineIndex$1(parent);
26
- const childNewLineIndex = getNewLineIndex$1(child);
27
- if (childNewLineIndex === -1) {
28
- return parent;
29
- }
30
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
31
- const childRest = child.slice(childNewLineIndex);
32
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
33
- if (parentFirstLine.includes(childFirstLine)) {
34
- return parentFirstLine + childRest;
35
- }
36
- return child;
1
+ const toCommandId = key => {
2
+ const dotIndex = key.indexOf('.');
3
+ return key.slice(dotIndex + 1);
37
4
  };
38
- class VError extends Error {
39
- constructor(error, message) {
40
- const combinedMessage = getCombinedMessage(error, message);
41
- super(combinedMessage);
42
- this.name = 'VError';
43
- if (error instanceof Error) {
44
- this.stack = mergeStacks(this.stack, error.stack);
45
- }
46
- if (error.codeFrame) {
47
- // @ts-ignore
48
- this.codeFrame = error.codeFrame;
49
- }
50
- if (error.code) {
51
- // @ts-ignore
52
- this.code = error.code;
53
- }
54
- }
55
- }
56
-
57
- class AssertionError extends Error {
58
- constructor(message) {
59
- super(message);
60
- this.name = 'AssertionError';
61
- }
62
- }
63
- const Object$1 = 1;
64
- const Number$1 = 2;
65
- const Array$1 = 3;
66
- const String = 4;
67
- const Boolean$1 = 5;
68
- const Function = 6;
69
- const Null = 7;
70
- const Unknown = 8;
71
- const getType = value => {
72
- switch (typeof value) {
73
- case 'number':
74
- return Number$1;
75
- case 'function':
76
- return Function;
77
- case 'string':
78
- return String;
79
- case 'object':
80
- if (value === null) {
81
- return Null;
5
+ const create$a = () => {
6
+ const states = Object.create(null);
7
+ const commandMapRef = {};
8
+ return {
9
+ clear() {
10
+ for (const key of Object.keys(states)) {
11
+ delete states[key];
82
12
  }
83
- if (Array.isArray(value)) {
84
- return Array$1;
13
+ },
14
+ diff(uid, modules, numbers) {
15
+ const {
16
+ newState,
17
+ oldState
18
+ } = states[uid];
19
+ const diffResult = [];
20
+ for (let i = 0; i < modules.length; i++) {
21
+ const fn = modules[i];
22
+ if (!fn(oldState, newState)) {
23
+ diffResult.push(numbers[i]);
24
+ }
85
25
  }
86
- return Object$1;
87
- case 'boolean':
88
- return Boolean$1;
89
- default:
90
- return Unknown;
91
- }
26
+ return diffResult;
27
+ },
28
+ dispose(uid) {
29
+ delete states[uid];
30
+ },
31
+ get(uid) {
32
+ return states[uid];
33
+ },
34
+ getCommandIds() {
35
+ const keys = Object.keys(commandMapRef);
36
+ const ids = keys.map(toCommandId);
37
+ return ids;
38
+ },
39
+ getKeys() {
40
+ return Object.keys(states).map(key => {
41
+ return Number.parseFloat(key);
42
+ });
43
+ },
44
+ registerCommands(commandMap) {
45
+ Object.assign(commandMapRef, commandMap);
46
+ },
47
+ set(uid, oldState, newState) {
48
+ states[uid] = {
49
+ newState,
50
+ oldState
51
+ };
52
+ },
53
+ wrapCommand(fn) {
54
+ const wrapped = async (uid, ...args) => {
55
+ const {
56
+ newState,
57
+ oldState
58
+ } = states[uid];
59
+ const newerState = await fn(newState, ...args);
60
+ if (oldState === newerState || newState === newerState) {
61
+ return;
62
+ }
63
+ const latestOld = states[uid];
64
+ const latestNew = {
65
+ ...latestOld.newState,
66
+ ...newerState
67
+ };
68
+ states[uid] = {
69
+ newState: latestNew,
70
+ oldState: latestOld.oldState
71
+ };
72
+ };
73
+ return wrapped;
74
+ },
75
+ wrapGetter(fn) {
76
+ const wrapped = (uid, ...args) => {
77
+ const {
78
+ newState
79
+ } = states[uid];
80
+ return fn(newState, ...args);
81
+ };
82
+ return wrapped;
83
+ },
84
+ wrapLoadContent(fn) {
85
+ const wrapped = async (uid, ...args) => {
86
+ const {
87
+ newState,
88
+ oldState
89
+ } = states[uid];
90
+ const result = await fn(newState, ...args);
91
+ const {
92
+ error,
93
+ state
94
+ } = result;
95
+ if (oldState === state || newState === state) {
96
+ return {
97
+ error
98
+ };
99
+ }
100
+ const latestOld = states[uid];
101
+ const latestNew = {
102
+ ...latestOld.newState,
103
+ ...state
104
+ };
105
+ states[uid] = {
106
+ newState: latestNew,
107
+ oldState: latestOld.oldState
108
+ };
109
+ return {
110
+ error
111
+ };
112
+ };
113
+ return wrapped;
114
+ }
115
+ };
92
116
  };
93
- const number = value => {
94
- const type = getType(value);
95
- if (type !== Number$1) {
96
- throw new AssertionError('expected value to be of type number');
97
- }
117
+ const terminate = () => {
118
+ globalThis.close();
98
119
  };
99
120
 
100
- const isMessagePort = value => {
101
- return value && value instanceof MessagePort;
102
- };
103
- const isMessagePortMain = value => {
104
- return value && value.constructor && value.constructor.name === 'MessagePortMain';
121
+ const {
122
+ diff,
123
+ get: get$2,
124
+ getCommandIds,
125
+ registerCommands,
126
+ set: set$4,
127
+ wrapCommand,
128
+ wrapGetter
129
+ } = create$a();
130
+
131
+ // TODO parentUid might ot be needed
132
+ const create$9 = (id, uri, x, y, width, height, args, parentUid, platform = 0) => {
133
+ const state = {
134
+ accountEnabled: true,
135
+ activityBarItems: [],
136
+ currentViewletId: '',
137
+ filteredItems: [],
138
+ focus: 0,
139
+ focused: false,
140
+ focusedIndex: -1,
141
+ height,
142
+ initial: true,
143
+ itemHeight: 48,
144
+ numberOfVisibleItems: 0,
145
+ platform,
146
+ scrollBarHeight: 0,
147
+ selectedIndex: -1,
148
+ sideBarLocation: 0,
149
+ sideBarVisible: false,
150
+ uid: id,
151
+ updateProgress: 0,
152
+ updateState: '',
153
+ userLoginState: 'logged out',
154
+ width,
155
+ x,
156
+ y
157
+ };
158
+ set$4(id, state, state);
159
+ return state;
105
160
  };
106
- const isOffscreenCanvas = value => {
107
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
161
+
162
+ const isEqual$2 = (oldState, newState) => {
163
+ return oldState.itemHeight === newState.itemHeight;
108
164
  };
109
- const isInstanceOf = (value, constructorName) => {
110
- return value?.constructor?.name === constructorName;
165
+
166
+ const isEqual$1 = (oldState, newState) => {
167
+ return oldState.focused === newState.focused && oldState.focus === newState.focus;
111
168
  };
112
- const isSocket = value => {
113
- return isInstanceOf(value, 'Socket');
114
- };
115
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
116
- const isTransferrable = value => {
117
- for (const fn of transferrables) {
118
- if (fn(value)) {
119
- return true;
120
- }
121
- }
122
- return false;
123
- };
124
- const walkValue = (value, transferrables, isTransferrable) => {
125
- if (!value) {
126
- return;
127
- }
128
- if (isTransferrable(value)) {
129
- transferrables.push(value);
130
- return;
131
- }
132
- if (Array.isArray(value)) {
133
- for (const item of value) {
134
- walkValue(item, transferrables, isTransferrable);
135
- }
136
- return;
137
- }
138
- if (typeof value === 'object') {
139
- for (const property of Object.values(value)) {
140
- walkValue(property, transferrables, isTransferrable);
141
- }
142
- return;
143
- }
144
- };
145
- const getTransferrables = value => {
146
- const transferrables = [];
147
- walkValue(value, transferrables, isTransferrable);
148
- return transferrables;
149
- };
150
- const attachEvents = that => {
151
- const handleMessage = (...args) => {
152
- const data = that.getData(...args);
153
- that.dispatchEvent(new MessageEvent('message', {
154
- data
155
- }));
156
- };
157
- that.onMessage(handleMessage);
158
- const handleClose = event => {
159
- that.dispatchEvent(new Event('close'));
160
- };
161
- that.onClose(handleClose);
162
- };
163
- class Ipc extends EventTarget {
164
- constructor(rawIpc) {
165
- super();
166
- this._rawIpc = rawIpc;
167
- attachEvents(this);
168
- }
169
- }
170
- const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
171
- const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
172
- const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
173
- const NewLine$1 = '\n';
174
- const joinLines$1 = lines => {
175
- return lines.join(NewLine$1);
169
+
170
+ const isEqual = (oldState, newState) => {
171
+ return oldState.activityBarItems === newState.activityBarItems && oldState.filteredItems === newState.filteredItems && oldState.focusedIndex === newState.focusedIndex && oldState.sideBarLocation === newState.sideBarLocation && oldState.sideBarVisible === newState.sideBarVisible && oldState.updateProgress === newState.updateProgress && oldState.updateState === newState.updateState;
176
172
  };
177
- const RE_AT = /^\s+at/;
178
- const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
179
- const isNormalStackLine = line => {
180
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
173
+
174
+ const RenderItems = 4;
175
+ const RenderFocus = 6;
176
+ const RenderFocusContext = 7;
177
+ const RenderCss = 11;
178
+ const RenderIncremental = 12;
179
+
180
+ const modules = [isEqual, isEqual$1, isEqual$1, isEqual$2];
181
+ const numbers = [RenderIncremental, RenderFocus, RenderFocusContext, RenderCss];
182
+
183
+ const diff2 = uid => {
184
+ return diff(uid, modules, numbers);
181
185
  };
182
- const getDetails = lines => {
183
- const index = lines.findIndex(isNormalStackLine);
184
- if (index === -1) {
185
- return {
186
- actualMessage: joinLines$1(lines),
187
- rest: []
188
- };
189
- }
190
- let lastIndex = index - 1;
191
- while (++lastIndex < lines.length) {
192
- if (!isNormalStackLine(lines[lastIndex])) {
193
- break;
194
- }
186
+
187
+ const List = 1;
188
+
189
+ const focus = state => {
190
+ const {
191
+ focus
192
+ } = state;
193
+ if (focus) {
194
+ return state;
195
195
  }
196
196
  return {
197
- actualMessage: lines[index - 1],
198
- rest: lines.slice(index, lastIndex)
197
+ ...state,
198
+ focus: List
199
199
  };
200
200
  };
201
- const splitLines$1 = lines => {
202
- return lines.split(NewLine$1);
203
- };
204
- const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
205
- const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
206
- const isMessageCodeBlockStartIndex = line => {
207
- return RE_MESSAGE_CODE_BLOCK_START.test(line);
208
- };
209
- const isMessageCodeBlockEndIndex = line => {
210
- return RE_MESSAGE_CODE_BLOCK_END.test(line);
211
- };
212
- const getMessageCodeBlock = stderr => {
213
- const lines = splitLines$1(stderr);
214
- const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
215
- const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
216
- const relevantLines = lines.slice(startIndex, endIndex);
217
- const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
218
- return relevantMessage;
219
- };
220
- const isModuleNotFoundMessage = line => {
221
- return line.includes('[ERR_MODULE_NOT_FOUND]');
222
- };
223
- const getModuleNotFoundError = stderr => {
224
- const lines = splitLines$1(stderr);
225
- const messageIndex = lines.findIndex(isModuleNotFoundMessage);
226
- const message = lines[messageIndex];
201
+
202
+ const focusIndex = (state, index) => {
227
203
  return {
228
- code: ERR_MODULE_NOT_FOUND,
229
- message
204
+ ...state,
205
+ focused: true,
206
+ focusedIndex: index
230
207
  };
231
208
  };
232
- const isModuleNotFoundError = stderr => {
233
- if (!stderr) {
234
- return false;
235
- }
236
- return stderr.includes('ERR_MODULE_NOT_FOUND');
237
- };
238
- const isModulesSyntaxError = stderr => {
239
- if (!stderr) {
240
- return false;
241
- }
242
- return stderr.includes('SyntaxError: Cannot use import statement outside a module');
243
- };
244
- const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
245
- const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
246
- const isUnhelpfulNativeModuleError = stderr => {
247
- return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
209
+
210
+ const focusFirst = state => {
211
+ return focusIndex(state, -1);
248
212
  };
249
- const getNativeModuleErrorMessage = stderr => {
250
- const message = getMessageCodeBlock(stderr);
251
- return {
252
- code: E_INCOMPATIBLE_NATIVE_MODULE,
253
- message: `Incompatible native node module: ${message}`
254
- };
213
+
214
+ const focusLast = state => {
215
+ return focusIndex(state, -1);
255
216
  };
256
- const getModuleSyntaxError = () => {
257
- return {
258
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
259
- message: `ES Modules are not supported in electron`
260
- };
217
+
218
+ const focusNext = state => {
219
+ return focusIndex(state, -1);
261
220
  };
262
- const getHelpfulChildProcessError = (stdout, stderr) => {
263
- if (isUnhelpfulNativeModuleError(stderr)) {
264
- return getNativeModuleErrorMessage(stderr);
265
- }
266
- if (isModulesSyntaxError(stderr)) {
267
- return getModuleSyntaxError();
268
- }
269
- if (isModuleNotFoundError(stderr)) {
270
- return getModuleNotFoundError(stderr);
271
- }
272
- const lines = splitLines$1(stderr);
221
+
222
+ const focusNone = state => {
273
223
  const {
274
- actualMessage,
275
- rest
276
- } = getDetails(lines);
277
- return {
278
- code: '',
279
- message: actualMessage,
280
- stack: rest
281
- };
282
- };
283
- class IpcError extends VError {
284
- // @ts-ignore
285
- constructor(betterMessage, stdout = '', stderr = '') {
286
- if (stdout || stderr) {
287
- // @ts-ignore
288
- const {
289
- code,
290
- message,
291
- stack
292
- } = getHelpfulChildProcessError(stdout, stderr);
293
- const cause = new Error(message);
294
- // @ts-ignore
295
- cause.code = code;
296
- cause.stack = stack;
297
- super(cause, betterMessage);
298
- } else {
299
- super(betterMessage);
300
- }
301
- // @ts-ignore
302
- this.name = 'IpcError';
303
- // @ts-ignore
304
- this.stdout = stdout;
305
- // @ts-ignore
306
- this.stderr = stderr;
307
- }
308
- }
309
- const readyMessage = 'ready';
310
- const getData$2 = event => {
311
- return event.data;
312
- };
313
- const listen$7 = () => {
314
- // @ts-ignore
315
- if (typeof WorkerGlobalScope === 'undefined') {
316
- throw new TypeError('module is not in web worker scope');
224
+ focusedIndex
225
+ } = state;
226
+ if (focusedIndex === -1) {
227
+ return state;
317
228
  }
318
- return globalThis;
319
- };
320
- const signal$8 = global => {
321
- global.postMessage(readyMessage);
229
+ return focusIndex(state, -1);
322
230
  };
323
- class IpcChildWithModuleWorker extends Ipc {
324
- getData(event) {
325
- return getData$2(event);
326
- }
327
- send(message) {
328
- // @ts-ignore
329
- this._rawIpc.postMessage(message);
330
- }
331
- sendAndTransfer(message) {
332
- const transfer = getTransferrables(message);
333
- // @ts-ignore
334
- this._rawIpc.postMessage(message, transfer);
335
- }
336
- dispose() {
337
- // ignore
338
- }
339
- onClose(callback) {
340
- // ignore
341
- }
342
- onMessage(callback) {
343
- this._rawIpc.addEventListener('message', callback);
344
- }
345
- }
346
- const wrap$f = global => {
347
- return new IpcChildWithModuleWorker(global);
231
+
232
+ const Button$2 = 'button';
233
+ const None$1 = 'none';
234
+ const Tab$1 = 'tab';
235
+ const ToolBar = 'toolbar';
236
+
237
+ const Div = 4;
238
+ const Text = 12;
239
+ const Reference = 100;
240
+
241
+ const Button$1 = 'event.button';
242
+ const ClientX = 'event.clientX';
243
+ const ClientY = 'event.clientY';
244
+
245
+ const Enter = 3;
246
+ const Space = 9;
247
+ const PageUp = 10;
248
+ const PageDown = 11;
249
+ const End = 255;
250
+ const Home = 12;
251
+ const UpArrow = 14;
252
+ const DownArrow = 16;
253
+
254
+ const ActivityBar$3 = 1;
255
+ const Settings$1 = 12;
256
+ const ActivityBarAdditionalViews = 17;
257
+
258
+ const Separator = 1;
259
+ const None = 0;
260
+ const Checked = 2;
261
+ const Unchecked = 3;
262
+
263
+ const LeftClick = 0;
264
+
265
+ const Web = 1;
266
+
267
+ const ExtensionManagementWorker = 9006;
268
+ const RendererWorker = 1;
269
+
270
+ const Left = 1;
271
+ const Right = 2;
272
+
273
+ const SetCss = 'Viewlet.setCss';
274
+ const SetDom2 = 'Viewlet.setDom2';
275
+ const SetFocusContext = 'Viewlet.setFocusContext';
276
+ const SetPatches = 'Viewlet.setPatches';
277
+
278
+ const FocusActivityBar = 5;
279
+ const FocusExplorer = 13;
280
+
281
+ const getKeyBindings = () => {
282
+ return [{
283
+ command: 'ActivityBar.focusNext',
284
+ key: DownArrow,
285
+ when: FocusActivityBar
286
+ }, {
287
+ command: 'ActivityBar.focusPrevious',
288
+ key: UpArrow,
289
+ when: FocusActivityBar
290
+ }, {
291
+ command: 'ActivityBar.focusFirst',
292
+ key: Home,
293
+ when: FocusActivityBar
294
+ }, {
295
+ command: 'ActivityBar.focusFirst',
296
+ key: PageUp,
297
+ when: FocusActivityBar
298
+ }, {
299
+ command: 'ActivityBar.focusLast',
300
+ key: PageDown,
301
+ when: FocusActivityBar
302
+ }, {
303
+ command: 'ActivityBar.focusLast',
304
+ key: End,
305
+ when: FocusActivityBar
306
+ }, {
307
+ command: 'ActivityBar.selectCurrent',
308
+ key: Space,
309
+ when: FocusActivityBar
310
+ }, {
311
+ command: 'ActivityBar.selectCurrent',
312
+ key: Enter,
313
+ when: FocusActivityBar
314
+ }];
348
315
  };
349
- const waitForFirstMessage = async port => {
316
+
317
+ const getMenuEntriesAccountLoggedIn = state => {
350
318
  const {
351
- promise,
352
- resolve
353
- } = Promise.withResolvers();
354
- port.addEventListener('message', resolve, {
355
- once: true
356
- });
357
- const event = await promise;
358
- // @ts-ignore
359
- return event.data;
319
+ userLoginState
320
+ } = state;
321
+ const signOutLabel = userLoginState === 'logging out' ? 'Signing Out...' : 'Sign Out';
322
+ return [{
323
+ command: 'ActivityBar.handleClickSignOut',
324
+ flags: None,
325
+ id: 'signOut',
326
+ label: signOutLabel
327
+ }];
360
328
  };
361
- const listen$6 = async () => {
362
- const parentIpcRaw = listen$7();
363
- signal$8(parentIpcRaw);
364
- const parentIpc = wrap$f(parentIpcRaw);
365
- const firstMessage = await waitForFirstMessage(parentIpc);
366
- if (firstMessage.method !== 'initialize') {
367
- throw new IpcError('unexpected first message');
368
- }
369
- const type = firstMessage.params[0];
370
- if (type === 'message-port') {
371
- parentIpc.send({
372
- id: firstMessage.id,
373
- jsonrpc: '2.0',
374
- result: null
375
- });
376
- parentIpc.dispose();
377
- const port = firstMessage.params[1];
378
- return port;
379
- }
380
- return globalThis;
329
+ const getMenuEntriesAccountLoggedOut = state => {
330
+ const {
331
+ userLoginState
332
+ } = state;
333
+ const signInLabel = userLoginState === 'logging in' ? 'Signing In...' : 'Sign In';
334
+ return [{
335
+ command: 'ActivityBar.handleClickSignIn',
336
+ flags: None,
337
+ id: 'signIn',
338
+ label: signInLabel
339
+ }];
381
340
  };
382
- class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
383
- getData(event) {
384
- return getData$2(event);
385
- }
386
- send(message) {
387
- this._rawIpc.postMessage(message);
388
- }
389
- sendAndTransfer(message) {
390
- const transfer = getTransferrables(message);
391
- this._rawIpc.postMessage(message, transfer);
392
- }
393
- dispose() {
394
- if (this._rawIpc.close) {
395
- this._rawIpc.close();
396
- }
397
- }
398
- onClose(callback) {
399
- // ignore
341
+ const getMenuEntriesAccount = state => {
342
+ // TODO maybe query it now from layout?
343
+ const {
344
+ userLoginState
345
+ } = state;
346
+ if (userLoginState === 'logged in' || userLoginState === 'logging out') {
347
+ return getMenuEntriesAccountLoggedIn(state);
400
348
  }
401
- onMessage(callback) {
402
- this._rawIpc.addEventListener('message', callback);
403
- this._rawIpc.start();
349
+ return getMenuEntriesAccountLoggedOut(state);
350
+ };
351
+
352
+ const Tab = 1;
353
+ const Button = 1 << 1;
354
+ const Progress = 1 << 2;
355
+ const Enabled = 1 << 3;
356
+ const Selected = 1 << 4;
357
+ const Focused = 1 << 5;
358
+ const MarginTop = 1 << 6;
359
+
360
+ const emptyObject = {};
361
+ const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
362
+ const i18nString = (key, placeholders = emptyObject) => {
363
+ if (placeholders === emptyObject) {
364
+ return key;
404
365
  }
405
- }
406
- const wrap$e = port => {
407
- return new IpcChildWithModuleWorkerAndMessagePort(port);
366
+ const replacer = (match, rest) => {
367
+ return placeholders[rest];
368
+ };
369
+ return key.replaceAll(RE_PLACEHOLDER, replacer);
408
370
  };
409
- const IpcChildWithModuleWorkerAndMessagePort$1 = {
410
- __proto__: null,
411
- listen: listen$6,
412
- wrap: wrap$e
371
+
372
+ const Account$1 = 'Account';
373
+ const Explorer$1 = 'Explorer';
374
+ const Search$2 = 'Search';
375
+ const SourceControl$2 = 'Source Control';
376
+ const RunAndDebug$1 = 'Run and Debug';
377
+ const Extensions$2 = 'Extensions';
378
+ const Settings = 'Settings';
379
+ const AdditionalViews = 'Additional Views';
380
+ const ActivityBar$2 = 'Activity Bar';
381
+ const MoveSideBarLeft = 'Move Side Bar Left';
382
+ const MoveSideBarRight = 'Move Side Bar Right';
383
+ const HideActivityBar = 'Hide Activity Bar';
384
+ const CheckForUpdates = 'Check For Updates';
385
+ const ColorTheme = 'Color Theme';
386
+ const CommandPalette = 'Command Palette';
387
+ const KeyboardShortcuts = 'Keyboard Shortcuts';
388
+
389
+ const account = () => {
390
+ return i18nString(Account$1);
413
391
  };
414
- const addListener = (emitter, type, callback) => {
415
- if ('addEventListener' in emitter) {
416
- emitter.addEventListener(type, callback);
417
- } else {
418
- emitter.on(type, callback);
419
- }
392
+ const explorer = () => {
393
+ return i18nString(Explorer$1);
420
394
  };
421
- const removeListener = (emitter, type, callback) => {
422
- if ('removeEventListener' in emitter) {
423
- emitter.removeEventListener(type, callback);
424
- } else {
425
- emitter.off(type, callback);
426
- }
395
+ const search = () => {
396
+ return i18nString(Search$2);
427
397
  };
428
- const getFirstEvent = (eventEmitter, eventMap) => {
429
- const {
430
- promise,
431
- resolve
432
- } = Promise.withResolvers();
433
- const listenerMap = Object.create(null);
434
- const cleanup = value => {
435
- for (const event of Object.keys(eventMap)) {
436
- removeListener(eventEmitter, event, listenerMap[event]);
437
- }
438
- resolve(value);
439
- };
440
- for (const [event, type] of Object.entries(eventMap)) {
441
- const listener = event => {
442
- cleanup({
443
- event,
444
- type
445
- });
446
- };
447
- addListener(eventEmitter, event, listener);
448
- listenerMap[event] = listener;
449
- }
450
- return promise;
398
+ const sourceControl = () => {
399
+ return i18nString(SourceControl$2);
451
400
  };
452
- const Message$1 = 3;
453
- const create$5$1 = async ({
454
- isMessagePortOpen,
455
- messagePort
456
- }) => {
457
- if (!isMessagePort(messagePort)) {
458
- throw new IpcError('port must be of type MessagePort');
459
- }
460
- if (isMessagePortOpen) {
461
- return messagePort;
462
- }
463
- const eventPromise = getFirstEvent(messagePort, {
464
- message: Message$1
465
- });
466
- messagePort.start();
467
- const {
468
- event,
469
- type
470
- } = await eventPromise;
471
- if (type !== Message$1) {
472
- throw new IpcError('Failed to wait for ipc message');
473
- }
474
- if (event.data !== readyMessage) {
475
- throw new IpcError('unexpected first message');
476
- }
477
- return messagePort;
401
+ const runAndDebug = () => {
402
+ return i18nString(RunAndDebug$1);
478
403
  };
479
- const signal$1 = messagePort => {
480
- messagePort.start();
404
+ const extensions = () => {
405
+ return i18nString(Extensions$2);
481
406
  };
482
- class IpcParentWithMessagePort extends Ipc {
483
- getData = getData$2;
484
- send(message) {
485
- this._rawIpc.postMessage(message);
486
- }
487
- sendAndTransfer(message) {
488
- const transfer = getTransferrables(message);
489
- this._rawIpc.postMessage(message, transfer);
490
- }
491
- dispose() {
492
- this._rawIpc.close();
493
- }
494
- onMessage(callback) {
495
- this._rawIpc.addEventListener('message', callback);
496
- }
497
- onClose(callback) {}
498
- }
499
- const wrap$5 = messagePort => {
500
- return new IpcParentWithMessagePort(messagePort);
407
+ const settings = () => {
408
+ return i18nString(Settings);
501
409
  };
502
- const IpcParentWithMessagePort$1 = {
503
- __proto__: null,
504
- create: create$5$1,
505
- signal: signal$1,
506
- wrap: wrap$5
410
+ const additionalViews = () => {
411
+ return i18nString(AdditionalViews);
507
412
  };
508
-
509
- class CommandNotFoundError extends Error {
510
- constructor(command) {
511
- super(`Command not found ${command}`);
512
- this.name = 'CommandNotFoundError';
513
- }
514
- }
515
- const commands = Object.create(null);
516
- const register = commandMap => {
517
- Object.assign(commands, commandMap);
413
+ const activityBar = () => {
414
+ return i18nString(ActivityBar$2);
518
415
  };
519
- const getCommand = key => {
520
- return commands[key];
416
+ const moveSideBarRight = () => {
417
+ return i18nString(MoveSideBarRight);
521
418
  };
522
- const execute = (command, ...args) => {
523
- const fn = getCommand(command);
524
- if (!fn) {
525
- throw new CommandNotFoundError(command);
526
- }
527
- return fn(...args);
419
+ const moveSideBarLeft = () => {
420
+ return i18nString(MoveSideBarLeft);
528
421
  };
529
-
530
- const Two$1 = '2.0';
531
- const callbacks = Object.create(null);
532
- const get$2 = id => {
533
- return callbacks[id];
422
+ const hideActivityBar = () => {
423
+ return i18nString(HideActivityBar);
534
424
  };
535
- const remove$1 = id => {
536
- delete callbacks[id];
425
+ const checkForUpdates = () => {
426
+ return i18nString(CheckForUpdates);
537
427
  };
538
- class JsonRpcError extends Error {
539
- constructor(message) {
540
- super(message);
541
- this.name = 'JsonRpcError';
542
- }
543
- }
544
- const NewLine = '\n';
545
- const DomException = 'DOMException';
546
- const ReferenceError$1 = 'ReferenceError';
547
- const SyntaxError$1 = 'SyntaxError';
548
- const TypeError$1 = 'TypeError';
549
- const getErrorConstructor = (message, type) => {
550
- if (type) {
551
- switch (type) {
552
- case DomException:
553
- return DOMException;
554
- case ReferenceError$1:
555
- return ReferenceError;
556
- case SyntaxError$1:
557
- return SyntaxError;
558
- case TypeError$1:
559
- return TypeError;
560
- default:
561
- return Error;
562
- }
563
- }
564
- if (message.startsWith('TypeError: ')) {
565
- return TypeError;
566
- }
567
- if (message.startsWith('SyntaxError: ')) {
568
- return SyntaxError;
569
- }
570
- if (message.startsWith('ReferenceError: ')) {
571
- return ReferenceError;
572
- }
573
- return Error;
428
+ const commandPalette = () => {
429
+ return i18nString(CommandPalette);
574
430
  };
575
- const constructError = (message, type, name) => {
576
- const ErrorConstructor = getErrorConstructor(message, type);
577
- if (ErrorConstructor === DOMException && name) {
578
- return new ErrorConstructor(message, name);
579
- }
580
- if (ErrorConstructor === Error) {
581
- const error = new Error(message);
582
- if (name && name !== 'VError') {
583
- error.name = name;
584
- }
585
- return error;
586
- }
587
- return new ErrorConstructor(message);
431
+ const keyboardShortcuts = () => {
432
+ return i18nString(KeyboardShortcuts);
588
433
  };
589
- const joinLines = lines => {
590
- return lines.join(NewLine);
434
+ const colorTheme = () => {
435
+ return i18nString(ColorTheme);
591
436
  };
592
- const splitLines = lines => {
593
- return lines.split(NewLine);
437
+
438
+ const menuEntryMoveSideBar = sideBarLocation => {
439
+ switch (sideBarLocation) {
440
+ case Left:
441
+ return {
442
+ command: 'Layout.moveSideBarRight',
443
+ flags: None,
444
+ id: 'moveSideBarRight',
445
+ label: moveSideBarRight()
446
+ };
447
+ case Right:
448
+ return {
449
+ command: 'Layout.moveSideBarLeft',
450
+ flags: None,
451
+ id: 'moveSideBarLeft',
452
+ label: moveSideBarLeft()
453
+ };
454
+ default:
455
+ throw new Error('unexpected side bar location');
456
+ }
594
457
  };
595
- const getCurrentStack = () => {
596
- const stackLinesToSkip = 3;
597
- const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
598
- return currentStack;
458
+
459
+ const menuEntrySeparator = {
460
+ command: '',
461
+ flags: Separator,
462
+ id: 'separator',
463
+ label: ''
599
464
  };
600
- const getNewLineIndex = (string, startIndex = undefined) => {
601
- return string.indexOf(NewLine, startIndex);
465
+
466
+ const toContextMenuItem$1 = activityBarItem => {
467
+ const isEnabled = activityBarItem.flags & Enabled;
468
+ return {
469
+ args: [activityBarItem.id],
470
+ command: 'ActivityBar.toggleActivityBarItem',
471
+ flags: isEnabled ? Checked : Unchecked,
472
+ id: `toggle-${activityBarItem.id}`,
473
+ label: activityBarItem.id
474
+ };
602
475
  };
603
- const getParentStack = error => {
604
- let parentStack = error.stack || error.data || error.message || '';
605
- if (parentStack.startsWith(' at')) {
606
- parentStack = error.message + NewLine + parentStack;
476
+
477
+ const getMenuEntriesActivityBar = state => {
478
+ const {
479
+ activityBarItems,
480
+ sideBarLocation
481
+ } = state;
482
+ const topItems = activityBarItems.filter(item => !(item.flags & Button));
483
+ const bottomItems = activityBarItems.filter(item => item.flags & Button);
484
+ const entries = [...topItems.map(toContextMenuItem$1)];
485
+ if (bottomItems.length > 0) {
486
+ entries.push(menuEntrySeparator, ...bottomItems.map(toContextMenuItem$1));
607
487
  }
608
- return parentStack;
488
+ return [...entries, menuEntrySeparator, menuEntryMoveSideBar(sideBarLocation), {
489
+ command: 'Layout.hideActivityBar',
490
+ flags: None,
491
+ id: 'hideActivityBar',
492
+ label: hideActivityBar()
493
+ }];
609
494
  };
610
- const MethodNotFound = -32601;
611
- const Custom = -32001;
612
- const restoreJsonRpcError = error => {
613
- const currentStack = getCurrentStack();
614
- if (error && error instanceof Error) {
615
- if (typeof error.stack === 'string') {
616
- error.stack = error.stack + NewLine + currentStack;
617
- }
618
- return error;
619
- }
620
- if (error && error.code && error.code === MethodNotFound) {
621
- const restoredError = new JsonRpcError(error.message);
622
- const parentStack = getParentStack(error);
623
- restoredError.stack = parentStack + NewLine + currentStack;
624
- return restoredError;
495
+
496
+ const getNumberOfVisibleItems = state => {
497
+ const {
498
+ height,
499
+ itemHeight
500
+ } = state;
501
+ const numberOfVisibleItemsTop = Math.floor(height / itemHeight);
502
+ return numberOfVisibleItemsTop;
503
+ };
504
+ const getHiddenItems = state => {
505
+ const numberOfVisibleItems = getNumberOfVisibleItems(state);
506
+ const items = state.activityBarItems.filter(item => item.flags & Enabled);
507
+ if (numberOfVisibleItems >= items.length) {
508
+ return [];
625
509
  }
626
- if (error && error.message) {
627
- const restoredError = constructError(error.message, error.type, error.name);
628
- if (error.data) {
629
- if (error.data.stack && error.data.type && error.message) {
630
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
631
- } else if (error.data.stack) {
632
- restoredError.stack = error.data.stack;
633
- }
634
- if (error.data.codeFrame) {
635
- // @ts-ignore
636
- restoredError.codeFrame = error.data.codeFrame;
637
- }
638
- if (error.data.code) {
639
- // @ts-ignore
640
- restoredError.code = error.data.code;
641
- }
642
- if (error.data.type) {
643
- // @ts-ignore
644
- restoredError.name = error.data.type;
645
- }
646
- } else {
647
- if (error.stack) {
648
- const lowerStack = restoredError.stack || '';
649
- // @ts-ignore
650
- const indexNewLine = getNewLineIndex(lowerStack);
651
- const parentStack = getParentStack(error);
652
- // @ts-ignore
653
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
654
- }
655
- if (error.codeFrame) {
656
- // @ts-ignore
657
- restoredError.codeFrame = error.codeFrame;
658
- }
659
- }
660
- return restoredError;
661
- }
662
- if (typeof error === 'string') {
663
- return new Error(`JsonRpc Error: ${error}`);
510
+ return items.slice(numberOfVisibleItems - 2, -1);
511
+ };
512
+
513
+ const toContextMenuItem = activityBarItem => {
514
+ return {
515
+ command: '-1',
516
+ // TODO
517
+ flags: None,
518
+ id: '8000',
519
+ // TODO
520
+ label: activityBarItem.id
521
+ };
522
+ };
523
+ const getMenuEntriesAdditionalViews = state => {
524
+ const hiddenActivityBarItems = getHiddenItems(state);
525
+ return hiddenActivityBarItems.map(toContextMenuItem);
526
+ };
527
+
528
+ const keyBindingsUri = 'app://keybindings';
529
+ const getMenuEntriesSettings = state => {
530
+ const {
531
+ platform
532
+ } = state;
533
+ const items = [{
534
+ command: 'QuickPick.showEverything',
535
+ flags: None,
536
+ id: 'commandPalette',
537
+ label: commandPalette()
538
+ }, menuEntrySeparator, {
539
+ command: 'Preferences.openSettingsJson',
540
+ flags: None,
541
+ id: 'settings',
542
+ label: settings()
543
+ }, {
544
+ args: [keyBindingsUri],
545
+ command: 'Main.openUri',
546
+ flags: None,
547
+ id: 'keyboardShortcuts',
548
+ label: keyboardShortcuts()
549
+ }, {
550
+ command: 'QuickPick.showColorTheme',
551
+ flags: None,
552
+ id: 'colorTheme',
553
+ label: colorTheme()
554
+ }];
555
+ if (platform !== Web) {
556
+ items.push(menuEntrySeparator, {
557
+ command: 'AutoUpdater.checkForUpdates',
558
+ flags: None,
559
+ id: 'checkForUpdates',
560
+ label: checkForUpdates()
561
+ });
664
562
  }
665
- return new Error(`JsonRpc Error: ${error}`);
563
+ return items;
666
564
  };
667
- const unwrapJsonRpcResult = responseMessage => {
668
- if ('error' in responseMessage) {
669
- const restoredError = restoreJsonRpcError(responseMessage.error);
670
- throw restoredError;
565
+
566
+ const normalizeLine = line => {
567
+ if (line.startsWith('Error: ')) {
568
+ return line.slice('Error: '.length);
671
569
  }
672
- if ('result' in responseMessage) {
673
- return responseMessage.result;
570
+ if (line.startsWith('VError: ')) {
571
+ return line.slice('VError: '.length);
674
572
  }
675
- throw new JsonRpcError('unexpected response message');
676
- };
677
- const warn = (...args) => {
678
- console.warn(...args);
573
+ return line;
679
574
  };
680
- const resolve = (id, response) => {
681
- const fn = get$2(id);
682
- if (!fn) {
683
- console.log(response);
684
- warn(`callback ${id} may already be disposed`);
685
- return;
575
+ const getCombinedMessage = (error, message) => {
576
+ const stringifiedError = normalizeLine(`${error}`);
577
+ if (message) {
578
+ return `${message}: ${stringifiedError}`;
686
579
  }
687
- fn(response);
688
- remove$1(id);
580
+ return stringifiedError;
689
581
  };
690
- const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
691
- const getErrorType = prettyError => {
692
- if (prettyError && prettyError.type) {
693
- return prettyError.type;
582
+ const NewLine$2 = '\n';
583
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
584
+ return string.indexOf(NewLine$2, startIndex);
585
+ };
586
+ const mergeStacks = (parent, child) => {
587
+ if (!child) {
588
+ return parent;
694
589
  }
695
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
696
- return prettyError.constructor.name;
590
+ const parentNewLineIndex = getNewLineIndex$1(parent);
591
+ const childNewLineIndex = getNewLineIndex$1(child);
592
+ if (childNewLineIndex === -1) {
593
+ return parent;
697
594
  }
698
- return undefined;
699
- };
700
- const isAlreadyStack = line => {
701
- return line.trim().startsWith('at ');
595
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
596
+ const childRest = child.slice(childNewLineIndex);
597
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
598
+ if (parentFirstLine.includes(childFirstLine)) {
599
+ return parentFirstLine + childRest;
600
+ }
601
+ return child;
702
602
  };
703
- const getStack = prettyError => {
704
- const stackString = prettyError.stack || '';
705
- const newLineIndex = stackString.indexOf('\n');
706
- if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
707
- return stackString.slice(newLineIndex + 1);
603
+ class VError extends Error {
604
+ constructor(error, message) {
605
+ const combinedMessage = getCombinedMessage(error, message);
606
+ super(combinedMessage);
607
+ this.name = 'VError';
608
+ if (error instanceof Error) {
609
+ this.stack = mergeStacks(this.stack, error.stack);
610
+ }
611
+ if (error.codeFrame) {
612
+ // @ts-ignore
613
+ this.codeFrame = error.codeFrame;
614
+ }
615
+ if (error.code) {
616
+ // @ts-ignore
617
+ this.code = error.code;
618
+ }
619
+ }
620
+ }
621
+
622
+ class AssertionError extends Error {
623
+ constructor(message) {
624
+ super(message);
625
+ this.name = 'AssertionError';
626
+ }
627
+ }
628
+ const Object$1 = 1;
629
+ const Number$1 = 2;
630
+ const Array$1 = 3;
631
+ const String = 4;
632
+ const Boolean$1 = 5;
633
+ const Function = 6;
634
+ const Null = 7;
635
+ const Unknown = 8;
636
+ const getType = value => {
637
+ switch (typeof value) {
638
+ case 'number':
639
+ return Number$1;
640
+ case 'function':
641
+ return Function;
642
+ case 'string':
643
+ return String;
644
+ case 'object':
645
+ if (value === null) {
646
+ return Null;
647
+ }
648
+ if (Array.isArray(value)) {
649
+ return Array$1;
650
+ }
651
+ return Object$1;
652
+ case 'boolean':
653
+ return Boolean$1;
654
+ default:
655
+ return Unknown;
708
656
  }
709
- return stackString;
710
657
  };
711
- const getErrorProperty = (error, prettyError) => {
712
- if (error && error.code === E_COMMAND_NOT_FOUND) {
713
- return {
714
- code: MethodNotFound,
715
- data: error.stack,
716
- message: error.message
717
- };
658
+ const number = value => {
659
+ const type = getType(value);
660
+ if (type !== Number$1) {
661
+ throw new AssertionError('expected value to be of type number');
718
662
  }
719
- return {
720
- code: Custom,
721
- data: {
722
- code: prettyError.code,
723
- codeFrame: prettyError.codeFrame,
724
- name: prettyError.name,
725
- stack: getStack(prettyError),
726
- type: getErrorType(prettyError)
727
- },
728
- message: prettyError.message
729
- };
730
663
  };
731
- const create$1$1 = (id, error) => {
732
- return {
733
- error,
734
- id,
735
- jsonrpc: Two$1
736
- };
664
+
665
+ const isMessagePort = value => {
666
+ return value && value instanceof MessagePort;
737
667
  };
738
- const getErrorResponse = (id, error, preparePrettyError, logError) => {
739
- const prettyError = preparePrettyError(error);
740
- logError(error, prettyError);
741
- const errorProperty = getErrorProperty(error, prettyError);
742
- return create$1$1(id, errorProperty);
668
+ const isMessagePortMain = value => {
669
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
743
670
  };
744
- const create$a = (message, result) => {
745
- return {
746
- id: message.id,
747
- jsonrpc: Two$1,
748
- result: result ?? null
749
- };
671
+ const isOffscreenCanvas = value => {
672
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
750
673
  };
751
- const getSuccessResponse = (message, result) => {
752
- const resultProperty = result ?? null;
753
- return create$a(message, resultProperty);
674
+ const isInstanceOf = (value, constructorName) => {
675
+ return value?.constructor?.name === constructorName;
754
676
  };
755
- const getErrorResponseSimple = (id, error) => {
756
- return {
757
- error: {
758
- code: Custom,
759
- data: error,
760
- // @ts-ignore
761
- message: error.message
762
- },
763
- id,
764
- jsonrpc: Two$1
765
- };
677
+ const isSocket = value => {
678
+ return isInstanceOf(value, 'Socket');
766
679
  };
767
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
768
- try {
769
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
770
- return getSuccessResponse(message, result);
771
- } catch (error) {
772
- if (ipc.canUseSimpleErrorResponse) {
773
- return getErrorResponseSimple(message.id, error);
680
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
681
+ const isTransferrable = value => {
682
+ for (const fn of transferrables) {
683
+ if (fn(value)) {
684
+ return true;
774
685
  }
775
- return getErrorResponse(message.id, error, preparePrettyError, logError);
776
686
  }
777
- };
778
- const defaultPreparePrettyError = error => {
779
- return error;
780
- };
781
- const defaultLogError = () => {
782
- // ignore
783
- };
784
- const defaultRequiresSocket = () => {
785
687
  return false;
786
688
  };
787
- const defaultResolve = resolve;
788
-
789
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
790
- const normalizeParams = args => {
791
- if (args.length === 1) {
792
- const options = args[0];
793
- return {
794
- execute: options.execute,
795
- ipc: options.ipc,
796
- logError: options.logError || defaultLogError,
797
- message: options.message,
798
- preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
799
- requiresSocket: options.requiresSocket || defaultRequiresSocket,
800
- resolve: options.resolve || defaultResolve
801
- };
689
+ const walkValue = (value, transferrables, isTransferrable) => {
690
+ if (!value) {
691
+ return;
802
692
  }
803
- return {
804
- execute: args[2],
805
- ipc: args[0],
806
- logError: args[5],
807
- message: args[1],
808
- preparePrettyError: args[4],
809
- requiresSocket: args[6],
810
- resolve: args[3]
811
- };
812
- };
813
- const handleJsonRpcMessage = async (...args) => {
814
- const options = normalizeParams(args);
815
- const {
816
- execute,
817
- ipc,
818
- logError,
819
- message,
820
- preparePrettyError,
821
- requiresSocket,
822
- resolve
823
- } = options;
824
- if ('id' in message) {
825
- if ('method' in message) {
826
- const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
827
- try {
828
- ipc.send(response);
829
- } catch (error) {
830
- const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
831
- ipc.send(errorResponse);
832
- }
833
- return;
693
+ if (isTransferrable(value)) {
694
+ transferrables.push(value);
695
+ return;
696
+ }
697
+ if (Array.isArray(value)) {
698
+ for (const item of value) {
699
+ walkValue(item, transferrables, isTransferrable);
834
700
  }
835
- resolve(message.id, message);
836
701
  return;
837
702
  }
838
- if ('method' in message) {
839
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
703
+ if (typeof value === 'object') {
704
+ for (const property of Object.values(value)) {
705
+ walkValue(property, transferrables, isTransferrable);
706
+ }
840
707
  return;
841
708
  }
842
- throw new JsonRpcError('unexpected message');
843
709
  };
844
-
845
- const Two = '2.0';
846
-
847
- const create$9 = (method, params) => {
848
- return {
849
- jsonrpc: Two,
850
- method,
851
- params
852
- };
710
+ const getTransferrables = value => {
711
+ const transferrables = [];
712
+ walkValue(value, transferrables, isTransferrable);
713
+ return transferrables;
853
714
  };
854
-
855
- const create$8 = (id, method, params) => {
856
- const message = {
857
- id,
858
- jsonrpc: Two,
859
- method,
860
- params
715
+ const attachEvents = that => {
716
+ const handleMessage = (...args) => {
717
+ const data = that.getData(...args);
718
+ that.dispatchEvent(new MessageEvent('message', {
719
+ data
720
+ }));
861
721
  };
862
- return message;
863
- };
864
-
865
- let id = 0;
866
- const create$7 = () => {
867
- return ++id;
868
- };
869
-
870
- const registerPromise = map => {
871
- const id = create$7();
872
- const {
873
- promise,
874
- resolve
875
- } = Promise.withResolvers();
876
- map[id] = resolve;
877
- return {
878
- id,
879
- promise
722
+ that.onMessage(handleMessage);
723
+ const handleClose = event => {
724
+ that.dispatchEvent(new Event('close'));
880
725
  };
726
+ that.onClose(handleClose);
881
727
  };
882
-
883
- const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
884
- const {
885
- id,
886
- promise
887
- } = registerPromise(callbacks);
888
- const message = create$8(id, method, params);
889
- if (useSendAndTransfer && ipc.sendAndTransfer) {
890
- ipc.sendAndTransfer(message);
891
- } else {
892
- ipc.send(message);
728
+ class Ipc extends EventTarget {
729
+ constructor(rawIpc) {
730
+ super();
731
+ this._rawIpc = rawIpc;
732
+ attachEvents(this);
893
733
  }
894
- const responseMessage = await promise;
895
- return unwrapJsonRpcResult(responseMessage);
734
+ }
735
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
736
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
737
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
738
+ const NewLine$1 = '\n';
739
+ const joinLines$1 = lines => {
740
+ return lines.join(NewLine$1);
896
741
  };
897
- const createRpc = ipc => {
898
- const callbacks = Object.create(null);
899
- ipc._resolve = (id, response) => {
900
- const fn = callbacks[id];
901
- if (!fn) {
902
- console.warn(`callback ${id} may already be disposed`);
903
- return;
904
- }
905
- fn(response);
906
- delete callbacks[id];
907
- };
908
- const rpc = {
909
- async dispose() {
910
- await ipc?.dispose();
911
- },
912
- invoke(method, ...params) {
913
- return invokeHelper(callbacks, ipc, method, params, false);
914
- },
915
- invokeAndTransfer(method, ...params) {
916
- return invokeHelper(callbacks, ipc, method, params, true);
917
- },
918
- // @ts-ignore
919
- ipc,
920
- /**
921
- * @deprecated
922
- */
923
- send(method, ...params) {
924
- const message = create$9(method, params);
925
- ipc.send(message);
742
+ const RE_AT = /^\s+at/;
743
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
744
+ const isNormalStackLine = line => {
745
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
746
+ };
747
+ const getDetails = lines => {
748
+ const index = lines.findIndex(isNormalStackLine);
749
+ if (index === -1) {
750
+ return {
751
+ actualMessage: joinLines$1(lines),
752
+ rest: []
753
+ };
754
+ }
755
+ let lastIndex = index - 1;
756
+ while (++lastIndex < lines.length) {
757
+ if (!isNormalStackLine(lines[lastIndex])) {
758
+ break;
926
759
  }
760
+ }
761
+ return {
762
+ actualMessage: lines[index - 1],
763
+ rest: lines.slice(index, lastIndex)
927
764
  };
928
- return rpc;
929
765
  };
930
-
931
- const requiresSocket = () => {
932
- return false;
766
+ const splitLines$1 = lines => {
767
+ return lines.split(NewLine$1);
933
768
  };
934
- const preparePrettyError = error => {
935
- return error;
769
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
770
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
771
+ const isMessageCodeBlockStartIndex = line => {
772
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
936
773
  };
937
- const logError = () => {
938
- // handled by renderer worker
774
+ const isMessageCodeBlockEndIndex = line => {
775
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
939
776
  };
940
- const handleMessage = event => {
941
- const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
942
- const actualExecute = event?.target?.execute || execute;
943
- return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
777
+ const getMessageCodeBlock = stderr => {
778
+ const lines = splitLines$1(stderr);
779
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
780
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
781
+ const relevantLines = lines.slice(startIndex, endIndex);
782
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
783
+ return relevantMessage;
944
784
  };
945
-
946
- const handleIpc = ipc => {
947
- if ('addEventListener' in ipc) {
948
- ipc.addEventListener('message', handleMessage);
949
- } else if ('on' in ipc) {
950
- // deprecated
951
- ipc.on('message', handleMessage);
952
- }
785
+ const isModuleNotFoundMessage = line => {
786
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
953
787
  };
954
-
955
- const listen$1 = async (module, options) => {
956
- const rawIpc = await module.listen(options);
957
- if (module.signal) {
958
- module.signal(rawIpc);
959
- }
960
- const ipc = module.wrap(rawIpc);
961
- return ipc;
788
+ const getModuleNotFoundError = stderr => {
789
+ const lines = splitLines$1(stderr);
790
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
791
+ const message = lines[messageIndex];
792
+ return {
793
+ code: ERR_MODULE_NOT_FOUND,
794
+ message
795
+ };
962
796
  };
963
-
964
- const create$6 = async ({
965
- commandMap,
966
- isMessagePortOpen = true,
967
- messagePort
968
- }) => {
969
- // TODO create a commandMap per rpc instance
970
- register(commandMap);
971
- const rawIpc = await IpcParentWithMessagePort$1.create({
972
- isMessagePortOpen,
973
- messagePort
974
- });
975
- const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
976
- handleIpc(ipc);
977
- const rpc = createRpc(ipc);
978
- messagePort.start();
979
- return rpc;
797
+ const isModuleNotFoundError = stderr => {
798
+ if (!stderr) {
799
+ return false;
800
+ }
801
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
980
802
  };
981
-
982
- const create$5 = async ({
983
- commandMap,
984
- isMessagePortOpen,
985
- send
986
- }) => {
987
- const {
988
- port1,
989
- port2
990
- } = new MessageChannel();
991
- await send(port1);
992
- return create$6({
993
- commandMap,
994
- isMessagePortOpen,
995
- messagePort: port2
996
- });
803
+ const isModulesSyntaxError = stderr => {
804
+ if (!stderr) {
805
+ return false;
806
+ }
807
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
997
808
  };
998
-
999
- const createSharedLazyRpc = factory => {
1000
- let rpcPromise;
1001
- const getOrCreate = () => {
1002
- if (!rpcPromise) {
1003
- rpcPromise = factory();
1004
- }
1005
- return rpcPromise;
1006
- };
809
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
810
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
811
+ const isUnhelpfulNativeModuleError = stderr => {
812
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
813
+ };
814
+ const getNativeModuleErrorMessage = stderr => {
815
+ const message = getMessageCodeBlock(stderr);
1007
816
  return {
1008
- async dispose() {
1009
- const rpc = await getOrCreate();
1010
- await rpc.dispose();
1011
- },
1012
- async invoke(method, ...params) {
1013
- const rpc = await getOrCreate();
1014
- return rpc.invoke(method, ...params);
1015
- },
1016
- async invokeAndTransfer(method, ...params) {
1017
- const rpc = await getOrCreate();
1018
- return rpc.invokeAndTransfer(method, ...params);
1019
- },
1020
- async send(method, ...params) {
1021
- const rpc = await getOrCreate();
1022
- rpc.send(method, ...params);
1023
- }
817
+ code: E_INCOMPATIBLE_NATIVE_MODULE,
818
+ message: `Incompatible native node module: ${message}`
1024
819
  };
1025
820
  };
1026
-
1027
- const create$4 = async ({
1028
- commandMap,
1029
- isMessagePortOpen,
1030
- send
1031
- }) => {
1032
- return createSharedLazyRpc(() => {
1033
- return create$5({
1034
- commandMap,
1035
- isMessagePortOpen,
1036
- send
1037
- });
1038
- });
1039
- };
1040
-
1041
- const create$3 = async ({
1042
- commandMap
1043
- }) => {
1044
- // TODO create a commandMap per rpc instance
1045
- register(commandMap);
1046
- const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
1047
- handleIpc(ipc);
1048
- const rpc = createRpc(ipc);
1049
- return rpc;
1050
- };
1051
-
1052
- const createMockRpc = ({
1053
- commandMap
1054
- }) => {
1055
- const invocations = [];
1056
- const invoke = (method, ...params) => {
1057
- invocations.push([method, ...params]);
1058
- const command = commandMap[method];
1059
- if (!command) {
1060
- throw new Error(`command ${method} not found`);
1061
- }
1062
- return command(...params);
821
+ const getModuleSyntaxError = () => {
822
+ return {
823
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
824
+ message: `ES Modules are not supported in electron`
1063
825
  };
1064
- const mockRpc = {
1065
- invocations,
1066
- invoke,
1067
- invokeAndTransfer: invoke
826
+ };
827
+ const getHelpfulChildProcessError = (stdout, stderr) => {
828
+ if (isUnhelpfulNativeModuleError(stderr)) {
829
+ return getNativeModuleErrorMessage(stderr);
830
+ }
831
+ if (isModulesSyntaxError(stderr)) {
832
+ return getModuleSyntaxError();
833
+ }
834
+ if (isModuleNotFoundError(stderr)) {
835
+ return getModuleNotFoundError(stderr);
836
+ }
837
+ const lines = splitLines$1(stderr);
838
+ const {
839
+ actualMessage,
840
+ rest
841
+ } = getDetails(lines);
842
+ return {
843
+ code: '',
844
+ message: actualMessage,
845
+ stack: rest
1068
846
  };
1069
- return mockRpc;
1070
847
  };
1071
-
1072
- const rpcs = Object.create(null);
1073
- const set$4 = (id, rpc) => {
1074
- rpcs[id] = rpc;
848
+ class IpcError extends VError {
849
+ // @ts-ignore
850
+ constructor(betterMessage, stdout = '', stderr = '') {
851
+ if (stdout || stderr) {
852
+ // @ts-ignore
853
+ const {
854
+ code,
855
+ message,
856
+ stack
857
+ } = getHelpfulChildProcessError(stdout, stderr);
858
+ const cause = new Error(message);
859
+ // @ts-ignore
860
+ cause.code = code;
861
+ cause.stack = stack;
862
+ super(cause, betterMessage);
863
+ } else {
864
+ super(betterMessage);
865
+ }
866
+ // @ts-ignore
867
+ this.name = 'IpcError';
868
+ // @ts-ignore
869
+ this.stdout = stdout;
870
+ // @ts-ignore
871
+ this.stderr = stderr;
872
+ }
873
+ }
874
+ const readyMessage = 'ready';
875
+ const getData$2 = event => {
876
+ return event.data;
1075
877
  };
1076
- const get$1 = id => {
1077
- return rpcs[id];
878
+ const listen$7 = () => {
879
+ // @ts-ignore
880
+ if (typeof WorkerGlobalScope === 'undefined') {
881
+ throw new TypeError('module is not in web worker scope');
882
+ }
883
+ return globalThis;
1078
884
  };
1079
- const remove = id => {
1080
- delete rpcs[id];
885
+ const signal$8 = global => {
886
+ global.postMessage(readyMessage);
1081
887
  };
1082
-
1083
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
1084
- const create$2 = rpcId => {
1085
- return {
1086
- async dispose() {
1087
- const rpc = get$1(rpcId);
1088
- await rpc.dispose();
1089
- },
888
+ class IpcChildWithModuleWorker extends Ipc {
889
+ getData(event) {
890
+ return getData$2(event);
891
+ }
892
+ send(message) {
1090
893
  // @ts-ignore
1091
- invoke(method, ...params) {
1092
- const rpc = get$1(rpcId);
1093
- // @ts-ignore
1094
- return rpc.invoke(method, ...params);
1095
- },
894
+ this._rawIpc.postMessage(message);
895
+ }
896
+ sendAndTransfer(message) {
897
+ const transfer = getTransferrables(message);
1096
898
  // @ts-ignore
1097
- invokeAndTransfer(method, ...params) {
1098
- const rpc = get$1(rpcId);
1099
- // @ts-ignore
1100
- return rpc.invokeAndTransfer(method, ...params);
1101
- },
1102
- registerMockRpc(commandMap) {
1103
- const mockRpc = createMockRpc({
1104
- commandMap
1105
- });
1106
- set$4(rpcId, mockRpc);
1107
- // @ts-ignore
1108
- mockRpc[Symbol.dispose] = () => {
1109
- remove(rpcId);
1110
- };
1111
- // @ts-ignore
1112
- return mockRpc;
1113
- },
1114
- set(rpc) {
1115
- set$4(rpcId, rpc);
1116
- }
1117
- };
899
+ this._rawIpc.postMessage(message, transfer);
900
+ }
901
+ dispose() {
902
+ // ignore
903
+ }
904
+ onClose(callback) {
905
+ // ignore
906
+ }
907
+ onMessage(callback) {
908
+ this._rawIpc.addEventListener('message', callback);
909
+ }
910
+ }
911
+ const wrap$f = global => {
912
+ return new IpcChildWithModuleWorker(global);
913
+ };
914
+ const waitForFirstMessage = async port => {
915
+ const {
916
+ promise,
917
+ resolve
918
+ } = Promise.withResolvers();
919
+ port.addEventListener('message', resolve, {
920
+ once: true
921
+ });
922
+ const event = await promise;
923
+ // @ts-ignore
924
+ return event.data;
925
+ };
926
+ const listen$6 = async () => {
927
+ const parentIpcRaw = listen$7();
928
+ signal$8(parentIpcRaw);
929
+ const parentIpc = wrap$f(parentIpcRaw);
930
+ const firstMessage = await waitForFirstMessage(parentIpc);
931
+ if (firstMessage.method !== 'initialize') {
932
+ throw new IpcError('unexpected first message');
933
+ }
934
+ const type = firstMessage.params[0];
935
+ if (type === 'message-port') {
936
+ parentIpc.send({
937
+ id: firstMessage.id,
938
+ jsonrpc: '2.0',
939
+ result: null
940
+ });
941
+ parentIpc.dispose();
942
+ const port = firstMessage.params[1];
943
+ return port;
944
+ }
945
+ return globalThis;
946
+ };
947
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
948
+ getData(event) {
949
+ return getData$2(event);
950
+ }
951
+ send(message) {
952
+ this._rawIpc.postMessage(message);
953
+ }
954
+ sendAndTransfer(message) {
955
+ const transfer = getTransferrables(message);
956
+ this._rawIpc.postMessage(message, transfer);
957
+ }
958
+ dispose() {
959
+ if (this._rawIpc.close) {
960
+ this._rawIpc.close();
961
+ }
962
+ }
963
+ onClose(callback) {
964
+ // ignore
965
+ }
966
+ onMessage(callback) {
967
+ this._rawIpc.addEventListener('message', callback);
968
+ this._rawIpc.start();
969
+ }
970
+ }
971
+ const wrap$e = port => {
972
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
973
+ };
974
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
975
+ __proto__: null,
976
+ listen: listen$6,
977
+ wrap: wrap$e
978
+ };
979
+ const addListener = (emitter, type, callback) => {
980
+ if ('addEventListener' in emitter) {
981
+ emitter.addEventListener(type, callback);
982
+ } else {
983
+ emitter.on(type, callback);
984
+ }
985
+ };
986
+ const removeListener = (emitter, type, callback) => {
987
+ if ('removeEventListener' in emitter) {
988
+ emitter.removeEventListener(type, callback);
989
+ } else {
990
+ emitter.off(type, callback);
991
+ }
992
+ };
993
+ const getFirstEvent = (eventEmitter, eventMap) => {
994
+ const {
995
+ promise,
996
+ resolve
997
+ } = Promise.withResolvers();
998
+ const listenerMap = Object.create(null);
999
+ const cleanup = value => {
1000
+ for (const event of Object.keys(eventMap)) {
1001
+ removeListener(eventEmitter, event, listenerMap[event]);
1002
+ }
1003
+ resolve(value);
1004
+ };
1005
+ for (const [event, type] of Object.entries(eventMap)) {
1006
+ const listener = event => {
1007
+ cleanup({
1008
+ event,
1009
+ type
1010
+ });
1011
+ };
1012
+ addListener(eventEmitter, event, listener);
1013
+ listenerMap[event] = listener;
1014
+ }
1015
+ return promise;
1016
+ };
1017
+ const Message$1 = 3;
1018
+ const create$5$1 = async ({
1019
+ isMessagePortOpen,
1020
+ messagePort
1021
+ }) => {
1022
+ if (!isMessagePort(messagePort)) {
1023
+ throw new IpcError('port must be of type MessagePort');
1024
+ }
1025
+ if (isMessagePortOpen) {
1026
+ return messagePort;
1027
+ }
1028
+ const eventPromise = getFirstEvent(messagePort, {
1029
+ message: Message$1
1030
+ });
1031
+ messagePort.start();
1032
+ const {
1033
+ event,
1034
+ type
1035
+ } = await eventPromise;
1036
+ if (type !== Message$1) {
1037
+ throw new IpcError('Failed to wait for ipc message');
1038
+ }
1039
+ if (event.data !== readyMessage) {
1040
+ throw new IpcError('unexpected first message');
1041
+ }
1042
+ return messagePort;
1043
+ };
1044
+ const signal$1 = messagePort => {
1045
+ messagePort.start();
1046
+ };
1047
+ class IpcParentWithMessagePort extends Ipc {
1048
+ getData = getData$2;
1049
+ send(message) {
1050
+ this._rawIpc.postMessage(message);
1051
+ }
1052
+ sendAndTransfer(message) {
1053
+ const transfer = getTransferrables(message);
1054
+ this._rawIpc.postMessage(message, transfer);
1055
+ }
1056
+ dispose() {
1057
+ this._rawIpc.close();
1058
+ }
1059
+ onMessage(callback) {
1060
+ this._rawIpc.addEventListener('message', callback);
1061
+ }
1062
+ onClose(callback) {}
1063
+ }
1064
+ const wrap$5 = messagePort => {
1065
+ return new IpcParentWithMessagePort(messagePort);
1066
+ };
1067
+ const IpcParentWithMessagePort$1 = {
1068
+ __proto__: null,
1069
+ create: create$5$1,
1070
+ signal: signal$1,
1071
+ wrap: wrap$5
1118
1072
  };
1119
1073
 
1120
- const {
1121
- set: set$3
1122
- } = create$2(6010);
1123
-
1124
- const Button$2 = 'button';
1125
- const None$1 = 'none';
1126
- const Tab$1 = 'tab';
1127
- const ToolBar = 'toolbar';
1128
-
1129
- const Div = 4;
1130
- const Text = 12;
1131
- const Reference = 100;
1132
-
1133
- const Button$1 = 'event.button';
1134
- const ClientX = 'event.clientX';
1135
- const ClientY = 'event.clientY';
1136
-
1137
- const Enter = 3;
1138
- const Space = 9;
1139
- const PageUp = 10;
1140
- const PageDown = 11;
1141
- const End = 255;
1142
- const Home = 12;
1143
- const UpArrow = 14;
1144
- const DownArrow = 16;
1145
-
1146
- const ActivityBar$3 = 1;
1147
- const Settings$1 = 12;
1148
- const ActivityBarAdditionalViews = 17;
1149
-
1150
- const Separator = 1;
1151
- const None = 0;
1152
- const Checked = 2;
1153
- const Unchecked = 3;
1154
-
1155
- const LeftClick = 0;
1156
-
1157
- const Web = 1;
1158
-
1159
- const ExtensionManagementWorker = 9006;
1160
- const RendererWorker = 1;
1161
-
1162
- const Left = 1;
1163
- const Right = 2;
1164
-
1165
- const SetCss = 'Viewlet.setCss';
1166
- const SetDom2 = 'Viewlet.setDom2';
1167
- const SetFocusContext = 'Viewlet.setFocusContext';
1168
- const SetPatches = 'Viewlet.setPatches';
1169
-
1170
- const FocusActivityBar = 5;
1171
- const FocusExplorer = 13;
1172
-
1173
- const {
1174
- invoke: invoke$1,
1175
- set: set$2
1176
- } = create$2(ExtensionManagementWorker);
1177
-
1178
- const {
1179
- invoke,
1180
- invokeAndTransfer,
1181
- set: set$1
1182
- } = create$2(RendererWorker);
1183
- const showContextMenu2 = async (uid, menuId, x, y, args) => {
1184
- number(uid);
1185
- number(menuId);
1186
- number(x);
1187
- number(y);
1188
- await invoke('ContextMenu.show2', uid, menuId, x, y, args);
1074
+ class CommandNotFoundError extends Error {
1075
+ constructor(command) {
1076
+ super(`Command not found ${command}`);
1077
+ this.name = 'CommandNotFoundError';
1078
+ }
1079
+ }
1080
+ const commands = Object.create(null);
1081
+ const register = commandMap => {
1082
+ Object.assign(commands, commandMap);
1189
1083
  };
1190
- const sendMessagePortToAuthWorker = async (port, rpcId) => {
1191
- const command = 'HandleMessagePort.handleMessagePort';
1192
- await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToAuthWorker', port, command, rpcId);
1084
+ const getCommand = key => {
1085
+ return commands[key];
1086
+ };
1087
+ const execute = (command, ...args) => {
1088
+ const fn = getCommand(command);
1089
+ if (!fn) {
1090
+ throw new CommandNotFoundError(command);
1091
+ }
1092
+ return fn(...args);
1193
1093
  };
1194
1094
 
1195
- const toCommandId = key => {
1196
- const dotIndex = key.indexOf('.');
1197
- return key.slice(dotIndex + 1);
1095
+ const Two$1 = '2.0';
1096
+ const callbacks = Object.create(null);
1097
+ const get$1 = id => {
1098
+ return callbacks[id];
1198
1099
  };
1199
- const create$1 = () => {
1200
- const states = Object.create(null);
1201
- const commandMapRef = {};
1202
- return {
1203
- clear() {
1204
- for (const key of Object.keys(states)) {
1205
- delete states[key];
1100
+ const remove$1 = id => {
1101
+ delete callbacks[id];
1102
+ };
1103
+ class JsonRpcError extends Error {
1104
+ constructor(message) {
1105
+ super(message);
1106
+ this.name = 'JsonRpcError';
1107
+ }
1108
+ }
1109
+ const NewLine = '\n';
1110
+ const DomException = 'DOMException';
1111
+ const ReferenceError$1 = 'ReferenceError';
1112
+ const SyntaxError$1 = 'SyntaxError';
1113
+ const TypeError$1 = 'TypeError';
1114
+ const getErrorConstructor = (message, type) => {
1115
+ if (type) {
1116
+ switch (type) {
1117
+ case DomException:
1118
+ return DOMException;
1119
+ case ReferenceError$1:
1120
+ return ReferenceError;
1121
+ case SyntaxError$1:
1122
+ return SyntaxError;
1123
+ case TypeError$1:
1124
+ return TypeError;
1125
+ default:
1126
+ return Error;
1127
+ }
1128
+ }
1129
+ if (message.startsWith('TypeError: ')) {
1130
+ return TypeError;
1131
+ }
1132
+ if (message.startsWith('SyntaxError: ')) {
1133
+ return SyntaxError;
1134
+ }
1135
+ if (message.startsWith('ReferenceError: ')) {
1136
+ return ReferenceError;
1137
+ }
1138
+ return Error;
1139
+ };
1140
+ const constructError = (message, type, name) => {
1141
+ const ErrorConstructor = getErrorConstructor(message, type);
1142
+ if (ErrorConstructor === DOMException && name) {
1143
+ return new ErrorConstructor(message, name);
1144
+ }
1145
+ if (ErrorConstructor === Error) {
1146
+ const error = new Error(message);
1147
+ if (name && name !== 'VError') {
1148
+ error.name = name;
1149
+ }
1150
+ return error;
1151
+ }
1152
+ return new ErrorConstructor(message);
1153
+ };
1154
+ const joinLines = lines => {
1155
+ return lines.join(NewLine);
1156
+ };
1157
+ const splitLines = lines => {
1158
+ return lines.split(NewLine);
1159
+ };
1160
+ const getCurrentStack = () => {
1161
+ const stackLinesToSkip = 3;
1162
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
1163
+ return currentStack;
1164
+ };
1165
+ const getNewLineIndex = (string, startIndex = undefined) => {
1166
+ return string.indexOf(NewLine, startIndex);
1167
+ };
1168
+ const getParentStack = error => {
1169
+ let parentStack = error.stack || error.data || error.message || '';
1170
+ if (parentStack.startsWith(' at')) {
1171
+ parentStack = error.message + NewLine + parentStack;
1172
+ }
1173
+ return parentStack;
1174
+ };
1175
+ const MethodNotFound = -32601;
1176
+ const Custom = -32001;
1177
+ const restoreJsonRpcError = error => {
1178
+ const currentStack = getCurrentStack();
1179
+ if (error && error instanceof Error) {
1180
+ if (typeof error.stack === 'string') {
1181
+ error.stack = error.stack + NewLine + currentStack;
1182
+ }
1183
+ return error;
1184
+ }
1185
+ if (error && error.code && error.code === MethodNotFound) {
1186
+ const restoredError = new JsonRpcError(error.message);
1187
+ const parentStack = getParentStack(error);
1188
+ restoredError.stack = parentStack + NewLine + currentStack;
1189
+ return restoredError;
1190
+ }
1191
+ if (error && error.message) {
1192
+ const restoredError = constructError(error.message, error.type, error.name);
1193
+ if (error.data) {
1194
+ if (error.data.stack && error.data.type && error.message) {
1195
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
1196
+ } else if (error.data.stack) {
1197
+ restoredError.stack = error.data.stack;
1206
1198
  }
1207
- },
1208
- diff(uid, modules, numbers) {
1209
- const {
1210
- newState,
1211
- oldState
1212
- } = states[uid];
1213
- const diffResult = [];
1214
- for (let i = 0; i < modules.length; i++) {
1215
- const fn = modules[i];
1216
- if (!fn(oldState, newState)) {
1217
- diffResult.push(numbers[i]);
1218
- }
1199
+ if (error.data.codeFrame) {
1200
+ // @ts-ignore
1201
+ restoredError.codeFrame = error.data.codeFrame;
1202
+ }
1203
+ if (error.data.code) {
1204
+ // @ts-ignore
1205
+ restoredError.code = error.data.code;
1206
+ }
1207
+ if (error.data.type) {
1208
+ // @ts-ignore
1209
+ restoredError.name = error.data.type;
1210
+ }
1211
+ } else {
1212
+ if (error.stack) {
1213
+ const lowerStack = restoredError.stack || '';
1214
+ // @ts-ignore
1215
+ const indexNewLine = getNewLineIndex(lowerStack);
1216
+ const parentStack = getParentStack(error);
1217
+ // @ts-ignore
1218
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
1219
+ }
1220
+ if (error.codeFrame) {
1221
+ // @ts-ignore
1222
+ restoredError.codeFrame = error.codeFrame;
1219
1223
  }
1220
- return diffResult;
1221
- },
1222
- dispose(uid) {
1223
- delete states[uid];
1224
- },
1225
- get(uid) {
1226
- return states[uid];
1227
- },
1228
- getCommandIds() {
1229
- const keys = Object.keys(commandMapRef);
1230
- const ids = keys.map(toCommandId);
1231
- return ids;
1232
- },
1233
- getKeys() {
1234
- return Object.keys(states).map(key => {
1235
- return Number.parseFloat(key);
1236
- });
1237
- },
1238
- registerCommands(commandMap) {
1239
- Object.assign(commandMapRef, commandMap);
1240
- },
1241
- set(uid, oldState, newState) {
1242
- states[uid] = {
1243
- newState,
1244
- oldState
1245
- };
1246
- },
1247
- wrapCommand(fn) {
1248
- const wrapped = async (uid, ...args) => {
1249
- const {
1250
- newState,
1251
- oldState
1252
- } = states[uid];
1253
- const newerState = await fn(newState, ...args);
1254
- if (oldState === newerState || newState === newerState) {
1255
- return;
1256
- }
1257
- const latestOld = states[uid];
1258
- const latestNew = {
1259
- ...latestOld.newState,
1260
- ...newerState
1261
- };
1262
- states[uid] = {
1263
- newState: latestNew,
1264
- oldState: latestOld.oldState
1265
- };
1266
- };
1267
- return wrapped;
1268
- },
1269
- wrapGetter(fn) {
1270
- const wrapped = (uid, ...args) => {
1271
- const {
1272
- newState
1273
- } = states[uid];
1274
- return fn(newState, ...args);
1275
- };
1276
- return wrapped;
1277
- },
1278
- wrapLoadContent(fn) {
1279
- const wrapped = async (uid, ...args) => {
1280
- const {
1281
- newState,
1282
- oldState
1283
- } = states[uid];
1284
- const result = await fn(newState, ...args);
1285
- const {
1286
- error,
1287
- state
1288
- } = result;
1289
- if (oldState === state || newState === state) {
1290
- return {
1291
- error
1292
- };
1293
- }
1294
- const latestOld = states[uid];
1295
- const latestNew = {
1296
- ...latestOld.newState,
1297
- ...state
1298
- };
1299
- states[uid] = {
1300
- newState: latestNew,
1301
- oldState: latestOld.oldState
1302
- };
1303
- return {
1304
- error
1305
- };
1306
- };
1307
- return wrapped;
1308
1224
  }
1309
- };
1225
+ return restoredError;
1226
+ }
1227
+ if (typeof error === 'string') {
1228
+ return new Error(`JsonRpc Error: ${error}`);
1229
+ }
1230
+ return new Error(`JsonRpc Error: ${error}`);
1310
1231
  };
1311
- const terminate = () => {
1312
- globalThis.close();
1232
+ const unwrapJsonRpcResult = responseMessage => {
1233
+ if ('error' in responseMessage) {
1234
+ const restoredError = restoreJsonRpcError(responseMessage.error);
1235
+ throw restoredError;
1236
+ }
1237
+ if ('result' in responseMessage) {
1238
+ return responseMessage.result;
1239
+ }
1240
+ throw new JsonRpcError('unexpected response message');
1313
1241
  };
1314
-
1315
- const {
1316
- diff,
1317
- get,
1318
- getCommandIds,
1319
- registerCommands,
1320
- set,
1321
- wrapCommand,
1322
- wrapGetter
1323
- } = create$1();
1324
-
1325
- // TODO parentUid might ot be needed
1326
- const create = (id, uri, x, y, width, height, args, parentUid, platform = 0) => {
1327
- const state = {
1328
- accountEnabled: true,
1329
- activityBarItems: [],
1330
- currentViewletId: '',
1331
- filteredItems: [],
1332
- focus: 0,
1333
- focused: false,
1334
- focusedIndex: -1,
1335
- height,
1336
- initial: true,
1337
- itemHeight: 48,
1338
- numberOfVisibleItems: 0,
1339
- platform,
1340
- scrollBarHeight: 0,
1341
- selectedIndex: -1,
1342
- sideBarLocation: 0,
1343
- sideBarVisible: false,
1344
- uid: id,
1345
- updateProgress: 0,
1346
- updateState: '',
1347
- userLoginState: 'logged out',
1348
- width,
1349
- x,
1350
- y
1351
- };
1352
- set(id, state, state);
1353
- return state;
1242
+ const warn = (...args) => {
1243
+ console.warn(...args);
1354
1244
  };
1355
-
1356
- const isEqual$2 = (oldState, newState) => {
1357
- return oldState.itemHeight === newState.itemHeight;
1245
+ const resolve = (id, response) => {
1246
+ const fn = get$1(id);
1247
+ if (!fn) {
1248
+ console.log(response);
1249
+ warn(`callback ${id} may already be disposed`);
1250
+ return;
1251
+ }
1252
+ fn(response);
1253
+ remove$1(id);
1358
1254
  };
1359
-
1360
- const isEqual$1 = (oldState, newState) => {
1361
- return oldState.focused === newState.focused && oldState.focus === newState.focus;
1255
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
1256
+ const getErrorType = prettyError => {
1257
+ if (prettyError && prettyError.type) {
1258
+ return prettyError.type;
1259
+ }
1260
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
1261
+ return prettyError.constructor.name;
1262
+ }
1263
+ return undefined;
1362
1264
  };
1363
-
1364
- const isEqual = (oldState, newState) => {
1365
- return oldState.activityBarItems === newState.activityBarItems && oldState.filteredItems === newState.filteredItems && oldState.focusedIndex === newState.focusedIndex && oldState.sideBarLocation === newState.sideBarLocation && oldState.sideBarVisible === newState.sideBarVisible && oldState.updateProgress === newState.updateProgress && oldState.updateState === newState.updateState;
1265
+ const isAlreadyStack = line => {
1266
+ return line.trim().startsWith('at ');
1366
1267
  };
1367
-
1368
- const RenderItems = 4;
1369
- const RenderFocus = 6;
1370
- const RenderFocusContext = 7;
1371
- const RenderCss = 11;
1372
- const RenderIncremental = 12;
1373
-
1374
- const modules = [isEqual, isEqual$1, isEqual$1, isEqual$2];
1375
- const numbers = [RenderIncremental, RenderFocus, RenderFocusContext, RenderCss];
1376
-
1377
- const diff2 = uid => {
1378
- return diff(uid, modules, numbers);
1268
+ const getStack = prettyError => {
1269
+ const stackString = prettyError.stack || '';
1270
+ const newLineIndex = stackString.indexOf('\n');
1271
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
1272
+ return stackString.slice(newLineIndex + 1);
1273
+ }
1274
+ return stackString;
1379
1275
  };
1380
-
1381
- const List = 1;
1382
-
1383
- const focus = state => {
1384
- const {
1385
- focus
1386
- } = state;
1387
- if (focus) {
1388
- return state;
1276
+ const getErrorProperty = (error, prettyError) => {
1277
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
1278
+ return {
1279
+ code: MethodNotFound,
1280
+ data: error.stack,
1281
+ message: error.message
1282
+ };
1389
1283
  }
1390
1284
  return {
1391
- ...state,
1392
- focus: List
1285
+ code: Custom,
1286
+ data: {
1287
+ code: prettyError.code,
1288
+ codeFrame: prettyError.codeFrame,
1289
+ name: prettyError.name,
1290
+ stack: getStack(prettyError),
1291
+ type: getErrorType(prettyError)
1292
+ },
1293
+ message: prettyError.message
1393
1294
  };
1394
1295
  };
1395
-
1396
- const focusIndex = (state, index) => {
1296
+ const create$1$1 = (id, error) => {
1397
1297
  return {
1398
- ...state,
1399
- focused: true,
1400
- focusedIndex: index
1298
+ error,
1299
+ id,
1300
+ jsonrpc: Two$1
1401
1301
  };
1402
1302
  };
1403
-
1404
- const focusFirst = state => {
1405
- return focusIndex(state, -1);
1406
- };
1407
-
1408
- const focusLast = state => {
1409
- return focusIndex(state, -1);
1410
- };
1411
-
1412
- const focusNext = state => {
1413
- return focusIndex(state, -1);
1414
- };
1415
-
1416
- const focusNone = state => {
1417
- const {
1418
- focusedIndex
1419
- } = state;
1420
- if (focusedIndex === -1) {
1421
- return state;
1422
- }
1423
- return focusIndex(state, -1);
1303
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
1304
+ const prettyError = preparePrettyError(error);
1305
+ logError(error, prettyError);
1306
+ const errorProperty = getErrorProperty(error, prettyError);
1307
+ return create$1$1(id, errorProperty);
1424
1308
  };
1425
-
1426
- const getKeyBindings = () => {
1427
- return [{
1428
- command: 'ActivityBar.focusNext',
1429
- key: DownArrow,
1430
- when: FocusActivityBar
1431
- }, {
1432
- command: 'ActivityBar.focusPrevious',
1433
- key: UpArrow,
1434
- when: FocusActivityBar
1435
- }, {
1436
- command: 'ActivityBar.focusFirst',
1437
- key: Home,
1438
- when: FocusActivityBar
1439
- }, {
1440
- command: 'ActivityBar.focusFirst',
1441
- key: PageUp,
1442
- when: FocusActivityBar
1443
- }, {
1444
- command: 'ActivityBar.focusLast',
1445
- key: PageDown,
1446
- when: FocusActivityBar
1447
- }, {
1448
- command: 'ActivityBar.focusLast',
1449
- key: End,
1450
- when: FocusActivityBar
1451
- }, {
1452
- command: 'ActivityBar.selectCurrent',
1453
- key: Space,
1454
- when: FocusActivityBar
1455
- }, {
1456
- command: 'ActivityBar.selectCurrent',
1457
- key: Enter,
1458
- when: FocusActivityBar
1459
- }];
1309
+ const create$8 = (message, result) => {
1310
+ return {
1311
+ id: message.id,
1312
+ jsonrpc: Two$1,
1313
+ result: result ?? null
1314
+ };
1460
1315
  };
1461
-
1462
- const getMenuEntriesAccountLoggedIn = state => {
1463
- const {
1464
- userLoginState
1465
- } = state;
1466
- const signOutLabel = userLoginState === 'logging out' ? 'Signing Out...' : 'Sign Out';
1467
- return [{
1468
- command: 'ActivityBar.handleClickSignOut',
1469
- flags: None,
1470
- id: 'signOut',
1471
- label: signOutLabel
1472
- }];
1316
+ const getSuccessResponse = (message, result) => {
1317
+ const resultProperty = result ?? null;
1318
+ return create$8(message, resultProperty);
1473
1319
  };
1474
- const getMenuEntriesAccountLoggedOut = state => {
1475
- const {
1476
- userLoginState
1477
- } = state;
1478
- const signInLabel = userLoginState === 'logging in' ? 'Signing In...' : 'Sign In';
1479
- return [{
1480
- command: 'ActivityBar.handleClickSignIn',
1481
- flags: None,
1482
- id: 'signIn',
1483
- label: signInLabel
1484
- }];
1320
+ const getErrorResponseSimple = (id, error) => {
1321
+ return {
1322
+ error: {
1323
+ code: Custom,
1324
+ data: error,
1325
+ // @ts-ignore
1326
+ message: error.message
1327
+ },
1328
+ id,
1329
+ jsonrpc: Two$1
1330
+ };
1485
1331
  };
1486
- const getMenuEntriesAccount = state => {
1487
- // TODO maybe query it now from layout?
1488
- const {
1489
- userLoginState
1490
- } = state;
1491
- if (userLoginState === 'logged in' || userLoginState === 'logging out') {
1492
- return getMenuEntriesAccountLoggedIn(state);
1332
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
1333
+ try {
1334
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
1335
+ return getSuccessResponse(message, result);
1336
+ } catch (error) {
1337
+ if (ipc.canUseSimpleErrorResponse) {
1338
+ return getErrorResponseSimple(message.id, error);
1339
+ }
1340
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
1493
1341
  }
1494
- return getMenuEntriesAccountLoggedOut(state);
1495
1342
  };
1343
+ const defaultPreparePrettyError = error => {
1344
+ return error;
1345
+ };
1346
+ const defaultLogError = () => {
1347
+ // ignore
1348
+ };
1349
+ const defaultRequiresSocket = () => {
1350
+ return false;
1351
+ };
1352
+ const defaultResolve = resolve;
1496
1353
 
1497
- const Tab = 1;
1498
- const Button = 1 << 1;
1499
- const Progress = 1 << 2;
1500
- const Enabled = 1 << 3;
1501
- const Selected = 1 << 4;
1502
- const Focused = 1 << 5;
1503
- const MarginTop = 1 << 6;
1504
-
1505
- const emptyObject = {};
1506
- const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
1507
- const i18nString = (key, placeholders = emptyObject) => {
1508
- if (placeholders === emptyObject) {
1509
- return key;
1354
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
1355
+ const normalizeParams = args => {
1356
+ if (args.length === 1) {
1357
+ const options = args[0];
1358
+ return {
1359
+ execute: options.execute,
1360
+ ipc: options.ipc,
1361
+ logError: options.logError || defaultLogError,
1362
+ message: options.message,
1363
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
1364
+ requiresSocket: options.requiresSocket || defaultRequiresSocket,
1365
+ resolve: options.resolve || defaultResolve
1366
+ };
1510
1367
  }
1511
- const replacer = (match, rest) => {
1512
- return placeholders[rest];
1368
+ return {
1369
+ execute: args[2],
1370
+ ipc: args[0],
1371
+ logError: args[5],
1372
+ message: args[1],
1373
+ preparePrettyError: args[4],
1374
+ requiresSocket: args[6],
1375
+ resolve: args[3]
1513
1376
  };
1514
- return key.replaceAll(RE_PLACEHOLDER, replacer);
1377
+ };
1378
+ const handleJsonRpcMessage = async (...args) => {
1379
+ const options = normalizeParams(args);
1380
+ const {
1381
+ execute,
1382
+ ipc,
1383
+ logError,
1384
+ message,
1385
+ preparePrettyError,
1386
+ requiresSocket,
1387
+ resolve
1388
+ } = options;
1389
+ if ('id' in message) {
1390
+ if ('method' in message) {
1391
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
1392
+ try {
1393
+ ipc.send(response);
1394
+ } catch (error) {
1395
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
1396
+ ipc.send(errorResponse);
1397
+ }
1398
+ return;
1399
+ }
1400
+ resolve(message.id, message);
1401
+ return;
1402
+ }
1403
+ if ('method' in message) {
1404
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
1405
+ return;
1406
+ }
1407
+ throw new JsonRpcError('unexpected message');
1515
1408
  };
1516
1409
 
1517
- const Account$1 = 'Account';
1518
- const Explorer$1 = 'Explorer';
1519
- const Search$2 = 'Search';
1520
- const SourceControl$2 = 'Source Control';
1521
- const RunAndDebug$1 = 'Run and Debug';
1522
- const Extensions$2 = 'Extensions';
1523
- const Settings = 'Settings';
1524
- const AdditionalViews = 'Additional Views';
1525
- const ActivityBar$2 = 'Activity Bar';
1526
- const MoveSideBarLeft = 'Move Side Bar Left';
1527
- const MoveSideBarRight = 'Move Side Bar Right';
1528
- const HideActivityBar = 'Hide Activity Bar';
1529
- const CheckForUpdates = 'Check For Updates';
1530
- const ColorTheme = 'Color Theme';
1531
- const CommandPalette = 'Command Palette';
1532
- const KeyboardShortcuts = 'Keyboard Shortcuts';
1410
+ const Two = '2.0';
1533
1411
 
1534
- const account = () => {
1535
- return i18nString(Account$1);
1536
- };
1537
- const explorer = () => {
1538
- return i18nString(Explorer$1);
1412
+ const create$7 = (method, params) => {
1413
+ return {
1414
+ jsonrpc: Two,
1415
+ method,
1416
+ params
1417
+ };
1539
1418
  };
1540
- const search = () => {
1541
- return i18nString(Search$2);
1419
+
1420
+ const create$6 = (id, method, params) => {
1421
+ const message = {
1422
+ id,
1423
+ jsonrpc: Two,
1424
+ method,
1425
+ params
1426
+ };
1427
+ return message;
1542
1428
  };
1543
- const sourceControl = () => {
1544
- return i18nString(SourceControl$2);
1429
+
1430
+ let id = 0;
1431
+ const create$5 = () => {
1432
+ return ++id;
1545
1433
  };
1546
- const runAndDebug = () => {
1547
- return i18nString(RunAndDebug$1);
1434
+
1435
+ const registerPromise = map => {
1436
+ const id = create$5();
1437
+ const {
1438
+ promise,
1439
+ resolve
1440
+ } = Promise.withResolvers();
1441
+ map[id] = resolve;
1442
+ return {
1443
+ id,
1444
+ promise
1445
+ };
1548
1446
  };
1549
- const extensions = () => {
1550
- return i18nString(Extensions$2);
1447
+
1448
+ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
1449
+ const {
1450
+ id,
1451
+ promise
1452
+ } = registerPromise(callbacks);
1453
+ const message = create$6(id, method, params);
1454
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
1455
+ ipc.sendAndTransfer(message);
1456
+ } else {
1457
+ ipc.send(message);
1458
+ }
1459
+ const responseMessage = await promise;
1460
+ return unwrapJsonRpcResult(responseMessage);
1551
1461
  };
1552
- const settings = () => {
1553
- return i18nString(Settings);
1462
+ const createRpc = ipc => {
1463
+ const callbacks = Object.create(null);
1464
+ ipc._resolve = (id, response) => {
1465
+ const fn = callbacks[id];
1466
+ if (!fn) {
1467
+ console.warn(`callback ${id} may already be disposed`);
1468
+ return;
1469
+ }
1470
+ fn(response);
1471
+ delete callbacks[id];
1472
+ };
1473
+ const rpc = {
1474
+ async dispose() {
1475
+ await ipc?.dispose();
1476
+ },
1477
+ invoke(method, ...params) {
1478
+ return invokeHelper(callbacks, ipc, method, params, false);
1479
+ },
1480
+ invokeAndTransfer(method, ...params) {
1481
+ return invokeHelper(callbacks, ipc, method, params, true);
1482
+ },
1483
+ // @ts-ignore
1484
+ ipc,
1485
+ /**
1486
+ * @deprecated
1487
+ */
1488
+ send(method, ...params) {
1489
+ const message = create$7(method, params);
1490
+ ipc.send(message);
1491
+ }
1492
+ };
1493
+ return rpc;
1554
1494
  };
1555
- const additionalViews = () => {
1556
- return i18nString(AdditionalViews);
1495
+
1496
+ const requiresSocket = () => {
1497
+ return false;
1557
1498
  };
1558
- const activityBar = () => {
1559
- return i18nString(ActivityBar$2);
1499
+ const preparePrettyError = error => {
1500
+ return error;
1560
1501
  };
1561
- const moveSideBarRight = () => {
1562
- return i18nString(MoveSideBarRight);
1502
+ const logError = () => {
1503
+ // handled by renderer worker
1563
1504
  };
1564
- const moveSideBarLeft = () => {
1565
- return i18nString(MoveSideBarLeft);
1505
+ const handleMessage = event => {
1506
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
1507
+ const actualExecute = event?.target?.execute || execute;
1508
+ return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
1566
1509
  };
1567
- const hideActivityBar = () => {
1568
- return i18nString(HideActivityBar);
1510
+
1511
+ const handleIpc = ipc => {
1512
+ if ('addEventListener' in ipc) {
1513
+ ipc.addEventListener('message', handleMessage);
1514
+ } else if ('on' in ipc) {
1515
+ // deprecated
1516
+ ipc.on('message', handleMessage);
1517
+ }
1569
1518
  };
1570
- const checkForUpdates = () => {
1571
- return i18nString(CheckForUpdates);
1519
+
1520
+ const listen$1 = async (module, options) => {
1521
+ const rawIpc = await module.listen(options);
1522
+ if (module.signal) {
1523
+ module.signal(rawIpc);
1524
+ }
1525
+ const ipc = module.wrap(rawIpc);
1526
+ return ipc;
1572
1527
  };
1573
- const commandPalette = () => {
1574
- return i18nString(CommandPalette);
1528
+
1529
+ const create$4 = async ({
1530
+ commandMap,
1531
+ isMessagePortOpen = true,
1532
+ messagePort
1533
+ }) => {
1534
+ // TODO create a commandMap per rpc instance
1535
+ register(commandMap);
1536
+ const rawIpc = await IpcParentWithMessagePort$1.create({
1537
+ isMessagePortOpen,
1538
+ messagePort
1539
+ });
1540
+ const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
1541
+ handleIpc(ipc);
1542
+ const rpc = createRpc(ipc);
1543
+ messagePort.start();
1544
+ return rpc;
1575
1545
  };
1576
- const keyboardShortcuts = () => {
1577
- return i18nString(KeyboardShortcuts);
1546
+
1547
+ const create$3 = async ({
1548
+ commandMap,
1549
+ isMessagePortOpen,
1550
+ send
1551
+ }) => {
1552
+ const {
1553
+ port1,
1554
+ port2
1555
+ } = new MessageChannel();
1556
+ await send(port1);
1557
+ return create$4({
1558
+ commandMap,
1559
+ isMessagePortOpen,
1560
+ messagePort: port2
1561
+ });
1578
1562
  };
1579
- const colorTheme = () => {
1580
- return i18nString(ColorTheme);
1563
+
1564
+ const createSharedLazyRpc = factory => {
1565
+ let rpcPromise;
1566
+ const getOrCreate = () => {
1567
+ if (!rpcPromise) {
1568
+ rpcPromise = factory();
1569
+ }
1570
+ return rpcPromise;
1571
+ };
1572
+ return {
1573
+ async dispose() {
1574
+ const rpc = await getOrCreate();
1575
+ await rpc.dispose();
1576
+ },
1577
+ async invoke(method, ...params) {
1578
+ const rpc = await getOrCreate();
1579
+ return rpc.invoke(method, ...params);
1580
+ },
1581
+ async invokeAndTransfer(method, ...params) {
1582
+ const rpc = await getOrCreate();
1583
+ return rpc.invokeAndTransfer(method, ...params);
1584
+ },
1585
+ async send(method, ...params) {
1586
+ const rpc = await getOrCreate();
1587
+ rpc.send(method, ...params);
1588
+ }
1589
+ };
1581
1590
  };
1582
1591
 
1583
- const menuEntryMoveSideBar = sideBarLocation => {
1584
- switch (sideBarLocation) {
1585
- case Left:
1586
- return {
1587
- command: 'Layout.moveSideBarRight',
1588
- flags: None,
1589
- id: 'moveSideBarRight',
1590
- label: moveSideBarRight()
1591
- };
1592
- case Right:
1593
- return {
1594
- command: 'Layout.moveSideBarLeft',
1595
- flags: None,
1596
- id: 'moveSideBarLeft',
1597
- label: moveSideBarLeft()
1598
- };
1599
- default:
1600
- throw new Error('unexpected side bar location');
1601
- }
1592
+ const create$2 = async ({
1593
+ commandMap,
1594
+ isMessagePortOpen,
1595
+ send
1596
+ }) => {
1597
+ return createSharedLazyRpc(() => {
1598
+ return create$3({
1599
+ commandMap,
1600
+ isMessagePortOpen,
1601
+ send
1602
+ });
1603
+ });
1602
1604
  };
1603
1605
 
1604
- const menuEntrySeparator = {
1605
- command: '',
1606
- flags: Separator,
1607
- id: 'separator',
1608
- label: ''
1606
+ const create$1 = async ({
1607
+ commandMap
1608
+ }) => {
1609
+ // TODO create a commandMap per rpc instance
1610
+ register(commandMap);
1611
+ const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
1612
+ handleIpc(ipc);
1613
+ const rpc = createRpc(ipc);
1614
+ return rpc;
1609
1615
  };
1610
1616
 
1611
- const toContextMenuItem$1 = activityBarItem => {
1612
- const isEnabled = activityBarItem.flags & Enabled;
1613
- return {
1614
- args: [activityBarItem.id],
1615
- command: 'ActivityBar.toggleActivityBarItem',
1616
- flags: isEnabled ? Checked : Unchecked,
1617
- id: `toggle-${activityBarItem.id}`,
1618
- label: activityBarItem.id
1617
+ const createMockRpc = ({
1618
+ commandMap
1619
+ }) => {
1620
+ const invocations = [];
1621
+ const invoke = (method, ...params) => {
1622
+ invocations.push([method, ...params]);
1623
+ const command = commandMap[method];
1624
+ if (!command) {
1625
+ throw new Error(`command ${method} not found`);
1626
+ }
1627
+ return command(...params);
1628
+ };
1629
+ const mockRpc = {
1630
+ invocations,
1631
+ invoke,
1632
+ invokeAndTransfer: invoke
1619
1633
  };
1634
+ return mockRpc;
1620
1635
  };
1621
1636
 
1622
- const getMenuEntriesActivityBar = state => {
1623
- const {
1624
- activityBarItems,
1625
- sideBarLocation
1626
- } = state;
1627
- const topItems = activityBarItems.filter(item => !(item.flags & Button));
1628
- const bottomItems = activityBarItems.filter(item => item.flags & Button);
1629
- const entries = [...topItems.map(toContextMenuItem$1)];
1630
- if (bottomItems.length > 0) {
1631
- entries.push(menuEntrySeparator, ...bottomItems.map(toContextMenuItem$1));
1632
- }
1633
- return [...entries, menuEntrySeparator, menuEntryMoveSideBar(sideBarLocation), {
1634
- command: 'Layout.hideActivityBar',
1635
- flags: None,
1636
- id: 'hideActivityBar',
1637
- label: hideActivityBar()
1638
- }];
1637
+ const rpcs = Object.create(null);
1638
+ const set$3 = (id, rpc) => {
1639
+ rpcs[id] = rpc;
1639
1640
  };
1640
-
1641
- const getNumberOfVisibleItems = state => {
1642
- const {
1643
- height,
1644
- itemHeight
1645
- } = state;
1646
- const numberOfVisibleItemsTop = Math.floor(height / itemHeight);
1647
- return numberOfVisibleItemsTop;
1641
+ const get = id => {
1642
+ return rpcs[id];
1648
1643
  };
1649
- const getHiddenItems = state => {
1650
- const numberOfVisibleItems = getNumberOfVisibleItems(state);
1651
- const items = state.activityBarItems.filter(item => item.flags & Enabled);
1652
- if (numberOfVisibleItems >= items.length) {
1653
- return [];
1654
- }
1655
- return items.slice(numberOfVisibleItems - 2, -1);
1644
+ const remove = id => {
1645
+ delete rpcs[id];
1656
1646
  };
1657
1647
 
1658
- const toContextMenuItem = activityBarItem => {
1648
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1649
+ const create = rpcId => {
1659
1650
  return {
1660
- command: '-1',
1661
- // TODO
1662
- flags: None,
1663
- id: '8000',
1664
- // TODO
1665
- label: activityBarItem.id
1651
+ async dispose() {
1652
+ const rpc = get(rpcId);
1653
+ await rpc.dispose();
1654
+ },
1655
+ // @ts-ignore
1656
+ invoke(method, ...params) {
1657
+ const rpc = get(rpcId);
1658
+ // @ts-ignore
1659
+ return rpc.invoke(method, ...params);
1660
+ },
1661
+ // @ts-ignore
1662
+ invokeAndTransfer(method, ...params) {
1663
+ const rpc = get(rpcId);
1664
+ // @ts-ignore
1665
+ return rpc.invokeAndTransfer(method, ...params);
1666
+ },
1667
+ registerMockRpc(commandMap) {
1668
+ const mockRpc = createMockRpc({
1669
+ commandMap
1670
+ });
1671
+ set$3(rpcId, mockRpc);
1672
+ // @ts-ignore
1673
+ mockRpc[Symbol.dispose] = () => {
1674
+ remove(rpcId);
1675
+ };
1676
+ // @ts-ignore
1677
+ return mockRpc;
1678
+ },
1679
+ set(rpc) {
1680
+ set$3(rpcId, rpc);
1681
+ }
1666
1682
  };
1667
1683
  };
1668
- const getMenuEntriesAdditionalViews = state => {
1669
- const hiddenActivityBarItems = getHiddenItems(state);
1670
- return hiddenActivityBarItems.map(toContextMenuItem);
1671
- };
1672
1684
 
1673
- const keyBindingsUri = 'app://keybindings';
1674
- const getMenuEntriesSettings = state => {
1675
- const {
1676
- platform
1677
- } = state;
1678
- const items = [{
1679
- command: 'QuickPick.showEverything',
1680
- flags: None,
1681
- id: 'commandPalette',
1682
- label: commandPalette()
1683
- }, menuEntrySeparator, {
1684
- command: 'Preferences.openSettingsJson',
1685
- flags: None,
1686
- id: 'settings',
1687
- label: settings()
1688
- }, {
1689
- args: [keyBindingsUri],
1690
- command: 'Main.openUri',
1691
- flags: None,
1692
- id: 'keyboardShortcuts',
1693
- label: keyboardShortcuts()
1694
- }, {
1695
- command: 'QuickPick.showColorTheme',
1696
- flags: None,
1697
- id: 'colorTheme',
1698
- label: colorTheme()
1699
- }];
1700
- if (platform !== Web) {
1701
- items.push(menuEntrySeparator, {
1702
- command: 'AutoUpdater.checkForUpdates',
1703
- flags: None,
1704
- id: 'checkForUpdates',
1705
- label: checkForUpdates()
1706
- });
1707
- }
1708
- return items;
1685
+ const {
1686
+ set: set$2
1687
+ } = create(6010);
1688
+
1689
+ const {
1690
+ invoke: invoke$1,
1691
+ set: set$1
1692
+ } = create(ExtensionManagementWorker);
1693
+
1694
+ const {
1695
+ invoke,
1696
+ invokeAndTransfer,
1697
+ set
1698
+ } = create(RendererWorker);
1699
+ const showContextMenu2 = async (uid, menuId, x, y, args) => {
1700
+ number(uid);
1701
+ number(menuId);
1702
+ number(x);
1703
+ number(y);
1704
+ await invoke('ContextMenu.show2', uid, menuId, x, y, args);
1705
+ };
1706
+ const sendMessagePortToAuthWorker = async (port, rpcId) => {
1707
+ const command = 'HandleMessagePort.handleMessagePort';
1708
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToAuthWorker', port, command, rpcId);
1709
+ };
1710
+ const sendMessagePortToExtensionManagementWorker = async (port, rpcId) => {
1711
+ const command = 'Extensions.handleMessagePort';
1712
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionManagementWorker', port, command, rpcId);
1709
1713
  };
1710
1714
 
1711
1715
  const show2 = async (uid, menuId, x, y, args) => {
@@ -2011,11 +2015,11 @@ const handleContextMenu = async (state, button, eventX, eventY) => {
2011
2015
  const commandMap$1 = {};
2012
2016
 
2013
2017
  const handleExtensionManagementMessagePort = async port => {
2014
- const rpc = await create$6({
2018
+ const rpc = await create$4({
2015
2019
  commandMap: commandMap$1,
2016
2020
  messagePort: port
2017
2021
  });
2018
- set$2(rpc);
2022
+ set$1(rpc);
2019
2023
  };
2020
2024
 
2021
2025
  const Explorer = 'Explorer';
@@ -2909,8 +2913,8 @@ const render2 = (uid, diffResult) => {
2909
2913
  const {
2910
2914
  newState,
2911
2915
  oldState
2912
- } = get(uid);
2913
- set(uid, newState, newState);
2916
+ } = get$2(uid);
2917
+ set$4(uid, newState, newState);
2914
2918
  const commands = applyRender(oldState, newState, diffResult);
2915
2919
  return commands;
2916
2920
  };
@@ -2990,7 +2994,7 @@ const toggleActivityBarItem = async (state, itemId) => {
2990
2994
  };
2991
2995
 
2992
2996
  const commandMap = {
2993
- 'ActivityBar.create': create,
2997
+ 'ActivityBar.create': create$9,
2994
2998
  'ActivityBar.diff2': diff2,
2995
2999
  'ActivityBar.focus': wrapCommand(focus),
2996
3000
  'ActivityBar.focusFirst': wrapCommand(focusFirst),
@@ -3029,25 +3033,39 @@ const commandMap = {
3029
3033
  'ActivityBar.toggleActivityBarItem': wrapCommand(toggleActivityBarItem)
3030
3034
  };
3031
3035
 
3032
- const send = async port => {
3036
+ const send$1 = async port => {
3033
3037
  // @ts-ignore
3034
3038
  await sendMessagePortToAuthWorker(port);
3035
3039
  };
3036
3040
  const initializeAuthWorker = async () => {
3037
- const rpc = await create$4({
3041
+ const rpc = await create$2({
3042
+ commandMap: {},
3043
+ send: send$1
3044
+ });
3045
+ set$2(rpc);
3046
+ };
3047
+
3048
+ const send = async port => {
3049
+ await sendMessagePortToExtensionManagementWorker(port, 0);
3050
+ };
3051
+ const initializeExtensionManagementWorker = async () => {
3052
+ const rpc = await create$2({
3038
3053
  commandMap: {},
3039
3054
  send
3040
3055
  });
3041
- set$3(rpc);
3056
+ set$1(rpc);
3042
3057
  };
3043
3058
 
3044
- const listen = async () => {
3045
- registerCommands(commandMap);
3046
- const rpc = await create$3({
3059
+ const initializeRendererWorker = async () => {
3060
+ const rpc = await create$1({
3047
3061
  commandMap: commandMap
3048
3062
  });
3049
- set$1(rpc);
3050
- await initializeAuthWorker();
3063
+ set(rpc);
3064
+ };
3065
+
3066
+ const listen = async () => {
3067
+ registerCommands(commandMap);
3068
+ await Promise.all([initializeRendererWorker(), initializeAuthWorker(), initializeExtensionManagementWorker()]);
3051
3069
  };
3052
3070
 
3053
3071
  const main = async () => {