11"use strict" ;
22
3- const fs = require ( "fs" ) ;
3+ const fs = require ( "fs/promises " ) ;
44const http = require ( "http" ) ;
55const https = require ( "https" ) ;
66const path = require ( "path" ) ;
77const topSites = require ( "top-sites" ) ;
88const url = require ( "url" ) ;
99
10- const BENCH_COOKIES_FILE = path . join ( __dirname , "parse-top.json" ) ;
11- const domains = topSites . slice ( 0 , 30 ) . map ( ( x ) => x . rootDomain ) ;
12-
13- getAllCookies ( domains , function ( err , cookies ) {
14- if ( err ) throw err ;
15-
16- const str = JSON . stringify (
17- Object . fromEntries (
18- Object . keys ( cookies )
19- . sort ( )
20- . map ( ( key ) => [ key , cookies [ key ] ] )
21- . concat ( [ [ "example.com" , "" ] ] ) ,
22- ) ,
23- null ,
24- 2 ,
10+ const BENCH_COOKIES_FILE = path . join ( __dirname , "top-cookie.json" ) ;
11+ const BENCH_SET_COOKIES_FILE = path . join ( __dirname , "top-set-cookie.json" ) ;
12+ const domains = topSites . slice ( 0 , 20 ) . map ( ( x ) => x . rootDomain ) ;
13+
14+ async function main ( ) {
15+ const [ cookies , setCookies ] = await getAllCookies ( domains ) ;
16+
17+ await fs . writeFile (
18+ BENCH_COOKIES_FILE ,
19+ JSON . stringify ( sortObject ( cookies ) , null , 2 ) + "\n" ,
2520 ) ;
2621
27- fs . writeFile ( BENCH_COOKIES_FILE , `${ str } \n` , function ( err ) {
28- if ( err ) throw err ;
29- console . log ( "Cookies saved to" , BENCH_COOKIES_FILE ) ;
30- process . exit ( ) ;
31- } ) ;
32- } ) ;
22+ await fs . writeFile (
23+ BENCH_SET_COOKIES_FILE ,
24+ JSON . stringify ( sortObject ( setCookies ) , null , 2 ) + "\n" ,
25+ ) ;
26+
27+ console . log ( "Cookies saved" ) ;
28+ process . exit ( ) ;
29+ }
30+
31+ main ( ) ;
3332
34- function get ( href , callback ) {
33+ function get ( href ) {
3534 const protocol = url . parse ( href , false , true ) . protocol ;
3635 const proto = protocol === "https:" ? https : http ;
3736
38- proto
39- . get ( href )
40- . on ( "error" , callback )
41- . on ( "response" , function ( res ) {
42- if (
43- res . headers . location &&
44- res . statusCode >= 300 &&
45- res . statusCode < 400
46- ) {
47- get ( url . resolve ( href , res . headers . location ) , callback ) ;
48- } else {
49- callback ( null , res ) ;
50- }
51- } ) ;
37+ return new Promise ( ( resolve , reject ) => {
38+ proto
39+ . get ( href )
40+ . on ( "error" , reject )
41+ . on ( "response" , function ( res ) {
42+ if (
43+ res . headers . location &&
44+ res . statusCode >= 300 &&
45+ res . statusCode < 400
46+ ) {
47+ return resolve ( get ( url . resolve ( href , res . headers . location ) ) ) ;
48+ }
49+
50+ return resolve ( res ) ;
51+ } ) ;
52+ } ) ;
5253}
5354
54- function getAllCookies ( domains , callback ) {
55- const all = Object . create ( null ) ;
56- let wait = domains . length ;
57-
58- domains . forEach ( function ( domain ) {
59- getCookies ( domain , function ( err , cookies ) {
60- if ( ! err && cookies . length ) {
61- all [ domain ] = cookies . map ( obfuscate ) . join ( "; " ) ;
62- }
63- if ( ! -- wait ) {
64- callback ( null , all ) ;
65- }
55+ async function getAllCookies ( domains ) {
56+ const allCookies = Object . create ( null ) ;
57+ const allSetCookies = Object . create ( null ) ;
58+
59+ for ( const domain of domains ) {
60+ const setCookies = await getSetCookies ( domain ) ;
61+ if ( ! setCookies . length ) continue ;
62+
63+ const cookies = toCookies ( setCookies ) ;
64+ allCookies [ domain ] = cookies . map ( obfuscate ) . join ( "; " ) ;
65+ allSetCookies [ domain ] = setCookies . map ( ( header , index ) => {
66+ const attrs = header . split ( ";" ) ;
67+ return [ obfuscate ( attrs . shift ( ) , index ) , ...attrs ] . join ( "; " ) ;
6668 } ) ;
67- } ) ;
69+ }
70+
71+ return [ allCookies , allSetCookies ] ;
6872}
6973
70- function getCookies ( domain , callback ) {
74+ async function getSetCookies ( domain ) {
7175 const href = url . format ( { hostname : domain , protocol : "http" } ) ;
72- get ( href , function ( err , res ) {
73- if ( err ) return callback ( err ) ;
74- const cookies = ( res . headers [ "set-cookie" ] || [ ] ) . map ( function ( c ) {
75- return c . split ( ";" ) [ 0 ] ;
76- } ) ;
77- callback ( null , cookies ) ;
78- } ) ;
76+ try {
77+ const res = await get ( href ) ;
78+ return res . headers [ "set-cookie" ] || [ ] ;
79+ } catch ( err ) {
80+ if ( err . code === "ENOTFOUND" ) return [ ] ;
81+ throw err ;
82+ }
83+ }
84+
85+ function toCookies ( setCookies ) {
86+ return setCookies . map ( ( c ) => c . split ( ";" ) [ 0 ] ) ;
7987}
8088
8189function obfuscate ( str , index ) {
@@ -96,3 +104,7 @@ function obfuscate(str, index) {
96104 return "%22" ;
97105 } ) ;
98106}
107+
108+ function sortObject ( obj ) {
109+ return Object . fromEntries ( Object . entries ( obj ) . sort ( ) ) ;
110+ }
0 commit comments