Skip to content
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

fix: Unable to shorten URL #696

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"bytes": "^3.0.0",
"dynamoose": "^1.11.1",
"express": "^4.16.3",
"jsonpack": "^1.1.5",
"lodash.merge": "^4.6.2",
"lz-string": "^1.4.4",
"mustache-express": "^1.3.0",
"serverless-dynamodb-local": "^0.2.39",
"serverless-http": "^2.7.0"
Expand Down
25 changes: 13 additions & 12 deletions src/app/resultsPage/createURL.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const lodashMerge = require('lodash.merge')
const jsonpack = require('jsonpack/main')
const lzString = require('lz-string')
const { shortenURL } = require('./shortenURL')

const createURL = async ({
Expand All @@ -18,17 +18,18 @@ const createURL = async ({
return strippedResult
})

const packedJSON = jsonpack.pack({
details: {
repoOwner,
repoName,
repoCurrentBranch,
repoBranchBase,
commitSha,
},
results: strippedResultsForURL,
})
const urlResultData = encodeURIComponent(packedJSON)
const urlResultData = lzString.compressToEncodedURIComponent(
JSON.stringify({
details: {
repoOwner,
repoName,
repoCurrentBranch,
repoBranchBase,
commitSha,
},
results: strippedResultsForURL,
}),
)
const longURL = `${bundlewatchServiceHost}/results?d=${urlResultData}`
return shortenURL(longURL)
}
Expand Down
11 changes: 9 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const bodyParser = require('body-parser')
const bytes = require('bytes')
const express = require('express')
const jsonpack = require('jsonpack/main')
const lzString = require('lz-string')
const mustacheExpress = require('mustache-express')
const serverless = require('serverless-http')

Expand Down Expand Up @@ -143,7 +143,14 @@ function createServerlessApp() {
'/results',
asyncMiddleware(async (req, res) => {
let { d } = req.query
const unpacked = jsonpack.unpack(d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we would like to keep both implementations here in order to fallback to jsonpack if the data isn't compressed so that older links are still accessible, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would probably be ideal, yes. I'll have to find some time to get back to this but some possibilities might be:

  • Try to detect if the data has been compressed with lz-string in the handler here, although I don't see provided function from lz-string to do this.
  • Create a new major version with this breaking change.
  • Add a new request param in the CLI/Node app here that indicates compression with lz-string, then check for the presence of that request param in this handler.

My preference would be for the last option.

let unpacked = {}
try {
unpacked = JSON.parse(
lzString.decompressFromEncodedURIComponent(d),
)
} catch (error) {
return res.status(500).json({ error })
}
const validation = unpackedJsonSchema.validate(unpacked)
if (validation.error) {
return res.status(422).json({ errors: validation.error })
Expand Down
Loading