-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/custom metadata header alloc (#231)
* fix: clear buffer when allocating for custom metadata header * test: add composite metadata tests - encodeAndAddCustomMetadata - encodeCustomMetadataHeader Signed-off-by: Kevin Viglucci <[email protected]>
- Loading branch information
Showing
7 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/rsocket-composite-metadata/__tests__/encodeAndAddCustomMetadata.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { encodeAndAddCustomMetadata } from "rsocket-composite-metadata"; | ||
import { hex } from "./test-utils/hex"; | ||
|
||
describe("encodeAndAddCustomMetadata", () => { | ||
it("throws if custom mimtype length is less than 1", () => { | ||
expect(() => | ||
encodeAndAddCustomMetadata(Buffer.from([]), "", Buffer.from("1234")) | ||
).toThrow( | ||
"Custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128" | ||
); | ||
}); | ||
|
||
it("throws if custom mimtype length is greater than 127", () => { | ||
let mime = ""; | ||
while (mime.length < 130) { | ||
mime += "a"; | ||
} | ||
expect(() => | ||
encodeAndAddCustomMetadata(Buffer.from([]), mime, Buffer.from("1234")) | ||
).toThrow( | ||
"Custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128" | ||
); | ||
}); | ||
|
||
it("encodes the header and payload as per spec", () => { | ||
const { c, u, s, t, o, m } = hex; | ||
const metadata = encodeAndAddCustomMetadata( | ||
Buffer.from([]), | ||
"custom", | ||
Buffer.from("1234") | ||
); | ||
const expectedHeaderLength8 = "05"; | ||
const expectedPayloadLength24 = "000004"; | ||
const expectedHeader = `${expectedHeaderLength8}${c}${u}${s}${t}${o}${m}${expectedPayloadLength24}`; | ||
const expectedPayload = `${hex["1"]}${hex["2"]}${hex["3"]}${hex["4"]}`; | ||
expect(metadata.toString("hex")).toBe( | ||
`${expectedHeader}${expectedPayload}` | ||
); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/rsocket-composite-metadata/__tests__/encodeCustomMetadataHeader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { encodeCustomMetadataHeader } from "rsocket-composite-metadata"; | ||
import { hex } from "./test-utils/hex"; | ||
|
||
describe("encodeCustomMetadataHeader", () => { | ||
it("throws if length is less than 1", () => { | ||
expect(() => encodeCustomMetadataHeader("", 0)).toThrow( | ||
"Custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128" | ||
); | ||
}); | ||
|
||
it("throws if length is greater than 127", () => { | ||
let mime = ""; | ||
while (mime.length < 130) { | ||
mime += "a"; | ||
} | ||
expect(() => encodeCustomMetadataHeader(mime, mime.length)).toThrow( | ||
"Custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128" | ||
); | ||
}); | ||
|
||
it("encodes the header as per spec", () => { | ||
const { t, e, s } = hex; | ||
const mime = "test"; | ||
// length minus 1 (uint8) | ||
const expectedLength8 = "03"; | ||
// full length (uint24) | ||
const expectedLength24 = "000004"; | ||
const header = encodeCustomMetadataHeader(mime, mime.length); | ||
expect(header.toString("hex")).toBe( | ||
`${expectedLength8}${t}${e}${s}${t}${expectedLength24}` | ||
); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/rsocket-composite-metadata/__tests__/test-utils/hex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function numHex(s) { | ||
let a = s.toString(16); | ||
if (a.length % 2 > 0) { | ||
a = "0" + a; | ||
} | ||
return a; | ||
} | ||
|
||
function strHex(s) { | ||
let a = ""; | ||
for (let i = 0; i < s.length; i++) { | ||
a = a + numHex(s.charCodeAt(i)); | ||
} | ||
|
||
return a; | ||
} | ||
|
||
const alphabetNumeric = "abcdefghijklmnopqrstuvqxyz0123456789"; | ||
|
||
export const hex: any = {}; | ||
|
||
alphabetNumeric.split("").forEach((c) => { | ||
hex[c] = strHex(c); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { Config } from "@jest/types"; | ||
import { pathsToModuleNameMapper } from "ts-jest/utils"; | ||
import { compilerOptions } from "../../tsconfig.json"; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: "ts-jest", | ||
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { | ||
// This has to match the baseUrl defined in tsconfig.json. | ||
prefix: "<rootDir>/../../", | ||
}), | ||
modulePathIgnorePatterns: ["<rootDir>/__tests__/test-utils"], | ||
collectCoverage: true, | ||
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!**/node_modules/**"], | ||
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
expect.extend({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters