Skip to content

Commit 753c274

Browse files
feat: remove __dirname, __filename [EXT-6803] (#779)
* fix: __dirname problems in some hosting envs [EXT-6803]
1 parent ee124fe commit 753c274

10 files changed

Lines changed: 1775 additions & 229 deletions

File tree

package-lock.json

Lines changed: 1670 additions & 143 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
"prepare": "husky install",
3838
"lint": "eslint --ext .ts ./src",
3939
"lint:fix": "npm run lint -- --fix",
40-
"pretest": "echo ' 🔑 Creating valid keypair for testing' && sh test/make-private-keys.sh &> /dev/null",
41-
"test:unit": "mocha -r dotenv/config -r ts-node/register ./src/**/*.spec.ts",
42-
"test:integration": "mocha -r dotenv/config -r ts-node/register --timeout 10000 ./test/**/*.test.ts",
40+
"pretest": "echo ' 🔑 Creating valid keypair for testing' && bash test/make-private-keys.sh",
41+
"test:unit": "vitest run src",
42+
"test:integration": "vitest run test",
4343
"test": "npm run test:unit && npm run test:integration",
4444
"build": "rm -rf lib && tsup",
4545
"postbuild": "cp ./lib/index.d.ts ./lib/index.d.cts",
@@ -69,7 +69,9 @@
6969
"@types/sinon": "17.0.3",
7070
"@typescript-eslint/eslint-plugin": "7.10.0",
7171
"@typescript-eslint/parser": "7.10.0",
72+
"@vitest/coverage-v8": "^3.2.4",
7273
"base64url": "3.0.1",
74+
"cross-env": "^7.0.3",
7375
"dotenv": "16.4.5",
7476
"eslint": "8.57.0",
7577
"eslint-config-prettier": "9.1.0",
@@ -84,7 +86,8 @@
8486
"ts-node": "10.9.2",
8587
"tsup": "^8.3.5",
8688
"typedoc": "0.25.13",
87-
"typescript": "5.4.5"
89+
"typescript": "5.4.5",
90+
"vitest": "^3.2.4"
8891
},
8992
"lint-staged": {
9093
"*.ts": [

src/keys/get-management-token.spec.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as assert from 'assert'
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
22
import * as fs from 'fs'
33
import * as path from 'path'
44

@@ -29,6 +29,16 @@ const noop = () => {}
2929
const defaultCache: LRUCache<string, string> = new LRUCache({ max: 10 })
3030

3131
describe('getManagementToken', () => {
32+
let fetchStub: sinon.SinonStub
33+
34+
beforeEach(() => {
35+
fetchStub = sinon.stub(global, 'fetch')
36+
})
37+
38+
afterEach(() => {
39+
fetchStub.restore()
40+
})
41+
3242
it('fetches a token', async () => {
3343
const mockToken = 'token'
3444
const logger = noop as unknown as Logger
@@ -39,13 +49,13 @@ describe('getManagementToken', () => {
3949

4050
const result = await getManagementToken(PRIVATE_KEY, DEFAULT_OPTIONS)
4151

42-
assert.deepStrictEqual(result, mockToken)
43-
assert(
52+
expect(result).toEqual(mockToken)
53+
expect(
4454
post.calledWith(
4555
`spaces/${SPACE_ID}/environments/${ENVIRONMENT_ID}/app_installations/${APP_ID}/access_tokens`,
4656
sinon.match({ headers: { Authorization: sinon.match.string } }),
4757
),
48-
)
58+
).toBe(true)
4959
})
5060

5161
it('caches token while valid', async () => {
@@ -61,11 +71,11 @@ describe('getManagementToken', () => {
6171

6272
const optionsWithCaching = { ...DEFAULT_OPTIONS, reuseToken: true }
6373
const result = await getManagementToken(PRIVATE_KEY, optionsWithCaching)
64-
assert.strictEqual(result, mockToken)
74+
expect(result).toBe(mockToken)
6575
const secondResult = await getManagementToken(PRIVATE_KEY, optionsWithCaching)
66-
assert.strictEqual(secondResult, mockToken)
76+
expect(secondResult).toBe(mockToken)
6777

68-
assert(post.calledOnce)
78+
expect(post.calledOnce).toBe(true)
6979
})
7080

7181
it('does not cache expired token', async () => {
@@ -82,7 +92,7 @@ describe('getManagementToken', () => {
8292

8393
const optionsWithCaching = { ...DEFAULT_OPTIONS, reuseToken: true }
8494
const result = await getManagementToken(PRIVATE_KEY, optionsWithCaching)
85-
assert.strictEqual(result, mockToken)
95+
expect(result).toBe(mockToken)
8696

8797
// Overwrite TTL expiry to 5ms
8898
const cacheKey = APP_ID + SPACE_ID + ENVIRONMENT_ID + PRIVATE_KEY.slice(32, 132)
@@ -94,8 +104,8 @@ describe('getManagementToken', () => {
94104
})
95105

96106
const secondResult = await getManagementToken(PRIVATE_KEY, optionsWithCaching)
97-
assert.strictEqual(secondResult, mockToken)
98-
assert(post.calledTwice)
107+
expect(secondResult).toBe(mockToken)
108+
expect(post.calledTwice).toBe(true)
99109
})
100110

101111
describe('when using a keyId', () => {
@@ -109,27 +119,27 @@ describe('getManagementToken', () => {
109119

110120
const result = await getManagementToken(PRIVATE_KEY, { ...DEFAULT_OPTIONS, keyId: 'keyId' })
111121

112-
assert.deepStrictEqual(result, mockToken)
113-
assert(
122+
expect(result).toEqual(mockToken)
123+
expect(
114124
post.calledWith(
115125
`spaces/${SPACE_ID}/environments/${ENVIRONMENT_ID}/app_installations/${APP_ID}/access_tokens`,
116126
sinon.match({ headers: { Authorization: sinon.match.string } }),
117127
),
118-
)
128+
).toBe(true)
119129
})
120130
})
121131

122132
describe('when private key is incorrect', () => {
123133
it('throws if missing', async () => {
124-
await assert.rejects(async () => {
134+
await expect(async () => {
125135
// @ts-ignore Testing javascript code
126136
await getManagementToken(undefined, DEFAULT_OPTIONS)
127-
})
137+
}).rejects.toThrow()
128138
})
129139
it('throws if generated with wrong algorithm', async () => {
130-
await assert.rejects(async () => {
140+
await expect(async () => {
131141
await getManagementToken('not_a_private_key', DEFAULT_OPTIONS)
132-
})
142+
}).rejects.toThrow()
133143
})
134144
})
135145

@@ -140,9 +150,9 @@ describe('getManagementToken', () => {
140150
const httpClient = { post } as unknown as HttpClient
141151
const getManagementToken = createGetManagementToken(logger, httpClient, defaultCache)
142152

143-
await assert.rejects(async () => {
153+
await expect(async () => {
144154
await getManagementToken(PRIVATE_KEY, DEFAULT_OPTIONS)
145-
}, HttpError)
155+
}).rejects.toThrow(HttpError)
146156
})
147157
})
148158
})

src/keys/get-management-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export const getManagementToken = (privateKey: string, opts: GetManagementTokenO
154154
const httpClientOpts = typeof opts.host !== 'undefined' ? { prefixUrl: opts.host } : {}
155155

156156
return createGetManagementToken(
157-
createLogger({ filename: __filename }),
157+
createLogger({ namespace: 'get-management-token.js' }),
158158
createHttpClient(httpClientOpts),
159159
defaultCache!,
160160
)(privateKey, opts)

src/requests/sign-request.spec.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as assert from 'assert'
1+
import { describe, it, expect } from 'vitest'
22
import { CanonicalRequest, Secret } from './typings'
33
import { signRequest } from './sign-request'
44

@@ -11,7 +11,7 @@ const VALID_REQUEST: CanonicalRequest = {
1111

1212
const assertThrowsForFieldInValues = (field: keyof CanonicalRequest, values: any[]) => {
1313
for (const value of values) {
14-
assert.throws(() => {
14+
expect(() => {
1515
signRequest(
1616
VALID_SECRET,
1717
{
@@ -21,7 +21,7 @@ const assertThrowsForFieldInValues = (field: keyof CanonicalRequest, values: any
2121
},
2222
VALID_TIMESTAMP,
2323
)
24-
}, `Did not throw for ${field.toString()}:${value}`)
24+
}).toThrow()
2525
}
2626
}
2727

@@ -41,15 +41,15 @@ describe('create-signature', () => {
4141
})
4242
it('does not throw with empty bodies', () => {
4343
const { body, ...requestWithoutBody } = VALID_REQUEST
44-
assert.doesNotThrow(() => {
44+
expect(() => {
4545
signRequest(VALID_SECRET, requestWithoutBody, VALID_TIMESTAMP)
46-
})
46+
}).not.toThrow()
4747
})
4848
it('does not throw with empty headers', () => {
4949
const { headers, ...requestWithoutBody } = VALID_REQUEST
50-
assert.doesNotThrow(() => {
50+
expect(() => {
5151
signRequest(VALID_SECRET, requestWithoutBody, VALID_TIMESTAMP)
52-
})
52+
}).not.toThrow()
5353
})
5454
})
5555

@@ -58,16 +58,16 @@ describe('create-signature', () => {
5858
const invalidSecrets = [undefined, false, 'too-short', 1103379941037 /* too old */]
5959

6060
for (const secret of invalidSecrets) {
61-
assert.throws(() => {
61+
expect(() => {
6262
// @ts-ignore
6363
signRequest(secret, VALID_REQUEST, VALID_TIMESTAMP)
64-
}, `Did not throw for ${secret}`)
64+
}).toThrow()
6565
}
6666
})
6767
it('does not throw if valid', () => {
68-
assert.doesNotThrow(() => {
68+
expect(() => {
6969
signRequest(VALID_SECRET, VALID_REQUEST, VALID_TIMESTAMP)
70-
})
70+
}).not.toThrow()
7171
})
7272
})
7373

@@ -76,16 +76,16 @@ describe('create-signature', () => {
7676
const invalidTimestamps = [1, false, 'string']
7777

7878
for (const timestamp of invalidTimestamps) {
79-
assert.throws(() => {
79+
expect(() => {
8080
// @ts-ignore
8181
signRequest(VALID_SECRET, VALID_REQUEST, timestamp)
82-
}, `Did not throw for ${timestamp}`)
82+
}).toThrow()
8383
}
8484
})
8585
it('does not throw if missing', () => {
86-
assert.doesNotThrow(() => {
86+
expect(() => {
8787
signRequest(VALID_SECRET, VALID_REQUEST)
88-
})
88+
}).not.toThrow()
8989
})
9090
})
9191

@@ -95,12 +95,13 @@ describe('create-signature', () => {
9595

9696
const headers = { headerOne }
9797

98-
assert.notStrictEqual(
98+
expect(
9999
signRequest(
100100
VALID_SECRET,
101101
{ ...VALID_REQUEST, path: '/api/resources?q=1&w=2', headers },
102102
VALID_TIMESTAMP,
103103
),
104+
).not.toBe(
104105
signRequest(
105106
VALID_SECRET,
106107
{ ...VALID_REQUEST, path: '/api/resources?w=2&q=1', headers },
@@ -114,12 +115,13 @@ describe('create-signature', () => {
114115

115116
const headers = { headerOne }
116117

117-
assert.notStrictEqual(
118+
expect(
118119
signRequest(
119120
VALID_SECRET,
120121
{ ...VALID_REQUEST, path: '/api/resources?q=1&w=2', headers },
121122
VALID_TIMESTAMP,
122123
),
124+
).not.toBe(
123125
signRequest(
124126
VALID_SECRET,
125127
{ ...VALID_REQUEST, path: '/api/resources?q=12', headers },
@@ -133,7 +135,7 @@ describe('create-signature', () => {
133135

134136
const headers = { headerTwo }
135137

136-
assert.doesNotThrow(() =>
138+
expect(() =>
137139
signRequest(
138140
VALID_SECRET,
139141
{
@@ -142,7 +144,7 @@ describe('create-signature', () => {
142144
},
143145
VALID_TIMESTAMP,
144146
),
145-
)
147+
).not.toThrow()
146148
})
147149

148150
it('generates same signature if headers key are provided with different casing', () => {
@@ -152,8 +154,7 @@ describe('create-signature', () => {
152154
const headers = { headerOne, headerTwo }
153155
const headersCased = { headerone: headerOne, headerTWO: headerTwo }
154156

155-
assert.deepStrictEqual(
156-
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP),
157+
expect(signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP)).toEqual(
157158
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers: headersCased }, VALID_TIMESTAMP),
158159
)
159160
})
@@ -164,15 +165,13 @@ describe('create-signature', () => {
164165
const headers = { headerOne, headerTwo }
165166
const headersSpaced = { ' headerOne': headerOne, 'headerTwo ': headerTwo }
166167

167-
assert.deepStrictEqual(
168-
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP),
168+
expect(signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP)).toEqual(
169169
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers: headersSpaced }, VALID_TIMESTAMP),
170170
)
171171
})
172172
it('generates different signatures with different secrets', () => {
173173
const newSecret = `q${VALID_SECRET.slice(1, VALID_SECRET.length)}`
174-
assert.notStrictEqual(
175-
signRequest(newSecret, VALID_REQUEST, VALID_TIMESTAMP),
174+
expect(signRequest(newSecret, VALID_REQUEST, VALID_TIMESTAMP)).not.toBe(
176175
signRequest(VALID_SECRET, VALID_REQUEST, VALID_TIMESTAMP),
177176
)
178177
})
@@ -181,16 +180,15 @@ describe('create-signature', () => {
181180
const headers = { 'x-contentful-webhook-request-attempt': '1' }
182181
const retryHeaders = { 'x-contentful-webhook-request-attempt': '2' }
183182

184-
assert.notStrictEqual(
185-
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP),
183+
expect(signRequest(VALID_SECRET, { ...VALID_REQUEST, headers }, VALID_TIMESTAMP)).not.toBe(
186184
signRequest(VALID_SECRET, { ...VALID_REQUEST, headers: retryHeaders }, VALID_TIMESTAMP),
187185
)
188186
})
189187

190188
it('does not return undefined headers', () => {
191189
const result = signRequest(VALID_SECRET, VALID_REQUEST, undefined)
192190

193-
assert.ok(Object.values(result).every((h) => typeof h !== 'undefined'))
191+
expect(Object.values(result).every((h) => typeof h !== 'undefined')).toBe(true)
194192
})
195193

196194
it('CRN header is optional', () => {
@@ -200,8 +198,8 @@ describe('create-signature', () => {
200198
envId: 'envId',
201199
})
202200

203-
assert.ok(!result['x-contentful-signed-headers'].includes('x-contentful-crn'))
204-
assert.equal(result['x-contentful-crn'], undefined)
201+
expect(result['x-contentful-signed-headers'].includes('x-contentful-crn')).toBe(false)
202+
expect(result['x-contentful-crn']).toBe(undefined)
205203
})
206204

207205
it('includes CRN header', () => {
@@ -212,8 +210,8 @@ describe('create-signature', () => {
212210
envId: 'envId',
213211
})
214212

215-
assert.ok(result['x-contentful-signed-headers'].includes('x-contentful-crn'))
216-
assert.equal(result['x-contentful-crn'], 'this-is-a-crn')
213+
expect(result['x-contentful-signed-headers'].includes('x-contentful-crn')).toBe(true)
214+
expect(result['x-contentful-crn']).toBe('this-is-a-crn')
217215
})
218216
})
219217
})

0 commit comments

Comments
 (0)