carp-streamer
Advanced tools
Comparing version 0.7.1 to 0.7.2
@@ -68,3 +68,3 @@ "use strict"; | ||
const self = this; | ||
const callback = (error = null, result = { status: SyncResultStatus.UNKNOWN }) => { | ||
const callback = (error, result = { status: SyncResultStatus.UNKNOWN }) => { | ||
self.emit(SyncEventType.SYNCHRONIZE, error, result.absolutePath, result.status); | ||
@@ -199,8 +199,7 @@ }; | ||
debug('Uploading `%s`...', this.relativePath); | ||
const stream = this.createReadStream(); | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
stream.on('error', reject); | ||
this.finder.uploadFile(base, stream, stats, folder) | ||
.then(() => resolve(SyncResultStatus.UPLOADED)).catch(reject); | ||
})); | ||
yield new Promise((resolve, reject) => { | ||
const stream = this.createReadStream(); | ||
stream.once('error', reject); | ||
this.finder.uploadFile(base, stream, stats, folder).then(resolve).catch(reject); | ||
}); | ||
} | ||
@@ -216,10 +215,9 @@ return SyncResultStatus.UPLOADED; | ||
if (!pretend) { | ||
const remoteFile = this.remoteFile; | ||
const stream = this.createReadStream(); | ||
debug('Upgrading `%s`...', this.relativePath); | ||
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
stream.on('error', reject); | ||
this.finder.uploadNewFileVersion(remoteFile, stream, stats) | ||
.then(() => resolve(SyncResultStatus.UPGRADED)).catch(reject); | ||
})); | ||
const file = this.remoteFile; | ||
yield new Promise((resolve, reject) => { | ||
const stream = this.createReadStream(); | ||
stream.once('error', reject); | ||
this.finder.uploadNewFileVersion(file, stream, stats).then(resolve).catch(reject); | ||
}); | ||
} | ||
@@ -236,4 +234,4 @@ return SyncResultStatus.UPGRADED; | ||
stream.on('data', chunk => hash.update(chunk)); | ||
stream.on('close', () => resolve(hash.digest('hex'))); | ||
stream.on('error', reject); | ||
stream.once('close', () => resolve(hash.digest('hex'))); | ||
stream.once('error', reject); | ||
}); | ||
@@ -240,0 +238,0 @@ } |
@@ -105,3 +105,3 @@ "use strict"; | ||
const subFolder = yield finder.findFolderByName(folderName); | ||
const folder = subFolder || (yield makeRetriable(finder.createFolder, finder, BoxFinder.retryIfFolderConflictError)(folderName)); | ||
const folder = subFolder || (yield finder.createFolder(folderName)); | ||
return this.createFolderByPath(folderPath.slice(1), finder.new(folder)); | ||
@@ -143,5 +143,9 @@ }); | ||
} | ||
uploadFile(name, stream, stats, folder) { | ||
uploadFile(name, content, stats, folder) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const folderId = (folder || this.current).id; | ||
const options = { | ||
content_created_at: stats && toRFC3339String(stats.ctime), | ||
content_modified_at: stats && toRFC3339String(stats.mtime), | ||
}; | ||
try { | ||
@@ -160,3 +164,3 @@ const result = yield this.files.preflightUploadFile(folderId, { name, size: stats && stats.size }); | ||
const finder = (folder ? this.new(folder) : this); | ||
return finder.uploadNewFileVersion(item, stream); | ||
return finder.files.uploadNewFileVersion(item.id, content, options); | ||
} | ||
@@ -168,10 +172,6 @@ else { | ||
debug('uploading %s...', name); | ||
const options = { | ||
content_created_at: stats && toRFC3339String(stats.ctime), | ||
content_modified_at: stats && toRFC3339String(stats.mtime), | ||
}; | ||
return this.files.uploadFile(folderId, name, stream, options); | ||
return this.files.uploadFile(folderId, name, content, options); | ||
}); | ||
} | ||
uploadNewFileVersion(file, stream, stats) { | ||
uploadNewFileVersion(file, content, stats) { | ||
const options = { | ||
@@ -181,3 +181,3 @@ content_created_at: stats && toRFC3339String(stats.ctime), | ||
}; | ||
return this.files.uploadNewFileVersion(file.id, stream, options); | ||
return this.files.uploadNewFileVersion(file.id, content, options); | ||
} | ||
@@ -188,6 +188,4 @@ new(folder) { | ||
createFolder(folderName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const parentFolderId = this.current.id; | ||
return yield this.folders.create(parentFolderId, folderName); | ||
}); | ||
const parentFolderId = this.current.id; | ||
return makeRetriable(this.folders.create, this.folders, retryIfFolderConflictError)(parentFolderId, folderName); | ||
} | ||
@@ -231,21 +229,2 @@ findFileByName(fileName) { | ||
} | ||
BoxFinder.retryIfFolderConflictError = (error, method, that, args, retryTimes) => __awaiter(this, void 0, void 0, function* () { | ||
const [folderName] = args; | ||
debug(`Failed to create folder '%s' (parent folder id: %s).`, folderName, that.current.id); | ||
if (!isResponseError(error) || error.statusCode !== 409) { | ||
throw error; | ||
} | ||
debug('API Response Error: %s', error.message); | ||
const item = findConflictItem(error); | ||
if (item) { | ||
debug('Found existing folder with that name: %s', item.name); | ||
return that.folders.get(item.id); | ||
} | ||
else { | ||
debug('Retries %d more times.', retryTimes); | ||
const retryAfter = determineDelayTime(retryTimes, error); | ||
debug(`Waiting time is %d milliseconds.`, retryAfter); | ||
return makeRetriable(method, that, BoxFinder.retryIfFolderConflictError, retryTimes - 1, retryAfter)(...args); | ||
} | ||
}); | ||
exports.BoxFinder = BoxFinder; | ||
@@ -280,2 +259,21 @@ function makeRetriable(method, that, callback, retryTimes = config_1.INIT_RETRY_TIMES, delay = 0) { | ||
}; | ||
const retryIfFolderConflictError = (error, method, that, args, retryTimes) => __awaiter(this, void 0, void 0, function* () { | ||
const [parentFolderId, folderName] = args; | ||
debug(`Failed to create folder '%s' (parent folder id: %s).`, folderName, parentFolderId); | ||
if (!isResponseError(error) || error.statusCode !== 409) { | ||
throw error; | ||
} | ||
debug('API Response Error: %s', error.message); | ||
const item = findConflictItem(error); | ||
if (item) { | ||
debug('Found existing folder with that name: %s', item.name); | ||
return that.get(item.id); | ||
} | ||
else { | ||
debug('Retries %d more times.', retryTimes); | ||
const retryAfter = determineDelayTime(retryTimes, error); | ||
debug(`Waiting time is %d milliseconds.`, retryAfter); | ||
return makeRetriable(method, that, retryIfFolderConflictError, retryTimes - 1, retryAfter)(...args); | ||
} | ||
}); | ||
function proxyToTrapTooManyRequests(target) { | ||
@@ -282,0 +280,0 @@ return new Proxy(target, { |
{ | ||
"name": "carp-streamer", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"description": "Carp streamer", | ||
@@ -5,0 +5,0 @@ "bin": "dist/index.js", |
@@ -37,3 +37,3 @@ import async from 'async'; | ||
const self = this; | ||
const callback: async.AsyncResultCallback<SyncResult> = (error = null, result = { status: SyncResultStatus.UNKNOWN }) => { | ||
const callback: async.AsyncResultCallback<SyncResult> = (error, result = { status: SyncResultStatus.UNKNOWN }) => { | ||
self.emit(SyncEventType.SYNCHRONIZE, error, result.absolutePath, result.status); | ||
@@ -159,7 +159,6 @@ }; | ||
debug('Uploading `%s`...', this.relativePath); | ||
const stream = this.createReadStream(); | ||
return new Promise<SyncResultStatus>(async (resolve, reject) => { | ||
stream.on('error', reject); | ||
this.finder.uploadFile(base, stream, stats, folder) | ||
.then(() => resolve(SyncResultStatus.UPLOADED)).catch(reject); | ||
await new Promise<box.File>((resolve, reject) => { | ||
const stream = this.createReadStream(); | ||
stream.once('error', reject); | ||
this.finder.uploadFile(base, stream, stats, folder).then(resolve).catch(reject); | ||
}); | ||
@@ -174,9 +173,8 @@ } | ||
if (!pretend) { | ||
const remoteFile = this.remoteFile; | ||
const stream = this.createReadStream(); | ||
debug('Upgrading `%s`...', this.relativePath); | ||
return new Promise<SyncResultStatus>(async (resolve, reject) => { | ||
stream.on('error', reject); | ||
this.finder.uploadNewFileVersion(remoteFile, stream, stats) | ||
.then(() => resolve(SyncResultStatus.UPGRADED)).catch(reject); | ||
const file = this.remoteFile; | ||
await new Promise<box.File>((resolve, reject) => { | ||
const stream = this.createReadStream(); | ||
stream.once('error', reject); | ||
this.finder.uploadNewFileVersion(file, stream, stats).then(resolve).catch(reject); | ||
}); | ||
@@ -194,4 +192,4 @@ } | ||
stream.on('data', chunk => hash.update(chunk)); | ||
stream.on('close', () => resolve(hash.digest('hex'))); | ||
stream.on('error', reject); | ||
stream.once('close', () => resolve(hash.digest('hex'))); | ||
stream.once('error', reject); | ||
}); | ||
@@ -198,0 +196,0 @@ } |
@@ -74,3 +74,3 @@ import BoxSDK, * as box from 'box-node-sdk'; | ||
const subFolder = await finder.findFolderByName(folderName); | ||
const folder = subFolder || await makeRetriable(finder.createFolder, finder, BoxFinder.retryIfFolderConflictError)(folderName); | ||
const folder = subFolder || await finder.createFolder(folderName); | ||
return this.createFolderByPath(folderPath.slice(1), finder.new(folder)); | ||
@@ -91,20 +91,2 @@ } | ||
private static retryIfFolderConflictError: RetryCallback<box.Folder, BoxFinder> = async (error, method, that, args, retryTimes) => { | ||
const [folderName] = args; | ||
debug(`Failed to create folder '%s' (parent folder id: %s).`, folderName, that.current.id); | ||
if (!isResponseError(error) || error.statusCode !== 409) { throw error; } | ||
debug('API Response Error: %s', error.message); | ||
const item = findConflictItem(error); | ||
if (item) { | ||
debug('Found existing folder with that name: %s', item.name); | ||
return that.folders.get(item.id); | ||
} else { | ||
debug('Retries %d more times.', retryTimes); | ||
const retryAfter = determineDelayTime(retryTimes, error); | ||
debug(`Waiting time is %d milliseconds.`, retryAfter); | ||
return makeRetriable(method, that, BoxFinder.retryIfFolderConflictError, retryTimes - 1, retryAfter)(...args); | ||
} | ||
} | ||
private files: box.Files; | ||
@@ -137,4 +119,8 @@ private folders: box.Folders; | ||
public async uploadFile(name: string, stream: ReadStream, stats?: Stats, folder?: box.MiniFolder) { | ||
public async uploadFile(name: string, content: string | Buffer | ReadStream, stats?: Stats, folder?: box.MiniFolder) { | ||
const folderId = (folder || this.current).id; | ||
const options = { | ||
content_created_at: stats && toRFC3339String(stats.ctime), | ||
content_modified_at: stats && toRFC3339String(stats.mtime), | ||
}; | ||
try { | ||
@@ -151,3 +137,3 @@ const result = await this.files.preflightUploadFile(folderId, { name, size: stats && stats.size }); | ||
const finder = (folder ? this.new(folder) : this); | ||
return finder.uploadNewFileVersion(item, stream); | ||
return finder.files.uploadNewFileVersion(item.id, content, options); | ||
} else { | ||
@@ -158,10 +144,6 @@ throw error; | ||
debug('uploading %s...', name); | ||
const options = { | ||
content_created_at: stats && toRFC3339String(stats.ctime), | ||
content_modified_at: stats && toRFC3339String(stats.mtime), | ||
}; | ||
return this.files.uploadFile(folderId, name, stream, options); | ||
return this.files.uploadFile(folderId, name, content, options); | ||
} | ||
public uploadNewFileVersion(file: box.MiniFile, stream: ReadStream, stats?: Stats) { | ||
public uploadNewFileVersion(file: box.MiniFile, content: string | Buffer | ReadStream, stats?: Stats) { | ||
const options = { | ||
@@ -171,3 +153,3 @@ content_created_at: stats && toRFC3339String(stats.ctime), | ||
}; | ||
return this.files.uploadNewFileVersion(file.id, stream, options); | ||
return this.files.uploadNewFileVersion(file.id, content, options); | ||
} | ||
@@ -179,5 +161,5 @@ | ||
private async createFolder(folderName: string): Promise<box.Folder> { | ||
private createFolder(folderName: string): Promise<box.Folder> { | ||
const parentFolderId = this.current.id; | ||
return await this.folders.create(parentFolderId, folderName); | ||
return makeRetriable(this.folders.create, this.folders, retryIfFolderConflictError)(parentFolderId, folderName); | ||
} | ||
@@ -244,2 +226,20 @@ | ||
const retryIfFolderConflictError: RetryCallback<box.Folder, box.Folders> = async (error, method, that, args, retryTimes) => { | ||
const [parentFolderId, folderName] = args; | ||
debug(`Failed to create folder '%s' (parent folder id: %s).`, folderName, parentFolderId); | ||
if (!isResponseError(error) || error.statusCode !== 409) { throw error; } | ||
debug('API Response Error: %s', error.message); | ||
const item = findConflictItem(error); | ||
if (item) { | ||
debug('Found existing folder with that name: %s', item.name); | ||
return that.get(item.id); | ||
} else { | ||
debug('Retries %d more times.', retryTimes); | ||
const retryAfter = determineDelayTime(retryTimes, error); | ||
debug(`Waiting time is %d milliseconds.`, retryAfter); | ||
return makeRetriable(method, that, retryIfFolderConflictError, retryTimes - 1, retryAfter)(...args); | ||
} | ||
}; | ||
function proxyToTrapTooManyRequests<T extends object>(target: T): T { | ||
@@ -246,0 +246,0 @@ return new Proxy(target, { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
95707
1604