Skip to content

Commit

Permalink
deps: drop qs (#2040)
Browse files Browse the repository at this point in the history
Co-authored-by: Shane Osbourne <sosbourne@duckduckgo.com>
  • Loading branch information
shakyShane and Shane Osbourne committed May 17, 2023
1 parent 6ffc212 commit d0c50e0
Show file tree
Hide file tree
Showing 6 changed files with 2,428 additions and 7,189 deletions.
7 changes: 4 additions & 3 deletions packages/browser-sync/lib/http-protocol.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

var queryString = require("qs");
var proto = exports;
var instanceMethods = ["exit", "notify", "pause", "resume"];
var getBody = require("raw-body");
const { parseParams, serializeParams } = require("./utils");
const permittedSocketEvents = [
"file:reload",
"browser:reload",
Expand Down Expand Up @@ -39,7 +39,7 @@ proto.getUrl = function(args, url) {
url,
require("./config").httpProtocol.path,
"?",
queryString.stringify(args)
serializeParams(args).toString()
].join("");
};

Expand Down Expand Up @@ -72,7 +72,8 @@ proto.middleware = function(bs) {
}
});
}
var params = queryString.parse(req.url.replace(/^.*\?/, ""));
var split = req.url.split("?");
var params = parseParams(split[1]);
var output;

if (!Object.keys(params).length) {
Expand Down
5 changes: 3 additions & 2 deletions packages/browser-sync/lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ var Immutable = require("immutable");
var Map = Immutable.Map;
var isMap = Immutable.Map.isMap;
var List = Immutable.List;
var qs = require("qs");
var path = require("path");
var fs = require("fs");
const { parseParams } = require("./utils");

var Plugin = Immutable.Record({
moduleName: "",
Expand Down Expand Up @@ -175,7 +175,8 @@ function getFromString(string) {

if (split.length > 1) {
return outGoing.update("options", function(opts) {
return opts.mergeDeep(qs.parse(split[1]));
const parsed = parseParams(split[1]);
return opts.mergeDeep(parsed);
});
}

Expand Down
51 changes: 51 additions & 0 deletions packages/browser-sync/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,54 @@ export const connect = require("connect");
export const serveStatic = require("./server/serve-static-wrapper").default();
export const easyExtender = require("easy-extender");
export { UAParser, devIp };

/**
* Just for backwards compat around old argument styles
*/
export function parseParams(search: string): Record<string, any> {
const params = new URLSearchParams(search);
const parsed = Object.create(null);
for (let [key, value] of params) {
let nextKey = key;
let arrayType = false;

if (nextKey.slice(-2) === "[]") {
nextKey = key.slice(0, -2);
arrayType = true;
}

const curr = parsed[nextKey];

if (curr && Array.isArray(curr)) {
curr.push(value);
} else if (curr) {
// if it already exists, but is not already an array, upgrade to array
parsed[nextKey] = [curr, value];
} else {
// otherwise create the original value
if (arrayType) {
parsed[nextKey] = [value];
} else {
parsed[nextKey] = value;
}
}
}
return parsed;
}

/**
* Also for backwards compat around old argument styles
*/
export function serializeParams(args: Record<string, any> = {}): URLSearchParams {
const output = new URLSearchParams();
for (let [key, value] of Object.entries(args)) {
if (Array.isArray(value)) {
for (let valueElement of value) {
output.append(key, valueElement);
}
} else {
output.append(key, String(value));
}
}
return output;
}

0 comments on commit d0c50e0

Please sign in to comment.