Skip to content

Commit

Permalink
Merge pull request #2010 from h3poteto/feat/link-header
Browse files Browse the repository at this point in the history
Replace parse-link-header library for dual package
  • Loading branch information
h3poteto authored Oct 29, 2023
2 parents c40f7a5 + b538aeb commit 8b69f6a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
2 changes: 0 additions & 2 deletions example/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
"devDependencies": {
"assert": "^2.1.0",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"events": "^3.3.0",
"http-server": "^14.1.1",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"process": "^0.11.10",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"ts-loader": "^9.4.4",
Expand Down
1 change: 0 additions & 1 deletion example/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = {
stream: require.resolve('stream-browserify'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
querystring: require.resolve('querystring-es3'),
os: require.resolve('os-browserify/browser')
}
},
Expand Down
2 changes: 0 additions & 2 deletions megalodon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"form-data": "^4.0.0",
"https-proxy-agent": "^7.0.2",
"object-assign-deep": "^0.4.0",
"parse-link-header": "^2.0.0",
"socks-proxy-agent": "^8.0.2",
"uuid": "^9.0.1",
"ws": "8.14.2",
Expand All @@ -78,7 +77,6 @@
"@types/form-data": "^2.5.0",
"@types/jest": "^29.5.5",
"@types/object-assign-deep": "^0.4.1",
"@types/parse-link-header": "^2.0.1",
"@types/uuid": "^9.0.4",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
Expand Down
6 changes: 3 additions & 3 deletions megalodon/src/friendica.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OAuth2Client } from '@badgateway/oauth2-client'
import FormData from 'form-data'
import parseLinkHeader from 'parse-link-header'
import { parseLinkHeader } from './parse_link_header'

import FriendicaAPI from './friendica/api_client'
import WebSocket from './friendica/web_socket'
Expand Down Expand Up @@ -489,8 +489,8 @@ export default class Friendica implements MegalodonInterface {
})
if (get_all && converted.headers.link) {
let parsed = parseLinkHeader(converted.headers.link)
while (parsed?.next) {
const nextRes = await this.client.get<Array<FriendicaAPI.Entity.Account>>(parsed?.next.url, undefined, undefined, true)
while (parsed['next']) {
const nextRes = await this.client.get<Array<FriendicaAPI.Entity.Account>>(parsed['next'], undefined, undefined, true)
converted = Object.assign({}, converted, {
data: [...converted.data, ...nextRes.data.map(a => FriendicaAPI.Converter.account(a))]
})
Expand Down
6 changes: 3 additions & 3 deletions megalodon/src/mastodon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OAuth2Client } from '@badgateway/oauth2-client'
import FormData from 'form-data'
import parseLinkHeader from 'parse-link-header'
import { parseLinkHeader } from './parse_link_header'

import MastodonAPI from './mastodon/api_client'
import Streaming from './mastodon/web_socket'
Expand Down Expand Up @@ -565,8 +565,8 @@ export default class Mastodon implements MegalodonInterface {
})
if (get_all && converted.headers.link) {
let parsed = parseLinkHeader(converted.headers.link)
while (parsed?.next) {
const nextRes = await this.client.get<Array<MastodonAPI.Entity.Account>>(parsed?.next.url, undefined, undefined, true)
while (parsed['next']) {
const nextRes = await this.client.get<Array<MastodonAPI.Entity.Account>>(parsed['next'], undefined, undefined, true)
converted = Object.assign({}, converted, {
data: [...converted.data, ...nextRes.data.map(a => MastodonAPI.Converter.account(a))]
})
Expand Down
25 changes: 25 additions & 0 deletions megalodon/src/parse_link_header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Refs: https://gist.github.com/Honkalonkalooohhhhh/dfe8a3721f77ded62ff8038ead2afd77
export function parseLinkHeader(header: string): LinkHeader {
const links: LinkHeader = {}

if (header.length === 0) {
throw new Error('input must not be of zero length')
}

const parts = header.split(',')
// Parse each part into a named link
parts.forEach(part => {
const section: string[] = part.split(';')

if (section.length !== 2) {
throw new Error("section could not be split on ';'")
}
const url = section[0].replace(/<(.*)>/, '$1').trim()
const name = section[1].replace(/rel="(.*)"/, '$1').trim()
links[name] = url
})

return links
}

export type LinkHeader = { [key: string]: string }
16 changes: 2 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,6 @@
resolved "https://registry.yarnpkg.com/@types/object-assign-deep/-/object-assign-deep-0.4.1.tgz#6bb3c9f81ab6ee8af09276fded7b231b85ea406f"
integrity sha512-uWJatOM1JKDdF6Fwa16124b76BtxvTz5Lv+ORGuI7dwqU4iqExXpeHrHOi1c8BU4FgSJ6PdH0skR9Zmz8+MUqQ==

"@types/parse-link-header@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/parse-link-header/-/parse-link-header-2.0.1.tgz#be4b412eb36e5d6bffc481e3f6e38b7706a4c9ee"
integrity sha512-BrKNSrRTqn3UkMXvdVtr/znJch0PMBpEvEP8oBkxDx7eEGntuFLI+WpA5HGsNHK4SlqyhaMa+Ks0ViwyixQB5w==

"@types/semver@^7.5.0":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a"
Expand Down Expand Up @@ -2103,7 +2098,7 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

crypto-browserify@^3.11.0, crypto-browserify@^3.12.0:
crypto-browserify@^3.11.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
integrity sha512-fz4spIh+znjO2VjL+IdhEpRJ3YN6sMzITSBijk6FK2UvTqruSQW+/cCZTSNsMiZNvUeq0CqurF+dAbyiGOY6Wg==
Expand Down Expand Up @@ -4215,13 +4210,6 @@ parse-json@^5.2.0:
json-parse-even-better-errors "^2.3.0"
lines-and-columns "^1.1.6"

parse-link-header@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/parse-link-header/-/parse-link-header-2.0.0.tgz#949353e284f8aa01f2ac857a98f692b57733f6b7"
integrity sha512-xjU87V0VyHZybn2RrCX5TIFGxTVZE6zqqZWMPlIKiSKuWh/X5WZdt+w1Ki1nXB+8L/KtL+nZ4iq+sfI6MrhhMw==
dependencies:
xtend "~4.0.1"

path-browserify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd"
Expand Down Expand Up @@ -5267,7 +5255,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ws/-/ws-8.14.2.tgz#6c249a806eb2db7a20d26d51e7709eab7b2e6c7f"
integrity sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==

xtend@^4.0.2, xtend@~4.0.1:
xtend@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
Expand Down

0 comments on commit 8b69f6a

Please sign in to comment.