Skip to content

Commit

Permalink
fix: Replace node.js' url with whatwg URL
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed May 3, 2024
1 parent 11ec7ef commit e774208
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
13 changes: 8 additions & 5 deletions src/lib/adapters/Caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Logger from '../Logger'
import Adapter from '../interfaces/Adapter'
import difference from 'lodash/difference'

import url from 'url'
import Ordering from '../interfaces/Ordering'
import {
MissingItemOrderError,
Expand All @@ -30,7 +29,7 @@ export default class CachingAdapter implements Adapter, BulkImportResource {

getLabel():string {
const data = this.getData()
return data.username + '@' + url.parse(data.url).hostname
return data.username + '@' + new URL(data.url).hostname
}

async getBookmarksTree(): Promise<Folder> {
Expand All @@ -41,9 +40,13 @@ export default class CachingAdapter implements Adapter, BulkImportResource {
if (bm.url === 'data:') {
return false
}
return Boolean(['https:', 'http:', 'ftp:', 'data:', 'javascript:', 'chrome:', 'file:'].includes(
url.parse(bm.url).protocol
))
try {
return Boolean(['https:', 'http:', 'ftp:', 'data:', 'javascript:', 'chrome:', 'file:'].includes(
new URL(bm.url).protocol
))
} catch (e) {
return false
}
}

async createBookmark(bm:Bookmark):Promise<string|number> {
Expand Down
28 changes: 13 additions & 15 deletions src/lib/adapters/NextcloudBookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Bookmark, Folder, ItemLocation, TItem } from '../Tree'
import { Base64 } from 'js-base64'
import AsyncLock from 'async-lock'
import * as Parallel from 'async-parallel'
import url from 'url'
import PQueue from 'p-queue'
import flatten from 'lodash/flatten'
import { BulkImportResource, LoadFolderChildrenResource, OrderFolderResource } from '../interfaces/Resource'
Expand Down Expand Up @@ -106,27 +105,26 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes

getLabel():string {
const data = this.getData()
return data.username.includes('@') ? data.username + ' on ' + url.parse(data.url).hostname : data.username + '@' + url.parse(data.url).hostname
return data.username.includes('@') ? data.username + ' on ' + new URL(data.url).hostname : data.username + '@' + new URL(data.url).hostname
}

acceptsBookmark(bm: Bookmark):boolean {
return Boolean(~['https:', 'http:', 'ftp:'].indexOf(url.parse(bm.url).protocol))
try {
return Boolean(~['https:', 'http:', 'ftp:'].indexOf(new URL(bm.url).protocol))
} catch (e) {
return false
}
}

normalizeServerURL(input:string):string {
const serverURL = url.parse(input)
const serverURL = new URL(input)
const indexLoc = serverURL.pathname.indexOf('index.php')
return url.format({
protocol: serverURL.protocol,
auth: serverURL.auth,
host: serverURL.host,
port: serverURL.port,
pathname:
serverURL.pathname.substr(0, ~indexLoc ? indexLoc : undefined) +
(!~indexLoc && serverURL.pathname[serverURL.pathname.length - 1] !== '/'
? '/'
: ''),
})
if (!serverURL.pathname) serverURL.pathname = ''
serverURL.search = ''
serverURL.hash = ''
serverURL.pathname = serverURL.pathname.substring(0, ~indexLoc ? indexLoc : undefined)
const output = serverURL.toString()
return output + (output[output.length - 1] !== '/' ? '/' : '')
}

timeout(ms) {
Expand Down
16 changes: 5 additions & 11 deletions src/lib/adapters/WebDav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import XbelSerializer from '../serializers/Xbel'
import Logger from '../Logger'
import { Base64 } from 'js-base64'

import url from 'url'
import Crypto from '../Crypto'
import {
AuthenticationError,
Expand Down Expand Up @@ -51,17 +50,12 @@ export default class WebDavAdapter extends CachingAdapter {
}

normalizeServerURL(input) {
const serverURL = url.parse(input)
const serverURL = new URL(input)
if (!serverURL.pathname) serverURL.pathname = ''
return url.format({
protocol: serverURL.protocol,
auth: serverURL.auth,
host: serverURL.host,
port: serverURL.port,
pathname:
serverURL.pathname +
(serverURL.pathname[serverURL.pathname.length - 1] !== '/' ? '/' : '')
})
serverURL.search = ''
serverURL.hash = ''
const output = serverURL.toString()
return output + (output[output.length - 1] !== '/' ? '/' : '')
}

cancel() {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/browser/BrowserTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import PQueue from 'p-queue'
import Account from '../Account'
import { Bookmark, Folder, ItemLocation, ItemType } from '../Tree'
import Ordering from '../interfaces/Ordering'
import url from 'url'
import random from 'random'
import seedrandom from 'seedrandom'
import { isVivaldi } from './BrowserDetection'
Expand Down Expand Up @@ -123,7 +122,7 @@ export default class BrowserTree implements IResource {
return
}
try {
if (self.location.protocol === 'moz-extension:' && url.parse(bookmark.url).hostname === 'separator.floccus.org') {
if (self.location.protocol === 'moz-extension:' && new URL(bookmark.url).hostname === 'separator.floccus.org') {
const node = await this.queue.add(async() => {
Logger.log('(local)CREATE: executing create ', bookmark)
return browser.bookmarks.create({
Expand Down Expand Up @@ -154,7 +153,7 @@ export default class BrowserTree implements IResource {
return
}
try {
if (self.location.protocol === 'moz-extension:' && url.parse(bookmark.url).hostname === 'separator.floccus.org') {
if (self.location.protocol === 'moz-extension:' && new URL(bookmark.url).hostname === 'separator.floccus.org') {
// noop
} else {
await this.queue.add(async() => {
Expand Down

0 comments on commit e774208

Please sign in to comment.