@nordicsemiconductor/nrf-jlink-js 0.0.8 → 0.0.10

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/dist/common.d.ts CHANGED
@@ -7,4 +7,4 @@ export interface JLinkIndex {
7
7
  jlinkUrls: JLinkVariant;
8
8
  }
9
9
  export declare const fetchIndex: () => Promise<JLinkIndex>;
10
- export declare const saveToFile: (stream: NodeJS.ReadableStream, destinationFile: string) => Promise<string>;
10
+ export declare const saveToFile: (destinationFile: string, data: Buffer) => Promise<string>;
package/dist/common.js CHANGED
@@ -40,27 +40,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.saveToFile = exports.fetchIndex = exports.archs = exports.platforms = void 0;
43
- var axios_1 = __importDefault(require("axios"));
44
43
  var fs_1 = require("fs");
45
44
  var path_1 = __importDefault(require("path"));
46
45
  exports.platforms = ['darwin', 'linux', 'win32'];
47
46
  exports.archs = ['arm64', 'x64'];
48
47
  var fetchJSON = function (url) { return __awaiter(void 0, void 0, void 0, function () {
49
- var _a, status, data;
50
- return __generator(this, function (_b) {
51
- switch (_b.label) {
52
- case 0: return [4 /*yield*/, axios_1.default.get(url, {
53
- responseType: 'json',
48
+ var response;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0: return [4 /*yield*/, fetch(url, {
54
52
  headers: {
55
53
  Range: 'bytes=0-',
56
54
  },
57
55
  })];
58
56
  case 1:
59
- _a = _b.sent(), status = _a.status, data = _a.data;
60
- if (status !== 200 && status !== 206) {
57
+ response = _a.sent();
58
+ if (!response.ok) {
61
59
  throw new Error("Unable to fetch file from ".concat(indexUrl, ". Got status code ").concat(status, "."));
62
60
  }
63
- return [2 /*return*/, data];
61
+ return [2 /*return*/, response.json()];
64
62
  }
65
63
  });
66
64
  }); };
@@ -83,17 +81,16 @@ var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function
83
81
  });
84
82
  }); };
85
83
  exports.fetchIndex = fetchIndex;
86
- var saveToFile = function (stream, destinationFile) {
87
- (0, fs_1.mkdirSync)(path_1.default.dirname(destinationFile), { recursive: true });
88
- return new Promise(function (resolve, reject) {
89
- var file = (0, fs_1.createWriteStream)(destinationFile);
90
- stream.pipe(file);
91
- stream.on('error', reject);
92
- stream.on('end', function () {
93
- file.end(function () {
94
- return resolve(destinationFile);
95
- });
96
- });
84
+ var saveToFile = function (destinationFile, data) { return __awaiter(void 0, void 0, void 0, function () {
85
+ return __generator(this, function (_a) {
86
+ (0, fs_1.mkdirSync)(path_1.default.dirname(destinationFile), { recursive: true });
87
+ try {
88
+ (0, fs_1.writeFileSync)(destinationFile, data);
89
+ }
90
+ catch (e) {
91
+ throw new Error("Unable to write file to ".concat(destinationFile, ". Error: ").concat(e));
92
+ }
93
+ return [2 /*return*/, destinationFile];
97
94
  });
98
- };
95
+ }); };
99
96
  exports.saveToFile = saveToFile;
package/dist/jlink.d.ts CHANGED
@@ -10,6 +10,6 @@ interface JLinkState {
10
10
  installedVersion?: string;
11
11
  }
12
12
  export declare const getVersionToInstall: (fallbackVersion?: string) => Promise<JLinkState>;
13
- export declare const downloadAndSaveJLink: (destination: string, onUpdate?: (update: Update) => void) => Promise<string>;
13
+ export declare const downloadAndSaveJLink: (destinationDir: string, destinationFileName?: string, onUpdate?: (update: Update) => void) => Promise<string>;
14
14
  export declare const downloadAndInstallJLink: (onUpdate?: (update: Update) => void) => Promise<void>;
15
15
  export {};
package/dist/jlink.js CHANGED
@@ -35,6 +35,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
38
47
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
49
  };
@@ -44,7 +53,6 @@ var child_process_1 = require("child_process");
44
53
  var os_1 = __importDefault(require("os"));
45
54
  var path_1 = __importDefault(require("path"));
46
55
  var semver_1 = __importDefault(require("semver"));
47
- var axios_1 = __importDefault(require("axios"));
48
56
  var common_1 = require("./common");
49
57
  var getJLinkExePath = function () {
50
58
  switch (os_1.default.platform()) {
@@ -103,48 +111,77 @@ var getInstalledJLinkVersion = function () {
103
111
  });
104
112
  });
105
113
  };
106
- var downloadJLink = function (_a, onUpdate_1, destinationFilePath_1) { return __awaiter(void 0, [_a, onUpdate_1, destinationFilePath_1], void 0, function (_b, onUpdate, destinationFilePath) {
107
- var platform, arch, url, _c, status, stream;
108
- var _d;
109
- var jlinkUrls = _b.jlinkUrls;
110
- return __generator(this, function (_e) {
111
- switch (_e.label) {
112
- case 0:
113
- platform = os_1.default.platform();
114
- arch = os_1.default.arch();
115
- // @ts-expect-error It is quite literally checked right before
116
- if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
117
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
118
- }
119
- url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
120
- if (!url) {
121
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
122
- }
123
- return [4 /*yield*/, axios_1.default.get(url, {
124
- responseType: 'stream',
125
- headers: {
126
- Range: 'bytes=0-',
127
- },
128
- onDownloadProgress: function (_a) {
129
- var loaded = _a.loaded, total = _a.total;
130
- return loaded &&
131
- total &&
132
- (onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
133
- step: 'download',
134
- percentage: Number(((loaded / total) * 100).toFixed(2)),
135
- }));
136
- },
137
- })];
138
- case 1:
139
- _c = _e.sent(), status = _c.status, stream = _c.data;
140
- if (status !== 200 && status !== 206) {
141
- throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
142
- }
143
- return [4 /*yield*/, (0, common_1.saveToFile)(stream, destinationFilePath || path_1.default.join(os_1.default.tmpdir(), path_1.default.basename(url)))];
144
- case 2: return [2 /*return*/, _e.sent()];
145
- }
114
+ var downloadJLink = function (_a, onUpdate_1) {
115
+ var args_1 = [];
116
+ for (var _i = 2; _i < arguments.length; _i++) {
117
+ args_1[_i - 2] = arguments[_i];
118
+ }
119
+ return __awaiter(void 0, __spreadArray([_a, onUpdate_1], args_1, true), void 0, function (_b, onUpdate, destinationDir, destinationFileName) {
120
+ var platform, arch, url, response, hasContentLength, contentLength, reader, chunks, receivedLength, _c, done, value, chunksAll, position;
121
+ var _d, _e;
122
+ var jlinkUrls = _b.jlinkUrls;
123
+ if (destinationDir === void 0) { destinationDir = os_1.default.tmpdir(); }
124
+ return __generator(this, function (_f) {
125
+ switch (_f.label) {
126
+ case 0:
127
+ platform = os_1.default.platform();
128
+ arch = os_1.default.arch();
129
+ // @ts-expect-error It is quite literally checked right before
130
+ if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
131
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
132
+ }
133
+ url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
134
+ if (!url) {
135
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
136
+ }
137
+ return [4 /*yield*/, fetch(url, {
138
+ headers: {
139
+ Range: 'bytes=0-',
140
+ },
141
+ })];
142
+ case 1:
143
+ response = _f.sent();
144
+ if (!response.ok) {
145
+ throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
146
+ }
147
+ hasContentLength = response.headers.has('content-length');
148
+ contentLength = hasContentLength
149
+ ? Number(response.headers.get('content-length'))
150
+ : 1;
151
+ reader = (_e = response.body) === null || _e === void 0 ? void 0 : _e.getReader();
152
+ chunks = [];
153
+ receivedLength = 0;
154
+ _f.label = 2;
155
+ case 2:
156
+ if (!reader) return [3 /*break*/, 4];
157
+ return [4 /*yield*/, reader.read()];
158
+ case 3:
159
+ _c = _f.sent(), done = _c.done, value = _c.value;
160
+ if (done) {
161
+ return [3 /*break*/, 4];
162
+ }
163
+ chunks.push(value);
164
+ receivedLength += value.length;
165
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
166
+ step: 'download',
167
+ percentage: hasContentLength
168
+ ? Number(((receivedLength / contentLength) * 100).toFixed(2))
169
+ : 0,
170
+ });
171
+ return [3 /*break*/, 2];
172
+ case 4:
173
+ chunksAll = new Uint8Array(receivedLength);
174
+ position = 0;
175
+ chunks.forEach(function (chunk) {
176
+ chunksAll.set(chunk, position);
177
+ position += chunk.length;
178
+ });
179
+ return [4 /*yield*/, (0, common_1.saveToFile)(path_1.default.join(destinationDir, destinationFileName || path_1.default.basename(url)), Buffer.from(chunksAll))];
180
+ case 5: return [2 /*return*/, _f.sent()];
181
+ }
182
+ });
146
183
  });
147
- }); };
184
+ };
148
185
  var installJLink = function (installerPath, onUpdate) {
149
186
  var command;
150
187
  var args;
@@ -214,7 +251,7 @@ var getVersionToInstall = function (fallbackVersion) { return __awaiter(void 0,
214
251
  });
215
252
  }); };
216
253
  exports.getVersionToInstall = getVersionToInstall;
217
- var downloadAndSaveJLink = function (destination, onUpdate) { return (0, common_1.fetchIndex)().then(function (v) { return downloadJLink(v, onUpdate, destination); }); };
254
+ var downloadAndSaveJLink = function (destinationDir, destinationFileName, onUpdate) { return (0, common_1.fetchIndex)().then(function (v) { return downloadJLink(v, onUpdate, destinationDir, destinationFileName); }); };
218
255
  exports.downloadAndSaveJLink = downloadAndSaveJLink;
219
256
  var downloadAndInstallJLink = function (onUpdate) {
220
257
  return (0, common_1.fetchIndex)()
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@nordicsemiconductor/nrf-jlink-js",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "main": "dist",
5
5
  "types": "dist",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
9
9
  "dependencies": {
10
- "axios": "^1.7.9",
11
10
  "semver": "^7.7.2"
12
11
  },
13
12
  "scripts": {