From 20d5ad78bc140881d443476b4aa658d90c7ded4e Mon Sep 17 00:00:00 2001 From: Wang Zishi Date: Mon, 14 Dec 2015 12:14:05 +0800 Subject: [PATCH] Add definitions for cookies v0.5.8 for [cookies](https://github.com/pillarjs/cookies) v0.5.8 --- cookies/cookies-tests.ts | 42 +++++++++++++++++ cookies/cookies.d.ts | 98 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 cookies/cookies-tests.ts create mode 100644 cookies/cookies.d.ts diff --git a/cookies/cookies-tests.ts b/cookies/cookies-tests.ts new file mode 100644 index 0000000000..3e3274a4d4 --- /dev/null +++ b/cookies/cookies-tests.ts @@ -0,0 +1,42 @@ +/// +/// + +import * as Cookies from 'cookies'; +import * as http from 'http'; + +const server = http.createServer((req, res) => { + const cookies = new Cookies(req, res); + let unsigned: string, + signed: string, + tampered: string + + if (req.url == "/set") { + cookies + // set a regular cookie + .set("unsigned", "foo", { httpOnly: false }) + + // set a signed cookie + .set("signed", "bar", { signed: true }) + + // mimic a signed cookie, but with a bogus signature + .set("tampered", "baz") + .set("tampered.sig", "bogus") + + res.writeHead(302, { "Location": "/" }) + return res.end("Now let's check.") + } + + unsigned = cookies.get("unsigned") + signed = cookies.get("signed", { signed: true }) + tampered = cookies.get("tampered", { signed: true }) + + res.writeHead(200, { "Content-Type": "text/plain" }) + res.end( + "unsigned expected: foo\n\n" + + "unsigned actual: " + unsigned + "\n\n" + + "signed expected: bar\n\n" + + "signed actual: " + signed + "\n\n" + + "tampered expected: undefined\n\n" + + "tampered: " + tampered + "\n\n" + ) +}) \ No newline at end of file diff --git a/cookies/cookies.d.ts b/cookies/cookies.d.ts new file mode 100644 index 0000000000..24984f7fdd --- /dev/null +++ b/cookies/cookies.d.ts @@ -0,0 +1,98 @@ +// Type definitions for cookie-parser v0.5.1 +// Project: https://github.com/pillarjs/cookies +// Definitions by: Wang Zishi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "cookies" { + import * as http from "http" + + interface ICookies { + /** + * This extracts the cookie with the given name from the + * Cookie header in the request. If such a cookie exists, + * its value is returned. Otherwise, nothing is returned. + */ + get(name: string): string; + /** + * This extracts the cookie with the given name from the + * Cookie header in the request. If such a cookie exists, + * its value is returned. Otherwise, nothing is returned. + */ + get(name: string, opts?: IOptions): string; + + /** + * This sets the given cookie in the response and returns + * the current context to allow chaining.If the value is omitted, + * an outbound header with an expired date is used to delete the cookie. + */ + set(name: string, value: string): ICookies; + /** + * This sets the given cookie in the response and returns + * the current context to allow chaining.If the value is omitted, + * an outbound header with an expired date is used to delete the cookie. + */ + set(name: string, value: string, opts?: IOptions): ICookies; + } + + interface IOptions { + /** + * a number representing the milliseconds from Date.now() for expiry + */ + maxAge?: number; + /** + * a Date object indicating the cookie's expiration + * date (expires at the end of session by default). + */ + expires?: Date; + /** + * a string indicating the path of the cookie (/ by default). + */ + path?: string; + /** + * a string indicating the domain of the cookie (no default). + */ + domain?: string; + /** + * a boolean indicating whether the cookie is only to be sent + * over HTTPS (false by default for HTTP, true by default for HTTPS). + */ + secure?: boolean; + /** + * a boolean indicating whether the cookie is only to be sent + * over HTTPS (use this if you handle SSL not in your node process). + */ + secureProxy?: boolean; + /** + * a boolean indicating whether the cookie is only to be sent over HTTP(S), + * and not made available to client JavaScript (true by default). + */ + httpOnly?: boolean; + /** + * a boolean indicating whether the cookie is to be signed (false by default). + * If this is true, another cookie of the same name with the .sig suffix + * appended will also be sent, with a 27-byte url-safe base64 SHA1 value + * representing the hash of cookie-name=cookie-value against the first Keygrip key. + * This signature key is used to detect tampering the next time a cookie is received. + */ + signed?: boolean; + /** + * a boolean indicating whether to overwrite previously set + * cookies of the same name (false by default). If this is true, + * all cookies set during the same request with the same + * name (regardless of path or domain) are filtered out of + * the Set-Cookie header when setting this cookie. + */ + overwrite?: boolean; + } + + interface CookiesStatic { + new (request: http.IncomingMessage, response: http.ServerResponse): ICookies; + new (request: http.IncomingMessage, response: http.ServerResponse, keys?: Array): ICookies; + } + + const _tmp: CookiesStatic; + + export = _tmp +} \ No newline at end of file