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

Enable isolated declarations #434

Closed
wants to merge 5 commits into from
Closed
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 lib/__tests__/data/dates/bsd-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ export default [
test: 'IAintNoDateFool',
expected: null,
},
]
] as const
2 changes: 1 addition & 1 deletion lib/__tests__/data/dates/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ export default [
test: 'Thu, 10 Dec 2009 13:57:2 GMT',
expected: 'Thu, 10 Dec 2009 13:57:02 GMT',
},
]
] as const
2 changes: 1 addition & 1 deletion lib/__tests__/data/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,4 @@ export default [
received: ['\tfoo\t=\tbar\t \t;\tttt'],
sent: [{ name: 'foo', value: 'bar' }],
},
]
] as const
7 changes: 4 additions & 3 deletions lib/__tests__/ietf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ describe('IETF http state tests', () => {
const jar = new CookieJar()
const expected = testCase.sent
const sentFrom = `http://home.example.org/cookie-parser?${testCase.test}`
const sentTo = testCase['sent-to']
? url.resolve('http://home.example.org', testCase['sent-to'])
: `http://home.example.org/cookie-parser-result?${testCase.test}`
const sentTo =
'sent-to' in testCase
? url.resolve('http://home.example.org', testCase['sent-to'])
: `http://home.example.org/cookie-parser-result?${testCase.test}`

testCase['received'].forEach((cookieStr) => {
jar.setCookieSync(cookieStr, sentFrom, { ignoreError: true })
Expand Down
8 changes: 6 additions & 2 deletions lib/cookie/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
* - `unsafe-disabled` - Disables cookie prefix checking.
* @public
*/
export const PrefixSecurityEnum = Object.freeze({
export const PrefixSecurityEnum: {
readonly SILENT: 'silent'
readonly STRICT: 'strict'
readonly DISABLED: 'unsafe-disabled'
} = Object.freeze({
SILENT: 'silent',
STRICT: 'strict',
DISABLED: 'unsafe-disabled',
Expand All @@ -32,7 +36,7 @@ const IP_V6_REGEX = `
.replace(/\s*\/\/.*$/gm, '')
.replace(/\n/g, '')
.trim()
export const IP_V6_REGEX_OBJECT = new RegExp(`^${IP_V6_REGEX}$`)
export const IP_V6_REGEX_OBJECT: RegExp = new RegExp(`^${IP_V6_REGEX}$`)

/**
* A JSON representation of a {@link CookieJar}.
Expand Down
34 changes: 20 additions & 14 deletions lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,6 @@ export class Cookie {
this.creationIndex = Cookie.cookiesCreated
}

[Symbol.for('nodejs.util.inspect.custom')](): string {
const now = Date.now()
const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : '?'
const createAge =
this.creation && this.creation !== 'Infinity'
? `${String(now - this.creation.getTime())}ms`
: '?'
const accessAge =
this.lastAccessed && this.lastAccessed !== 'Infinity'
? `${String(now - this.lastAccessed.getTime())}ms`
: '?'
return `Cookie="${this.toString()}; hostOnly=${hostOnly}; aAge=${accessAge}; cAge=${createAge}"`
}

/**
* For convenience in using `JSON.stringify(cookie)`. Returns a plain-old Object that can be JSON-serialized.
*
Expand Down Expand Up @@ -987,3 +973,23 @@ export class Cookie {
'sameSite',
] as const
}

// Using TypeScript's isolated declarations mode means we can't use computed properties.
// Creating this method outside the class means that it's not visible to TypeScript code,
// even though it is present at runtime. That's probably fine, because this is a low-level
// detail that we don't expect users to be calling directly.
;(Cookie.prototype as unknown as Record<symbol, unknown>)[
Symbol.for('nodejs.util.inspect.custom')
] = function inspect(this: Cookie): string {
const now = Date.now()
const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : '?'
const createAge =
this.creation && this.creation !== 'Infinity'
? `${String(now - this.creation.getTime())}ms`
: '?'
const accessAge =
this.lastAccessed && this.lastAccessed !== 'Infinity'
? `${String(now - this.lastAccessed.getTime())}ms`
: '?'
return `Cookie="${this.toString()}; hostOnly=${hostOnly}; aAge=${accessAge}; cAge=${createAge}"`
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"version": "genversion --template version-template.ejs --force lib/version.ts && git add lib/version.ts",
"test": "npm run test:ts && npm run test:legacy",
"test:ts": "jest",
"test:legacy": "npm run build -- --declaration false && ./test/scripts/vows.js test/*_test.js",
"test:legacy": "npm run build -- --isolatedDeclarations false --declaration false && ./test/scripts/vows.js test/*_test.js",
"typecheck": "tsc --noEmit",
"cover": "jest --coverage",
"lint": "eslint .",
Expand Down
7 changes: 3 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"rootDir": "./lib/",
/* Emit */
"declaration": true,
"isolatedDeclarations": true,
"outDir": "./dist/",
"importsNotUsedAsValues": "error",
"verbatimModuleSyntax": false,
/* Interop Constraints */
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand All @@ -24,9 +25,7 @@
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
/* Compatibility */
"ignoreDeprecations": "5.0"
"allowUnreachableCode": false
},
"include": ["lib"],
"exclude": ["jest.config.ts"]
Expand Down
Loading