@nordicsemiconductor/nrf-jlink-js 0.0.8 → 0.0.9

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.js CHANGED
@@ -44,7 +44,6 @@ var child_process_1 = require("child_process");
44
44
  var os_1 = __importDefault(require("os"));
45
45
  var path_1 = __importDefault(require("path"));
46
46
  var semver_1 = __importDefault(require("semver"));
47
- var axios_1 = __importDefault(require("axios"));
48
47
  var common_1 = require("./common");
49
48
  var getJLinkExePath = function () {
50
49
  switch (os_1.default.platform()) {
@@ -104,11 +103,11 @@ var getInstalledJLinkVersion = function () {
104
103
  });
105
104
  };
106
105
  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;
106
+ var platform, arch, url, response, hasContentLength, contentLength, reader, chunks, receivedLength, _c, done, value, chunksAll, position;
107
+ var _d, _e;
109
108
  var jlinkUrls = _b.jlinkUrls;
110
- return __generator(this, function (_e) {
111
- switch (_e.label) {
109
+ return __generator(this, function (_f) {
110
+ switch (_f.label) {
112
111
  case 0:
113
112
  platform = os_1.default.platform();
114
113
  arch = os_1.default.arch();
@@ -120,28 +119,50 @@ var downloadJLink = function (_a, onUpdate_1, destinationFilePath_1) { return __
120
119
  if (!url) {
121
120
  throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
122
121
  }
123
- return [4 /*yield*/, axios_1.default.get(url, {
124
- responseType: 'stream',
122
+ return [4 /*yield*/, fetch(url, {
125
123
  headers: {
126
124
  Range: 'bytes=0-',
127
125
  },
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
126
  })];
138
127
  case 1:
139
- _c = _e.sent(), status = _c.status, stream = _c.data;
140
- if (status !== 200 && status !== 206) {
128
+ response = _f.sent();
129
+ if (!response.ok) {
141
130
  throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
142
131
  }
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()];
132
+ hasContentLength = response.headers.has('content-length');
133
+ contentLength = hasContentLength
134
+ ? Number(response.headers.get('content-length'))
135
+ : 1;
136
+ reader = (_e = response.body) === null || _e === void 0 ? void 0 : _e.getReader();
137
+ chunks = [];
138
+ receivedLength = 0;
139
+ _f.label = 2;
140
+ case 2:
141
+ if (!reader) return [3 /*break*/, 4];
142
+ return [4 /*yield*/, reader.read()];
143
+ case 3:
144
+ _c = _f.sent(), done = _c.done, value = _c.value;
145
+ if (done) {
146
+ return [3 /*break*/, 4];
147
+ }
148
+ chunks.push(value);
149
+ receivedLength += value.length;
150
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
151
+ step: 'download',
152
+ percentage: hasContentLength
153
+ ? Number(((receivedLength / contentLength) * 100).toFixed(2))
154
+ : 0,
155
+ });
156
+ return [3 /*break*/, 2];
157
+ case 4:
158
+ chunksAll = new Uint8Array(receivedLength);
159
+ position = 0;
160
+ chunks.forEach(function (chunk) {
161
+ chunksAll.set(chunk, position);
162
+ position += chunk.length;
163
+ });
164
+ return [4 /*yield*/, (0, common_1.saveToFile)(destinationFilePath || path_1.default.join(os_1.default.tmpdir(), path_1.default.basename(url)), Buffer.from(chunksAll))];
165
+ case 5: return [2 /*return*/, _f.sent()];
145
166
  }
146
167
  });
147
168
  }); };
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.9",
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": {