Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Version 5 refactor (#31)
Browse files Browse the repository at this point in the history
Changes include:

### Major, breaking changes

* _Callback signature has been removed._ Only Promise based interface now.
* _No more default export._ Only named export `getUri()` is available now.
* Minimum Node.js version is now 14.x.
* The `ftp://` protocol has been switched from `ftp` to [`basic-ftp`](https://www.npmjs.com/package/basic-ftp) module.
  * Closes #16
  * Closes #18

### Minor changes

* Proper options typings are now inferred based on the protocol of the URI provided to `getUri()`. For example: `getUri('ftp://...')` will have `FtpOptions` as the second parameter implicitly.
* A `URL` instance can now be provided as the first argument to `getUri()`.

### Misc

* Use pnpm for GH Actions.
* Use Jest for tests.
* Refactor tests to TypeScript.
  • Loading branch information
TooTallNate authored Apr 26, 2023
1 parent fc0f1aa commit 0b7b999
Show file tree
Hide file tree
Showing 31 changed files with 3,097 additions and 776 deletions.
32 changes: 10 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,20 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [8.x, 10.x, 12.x, 14.x]
node-version: [14.x, 16.x, 18.x, 20.x]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v1

- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 7.32.2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Print Node.js Version
run: node --version

- name: Install Dependencies
run: npm install
env:
CI: true

- name: Run "build" step
run: npm run build --if-present
env:
CI: true

- name: Run tests
run: npm test
env:
CI: true
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm build
- run: pnpm test
75 changes: 32 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ Example

To simply get a `stream.Readable` instance from a `file:` URI, try something like:

``` js
var getUri = require('get-uri');
```ts
import { getUri } from 'get-uri';

// `file:` maps to a `fs.ReadStream` instance…
getUri('file:///Users/nrajlich/wat.json', function (err, rs) {
if (err) throw err;
rs.pipe(process.stdout);
});
const stream = await getUri('file:///Users/nrajlich/wat.json');
stream.pipe(process.stdout);
```


Expand All @@ -50,19 +48,17 @@ destination server, then a `NotFoundError` will be returned. The `code` of the
error instance is set to `"ENOTFOUND"`, so you can special-case that in your code
to detect when a bad filename is requested:

``` js
getUri('http://example.com/resource.json', function (err, rs) {
if (err) {
if ('ENOTFOUND' == err.code) {
// bad file path requested
} else {
// something else bad happened...
throw err;
}
```ts
try {
await getUri('http://example.com/resource.json');
} catch (err) {
if (err.code === 'ENOTFOUND') {
// bad file path requested
} else {
// something else bad happened...
throw err;
}

// your app code…
});
}
```


Expand All @@ -83,47 +79,40 @@ When the `"ENOTMODIFIED"` error occurs, then you can safely re-use the
results from the previous `getUri()` call for that same URI:

``` js
// maps to a `fs.ReadStream` instance
getUri('http://example.com/resource.json', function (err, rs) {
if (err) throw err;
// First time fetches for real
const stream = await getUri('http://example.com/resource.json');

try {
// … some time later, if you need to get this same URI again, pass in the
// previous `stream.Readable` instance as `cache` option to potentially
// receive an "ENOTMODIFIED" response:
var opts = { cache: rs };
getUri('http://example.com/resource.json', opts, function (err, rs2) {
if (err) {
if ('ENOTFOUND' == err.code) {
// bad file path requested
} else if ('ENOTMODIFIED' == err.code) {
// source file has not been modified since last time it was requested,
// so `rs2` is undefined and you are expected to re-use results from
// a previous call to `getUri()`
} else {
// something else bad happened...
throw err;
}
}
});
});
// have an "ENOTMODIFIED" error thrown:
await getUri('http://example.com/resource.json', { cache: stream });
} catch (err) {
if (err.code === 'ENOTMODIFIED') {
// source file has not been modified since last time it was requested,
// so you are expected to re-use results from a previous call to `getUri()`
} else {
// something else bad happened...
throw err;
}
}
```


API
---

### getUri(String uri[, Object options,] Function callback)
### getUri(uri: string | URL, options?: Object]): Promise<Readable>

A `uri` String is required. An optional `options` object may be passed in:
A `uri` is required. An optional `options` object may be passed in:

- `cache` - A `stream.Readable` instance from a previous call to `getUri()` with the same URI. If this option is passed in, and the destination endpoint has not been modified, then an `ENOTMODIFIED` error is returned
- `cache` - A `stream.Readable` instance from a previous call to `getUri()` with the same URI. If this option is passed in, and the destination endpoint has not been modified, then an `ENOTMODIFIED` error is thrown

Any other options passed in to the `options` object will be passed through
to the low-level connection creation functions (`http.get()`, `ftp.connect()`,
etc).

Invokes the given `callback` function with a `stream.Readable` instance to
read the resource at the given `uri`.
Returns a `stream.Readable` instance to read the resource at the given `uri`.

License
-------
Expand Down
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"test": "mocha --reporter spec",
"test": "jest",
"prepublishOnly": "npm run build"
},
"repository": {
Expand All @@ -37,26 +37,27 @@
"url": "https://github.com/TooTallNate/node-get-uri/issues"
},
"devDependencies": {
"@types/debug": "4",
"@types/fs-extra": "^8.0.1",
"@types/ftp": "^0.3.30",
"@types/node": "^12.12.11",
"@types/debug": "^4.1.7",
"@types/fs-extra": "^8.1.2",
"@types/ftpd": "^0.2.35",
"@types/jest": "^29.5.1",
"@types/node": "^14.18.43",
"async-listen": "^2.1.0",
"ftpd": "https://files-jg1s1zt9l.n8.io/ftpd-v0.2.14.tgz",
"mocha": "^6.2.2",
"rimraf": "^3.0.0",
"jest": "^29.5.0",
"rimraf": "^3.0.2",
"st": "^1.2.2",
"stream-to-array": "2",
"typescript": "^4.4.3"
"ts-jest": "^29.1.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@tootallnate/once": "2",
"data-uri-to-buffer": "3",
"debug": "4",
"file-uri-to-path": "2",
"fs-extra": "^8.1.0",
"ftp": "^0.3.10"
"@tootallnate/once": "^2.0.0",
"basic-ftp": "^5.0.2",
"data-uri-to-buffer": "^3.0.1",
"debug": "^4.3.4",
"fs-extra": "^8.1.0"
},
"engines": {
"node": ">= 8"
"node": ">= 14"
}
}
Loading

0 comments on commit 0b7b999

Please sign in to comment.