-
Notifications
You must be signed in to change notification settings - Fork 0
Modernize for Node.js 22+ — ESM, async/await, ya-disk v5 #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
718c17e
Chore: [#341] Upgrade ya-disk from v3 to v5
RomiC 7865768
Feat: [#341] Switch public API to Promise-based with ya-disk v5
RomiC 0aad86c
Feat: [#341] Convert to ESM with dual CJS/ESM exports
RomiC aedddc1
Chore: [#341] Replace deprecated url.parse() with direct URL strings
RomiC 2adbd37
Chore: [#341] Use Promise.withResolvers() in feature tests and remove…
RomiC 3f01e76
fixup! Feat: [#341] Convert to ESM with dual CJS/ESM exports
RomiC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import fs from 'fs'; | ||
| import fs from 'node:fs'; | ||
|
|
||
| import { download } from '../index'; | ||
| import { download } from '../index.js'; | ||
|
|
||
| const { API_TOKEN = '' } = process.env; | ||
| const fileToSave = fs.createWriteStream('./Mountains.jpg'); | ||
|
|
||
| download(API_TOKEN, 'disk:/Горы.jpg', (download) => download.pipe(fileToSave)); | ||
| const stream = await download(API_TOKEN, 'disk:/Горы.jpg'); | ||
| stream.pipe(fileToSave); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| import { upload } from '../index'; | ||
| import { upload } from '../index.js'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const { API_TOKEN = '' } = process.env; | ||
| const fileToUpload = fs.createReadStream(path.join(__dirname, 'upload.js')); | ||
|
|
||
| upload( | ||
| API_TOKEN, | ||
| 'disk:/Приложения/ya-disk-api/upload.js', | ||
| true, | ||
| (stream) => fileToUpload.pipe(stream), | ||
| (err) => process.stderr.write(err) | ||
| ); | ||
| try { | ||
| const stream = await upload( | ||
| API_TOKEN, | ||
| 'disk:/Приложения/ya-disk-api/upload.js', | ||
| true | ||
| ); | ||
| fileToUpload.pipe(stream); | ||
| } catch (err) { | ||
| process.stderr.write(err); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,2 @@ | ||
| module.exports = { | ||
| upload: require('./lib/upload'), | ||
| download: require('./lib/download') | ||
| }; | ||
| export { upload } from './lib/upload.js'; | ||
| export { download } from './lib/download.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,14 @@ | ||
| const { download } = require('ya-disk'); | ||
| import { download as yaDiskDownload } from 'ya-disk'; | ||
|
|
||
| const followRedirect = require('./followRedirect'); | ||
| import { followRedirect } from './followRedirect.js'; | ||
|
|
||
| /** | ||
| * Creates readable stream to download file from the Yadex.Disk | ||
| * Creates readable stream to download file from Yandex.Disk | ||
| * @param {string} token OAuth-token | ||
| * @param {string} file Path+filename on the storage | ||
| * @param {function} [onReady] Callback to be called after stream being ready | ||
| * @param {function} [onError] Callback to be called in case of any error | ||
| * @returns {Promise<import('stream').Readable>} | ||
| */ | ||
| module.exports = (token, file, onReady, onError) => | ||
| download.link( | ||
| token, | ||
| file, | ||
| ({ href, method }) => followRedirect(href, method, (req) => onReady(req)), | ||
| onError | ||
| ); | ||
| export async function download(token, file) { | ||
| const { href, method } = await yaDiskDownload.link(token, file); | ||
| return followRedirect(href, method); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
| const https = require('https'); | ||
| const { parse: urlParse } = require('url'); | ||
| import https from 'node:https'; | ||
|
|
||
| const followRedirect = (url, method = 'GET', onReady) => { | ||
| const req = https.request( | ||
| Object.assign({}, urlParse(url), { method }), | ||
| (res) => { | ||
| if (res.statusCode >= 300 && res.statusCode < 400) { | ||
| res.destroy(); | ||
| followRedirect(res.headers.location, method, onReady); | ||
| } else if (res.statusCode === 200 && typeof onReady === 'function') { | ||
| onReady(res); | ||
| } | ||
| } | ||
| ); | ||
| export function followRedirect(url, method = 'GET') { | ||
| return new Promise((resolve, reject) => { | ||
| const makeRequest = (url) => { | ||
| const req = https.request(url, { method }, (res) => { | ||
| if (res.statusCode >= 300 && res.statusCode < 400) { | ||
| res.destroy(); | ||
| makeRequest(res.headers.location); | ||
| } else if (res.statusCode === 200) { | ||
| resolve(res); | ||
| } else { | ||
| reject(new Error(`Unexpected status code: ${res.statusCode}`)); | ||
| } | ||
| }); | ||
|
|
||
| req.end(); | ||
| }; | ||
| req.on('error', reject); | ||
| req.end(); | ||
| }; | ||
|
|
||
| module.exports = followRedirect; | ||
| makeRequest(url); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,14 @@ | ||
| const https = require('https'); | ||
| const { upload } = require('ya-disk'); | ||
| const { parse: urlParse } = require('url'); | ||
| import https from 'node:https'; | ||
| import { upload as yaDiskUpload } from 'ya-disk'; | ||
|
|
||
| /** | ||
| * Create writable stream for uploading file to Yandex.Disk | ||
| * Creates writable stream for uploading file to Yandex.Disk | ||
| * @param {string} token OAuth-token | ||
| * @param {string} file Path+filename on the storage | ||
| * @param {boolean} [overwrite=true] Overwrite existing file | ||
| * @param {function} onReady Callback to be called after stream being ready | ||
| * @param {function} onError Callback to be called in case of any error | ||
| * @returns {Promise<import('stream').Writable>} | ||
| */ | ||
| module.exports = (token, file, overwrite = true, onReady, onError) => | ||
| upload.link( | ||
| token, | ||
| file, | ||
| overwrite, | ||
| ({ href, method }) => | ||
| onReady(https.request(Object.assign({}, urlParse(href), { method }))), | ||
| onError | ||
| ); | ||
| export async function upload(token, file, overwrite = true) { | ||
| const { href, method } = await yaDiskUpload.link(token, file, overwrite); | ||
| return https.request(href, { method }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.