Skip to content

Commit

Permalink
feat: auto update USER_AGENT version to urllib package.version (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored May 21, 2023
1 parent fc1d599 commit d4e5f39
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
},
"scripts": {
"lint": "eslint src test --ext .ts --cache",
"build": "npm run clean && npm run build:dist",
"prebuild": "npm run clean",
"build": "tsc --version && npm run build:cjs && npm run build:esm && npm run build:version",
"postbuild": "rm -rf src/*.tsbuildinfo",
"build:cjs": "tsc -p ./tsconfig.build.cjs.json",
"build:esm": "tsc -p ./tsconfig.build.esm.json && node ./scripts/esm_import_fix.js",
"build:dist": "tsc --version && npm run build:cjs && npm run build:esm",
"build:version": "node ./scripts/replace_urllib_version.js",
"build:cjs:test": "cd test/cjs && rm -rf node_modules && npm link ../.. && node index.js",
"build:esm:test": "cd test/esm && rm -rf node_modules && npm link ../.. && node index.js",
"build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run test-tsc",
Expand All @@ -55,7 +57,7 @@
"ci": "npm run lint && npm run cov && npm run build:test",
"contributor": "git-contributor",
"clean": "rm -rf src/*.tsbuildinfo src/cjs/*.ts src/cjs/*.js src/esm/*.ts src/esm/*.js",
"prepack": "npm run build && rm -rf src/*.tsbuildinfo"
"prepublishOnly": "npm run build"
},
"dependencies": {
"default-user-agent": "^1.0.0",
Expand Down
28 changes: 28 additions & 0 deletions scripts/replace_urllib_version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

import fs from 'fs/promises';
import path from 'path';

async function main() {
const root = process.cwd();
const pkg = JSON.parse(await fs.readFile(path.join(root, 'package.json')));
const files = [
path.join(root, 'src/cjs/HttpClient.js'),
path.join(root, 'src/esm/HttpClient.js'),
];
for (const file of files) {
const content = await fs.readFile(file, 'utf-8');
// replace "('node-urllib', 'VERSION')" to "('node-urllib', 'pkg.version')"
const newContent = content.replace(/\(\'node-urllib\', \'VERSION\'\)/, (match) => {
const after = `('node-urllib', '${pkg.version}')`;
console.log('[%s] %s => %s', file, match, after);
return after;
});
if (newContent !== content) {
await fs.writeFile(file, newContent);
console.log('Replace version on %s', file);
}
}
}

main();
2 changes: 1 addition & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class HttpClientRequestTimeoutError extends Error {
}
}

export const HEADER_USER_AGENT = createUserAgent('node-urllib', '3.0.0');
export const HEADER_USER_AGENT = createUserAgent('node-urllib', 'VERSION');

function getFileName(stream: Readable) {
const filePath: string = (stream as any).path;
Expand Down
2 changes: 1 addition & 1 deletion test/options.headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('options.headers.test.ts', () => {
});
assert.equal(status, 200);
assert.equal(headers['x-foo'], 'bar');
assert.match(data.headers['user-agent'], /node-urllib\/3\.0\.0 Node\.js\//);
assert.match(data.headers['user-agent'], /node-urllib\/VERSION Node\.js\//);
assert.equal(data.headers['accept-encoding'], undefined);
assert.equal(data.headers.connection, 'keep-alive');
assert.equal(data.headers.accept, 'application/json');
Expand Down
2 changes: 1 addition & 1 deletion test/user-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('keep-alive-header.test.ts', () => {
});
assert.equal(response.status, 200);
// console.log(response.data.headers);
assert.match(response.data.headers['user-agent'], /^node\-urllib\/3\.\d+\.\d+ Node\.js\/\d+\.\d+\.\d+ \(/);
assert.match(response.data.headers['user-agent'], /^node\-urllib\/VERSION Node\.js\/\d+\.\d+\.\d+ \(/);
});

it('should return no user agent if user-agent header is set to empty string', async () => {
Expand Down

0 comments on commit d4e5f39

Please sign in to comment.