Skip to content

Commit

Permalink
Migrate from CommonJS to ESM package
Browse files Browse the repository at this point in the history
- Update to Yarn Berry
- Combine Node package
- Temporary disable client side unit tests
- Remove deprecated shields in README
- Update `@tokenizer/range` to v0.8.0
  • Loading branch information
Borewit committed Jul 24, 2024
1 parent 31751c2 commit ac1a738
Show file tree
Hide file tree
Showing 19 changed files with 5,046 additions and 7,020 deletions.
5 changes: 1 addition & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ updates:
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
open-pull-requests-limit: 15
labels:
- dependencies
versioning-strategy: increase
ignore:
- dependency-name: "node-fetch"
versions: [ "3.x", "4.x" ] # Pure ES-Module
17 changes: 10 additions & 7 deletions .github/workflows/karma-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Karma CI

on:
push:
workflow_dispatch: # Disable work flow

jobs:

Expand All @@ -17,22 +18,24 @@ jobs:
steps:

- name: 'Checkout the repository'
uses: actions/checkout@v2
uses: actions/checkout@v4

- uses: actions/setup-node@v1
- uses: actions/setup-node@v4
with:
node-version: 16.x
node-version: 20.x

- run: corepack enable yarn

- name: Install dependencies
run: yarn install

- name: Run XVFB headless test
uses: GabrielBB/xvfb-action@v1
uses: coactions/setup-xvfb@v1
with:
run: yarn run karma-headless --browsers ${{ matrix.browsers }}

- name: Coveralls Parallel
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.github_token }}
flag-name: run-${{ matrix.test_number }}
Expand All @@ -43,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
30 changes: 17 additions & 13 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ jobs:

steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/setup-node@v1
- uses: actions/setup-node@v4
with:
node-version: 16.x
node-version: 20.x

- name: Install production dependencies, check node engine compatiblity
run: yarn install --production=true
- run: corepack enable yarn

- name: Install development dependencies
run: yarn install --production=false --ignore-engines
run: yarn install

- name: Build & Code analysis
run: yarn run lint
Expand All @@ -29,7 +28,7 @@ jobs:
run: yarn run build

- name: Upload build
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: build
path: |
Expand All @@ -45,28 +44,33 @@ jobs:

needs: build

env:
YARN_IGNORE_NODE: 1

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 16.x, 18.x]
node-version: [18.x, 20.x, 22.x]

steps:

- name: 'Checkout the repository'
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Test with Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- run: corepack enable yarn

- name: Install dependencies
run: yarn install --ignore-engines
run: yarn install

- name: Download build
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: build

- name: Test with Node.js ${{ matrix.node-version }}
run: yarn run test-node
run: yarn run test

5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[![Karma CI](https://github.com/Borewit/tokenizer-http/actions/workflows/karma-ci.yml/badge.svg)](https://github.com/Borewit/tokenizer-http/actions/workflows/karma-ci.yml)
[![Node.js CI](https://github.com/Borewit/tokenizer-http/actions/workflows/nodejs-ci.yml/badge.svg?branch=master)](https://github.com/Borewit/tokenizer-http/actions/workflows/nodejs-ci.yml)
[![npm version](https://badge.fury.io/js/%40tokenizer%2Fhttp.svg)](https://www.npmjs.com/package/@tokenizer/http)
[![npm downloads](http://img.shields.io/npm/dm/@tokenizer/http.svg)](https://npmcharts.com/compare/@tokenizer/http?interval=30)
[![Known Vulnerabilities](https://snyk.io/test/github/Borewit/tokenizer-http/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/tokenizer-http?targetFile=package.json)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/Borewit/tokenizer-http.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/tokenizer-http/alerts/)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Borewit/tokenizer-http.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/tokenizer-http/context:javascript)
[![Minified size](https://badgen.net/bundlephobia/min/@tokenizer/http)](https://bundlephobia.com/result?p=@tokenizer/http)

# @tokenizer/http

Expand Down
88 changes: 0 additions & 88 deletions karma.conf.js

This file was deleted.

10 changes: 5 additions & 5 deletions lib/chunked-file-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const NOT_FOUND = -1;

export interface IChunk {
offset: number;
data: ArrayBuffer;
data: Uint8Array;
}

interface IChunkRange {
Expand All @@ -24,7 +24,7 @@ export class ChunkedFileData {
/**
* Adds data to the file storage at a specific offset.
*/
public addData(offset: number, data: ArrayBuffer): void {
public addData(offset: number, data: Uint8Array): void {

const offsetEnd = offset + data.byteLength - 1;
const chunkRange = this._getChunkRange(offset, offsetEnd);
Expand Down Expand Up @@ -82,7 +82,7 @@ export class ChunkedFileData {
return false;
}

public readToBuffer(buffer: Buffer, offset: number, position: number, length: number) {
public readToBuffer(buffer: Uint8Array, offset: number, position: number, length: number): void {

const _pos_offset = position ;
let dataChunk: IChunk;
Expand All @@ -95,7 +95,7 @@ export class ChunkedFileData {
dataChunk = this._fileData[i];
const chunkOffset = _pos_offset - dataChunkStart;
const chunkLength = Math.min(length, dataChunk.data.byteLength - chunkOffset);
Buffer.from(dataChunk.data).copy(buffer, offset, chunkOffset, chunkOffset + chunkLength);
buffer.set(dataChunk.data.subarray(chunkOffset, chunkOffset + chunkLength), offset);
if (chunkLength < length) {
return this.readToBuffer(buffer, offset + chunkLength, position + chunkLength, length - chunkLength);
}
Expand All @@ -105,7 +105,7 @@ export class ChunkedFileData {
throw new Error(`Offset ${_pos_offset} hasn't been loaded yet.`);
}

private _concatData(buffer1: ArrayBuffer, buffer2: ArrayBuffer): ArrayBuffer {
private _concatData(buffer1: Uint8Array, buffer2: Uint8Array): Uint8Array {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
Expand Down
53 changes: 16 additions & 37 deletions lib/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as initDebug from 'debug';
import * as _fetch from 'node-fetch';
import { IContentRangeType, IHeadRequestInfo, IRangeRequestResponse, IRangeRequestClient, parseContentRange } from '@tokenizer/range'; // Add 'fetch' API for node.js
import initDebug from 'debug';

import { IHeadRequestInfo, IRangeRequestResponse, IRangeRequestClient } from '@tokenizer/range';
import { ResponseInfo } from './response-info.js'; // Add 'fetch' API for node.js

const debug = initDebug('streaming-http-token-reader:http-client');

Expand All @@ -16,43 +17,22 @@ const DEFAULT_CONFIG = {
};

/**
* Simple HTTP-client, which both works in node.js and browser
* Simple HTTP-client, which works both in node.js and browser
*/
export class HttpClient implements IRangeRequestClient {

private static getContentLength(headers: _fetch.Headers): number {
const contentLength = headers.get('Content-Length');
return contentLength ? parseInt(contentLength, 10) : undefined;
}

private static parseContentRange(headers: _fetch.Headers): IContentRangeType {
const contentRange = headers.get('Content-Range');
return parseContentRange(contentRange);
}

private static makeResponse(resp): IRangeRequestResponse {
const contentRange = HttpClient.parseContentRange(resp.headers);
return {
url: resp.url,
size: contentRange ? contentRange.instanceLength : HttpClient.getContentLength(resp.headers),
mimeType: resp.headers.get('Content-Type'),
contentRange,
arrayBuffer: () => resp.arrayBuffer()
};
}

public resolvedUrl: string;
private config: HttpClientConfig;
public resolvedUrl?: string;
private readonly config: HttpClientConfig;

constructor(private url: string, config?: HttpClientConfig) {
this.config = DEFAULT_CONFIG;
Object.assign(this.config, config);
}

public async getHeadInfo(): Promise<IHeadRequestInfo> {
const response = await _fetch(this.url, {method: 'HEAD'});
if (this.config.resolveUrl) this.resolvedUrl = response.url;
return HttpClient.makeResponse(response);
const response = new ResponseInfo(await fetch(this.url, {method: 'HEAD'}));
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
}

public async getResponse(method: string, range?: [number, number]): Promise<IRangeRequestResponse> {
Expand All @@ -62,15 +42,14 @@ export class HttpClient implements IRangeRequestClient {
debug(`_getResponse ${method} (range not provided)`);
}

const headers = new _fetch.Headers();
headers.set('Range', 'bytes=' + range[0] + '-' + range[1]);
const headers = new Headers();

const response = await _fetch(this.resolvedUrl || this.url, {method, headers});
if (response.ok) {
if (this.config.resolveUrl) this.resolvedUrl = response.url;
return HttpClient.makeResponse(response);
const response = new ResponseInfo(await fetch(this.resolvedUrl || this.url, {method, headers}));
if (response.response.ok) {
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
} else {
throw new Error(`Unexpected HTTP response status=${response.status}`);
throw new Error(`Unexpected HTTP response status=${response.response.status}`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { HttpClient, HttpClientConfig } from './http-client';
import { HttpClient, HttpClientConfig } from './http-client.js';
import * as rangeTokenizer from '@tokenizer/range';
import { ITokenizer } from 'strtok3/lib/core';
import { type ITokenizer } from 'strtok3';

export { HttpClient, HttpClientConfig } from './http-client.js';

/**
* Create and initialize the HTTP tokenizer
Expand Down
Loading

0 comments on commit ac1a738

Please sign in to comment.