Skip to content

Commit

Permalink
Refactor id generation (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot authored Sep 19, 2023
1 parent 5a0254b commit c1b3e13
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
27 changes: 15 additions & 12 deletions packages/core/src/nanoid.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// @ts-nocheck
// https://github.com/ai/nanoid
// Currently does not support commonjs imports, so we copypasta this file here
import { webcrypto } from 'crypto';
export type CryptoModule = {
getRandomValues<T extends ArrayBufferView | null>(array: T): T;
};

export const nanoid = (t = 21) =>
webcrypto
.getRandomValues(new Uint8Array(t))
.reduce(
(t: any, e: any) =>
(t += (e &= 63) < 36 ? e.toString(36) : e < 62 ? (e - 26).toString(36).toUpperCase() : e < 63 ? '_' : '-'),
'',
);
// cross compatible nanoid implementation from
// https://github.com/ai/nanoid
export const nanoid =
(crypto: CryptoModule) =>
(t = 21) =>
crypto
.getRandomValues(new Uint8Array(t))
.reduce(
(t: any, e: any) =>
(t += (e &= 63) < 36 ? e.toString(36) : e < 62 ? (e - 26).toString(36).toUpperCase() : e < 63 ? '_' : '-'),
'',
);
5 changes: 3 additions & 2 deletions packages/node/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import http from 'http';
import https from 'https';
import { types as utilTypes } from 'util';

import { Event, HttpRequest, nanoid } from '@envy/core';
import { Event, HttpRequest } from '@envy/core';
import { wrap } from 'shimmer';

// eslint thinks zlib is node20:builtin, but this is a node module
// eslint-disable-next-line import/order
import { createBrotliDecompress, unzip } from 'zlib';

import { generateId } from './id';
import log from './log';
import { Plugin } from './plugin';

Expand All @@ -29,7 +30,7 @@ export const Http: Plugin = (_options, exporter) => {
function override(module: any) {
_wrap(module, 'request', (original: any) => {
return function (this: any, ...args: http.ClientRequestArgs[]) {
const id = nanoid();
const id = generateId();
const startTs = Date.now();

const request = original.apply(this, args) as http.ClientRequest;
Expand Down
5 changes: 5 additions & 0 deletions packages/node/src/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { webcrypto } from 'node:crypto';

import { nanoid } from '@envy/core';

export const generateId = nanoid(webcrypto as any);
3 changes: 3 additions & 0 deletions packages/web/src/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { nanoid } from '@envy/core';

export const generateId = nanoid(crypto);
8 changes: 0 additions & 8 deletions packages/web/src/nanoid.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/web/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DEFAULT_WEB_SOCKET_PORT, Event } from '@envy/core';

import { fetchRequestToEvent, fetchResponseToEvent } from './http';
import { generateId } from './id';
import log from './log';
import { nanoid } from './nanoid';
import { Options } from './options';

export interface TracingOptions extends Options {
Expand All @@ -29,7 +29,7 @@ export async function enableTracing(options: TracingOptions): Promise<void> {
const { fetch: originalFetch } = window;
window.fetch = async (...args) => {
const tsReq = Date.now();
const id = nanoid();
const id = generateId();

const reqEvent = fetchRequestToEvent(tsReq, id, ...args);
if (ws.readyState === ws.OPEN) {
Expand Down

0 comments on commit c1b3e13

Please sign in to comment.