Skip to content

Commit

Permalink
CW Issue #2189 - Remove deprecated request module from NodeJS/Types…
Browse files Browse the repository at this point in the history
…cript code. (#63)
  • Loading branch information
jgwest authored Mar 18, 2020
1 parent a8c0169 commit 85a9f38
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 964 deletions.
647 changes: 271 additions & 376 deletions Filewatcherd-TypeScript/package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions Filewatcherd-TypeScript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codewind-filewatcher",
"version": "0.9.0",
"version": "0.11.0",
"description": "File change notification client that recursively monitors project directories and notifies the Codewind server of file changes",
"repository": "https://github.com/eclipse/codewind-filewatchers/",
"license": "EPL-2.0",
Expand All @@ -22,15 +22,13 @@
},
"dependencies": {
"chokidar": "^3.0.2",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"got": "^10.6.0",
"ws": "^6.2.1"
},
"devDependencies": {
"@types/node": "^10.12.1",
"@types/request": "^2.48.2",
"@types/request-promise-native": "^1.0.16",
"@types/ws": "^6.0.2",
"@types/got": "^9.6.9",
"concurrently": "^3.6.1",
"nodemon": "^1.18.11",
"tslint": "^5.16.0",
Expand Down
9 changes: 9 additions & 0 deletions Filewatcherd-TypeScript/run-dev.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

@echo off

set CODEWIND_URL_ROOT=http://localhost:9090

rem REPLACE THIS reference to MockCwctlSync JAR, when running via this batch file.
set MOCK_CWCTL_INSTALLER_PATH=c:\Codewind\Git\codewind-filewatchers\Tests\MockCwctlSync\target\MockCwctlSync-0.0.1-SNAPSHOT.jar

npm run watch
7 changes: 7 additions & 0 deletions Filewatcherd-TypeScript/run-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

CODEWIND_URL_ROOT=http://localhost:9090

export MOCK_CWCTL_INSTALLER_PATH=`cd ../;pwd`/Tests/MockCwctlSync/target/MockCwctlSync-0.0.1-SNAPSHOT.jar

npm run watch
46 changes: 2 additions & 44 deletions Filewatcherd-TypeScript/src/lib/FileChangeEventBatchUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ export class FileChangeEventBatchUtil {

private readonly _projectId: string;

private readonly DISABLE_CWCTL_CLI_SYNC: boolean; // Enable this for debugging purposes.

public constructor(projectId: string, parent: FileWatcher) {
this._parent = parent;
this._files = new Array<ChangedFileEntry>();
this._projectId = projectId;

const debugEnvVar = process.env.DISABLE_CWCTL_CLI_SYNC;

this.DISABLE_CWCTL_CLI_SYNC = debugEnvVar && debugEnvVar === "true";
}

/**
Expand Down Expand Up @@ -146,45 +141,8 @@ export class FileChangeEventBatchUtil {
log.info("Batch change summary for " + this._projectId + " @ "
+ mostRecentTimestamp.timestamp + ": " + eventSummary);

if (!this.DISABLE_CWCTL_CLI_SYNC) {
// Use CWCTL CLI sync command
this._parent.informCwctlOfFileChangesAsync(this._projectId);

} else {

// Use the old way of communicating file values.

// TODO: Remove this entire else block once CWCTL sync is mature.

// Split the entries into requests, ensure that each request is no larger
// then a given size.
const fileListsToSend = new Array<IChangedFileEntryJson[]>();
while (entries.length > 0) {
const currList: IChangedFileEntryJson[] = new Array<IChangedFileEntryJson>();
while (currList.length < FileChangeEventBatchUtil.MAX_REQUEST_SIZE_IN_PATHS && entries.length > 0) {
const nextPath = entries.splice(0, 1);

currList.push(nextPath[0].toJson());
}

if (currList.length > 0) {
fileListsToSend.push(currList);
}
}

const base64Compressed = new Array<string>();
for (const array of fileListsToSend) {
const str = JSON.stringify(array);
// log.debug("JSON contents: " + str);
const strBuffer = zlib.deflateSync(str);
base64Compressed.push(strBuffer.toString("base64"));
}

if (base64Compressed.length > 0) {
this._parent.sendBulkFileChanges(this._projectId, mostRecentTimestamp.timestamp, base64Compressed);
}

}
// Use CWCTL CLI sync command
this._parent.informCwctlOfFileChangesAsync(this._projectId);

}

Expand Down
53 changes: 20 additions & 33 deletions Filewatcherd-TypeScript/src/lib/FileWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* Copyright (c) 2019, 2020 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -17,12 +17,12 @@ import { ProjectToWatch } from "./ProjectToWatch";
import { WatchEventEntry } from "./WatchEventEntry";

import { ChangedFileEntry } from "./ChangedFileEntry";
import { HttpPostOutputQueue } from "./HttpPostOutputQueue";
import { PathFilter } from "./PathFilter";
import { ProjectToWatchFromWebSocket } from "./ProjectToWatchFromWebSocket";
import { WebSocketManagerThread } from "./WebSocketManagerThread";

import * as request from "request-promise-native";
import got from "got";

import { AuthTokenWrapper } from "./AuthTokenWrapper";
import { DebugTimer } from "./DebugTimer";
import { ExponentialBackoffUtil } from "./ExponentialBackoffUtil";
Expand Down Expand Up @@ -51,8 +51,6 @@ export class FileWatcher {
private readonly _internalWatchService: IWatchService;
private readonly _externalWatchService: IWatchService;

private readonly _outputQueue: HttpPostOutputQueue;

private readonly _webSocketManager: WebSocketManagerThread;

private readonly _clientUuid: string;
Expand Down Expand Up @@ -84,12 +82,14 @@ export class FileWatcher {

this._projectsMap = new Map<string, ProjectObject>();

if (!urlParam || urlParam.trim().length === 0) {
throw new Error("No URL specified; if running standalone then set CODEWIND_URL_ROOT");
}

this._baseUrl = PathUtils.stripTrailingSlash(urlParam);

this._individualFileWatchService = new IndividualFileWatchService(this);

this._outputQueue = new HttpPostOutputQueue(this._baseUrl, this._authTokenWrapper);

let calculatedWsUrl = this._baseUrl;
calculatedWsUrl = calculatedWsUrl.replace("http://", "ws://");
calculatedWsUrl = calculatedWsUrl.replace("https://", "wss://");
Expand Down Expand Up @@ -274,12 +274,6 @@ export class FileWatcher {

}

public sendBulkFileChanges(projectId: string, mostRecentEntryTimestamp: number, base64Compressed: string[]) {
if (this._disposed) { return; }

this._outputQueue.addToQueue(projectId, mostRecentEntryTimestamp, base64Compressed);
}

public updateFileWatchStateFromWebSocket(ptwList: ProjectToWatchFromWebSocket[]) {
if (this._disposed) { return; }

Expand Down Expand Up @@ -324,38 +318,34 @@ export class FileWatcher {
success: successParam,
};

const options = {
body: payload,
followRedirect: false,
json: true,
const requestObj = {
headers: {},
json: payload,
rejectUnauthorized: false,
resolveWithFullResponse: true,
retry: 0,
timeout: 20000,
} as request.RequestPromiseOptions;
};

const authToken = this._authTokenWrapper.getLatestToken();
if (authToken && authToken.accessToken) {

options.auth = {
bearer: authToken.accessToken,
};
requestObj.headers = { Authorization: "Bearer " + authToken.accessToken };
}

const url = this._baseUrl + "/api/v1/projects/" + ptw.projectId + "/file-changes/"
+ ptw.projectWatchStateId + "/status?clientUuid=" + this._clientUuid;

log.info("Issuing PUT request to '" + url + "' for " + ptw.projectId);
try {
const result = await request.put(url, options);
const response = await got.put(url, requestObj);

if (result.statusCode !== 200) {
log.error("Unexpected error code " + result.statusCode
if (response.statusCode !== 200) {
log.error("Unexpected error code " + response.statusCode
+ " from '" + url + "' for " + ptw.projectId);

// Inform bad token if we are redirected to an OIDC endpoint
if (authToken && authToken.accessToken && result.statusCode && result.statusCode === 302
&& result.headers && result.headers.location
&& result.headers.location.indexOf("openid-connect/auth") !== -1) {
if (authToken && authToken.accessToken && response.statusCode && response.statusCode === 302
&& response.headers && response.headers.location
&& response.headers.location.indexOf("openid-connect/auth") !== -1) {

this._authTokenWrapper.informBadToken(authToken);
}
Expand All @@ -367,7 +357,7 @@ export class FileWatcher {
}

} catch (err) {
log.error("Unable to connect to '" + url + "', " + err.message + " for " + ptw.projectId);
log.error("PUT request unable to connect to '" + url + "', " + err.message + " for " + ptw.projectId);
sendSuccess = false;

// Inform bad token if we are redirected to an OIDC endpoint
Expand Down Expand Up @@ -424,7 +414,6 @@ export class FileWatcher {
this._externalWatchService.dispose();
}

this._outputQueue.dispose();
this._webSocketManager.dispose();

this._projectsMap.forEach((e) => {
Expand Down Expand Up @@ -470,8 +459,6 @@ export class FileWatcher {
result += "\n";
}

result += "\nHTTP Post Output Queue:\n" + this._outputQueue.generateDebugString().trim() + "\n\n";

result += "---------------------------------------------------------------------------------------\n\n";

return result;
Expand Down
45 changes: 21 additions & 24 deletions Filewatcherd-TypeScript/src/lib/HttpGetStatusThread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* Copyright (c) 2019, 2020 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -9,12 +9,10 @@
* IBM Corporation - initial API and implementation
*******************************************************************************/

import * as request from "request-promise-native";

import * as models from "./Models";

import * as log from "./Logger";

import got from "got";

import { ExponentialBackoffUtil } from "./ExponentialBackoffUtil";
import { FileWatcher } from "./FileWatcher";
import { ProjectToWatch } from "./ProjectToWatch";
Expand Down Expand Up @@ -73,21 +71,17 @@ export class HttpGetStatusThread {

private async doHttpGet(): Promise<ProjectToWatch[]> {

const options = {
followRedirect: false,
json: true,
const requestObj = {
headers: {},
rejectUnauthorized: false,
resolveWithFullResponse: true,
retry: 0,
timeout: 20000,
} as request.RequestPromiseOptions;
};

const authTokenWrapper = this._parent.authTokenWrapper;
const authToken = authTokenWrapper.getLatestToken();
if (authToken && authToken.accessToken) {

options.auth = {
bearer: authToken.accessToken,
};
requestObj.headers = { Authorization: "Bearer " + authToken.accessToken };
}

try {
Expand All @@ -96,18 +90,17 @@ export class HttpGetStatusThread {

log.info("Initiating GET request to " + url);

const httpResult = await request.get(url, options);
const response = await got(url, requestObj);

if (httpResult.statusCode && httpResult.statusCode === 200 && httpResult.body) {
if (response.statusCode === 200 && response.body) {

// Strip EOL characters to ensure it fits on one log line.
let bodyVal = JSON.stringify(httpResult.body);
bodyVal = bodyVal.replace("\n", "");
bodyVal = bodyVal.replace("\r", "");
const w = JSON.parse(response.body);

log.info("GET response received: " + bodyVal);
const debugVal = JSON.stringify(w).replace("\n", "").replace("\r", "");

log.info("GET response received: " + debugVal);

const w: models.IWatchedProjectListJson = httpResult.body;
if (w == null || w === undefined) {
log.error("Expected value not found for GET watchlist endpoint");
return null;
Expand All @@ -130,10 +123,14 @@ export class HttpGetStatusThread {

} else {

if (response) {
log.info("GET request failed, details: " + response.statusCode + " " + response.body);
}

// Inform bad token if we are redirected to an OIDC endpoint
if (authToken && authToken.accessToken && httpResult.statusCode && httpResult.statusCode === 302
&& httpResult.headers && httpResult.headers.location
&& httpResult.headers.location.indexOf("openid-connect/auth") !== -1) {
if (authToken && authToken.accessToken && response.statusCode && response.statusCode === 302
&& response.headers && response.headers.location
&& response.headers.location.indexOf("openid-connect/auth") !== -1) {

authTokenWrapper.informBadToken(authToken);
}
Expand Down
Loading

0 comments on commit 85a9f38

Please sign in to comment.