url#parse TypeScript Examples

The following examples show how to use url#parse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlaywrightController.ts    From mockiavelli with MIT License 6 votes vote down vote up
private toBrowserRequest(request: PlaywrightRequest): BrowserRequest {
        // TODO find a better alternative for url.parse
        const { pathname, query, protocol, host } = parse(request.url(), true);

        return {
            type: request.resourceType() as BrowserRequestType,
            url: request.url(),
            body: tryJsonParse(request.postData()),
            method: request.method(),
            headers: (request.headers() as Record<string, string>) || {},
            path: pathname ?? '',
            hostname: `${protocol}//${host}`,
            query: query,
            sourceOrigin: this.getRequestOrigin(request),
        };
    }
Example #2
Source File: index.ts    From Uptimo with MIT License 6 votes vote down vote up
app.prepare().then(async() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url!, true)
    handle(req, res, parsedUrl)
  }).listen(port);

  
  const { ping } = await import('../lib/pinger');
  ping();

  // tslint:disable-next-line:no-console
  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? 'development' : process.env.NODE_ENV
    }`
  )
})
Example #3
Source File: index.ts    From lerna-monorepo-typescript-example with MIT License 6 votes vote down vote up
app.prepare().then(() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    const { pathname, query } = parsedUrl;

    if (pathname === '/') {
      app.render(req, res, '/', query);
    } else {
      handle(req, res, parsedUrl);
    }
  }).listen(port);

  logger(
    `> Server listening at http://localhost:${port} as ${
      dev ? 'development' : process.env.NODE_ENV
    }`,
  );
});
Example #4
Source File: setupServer.ts    From next-api-decorators with MIT License 6 votes vote down vote up
export function setupServer(handler: NextApiHandler, disableBodyParser?: boolean): http.Server | express.Express {
  if (process.env.TEST_SERVER === 'nextjs') {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    handler.config = { api: { bodyParser: !disableBodyParser } };
    return http.createServer((req, res) => {
      const parsedUrl = parse(req.url as any, true);
      return apiResolver(req, res, parsedUrl.query, handler, {} as any, false);
    });
  }

  return express()
    .use(express.json())
    .all('*', handler as any);
}
Example #5
Source File: lockfile.test.ts    From tesseract-server with MIT License 6 votes vote down vote up
describe('yarn.lock', () => {
  it('should only have resolved urls from registry.npmjs.org', async () => {
    const lockfile = await readFile(join(__dirname, '../yarn.lock'), {
      encoding: 'utf8',
    });

    const matches = Array.from(lockfile.matchAll(RESOLVED_REGEX));
    matches
      .map(match => match[1]?.trim())
      .filter(url => url?.trim())
      .map(url => parse(url))
      .forEach(url => {
        expect(url.hostname).toBe('registry.npmjs.org');
      });
  });
});
Example #6
Source File: next-fixture.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
test = base.extend<{
  port: string;
}>({
  port: [
    // eslint-disable-next-line no-empty-pattern
    async ({}, use) => {
      const app = next({
        dev: false,
        dir: path.resolve(__dirname, ".."),
      });
      await app.prepare();
      const handle = app.getRequestHandler();
      // start next server on arbitrary port
      const server: Server = await new Promise((resolve) => {
        const server = createServer((req, res) => {
          if (req.url) {
            const parsedUrl = parse(req.url, true);
            handle(req, res, parsedUrl);
          }
        });
        server.listen((error: any) => {
          if (error) throw error;
          resolve(server);
        });
      });
      // get the randomly assigned port from the server
      const port = String((server.address() as AddressInfo).port);
      // provide port to tests
      await use(port);
    },
    {
      //@ts-ignore
      scope: "worker",
    },
  ],
})
Example #7
Source File: git.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
static getGitProviderOwnerAndRepositoryFromHttpUrl(remote: string): string[] {
    // it must be http or https based remote
    const { protocol = "https:", hostname, pathname } = parse(remote);
    if (!protocol) {
      throw Error("impossible");
    }
    // domain names are not case-sensetive
    if (!hostname || !pathname) {
      throw new Error("Not a Provider remote!");
    }
    const match = pathname.match(/\/(.*?)\/(.*?)(?:.git)?$/);
    if (!match) {
      throw new Error("Not a Provider remote!");
    }
    return [protocol, hostname, ...match.slice(1, 3)];
  }
Example #8
Source File: Image.ts    From Asena with MIT License 6 votes vote down vote up
isValidImage(): Promise<boolean>{
        return new Promise<boolean>(resolve => {
            const q = parse(this.url, true);
            const options = {
                path: q.pathname,
                host: q.hostname,
                port: q.port,
                headers: {
                    'Content-Type': 'arraybuffer',
                    'Range': 'bytes=0-7'
                },
                timeout: 2500
            }

            const request = https.get(options, res => {
                const chunks = []

                res.on('data', data => chunks.push(data))
                res.on('end', () => {
                    const buffer = Buffer.concat(chunks)
                    for(const bytes of MagicBytes){
                        if(bytes.equals(buffer.slice(0, bytes.length))){
                            resolve(true)
                        }
                    }

                    resolve(false)
                })
                res.on('error', () => {
                    resolve(false)
                })
            })

            request.on('timeout', () => {
                request.destroy()
                resolve(false)
            })
        })
    }
Example #9
Source File: koa-cors.ts    From aiyou-wechaty-robot with Mozilla Public License 2.0 6 votes vote down vote up
koaCors = async (ctx: Context, next) => {
  const origin = parse(ctx.get('origin') || ctx.get('referer') || '');
  if (origin.protocol && origin.host) {
    ctx.set('Access-Control-Allow-Origin', `${origin.protocol}//${origin.host}`);
    ctx.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT');
    ctx.set('Access-Control-Allow-Headers', 'X-Requested-With, User-Agent, Referer, Content-Type, Cache-Control,accesstoken');
    ctx.set('Access-Control-Max-Age', '86400');
    ctx.set('Access-Control-Allow-Credentials', 'true');
  }
  if (ctx.method !== 'OPTIONS') {
    await next();
  } else {
    ctx.body = '';
    ctx.status = 204;
  }
}
Example #10
Source File: browserRequest.ts    From mockiavelli with MIT License 6 votes vote down vote up
browserRequest = makeFactory<BrowserRequest>({
    url: '/',
    method: 'GET',
    path: '',
    hostname: '',
    body: '',
    type: 'xhr',
    headers: {},
    query: {},
    sourceOrigin: '',
})
    .withDerivation('path', req => parse(req.url).pathname || '')
    .withDerivation('hostname', req => parse(req.url).hostname || '')
    .withDerivation('query', req => parse(req.url, true).query || {})
    .withDerivation('hostname', req => {
        const { protocol, host } = parse(req.url);
        return `${protocol}//${host}`;
    })
    .withDerivation('sourceOrigin', req => req.hostname)
Example #11
Source File: utils.ts    From mockiavelli with MIT License 6 votes vote down vote up
export function getOrigin(originUrl?: string) {
    const { protocol, host } = parse(originUrl ?? '');
    if (protocol && host) {
        return `${protocol}//${host}`;
    }
    return '';
}
Example #12
Source File: utils.ts    From mockiavelli with MIT License 6 votes vote down vote up
export function tryJsonParse(data: any): any | undefined {
    try {
        return JSON.parse(data);
    } catch (e) {
        return data;
    }
}
Example #13
Source File: mock.ts    From mockiavelli with MIT License 6 votes vote down vote up
private parseMatcher(matcher: RequestMatcher) {
        // TODO find a better alternative for url.parse
        const { protocol, host, pathname, query } = parse(matcher.url, true);
        const hasHostname = protocol && host;

        return {
            method: matcher.method,
            hostname: hasHostname ? `${protocol}//${host}` : undefined,
            query: matcher.query ? matcher.query : query,
            path: pathname ?? '',
            pathMatch: match<PathParameters>(pathname ?? ''),
            body: matcher.body,
        };
    }
Example #14
Source File: PuppeteerController.ts    From mockiavelli with MIT License 6 votes vote down vote up
private toBrowserRequest(request: PuppeteerRequest): BrowserRequest {
        // TODO find a better alternative for url.parse
        const { pathname, query, protocol, host } = parse(request.url(), true);

        return {
            type: request.resourceType(),
            url: request.url(),
            body: tryJsonParse(request.postData()),
            method: request.method(),
            headers: request.headers() || {},
            path: pathname ?? '',
            hostname: `${protocol}//${host}`,
            query: query,
            sourceOrigin: this.getRequestOrigin(request),
        };
    }
Example #15
Source File: parser.ts    From banners with MIT License 5 votes vote down vote up
export function parseRequest(req: IncomingMessage) {
    console.log('HTTP ' + req.url);
    const { pathname, query } = parse(req.url || '/', true);
    const { fontSize, images, widths, heights, theme, md, showWatermark, pattern, packageManager, packageName, description, style } = (query || {});

    if (Array.isArray(fontSize)) {
        throw new Error('Expected a single fontSize');
    }
    if (Array.isArray(theme)) {
        throw new Error('Expected a single theme');
    }
    if (Array.isArray(style)) {
        throw new Error('Expected a single style');
    }
    if (Array.isArray(pattern)) {
        throw new Error('Expected a single pattern');
    }
    if (Array.isArray(packageManager)) {
        throw new Error('Expected a single package manager');
    }
    if (Array.isArray(packageName)) {
        throw new Error('Expected a single package name');
    }
    if (Array.isArray(description)) {
        throw new Error('Expected a single package name');
    }
    
    const arr = (pathname || '/').slice(1).split('.');
    let extension = '';
    let text = '';
    if (arr.length === 0) {
        text = '';
    } else if (arr.length === 1) {
        text = arr[0];
    } else {
        extension = arr.pop() as string;
        text = arr.join('.');
    }

    const parsedRequest: ParsedRequest = {
        fileType: extension === 'jpeg' ? extension : 'png',
        text: decodeURIComponent(text),
        theme: theme === 'dark' ? 'dark' : 'light',
        style: style === 'style_2' ? 'style_2' : 'style_1',
        md: md === '1' || md === 'true',
        showWatermark: showWatermark === '1' || showWatermark === 'true',
        fontSize: fontSize || '96px',
        pattern: pattern || 'circuitBoard',
        packageManager: packageManager || '',
        packageName: packageName || '',
        description: description || '',
        images: getArray(images),
        widths: getArray(widths),
        heights: getArray(heights),
    };
    parsedRequest.images = getDefaultImages(parsedRequest.images);
    return parsedRequest;
}
Example #16
Source File: PuppeteerRequest.ts    From mockiavelli with MIT License 5 votes vote down vote up
public static create(
        data: Partial<{
            postData: string;
            url: string;
            method: ReturnType<PuppeteerRequest['method']>;
            headers: ReturnType<PuppeteerRequest['headers']>;
            resourceType: ReturnType<PuppeteerRequest['resourceType']>;
            isNavigationRequest: boolean;
            frame: ReturnType<PuppeteerRequest['frame']>;
            redirectChain: PuppeteerRequestMock[];
            response: Response;
            origin: string;
        }>
    ) {
        const req = new PuppeteerRequestMock();

        if (data.postData !== undefined) {
            req._postData = data.postData;
        }

        if (data.url !== undefined) {
            req._url = data.url;
        }

        if (data.method !== undefined) {
            req._method = data.method;
        }

        if (data.headers !== undefined) {
            req._headers = data.headers;
        }

        if (data.isNavigationRequest !== undefined) {
            req._isNavigationRequest = data.isNavigationRequest;
        }

        if (data.redirectChain) {
            req._redirectChain = data.redirectChain;
        }

        if (data.resourceType) {
            req._resourceType = data.resourceType;
        }

        if (data.response) {
            req._response = data.response;
        }

        if (data.origin !== undefined) {
            // @ts-ignore
            req._frame = new FrameFixture(data.origin);
        } else {
            const { protocol, host } = parse(req._url);
            const origin = `${protocol}//${host}`;
            // @ts-ignore
            req._frame = new FrameFixture(origin);
        }

        return req;
    }
Example #17
Source File: admin.ts    From hoprnet with GNU General Public License v3.0 5 votes vote down vote up
async setup() {
    let adminPath: string
    for (const adminRelPath of ['../hopr-admin', './hopr-admin']) {
      const adminPathInt = new URL(adminRelPath, import.meta.url).pathname
      const nextPath = path.resolve(adminPathInt, '.next')
      if (!fs.existsSync(nextPath)) {
        continue
      }
      adminPath = adminPathInt
      break
    }

    if (!adminPath) {
      console.log('Failed to start Admin interface: could not find NextJS app')
      process.exit(1)
    }

    debugLog('using', adminPath)

    const nextConfig = {
      dev: NODE_ENV === 'development',
      dir: adminPath
    } as any

    if (NODE_ENV === 'development') {
      nextConfig.conf = {
        distDir: `build/${this.port}`
      }
    }

    this.app = next(nextConfig)
    const handle = this.app.getRequestHandler()
    await this.app.prepare()

    this.server = http.createServer((req, res) => {
      const parsedUrl = parse(req.url || '', true)
      handle(req, res, parsedUrl)
    })

    this.server.once('error', (err: any) => {
      console.log('Failed to start Admin interface')
      console.log(err)
      process.exit(1)
    })

    this.server.listen(this.port, this.host)
    this.logs.log('Admin server listening on port ' + this.port)
  }
Example #18
Source File: listTableList.ts    From ui-visualization with MIT License 5 votes vote down vote up
function getRule(req: Request, res: Response, u: string) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { current = 1, pageSize = 10 } = req.query;
  const params = (parse(realUrl, true).query as unknown) as TableListParams;

  let dataSource = [...tableListDataSource].slice(
    ((current as number) - 1) * (pageSize as number),
    (current as number) * (pageSize as number),
  );
  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource: TableListItem[] = [];
    status.forEach((s: string) => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter((item) => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }
          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  }
  const result = {
    data: dataSource,
    total: tableListDataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };

  return res.json(result);
}
Example #19
Source File: path.ts    From nextjs-basic-auth-middleware with MIT License 5 votes vote down vote up
pathInRequest = (paths: string[], req: IncomingMessage) => {
  if (req.url === undefined) {
    return false
  }

  const path = parse(req.url).pathname
  return paths.some(item => path?.startsWith(item))
}
Example #20
Source File: listTableList.ts    From ant-design-pro-V5-multitab with MIT License 5 votes vote down vote up
function getRule(req: Request, res: Response, u: string) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { current = 1, pageSize = 10 } = req.query;
  const params = (parse(realUrl, true).query as unknown) as TableListParams;

  let dataSource = [...tableListDataSource].slice(
    ((current as number) - 1) * (pageSize as number),
    (current as number) * (pageSize as number),
  );
  const sorter = JSON.parse(params.sorter as any);
  if (sorter) {
    dataSource = dataSource.sort((prev, next) => {
      let sortNumber = 0;
      Object.keys(sorter).forEach((key) => {
        if (sorter[key] === 'descend') {
          if (prev[key] - next[key] > 0) {
            sortNumber += -1;
          } else {
            sortNumber += 1;
          }
          return;
        }
        if (prev[key] - next[key] > 0) {
          sortNumber += 1;
        } else {
          sortNumber += -1;
        }
      });
      return sortNumber;
    });
  }
  if (params.filter) {
    const filter = JSON.parse(params.filter as any) as {
      [key: string]: string[];
    };
    if (Object.keys(filter).length > 0) {
      dataSource = dataSource.filter((item) => {
        return Object.keys(filter).some((key) => {
          if (!filter[key]) {
            return true;
          }
          if (filter[key].includes(`${item[key]}`)) {
            return true;
          }
          return false;
        });
      });
    }
  }

  if (params.name) {
    dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  }
  const result = {
    data: dataSource,
    total: tableListDataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };

  return res.json(result);
}
Example #21
Source File: listTableList.ts    From anew-server with MIT License 5 votes vote down vote up
function getRule(req: Request, res: Response, u: string) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { current = 1, pageSize = 10 } = req.query;
  const params = parse(realUrl, true).query as unknown as API.PageParams &
    API.RuleListItem & {
      sorter: any;
      filter: any;
    };

  let dataSource = [...tableListDataSource].slice(
    ((current as number) - 1) * (pageSize as number),
    (current as number) * (pageSize as number),
  );
  const sorter = JSON.parse(params.sorter || ('{}' as any));
  if (sorter) {
    dataSource = dataSource.sort((prev, next) => {
      let sortNumber = 0;
      Object.keys(sorter).forEach((key) => {
        if (sorter[key] === 'descend') {
          if (prev[key] - next[key] > 0) {
            sortNumber += -1;
          } else {
            sortNumber += 1;
          }
          return;
        }
        if (prev[key] - next[key] > 0) {
          sortNumber += 1;
        } else {
          sortNumber += -1;
        }
      });
      return sortNumber;
    });
  }
  if (params.filter) {
    const filter = JSON.parse(params.filter as any) as {
      [key: string]: string[];
    };
    if (Object.keys(filter).length > 0) {
      dataSource = dataSource.filter((item) => {
        return Object.keys(filter).some((key) => {
          if (!filter[key]) {
            return true;
          }
          if (filter[key].includes(`${item[key]}`)) {
            return true;
          }
          return false;
        });
      });
    }
  }

  if (params.name) {
    dataSource = dataSource.filter((data) => data?.name?.includes(params.name || ''));
  }
  const result = {
    data: dataSource,
    total: tableListDataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.current}`, 10) || 1,
  };

  return res.json(result);
}
Example #22
Source File: index.ts    From docs-components with MIT License 5 votes vote down vote up
export function isActiveItem(router: Router, href: string, singlePage?: boolean) {
    if (singlePage) {
        return normalizeHash(router.hash) === normalizeHash(parse(href).hash);
    }

    return normalizePath(router.pathname) === normalizePath(parse(href).pathname);
}
Example #23
Source File: functions.ts    From hsolve with MIT License 5 votes vote down vote up
createAgents = (proxy: string): Agents => {
  return {
    http: new HttpProxyAgent(parse(proxy)) as unknown as http.Agent,
    https: new HttpsProxyAgent(parse(proxy)) as unknown as https.Agent,
  }
}
Example #24
Source File: server.ts    From Cromwell with MIT License 4 votes vote down vote up
startNextServer = async (options?: {
    dev?: boolean;
    port?: number;
    dir?: string;
    targetThemeName?: string;
}) => {
    const config = await readCMSConfig();
    const authSettings = await getAuthSettings();
    setStoreItem('cmsSettings', config);

    const themeName = options?.targetThemeName ?? config?.defaultSettings?.publicSettings?.themeName;
    if (!themeName) {
        logger.error('No theme name provided');
        return;
    }

    const port = options?.port ?? 3000;
    const app = next({
        dev: options?.dev ?? false,
        dir: options?.dir,
    });

    if (config.monolith) {
        try {
            const dbConnector: typeof import('@cromwell/core-backend/dist/helpers/connect-database')
                = require('@cromwell/core-backend/dist/helpers/connect-database');

            await dbConnector.connectDatabase({
                development: config.env === 'dev',
            });
        } catch (error) {
            logger.error(error);
        }
    }

    const handle = app.getRequestHandler();
    await app.prepare();

    const server = createServer(async (req, res) => {
        const parsedUrl = parse(req.url!, true);
        const { pathname, search } = parsedUrl;

        // Static file serving
        try {
            if (req.url) {
                const filePath = join(process.cwd(), 'public', req.url);
                if ((await fs.lstat(filePath)).isFile()) {
                    send(req, filePath).pipe(res);
                    return;
                }
            }
        } catch (err) { }

        // Pass settings to frontend via cookies
        res.setHeader('Set-Cookie', cookie.serialize('crw_cms_config', JSON.stringify({
            apiUrl: config.apiUrl,
            adminUrl: config.adminUrl,
            frontendUrl: config.frontendUrl,
            centralServerUrl: config.centralServerUrl,
        } as TCmsSettings), {
            maxAge: 60 * 60 * 24 * 7, // 1 week
            path: '/',
        }));

        await processCacheRequest({ app: app as any, req, res, authSettings, port, themeName });

        if (!pathname) {
            handle(req, res, parsedUrl);
            return;
        }

        // Handle redirects
        const redirect = findRedirect(pathname, search);

        if (redirect) {
            if (redirect.type === 'redirect' && redirect.to) {
                res.writeHead(redirect?.statusCode ?? redirect?.permanent ? 301 : 307, {
                    Location: redirect.to,
                });
                res.end();
                return;
            }

            if (redirect.type === 'rewrite' && redirect.from) {
                parsedUrl.pathname = redirect.from;
                req.url = redirect.from;
                app.render(req, res, redirect.from);
                return;
            }
        }

        // Default handle
        handle(req, res, parsedUrl);
    })

    const success = await new Promise(done => {
        server.on('error', (err) => {
            logger.error(err);
            done(false);
        });

        server.listen(port, () => {
            done(true);
            logger.log(`> Next.js server ready on http://localhost:${port}`)
        })

        setTimeout(() => done(false), 45000);
    })

    return success;
}