-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add support for Edge Runtime (#29)
* Add support for Edge Runtime * fix non-node detection * changes made based on review * remove lock file * remove node 14, add node 20 to CI matrix * rename test:esm to test:webcrypto * fix variable mismatch * add auto publish for push to main, switch to node:test, add esm test * Changes based on review * BREAKING CHANGE: Add ESM output to support Edge / Browser runtimes * feat: Adding ESM to support Edge / Browser runtimes BREAKING CHANGE: Internals have been changed, new output has been created.
- Loading branch information
Showing
13 changed files
with
183 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
name: CI | ||
|
||
on: | ||
- push | ||
- pull_request | ||
push: | ||
branches: ['main'] | ||
pull_request: | ||
branches: ['main'] | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} | ||
|
||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
name: Node.js ${{ matrix.node }} on ${{ matrix.os }} | ||
timeout-minutes: 5 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 16 | ||
- 14 | ||
os: [ubuntu-latest] | ||
node: [18, 20] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Use pnpm | ||
run: corepack enable pnpm && pnpm --version | ||
- name: Use Node.js ${{ matrix.node }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: yarn install | ||
- run: yarn test | ||
node-version: ${{ matrix.node }} | ||
cache: 'pnpm' | ||
- run: pnpm install | ||
- run: pnpm test | ||
- name: Maybe Release | ||
if: matrix.os == 'ubuntu-latest' && matrix.node == 18 && github.event_name == 'push' && github.ref == 'refs/heads/main' | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN_ELEVATED }} | ||
run: pnpm dlx semantic-release@19.0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: PR | ||
on: | ||
pull_request: | ||
types: [opened, edited, synchronize] | ||
pull_request_target: | ||
types: [opened, edited, synchronize] | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: amannn/action-semantic-pull-request@0b14f54ac155d88e12522156e52cb6e397745cfd | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v18.16.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
// Charset from github.com/coreh/uid2 | ||
const UIDCHARS = | ||
export const UIDCHARS = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
||
export default UIDCHARS; | ||
module.exports = UIDCHARS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { RandomBytes } from './types'; | ||
import { randomBytes as nodeRandomBytes } from 'crypto'; | ||
|
||
export const randomBytes: RandomBytes = nodeRandomBytes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type RandomBytes = ( | ||
size: number, | ||
callback?: (err: Error | null, buf: Uint8Array) => void, | ||
) => Uint8Array; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { RandomBytes } from './types'; | ||
|
||
export const randomBytes: RandomBytes = (size, callback) => { | ||
if (size < 0 || size > 65536) { | ||
throw new RangeError( | ||
'The value of "size" is out of range. It must be >= 0 && <= 65536. Received ' + | ||
size, | ||
); | ||
} | ||
|
||
var array = new Uint8Array(size); | ||
globalThis.crypto.getRandomValues(array); | ||
|
||
if (!callback) { | ||
return array; | ||
} | ||
|
||
// Mimic async behavior with setTimeout | ||
setTimeout(() => callback(null, array), 0); | ||
|
||
return new Uint8Array(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Packages | ||
import test from 'node:test'; | ||
import assert from 'node:assert'; | ||
|
||
// Utilities | ||
import {UIDCHARS} from '../lib/esm/chars.js'; | ||
import {uid} from '../lib/esm/index.js'; | ||
|
||
test('should be correct length', async () => { | ||
const len = 1 + Math.floor(Math.random() * 1000); | ||
const u = await uid(len); | ||
assert.strictEqual(u.length, len); | ||
}); | ||
|
||
test('should contain only UIDCHARS', async () => { | ||
const u = await uid(20); | ||
u.split('').forEach((l) => assert.notStrictEqual(UIDCHARS.indexOf(l), -1)); | ||
}); | ||
|
||
test('should throw if no integer supplied', async () => { | ||
uid() | ||
.then(console.log) | ||
.catch((err) => { | ||
assert.strictEqual(err instanceof Error, true); | ||
}); | ||
}); | ||
|
||
test('should throw if integer is not positive value', async () => { | ||
uid(0) | ||
.then(console.log) | ||
.catch((err) => { | ||
assert.strictEqual(err instanceof Error, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
// Packages | ||
const test = require('ava'); | ||
const test = require('node:test'); | ||
const assert = require('node:assert') | ||
|
||
// Utilities | ||
const UIDCHARS = require('../lib/chars'); | ||
const uid = require('../lib'); | ||
const {UIDCHARS} = require('../lib/cjs/chars'); | ||
const { uid } = require('../lib/cjs'); | ||
|
||
test('should be correct length', async (t) => { | ||
test('should be correct length', async () => { | ||
const len = 1 + Math.floor(Math.random() * 1000); | ||
const u = await uid(len); | ||
t.is(u.length, len); | ||
assert.strictEqual(u.length, len); | ||
}); | ||
|
||
test('should contain only UIDCHARS', async (t) => { | ||
test('should contain only UIDCHARS', async () => { | ||
const u = await uid(20); | ||
u.split('').forEach((l) => t.not(UIDCHARS.indexOf(l), -1)); | ||
u.split('').forEach((l) => assert.notStrictEqual(UIDCHARS.indexOf(l), -1)); | ||
}); | ||
|
||
test('should throw if no integer supplied', async (t) => { | ||
test('should throw if no integer supplied', async () => { | ||
uid() | ||
.then(console.log) | ||
.catch((err) => { | ||
t.is(err instanceof Error, true); | ||
assert.strictEqual(err instanceof Error, true); | ||
}); | ||
}); | ||
|
||
test('should throw if integer is not positive value', async (t) => { | ||
test('should throw if integer is not positive value', async () => { | ||
uid(0) | ||
.then(console.log) | ||
.catch((err) => { | ||
t.is(err instanceof Error, true); | ||
assert.strictEqual(err instanceof Error, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters