Skip to content

Commit

Permalink
Upgrade Expo SDK to 51 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishikawa authored Jun 11, 2024
1 parent 72f46ef commit db8af36
Show file tree
Hide file tree
Showing 12 changed files with 1,628 additions and 1,706 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x]
node-version: [18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.3.0

- Expo SDK 51
- Add `Crypto.getRandomValues()`
- Add `Crypto.randomUUID()`

## 1.1.2

- Upgrade dependencies.
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ Node.js environment.
## Motivation

The [jest-expo](https://www.npmjs.com/package/jest-expo) is doing a great job for mocking Expo SDK while
running on Node.js environment. But I wanted to make primitive operations like random, crypto work normally
rather than just returning `undefined`.
running on Node.js environment. But I wanted to make primitive operations like random, crypto work normally rather than just returning `undefined`.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "node-expo-sdk",
"version": "1.1.2",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "test"
"test": "yarn workspaces run test"
},
"private": true,
"workspaces": [
Expand Down
36 changes: 36 additions & 0 deletions packages/node-expo-crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import * as crypto from 'crypto';

// node_modules/expo-crypto/src/Crypto.types.ts
export enum CryptoDigestAlgorithm {
MD2 = 'MD2',
MD4 = 'MD4',
MD5 = 'MD5',
SHA1 = 'SHA-1',
SHA256 = 'SHA-256',
SHA384 = 'SHA-384',
SHA512 = 'SHA-512'
}

export enum CryptoEncoding {
HEX = 'hex',
BASE64 = 'base64'
}

export type CryptoDigestOptions = {
encoding: CryptoEncoding;
};

// @docsMissing
export type Digest = string;

export function digestStringAsync(
algorithm: string,
data: string,
Expand All @@ -19,3 +42,16 @@ export function getRandomBytes(byteCount: number): Uint8Array {
export function getRandomBytesAsync(byteCount: number): Promise<Uint8Array> {
return Promise.resolve(getRandomBytes(byteCount));
}

type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array;

export function getRandomValues(typedArray: TypedArray): TypedArray {
const buffer = crypto.randomBytes(typedArray.byteLength);
const view = new Uint8Array(buffer.buffer);
typedArray.set(view);
return typedArray;
}

export function randomUUID(): string {
return crypto.randomUUID();
}
15 changes: 1 addition & 14 deletions packages/node-expo-crypto/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,4 @@
* See the Jest documentation at https://jestjs.io/docs/en/configuration#setupfilesafterenv-array for
* more details.
*/
jest.mock('expo-crypto/build/ExpoCrypto', () => ({
digestStringAsync: jest.fn((algorithm, data, options) => {
const crypto = require('./dist/index');
return crypto.digestStringAsync(algorithm, data, options);
}),
getRandomBytes: jest.fn(byteCount => {
const random = require('./dist/index');
return random.getRandomBytes(byteCount);
}),
getRandomBytesAsync: jest.fn(byteCount => {
const random = require('./dist/index');
return random.getRandomBytesAsync(byteCount);
})
}));
jest.mock('expo-crypto', () => require('./dist/index'));
6 changes: 3 additions & 3 deletions packages/node-expo-crypto/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-expo-crypto",
"version": "1.2.0",
"version": "1.3.0",
"description": "The node implementation for expo-crypto",
"author": "Takanori Ishikawa <ishikawa_takanori@mac.com> (https://github.com/ishikawa/)",
"license": "MIT",
Expand Down Expand Up @@ -40,8 +40,8 @@
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-jest": "^24.1.3",
"expo": "^48.0.0",
"expo-crypto": "~12.2.1",
"expo": "^51.0.0",
"expo-crypto": "~13.0.0",
"jest": "^26.6.3",
"jest-expo": "^45.0.0",
"react": "17.0.2",
Expand Down
39 changes: 37 additions & 2 deletions packages/node-expo-crypto/test/expo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { digestStringAsync, getRandomBytes } from 'node-expo-crypto';
import { CryptoDigestAlgorithm, CryptoEncoding } from 'expo-crypto';
import {
digestStringAsync,
getRandomBytes,
getRandomValues,
randomUUID,
CryptoDigestAlgorithm,
CryptoEncoding
} from 'node-expo-crypto';

const INPUT = 'hi';

Expand Down Expand Up @@ -59,3 +65,32 @@ describe('getRandomBytes', () => {
expect(getRandomBytes(8)).not.toEqual(getRandomBytes(8));
});
});

describe('getRandomValues', () => {
test('get random values', async () => {
const typedArray = new Uint8Array(8);
const values = getRandomValues(typedArray);
expect(values).toHaveLength(8);
});

test('returns unique values', async () => {
const typedArray1 = new Uint8Array(8);
const typedArray2 = new Uint8Array(8);

getRandomValues(typedArray1);
getRandomValues(typedArray2);

expect(typedArray1).not.toEqual(getRandomValues(typedArray2));
});
});

describe('randomUUID', () => {
test('get random UUID', async () => {
const uuid = randomUUID();
expect(uuid).toHaveLength(36);
});

test('returns unique values', async () => {
expect(randomUUID()).not.toEqual(randomUUID());
});
});
38 changes: 37 additions & 1 deletion packages/node-expo-crypto/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { digestStringAsync, CryptoDigestAlgorithm, CryptoEncoding, getRandomBytes } from 'expo-crypto';
import {
digestStringAsync,
CryptoDigestAlgorithm,
CryptoEncoding,
getRandomBytes,
getRandomValues,
randomUUID
} from 'expo-crypto';

const INPUT = 'hi';

Expand Down Expand Up @@ -58,3 +65,32 @@ describe('getRandomBytes', () => {
expect(getRandomBytes(8)).not.toEqual(getRandomBytes(8));
});
});

describe('getRandomValues', () => {
test('get random values', async () => {
const typedArray = new Uint8Array(8);
const values = getRandomValues(typedArray);
expect(values).toHaveLength(8);
});

test('returns unique values', async () => {
const typedArray1 = new Uint8Array(8);
const typedArray2 = new Uint8Array(8);

getRandomValues(typedArray1);
getRandomValues(typedArray2);

expect(typedArray1).not.toEqual(getRandomValues(typedArray2));
});
});

describe('randomUUID', () => {
test('get random UUID', async () => {
const uuid = randomUUID();
expect(uuid).toHaveLength(36);
});

test('returns unique values', async () => {
expect(randomUUID()).not.toEqual(randomUUID());
});
});
11 changes: 1 addition & 10 deletions packages/node-expo-random/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,4 @@
* See the Jest documentation at https://jestjs.io/docs/en/configuration#setupfilesafterenv-array for
* more details.
*/
jest.mock('expo-random/build/ExpoRandom', () => ({
getRandomBytes: jest.fn(byteCount => {
const random = require('./dist/index');
return random.getRandomBytes(byteCount);
}),
getRandomBytesAsync: jest.fn(byteCount => {
const random = require('./dist/index');
return random.getRandomBytesAsync(byteCount);
})
}));
jest.mock('expo-random', () => require('./dist/index'));
6 changes: 3 additions & 3 deletions packages/node-expo-random/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-expo-random",
"version": "1.1.2",
"version": "1.3.0",
"description": "The node implementation for expo-random",
"author": "Takanori Ishikawa <ishikawa_takanori@mac.com> (https://github.com/ishikawa/)",
"license": "MIT",
Expand Down Expand Up @@ -40,8 +40,8 @@
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-jest": "^24.1.3",
"expo": "^45.0.0",
"expo-random": "~12.2.0",
"expo": "^51.0.0",
"expo-random": "~14.0.1",
"jest": "^26.6.3",
"jest-expo": "^45.0.0",
"react": "17.0.2",
Expand Down
Loading

0 comments on commit db8af36

Please sign in to comment.