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

Encode JSON Strings in Headers #2744

Merged
merged 3 commits into from
Mar 31, 2023
Merged
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
11 changes: 11 additions & 0 deletions packages/zealot/src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ test("#load a stream", async () => {

test("#timeout test", async () => {
jest.useFakeTimers()
const client = new Client("http://localhost:9000")
client.fetch = jest.fn((url, opts = {}) => {
return new Promise((_, reject) => {
opts.signal?.addEventListener("abort", () =>
Expand All @@ -170,3 +171,13 @@ test("#timeout test", async () => {
jest.advanceTimersByTime(60_000)
await expect(p).rejects.toEqual("ABORTED IN MOCK TEST")
})

test("#load with unicode in commit message", async () => {
const {
pool: {id},
} = await client.createPool("unicode")
await client.load("1 2 3", {
pool: id,
message: {author: "🤷‍♂️", body: "中文测试-⛔️"},
})
})
11 changes: 5 additions & 6 deletions packages/zealot/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
defaults,
getEnv,
getLoadContentType,
json,
jsonHeader,
parseContent,
toJS,
wrapAbort,
Expand Down Expand Up @@ -54,7 +54,7 @@ export class Client {
const poolId = typeof pool === "string" ? pool : pool.id
const branch = opts.branch || "main"
let headers = new Headers()
if (opts.message) headers.set("Zed-Commit", json(opts.message))
if (opts.message) headers.set("Zed-Commit", jsonHeader(opts.message))
const res = await this.send({
path: `/pool/${poolId}/branch/${encodeURIComponent(branch)}`,
method: "POST",
Expand All @@ -77,7 +77,7 @@ export class Client {
const result = await this.send({
method: "POST",
path: `/query?ctrl=${options.controlMessages}`,
body: json({query}),
body: JSON.stringify({query}),
contentType: "application/json",
format: options.format,
signal: abortCtl.signal,
Expand All @@ -97,7 +97,7 @@ export class Client {
return this.send({
method: "POST",
path: "/pool",
body: json({name, layout}),
body: JSON.stringify({name, layout}),
contentType: "application/json",
}).then(toJS)
}
Expand Down Expand Up @@ -137,7 +137,7 @@ export class Client {
await this.send({
method: "PUT",
path: `/pool/${poolId}`,
body: json(args),
body: JSON.stringify(args),
contentType: "application/json",
})
return true
Expand Down Expand Up @@ -173,7 +173,6 @@ export class Client {
}) {
const abortCtl = wrapAbort(opts.signal)
const clearTimer = this.setTimeout(() => {
console.error("request timed out:", opts)
abortCtl.abort()
}, opts.timeout)
const fetch = (opts.fetch || this.fetch) as Types.NodeFetch // Make typescript happy
Expand Down
9 changes: 7 additions & 2 deletions packages/zealot/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ export async function toJS(res: Response | NodeResponse) {
return decode(j).toJS()
}

export function json(obj: any) {
return JSON.stringify(obj)
const charsToEncode = /[\u007f-\uffff]/g

export function jsonHeader(obj: any) {
// https://stackoverflow.com/a/40347926
return JSON.stringify(obj).replace(charsToEncode, function (c) {
return "\\u" + ("000" + c.charCodeAt(0).toString(16)).slice(-4)
})
}

export function wrapAbort(signal?: AbortSignal) {
Expand Down