From 8960289e70860da33815c6931f206d1088ea29c2 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 29 Oct 2023 21:28:09 +0900 Subject: [PATCH 1/2] Replace parse-link-header library for dual package --- example/webpack/package.json | 2 -- example/webpack/webpack.config.js | 1 - megalodon/package.json | 2 -- megalodon/src/friendica.ts | 6 +++--- megalodon/src/mastodon.ts | 6 +++--- megalodon/src/parse_link_header.ts | 28 ++++++++++++++++++++++++++++ yarn.lock | 16 ++-------------- 7 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 megalodon/src/parse_link_header.ts diff --git a/example/webpack/package.json b/example/webpack/package.json index b15bd2646..75a1d5e09 100644 --- a/example/webpack/package.json +++ b/example/webpack/package.json @@ -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", diff --git a/example/webpack/webpack.config.js b/example/webpack/webpack.config.js index 5cf771880..de11f511c 100644 --- a/example/webpack/webpack.config.js +++ b/example/webpack/webpack.config.js @@ -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') } }, diff --git a/megalodon/package.json b/megalodon/package.json index 75f0118c1..f70db0cd2 100644 --- a/megalodon/package.json +++ b/megalodon/package.json @@ -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", @@ -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", diff --git a/megalodon/src/friendica.ts b/megalodon/src/friendica.ts index dd253b610..410471689 100644 --- a/megalodon/src/friendica.ts +++ b/megalodon/src/friendica.ts @@ -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' @@ -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>(parsed?.next.url, undefined, undefined, true) + while (parsed['next']) { + const nextRes = await this.client.get>(parsed['next'], undefined, undefined, true) converted = Object.assign({}, converted, { data: [...converted.data, ...nextRes.data.map(a => FriendicaAPI.Converter.account(a))] }) diff --git a/megalodon/src/mastodon.ts b/megalodon/src/mastodon.ts index 412ab64a7..f435d93a5 100644 --- a/megalodon/src/mastodon.ts +++ b/megalodon/src/mastodon.ts @@ -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' @@ -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>(parsed?.next.url, undefined, undefined, true) + while (parsed['next']) { + const nextRes = await this.client.get>(parsed['next'], undefined, undefined, true) converted = Object.assign({}, converted, { data: [...converted.data, ...nextRes.data.map(a => MastodonAPI.Converter.account(a))] }) diff --git a/megalodon/src/parse_link_header.ts b/megalodon/src/parse_link_header.ts new file mode 100644 index 000000000..9eb49982c --- /dev/null +++ b/megalodon/src/parse_link_header.ts @@ -0,0 +1,28 @@ +// Refs: https://gist.github.com/Honkalonkalooohhhhh/dfe8a3721f77ded62ff8038ead2afd77 +export function parseLinkHeader(header: string): LinkHeader { + let parts: string[] + let links: LinkHeader = {} + + if (header.length === 0) { + throw new Error('input must not be of zero length') + } + + parts = header.split(',') + // Parse each part into a named link + parts.forEach(part => { + let section: string[] = part.split(';') + let url: string + let name: string + + if (section.length !== 2) { + throw new Error("section could not be split on ';'") + } + url = section[0].replace(/<(.*)>/, '$1').trim() + name = section[1].replace(/rel="(.*)"/, '$1').trim() + links[name] = url + }) + + return links +} + +export type LinkHeader = { [key: string]: string } diff --git a/yarn.lock b/yarn.lock index e5f9a6ac2..9850656dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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== @@ -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" @@ -5267,7 +5255,7 @@ ws@8.14.2: 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== From b538aeb58ef81944c33ae81029562f45c74a46fe Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 29 Oct 2023 21:30:51 +0900 Subject: [PATCH 2/2] Fix lint --- megalodon/src/parse_link_header.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/megalodon/src/parse_link_header.ts b/megalodon/src/parse_link_header.ts index 9eb49982c..ef2f11cf7 100644 --- a/megalodon/src/parse_link_header.ts +++ b/megalodon/src/parse_link_header.ts @@ -1,24 +1,21 @@ // Refs: https://gist.github.com/Honkalonkalooohhhhh/dfe8a3721f77ded62ff8038ead2afd77 export function parseLinkHeader(header: string): LinkHeader { - let parts: string[] - let links: LinkHeader = {} + const links: LinkHeader = {} if (header.length === 0) { throw new Error('input must not be of zero length') } - parts = header.split(',') + const parts = header.split(',') // Parse each part into a named link parts.forEach(part => { - let section: string[] = part.split(';') - let url: string - let name: string + const section: string[] = part.split(';') if (section.length !== 2) { throw new Error("section could not be split on ';'") } - url = section[0].replace(/<(.*)>/, '$1').trim() - name = section[1].replace(/rel="(.*)"/, '$1').trim() + const url = section[0].replace(/<(.*)>/, '$1').trim() + const name = section[1].replace(/rel="(.*)"/, '$1').trim() links[name] = url })