Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __test__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = __importDefault(require("ava"));
const node_process_1 = __importDefault(require("node:process"));
const indax_js_1 = require("../indax.js");
const indax_1 = require("../indax");
const smbURL = node_process_1.default.env.SMB_URL || 'smb://127.0.0.1/Users/Shared/smb/';
//const smbPath = process.env.SMB_PATH;
let cachedRoot;
let testPermissions = false;
let testResolve = false;
async function getRootHandle() {
if (!cachedRoot) {
cachedRoot = new indax_js_1.SmbDirectoryHandle(smbURL);
cachedRoot = new indax_1.SmbDirectoryHandle(smbURL);
}
//let cachedRoot = new SmbDirectoryHandle(smbURL);
let subRoot = cachedRoot;
Expand Down
38 changes: 19 additions & 19 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import test from 'ava'

import process from 'node:process';
import { SmbDirectoryHandle, SmbFileHandle } from '../indax.js'
import { SmbDirectoryHandle, SmbFileHandle } from '../indax';

const smbURL = process.env.SMB_URL || 'smb://127.0.0.1/Users/Shared/smb/';
//const smbPath = process.env.SMB_PATH;
Expand All @@ -30,7 +30,7 @@ let testResolve = false;

async function getRootHandle(): Promise<FileSystemDirectoryHandle> {
if (!cachedRoot) {
cachedRoot = new SmbDirectoryHandle(smbURL);
cachedRoot = new SmbDirectoryHandle(smbURL) as any as FileSystemDirectoryHandle;
}
//let cachedRoot = new SmbDirectoryHandle(smbURL);
let subRoot: FileSystemDirectoryHandle = cachedRoot;
Expand All @@ -42,7 +42,7 @@ async function getRootHandle(): Promise<FileSystemDirectoryHandle> {

test.serial('should have correct properties for directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
t.is(dirHandle.kind, 'directory');
t.is(dirHandle.name, 'first');
})
Expand Down Expand Up @@ -94,29 +94,29 @@ test.serial('should not be same entry as others for file', async (t) => {

test.serial('should be granted read permission when querying on directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const perm = await dirHandle.queryPermission({mode: 'read'});
t.is(perm, 'granted');
})

test.serial('should be granted readwrite permission when querying on directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const perm = await dirHandle.queryPermission({mode: 'readwrite'});
t.is(perm, 'granted');
})

test.serial('should be granted read permission when querying on read-only directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as any as SmbDirectoryHandle;
const perm = await dirHandle.queryPermission({mode: 'read'});
t.is(perm, 'granted');
})

if (testPermissions) {
test.serial('should be denied readwrite permission when querying on read-only directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as any as SmbDirectoryHandle;
const perm = await dirHandle.queryPermission({mode: 'readwrite'});
t.is(perm, 'denied');
})
Expand All @@ -131,8 +131,8 @@ if (testPermissions) {
const count = process.env.TEST_USING_MOCKS ? 1000 : 10;
const rootHandle = await getRootHandle();
const [first, quatre] = await Promise.all([
rootHandle.getDirectoryHandle('first') as Promise<SmbDirectoryHandle>,
rootHandle.getDirectoryHandle('quatre') as Promise<SmbDirectoryHandle>,
rootHandle.getDirectoryHandle('first') as any as Promise<SmbDirectoryHandle>,
rootHandle.getDirectoryHandle('quatre') as any as Promise<SmbDirectoryHandle>,
]);
for (let i = 0; i < count; i++) {
const [firstPerm, quatrePerm] = await Promise.all([
Expand All @@ -147,14 +147,14 @@ if (testPermissions) {

test.serial('should be granted read permission when requesting on directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const perm = await dirHandle.requestPermission({mode: 'read'});
t.is(perm, 'granted');
})

test.serial('should be granted readwrite permission when requesting on directory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const perm = await dirHandle.requestPermission({mode: 'readwrite'});
t.is(perm, 'granted');
})
Expand Down Expand Up @@ -222,7 +222,7 @@ test.serial('should iterate through directory', async (t) => {

test.serial('should iterate through subdirectory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const expectedEntries = [
{key: 'comment', value: {kind: 'file', name: 'comment'}},
];
Expand All @@ -242,8 +242,8 @@ test.serial('should iterate through subdirectory', async (t) => {

test.serial('should iterate through subsubdirectory', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const subdirHandle = await dirHandle.getDirectoryHandle('place', {create: true}) as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const subdirHandle = await dirHandle.getDirectoryHandle('place', {create: true}) as any as SmbDirectoryHandle;
const expectedEntries = [];
let i = 0;
for await (const [ _key, _value ] of subdirHandle) {
Expand Down Expand Up @@ -285,7 +285,7 @@ test.serial('should iterate through entries', async (t) => {

test.serial('should iterate through subdirectory entries', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as any as SmbDirectoryHandle;
const expectedEntries = [
{key: 'points', value: {kind: 'file', name: 'points'}},
];
Expand Down Expand Up @@ -320,7 +320,7 @@ test.serial('should iterate through keys', async (t) => {

test.serial('should iterate through subdirectory keys', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('quatre') as any as SmbDirectoryHandle;
const expectedKeys = ['points'];
let i = 0;
for await (const key of dirHandle.keys()) {
Expand Down Expand Up @@ -360,7 +360,7 @@ test.serial('should iterate through values', async (t) => {

test.serial('should iterate through subdirectory values', async (t) => {
const rootHandle = await getRootHandle();
const dirHandle = await rootHandle.getDirectoryHandle('first') as SmbDirectoryHandle;
const dirHandle = await rootHandle.getDirectoryHandle('first') as any as SmbDirectoryHandle;
const expectedValues = [
{kind: 'file', name: 'comment'},
];
Expand Down Expand Up @@ -1296,7 +1296,7 @@ if (!process.env.TEST_USING_MOCKS) {
test.serial.skip('should handle watch', async (t) => {
const sleep = async (ms: number) => { return new Promise((resolve) => setTimeout(resolve, ms)); };
const rootHandle = await getRootHandle();
const smbHandle = rootHandle as SmbDirectoryHandle;
const smbHandle = rootHandle as any as SmbDirectoryHandle;
const caught: {path: string, action: string}[] = [];
const watcher = smbHandle.watch(async (watchEvent) => {
caught.push(watchEvent);
Expand Down Expand Up @@ -1356,7 +1356,7 @@ if (!process.env.TEST_USING_MOCKS) {
const rootHandle = await getRootHandle();
const subHandle = await rootHandle.getDirectoryHandle("subbed", {create: true});
const subSubHandle = await subHandle.getDirectoryHandle("sub", {create: true});
const smbHandle = subHandle as SmbDirectoryHandle;
const smbHandle = subHandle as any as SmbDirectoryHandle;
const caught: {path: string, action: string}[] = [];
const watcher = smbHandle.watch(async (watchEvent) => {
caught.push(watchEvent);
Expand Down
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,25 @@ else
for x in `cat index.js | grep -o "smb-js\..*\.node" | sort | uniq`; do
cat index.js | sed "s/join(__dirname, '$x')/new URL('$x', import.meta.url)/g" > index.js~
mv index.js{~,}

cat index.js | sed "s/require('.\/$x')/require(new URL('$x', import.meta.url).pathname)/g" > index.js~
mv index.js{~,}
done

# also remove check for universal binary for darwin, as we know we won't have one
cat index.js | grep -v "'smb-js.darwin-universal.node'" > index.js~
mv index.js{~,}

# finally, change way in which things are exported from index.js
cat index.js | grep -v 'module.exports.' | sed "s/^const {$/export const {/" > index.js~
mv index.js{~,}

# change indax.js to require index.cjs instead of just index
cat indax.js | sed "s/index\"/index.cjs\"/" > indax.js~
mv indax.js{~,}

mv index.js index.cjs
mv indax.js indax.cjs
fi

if [ "${NODE_OS}" == "darwin" ]; then
Expand Down
35 changes: 28 additions & 7 deletions indax.js → indax.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmbWritableFileStream = exports.SmbFileHandle = exports.SmbDirectoryHandle = exports.SmbHandle = void 0;
const index_js_1 = require("./index.js");
const index_1 = require("./index.cjs");
class SmbHandle {
_jsh;
kind;
name;
constructor(_jsh) {
this._jsh = _jsh;
this.kind = _jsh.kind;
Expand All @@ -45,30 +47,35 @@ class SmbHandle {
}
exports.SmbHandle = SmbHandle;
class SmbDirectoryHandle extends SmbHandle {
// @ts-ignore
[Symbol.asyncIterator] = this.entries;
_js;
constructor(param) {
const [url, toWrap] = typeof param === 'string' ? [param] : ['', param];
const _js = toWrap || new index_js_1.JsSmbDirectoryHandle(url);
const _js = toWrap || new index_1.JsSmbDirectoryHandle(url);
super(_js.toHandle());
this[_a] = this.entries;
this[Symbol.asyncIterator] = this.entries;
this._js = _js;
this.getFile = this.getFileHandle;
this.getDirectory = this.getDirectoryHandle;
this.getEntries = this.values;
}
// @ts-ignore
async *entries() {
for await (const [key, value] of this._js.entries()) {
yield [key, value instanceof index_js_1.JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) : new SmbFileHandle(value)];
yield [key, value instanceof index_1.JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) : new SmbFileHandle(value)];
}
}
// @ts-ignore
async *keys() {
for await (const key of this._js.keys()) {
yield key;
}
}
// @ts-ignore
async *values() {
for await (const value of this._js.values()) {
yield value instanceof index_js_1.JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) : new SmbFileHandle(value);
yield value instanceof index_1.JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) : new SmbFileHandle(value);
}
}
async getDirectoryHandle(name, options) {
Expand Down Expand Up @@ -114,13 +121,25 @@ class SmbDirectoryHandle extends SmbHandle {
async resolve(possibleDescendant) {
return this._js.resolve(possibleDescendant._jsh || possibleDescendant);
}
/**
* @deprecated Old property just for Chromium <=85. Use `.getFileHandle()` in the new API.
*/
getFile;
/**
* @deprecated Old property just for Chromium <=85. Use `.getDirectoryHandle()` in the new API.
*/
getDirectory;
/**
* @deprecated Old property just for Chromium <=85. Use `.keys()`, `.values()`, `.entries()`, or the directory itself as an async iterable in the new API.
*/
getEntries;
watch(callback) {
return this._js.watch(callback);
}
}
exports.SmbDirectoryHandle = SmbDirectoryHandle;
_a = Symbol.asyncIterator;
class SmbFileHandle extends SmbHandle {
_js;
constructor(_js) {
super(_js.toHandle());
this._js = _js;
Expand All @@ -142,6 +161,8 @@ class SmbFileHandle extends SmbHandle {
}
exports.SmbFileHandle = SmbFileHandle;
class SmbWritableFileStream {
_js;
locked;
constructor(_js) {
this._js = _js;
this.locked = _js.locked;
Expand Down
12 changes: 8 additions & 4 deletions indax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
JsSmbDirectoryHandle,
JsSmbFileHandle,
JsSmbWritableFileStream,
} from './index.js';
} from './index';

type SmbHandlePermissionDescriptor = JsSmbHandlePermissionDescriptor;
// @ts-ignore
Expand Down Expand Up @@ -63,6 +63,7 @@ export class SmbHandle implements FileSystemHandle {
}

export class SmbDirectoryHandle extends SmbHandle implements FileSystemDirectoryHandle {
// @ts-ignore
[Symbol.asyncIterator]: SmbDirectoryHandle['entries'] = this.entries
declare readonly kind: 'directory'
private _js: JsSmbDirectoryHandle
Expand All @@ -78,26 +79,29 @@ export class SmbDirectoryHandle extends SmbHandle implements FileSystemDirectory
this.getDirectory = this.getDirectoryHandle;
this.getEntries = this.values;
}
// @ts-ignore
async *entries(): AsyncIterableIterator<[string, FileSystemDirectoryHandle | FileSystemFileHandle]> {
for await (const [key, value] of this._js.entries()) {
yield [key, value instanceof JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) as FileSystemDirectoryHandle : new SmbFileHandle(value) as FileSystemFileHandle];
yield [key, value instanceof JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) as any as FileSystemDirectoryHandle : new SmbFileHandle(value) as FileSystemFileHandle];
}
}
// @ts-ignore
async *keys(): AsyncIterableIterator<string> {
for await (const key of this._js.keys()) {
yield key;
}
}
// @ts-ignore
async *values(): AsyncIterableIterator<FileSystemDirectoryHandle | FileSystemFileHandle> {
for await (const value of this._js.values()) {
yield value instanceof JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) as FileSystemDirectoryHandle : new SmbFileHandle(value) as FileSystemFileHandle;
yield value instanceof JsSmbDirectoryHandle ? new SmbDirectoryHandle(value) as any as FileSystemDirectoryHandle : new SmbFileHandle(value) as FileSystemFileHandle;
}
}
async getDirectoryHandle(name: string, options?: FileSystemGetDirectoryOptions): Promise<FileSystemDirectoryHandle> {
//console.log("getDirectoryHandle: ", name);
return new Promise(async (resolve, reject) => {
await this._js.getDirectoryHandle(name, options as JsSmbGetDirectoryOptions)
.then((handle) => resolve(new SmbDirectoryHandle(handle) as FileSystemDirectoryHandle))
.then((handle) => resolve(new SmbDirectoryHandle(handle) as any as FileSystemDirectoryHandle))
.catch((reason) => {
let errMsg: string = reason.message;
if (errMsg !== undefined) {
Expand Down
Loading
Loading