@theia/application-package 1.34.2 → 1.34.3

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.
package/src/json-file.ts CHANGED
@@ -1,25 +1,25 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import * as fs from 'fs';
18
- import writeJsonFile = require('write-json-file');
19
-
20
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- function readJsonFile(path: string): any {
22
- return JSON.parse(fs.readFileSync(path, { encoding: 'utf-8' }));
23
- }
24
-
25
- export { writeJsonFile, readJsonFile };
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import * as fs from 'fs';
18
+ import writeJsonFile = require('write-json-file');
19
+
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ function readJsonFile(path: string): any {
22
+ return JSON.parse(fs.readFileSync(path, { encoding: 'utf-8' }));
23
+ }
24
+
25
+ export { writeJsonFile, readJsonFile };
@@ -1,169 +1,169 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
- import * as request from 'request';
19
- import * as nano from 'nano';
20
- import { NpmRegistryProps } from './application-props';
21
-
22
- export interface IChangeStream {
23
- on(event: 'data', cb: (change: { id: string }) => void): void;
24
- destroy(): void;
25
- }
26
-
27
- export interface Author {
28
- name: string;
29
- email: string;
30
- }
31
-
32
- export interface Maintainer {
33
- username: string;
34
- email: string;
35
- }
36
-
37
- export interface Dependencies {
38
- [name: string]: string | undefined;
39
- }
40
-
41
- export interface NodePackage {
42
- name?: string;
43
- version?: string;
44
- description?: string;
45
- publisher?: Maintainer;
46
- author?: string | Author;
47
- maintainers?: Maintainer[];
48
- keywords?: string[];
49
- dependencies?: Dependencies;
50
- peerDependencies?: Dependencies;
51
- [property: string]: any;
52
- }
53
-
54
- export interface PublishedNodePackage extends NodePackage {
55
- name: string;
56
- version: string;
57
- }
58
- export namespace PublishedNodePackage {
59
- export function is(pck: NodePackage | undefined): pck is PublishedNodePackage {
60
- return !!pck && !!pck.name && !!pck.version;
61
- }
62
- }
63
-
64
- export interface ViewResult {
65
- 'dist-tags': {
66
- [tag: string]: string
67
- }
68
- 'versions': {
69
- [version: string]: NodePackage
70
- },
71
- 'readme': string;
72
- [key: string]: any
73
- }
74
-
75
- export function sortByKey(object: { [key: string]: any }): {
76
- [key: string]: any;
77
- } {
78
- return Object.keys(object).sort().reduce((sorted, key) => {
79
- sorted[key] = object[key];
80
- return sorted;
81
- }, {} as { [key: string]: any });
82
- }
83
-
84
- export class NpmRegistryOptions {
85
- /**
86
- * Default: false.
87
- */
88
- readonly watchChanges: boolean;
89
- }
90
-
91
- export class NpmRegistry {
92
-
93
- readonly props: NpmRegistryProps = { ...NpmRegistryProps.DEFAULT };
94
- protected readonly options: NpmRegistryOptions;
95
-
96
- protected changes?: nano.ChangesReaderScope;
97
- protected readonly index = new Map<string, Promise<ViewResult>>();
98
-
99
- constructor(options?: Partial<NpmRegistryOptions>) {
100
- this.options = {
101
- watchChanges: false,
102
- ...options
103
- };
104
- this.resetIndex();
105
- }
106
-
107
- updateProps(props?: Partial<NpmRegistryProps>): void {
108
- const oldRegistry = this.props.registry;
109
- Object.assign(this.props, props);
110
- const newRegistry = this.props.registry;
111
- if (oldRegistry !== newRegistry) {
112
- this.resetIndex();
113
- }
114
- }
115
- protected resetIndex(): void {
116
- this.index.clear();
117
- if (this.options.watchChanges && this.props.registry === NpmRegistryProps.DEFAULT.registry) {
118
- if (this.changes) {
119
- this.changes.stop();
120
- }
121
- // Invalidate index with NPM registry web hooks
122
- this.changes = nano('https://replicate.npmjs.com').use('registry').changesReader;
123
- this.changes.get({}).on('change', change => this.invalidate(change.id));
124
- }
125
- }
126
- protected invalidate(name: string): void {
127
- if (this.index.delete(name)) {
128
- this.view(name);
129
- }
130
- }
131
-
132
- view(name: string): Promise<ViewResult> {
133
- const indexed = this.index.get(name);
134
- if (indexed) {
135
- return indexed;
136
- }
137
- const result = this.doView(name);
138
- this.index.set(name, result);
139
- result.catch(() => this.index.delete(name));
140
- return result;
141
- }
142
-
143
- protected doView(name: string): Promise<ViewResult> {
144
- let url = this.props.registry;
145
- if (name[0] === '@') {
146
- url += '@' + encodeURIComponent(name.substr(1));
147
- } else {
148
- url += encodeURIComponent(name);
149
- }
150
- const headers: {
151
- [header: string]: string
152
- } = {};
153
- return new Promise((resolve, reject) => {
154
- request({
155
- url, headers
156
- }, (err, response, body) => {
157
- if (err) {
158
- reject(err);
159
- } else if (response.statusCode !== 200) {
160
- reject(new Error(`${response.statusCode}: ${response.statusMessage} for ${url}`));
161
- } else {
162
- const data = JSON.parse(body);
163
- resolve(data);
164
- }
165
- });
166
- });
167
- }
168
-
169
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /* eslint-disable @typescript-eslint/no-explicit-any */
18
+ import * as request from 'request';
19
+ import * as nano from 'nano';
20
+ import { NpmRegistryProps } from './application-props';
21
+
22
+ export interface IChangeStream {
23
+ on(event: 'data', cb: (change: { id: string }) => void): void;
24
+ destroy(): void;
25
+ }
26
+
27
+ export interface Author {
28
+ name: string;
29
+ email: string;
30
+ }
31
+
32
+ export interface Maintainer {
33
+ username: string;
34
+ email: string;
35
+ }
36
+
37
+ export interface Dependencies {
38
+ [name: string]: string | undefined;
39
+ }
40
+
41
+ export interface NodePackage {
42
+ name?: string;
43
+ version?: string;
44
+ description?: string;
45
+ publisher?: Maintainer;
46
+ author?: string | Author;
47
+ maintainers?: Maintainer[];
48
+ keywords?: string[];
49
+ dependencies?: Dependencies;
50
+ peerDependencies?: Dependencies;
51
+ [property: string]: any;
52
+ }
53
+
54
+ export interface PublishedNodePackage extends NodePackage {
55
+ name: string;
56
+ version: string;
57
+ }
58
+ export namespace PublishedNodePackage {
59
+ export function is(pck: NodePackage | undefined): pck is PublishedNodePackage {
60
+ return !!pck && !!pck.name && !!pck.version;
61
+ }
62
+ }
63
+
64
+ export interface ViewResult {
65
+ 'dist-tags': {
66
+ [tag: string]: string
67
+ }
68
+ 'versions': {
69
+ [version: string]: NodePackage
70
+ },
71
+ 'readme': string;
72
+ [key: string]: any
73
+ }
74
+
75
+ export function sortByKey(object: { [key: string]: any }): {
76
+ [key: string]: any;
77
+ } {
78
+ return Object.keys(object).sort().reduce((sorted, key) => {
79
+ sorted[key] = object[key];
80
+ return sorted;
81
+ }, {} as { [key: string]: any });
82
+ }
83
+
84
+ export class NpmRegistryOptions {
85
+ /**
86
+ * Default: false.
87
+ */
88
+ readonly watchChanges: boolean;
89
+ }
90
+
91
+ export class NpmRegistry {
92
+
93
+ readonly props: NpmRegistryProps = { ...NpmRegistryProps.DEFAULT };
94
+ protected readonly options: NpmRegistryOptions;
95
+
96
+ protected changes?: nano.ChangesReaderScope;
97
+ protected readonly index = new Map<string, Promise<ViewResult>>();
98
+
99
+ constructor(options?: Partial<NpmRegistryOptions>) {
100
+ this.options = {
101
+ watchChanges: false,
102
+ ...options
103
+ };
104
+ this.resetIndex();
105
+ }
106
+
107
+ updateProps(props?: Partial<NpmRegistryProps>): void {
108
+ const oldRegistry = this.props.registry;
109
+ Object.assign(this.props, props);
110
+ const newRegistry = this.props.registry;
111
+ if (oldRegistry !== newRegistry) {
112
+ this.resetIndex();
113
+ }
114
+ }
115
+ protected resetIndex(): void {
116
+ this.index.clear();
117
+ if (this.options.watchChanges && this.props.registry === NpmRegistryProps.DEFAULT.registry) {
118
+ if (this.changes) {
119
+ this.changes.stop();
120
+ }
121
+ // Invalidate index with NPM registry web hooks
122
+ this.changes = nano('https://replicate.npmjs.com').use('registry').changesReader;
123
+ this.changes.get({}).on('change', change => this.invalidate(change.id));
124
+ }
125
+ }
126
+ protected invalidate(name: string): void {
127
+ if (this.index.delete(name)) {
128
+ this.view(name);
129
+ }
130
+ }
131
+
132
+ view(name: string): Promise<ViewResult> {
133
+ const indexed = this.index.get(name);
134
+ if (indexed) {
135
+ return indexed;
136
+ }
137
+ const result = this.doView(name);
138
+ this.index.set(name, result);
139
+ result.catch(() => this.index.delete(name));
140
+ return result;
141
+ }
142
+
143
+ protected doView(name: string): Promise<ViewResult> {
144
+ let url = this.props.registry;
145
+ if (name[0] === '@') {
146
+ url += '@' + encodeURIComponent(name.substr(1));
147
+ } else {
148
+ url += encodeURIComponent(name);
149
+ }
150
+ const headers: {
151
+ [header: string]: string
152
+ } = {};
153
+ return new Promise((resolve, reject) => {
154
+ request({
155
+ url, headers
156
+ }, (err, response, body) => {
157
+ if (err) {
158
+ reject(err);
159
+ } else if (response.statusCode !== 200) {
160
+ reject(new Error(`${response.statusCode}: ${response.statusMessage} for ${url}`));
161
+ } else {
162
+ const data = JSON.parse(body);
163
+ resolve(data);
164
+ }
165
+ });
166
+ });
167
+ }
168
+
169
+ }